Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/Handler/CertificateEngine/CfsslHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\CrlMapper;
use OCA\Libresign\Enum\CertificateType;
use OCA\Libresign\Exception\EmptyCertificateException;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\CfsslServerHandler;
use OCA\Libresign\Helper\ConfigureCheckHelper;
Expand Down Expand Up @@ -78,6 +79,10 @@ public function generateRootCert(
string $commonName,
array $names = [],
): void {
if (empty($commonName)) {
throw new EmptyCertificateException('Common Name (CN) cannot be empty for root certificate');
}

$this->cfsslServerHandler->createConfigServer(
$commonName,
$names,
Expand Down
5 changes: 5 additions & 0 deletions lib/Handler/CertificateEngine/OpenSslHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OCA\Libresign\Db\CrlMapper;
use OCA\Libresign\Enum\CertificateType;
use OCA\Libresign\Exception\EmptyCertificateException;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Service\CaIdentifierService;
use OCA\Libresign\Service\CertificatePolicyService;
Expand Down Expand Up @@ -61,6 +62,10 @@ public function generateRootCert(
string $commonName,
array $names = [],
): void {
if (empty($commonName)) {
throw new EmptyCertificateException('Common Name (CN) cannot be empty for root certificate');
}

$privateKey = openssl_pkey_new([
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public function testEmptyCertificate(): void {
$signerInstance->readCertificate('', '');
}

public function testEmptyCommonNameThrowsException(): void {
$rootInstance = $this->getInstance();
$this->expectException(EmptyCertificateException::class);
$this->expectExceptionMessage('Common Name (CN) cannot be empty for root certificate');
$rootInstance->generateRootCert('', []);
}

public function testInvalidPassword(): void {
// Create root cert
$rootInstance = $this->getInstance();
Expand Down
4 changes: 3 additions & 1 deletion tests/php/Unit/Handler/SignEngine/JSignPdfHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function setUpBeforeClass(): void {
$certificateEngine = self::$certificateEngineFactory->getEngine();
$certificateEngine
->setConfigPath(\OCP\Server::get(ITempManager::class)->getTemporaryFolder('certificate'))
->generateRootCert('', []);
->generateRootCert('Test Root CA', []);

self::$certificateContent = $certificateEngine
->setHosts(['user@email.tld'])
Expand Down Expand Up @@ -92,6 +92,7 @@ private function getInstance(array $methods = []): JSignPdfHandler|MockObject {
$this->signatureBackgroundService,
$certificateEngineFactory,
$this->javaHelper,
$this->createMock(\OCA\Libresign\Service\DocMdpConfigService::class),
);
}
return $this->getMockBuilder(JSignPdfHandler::class)
Expand All @@ -103,6 +104,7 @@ private function getInstance(array $methods = []): JSignPdfHandler|MockObject {
$this->signatureBackgroundService,
$certificateEngineFactory,
$this->javaHelper,
$this->createMock(\OCA\Libresign\Service\DocMdpConfigService::class),
])
->onlyMethods($methods)
->getMock();
Expand Down
Loading