@@ -13,136 +13,136 @@ |
||
13 | 13 | */ |
14 | 14 | class CertificateChain implements \Countable, \IteratorAggregate |
15 | 15 | { |
16 | - /** |
|
17 | - * List of certificates in a chain. |
|
18 | - * |
|
19 | - * @var Certificate[] |
|
20 | - */ |
|
21 | - protected $_certs; |
|
16 | + /** |
|
17 | + * List of certificates in a chain. |
|
18 | + * |
|
19 | + * @var Certificate[] |
|
20 | + */ |
|
21 | + protected $_certs; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Constructor. |
|
25 | - * |
|
26 | - * @param Certificate ...$certs List of certificates, end-entity first |
|
27 | - */ |
|
28 | - public function __construct(Certificate ...$certs) |
|
29 | - { |
|
30 | - $this->_certs = $certs; |
|
31 | - } |
|
23 | + /** |
|
24 | + * Constructor. |
|
25 | + * |
|
26 | + * @param Certificate ...$certs List of certificates, end-entity first |
|
27 | + */ |
|
28 | + public function __construct(Certificate ...$certs) |
|
29 | + { |
|
30 | + $this->_certs = $certs; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Initialize from a list of PEMs. |
|
35 | - * |
|
36 | - * @param PEM ...$pems |
|
37 | - * |
|
38 | - * @return self |
|
39 | - */ |
|
40 | - public static function fromPEMs(PEM ...$pems): self |
|
41 | - { |
|
42 | - $certs = array_map( |
|
43 | - function (PEM $pem) { |
|
44 | - return Certificate::fromPEM($pem); |
|
45 | - }, $pems); |
|
46 | - return new self(...$certs); |
|
47 | - } |
|
33 | + /** |
|
34 | + * Initialize from a list of PEMs. |
|
35 | + * |
|
36 | + * @param PEM ...$pems |
|
37 | + * |
|
38 | + * @return self |
|
39 | + */ |
|
40 | + public static function fromPEMs(PEM ...$pems): self |
|
41 | + { |
|
42 | + $certs = array_map( |
|
43 | + function (PEM $pem) { |
|
44 | + return Certificate::fromPEM($pem); |
|
45 | + }, $pems); |
|
46 | + return new self(...$certs); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Initialize from a string containing multiple PEM blocks. |
|
51 | - * |
|
52 | - * @param string $str |
|
53 | - * |
|
54 | - * @return self |
|
55 | - */ |
|
56 | - public static function fromPEMString(string $str): self |
|
57 | - { |
|
58 | - $pems = PEMBundle::fromString($str)->all(); |
|
59 | - return self::fromPEMs(...$pems); |
|
60 | - } |
|
49 | + /** |
|
50 | + * Initialize from a string containing multiple PEM blocks. |
|
51 | + * |
|
52 | + * @param string $str |
|
53 | + * |
|
54 | + * @return self |
|
55 | + */ |
|
56 | + public static function fromPEMString(string $str): self |
|
57 | + { |
|
58 | + $pems = PEMBundle::fromString($str)->all(); |
|
59 | + return self::fromPEMs(...$pems); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Get all certificates in a chain ordered from the end-entity certificate |
|
64 | - * to the trust anchor. |
|
65 | - * |
|
66 | - * @return Certificate[] |
|
67 | - */ |
|
68 | - public function certificates(): array |
|
69 | - { |
|
70 | - return $this->_certs; |
|
71 | - } |
|
62 | + /** |
|
63 | + * Get all certificates in a chain ordered from the end-entity certificate |
|
64 | + * to the trust anchor. |
|
65 | + * |
|
66 | + * @return Certificate[] |
|
67 | + */ |
|
68 | + public function certificates(): array |
|
69 | + { |
|
70 | + return $this->_certs; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Get the end-entity certificate. |
|
75 | - * |
|
76 | - * @throws \LogicException |
|
77 | - * |
|
78 | - * @return Certificate |
|
79 | - */ |
|
80 | - public function endEntityCertificate(): Certificate |
|
81 | - { |
|
82 | - if (!count($this->_certs)) { |
|
83 | - throw new \LogicException('No certificates.'); |
|
84 | - } |
|
85 | - return $this->_certs[0]; |
|
86 | - } |
|
73 | + /** |
|
74 | + * Get the end-entity certificate. |
|
75 | + * |
|
76 | + * @throws \LogicException |
|
77 | + * |
|
78 | + * @return Certificate |
|
79 | + */ |
|
80 | + public function endEntityCertificate(): Certificate |
|
81 | + { |
|
82 | + if (!count($this->_certs)) { |
|
83 | + throw new \LogicException('No certificates.'); |
|
84 | + } |
|
85 | + return $this->_certs[0]; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Get the trust anchor certificate. |
|
90 | - * |
|
91 | - * @throws \LogicException |
|
92 | - * |
|
93 | - * @return Certificate |
|
94 | - */ |
|
95 | - public function trustAnchorCertificate(): Certificate |
|
96 | - { |
|
97 | - if (!count($this->_certs)) { |
|
98 | - throw new \LogicException('No certificates.'); |
|
99 | - } |
|
100 | - return $this->_certs[count($this->_certs) - 1]; |
|
101 | - } |
|
88 | + /** |
|
89 | + * Get the trust anchor certificate. |
|
90 | + * |
|
91 | + * @throws \LogicException |
|
92 | + * |
|
93 | + * @return Certificate |
|
94 | + */ |
|
95 | + public function trustAnchorCertificate(): Certificate |
|
96 | + { |
|
97 | + if (!count($this->_certs)) { |
|
98 | + throw new \LogicException('No certificates.'); |
|
99 | + } |
|
100 | + return $this->_certs[count($this->_certs) - 1]; |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Convert certificate chain to certification path. |
|
105 | - * |
|
106 | - * @return CertificationPath |
|
107 | - */ |
|
108 | - public function certificationPath(): CertificationPath |
|
109 | - { |
|
110 | - return CertificationPath::fromCertificateChain($this); |
|
111 | - } |
|
103 | + /** |
|
104 | + * Convert certificate chain to certification path. |
|
105 | + * |
|
106 | + * @return CertificationPath |
|
107 | + */ |
|
108 | + public function certificationPath(): CertificationPath |
|
109 | + { |
|
110 | + return CertificationPath::fromCertificateChain($this); |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * Convert certificate chain to string of PEM blocks. |
|
115 | - * |
|
116 | - * @return string |
|
117 | - */ |
|
118 | - public function toPEMString(): string |
|
119 | - { |
|
120 | - return implode("\n", |
|
121 | - array_map( |
|
122 | - function (Certificate $cert) { |
|
123 | - return $cert->toPEM()->string(); |
|
124 | - }, $this->_certs)); |
|
125 | - } |
|
113 | + /** |
|
114 | + * Convert certificate chain to string of PEM blocks. |
|
115 | + * |
|
116 | + * @return string |
|
117 | + */ |
|
118 | + public function toPEMString(): string |
|
119 | + { |
|
120 | + return implode("\n", |
|
121 | + array_map( |
|
122 | + function (Certificate $cert) { |
|
123 | + return $cert->toPEM()->string(); |
|
124 | + }, $this->_certs)); |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * @see \Countable::count() |
|
129 | - * |
|
130 | - * @return int |
|
131 | - */ |
|
132 | - public function count(): int |
|
133 | - { |
|
134 | - return count($this->_certs); |
|
135 | - } |
|
127 | + /** |
|
128 | + * @see \Countable::count() |
|
129 | + * |
|
130 | + * @return int |
|
131 | + */ |
|
132 | + public function count(): int |
|
133 | + { |
|
134 | + return count($this->_certs); |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * Get iterator for certificates. |
|
139 | - * |
|
140 | - * @see \IteratorAggregate::getIterator() |
|
141 | - * |
|
142 | - * @return \ArrayIterator |
|
143 | - */ |
|
144 | - public function getIterator(): \ArrayIterator |
|
145 | - { |
|
146 | - return new \ArrayIterator($this->_certs); |
|
147 | - } |
|
137 | + /** |
|
138 | + * Get iterator for certificates. |
|
139 | + * |
|
140 | + * @see \IteratorAggregate::getIterator() |
|
141 | + * |
|
142 | + * @return \ArrayIterator |
|
143 | + */ |
|
144 | + public function getIterator(): \ArrayIterator |
|
145 | + { |
|
146 | + return new \ArrayIterator($this->_certs); |
|
147 | + } |
|
148 | 148 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate; |
6 | 6 | |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | public static function fromPEMs(PEM ...$pems): self |
41 | 41 | { |
42 | 42 | $certs = array_map( |
43 | - function (PEM $pem) { |
|
43 | + function(PEM $pem) { |
|
44 | 44 | return Certificate::fromPEM($pem); |
45 | 45 | }, $pems); |
46 | 46 | return new self(...$certs); |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | { |
120 | 120 | return implode("\n", |
121 | 121 | array_map( |
122 | - function (Certificate $cert) { |
|
122 | + function(Certificate $cert) { |
|
123 | 123 | return $cert->toPEM()->string(); |
124 | 124 | }, $this->_certs)); |
125 | 125 | } |
@@ -27,622 +27,622 @@ |
||
27 | 27 | */ |
28 | 28 | class TBSCertificate |
29 | 29 | { |
30 | - // Certificate version enumerations |
|
31 | - const VERSION_1 = 0; |
|
32 | - const VERSION_2 = 1; |
|
33 | - const VERSION_3 = 2; |
|
34 | - |
|
35 | - /** |
|
36 | - * Certificate version. |
|
37 | - * |
|
38 | - * @var null|int |
|
39 | - */ |
|
40 | - protected $_version; |
|
41 | - |
|
42 | - /** |
|
43 | - * Serial number. |
|
44 | - * |
|
45 | - * @var null|string |
|
46 | - */ |
|
47 | - protected $_serialNumber; |
|
48 | - |
|
49 | - /** |
|
50 | - * Signature algorithm. |
|
51 | - * |
|
52 | - * @var null|SignatureAlgorithmIdentifier |
|
53 | - */ |
|
54 | - protected $_signature; |
|
55 | - |
|
56 | - /** |
|
57 | - * Certificate issuer. |
|
58 | - * |
|
59 | - * @var Name |
|
60 | - */ |
|
61 | - protected $_issuer; |
|
62 | - |
|
63 | - /** |
|
64 | - * Certificate validity period. |
|
65 | - * |
|
66 | - * @var Validity |
|
67 | - */ |
|
68 | - protected $_validity; |
|
69 | - |
|
70 | - /** |
|
71 | - * Certificate subject. |
|
72 | - * |
|
73 | - * @var Name |
|
74 | - */ |
|
75 | - protected $_subject; |
|
76 | - |
|
77 | - /** |
|
78 | - * Subject public key. |
|
79 | - * |
|
80 | - * @var PublicKeyInfo |
|
81 | - */ |
|
82 | - protected $_subjectPublicKeyInfo; |
|
83 | - |
|
84 | - /** |
|
85 | - * Issuer unique identifier. |
|
86 | - * |
|
87 | - * @var null|UniqueIdentifier |
|
88 | - */ |
|
89 | - protected $_issuerUniqueID; |
|
90 | - |
|
91 | - /** |
|
92 | - * Subject unique identifier. |
|
93 | - * |
|
94 | - * @var null|UniqueIdentifier |
|
95 | - */ |
|
96 | - protected $_subjectUniqueID; |
|
97 | - |
|
98 | - /** |
|
99 | - * Extensions. |
|
100 | - * |
|
101 | - * @var Extensions |
|
102 | - */ |
|
103 | - protected $_extensions; |
|
104 | - |
|
105 | - /** |
|
106 | - * Constructor. |
|
107 | - * |
|
108 | - * @param Name $subject Certificate subject |
|
109 | - * @param PublicKeyInfo $pki Subject public key |
|
110 | - * @param Name $issuer Certificate issuer |
|
111 | - * @param Validity $validity Validity period |
|
112 | - */ |
|
113 | - public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer, |
|
114 | - Validity $validity) |
|
115 | - { |
|
116 | - $this->_subject = $subject; |
|
117 | - $this->_subjectPublicKeyInfo = $pki; |
|
118 | - $this->_issuer = $issuer; |
|
119 | - $this->_validity = $validity; |
|
120 | - $this->_extensions = new Extensions(); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Initialize from ASN.1. |
|
125 | - * |
|
126 | - * @param Sequence $seq |
|
127 | - * |
|
128 | - * @return self |
|
129 | - */ |
|
130 | - public static function fromASN1(Sequence $seq): self |
|
131 | - { |
|
132 | - $idx = 0; |
|
133 | - if ($seq->hasTagged(0)) { |
|
134 | - ++$idx; |
|
135 | - $version = $seq->getTagged(0)->asExplicit()->asInteger()->intNumber(); |
|
136 | - } else { |
|
137 | - $version = self::VERSION_1; |
|
138 | - } |
|
139 | - $serial = $seq->at($idx++)->asInteger()->number(); |
|
140 | - $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
141 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
142 | - throw new \UnexpectedValueException( |
|
143 | - 'Unsupported signature algorithm ' . $algo->name() . '.'); |
|
144 | - } |
|
145 | - $issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
146 | - $validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
|
147 | - $subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
148 | - $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
|
149 | - $tbs_cert = new self($subject, $pki, $issuer, $validity); |
|
150 | - $tbs_cert->_version = $version; |
|
151 | - $tbs_cert->_serialNumber = $serial; |
|
152 | - $tbs_cert->_signature = $algo; |
|
153 | - if ($seq->hasTagged(1)) { |
|
154 | - $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
155 | - $seq->getTagged(1)->asImplicit(Element::TYPE_BIT_STRING) |
|
156 | - ->asBitString()); |
|
157 | - } |
|
158 | - if ($seq->hasTagged(2)) { |
|
159 | - $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1( |
|
160 | - $seq->getTagged(2)->asImplicit(Element::TYPE_BIT_STRING) |
|
161 | - ->asBitString()); |
|
162 | - } |
|
163 | - if ($seq->hasTagged(3)) { |
|
164 | - $tbs_cert->_extensions = Extensions::fromASN1( |
|
165 | - $seq->getTagged(3)->asExplicit()->asSequence()); |
|
166 | - } |
|
167 | - return $tbs_cert; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Initialize from certification request. |
|
172 | - * |
|
173 | - * Note that signature is not verified and must be done by the caller. |
|
174 | - * |
|
175 | - * @param CertificationRequest $cr |
|
176 | - * |
|
177 | - * @return self |
|
178 | - */ |
|
179 | - public static function fromCSR(CertificationRequest $cr): self |
|
180 | - { |
|
181 | - $cri = $cr->certificationRequestInfo(); |
|
182 | - $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(), |
|
183 | - Validity::fromStrings(null, null)); |
|
184 | - // if CSR has Extension Request attribute |
|
185 | - if ($cri->hasAttributes()) { |
|
186 | - $attribs = $cri->attributes(); |
|
187 | - if ($attribs->hasExtensionRequest()) { |
|
188 | - $tbs_cert = $tbs_cert->withExtensions( |
|
189 | - $attribs->extensionRequest()->extensions()); |
|
190 | - } |
|
191 | - } |
|
192 | - // add Subject Key Identifier extension |
|
193 | - return $tbs_cert->withAdditionalExtensions( |
|
194 | - new SubjectKeyIdentifierExtension(false, |
|
195 | - $cri->subjectPKInfo()->keyIdentifier())); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Get self with fields set from the issuer's certificate. |
|
200 | - * |
|
201 | - * Issuer shall be set to issuing certificate's subject. |
|
202 | - * Authority key identifier extensions shall be added with a key identifier |
|
203 | - * set to issuing certificate's public key identifier. |
|
204 | - * |
|
205 | - * @param Certificate $cert Issuing party's certificate |
|
206 | - * |
|
207 | - * @return self |
|
208 | - */ |
|
209 | - public function withIssuerCertificate(Certificate $cert): self |
|
210 | - { |
|
211 | - $obj = clone $this; |
|
212 | - // set issuer DN from cert's subject |
|
213 | - $obj->_issuer = $cert->tbsCertificate()->subject(); |
|
214 | - // add authority key identifier extension |
|
215 | - $key_id = $cert->tbsCertificate()->subjectPublicKeyInfo()->keyIdentifier(); |
|
216 | - $obj->_extensions = $obj->_extensions->withExtensions( |
|
217 | - new AuthorityKeyIdentifierExtension(false, $key_id)); |
|
218 | - return $obj; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Get self with given version. |
|
223 | - * |
|
224 | - * If version is not set, appropriate version is automatically |
|
225 | - * determined during signing. |
|
226 | - * |
|
227 | - * @param int $version |
|
228 | - * |
|
229 | - * @return self |
|
230 | - */ |
|
231 | - public function withVersion(int $version): self |
|
232 | - { |
|
233 | - $obj = clone $this; |
|
234 | - $obj->_version = $version; |
|
235 | - return $obj; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Get self with given serial number. |
|
240 | - * |
|
241 | - * @param int|string $serial Base 10 number |
|
242 | - * |
|
243 | - * @return self |
|
244 | - */ |
|
245 | - public function withSerialNumber($serial): self |
|
246 | - { |
|
247 | - $obj = clone $this; |
|
248 | - $obj->_serialNumber = strval($serial); |
|
249 | - return $obj; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Get self with random positive serial number. |
|
254 | - * |
|
255 | - * @param int $size Number of random bytes |
|
256 | - * |
|
257 | - * @return self |
|
258 | - */ |
|
259 | - public function withRandomSerialNumber(int $size = 16): self |
|
260 | - { |
|
261 | - // ensure that first byte is always non-zero and having first bit unset |
|
262 | - $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
263 | - for ($i = 1; $i < $size; ++$i) { |
|
264 | - $num <<= 8; |
|
265 | - $num += mt_rand(0, 0xff); |
|
266 | - } |
|
267 | - return $this->withSerialNumber(gmp_strval($num, 10)); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Get self with given signature algorithm. |
|
272 | - * |
|
273 | - * @param SignatureAlgorithmIdentifier $algo |
|
274 | - * |
|
275 | - * @return self |
|
276 | - */ |
|
277 | - public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
278 | - { |
|
279 | - $obj = clone $this; |
|
280 | - $obj->_signature = $algo; |
|
281 | - return $obj; |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * Get self with given issuer. |
|
286 | - * |
|
287 | - * @param Name $issuer |
|
288 | - * |
|
289 | - * @return self |
|
290 | - */ |
|
291 | - public function withIssuer(Name $issuer): self |
|
292 | - { |
|
293 | - $obj = clone $this; |
|
294 | - $obj->_issuer = $issuer; |
|
295 | - return $obj; |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Get self with given validity. |
|
300 | - * |
|
301 | - * @param Validity $validity |
|
302 | - * |
|
303 | - * @return self |
|
304 | - */ |
|
305 | - public function withValidity(Validity $validity): self |
|
306 | - { |
|
307 | - $obj = clone $this; |
|
308 | - $obj->_validity = $validity; |
|
309 | - return $obj; |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * Get self with given subject. |
|
314 | - * |
|
315 | - * @param Name $subject |
|
316 | - * |
|
317 | - * @return self |
|
318 | - */ |
|
319 | - public function withSubject(Name $subject): self |
|
320 | - { |
|
321 | - $obj = clone $this; |
|
322 | - $obj->_subject = $subject; |
|
323 | - return $obj; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Get self with given subject public key info. |
|
328 | - * |
|
329 | - * @param PublicKeyInfo $pub_key_info |
|
330 | - * |
|
331 | - * @return self |
|
332 | - */ |
|
333 | - public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self |
|
334 | - { |
|
335 | - $obj = clone $this; |
|
336 | - $obj->_subjectPublicKeyInfo = $pub_key_info; |
|
337 | - return $obj; |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Get self with issuer unique ID. |
|
342 | - * |
|
343 | - * @param UniqueIdentifier $id |
|
344 | - * |
|
345 | - * @return self |
|
346 | - */ |
|
347 | - public function withIssuerUniqueID(UniqueIdentifier $id): self |
|
348 | - { |
|
349 | - $obj = clone $this; |
|
350 | - $obj->_issuerUniqueID = $id; |
|
351 | - return $obj; |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Get self with subject unique ID. |
|
356 | - * |
|
357 | - * @param UniqueIdentifier $id |
|
358 | - * |
|
359 | - * @return self |
|
360 | - */ |
|
361 | - public function withSubjectUniqueID(UniqueIdentifier $id): self |
|
362 | - { |
|
363 | - $obj = clone $this; |
|
364 | - $obj->_subjectUniqueID = $id; |
|
365 | - return $obj; |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Get self with given extensions. |
|
370 | - * |
|
371 | - * @param Extensions $extensions |
|
372 | - * |
|
373 | - * @return self |
|
374 | - */ |
|
375 | - public function withExtensions(Extensions $extensions): self |
|
376 | - { |
|
377 | - $obj = clone $this; |
|
378 | - $obj->_extensions = $extensions; |
|
379 | - return $obj; |
|
380 | - } |
|
381 | - |
|
382 | - /** |
|
383 | - * Get self with extensions added. |
|
384 | - * |
|
385 | - * @param Extension ...$exts One or more Extension objects |
|
386 | - * |
|
387 | - * @return self |
|
388 | - */ |
|
389 | - public function withAdditionalExtensions(Extension ...$exts): self |
|
390 | - { |
|
391 | - $obj = clone $this; |
|
392 | - $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
393 | - return $obj; |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Check whether version is set. |
|
398 | - * |
|
399 | - * @return bool |
|
400 | - */ |
|
401 | - public function hasVersion(): bool |
|
402 | - { |
|
403 | - return isset($this->_version); |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * Get certificate version. |
|
408 | - * |
|
409 | - * @throws \LogicException If not set |
|
410 | - * |
|
411 | - * @return int |
|
412 | - */ |
|
413 | - public function version(): int |
|
414 | - { |
|
415 | - if (!$this->hasVersion()) { |
|
416 | - throw new \LogicException('version not set.'); |
|
417 | - } |
|
418 | - return $this->_version; |
|
419 | - } |
|
420 | - |
|
421 | - /** |
|
422 | - * Check whether serial number is set. |
|
423 | - * |
|
424 | - * @return bool |
|
425 | - */ |
|
426 | - public function hasSerialNumber(): bool |
|
427 | - { |
|
428 | - return isset($this->_serialNumber); |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Get serial number. |
|
433 | - * |
|
434 | - * @throws \LogicException If not set |
|
435 | - * |
|
436 | - * @return string Base 10 integer |
|
437 | - */ |
|
438 | - public function serialNumber(): string |
|
439 | - { |
|
440 | - if (!$this->hasSerialNumber()) { |
|
441 | - throw new \LogicException('serialNumber not set.'); |
|
442 | - } |
|
443 | - return $this->_serialNumber; |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Check whether signature algorithm is set. |
|
448 | - * |
|
449 | - * @return bool |
|
450 | - */ |
|
451 | - public function hasSignature(): bool |
|
452 | - { |
|
453 | - return isset($this->_signature); |
|
454 | - } |
|
455 | - |
|
456 | - /** |
|
457 | - * Get signature algorithm. |
|
458 | - * |
|
459 | - * @throws \LogicException If not set |
|
460 | - * |
|
461 | - * @return SignatureAlgorithmIdentifier |
|
462 | - */ |
|
463 | - public function signature(): SignatureAlgorithmIdentifier |
|
464 | - { |
|
465 | - if (!$this->hasSignature()) { |
|
466 | - throw new \LogicException('signature not set.'); |
|
467 | - } |
|
468 | - return $this->_signature; |
|
469 | - } |
|
470 | - |
|
471 | - /** |
|
472 | - * Get issuer. |
|
473 | - * |
|
474 | - * @return Name |
|
475 | - */ |
|
476 | - public function issuer(): Name |
|
477 | - { |
|
478 | - return $this->_issuer; |
|
479 | - } |
|
480 | - |
|
481 | - /** |
|
482 | - * Get validity period. |
|
483 | - * |
|
484 | - * @return Validity |
|
485 | - */ |
|
486 | - public function validity(): Validity |
|
487 | - { |
|
488 | - return $this->_validity; |
|
489 | - } |
|
490 | - |
|
491 | - /** |
|
492 | - * Get subject. |
|
493 | - * |
|
494 | - * @return Name |
|
495 | - */ |
|
496 | - public function subject(): Name |
|
497 | - { |
|
498 | - return $this->_subject; |
|
499 | - } |
|
500 | - |
|
501 | - /** |
|
502 | - * Get subject public key. |
|
503 | - * |
|
504 | - * @return PublicKeyInfo |
|
505 | - */ |
|
506 | - public function subjectPublicKeyInfo(): PublicKeyInfo |
|
507 | - { |
|
508 | - return $this->_subjectPublicKeyInfo; |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * Whether issuer unique identifier is present. |
|
513 | - * |
|
514 | - * @return bool |
|
515 | - */ |
|
516 | - public function hasIssuerUniqueID(): bool |
|
517 | - { |
|
518 | - return isset($this->_issuerUniqueID); |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Get issuerUniqueID. |
|
523 | - * |
|
524 | - * @throws \LogicException If not set |
|
525 | - * |
|
526 | - * @return UniqueIdentifier |
|
527 | - */ |
|
528 | - public function issuerUniqueID(): UniqueIdentifier |
|
529 | - { |
|
530 | - if (!$this->hasIssuerUniqueID()) { |
|
531 | - throw new \LogicException('issuerUniqueID not set.'); |
|
532 | - } |
|
533 | - return $this->_issuerUniqueID; |
|
534 | - } |
|
535 | - |
|
536 | - /** |
|
537 | - * Whether subject unique identifier is present. |
|
538 | - * |
|
539 | - * @return bool |
|
540 | - */ |
|
541 | - public function hasSubjectUniqueID(): bool |
|
542 | - { |
|
543 | - return isset($this->_subjectUniqueID); |
|
544 | - } |
|
545 | - |
|
546 | - /** |
|
547 | - * Get subjectUniqueID. |
|
548 | - * |
|
549 | - * @throws \LogicException If not set |
|
550 | - * |
|
551 | - * @return UniqueIdentifier |
|
552 | - */ |
|
553 | - public function subjectUniqueID(): UniqueIdentifier |
|
554 | - { |
|
555 | - if (!$this->hasSubjectUniqueID()) { |
|
556 | - throw new \LogicException('subjectUniqueID not set.'); |
|
557 | - } |
|
558 | - return $this->_subjectUniqueID; |
|
559 | - } |
|
560 | - |
|
561 | - /** |
|
562 | - * Get extensions. |
|
563 | - * |
|
564 | - * @return Extensions |
|
565 | - */ |
|
566 | - public function extensions(): Extensions |
|
567 | - { |
|
568 | - return $this->_extensions; |
|
569 | - } |
|
570 | - |
|
571 | - /** |
|
572 | - * Generate ASN.1 structure. |
|
573 | - * |
|
574 | - * @return Sequence |
|
575 | - */ |
|
576 | - public function toASN1(): Sequence |
|
577 | - { |
|
578 | - $elements = []; |
|
579 | - $version = $this->version(); |
|
580 | - // if version is not default |
|
581 | - if (self::VERSION_1 != $version) { |
|
582 | - $elements[] = new ExplicitlyTaggedType(0, new Integer($version)); |
|
583 | - } |
|
584 | - $serial = $this->serialNumber(); |
|
585 | - $signature = $this->signature(); |
|
586 | - // add required elements |
|
587 | - array_push($elements, new Integer($serial), $signature->toASN1(), |
|
588 | - $this->_issuer->toASN1(), $this->_validity->toASN1(), |
|
589 | - $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1()); |
|
590 | - if (isset($this->_issuerUniqueID)) { |
|
591 | - $elements[] = new ImplicitlyTaggedType(1, |
|
592 | - $this->_issuerUniqueID->toASN1()); |
|
593 | - } |
|
594 | - if (isset($this->_subjectUniqueID)) { |
|
595 | - $elements[] = new ImplicitlyTaggedType(2, |
|
596 | - $this->_subjectUniqueID->toASN1()); |
|
597 | - } |
|
598 | - if (count($this->_extensions)) { |
|
599 | - $elements[] = new ExplicitlyTaggedType(3, |
|
600 | - $this->_extensions->toASN1()); |
|
601 | - } |
|
602 | - return new Sequence(...$elements); |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * Create signed certificate. |
|
607 | - * |
|
608 | - * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
609 | - * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
610 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
611 | - * |
|
612 | - * @return Certificate |
|
613 | - */ |
|
614 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
615 | - PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): Certificate |
|
616 | - { |
|
617 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
618 | - $tbs_cert = clone $this; |
|
619 | - if (!isset($tbs_cert->_version)) { |
|
620 | - $tbs_cert->_version = $tbs_cert->_determineVersion(); |
|
621 | - } |
|
622 | - if (!isset($tbs_cert->_serialNumber)) { |
|
623 | - $tbs_cert->_serialNumber = strval(0); |
|
624 | - } |
|
625 | - $tbs_cert->_signature = $algo; |
|
626 | - $data = $tbs_cert->toASN1()->toDER(); |
|
627 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
628 | - return new Certificate($tbs_cert, $algo, $signature); |
|
629 | - } |
|
630 | - |
|
631 | - /** |
|
632 | - * Determine minimum version for the certificate. |
|
633 | - * |
|
634 | - * @return int |
|
635 | - */ |
|
636 | - protected function _determineVersion(): int |
|
637 | - { |
|
638 | - // if extensions are present |
|
639 | - if (count($this->_extensions)) { |
|
640 | - return self::VERSION_3; |
|
641 | - } |
|
642 | - // if UniqueIdentifier is present |
|
643 | - if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) { |
|
644 | - return self::VERSION_2; |
|
645 | - } |
|
646 | - return self::VERSION_1; |
|
647 | - } |
|
30 | + // Certificate version enumerations |
|
31 | + const VERSION_1 = 0; |
|
32 | + const VERSION_2 = 1; |
|
33 | + const VERSION_3 = 2; |
|
34 | + |
|
35 | + /** |
|
36 | + * Certificate version. |
|
37 | + * |
|
38 | + * @var null|int |
|
39 | + */ |
|
40 | + protected $_version; |
|
41 | + |
|
42 | + /** |
|
43 | + * Serial number. |
|
44 | + * |
|
45 | + * @var null|string |
|
46 | + */ |
|
47 | + protected $_serialNumber; |
|
48 | + |
|
49 | + /** |
|
50 | + * Signature algorithm. |
|
51 | + * |
|
52 | + * @var null|SignatureAlgorithmIdentifier |
|
53 | + */ |
|
54 | + protected $_signature; |
|
55 | + |
|
56 | + /** |
|
57 | + * Certificate issuer. |
|
58 | + * |
|
59 | + * @var Name |
|
60 | + */ |
|
61 | + protected $_issuer; |
|
62 | + |
|
63 | + /** |
|
64 | + * Certificate validity period. |
|
65 | + * |
|
66 | + * @var Validity |
|
67 | + */ |
|
68 | + protected $_validity; |
|
69 | + |
|
70 | + /** |
|
71 | + * Certificate subject. |
|
72 | + * |
|
73 | + * @var Name |
|
74 | + */ |
|
75 | + protected $_subject; |
|
76 | + |
|
77 | + /** |
|
78 | + * Subject public key. |
|
79 | + * |
|
80 | + * @var PublicKeyInfo |
|
81 | + */ |
|
82 | + protected $_subjectPublicKeyInfo; |
|
83 | + |
|
84 | + /** |
|
85 | + * Issuer unique identifier. |
|
86 | + * |
|
87 | + * @var null|UniqueIdentifier |
|
88 | + */ |
|
89 | + protected $_issuerUniqueID; |
|
90 | + |
|
91 | + /** |
|
92 | + * Subject unique identifier. |
|
93 | + * |
|
94 | + * @var null|UniqueIdentifier |
|
95 | + */ |
|
96 | + protected $_subjectUniqueID; |
|
97 | + |
|
98 | + /** |
|
99 | + * Extensions. |
|
100 | + * |
|
101 | + * @var Extensions |
|
102 | + */ |
|
103 | + protected $_extensions; |
|
104 | + |
|
105 | + /** |
|
106 | + * Constructor. |
|
107 | + * |
|
108 | + * @param Name $subject Certificate subject |
|
109 | + * @param PublicKeyInfo $pki Subject public key |
|
110 | + * @param Name $issuer Certificate issuer |
|
111 | + * @param Validity $validity Validity period |
|
112 | + */ |
|
113 | + public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer, |
|
114 | + Validity $validity) |
|
115 | + { |
|
116 | + $this->_subject = $subject; |
|
117 | + $this->_subjectPublicKeyInfo = $pki; |
|
118 | + $this->_issuer = $issuer; |
|
119 | + $this->_validity = $validity; |
|
120 | + $this->_extensions = new Extensions(); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Initialize from ASN.1. |
|
125 | + * |
|
126 | + * @param Sequence $seq |
|
127 | + * |
|
128 | + * @return self |
|
129 | + */ |
|
130 | + public static function fromASN1(Sequence $seq): self |
|
131 | + { |
|
132 | + $idx = 0; |
|
133 | + if ($seq->hasTagged(0)) { |
|
134 | + ++$idx; |
|
135 | + $version = $seq->getTagged(0)->asExplicit()->asInteger()->intNumber(); |
|
136 | + } else { |
|
137 | + $version = self::VERSION_1; |
|
138 | + } |
|
139 | + $serial = $seq->at($idx++)->asInteger()->number(); |
|
140 | + $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
141 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
142 | + throw new \UnexpectedValueException( |
|
143 | + 'Unsupported signature algorithm ' . $algo->name() . '.'); |
|
144 | + } |
|
145 | + $issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
146 | + $validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
|
147 | + $subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
148 | + $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
|
149 | + $tbs_cert = new self($subject, $pki, $issuer, $validity); |
|
150 | + $tbs_cert->_version = $version; |
|
151 | + $tbs_cert->_serialNumber = $serial; |
|
152 | + $tbs_cert->_signature = $algo; |
|
153 | + if ($seq->hasTagged(1)) { |
|
154 | + $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
155 | + $seq->getTagged(1)->asImplicit(Element::TYPE_BIT_STRING) |
|
156 | + ->asBitString()); |
|
157 | + } |
|
158 | + if ($seq->hasTagged(2)) { |
|
159 | + $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1( |
|
160 | + $seq->getTagged(2)->asImplicit(Element::TYPE_BIT_STRING) |
|
161 | + ->asBitString()); |
|
162 | + } |
|
163 | + if ($seq->hasTagged(3)) { |
|
164 | + $tbs_cert->_extensions = Extensions::fromASN1( |
|
165 | + $seq->getTagged(3)->asExplicit()->asSequence()); |
|
166 | + } |
|
167 | + return $tbs_cert; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Initialize from certification request. |
|
172 | + * |
|
173 | + * Note that signature is not verified and must be done by the caller. |
|
174 | + * |
|
175 | + * @param CertificationRequest $cr |
|
176 | + * |
|
177 | + * @return self |
|
178 | + */ |
|
179 | + public static function fromCSR(CertificationRequest $cr): self |
|
180 | + { |
|
181 | + $cri = $cr->certificationRequestInfo(); |
|
182 | + $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(), |
|
183 | + Validity::fromStrings(null, null)); |
|
184 | + // if CSR has Extension Request attribute |
|
185 | + if ($cri->hasAttributes()) { |
|
186 | + $attribs = $cri->attributes(); |
|
187 | + if ($attribs->hasExtensionRequest()) { |
|
188 | + $tbs_cert = $tbs_cert->withExtensions( |
|
189 | + $attribs->extensionRequest()->extensions()); |
|
190 | + } |
|
191 | + } |
|
192 | + // add Subject Key Identifier extension |
|
193 | + return $tbs_cert->withAdditionalExtensions( |
|
194 | + new SubjectKeyIdentifierExtension(false, |
|
195 | + $cri->subjectPKInfo()->keyIdentifier())); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Get self with fields set from the issuer's certificate. |
|
200 | + * |
|
201 | + * Issuer shall be set to issuing certificate's subject. |
|
202 | + * Authority key identifier extensions shall be added with a key identifier |
|
203 | + * set to issuing certificate's public key identifier. |
|
204 | + * |
|
205 | + * @param Certificate $cert Issuing party's certificate |
|
206 | + * |
|
207 | + * @return self |
|
208 | + */ |
|
209 | + public function withIssuerCertificate(Certificate $cert): self |
|
210 | + { |
|
211 | + $obj = clone $this; |
|
212 | + // set issuer DN from cert's subject |
|
213 | + $obj->_issuer = $cert->tbsCertificate()->subject(); |
|
214 | + // add authority key identifier extension |
|
215 | + $key_id = $cert->tbsCertificate()->subjectPublicKeyInfo()->keyIdentifier(); |
|
216 | + $obj->_extensions = $obj->_extensions->withExtensions( |
|
217 | + new AuthorityKeyIdentifierExtension(false, $key_id)); |
|
218 | + return $obj; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Get self with given version. |
|
223 | + * |
|
224 | + * If version is not set, appropriate version is automatically |
|
225 | + * determined during signing. |
|
226 | + * |
|
227 | + * @param int $version |
|
228 | + * |
|
229 | + * @return self |
|
230 | + */ |
|
231 | + public function withVersion(int $version): self |
|
232 | + { |
|
233 | + $obj = clone $this; |
|
234 | + $obj->_version = $version; |
|
235 | + return $obj; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Get self with given serial number. |
|
240 | + * |
|
241 | + * @param int|string $serial Base 10 number |
|
242 | + * |
|
243 | + * @return self |
|
244 | + */ |
|
245 | + public function withSerialNumber($serial): self |
|
246 | + { |
|
247 | + $obj = clone $this; |
|
248 | + $obj->_serialNumber = strval($serial); |
|
249 | + return $obj; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Get self with random positive serial number. |
|
254 | + * |
|
255 | + * @param int $size Number of random bytes |
|
256 | + * |
|
257 | + * @return self |
|
258 | + */ |
|
259 | + public function withRandomSerialNumber(int $size = 16): self |
|
260 | + { |
|
261 | + // ensure that first byte is always non-zero and having first bit unset |
|
262 | + $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
263 | + for ($i = 1; $i < $size; ++$i) { |
|
264 | + $num <<= 8; |
|
265 | + $num += mt_rand(0, 0xff); |
|
266 | + } |
|
267 | + return $this->withSerialNumber(gmp_strval($num, 10)); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Get self with given signature algorithm. |
|
272 | + * |
|
273 | + * @param SignatureAlgorithmIdentifier $algo |
|
274 | + * |
|
275 | + * @return self |
|
276 | + */ |
|
277 | + public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
278 | + { |
|
279 | + $obj = clone $this; |
|
280 | + $obj->_signature = $algo; |
|
281 | + return $obj; |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * Get self with given issuer. |
|
286 | + * |
|
287 | + * @param Name $issuer |
|
288 | + * |
|
289 | + * @return self |
|
290 | + */ |
|
291 | + public function withIssuer(Name $issuer): self |
|
292 | + { |
|
293 | + $obj = clone $this; |
|
294 | + $obj->_issuer = $issuer; |
|
295 | + return $obj; |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Get self with given validity. |
|
300 | + * |
|
301 | + * @param Validity $validity |
|
302 | + * |
|
303 | + * @return self |
|
304 | + */ |
|
305 | + public function withValidity(Validity $validity): self |
|
306 | + { |
|
307 | + $obj = clone $this; |
|
308 | + $obj->_validity = $validity; |
|
309 | + return $obj; |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * Get self with given subject. |
|
314 | + * |
|
315 | + * @param Name $subject |
|
316 | + * |
|
317 | + * @return self |
|
318 | + */ |
|
319 | + public function withSubject(Name $subject): self |
|
320 | + { |
|
321 | + $obj = clone $this; |
|
322 | + $obj->_subject = $subject; |
|
323 | + return $obj; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Get self with given subject public key info. |
|
328 | + * |
|
329 | + * @param PublicKeyInfo $pub_key_info |
|
330 | + * |
|
331 | + * @return self |
|
332 | + */ |
|
333 | + public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self |
|
334 | + { |
|
335 | + $obj = clone $this; |
|
336 | + $obj->_subjectPublicKeyInfo = $pub_key_info; |
|
337 | + return $obj; |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Get self with issuer unique ID. |
|
342 | + * |
|
343 | + * @param UniqueIdentifier $id |
|
344 | + * |
|
345 | + * @return self |
|
346 | + */ |
|
347 | + public function withIssuerUniqueID(UniqueIdentifier $id): self |
|
348 | + { |
|
349 | + $obj = clone $this; |
|
350 | + $obj->_issuerUniqueID = $id; |
|
351 | + return $obj; |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Get self with subject unique ID. |
|
356 | + * |
|
357 | + * @param UniqueIdentifier $id |
|
358 | + * |
|
359 | + * @return self |
|
360 | + */ |
|
361 | + public function withSubjectUniqueID(UniqueIdentifier $id): self |
|
362 | + { |
|
363 | + $obj = clone $this; |
|
364 | + $obj->_subjectUniqueID = $id; |
|
365 | + return $obj; |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Get self with given extensions. |
|
370 | + * |
|
371 | + * @param Extensions $extensions |
|
372 | + * |
|
373 | + * @return self |
|
374 | + */ |
|
375 | + public function withExtensions(Extensions $extensions): self |
|
376 | + { |
|
377 | + $obj = clone $this; |
|
378 | + $obj->_extensions = $extensions; |
|
379 | + return $obj; |
|
380 | + } |
|
381 | + |
|
382 | + /** |
|
383 | + * Get self with extensions added. |
|
384 | + * |
|
385 | + * @param Extension ...$exts One or more Extension objects |
|
386 | + * |
|
387 | + * @return self |
|
388 | + */ |
|
389 | + public function withAdditionalExtensions(Extension ...$exts): self |
|
390 | + { |
|
391 | + $obj = clone $this; |
|
392 | + $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
393 | + return $obj; |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Check whether version is set. |
|
398 | + * |
|
399 | + * @return bool |
|
400 | + */ |
|
401 | + public function hasVersion(): bool |
|
402 | + { |
|
403 | + return isset($this->_version); |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * Get certificate version. |
|
408 | + * |
|
409 | + * @throws \LogicException If not set |
|
410 | + * |
|
411 | + * @return int |
|
412 | + */ |
|
413 | + public function version(): int |
|
414 | + { |
|
415 | + if (!$this->hasVersion()) { |
|
416 | + throw new \LogicException('version not set.'); |
|
417 | + } |
|
418 | + return $this->_version; |
|
419 | + } |
|
420 | + |
|
421 | + /** |
|
422 | + * Check whether serial number is set. |
|
423 | + * |
|
424 | + * @return bool |
|
425 | + */ |
|
426 | + public function hasSerialNumber(): bool |
|
427 | + { |
|
428 | + return isset($this->_serialNumber); |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Get serial number. |
|
433 | + * |
|
434 | + * @throws \LogicException If not set |
|
435 | + * |
|
436 | + * @return string Base 10 integer |
|
437 | + */ |
|
438 | + public function serialNumber(): string |
|
439 | + { |
|
440 | + if (!$this->hasSerialNumber()) { |
|
441 | + throw new \LogicException('serialNumber not set.'); |
|
442 | + } |
|
443 | + return $this->_serialNumber; |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Check whether signature algorithm is set. |
|
448 | + * |
|
449 | + * @return bool |
|
450 | + */ |
|
451 | + public function hasSignature(): bool |
|
452 | + { |
|
453 | + return isset($this->_signature); |
|
454 | + } |
|
455 | + |
|
456 | + /** |
|
457 | + * Get signature algorithm. |
|
458 | + * |
|
459 | + * @throws \LogicException If not set |
|
460 | + * |
|
461 | + * @return SignatureAlgorithmIdentifier |
|
462 | + */ |
|
463 | + public function signature(): SignatureAlgorithmIdentifier |
|
464 | + { |
|
465 | + if (!$this->hasSignature()) { |
|
466 | + throw new \LogicException('signature not set.'); |
|
467 | + } |
|
468 | + return $this->_signature; |
|
469 | + } |
|
470 | + |
|
471 | + /** |
|
472 | + * Get issuer. |
|
473 | + * |
|
474 | + * @return Name |
|
475 | + */ |
|
476 | + public function issuer(): Name |
|
477 | + { |
|
478 | + return $this->_issuer; |
|
479 | + } |
|
480 | + |
|
481 | + /** |
|
482 | + * Get validity period. |
|
483 | + * |
|
484 | + * @return Validity |
|
485 | + */ |
|
486 | + public function validity(): Validity |
|
487 | + { |
|
488 | + return $this->_validity; |
|
489 | + } |
|
490 | + |
|
491 | + /** |
|
492 | + * Get subject. |
|
493 | + * |
|
494 | + * @return Name |
|
495 | + */ |
|
496 | + public function subject(): Name |
|
497 | + { |
|
498 | + return $this->_subject; |
|
499 | + } |
|
500 | + |
|
501 | + /** |
|
502 | + * Get subject public key. |
|
503 | + * |
|
504 | + * @return PublicKeyInfo |
|
505 | + */ |
|
506 | + public function subjectPublicKeyInfo(): PublicKeyInfo |
|
507 | + { |
|
508 | + return $this->_subjectPublicKeyInfo; |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * Whether issuer unique identifier is present. |
|
513 | + * |
|
514 | + * @return bool |
|
515 | + */ |
|
516 | + public function hasIssuerUniqueID(): bool |
|
517 | + { |
|
518 | + return isset($this->_issuerUniqueID); |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Get issuerUniqueID. |
|
523 | + * |
|
524 | + * @throws \LogicException If not set |
|
525 | + * |
|
526 | + * @return UniqueIdentifier |
|
527 | + */ |
|
528 | + public function issuerUniqueID(): UniqueIdentifier |
|
529 | + { |
|
530 | + if (!$this->hasIssuerUniqueID()) { |
|
531 | + throw new \LogicException('issuerUniqueID not set.'); |
|
532 | + } |
|
533 | + return $this->_issuerUniqueID; |
|
534 | + } |
|
535 | + |
|
536 | + /** |
|
537 | + * Whether subject unique identifier is present. |
|
538 | + * |
|
539 | + * @return bool |
|
540 | + */ |
|
541 | + public function hasSubjectUniqueID(): bool |
|
542 | + { |
|
543 | + return isset($this->_subjectUniqueID); |
|
544 | + } |
|
545 | + |
|
546 | + /** |
|
547 | + * Get subjectUniqueID. |
|
548 | + * |
|
549 | + * @throws \LogicException If not set |
|
550 | + * |
|
551 | + * @return UniqueIdentifier |
|
552 | + */ |
|
553 | + public function subjectUniqueID(): UniqueIdentifier |
|
554 | + { |
|
555 | + if (!$this->hasSubjectUniqueID()) { |
|
556 | + throw new \LogicException('subjectUniqueID not set.'); |
|
557 | + } |
|
558 | + return $this->_subjectUniqueID; |
|
559 | + } |
|
560 | + |
|
561 | + /** |
|
562 | + * Get extensions. |
|
563 | + * |
|
564 | + * @return Extensions |
|
565 | + */ |
|
566 | + public function extensions(): Extensions |
|
567 | + { |
|
568 | + return $this->_extensions; |
|
569 | + } |
|
570 | + |
|
571 | + /** |
|
572 | + * Generate ASN.1 structure. |
|
573 | + * |
|
574 | + * @return Sequence |
|
575 | + */ |
|
576 | + public function toASN1(): Sequence |
|
577 | + { |
|
578 | + $elements = []; |
|
579 | + $version = $this->version(); |
|
580 | + // if version is not default |
|
581 | + if (self::VERSION_1 != $version) { |
|
582 | + $elements[] = new ExplicitlyTaggedType(0, new Integer($version)); |
|
583 | + } |
|
584 | + $serial = $this->serialNumber(); |
|
585 | + $signature = $this->signature(); |
|
586 | + // add required elements |
|
587 | + array_push($elements, new Integer($serial), $signature->toASN1(), |
|
588 | + $this->_issuer->toASN1(), $this->_validity->toASN1(), |
|
589 | + $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1()); |
|
590 | + if (isset($this->_issuerUniqueID)) { |
|
591 | + $elements[] = new ImplicitlyTaggedType(1, |
|
592 | + $this->_issuerUniqueID->toASN1()); |
|
593 | + } |
|
594 | + if (isset($this->_subjectUniqueID)) { |
|
595 | + $elements[] = new ImplicitlyTaggedType(2, |
|
596 | + $this->_subjectUniqueID->toASN1()); |
|
597 | + } |
|
598 | + if (count($this->_extensions)) { |
|
599 | + $elements[] = new ExplicitlyTaggedType(3, |
|
600 | + $this->_extensions->toASN1()); |
|
601 | + } |
|
602 | + return new Sequence(...$elements); |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * Create signed certificate. |
|
607 | + * |
|
608 | + * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
609 | + * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
610 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
611 | + * |
|
612 | + * @return Certificate |
|
613 | + */ |
|
614 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
615 | + PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): Certificate |
|
616 | + { |
|
617 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
618 | + $tbs_cert = clone $this; |
|
619 | + if (!isset($tbs_cert->_version)) { |
|
620 | + $tbs_cert->_version = $tbs_cert->_determineVersion(); |
|
621 | + } |
|
622 | + if (!isset($tbs_cert->_serialNumber)) { |
|
623 | + $tbs_cert->_serialNumber = strval(0); |
|
624 | + } |
|
625 | + $tbs_cert->_signature = $algo; |
|
626 | + $data = $tbs_cert->toASN1()->toDER(); |
|
627 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
628 | + return new Certificate($tbs_cert, $algo, $signature); |
|
629 | + } |
|
630 | + |
|
631 | + /** |
|
632 | + * Determine minimum version for the certificate. |
|
633 | + * |
|
634 | + * @return int |
|
635 | + */ |
|
636 | + protected function _determineVersion(): int |
|
637 | + { |
|
638 | + // if extensions are present |
|
639 | + if (count($this->_extensions)) { |
|
640 | + return self::VERSION_3; |
|
641 | + } |
|
642 | + // if UniqueIdentifier is present |
|
643 | + if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) { |
|
644 | + return self::VERSION_2; |
|
645 | + } |
|
646 | + return self::VERSION_1; |
|
647 | + } |
|
648 | 648 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate; |
6 | 6 |
@@ -20,242 +20,242 @@ |
||
20 | 20 | */ |
21 | 21 | class Certificate |
22 | 22 | { |
23 | - /** |
|
24 | - * "To be signed" certificate information. |
|
25 | - * |
|
26 | - * @var TBSCertificate |
|
27 | - */ |
|
28 | - protected $_tbsCertificate; |
|
23 | + /** |
|
24 | + * "To be signed" certificate information. |
|
25 | + * |
|
26 | + * @var TBSCertificate |
|
27 | + */ |
|
28 | + protected $_tbsCertificate; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Signature algorithm. |
|
32 | - * |
|
33 | - * @var SignatureAlgorithmIdentifier |
|
34 | - */ |
|
35 | - protected $_signatureAlgorithm; |
|
30 | + /** |
|
31 | + * Signature algorithm. |
|
32 | + * |
|
33 | + * @var SignatureAlgorithmIdentifier |
|
34 | + */ |
|
35 | + protected $_signatureAlgorithm; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Signature value. |
|
39 | - * |
|
40 | - * @var Signature |
|
41 | - */ |
|
42 | - protected $_signatureValue; |
|
37 | + /** |
|
38 | + * Signature value. |
|
39 | + * |
|
40 | + * @var Signature |
|
41 | + */ |
|
42 | + protected $_signatureValue; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Constructor. |
|
46 | - * |
|
47 | - * @param TBSCertificate $tbsCert |
|
48 | - * @param SignatureAlgorithmIdentifier $algo |
|
49 | - * @param Signature $signature |
|
50 | - */ |
|
51 | - public function __construct(TBSCertificate $tbsCert, |
|
52 | - SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
53 | - { |
|
54 | - $this->_tbsCertificate = $tbsCert; |
|
55 | - $this->_signatureAlgorithm = $algo; |
|
56 | - $this->_signatureValue = $signature; |
|
57 | - } |
|
44 | + /** |
|
45 | + * Constructor. |
|
46 | + * |
|
47 | + * @param TBSCertificate $tbsCert |
|
48 | + * @param SignatureAlgorithmIdentifier $algo |
|
49 | + * @param Signature $signature |
|
50 | + */ |
|
51 | + public function __construct(TBSCertificate $tbsCert, |
|
52 | + SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
53 | + { |
|
54 | + $this->_tbsCertificate = $tbsCert; |
|
55 | + $this->_signatureAlgorithm = $algo; |
|
56 | + $this->_signatureValue = $signature; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Get certificate as a PEM formatted string. |
|
61 | - * |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - public function __toString(): string |
|
65 | - { |
|
66 | - return $this->toPEM()->string(); |
|
67 | - } |
|
59 | + /** |
|
60 | + * Get certificate as a PEM formatted string. |
|
61 | + * |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + public function __toString(): string |
|
65 | + { |
|
66 | + return $this->toPEM()->string(); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Initialize from ASN.1. |
|
71 | - * |
|
72 | - * @param Sequence $seq |
|
73 | - * |
|
74 | - * @return self |
|
75 | - */ |
|
76 | - public static function fromASN1(Sequence $seq): self |
|
77 | - { |
|
78 | - $tbsCert = TBSCertificate::fromASN1($seq->at(0)->asSequence()); |
|
79 | - $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
80 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
81 | - throw new \UnexpectedValueException( |
|
82 | - 'Unsupported signature algorithm ' . $algo->oid() . '.'); |
|
83 | - } |
|
84 | - $signature = Signature::fromSignatureData( |
|
85 | - $seq->at(2)->asBitString()->string(), $algo); |
|
86 | - return new self($tbsCert, $algo, $signature); |
|
87 | - } |
|
69 | + /** |
|
70 | + * Initialize from ASN.1. |
|
71 | + * |
|
72 | + * @param Sequence $seq |
|
73 | + * |
|
74 | + * @return self |
|
75 | + */ |
|
76 | + public static function fromASN1(Sequence $seq): self |
|
77 | + { |
|
78 | + $tbsCert = TBSCertificate::fromASN1($seq->at(0)->asSequence()); |
|
79 | + $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
80 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
81 | + throw new \UnexpectedValueException( |
|
82 | + 'Unsupported signature algorithm ' . $algo->oid() . '.'); |
|
83 | + } |
|
84 | + $signature = Signature::fromSignatureData( |
|
85 | + $seq->at(2)->asBitString()->string(), $algo); |
|
86 | + return new self($tbsCert, $algo, $signature); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Initialize from DER. |
|
91 | - * |
|
92 | - * @param string $data |
|
93 | - * |
|
94 | - * @return self |
|
95 | - */ |
|
96 | - public static function fromDER(string $data): self |
|
97 | - { |
|
98 | - return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
|
99 | - } |
|
89 | + /** |
|
90 | + * Initialize from DER. |
|
91 | + * |
|
92 | + * @param string $data |
|
93 | + * |
|
94 | + * @return self |
|
95 | + */ |
|
96 | + public static function fromDER(string $data): self |
|
97 | + { |
|
98 | + return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Initialize from PEM. |
|
103 | - * |
|
104 | - * @param PEM $pem |
|
105 | - * |
|
106 | - * @throws \UnexpectedValueException |
|
107 | - * |
|
108 | - * @return self |
|
109 | - */ |
|
110 | - public static function fromPEM(PEM $pem): self |
|
111 | - { |
|
112 | - if (PEM::TYPE_CERTIFICATE != $pem->type()) { |
|
113 | - throw new \UnexpectedValueException('Invalid PEM type.'); |
|
114 | - } |
|
115 | - return self::fromDER($pem->data()); |
|
116 | - } |
|
101 | + /** |
|
102 | + * Initialize from PEM. |
|
103 | + * |
|
104 | + * @param PEM $pem |
|
105 | + * |
|
106 | + * @throws \UnexpectedValueException |
|
107 | + * |
|
108 | + * @return self |
|
109 | + */ |
|
110 | + public static function fromPEM(PEM $pem): self |
|
111 | + { |
|
112 | + if (PEM::TYPE_CERTIFICATE != $pem->type()) { |
|
113 | + throw new \UnexpectedValueException('Invalid PEM type.'); |
|
114 | + } |
|
115 | + return self::fromDER($pem->data()); |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Get certificate information. |
|
120 | - * |
|
121 | - * @return TBSCertificate |
|
122 | - */ |
|
123 | - public function tbsCertificate(): TBSCertificate |
|
124 | - { |
|
125 | - return $this->_tbsCertificate; |
|
126 | - } |
|
118 | + /** |
|
119 | + * Get certificate information. |
|
120 | + * |
|
121 | + * @return TBSCertificate |
|
122 | + */ |
|
123 | + public function tbsCertificate(): TBSCertificate |
|
124 | + { |
|
125 | + return $this->_tbsCertificate; |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Get signature algorithm. |
|
130 | - * |
|
131 | - * @return SignatureAlgorithmIdentifier |
|
132 | - */ |
|
133 | - public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
134 | - { |
|
135 | - return $this->_signatureAlgorithm; |
|
136 | - } |
|
128 | + /** |
|
129 | + * Get signature algorithm. |
|
130 | + * |
|
131 | + * @return SignatureAlgorithmIdentifier |
|
132 | + */ |
|
133 | + public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
134 | + { |
|
135 | + return $this->_signatureAlgorithm; |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * Get signature value. |
|
140 | - * |
|
141 | - * @return Signature |
|
142 | - */ |
|
143 | - public function signatureValue(): Signature |
|
144 | - { |
|
145 | - return $this->_signatureValue; |
|
146 | - } |
|
138 | + /** |
|
139 | + * Get signature value. |
|
140 | + * |
|
141 | + * @return Signature |
|
142 | + */ |
|
143 | + public function signatureValue(): Signature |
|
144 | + { |
|
145 | + return $this->_signatureValue; |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * Check whether certificate is self-issued. |
|
150 | - * |
|
151 | - * @return bool |
|
152 | - */ |
|
153 | - public function isSelfIssued(): bool |
|
154 | - { |
|
155 | - return $this->_tbsCertificate->subject()->equals( |
|
156 | - $this->_tbsCertificate->issuer()); |
|
157 | - } |
|
148 | + /** |
|
149 | + * Check whether certificate is self-issued. |
|
150 | + * |
|
151 | + * @return bool |
|
152 | + */ |
|
153 | + public function isSelfIssued(): bool |
|
154 | + { |
|
155 | + return $this->_tbsCertificate->subject()->equals( |
|
156 | + $this->_tbsCertificate->issuer()); |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * Check whether certificate is semantically equal to another. |
|
161 | - * |
|
162 | - * @param Certificate $cert Certificate to compare to |
|
163 | - * |
|
164 | - * @return bool |
|
165 | - */ |
|
166 | - public function equals(Certificate $cert): bool |
|
167 | - { |
|
168 | - return $this->_hasEqualSerialNumber($cert) && |
|
169 | - $this->_hasEqualPublicKey($cert) && $this->_hasEqualSubject($cert); |
|
170 | - } |
|
159 | + /** |
|
160 | + * Check whether certificate is semantically equal to another. |
|
161 | + * |
|
162 | + * @param Certificate $cert Certificate to compare to |
|
163 | + * |
|
164 | + * @return bool |
|
165 | + */ |
|
166 | + public function equals(Certificate $cert): bool |
|
167 | + { |
|
168 | + return $this->_hasEqualSerialNumber($cert) && |
|
169 | + $this->_hasEqualPublicKey($cert) && $this->_hasEqualSubject($cert); |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * Generate ASN.1 structure. |
|
174 | - * |
|
175 | - * @return Sequence |
|
176 | - */ |
|
177 | - public function toASN1(): Sequence |
|
178 | - { |
|
179 | - return new Sequence($this->_tbsCertificate->toASN1(), |
|
180 | - $this->_signatureAlgorithm->toASN1(), |
|
181 | - $this->_signatureValue->bitString()); |
|
182 | - } |
|
172 | + /** |
|
173 | + * Generate ASN.1 structure. |
|
174 | + * |
|
175 | + * @return Sequence |
|
176 | + */ |
|
177 | + public function toASN1(): Sequence |
|
178 | + { |
|
179 | + return new Sequence($this->_tbsCertificate->toASN1(), |
|
180 | + $this->_signatureAlgorithm->toASN1(), |
|
181 | + $this->_signatureValue->bitString()); |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * Get certificate as a DER. |
|
186 | - * |
|
187 | - * @return string |
|
188 | - */ |
|
189 | - public function toDER(): string |
|
190 | - { |
|
191 | - return $this->toASN1()->toDER(); |
|
192 | - } |
|
184 | + /** |
|
185 | + * Get certificate as a DER. |
|
186 | + * |
|
187 | + * @return string |
|
188 | + */ |
|
189 | + public function toDER(): string |
|
190 | + { |
|
191 | + return $this->toASN1()->toDER(); |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * Get certificate as a PEM. |
|
196 | - * |
|
197 | - * @return PEM |
|
198 | - */ |
|
199 | - public function toPEM(): PEM |
|
200 | - { |
|
201 | - return new PEM(PEM::TYPE_CERTIFICATE, $this->toDER()); |
|
202 | - } |
|
194 | + /** |
|
195 | + * Get certificate as a PEM. |
|
196 | + * |
|
197 | + * @return PEM |
|
198 | + */ |
|
199 | + public function toPEM(): PEM |
|
200 | + { |
|
201 | + return new PEM(PEM::TYPE_CERTIFICATE, $this->toDER()); |
|
202 | + } |
|
203 | 203 | |
204 | - /** |
|
205 | - * Verify certificate signature. |
|
206 | - * |
|
207 | - * @param PublicKeyInfo $pubkey_info Issuer's public key |
|
208 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
209 | - * |
|
210 | - * @return bool True if certificate signature is valid |
|
211 | - */ |
|
212 | - public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool |
|
213 | - { |
|
214 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
215 | - $data = $this->_tbsCertificate->toASN1()->toDER(); |
|
216 | - return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
217 | - $this->_signatureAlgorithm); |
|
218 | - } |
|
204 | + /** |
|
205 | + * Verify certificate signature. |
|
206 | + * |
|
207 | + * @param PublicKeyInfo $pubkey_info Issuer's public key |
|
208 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
209 | + * |
|
210 | + * @return bool True if certificate signature is valid |
|
211 | + */ |
|
212 | + public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool |
|
213 | + { |
|
214 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
215 | + $data = $this->_tbsCertificate->toASN1()->toDER(); |
|
216 | + return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
217 | + $this->_signatureAlgorithm); |
|
218 | + } |
|
219 | 219 | |
220 | - /** |
|
221 | - * Check whether certificate has serial number equal to another. |
|
222 | - * |
|
223 | - * @param Certificate $cert |
|
224 | - * |
|
225 | - * @return bool |
|
226 | - */ |
|
227 | - private function _hasEqualSerialNumber(Certificate $cert): bool |
|
228 | - { |
|
229 | - $sn1 = $this->_tbsCertificate->serialNumber(); |
|
230 | - $sn2 = $cert->_tbsCertificate->serialNumber(); |
|
231 | - return $sn1 == $sn2; |
|
232 | - } |
|
220 | + /** |
|
221 | + * Check whether certificate has serial number equal to another. |
|
222 | + * |
|
223 | + * @param Certificate $cert |
|
224 | + * |
|
225 | + * @return bool |
|
226 | + */ |
|
227 | + private function _hasEqualSerialNumber(Certificate $cert): bool |
|
228 | + { |
|
229 | + $sn1 = $this->_tbsCertificate->serialNumber(); |
|
230 | + $sn2 = $cert->_tbsCertificate->serialNumber(); |
|
231 | + return $sn1 == $sn2; |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * Check whether certificate has public key equal to another. |
|
236 | - * |
|
237 | - * @param Certificate $cert |
|
238 | - * |
|
239 | - * @return bool |
|
240 | - */ |
|
241 | - private function _hasEqualPublicKey(Certificate $cert): bool |
|
242 | - { |
|
243 | - $kid1 = $this->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
244 | - $kid2 = $cert->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
245 | - return $kid1 == $kid2; |
|
246 | - } |
|
234 | + /** |
|
235 | + * Check whether certificate has public key equal to another. |
|
236 | + * |
|
237 | + * @param Certificate $cert |
|
238 | + * |
|
239 | + * @return bool |
|
240 | + */ |
|
241 | + private function _hasEqualPublicKey(Certificate $cert): bool |
|
242 | + { |
|
243 | + $kid1 = $this->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
244 | + $kid2 = $cert->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
245 | + return $kid1 == $kid2; |
|
246 | + } |
|
247 | 247 | |
248 | - /** |
|
249 | - * Check whether certificate has subject equal to another. |
|
250 | - * |
|
251 | - * @param Certificate $cert |
|
252 | - * |
|
253 | - * @return bool |
|
254 | - */ |
|
255 | - private function _hasEqualSubject(Certificate $cert): bool |
|
256 | - { |
|
257 | - $dn1 = $this->_tbsCertificate->subject(); |
|
258 | - $dn2 = $cert->_tbsCertificate->subject(); |
|
259 | - return $dn1->equals($dn2); |
|
260 | - } |
|
248 | + /** |
|
249 | + * Check whether certificate has subject equal to another. |
|
250 | + * |
|
251 | + * @param Certificate $cert |
|
252 | + * |
|
253 | + * @return bool |
|
254 | + */ |
|
255 | + private function _hasEqualSubject(Certificate $cert): bool |
|
256 | + { |
|
257 | + $dn1 = $this->_tbsCertificate->subject(); |
|
258 | + $dn2 = $cert->_tbsCertificate->subject(); |
|
259 | + return $dn1->equals($dn2); |
|
260 | + } |
|
261 | 261 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate; |
6 | 6 |
@@ -17,108 +17,108 @@ |
||
17 | 17 | */ |
18 | 18 | class Time |
19 | 19 | { |
20 | - use DateTimeHelper; |
|
20 | + use DateTimeHelper; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Datetime. |
|
24 | - * |
|
25 | - * @var \DateTimeImmutable |
|
26 | - */ |
|
27 | - protected $_dt; |
|
22 | + /** |
|
23 | + * Datetime. |
|
24 | + * |
|
25 | + * @var \DateTimeImmutable |
|
26 | + */ |
|
27 | + protected $_dt; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Time ASN.1 type tag. |
|
31 | - * |
|
32 | - * @var int |
|
33 | - */ |
|
34 | - protected $_type; |
|
29 | + /** |
|
30 | + * Time ASN.1 type tag. |
|
31 | + * |
|
32 | + * @var int |
|
33 | + */ |
|
34 | + protected $_type; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Constructor. |
|
38 | - * |
|
39 | - * @param \DateTimeImmutable $dt |
|
40 | - */ |
|
41 | - public function __construct(\DateTimeImmutable $dt) |
|
42 | - { |
|
43 | - $this->_dt = $dt; |
|
44 | - $this->_type = self::_determineType($dt); |
|
45 | - } |
|
36 | + /** |
|
37 | + * Constructor. |
|
38 | + * |
|
39 | + * @param \DateTimeImmutable $dt |
|
40 | + */ |
|
41 | + public function __construct(\DateTimeImmutable $dt) |
|
42 | + { |
|
43 | + $this->_dt = $dt; |
|
44 | + $this->_type = self::_determineType($dt); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Initialize from ASN.1. |
|
49 | - * |
|
50 | - * @param TimeType $el |
|
51 | - * |
|
52 | - * @return self |
|
53 | - */ |
|
54 | - public static function fromASN1(TimeType $el): self |
|
55 | - { |
|
56 | - $obj = new self($el->dateTime()); |
|
57 | - $obj->_type = $el->tag(); |
|
58 | - return $obj; |
|
59 | - } |
|
47 | + /** |
|
48 | + * Initialize from ASN.1. |
|
49 | + * |
|
50 | + * @param TimeType $el |
|
51 | + * |
|
52 | + * @return self |
|
53 | + */ |
|
54 | + public static function fromASN1(TimeType $el): self |
|
55 | + { |
|
56 | + $obj = new self($el->dateTime()); |
|
57 | + $obj->_type = $el->tag(); |
|
58 | + return $obj; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Initialize from date string. |
|
63 | - * |
|
64 | - * @param null|string $time |
|
65 | - * @param null|string $tz |
|
66 | - * |
|
67 | - * @return self |
|
68 | - */ |
|
69 | - public static function fromString(?string $time, ?string $tz = null): self |
|
70 | - { |
|
71 | - return new self(self::_createDateTime($time, $tz)); |
|
72 | - } |
|
61 | + /** |
|
62 | + * Initialize from date string. |
|
63 | + * |
|
64 | + * @param null|string $time |
|
65 | + * @param null|string $tz |
|
66 | + * |
|
67 | + * @return self |
|
68 | + */ |
|
69 | + public static function fromString(?string $time, ?string $tz = null): self |
|
70 | + { |
|
71 | + return new self(self::_createDateTime($time, $tz)); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Get datetime. |
|
76 | - * |
|
77 | - * @return \DateTimeImmutable |
|
78 | - */ |
|
79 | - public function dateTime(): \DateTimeImmutable |
|
80 | - { |
|
81 | - return $this->_dt; |
|
82 | - } |
|
74 | + /** |
|
75 | + * Get datetime. |
|
76 | + * |
|
77 | + * @return \DateTimeImmutable |
|
78 | + */ |
|
79 | + public function dateTime(): \DateTimeImmutable |
|
80 | + { |
|
81 | + return $this->_dt; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Generate ASN.1. |
|
86 | - * |
|
87 | - * @throws \UnexpectedValueException |
|
88 | - * |
|
89 | - * @return TimeType |
|
90 | - */ |
|
91 | - public function toASN1(): TimeType |
|
92 | - { |
|
93 | - $dt = $this->_dt; |
|
94 | - switch ($this->_type) { |
|
95 | - case Element::TYPE_UTC_TIME: |
|
96 | - return new UTCTime($dt); |
|
97 | - case Element::TYPE_GENERALIZED_TIME: |
|
98 | - // GeneralizedTime must not contain fractional seconds |
|
99 | - // (rfc5280 4.1.2.5.2) |
|
100 | - if (0 != $dt->format('u')) { |
|
101 | - // remove fractional seconds (round down) |
|
102 | - $dt = self::_roundDownFractionalSeconds($dt); |
|
103 | - } |
|
104 | - return new GeneralizedTime($dt); |
|
105 | - } |
|
106 | - throw new \UnexpectedValueException( |
|
107 | - 'Time type ' . Element::tagToName($this->_type) . ' not supported.'); |
|
108 | - } |
|
84 | + /** |
|
85 | + * Generate ASN.1. |
|
86 | + * |
|
87 | + * @throws \UnexpectedValueException |
|
88 | + * |
|
89 | + * @return TimeType |
|
90 | + */ |
|
91 | + public function toASN1(): TimeType |
|
92 | + { |
|
93 | + $dt = $this->_dt; |
|
94 | + switch ($this->_type) { |
|
95 | + case Element::TYPE_UTC_TIME: |
|
96 | + return new UTCTime($dt); |
|
97 | + case Element::TYPE_GENERALIZED_TIME: |
|
98 | + // GeneralizedTime must not contain fractional seconds |
|
99 | + // (rfc5280 4.1.2.5.2) |
|
100 | + if (0 != $dt->format('u')) { |
|
101 | + // remove fractional seconds (round down) |
|
102 | + $dt = self::_roundDownFractionalSeconds($dt); |
|
103 | + } |
|
104 | + return new GeneralizedTime($dt); |
|
105 | + } |
|
106 | + throw new \UnexpectedValueException( |
|
107 | + 'Time type ' . Element::tagToName($this->_type) . ' not supported.'); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Determine whether to use UTCTime or GeneralizedTime ASN.1 type. |
|
112 | - * |
|
113 | - * @param \DateTimeImmutable $dt |
|
114 | - * |
|
115 | - * @return int Type tag |
|
116 | - */ |
|
117 | - protected static function _determineType(\DateTimeImmutable $dt): int |
|
118 | - { |
|
119 | - if ($dt->format('Y') >= 2050) { |
|
120 | - return Element::TYPE_GENERALIZED_TIME; |
|
121 | - } |
|
122 | - return Element::TYPE_UTC_TIME; |
|
123 | - } |
|
110 | + /** |
|
111 | + * Determine whether to use UTCTime or GeneralizedTime ASN.1 type. |
|
112 | + * |
|
113 | + * @param \DateTimeImmutable $dt |
|
114 | + * |
|
115 | + * @return int Type tag |
|
116 | + */ |
|
117 | + protected static function _determineType(\DateTimeImmutable $dt): int |
|
118 | + { |
|
119 | + if ($dt->format('Y') >= 2050) { |
|
120 | + return Element::TYPE_GENERALIZED_TIME; |
|
121 | + } |
|
122 | + return Element::TYPE_UTC_TIME; |
|
123 | + } |
|
124 | 124 | } |
@@ -92,16 +92,16 @@ |
||
92 | 92 | { |
93 | 93 | $dt = $this->_dt; |
94 | 94 | switch ($this->_type) { |
95 | - case Element::TYPE_UTC_TIME: |
|
96 | - return new UTCTime($dt); |
|
97 | - case Element::TYPE_GENERALIZED_TIME: |
|
98 | - // GeneralizedTime must not contain fractional seconds |
|
99 | - // (rfc5280 4.1.2.5.2) |
|
100 | - if (0 != $dt->format('u')) { |
|
101 | - // remove fractional seconds (round down) |
|
102 | - $dt = self::_roundDownFractionalSeconds($dt); |
|
103 | - } |
|
104 | - return new GeneralizedTime($dt); |
|
95 | + case Element::TYPE_UTC_TIME: |
|
96 | + return new UTCTime($dt); |
|
97 | + case Element::TYPE_GENERALIZED_TIME: |
|
98 | + // GeneralizedTime must not contain fractional seconds |
|
99 | + // (rfc5280 4.1.2.5.2) |
|
100 | + if (0 != $dt->format('u')) { |
|
101 | + // remove fractional seconds (round down) |
|
102 | + $dt = self::_roundDownFractionalSeconds($dt); |
|
103 | + } |
|
104 | + return new GeneralizedTime($dt); |
|
105 | 105 | } |
106 | 106 | throw new \UnexpectedValueException( |
107 | 107 | 'Time type ' . Element::tagToName($this->_type) . ' not supported.'); |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate; |
6 | 6 |
@@ -16,139 +16,139 @@ |
||
16 | 16 | */ |
17 | 17 | class CertificationPathBuilder |
18 | 18 | { |
19 | - /** |
|
20 | - * Trust anchors. |
|
21 | - * |
|
22 | - * @var CertificateBundle |
|
23 | - */ |
|
24 | - protected $_trustList; |
|
19 | + /** |
|
20 | + * Trust anchors. |
|
21 | + * |
|
22 | + * @var CertificateBundle |
|
23 | + */ |
|
24 | + protected $_trustList; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Constructor. |
|
28 | - * |
|
29 | - * @param CertificateBundle $trust_list List of trust anchors |
|
30 | - */ |
|
31 | - public function __construct(CertificateBundle $trust_list) |
|
32 | - { |
|
33 | - $this->_trustList = $trust_list; |
|
34 | - } |
|
26 | + /** |
|
27 | + * Constructor. |
|
28 | + * |
|
29 | + * @param CertificateBundle $trust_list List of trust anchors |
|
30 | + */ |
|
31 | + public function __construct(CertificateBundle $trust_list) |
|
32 | + { |
|
33 | + $this->_trustList = $trust_list; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Get all certification paths to given target certificate from |
|
38 | - * any trust anchor. |
|
39 | - * |
|
40 | - * @param Certificate $target Target certificate |
|
41 | - * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
42 | - * |
|
43 | - * @return CertificationPath[] |
|
44 | - */ |
|
45 | - public function allPathsToTarget(Certificate $target, |
|
46 | - ?CertificateBundle $intermediate = null): array |
|
47 | - { |
|
48 | - $paths = $this->_resolvePathsToTarget($target, $intermediate); |
|
49 | - // map paths to CertificationPath objects |
|
50 | - return array_map( |
|
51 | - function ($certs) { |
|
52 | - return new CertificationPath(...$certs); |
|
53 | - }, $paths); |
|
54 | - } |
|
36 | + /** |
|
37 | + * Get all certification paths to given target certificate from |
|
38 | + * any trust anchor. |
|
39 | + * |
|
40 | + * @param Certificate $target Target certificate |
|
41 | + * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
42 | + * |
|
43 | + * @return CertificationPath[] |
|
44 | + */ |
|
45 | + public function allPathsToTarget(Certificate $target, |
|
46 | + ?CertificateBundle $intermediate = null): array |
|
47 | + { |
|
48 | + $paths = $this->_resolvePathsToTarget($target, $intermediate); |
|
49 | + // map paths to CertificationPath objects |
|
50 | + return array_map( |
|
51 | + function ($certs) { |
|
52 | + return new CertificationPath(...$certs); |
|
53 | + }, $paths); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get shortest path to given target certificate from any trust anchor. |
|
58 | - * |
|
59 | - * @param Certificate $target Target certificate |
|
60 | - * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
61 | - * |
|
62 | - * @throws PathBuildingException |
|
63 | - * |
|
64 | - * @return CertificationPath |
|
65 | - */ |
|
66 | - public function shortestPathToTarget(Certificate $target, |
|
67 | - ?CertificateBundle $intermediate = null): CertificationPath |
|
68 | - { |
|
69 | - $paths = $this->allPathsToTarget($target, $intermediate); |
|
70 | - if (!count($paths)) { |
|
71 | - throw new PathBuildingException('No certification paths.'); |
|
72 | - } |
|
73 | - usort($paths, |
|
74 | - function ($a, $b) { |
|
75 | - return count($a) < count($b) ? -1 : 1; |
|
76 | - }); |
|
77 | - return reset($paths); |
|
78 | - } |
|
56 | + /** |
|
57 | + * Get shortest path to given target certificate from any trust anchor. |
|
58 | + * |
|
59 | + * @param Certificate $target Target certificate |
|
60 | + * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
61 | + * |
|
62 | + * @throws PathBuildingException |
|
63 | + * |
|
64 | + * @return CertificationPath |
|
65 | + */ |
|
66 | + public function shortestPathToTarget(Certificate $target, |
|
67 | + ?CertificateBundle $intermediate = null): CertificationPath |
|
68 | + { |
|
69 | + $paths = $this->allPathsToTarget($target, $intermediate); |
|
70 | + if (!count($paths)) { |
|
71 | + throw new PathBuildingException('No certification paths.'); |
|
72 | + } |
|
73 | + usort($paths, |
|
74 | + function ($a, $b) { |
|
75 | + return count($a) < count($b) ? -1 : 1; |
|
76 | + }); |
|
77 | + return reset($paths); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Find all issuers of the target certificate from a given bundle. |
|
82 | - * |
|
83 | - * @param Certificate $target Target certificate |
|
84 | - * @param CertificateBundle $bundle Certificates to search |
|
85 | - * |
|
86 | - * @return Certificate[] |
|
87 | - */ |
|
88 | - protected function _findIssuers(Certificate $target, |
|
89 | - CertificateBundle $bundle): array |
|
90 | - { |
|
91 | - $issuers = []; |
|
92 | - $issuer_name = $target->tbsCertificate()->issuer(); |
|
93 | - $extensions = $target->tbsCertificate()->extensions(); |
|
94 | - // find by authority key identifier |
|
95 | - if ($extensions->hasAuthorityKeyIdentifier()) { |
|
96 | - $ext = $extensions->authorityKeyIdentifier(); |
|
97 | - if ($ext->hasKeyIdentifier()) { |
|
98 | - foreach ($bundle->allBySubjectKeyIdentifier( |
|
99 | - $ext->keyIdentifier()) as $issuer) { |
|
100 | - // check that issuer name matches |
|
101 | - if ($issuer->tbsCertificate()->subject()->equals($issuer_name)) { |
|
102 | - $issuers[] = $issuer; |
|
103 | - } |
|
104 | - } |
|
105 | - } |
|
106 | - } |
|
107 | - return $issuers; |
|
108 | - } |
|
80 | + /** |
|
81 | + * Find all issuers of the target certificate from a given bundle. |
|
82 | + * |
|
83 | + * @param Certificate $target Target certificate |
|
84 | + * @param CertificateBundle $bundle Certificates to search |
|
85 | + * |
|
86 | + * @return Certificate[] |
|
87 | + */ |
|
88 | + protected function _findIssuers(Certificate $target, |
|
89 | + CertificateBundle $bundle): array |
|
90 | + { |
|
91 | + $issuers = []; |
|
92 | + $issuer_name = $target->tbsCertificate()->issuer(); |
|
93 | + $extensions = $target->tbsCertificate()->extensions(); |
|
94 | + // find by authority key identifier |
|
95 | + if ($extensions->hasAuthorityKeyIdentifier()) { |
|
96 | + $ext = $extensions->authorityKeyIdentifier(); |
|
97 | + if ($ext->hasKeyIdentifier()) { |
|
98 | + foreach ($bundle->allBySubjectKeyIdentifier( |
|
99 | + $ext->keyIdentifier()) as $issuer) { |
|
100 | + // check that issuer name matches |
|
101 | + if ($issuer->tbsCertificate()->subject()->equals($issuer_name)) { |
|
102 | + $issuers[] = $issuer; |
|
103 | + } |
|
104 | + } |
|
105 | + } |
|
106 | + } |
|
107 | + return $issuers; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Resolve all possible certification paths from any trust anchor to |
|
112 | - * the target certificate, using optional intermediate certificates. |
|
113 | - * |
|
114 | - * Helper method for allPathsToTarget to be called recursively. |
|
115 | - * |
|
116 | - * @todo Implement loop detection |
|
117 | - * |
|
118 | - * @param Certificate $target |
|
119 | - * @param null|CertificateBundle $intermediate |
|
120 | - * |
|
121 | - * @return array[] Array of arrays containing path certificates |
|
122 | - */ |
|
123 | - private function _resolvePathsToTarget(Certificate $target, |
|
124 | - ?CertificateBundle $intermediate = null): array |
|
125 | - { |
|
126 | - // array of possible paths |
|
127 | - $paths = []; |
|
128 | - // signed by certificate in the trust list |
|
129 | - foreach ($this->_findIssuers($target, $this->_trustList) as $issuer) { |
|
130 | - // if target is self-signed, path consists of only |
|
131 | - // the target certificate |
|
132 | - if ($target->equals($issuer)) { |
|
133 | - $paths[] = [$target]; |
|
134 | - } else { |
|
135 | - $paths[] = [$issuer, $target]; |
|
136 | - } |
|
137 | - } |
|
138 | - if (isset($intermediate)) { |
|
139 | - // signed by intermediate certificate |
|
140 | - foreach ($this->_findIssuers($target, $intermediate) as $issuer) { |
|
141 | - // intermediate certificate must not be self-signed |
|
142 | - if ($issuer->isSelfIssued()) { |
|
143 | - continue; |
|
144 | - } |
|
145 | - // resolve paths to issuer |
|
146 | - $subpaths = $this->_resolvePathsToTarget($issuer, $intermediate); |
|
147 | - foreach ($subpaths as $path) { |
|
148 | - $paths[] = array_merge($path, [$target]); |
|
149 | - } |
|
150 | - } |
|
151 | - } |
|
152 | - return $paths; |
|
153 | - } |
|
110 | + /** |
|
111 | + * Resolve all possible certification paths from any trust anchor to |
|
112 | + * the target certificate, using optional intermediate certificates. |
|
113 | + * |
|
114 | + * Helper method for allPathsToTarget to be called recursively. |
|
115 | + * |
|
116 | + * @todo Implement loop detection |
|
117 | + * |
|
118 | + * @param Certificate $target |
|
119 | + * @param null|CertificateBundle $intermediate |
|
120 | + * |
|
121 | + * @return array[] Array of arrays containing path certificates |
|
122 | + */ |
|
123 | + private function _resolvePathsToTarget(Certificate $target, |
|
124 | + ?CertificateBundle $intermediate = null): array |
|
125 | + { |
|
126 | + // array of possible paths |
|
127 | + $paths = []; |
|
128 | + // signed by certificate in the trust list |
|
129 | + foreach ($this->_findIssuers($target, $this->_trustList) as $issuer) { |
|
130 | + // if target is self-signed, path consists of only |
|
131 | + // the target certificate |
|
132 | + if ($target->equals($issuer)) { |
|
133 | + $paths[] = [$target]; |
|
134 | + } else { |
|
135 | + $paths[] = [$issuer, $target]; |
|
136 | + } |
|
137 | + } |
|
138 | + if (isset($intermediate)) { |
|
139 | + // signed by intermediate certificate |
|
140 | + foreach ($this->_findIssuers($target, $intermediate) as $issuer) { |
|
141 | + // intermediate certificate must not be self-signed |
|
142 | + if ($issuer->isSelfIssued()) { |
|
143 | + continue; |
|
144 | + } |
|
145 | + // resolve paths to issuer |
|
146 | + $subpaths = $this->_resolvePathsToTarget($issuer, $intermediate); |
|
147 | + foreach ($subpaths as $path) { |
|
148 | + $paths[] = array_merge($path, [$target]); |
|
149 | + } |
|
150 | + } |
|
151 | + } |
|
152 | + return $paths; |
|
153 | + } |
|
154 | 154 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\CertificationPath\PathBuilding; |
6 | 6 | |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | $paths = $this->_resolvePathsToTarget($target, $intermediate); |
49 | 49 | // map paths to CertificationPath objects |
50 | 50 | return array_map( |
51 | - function ($certs) { |
|
51 | + function($certs) { |
|
52 | 52 | return new CertificationPath(...$certs); |
53 | 53 | }, $paths); |
54 | 54 | } |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | throw new PathBuildingException('No certification paths.'); |
72 | 72 | } |
73 | 73 | usort($paths, |
74 | - function ($a, $b) { |
|
74 | + function($a, $b) { |
|
75 | 75 | return count($a) < count($b) ? -1 : 1; |
76 | 76 | }); |
77 | 77 | return reset($paths); |
@@ -20,500 +20,500 @@ |
||
20 | 20 | */ |
21 | 21 | class ValidatorState |
22 | 22 | { |
23 | - /** |
|
24 | - * Length of the certification path (n). |
|
25 | - * |
|
26 | - * @var int |
|
27 | - */ |
|
28 | - protected $_pathLength; |
|
29 | - |
|
30 | - /** |
|
31 | - * Current index in the certification path in the range of 1..n (i). |
|
32 | - * |
|
33 | - * @var int |
|
34 | - */ |
|
35 | - protected $_index; |
|
36 | - |
|
37 | - /** |
|
38 | - * Valid policy tree (valid_policy_tree). |
|
39 | - * |
|
40 | - * A tree of certificate policies with their optional qualifiers. |
|
41 | - * Each of the leaves of the tree represents a valid policy at this stage in |
|
42 | - * the certification path validation. |
|
43 | - * Once the tree is set to NULL, policy processing ceases. |
|
44 | - * |
|
45 | - * @var null|PolicyTree |
|
46 | - */ |
|
47 | - protected $_validPolicyTree; |
|
48 | - |
|
49 | - /** |
|
50 | - * Permitted subtrees (permitted_subtrees). |
|
51 | - * |
|
52 | - * A set of root names for each name type defining a set of subtrees within |
|
53 | - * which all subject names in subsequent certificates in the certification |
|
54 | - * path must fall. |
|
55 | - * |
|
56 | - * @var mixed |
|
57 | - */ |
|
58 | - protected $_permittedSubtrees; |
|
59 | - |
|
60 | - /** |
|
61 | - * Excluded subtrees (excluded_subtrees). |
|
62 | - * |
|
63 | - * A set of root names for each name type defining a set of subtrees within |
|
64 | - * which no subject name in subsequent certificates in the certification |
|
65 | - * path may fall. |
|
66 | - * |
|
67 | - * @var mixed |
|
68 | - */ |
|
69 | - protected $_excludedSubtrees; |
|
70 | - |
|
71 | - /** |
|
72 | - * Explicit policy (explicit_policy). |
|
73 | - * |
|
74 | - * An integer that indicates if a non-NULL valid_policy_tree is required. |
|
75 | - * |
|
76 | - * @var int |
|
77 | - */ |
|
78 | - protected $_explicitPolicy; |
|
79 | - |
|
80 | - /** |
|
81 | - * Inhibit anyPolicy (inhibit_anyPolicy). |
|
82 | - * |
|
83 | - * An integer that indicates whether the anyPolicy policy identifier is |
|
84 | - * considered a match. |
|
85 | - * |
|
86 | - * @var int |
|
87 | - */ |
|
88 | - protected $_inhibitAnyPolicy; |
|
89 | - |
|
90 | - /** |
|
91 | - * Policy mapping (policy_mapping). |
|
92 | - * |
|
93 | - * An integer that indicates if policy mapping is permitted. |
|
94 | - * |
|
95 | - * @var int |
|
96 | - */ |
|
97 | - protected $_policyMapping; |
|
98 | - |
|
99 | - /** |
|
100 | - * Working public key algorithm (working_public_key_algorithm). |
|
101 | - * |
|
102 | - * The digital signature algorithm used to verify the signature of a |
|
103 | - * certificate. |
|
104 | - * |
|
105 | - * @var AlgorithmIdentifierType |
|
106 | - */ |
|
107 | - protected $_workingPublicKeyAlgorithm; |
|
108 | - |
|
109 | - /** |
|
110 | - * Working public key (working_public_key). |
|
111 | - * |
|
112 | - * The public key used to verify the signature of a certificate. |
|
113 | - * |
|
114 | - * @var PublicKeyInfo |
|
115 | - */ |
|
116 | - protected $_workingPublicKey; |
|
117 | - |
|
118 | - /** |
|
119 | - * Working public key parameters (working_public_key_parameters). |
|
120 | - * |
|
121 | - * Parameters associated with the current public key that may be required to |
|
122 | - * verify a signature. |
|
123 | - * |
|
124 | - * @var null|Element |
|
125 | - */ |
|
126 | - protected $_workingPublicKeyParameters; |
|
127 | - |
|
128 | - /** |
|
129 | - * Working issuer name (working_issuer_name). |
|
130 | - * |
|
131 | - * The issuer distinguished name expected in the next certificate in the |
|
132 | - * chain. |
|
133 | - * |
|
134 | - * @var Name |
|
135 | - */ |
|
136 | - protected $_workingIssuerName; |
|
137 | - |
|
138 | - /** |
|
139 | - * Maximum certification path length (max_path_length). |
|
140 | - * |
|
141 | - * @var int |
|
142 | - */ |
|
143 | - protected $_maxPathLength; |
|
144 | - |
|
145 | - /** |
|
146 | - * Constructor. |
|
147 | - */ |
|
148 | - protected function __construct() |
|
149 | - { |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Initialize variables according to RFC 5280 6.1.2. |
|
154 | - * |
|
155 | - * @see https://tools.ietf.org/html/rfc5280#section-6.1.2 |
|
156 | - * |
|
157 | - * @param PathValidationConfig $config |
|
158 | - * @param Certificate $trust_anchor Trust anchor certificate |
|
159 | - * @param int $n Number of certificates |
|
160 | - * in the certification path |
|
161 | - * |
|
162 | - * @return self |
|
163 | - */ |
|
164 | - public static function initialize(PathValidationConfig $config, |
|
165 | - Certificate $trust_anchor, int $n): self |
|
166 | - { |
|
167 | - $state = new self(); |
|
168 | - $state->_pathLength = $n; |
|
169 | - $state->_index = 1; |
|
170 | - $state->_validPolicyTree = new PolicyTree(PolicyNode::anyPolicyNode()); |
|
171 | - $state->_permittedSubtrees = null; |
|
172 | - $state->_excludedSubtrees = null; |
|
173 | - $state->_explicitPolicy = $config->explicitPolicy() ? 0 : $n + 1; |
|
174 | - $state->_inhibitAnyPolicy = $config->anyPolicyInhibit() ? 0 : $n + 1; |
|
175 | - $state->_policyMapping = $config->policyMappingInhibit() ? 0 : $n + 1; |
|
176 | - $state->_workingPublicKeyAlgorithm = $trust_anchor->signatureAlgorithm(); |
|
177 | - $tbsCert = $trust_anchor->tbsCertificate(); |
|
178 | - $state->_workingPublicKey = $tbsCert->subjectPublicKeyInfo(); |
|
179 | - $state->_workingPublicKeyParameters = self::getAlgorithmParameters( |
|
180 | - $state->_workingPublicKey->algorithmIdentifier()); |
|
181 | - $state->_workingIssuerName = $tbsCert->issuer(); |
|
182 | - $state->_maxPathLength = $config->maxLength(); |
|
183 | - return $state; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Get self with current certification path index set. |
|
188 | - * |
|
189 | - * @param int $index |
|
190 | - * |
|
191 | - * @return self |
|
192 | - */ |
|
193 | - public function withIndex(int $index): self |
|
194 | - { |
|
195 | - $state = clone $this; |
|
196 | - $state->_index = $index; |
|
197 | - return $state; |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Get self with valid_policy_tree. |
|
202 | - * |
|
203 | - * @param PolicyTree $policy_tree |
|
204 | - * |
|
205 | - * @return self |
|
206 | - */ |
|
207 | - public function withValidPolicyTree(PolicyTree $policy_tree): self |
|
208 | - { |
|
209 | - $state = clone $this; |
|
210 | - $state->_validPolicyTree = $policy_tree; |
|
211 | - return $state; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Get self with valid_policy_tree set to null. |
|
216 | - * |
|
217 | - * @return self |
|
218 | - */ |
|
219 | - public function withoutValidPolicyTree(): self |
|
220 | - { |
|
221 | - $state = clone $this; |
|
222 | - $state->_validPolicyTree = null; |
|
223 | - return $state; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Get self with explicit_policy. |
|
228 | - * |
|
229 | - * @param int $num |
|
230 | - * |
|
231 | - * @return self |
|
232 | - */ |
|
233 | - public function withExplicitPolicy(int $num): self |
|
234 | - { |
|
235 | - $state = clone $this; |
|
236 | - $state->_explicitPolicy = $num; |
|
237 | - return $state; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Get self with inhibit_anyPolicy. |
|
242 | - * |
|
243 | - * @param int $num |
|
244 | - * |
|
245 | - * @return self |
|
246 | - */ |
|
247 | - public function withInhibitAnyPolicy(int $num): self |
|
248 | - { |
|
249 | - $state = clone $this; |
|
250 | - $state->_inhibitAnyPolicy = $num; |
|
251 | - return $state; |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * Get self with policy_mapping. |
|
256 | - * |
|
257 | - * @param int $num |
|
258 | - * |
|
259 | - * @return self |
|
260 | - */ |
|
261 | - public function withPolicyMapping(int $num): self |
|
262 | - { |
|
263 | - $state = clone $this; |
|
264 | - $state->_policyMapping = $num; |
|
265 | - return $state; |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * Get self with working_public_key_algorithm. |
|
270 | - * |
|
271 | - * @param AlgorithmIdentifierType $algo |
|
272 | - * |
|
273 | - * @return self |
|
274 | - */ |
|
275 | - public function withWorkingPublicKeyAlgorithm(AlgorithmIdentifierType $algo): self |
|
276 | - { |
|
277 | - $state = clone $this; |
|
278 | - $state->_workingPublicKeyAlgorithm = $algo; |
|
279 | - return $state; |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * Get self with working_public_key. |
|
284 | - * |
|
285 | - * @param PublicKeyInfo $pubkey_info |
|
286 | - * |
|
287 | - * @return self |
|
288 | - */ |
|
289 | - public function withWorkingPublicKey(PublicKeyInfo $pubkey_info): self |
|
290 | - { |
|
291 | - $state = clone $this; |
|
292 | - $state->_workingPublicKey = $pubkey_info; |
|
293 | - return $state; |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Get self with working_public_key_parameters. |
|
298 | - * |
|
299 | - * @param null|Element $params |
|
300 | - * |
|
301 | - * @return self |
|
302 | - */ |
|
303 | - public function withWorkingPublicKeyParameters(?Element $params = null): self |
|
304 | - { |
|
305 | - $state = clone $this; |
|
306 | - $state->_workingPublicKeyParameters = $params; |
|
307 | - return $state; |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Get self with working_issuer_name. |
|
312 | - * |
|
313 | - * @param Name $issuer |
|
314 | - * |
|
315 | - * @return self |
|
316 | - */ |
|
317 | - public function withWorkingIssuerName(Name $issuer): self |
|
318 | - { |
|
319 | - $state = clone $this; |
|
320 | - $state->_workingIssuerName = $issuer; |
|
321 | - return $state; |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * Get self with max_path_length. |
|
326 | - * |
|
327 | - * @param int $length |
|
328 | - * |
|
329 | - * @return self |
|
330 | - */ |
|
331 | - public function withMaxPathLength(int $length): self |
|
332 | - { |
|
333 | - $state = clone $this; |
|
334 | - $state->_maxPathLength = $length; |
|
335 | - return $state; |
|
336 | - } |
|
337 | - |
|
338 | - /** |
|
339 | - * Get the certification path length (n). |
|
340 | - * |
|
341 | - * @return int |
|
342 | - */ |
|
343 | - public function pathLength(): int |
|
344 | - { |
|
345 | - return $this->_pathLength; |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * Get the current index in certification path in the range of 1..n. |
|
350 | - * |
|
351 | - * @return int |
|
352 | - */ |
|
353 | - public function index(): int |
|
354 | - { |
|
355 | - return $this->_index; |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Check whether valid_policy_tree is present. |
|
360 | - * |
|
361 | - * @return bool |
|
362 | - */ |
|
363 | - public function hasValidPolicyTree(): bool |
|
364 | - { |
|
365 | - return isset($this->_validPolicyTree); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Get valid_policy_tree. |
|
370 | - * |
|
371 | - * @throws \LogicException If not set |
|
372 | - * |
|
373 | - * @return PolicyTree |
|
374 | - */ |
|
375 | - public function validPolicyTree(): PolicyTree |
|
376 | - { |
|
377 | - if (!$this->hasValidPolicyTree()) { |
|
378 | - throw new \LogicException('valid_policy_tree not set.'); |
|
379 | - } |
|
380 | - return $this->_validPolicyTree; |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * Get permitted_subtrees. |
|
385 | - * |
|
386 | - * @return mixed |
|
387 | - */ |
|
388 | - public function permittedSubtrees() |
|
389 | - { |
|
390 | - return $this->_permittedSubtrees; |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Get excluded_subtrees. |
|
395 | - * |
|
396 | - * @return mixed |
|
397 | - */ |
|
398 | - public function excludedSubtrees() |
|
399 | - { |
|
400 | - return $this->_excludedSubtrees; |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * Get explicit_policy. |
|
405 | - * |
|
406 | - * @return int |
|
407 | - */ |
|
408 | - public function explicitPolicy(): int |
|
409 | - { |
|
410 | - return $this->_explicitPolicy; |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * Get inhibit_anyPolicy. |
|
415 | - * |
|
416 | - * @return int |
|
417 | - */ |
|
418 | - public function inhibitAnyPolicy(): int |
|
419 | - { |
|
420 | - return $this->_inhibitAnyPolicy; |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * Get policy_mapping. |
|
425 | - * |
|
426 | - * @return int |
|
427 | - */ |
|
428 | - public function policyMapping(): int |
|
429 | - { |
|
430 | - return $this->_policyMapping; |
|
431 | - } |
|
432 | - |
|
433 | - /** |
|
434 | - * Get working_public_key_algorithm. |
|
435 | - * |
|
436 | - * @return AlgorithmIdentifierType |
|
437 | - */ |
|
438 | - public function workingPublicKeyAlgorithm(): AlgorithmIdentifierType |
|
439 | - { |
|
440 | - return $this->_workingPublicKeyAlgorithm; |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * Get working_public_key. |
|
445 | - * |
|
446 | - * @return PublicKeyInfo |
|
447 | - */ |
|
448 | - public function workingPublicKey(): PublicKeyInfo |
|
449 | - { |
|
450 | - return $this->_workingPublicKey; |
|
451 | - } |
|
452 | - |
|
453 | - /** |
|
454 | - * Get working_public_key_parameters. |
|
455 | - * |
|
456 | - * @return null|Element |
|
457 | - */ |
|
458 | - public function workingPublicKeyParameters(): ?Element |
|
459 | - { |
|
460 | - return $this->_workingPublicKeyParameters; |
|
461 | - } |
|
462 | - |
|
463 | - /** |
|
464 | - * Get working_issuer_name. |
|
465 | - * |
|
466 | - * @return Name |
|
467 | - */ |
|
468 | - public function workingIssuerName(): Name |
|
469 | - { |
|
470 | - return $this->_workingIssuerName; |
|
471 | - } |
|
472 | - |
|
473 | - /** |
|
474 | - * Get maximum certification path length. |
|
475 | - * |
|
476 | - * @return int |
|
477 | - */ |
|
478 | - public function maxPathLength(): int |
|
479 | - { |
|
480 | - return $this->_maxPathLength; |
|
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Check whether processing the final certificate of the certification path. |
|
485 | - * |
|
486 | - * @return bool |
|
487 | - */ |
|
488 | - public function isFinal(): bool |
|
489 | - { |
|
490 | - return $this->_index === $this->_pathLength; |
|
491 | - } |
|
492 | - |
|
493 | - /** |
|
494 | - * Get the path validation result. |
|
495 | - * |
|
496 | - * @param Certificate[] $certificates Certificates in a certification path |
|
497 | - * |
|
498 | - * @return PathValidationResult |
|
499 | - */ |
|
500 | - public function getResult(array $certificates): PathValidationResult |
|
501 | - { |
|
502 | - return new PathValidationResult($certificates, $this->_validPolicyTree, |
|
503 | - $this->_workingPublicKey, $this->_workingPublicKeyAlgorithm, |
|
504 | - $this->_workingPublicKeyParameters); |
|
505 | - } |
|
506 | - |
|
507 | - /** |
|
508 | - * Get ASN.1 parameters from algorithm identifier. |
|
509 | - * |
|
510 | - * @param AlgorithmIdentifierType $algo |
|
511 | - * |
|
512 | - * @return null|Element ASN.1 element or null if parameters are omitted |
|
513 | - */ |
|
514 | - public static function getAlgorithmParameters(AlgorithmIdentifierType $algo): ?Element |
|
515 | - { |
|
516 | - $seq = $algo->toASN1(); |
|
517 | - return $seq->has(1) ? $seq->at(1)->asElement() : null; |
|
518 | - } |
|
23 | + /** |
|
24 | + * Length of the certification path (n). |
|
25 | + * |
|
26 | + * @var int |
|
27 | + */ |
|
28 | + protected $_pathLength; |
|
29 | + |
|
30 | + /** |
|
31 | + * Current index in the certification path in the range of 1..n (i). |
|
32 | + * |
|
33 | + * @var int |
|
34 | + */ |
|
35 | + protected $_index; |
|
36 | + |
|
37 | + /** |
|
38 | + * Valid policy tree (valid_policy_tree). |
|
39 | + * |
|
40 | + * A tree of certificate policies with their optional qualifiers. |
|
41 | + * Each of the leaves of the tree represents a valid policy at this stage in |
|
42 | + * the certification path validation. |
|
43 | + * Once the tree is set to NULL, policy processing ceases. |
|
44 | + * |
|
45 | + * @var null|PolicyTree |
|
46 | + */ |
|
47 | + protected $_validPolicyTree; |
|
48 | + |
|
49 | + /** |
|
50 | + * Permitted subtrees (permitted_subtrees). |
|
51 | + * |
|
52 | + * A set of root names for each name type defining a set of subtrees within |
|
53 | + * which all subject names in subsequent certificates in the certification |
|
54 | + * path must fall. |
|
55 | + * |
|
56 | + * @var mixed |
|
57 | + */ |
|
58 | + protected $_permittedSubtrees; |
|
59 | + |
|
60 | + /** |
|
61 | + * Excluded subtrees (excluded_subtrees). |
|
62 | + * |
|
63 | + * A set of root names for each name type defining a set of subtrees within |
|
64 | + * which no subject name in subsequent certificates in the certification |
|
65 | + * path may fall. |
|
66 | + * |
|
67 | + * @var mixed |
|
68 | + */ |
|
69 | + protected $_excludedSubtrees; |
|
70 | + |
|
71 | + /** |
|
72 | + * Explicit policy (explicit_policy). |
|
73 | + * |
|
74 | + * An integer that indicates if a non-NULL valid_policy_tree is required. |
|
75 | + * |
|
76 | + * @var int |
|
77 | + */ |
|
78 | + protected $_explicitPolicy; |
|
79 | + |
|
80 | + /** |
|
81 | + * Inhibit anyPolicy (inhibit_anyPolicy). |
|
82 | + * |
|
83 | + * An integer that indicates whether the anyPolicy policy identifier is |
|
84 | + * considered a match. |
|
85 | + * |
|
86 | + * @var int |
|
87 | + */ |
|
88 | + protected $_inhibitAnyPolicy; |
|
89 | + |
|
90 | + /** |
|
91 | + * Policy mapping (policy_mapping). |
|
92 | + * |
|
93 | + * An integer that indicates if policy mapping is permitted. |
|
94 | + * |
|
95 | + * @var int |
|
96 | + */ |
|
97 | + protected $_policyMapping; |
|
98 | + |
|
99 | + /** |
|
100 | + * Working public key algorithm (working_public_key_algorithm). |
|
101 | + * |
|
102 | + * The digital signature algorithm used to verify the signature of a |
|
103 | + * certificate. |
|
104 | + * |
|
105 | + * @var AlgorithmIdentifierType |
|
106 | + */ |
|
107 | + protected $_workingPublicKeyAlgorithm; |
|
108 | + |
|
109 | + /** |
|
110 | + * Working public key (working_public_key). |
|
111 | + * |
|
112 | + * The public key used to verify the signature of a certificate. |
|
113 | + * |
|
114 | + * @var PublicKeyInfo |
|
115 | + */ |
|
116 | + protected $_workingPublicKey; |
|
117 | + |
|
118 | + /** |
|
119 | + * Working public key parameters (working_public_key_parameters). |
|
120 | + * |
|
121 | + * Parameters associated with the current public key that may be required to |
|
122 | + * verify a signature. |
|
123 | + * |
|
124 | + * @var null|Element |
|
125 | + */ |
|
126 | + protected $_workingPublicKeyParameters; |
|
127 | + |
|
128 | + /** |
|
129 | + * Working issuer name (working_issuer_name). |
|
130 | + * |
|
131 | + * The issuer distinguished name expected in the next certificate in the |
|
132 | + * chain. |
|
133 | + * |
|
134 | + * @var Name |
|
135 | + */ |
|
136 | + protected $_workingIssuerName; |
|
137 | + |
|
138 | + /** |
|
139 | + * Maximum certification path length (max_path_length). |
|
140 | + * |
|
141 | + * @var int |
|
142 | + */ |
|
143 | + protected $_maxPathLength; |
|
144 | + |
|
145 | + /** |
|
146 | + * Constructor. |
|
147 | + */ |
|
148 | + protected function __construct() |
|
149 | + { |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Initialize variables according to RFC 5280 6.1.2. |
|
154 | + * |
|
155 | + * @see https://tools.ietf.org/html/rfc5280#section-6.1.2 |
|
156 | + * |
|
157 | + * @param PathValidationConfig $config |
|
158 | + * @param Certificate $trust_anchor Trust anchor certificate |
|
159 | + * @param int $n Number of certificates |
|
160 | + * in the certification path |
|
161 | + * |
|
162 | + * @return self |
|
163 | + */ |
|
164 | + public static function initialize(PathValidationConfig $config, |
|
165 | + Certificate $trust_anchor, int $n): self |
|
166 | + { |
|
167 | + $state = new self(); |
|
168 | + $state->_pathLength = $n; |
|
169 | + $state->_index = 1; |
|
170 | + $state->_validPolicyTree = new PolicyTree(PolicyNode::anyPolicyNode()); |
|
171 | + $state->_permittedSubtrees = null; |
|
172 | + $state->_excludedSubtrees = null; |
|
173 | + $state->_explicitPolicy = $config->explicitPolicy() ? 0 : $n + 1; |
|
174 | + $state->_inhibitAnyPolicy = $config->anyPolicyInhibit() ? 0 : $n + 1; |
|
175 | + $state->_policyMapping = $config->policyMappingInhibit() ? 0 : $n + 1; |
|
176 | + $state->_workingPublicKeyAlgorithm = $trust_anchor->signatureAlgorithm(); |
|
177 | + $tbsCert = $trust_anchor->tbsCertificate(); |
|
178 | + $state->_workingPublicKey = $tbsCert->subjectPublicKeyInfo(); |
|
179 | + $state->_workingPublicKeyParameters = self::getAlgorithmParameters( |
|
180 | + $state->_workingPublicKey->algorithmIdentifier()); |
|
181 | + $state->_workingIssuerName = $tbsCert->issuer(); |
|
182 | + $state->_maxPathLength = $config->maxLength(); |
|
183 | + return $state; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Get self with current certification path index set. |
|
188 | + * |
|
189 | + * @param int $index |
|
190 | + * |
|
191 | + * @return self |
|
192 | + */ |
|
193 | + public function withIndex(int $index): self |
|
194 | + { |
|
195 | + $state = clone $this; |
|
196 | + $state->_index = $index; |
|
197 | + return $state; |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Get self with valid_policy_tree. |
|
202 | + * |
|
203 | + * @param PolicyTree $policy_tree |
|
204 | + * |
|
205 | + * @return self |
|
206 | + */ |
|
207 | + public function withValidPolicyTree(PolicyTree $policy_tree): self |
|
208 | + { |
|
209 | + $state = clone $this; |
|
210 | + $state->_validPolicyTree = $policy_tree; |
|
211 | + return $state; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Get self with valid_policy_tree set to null. |
|
216 | + * |
|
217 | + * @return self |
|
218 | + */ |
|
219 | + public function withoutValidPolicyTree(): self |
|
220 | + { |
|
221 | + $state = clone $this; |
|
222 | + $state->_validPolicyTree = null; |
|
223 | + return $state; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Get self with explicit_policy. |
|
228 | + * |
|
229 | + * @param int $num |
|
230 | + * |
|
231 | + * @return self |
|
232 | + */ |
|
233 | + public function withExplicitPolicy(int $num): self |
|
234 | + { |
|
235 | + $state = clone $this; |
|
236 | + $state->_explicitPolicy = $num; |
|
237 | + return $state; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Get self with inhibit_anyPolicy. |
|
242 | + * |
|
243 | + * @param int $num |
|
244 | + * |
|
245 | + * @return self |
|
246 | + */ |
|
247 | + public function withInhibitAnyPolicy(int $num): self |
|
248 | + { |
|
249 | + $state = clone $this; |
|
250 | + $state->_inhibitAnyPolicy = $num; |
|
251 | + return $state; |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * Get self with policy_mapping. |
|
256 | + * |
|
257 | + * @param int $num |
|
258 | + * |
|
259 | + * @return self |
|
260 | + */ |
|
261 | + public function withPolicyMapping(int $num): self |
|
262 | + { |
|
263 | + $state = clone $this; |
|
264 | + $state->_policyMapping = $num; |
|
265 | + return $state; |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * Get self with working_public_key_algorithm. |
|
270 | + * |
|
271 | + * @param AlgorithmIdentifierType $algo |
|
272 | + * |
|
273 | + * @return self |
|
274 | + */ |
|
275 | + public function withWorkingPublicKeyAlgorithm(AlgorithmIdentifierType $algo): self |
|
276 | + { |
|
277 | + $state = clone $this; |
|
278 | + $state->_workingPublicKeyAlgorithm = $algo; |
|
279 | + return $state; |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * Get self with working_public_key. |
|
284 | + * |
|
285 | + * @param PublicKeyInfo $pubkey_info |
|
286 | + * |
|
287 | + * @return self |
|
288 | + */ |
|
289 | + public function withWorkingPublicKey(PublicKeyInfo $pubkey_info): self |
|
290 | + { |
|
291 | + $state = clone $this; |
|
292 | + $state->_workingPublicKey = $pubkey_info; |
|
293 | + return $state; |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Get self with working_public_key_parameters. |
|
298 | + * |
|
299 | + * @param null|Element $params |
|
300 | + * |
|
301 | + * @return self |
|
302 | + */ |
|
303 | + public function withWorkingPublicKeyParameters(?Element $params = null): self |
|
304 | + { |
|
305 | + $state = clone $this; |
|
306 | + $state->_workingPublicKeyParameters = $params; |
|
307 | + return $state; |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Get self with working_issuer_name. |
|
312 | + * |
|
313 | + * @param Name $issuer |
|
314 | + * |
|
315 | + * @return self |
|
316 | + */ |
|
317 | + public function withWorkingIssuerName(Name $issuer): self |
|
318 | + { |
|
319 | + $state = clone $this; |
|
320 | + $state->_workingIssuerName = $issuer; |
|
321 | + return $state; |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * Get self with max_path_length. |
|
326 | + * |
|
327 | + * @param int $length |
|
328 | + * |
|
329 | + * @return self |
|
330 | + */ |
|
331 | + public function withMaxPathLength(int $length): self |
|
332 | + { |
|
333 | + $state = clone $this; |
|
334 | + $state->_maxPathLength = $length; |
|
335 | + return $state; |
|
336 | + } |
|
337 | + |
|
338 | + /** |
|
339 | + * Get the certification path length (n). |
|
340 | + * |
|
341 | + * @return int |
|
342 | + */ |
|
343 | + public function pathLength(): int |
|
344 | + { |
|
345 | + return $this->_pathLength; |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * Get the current index in certification path in the range of 1..n. |
|
350 | + * |
|
351 | + * @return int |
|
352 | + */ |
|
353 | + public function index(): int |
|
354 | + { |
|
355 | + return $this->_index; |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Check whether valid_policy_tree is present. |
|
360 | + * |
|
361 | + * @return bool |
|
362 | + */ |
|
363 | + public function hasValidPolicyTree(): bool |
|
364 | + { |
|
365 | + return isset($this->_validPolicyTree); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Get valid_policy_tree. |
|
370 | + * |
|
371 | + * @throws \LogicException If not set |
|
372 | + * |
|
373 | + * @return PolicyTree |
|
374 | + */ |
|
375 | + public function validPolicyTree(): PolicyTree |
|
376 | + { |
|
377 | + if (!$this->hasValidPolicyTree()) { |
|
378 | + throw new \LogicException('valid_policy_tree not set.'); |
|
379 | + } |
|
380 | + return $this->_validPolicyTree; |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * Get permitted_subtrees. |
|
385 | + * |
|
386 | + * @return mixed |
|
387 | + */ |
|
388 | + public function permittedSubtrees() |
|
389 | + { |
|
390 | + return $this->_permittedSubtrees; |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Get excluded_subtrees. |
|
395 | + * |
|
396 | + * @return mixed |
|
397 | + */ |
|
398 | + public function excludedSubtrees() |
|
399 | + { |
|
400 | + return $this->_excludedSubtrees; |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * Get explicit_policy. |
|
405 | + * |
|
406 | + * @return int |
|
407 | + */ |
|
408 | + public function explicitPolicy(): int |
|
409 | + { |
|
410 | + return $this->_explicitPolicy; |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * Get inhibit_anyPolicy. |
|
415 | + * |
|
416 | + * @return int |
|
417 | + */ |
|
418 | + public function inhibitAnyPolicy(): int |
|
419 | + { |
|
420 | + return $this->_inhibitAnyPolicy; |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * Get policy_mapping. |
|
425 | + * |
|
426 | + * @return int |
|
427 | + */ |
|
428 | + public function policyMapping(): int |
|
429 | + { |
|
430 | + return $this->_policyMapping; |
|
431 | + } |
|
432 | + |
|
433 | + /** |
|
434 | + * Get working_public_key_algorithm. |
|
435 | + * |
|
436 | + * @return AlgorithmIdentifierType |
|
437 | + */ |
|
438 | + public function workingPublicKeyAlgorithm(): AlgorithmIdentifierType |
|
439 | + { |
|
440 | + return $this->_workingPublicKeyAlgorithm; |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * Get working_public_key. |
|
445 | + * |
|
446 | + * @return PublicKeyInfo |
|
447 | + */ |
|
448 | + public function workingPublicKey(): PublicKeyInfo |
|
449 | + { |
|
450 | + return $this->_workingPublicKey; |
|
451 | + } |
|
452 | + |
|
453 | + /** |
|
454 | + * Get working_public_key_parameters. |
|
455 | + * |
|
456 | + * @return null|Element |
|
457 | + */ |
|
458 | + public function workingPublicKeyParameters(): ?Element |
|
459 | + { |
|
460 | + return $this->_workingPublicKeyParameters; |
|
461 | + } |
|
462 | + |
|
463 | + /** |
|
464 | + * Get working_issuer_name. |
|
465 | + * |
|
466 | + * @return Name |
|
467 | + */ |
|
468 | + public function workingIssuerName(): Name |
|
469 | + { |
|
470 | + return $this->_workingIssuerName; |
|
471 | + } |
|
472 | + |
|
473 | + /** |
|
474 | + * Get maximum certification path length. |
|
475 | + * |
|
476 | + * @return int |
|
477 | + */ |
|
478 | + public function maxPathLength(): int |
|
479 | + { |
|
480 | + return $this->_maxPathLength; |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Check whether processing the final certificate of the certification path. |
|
485 | + * |
|
486 | + * @return bool |
|
487 | + */ |
|
488 | + public function isFinal(): bool |
|
489 | + { |
|
490 | + return $this->_index === $this->_pathLength; |
|
491 | + } |
|
492 | + |
|
493 | + /** |
|
494 | + * Get the path validation result. |
|
495 | + * |
|
496 | + * @param Certificate[] $certificates Certificates in a certification path |
|
497 | + * |
|
498 | + * @return PathValidationResult |
|
499 | + */ |
|
500 | + public function getResult(array $certificates): PathValidationResult |
|
501 | + { |
|
502 | + return new PathValidationResult($certificates, $this->_validPolicyTree, |
|
503 | + $this->_workingPublicKey, $this->_workingPublicKeyAlgorithm, |
|
504 | + $this->_workingPublicKeyParameters); |
|
505 | + } |
|
506 | + |
|
507 | + /** |
|
508 | + * Get ASN.1 parameters from algorithm identifier. |
|
509 | + * |
|
510 | + * @param AlgorithmIdentifierType $algo |
|
511 | + * |
|
512 | + * @return null|Element ASN.1 element or null if parameters are omitted |
|
513 | + */ |
|
514 | + public static function getAlgorithmParameters(AlgorithmIdentifierType $algo): ?Element |
|
515 | + { |
|
516 | + $seq = $algo->toASN1(); |
|
517 | + return $seq->has(1) ? $seq->at(1)->asElement() : null; |
|
518 | + } |
|
519 | 519 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\CertificationPath\PathValidation; |
6 | 6 |
@@ -18,81 +18,81 @@ |
||
18 | 18 | */ |
19 | 19 | class PathValidationResult |
20 | 20 | { |
21 | - /** |
|
22 | - * Certificates in a certification path. |
|
23 | - * |
|
24 | - * @var Certificate[] |
|
25 | - */ |
|
26 | - protected $_certificates; |
|
21 | + /** |
|
22 | + * Certificates in a certification path. |
|
23 | + * |
|
24 | + * @var Certificate[] |
|
25 | + */ |
|
26 | + protected $_certificates; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Valid policy tree. |
|
30 | - * |
|
31 | - * @var null|PolicyTree |
|
32 | - */ |
|
33 | - protected $_policyTree; |
|
28 | + /** |
|
29 | + * Valid policy tree. |
|
30 | + * |
|
31 | + * @var null|PolicyTree |
|
32 | + */ |
|
33 | + protected $_policyTree; |
|
34 | 34 | |
35 | - /** |
|
36 | - * End-entity certificate's public key. |
|
37 | - * |
|
38 | - * @var PublicKeyInfo |
|
39 | - */ |
|
40 | - protected $_publicKeyInfo; |
|
35 | + /** |
|
36 | + * End-entity certificate's public key. |
|
37 | + * |
|
38 | + * @var PublicKeyInfo |
|
39 | + */ |
|
40 | + protected $_publicKeyInfo; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Public key algorithm. |
|
44 | - * |
|
45 | - * @var AlgorithmIdentifierType |
|
46 | - */ |
|
47 | - protected $_publicKeyAlgo; |
|
42 | + /** |
|
43 | + * Public key algorithm. |
|
44 | + * |
|
45 | + * @var AlgorithmIdentifierType |
|
46 | + */ |
|
47 | + protected $_publicKeyAlgo; |
|
48 | 48 | |
49 | - /** |
|
50 | - * Public key parameters. |
|
51 | - * |
|
52 | - * @var null|Element |
|
53 | - */ |
|
54 | - protected $_publicKeyParameters; |
|
49 | + /** |
|
50 | + * Public key parameters. |
|
51 | + * |
|
52 | + * @var null|Element |
|
53 | + */ |
|
54 | + protected $_publicKeyParameters; |
|
55 | 55 | |
56 | - /** |
|
57 | - * Constructor. |
|
58 | - * |
|
59 | - * @param Certificate[] $certificates Certificates in a certification path |
|
60 | - * @param null|PolicyTree $policy_tree Valid policy tree |
|
61 | - * @param PublicKeyInfo $pubkey_info Public key of the end-entity certificate |
|
62 | - * @param AlgorithmIdentifierType $algo Public key algorithm of the end-entity certificate |
|
63 | - * @param null|Element $params Algorithm parameters |
|
64 | - */ |
|
65 | - public function __construct(array $certificates, ?PolicyTree $policy_tree, |
|
66 | - PublicKeyInfo $pubkey_info, AlgorithmIdentifierType $algo, |
|
67 | - ?Element $params = null) |
|
68 | - { |
|
69 | - $this->_certificates = array_values($certificates); |
|
70 | - $this->_policyTree = $policy_tree; |
|
71 | - $this->_publicKeyInfo = $pubkey_info; |
|
72 | - $this->_publicKeyAlgo = $algo; |
|
73 | - $this->_publicKeyParameters = $params; |
|
74 | - } |
|
56 | + /** |
|
57 | + * Constructor. |
|
58 | + * |
|
59 | + * @param Certificate[] $certificates Certificates in a certification path |
|
60 | + * @param null|PolicyTree $policy_tree Valid policy tree |
|
61 | + * @param PublicKeyInfo $pubkey_info Public key of the end-entity certificate |
|
62 | + * @param AlgorithmIdentifierType $algo Public key algorithm of the end-entity certificate |
|
63 | + * @param null|Element $params Algorithm parameters |
|
64 | + */ |
|
65 | + public function __construct(array $certificates, ?PolicyTree $policy_tree, |
|
66 | + PublicKeyInfo $pubkey_info, AlgorithmIdentifierType $algo, |
|
67 | + ?Element $params = null) |
|
68 | + { |
|
69 | + $this->_certificates = array_values($certificates); |
|
70 | + $this->_policyTree = $policy_tree; |
|
71 | + $this->_publicKeyInfo = $pubkey_info; |
|
72 | + $this->_publicKeyAlgo = $algo; |
|
73 | + $this->_publicKeyParameters = $params; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Get end-entity certificate. |
|
78 | - * |
|
79 | - * @return Certificate |
|
80 | - */ |
|
81 | - public function certificate(): Certificate |
|
82 | - { |
|
83 | - return $this->_certificates[count($this->_certificates) - 1]; |
|
84 | - } |
|
76 | + /** |
|
77 | + * Get end-entity certificate. |
|
78 | + * |
|
79 | + * @return Certificate |
|
80 | + */ |
|
81 | + public function certificate(): Certificate |
|
82 | + { |
|
83 | + return $this->_certificates[count($this->_certificates) - 1]; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Get certificate policies of the end-entity certificate. |
|
88 | - * |
|
89 | - * @return PolicyInformation[] |
|
90 | - */ |
|
91 | - public function policies(): array |
|
92 | - { |
|
93 | - if (!$this->_policyTree) { |
|
94 | - return []; |
|
95 | - } |
|
96 | - return $this->_policyTree->policiesAtDepth(count($this->_certificates)); |
|
97 | - } |
|
86 | + /** |
|
87 | + * Get certificate policies of the end-entity certificate. |
|
88 | + * |
|
89 | + * @return PolicyInformation[] |
|
90 | + */ |
|
91 | + public function policies(): array |
|
92 | + { |
|
93 | + if (!$this->_policyTree) { |
|
94 | + return []; |
|
95 | + } |
|
96 | + return $this->_policyTree->policiesAtDepth(count($this->_certificates)); |
|
97 | + } |
|
98 | 98 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\CertificationPath\PathValidation; |
6 | 6 |
@@ -14,282 +14,282 @@ |
||
14 | 14 | */ |
15 | 15 | class PathValidationConfig |
16 | 16 | { |
17 | - /** |
|
18 | - * Maximum allowed certification path length. |
|
19 | - * |
|
20 | - * @var int |
|
21 | - */ |
|
22 | - protected $_maxLength; |
|
17 | + /** |
|
18 | + * Maximum allowed certification path length. |
|
19 | + * |
|
20 | + * @var int |
|
21 | + */ |
|
22 | + protected $_maxLength; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Reference time. |
|
26 | - * |
|
27 | - * @var \DateTimeImmutable |
|
28 | - */ |
|
29 | - protected $_dateTime; |
|
24 | + /** |
|
25 | + * Reference time. |
|
26 | + * |
|
27 | + * @var \DateTimeImmutable |
|
28 | + */ |
|
29 | + protected $_dateTime; |
|
30 | 30 | |
31 | - /** |
|
32 | - * List of acceptable policy identifiers. |
|
33 | - * |
|
34 | - * @var string[] |
|
35 | - */ |
|
36 | - protected $_policySet; |
|
31 | + /** |
|
32 | + * List of acceptable policy identifiers. |
|
33 | + * |
|
34 | + * @var string[] |
|
35 | + */ |
|
36 | + protected $_policySet; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Trust anchor certificate. |
|
40 | - * |
|
41 | - * If not set, path validation uses the first certificate of the path. |
|
42 | - * |
|
43 | - * @var null|Certificate |
|
44 | - */ |
|
45 | - protected $_trustAnchor; |
|
38 | + /** |
|
39 | + * Trust anchor certificate. |
|
40 | + * |
|
41 | + * If not set, path validation uses the first certificate of the path. |
|
42 | + * |
|
43 | + * @var null|Certificate |
|
44 | + */ |
|
45 | + protected $_trustAnchor; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Whether policy mapping in inhibited. |
|
49 | - * |
|
50 | - * Setting this to true disallows policy mapping. |
|
51 | - * |
|
52 | - * @var bool |
|
53 | - */ |
|
54 | - protected $_policyMappingInhibit; |
|
47 | + /** |
|
48 | + * Whether policy mapping in inhibited. |
|
49 | + * |
|
50 | + * Setting this to true disallows policy mapping. |
|
51 | + * |
|
52 | + * @var bool |
|
53 | + */ |
|
54 | + protected $_policyMappingInhibit; |
|
55 | 55 | |
56 | - /** |
|
57 | - * Whether the path must be valid for at least one policy in the |
|
58 | - * initial policy set. |
|
59 | - * |
|
60 | - * @var bool |
|
61 | - */ |
|
62 | - protected $_explicitPolicy; |
|
56 | + /** |
|
57 | + * Whether the path must be valid for at least one policy in the |
|
58 | + * initial policy set. |
|
59 | + * |
|
60 | + * @var bool |
|
61 | + */ |
|
62 | + protected $_explicitPolicy; |
|
63 | 63 | |
64 | - /** |
|
65 | - * Whether anyPolicy OID processing should be inhibited. |
|
66 | - * |
|
67 | - * Setting this to true disallows the usage of anyPolicy. |
|
68 | - * |
|
69 | - * @var bool |
|
70 | - */ |
|
71 | - protected $_anyPolicyInhibit; |
|
64 | + /** |
|
65 | + * Whether anyPolicy OID processing should be inhibited. |
|
66 | + * |
|
67 | + * Setting this to true disallows the usage of anyPolicy. |
|
68 | + * |
|
69 | + * @var bool |
|
70 | + */ |
|
71 | + protected $_anyPolicyInhibit; |
|
72 | 72 | |
73 | - /** |
|
74 | - * @todo Implement |
|
75 | - * |
|
76 | - * @var mixed |
|
77 | - */ |
|
78 | - protected $_permittedSubtrees; |
|
73 | + /** |
|
74 | + * @todo Implement |
|
75 | + * |
|
76 | + * @var mixed |
|
77 | + */ |
|
78 | + protected $_permittedSubtrees; |
|
79 | 79 | |
80 | - /** |
|
81 | - * @todo Implement |
|
82 | - * |
|
83 | - * @var mixed |
|
84 | - */ |
|
85 | - protected $_excludedSubtrees; |
|
80 | + /** |
|
81 | + * @todo Implement |
|
82 | + * |
|
83 | + * @var mixed |
|
84 | + */ |
|
85 | + protected $_excludedSubtrees; |
|
86 | 86 | |
87 | - /** |
|
88 | - * Constructor. |
|
89 | - * |
|
90 | - * @param \DateTimeImmutable $dt Reference date and time |
|
91 | - * @param int $max_length Maximum certification path length |
|
92 | - */ |
|
93 | - public function __construct(\DateTimeImmutable $dt, int $max_length) |
|
94 | - { |
|
95 | - $this->_dateTime = $dt; |
|
96 | - $this->_maxLength = $max_length; |
|
97 | - $this->_policySet = [PolicyInformation::OID_ANY_POLICY]; |
|
98 | - $this->_policyMappingInhibit = false; |
|
99 | - $this->_explicitPolicy = false; |
|
100 | - $this->_anyPolicyInhibit = false; |
|
101 | - } |
|
87 | + /** |
|
88 | + * Constructor. |
|
89 | + * |
|
90 | + * @param \DateTimeImmutable $dt Reference date and time |
|
91 | + * @param int $max_length Maximum certification path length |
|
92 | + */ |
|
93 | + public function __construct(\DateTimeImmutable $dt, int $max_length) |
|
94 | + { |
|
95 | + $this->_dateTime = $dt; |
|
96 | + $this->_maxLength = $max_length; |
|
97 | + $this->_policySet = [PolicyInformation::OID_ANY_POLICY]; |
|
98 | + $this->_policyMappingInhibit = false; |
|
99 | + $this->_explicitPolicy = false; |
|
100 | + $this->_anyPolicyInhibit = false; |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Get default configuration. |
|
105 | - * |
|
106 | - * @return self |
|
107 | - */ |
|
108 | - public static function defaultConfig(): self |
|
109 | - { |
|
110 | - return new self(new \DateTimeImmutable(), 3); |
|
111 | - } |
|
103 | + /** |
|
104 | + * Get default configuration. |
|
105 | + * |
|
106 | + * @return self |
|
107 | + */ |
|
108 | + public static function defaultConfig(): self |
|
109 | + { |
|
110 | + return new self(new \DateTimeImmutable(), 3); |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * Get self with maximum path length. |
|
115 | - * |
|
116 | - * @param int $length |
|
117 | - * |
|
118 | - * @return self |
|
119 | - */ |
|
120 | - public function withMaxLength(int $length): self |
|
121 | - { |
|
122 | - $obj = clone $this; |
|
123 | - $obj->_maxLength = $length; |
|
124 | - return $obj; |
|
125 | - } |
|
113 | + /** |
|
114 | + * Get self with maximum path length. |
|
115 | + * |
|
116 | + * @param int $length |
|
117 | + * |
|
118 | + * @return self |
|
119 | + */ |
|
120 | + public function withMaxLength(int $length): self |
|
121 | + { |
|
122 | + $obj = clone $this; |
|
123 | + $obj->_maxLength = $length; |
|
124 | + return $obj; |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * Get self with reference date and time. |
|
129 | - * |
|
130 | - * @param \DateTimeImmutable $dt |
|
131 | - * |
|
132 | - * @return self |
|
133 | - */ |
|
134 | - public function withDateTime(\DateTimeImmutable $dt): self |
|
135 | - { |
|
136 | - $obj = clone $this; |
|
137 | - $obj->_dateTime = $dt; |
|
138 | - return $obj; |
|
139 | - } |
|
127 | + /** |
|
128 | + * Get self with reference date and time. |
|
129 | + * |
|
130 | + * @param \DateTimeImmutable $dt |
|
131 | + * |
|
132 | + * @return self |
|
133 | + */ |
|
134 | + public function withDateTime(\DateTimeImmutable $dt): self |
|
135 | + { |
|
136 | + $obj = clone $this; |
|
137 | + $obj->_dateTime = $dt; |
|
138 | + return $obj; |
|
139 | + } |
|
140 | 140 | |
141 | - /** |
|
142 | - * Get self with trust anchor certificate. |
|
143 | - * |
|
144 | - * @param Certificate $ca |
|
145 | - * |
|
146 | - * @return self |
|
147 | - */ |
|
148 | - public function withTrustAnchor(Certificate $ca): self |
|
149 | - { |
|
150 | - $obj = clone $this; |
|
151 | - $obj->_trustAnchor = $ca; |
|
152 | - return $obj; |
|
153 | - } |
|
141 | + /** |
|
142 | + * Get self with trust anchor certificate. |
|
143 | + * |
|
144 | + * @param Certificate $ca |
|
145 | + * |
|
146 | + * @return self |
|
147 | + */ |
|
148 | + public function withTrustAnchor(Certificate $ca): self |
|
149 | + { |
|
150 | + $obj = clone $this; |
|
151 | + $obj->_trustAnchor = $ca; |
|
152 | + return $obj; |
|
153 | + } |
|
154 | 154 | |
155 | - /** |
|
156 | - * Get self with initial-policy-mapping-inhibit set. |
|
157 | - * |
|
158 | - * @param bool $flag |
|
159 | - * |
|
160 | - * @return self |
|
161 | - */ |
|
162 | - public function withPolicyMappingInhibit(bool $flag): self |
|
163 | - { |
|
164 | - $obj = clone $this; |
|
165 | - $obj->_policyMappingInhibit = $flag; |
|
166 | - return $obj; |
|
167 | - } |
|
155 | + /** |
|
156 | + * Get self with initial-policy-mapping-inhibit set. |
|
157 | + * |
|
158 | + * @param bool $flag |
|
159 | + * |
|
160 | + * @return self |
|
161 | + */ |
|
162 | + public function withPolicyMappingInhibit(bool $flag): self |
|
163 | + { |
|
164 | + $obj = clone $this; |
|
165 | + $obj->_policyMappingInhibit = $flag; |
|
166 | + return $obj; |
|
167 | + } |
|
168 | 168 | |
169 | - /** |
|
170 | - * Get self with initial-explicit-policy set. |
|
171 | - * |
|
172 | - * @param bool $flag |
|
173 | - * |
|
174 | - * @return self |
|
175 | - */ |
|
176 | - public function withExplicitPolicy(bool $flag): self |
|
177 | - { |
|
178 | - $obj = clone $this; |
|
179 | - $obj->_explicitPolicy = $flag; |
|
180 | - return $obj; |
|
181 | - } |
|
169 | + /** |
|
170 | + * Get self with initial-explicit-policy set. |
|
171 | + * |
|
172 | + * @param bool $flag |
|
173 | + * |
|
174 | + * @return self |
|
175 | + */ |
|
176 | + public function withExplicitPolicy(bool $flag): self |
|
177 | + { |
|
178 | + $obj = clone $this; |
|
179 | + $obj->_explicitPolicy = $flag; |
|
180 | + return $obj; |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * Get self with initial-any-policy-inhibit set. |
|
185 | - * |
|
186 | - * @param bool $flag |
|
187 | - * |
|
188 | - * @return self |
|
189 | - */ |
|
190 | - public function withAnyPolicyInhibit(bool $flag): self |
|
191 | - { |
|
192 | - $obj = clone $this; |
|
193 | - $obj->_anyPolicyInhibit = $flag; |
|
194 | - return $obj; |
|
195 | - } |
|
183 | + /** |
|
184 | + * Get self with initial-any-policy-inhibit set. |
|
185 | + * |
|
186 | + * @param bool $flag |
|
187 | + * |
|
188 | + * @return self |
|
189 | + */ |
|
190 | + public function withAnyPolicyInhibit(bool $flag): self |
|
191 | + { |
|
192 | + $obj = clone $this; |
|
193 | + $obj->_anyPolicyInhibit = $flag; |
|
194 | + return $obj; |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Get self with user-initial-policy-set set to policy OIDs. |
|
199 | - * |
|
200 | - * @param string ...$policies List of policy OIDs |
|
201 | - * |
|
202 | - * @return self |
|
203 | - */ |
|
204 | - public function withPolicySet(string ...$policies): self |
|
205 | - { |
|
206 | - $obj = clone $this; |
|
207 | - $obj->_policySet = $policies; |
|
208 | - return $obj; |
|
209 | - } |
|
197 | + /** |
|
198 | + * Get self with user-initial-policy-set set to policy OIDs. |
|
199 | + * |
|
200 | + * @param string ...$policies List of policy OIDs |
|
201 | + * |
|
202 | + * @return self |
|
203 | + */ |
|
204 | + public function withPolicySet(string ...$policies): self |
|
205 | + { |
|
206 | + $obj = clone $this; |
|
207 | + $obj->_policySet = $policies; |
|
208 | + return $obj; |
|
209 | + } |
|
210 | 210 | |
211 | - /** |
|
212 | - * Get maximum certification path length. |
|
213 | - * |
|
214 | - * @return int |
|
215 | - */ |
|
216 | - public function maxLength(): int |
|
217 | - { |
|
218 | - return $this->_maxLength; |
|
219 | - } |
|
211 | + /** |
|
212 | + * Get maximum certification path length. |
|
213 | + * |
|
214 | + * @return int |
|
215 | + */ |
|
216 | + public function maxLength(): int |
|
217 | + { |
|
218 | + return $this->_maxLength; |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * Get reference date and time. |
|
223 | - * |
|
224 | - * @return \DateTimeImmutable |
|
225 | - */ |
|
226 | - public function dateTime(): \DateTimeImmutable |
|
227 | - { |
|
228 | - return $this->_dateTime; |
|
229 | - } |
|
221 | + /** |
|
222 | + * Get reference date and time. |
|
223 | + * |
|
224 | + * @return \DateTimeImmutable |
|
225 | + */ |
|
226 | + public function dateTime(): \DateTimeImmutable |
|
227 | + { |
|
228 | + return $this->_dateTime; |
|
229 | + } |
|
230 | 230 | |
231 | - /** |
|
232 | - * Get user-initial-policy-set. |
|
233 | - * |
|
234 | - * @return string[] Array of OID's |
|
235 | - */ |
|
236 | - public function policySet(): array |
|
237 | - { |
|
238 | - return $this->_policySet; |
|
239 | - } |
|
231 | + /** |
|
232 | + * Get user-initial-policy-set. |
|
233 | + * |
|
234 | + * @return string[] Array of OID's |
|
235 | + */ |
|
236 | + public function policySet(): array |
|
237 | + { |
|
238 | + return $this->_policySet; |
|
239 | + } |
|
240 | 240 | |
241 | - /** |
|
242 | - * Check whether trust anchor certificate is set. |
|
243 | - * |
|
244 | - * @return bool |
|
245 | - */ |
|
246 | - public function hasTrustAnchor(): bool |
|
247 | - { |
|
248 | - return isset($this->_trustAnchor); |
|
249 | - } |
|
241 | + /** |
|
242 | + * Check whether trust anchor certificate is set. |
|
243 | + * |
|
244 | + * @return bool |
|
245 | + */ |
|
246 | + public function hasTrustAnchor(): bool |
|
247 | + { |
|
248 | + return isset($this->_trustAnchor); |
|
249 | + } |
|
250 | 250 | |
251 | - /** |
|
252 | - * Get trust anchor certificate. |
|
253 | - * |
|
254 | - * @throws \LogicException If not set |
|
255 | - * |
|
256 | - * @return Certificate |
|
257 | - */ |
|
258 | - public function trustAnchor(): Certificate |
|
259 | - { |
|
260 | - if (!$this->hasTrustAnchor()) { |
|
261 | - throw new \LogicException('No trust anchor.'); |
|
262 | - } |
|
263 | - return $this->_trustAnchor; |
|
264 | - } |
|
251 | + /** |
|
252 | + * Get trust anchor certificate. |
|
253 | + * |
|
254 | + * @throws \LogicException If not set |
|
255 | + * |
|
256 | + * @return Certificate |
|
257 | + */ |
|
258 | + public function trustAnchor(): Certificate |
|
259 | + { |
|
260 | + if (!$this->hasTrustAnchor()) { |
|
261 | + throw new \LogicException('No trust anchor.'); |
|
262 | + } |
|
263 | + return $this->_trustAnchor; |
|
264 | + } |
|
265 | 265 | |
266 | - /** |
|
267 | - * Get initial-policy-mapping-inhibit. |
|
268 | - * |
|
269 | - * @return bool |
|
270 | - */ |
|
271 | - public function policyMappingInhibit(): bool |
|
272 | - { |
|
273 | - return $this->_policyMappingInhibit; |
|
274 | - } |
|
266 | + /** |
|
267 | + * Get initial-policy-mapping-inhibit. |
|
268 | + * |
|
269 | + * @return bool |
|
270 | + */ |
|
271 | + public function policyMappingInhibit(): bool |
|
272 | + { |
|
273 | + return $this->_policyMappingInhibit; |
|
274 | + } |
|
275 | 275 | |
276 | - /** |
|
277 | - * Get initial-explicit-policy. |
|
278 | - * |
|
279 | - * @return bool |
|
280 | - */ |
|
281 | - public function explicitPolicy(): bool |
|
282 | - { |
|
283 | - return $this->_explicitPolicy; |
|
284 | - } |
|
276 | + /** |
|
277 | + * Get initial-explicit-policy. |
|
278 | + * |
|
279 | + * @return bool |
|
280 | + */ |
|
281 | + public function explicitPolicy(): bool |
|
282 | + { |
|
283 | + return $this->_explicitPolicy; |
|
284 | + } |
|
285 | 285 | |
286 | - /** |
|
287 | - * Get initial-any-policy-inhibit. |
|
288 | - * |
|
289 | - * @return bool |
|
290 | - */ |
|
291 | - public function anyPolicyInhibit(): bool |
|
292 | - { |
|
293 | - return $this->_anyPolicyInhibit; |
|
294 | - } |
|
286 | + /** |
|
287 | + * Get initial-any-policy-inhibit. |
|
288 | + * |
|
289 | + * @return bool |
|
290 | + */ |
|
291 | + public function anyPolicyInhibit(): bool |
|
292 | + { |
|
293 | + return $this->_anyPolicyInhibit; |
|
294 | + } |
|
295 | 295 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\CertificationPath\PathValidation; |
6 | 6 |
@@ -17,599 +17,599 @@ |
||
17 | 17 | */ |
18 | 18 | class PathValidator |
19 | 19 | { |
20 | - /** |
|
21 | - * Crypto engine. |
|
22 | - * |
|
23 | - * @var Crypto |
|
24 | - */ |
|
25 | - protected $_crypto; |
|
20 | + /** |
|
21 | + * Crypto engine. |
|
22 | + * |
|
23 | + * @var Crypto |
|
24 | + */ |
|
25 | + protected $_crypto; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Path validation configuration. |
|
29 | - * |
|
30 | - * @var PathValidationConfig |
|
31 | - */ |
|
32 | - protected $_config; |
|
27 | + /** |
|
28 | + * Path validation configuration. |
|
29 | + * |
|
30 | + * @var PathValidationConfig |
|
31 | + */ |
|
32 | + protected $_config; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Certification path. |
|
36 | - * |
|
37 | - * @var Certificate[] |
|
38 | - */ |
|
39 | - protected $_certificates; |
|
34 | + /** |
|
35 | + * Certification path. |
|
36 | + * |
|
37 | + * @var Certificate[] |
|
38 | + */ |
|
39 | + protected $_certificates; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Certification path trust anchor. |
|
43 | - * |
|
44 | - * @var Certificate |
|
45 | - */ |
|
46 | - protected $_trustAnchor; |
|
41 | + /** |
|
42 | + * Certification path trust anchor. |
|
43 | + * |
|
44 | + * @var Certificate |
|
45 | + */ |
|
46 | + protected $_trustAnchor; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Constructor. |
|
50 | - * |
|
51 | - * @param Crypto $crypto Crypto engine |
|
52 | - * @param PathValidationConfig $config Validation config |
|
53 | - * @param Certificate ...$certificates Certificates from the trust anchor to |
|
54 | - * the end-entity certificate |
|
55 | - */ |
|
56 | - public function __construct(Crypto $crypto, PathValidationConfig $config, |
|
57 | - Certificate ...$certificates) |
|
58 | - { |
|
59 | - if (!count($certificates)) { |
|
60 | - throw new \LogicException('No certificates.'); |
|
61 | - } |
|
62 | - $this->_crypto = $crypto; |
|
63 | - $this->_config = $config; |
|
64 | - $this->_certificates = $certificates; |
|
65 | - // if trust anchor is explicitly given in configuration |
|
66 | - if ($config->hasTrustAnchor()) { |
|
67 | - $this->_trustAnchor = $config->trustAnchor(); |
|
68 | - } else { |
|
69 | - $this->_trustAnchor = $certificates[0]; |
|
70 | - } |
|
71 | - } |
|
48 | + /** |
|
49 | + * Constructor. |
|
50 | + * |
|
51 | + * @param Crypto $crypto Crypto engine |
|
52 | + * @param PathValidationConfig $config Validation config |
|
53 | + * @param Certificate ...$certificates Certificates from the trust anchor to |
|
54 | + * the end-entity certificate |
|
55 | + */ |
|
56 | + public function __construct(Crypto $crypto, PathValidationConfig $config, |
|
57 | + Certificate ...$certificates) |
|
58 | + { |
|
59 | + if (!count($certificates)) { |
|
60 | + throw new \LogicException('No certificates.'); |
|
61 | + } |
|
62 | + $this->_crypto = $crypto; |
|
63 | + $this->_config = $config; |
|
64 | + $this->_certificates = $certificates; |
|
65 | + // if trust anchor is explicitly given in configuration |
|
66 | + if ($config->hasTrustAnchor()) { |
|
67 | + $this->_trustAnchor = $config->trustAnchor(); |
|
68 | + } else { |
|
69 | + $this->_trustAnchor = $certificates[0]; |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Validate certification path. |
|
75 | - * |
|
76 | - * @throws PathValidationException |
|
77 | - * |
|
78 | - * @return PathValidationResult |
|
79 | - */ |
|
80 | - public function validate(): PathValidationResult |
|
81 | - { |
|
82 | - $n = count($this->_certificates); |
|
83 | - $state = ValidatorState::initialize( |
|
84 | - $this->_config, $this->_trustAnchor, $n); |
|
85 | - for ($i = 0; $i < $n; ++$i) { |
|
86 | - $state = $state->withIndex($i + 1); |
|
87 | - $cert = $this->_certificates[$i]; |
|
88 | - // process certificate (section 6.1.3.) |
|
89 | - $state = $this->_processCertificate($state, $cert); |
|
90 | - if (!$state->isFinal()) { |
|
91 | - // prepare next certificate (section 6.1.4.) |
|
92 | - $state = $this->_prepareNext($state, $cert); |
|
93 | - } |
|
94 | - } |
|
95 | - if (!isset($cert)) { |
|
96 | - throw new \LogicException('No certificates.'); |
|
97 | - } |
|
98 | - // wrap-up (section 6.1.5.) |
|
99 | - $state = $this->_wrapUp($state, $cert); |
|
100 | - // return outputs |
|
101 | - return $state->getResult($this->_certificates); |
|
102 | - } |
|
73 | + /** |
|
74 | + * Validate certification path. |
|
75 | + * |
|
76 | + * @throws PathValidationException |
|
77 | + * |
|
78 | + * @return PathValidationResult |
|
79 | + */ |
|
80 | + public function validate(): PathValidationResult |
|
81 | + { |
|
82 | + $n = count($this->_certificates); |
|
83 | + $state = ValidatorState::initialize( |
|
84 | + $this->_config, $this->_trustAnchor, $n); |
|
85 | + for ($i = 0; $i < $n; ++$i) { |
|
86 | + $state = $state->withIndex($i + 1); |
|
87 | + $cert = $this->_certificates[$i]; |
|
88 | + // process certificate (section 6.1.3.) |
|
89 | + $state = $this->_processCertificate($state, $cert); |
|
90 | + if (!$state->isFinal()) { |
|
91 | + // prepare next certificate (section 6.1.4.) |
|
92 | + $state = $this->_prepareNext($state, $cert); |
|
93 | + } |
|
94 | + } |
|
95 | + if (!isset($cert)) { |
|
96 | + throw new \LogicException('No certificates.'); |
|
97 | + } |
|
98 | + // wrap-up (section 6.1.5.) |
|
99 | + $state = $this->_wrapUp($state, $cert); |
|
100 | + // return outputs |
|
101 | + return $state->getResult($this->_certificates); |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * Apply basic certificate processing according to RFC 5280 section 6.1.3. |
|
106 | - * |
|
107 | - * @see https://tools.ietf.org/html/rfc5280#section-6.1.3 |
|
108 | - * |
|
109 | - * @param ValidatorState $state |
|
110 | - * @param Certificate $cert |
|
111 | - * |
|
112 | - * @throws PathValidationException |
|
113 | - * |
|
114 | - * @return ValidatorState |
|
115 | - */ |
|
116 | - private function _processCertificate(ValidatorState $state, |
|
117 | - Certificate $cert): ValidatorState |
|
118 | - { |
|
119 | - // (a.1) verify signature |
|
120 | - $this->_verifySignature($state, $cert); |
|
121 | - // (a.2) check validity period |
|
122 | - $this->_checkValidity($cert); |
|
123 | - // (a.3) check that certificate is not revoked |
|
124 | - $this->_checkRevocation($cert); |
|
125 | - // (a.4) check issuer |
|
126 | - $this->_checkIssuer($state, $cert); |
|
127 | - // (b)(c) if certificate is self-issued and it is not |
|
128 | - // the final certificate in the path, skip this step |
|
129 | - if (!($cert->isSelfIssued() && !$state->isFinal())) { |
|
130 | - // (b) check permitted subtrees |
|
131 | - $this->_checkPermittedSubtrees($state, $cert); |
|
132 | - // (c) check excluded subtrees |
|
133 | - $this->_checkExcludedSubtrees($state, $cert); |
|
134 | - } |
|
135 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
136 | - if ($extensions->hasCertificatePolicies()) { |
|
137 | - // (d) process policy information |
|
138 | - if ($state->hasValidPolicyTree()) { |
|
139 | - $state = $state->validPolicyTree()->processPolicies($state, $cert); |
|
140 | - } |
|
141 | - } else { |
|
142 | - // (e) certificate policies extension not present, |
|
143 | - // set the valid_policy_tree to NULL |
|
144 | - $state = $state->withoutValidPolicyTree(); |
|
145 | - } |
|
146 | - // (f) check that explicit_policy > 0 or valid_policy_tree is set |
|
147 | - if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
148 | - throw new PathValidationException('No valid policies.'); |
|
149 | - } |
|
150 | - return $state; |
|
151 | - } |
|
104 | + /** |
|
105 | + * Apply basic certificate processing according to RFC 5280 section 6.1.3. |
|
106 | + * |
|
107 | + * @see https://tools.ietf.org/html/rfc5280#section-6.1.3 |
|
108 | + * |
|
109 | + * @param ValidatorState $state |
|
110 | + * @param Certificate $cert |
|
111 | + * |
|
112 | + * @throws PathValidationException |
|
113 | + * |
|
114 | + * @return ValidatorState |
|
115 | + */ |
|
116 | + private function _processCertificate(ValidatorState $state, |
|
117 | + Certificate $cert): ValidatorState |
|
118 | + { |
|
119 | + // (a.1) verify signature |
|
120 | + $this->_verifySignature($state, $cert); |
|
121 | + // (a.2) check validity period |
|
122 | + $this->_checkValidity($cert); |
|
123 | + // (a.3) check that certificate is not revoked |
|
124 | + $this->_checkRevocation($cert); |
|
125 | + // (a.4) check issuer |
|
126 | + $this->_checkIssuer($state, $cert); |
|
127 | + // (b)(c) if certificate is self-issued and it is not |
|
128 | + // the final certificate in the path, skip this step |
|
129 | + if (!($cert->isSelfIssued() && !$state->isFinal())) { |
|
130 | + // (b) check permitted subtrees |
|
131 | + $this->_checkPermittedSubtrees($state, $cert); |
|
132 | + // (c) check excluded subtrees |
|
133 | + $this->_checkExcludedSubtrees($state, $cert); |
|
134 | + } |
|
135 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
136 | + if ($extensions->hasCertificatePolicies()) { |
|
137 | + // (d) process policy information |
|
138 | + if ($state->hasValidPolicyTree()) { |
|
139 | + $state = $state->validPolicyTree()->processPolicies($state, $cert); |
|
140 | + } |
|
141 | + } else { |
|
142 | + // (e) certificate policies extension not present, |
|
143 | + // set the valid_policy_tree to NULL |
|
144 | + $state = $state->withoutValidPolicyTree(); |
|
145 | + } |
|
146 | + // (f) check that explicit_policy > 0 or valid_policy_tree is set |
|
147 | + if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
148 | + throw new PathValidationException('No valid policies.'); |
|
149 | + } |
|
150 | + return $state; |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Apply preparation for the certificate i+1 according to rfc5280 section |
|
155 | - * 6.1.4. |
|
156 | - * |
|
157 | - * @see https://tools.ietf.org/html/rfc5280#section-6.1.4 |
|
158 | - * |
|
159 | - * @param ValidatorState $state |
|
160 | - * @param Certificate $cert |
|
161 | - * |
|
162 | - * @return ValidatorState |
|
163 | - */ |
|
164 | - private function _prepareNext(ValidatorState $state, |
|
165 | - Certificate $cert): ValidatorState |
|
166 | - { |
|
167 | - // (a)(b) if policy mappings extension is present |
|
168 | - $state = $this->_preparePolicyMappings($state, $cert); |
|
169 | - // (c) assign working_issuer_name |
|
170 | - $state = $state->withWorkingIssuerName( |
|
171 | - $cert->tbsCertificate()->subject()); |
|
172 | - // (d)(e)(f) |
|
173 | - $state = $this->_setPublicKeyState($state, $cert); |
|
174 | - // (g) if name constraints extension is present |
|
175 | - $state = $this->_prepareNameConstraints($state, $cert); |
|
176 | - // (h) if certificate is not self-issued |
|
177 | - if (!$cert->isSelfIssued()) { |
|
178 | - $state = $this->_prepareNonSelfIssued($state); |
|
179 | - } |
|
180 | - // (i) if policy constraints extension is present |
|
181 | - $state = $this->_preparePolicyConstraints($state, $cert); |
|
182 | - // (j) if inhibit any policy extension is present |
|
183 | - $state = $this->_prepareInhibitAnyPolicy($state, $cert); |
|
184 | - // (k) check basic constraints |
|
185 | - $this->_processBasicContraints($cert); |
|
186 | - // (l) verify max_path_length |
|
187 | - $state = $this->_verifyMaxPathLength($state, $cert); |
|
188 | - // (m) check pathLenContraint |
|
189 | - $state = $this->_processPathLengthContraint($state, $cert); |
|
190 | - // (n) check key usage |
|
191 | - $this->_checkKeyUsage($cert); |
|
192 | - // (o) process relevant extensions |
|
193 | - return $this->_processExtensions($state, $cert); |
|
194 | - } |
|
153 | + /** |
|
154 | + * Apply preparation for the certificate i+1 according to rfc5280 section |
|
155 | + * 6.1.4. |
|
156 | + * |
|
157 | + * @see https://tools.ietf.org/html/rfc5280#section-6.1.4 |
|
158 | + * |
|
159 | + * @param ValidatorState $state |
|
160 | + * @param Certificate $cert |
|
161 | + * |
|
162 | + * @return ValidatorState |
|
163 | + */ |
|
164 | + private function _prepareNext(ValidatorState $state, |
|
165 | + Certificate $cert): ValidatorState |
|
166 | + { |
|
167 | + // (a)(b) if policy mappings extension is present |
|
168 | + $state = $this->_preparePolicyMappings($state, $cert); |
|
169 | + // (c) assign working_issuer_name |
|
170 | + $state = $state->withWorkingIssuerName( |
|
171 | + $cert->tbsCertificate()->subject()); |
|
172 | + // (d)(e)(f) |
|
173 | + $state = $this->_setPublicKeyState($state, $cert); |
|
174 | + // (g) if name constraints extension is present |
|
175 | + $state = $this->_prepareNameConstraints($state, $cert); |
|
176 | + // (h) if certificate is not self-issued |
|
177 | + if (!$cert->isSelfIssued()) { |
|
178 | + $state = $this->_prepareNonSelfIssued($state); |
|
179 | + } |
|
180 | + // (i) if policy constraints extension is present |
|
181 | + $state = $this->_preparePolicyConstraints($state, $cert); |
|
182 | + // (j) if inhibit any policy extension is present |
|
183 | + $state = $this->_prepareInhibitAnyPolicy($state, $cert); |
|
184 | + // (k) check basic constraints |
|
185 | + $this->_processBasicContraints($cert); |
|
186 | + // (l) verify max_path_length |
|
187 | + $state = $this->_verifyMaxPathLength($state, $cert); |
|
188 | + // (m) check pathLenContraint |
|
189 | + $state = $this->_processPathLengthContraint($state, $cert); |
|
190 | + // (n) check key usage |
|
191 | + $this->_checkKeyUsage($cert); |
|
192 | + // (o) process relevant extensions |
|
193 | + return $this->_processExtensions($state, $cert); |
|
194 | + } |
|
195 | 195 | |
196 | - /** |
|
197 | - * Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
|
198 | - * |
|
199 | - * @see https://tools.ietf.org/html/rfc5280#section-6.1.5 |
|
200 | - * |
|
201 | - * @param ValidatorState $state |
|
202 | - * @param Certificate $cert |
|
203 | - * |
|
204 | - * @throws PathValidationException |
|
205 | - * |
|
206 | - * @return ValidatorState |
|
207 | - */ |
|
208 | - private function _wrapUp(ValidatorState $state, |
|
209 | - Certificate $cert): ValidatorState |
|
210 | - { |
|
211 | - $tbs_cert = $cert->tbsCertificate(); |
|
212 | - $extensions = $tbs_cert->extensions(); |
|
213 | - // (a) |
|
214 | - if ($state->explicitPolicy() > 0) { |
|
215 | - $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
216 | - } |
|
217 | - // (b) |
|
218 | - if ($extensions->hasPolicyConstraints()) { |
|
219 | - $ext = $extensions->policyConstraints(); |
|
220 | - if ($ext->hasRequireExplicitPolicy() && |
|
221 | - 0 === $ext->requireExplicitPolicy()) { |
|
222 | - $state = $state->withExplicitPolicy(0); |
|
223 | - } |
|
224 | - } |
|
225 | - // (c)(d)(e) |
|
226 | - $state = $this->_setPublicKeyState($state, $cert); |
|
227 | - // (f) process relevant extensions |
|
228 | - $state = $this->_processExtensions($state, $cert); |
|
229 | - // (g) intersection of valid_policy_tree and the initial-policy-set |
|
230 | - $state = $this->_calculatePolicyIntersection($state); |
|
231 | - // check that explicit_policy > 0 or valid_policy_tree is set |
|
232 | - if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
233 | - throw new PathValidationException('No valid policies.'); |
|
234 | - } |
|
235 | - // path validation succeeded |
|
236 | - return $state; |
|
237 | - } |
|
196 | + /** |
|
197 | + * Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
|
198 | + * |
|
199 | + * @see https://tools.ietf.org/html/rfc5280#section-6.1.5 |
|
200 | + * |
|
201 | + * @param ValidatorState $state |
|
202 | + * @param Certificate $cert |
|
203 | + * |
|
204 | + * @throws PathValidationException |
|
205 | + * |
|
206 | + * @return ValidatorState |
|
207 | + */ |
|
208 | + private function _wrapUp(ValidatorState $state, |
|
209 | + Certificate $cert): ValidatorState |
|
210 | + { |
|
211 | + $tbs_cert = $cert->tbsCertificate(); |
|
212 | + $extensions = $tbs_cert->extensions(); |
|
213 | + // (a) |
|
214 | + if ($state->explicitPolicy() > 0) { |
|
215 | + $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
216 | + } |
|
217 | + // (b) |
|
218 | + if ($extensions->hasPolicyConstraints()) { |
|
219 | + $ext = $extensions->policyConstraints(); |
|
220 | + if ($ext->hasRequireExplicitPolicy() && |
|
221 | + 0 === $ext->requireExplicitPolicy()) { |
|
222 | + $state = $state->withExplicitPolicy(0); |
|
223 | + } |
|
224 | + } |
|
225 | + // (c)(d)(e) |
|
226 | + $state = $this->_setPublicKeyState($state, $cert); |
|
227 | + // (f) process relevant extensions |
|
228 | + $state = $this->_processExtensions($state, $cert); |
|
229 | + // (g) intersection of valid_policy_tree and the initial-policy-set |
|
230 | + $state = $this->_calculatePolicyIntersection($state); |
|
231 | + // check that explicit_policy > 0 or valid_policy_tree is set |
|
232 | + if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
233 | + throw new PathValidationException('No valid policies.'); |
|
234 | + } |
|
235 | + // path validation succeeded |
|
236 | + return $state; |
|
237 | + } |
|
238 | 238 | |
239 | - /** |
|
240 | - * Update working_public_key, working_public_key_parameters and |
|
241 | - * working_public_key_algorithm state variables from certificate. |
|
242 | - * |
|
243 | - * @param ValidatorState $state |
|
244 | - * @param Certificate $cert |
|
245 | - * |
|
246 | - * @return ValidatorState |
|
247 | - */ |
|
248 | - private function _setPublicKeyState(ValidatorState $state, |
|
249 | - Certificate $cert): ValidatorState |
|
250 | - { |
|
251 | - $pk_info = $cert->tbsCertificate()->subjectPublicKeyInfo(); |
|
252 | - // assign working_public_key |
|
253 | - $state = $state->withWorkingPublicKey($pk_info); |
|
254 | - // assign working_public_key_parameters |
|
255 | - $params = ValidatorState::getAlgorithmParameters( |
|
256 | - $pk_info->algorithmIdentifier()); |
|
257 | - if (null !== $params) { |
|
258 | - $state = $state->withWorkingPublicKeyParameters($params); |
|
259 | - } else { |
|
260 | - // if algorithms differ, set parameters to null |
|
261 | - if ($pk_info->algorithmIdentifier()->oid() !== |
|
262 | - $state->workingPublicKeyAlgorithm()->oid()) { |
|
263 | - $state = $state->withWorkingPublicKeyParameters(null); |
|
264 | - } |
|
265 | - } |
|
266 | - // assign working_public_key_algorithm |
|
267 | - return $state->withWorkingPublicKeyAlgorithm( |
|
268 | - $pk_info->algorithmIdentifier()); |
|
269 | - } |
|
239 | + /** |
|
240 | + * Update working_public_key, working_public_key_parameters and |
|
241 | + * working_public_key_algorithm state variables from certificate. |
|
242 | + * |
|
243 | + * @param ValidatorState $state |
|
244 | + * @param Certificate $cert |
|
245 | + * |
|
246 | + * @return ValidatorState |
|
247 | + */ |
|
248 | + private function _setPublicKeyState(ValidatorState $state, |
|
249 | + Certificate $cert): ValidatorState |
|
250 | + { |
|
251 | + $pk_info = $cert->tbsCertificate()->subjectPublicKeyInfo(); |
|
252 | + // assign working_public_key |
|
253 | + $state = $state->withWorkingPublicKey($pk_info); |
|
254 | + // assign working_public_key_parameters |
|
255 | + $params = ValidatorState::getAlgorithmParameters( |
|
256 | + $pk_info->algorithmIdentifier()); |
|
257 | + if (null !== $params) { |
|
258 | + $state = $state->withWorkingPublicKeyParameters($params); |
|
259 | + } else { |
|
260 | + // if algorithms differ, set parameters to null |
|
261 | + if ($pk_info->algorithmIdentifier()->oid() !== |
|
262 | + $state->workingPublicKeyAlgorithm()->oid()) { |
|
263 | + $state = $state->withWorkingPublicKeyParameters(null); |
|
264 | + } |
|
265 | + } |
|
266 | + // assign working_public_key_algorithm |
|
267 | + return $state->withWorkingPublicKeyAlgorithm( |
|
268 | + $pk_info->algorithmIdentifier()); |
|
269 | + } |
|
270 | 270 | |
271 | - /** |
|
272 | - * Verify certificate signature. |
|
273 | - * |
|
274 | - * @param ValidatorState $state |
|
275 | - * @param Certificate $cert |
|
276 | - * |
|
277 | - * @throws PathValidationException |
|
278 | - */ |
|
279 | - private function _verifySignature(ValidatorState $state, |
|
280 | - Certificate $cert): void |
|
281 | - { |
|
282 | - try { |
|
283 | - $valid = $cert->verify($state->workingPublicKey(), $this->_crypto); |
|
284 | - } catch (\RuntimeException $e) { |
|
285 | - throw new PathValidationException( |
|
286 | - 'Failed to verify signature: ' . $e->getMessage(), 0, $e); |
|
287 | - } |
|
288 | - if (!$valid) { |
|
289 | - throw new PathValidationException( |
|
290 | - "Certificate signature doesn't match."); |
|
291 | - } |
|
292 | - } |
|
271 | + /** |
|
272 | + * Verify certificate signature. |
|
273 | + * |
|
274 | + * @param ValidatorState $state |
|
275 | + * @param Certificate $cert |
|
276 | + * |
|
277 | + * @throws PathValidationException |
|
278 | + */ |
|
279 | + private function _verifySignature(ValidatorState $state, |
|
280 | + Certificate $cert): void |
|
281 | + { |
|
282 | + try { |
|
283 | + $valid = $cert->verify($state->workingPublicKey(), $this->_crypto); |
|
284 | + } catch (\RuntimeException $e) { |
|
285 | + throw new PathValidationException( |
|
286 | + 'Failed to verify signature: ' . $e->getMessage(), 0, $e); |
|
287 | + } |
|
288 | + if (!$valid) { |
|
289 | + throw new PathValidationException( |
|
290 | + "Certificate signature doesn't match."); |
|
291 | + } |
|
292 | + } |
|
293 | 293 | |
294 | - /** |
|
295 | - * Check certificate validity. |
|
296 | - * |
|
297 | - * @param Certificate $cert |
|
298 | - * |
|
299 | - * @throws PathValidationException |
|
300 | - */ |
|
301 | - private function _checkValidity(Certificate $cert): void |
|
302 | - { |
|
303 | - $refdt = $this->_config->dateTime(); |
|
304 | - $validity = $cert->tbsCertificate()->validity(); |
|
305 | - if ($validity->notBefore()->dateTime()->diff($refdt)->invert) { |
|
306 | - throw new PathValidationException( |
|
307 | - 'Certificate validity period has not started.'); |
|
308 | - } |
|
309 | - if ($refdt->diff($validity->notAfter()->dateTime())->invert) { |
|
310 | - throw new PathValidationException('Certificate has expired.'); |
|
311 | - } |
|
312 | - } |
|
294 | + /** |
|
295 | + * Check certificate validity. |
|
296 | + * |
|
297 | + * @param Certificate $cert |
|
298 | + * |
|
299 | + * @throws PathValidationException |
|
300 | + */ |
|
301 | + private function _checkValidity(Certificate $cert): void |
|
302 | + { |
|
303 | + $refdt = $this->_config->dateTime(); |
|
304 | + $validity = $cert->tbsCertificate()->validity(); |
|
305 | + if ($validity->notBefore()->dateTime()->diff($refdt)->invert) { |
|
306 | + throw new PathValidationException( |
|
307 | + 'Certificate validity period has not started.'); |
|
308 | + } |
|
309 | + if ($refdt->diff($validity->notAfter()->dateTime())->invert) { |
|
310 | + throw new PathValidationException('Certificate has expired.'); |
|
311 | + } |
|
312 | + } |
|
313 | 313 | |
314 | - /** |
|
315 | - * Check certificate revocation. |
|
316 | - * |
|
317 | - * @param Certificate $cert |
|
318 | - */ |
|
319 | - private function _checkRevocation(Certificate $cert) |
|
320 | - { |
|
321 | - // @todo Implement CRL handling |
|
322 | - } |
|
314 | + /** |
|
315 | + * Check certificate revocation. |
|
316 | + * |
|
317 | + * @param Certificate $cert |
|
318 | + */ |
|
319 | + private function _checkRevocation(Certificate $cert) |
|
320 | + { |
|
321 | + // @todo Implement CRL handling |
|
322 | + } |
|
323 | 323 | |
324 | - /** |
|
325 | - * Check certificate issuer. |
|
326 | - * |
|
327 | - * @param ValidatorState $state |
|
328 | - * @param Certificate $cert |
|
329 | - * |
|
330 | - * @throws PathValidationException |
|
331 | - */ |
|
332 | - private function _checkIssuer(ValidatorState $state, Certificate $cert): void |
|
333 | - { |
|
334 | - if (!$cert->tbsCertificate()->issuer()->equals($state->workingIssuerName())) { |
|
335 | - throw new PathValidationException('Certification issuer mismatch.'); |
|
336 | - } |
|
337 | - } |
|
324 | + /** |
|
325 | + * Check certificate issuer. |
|
326 | + * |
|
327 | + * @param ValidatorState $state |
|
328 | + * @param Certificate $cert |
|
329 | + * |
|
330 | + * @throws PathValidationException |
|
331 | + */ |
|
332 | + private function _checkIssuer(ValidatorState $state, Certificate $cert): void |
|
333 | + { |
|
334 | + if (!$cert->tbsCertificate()->issuer()->equals($state->workingIssuerName())) { |
|
335 | + throw new PathValidationException('Certification issuer mismatch.'); |
|
336 | + } |
|
337 | + } |
|
338 | 338 | |
339 | - /** |
|
340 | - * @param ValidatorState $state |
|
341 | - * @param Certificate $cert |
|
342 | - */ |
|
343 | - private function _checkPermittedSubtrees(ValidatorState $state, Certificate $cert) |
|
344 | - { |
|
345 | - // @todo Implement |
|
346 | - $state->permittedSubtrees(); |
|
347 | - } |
|
339 | + /** |
|
340 | + * @param ValidatorState $state |
|
341 | + * @param Certificate $cert |
|
342 | + */ |
|
343 | + private function _checkPermittedSubtrees(ValidatorState $state, Certificate $cert) |
|
344 | + { |
|
345 | + // @todo Implement |
|
346 | + $state->permittedSubtrees(); |
|
347 | + } |
|
348 | 348 | |
349 | - /** |
|
350 | - * @param ValidatorState $state |
|
351 | - * @param Certificate $cert |
|
352 | - */ |
|
353 | - private function _checkExcludedSubtrees(ValidatorState $state, Certificate $cert) |
|
354 | - { |
|
355 | - // @todo Implement |
|
356 | - $state->excludedSubtrees(); |
|
357 | - } |
|
349 | + /** |
|
350 | + * @param ValidatorState $state |
|
351 | + * @param Certificate $cert |
|
352 | + */ |
|
353 | + private function _checkExcludedSubtrees(ValidatorState $state, Certificate $cert) |
|
354 | + { |
|
355 | + // @todo Implement |
|
356 | + $state->excludedSubtrees(); |
|
357 | + } |
|
358 | 358 | |
359 | - /** |
|
360 | - * Apply policy mappings handling for the preparation step. |
|
361 | - * |
|
362 | - * @param ValidatorState $state |
|
363 | - * @param Certificate $cert |
|
364 | - * |
|
365 | - * @throws PathValidationException |
|
366 | - * |
|
367 | - * @return ValidatorState |
|
368 | - */ |
|
369 | - private function _preparePolicyMappings(ValidatorState $state, |
|
370 | - Certificate $cert): ValidatorState |
|
371 | - { |
|
372 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
373 | - if ($extensions->hasPolicyMappings()) { |
|
374 | - // (a) verify that anyPolicy mapping is not used |
|
375 | - if ($extensions->policyMappings()->hasAnyPolicyMapping()) { |
|
376 | - throw new PathValidationException('anyPolicy mapping found.'); |
|
377 | - } |
|
378 | - // (b) process policy mappings |
|
379 | - if ($state->hasValidPolicyTree()) { |
|
380 | - $state = $state->validPolicyTree()->processMappings($state, $cert); |
|
381 | - } |
|
382 | - } |
|
383 | - return $state; |
|
384 | - } |
|
359 | + /** |
|
360 | + * Apply policy mappings handling for the preparation step. |
|
361 | + * |
|
362 | + * @param ValidatorState $state |
|
363 | + * @param Certificate $cert |
|
364 | + * |
|
365 | + * @throws PathValidationException |
|
366 | + * |
|
367 | + * @return ValidatorState |
|
368 | + */ |
|
369 | + private function _preparePolicyMappings(ValidatorState $state, |
|
370 | + Certificate $cert): ValidatorState |
|
371 | + { |
|
372 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
373 | + if ($extensions->hasPolicyMappings()) { |
|
374 | + // (a) verify that anyPolicy mapping is not used |
|
375 | + if ($extensions->policyMappings()->hasAnyPolicyMapping()) { |
|
376 | + throw new PathValidationException('anyPolicy mapping found.'); |
|
377 | + } |
|
378 | + // (b) process policy mappings |
|
379 | + if ($state->hasValidPolicyTree()) { |
|
380 | + $state = $state->validPolicyTree()->processMappings($state, $cert); |
|
381 | + } |
|
382 | + } |
|
383 | + return $state; |
|
384 | + } |
|
385 | 385 | |
386 | - /** |
|
387 | - * Apply name constraints handling for the preparation step. |
|
388 | - * |
|
389 | - * @param ValidatorState $state |
|
390 | - * @param Certificate $cert |
|
391 | - * |
|
392 | - * @return ValidatorState |
|
393 | - */ |
|
394 | - private function _prepareNameConstraints(ValidatorState $state, |
|
395 | - Certificate $cert): ValidatorState |
|
396 | - { |
|
397 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
398 | - if ($extensions->hasNameConstraints()) { |
|
399 | - $state = $this->_processNameConstraints($state, $cert); |
|
400 | - } |
|
401 | - return $state; |
|
402 | - } |
|
386 | + /** |
|
387 | + * Apply name constraints handling for the preparation step. |
|
388 | + * |
|
389 | + * @param ValidatorState $state |
|
390 | + * @param Certificate $cert |
|
391 | + * |
|
392 | + * @return ValidatorState |
|
393 | + */ |
|
394 | + private function _prepareNameConstraints(ValidatorState $state, |
|
395 | + Certificate $cert): ValidatorState |
|
396 | + { |
|
397 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
398 | + if ($extensions->hasNameConstraints()) { |
|
399 | + $state = $this->_processNameConstraints($state, $cert); |
|
400 | + } |
|
401 | + return $state; |
|
402 | + } |
|
403 | 403 | |
404 | - /** |
|
405 | - * Apply preparation for a non-self-signed certificate. |
|
406 | - * |
|
407 | - * @param ValidatorState $state |
|
408 | - * |
|
409 | - * @return ValidatorState |
|
410 | - */ |
|
411 | - private function _prepareNonSelfIssued(ValidatorState $state): ValidatorState |
|
412 | - { |
|
413 | - // (h.1) |
|
414 | - if ($state->explicitPolicy() > 0) { |
|
415 | - $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
416 | - } |
|
417 | - // (h.2) |
|
418 | - if ($state->policyMapping() > 0) { |
|
419 | - $state = $state->withPolicyMapping($state->policyMapping() - 1); |
|
420 | - } |
|
421 | - // (h.3) |
|
422 | - if ($state->inhibitAnyPolicy() > 0) { |
|
423 | - $state = $state->withInhibitAnyPolicy($state->inhibitAnyPolicy() - 1); |
|
424 | - } |
|
425 | - return $state; |
|
426 | - } |
|
404 | + /** |
|
405 | + * Apply preparation for a non-self-signed certificate. |
|
406 | + * |
|
407 | + * @param ValidatorState $state |
|
408 | + * |
|
409 | + * @return ValidatorState |
|
410 | + */ |
|
411 | + private function _prepareNonSelfIssued(ValidatorState $state): ValidatorState |
|
412 | + { |
|
413 | + // (h.1) |
|
414 | + if ($state->explicitPolicy() > 0) { |
|
415 | + $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
416 | + } |
|
417 | + // (h.2) |
|
418 | + if ($state->policyMapping() > 0) { |
|
419 | + $state = $state->withPolicyMapping($state->policyMapping() - 1); |
|
420 | + } |
|
421 | + // (h.3) |
|
422 | + if ($state->inhibitAnyPolicy() > 0) { |
|
423 | + $state = $state->withInhibitAnyPolicy($state->inhibitAnyPolicy() - 1); |
|
424 | + } |
|
425 | + return $state; |
|
426 | + } |
|
427 | 427 | |
428 | - /** |
|
429 | - * Apply policy constraints handling for the preparation step. |
|
430 | - * |
|
431 | - * @param ValidatorState $state |
|
432 | - * @param Certificate $cert |
|
433 | - * |
|
434 | - * @return ValidatorState |
|
435 | - */ |
|
436 | - private function _preparePolicyConstraints(ValidatorState $state, |
|
437 | - Certificate $cert): ValidatorState |
|
438 | - { |
|
439 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
440 | - if (!$extensions->hasPolicyConstraints()) { |
|
441 | - return $state; |
|
442 | - } |
|
443 | - $ext = $extensions->policyConstraints(); |
|
444 | - // (i.1) |
|
445 | - if ($ext->hasRequireExplicitPolicy() && |
|
446 | - $ext->requireExplicitPolicy() < $state->explicitPolicy()) { |
|
447 | - $state = $state->withExplicitPolicy($ext->requireExplicitPolicy()); |
|
448 | - } |
|
449 | - // (i.2) |
|
450 | - if ($ext->hasInhibitPolicyMapping() && |
|
451 | - $ext->inhibitPolicyMapping() < $state->policyMapping()) { |
|
452 | - $state = $state->withPolicyMapping($ext->inhibitPolicyMapping()); |
|
453 | - } |
|
454 | - return $state; |
|
455 | - } |
|
428 | + /** |
|
429 | + * Apply policy constraints handling for the preparation step. |
|
430 | + * |
|
431 | + * @param ValidatorState $state |
|
432 | + * @param Certificate $cert |
|
433 | + * |
|
434 | + * @return ValidatorState |
|
435 | + */ |
|
436 | + private function _preparePolicyConstraints(ValidatorState $state, |
|
437 | + Certificate $cert): ValidatorState |
|
438 | + { |
|
439 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
440 | + if (!$extensions->hasPolicyConstraints()) { |
|
441 | + return $state; |
|
442 | + } |
|
443 | + $ext = $extensions->policyConstraints(); |
|
444 | + // (i.1) |
|
445 | + if ($ext->hasRequireExplicitPolicy() && |
|
446 | + $ext->requireExplicitPolicy() < $state->explicitPolicy()) { |
|
447 | + $state = $state->withExplicitPolicy($ext->requireExplicitPolicy()); |
|
448 | + } |
|
449 | + // (i.2) |
|
450 | + if ($ext->hasInhibitPolicyMapping() && |
|
451 | + $ext->inhibitPolicyMapping() < $state->policyMapping()) { |
|
452 | + $state = $state->withPolicyMapping($ext->inhibitPolicyMapping()); |
|
453 | + } |
|
454 | + return $state; |
|
455 | + } |
|
456 | 456 | |
457 | - /** |
|
458 | - * Apply inhibit any-policy handling for the preparation step. |
|
459 | - * |
|
460 | - * @param ValidatorState $state |
|
461 | - * @param Certificate $cert |
|
462 | - * |
|
463 | - * @return ValidatorState |
|
464 | - */ |
|
465 | - private function _prepareInhibitAnyPolicy(ValidatorState $state, |
|
466 | - Certificate $cert): ValidatorState |
|
467 | - { |
|
468 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
469 | - if ($extensions->hasInhibitAnyPolicy()) { |
|
470 | - $ext = $extensions->inhibitAnyPolicy(); |
|
471 | - if ($ext->skipCerts() < $state->inhibitAnyPolicy()) { |
|
472 | - $state = $state->withInhibitAnyPolicy($ext->skipCerts()); |
|
473 | - } |
|
474 | - } |
|
475 | - return $state; |
|
476 | - } |
|
457 | + /** |
|
458 | + * Apply inhibit any-policy handling for the preparation step. |
|
459 | + * |
|
460 | + * @param ValidatorState $state |
|
461 | + * @param Certificate $cert |
|
462 | + * |
|
463 | + * @return ValidatorState |
|
464 | + */ |
|
465 | + private function _prepareInhibitAnyPolicy(ValidatorState $state, |
|
466 | + Certificate $cert): ValidatorState |
|
467 | + { |
|
468 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
469 | + if ($extensions->hasInhibitAnyPolicy()) { |
|
470 | + $ext = $extensions->inhibitAnyPolicy(); |
|
471 | + if ($ext->skipCerts() < $state->inhibitAnyPolicy()) { |
|
472 | + $state = $state->withInhibitAnyPolicy($ext->skipCerts()); |
|
473 | + } |
|
474 | + } |
|
475 | + return $state; |
|
476 | + } |
|
477 | 477 | |
478 | - /** |
|
479 | - * Verify maximum certification path length for the preparation step. |
|
480 | - * |
|
481 | - * @param ValidatorState $state |
|
482 | - * @param Certificate $cert |
|
483 | - * |
|
484 | - * @throws PathValidationException |
|
485 | - * |
|
486 | - * @return ValidatorState |
|
487 | - */ |
|
488 | - private function _verifyMaxPathLength(ValidatorState $state, |
|
489 | - Certificate $cert): ValidatorState |
|
490 | - { |
|
491 | - if (!$cert->isSelfIssued()) { |
|
492 | - if ($state->maxPathLength() <= 0) { |
|
493 | - throw new PathValidationException( |
|
494 | - 'Certification path length exceeded.'); |
|
495 | - } |
|
496 | - $state = $state->withMaxPathLength($state->maxPathLength() - 1); |
|
497 | - } |
|
498 | - return $state; |
|
499 | - } |
|
478 | + /** |
|
479 | + * Verify maximum certification path length for the preparation step. |
|
480 | + * |
|
481 | + * @param ValidatorState $state |
|
482 | + * @param Certificate $cert |
|
483 | + * |
|
484 | + * @throws PathValidationException |
|
485 | + * |
|
486 | + * @return ValidatorState |
|
487 | + */ |
|
488 | + private function _verifyMaxPathLength(ValidatorState $state, |
|
489 | + Certificate $cert): ValidatorState |
|
490 | + { |
|
491 | + if (!$cert->isSelfIssued()) { |
|
492 | + if ($state->maxPathLength() <= 0) { |
|
493 | + throw new PathValidationException( |
|
494 | + 'Certification path length exceeded.'); |
|
495 | + } |
|
496 | + $state = $state->withMaxPathLength($state->maxPathLength() - 1); |
|
497 | + } |
|
498 | + return $state; |
|
499 | + } |
|
500 | 500 | |
501 | - /** |
|
502 | - * Check key usage extension for the preparation step. |
|
503 | - * |
|
504 | - * @param Certificate $cert |
|
505 | - * |
|
506 | - * @throws PathValidationException |
|
507 | - */ |
|
508 | - private function _checkKeyUsage(Certificate $cert): void |
|
509 | - { |
|
510 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
511 | - if ($extensions->hasKeyUsage()) { |
|
512 | - $ext = $extensions->keyUsage(); |
|
513 | - if (!$ext->isKeyCertSign()) { |
|
514 | - throw new PathValidationException('keyCertSign usage not set.'); |
|
515 | - } |
|
516 | - } |
|
517 | - } |
|
501 | + /** |
|
502 | + * Check key usage extension for the preparation step. |
|
503 | + * |
|
504 | + * @param Certificate $cert |
|
505 | + * |
|
506 | + * @throws PathValidationException |
|
507 | + */ |
|
508 | + private function _checkKeyUsage(Certificate $cert): void |
|
509 | + { |
|
510 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
511 | + if ($extensions->hasKeyUsage()) { |
|
512 | + $ext = $extensions->keyUsage(); |
|
513 | + if (!$ext->isKeyCertSign()) { |
|
514 | + throw new PathValidationException('keyCertSign usage not set.'); |
|
515 | + } |
|
516 | + } |
|
517 | + } |
|
518 | 518 | |
519 | - /** |
|
520 | - * @param ValidatorState $state |
|
521 | - * @param Certificate $cert |
|
522 | - * |
|
523 | - * @return ValidatorState |
|
524 | - */ |
|
525 | - private function _processNameConstraints(ValidatorState $state, |
|
526 | - Certificate $cert): ValidatorState |
|
527 | - { |
|
528 | - // @todo Implement |
|
529 | - return $state; |
|
530 | - } |
|
519 | + /** |
|
520 | + * @param ValidatorState $state |
|
521 | + * @param Certificate $cert |
|
522 | + * |
|
523 | + * @return ValidatorState |
|
524 | + */ |
|
525 | + private function _processNameConstraints(ValidatorState $state, |
|
526 | + Certificate $cert): ValidatorState |
|
527 | + { |
|
528 | + // @todo Implement |
|
529 | + return $state; |
|
530 | + } |
|
531 | 531 | |
532 | - /** |
|
533 | - * Process basic constraints extension. |
|
534 | - * |
|
535 | - * @param Certificate $cert |
|
536 | - * |
|
537 | - * @throws PathValidationException |
|
538 | - */ |
|
539 | - private function _processBasicContraints(Certificate $cert): void |
|
540 | - { |
|
541 | - if (TBSCertificate::VERSION_3 === $cert->tbsCertificate()->version()) { |
|
542 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
543 | - if (!$extensions->hasBasicConstraints()) { |
|
544 | - throw new PathValidationException( |
|
545 | - 'v3 certificate must have basicConstraints extension.'); |
|
546 | - } |
|
547 | - // verify that cA is set to TRUE |
|
548 | - if (!$extensions->basicConstraints()->isCA()) { |
|
549 | - throw new PathValidationException( |
|
550 | - 'Certificate is not a CA certificate.'); |
|
551 | - } |
|
552 | - } |
|
553 | - } |
|
532 | + /** |
|
533 | + * Process basic constraints extension. |
|
534 | + * |
|
535 | + * @param Certificate $cert |
|
536 | + * |
|
537 | + * @throws PathValidationException |
|
538 | + */ |
|
539 | + private function _processBasicContraints(Certificate $cert): void |
|
540 | + { |
|
541 | + if (TBSCertificate::VERSION_3 === $cert->tbsCertificate()->version()) { |
|
542 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
543 | + if (!$extensions->hasBasicConstraints()) { |
|
544 | + throw new PathValidationException( |
|
545 | + 'v3 certificate must have basicConstraints extension.'); |
|
546 | + } |
|
547 | + // verify that cA is set to TRUE |
|
548 | + if (!$extensions->basicConstraints()->isCA()) { |
|
549 | + throw new PathValidationException( |
|
550 | + 'Certificate is not a CA certificate.'); |
|
551 | + } |
|
552 | + } |
|
553 | + } |
|
554 | 554 | |
555 | - /** |
|
556 | - * Process pathLenConstraint. |
|
557 | - * |
|
558 | - * @param ValidatorState $state |
|
559 | - * @param Certificate $cert |
|
560 | - * |
|
561 | - * @return ValidatorState |
|
562 | - */ |
|
563 | - private function _processPathLengthContraint(ValidatorState $state, |
|
564 | - Certificate $cert): ValidatorState |
|
565 | - { |
|
566 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
567 | - if ($extensions->hasBasicConstraints()) { |
|
568 | - $ext = $extensions->basicConstraints(); |
|
569 | - if ($ext->hasPathLen()) { |
|
570 | - if ($ext->pathLen() < $state->maxPathLength()) { |
|
571 | - $state = $state->withMaxPathLength($ext->pathLen()); |
|
572 | - } |
|
573 | - } |
|
574 | - } |
|
575 | - return $state; |
|
576 | - } |
|
555 | + /** |
|
556 | + * Process pathLenConstraint. |
|
557 | + * |
|
558 | + * @param ValidatorState $state |
|
559 | + * @param Certificate $cert |
|
560 | + * |
|
561 | + * @return ValidatorState |
|
562 | + */ |
|
563 | + private function _processPathLengthContraint(ValidatorState $state, |
|
564 | + Certificate $cert): ValidatorState |
|
565 | + { |
|
566 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
567 | + if ($extensions->hasBasicConstraints()) { |
|
568 | + $ext = $extensions->basicConstraints(); |
|
569 | + if ($ext->hasPathLen()) { |
|
570 | + if ($ext->pathLen() < $state->maxPathLength()) { |
|
571 | + $state = $state->withMaxPathLength($ext->pathLen()); |
|
572 | + } |
|
573 | + } |
|
574 | + } |
|
575 | + return $state; |
|
576 | + } |
|
577 | 577 | |
578 | - /** |
|
579 | - * @param ValidatorState $state |
|
580 | - * @param Certificate $cert |
|
581 | - * |
|
582 | - * @return ValidatorState |
|
583 | - */ |
|
584 | - private function _processExtensions(ValidatorState $state, |
|
585 | - Certificate $cert): ValidatorState |
|
586 | - { |
|
587 | - // @todo Implement |
|
588 | - return $state; |
|
589 | - } |
|
578 | + /** |
|
579 | + * @param ValidatorState $state |
|
580 | + * @param Certificate $cert |
|
581 | + * |
|
582 | + * @return ValidatorState |
|
583 | + */ |
|
584 | + private function _processExtensions(ValidatorState $state, |
|
585 | + Certificate $cert): ValidatorState |
|
586 | + { |
|
587 | + // @todo Implement |
|
588 | + return $state; |
|
589 | + } |
|
590 | 590 | |
591 | - /** |
|
592 | - * @param ValidatorState $state |
|
593 | - * |
|
594 | - * @return ValidatorState |
|
595 | - */ |
|
596 | - private function _calculatePolicyIntersection(ValidatorState $state): ValidatorState |
|
597 | - { |
|
598 | - // (i) If the valid_policy_tree is NULL, the intersection is NULL |
|
599 | - if (!$state->hasValidPolicyTree()) { |
|
600 | - return $state; |
|
601 | - } |
|
602 | - // (ii) If the valid_policy_tree is not NULL and |
|
603 | - // the user-initial-policy-set is any-policy, the intersection |
|
604 | - // is the entire valid_policy_tree |
|
605 | - $initial_policies = $this->_config->policySet(); |
|
606 | - if (in_array(PolicyInformation::OID_ANY_POLICY, $initial_policies)) { |
|
607 | - return $state; |
|
608 | - } |
|
609 | - // (iii) If the valid_policy_tree is not NULL and the |
|
610 | - // user-initial-policy-set is not any-policy, calculate |
|
611 | - // the intersection of the valid_policy_tree and the |
|
612 | - // user-initial-policy-set as follows |
|
613 | - return $state->validPolicyTree()->calculateIntersection($state, $initial_policies); |
|
614 | - } |
|
591 | + /** |
|
592 | + * @param ValidatorState $state |
|
593 | + * |
|
594 | + * @return ValidatorState |
|
595 | + */ |
|
596 | + private function _calculatePolicyIntersection(ValidatorState $state): ValidatorState |
|
597 | + { |
|
598 | + // (i) If the valid_policy_tree is NULL, the intersection is NULL |
|
599 | + if (!$state->hasValidPolicyTree()) { |
|
600 | + return $state; |
|
601 | + } |
|
602 | + // (ii) If the valid_policy_tree is not NULL and |
|
603 | + // the user-initial-policy-set is any-policy, the intersection |
|
604 | + // is the entire valid_policy_tree |
|
605 | + $initial_policies = $this->_config->policySet(); |
|
606 | + if (in_array(PolicyInformation::OID_ANY_POLICY, $initial_policies)) { |
|
607 | + return $state; |
|
608 | + } |
|
609 | + // (iii) If the valid_policy_tree is not NULL and the |
|
610 | + // user-initial-policy-set is not any-policy, calculate |
|
611 | + // the intersection of the valid_policy_tree and the |
|
612 | + // user-initial-policy-set as follows |
|
613 | + return $state->validPolicyTree()->calculateIntersection($state, $initial_policies); |
|
614 | + } |
|
615 | 615 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\CertificationPath\PathValidation; |
6 | 6 |