Skip to content

Commit cec29ae

Browse files
fix: validate empty commonName in certificate generation
- Add validation to prevent empty Common Name (CN) in OpenSslHandler and CfsslHandler - Throw EmptyCertificateException with clear message when CN is empty - Fix JSignPdfHandlerTest to use valid commonName 'Test Root CA' - Add unit test to verify empty CN validation works correctly The owner field in libresign_crl table is mandatory without default value. Previously, generateRootCert('') would fail at database level with unclear error. Now it fails early with proper validation message. Signed-off-by: Vitor Mattos <[email protected]>
1 parent a641dfb commit cec29ae

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;
@@ -77,6 +78,10 @@ public function generateRootCert(
7778
string $commonName,
7879
array $names = [],
7980
): void {
81+
if (empty($commonName)) {
82+
throw new EmptyCertificateException('Common Name (CN) cannot be empty for root certificate');
83+
}
84+
8085
$this->cfsslServerHandler->createConfigServer(
8186
$commonName,
8287
$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;
@@ -60,6 +61,10 @@ public function generateRootCert(
6061
string $commonName,
6162
array $names = [],
6263
): void {
64+
if (empty($commonName)) {
65+
throw new EmptyCertificateException('Common Name (CN) cannot be empty for root certificate');
66+
}
67+
6368
$privateKey = openssl_pkey_new([
6469
'private_key_bits' => 2048,
6570
'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)