@@ -19,235 +19,235 @@ |
||
19 | 19 | */ |
20 | 20 | class Certificate |
21 | 21 | { |
22 | - /** |
|
23 | - * "To be signed" certificate information. |
|
24 | - * |
|
25 | - * @var TBSCertificate $_tbsCertificate |
|
26 | - */ |
|
27 | - protected $_tbsCertificate; |
|
28 | - |
|
29 | - /** |
|
30 | - * Signature algorithm. |
|
31 | - * |
|
32 | - * @var SignatureAlgorithmIdentifier $_signatureAlgorithm |
|
33 | - */ |
|
34 | - protected $_signatureAlgorithm; |
|
35 | - |
|
36 | - /** |
|
37 | - * Signature value. |
|
38 | - * |
|
39 | - * @var Signature $_signatureValue |
|
40 | - */ |
|
41 | - protected $_signatureValue; |
|
42 | - |
|
43 | - /** |
|
44 | - * Constructor. |
|
45 | - * |
|
46 | - * @param TBSCertificate $tbsCert |
|
47 | - * @param SignatureAlgorithmIdentifier $algo |
|
48 | - * @param Signature $signature |
|
49 | - */ |
|
50 | - public function __construct(TBSCertificate $tbsCert, |
|
51 | - SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
52 | - { |
|
53 | - $this->_tbsCertificate = $tbsCert; |
|
54 | - $this->_signatureAlgorithm = $algo; |
|
55 | - $this->_signatureValue = $signature; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Initialize from ASN.1. |
|
60 | - * |
|
61 | - * @param Sequence $seq |
|
62 | - * @return self |
|
63 | - */ |
|
64 | - public static function fromASN1(Sequence $seq) |
|
65 | - { |
|
66 | - $tbsCert = TBSCertificate::fromASN1($seq->at(0)->asSequence()); |
|
67 | - $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
68 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
69 | - throw new \UnexpectedValueException( |
|
70 | - "Unsupported signature algorithm " . $algo->oid() . "."); |
|
71 | - } |
|
72 | - $signature = Signature::fromSignatureData( |
|
73 | - $seq->at(2) |
|
74 | - ->asBitString() |
|
75 | - ->string(), $algo); |
|
76 | - return new self($tbsCert, $algo, $signature); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Initialize from DER. |
|
81 | - * |
|
82 | - * @param string $data |
|
83 | - * @return self |
|
84 | - */ |
|
85 | - public static function fromDER(string $data) |
|
86 | - { |
|
87 | - return self::fromASN1(Sequence::fromDER($data)); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Initialize from PEM. |
|
92 | - * |
|
93 | - * @param PEM $pem |
|
94 | - * @throws \UnexpectedValueException |
|
95 | - * @return self |
|
96 | - */ |
|
97 | - public static function fromPEM(PEM $pem) |
|
98 | - { |
|
99 | - if ($pem->type() != PEM::TYPE_CERTIFICATE) { |
|
100 | - throw new \UnexpectedValueException("Invalid PEM type."); |
|
101 | - } |
|
102 | - return self::fromDER($pem->data()); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Get certificate information. |
|
107 | - * |
|
108 | - * @return TBSCertificate |
|
109 | - */ |
|
110 | - public function tbsCertificate(): TBSCertificate |
|
111 | - { |
|
112 | - return $this->_tbsCertificate; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Get signature algorithm. |
|
117 | - * |
|
118 | - * @return SignatureAlgorithmIdentifier |
|
119 | - */ |
|
120 | - public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
121 | - { |
|
122 | - return $this->_signatureAlgorithm; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Get signature value. |
|
127 | - * |
|
128 | - * @return Signature |
|
129 | - */ |
|
130 | - public function signatureValue(): Signature |
|
131 | - { |
|
132 | - return $this->_signatureValue; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Check whether certificate is self-issued. |
|
137 | - * |
|
138 | - * @return bool |
|
139 | - */ |
|
140 | - public function isSelfIssued(): bool |
|
141 | - { |
|
142 | - return $this->_tbsCertificate->subject()->equals( |
|
143 | - $this->_tbsCertificate->issuer()); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Check whether certificate is semantically equal to another. |
|
148 | - * |
|
149 | - * @param Certificate $cert Certificate to compare to |
|
150 | - * @return bool |
|
151 | - */ |
|
152 | - public function equals(Certificate $cert): bool |
|
153 | - { |
|
154 | - return $this->_hasEqualSerialNumber($cert) && |
|
155 | - $this->_hasEqualPublicKey($cert) && $this->_hasEqualSubject($cert); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Check whether certificate has serial number equal to another. |
|
160 | - * |
|
161 | - * @param Certificate $cert |
|
162 | - * @return bool |
|
163 | - */ |
|
164 | - private function _hasEqualSerialNumber(Certificate $cert): bool |
|
165 | - { |
|
166 | - $sn1 = $this->_tbsCertificate->serialNumber(); |
|
167 | - $sn2 = $cert->_tbsCertificate->serialNumber(); |
|
168 | - return $sn1 == $sn2; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Check whether certificate has public key equal to another. |
|
173 | - * |
|
174 | - * @param Certificate $cert |
|
175 | - * @return bool |
|
176 | - */ |
|
177 | - private function _hasEqualPublicKey(Certificate $cert): bool |
|
178 | - { |
|
179 | - $kid1 = $this->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
180 | - $kid2 = $cert->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
181 | - return $kid1 == $kid2; |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Check whether certificate has subject equal to another. |
|
186 | - * |
|
187 | - * @param Certificate $cert |
|
188 | - * @return bool |
|
189 | - */ |
|
190 | - private function _hasEqualSubject(Certificate $cert): bool |
|
191 | - { |
|
192 | - $dn1 = $this->_tbsCertificate->subject(); |
|
193 | - $dn2 = $cert->_tbsCertificate->subject(); |
|
194 | - return $dn1->equals($dn2); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Generate ASN.1 structure. |
|
199 | - * |
|
200 | - * @return Sequence |
|
201 | - */ |
|
202 | - public function toASN1(): Sequence |
|
203 | - { |
|
204 | - return new Sequence($this->_tbsCertificate->toASN1(), |
|
205 | - $this->_signatureAlgorithm->toASN1(), |
|
206 | - $this->_signatureValue->bitString()); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Get certificate as a DER. |
|
211 | - * |
|
212 | - * @return string |
|
213 | - */ |
|
214 | - public function toDER(): string |
|
215 | - { |
|
216 | - return $this->toASN1()->toDER(); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Get certificate as a PEM. |
|
221 | - * |
|
222 | - * @return PEM |
|
223 | - */ |
|
224 | - public function toPEM(): PEM |
|
225 | - { |
|
226 | - return new PEM(PEM::TYPE_CERTIFICATE, $this->toDER()); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Verify certificate signature. |
|
231 | - * |
|
232 | - * @param PublicKeyInfo $pubkey_info Issuer's public key |
|
233 | - * @param Crypto|null $crypto Crypto engine, use default if not set |
|
234 | - * @return bool True if certificate signature is valid |
|
235 | - */ |
|
236 | - public function verify(PublicKeyInfo $pubkey_info, Crypto $crypto = null): bool |
|
237 | - { |
|
238 | - $crypto = $crypto ?: Crypto::getDefault(); |
|
239 | - $data = $this->_tbsCertificate->toASN1()->toDER(); |
|
240 | - return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
241 | - $this->_signatureAlgorithm); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Get certificate as a PEM formatted string. |
|
246 | - * |
|
247 | - * @return string |
|
248 | - */ |
|
249 | - public function __toString() |
|
250 | - { |
|
251 | - return $this->toPEM()->string(); |
|
252 | - } |
|
22 | + /** |
|
23 | + * "To be signed" certificate information. |
|
24 | + * |
|
25 | + * @var TBSCertificate $_tbsCertificate |
|
26 | + */ |
|
27 | + protected $_tbsCertificate; |
|
28 | + |
|
29 | + /** |
|
30 | + * Signature algorithm. |
|
31 | + * |
|
32 | + * @var SignatureAlgorithmIdentifier $_signatureAlgorithm |
|
33 | + */ |
|
34 | + protected $_signatureAlgorithm; |
|
35 | + |
|
36 | + /** |
|
37 | + * Signature value. |
|
38 | + * |
|
39 | + * @var Signature $_signatureValue |
|
40 | + */ |
|
41 | + protected $_signatureValue; |
|
42 | + |
|
43 | + /** |
|
44 | + * Constructor. |
|
45 | + * |
|
46 | + * @param TBSCertificate $tbsCert |
|
47 | + * @param SignatureAlgorithmIdentifier $algo |
|
48 | + * @param Signature $signature |
|
49 | + */ |
|
50 | + public function __construct(TBSCertificate $tbsCert, |
|
51 | + SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
52 | + { |
|
53 | + $this->_tbsCertificate = $tbsCert; |
|
54 | + $this->_signatureAlgorithm = $algo; |
|
55 | + $this->_signatureValue = $signature; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Initialize from ASN.1. |
|
60 | + * |
|
61 | + * @param Sequence $seq |
|
62 | + * @return self |
|
63 | + */ |
|
64 | + public static function fromASN1(Sequence $seq) |
|
65 | + { |
|
66 | + $tbsCert = TBSCertificate::fromASN1($seq->at(0)->asSequence()); |
|
67 | + $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
68 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
69 | + throw new \UnexpectedValueException( |
|
70 | + "Unsupported signature algorithm " . $algo->oid() . "."); |
|
71 | + } |
|
72 | + $signature = Signature::fromSignatureData( |
|
73 | + $seq->at(2) |
|
74 | + ->asBitString() |
|
75 | + ->string(), $algo); |
|
76 | + return new self($tbsCert, $algo, $signature); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Initialize from DER. |
|
81 | + * |
|
82 | + * @param string $data |
|
83 | + * @return self |
|
84 | + */ |
|
85 | + public static function fromDER(string $data) |
|
86 | + { |
|
87 | + return self::fromASN1(Sequence::fromDER($data)); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Initialize from PEM. |
|
92 | + * |
|
93 | + * @param PEM $pem |
|
94 | + * @throws \UnexpectedValueException |
|
95 | + * @return self |
|
96 | + */ |
|
97 | + public static function fromPEM(PEM $pem) |
|
98 | + { |
|
99 | + if ($pem->type() != PEM::TYPE_CERTIFICATE) { |
|
100 | + throw new \UnexpectedValueException("Invalid PEM type."); |
|
101 | + } |
|
102 | + return self::fromDER($pem->data()); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Get certificate information. |
|
107 | + * |
|
108 | + * @return TBSCertificate |
|
109 | + */ |
|
110 | + public function tbsCertificate(): TBSCertificate |
|
111 | + { |
|
112 | + return $this->_tbsCertificate; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Get signature algorithm. |
|
117 | + * |
|
118 | + * @return SignatureAlgorithmIdentifier |
|
119 | + */ |
|
120 | + public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
121 | + { |
|
122 | + return $this->_signatureAlgorithm; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Get signature value. |
|
127 | + * |
|
128 | + * @return Signature |
|
129 | + */ |
|
130 | + public function signatureValue(): Signature |
|
131 | + { |
|
132 | + return $this->_signatureValue; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Check whether certificate is self-issued. |
|
137 | + * |
|
138 | + * @return bool |
|
139 | + */ |
|
140 | + public function isSelfIssued(): bool |
|
141 | + { |
|
142 | + return $this->_tbsCertificate->subject()->equals( |
|
143 | + $this->_tbsCertificate->issuer()); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Check whether certificate is semantically equal to another. |
|
148 | + * |
|
149 | + * @param Certificate $cert Certificate to compare to |
|
150 | + * @return bool |
|
151 | + */ |
|
152 | + public function equals(Certificate $cert): bool |
|
153 | + { |
|
154 | + return $this->_hasEqualSerialNumber($cert) && |
|
155 | + $this->_hasEqualPublicKey($cert) && $this->_hasEqualSubject($cert); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Check whether certificate has serial number equal to another. |
|
160 | + * |
|
161 | + * @param Certificate $cert |
|
162 | + * @return bool |
|
163 | + */ |
|
164 | + private function _hasEqualSerialNumber(Certificate $cert): bool |
|
165 | + { |
|
166 | + $sn1 = $this->_tbsCertificate->serialNumber(); |
|
167 | + $sn2 = $cert->_tbsCertificate->serialNumber(); |
|
168 | + return $sn1 == $sn2; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Check whether certificate has public key equal to another. |
|
173 | + * |
|
174 | + * @param Certificate $cert |
|
175 | + * @return bool |
|
176 | + */ |
|
177 | + private function _hasEqualPublicKey(Certificate $cert): bool |
|
178 | + { |
|
179 | + $kid1 = $this->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
180 | + $kid2 = $cert->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
181 | + return $kid1 == $kid2; |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Check whether certificate has subject equal to another. |
|
186 | + * |
|
187 | + * @param Certificate $cert |
|
188 | + * @return bool |
|
189 | + */ |
|
190 | + private function _hasEqualSubject(Certificate $cert): bool |
|
191 | + { |
|
192 | + $dn1 = $this->_tbsCertificate->subject(); |
|
193 | + $dn2 = $cert->_tbsCertificate->subject(); |
|
194 | + return $dn1->equals($dn2); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Generate ASN.1 structure. |
|
199 | + * |
|
200 | + * @return Sequence |
|
201 | + */ |
|
202 | + public function toASN1(): Sequence |
|
203 | + { |
|
204 | + return new Sequence($this->_tbsCertificate->toASN1(), |
|
205 | + $this->_signatureAlgorithm->toASN1(), |
|
206 | + $this->_signatureValue->bitString()); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Get certificate as a DER. |
|
211 | + * |
|
212 | + * @return string |
|
213 | + */ |
|
214 | + public function toDER(): string |
|
215 | + { |
|
216 | + return $this->toASN1()->toDER(); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Get certificate as a PEM. |
|
221 | + * |
|
222 | + * @return PEM |
|
223 | + */ |
|
224 | + public function toPEM(): PEM |
|
225 | + { |
|
226 | + return new PEM(PEM::TYPE_CERTIFICATE, $this->toDER()); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Verify certificate signature. |
|
231 | + * |
|
232 | + * @param PublicKeyInfo $pubkey_info Issuer's public key |
|
233 | + * @param Crypto|null $crypto Crypto engine, use default if not set |
|
234 | + * @return bool True if certificate signature is valid |
|
235 | + */ |
|
236 | + public function verify(PublicKeyInfo $pubkey_info, Crypto $crypto = null): bool |
|
237 | + { |
|
238 | + $crypto = $crypto ?: Crypto::getDefault(); |
|
239 | + $data = $this->_tbsCertificate->toASN1()->toDER(); |
|
240 | + return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
241 | + $this->_signatureAlgorithm); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Get certificate as a PEM formatted string. |
|
246 | + * |
|
247 | + * @return string |
|
248 | + */ |
|
249 | + public function __toString() |
|
250 | + { |
|
251 | + return $this->toPEM()->string(); |
|
252 | + } |
|
253 | 253 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\Certificate; |
6 | 6 |
@@ -18,413 +18,413 @@ |
||
18 | 18 | */ |
19 | 19 | class Extensions implements \Countable, \IteratorAggregate |
20 | 20 | { |
21 | - /** |
|
22 | - * Extensions. |
|
23 | - * |
|
24 | - * @var Extension\Extension[] $_extensions |
|
25 | - */ |
|
26 | - protected $_extensions; |
|
27 | - |
|
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param Extension\Extension[] ...$extensions Extension objects |
|
32 | - */ |
|
33 | - public function __construct(Extension\Extension ...$extensions) |
|
34 | - { |
|
35 | - $this->_extensions = array(); |
|
36 | - foreach ($extensions as $ext) { |
|
37 | - $this->_extensions[$ext->oid()] = $ext; |
|
38 | - } |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * Initialize from ASN.1. |
|
43 | - * |
|
44 | - * @param Sequence $seq |
|
45 | - * @return self |
|
46 | - */ |
|
47 | - public static function fromASN1(Sequence $seq) |
|
48 | - { |
|
49 | - $extensions = array_map( |
|
50 | - function (UnspecifiedType $el) { |
|
51 | - return Extension\Extension::fromASN1($el->asSequence()); |
|
52 | - }, $seq->elements()); |
|
53 | - return new self(...$extensions); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Generate ASN.1 structure. |
|
58 | - * |
|
59 | - * @return Sequence |
|
60 | - */ |
|
61 | - public function toASN1(): Sequence |
|
62 | - { |
|
63 | - $elements = array_values( |
|
64 | - array_map( |
|
65 | - function ($ext) { |
|
66 | - return $ext->toASN1(); |
|
67 | - }, $this->_extensions)); |
|
68 | - return new Sequence(...$elements); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Get self with extensions added. |
|
73 | - * |
|
74 | - * @param Extension\Extension ...$ext One or more extensions to add |
|
75 | - * @return self |
|
76 | - */ |
|
77 | - public function withExtensions(Extension\Extension ...$exts) |
|
78 | - { |
|
79 | - $obj = clone $this; |
|
80 | - foreach ($exts as $ext) { |
|
81 | - $obj->_extensions[$ext->oid()] = $ext; |
|
82 | - } |
|
83 | - return $obj; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Check whether extension is present. |
|
88 | - * |
|
89 | - * @param string $oid Extensions OID |
|
90 | - * @return bool |
|
91 | - */ |
|
92 | - public function has(string $oid): bool |
|
93 | - { |
|
94 | - return isset($this->_extensions[$oid]); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Get extension by OID. |
|
99 | - * |
|
100 | - * @param string $oid |
|
101 | - * @throws \LogicException If extension is not present |
|
102 | - * @return Extension\Extension |
|
103 | - */ |
|
104 | - public function get(string $oid): Extension\Extension |
|
105 | - { |
|
106 | - if (!$this->has($oid)) { |
|
107 | - throw new \LogicException("No extension by OID $oid."); |
|
108 | - } |
|
109 | - return $this->_extensions[$oid]; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Check whether 'Authority Key Identifier' extension is present. |
|
114 | - * |
|
115 | - * @return bool |
|
116 | - */ |
|
117 | - public function hasAuthorityKeyIdentifier(): bool |
|
118 | - { |
|
119 | - return $this->has(Extension\Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Get 'Authority Key Identifier' extension. |
|
124 | - * |
|
125 | - * @throws \LogicException If extension is not present |
|
126 | - * @return \X509\Certificate\Extension\AuthorityKeyIdentifierExtension |
|
127 | - */ |
|
128 | - public function authorityKeyIdentifier(): Extension\AuthorityKeyIdentifierExtension |
|
129 | - { |
|
130 | - /** @var Extension\AuthorityKeyIdentifierExtension $keyIdentifier */ |
|
131 | - $keyIdentifier = $this->get(Extension\Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
|
132 | - return $keyIdentifier; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Check whether 'Subject Key Identifier' extension is present. |
|
137 | - * |
|
138 | - * @return bool |
|
139 | - */ |
|
140 | - public function hasSubjectKeyIdentifier(): bool |
|
141 | - { |
|
142 | - return $this->has(Extension\Extension::OID_SUBJECT_KEY_IDENTIFIER); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Get 'Subject Key Identifier' extension. |
|
147 | - * |
|
148 | - * @throws \LogicException If extension is not present |
|
149 | - * @return \X509\Certificate\Extension\SubjectKeyIdentifierExtension |
|
150 | - */ |
|
151 | - public function subjectKeyIdentifier() |
|
152 | - { |
|
153 | - /** @var Extension\SubjectKeyIdentifierExtension $subjectKeyIdentifier */ |
|
154 | - $subjectKeyIdentifier = $this->get(Extension\Extension::OID_SUBJECT_KEY_IDENTIFIER); |
|
155 | - return $subjectKeyIdentifier; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Check whether 'Key Usage' extension is present. |
|
160 | - * |
|
161 | - * @return bool |
|
162 | - */ |
|
163 | - public function hasKeyUsage(): bool |
|
164 | - { |
|
165 | - return $this->has(Extension\Extension::OID_KEY_USAGE); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Get 'Key Usage' extension. |
|
170 | - * |
|
171 | - * @throws \LogicException If extension is not present |
|
172 | - * @return \X509\Certificate\Extension\KeyUsageExtension |
|
173 | - */ |
|
174 | - public function keyUsage() |
|
175 | - { |
|
176 | - /** @var Extension\KeyUsageExtension $keyUsage */ |
|
177 | - $keyUsage = $this->get(Extension\Extension::OID_KEY_USAGE); |
|
178 | - return $keyUsage; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Check whether 'Certificate Policies' extension is present. |
|
183 | - * |
|
184 | - * @return bool |
|
185 | - */ |
|
186 | - public function hasCertificatePolicies(): bool |
|
187 | - { |
|
188 | - return $this->has(Extension\Extension::OID_CERTIFICATE_POLICIES); |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Get 'Certificate Policies' extension. |
|
193 | - * |
|
194 | - * @throws \LogicException If extension is not present |
|
195 | - * @return \X509\Certificate\Extension\CertificatePoliciesExtension |
|
196 | - */ |
|
197 | - public function certificatePolicies() |
|
198 | - { |
|
199 | - /** @var Extension\CertificatePoliciesExtension $certPolicies */ |
|
200 | - $certPolicies = $this->get(Extension\Extension::OID_CERTIFICATE_POLICIES); |
|
201 | - return $certPolicies; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Check whether 'Policy Mappings' extension is present. |
|
206 | - * |
|
207 | - * @return bool |
|
208 | - */ |
|
209 | - public function hasPolicyMappings(): bool |
|
210 | - { |
|
211 | - return $this->has(Extension\Extension::OID_POLICY_MAPPINGS); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Get 'Policy Mappings' extension. |
|
216 | - * |
|
217 | - * @throws \LogicException If extension is not present |
|
218 | - * @return \X509\Certificate\Extension\PolicyMappingsExtension |
|
219 | - */ |
|
220 | - public function policyMappings() |
|
221 | - { |
|
222 | - /** @var Extension\PolicyMappingsExtension $policyMappings */ |
|
223 | - $policyMappings = $this->get(Extension\Extension::OID_POLICY_MAPPINGS); |
|
224 | - return $policyMappings; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Check whether 'Subject Alternative Name' extension is present. |
|
229 | - * |
|
230 | - * @return bool |
|
231 | - */ |
|
232 | - public function hasSubjectAlternativeName(): bool |
|
233 | - { |
|
234 | - return $this->has(Extension\Extension::OID_SUBJECT_ALT_NAME); |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Get 'Subject Alternative Name' extension. |
|
239 | - * |
|
240 | - * @throws \LogicException If extension is not present |
|
241 | - * @return \X509\Certificate\Extension\SubjectAlternativeNameExtension |
|
242 | - */ |
|
243 | - public function subjectAlternativeName() |
|
244 | - { |
|
245 | - /** @var Extension\SubjectAlternativeNameExtension $subjectAltName */ |
|
246 | - $subjectAltName = $this->get(Extension\Extension::OID_SUBJECT_ALT_NAME); |
|
247 | - return $subjectAltName; |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Check whether 'Issuer Alternative Name' extension is present. |
|
252 | - * |
|
253 | - * @return bool |
|
254 | - */ |
|
255 | - public function hasIssuerAlternativeName(): bool |
|
256 | - { |
|
257 | - return $this->has(Extension\Extension::OID_ISSUER_ALT_NAME); |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Get 'Issuer Alternative Name' extension. |
|
262 | - * |
|
263 | - * @return \X509\Certificate\Extension\IssuerAlternativeNameExtension |
|
264 | - */ |
|
265 | - public function issuerAlternativeName() |
|
266 | - { |
|
267 | - /** @var Extension\IssuerAlternativeNameExtension $issuerAltName */ |
|
268 | - $issuerAltName = $this->get(Extension\Extension::OID_ISSUER_ALT_NAME); |
|
269 | - return $issuerAltName; |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Check whether 'Basic Constraints' extension is present. |
|
274 | - * |
|
275 | - * @return bool |
|
276 | - */ |
|
277 | - public function hasBasicConstraints(): bool |
|
278 | - { |
|
279 | - return $this->has(Extension\Extension::OID_BASIC_CONSTRAINTS); |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * Get 'Basic Constraints' extension. |
|
284 | - * |
|
285 | - * @throws \LogicException If extension is not present |
|
286 | - * @return \X509\Certificate\Extension\BasicConstraintsExtension |
|
287 | - */ |
|
288 | - public function basicConstraints() |
|
289 | - { |
|
290 | - /** @var Extension\BasicConstraintsExtension $basicConstraints */ |
|
291 | - $basicConstraints = $this->get(Extension\Extension::OID_BASIC_CONSTRAINTS); |
|
292 | - return $basicConstraints; |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * Check whether 'Name Constraints' extension is present. |
|
297 | - * |
|
298 | - * @return bool |
|
299 | - */ |
|
300 | - public function hasNameConstraints(): bool |
|
301 | - { |
|
302 | - return $this->has(Extension\Extension::OID_NAME_CONSTRAINTS); |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Get 'Name Constraints' extension. |
|
307 | - * |
|
308 | - * @throws \LogicException If extension is not present |
|
309 | - * @return \X509\Certificate\Extension\NameConstraintsExtension |
|
310 | - */ |
|
311 | - public function nameConstraints() |
|
312 | - { |
|
313 | - /** @var Extension\NameConstraintsExtension $nameConstraints */ |
|
314 | - $nameConstraints = $this->get(Extension\Extension::OID_NAME_CONSTRAINTS); |
|
315 | - return $nameConstraints; |
|
316 | - } |
|
317 | - |
|
318 | - /** |
|
319 | - * Check whether 'Policy Constraints' extension is present. |
|
320 | - * |
|
321 | - * @return bool |
|
322 | - */ |
|
323 | - public function hasPolicyConstraints(): bool |
|
324 | - { |
|
325 | - return $this->has(Extension\Extension::OID_POLICY_CONSTRAINTS); |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Get 'Policy Constraints' extension. |
|
330 | - * |
|
331 | - * @throws \LogicException If extension is not present |
|
332 | - * @return \X509\Certificate\Extension\PolicyConstraintsExtension |
|
333 | - */ |
|
334 | - public function policyConstraints() |
|
335 | - { |
|
336 | - /** @var Extension\PolicyConstraintsExtension $policyConstraints */ |
|
337 | - $policyConstraints = $this->get(Extension\Extension::OID_POLICY_CONSTRAINTS); |
|
338 | - return $policyConstraints; |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * Check whether 'Extended Key Usage' extension is present. |
|
343 | - * |
|
344 | - * @return bool |
|
345 | - */ |
|
346 | - public function hasExtendedKeyUsage(): bool |
|
347 | - { |
|
348 | - return $this->has(Extension\Extension::OID_EXT_KEY_USAGE); |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * Get 'Extended Key Usage' extension. |
|
353 | - * |
|
354 | - * @throws \LogicException If extension is not present |
|
355 | - * @return \X509\Certificate\Extension\ExtendedKeyUsageExtension |
|
356 | - */ |
|
357 | - public function extendedKeyUsage() |
|
358 | - { |
|
359 | - /** @var Extension\ExtendedKeyUsageExtension $keyUsage */ |
|
360 | - $keyUsage = $this->get(Extension\Extension::OID_EXT_KEY_USAGE); |
|
361 | - return $keyUsage; |
|
362 | - } |
|
363 | - |
|
364 | - /** |
|
365 | - * Check whether 'CRL Distribution Points' extension is present. |
|
366 | - * |
|
367 | - * @return bool |
|
368 | - */ |
|
369 | - public function hasCRLDistributionPoints(): bool |
|
370 | - { |
|
371 | - return $this->has(Extension\Extension::OID_CRL_DISTRIBUTION_POINTS); |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * Get 'CRL Distribution Points' extension. |
|
376 | - * |
|
377 | - * @throws \LogicException If extension is not present |
|
378 | - * @return \X509\Certificate\Extension\CRLDistributionPointsExtension |
|
379 | - */ |
|
380 | - public function crlDistributionPoints() |
|
381 | - { |
|
382 | - /** @var Extension\CRLDistributionPointsExtension $crlDist */ |
|
383 | - $crlDist = $this->get(Extension\Extension::OID_CRL_DISTRIBUTION_POINTS); |
|
384 | - return $crlDist; |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Check whether 'Inhibit anyPolicy' extension is present. |
|
389 | - * |
|
390 | - * @return bool |
|
391 | - */ |
|
392 | - public function hasInhibitAnyPolicy(): bool |
|
393 | - { |
|
394 | - return $this->has(Extension\Extension::OID_INHIBIT_ANY_POLICY); |
|
395 | - } |
|
396 | - |
|
397 | - /** |
|
398 | - * Get 'Inhibit anyPolicy' extension. |
|
399 | - * |
|
400 | - * @throws \LogicException If extension is not present |
|
401 | - * @return \X509\Certificate\Extension\InhibitAnyPolicyExtension |
|
402 | - */ |
|
403 | - public function inhibitAnyPolicy() |
|
404 | - { |
|
405 | - /** @var Extension\InhibitAnyPolicyExtension $inhibitAny */ |
|
406 | - $inhibitAny = $this->get(Extension\Extension::OID_INHIBIT_ANY_POLICY); |
|
407 | - return $inhibitAny; |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * |
|
412 | - * @see \Countable::count() |
|
413 | - * @return int |
|
414 | - */ |
|
415 | - public function count(): int |
|
416 | - { |
|
417 | - return count($this->_extensions); |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Get iterator for extensions. |
|
422 | - * |
|
423 | - * @see \IteratorAggregate::getIterator() |
|
424 | - * @return \Traversable |
|
425 | - */ |
|
426 | - public function getIterator(): \Traversable |
|
427 | - { |
|
428 | - return new \ArrayIterator($this->_extensions); |
|
429 | - } |
|
21 | + /** |
|
22 | + * Extensions. |
|
23 | + * |
|
24 | + * @var Extension\Extension[] $_extensions |
|
25 | + */ |
|
26 | + protected $_extensions; |
|
27 | + |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param Extension\Extension[] ...$extensions Extension objects |
|
32 | + */ |
|
33 | + public function __construct(Extension\Extension ...$extensions) |
|
34 | + { |
|
35 | + $this->_extensions = array(); |
|
36 | + foreach ($extensions as $ext) { |
|
37 | + $this->_extensions[$ext->oid()] = $ext; |
|
38 | + } |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * Initialize from ASN.1. |
|
43 | + * |
|
44 | + * @param Sequence $seq |
|
45 | + * @return self |
|
46 | + */ |
|
47 | + public static function fromASN1(Sequence $seq) |
|
48 | + { |
|
49 | + $extensions = array_map( |
|
50 | + function (UnspecifiedType $el) { |
|
51 | + return Extension\Extension::fromASN1($el->asSequence()); |
|
52 | + }, $seq->elements()); |
|
53 | + return new self(...$extensions); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Generate ASN.1 structure. |
|
58 | + * |
|
59 | + * @return Sequence |
|
60 | + */ |
|
61 | + public function toASN1(): Sequence |
|
62 | + { |
|
63 | + $elements = array_values( |
|
64 | + array_map( |
|
65 | + function ($ext) { |
|
66 | + return $ext->toASN1(); |
|
67 | + }, $this->_extensions)); |
|
68 | + return new Sequence(...$elements); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Get self with extensions added. |
|
73 | + * |
|
74 | + * @param Extension\Extension ...$ext One or more extensions to add |
|
75 | + * @return self |
|
76 | + */ |
|
77 | + public function withExtensions(Extension\Extension ...$exts) |
|
78 | + { |
|
79 | + $obj = clone $this; |
|
80 | + foreach ($exts as $ext) { |
|
81 | + $obj->_extensions[$ext->oid()] = $ext; |
|
82 | + } |
|
83 | + return $obj; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Check whether extension is present. |
|
88 | + * |
|
89 | + * @param string $oid Extensions OID |
|
90 | + * @return bool |
|
91 | + */ |
|
92 | + public function has(string $oid): bool |
|
93 | + { |
|
94 | + return isset($this->_extensions[$oid]); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Get extension by OID. |
|
99 | + * |
|
100 | + * @param string $oid |
|
101 | + * @throws \LogicException If extension is not present |
|
102 | + * @return Extension\Extension |
|
103 | + */ |
|
104 | + public function get(string $oid): Extension\Extension |
|
105 | + { |
|
106 | + if (!$this->has($oid)) { |
|
107 | + throw new \LogicException("No extension by OID $oid."); |
|
108 | + } |
|
109 | + return $this->_extensions[$oid]; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Check whether 'Authority Key Identifier' extension is present. |
|
114 | + * |
|
115 | + * @return bool |
|
116 | + */ |
|
117 | + public function hasAuthorityKeyIdentifier(): bool |
|
118 | + { |
|
119 | + return $this->has(Extension\Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Get 'Authority Key Identifier' extension. |
|
124 | + * |
|
125 | + * @throws \LogicException If extension is not present |
|
126 | + * @return \X509\Certificate\Extension\AuthorityKeyIdentifierExtension |
|
127 | + */ |
|
128 | + public function authorityKeyIdentifier(): Extension\AuthorityKeyIdentifierExtension |
|
129 | + { |
|
130 | + /** @var Extension\AuthorityKeyIdentifierExtension $keyIdentifier */ |
|
131 | + $keyIdentifier = $this->get(Extension\Extension::OID_AUTHORITY_KEY_IDENTIFIER); |
|
132 | + return $keyIdentifier; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Check whether 'Subject Key Identifier' extension is present. |
|
137 | + * |
|
138 | + * @return bool |
|
139 | + */ |
|
140 | + public function hasSubjectKeyIdentifier(): bool |
|
141 | + { |
|
142 | + return $this->has(Extension\Extension::OID_SUBJECT_KEY_IDENTIFIER); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Get 'Subject Key Identifier' extension. |
|
147 | + * |
|
148 | + * @throws \LogicException If extension is not present |
|
149 | + * @return \X509\Certificate\Extension\SubjectKeyIdentifierExtension |
|
150 | + */ |
|
151 | + public function subjectKeyIdentifier() |
|
152 | + { |
|
153 | + /** @var Extension\SubjectKeyIdentifierExtension $subjectKeyIdentifier */ |
|
154 | + $subjectKeyIdentifier = $this->get(Extension\Extension::OID_SUBJECT_KEY_IDENTIFIER); |
|
155 | + return $subjectKeyIdentifier; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Check whether 'Key Usage' extension is present. |
|
160 | + * |
|
161 | + * @return bool |
|
162 | + */ |
|
163 | + public function hasKeyUsage(): bool |
|
164 | + { |
|
165 | + return $this->has(Extension\Extension::OID_KEY_USAGE); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Get 'Key Usage' extension. |
|
170 | + * |
|
171 | + * @throws \LogicException If extension is not present |
|
172 | + * @return \X509\Certificate\Extension\KeyUsageExtension |
|
173 | + */ |
|
174 | + public function keyUsage() |
|
175 | + { |
|
176 | + /** @var Extension\KeyUsageExtension $keyUsage */ |
|
177 | + $keyUsage = $this->get(Extension\Extension::OID_KEY_USAGE); |
|
178 | + return $keyUsage; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Check whether 'Certificate Policies' extension is present. |
|
183 | + * |
|
184 | + * @return bool |
|
185 | + */ |
|
186 | + public function hasCertificatePolicies(): bool |
|
187 | + { |
|
188 | + return $this->has(Extension\Extension::OID_CERTIFICATE_POLICIES); |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Get 'Certificate Policies' extension. |
|
193 | + * |
|
194 | + * @throws \LogicException If extension is not present |
|
195 | + * @return \X509\Certificate\Extension\CertificatePoliciesExtension |
|
196 | + */ |
|
197 | + public function certificatePolicies() |
|
198 | + { |
|
199 | + /** @var Extension\CertificatePoliciesExtension $certPolicies */ |
|
200 | + $certPolicies = $this->get(Extension\Extension::OID_CERTIFICATE_POLICIES); |
|
201 | + return $certPolicies; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Check whether 'Policy Mappings' extension is present. |
|
206 | + * |
|
207 | + * @return bool |
|
208 | + */ |
|
209 | + public function hasPolicyMappings(): bool |
|
210 | + { |
|
211 | + return $this->has(Extension\Extension::OID_POLICY_MAPPINGS); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Get 'Policy Mappings' extension. |
|
216 | + * |
|
217 | + * @throws \LogicException If extension is not present |
|
218 | + * @return \X509\Certificate\Extension\PolicyMappingsExtension |
|
219 | + */ |
|
220 | + public function policyMappings() |
|
221 | + { |
|
222 | + /** @var Extension\PolicyMappingsExtension $policyMappings */ |
|
223 | + $policyMappings = $this->get(Extension\Extension::OID_POLICY_MAPPINGS); |
|
224 | + return $policyMappings; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Check whether 'Subject Alternative Name' extension is present. |
|
229 | + * |
|
230 | + * @return bool |
|
231 | + */ |
|
232 | + public function hasSubjectAlternativeName(): bool |
|
233 | + { |
|
234 | + return $this->has(Extension\Extension::OID_SUBJECT_ALT_NAME); |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Get 'Subject Alternative Name' extension. |
|
239 | + * |
|
240 | + * @throws \LogicException If extension is not present |
|
241 | + * @return \X509\Certificate\Extension\SubjectAlternativeNameExtension |
|
242 | + */ |
|
243 | + public function subjectAlternativeName() |
|
244 | + { |
|
245 | + /** @var Extension\SubjectAlternativeNameExtension $subjectAltName */ |
|
246 | + $subjectAltName = $this->get(Extension\Extension::OID_SUBJECT_ALT_NAME); |
|
247 | + return $subjectAltName; |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Check whether 'Issuer Alternative Name' extension is present. |
|
252 | + * |
|
253 | + * @return bool |
|
254 | + */ |
|
255 | + public function hasIssuerAlternativeName(): bool |
|
256 | + { |
|
257 | + return $this->has(Extension\Extension::OID_ISSUER_ALT_NAME); |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Get 'Issuer Alternative Name' extension. |
|
262 | + * |
|
263 | + * @return \X509\Certificate\Extension\IssuerAlternativeNameExtension |
|
264 | + */ |
|
265 | + public function issuerAlternativeName() |
|
266 | + { |
|
267 | + /** @var Extension\IssuerAlternativeNameExtension $issuerAltName */ |
|
268 | + $issuerAltName = $this->get(Extension\Extension::OID_ISSUER_ALT_NAME); |
|
269 | + return $issuerAltName; |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Check whether 'Basic Constraints' extension is present. |
|
274 | + * |
|
275 | + * @return bool |
|
276 | + */ |
|
277 | + public function hasBasicConstraints(): bool |
|
278 | + { |
|
279 | + return $this->has(Extension\Extension::OID_BASIC_CONSTRAINTS); |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * Get 'Basic Constraints' extension. |
|
284 | + * |
|
285 | + * @throws \LogicException If extension is not present |
|
286 | + * @return \X509\Certificate\Extension\BasicConstraintsExtension |
|
287 | + */ |
|
288 | + public function basicConstraints() |
|
289 | + { |
|
290 | + /** @var Extension\BasicConstraintsExtension $basicConstraints */ |
|
291 | + $basicConstraints = $this->get(Extension\Extension::OID_BASIC_CONSTRAINTS); |
|
292 | + return $basicConstraints; |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * Check whether 'Name Constraints' extension is present. |
|
297 | + * |
|
298 | + * @return bool |
|
299 | + */ |
|
300 | + public function hasNameConstraints(): bool |
|
301 | + { |
|
302 | + return $this->has(Extension\Extension::OID_NAME_CONSTRAINTS); |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Get 'Name Constraints' extension. |
|
307 | + * |
|
308 | + * @throws \LogicException If extension is not present |
|
309 | + * @return \X509\Certificate\Extension\NameConstraintsExtension |
|
310 | + */ |
|
311 | + public function nameConstraints() |
|
312 | + { |
|
313 | + /** @var Extension\NameConstraintsExtension $nameConstraints */ |
|
314 | + $nameConstraints = $this->get(Extension\Extension::OID_NAME_CONSTRAINTS); |
|
315 | + return $nameConstraints; |
|
316 | + } |
|
317 | + |
|
318 | + /** |
|
319 | + * Check whether 'Policy Constraints' extension is present. |
|
320 | + * |
|
321 | + * @return bool |
|
322 | + */ |
|
323 | + public function hasPolicyConstraints(): bool |
|
324 | + { |
|
325 | + return $this->has(Extension\Extension::OID_POLICY_CONSTRAINTS); |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Get 'Policy Constraints' extension. |
|
330 | + * |
|
331 | + * @throws \LogicException If extension is not present |
|
332 | + * @return \X509\Certificate\Extension\PolicyConstraintsExtension |
|
333 | + */ |
|
334 | + public function policyConstraints() |
|
335 | + { |
|
336 | + /** @var Extension\PolicyConstraintsExtension $policyConstraints */ |
|
337 | + $policyConstraints = $this->get(Extension\Extension::OID_POLICY_CONSTRAINTS); |
|
338 | + return $policyConstraints; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * Check whether 'Extended Key Usage' extension is present. |
|
343 | + * |
|
344 | + * @return bool |
|
345 | + */ |
|
346 | + public function hasExtendedKeyUsage(): bool |
|
347 | + { |
|
348 | + return $this->has(Extension\Extension::OID_EXT_KEY_USAGE); |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * Get 'Extended Key Usage' extension. |
|
353 | + * |
|
354 | + * @throws \LogicException If extension is not present |
|
355 | + * @return \X509\Certificate\Extension\ExtendedKeyUsageExtension |
|
356 | + */ |
|
357 | + public function extendedKeyUsage() |
|
358 | + { |
|
359 | + /** @var Extension\ExtendedKeyUsageExtension $keyUsage */ |
|
360 | + $keyUsage = $this->get(Extension\Extension::OID_EXT_KEY_USAGE); |
|
361 | + return $keyUsage; |
|
362 | + } |
|
363 | + |
|
364 | + /** |
|
365 | + * Check whether 'CRL Distribution Points' extension is present. |
|
366 | + * |
|
367 | + * @return bool |
|
368 | + */ |
|
369 | + public function hasCRLDistributionPoints(): bool |
|
370 | + { |
|
371 | + return $this->has(Extension\Extension::OID_CRL_DISTRIBUTION_POINTS); |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * Get 'CRL Distribution Points' extension. |
|
376 | + * |
|
377 | + * @throws \LogicException If extension is not present |
|
378 | + * @return \X509\Certificate\Extension\CRLDistributionPointsExtension |
|
379 | + */ |
|
380 | + public function crlDistributionPoints() |
|
381 | + { |
|
382 | + /** @var Extension\CRLDistributionPointsExtension $crlDist */ |
|
383 | + $crlDist = $this->get(Extension\Extension::OID_CRL_DISTRIBUTION_POINTS); |
|
384 | + return $crlDist; |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Check whether 'Inhibit anyPolicy' extension is present. |
|
389 | + * |
|
390 | + * @return bool |
|
391 | + */ |
|
392 | + public function hasInhibitAnyPolicy(): bool |
|
393 | + { |
|
394 | + return $this->has(Extension\Extension::OID_INHIBIT_ANY_POLICY); |
|
395 | + } |
|
396 | + |
|
397 | + /** |
|
398 | + * Get 'Inhibit anyPolicy' extension. |
|
399 | + * |
|
400 | + * @throws \LogicException If extension is not present |
|
401 | + * @return \X509\Certificate\Extension\InhibitAnyPolicyExtension |
|
402 | + */ |
|
403 | + public function inhibitAnyPolicy() |
|
404 | + { |
|
405 | + /** @var Extension\InhibitAnyPolicyExtension $inhibitAny */ |
|
406 | + $inhibitAny = $this->get(Extension\Extension::OID_INHIBIT_ANY_POLICY); |
|
407 | + return $inhibitAny; |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * |
|
412 | + * @see \Countable::count() |
|
413 | + * @return int |
|
414 | + */ |
|
415 | + public function count(): int |
|
416 | + { |
|
417 | + return count($this->_extensions); |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Get iterator for extensions. |
|
422 | + * |
|
423 | + * @see \IteratorAggregate::getIterator() |
|
424 | + * @return \Traversable |
|
425 | + */ |
|
426 | + public function getIterator(): \Traversable |
|
427 | + { |
|
428 | + return new \ArrayIterator($this->_extensions); |
|
429 | + } |
|
430 | 430 | } |
@@ -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 X509\Certificate; |
6 | 6 | |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | public static function fromASN1(Sequence $seq) |
48 | 48 | { |
49 | 49 | $extensions = array_map( |
50 | - function (UnspecifiedType $el) { |
|
50 | + function(UnspecifiedType $el) { |
|
51 | 51 | return Extension\Extension::fromASN1($el->asSequence()); |
52 | 52 | }, $seq->elements()); |
53 | 53 | return new self(...$extensions); |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | { |
63 | 63 | $elements = array_values( |
64 | 64 | array_map( |
65 | - function ($ext) { |
|
65 | + function($ext) { |
|
66 | 66 | return $ext->toASN1(); |
67 | 67 | }, $this->_extensions)); |
68 | 68 | return new Sequence(...$elements); |
@@ -13,71 +13,71 @@ |
||
13 | 13 | */ |
14 | 14 | class UniqueIdentifier |
15 | 15 | { |
16 | - /** |
|
17 | - * Identifier. |
|
18 | - * |
|
19 | - * @var BitString $_uid |
|
20 | - */ |
|
21 | - protected $_uid; |
|
16 | + /** |
|
17 | + * Identifier. |
|
18 | + * |
|
19 | + * @var BitString $_uid |
|
20 | + */ |
|
21 | + protected $_uid; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Constructor. |
|
25 | - * |
|
26 | - * @param BitString $bs |
|
27 | - */ |
|
28 | - public function __construct(BitString $bs) |
|
29 | - { |
|
30 | - $this->_uid = $bs; |
|
31 | - } |
|
23 | + /** |
|
24 | + * Constructor. |
|
25 | + * |
|
26 | + * @param BitString $bs |
|
27 | + */ |
|
28 | + public function __construct(BitString $bs) |
|
29 | + { |
|
30 | + $this->_uid = $bs; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Initialize from ASN.1. |
|
35 | - * |
|
36 | - * @param BitString $bs |
|
37 | - */ |
|
38 | - public static function fromASN1(BitString $bs) |
|
39 | - { |
|
40 | - return new self($bs); |
|
41 | - } |
|
33 | + /** |
|
34 | + * Initialize from ASN.1. |
|
35 | + * |
|
36 | + * @param BitString $bs |
|
37 | + */ |
|
38 | + public static function fromASN1(BitString $bs) |
|
39 | + { |
|
40 | + return new self($bs); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Initialize from string. |
|
45 | - * |
|
46 | - * @param string $str |
|
47 | - * @return self |
|
48 | - */ |
|
49 | - public static function fromString(string $str) |
|
50 | - { |
|
51 | - return new self(new BitString($str)); |
|
52 | - } |
|
43 | + /** |
|
44 | + * Initialize from string. |
|
45 | + * |
|
46 | + * @param string $str |
|
47 | + * @return self |
|
48 | + */ |
|
49 | + public static function fromString(string $str) |
|
50 | + { |
|
51 | + return new self(new BitString($str)); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Get unique identifier as a string. |
|
56 | - * |
|
57 | - * @return string |
|
58 | - */ |
|
59 | - public function string(): string |
|
60 | - { |
|
61 | - return $this->_uid->string(); |
|
62 | - } |
|
54 | + /** |
|
55 | + * Get unique identifier as a string. |
|
56 | + * |
|
57 | + * @return string |
|
58 | + */ |
|
59 | + public function string(): string |
|
60 | + { |
|
61 | + return $this->_uid->string(); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Get unique identifier as a bit string. |
|
66 | - * |
|
67 | - * @return BitString |
|
68 | - */ |
|
69 | - public function bitString(): BitString |
|
70 | - { |
|
71 | - return $this->_uid; |
|
72 | - } |
|
64 | + /** |
|
65 | + * Get unique identifier as a bit string. |
|
66 | + * |
|
67 | + * @return BitString |
|
68 | + */ |
|
69 | + public function bitString(): BitString |
|
70 | + { |
|
71 | + return $this->_uid; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Get ASN.1 element. |
|
76 | - * |
|
77 | - * @return BitString |
|
78 | - */ |
|
79 | - public function toASN1(): BitString |
|
80 | - { |
|
81 | - return $this->_uid; |
|
82 | - } |
|
74 | + /** |
|
75 | + * Get ASN.1 element. |
|
76 | + * |
|
77 | + * @return BitString |
|
78 | + */ |
|
79 | + public function toASN1(): BitString |
|
80 | + { |
|
81 | + return $this->_uid; |
|
82 | + } |
|
83 | 83 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\Certificate; |
6 | 6 |
@@ -12,208 +12,208 @@ |
||
12 | 12 | */ |
13 | 13 | class CertificateBundle implements \Countable, \IteratorAggregate |
14 | 14 | { |
15 | - /** |
|
16 | - * Certificates. |
|
17 | - * |
|
18 | - * @var Certificate[] $_certs |
|
19 | - */ |
|
20 | - protected $_certs; |
|
15 | + /** |
|
16 | + * Certificates. |
|
17 | + * |
|
18 | + * @var Certificate[] $_certs |
|
19 | + */ |
|
20 | + protected $_certs; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Mapping from public key id to array of certificates. |
|
24 | - * |
|
25 | - * @var null|(Certificate[])[] |
|
26 | - */ |
|
27 | - private $_keyIdMap; |
|
22 | + /** |
|
23 | + * Mapping from public key id to array of certificates. |
|
24 | + * |
|
25 | + * @var null|(Certificate[])[] |
|
26 | + */ |
|
27 | + private $_keyIdMap; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @param Certificate[] $certs Certificate objects |
|
33 | - */ |
|
34 | - public function __construct(Certificate ...$certs) |
|
35 | - { |
|
36 | - $this->_certs = $certs; |
|
37 | - } |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @param Certificate[] $certs Certificate objects |
|
33 | + */ |
|
34 | + public function __construct(Certificate ...$certs) |
|
35 | + { |
|
36 | + $this->_certs = $certs; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Reset internal cached variables on clone. |
|
41 | - */ |
|
42 | - public function __clone() |
|
43 | - { |
|
44 | - $this->_keyIdMap = null; |
|
45 | - } |
|
39 | + /** |
|
40 | + * Reset internal cached variables on clone. |
|
41 | + */ |
|
42 | + public function __clone() |
|
43 | + { |
|
44 | + $this->_keyIdMap = null; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Initialize from PEMs. |
|
49 | - * |
|
50 | - * @param PEM[] $pems PEM objects |
|
51 | - * @return self |
|
52 | - */ |
|
53 | - public static function fromPEMs(PEM ...$pems) |
|
54 | - { |
|
55 | - $certs = array_map( |
|
56 | - function ($pem) { |
|
57 | - return Certificate::fromPEM($pem); |
|
58 | - }, $pems); |
|
59 | - return new self(...$certs); |
|
60 | - } |
|
47 | + /** |
|
48 | + * Initialize from PEMs. |
|
49 | + * |
|
50 | + * @param PEM[] $pems PEM objects |
|
51 | + * @return self |
|
52 | + */ |
|
53 | + public static function fromPEMs(PEM ...$pems) |
|
54 | + { |
|
55 | + $certs = array_map( |
|
56 | + function ($pem) { |
|
57 | + return Certificate::fromPEM($pem); |
|
58 | + }, $pems); |
|
59 | + return new self(...$certs); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Initialize from PEM bundle. |
|
64 | - * |
|
65 | - * @param PEMBundle $pem_bundle |
|
66 | - * @return self |
|
67 | - */ |
|
68 | - public static function fromPEMBundle(PEMBundle $pem_bundle) |
|
69 | - { |
|
70 | - return self::fromPEMs(...$pem_bundle->all()); |
|
71 | - } |
|
62 | + /** |
|
63 | + * Initialize from PEM bundle. |
|
64 | + * |
|
65 | + * @param PEMBundle $pem_bundle |
|
66 | + * @return self |
|
67 | + */ |
|
68 | + public static function fromPEMBundle(PEMBundle $pem_bundle) |
|
69 | + { |
|
70 | + return self::fromPEMs(...$pem_bundle->all()); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Get self with certificates added. |
|
75 | - * |
|
76 | - * @param Certificate[] $cert |
|
77 | - * @return self |
|
78 | - */ |
|
79 | - public function withCertificates(Certificate ...$cert) |
|
80 | - { |
|
81 | - $obj = clone $this; |
|
82 | - $obj->_certs = array_merge($obj->_certs, $cert); |
|
83 | - return $obj; |
|
84 | - } |
|
73 | + /** |
|
74 | + * Get self with certificates added. |
|
75 | + * |
|
76 | + * @param Certificate[] $cert |
|
77 | + * @return self |
|
78 | + */ |
|
79 | + public function withCertificates(Certificate ...$cert) |
|
80 | + { |
|
81 | + $obj = clone $this; |
|
82 | + $obj->_certs = array_merge($obj->_certs, $cert); |
|
83 | + return $obj; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Get self with certificates from PEMBundle added. |
|
88 | - * |
|
89 | - * @param PEMBundle $pem_bundle |
|
90 | - * @return self |
|
91 | - */ |
|
92 | - public function withPEMBundle(PEMBundle $pem_bundle) |
|
93 | - { |
|
94 | - $certs = $this->_certs; |
|
95 | - foreach ($pem_bundle as $pem) { |
|
96 | - $certs[] = Certificate::fromPEM($pem); |
|
97 | - } |
|
98 | - return new self(...$certs); |
|
99 | - } |
|
86 | + /** |
|
87 | + * Get self with certificates from PEMBundle added. |
|
88 | + * |
|
89 | + * @param PEMBundle $pem_bundle |
|
90 | + * @return self |
|
91 | + */ |
|
92 | + public function withPEMBundle(PEMBundle $pem_bundle) |
|
93 | + { |
|
94 | + $certs = $this->_certs; |
|
95 | + foreach ($pem_bundle as $pem) { |
|
96 | + $certs[] = Certificate::fromPEM($pem); |
|
97 | + } |
|
98 | + return new self(...$certs); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Get self with single certificate from PEM added. |
|
103 | - * |
|
104 | - * @param PEM $pem |
|
105 | - * @return self |
|
106 | - */ |
|
107 | - public function withPEM(PEM $pem) |
|
108 | - { |
|
109 | - $certs = $this->_certs; |
|
110 | - $certs[] = Certificate::fromPEM($pem); |
|
111 | - return new self(...$certs); |
|
112 | - } |
|
101 | + /** |
|
102 | + * Get self with single certificate from PEM added. |
|
103 | + * |
|
104 | + * @param PEM $pem |
|
105 | + * @return self |
|
106 | + */ |
|
107 | + public function withPEM(PEM $pem) |
|
108 | + { |
|
109 | + $certs = $this->_certs; |
|
110 | + $certs[] = Certificate::fromPEM($pem); |
|
111 | + return new self(...$certs); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Check whether bundle contains a given certificate. |
|
116 | - * |
|
117 | - * @param Certificate $cert |
|
118 | - * @return bool |
|
119 | - */ |
|
120 | - public function contains(Certificate $cert): bool |
|
121 | - { |
|
122 | - $id = self::_getCertKeyId($cert); |
|
123 | - $map = $this->_getKeyIdMap(); |
|
124 | - if (!isset($map[$id])) { |
|
125 | - return false; |
|
126 | - } |
|
127 | - foreach ($map[$id] as $c) { |
|
128 | - /** @var Certificate $c */ |
|
129 | - if ($cert->equals($c)) { |
|
130 | - return true; |
|
131 | - } |
|
132 | - } |
|
133 | - return false; |
|
134 | - } |
|
114 | + /** |
|
115 | + * Check whether bundle contains a given certificate. |
|
116 | + * |
|
117 | + * @param Certificate $cert |
|
118 | + * @return bool |
|
119 | + */ |
|
120 | + public function contains(Certificate $cert): bool |
|
121 | + { |
|
122 | + $id = self::_getCertKeyId($cert); |
|
123 | + $map = $this->_getKeyIdMap(); |
|
124 | + if (!isset($map[$id])) { |
|
125 | + return false; |
|
126 | + } |
|
127 | + foreach ($map[$id] as $c) { |
|
128 | + /** @var Certificate $c */ |
|
129 | + if ($cert->equals($c)) { |
|
130 | + return true; |
|
131 | + } |
|
132 | + } |
|
133 | + return false; |
|
134 | + } |
|
135 | 135 | |
136 | - /** |
|
137 | - * Get all certificates that have given subject key identifier. |
|
138 | - * |
|
139 | - * @param string $id |
|
140 | - * @return Certificate[] |
|
141 | - */ |
|
142 | - public function allBySubjectKeyIdentifier($id): array |
|
143 | - { |
|
144 | - $map = $this->_getKeyIdMap(); |
|
145 | - if (!isset($map[$id])) { |
|
146 | - return array(); |
|
147 | - } |
|
148 | - return $map[$id]; |
|
149 | - } |
|
136 | + /** |
|
137 | + * Get all certificates that have given subject key identifier. |
|
138 | + * |
|
139 | + * @param string $id |
|
140 | + * @return Certificate[] |
|
141 | + */ |
|
142 | + public function allBySubjectKeyIdentifier($id): array |
|
143 | + { |
|
144 | + $map = $this->_getKeyIdMap(); |
|
145 | + if (!isset($map[$id])) { |
|
146 | + return array(); |
|
147 | + } |
|
148 | + return $map[$id]; |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Get all certificates in a bundle. |
|
153 | - * |
|
154 | - * @return Certificate[] |
|
155 | - */ |
|
156 | - public function all(): array |
|
157 | - { |
|
158 | - return $this->_certs; |
|
159 | - } |
|
151 | + /** |
|
152 | + * Get all certificates in a bundle. |
|
153 | + * |
|
154 | + * @return Certificate[] |
|
155 | + */ |
|
156 | + public function all(): array |
|
157 | + { |
|
158 | + return $this->_certs; |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * Get certificate mapping by public key id. |
|
163 | - * |
|
164 | - * @return (Certificate[])[] |
|
165 | - */ |
|
166 | - private function _getKeyIdMap(): array |
|
167 | - { |
|
168 | - // lazily build mapping |
|
169 | - if (!isset($this->_keyIdMap)) { |
|
170 | - $this->_keyIdMap = array(); |
|
171 | - foreach ($this->_certs as $cert) { |
|
172 | - $id = self::_getCertKeyId($cert); |
|
173 | - if (!isset($this->_keyIdMap[$id])) { |
|
174 | - $this->_keyIdMap[$id] = array(); |
|
175 | - } |
|
176 | - array_push($this->_keyIdMap[$id], $cert); |
|
177 | - } |
|
178 | - } |
|
179 | - return $this->_keyIdMap; |
|
180 | - } |
|
161 | + /** |
|
162 | + * Get certificate mapping by public key id. |
|
163 | + * |
|
164 | + * @return (Certificate[])[] |
|
165 | + */ |
|
166 | + private function _getKeyIdMap(): array |
|
167 | + { |
|
168 | + // lazily build mapping |
|
169 | + if (!isset($this->_keyIdMap)) { |
|
170 | + $this->_keyIdMap = array(); |
|
171 | + foreach ($this->_certs as $cert) { |
|
172 | + $id = self::_getCertKeyId($cert); |
|
173 | + if (!isset($this->_keyIdMap[$id])) { |
|
174 | + $this->_keyIdMap[$id] = array(); |
|
175 | + } |
|
176 | + array_push($this->_keyIdMap[$id], $cert); |
|
177 | + } |
|
178 | + } |
|
179 | + return $this->_keyIdMap; |
|
180 | + } |
|
181 | 181 | |
182 | - /** |
|
183 | - * Get public key id for the certificate. |
|
184 | - * |
|
185 | - * @param Certificate $cert |
|
186 | - * @return string |
|
187 | - */ |
|
188 | - private static function _getCertKeyId(Certificate $cert): string |
|
189 | - { |
|
190 | - $exts = $cert->tbsCertificate()->extensions(); |
|
191 | - if ($exts->hasSubjectKeyIdentifier()) { |
|
192 | - return $exts->subjectKeyIdentifier()->keyIdentifier(); |
|
193 | - } |
|
194 | - return $cert->tbsCertificate() |
|
195 | - ->subjectPublicKeyInfo() |
|
196 | - ->keyIdentifier(); |
|
197 | - } |
|
182 | + /** |
|
183 | + * Get public key id for the certificate. |
|
184 | + * |
|
185 | + * @param Certificate $cert |
|
186 | + * @return string |
|
187 | + */ |
|
188 | + private static function _getCertKeyId(Certificate $cert): string |
|
189 | + { |
|
190 | + $exts = $cert->tbsCertificate()->extensions(); |
|
191 | + if ($exts->hasSubjectKeyIdentifier()) { |
|
192 | + return $exts->subjectKeyIdentifier()->keyIdentifier(); |
|
193 | + } |
|
194 | + return $cert->tbsCertificate() |
|
195 | + ->subjectPublicKeyInfo() |
|
196 | + ->keyIdentifier(); |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * |
|
201 | - * @see \Countable::count() |
|
202 | - * @return int |
|
203 | - */ |
|
204 | - public function count(): int |
|
205 | - { |
|
206 | - return count($this->_certs); |
|
207 | - } |
|
199 | + /** |
|
200 | + * |
|
201 | + * @see \Countable::count() |
|
202 | + * @return int |
|
203 | + */ |
|
204 | + public function count(): int |
|
205 | + { |
|
206 | + return count($this->_certs); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Get iterator for certificates. |
|
211 | - * |
|
212 | - * @see \IteratorAggregate::getIterator() |
|
213 | - * @return \ArrayIterator |
|
214 | - */ |
|
215 | - public function getIterator(): \ArrayIterator |
|
216 | - { |
|
217 | - return new \ArrayIterator($this->_certs); |
|
218 | - } |
|
209 | + /** |
|
210 | + * Get iterator for certificates. |
|
211 | + * |
|
212 | + * @see \IteratorAggregate::getIterator() |
|
213 | + * @return \ArrayIterator |
|
214 | + */ |
|
215 | + public function getIterator(): \ArrayIterator |
|
216 | + { |
|
217 | + return new \ArrayIterator($this->_certs); |
|
218 | + } |
|
219 | 219 | } |
@@ -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 X509\Certificate; |
6 | 6 | |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | public static function fromPEMs(PEM ...$pems) |
54 | 54 | { |
55 | 55 | $certs = array_map( |
56 | - function ($pem) { |
|
56 | + function($pem) { |
|
57 | 57 | return Certificate::fromPEM($pem); |
58 | 58 | }, $pems); |
59 | 59 | return new self(...$certs); |
@@ -17,104 +17,104 @@ |
||
17 | 17 | */ |
18 | 18 | class Time |
19 | 19 | { |
20 | - use DateTimeHelper; |
|
20 | + use DateTimeHelper; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Datetime. |
|
24 | - * |
|
25 | - * @var \DateTimeImmutable $_dt |
|
26 | - */ |
|
27 | - protected $_dt; |
|
22 | + /** |
|
23 | + * Datetime. |
|
24 | + * |
|
25 | + * @var \DateTimeImmutable $_dt |
|
26 | + */ |
|
27 | + protected $_dt; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Time ASN.1 type tag. |
|
31 | - * |
|
32 | - * @var int $_type |
|
33 | - */ |
|
34 | - protected $_type; |
|
29 | + /** |
|
30 | + * Time ASN.1 type tag. |
|
31 | + * |
|
32 | + * @var int $_type |
|
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 | - * @return self |
|
52 | - */ |
|
53 | - public static function fromASN1(TimeType $el) |
|
54 | - { |
|
55 | - $obj = new self($el->dateTime()); |
|
56 | - $obj->_type = $el->tag(); |
|
57 | - return $obj; |
|
58 | - } |
|
47 | + /** |
|
48 | + * Initialize from ASN.1. |
|
49 | + * |
|
50 | + * @param TimeType $el |
|
51 | + * @return self |
|
52 | + */ |
|
53 | + public static function fromASN1(TimeType $el) |
|
54 | + { |
|
55 | + $obj = new self($el->dateTime()); |
|
56 | + $obj->_type = $el->tag(); |
|
57 | + return $obj; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Initialize from date string. |
|
62 | - * |
|
63 | - * @param string|null $time |
|
64 | - * @param string|null $tz |
|
65 | - * @return self |
|
66 | - */ |
|
67 | - public static function fromString($time, $tz = null) |
|
68 | - { |
|
69 | - return new self(self::_createDateTime($time, $tz)); |
|
70 | - } |
|
60 | + /** |
|
61 | + * Initialize from date string. |
|
62 | + * |
|
63 | + * @param string|null $time |
|
64 | + * @param string|null $tz |
|
65 | + * @return self |
|
66 | + */ |
|
67 | + public static function fromString($time, $tz = null) |
|
68 | + { |
|
69 | + return new self(self::_createDateTime($time, $tz)); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Get datetime. |
|
74 | - * |
|
75 | - * @return \DateTimeImmutable |
|
76 | - */ |
|
77 | - public function dateTime(): \DateTimeImmutable |
|
78 | - { |
|
79 | - return $this->_dt; |
|
80 | - } |
|
72 | + /** |
|
73 | + * Get datetime. |
|
74 | + * |
|
75 | + * @return \DateTimeImmutable |
|
76 | + */ |
|
77 | + public function dateTime(): \DateTimeImmutable |
|
78 | + { |
|
79 | + return $this->_dt; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Generate ASN.1. |
|
84 | - * |
|
85 | - * @throws \UnexpectedValueException |
|
86 | - * @return TimeType |
|
87 | - */ |
|
88 | - public function toASN1(): TimeType |
|
89 | - { |
|
90 | - $dt = $this->_dt; |
|
91 | - switch ($this->_type) { |
|
92 | - case Element::TYPE_UTC_TIME: |
|
93 | - return new UTCTime($dt); |
|
94 | - case Element::TYPE_GENERALIZED_TIME: |
|
95 | - // GeneralizedTime must not contain fractional seconds |
|
96 | - // (rfc5280 4.1.2.5.2) |
|
97 | - if ($dt->format("u") != 0) { |
|
98 | - // remove fractional seconds (round down) |
|
99 | - $dt = self::_roundDownFractionalSeconds($dt); |
|
100 | - } |
|
101 | - return new GeneralizedTime($dt); |
|
102 | - } |
|
103 | - throw new \UnexpectedValueException( |
|
104 | - "Time type " . Element::tagToName($this->_type) . " not supported."); |
|
105 | - } |
|
82 | + /** |
|
83 | + * Generate ASN.1. |
|
84 | + * |
|
85 | + * @throws \UnexpectedValueException |
|
86 | + * @return TimeType |
|
87 | + */ |
|
88 | + public function toASN1(): TimeType |
|
89 | + { |
|
90 | + $dt = $this->_dt; |
|
91 | + switch ($this->_type) { |
|
92 | + case Element::TYPE_UTC_TIME: |
|
93 | + return new UTCTime($dt); |
|
94 | + case Element::TYPE_GENERALIZED_TIME: |
|
95 | + // GeneralizedTime must not contain fractional seconds |
|
96 | + // (rfc5280 4.1.2.5.2) |
|
97 | + if ($dt->format("u") != 0) { |
|
98 | + // remove fractional seconds (round down) |
|
99 | + $dt = self::_roundDownFractionalSeconds($dt); |
|
100 | + } |
|
101 | + return new GeneralizedTime($dt); |
|
102 | + } |
|
103 | + throw new \UnexpectedValueException( |
|
104 | + "Time type " . Element::tagToName($this->_type) . " not supported."); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Determine whether to use UTCTime or GeneralizedTime ASN.1 type. |
|
109 | - * |
|
110 | - * @param \DateTimeImmutable $dt |
|
111 | - * @return int Type tag |
|
112 | - */ |
|
113 | - protected static function _determineType(\DateTimeImmutable $dt): int |
|
114 | - { |
|
115 | - if ($dt->format("Y") >= 2050) { |
|
116 | - return Element::TYPE_GENERALIZED_TIME; |
|
117 | - } |
|
118 | - return Element::TYPE_UTC_TIME; |
|
119 | - } |
|
107 | + /** |
|
108 | + * Determine whether to use UTCTime or GeneralizedTime ASN.1 type. |
|
109 | + * |
|
110 | + * @param \DateTimeImmutable $dt |
|
111 | + * @return int Type tag |
|
112 | + */ |
|
113 | + protected static function _determineType(\DateTimeImmutable $dt): int |
|
114 | + { |
|
115 | + if ($dt->format("Y") >= 2050) { |
|
116 | + return Element::TYPE_GENERALIZED_TIME; |
|
117 | + } |
|
118 | + return Element::TYPE_UTC_TIME; |
|
119 | + } |
|
120 | 120 | } |
@@ -89,16 +89,16 @@ |
||
89 | 89 | { |
90 | 90 | $dt = $this->_dt; |
91 | 91 | switch ($this->_type) { |
92 | - case Element::TYPE_UTC_TIME: |
|
93 | - return new UTCTime($dt); |
|
94 | - case Element::TYPE_GENERALIZED_TIME: |
|
95 | - // GeneralizedTime must not contain fractional seconds |
|
96 | - // (rfc5280 4.1.2.5.2) |
|
97 | - if ($dt->format("u") != 0) { |
|
98 | - // remove fractional seconds (round down) |
|
99 | - $dt = self::_roundDownFractionalSeconds($dt); |
|
100 | - } |
|
101 | - return new GeneralizedTime($dt); |
|
92 | + case Element::TYPE_UTC_TIME: |
|
93 | + return new UTCTime($dt); |
|
94 | + case Element::TYPE_GENERALIZED_TIME: |
|
95 | + // GeneralizedTime must not contain fractional seconds |
|
96 | + // (rfc5280 4.1.2.5.2) |
|
97 | + if ($dt->format("u") != 0) { |
|
98 | + // remove fractional seconds (round down) |
|
99 | + $dt = self::_roundDownFractionalSeconds($dt); |
|
100 | + } |
|
101 | + return new GeneralizedTime($dt); |
|
102 | 102 | } |
103 | 103 | throw new \UnexpectedValueException( |
104 | 104 | "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 X509\Certificate; |
6 | 6 |
@@ -27,611 +27,611 @@ |
||
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 int |
|
39 | - */ |
|
40 | - protected $_version; |
|
41 | - |
|
42 | - /** |
|
43 | - * Serial number. |
|
44 | - * |
|
45 | - * @var int|string |
|
46 | - */ |
|
47 | - protected $_serialNumber; |
|
48 | - |
|
49 | - /** |
|
50 | - * Signature algorithm. |
|
51 | - * |
|
52 | - * @var SignatureAlgorithmIdentifier |
|
53 | - */ |
|
54 | - protected $_signature; |
|
55 | - |
|
56 | - /** |
|
57 | - * Certificate issuer. |
|
58 | - * |
|
59 | - * @var Name $_issuer |
|
60 | - */ |
|
61 | - protected $_issuer; |
|
62 | - |
|
63 | - /** |
|
64 | - * Certificate validity period. |
|
65 | - * |
|
66 | - * @var Validity $_validity |
|
67 | - */ |
|
68 | - protected $_validity; |
|
69 | - |
|
70 | - /** |
|
71 | - * Certificate subject. |
|
72 | - * |
|
73 | - * @var Name $_subject |
|
74 | - */ |
|
75 | - protected $_subject; |
|
76 | - |
|
77 | - /** |
|
78 | - * Subject public key. |
|
79 | - * |
|
80 | - * @var PublicKeyInfo $_subjectPublicKeyInfo |
|
81 | - */ |
|
82 | - protected $_subjectPublicKeyInfo; |
|
83 | - |
|
84 | - /** |
|
85 | - * Issuer unique identifier. |
|
86 | - * |
|
87 | - * @var UniqueIdentifier|null $_issuerUniqueID |
|
88 | - */ |
|
89 | - protected $_issuerUniqueID; |
|
90 | - |
|
91 | - /** |
|
92 | - * Subject unique identifier. |
|
93 | - * |
|
94 | - * @var UniqueIdentifier|null $_subjectUniqueID |
|
95 | - */ |
|
96 | - protected $_subjectUniqueID; |
|
97 | - |
|
98 | - /** |
|
99 | - * Extensions. |
|
100 | - * |
|
101 | - * @var Extensions $_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 | - * @return self |
|
128 | - */ |
|
129 | - public static function fromASN1(Sequence $seq) |
|
130 | - { |
|
131 | - $idx = 0; |
|
132 | - if ($seq->hasTagged(0)) { |
|
133 | - $idx++; |
|
134 | - $version = intval( |
|
135 | - $seq->getTagged(0) |
|
136 | - ->asExplicit() |
|
137 | - ->asInteger() |
|
138 | - ->number()); |
|
139 | - } else { |
|
140 | - $version = self::VERSION_1; |
|
141 | - } |
|
142 | - $serial = (int) $seq->at($idx++) |
|
143 | - ->asInteger() |
|
144 | - ->number(); |
|
145 | - $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
146 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
147 | - throw new \UnexpectedValueException( |
|
148 | - "Unsupported signature algorithm " . $algo->name() . "."); |
|
149 | - } |
|
150 | - $issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
151 | - $validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
|
152 | - $subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
153 | - $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
|
154 | - $tbs_cert = new self($subject, $pki, $issuer, $validity); |
|
155 | - $tbs_cert->_version = $version; |
|
156 | - $tbs_cert->_serialNumber = $serial; |
|
157 | - $tbs_cert->_signature = $algo; |
|
158 | - if ($seq->hasTagged(1)) { |
|
159 | - $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
160 | - $seq->getTagged(1) |
|
161 | - ->asImplicit(Element::TYPE_BIT_STRING) |
|
162 | - ->asBitString()); |
|
163 | - } |
|
164 | - if ($seq->hasTagged(2)) { |
|
165 | - $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1( |
|
166 | - $seq->getTagged(2) |
|
167 | - ->asImplicit(Element::TYPE_BIT_STRING) |
|
168 | - ->asBitString()); |
|
169 | - } |
|
170 | - if ($seq->hasTagged(3)) { |
|
171 | - $tbs_cert->_extensions = Extensions::fromASN1( |
|
172 | - $seq->getTagged(3) |
|
173 | - ->asExplicit() |
|
174 | - ->asSequence()); |
|
175 | - } |
|
176 | - return $tbs_cert; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Initialize from certification request. |
|
181 | - * |
|
182 | - * Note that signature is not verified and must be done by the caller. |
|
183 | - * |
|
184 | - * @param CertificationRequest $cr |
|
185 | - * @return self |
|
186 | - */ |
|
187 | - public static function fromCSR(CertificationRequest $cr) |
|
188 | - { |
|
189 | - $cri = $cr->certificationRequestInfo(); |
|
190 | - $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(), |
|
191 | - Validity::fromStrings(null, null)); |
|
192 | - // if CSR has Extension Request attribute |
|
193 | - if ($cri->hasAttributes()) { |
|
194 | - $attribs = $cri->attributes(); |
|
195 | - if ($attribs->hasExtensionRequest()) { |
|
196 | - $tbs_cert = $tbs_cert->withExtensions( |
|
197 | - $attribs->extensionRequest() |
|
198 | - ->extensions()); |
|
199 | - } |
|
200 | - } |
|
201 | - // add Subject Key Identifier extension |
|
202 | - $tbs_cert = $tbs_cert->withAdditionalExtensions( |
|
203 | - new SubjectKeyIdentifierExtension(false, |
|
204 | - $cri->subjectPKInfo() |
|
205 | - ->keyIdentifier())); |
|
206 | - return $tbs_cert; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Get self with fields set from the issuer's certificate. |
|
211 | - * |
|
212 | - * Issuer shall be set to issuing certificate's subject. |
|
213 | - * Authority key identifier extensions shall be added with a key identifier |
|
214 | - * set to issuing certificate's public key identifier. |
|
215 | - * |
|
216 | - * @param Certificate $cert Issuing party's certificate |
|
217 | - * @return self |
|
218 | - */ |
|
219 | - public function withIssuerCertificate(Certificate $cert) |
|
220 | - { |
|
221 | - $obj = clone $this; |
|
222 | - // set issuer DN from cert's subject |
|
223 | - $obj->_issuer = $cert->tbsCertificate()->subject(); |
|
224 | - // add authority key identifier extension |
|
225 | - $key_id = $cert->tbsCertificate() |
|
226 | - ->subjectPublicKeyInfo() |
|
227 | - ->keyIdentifier(); |
|
228 | - $obj->_extensions = $obj->_extensions->withExtensions( |
|
229 | - new AuthorityKeyIdentifierExtension(false, $key_id)); |
|
230 | - return $obj; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Get self with given version. |
|
235 | - * |
|
236 | - * If version is not set, appropriate version is automatically |
|
237 | - * determined during signing. |
|
238 | - * |
|
239 | - * @param int $version |
|
240 | - * @return self |
|
241 | - */ |
|
242 | - public function withVersion(int $version) |
|
243 | - { |
|
244 | - $obj = clone $this; |
|
245 | - $obj->_version = $version; |
|
246 | - return $obj; |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * Get self with given serial number. |
|
251 | - * |
|
252 | - * @param int|string $serial Base 10 number |
|
253 | - * @return self |
|
254 | - */ |
|
255 | - public function withSerialNumber($serial) |
|
256 | - { |
|
257 | - $obj = clone $this; |
|
258 | - $obj->_serialNumber = $serial; |
|
259 | - return $obj; |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Get self with random positive serial number. |
|
264 | - * |
|
265 | - * @param int $size Number of random bytes |
|
266 | - * @return self |
|
267 | - */ |
|
268 | - public function withRandomSerialNumber(int $size = 16) |
|
269 | - { |
|
270 | - // ensure that first byte is always non-zero and having first bit unset |
|
271 | - $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
272 | - for ($i = 1; $i < $size; ++$i) { |
|
273 | - $num <<= 8; |
|
274 | - $num += mt_rand(0, 0xff); |
|
275 | - } |
|
276 | - return $this->withSerialNumber(gmp_strval($num, 10)); |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Get self with given signature algorithm. |
|
281 | - * |
|
282 | - * @param SignatureAlgorithmIdentifier $algo |
|
283 | - * @return self |
|
284 | - */ |
|
285 | - public function withSignature(SignatureAlgorithmIdentifier $algo) |
|
286 | - { |
|
287 | - $obj = clone $this; |
|
288 | - $obj->_signature = $algo; |
|
289 | - return $obj; |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Get self with given issuer. |
|
294 | - * |
|
295 | - * @param Name $issuer |
|
296 | - * @return self |
|
297 | - */ |
|
298 | - public function withIssuer(Name $issuer) |
|
299 | - { |
|
300 | - $obj = clone $this; |
|
301 | - $obj->_issuer = $issuer; |
|
302 | - return $obj; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Get self with given validity. |
|
307 | - * |
|
308 | - * @param Validity $validity |
|
309 | - * @return self |
|
310 | - */ |
|
311 | - public function withValidity(Validity $validity) |
|
312 | - { |
|
313 | - $obj = clone $this; |
|
314 | - $obj->_validity = $validity; |
|
315 | - return $obj; |
|
316 | - } |
|
317 | - |
|
318 | - /** |
|
319 | - * Get self with given subject. |
|
320 | - * |
|
321 | - * @param Name $subject |
|
322 | - * @return self |
|
323 | - */ |
|
324 | - public function withSubject(Name $subject) |
|
325 | - { |
|
326 | - $obj = clone $this; |
|
327 | - $obj->_subject = $subject; |
|
328 | - return $obj; |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * Get self with given subject public key info. |
|
333 | - * |
|
334 | - * @param PublicKeyInfo $pub_key_info |
|
335 | - * @return self |
|
336 | - */ |
|
337 | - public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info) |
|
338 | - { |
|
339 | - $obj = clone $this; |
|
340 | - $obj->_subjectPublicKeyInfo = $pub_key_info; |
|
341 | - return $obj; |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Get self with issuer unique ID. |
|
346 | - * |
|
347 | - * @param UniqueIdentifier $id |
|
348 | - * @return self |
|
349 | - */ |
|
350 | - public function withIssuerUniqueID(UniqueIdentifier $id) |
|
351 | - { |
|
352 | - $obj = clone $this; |
|
353 | - $obj->_issuerUniqueID = $id; |
|
354 | - return $obj; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Get self with subject unique ID. |
|
359 | - * |
|
360 | - * @param UniqueIdentifier $id |
|
361 | - * @return self |
|
362 | - */ |
|
363 | - public function withSubjectUniqueID(UniqueIdentifier $id) |
|
364 | - { |
|
365 | - $obj = clone $this; |
|
366 | - $obj->_subjectUniqueID = $id; |
|
367 | - return $obj; |
|
368 | - } |
|
369 | - |
|
370 | - /** |
|
371 | - * Get self with given extensions. |
|
372 | - * |
|
373 | - * @param Extensions $extensions |
|
374 | - * @return self |
|
375 | - */ |
|
376 | - public function withExtensions(Extensions $extensions) |
|
377 | - { |
|
378 | - $obj = clone $this; |
|
379 | - $obj->_extensions = $extensions; |
|
380 | - return $obj; |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * Get self with extensions added. |
|
385 | - * |
|
386 | - * @param Extension ...$exts One or more Extension objects |
|
387 | - * @return self |
|
388 | - */ |
|
389 | - public function withAdditionalExtensions(Extension ...$exts) |
|
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 | - * @return int |
|
410 | - */ |
|
411 | - public function version() |
|
412 | - { |
|
413 | - if (!$this->hasVersion()) { |
|
414 | - throw new \LogicException("version not set."); |
|
415 | - } |
|
416 | - return $this->_version; |
|
417 | - } |
|
418 | - |
|
419 | - /** |
|
420 | - * Check whether serial number is set. |
|
421 | - * |
|
422 | - * @return bool |
|
423 | - */ |
|
424 | - public function hasSerialNumber(): bool |
|
425 | - { |
|
426 | - return isset($this->_serialNumber); |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Get serial number. |
|
431 | - * |
|
432 | - * @return int|string Base 10 integer |
|
433 | - */ |
|
434 | - public function serialNumber() |
|
435 | - { |
|
436 | - if (!$this->hasSerialNumber()) { |
|
437 | - throw new \LogicException("serialNumber not set."); |
|
438 | - } |
|
439 | - return $this->_serialNumber; |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * Check whether signature algorithm is set. |
|
444 | - * |
|
445 | - * @return bool |
|
446 | - */ |
|
447 | - public function hasSignature(): bool |
|
448 | - { |
|
449 | - return isset($this->_signature); |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * Get signature algorithm. |
|
454 | - * |
|
455 | - * @return SignatureAlgorithmIdentifier |
|
456 | - */ |
|
457 | - public function signature() |
|
458 | - { |
|
459 | - if (!$this->hasSignature()) { |
|
460 | - throw new \LogicException("signature not set."); |
|
461 | - } |
|
462 | - return $this->_signature; |
|
463 | - } |
|
464 | - |
|
465 | - /** |
|
466 | - * Get issuer. |
|
467 | - * |
|
468 | - * @return Name |
|
469 | - */ |
|
470 | - public function issuer() |
|
471 | - { |
|
472 | - return $this->_issuer; |
|
473 | - } |
|
474 | - |
|
475 | - /** |
|
476 | - * Get validity period. |
|
477 | - * |
|
478 | - * @return Validity |
|
479 | - */ |
|
480 | - public function validity() |
|
481 | - { |
|
482 | - return $this->_validity; |
|
483 | - } |
|
484 | - |
|
485 | - /** |
|
486 | - * Get subject. |
|
487 | - * |
|
488 | - * @return Name |
|
489 | - */ |
|
490 | - public function subject() |
|
491 | - { |
|
492 | - return $this->_subject; |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * Get subject public key. |
|
497 | - * |
|
498 | - * @return PublicKeyInfo |
|
499 | - */ |
|
500 | - public function subjectPublicKeyInfo() |
|
501 | - { |
|
502 | - return $this->_subjectPublicKeyInfo; |
|
503 | - } |
|
504 | - |
|
505 | - /** |
|
506 | - * Whether issuer unique identifier is present. |
|
507 | - * |
|
508 | - * @return bool |
|
509 | - */ |
|
510 | - public function hasIssuerUniqueID(): bool |
|
511 | - { |
|
512 | - return isset($this->_issuerUniqueID); |
|
513 | - } |
|
514 | - |
|
515 | - /** |
|
516 | - * Get issuerUniqueID. |
|
517 | - * |
|
518 | - * @return UniqueIdentifier |
|
519 | - */ |
|
520 | - public function issuerUniqueID() |
|
521 | - { |
|
522 | - if (!$this->hasIssuerUniqueID()) { |
|
523 | - throw new \LogicException("issuerUniqueID not set."); |
|
524 | - } |
|
525 | - return $this->_issuerUniqueID; |
|
526 | - } |
|
527 | - |
|
528 | - /** |
|
529 | - * Whether subject unique identifier is present. |
|
530 | - * |
|
531 | - * @return bool |
|
532 | - */ |
|
533 | - public function hasSubjectUniqueID(): bool |
|
534 | - { |
|
535 | - return isset($this->_subjectUniqueID); |
|
536 | - } |
|
537 | - |
|
538 | - /** |
|
539 | - * Get subjectUniqueID. |
|
540 | - * |
|
541 | - * @return UniqueIdentifier |
|
542 | - */ |
|
543 | - public function subjectUniqueID() |
|
544 | - { |
|
545 | - if (!$this->hasSubjectUniqueID()) { |
|
546 | - throw new \LogicException("subjectUniqueID not set."); |
|
547 | - } |
|
548 | - return $this->_subjectUniqueID; |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * Get extensions. |
|
553 | - * |
|
554 | - * @return Extensions |
|
555 | - */ |
|
556 | - public function extensions() |
|
557 | - { |
|
558 | - return $this->_extensions; |
|
559 | - } |
|
560 | - |
|
561 | - /** |
|
562 | - * Generate ASN.1 structure. |
|
563 | - * |
|
564 | - * @return Sequence |
|
565 | - */ |
|
566 | - public function toASN1() |
|
567 | - { |
|
568 | - $elements = array(); |
|
569 | - $version = $this->version(); |
|
570 | - // if version is not default |
|
571 | - if ($version != self::VERSION_1) { |
|
572 | - $elements[] = new ExplicitlyTaggedType(0, new Integer($version)); |
|
573 | - } |
|
574 | - $serial = $this->serialNumber(); |
|
575 | - $signature = $this->signature(); |
|
576 | - // add required elements |
|
577 | - array_push($elements, new Integer($serial), $signature->toASN1(), |
|
578 | - $this->_issuer->toASN1(), $this->_validity->toASN1(), |
|
579 | - $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1()); |
|
580 | - if (isset($this->_issuerUniqueID)) { |
|
581 | - $elements[] = new ImplicitlyTaggedType(1, |
|
582 | - $this->_issuerUniqueID->toASN1()); |
|
583 | - } |
|
584 | - if (isset($this->_subjectUniqueID)) { |
|
585 | - $elements[] = new ImplicitlyTaggedType(2, |
|
586 | - $this->_subjectUniqueID->toASN1()); |
|
587 | - } |
|
588 | - if (count($this->_extensions)) { |
|
589 | - $elements[] = new ExplicitlyTaggedType(3, |
|
590 | - $this->_extensions->toASN1()); |
|
591 | - } |
|
592 | - return new Sequence(...$elements); |
|
593 | - } |
|
594 | - |
|
595 | - /** |
|
596 | - * Create signed certificate. |
|
597 | - * |
|
598 | - * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
599 | - * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
600 | - * @param Crypto|null $crypto Crypto engine, use default if not set |
|
601 | - * @return Certificate |
|
602 | - */ |
|
603 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
604 | - PrivateKeyInfo $privkey_info, Crypto $crypto = null) |
|
605 | - { |
|
606 | - $crypto = $crypto ?: Crypto::getDefault(); |
|
607 | - $tbs_cert = clone $this; |
|
608 | - if (!isset($tbs_cert->_version)) { |
|
609 | - $tbs_cert->_version = $tbs_cert->_determineVersion(); |
|
610 | - } |
|
611 | - if (!isset($tbs_cert->_serialNumber)) { |
|
612 | - $tbs_cert->_serialNumber = 0; |
|
613 | - } |
|
614 | - $tbs_cert->_signature = $algo; |
|
615 | - $data = $tbs_cert->toASN1()->toDER(); |
|
616 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
617 | - return new Certificate($tbs_cert, $algo, $signature); |
|
618 | - } |
|
619 | - |
|
620 | - /** |
|
621 | - * Determine minimum version for the certificate. |
|
622 | - * |
|
623 | - * @return int |
|
624 | - */ |
|
625 | - protected function _determineVersion(): int |
|
626 | - { |
|
627 | - // if extensions are present |
|
628 | - if (count($this->_extensions)) { |
|
629 | - return self::VERSION_3; |
|
630 | - } |
|
631 | - // if UniqueIdentifier is present |
|
632 | - if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) { |
|
633 | - return self::VERSION_2; |
|
634 | - } |
|
635 | - return self::VERSION_1; |
|
636 | - } |
|
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 int |
|
39 | + */ |
|
40 | + protected $_version; |
|
41 | + |
|
42 | + /** |
|
43 | + * Serial number. |
|
44 | + * |
|
45 | + * @var int|string |
|
46 | + */ |
|
47 | + protected $_serialNumber; |
|
48 | + |
|
49 | + /** |
|
50 | + * Signature algorithm. |
|
51 | + * |
|
52 | + * @var SignatureAlgorithmIdentifier |
|
53 | + */ |
|
54 | + protected $_signature; |
|
55 | + |
|
56 | + /** |
|
57 | + * Certificate issuer. |
|
58 | + * |
|
59 | + * @var Name $_issuer |
|
60 | + */ |
|
61 | + protected $_issuer; |
|
62 | + |
|
63 | + /** |
|
64 | + * Certificate validity period. |
|
65 | + * |
|
66 | + * @var Validity $_validity |
|
67 | + */ |
|
68 | + protected $_validity; |
|
69 | + |
|
70 | + /** |
|
71 | + * Certificate subject. |
|
72 | + * |
|
73 | + * @var Name $_subject |
|
74 | + */ |
|
75 | + protected $_subject; |
|
76 | + |
|
77 | + /** |
|
78 | + * Subject public key. |
|
79 | + * |
|
80 | + * @var PublicKeyInfo $_subjectPublicKeyInfo |
|
81 | + */ |
|
82 | + protected $_subjectPublicKeyInfo; |
|
83 | + |
|
84 | + /** |
|
85 | + * Issuer unique identifier. |
|
86 | + * |
|
87 | + * @var UniqueIdentifier|null $_issuerUniqueID |
|
88 | + */ |
|
89 | + protected $_issuerUniqueID; |
|
90 | + |
|
91 | + /** |
|
92 | + * Subject unique identifier. |
|
93 | + * |
|
94 | + * @var UniqueIdentifier|null $_subjectUniqueID |
|
95 | + */ |
|
96 | + protected $_subjectUniqueID; |
|
97 | + |
|
98 | + /** |
|
99 | + * Extensions. |
|
100 | + * |
|
101 | + * @var Extensions $_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 | + * @return self |
|
128 | + */ |
|
129 | + public static function fromASN1(Sequence $seq) |
|
130 | + { |
|
131 | + $idx = 0; |
|
132 | + if ($seq->hasTagged(0)) { |
|
133 | + $idx++; |
|
134 | + $version = intval( |
|
135 | + $seq->getTagged(0) |
|
136 | + ->asExplicit() |
|
137 | + ->asInteger() |
|
138 | + ->number()); |
|
139 | + } else { |
|
140 | + $version = self::VERSION_1; |
|
141 | + } |
|
142 | + $serial = (int) $seq->at($idx++) |
|
143 | + ->asInteger() |
|
144 | + ->number(); |
|
145 | + $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
146 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
147 | + throw new \UnexpectedValueException( |
|
148 | + "Unsupported signature algorithm " . $algo->name() . "."); |
|
149 | + } |
|
150 | + $issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
151 | + $validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
|
152 | + $subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
153 | + $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
|
154 | + $tbs_cert = new self($subject, $pki, $issuer, $validity); |
|
155 | + $tbs_cert->_version = $version; |
|
156 | + $tbs_cert->_serialNumber = $serial; |
|
157 | + $tbs_cert->_signature = $algo; |
|
158 | + if ($seq->hasTagged(1)) { |
|
159 | + $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
160 | + $seq->getTagged(1) |
|
161 | + ->asImplicit(Element::TYPE_BIT_STRING) |
|
162 | + ->asBitString()); |
|
163 | + } |
|
164 | + if ($seq->hasTagged(2)) { |
|
165 | + $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1( |
|
166 | + $seq->getTagged(2) |
|
167 | + ->asImplicit(Element::TYPE_BIT_STRING) |
|
168 | + ->asBitString()); |
|
169 | + } |
|
170 | + if ($seq->hasTagged(3)) { |
|
171 | + $tbs_cert->_extensions = Extensions::fromASN1( |
|
172 | + $seq->getTagged(3) |
|
173 | + ->asExplicit() |
|
174 | + ->asSequence()); |
|
175 | + } |
|
176 | + return $tbs_cert; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Initialize from certification request. |
|
181 | + * |
|
182 | + * Note that signature is not verified and must be done by the caller. |
|
183 | + * |
|
184 | + * @param CertificationRequest $cr |
|
185 | + * @return self |
|
186 | + */ |
|
187 | + public static function fromCSR(CertificationRequest $cr) |
|
188 | + { |
|
189 | + $cri = $cr->certificationRequestInfo(); |
|
190 | + $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(), |
|
191 | + Validity::fromStrings(null, null)); |
|
192 | + // if CSR has Extension Request attribute |
|
193 | + if ($cri->hasAttributes()) { |
|
194 | + $attribs = $cri->attributes(); |
|
195 | + if ($attribs->hasExtensionRequest()) { |
|
196 | + $tbs_cert = $tbs_cert->withExtensions( |
|
197 | + $attribs->extensionRequest() |
|
198 | + ->extensions()); |
|
199 | + } |
|
200 | + } |
|
201 | + // add Subject Key Identifier extension |
|
202 | + $tbs_cert = $tbs_cert->withAdditionalExtensions( |
|
203 | + new SubjectKeyIdentifierExtension(false, |
|
204 | + $cri->subjectPKInfo() |
|
205 | + ->keyIdentifier())); |
|
206 | + return $tbs_cert; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Get self with fields set from the issuer's certificate. |
|
211 | + * |
|
212 | + * Issuer shall be set to issuing certificate's subject. |
|
213 | + * Authority key identifier extensions shall be added with a key identifier |
|
214 | + * set to issuing certificate's public key identifier. |
|
215 | + * |
|
216 | + * @param Certificate $cert Issuing party's certificate |
|
217 | + * @return self |
|
218 | + */ |
|
219 | + public function withIssuerCertificate(Certificate $cert) |
|
220 | + { |
|
221 | + $obj = clone $this; |
|
222 | + // set issuer DN from cert's subject |
|
223 | + $obj->_issuer = $cert->tbsCertificate()->subject(); |
|
224 | + // add authority key identifier extension |
|
225 | + $key_id = $cert->tbsCertificate() |
|
226 | + ->subjectPublicKeyInfo() |
|
227 | + ->keyIdentifier(); |
|
228 | + $obj->_extensions = $obj->_extensions->withExtensions( |
|
229 | + new AuthorityKeyIdentifierExtension(false, $key_id)); |
|
230 | + return $obj; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Get self with given version. |
|
235 | + * |
|
236 | + * If version is not set, appropriate version is automatically |
|
237 | + * determined during signing. |
|
238 | + * |
|
239 | + * @param int $version |
|
240 | + * @return self |
|
241 | + */ |
|
242 | + public function withVersion(int $version) |
|
243 | + { |
|
244 | + $obj = clone $this; |
|
245 | + $obj->_version = $version; |
|
246 | + return $obj; |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * Get self with given serial number. |
|
251 | + * |
|
252 | + * @param int|string $serial Base 10 number |
|
253 | + * @return self |
|
254 | + */ |
|
255 | + public function withSerialNumber($serial) |
|
256 | + { |
|
257 | + $obj = clone $this; |
|
258 | + $obj->_serialNumber = $serial; |
|
259 | + return $obj; |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Get self with random positive serial number. |
|
264 | + * |
|
265 | + * @param int $size Number of random bytes |
|
266 | + * @return self |
|
267 | + */ |
|
268 | + public function withRandomSerialNumber(int $size = 16) |
|
269 | + { |
|
270 | + // ensure that first byte is always non-zero and having first bit unset |
|
271 | + $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
272 | + for ($i = 1; $i < $size; ++$i) { |
|
273 | + $num <<= 8; |
|
274 | + $num += mt_rand(0, 0xff); |
|
275 | + } |
|
276 | + return $this->withSerialNumber(gmp_strval($num, 10)); |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Get self with given signature algorithm. |
|
281 | + * |
|
282 | + * @param SignatureAlgorithmIdentifier $algo |
|
283 | + * @return self |
|
284 | + */ |
|
285 | + public function withSignature(SignatureAlgorithmIdentifier $algo) |
|
286 | + { |
|
287 | + $obj = clone $this; |
|
288 | + $obj->_signature = $algo; |
|
289 | + return $obj; |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Get self with given issuer. |
|
294 | + * |
|
295 | + * @param Name $issuer |
|
296 | + * @return self |
|
297 | + */ |
|
298 | + public function withIssuer(Name $issuer) |
|
299 | + { |
|
300 | + $obj = clone $this; |
|
301 | + $obj->_issuer = $issuer; |
|
302 | + return $obj; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Get self with given validity. |
|
307 | + * |
|
308 | + * @param Validity $validity |
|
309 | + * @return self |
|
310 | + */ |
|
311 | + public function withValidity(Validity $validity) |
|
312 | + { |
|
313 | + $obj = clone $this; |
|
314 | + $obj->_validity = $validity; |
|
315 | + return $obj; |
|
316 | + } |
|
317 | + |
|
318 | + /** |
|
319 | + * Get self with given subject. |
|
320 | + * |
|
321 | + * @param Name $subject |
|
322 | + * @return self |
|
323 | + */ |
|
324 | + public function withSubject(Name $subject) |
|
325 | + { |
|
326 | + $obj = clone $this; |
|
327 | + $obj->_subject = $subject; |
|
328 | + return $obj; |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * Get self with given subject public key info. |
|
333 | + * |
|
334 | + * @param PublicKeyInfo $pub_key_info |
|
335 | + * @return self |
|
336 | + */ |
|
337 | + public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info) |
|
338 | + { |
|
339 | + $obj = clone $this; |
|
340 | + $obj->_subjectPublicKeyInfo = $pub_key_info; |
|
341 | + return $obj; |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Get self with issuer unique ID. |
|
346 | + * |
|
347 | + * @param UniqueIdentifier $id |
|
348 | + * @return self |
|
349 | + */ |
|
350 | + public function withIssuerUniqueID(UniqueIdentifier $id) |
|
351 | + { |
|
352 | + $obj = clone $this; |
|
353 | + $obj->_issuerUniqueID = $id; |
|
354 | + return $obj; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Get self with subject unique ID. |
|
359 | + * |
|
360 | + * @param UniqueIdentifier $id |
|
361 | + * @return self |
|
362 | + */ |
|
363 | + public function withSubjectUniqueID(UniqueIdentifier $id) |
|
364 | + { |
|
365 | + $obj = clone $this; |
|
366 | + $obj->_subjectUniqueID = $id; |
|
367 | + return $obj; |
|
368 | + } |
|
369 | + |
|
370 | + /** |
|
371 | + * Get self with given extensions. |
|
372 | + * |
|
373 | + * @param Extensions $extensions |
|
374 | + * @return self |
|
375 | + */ |
|
376 | + public function withExtensions(Extensions $extensions) |
|
377 | + { |
|
378 | + $obj = clone $this; |
|
379 | + $obj->_extensions = $extensions; |
|
380 | + return $obj; |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * Get self with extensions added. |
|
385 | + * |
|
386 | + * @param Extension ...$exts One or more Extension objects |
|
387 | + * @return self |
|
388 | + */ |
|
389 | + public function withAdditionalExtensions(Extension ...$exts) |
|
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 | + * @return int |
|
410 | + */ |
|
411 | + public function version() |
|
412 | + { |
|
413 | + if (!$this->hasVersion()) { |
|
414 | + throw new \LogicException("version not set."); |
|
415 | + } |
|
416 | + return $this->_version; |
|
417 | + } |
|
418 | + |
|
419 | + /** |
|
420 | + * Check whether serial number is set. |
|
421 | + * |
|
422 | + * @return bool |
|
423 | + */ |
|
424 | + public function hasSerialNumber(): bool |
|
425 | + { |
|
426 | + return isset($this->_serialNumber); |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Get serial number. |
|
431 | + * |
|
432 | + * @return int|string Base 10 integer |
|
433 | + */ |
|
434 | + public function serialNumber() |
|
435 | + { |
|
436 | + if (!$this->hasSerialNumber()) { |
|
437 | + throw new \LogicException("serialNumber not set."); |
|
438 | + } |
|
439 | + return $this->_serialNumber; |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * Check whether signature algorithm is set. |
|
444 | + * |
|
445 | + * @return bool |
|
446 | + */ |
|
447 | + public function hasSignature(): bool |
|
448 | + { |
|
449 | + return isset($this->_signature); |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * Get signature algorithm. |
|
454 | + * |
|
455 | + * @return SignatureAlgorithmIdentifier |
|
456 | + */ |
|
457 | + public function signature() |
|
458 | + { |
|
459 | + if (!$this->hasSignature()) { |
|
460 | + throw new \LogicException("signature not set."); |
|
461 | + } |
|
462 | + return $this->_signature; |
|
463 | + } |
|
464 | + |
|
465 | + /** |
|
466 | + * Get issuer. |
|
467 | + * |
|
468 | + * @return Name |
|
469 | + */ |
|
470 | + public function issuer() |
|
471 | + { |
|
472 | + return $this->_issuer; |
|
473 | + } |
|
474 | + |
|
475 | + /** |
|
476 | + * Get validity period. |
|
477 | + * |
|
478 | + * @return Validity |
|
479 | + */ |
|
480 | + public function validity() |
|
481 | + { |
|
482 | + return $this->_validity; |
|
483 | + } |
|
484 | + |
|
485 | + /** |
|
486 | + * Get subject. |
|
487 | + * |
|
488 | + * @return Name |
|
489 | + */ |
|
490 | + public function subject() |
|
491 | + { |
|
492 | + return $this->_subject; |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * Get subject public key. |
|
497 | + * |
|
498 | + * @return PublicKeyInfo |
|
499 | + */ |
|
500 | + public function subjectPublicKeyInfo() |
|
501 | + { |
|
502 | + return $this->_subjectPublicKeyInfo; |
|
503 | + } |
|
504 | + |
|
505 | + /** |
|
506 | + * Whether issuer unique identifier is present. |
|
507 | + * |
|
508 | + * @return bool |
|
509 | + */ |
|
510 | + public function hasIssuerUniqueID(): bool |
|
511 | + { |
|
512 | + return isset($this->_issuerUniqueID); |
|
513 | + } |
|
514 | + |
|
515 | + /** |
|
516 | + * Get issuerUniqueID. |
|
517 | + * |
|
518 | + * @return UniqueIdentifier |
|
519 | + */ |
|
520 | + public function issuerUniqueID() |
|
521 | + { |
|
522 | + if (!$this->hasIssuerUniqueID()) { |
|
523 | + throw new \LogicException("issuerUniqueID not set."); |
|
524 | + } |
|
525 | + return $this->_issuerUniqueID; |
|
526 | + } |
|
527 | + |
|
528 | + /** |
|
529 | + * Whether subject unique identifier is present. |
|
530 | + * |
|
531 | + * @return bool |
|
532 | + */ |
|
533 | + public function hasSubjectUniqueID(): bool |
|
534 | + { |
|
535 | + return isset($this->_subjectUniqueID); |
|
536 | + } |
|
537 | + |
|
538 | + /** |
|
539 | + * Get subjectUniqueID. |
|
540 | + * |
|
541 | + * @return UniqueIdentifier |
|
542 | + */ |
|
543 | + public function subjectUniqueID() |
|
544 | + { |
|
545 | + if (!$this->hasSubjectUniqueID()) { |
|
546 | + throw new \LogicException("subjectUniqueID not set."); |
|
547 | + } |
|
548 | + return $this->_subjectUniqueID; |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * Get extensions. |
|
553 | + * |
|
554 | + * @return Extensions |
|
555 | + */ |
|
556 | + public function extensions() |
|
557 | + { |
|
558 | + return $this->_extensions; |
|
559 | + } |
|
560 | + |
|
561 | + /** |
|
562 | + * Generate ASN.1 structure. |
|
563 | + * |
|
564 | + * @return Sequence |
|
565 | + */ |
|
566 | + public function toASN1() |
|
567 | + { |
|
568 | + $elements = array(); |
|
569 | + $version = $this->version(); |
|
570 | + // if version is not default |
|
571 | + if ($version != self::VERSION_1) { |
|
572 | + $elements[] = new ExplicitlyTaggedType(0, new Integer($version)); |
|
573 | + } |
|
574 | + $serial = $this->serialNumber(); |
|
575 | + $signature = $this->signature(); |
|
576 | + // add required elements |
|
577 | + array_push($elements, new Integer($serial), $signature->toASN1(), |
|
578 | + $this->_issuer->toASN1(), $this->_validity->toASN1(), |
|
579 | + $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1()); |
|
580 | + if (isset($this->_issuerUniqueID)) { |
|
581 | + $elements[] = new ImplicitlyTaggedType(1, |
|
582 | + $this->_issuerUniqueID->toASN1()); |
|
583 | + } |
|
584 | + if (isset($this->_subjectUniqueID)) { |
|
585 | + $elements[] = new ImplicitlyTaggedType(2, |
|
586 | + $this->_subjectUniqueID->toASN1()); |
|
587 | + } |
|
588 | + if (count($this->_extensions)) { |
|
589 | + $elements[] = new ExplicitlyTaggedType(3, |
|
590 | + $this->_extensions->toASN1()); |
|
591 | + } |
|
592 | + return new Sequence(...$elements); |
|
593 | + } |
|
594 | + |
|
595 | + /** |
|
596 | + * Create signed certificate. |
|
597 | + * |
|
598 | + * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
599 | + * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
600 | + * @param Crypto|null $crypto Crypto engine, use default if not set |
|
601 | + * @return Certificate |
|
602 | + */ |
|
603 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
604 | + PrivateKeyInfo $privkey_info, Crypto $crypto = null) |
|
605 | + { |
|
606 | + $crypto = $crypto ?: Crypto::getDefault(); |
|
607 | + $tbs_cert = clone $this; |
|
608 | + if (!isset($tbs_cert->_version)) { |
|
609 | + $tbs_cert->_version = $tbs_cert->_determineVersion(); |
|
610 | + } |
|
611 | + if (!isset($tbs_cert->_serialNumber)) { |
|
612 | + $tbs_cert->_serialNumber = 0; |
|
613 | + } |
|
614 | + $tbs_cert->_signature = $algo; |
|
615 | + $data = $tbs_cert->toASN1()->toDER(); |
|
616 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
617 | + return new Certificate($tbs_cert, $algo, $signature); |
|
618 | + } |
|
619 | + |
|
620 | + /** |
|
621 | + * Determine minimum version for the certificate. |
|
622 | + * |
|
623 | + * @return int |
|
624 | + */ |
|
625 | + protected function _determineVersion(): int |
|
626 | + { |
|
627 | + // if extensions are present |
|
628 | + if (count($this->_extensions)) { |
|
629 | + return self::VERSION_3; |
|
630 | + } |
|
631 | + // if UniqueIdentifier is present |
|
632 | + if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) { |
|
633 | + return self::VERSION_2; |
|
634 | + } |
|
635 | + return self::VERSION_1; |
|
636 | + } |
|
637 | 637 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\Certificate; |
6 | 6 |
@@ -15,59 +15,59 @@ |
||
15 | 15 | */ |
16 | 16 | class DNSName extends GeneralName |
17 | 17 | { |
18 | - /** |
|
19 | - * DNS name. |
|
20 | - * |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - protected $_name; |
|
18 | + /** |
|
19 | + * DNS name. |
|
20 | + * |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + protected $_name; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Constructor. |
|
27 | - * |
|
28 | - * @param string $name Domain name |
|
29 | - */ |
|
30 | - public function __construct(string $name) |
|
31 | - { |
|
32 | - $this->_tag = self::TAG_DNS_NAME; |
|
33 | - $this->_name = $name; |
|
34 | - } |
|
25 | + /** |
|
26 | + * Constructor. |
|
27 | + * |
|
28 | + * @param string $name Domain name |
|
29 | + */ |
|
30 | + public function __construct(string $name) |
|
31 | + { |
|
32 | + $this->_tag = self::TAG_DNS_NAME; |
|
33 | + $this->_name = $name; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * @param UnspecifiedType $el |
|
39 | - * @return self |
|
40 | - */ |
|
41 | - public static function fromChosenASN1(UnspecifiedType $el) |
|
42 | - { |
|
43 | - return new self($el->asIA5String()->string()); |
|
44 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * @param UnspecifiedType $el |
|
39 | + * @return self |
|
40 | + */ |
|
41 | + public static function fromChosenASN1(UnspecifiedType $el) |
|
42 | + { |
|
43 | + return new self($el->asIA5String()->string()); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * |
|
48 | - * {@inheritdoc} |
|
49 | - */ |
|
50 | - public function string(): string |
|
51 | - { |
|
52 | - return $this->_name; |
|
53 | - } |
|
46 | + /** |
|
47 | + * |
|
48 | + * {@inheritdoc} |
|
49 | + */ |
|
50 | + public function string(): string |
|
51 | + { |
|
52 | + return $this->_name; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Get DNS name. |
|
57 | - * |
|
58 | - * @return string |
|
59 | - */ |
|
60 | - public function name(): string |
|
61 | - { |
|
62 | - return $this->_name; |
|
63 | - } |
|
55 | + /** |
|
56 | + * Get DNS name. |
|
57 | + * |
|
58 | + * @return string |
|
59 | + */ |
|
60 | + public function name(): string |
|
61 | + { |
|
62 | + return $this->_name; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * |
|
67 | - * {@inheritdoc} |
|
68 | - */ |
|
69 | - protected function _choiceASN1() |
|
70 | - { |
|
71 | - return new ImplicitlyTaggedType($this->_tag, new IA5String($this->_name)); |
|
72 | - } |
|
65 | + /** |
|
66 | + * |
|
67 | + * {@inheritdoc} |
|
68 | + */ |
|
69 | + protected function _choiceASN1() |
|
70 | + { |
|
71 | + return new ImplicitlyTaggedType($this->_tag, new IA5String($this->_name)); |
|
72 | + } |
|
73 | 73 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\GeneralName; |
6 | 6 |
@@ -16,59 +16,59 @@ |
||
16 | 16 | */ |
17 | 17 | class UniformResourceIdentifier extends GeneralName |
18 | 18 | { |
19 | - /** |
|
20 | - * URI. |
|
21 | - * |
|
22 | - * @var string $_uri |
|
23 | - */ |
|
24 | - protected $_uri; |
|
19 | + /** |
|
20 | + * URI. |
|
21 | + * |
|
22 | + * @var string $_uri |
|
23 | + */ |
|
24 | + protected $_uri; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Constructor. |
|
28 | - * |
|
29 | - * @param string $uri |
|
30 | - */ |
|
31 | - public function __construct(string $uri) |
|
32 | - { |
|
33 | - $this->_tag = self::TAG_URI; |
|
34 | - $this->_uri = $uri; |
|
35 | - } |
|
26 | + /** |
|
27 | + * Constructor. |
|
28 | + * |
|
29 | + * @param string $uri |
|
30 | + */ |
|
31 | + public function __construct(string $uri) |
|
32 | + { |
|
33 | + $this->_tag = self::TAG_URI; |
|
34 | + $this->_uri = $uri; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * |
|
39 | - * @param UnspecifiedType $el |
|
40 | - * @return self |
|
41 | - */ |
|
42 | - public static function fromChosenASN1(UnspecifiedType $el) |
|
43 | - { |
|
44 | - return new self($el->asIA5String()->string()); |
|
45 | - } |
|
37 | + /** |
|
38 | + * |
|
39 | + * @param UnspecifiedType $el |
|
40 | + * @return self |
|
41 | + */ |
|
42 | + public static function fromChosenASN1(UnspecifiedType $el) |
|
43 | + { |
|
44 | + return new self($el->asIA5String()->string()); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * |
|
49 | - * {@inheritdoc} |
|
50 | - */ |
|
51 | - public function string(): string |
|
52 | - { |
|
53 | - return $this->_uri; |
|
54 | - } |
|
47 | + /** |
|
48 | + * |
|
49 | + * {@inheritdoc} |
|
50 | + */ |
|
51 | + public function string(): string |
|
52 | + { |
|
53 | + return $this->_uri; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get URI. |
|
58 | - * |
|
59 | - * @return string |
|
60 | - */ |
|
61 | - public function uri(): string |
|
62 | - { |
|
63 | - return $this->_uri; |
|
64 | - } |
|
56 | + /** |
|
57 | + * Get URI. |
|
58 | + * |
|
59 | + * @return string |
|
60 | + */ |
|
61 | + public function uri(): string |
|
62 | + { |
|
63 | + return $this->_uri; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * |
|
68 | - * {@inheritdoc} |
|
69 | - */ |
|
70 | - protected function _choiceASN1() |
|
71 | - { |
|
72 | - return new ImplicitlyTaggedType($this->_tag, new IA5String($this->_uri)); |
|
73 | - } |
|
66 | + /** |
|
67 | + * |
|
68 | + * {@inheritdoc} |
|
69 | + */ |
|
70 | + protected function _choiceASN1() |
|
71 | + { |
|
72 | + return new ImplicitlyTaggedType($this->_tag, new IA5String($this->_uri)); |
|
73 | + } |
|
74 | 74 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\GeneralName; |
6 | 6 |
@@ -18,82 +18,82 @@ |
||
18 | 18 | */ |
19 | 19 | class OtherName extends GeneralName |
20 | 20 | { |
21 | - /** |
|
22 | - * Type OID. |
|
23 | - * |
|
24 | - * @var string $_type |
|
25 | - */ |
|
26 | - protected $_type; |
|
21 | + /** |
|
22 | + * Type OID. |
|
23 | + * |
|
24 | + * @var string $_type |
|
25 | + */ |
|
26 | + protected $_type; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Value. |
|
30 | - * |
|
31 | - * @var Element $_element |
|
32 | - */ |
|
33 | - protected $_element; |
|
28 | + /** |
|
29 | + * Value. |
|
30 | + * |
|
31 | + * @var Element $_element |
|
32 | + */ |
|
33 | + protected $_element; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Constructor. |
|
37 | - * |
|
38 | - * @param string $type_id OID |
|
39 | - * @param Element $el |
|
40 | - */ |
|
41 | - public function __construct($type_id, Element $el) |
|
42 | - { |
|
43 | - $this->_tag = self::TAG_OTHER_NAME; |
|
44 | - $this->_type = $type_id; |
|
45 | - $this->_element = $el; |
|
46 | - } |
|
35 | + /** |
|
36 | + * Constructor. |
|
37 | + * |
|
38 | + * @param string $type_id OID |
|
39 | + * @param Element $el |
|
40 | + */ |
|
41 | + public function __construct($type_id, Element $el) |
|
42 | + { |
|
43 | + $this->_tag = self::TAG_OTHER_NAME; |
|
44 | + $this->_type = $type_id; |
|
45 | + $this->_element = $el; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * |
|
50 | - * @param UnspecifiedType $el |
|
51 | - * @return self |
|
52 | - */ |
|
53 | - public static function fromChosenASN1(UnspecifiedType $el) |
|
54 | - { |
|
55 | - $seq = $el->asSequence(); |
|
56 | - $type_id = $seq->at(0) |
|
57 | - ->asObjectIdentifier() |
|
58 | - ->oid(); |
|
59 | - $value = $seq->getTagged(0) |
|
60 | - ->asExplicit() |
|
61 | - ->asElement(); |
|
62 | - return new self($type_id, $value); |
|
63 | - } |
|
64 | - public function string() |
|
65 | - { |
|
66 | - return $this->_type . "/#" . bin2hex($this->_element->toDER()); |
|
67 | - } |
|
48 | + /** |
|
49 | + * |
|
50 | + * @param UnspecifiedType $el |
|
51 | + * @return self |
|
52 | + */ |
|
53 | + public static function fromChosenASN1(UnspecifiedType $el) |
|
54 | + { |
|
55 | + $seq = $el->asSequence(); |
|
56 | + $type_id = $seq->at(0) |
|
57 | + ->asObjectIdentifier() |
|
58 | + ->oid(); |
|
59 | + $value = $seq->getTagged(0) |
|
60 | + ->asExplicit() |
|
61 | + ->asElement(); |
|
62 | + return new self($type_id, $value); |
|
63 | + } |
|
64 | + public function string() |
|
65 | + { |
|
66 | + return $this->_type . "/#" . bin2hex($this->_element->toDER()); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Get type OID. |
|
71 | - * |
|
72 | - * @return string |
|
73 | - */ |
|
74 | - public function type(): string |
|
75 | - { |
|
76 | - return $this->_type; |
|
77 | - } |
|
69 | + /** |
|
70 | + * Get type OID. |
|
71 | + * |
|
72 | + * @return string |
|
73 | + */ |
|
74 | + public function type(): string |
|
75 | + { |
|
76 | + return $this->_type; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Get value element. |
|
81 | - * |
|
82 | - * @return Element |
|
83 | - */ |
|
84 | - public function value(): Element |
|
85 | - { |
|
86 | - return $this->_element; |
|
87 | - } |
|
79 | + /** |
|
80 | + * Get value element. |
|
81 | + * |
|
82 | + * @return Element |
|
83 | + */ |
|
84 | + public function value(): Element |
|
85 | + { |
|
86 | + return $this->_element; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * |
|
91 | - * {@inheritdoc} |
|
92 | - */ |
|
93 | - protected function _choiceASN1() |
|
94 | - { |
|
95 | - return new ImplicitlyTaggedType($this->_tag, |
|
96 | - new Sequence(new ObjectIdentifier($this->_type), |
|
97 | - new ExplicitlyTaggedType(0, $this->_element))); |
|
98 | - } |
|
89 | + /** |
|
90 | + * |
|
91 | + * {@inheritdoc} |
|
92 | + */ |
|
93 | + protected function _choiceASN1() |
|
94 | + { |
|
95 | + return new ImplicitlyTaggedType($this->_tag, |
|
96 | + new Sequence(new ObjectIdentifier($this->_type), |
|
97 | + new ExplicitlyTaggedType(0, $this->_element))); |
|
98 | + } |
|
99 | 99 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types=1); |
|
3 | +declare(strict_types = 1); |
|
4 | 4 | |
5 | 5 | namespace X509\GeneralName; |
6 | 6 |