@@ -16,267 +16,267 @@ |
||
16 | 16 | */ |
17 | 17 | class PolicyNode implements \IteratorAggregate, \Countable |
18 | 18 | { |
19 | - /** |
|
20 | - * Policy OID. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $_validPolicy; |
|
19 | + /** |
|
20 | + * Policy OID. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $_validPolicy; |
|
25 | 25 | |
26 | - /** |
|
27 | - * List of qualifiers. |
|
28 | - * |
|
29 | - * @var PolicyQualifierInfo[] |
|
30 | - */ |
|
31 | - protected $_qualifiers; |
|
26 | + /** |
|
27 | + * List of qualifiers. |
|
28 | + * |
|
29 | + * @var PolicyQualifierInfo[] |
|
30 | + */ |
|
31 | + protected $_qualifiers; |
|
32 | 32 | |
33 | - /** |
|
34 | - * List of expected policy OIDs. |
|
35 | - * |
|
36 | - * @var string[] |
|
37 | - */ |
|
38 | - protected $_expectedPolicies; |
|
33 | + /** |
|
34 | + * List of expected policy OIDs. |
|
35 | + * |
|
36 | + * @var string[] |
|
37 | + */ |
|
38 | + protected $_expectedPolicies; |
|
39 | 39 | |
40 | - /** |
|
41 | - * List of child nodes. |
|
42 | - * |
|
43 | - * @var PolicyNode[] |
|
44 | - */ |
|
45 | - protected $_children; |
|
40 | + /** |
|
41 | + * List of child nodes. |
|
42 | + * |
|
43 | + * @var PolicyNode[] |
|
44 | + */ |
|
45 | + protected $_children; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Reference to the parent node. |
|
49 | - * |
|
50 | - * @var null|PolicyNode |
|
51 | - */ |
|
52 | - protected $_parent; |
|
47 | + /** |
|
48 | + * Reference to the parent node. |
|
49 | + * |
|
50 | + * @var null|PolicyNode |
|
51 | + */ |
|
52 | + protected $_parent; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Constructor. |
|
56 | - * |
|
57 | - * @param string $valid_policy Policy OID |
|
58 | - * @param PolicyQualifierInfo[] $qualifiers |
|
59 | - * @param string[] $expected_policies |
|
60 | - */ |
|
61 | - public function __construct(string $valid_policy, array $qualifiers, |
|
62 | - array $expected_policies) |
|
63 | - { |
|
64 | - $this->_validPolicy = $valid_policy; |
|
65 | - $this->_qualifiers = $qualifiers; |
|
66 | - $this->_expectedPolicies = $expected_policies; |
|
67 | - $this->_children = []; |
|
68 | - } |
|
54 | + /** |
|
55 | + * Constructor. |
|
56 | + * |
|
57 | + * @param string $valid_policy Policy OID |
|
58 | + * @param PolicyQualifierInfo[] $qualifiers |
|
59 | + * @param string[] $expected_policies |
|
60 | + */ |
|
61 | + public function __construct(string $valid_policy, array $qualifiers, |
|
62 | + array $expected_policies) |
|
63 | + { |
|
64 | + $this->_validPolicy = $valid_policy; |
|
65 | + $this->_qualifiers = $qualifiers; |
|
66 | + $this->_expectedPolicies = $expected_policies; |
|
67 | + $this->_children = []; |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Create initial node for the policy tree. |
|
72 | - * |
|
73 | - * @return self |
|
74 | - */ |
|
75 | - public static function anyPolicyNode(): self |
|
76 | - { |
|
77 | - return new self(PolicyInformation::OID_ANY_POLICY, [], |
|
78 | - [PolicyInformation::OID_ANY_POLICY]); |
|
79 | - } |
|
70 | + /** |
|
71 | + * Create initial node for the policy tree. |
|
72 | + * |
|
73 | + * @return self |
|
74 | + */ |
|
75 | + public static function anyPolicyNode(): self |
|
76 | + { |
|
77 | + return new self(PolicyInformation::OID_ANY_POLICY, [], |
|
78 | + [PolicyInformation::OID_ANY_POLICY]); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Get the valid policy OID. |
|
83 | - * |
|
84 | - * @return string |
|
85 | - */ |
|
86 | - public function validPolicy(): string |
|
87 | - { |
|
88 | - return $this->_validPolicy; |
|
89 | - } |
|
81 | + /** |
|
82 | + * Get the valid policy OID. |
|
83 | + * |
|
84 | + * @return string |
|
85 | + */ |
|
86 | + public function validPolicy(): string |
|
87 | + { |
|
88 | + return $this->_validPolicy; |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * Check whether node has anyPolicy as a valid policy. |
|
93 | - * |
|
94 | - * @return bool |
|
95 | - */ |
|
96 | - public function isAnyPolicy(): bool |
|
97 | - { |
|
98 | - return PolicyInformation::OID_ANY_POLICY === $this->_validPolicy; |
|
99 | - } |
|
91 | + /** |
|
92 | + * Check whether node has anyPolicy as a valid policy. |
|
93 | + * |
|
94 | + * @return bool |
|
95 | + */ |
|
96 | + public function isAnyPolicy(): bool |
|
97 | + { |
|
98 | + return PolicyInformation::OID_ANY_POLICY === $this->_validPolicy; |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Get the qualifier set. |
|
103 | - * |
|
104 | - * @return PolicyQualifierInfo[] |
|
105 | - */ |
|
106 | - public function qualifiers(): array |
|
107 | - { |
|
108 | - return $this->_qualifiers; |
|
109 | - } |
|
101 | + /** |
|
102 | + * Get the qualifier set. |
|
103 | + * |
|
104 | + * @return PolicyQualifierInfo[] |
|
105 | + */ |
|
106 | + public function qualifiers(): array |
|
107 | + { |
|
108 | + return $this->_qualifiers; |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Check whether node has OID as an expected policy. |
|
113 | - * |
|
114 | - * @param string $oid |
|
115 | - * |
|
116 | - * @return bool |
|
117 | - */ |
|
118 | - public function hasExpectedPolicy(string $oid): bool |
|
119 | - { |
|
120 | - return in_array($oid, $this->_expectedPolicies); |
|
121 | - } |
|
111 | + /** |
|
112 | + * Check whether node has OID as an expected policy. |
|
113 | + * |
|
114 | + * @param string $oid |
|
115 | + * |
|
116 | + * @return bool |
|
117 | + */ |
|
118 | + public function hasExpectedPolicy(string $oid): bool |
|
119 | + { |
|
120 | + return in_array($oid, $this->_expectedPolicies); |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * Get the expected policy set. |
|
125 | - * |
|
126 | - * @return string[] |
|
127 | - */ |
|
128 | - public function expectedPolicies(): array |
|
129 | - { |
|
130 | - return $this->_expectedPolicies; |
|
131 | - } |
|
123 | + /** |
|
124 | + * Get the expected policy set. |
|
125 | + * |
|
126 | + * @return string[] |
|
127 | + */ |
|
128 | + public function expectedPolicies(): array |
|
129 | + { |
|
130 | + return $this->_expectedPolicies; |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * Set expected policies. |
|
135 | - * |
|
136 | - * @param string ...$oids Policy OIDs |
|
137 | - */ |
|
138 | - public function setExpectedPolicies(string ...$oids): void |
|
139 | - { |
|
140 | - $this->_expectedPolicies = $oids; |
|
141 | - } |
|
133 | + /** |
|
134 | + * Set expected policies. |
|
135 | + * |
|
136 | + * @param string ...$oids Policy OIDs |
|
137 | + */ |
|
138 | + public function setExpectedPolicies(string ...$oids): void |
|
139 | + { |
|
140 | + $this->_expectedPolicies = $oids; |
|
141 | + } |
|
142 | 142 | |
143 | - /** |
|
144 | - * Check whether node has a child node with given valid policy OID. |
|
145 | - * |
|
146 | - * @param string $oid |
|
147 | - * |
|
148 | - * @return bool |
|
149 | - */ |
|
150 | - public function hasChildWithValidPolicy(string $oid): bool |
|
151 | - { |
|
152 | - foreach ($this->_children as $node) { |
|
153 | - if ($node->validPolicy() == $oid) { |
|
154 | - return true; |
|
155 | - } |
|
156 | - } |
|
157 | - return false; |
|
158 | - } |
|
143 | + /** |
|
144 | + * Check whether node has a child node with given valid policy OID. |
|
145 | + * |
|
146 | + * @param string $oid |
|
147 | + * |
|
148 | + * @return bool |
|
149 | + */ |
|
150 | + public function hasChildWithValidPolicy(string $oid): bool |
|
151 | + { |
|
152 | + foreach ($this->_children as $node) { |
|
153 | + if ($node->validPolicy() == $oid) { |
|
154 | + return true; |
|
155 | + } |
|
156 | + } |
|
157 | + return false; |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * Add child node. |
|
162 | - * |
|
163 | - * @param PolicyNode $node |
|
164 | - * |
|
165 | - * @return self |
|
166 | - */ |
|
167 | - public function addChild(PolicyNode $node): self |
|
168 | - { |
|
169 | - $id = spl_object_hash($node); |
|
170 | - $node->_parent = $this; |
|
171 | - $this->_children[$id] = $node; |
|
172 | - return $this; |
|
173 | - } |
|
160 | + /** |
|
161 | + * Add child node. |
|
162 | + * |
|
163 | + * @param PolicyNode $node |
|
164 | + * |
|
165 | + * @return self |
|
166 | + */ |
|
167 | + public function addChild(PolicyNode $node): self |
|
168 | + { |
|
169 | + $id = spl_object_hash($node); |
|
170 | + $node->_parent = $this; |
|
171 | + $this->_children[$id] = $node; |
|
172 | + return $this; |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * Get the child nodes. |
|
177 | - * |
|
178 | - * @return PolicyNode[] |
|
179 | - */ |
|
180 | - public function children(): array |
|
181 | - { |
|
182 | - return array_values($this->_children); |
|
183 | - } |
|
175 | + /** |
|
176 | + * Get the child nodes. |
|
177 | + * |
|
178 | + * @return PolicyNode[] |
|
179 | + */ |
|
180 | + public function children(): array |
|
181 | + { |
|
182 | + return array_values($this->_children); |
|
183 | + } |
|
184 | 184 | |
185 | - /** |
|
186 | - * Remove this node from the tree. |
|
187 | - * |
|
188 | - * @return self The removed node |
|
189 | - */ |
|
190 | - public function remove(): self |
|
191 | - { |
|
192 | - if ($this->_parent) { |
|
193 | - $id = spl_object_hash($this); |
|
194 | - unset($this->_parent->_children[$id], $this->_parent); |
|
195 | - } |
|
196 | - return $this; |
|
197 | - } |
|
185 | + /** |
|
186 | + * Remove this node from the tree. |
|
187 | + * |
|
188 | + * @return self The removed node |
|
189 | + */ |
|
190 | + public function remove(): self |
|
191 | + { |
|
192 | + if ($this->_parent) { |
|
193 | + $id = spl_object_hash($this); |
|
194 | + unset($this->_parent->_children[$id], $this->_parent); |
|
195 | + } |
|
196 | + return $this; |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Check whether node has a parent. |
|
201 | - * |
|
202 | - * @return bool |
|
203 | - */ |
|
204 | - public function hasParent(): bool |
|
205 | - { |
|
206 | - return isset($this->_parent); |
|
207 | - } |
|
199 | + /** |
|
200 | + * Check whether node has a parent. |
|
201 | + * |
|
202 | + * @return bool |
|
203 | + */ |
|
204 | + public function hasParent(): bool |
|
205 | + { |
|
206 | + return isset($this->_parent); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Get the parent node. |
|
211 | - * |
|
212 | - * @return null|PolicyNode |
|
213 | - */ |
|
214 | - public function parent(): ?PolicyNode |
|
215 | - { |
|
216 | - return $this->_parent; |
|
217 | - } |
|
209 | + /** |
|
210 | + * Get the parent node. |
|
211 | + * |
|
212 | + * @return null|PolicyNode |
|
213 | + */ |
|
214 | + public function parent(): ?PolicyNode |
|
215 | + { |
|
216 | + return $this->_parent; |
|
217 | + } |
|
218 | 218 | |
219 | - /** |
|
220 | - * Get chain of parent nodes from this node's parent to the root node. |
|
221 | - * |
|
222 | - * @return PolicyNode[] |
|
223 | - */ |
|
224 | - public function parents(): array |
|
225 | - { |
|
226 | - if (!$this->_parent) { |
|
227 | - return []; |
|
228 | - } |
|
229 | - $nodes = $this->_parent->parents(); |
|
230 | - $nodes[] = $this->_parent; |
|
231 | - return array_reverse($nodes); |
|
232 | - } |
|
219 | + /** |
|
220 | + * Get chain of parent nodes from this node's parent to the root node. |
|
221 | + * |
|
222 | + * @return PolicyNode[] |
|
223 | + */ |
|
224 | + public function parents(): array |
|
225 | + { |
|
226 | + if (!$this->_parent) { |
|
227 | + return []; |
|
228 | + } |
|
229 | + $nodes = $this->_parent->parents(); |
|
230 | + $nodes[] = $this->_parent; |
|
231 | + return array_reverse($nodes); |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * Walk tree from this node, applying a callback for each node. |
|
236 | - * |
|
237 | - * Nodes are traversed depth-first and callback shall be applied post-order. |
|
238 | - * |
|
239 | - * @param callable $fn |
|
240 | - */ |
|
241 | - public function walkNodes(callable $fn): void |
|
242 | - { |
|
243 | - foreach ($this->_children as $node) { |
|
244 | - $node->walkNodes($fn); |
|
245 | - } |
|
246 | - $fn($this); |
|
247 | - } |
|
234 | + /** |
|
235 | + * Walk tree from this node, applying a callback for each node. |
|
236 | + * |
|
237 | + * Nodes are traversed depth-first and callback shall be applied post-order. |
|
238 | + * |
|
239 | + * @param callable $fn |
|
240 | + */ |
|
241 | + public function walkNodes(callable $fn): void |
|
242 | + { |
|
243 | + foreach ($this->_children as $node) { |
|
244 | + $node->walkNodes($fn); |
|
245 | + } |
|
246 | + $fn($this); |
|
247 | + } |
|
248 | 248 | |
249 | - /** |
|
250 | - * Get the total number of nodes in a tree. |
|
251 | - * |
|
252 | - * @return int |
|
253 | - */ |
|
254 | - public function nodeCount(): int |
|
255 | - { |
|
256 | - $c = 1; |
|
257 | - foreach ($this->_children as $child) { |
|
258 | - $c += $child->nodeCount(); |
|
259 | - } |
|
260 | - return $c; |
|
261 | - } |
|
249 | + /** |
|
250 | + * Get the total number of nodes in a tree. |
|
251 | + * |
|
252 | + * @return int |
|
253 | + */ |
|
254 | + public function nodeCount(): int |
|
255 | + { |
|
256 | + $c = 1; |
|
257 | + foreach ($this->_children as $child) { |
|
258 | + $c += $child->nodeCount(); |
|
259 | + } |
|
260 | + return $c; |
|
261 | + } |
|
262 | 262 | |
263 | - /** |
|
264 | - * Get the number of child nodes. |
|
265 | - * |
|
266 | - * @see \Countable::count() |
|
267 | - */ |
|
268 | - public function count(): int |
|
269 | - { |
|
270 | - return count($this->_children); |
|
271 | - } |
|
263 | + /** |
|
264 | + * Get the number of child nodes. |
|
265 | + * |
|
266 | + * @see \Countable::count() |
|
267 | + */ |
|
268 | + public function count(): int |
|
269 | + { |
|
270 | + return count($this->_children); |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * Get iterator for the child nodes. |
|
275 | - * |
|
276 | - * @see \IteratorAggregate::getIterator() |
|
277 | - */ |
|
278 | - public function getIterator(): \ArrayIterator |
|
279 | - { |
|
280 | - return new \ArrayIterator($this->_children); |
|
281 | - } |
|
273 | + /** |
|
274 | + * Get iterator for the child nodes. |
|
275 | + * |
|
276 | + * @see \IteratorAggregate::getIterator() |
|
277 | + */ |
|
278 | + public function getIterator(): \ArrayIterator |
|
279 | + { |
|
280 | + return new \ArrayIterator($this->_children); |
|
281 | + } |
|
282 | 282 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\CertificationPath\Policy; |
6 | 6 |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\CertificationPath; |
6 | 6 |
@@ -24,178 +24,178 @@ |
||
24 | 24 | */ |
25 | 25 | class CertificationPath implements \Countable, \IteratorAggregate |
26 | 26 | { |
27 | - /** |
|
28 | - * Certification path. |
|
29 | - * |
|
30 | - * @var Certificate[] |
|
31 | - */ |
|
32 | - protected $_certificates; |
|
33 | - |
|
34 | - /** |
|
35 | - * Constructor. |
|
36 | - * |
|
37 | - * @param Certificate ...$certificates Certificates from the trust anchor |
|
38 | - * to the target end-entity certificate |
|
39 | - */ |
|
40 | - public function __construct(Certificate ...$certificates) |
|
41 | - { |
|
42 | - $this->_certificates = $certificates; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Initialize from a certificate chain. |
|
47 | - * |
|
48 | - * @param CertificateChain $chain |
|
49 | - * |
|
50 | - * @return self |
|
51 | - */ |
|
52 | - public static function fromCertificateChain(CertificateChain $chain): self |
|
53 | - { |
|
54 | - return new self(...array_reverse($chain->certificates(), false)); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Build certification path to given target. |
|
59 | - * |
|
60 | - * @param Certificate $target Target end-entity certificate |
|
61 | - * @param CertificateBundle $trust_anchors List of trust anchors |
|
62 | - * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
63 | - * |
|
64 | - * @return self |
|
65 | - */ |
|
66 | - public static function toTarget(Certificate $target, |
|
67 | - CertificateBundle $trust_anchors, ?CertificateBundle $intermediate = null): self |
|
68 | - { |
|
69 | - $builder = new CertificationPathBuilder($trust_anchors); |
|
70 | - return $builder->shortestPathToTarget($target, $intermediate); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Build certification path from given trust anchor to target certificate, |
|
75 | - * using intermediate certificates from given bundle. |
|
76 | - * |
|
77 | - * @param Certificate $trust_anchor Trust anchor certificate |
|
78 | - * @param Certificate $target Target end-entity certificate |
|
79 | - * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
80 | - * |
|
81 | - * @return self |
|
82 | - */ |
|
83 | - public static function fromTrustAnchorToTarget(Certificate $trust_anchor, |
|
84 | - Certificate $target, ?CertificateBundle $intermediate = null): self |
|
85 | - { |
|
86 | - return self::toTarget($target, new CertificateBundle($trust_anchor), |
|
87 | - $intermediate); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Get certificates. |
|
92 | - * |
|
93 | - * @return Certificate[] |
|
94 | - */ |
|
95 | - public function certificates(): array |
|
96 | - { |
|
97 | - return $this->_certificates; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Get the trust anchor certificate from the path. |
|
102 | - * |
|
103 | - * @throws \LogicException If path is empty |
|
104 | - * |
|
105 | - * @return Certificate |
|
106 | - */ |
|
107 | - public function trustAnchorCertificate(): Certificate |
|
108 | - { |
|
109 | - if (!count($this->_certificates)) { |
|
110 | - throw new \LogicException('No certificates.'); |
|
111 | - } |
|
112 | - return $this->_certificates[0]; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Get the end-entity certificate from the path. |
|
117 | - * |
|
118 | - * @throws \LogicException If path is empty |
|
119 | - * |
|
120 | - * @return Certificate |
|
121 | - */ |
|
122 | - public function endEntityCertificate(): Certificate |
|
123 | - { |
|
124 | - if (!count($this->_certificates)) { |
|
125 | - throw new \LogicException('No certificates.'); |
|
126 | - } |
|
127 | - return $this->_certificates[count($this->_certificates) - 1]; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Get certification path as a certificate chain. |
|
132 | - * |
|
133 | - * @return CertificateChain |
|
134 | - */ |
|
135 | - public function certificateChain(): CertificateChain |
|
136 | - { |
|
137 | - return new CertificateChain(...array_reverse($this->_certificates, false)); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Check whether certification path starts with one ore more given |
|
142 | - * certificates in parameter order. |
|
143 | - * |
|
144 | - * @param Certificate ...$certs Certificates |
|
145 | - * |
|
146 | - * @return bool |
|
147 | - */ |
|
148 | - public function startsWith(Certificate ...$certs): bool |
|
149 | - { |
|
150 | - $n = count($certs); |
|
151 | - if ($n > count($this->_certificates)) { |
|
152 | - return false; |
|
153 | - } |
|
154 | - for ($i = 0; $i < $n; ++$i) { |
|
155 | - if (!$certs[$i]->equals($this->_certificates[$i])) { |
|
156 | - return false; |
|
157 | - } |
|
158 | - } |
|
159 | - return true; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Validate certification path. |
|
164 | - * |
|
165 | - * @param PathValidationConfig $config |
|
166 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
167 | - * |
|
168 | - * @throws Exception\PathValidationException |
|
169 | - * |
|
170 | - * @return PathValidationResult |
|
171 | - */ |
|
172 | - public function validate(PathValidationConfig $config, |
|
173 | - ?Crypto $crypto = null): PathValidationResult |
|
174 | - { |
|
175 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
176 | - $validator = new PathValidator($crypto, $config, ...$this->_certificates); |
|
177 | - return $validator->validate(); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @see \Countable::count() |
|
182 | - * |
|
183 | - * @return int |
|
184 | - */ |
|
185 | - public function count(): int |
|
186 | - { |
|
187 | - return count($this->_certificates); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Get iterator for certificates. |
|
192 | - * |
|
193 | - * @see \IteratorAggregate::getIterator() |
|
194 | - * |
|
195 | - * @return \ArrayIterator |
|
196 | - */ |
|
197 | - public function getIterator(): \ArrayIterator |
|
198 | - { |
|
199 | - return new \ArrayIterator($this->_certificates); |
|
200 | - } |
|
27 | + /** |
|
28 | + * Certification path. |
|
29 | + * |
|
30 | + * @var Certificate[] |
|
31 | + */ |
|
32 | + protected $_certificates; |
|
33 | + |
|
34 | + /** |
|
35 | + * Constructor. |
|
36 | + * |
|
37 | + * @param Certificate ...$certificates Certificates from the trust anchor |
|
38 | + * to the target end-entity certificate |
|
39 | + */ |
|
40 | + public function __construct(Certificate ...$certificates) |
|
41 | + { |
|
42 | + $this->_certificates = $certificates; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Initialize from a certificate chain. |
|
47 | + * |
|
48 | + * @param CertificateChain $chain |
|
49 | + * |
|
50 | + * @return self |
|
51 | + */ |
|
52 | + public static function fromCertificateChain(CertificateChain $chain): self |
|
53 | + { |
|
54 | + return new self(...array_reverse($chain->certificates(), false)); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Build certification path to given target. |
|
59 | + * |
|
60 | + * @param Certificate $target Target end-entity certificate |
|
61 | + * @param CertificateBundle $trust_anchors List of trust anchors |
|
62 | + * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
63 | + * |
|
64 | + * @return self |
|
65 | + */ |
|
66 | + public static function toTarget(Certificate $target, |
|
67 | + CertificateBundle $trust_anchors, ?CertificateBundle $intermediate = null): self |
|
68 | + { |
|
69 | + $builder = new CertificationPathBuilder($trust_anchors); |
|
70 | + return $builder->shortestPathToTarget($target, $intermediate); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Build certification path from given trust anchor to target certificate, |
|
75 | + * using intermediate certificates from given bundle. |
|
76 | + * |
|
77 | + * @param Certificate $trust_anchor Trust anchor certificate |
|
78 | + * @param Certificate $target Target end-entity certificate |
|
79 | + * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
80 | + * |
|
81 | + * @return self |
|
82 | + */ |
|
83 | + public static function fromTrustAnchorToTarget(Certificate $trust_anchor, |
|
84 | + Certificate $target, ?CertificateBundle $intermediate = null): self |
|
85 | + { |
|
86 | + return self::toTarget($target, new CertificateBundle($trust_anchor), |
|
87 | + $intermediate); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Get certificates. |
|
92 | + * |
|
93 | + * @return Certificate[] |
|
94 | + */ |
|
95 | + public function certificates(): array |
|
96 | + { |
|
97 | + return $this->_certificates; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Get the trust anchor certificate from the path. |
|
102 | + * |
|
103 | + * @throws \LogicException If path is empty |
|
104 | + * |
|
105 | + * @return Certificate |
|
106 | + */ |
|
107 | + public function trustAnchorCertificate(): Certificate |
|
108 | + { |
|
109 | + if (!count($this->_certificates)) { |
|
110 | + throw new \LogicException('No certificates.'); |
|
111 | + } |
|
112 | + return $this->_certificates[0]; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Get the end-entity certificate from the path. |
|
117 | + * |
|
118 | + * @throws \LogicException If path is empty |
|
119 | + * |
|
120 | + * @return Certificate |
|
121 | + */ |
|
122 | + public function endEntityCertificate(): Certificate |
|
123 | + { |
|
124 | + if (!count($this->_certificates)) { |
|
125 | + throw new \LogicException('No certificates.'); |
|
126 | + } |
|
127 | + return $this->_certificates[count($this->_certificates) - 1]; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Get certification path as a certificate chain. |
|
132 | + * |
|
133 | + * @return CertificateChain |
|
134 | + */ |
|
135 | + public function certificateChain(): CertificateChain |
|
136 | + { |
|
137 | + return new CertificateChain(...array_reverse($this->_certificates, false)); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Check whether certification path starts with one ore more given |
|
142 | + * certificates in parameter order. |
|
143 | + * |
|
144 | + * @param Certificate ...$certs Certificates |
|
145 | + * |
|
146 | + * @return bool |
|
147 | + */ |
|
148 | + public function startsWith(Certificate ...$certs): bool |
|
149 | + { |
|
150 | + $n = count($certs); |
|
151 | + if ($n > count($this->_certificates)) { |
|
152 | + return false; |
|
153 | + } |
|
154 | + for ($i = 0; $i < $n; ++$i) { |
|
155 | + if (!$certs[$i]->equals($this->_certificates[$i])) { |
|
156 | + return false; |
|
157 | + } |
|
158 | + } |
|
159 | + return true; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Validate certification path. |
|
164 | + * |
|
165 | + * @param PathValidationConfig $config |
|
166 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
167 | + * |
|
168 | + * @throws Exception\PathValidationException |
|
169 | + * |
|
170 | + * @return PathValidationResult |
|
171 | + */ |
|
172 | + public function validate(PathValidationConfig $config, |
|
173 | + ?Crypto $crypto = null): PathValidationResult |
|
174 | + { |
|
175 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
176 | + $validator = new PathValidator($crypto, $config, ...$this->_certificates); |
|
177 | + return $validator->validate(); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @see \Countable::count() |
|
182 | + * |
|
183 | + * @return int |
|
184 | + */ |
|
185 | + public function count(): int |
|
186 | + { |
|
187 | + return count($this->_certificates); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Get iterator for certificates. |
|
192 | + * |
|
193 | + * @see \IteratorAggregate::getIterator() |
|
194 | + * |
|
195 | + * @return \ArrayIterator |
|
196 | + */ |
|
197 | + public function getIterator(): \ArrayIterator |
|
198 | + { |
|
199 | + return new \ArrayIterator($this->_certificates); |
|
200 | + } |
|
201 | 201 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\CertificationPath\Exception; |
6 | 6 |
@@ -12,114 +12,114 @@ |
||
12 | 12 | */ |
13 | 13 | class ACValidationConfig |
14 | 14 | { |
15 | - /** |
|
16 | - * Certification path of the AC holder. |
|
17 | - * |
|
18 | - * @var CertificationPath |
|
19 | - */ |
|
20 | - protected $_holderPath; |
|
15 | + /** |
|
16 | + * Certification path of the AC holder. |
|
17 | + * |
|
18 | + * @var CertificationPath |
|
19 | + */ |
|
20 | + protected $_holderPath; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Certification path of the AC issuer. |
|
24 | - * |
|
25 | - * @var CertificationPath |
|
26 | - */ |
|
27 | - protected $_issuerPath; |
|
22 | + /** |
|
23 | + * Certification path of the AC issuer. |
|
24 | + * |
|
25 | + * @var CertificationPath |
|
26 | + */ |
|
27 | + protected $_issuerPath; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Evaluation reference time. |
|
31 | - * |
|
32 | - * @var \DateTimeImmutable |
|
33 | - */ |
|
34 | - protected $_evalTime; |
|
29 | + /** |
|
30 | + * Evaluation reference time. |
|
31 | + * |
|
32 | + * @var \DateTimeImmutable |
|
33 | + */ |
|
34 | + protected $_evalTime; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Permitted targets. |
|
38 | - * |
|
39 | - * @var Target[] |
|
40 | - */ |
|
41 | - protected $_targets; |
|
36 | + /** |
|
37 | + * Permitted targets. |
|
38 | + * |
|
39 | + * @var Target[] |
|
40 | + */ |
|
41 | + protected $_targets; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Constructor. |
|
45 | - * |
|
46 | - * @param CertificationPath $holder_path Certification path of the AC holder |
|
47 | - * @param CertificationPath $issuer_path Certification path of the AC issuer |
|
48 | - */ |
|
49 | - public function __construct(CertificationPath $holder_path, |
|
50 | - CertificationPath $issuer_path) |
|
51 | - { |
|
52 | - $this->_holderPath = $holder_path; |
|
53 | - $this->_issuerPath = $issuer_path; |
|
54 | - $this->_evalTime = new \DateTimeImmutable(); |
|
55 | - $this->_targets = []; |
|
56 | - } |
|
43 | + /** |
|
44 | + * Constructor. |
|
45 | + * |
|
46 | + * @param CertificationPath $holder_path Certification path of the AC holder |
|
47 | + * @param CertificationPath $issuer_path Certification path of the AC issuer |
|
48 | + */ |
|
49 | + public function __construct(CertificationPath $holder_path, |
|
50 | + CertificationPath $issuer_path) |
|
51 | + { |
|
52 | + $this->_holderPath = $holder_path; |
|
53 | + $this->_issuerPath = $issuer_path; |
|
54 | + $this->_evalTime = new \DateTimeImmutable(); |
|
55 | + $this->_targets = []; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Get certification path of the AC's holder. |
|
60 | - * |
|
61 | - * @return CertificationPath |
|
62 | - */ |
|
63 | - public function holderPath(): CertificationPath |
|
64 | - { |
|
65 | - return $this->_holderPath; |
|
66 | - } |
|
58 | + /** |
|
59 | + * Get certification path of the AC's holder. |
|
60 | + * |
|
61 | + * @return CertificationPath |
|
62 | + */ |
|
63 | + public function holderPath(): CertificationPath |
|
64 | + { |
|
65 | + return $this->_holderPath; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Get certification path of the AC's issuer. |
|
70 | - * |
|
71 | - * @return CertificationPath |
|
72 | - */ |
|
73 | - public function issuerPath(): CertificationPath |
|
74 | - { |
|
75 | - return $this->_issuerPath; |
|
76 | - } |
|
68 | + /** |
|
69 | + * Get certification path of the AC's issuer. |
|
70 | + * |
|
71 | + * @return CertificationPath |
|
72 | + */ |
|
73 | + public function issuerPath(): CertificationPath |
|
74 | + { |
|
75 | + return $this->_issuerPath; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Get self with given evaluation reference time. |
|
80 | - * |
|
81 | - * @param \DateTimeImmutable $dt |
|
82 | - * |
|
83 | - * @return self |
|
84 | - */ |
|
85 | - public function withEvaluationTime(\DateTimeImmutable $dt): self |
|
86 | - { |
|
87 | - $obj = clone $this; |
|
88 | - $obj->_evalTime = $dt; |
|
89 | - return $obj; |
|
90 | - } |
|
78 | + /** |
|
79 | + * Get self with given evaluation reference time. |
|
80 | + * |
|
81 | + * @param \DateTimeImmutable $dt |
|
82 | + * |
|
83 | + * @return self |
|
84 | + */ |
|
85 | + public function withEvaluationTime(\DateTimeImmutable $dt): self |
|
86 | + { |
|
87 | + $obj = clone $this; |
|
88 | + $obj->_evalTime = $dt; |
|
89 | + return $obj; |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * Get the evaluation reference time. |
|
94 | - * |
|
95 | - * @return \DateTimeImmutable |
|
96 | - */ |
|
97 | - public function evaluationTime(): \DateTimeImmutable |
|
98 | - { |
|
99 | - return $this->_evalTime; |
|
100 | - } |
|
92 | + /** |
|
93 | + * Get the evaluation reference time. |
|
94 | + * |
|
95 | + * @return \DateTimeImmutable |
|
96 | + */ |
|
97 | + public function evaluationTime(): \DateTimeImmutable |
|
98 | + { |
|
99 | + return $this->_evalTime; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Get self with permitted targets. |
|
104 | - * |
|
105 | - * @param Target ...$targets |
|
106 | - * |
|
107 | - * @return self |
|
108 | - */ |
|
109 | - public function withTargets(Target ...$targets): self |
|
110 | - { |
|
111 | - $obj = clone $this; |
|
112 | - $obj->_targets = $targets; |
|
113 | - return $obj; |
|
114 | - } |
|
102 | + /** |
|
103 | + * Get self with permitted targets. |
|
104 | + * |
|
105 | + * @param Target ...$targets |
|
106 | + * |
|
107 | + * @return self |
|
108 | + */ |
|
109 | + public function withTargets(Target ...$targets): self |
|
110 | + { |
|
111 | + $obj = clone $this; |
|
112 | + $obj->_targets = $targets; |
|
113 | + return $obj; |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Get array of permitted targets. |
|
118 | - * |
|
119 | - * @return Target[] |
|
120 | - */ |
|
121 | - public function targets(): array |
|
122 | - { |
|
123 | - return $this->_targets; |
|
124 | - } |
|
116 | + /** |
|
117 | + * Get array of permitted targets. |
|
118 | + * |
|
119 | + * @return Target[] |
|
120 | + */ |
|
121 | + public function targets(): array |
|
122 | + { |
|
123 | + return $this->_targets; |
|
124 | + } |
|
125 | 125 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate\Validation; |
6 | 6 |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate\Validation\Exception; |
6 | 6 |
@@ -21,186 +21,186 @@ |
||
21 | 21 | */ |
22 | 22 | class ACValidator |
23 | 23 | { |
24 | - /** |
|
25 | - * Attribute certificate. |
|
26 | - * |
|
27 | - * @var AttributeCertificate |
|
28 | - */ |
|
29 | - protected $_ac; |
|
24 | + /** |
|
25 | + * Attribute certificate. |
|
26 | + * |
|
27 | + * @var AttributeCertificate |
|
28 | + */ |
|
29 | + protected $_ac; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Validation configuration. |
|
33 | - * |
|
34 | - * @var ACValidationConfig |
|
35 | - */ |
|
36 | - protected $_config; |
|
31 | + /** |
|
32 | + * Validation configuration. |
|
33 | + * |
|
34 | + * @var ACValidationConfig |
|
35 | + */ |
|
36 | + protected $_config; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Crypto engine. |
|
40 | - * |
|
41 | - * @var Crypto |
|
42 | - */ |
|
43 | - protected $_crypto; |
|
38 | + /** |
|
39 | + * Crypto engine. |
|
40 | + * |
|
41 | + * @var Crypto |
|
42 | + */ |
|
43 | + protected $_crypto; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Constructor. |
|
47 | - * |
|
48 | - * @param AttributeCertificate $ac Attribute certificate to validate |
|
49 | - * @param ACValidationConfig $config Validation configuration |
|
50 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
51 | - */ |
|
52 | - public function __construct(AttributeCertificate $ac, |
|
53 | - ACValidationConfig $config, ?Crypto $crypto = null) |
|
54 | - { |
|
55 | - $this->_ac = $ac; |
|
56 | - $this->_config = $config; |
|
57 | - $this->_crypto = $crypto ?? Crypto::getDefault(); |
|
58 | - } |
|
45 | + /** |
|
46 | + * Constructor. |
|
47 | + * |
|
48 | + * @param AttributeCertificate $ac Attribute certificate to validate |
|
49 | + * @param ACValidationConfig $config Validation configuration |
|
50 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
51 | + */ |
|
52 | + public function __construct(AttributeCertificate $ac, |
|
53 | + ACValidationConfig $config, ?Crypto $crypto = null) |
|
54 | + { |
|
55 | + $this->_ac = $ac; |
|
56 | + $this->_config = $config; |
|
57 | + $this->_crypto = $crypto ?? Crypto::getDefault(); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Validate attribute certificate. |
|
62 | - * |
|
63 | - * @throws ACValidationException If validation fails |
|
64 | - * |
|
65 | - * @return AttributeCertificate Validated AC |
|
66 | - */ |
|
67 | - public function validate(): AttributeCertificate |
|
68 | - { |
|
69 | - $this->_validateHolder(); |
|
70 | - $issuer = $this->_verifyIssuer(); |
|
71 | - $this->_validateIssuerProfile($issuer); |
|
72 | - $this->_validateTime(); |
|
73 | - $this->_validateTargeting(); |
|
74 | - return $this->_ac; |
|
75 | - } |
|
60 | + /** |
|
61 | + * Validate attribute certificate. |
|
62 | + * |
|
63 | + * @throws ACValidationException If validation fails |
|
64 | + * |
|
65 | + * @return AttributeCertificate Validated AC |
|
66 | + */ |
|
67 | + public function validate(): AttributeCertificate |
|
68 | + { |
|
69 | + $this->_validateHolder(); |
|
70 | + $issuer = $this->_verifyIssuer(); |
|
71 | + $this->_validateIssuerProfile($issuer); |
|
72 | + $this->_validateTime(); |
|
73 | + $this->_validateTargeting(); |
|
74 | + return $this->_ac; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Validate AC holder's certification. |
|
79 | - * |
|
80 | - * @throws ACValidationException |
|
81 | - * |
|
82 | - * @return Certificate Certificate of the AC's holder |
|
83 | - */ |
|
84 | - private function _validateHolder(): Certificate |
|
85 | - { |
|
86 | - $path = $this->_config->holderPath(); |
|
87 | - $config = PathValidationConfig::defaultConfig() |
|
88 | - ->withMaxLength(count($path)) |
|
89 | - ->withDateTime($this->_config->evaluationTime()); |
|
90 | - try { |
|
91 | - $holder = $path->validate($config, $this->_crypto)->certificate(); |
|
92 | - } catch (PathValidationException $e) { |
|
93 | - throw new ACValidationException( |
|
94 | - "Failed to validate holder PKC's certification path.", 0, $e); |
|
95 | - } |
|
96 | - if (!$this->_ac->isHeldBy($holder)) { |
|
97 | - throw new ACValidationException("Name mismatch of AC's holder PKC."); |
|
98 | - } |
|
99 | - return $holder; |
|
100 | - } |
|
77 | + /** |
|
78 | + * Validate AC holder's certification. |
|
79 | + * |
|
80 | + * @throws ACValidationException |
|
81 | + * |
|
82 | + * @return Certificate Certificate of the AC's holder |
|
83 | + */ |
|
84 | + private function _validateHolder(): Certificate |
|
85 | + { |
|
86 | + $path = $this->_config->holderPath(); |
|
87 | + $config = PathValidationConfig::defaultConfig() |
|
88 | + ->withMaxLength(count($path)) |
|
89 | + ->withDateTime($this->_config->evaluationTime()); |
|
90 | + try { |
|
91 | + $holder = $path->validate($config, $this->_crypto)->certificate(); |
|
92 | + } catch (PathValidationException $e) { |
|
93 | + throw new ACValidationException( |
|
94 | + "Failed to validate holder PKC's certification path.", 0, $e); |
|
95 | + } |
|
96 | + if (!$this->_ac->isHeldBy($holder)) { |
|
97 | + throw new ACValidationException("Name mismatch of AC's holder PKC."); |
|
98 | + } |
|
99 | + return $holder; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Verify AC's signature and issuer's certification. |
|
104 | - * |
|
105 | - * @throws ACValidationException |
|
106 | - * |
|
107 | - * @return Certificate Certificate of the AC's issuer |
|
108 | - */ |
|
109 | - private function _verifyIssuer(): Certificate |
|
110 | - { |
|
111 | - $path = $this->_config->issuerPath(); |
|
112 | - $config = PathValidationConfig::defaultConfig() |
|
113 | - ->withMaxLength(count($path)) |
|
114 | - ->withDateTime($this->_config->evaluationTime()); |
|
115 | - try { |
|
116 | - $issuer = $path->validate($config, $this->_crypto)->certificate(); |
|
117 | - } catch (PathValidationException $e) { |
|
118 | - throw new ACValidationException( |
|
119 | - "Failed to validate issuer PKC's certification path.", 0, $e); |
|
120 | - } |
|
121 | - if (!$this->_ac->isIssuedBy($issuer)) { |
|
122 | - throw new ACValidationException("Name mismatch of AC's issuer PKC."); |
|
123 | - } |
|
124 | - $pubkey_info = $issuer->tbsCertificate()->subjectPublicKeyInfo(); |
|
125 | - if (!$this->_ac->verify($pubkey_info, $this->_crypto)) { |
|
126 | - throw new ACValidationException('Failed to verify signature.'); |
|
127 | - } |
|
128 | - return $issuer; |
|
129 | - } |
|
102 | + /** |
|
103 | + * Verify AC's signature and issuer's certification. |
|
104 | + * |
|
105 | + * @throws ACValidationException |
|
106 | + * |
|
107 | + * @return Certificate Certificate of the AC's issuer |
|
108 | + */ |
|
109 | + private function _verifyIssuer(): Certificate |
|
110 | + { |
|
111 | + $path = $this->_config->issuerPath(); |
|
112 | + $config = PathValidationConfig::defaultConfig() |
|
113 | + ->withMaxLength(count($path)) |
|
114 | + ->withDateTime($this->_config->evaluationTime()); |
|
115 | + try { |
|
116 | + $issuer = $path->validate($config, $this->_crypto)->certificate(); |
|
117 | + } catch (PathValidationException $e) { |
|
118 | + throw new ACValidationException( |
|
119 | + "Failed to validate issuer PKC's certification path.", 0, $e); |
|
120 | + } |
|
121 | + if (!$this->_ac->isIssuedBy($issuer)) { |
|
122 | + throw new ACValidationException("Name mismatch of AC's issuer PKC."); |
|
123 | + } |
|
124 | + $pubkey_info = $issuer->tbsCertificate()->subjectPublicKeyInfo(); |
|
125 | + if (!$this->_ac->verify($pubkey_info, $this->_crypto)) { |
|
126 | + throw new ACValidationException('Failed to verify signature.'); |
|
127 | + } |
|
128 | + return $issuer; |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * Validate AC issuer's profile. |
|
133 | - * |
|
134 | - * @see https://tools.ietf.org/html/rfc5755#section-4.5 |
|
135 | - * |
|
136 | - * @param Certificate $cert |
|
137 | - * |
|
138 | - * @throws ACValidationException |
|
139 | - */ |
|
140 | - private function _validateIssuerProfile(Certificate $cert): void |
|
141 | - { |
|
142 | - $exts = $cert->tbsCertificate()->extensions(); |
|
143 | - if ($exts->hasKeyUsage() && !$exts->keyUsage()->isDigitalSignature()) { |
|
144 | - throw new ACValidationException( |
|
145 | - "Issuer PKC's Key Usage extension doesn't permit" . |
|
146 | - ' verification of digital signatures.'); |
|
147 | - } |
|
148 | - if ($exts->hasBasicConstraints() && $exts->basicConstraints()->isCA()) { |
|
149 | - throw new ACValidationException('Issuer PKC must not be a CA.'); |
|
150 | - } |
|
151 | - } |
|
131 | + /** |
|
132 | + * Validate AC issuer's profile. |
|
133 | + * |
|
134 | + * @see https://tools.ietf.org/html/rfc5755#section-4.5 |
|
135 | + * |
|
136 | + * @param Certificate $cert |
|
137 | + * |
|
138 | + * @throws ACValidationException |
|
139 | + */ |
|
140 | + private function _validateIssuerProfile(Certificate $cert): void |
|
141 | + { |
|
142 | + $exts = $cert->tbsCertificate()->extensions(); |
|
143 | + if ($exts->hasKeyUsage() && !$exts->keyUsage()->isDigitalSignature()) { |
|
144 | + throw new ACValidationException( |
|
145 | + "Issuer PKC's Key Usage extension doesn't permit" . |
|
146 | + ' verification of digital signatures.'); |
|
147 | + } |
|
148 | + if ($exts->hasBasicConstraints() && $exts->basicConstraints()->isCA()) { |
|
149 | + throw new ACValidationException('Issuer PKC must not be a CA.'); |
|
150 | + } |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Validate AC's validity period. |
|
155 | - * |
|
156 | - * @throws ACValidationException |
|
157 | - */ |
|
158 | - private function _validateTime(): void |
|
159 | - { |
|
160 | - $t = $this->_config->evaluationTime(); |
|
161 | - $validity = $this->_ac->acinfo()->validityPeriod(); |
|
162 | - if ($validity->notBeforeTime()->diff($t)->invert) { |
|
163 | - throw new ACValidationException('Validity period has not started.'); |
|
164 | - } |
|
165 | - if ($t->diff($validity->notAfterTime())->invert) { |
|
166 | - throw new ACValidationException('Attribute certificate has expired.'); |
|
167 | - } |
|
168 | - } |
|
153 | + /** |
|
154 | + * Validate AC's validity period. |
|
155 | + * |
|
156 | + * @throws ACValidationException |
|
157 | + */ |
|
158 | + private function _validateTime(): void |
|
159 | + { |
|
160 | + $t = $this->_config->evaluationTime(); |
|
161 | + $validity = $this->_ac->acinfo()->validityPeriod(); |
|
162 | + if ($validity->notBeforeTime()->diff($t)->invert) { |
|
163 | + throw new ACValidationException('Validity period has not started.'); |
|
164 | + } |
|
165 | + if ($t->diff($validity->notAfterTime())->invert) { |
|
166 | + throw new ACValidationException('Attribute certificate has expired.'); |
|
167 | + } |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * Validate AC's target information. |
|
172 | - * |
|
173 | - * @throws ACValidationException |
|
174 | - */ |
|
175 | - private function _validateTargeting(): void |
|
176 | - { |
|
177 | - $exts = $this->_ac->acinfo()->extensions(); |
|
178 | - // if target information extension is not present |
|
179 | - if (!$exts->has(Extension::OID_TARGET_INFORMATION)) { |
|
180 | - return; |
|
181 | - } |
|
182 | - $ext = $exts->get(Extension::OID_TARGET_INFORMATION); |
|
183 | - if ($ext instanceof TargetInformationExtension && |
|
184 | - !$this->_hasMatchingTarget($ext->targets())) { |
|
185 | - throw new ACValidationException( |
|
186 | - "Attribute certificate doesn't have a matching target."); |
|
187 | - } |
|
188 | - } |
|
170 | + /** |
|
171 | + * Validate AC's target information. |
|
172 | + * |
|
173 | + * @throws ACValidationException |
|
174 | + */ |
|
175 | + private function _validateTargeting(): void |
|
176 | + { |
|
177 | + $exts = $this->_ac->acinfo()->extensions(); |
|
178 | + // if target information extension is not present |
|
179 | + if (!$exts->has(Extension::OID_TARGET_INFORMATION)) { |
|
180 | + return; |
|
181 | + } |
|
182 | + $ext = $exts->get(Extension::OID_TARGET_INFORMATION); |
|
183 | + if ($ext instanceof TargetInformationExtension && |
|
184 | + !$this->_hasMatchingTarget($ext->targets())) { |
|
185 | + throw new ACValidationException( |
|
186 | + "Attribute certificate doesn't have a matching target."); |
|
187 | + } |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * Check whether validation configuration has matching targets. |
|
192 | - * |
|
193 | - * @param Targets $targets Set of eligible targets |
|
194 | - * |
|
195 | - * @return bool |
|
196 | - */ |
|
197 | - private function _hasMatchingTarget(Targets $targets): bool |
|
198 | - { |
|
199 | - foreach ($this->_config->targets() as $target) { |
|
200 | - if ($targets->hasTarget($target)) { |
|
201 | - return true; |
|
202 | - } |
|
203 | - } |
|
204 | - return false; |
|
205 | - } |
|
190 | + /** |
|
191 | + * Check whether validation configuration has matching targets. |
|
192 | + * |
|
193 | + * @param Targets $targets Set of eligible targets |
|
194 | + * |
|
195 | + * @return bool |
|
196 | + */ |
|
197 | + private function _hasMatchingTarget(Targets $targets): bool |
|
198 | + { |
|
199 | + foreach ($this->_config->targets() as $target) { |
|
200 | + if ($targets->hasTarget($target)) { |
|
201 | + return true; |
|
202 | + } |
|
203 | + } |
|
204 | + return false; |
|
205 | + } |
|
206 | 206 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate\Validation; |
6 | 6 |
@@ -22,450 +22,450 @@ |
||
22 | 22 | */ |
23 | 23 | class AttributeCertificateInfo |
24 | 24 | { |
25 | - const VERSION_2 = 1; |
|
26 | - |
|
27 | - /** |
|
28 | - * AC version. |
|
29 | - * |
|
30 | - * @var int |
|
31 | - */ |
|
32 | - protected $_version; |
|
33 | - |
|
34 | - /** |
|
35 | - * AC holder. |
|
36 | - * |
|
37 | - * @var Holder |
|
38 | - */ |
|
39 | - protected $_holder; |
|
40 | - |
|
41 | - /** |
|
42 | - * AC issuer. |
|
43 | - * |
|
44 | - * @var AttCertIssuer |
|
45 | - */ |
|
46 | - protected $_issuer; |
|
47 | - |
|
48 | - /** |
|
49 | - * Signature algorithm identifier. |
|
50 | - * |
|
51 | - * @var SignatureAlgorithmIdentifier |
|
52 | - */ |
|
53 | - protected $_signature; |
|
54 | - |
|
55 | - /** |
|
56 | - * AC serial number as a base 10 integer. |
|
57 | - * |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - protected $_serialNumber; |
|
61 | - |
|
62 | - /** |
|
63 | - * Validity period. |
|
64 | - * |
|
65 | - * @var AttCertValidityPeriod |
|
66 | - */ |
|
67 | - protected $_attrCertValidityPeriod; |
|
68 | - |
|
69 | - /** |
|
70 | - * Attributes. |
|
71 | - * |
|
72 | - * @var Attributes |
|
73 | - */ |
|
74 | - protected $_attributes; |
|
75 | - |
|
76 | - /** |
|
77 | - * Issuer unique identifier. |
|
78 | - * |
|
79 | - * @var null|UniqueIdentifier |
|
80 | - */ |
|
81 | - protected $_issuerUniqueID; |
|
82 | - |
|
83 | - /** |
|
84 | - * Extensions. |
|
85 | - * |
|
86 | - * @var Extensions |
|
87 | - */ |
|
88 | - protected $_extensions; |
|
89 | - |
|
90 | - /** |
|
91 | - * Constructor. |
|
92 | - * |
|
93 | - * @param Holder $holder AC holder |
|
94 | - * @param AttCertIssuer $issuer AC issuer |
|
95 | - * @param AttCertValidityPeriod $validity Validity |
|
96 | - * @param Attributes $attribs Attributes |
|
97 | - */ |
|
98 | - public function __construct(Holder $holder, AttCertIssuer $issuer, |
|
99 | - AttCertValidityPeriod $validity, Attributes $attribs) |
|
100 | - { |
|
101 | - $this->_version = self::VERSION_2; |
|
102 | - $this->_holder = $holder; |
|
103 | - $this->_issuer = $issuer; |
|
104 | - $this->_attrCertValidityPeriod = $validity; |
|
105 | - $this->_attributes = $attribs; |
|
106 | - $this->_extensions = new Extensions(); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Initialize from ASN.1. |
|
111 | - * |
|
112 | - * @param Sequence $seq |
|
113 | - * |
|
114 | - * @throws \UnexpectedValueException |
|
115 | - * |
|
116 | - * @return self |
|
117 | - */ |
|
118 | - public static function fromASN1(Sequence $seq): self |
|
119 | - { |
|
120 | - $idx = 0; |
|
121 | - $version = $seq->at($idx++)->asInteger()->intNumber(); |
|
122 | - if (self::VERSION_2 !== $version) { |
|
123 | - throw new \UnexpectedValueException('Version must be 2.'); |
|
124 | - } |
|
125 | - $holder = Holder::fromASN1($seq->at($idx++)->asSequence()); |
|
126 | - $issuer = AttCertIssuer::fromASN1($seq->at($idx++)); |
|
127 | - $signature = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
128 | - if (!$signature instanceof SignatureAlgorithmIdentifier) { |
|
129 | - throw new \UnexpectedValueException( |
|
130 | - 'Unsupported signature algorithm ' . $signature->oid() . '.'); |
|
131 | - } |
|
132 | - $serial = $seq->at($idx++)->asInteger()->number(); |
|
133 | - $validity = AttCertValidityPeriod::fromASN1($seq->at($idx++)->asSequence()); |
|
134 | - $attribs = Attributes::fromASN1($seq->at($idx++)->asSequence()); |
|
135 | - $obj = new self($holder, $issuer, $validity, $attribs); |
|
136 | - $obj->_signature = $signature; |
|
137 | - $obj->_serialNumber = $serial; |
|
138 | - if ($seq->has($idx, Element::TYPE_BIT_STRING)) { |
|
139 | - $obj->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
140 | - $seq->at($idx++)->asBitString()); |
|
141 | - } |
|
142 | - if ($seq->has($idx, Element::TYPE_SEQUENCE)) { |
|
143 | - $obj->_extensions = Extensions::fromASN1( |
|
144 | - $seq->at($idx++)->asSequence()); |
|
145 | - } |
|
146 | - return $obj; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Get self with holder. |
|
151 | - * |
|
152 | - * @param Holder $holder |
|
153 | - * |
|
154 | - * @return self |
|
155 | - */ |
|
156 | - public function withHolder(Holder $holder): self |
|
157 | - { |
|
158 | - $obj = clone $this; |
|
159 | - $obj->_holder = $holder; |
|
160 | - return $obj; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Get self with issuer. |
|
165 | - * |
|
166 | - * @param AttCertIssuer $issuer |
|
167 | - * |
|
168 | - * @return self |
|
169 | - */ |
|
170 | - public function withIssuer(AttCertIssuer $issuer): self |
|
171 | - { |
|
172 | - $obj = clone $this; |
|
173 | - $obj->_issuer = $issuer; |
|
174 | - return $obj; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Get self with signature algorithm identifier. |
|
179 | - * |
|
180 | - * @param SignatureAlgorithmIdentifier $algo |
|
181 | - * |
|
182 | - * @return self |
|
183 | - */ |
|
184 | - public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
185 | - { |
|
186 | - $obj = clone $this; |
|
187 | - $obj->_signature = $algo; |
|
188 | - return $obj; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Get self with serial number. |
|
193 | - * |
|
194 | - * @param int|string $serial Base 10 serial number |
|
195 | - * |
|
196 | - * @return self |
|
197 | - */ |
|
198 | - public function withSerialNumber($serial): self |
|
199 | - { |
|
200 | - $obj = clone $this; |
|
201 | - $obj->_serialNumber = strval($serial); |
|
202 | - return $obj; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Get self with random positive serial number. |
|
207 | - * |
|
208 | - * @param int $size Number of random bytes |
|
209 | - * |
|
210 | - * @return self |
|
211 | - */ |
|
212 | - public function withRandomSerialNumber(int $size = 16): self |
|
213 | - { |
|
214 | - // ensure that first byte is always non-zero and having first bit unset |
|
215 | - $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
216 | - for ($i = 1; $i < $size; ++$i) { |
|
217 | - $num <<= 8; |
|
218 | - $num += mt_rand(0, 0xff); |
|
219 | - } |
|
220 | - return $this->withSerialNumber(gmp_strval($num, 10)); |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Get self with validity period. |
|
225 | - * |
|
226 | - * @param AttCertValidityPeriod $validity |
|
227 | - * |
|
228 | - * @return self |
|
229 | - */ |
|
230 | - public function withValidity(AttCertValidityPeriod $validity): self |
|
231 | - { |
|
232 | - $obj = clone $this; |
|
233 | - $obj->_attrCertValidityPeriod = $validity; |
|
234 | - return $obj; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Get self with attributes. |
|
239 | - * |
|
240 | - * @param Attributes $attribs |
|
241 | - * |
|
242 | - * @return self |
|
243 | - */ |
|
244 | - public function withAttributes(Attributes $attribs): self |
|
245 | - { |
|
246 | - $obj = clone $this; |
|
247 | - $obj->_attributes = $attribs; |
|
248 | - return $obj; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Get self with issuer unique identifier. |
|
253 | - * |
|
254 | - * @param UniqueIdentifier $uid |
|
255 | - * |
|
256 | - * @return self |
|
257 | - */ |
|
258 | - public function withIssuerUniqueID(UniqueIdentifier $uid): self |
|
259 | - { |
|
260 | - $obj = clone $this; |
|
261 | - $obj->_issuerUniqueID = $uid; |
|
262 | - return $obj; |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Get self with extensions. |
|
267 | - * |
|
268 | - * @param Extensions $extensions |
|
269 | - * |
|
270 | - * @return self |
|
271 | - */ |
|
272 | - public function withExtensions(Extensions $extensions): self |
|
273 | - { |
|
274 | - $obj = clone $this; |
|
275 | - $obj->_extensions = $extensions; |
|
276 | - return $obj; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Get self with extensions added. |
|
281 | - * |
|
282 | - * @param Extension ...$exts One or more Extension objects |
|
283 | - * |
|
284 | - * @return self |
|
285 | - */ |
|
286 | - public function withAdditionalExtensions(Extension ...$exts): self |
|
287 | - { |
|
288 | - $obj = clone $this; |
|
289 | - $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
290 | - return $obj; |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * Get version. |
|
295 | - * |
|
296 | - * @return int |
|
297 | - */ |
|
298 | - public function version(): int |
|
299 | - { |
|
300 | - return $this->_version; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Get AC holder. |
|
305 | - * |
|
306 | - * @return Holder |
|
307 | - */ |
|
308 | - public function holder(): Holder |
|
309 | - { |
|
310 | - return $this->_holder; |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * Get AC issuer. |
|
315 | - * |
|
316 | - * @return AttCertIssuer |
|
317 | - */ |
|
318 | - public function issuer(): AttCertIssuer |
|
319 | - { |
|
320 | - return $this->_issuer; |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * Check whether signature is set. |
|
325 | - * |
|
326 | - * @return bool |
|
327 | - */ |
|
328 | - public function hasSignature(): bool |
|
329 | - { |
|
330 | - return isset($this->_signature); |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Get signature algorithm identifier. |
|
335 | - * |
|
336 | - * @throws \LogicException If not set |
|
337 | - * |
|
338 | - * @return SignatureAlgorithmIdentifier |
|
339 | - */ |
|
340 | - public function signature(): SignatureAlgorithmIdentifier |
|
341 | - { |
|
342 | - if (!$this->hasSignature()) { |
|
343 | - throw new \LogicException('signature not set.'); |
|
344 | - } |
|
345 | - return $this->_signature; |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * Check whether serial number is present. |
|
350 | - * |
|
351 | - * @return bool |
|
352 | - */ |
|
353 | - public function hasSerialNumber(): bool |
|
354 | - { |
|
355 | - return isset($this->_serialNumber); |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Get AC serial number as a base 10 integer. |
|
360 | - * |
|
361 | - * @throws \LogicException If not set |
|
362 | - * |
|
363 | - * @return string |
|
364 | - */ |
|
365 | - public function serialNumber(): string |
|
366 | - { |
|
367 | - if (!$this->hasSerialNumber()) { |
|
368 | - throw new \LogicException('serialNumber not set.'); |
|
369 | - } |
|
370 | - return $this->_serialNumber; |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * Get validity period. |
|
375 | - * |
|
376 | - * @return AttCertValidityPeriod |
|
377 | - */ |
|
378 | - public function validityPeriod(): AttCertValidityPeriod |
|
379 | - { |
|
380 | - return $this->_attrCertValidityPeriod; |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * Get attributes. |
|
385 | - * |
|
386 | - * @return Attributes |
|
387 | - */ |
|
388 | - public function attributes(): Attributes |
|
389 | - { |
|
390 | - return $this->_attributes; |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Check whether issuer unique identifier is present. |
|
395 | - * |
|
396 | - * @return bool |
|
397 | - */ |
|
398 | - public function hasIssuerUniqueID(): bool |
|
399 | - { |
|
400 | - return isset($this->_issuerUniqueID); |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * Get issuer unique identifier. |
|
405 | - * |
|
406 | - * @throws \LogicException If not set |
|
407 | - * |
|
408 | - * @return UniqueIdentifier |
|
409 | - */ |
|
410 | - public function issuerUniqueID(): UniqueIdentifier |
|
411 | - { |
|
412 | - if (!$this->hasIssuerUniqueID()) { |
|
413 | - throw new \LogicException('issuerUniqueID not set.'); |
|
414 | - } |
|
415 | - return $this->_issuerUniqueID; |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * Get extensions. |
|
420 | - * |
|
421 | - * @return Extensions |
|
422 | - */ |
|
423 | - public function extensions(): Extensions |
|
424 | - { |
|
425 | - return $this->_extensions; |
|
426 | - } |
|
427 | - |
|
428 | - /** |
|
429 | - * Get ASN.1 structure. |
|
430 | - * |
|
431 | - * @return Sequence |
|
432 | - */ |
|
433 | - public function toASN1(): Sequence |
|
434 | - { |
|
435 | - $elements = [new Integer($this->_version), $this->_holder->toASN1(), |
|
436 | - $this->_issuer->toASN1(), $this->signature()->toASN1(), |
|
437 | - new Integer($this->serialNumber()), |
|
438 | - $this->_attrCertValidityPeriod->toASN1(), |
|
439 | - $this->_attributes->toASN1(), ]; |
|
440 | - if (isset($this->_issuerUniqueID)) { |
|
441 | - $elements[] = $this->_issuerUniqueID->toASN1(); |
|
442 | - } |
|
443 | - if (count($this->_extensions)) { |
|
444 | - $elements[] = $this->_extensions->toASN1(); |
|
445 | - } |
|
446 | - return new Sequence(...$elements); |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * Create signed attribute certificate. |
|
451 | - * |
|
452 | - * @param SignatureAlgorithmIdentifier $algo Signature algorithm |
|
453 | - * @param PrivateKeyInfo $privkey_info Private key |
|
454 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
455 | - * |
|
456 | - * @return AttributeCertificate |
|
457 | - */ |
|
458 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
459 | - PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): AttributeCertificate |
|
460 | - { |
|
461 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
462 | - $aci = clone $this; |
|
463 | - if (!isset($aci->_serialNumber)) { |
|
464 | - $aci->_serialNumber = '0'; |
|
465 | - } |
|
466 | - $aci->_signature = $algo; |
|
467 | - $data = $aci->toASN1()->toDER(); |
|
468 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
469 | - return new AttributeCertificate($aci, $algo, $signature); |
|
470 | - } |
|
25 | + const VERSION_2 = 1; |
|
26 | + |
|
27 | + /** |
|
28 | + * AC version. |
|
29 | + * |
|
30 | + * @var int |
|
31 | + */ |
|
32 | + protected $_version; |
|
33 | + |
|
34 | + /** |
|
35 | + * AC holder. |
|
36 | + * |
|
37 | + * @var Holder |
|
38 | + */ |
|
39 | + protected $_holder; |
|
40 | + |
|
41 | + /** |
|
42 | + * AC issuer. |
|
43 | + * |
|
44 | + * @var AttCertIssuer |
|
45 | + */ |
|
46 | + protected $_issuer; |
|
47 | + |
|
48 | + /** |
|
49 | + * Signature algorithm identifier. |
|
50 | + * |
|
51 | + * @var SignatureAlgorithmIdentifier |
|
52 | + */ |
|
53 | + protected $_signature; |
|
54 | + |
|
55 | + /** |
|
56 | + * AC serial number as a base 10 integer. |
|
57 | + * |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + protected $_serialNumber; |
|
61 | + |
|
62 | + /** |
|
63 | + * Validity period. |
|
64 | + * |
|
65 | + * @var AttCertValidityPeriod |
|
66 | + */ |
|
67 | + protected $_attrCertValidityPeriod; |
|
68 | + |
|
69 | + /** |
|
70 | + * Attributes. |
|
71 | + * |
|
72 | + * @var Attributes |
|
73 | + */ |
|
74 | + protected $_attributes; |
|
75 | + |
|
76 | + /** |
|
77 | + * Issuer unique identifier. |
|
78 | + * |
|
79 | + * @var null|UniqueIdentifier |
|
80 | + */ |
|
81 | + protected $_issuerUniqueID; |
|
82 | + |
|
83 | + /** |
|
84 | + * Extensions. |
|
85 | + * |
|
86 | + * @var Extensions |
|
87 | + */ |
|
88 | + protected $_extensions; |
|
89 | + |
|
90 | + /** |
|
91 | + * Constructor. |
|
92 | + * |
|
93 | + * @param Holder $holder AC holder |
|
94 | + * @param AttCertIssuer $issuer AC issuer |
|
95 | + * @param AttCertValidityPeriod $validity Validity |
|
96 | + * @param Attributes $attribs Attributes |
|
97 | + */ |
|
98 | + public function __construct(Holder $holder, AttCertIssuer $issuer, |
|
99 | + AttCertValidityPeriod $validity, Attributes $attribs) |
|
100 | + { |
|
101 | + $this->_version = self::VERSION_2; |
|
102 | + $this->_holder = $holder; |
|
103 | + $this->_issuer = $issuer; |
|
104 | + $this->_attrCertValidityPeriod = $validity; |
|
105 | + $this->_attributes = $attribs; |
|
106 | + $this->_extensions = new Extensions(); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Initialize from ASN.1. |
|
111 | + * |
|
112 | + * @param Sequence $seq |
|
113 | + * |
|
114 | + * @throws \UnexpectedValueException |
|
115 | + * |
|
116 | + * @return self |
|
117 | + */ |
|
118 | + public static function fromASN1(Sequence $seq): self |
|
119 | + { |
|
120 | + $idx = 0; |
|
121 | + $version = $seq->at($idx++)->asInteger()->intNumber(); |
|
122 | + if (self::VERSION_2 !== $version) { |
|
123 | + throw new \UnexpectedValueException('Version must be 2.'); |
|
124 | + } |
|
125 | + $holder = Holder::fromASN1($seq->at($idx++)->asSequence()); |
|
126 | + $issuer = AttCertIssuer::fromASN1($seq->at($idx++)); |
|
127 | + $signature = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
128 | + if (!$signature instanceof SignatureAlgorithmIdentifier) { |
|
129 | + throw new \UnexpectedValueException( |
|
130 | + 'Unsupported signature algorithm ' . $signature->oid() . '.'); |
|
131 | + } |
|
132 | + $serial = $seq->at($idx++)->asInteger()->number(); |
|
133 | + $validity = AttCertValidityPeriod::fromASN1($seq->at($idx++)->asSequence()); |
|
134 | + $attribs = Attributes::fromASN1($seq->at($idx++)->asSequence()); |
|
135 | + $obj = new self($holder, $issuer, $validity, $attribs); |
|
136 | + $obj->_signature = $signature; |
|
137 | + $obj->_serialNumber = $serial; |
|
138 | + if ($seq->has($idx, Element::TYPE_BIT_STRING)) { |
|
139 | + $obj->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
140 | + $seq->at($idx++)->asBitString()); |
|
141 | + } |
|
142 | + if ($seq->has($idx, Element::TYPE_SEQUENCE)) { |
|
143 | + $obj->_extensions = Extensions::fromASN1( |
|
144 | + $seq->at($idx++)->asSequence()); |
|
145 | + } |
|
146 | + return $obj; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Get self with holder. |
|
151 | + * |
|
152 | + * @param Holder $holder |
|
153 | + * |
|
154 | + * @return self |
|
155 | + */ |
|
156 | + public function withHolder(Holder $holder): self |
|
157 | + { |
|
158 | + $obj = clone $this; |
|
159 | + $obj->_holder = $holder; |
|
160 | + return $obj; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Get self with issuer. |
|
165 | + * |
|
166 | + * @param AttCertIssuer $issuer |
|
167 | + * |
|
168 | + * @return self |
|
169 | + */ |
|
170 | + public function withIssuer(AttCertIssuer $issuer): self |
|
171 | + { |
|
172 | + $obj = clone $this; |
|
173 | + $obj->_issuer = $issuer; |
|
174 | + return $obj; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Get self with signature algorithm identifier. |
|
179 | + * |
|
180 | + * @param SignatureAlgorithmIdentifier $algo |
|
181 | + * |
|
182 | + * @return self |
|
183 | + */ |
|
184 | + public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
185 | + { |
|
186 | + $obj = clone $this; |
|
187 | + $obj->_signature = $algo; |
|
188 | + return $obj; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Get self with serial number. |
|
193 | + * |
|
194 | + * @param int|string $serial Base 10 serial number |
|
195 | + * |
|
196 | + * @return self |
|
197 | + */ |
|
198 | + public function withSerialNumber($serial): self |
|
199 | + { |
|
200 | + $obj = clone $this; |
|
201 | + $obj->_serialNumber = strval($serial); |
|
202 | + return $obj; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Get self with random positive serial number. |
|
207 | + * |
|
208 | + * @param int $size Number of random bytes |
|
209 | + * |
|
210 | + * @return self |
|
211 | + */ |
|
212 | + public function withRandomSerialNumber(int $size = 16): self |
|
213 | + { |
|
214 | + // ensure that first byte is always non-zero and having first bit unset |
|
215 | + $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
216 | + for ($i = 1; $i < $size; ++$i) { |
|
217 | + $num <<= 8; |
|
218 | + $num += mt_rand(0, 0xff); |
|
219 | + } |
|
220 | + return $this->withSerialNumber(gmp_strval($num, 10)); |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Get self with validity period. |
|
225 | + * |
|
226 | + * @param AttCertValidityPeriod $validity |
|
227 | + * |
|
228 | + * @return self |
|
229 | + */ |
|
230 | + public function withValidity(AttCertValidityPeriod $validity): self |
|
231 | + { |
|
232 | + $obj = clone $this; |
|
233 | + $obj->_attrCertValidityPeriod = $validity; |
|
234 | + return $obj; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Get self with attributes. |
|
239 | + * |
|
240 | + * @param Attributes $attribs |
|
241 | + * |
|
242 | + * @return self |
|
243 | + */ |
|
244 | + public function withAttributes(Attributes $attribs): self |
|
245 | + { |
|
246 | + $obj = clone $this; |
|
247 | + $obj->_attributes = $attribs; |
|
248 | + return $obj; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Get self with issuer unique identifier. |
|
253 | + * |
|
254 | + * @param UniqueIdentifier $uid |
|
255 | + * |
|
256 | + * @return self |
|
257 | + */ |
|
258 | + public function withIssuerUniqueID(UniqueIdentifier $uid): self |
|
259 | + { |
|
260 | + $obj = clone $this; |
|
261 | + $obj->_issuerUniqueID = $uid; |
|
262 | + return $obj; |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Get self with extensions. |
|
267 | + * |
|
268 | + * @param Extensions $extensions |
|
269 | + * |
|
270 | + * @return self |
|
271 | + */ |
|
272 | + public function withExtensions(Extensions $extensions): self |
|
273 | + { |
|
274 | + $obj = clone $this; |
|
275 | + $obj->_extensions = $extensions; |
|
276 | + return $obj; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Get self with extensions added. |
|
281 | + * |
|
282 | + * @param Extension ...$exts One or more Extension objects |
|
283 | + * |
|
284 | + * @return self |
|
285 | + */ |
|
286 | + public function withAdditionalExtensions(Extension ...$exts): self |
|
287 | + { |
|
288 | + $obj = clone $this; |
|
289 | + $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
290 | + return $obj; |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * Get version. |
|
295 | + * |
|
296 | + * @return int |
|
297 | + */ |
|
298 | + public function version(): int |
|
299 | + { |
|
300 | + return $this->_version; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Get AC holder. |
|
305 | + * |
|
306 | + * @return Holder |
|
307 | + */ |
|
308 | + public function holder(): Holder |
|
309 | + { |
|
310 | + return $this->_holder; |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * Get AC issuer. |
|
315 | + * |
|
316 | + * @return AttCertIssuer |
|
317 | + */ |
|
318 | + public function issuer(): AttCertIssuer |
|
319 | + { |
|
320 | + return $this->_issuer; |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * Check whether signature is set. |
|
325 | + * |
|
326 | + * @return bool |
|
327 | + */ |
|
328 | + public function hasSignature(): bool |
|
329 | + { |
|
330 | + return isset($this->_signature); |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Get signature algorithm identifier. |
|
335 | + * |
|
336 | + * @throws \LogicException If not set |
|
337 | + * |
|
338 | + * @return SignatureAlgorithmIdentifier |
|
339 | + */ |
|
340 | + public function signature(): SignatureAlgorithmIdentifier |
|
341 | + { |
|
342 | + if (!$this->hasSignature()) { |
|
343 | + throw new \LogicException('signature not set.'); |
|
344 | + } |
|
345 | + return $this->_signature; |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * Check whether serial number is present. |
|
350 | + * |
|
351 | + * @return bool |
|
352 | + */ |
|
353 | + public function hasSerialNumber(): bool |
|
354 | + { |
|
355 | + return isset($this->_serialNumber); |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Get AC serial number as a base 10 integer. |
|
360 | + * |
|
361 | + * @throws \LogicException If not set |
|
362 | + * |
|
363 | + * @return string |
|
364 | + */ |
|
365 | + public function serialNumber(): string |
|
366 | + { |
|
367 | + if (!$this->hasSerialNumber()) { |
|
368 | + throw new \LogicException('serialNumber not set.'); |
|
369 | + } |
|
370 | + return $this->_serialNumber; |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * Get validity period. |
|
375 | + * |
|
376 | + * @return AttCertValidityPeriod |
|
377 | + */ |
|
378 | + public function validityPeriod(): AttCertValidityPeriod |
|
379 | + { |
|
380 | + return $this->_attrCertValidityPeriod; |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * Get attributes. |
|
385 | + * |
|
386 | + * @return Attributes |
|
387 | + */ |
|
388 | + public function attributes(): Attributes |
|
389 | + { |
|
390 | + return $this->_attributes; |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Check whether issuer unique identifier is present. |
|
395 | + * |
|
396 | + * @return bool |
|
397 | + */ |
|
398 | + public function hasIssuerUniqueID(): bool |
|
399 | + { |
|
400 | + return isset($this->_issuerUniqueID); |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * Get issuer unique identifier. |
|
405 | + * |
|
406 | + * @throws \LogicException If not set |
|
407 | + * |
|
408 | + * @return UniqueIdentifier |
|
409 | + */ |
|
410 | + public function issuerUniqueID(): UniqueIdentifier |
|
411 | + { |
|
412 | + if (!$this->hasIssuerUniqueID()) { |
|
413 | + throw new \LogicException('issuerUniqueID not set.'); |
|
414 | + } |
|
415 | + return $this->_issuerUniqueID; |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * Get extensions. |
|
420 | + * |
|
421 | + * @return Extensions |
|
422 | + */ |
|
423 | + public function extensions(): Extensions |
|
424 | + { |
|
425 | + return $this->_extensions; |
|
426 | + } |
|
427 | + |
|
428 | + /** |
|
429 | + * Get ASN.1 structure. |
|
430 | + * |
|
431 | + * @return Sequence |
|
432 | + */ |
|
433 | + public function toASN1(): Sequence |
|
434 | + { |
|
435 | + $elements = [new Integer($this->_version), $this->_holder->toASN1(), |
|
436 | + $this->_issuer->toASN1(), $this->signature()->toASN1(), |
|
437 | + new Integer($this->serialNumber()), |
|
438 | + $this->_attrCertValidityPeriod->toASN1(), |
|
439 | + $this->_attributes->toASN1(), ]; |
|
440 | + if (isset($this->_issuerUniqueID)) { |
|
441 | + $elements[] = $this->_issuerUniqueID->toASN1(); |
|
442 | + } |
|
443 | + if (count($this->_extensions)) { |
|
444 | + $elements[] = $this->_extensions->toASN1(); |
|
445 | + } |
|
446 | + return new Sequence(...$elements); |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * Create signed attribute certificate. |
|
451 | + * |
|
452 | + * @param SignatureAlgorithmIdentifier $algo Signature algorithm |
|
453 | + * @param PrivateKeyInfo $privkey_info Private key |
|
454 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
455 | + * |
|
456 | + * @return AttributeCertificate |
|
457 | + */ |
|
458 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
459 | + PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): AttributeCertificate |
|
460 | + { |
|
461 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
462 | + $aci = clone $this; |
|
463 | + if (!isset($aci->_serialNumber)) { |
|
464 | + $aci->_serialNumber = '0'; |
|
465 | + } |
|
466 | + $aci->_signature = $algo; |
|
467 | + $data = $aci->toASN1()->toDER(); |
|
468 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
469 | + return new AttributeCertificate($aci, $algo, $signature); |
|
470 | + } |
|
471 | 471 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate; |
6 | 6 |
@@ -21,208 +21,208 @@ |
||
21 | 21 | */ |
22 | 22 | class AttributeCertificate |
23 | 23 | { |
24 | - /** |
|
25 | - * Attribute certificate info. |
|
26 | - * |
|
27 | - * @var AttributeCertificateInfo |
|
28 | - */ |
|
29 | - protected $_acinfo; |
|
30 | - |
|
31 | - /** |
|
32 | - * Signature algorithm identifier. |
|
33 | - * |
|
34 | - * @var SignatureAlgorithmIdentifier |
|
35 | - */ |
|
36 | - protected $_signatureAlgorithm; |
|
37 | - |
|
38 | - /** |
|
39 | - * Signature value. |
|
40 | - * |
|
41 | - * @var Signature |
|
42 | - */ |
|
43 | - protected $_signatureValue; |
|
44 | - |
|
45 | - /** |
|
46 | - * Constructor. |
|
47 | - * |
|
48 | - * @param AttributeCertificateInfo $acinfo |
|
49 | - * @param SignatureAlgorithmIdentifier $algo |
|
50 | - * @param Signature $signature |
|
51 | - */ |
|
52 | - public function __construct(AttributeCertificateInfo $acinfo, |
|
53 | - SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
54 | - { |
|
55 | - $this->_acinfo = $acinfo; |
|
56 | - $this->_signatureAlgorithm = $algo; |
|
57 | - $this->_signatureValue = $signature; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Get attribute certificate as a PEM formatted string. |
|
62 | - * |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - public function __toString(): string |
|
66 | - { |
|
67 | - return $this->toPEM()->string(); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Initialize from ASN.1. |
|
72 | - * |
|
73 | - * @param Sequence $seq |
|
74 | - * |
|
75 | - * @return self |
|
76 | - */ |
|
77 | - public static function fromASN1(Sequence $seq): self |
|
78 | - { |
|
79 | - $acinfo = AttributeCertificateInfo::fromASN1($seq->at(0)->asSequence()); |
|
80 | - $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
81 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
82 | - throw new \UnexpectedValueException( |
|
83 | - 'Unsupported signature algorithm ' . $algo->oid() . '.'); |
|
84 | - } |
|
85 | - $signature = Signature::fromSignatureData( |
|
86 | - $seq->at(2)->asBitString()->string(), $algo); |
|
87 | - return new self($acinfo, $algo, $signature); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Initialize from DER data. |
|
92 | - * |
|
93 | - * @param string $data |
|
94 | - * |
|
95 | - * @return self |
|
96 | - */ |
|
97 | - public static function fromDER(string $data): self |
|
98 | - { |
|
99 | - return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Initialize from PEM. |
|
104 | - * |
|
105 | - * @param PEM $pem |
|
106 | - * |
|
107 | - * @throws \UnexpectedValueException |
|
108 | - * |
|
109 | - * @return self |
|
110 | - */ |
|
111 | - public static function fromPEM(PEM $pem): self |
|
112 | - { |
|
113 | - if (PEM::TYPE_ATTRIBUTE_CERTIFICATE !== $pem->type()) { |
|
114 | - throw new \UnexpectedValueException('Invalid PEM type.'); |
|
115 | - } |
|
116 | - return self::fromDER($pem->data()); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Get attribute certificate info. |
|
121 | - * |
|
122 | - * @return AttributeCertificateInfo |
|
123 | - */ |
|
124 | - public function acinfo(): AttributeCertificateInfo |
|
125 | - { |
|
126 | - return $this->_acinfo; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Get signature algorithm identifier. |
|
131 | - * |
|
132 | - * @return SignatureAlgorithmIdentifier |
|
133 | - */ |
|
134 | - public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
135 | - { |
|
136 | - return $this->_signatureAlgorithm; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Get signature value. |
|
141 | - * |
|
142 | - * @return Signature |
|
143 | - */ |
|
144 | - public function signatureValue(): Signature |
|
145 | - { |
|
146 | - return $this->_signatureValue; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Get ASN.1 structure. |
|
151 | - * |
|
152 | - * @return Sequence |
|
153 | - */ |
|
154 | - public function toASN1(): Sequence |
|
155 | - { |
|
156 | - return new Sequence($this->_acinfo->toASN1(), |
|
157 | - $this->_signatureAlgorithm->toASN1(), |
|
158 | - $this->_signatureValue->bitString()); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Get attribute certificate as a DER. |
|
163 | - * |
|
164 | - * @return string |
|
165 | - */ |
|
166 | - public function toDER(): string |
|
167 | - { |
|
168 | - return $this->toASN1()->toDER(); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Get attribute certificate as a PEM. |
|
173 | - * |
|
174 | - * @return PEM |
|
175 | - */ |
|
176 | - public function toPEM(): PEM |
|
177 | - { |
|
178 | - return new PEM(PEM::TYPE_ATTRIBUTE_CERTIFICATE, $this->toDER()); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Check whether attribute certificate is issued to the subject identified |
|
183 | - * by given public key certificate. |
|
184 | - * |
|
185 | - * @param Certificate $cert Certificate |
|
186 | - * |
|
187 | - * @return bool |
|
188 | - */ |
|
189 | - public function isHeldBy(Certificate $cert): bool |
|
190 | - { |
|
191 | - if (!$this->_acinfo->holder()->identifiesPKC($cert)) { |
|
192 | - return false; |
|
193 | - } |
|
194 | - return true; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Check whether attribute certificate is issued by given public key |
|
199 | - * certificate. |
|
200 | - * |
|
201 | - * @param Certificate $cert Certificate |
|
202 | - * |
|
203 | - * @return bool |
|
204 | - */ |
|
205 | - public function isIssuedBy(Certificate $cert): bool |
|
206 | - { |
|
207 | - if (!$this->_acinfo->issuer()->identifiesPKC($cert)) { |
|
208 | - return false; |
|
209 | - } |
|
210 | - return true; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Verify signature. |
|
215 | - * |
|
216 | - * @param PublicKeyInfo $pubkey_info Signer's public key |
|
217 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
218 | - * |
|
219 | - * @return bool |
|
220 | - */ |
|
221 | - public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool |
|
222 | - { |
|
223 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
224 | - $data = $this->_acinfo->toASN1()->toDER(); |
|
225 | - return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
226 | - $this->_signatureAlgorithm); |
|
227 | - } |
|
24 | + /** |
|
25 | + * Attribute certificate info. |
|
26 | + * |
|
27 | + * @var AttributeCertificateInfo |
|
28 | + */ |
|
29 | + protected $_acinfo; |
|
30 | + |
|
31 | + /** |
|
32 | + * Signature algorithm identifier. |
|
33 | + * |
|
34 | + * @var SignatureAlgorithmIdentifier |
|
35 | + */ |
|
36 | + protected $_signatureAlgorithm; |
|
37 | + |
|
38 | + /** |
|
39 | + * Signature value. |
|
40 | + * |
|
41 | + * @var Signature |
|
42 | + */ |
|
43 | + protected $_signatureValue; |
|
44 | + |
|
45 | + /** |
|
46 | + * Constructor. |
|
47 | + * |
|
48 | + * @param AttributeCertificateInfo $acinfo |
|
49 | + * @param SignatureAlgorithmIdentifier $algo |
|
50 | + * @param Signature $signature |
|
51 | + */ |
|
52 | + public function __construct(AttributeCertificateInfo $acinfo, |
|
53 | + SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
54 | + { |
|
55 | + $this->_acinfo = $acinfo; |
|
56 | + $this->_signatureAlgorithm = $algo; |
|
57 | + $this->_signatureValue = $signature; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Get attribute certificate as a PEM formatted string. |
|
62 | + * |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + public function __toString(): string |
|
66 | + { |
|
67 | + return $this->toPEM()->string(); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Initialize from ASN.1. |
|
72 | + * |
|
73 | + * @param Sequence $seq |
|
74 | + * |
|
75 | + * @return self |
|
76 | + */ |
|
77 | + public static function fromASN1(Sequence $seq): self |
|
78 | + { |
|
79 | + $acinfo = AttributeCertificateInfo::fromASN1($seq->at(0)->asSequence()); |
|
80 | + $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
81 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
82 | + throw new \UnexpectedValueException( |
|
83 | + 'Unsupported signature algorithm ' . $algo->oid() . '.'); |
|
84 | + } |
|
85 | + $signature = Signature::fromSignatureData( |
|
86 | + $seq->at(2)->asBitString()->string(), $algo); |
|
87 | + return new self($acinfo, $algo, $signature); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Initialize from DER data. |
|
92 | + * |
|
93 | + * @param string $data |
|
94 | + * |
|
95 | + * @return self |
|
96 | + */ |
|
97 | + public static function fromDER(string $data): self |
|
98 | + { |
|
99 | + return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Initialize from PEM. |
|
104 | + * |
|
105 | + * @param PEM $pem |
|
106 | + * |
|
107 | + * @throws \UnexpectedValueException |
|
108 | + * |
|
109 | + * @return self |
|
110 | + */ |
|
111 | + public static function fromPEM(PEM $pem): self |
|
112 | + { |
|
113 | + if (PEM::TYPE_ATTRIBUTE_CERTIFICATE !== $pem->type()) { |
|
114 | + throw new \UnexpectedValueException('Invalid PEM type.'); |
|
115 | + } |
|
116 | + return self::fromDER($pem->data()); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Get attribute certificate info. |
|
121 | + * |
|
122 | + * @return AttributeCertificateInfo |
|
123 | + */ |
|
124 | + public function acinfo(): AttributeCertificateInfo |
|
125 | + { |
|
126 | + return $this->_acinfo; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Get signature algorithm identifier. |
|
131 | + * |
|
132 | + * @return SignatureAlgorithmIdentifier |
|
133 | + */ |
|
134 | + public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
135 | + { |
|
136 | + return $this->_signatureAlgorithm; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Get signature value. |
|
141 | + * |
|
142 | + * @return Signature |
|
143 | + */ |
|
144 | + public function signatureValue(): Signature |
|
145 | + { |
|
146 | + return $this->_signatureValue; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Get ASN.1 structure. |
|
151 | + * |
|
152 | + * @return Sequence |
|
153 | + */ |
|
154 | + public function toASN1(): Sequence |
|
155 | + { |
|
156 | + return new Sequence($this->_acinfo->toASN1(), |
|
157 | + $this->_signatureAlgorithm->toASN1(), |
|
158 | + $this->_signatureValue->bitString()); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Get attribute certificate as a DER. |
|
163 | + * |
|
164 | + * @return string |
|
165 | + */ |
|
166 | + public function toDER(): string |
|
167 | + { |
|
168 | + return $this->toASN1()->toDER(); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Get attribute certificate as a PEM. |
|
173 | + * |
|
174 | + * @return PEM |
|
175 | + */ |
|
176 | + public function toPEM(): PEM |
|
177 | + { |
|
178 | + return new PEM(PEM::TYPE_ATTRIBUTE_CERTIFICATE, $this->toDER()); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Check whether attribute certificate is issued to the subject identified |
|
183 | + * by given public key certificate. |
|
184 | + * |
|
185 | + * @param Certificate $cert Certificate |
|
186 | + * |
|
187 | + * @return bool |
|
188 | + */ |
|
189 | + public function isHeldBy(Certificate $cert): bool |
|
190 | + { |
|
191 | + if (!$this->_acinfo->holder()->identifiesPKC($cert)) { |
|
192 | + return false; |
|
193 | + } |
|
194 | + return true; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Check whether attribute certificate is issued by given public key |
|
199 | + * certificate. |
|
200 | + * |
|
201 | + * @param Certificate $cert Certificate |
|
202 | + * |
|
203 | + * @return bool |
|
204 | + */ |
|
205 | + public function isIssuedBy(Certificate $cert): bool |
|
206 | + { |
|
207 | + if (!$this->_acinfo->issuer()->identifiesPKC($cert)) { |
|
208 | + return false; |
|
209 | + } |
|
210 | + return true; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Verify signature. |
|
215 | + * |
|
216 | + * @param PublicKeyInfo $pubkey_info Signer's public key |
|
217 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
218 | + * |
|
219 | + * @return bool |
|
220 | + */ |
|
221 | + public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool |
|
222 | + { |
|
223 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
224 | + $data = $this->_acinfo->toASN1()->toDER(); |
|
225 | + return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
226 | + $this->_signatureAlgorithm); |
|
227 | + } |
|
228 | 228 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate; |
6 | 6 |
@@ -19,173 +19,173 @@ |
||
19 | 19 | */ |
20 | 20 | class IssuerSerial |
21 | 21 | { |
22 | - /** |
|
23 | - * Issuer name. |
|
24 | - * |
|
25 | - * @var GeneralNames |
|
26 | - */ |
|
27 | - protected $_issuer; |
|
28 | - |
|
29 | - /** |
|
30 | - * Serial number as a base 10 integer. |
|
31 | - * |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - protected $_serial; |
|
35 | - |
|
36 | - /** |
|
37 | - * Issuer unique ID. |
|
38 | - * |
|
39 | - * @var null|UniqueIdentifier |
|
40 | - */ |
|
41 | - protected $_issuerUID; |
|
42 | - |
|
43 | - /** |
|
44 | - * Constructor. |
|
45 | - * |
|
46 | - * @param GeneralNames $issuer |
|
47 | - * @param int|string $serial |
|
48 | - * @param null|UniqueIdentifier $uid |
|
49 | - */ |
|
50 | - public function __construct(GeneralNames $issuer, $serial, |
|
51 | - ?UniqueIdentifier $uid = null) |
|
52 | - { |
|
53 | - $this->_issuer = $issuer; |
|
54 | - $this->_serial = strval($serial); |
|
55 | - $this->_issuerUID = $uid; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Initialize from ASN.1. |
|
60 | - * |
|
61 | - * @param Sequence $seq |
|
62 | - * |
|
63 | - * @return self |
|
64 | - */ |
|
65 | - public static function fromASN1(Sequence $seq): IssuerSerial |
|
66 | - { |
|
67 | - $issuer = GeneralNames::fromASN1($seq->at(0)->asSequence()); |
|
68 | - $serial = $seq->at(1)->asInteger()->number(); |
|
69 | - $uid = null; |
|
70 | - if ($seq->has(2, Element::TYPE_BIT_STRING)) { |
|
71 | - $uid = UniqueIdentifier::fromASN1($seq->at(2)->asBitString()); |
|
72 | - } |
|
73 | - return new self($issuer, $serial, $uid); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Initialize from a public key certificate. |
|
78 | - * |
|
79 | - * @param Certificate $cert |
|
80 | - * |
|
81 | - * @return self |
|
82 | - */ |
|
83 | - public static function fromPKC(Certificate $cert): IssuerSerial |
|
84 | - { |
|
85 | - $tbsCert = $cert->tbsCertificate(); |
|
86 | - $issuer = new GeneralNames(new DirectoryName($tbsCert->issuer())); |
|
87 | - $serial = $tbsCert->serialNumber(); |
|
88 | - $uid = $tbsCert->hasIssuerUniqueID() ? $tbsCert->issuerUniqueID() : null; |
|
89 | - return new self($issuer, $serial, $uid); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Get issuer name. |
|
94 | - * |
|
95 | - * @return GeneralNames |
|
96 | - */ |
|
97 | - public function issuer(): GeneralNames |
|
98 | - { |
|
99 | - return $this->_issuer; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Get serial number. |
|
104 | - * |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - public function serial(): string |
|
108 | - { |
|
109 | - return $this->_serial; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Check whether issuer unique identifier is present. |
|
114 | - * |
|
115 | - * @return bool |
|
116 | - */ |
|
117 | - public function hasIssuerUID(): bool |
|
118 | - { |
|
119 | - return isset($this->_issuerUID); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Get issuer unique identifier. |
|
124 | - * |
|
125 | - * @throws \LogicException If not set |
|
126 | - * |
|
127 | - * @return UniqueIdentifier |
|
128 | - */ |
|
129 | - public function issuerUID(): UniqueIdentifier |
|
130 | - { |
|
131 | - if (!$this->hasIssuerUID()) { |
|
132 | - throw new \LogicException('issuerUID not set.'); |
|
133 | - } |
|
134 | - return $this->_issuerUID; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Generate ASN.1 structure. |
|
139 | - * |
|
140 | - * @return Sequence |
|
141 | - */ |
|
142 | - public function toASN1(): Sequence |
|
143 | - { |
|
144 | - $elements = [$this->_issuer->toASN1(), new Integer($this->_serial)]; |
|
145 | - if (isset($this->_issuerUID)) { |
|
146 | - $elements[] = $this->_issuerUID->toASN1(); |
|
147 | - } |
|
148 | - return new Sequence(...$elements); |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Check whether this IssuerSerial identifies given certificate. |
|
153 | - * |
|
154 | - * @param Certificate $cert |
|
155 | - * |
|
156 | - * @return bool |
|
157 | - */ |
|
158 | - public function identifiesPKC(Certificate $cert): bool |
|
159 | - { |
|
160 | - $tbs = $cert->tbsCertificate(); |
|
161 | - if (!$tbs->issuer()->equals($this->_issuer->firstDN())) { |
|
162 | - return false; |
|
163 | - } |
|
164 | - if ($tbs->serialNumber() !== $this->_serial) { |
|
165 | - return false; |
|
166 | - } |
|
167 | - if ($this->_issuerUID && !$this->_checkUniqueID($cert)) { |
|
168 | - return false; |
|
169 | - } |
|
170 | - return true; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Check whether issuerUID matches given certificate. |
|
175 | - * |
|
176 | - * @param Certificate $cert |
|
177 | - * |
|
178 | - * @return bool |
|
179 | - */ |
|
180 | - private function _checkUniqueID(Certificate $cert): bool |
|
181 | - { |
|
182 | - if (!$cert->tbsCertificate()->hasIssuerUniqueID()) { |
|
183 | - return false; |
|
184 | - } |
|
185 | - $uid = $cert->tbsCertificate()->issuerUniqueID()->string(); |
|
186 | - if ($this->_issuerUID->string() != $uid) { |
|
187 | - return false; |
|
188 | - } |
|
189 | - return true; |
|
190 | - } |
|
22 | + /** |
|
23 | + * Issuer name. |
|
24 | + * |
|
25 | + * @var GeneralNames |
|
26 | + */ |
|
27 | + protected $_issuer; |
|
28 | + |
|
29 | + /** |
|
30 | + * Serial number as a base 10 integer. |
|
31 | + * |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + protected $_serial; |
|
35 | + |
|
36 | + /** |
|
37 | + * Issuer unique ID. |
|
38 | + * |
|
39 | + * @var null|UniqueIdentifier |
|
40 | + */ |
|
41 | + protected $_issuerUID; |
|
42 | + |
|
43 | + /** |
|
44 | + * Constructor. |
|
45 | + * |
|
46 | + * @param GeneralNames $issuer |
|
47 | + * @param int|string $serial |
|
48 | + * @param null|UniqueIdentifier $uid |
|
49 | + */ |
|
50 | + public function __construct(GeneralNames $issuer, $serial, |
|
51 | + ?UniqueIdentifier $uid = null) |
|
52 | + { |
|
53 | + $this->_issuer = $issuer; |
|
54 | + $this->_serial = strval($serial); |
|
55 | + $this->_issuerUID = $uid; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Initialize from ASN.1. |
|
60 | + * |
|
61 | + * @param Sequence $seq |
|
62 | + * |
|
63 | + * @return self |
|
64 | + */ |
|
65 | + public static function fromASN1(Sequence $seq): IssuerSerial |
|
66 | + { |
|
67 | + $issuer = GeneralNames::fromASN1($seq->at(0)->asSequence()); |
|
68 | + $serial = $seq->at(1)->asInteger()->number(); |
|
69 | + $uid = null; |
|
70 | + if ($seq->has(2, Element::TYPE_BIT_STRING)) { |
|
71 | + $uid = UniqueIdentifier::fromASN1($seq->at(2)->asBitString()); |
|
72 | + } |
|
73 | + return new self($issuer, $serial, $uid); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Initialize from a public key certificate. |
|
78 | + * |
|
79 | + * @param Certificate $cert |
|
80 | + * |
|
81 | + * @return self |
|
82 | + */ |
|
83 | + public static function fromPKC(Certificate $cert): IssuerSerial |
|
84 | + { |
|
85 | + $tbsCert = $cert->tbsCertificate(); |
|
86 | + $issuer = new GeneralNames(new DirectoryName($tbsCert->issuer())); |
|
87 | + $serial = $tbsCert->serialNumber(); |
|
88 | + $uid = $tbsCert->hasIssuerUniqueID() ? $tbsCert->issuerUniqueID() : null; |
|
89 | + return new self($issuer, $serial, $uid); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Get issuer name. |
|
94 | + * |
|
95 | + * @return GeneralNames |
|
96 | + */ |
|
97 | + public function issuer(): GeneralNames |
|
98 | + { |
|
99 | + return $this->_issuer; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Get serial number. |
|
104 | + * |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + public function serial(): string |
|
108 | + { |
|
109 | + return $this->_serial; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Check whether issuer unique identifier is present. |
|
114 | + * |
|
115 | + * @return bool |
|
116 | + */ |
|
117 | + public function hasIssuerUID(): bool |
|
118 | + { |
|
119 | + return isset($this->_issuerUID); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Get issuer unique identifier. |
|
124 | + * |
|
125 | + * @throws \LogicException If not set |
|
126 | + * |
|
127 | + * @return UniqueIdentifier |
|
128 | + */ |
|
129 | + public function issuerUID(): UniqueIdentifier |
|
130 | + { |
|
131 | + if (!$this->hasIssuerUID()) { |
|
132 | + throw new \LogicException('issuerUID not set.'); |
|
133 | + } |
|
134 | + return $this->_issuerUID; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Generate ASN.1 structure. |
|
139 | + * |
|
140 | + * @return Sequence |
|
141 | + */ |
|
142 | + public function toASN1(): Sequence |
|
143 | + { |
|
144 | + $elements = [$this->_issuer->toASN1(), new Integer($this->_serial)]; |
|
145 | + if (isset($this->_issuerUID)) { |
|
146 | + $elements[] = $this->_issuerUID->toASN1(); |
|
147 | + } |
|
148 | + return new Sequence(...$elements); |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Check whether this IssuerSerial identifies given certificate. |
|
153 | + * |
|
154 | + * @param Certificate $cert |
|
155 | + * |
|
156 | + * @return bool |
|
157 | + */ |
|
158 | + public function identifiesPKC(Certificate $cert): bool |
|
159 | + { |
|
160 | + $tbs = $cert->tbsCertificate(); |
|
161 | + if (!$tbs->issuer()->equals($this->_issuer->firstDN())) { |
|
162 | + return false; |
|
163 | + } |
|
164 | + if ($tbs->serialNumber() !== $this->_serial) { |
|
165 | + return false; |
|
166 | + } |
|
167 | + if ($this->_issuerUID && !$this->_checkUniqueID($cert)) { |
|
168 | + return false; |
|
169 | + } |
|
170 | + return true; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Check whether issuerUID matches given certificate. |
|
175 | + * |
|
176 | + * @param Certificate $cert |
|
177 | + * |
|
178 | + * @return bool |
|
179 | + */ |
|
180 | + private function _checkUniqueID(Certificate $cert): bool |
|
181 | + { |
|
182 | + if (!$cert->tbsCertificate()->hasIssuerUniqueID()) { |
|
183 | + return false; |
|
184 | + } |
|
185 | + $uid = $cert->tbsCertificate()->issuerUniqueID()->string(); |
|
186 | + if ($this->_issuerUID->string() != $uid) { |
|
187 | + return false; |
|
188 | + } |
|
189 | + return true; |
|
190 | + } |
|
191 | 191 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\AttributeCertificate; |
6 | 6 |