Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
53.85% |
14 / 26 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Pkcs7Handler | |
53.85% |
14 / 26 |
|
60.00% |
3 / 5 |
7.46 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCertificateEngineFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sign | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| getP7sFile | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getCertificateChain | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors |
| 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | */ |
| 8 | |
| 9 | namespace OCA\Libresign\Handler\SignEngine; |
| 10 | |
| 11 | use DateTime; |
| 12 | use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory; |
| 13 | use OCA\Libresign\Service\FolderService; |
| 14 | use OCP\Files\File; |
| 15 | use OCP\IL10N; |
| 16 | use Psr\Log\LoggerInterface; |
| 17 | |
| 18 | class Pkcs7Handler extends SignEngineHandler { |
| 19 | public function __construct( |
| 20 | IL10N $l10n, |
| 21 | FolderService $folderService, |
| 22 | LoggerInterface $logger, |
| 23 | private CertificateEngineFactory $certificateEngineFactory, |
| 24 | ) { |
| 25 | parent::__construct($l10n, $folderService, $logger); |
| 26 | } |
| 27 | |
| 28 | #[\Override] |
| 29 | protected function getCertificateEngineFactory(): CertificateEngineFactory { |
| 30 | return $this->certificateEngineFactory; |
| 31 | } |
| 32 | |
| 33 | #[\Override] |
| 34 | public function sign(): File { |
| 35 | $this->beforeSign(); |
| 36 | |
| 37 | $p7sFile = $this->getP7sFile(); |
| 38 | openssl_pkcs12_read($this->getCertificate(), $certificateData, $this->getPassword()); |
| 39 | openssl_pkcs7_sign( |
| 40 | $this->getInputFile()->getInternalPath(), |
| 41 | $p7sFile->getInternalPath(), |
| 42 | $certificateData['cert'], |
| 43 | $certificateData['pkey'], |
| 44 | [], |
| 45 | PKCS7_DETACHED |
| 46 | ); |
| 47 | return $p7sFile; |
| 48 | } |
| 49 | |
| 50 | protected function getP7sFile(): File { |
| 51 | $newName = $this->getInputFile()->getName() . '.p7s'; |
| 52 | $p7sFile = $this->getInputFile() |
| 53 | ->getParent() |
| 54 | ->newFile($newName); |
| 55 | return $p7sFile; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @todo Replace this method by a real implementation that retrieves the certificate chain and not just the file's last modified time. |
| 60 | */ |
| 61 | #[\Override] |
| 62 | public function getCertificateChain($resource): array { |
| 63 | $metadata = stream_get_meta_data($resource); |
| 64 | $lastModifiedTime = filemtime($metadata['uri']); |
| 65 | return [ |
| 66 | [ |
| 67 | 'signingTime' => (new DateTime())->setTimestamp($lastModifiedTime)->setTimezone(new \DateTimeZone('UTC')), |
| 68 | ], |
| 69 | ]; |
| 70 | } |
| 71 | |
| 72 | } |