|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Libresign\Tests\Unit\Service\Crl; |
| 11 | + |
| 12 | +use OCA\Libresign\Service\Crl\CrlDistributionPointsExtractor; |
| 13 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +final class CrlDistributionPointsExtractorTest extends TestCase { |
| 17 | + private CrlDistributionPointsExtractor $extractor; |
| 18 | + |
| 19 | + protected function setUp(): void { |
| 20 | + $this->extractor = new CrlDistributionPointsExtractor(); |
| 21 | + } |
| 22 | + |
| 23 | + #[DataProvider('crlDistributionPointExtractionProvider')] |
| 24 | + public function testExtractFromExtensions(array $extensions, bool $expectedHasExtension, array $expectedUrls): void { |
| 25 | + $result = $this->extractor->extractFromExtensions($extensions); |
| 26 | + |
| 27 | + $this->assertSame($expectedHasExtension, $result['hasExtension']); |
| 28 | + $this->assertSame($expectedUrls, $result['urls']); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * RFC 5280 4.2.1.13 defines cRLDistributionPoints as DistributionPointName |
| 33 | + * with URI represented in GeneralNames. Tests cover common OpenSSL textual |
| 34 | + * outputs for HTTP and LDAP URIs and multiple distribution points. |
| 35 | + * |
| 36 | + * @return array<string, array{0: array<string, mixed>, 1: bool, 2: list<string>}> |
| 37 | + */ |
| 38 | + public static function crlDistributionPointExtractionProvider(): array { |
| 39 | + return [ |
| 40 | + 'oid-extension-with-http-uri' => [ |
| 41 | + [ |
| 42 | + '2.5.29.31' => "Full Name:\nURI:https://example.org/crl/root.crl", |
| 43 | + ], |
| 44 | + true, |
| 45 | + ['https://example.org/crl/root.crl'], |
| 46 | + ], |
| 47 | + 'x509v3-label-with-http-uri' => [ |
| 48 | + [ |
| 49 | + 'X509v3 CRL Distribution Points' => "Full Name:\n URI : https://example.org/crl/issuer.crl", |
| 50 | + ], |
| 51 | + true, |
| 52 | + ['https://example.org/crl/issuer.crl'], |
| 53 | + ], |
| 54 | + 'rfc-ldap-uri-with-dn-and-query' => [ |
| 55 | + [ |
| 56 | + 'crlDistributionPoints' => "Full Name:\nURI:ldap://ldap.example.com/cn=Example%20CA,ou=PKI,dc=example,dc=com?certificateRevocationList;binary", |
| 57 | + ], |
| 58 | + true, |
| 59 | + ['ldap://ldap.example.com/cn=Example%20CA,ou=PKI,dc=example,dc=com?certificateRevocationList;binary'], |
| 60 | + ], |
| 61 | + 'multiple-distribution-points-in-single-extension' => [ |
| 62 | + [ |
| 63 | + '2.5.29.31' => "Full Name:\nURI:https://pki.example.org/root.crl\nFull Name:\nURI:ldap://ldap.example.org/cn=RootCA,dc=example,dc=org?certificateRevocationList;binary", |
| 64 | + ], |
| 65 | + true, |
| 66 | + [ |
| 67 | + 'https://pki.example.org/root.crl', |
| 68 | + 'ldap://ldap.example.org/cn=RootCA,dc=example,dc=org?certificateRevocationList;binary', |
| 69 | + ], |
| 70 | + ], |
| 71 | + 'rfc-structure-with-reasons-and-crl-issuer' => [ |
| 72 | + [ |
| 73 | + '2.5.29.31' => "Full Name:\n URI:http://crl.example.org/root.crl\nReasons: keyCompromise, cACompromise\nCRL Issuer:\n DirName:/C=BR/O=Example/CN=Example CRL Issuer", |
| 74 | + ], |
| 75 | + true, |
| 76 | + ['http://crl.example.org/root.crl'], |
| 77 | + ], |
| 78 | + 'extension-name-is-trimmed-and-case-insensitive' => [ |
| 79 | + [ |
| 80 | + ' X509V3 CRL Distribution Points ' => "Full Name:\n URI:https://example.org/crl/mixed-case.crl", |
| 81 | + ], |
| 82 | + true, |
| 83 | + ['https://example.org/crl/mixed-case.crl'], |
| 84 | + ], |
| 85 | + 'uri-token-is-case-insensitive' => [ |
| 86 | + [ |
| 87 | + '2.5.29.31' => "Full Name:\nuri:ldap://ldap.example.net/cn=CA,dc=example,dc=net?certificateRevocationList;binary", |
| 88 | + ], |
| 89 | + true, |
| 90 | + ['ldap://ldap.example.net/cn=CA,dc=example,dc=net?certificateRevocationList;binary'], |
| 91 | + ], |
| 92 | + 'uri-with-tabs-and-extra-whitespace' => [ |
| 93 | + [ |
| 94 | + '2.5.29.31' => "Full Name:\n\tURI\t:\t https://example.org/crl/with-tabs.crl", |
| 95 | + ], |
| 96 | + true, |
| 97 | + ['https://example.org/crl/with-tabs.crl'], |
| 98 | + ], |
| 99 | + 'uri-line-with-closing-parenthesis-from-formatted-output' => [ |
| 100 | + [ |
| 101 | + '2.5.29.31' => "Distribution Point (1):\nURI:https://example.org/crl/formatted.crl)", |
| 102 | + ], |
| 103 | + true, |
| 104 | + ['https://example.org/crl/formatted.crl'], |
| 105 | + ], |
| 106 | + 'multiple-supported-extension-keys-are-merged-and-deduplicated' => [ |
| 107 | + [ |
| 108 | + '2.5.29.31' => "Full Name:\nURI:https://example.org/crl/shared.crl", |
| 109 | + 'crlDistributionPoints' => "Full Name:\nURI:https://example.org/crl/shared.crl\nURI:https://example.org/crl/extra.crl", |
| 110 | + ], |
| 111 | + true, |
| 112 | + [ |
| 113 | + 'https://example.org/crl/shared.crl', |
| 114 | + 'https://example.org/crl/extra.crl', |
| 115 | + ], |
| 116 | + ], |
| 117 | + 'array-extension-value-and-duplicates' => [ |
| 118 | + [ |
| 119 | + '2.5.29.31' => [ |
| 120 | + 'Full Name:', |
| 121 | + 'URI:https://example.org/crl/root.crl', |
| 122 | + 'URI:https://example.org/crl/root.crl', |
| 123 | + ], |
| 124 | + ], |
| 125 | + true, |
| 126 | + ['https://example.org/crl/root.crl'], |
| 127 | + ], |
| 128 | + 'known-extension-without-uri' => [ |
| 129 | + [ |
| 130 | + '2.5.29.31' => 'Distribution Point Name: relativeName=CN=DP1', |
| 131 | + ], |
| 132 | + true, |
| 133 | + [], |
| 134 | + ], |
| 135 | + 'known-extension-with-general-names-but-no-uri' => [ |
| 136 | + [ |
| 137 | + 'X509v3 CRL Distribution Points' => "Full Name:\nDNS:crl.example.org\nDirName:/C=BR/O=Example/CN=CRL Directory", |
| 138 | + ], |
| 139 | + true, |
| 140 | + [], |
| 141 | + ], |
| 142 | + 'multiple-supported-keys-preserve-first-seen-order' => [ |
| 143 | + [ |
| 144 | + 'crlDistributionPoints' => "Full Name:\nURI:https://example.org/crl/first.crl", |
| 145 | + '2.5.29.31' => "Full Name:\nURI:https://example.org/crl/second.crl", |
| 146 | + ], |
| 147 | + true, |
| 148 | + [ |
| 149 | + 'https://example.org/crl/first.crl', |
| 150 | + 'https://example.org/crl/second.crl', |
| 151 | + ], |
| 152 | + ], |
| 153 | + 'unknown-extension-name-should-not-match' => [ |
| 154 | + [ |
| 155 | + 'Issuer CRL Distribution Points' => "Full Name:\nURI:https://example.org/crl/issuer.crl", |
| 156 | + ], |
| 157 | + false, |
| 158 | + [], |
| 159 | + ], |
| 160 | + ]; |
| 161 | + } |
| 162 | +} |
0 commit comments