-
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathOpenSslHandlerTest.php
More file actions
301 lines (264 loc) · 9.29 KB
/
OpenSslHandlerTest.php
File metadata and controls
301 lines (264 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
use OCA\Libresign\Exception\EmptyCertificateException;
use OCA\Libresign\Exception\InvalidPasswordException;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\CertificateEngine\OpenSslHandler;
use OCA\Libresign\Service\CertificatePolicyService;
use OCP\Files\AppData\IAppDataFactory;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IDateTimeFormatter;
use OCP\ITempManager;
final class OpenSslHandlerTest extends \OCA\Libresign\Tests\Unit\TestCase {
private IConfig $config;
private IAppConfig $appConfig;
private IAppDataFactory $appDataFactory;
private IDateTimeFormatter $dateTimeFormatter;
private ITempManager $tempManager;
private OpenSslHandler $openSslHandler;
protected CertificatePolicyService $certificatePolicyService;
private string $tempDir;
public function setUp(): void {
$this->config = \OCP\Server::get(IConfig::class);
$this->appConfig = \OCP\Server::get(IAppConfig::class);
$this->appDataFactory = \OCP\Server::get(IAppDataFactory::class);
$this->dateTimeFormatter = \OCP\Server::get(IDateTimeFormatter::class);
$this->tempManager = \OCP\Server::get(ITempManager::class);
$this->certificatePolicyService = \OCP\Server::get(certificatePolicyService::class);
$this->tempDir = $this->tempManager->getTemporaryFolder('certificate');
}
private function getInstance(): OpenSslHandler {
$this->openSslHandler = new OpenSslHandler(
$this->config,
$this->appConfig,
$this->appDataFactory,
$this->dateTimeFormatter,
$this->tempManager,
$this->certificatePolicyService,
);
$this->openSslHandler->setConfigPath($this->tempDir);
return $this->openSslHandler;
}
public function testEmptyCertificate(): void {
$signerInstance = $this->getInstance();
// Test invalid password
$this->expectException(EmptyCertificateException::class);
$signerInstance->readCertificate('', '');
}
public function testInvalidPassword(): void {
// Create root cert
$rootInstance = $this->getInstance();
$rootInstance->generateRootCert('', []);
// Create signer cert
$signerInstance = $this->getInstance();
$signerInstance->setHosts(['user@email.tld']);
$signerInstance->setPassword('right password');
$certificateContent = $signerInstance->generateCertificate();
// Test invalid password
$this->expectException(InvalidPasswordException::class);
$signerInstance->readCertificate($certificateContent, 'invalid password');
}
public function testMaxLengthOfDistinguishedNamesWithSuccess(): void {
// Create root cert
$rootInstance = $this->getInstance();
$rootInstance->generateRootCert('', []);
// Create signer cert
$signerInstance = $this->getInstance();
$longName = str_repeat('a', 64);
$signerInstance->setCommonName($longName);
$signerInstance->setPassword('123456');
$certificateContent = $signerInstance->generateCertificate();
$parsed = $signerInstance->readCertificate($certificateContent, '123456');
$this->assertEquals($longName, $parsed['subject']['CN']);
}
public function testBiggerThanMaxLengthOfDistinguishedNamesWithError(): void {
// Create root cert
$rootInstance = $this->getInstance();
$rootInstance->generateRootCert('', []);
// Create signer cert
$signerInstance = $this->getInstance();
$longName = str_repeat('a', 65);
$signerInstance->setCommonName($longName);
$signerInstance->setPassword('123456');
$this->expectException(LibresignException::class);
$signerInstance->generateCertificate();
}
/**
* @dataProvider dataReadCertificate
*/
public function testReadCertificate(
string $commonName,
string $signerName,
array $hosts,
string $password,
array $csrNames,
array $root,
): void {
// Create root cert
$rootInstance = $this->getInstance();
if (isset($root['CN'])) {
$rootInstance->setCommonName($root['CN']);
}
if (isset($root['C'])) {
$rootInstance->setCountry($root['C']);
}
if (isset($root['ST'])) {
$rootInstance->setState($root['ST']);
}
if (isset($root['O'])) {
$rootInstance->setOrganization($root['O']);
}
if (isset($root['OU'])) {
$rootInstance->setOrganizationalUnit($root['OU']);
}
$rootInstance->generateRootCert($commonName, $root);
// Create signer cert
$signerInstance = $this->getInstance();
$signerInstance->setHosts($hosts);
$signerInstance->setPassword($password);
$signerInstance->setFriendlyName($signerName);
if (isset($csrNames['CN'])) {
$signerInstance->setCommonName($csrNames['CN']);
}
if (isset($csrNames['C'])) {
$signerInstance->setCountry($csrNames['C']);
}
if (isset($csrNames['ST'])) {
$signerInstance->setState($csrNames['ST']);
}
if (isset($csrNames['O'])) {
$signerInstance->setOrganization($csrNames['O']);
}
if (isset($csrNames['OU'])) {
$signerInstance->setOrganizationalUnit($csrNames['OU']);
}
$certificateContent = $signerInstance->generateCertificate();
// Parse signer cert
$parsed = $signerInstance->readCertificate($certificateContent, $password);
// Test total elements of extracerts
// The correct content is: cert signer, intermediate certs (if have), root cert
$this->assertArrayHasKey('extracerts', $parsed);
$this->assertCount(2, $parsed['extracerts']);
// Test name
$name = $this->csrArrayToString($csrNames);
$this->assertEquals($parsed['name'], $name);
$this->assertJsonStringEqualsJsonString(
json_encode($csrNames),
json_encode($parsed['subject'])
);
// Test subject
$this->assertEquals($csrNames, $parsed['subject']);
// Test issuer ony if was defined root distinguished names
if (count($root) === count($parsed['issuer'])) {
$this->assertEquals($root, $parsed['issuer']);
}
}
private function csrArrayToString(array $csr): string {
$return = '';
foreach ($csr as $key => $value) {
$return .= "/$key=$value";
}
return $return;
}
public static function dataReadCertificate(): array {
return [
[
'common name',
'Signer Name',
['user@domain.tld'],
'password',
[
'C' => 'CT',
'ST' => 'Some-State',
'O' => 'Organization Name',
],
[],
],
[
'common name',
'Signer Name',
['account:test'],
'password',
[
'C' => 'CT',
'ST' => 'Some-State',
'O' => 'Organization Name',
'OU' => 'Organization Unit',
'CN' => 'Common Name',
],
[
'C' => 'RT',
'ST' => 'Root-State',
'O' => 'Root Organization Name',
'OU' => 'Root Organization Unit',
'CN' => 'Root Common Name',
'UID' => 'account:test'
],
],
[
'common name',
'Signer Name',
['user@domain.tld'],
'password',
[
'C' => 'CT',
'ST' => 'Some-State',
'O' => 'Organization Name',
'OU' => 'Organization Unit',
'CN' => 'Common Name',
],
[
'C' => 'RT',
'ST' => 'Root-State',
'O' => 'Root Organization Name',
'OU' => 'Root Organization Unit',
'CN' => 'Root Common Name',
'UID' => 'email:user@domain.tld'
],
],
];
}
public function testSerialNumberGeneration(): void {
$rootInstance = $this->getInstance();
$rootInstance->generateRootCert('', []);
$signerInstance = $this->getInstance();
$signerInstance->setCommonName('Test User');
$signerInstance->setPassword('123456');
$certificate = $signerInstance->generateCertificate();
$parsed = $signerInstance->readCertificate($certificate, '123456');
$this->assertArrayHasKey('serialNumber', $parsed, 'Certificate should have serialNumber field');
$this->assertArrayHasKey('serialNumberHex', $parsed, 'Certificate should have serialNumberHex field');
$this->assertNotNull($parsed['serialNumber'], 'Serial number should not be null');
$this->assertNotNull($parsed['serialNumberHex'], 'Serial number hex should not be null');
$this->assertNotEquals('0', $parsed['serialNumber'], 'Serial number should not be zero');
$this->assertNotEquals('00', $parsed['serialNumberHex'], 'Serial number hex should not be zero');
$serialInt = (int)$parsed['serialNumber'];
$this->assertGreaterThanOrEqual(1000000, $serialInt, 'Serial number should be >= 1000000');
$this->assertLessThanOrEqual(2147483647, $serialInt, 'Serial number should be <= 2147483647');
$this->assertIsNumeric($parsed['serialNumber'], 'Serial number should be numeric');
$this->assertMatchesRegularExpression('/^[0-9A-Fa-f]+$/', $parsed['serialNumberHex'], 'Serial number hex should contain only hex characters');
}
public function testUniqueSerialNumbers(): void {
$rootInstance = $this->getInstance();
$rootInstance->generateRootCert('', []);
$serialNumbers = [];
$numCertificates = 3;
for ($i = 0; $i < $numCertificates; $i++) {
$signerInstance = $this->getInstance();
$signerInstance->setCommonName("Test Certificate $i");
$signerInstance->setPassword('123456');
$certificateContent = $signerInstance->generateCertificate();
$parsed = $signerInstance->readCertificate($certificateContent, '123456');
$serialNumber = $parsed['serialNumber'];
$this->assertNotEquals('0', $serialNumber, "Certificate $i should not have serial number 0");
$this->assertNotContains($serialNumber, $serialNumbers, "Certificate $i should have unique serial number");
$serialNumbers[] = $serialNumber;
}
$this->assertCount($numCertificates, array_unique($serialNumbers), 'All serial numbers should be unique');
}
}