Skip to content

Commit 635fed9

Browse files
committed
chore: handle error and cover with tests
Signed-off-by: Vitor Mattos <[email protected]>
1 parent 0424c8b commit 635fed9

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/Handler/CertificateEngine/OpenSslHandler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ public function generateCertificate(): string {
8686
'private_key_type' => OPENSSL_KEYTYPE_RSA,
8787
]);
8888

89-
$csr = openssl_csr_new($this->getCsrNames(), $privateKey);
89+
$csr = @openssl_csr_new($this->getCsrNames(), $privateKey);
90+
if ($csr === false) {
91+
$message = openssl_error_string();
92+
throw new LibresignException('OpenSSL error: ' . $message);
93+
}
9094

9195
$x509 = openssl_csr_sign($csr, $rootCertificate, $rootPrivateKey, $this->expirity(), [
9296
'config' => $this->getFilenameToLeafCert(),

tests/php/Unit/Handler/CertificateEngine/OpenSslHandlerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use OCA\Libresign\Exception\EmptyCertificateException;
1010
use OCA\Libresign\Exception\InvalidPasswordException;
11+
use OCA\Libresign\Exception\LibresignException;
1112
use OCA\Libresign\Handler\CertificateEngine\OpenSslHandler;
1213
use OCA\Libresign\Service\CertificatePolicyService;
1314
use OCP\Files\AppData\IAppDataFactory;
@@ -72,6 +73,35 @@ public function testInvalidPassword(): void {
7273
$signerInstance->readCertificate($certificateContent, 'invalid password');
7374
}
7475

76+
public function testMaxLengthOfDistinguishedNamesWithSuccess(): void {
77+
// Create root cert
78+
$rootInstance = $this->getInstance();
79+
$rootInstance->generateRootCert('', []);
80+
81+
// Create signer cert
82+
$signerInstance = $this->getInstance();
83+
$longName = str_repeat('a', 64);
84+
$signerInstance->setCommonName($longName);
85+
$signerInstance->setPassword('123456');
86+
$certificateContent = $signerInstance->generateCertificate();
87+
$parsed = $signerInstance->readCertificate($certificateContent, '123456');
88+
$this->assertEquals($longName, $parsed['subject']['CN']);
89+
}
90+
91+
public function testBiggerThanMaxLengthOfDistinguishedNamesWithError(): void {
92+
// Create root cert
93+
$rootInstance = $this->getInstance();
94+
$rootInstance->generateRootCert('', []);
95+
96+
// Create signer cert
97+
$signerInstance = $this->getInstance();
98+
$longName = str_repeat('a', 65);
99+
$signerInstance->setCommonName($longName);
100+
$signerInstance->setPassword('123456');
101+
$this->expectException(LibresignException::class);
102+
$signerInstance->generateCertificate();
103+
}
104+
75105
/**
76106
* @dataProvider dataReadCertificate
77107
*/

0 commit comments

Comments
 (0)