Skip to content

Commit a807423

Browse files
authored
Merge pull request #6015 from LibreSign/fix/validate-empty-common-name
fix: validate empty commonName in certificate generation
2 parents c7efeb9 + 45827cd commit a807423

4 files changed

Lines changed: 20 additions & 1 deletion

File tree

lib/Handler/CertificateEngine/CfsslHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCA\Libresign\AppInfo\Application;
1616
use OCA\Libresign\Db\CrlMapper;
1717
use OCA\Libresign\Enum\CertificateType;
18+
use OCA\Libresign\Exception\EmptyCertificateException;
1819
use OCA\Libresign\Exception\LibresignException;
1920
use OCA\Libresign\Handler\CfsslServerHandler;
2021
use OCA\Libresign\Helper\ConfigureCheckHelper;
@@ -78,6 +79,10 @@ public function generateRootCert(
7879
string $commonName,
7980
array $names = [],
8081
): void {
82+
if (empty($commonName)) {
83+
throw new EmptyCertificateException('Common Name (CN) cannot be empty for root certificate');
84+
}
85+
8186
$this->cfsslServerHandler->createConfigServer(
8287
$commonName,
8388
$names,

lib/Handler/CertificateEngine/OpenSslHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use OCA\Libresign\Db\CrlMapper;
1212
use OCA\Libresign\Enum\CertificateType;
13+
use OCA\Libresign\Exception\EmptyCertificateException;
1314
use OCA\Libresign\Exception\LibresignException;
1415
use OCA\Libresign\Service\CaIdentifierService;
1516
use OCA\Libresign\Service\CertificatePolicyService;
@@ -61,6 +62,10 @@ public function generateRootCert(
6162
string $commonName,
6263
array $names = [],
6364
): void {
65+
if (empty($commonName)) {
66+
throw new EmptyCertificateException('Common Name (CN) cannot be empty for root certificate');
67+
}
68+
6469
$privateKey = openssl_pkey_new([
6570
'private_key_bits' => 2048,
6671
'private_key_type' => OPENSSL_KEYTYPE_RSA,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ public function testEmptyCertificate(): void {
7777
$signerInstance->readCertificate('', '');
7878
}
7979

80+
public function testEmptyCommonNameThrowsException(): void {
81+
$rootInstance = $this->getInstance();
82+
$this->expectException(EmptyCertificateException::class);
83+
$this->expectExceptionMessage('Common Name (CN) cannot be empty for root certificate');
84+
$rootInstance->generateRootCert('', []);
85+
}
86+
8087
public function testInvalidPassword(): void {
8188
// Create root cert
8289
$rootInstance = $this->getInstance();

tests/php/Unit/Handler/SignEngine/JSignPdfHandlerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static function setUpBeforeClass(): void {
5050
$certificateEngine = self::$certificateEngineFactory->getEngine();
5151
$certificateEngine
5252
->setConfigPath(\OCP\Server::get(ITempManager::class)->getTemporaryFolder('certificate'))
53-
->generateRootCert('', []);
53+
->generateRootCert('Test Root CA', []);
5454

5555
self::$certificateContent = $certificateEngine
5656
->setHosts(['[email protected]'])
@@ -92,6 +92,7 @@ private function getInstance(array $methods = []): JSignPdfHandler|MockObject {
9292
$this->signatureBackgroundService,
9393
$certificateEngineFactory,
9494
$this->javaHelper,
95+
$this->createMock(\OCA\Libresign\Service\DocMdpConfigService::class),
9596
);
9697
}
9798
return $this->getMockBuilder(JSignPdfHandler::class)
@@ -103,6 +104,7 @@ private function getInstance(array $methods = []): JSignPdfHandler|MockObject {
103104
$this->signatureBackgroundService,
104105
$certificateEngineFactory,
105106
$this->javaHelper,
107+
$this->createMock(\OCA\Libresign\Service\DocMdpConfigService::class),
106108
])
107109
->onlyMethods($methods)
108110
->getMock();

0 commit comments

Comments
 (0)