@@ -18,190 +18,190 @@ |
||
18 | 18 | */ |
19 | 19 | class GeneralNames implements \Countable, \IteratorAggregate |
20 | 20 | { |
21 | - /** |
|
22 | - * GeneralName objects. |
|
23 | - * |
|
24 | - * @var GeneralName[] |
|
25 | - */ |
|
26 | - protected $_names; |
|
27 | - |
|
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param GeneralName ...$names One or more GeneralName objects |
|
32 | - */ |
|
33 | - public function __construct(GeneralName ...$names) |
|
34 | - { |
|
35 | - $this->_names = $names; |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * Initialize from ASN.1. |
|
40 | - * |
|
41 | - * @param Sequence $seq |
|
42 | - * |
|
43 | - * @throws \UnexpectedValueException |
|
44 | - * |
|
45 | - * @return self |
|
46 | - */ |
|
47 | - public static function fromASN1(Sequence $seq): GeneralNames |
|
48 | - { |
|
49 | - if (!count($seq)) { |
|
50 | - throw new \UnexpectedValueException( |
|
51 | - 'GeneralNames must have at least one GeneralName.'); |
|
52 | - } |
|
53 | - $names = array_map( |
|
54 | - function (UnspecifiedType $el) { |
|
55 | - return GeneralName::fromASN1($el->asTagged()); |
|
56 | - }, $seq->elements()); |
|
57 | - return new self(...$names); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Check whether GeneralNames contains a GeneralName of given type. |
|
62 | - * |
|
63 | - * @param int $tag One of `GeneralName::TAG_*` enumerations |
|
64 | - * |
|
65 | - * @return bool |
|
66 | - */ |
|
67 | - public function has(int $tag): bool |
|
68 | - { |
|
69 | - return null !== $this->_findFirst($tag); |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Get first GeneralName of given type. |
|
74 | - * |
|
75 | - * @param int $tag One of `GeneralName::TAG_*` enumerations |
|
76 | - * |
|
77 | - * @throws \OutOfBoundsException |
|
78 | - * |
|
79 | - * @return GeneralName |
|
80 | - */ |
|
81 | - public function firstOf(int $tag): GeneralName |
|
82 | - { |
|
83 | - $name = $this->_findFirst($tag); |
|
84 | - if (!$name) { |
|
85 | - throw new \UnexpectedValueException("No GeneralName by tag {$tag}."); |
|
86 | - } |
|
87 | - return $name; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Get all GeneralName objects of given type. |
|
92 | - * |
|
93 | - * @param int $tag One of `GeneralName::TAG_*` enumerations |
|
94 | - * |
|
95 | - * @return GeneralName[] |
|
96 | - */ |
|
97 | - public function allOf(int $tag): array |
|
98 | - { |
|
99 | - $names = array_filter($this->_names, |
|
100 | - function (GeneralName $name) use ($tag) { |
|
101 | - return $name->tag() === $tag; |
|
102 | - }); |
|
103 | - return array_values($names); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Get value of the first 'dNSName' type. |
|
108 | - * |
|
109 | - * @return string |
|
110 | - */ |
|
111 | - public function firstDNS(): string |
|
112 | - { |
|
113 | - $gn = $this->firstOf(GeneralName::TAG_DNS_NAME); |
|
114 | - if (!$gn instanceof DNSName) { |
|
115 | - throw new \RuntimeException( |
|
116 | - DNSName::class . ' expected, got ' . get_class($gn)); |
|
117 | - } |
|
118 | - return $gn->name(); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Get value of the first 'directoryName' type. |
|
123 | - * |
|
124 | - * @return Name |
|
125 | - */ |
|
126 | - public function firstDN(): Name |
|
127 | - { |
|
128 | - $gn = $this->firstOf(GeneralName::TAG_DIRECTORY_NAME); |
|
129 | - if (!$gn instanceof DirectoryName) { |
|
130 | - throw new \RuntimeException( |
|
131 | - DirectoryName::class . ' expected, got ' . get_class($gn)); |
|
132 | - } |
|
133 | - return $gn->dn(); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Get value of the first 'uniformResourceIdentifier' type. |
|
138 | - * |
|
139 | - * @return string |
|
140 | - */ |
|
141 | - public function firstURI(): string |
|
142 | - { |
|
143 | - $gn = $this->firstOf(GeneralName::TAG_URI); |
|
144 | - if (!$gn instanceof UniformResourceIdentifier) { |
|
145 | - throw new \RuntimeException( |
|
146 | - UniformResourceIdentifier::class . ' expected, got ' . get_class($gn)); |
|
147 | - } |
|
148 | - return $gn->uri(); |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Generate ASN.1 structure. |
|
153 | - * |
|
154 | - * @return Sequence |
|
155 | - */ |
|
156 | - public function toASN1(): Sequence |
|
157 | - { |
|
158 | - if (!count($this->_names)) { |
|
159 | - throw new \LogicException( |
|
160 | - 'GeneralNames must have at least one GeneralName.'); |
|
161 | - } |
|
162 | - $elements = array_map( |
|
163 | - function (GeneralName $name) { |
|
164 | - return $name->toASN1(); |
|
165 | - }, $this->_names); |
|
166 | - return new Sequence(...$elements); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @see \Countable::count() |
|
171 | - * |
|
172 | - * @return int |
|
173 | - */ |
|
174 | - public function count(): int |
|
175 | - { |
|
176 | - return count($this->_names); |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Get iterator for GeneralName objects. |
|
181 | - * |
|
182 | - * @see \IteratorAggregate::getIterator() |
|
183 | - * |
|
184 | - * @return \ArrayIterator |
|
185 | - */ |
|
186 | - public function getIterator(): \ArrayIterator |
|
187 | - { |
|
188 | - return new \ArrayIterator($this->_names); |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Find first GeneralName by given tag. |
|
193 | - * |
|
194 | - * @param int $tag |
|
195 | - * |
|
196 | - * @return null|GeneralName |
|
197 | - */ |
|
198 | - protected function _findFirst(int $tag): ?GeneralName |
|
199 | - { |
|
200 | - foreach ($this->_names as $name) { |
|
201 | - if ($name->tag() === $tag) { |
|
202 | - return $name; |
|
203 | - } |
|
204 | - } |
|
205 | - return null; |
|
206 | - } |
|
21 | + /** |
|
22 | + * GeneralName objects. |
|
23 | + * |
|
24 | + * @var GeneralName[] |
|
25 | + */ |
|
26 | + protected $_names; |
|
27 | + |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param GeneralName ...$names One or more GeneralName objects |
|
32 | + */ |
|
33 | + public function __construct(GeneralName ...$names) |
|
34 | + { |
|
35 | + $this->_names = $names; |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * Initialize from ASN.1. |
|
40 | + * |
|
41 | + * @param Sequence $seq |
|
42 | + * |
|
43 | + * @throws \UnexpectedValueException |
|
44 | + * |
|
45 | + * @return self |
|
46 | + */ |
|
47 | + public static function fromASN1(Sequence $seq): GeneralNames |
|
48 | + { |
|
49 | + if (!count($seq)) { |
|
50 | + throw new \UnexpectedValueException( |
|
51 | + 'GeneralNames must have at least one GeneralName.'); |
|
52 | + } |
|
53 | + $names = array_map( |
|
54 | + function (UnspecifiedType $el) { |
|
55 | + return GeneralName::fromASN1($el->asTagged()); |
|
56 | + }, $seq->elements()); |
|
57 | + return new self(...$names); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Check whether GeneralNames contains a GeneralName of given type. |
|
62 | + * |
|
63 | + * @param int $tag One of `GeneralName::TAG_*` enumerations |
|
64 | + * |
|
65 | + * @return bool |
|
66 | + */ |
|
67 | + public function has(int $tag): bool |
|
68 | + { |
|
69 | + return null !== $this->_findFirst($tag); |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Get first GeneralName of given type. |
|
74 | + * |
|
75 | + * @param int $tag One of `GeneralName::TAG_*` enumerations |
|
76 | + * |
|
77 | + * @throws \OutOfBoundsException |
|
78 | + * |
|
79 | + * @return GeneralName |
|
80 | + */ |
|
81 | + public function firstOf(int $tag): GeneralName |
|
82 | + { |
|
83 | + $name = $this->_findFirst($tag); |
|
84 | + if (!$name) { |
|
85 | + throw new \UnexpectedValueException("No GeneralName by tag {$tag}."); |
|
86 | + } |
|
87 | + return $name; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Get all GeneralName objects of given type. |
|
92 | + * |
|
93 | + * @param int $tag One of `GeneralName::TAG_*` enumerations |
|
94 | + * |
|
95 | + * @return GeneralName[] |
|
96 | + */ |
|
97 | + public function allOf(int $tag): array |
|
98 | + { |
|
99 | + $names = array_filter($this->_names, |
|
100 | + function (GeneralName $name) use ($tag) { |
|
101 | + return $name->tag() === $tag; |
|
102 | + }); |
|
103 | + return array_values($names); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Get value of the first 'dNSName' type. |
|
108 | + * |
|
109 | + * @return string |
|
110 | + */ |
|
111 | + public function firstDNS(): string |
|
112 | + { |
|
113 | + $gn = $this->firstOf(GeneralName::TAG_DNS_NAME); |
|
114 | + if (!$gn instanceof DNSName) { |
|
115 | + throw new \RuntimeException( |
|
116 | + DNSName::class . ' expected, got ' . get_class($gn)); |
|
117 | + } |
|
118 | + return $gn->name(); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Get value of the first 'directoryName' type. |
|
123 | + * |
|
124 | + * @return Name |
|
125 | + */ |
|
126 | + public function firstDN(): Name |
|
127 | + { |
|
128 | + $gn = $this->firstOf(GeneralName::TAG_DIRECTORY_NAME); |
|
129 | + if (!$gn instanceof DirectoryName) { |
|
130 | + throw new \RuntimeException( |
|
131 | + DirectoryName::class . ' expected, got ' . get_class($gn)); |
|
132 | + } |
|
133 | + return $gn->dn(); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Get value of the first 'uniformResourceIdentifier' type. |
|
138 | + * |
|
139 | + * @return string |
|
140 | + */ |
|
141 | + public function firstURI(): string |
|
142 | + { |
|
143 | + $gn = $this->firstOf(GeneralName::TAG_URI); |
|
144 | + if (!$gn instanceof UniformResourceIdentifier) { |
|
145 | + throw new \RuntimeException( |
|
146 | + UniformResourceIdentifier::class . ' expected, got ' . get_class($gn)); |
|
147 | + } |
|
148 | + return $gn->uri(); |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Generate ASN.1 structure. |
|
153 | + * |
|
154 | + * @return Sequence |
|
155 | + */ |
|
156 | + public function toASN1(): Sequence |
|
157 | + { |
|
158 | + if (!count($this->_names)) { |
|
159 | + throw new \LogicException( |
|
160 | + 'GeneralNames must have at least one GeneralName.'); |
|
161 | + } |
|
162 | + $elements = array_map( |
|
163 | + function (GeneralName $name) { |
|
164 | + return $name->toASN1(); |
|
165 | + }, $this->_names); |
|
166 | + return new Sequence(...$elements); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @see \Countable::count() |
|
171 | + * |
|
172 | + * @return int |
|
173 | + */ |
|
174 | + public function count(): int |
|
175 | + { |
|
176 | + return count($this->_names); |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Get iterator for GeneralName objects. |
|
181 | + * |
|
182 | + * @see \IteratorAggregate::getIterator() |
|
183 | + * |
|
184 | + * @return \ArrayIterator |
|
185 | + */ |
|
186 | + public function getIterator(): \ArrayIterator |
|
187 | + { |
|
188 | + return new \ArrayIterator($this->_names); |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Find first GeneralName by given tag. |
|
193 | + * |
|
194 | + * @param int $tag |
|
195 | + * |
|
196 | + * @return null|GeneralName |
|
197 | + */ |
|
198 | + protected function _findFirst(int $tag): ?GeneralName |
|
199 | + { |
|
200 | + foreach ($this->_names as $name) { |
|
201 | + if ($name->tag() === $tag) { |
|
202 | + return $name; |
|
203 | + } |
|
204 | + } |
|
205 | + return null; |
|
206 | + } |
|
207 | 207 | } |
@@ -24,206 +24,206 @@ |
||
24 | 24 | */ |
25 | 25 | class CertificationRequestInfo |
26 | 26 | { |
27 | - const VERSION_1 = 0; |
|
28 | - |
|
29 | - /** |
|
30 | - * Version. |
|
31 | - * |
|
32 | - * @var int |
|
33 | - */ |
|
34 | - protected $_version; |
|
35 | - |
|
36 | - /** |
|
37 | - * Subject. |
|
38 | - * |
|
39 | - * @var Name |
|
40 | - */ |
|
41 | - protected $_subject; |
|
42 | - |
|
43 | - /** |
|
44 | - * Public key info. |
|
45 | - * |
|
46 | - * @var PublicKeyInfo |
|
47 | - */ |
|
48 | - protected $_subjectPKInfo; |
|
49 | - |
|
50 | - /** |
|
51 | - * Attributes. |
|
52 | - * |
|
53 | - * @var null|Attributes |
|
54 | - */ |
|
55 | - protected $_attributes; |
|
56 | - |
|
57 | - /** |
|
58 | - * Constructor. |
|
59 | - * |
|
60 | - * @param Name $subject Subject |
|
61 | - * @param PublicKeyInfo $pkinfo Public key info |
|
62 | - */ |
|
63 | - public function __construct(Name $subject, PublicKeyInfo $pkinfo) |
|
64 | - { |
|
65 | - $this->_version = self::VERSION_1; |
|
66 | - $this->_subject = $subject; |
|
67 | - $this->_subjectPKInfo = $pkinfo; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Initialize from ASN.1. |
|
72 | - * |
|
73 | - * @param Sequence $seq |
|
74 | - * |
|
75 | - * @throws \UnexpectedValueException |
|
76 | - * |
|
77 | - * @return self |
|
78 | - */ |
|
79 | - public static function fromASN1(Sequence $seq): self |
|
80 | - { |
|
81 | - $version = $seq->at(0)->asInteger()->intNumber(); |
|
82 | - if (self::VERSION_1 !== $version) { |
|
83 | - throw new \UnexpectedValueException( |
|
84 | - "Version {$version} not supported."); |
|
85 | - } |
|
86 | - $subject = Name::fromASN1($seq->at(1)->asSequence()); |
|
87 | - $pkinfo = PublicKeyInfo::fromASN1($seq->at(2)->asSequence()); |
|
88 | - $obj = new self($subject, $pkinfo); |
|
89 | - if ($seq->hasTagged(0)) { |
|
90 | - $obj->_attributes = Attributes::fromASN1( |
|
91 | - $seq->getTagged(0)->asImplicit(Element::TYPE_SET)->asSet()); |
|
92 | - } |
|
93 | - return $obj; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Get version. |
|
98 | - * |
|
99 | - * @return int |
|
100 | - */ |
|
101 | - public function version(): int |
|
102 | - { |
|
103 | - return $this->_version; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Get self with subject. |
|
108 | - * |
|
109 | - * @param Name $subject |
|
110 | - * |
|
111 | - * @return self |
|
112 | - */ |
|
113 | - public function withSubject(Name $subject): self |
|
114 | - { |
|
115 | - $obj = clone $this; |
|
116 | - $obj->_subject = $subject; |
|
117 | - return $obj; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Get subject. |
|
122 | - * |
|
123 | - * @return Name |
|
124 | - */ |
|
125 | - public function subject(): Name |
|
126 | - { |
|
127 | - return $this->_subject; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Get subject public key info. |
|
132 | - * |
|
133 | - * @return PublicKeyInfo |
|
134 | - */ |
|
135 | - public function subjectPKInfo(): PublicKeyInfo |
|
136 | - { |
|
137 | - return $this->_subjectPKInfo; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Whether certification request info has attributes. |
|
142 | - * |
|
143 | - * @return bool |
|
144 | - */ |
|
145 | - public function hasAttributes(): bool |
|
146 | - { |
|
147 | - return isset($this->_attributes); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Get attributes. |
|
152 | - * |
|
153 | - * @throws \LogicException If not set |
|
154 | - * |
|
155 | - * @return Attributes |
|
156 | - */ |
|
157 | - public function attributes(): Attributes |
|
158 | - { |
|
159 | - if (!$this->hasAttributes()) { |
|
160 | - throw new \LogicException('No attributes.'); |
|
161 | - } |
|
162 | - return $this->_attributes; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Get instance of self with attributes. |
|
167 | - * |
|
168 | - * @param Attributes $attribs |
|
169 | - */ |
|
170 | - public function withAttributes(Attributes $attribs): self |
|
171 | - { |
|
172 | - $obj = clone $this; |
|
173 | - $obj->_attributes = $attribs; |
|
174 | - return $obj; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Get self with extension request attribute. |
|
179 | - * |
|
180 | - * @param Extensions $extensions Extensions to request |
|
181 | - * |
|
182 | - * @return self |
|
183 | - */ |
|
184 | - public function withExtensionRequest(Extensions $extensions): self |
|
185 | - { |
|
186 | - $obj = clone $this; |
|
187 | - if (!isset($obj->_attributes)) { |
|
188 | - $obj->_attributes = new Attributes(); |
|
189 | - } |
|
190 | - $obj->_attributes = $obj->_attributes->withUnique( |
|
191 | - Attribute::fromAttributeValues( |
|
192 | - new ExtensionRequestValue($extensions))); |
|
193 | - return $obj; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Generate ASN.1 structure. |
|
198 | - * |
|
199 | - * @return Sequence |
|
200 | - */ |
|
201 | - public function toASN1(): Sequence |
|
202 | - { |
|
203 | - $elements = [new Integer($this->_version), |
|
204 | - $this->_subject->toASN1(), $this->_subjectPKInfo->toASN1(), ]; |
|
205 | - if (isset($this->_attributes)) { |
|
206 | - $elements[] = new ImplicitlyTaggedType(0, |
|
207 | - $this->_attributes->toASN1()); |
|
208 | - } |
|
209 | - return new Sequence(...$elements); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Create signed CertificationRequest. |
|
214 | - * |
|
215 | - * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
216 | - * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
217 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
218 | - * |
|
219 | - * @return CertificationRequest |
|
220 | - */ |
|
221 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
222 | - PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): CertificationRequest |
|
223 | - { |
|
224 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
225 | - $data = $this->toASN1()->toDER(); |
|
226 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
227 | - return new CertificationRequest($this, $algo, $signature); |
|
228 | - } |
|
27 | + const VERSION_1 = 0; |
|
28 | + |
|
29 | + /** |
|
30 | + * Version. |
|
31 | + * |
|
32 | + * @var int |
|
33 | + */ |
|
34 | + protected $_version; |
|
35 | + |
|
36 | + /** |
|
37 | + * Subject. |
|
38 | + * |
|
39 | + * @var Name |
|
40 | + */ |
|
41 | + protected $_subject; |
|
42 | + |
|
43 | + /** |
|
44 | + * Public key info. |
|
45 | + * |
|
46 | + * @var PublicKeyInfo |
|
47 | + */ |
|
48 | + protected $_subjectPKInfo; |
|
49 | + |
|
50 | + /** |
|
51 | + * Attributes. |
|
52 | + * |
|
53 | + * @var null|Attributes |
|
54 | + */ |
|
55 | + protected $_attributes; |
|
56 | + |
|
57 | + /** |
|
58 | + * Constructor. |
|
59 | + * |
|
60 | + * @param Name $subject Subject |
|
61 | + * @param PublicKeyInfo $pkinfo Public key info |
|
62 | + */ |
|
63 | + public function __construct(Name $subject, PublicKeyInfo $pkinfo) |
|
64 | + { |
|
65 | + $this->_version = self::VERSION_1; |
|
66 | + $this->_subject = $subject; |
|
67 | + $this->_subjectPKInfo = $pkinfo; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Initialize from ASN.1. |
|
72 | + * |
|
73 | + * @param Sequence $seq |
|
74 | + * |
|
75 | + * @throws \UnexpectedValueException |
|
76 | + * |
|
77 | + * @return self |
|
78 | + */ |
|
79 | + public static function fromASN1(Sequence $seq): self |
|
80 | + { |
|
81 | + $version = $seq->at(0)->asInteger()->intNumber(); |
|
82 | + if (self::VERSION_1 !== $version) { |
|
83 | + throw new \UnexpectedValueException( |
|
84 | + "Version {$version} not supported."); |
|
85 | + } |
|
86 | + $subject = Name::fromASN1($seq->at(1)->asSequence()); |
|
87 | + $pkinfo = PublicKeyInfo::fromASN1($seq->at(2)->asSequence()); |
|
88 | + $obj = new self($subject, $pkinfo); |
|
89 | + if ($seq->hasTagged(0)) { |
|
90 | + $obj->_attributes = Attributes::fromASN1( |
|
91 | + $seq->getTagged(0)->asImplicit(Element::TYPE_SET)->asSet()); |
|
92 | + } |
|
93 | + return $obj; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Get version. |
|
98 | + * |
|
99 | + * @return int |
|
100 | + */ |
|
101 | + public function version(): int |
|
102 | + { |
|
103 | + return $this->_version; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Get self with subject. |
|
108 | + * |
|
109 | + * @param Name $subject |
|
110 | + * |
|
111 | + * @return self |
|
112 | + */ |
|
113 | + public function withSubject(Name $subject): self |
|
114 | + { |
|
115 | + $obj = clone $this; |
|
116 | + $obj->_subject = $subject; |
|
117 | + return $obj; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Get subject. |
|
122 | + * |
|
123 | + * @return Name |
|
124 | + */ |
|
125 | + public function subject(): Name |
|
126 | + { |
|
127 | + return $this->_subject; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Get subject public key info. |
|
132 | + * |
|
133 | + * @return PublicKeyInfo |
|
134 | + */ |
|
135 | + public function subjectPKInfo(): PublicKeyInfo |
|
136 | + { |
|
137 | + return $this->_subjectPKInfo; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Whether certification request info has attributes. |
|
142 | + * |
|
143 | + * @return bool |
|
144 | + */ |
|
145 | + public function hasAttributes(): bool |
|
146 | + { |
|
147 | + return isset($this->_attributes); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Get attributes. |
|
152 | + * |
|
153 | + * @throws \LogicException If not set |
|
154 | + * |
|
155 | + * @return Attributes |
|
156 | + */ |
|
157 | + public function attributes(): Attributes |
|
158 | + { |
|
159 | + if (!$this->hasAttributes()) { |
|
160 | + throw new \LogicException('No attributes.'); |
|
161 | + } |
|
162 | + return $this->_attributes; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Get instance of self with attributes. |
|
167 | + * |
|
168 | + * @param Attributes $attribs |
|
169 | + */ |
|
170 | + public function withAttributes(Attributes $attribs): self |
|
171 | + { |
|
172 | + $obj = clone $this; |
|
173 | + $obj->_attributes = $attribs; |
|
174 | + return $obj; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Get self with extension request attribute. |
|
179 | + * |
|
180 | + * @param Extensions $extensions Extensions to request |
|
181 | + * |
|
182 | + * @return self |
|
183 | + */ |
|
184 | + public function withExtensionRequest(Extensions $extensions): self |
|
185 | + { |
|
186 | + $obj = clone $this; |
|
187 | + if (!isset($obj->_attributes)) { |
|
188 | + $obj->_attributes = new Attributes(); |
|
189 | + } |
|
190 | + $obj->_attributes = $obj->_attributes->withUnique( |
|
191 | + Attribute::fromAttributeValues( |
|
192 | + new ExtensionRequestValue($extensions))); |
|
193 | + return $obj; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Generate ASN.1 structure. |
|
198 | + * |
|
199 | + * @return Sequence |
|
200 | + */ |
|
201 | + public function toASN1(): Sequence |
|
202 | + { |
|
203 | + $elements = [new Integer($this->_version), |
|
204 | + $this->_subject->toASN1(), $this->_subjectPKInfo->toASN1(), ]; |
|
205 | + if (isset($this->_attributes)) { |
|
206 | + $elements[] = new ImplicitlyTaggedType(0, |
|
207 | + $this->_attributes->toASN1()); |
|
208 | + } |
|
209 | + return new Sequence(...$elements); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Create signed CertificationRequest. |
|
214 | + * |
|
215 | + * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
216 | + * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
217 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
218 | + * |
|
219 | + * @return CertificationRequest |
|
220 | + */ |
|
221 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
222 | + PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): CertificationRequest |
|
223 | + { |
|
224 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
225 | + $data = $this->toASN1()->toDER(); |
|
226 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
227 | + return new CertificationRequest($this, $algo, $signature); |
|
228 | + } |
|
229 | 229 | } |
@@ -10,413 +10,413 @@ |
||
10 | 10 | |
11 | 11 | class PolicyTree |
12 | 12 | { |
13 | - /** |
|
14 | - * Root node at depth zero. |
|
15 | - * |
|
16 | - * @var null|PolicyNode |
|
17 | - */ |
|
18 | - protected $_root; |
|
13 | + /** |
|
14 | + * Root node at depth zero. |
|
15 | + * |
|
16 | + * @var null|PolicyNode |
|
17 | + */ |
|
18 | + protected $_root; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Constructor. |
|
22 | - * |
|
23 | - * @param PolicyNode $root Initial root node |
|
24 | - */ |
|
25 | - public function __construct(PolicyNode $root) |
|
26 | - { |
|
27 | - $this->_root = $root; |
|
28 | - } |
|
20 | + /** |
|
21 | + * Constructor. |
|
22 | + * |
|
23 | + * @param PolicyNode $root Initial root node |
|
24 | + */ |
|
25 | + public function __construct(PolicyNode $root) |
|
26 | + { |
|
27 | + $this->_root = $root; |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Process policy information from the certificate. |
|
32 | - * |
|
33 | - * Certificate policies extension must be present. |
|
34 | - * |
|
35 | - * @param ValidatorState $state |
|
36 | - * @param Certificate $cert |
|
37 | - * |
|
38 | - * @return ValidatorState |
|
39 | - */ |
|
40 | - public function processPolicies(ValidatorState $state, |
|
41 | - Certificate $cert): ValidatorState |
|
42 | - { |
|
43 | - $policies = $cert->tbsCertificate()->extensions()->certificatePolicies(); |
|
44 | - $tree = clone $this; |
|
45 | - // (d.1) for each policy P not equal to anyPolicy |
|
46 | - foreach ($policies as $policy) { |
|
47 | - if ($policy->isAnyPolicy()) { |
|
48 | - $tree->_processAnyPolicy($policy, $cert, $state); |
|
49 | - } else { |
|
50 | - $tree->_processPolicy($policy, $state); |
|
51 | - } |
|
52 | - } |
|
53 | - // if whole tree is pruned |
|
54 | - if (!$tree->_pruneTree($state->index() - 1)) { |
|
55 | - return $state->withoutValidPolicyTree(); |
|
56 | - } |
|
57 | - return $state->withValidPolicyTree($tree); |
|
58 | - } |
|
30 | + /** |
|
31 | + * Process policy information from the certificate. |
|
32 | + * |
|
33 | + * Certificate policies extension must be present. |
|
34 | + * |
|
35 | + * @param ValidatorState $state |
|
36 | + * @param Certificate $cert |
|
37 | + * |
|
38 | + * @return ValidatorState |
|
39 | + */ |
|
40 | + public function processPolicies(ValidatorState $state, |
|
41 | + Certificate $cert): ValidatorState |
|
42 | + { |
|
43 | + $policies = $cert->tbsCertificate()->extensions()->certificatePolicies(); |
|
44 | + $tree = clone $this; |
|
45 | + // (d.1) for each policy P not equal to anyPolicy |
|
46 | + foreach ($policies as $policy) { |
|
47 | + if ($policy->isAnyPolicy()) { |
|
48 | + $tree->_processAnyPolicy($policy, $cert, $state); |
|
49 | + } else { |
|
50 | + $tree->_processPolicy($policy, $state); |
|
51 | + } |
|
52 | + } |
|
53 | + // if whole tree is pruned |
|
54 | + if (!$tree->_pruneTree($state->index() - 1)) { |
|
55 | + return $state->withoutValidPolicyTree(); |
|
56 | + } |
|
57 | + return $state->withValidPolicyTree($tree); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Process policy mappings from the certificate. |
|
62 | - * |
|
63 | - * @param ValidatorState $state |
|
64 | - * @param Certificate $cert |
|
65 | - * |
|
66 | - * @return ValidatorState |
|
67 | - */ |
|
68 | - public function processMappings(ValidatorState $state, |
|
69 | - Certificate $cert): ValidatorState |
|
70 | - { |
|
71 | - $tree = clone $this; |
|
72 | - if ($state->policyMapping() > 0) { |
|
73 | - $tree->_applyMappings($cert, $state); |
|
74 | - } elseif (0 === $state->policyMapping()) { |
|
75 | - $tree->_deleteMappings($cert, $state); |
|
76 | - } |
|
77 | - // if whole tree is pruned |
|
78 | - if (!$tree->_root) { |
|
79 | - return $state->withoutValidPolicyTree(); |
|
80 | - } |
|
81 | - return $state->withValidPolicyTree($tree); |
|
82 | - } |
|
60 | + /** |
|
61 | + * Process policy mappings from the certificate. |
|
62 | + * |
|
63 | + * @param ValidatorState $state |
|
64 | + * @param Certificate $cert |
|
65 | + * |
|
66 | + * @return ValidatorState |
|
67 | + */ |
|
68 | + public function processMappings(ValidatorState $state, |
|
69 | + Certificate $cert): ValidatorState |
|
70 | + { |
|
71 | + $tree = clone $this; |
|
72 | + if ($state->policyMapping() > 0) { |
|
73 | + $tree->_applyMappings($cert, $state); |
|
74 | + } elseif (0 === $state->policyMapping()) { |
|
75 | + $tree->_deleteMappings($cert, $state); |
|
76 | + } |
|
77 | + // if whole tree is pruned |
|
78 | + if (!$tree->_root) { |
|
79 | + return $state->withoutValidPolicyTree(); |
|
80 | + } |
|
81 | + return $state->withValidPolicyTree($tree); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Calculate policy intersection as specified in Wrap-Up Procedure 6.1.5.g. |
|
86 | - * |
|
87 | - * @param ValidatorState $state |
|
88 | - * @param array $policies |
|
89 | - * |
|
90 | - * @return ValidatorState |
|
91 | - */ |
|
92 | - public function calculateIntersection(ValidatorState $state, |
|
93 | - array $policies): ValidatorState |
|
94 | - { |
|
95 | - $tree = clone $this; |
|
96 | - $valid_policy_node_set = $tree->_validPolicyNodeSet(); |
|
97 | - // 2. If the valid_policy of any node in the valid_policy_node_set |
|
98 | - // is not in the user-initial-policy-set and is not anyPolicy, |
|
99 | - // delete this node and all its children. |
|
100 | - $valid_policy_node_set = array_filter($valid_policy_node_set, |
|
101 | - function (PolicyNode $node) use ($policies) { |
|
102 | - if ($node->isAnyPolicy()) { |
|
103 | - return true; |
|
104 | - } |
|
105 | - if (in_array($node->validPolicy(), $policies)) { |
|
106 | - return true; |
|
107 | - } |
|
108 | - $node->remove(); |
|
109 | - return false; |
|
110 | - }); |
|
111 | - // array of valid policy OIDs |
|
112 | - $valid_policy_set = array_map( |
|
113 | - function (PolicyNode $node) { |
|
114 | - return $node->validPolicy(); |
|
115 | - }, $valid_policy_node_set); |
|
116 | - // 3. If the valid_policy_tree includes a node of depth n with |
|
117 | - // the valid_policy anyPolicy and the user-initial-policy-set |
|
118 | - // is not any-policy |
|
119 | - foreach ($tree->_nodesAtDepth($state->index()) as $node) { |
|
120 | - if ($node->hasParent() && $node->isAnyPolicy()) { |
|
121 | - // a. Set P-Q to the qualifier_set in the node of depth n |
|
122 | - // with valid_policy anyPolicy. |
|
123 | - $pq = $node->qualifiers(); |
|
124 | - // b. For each P-OID in the user-initial-policy-set that is not |
|
125 | - // the valid_policy of a node in the valid_policy_node_set, |
|
126 | - // create a child node whose parent is the node of depth n-1 |
|
127 | - // with the valid_policy anyPolicy. |
|
128 | - $poids = array_diff($policies, $valid_policy_set); |
|
129 | - foreach ($tree->_nodesAtDepth($state->index() - 1) as $parent) { |
|
130 | - if ($parent->isAnyPolicy()) { |
|
131 | - // Set the values in the child node as follows: |
|
132 | - // set the valid_policy to P-OID, set the qualifier_set |
|
133 | - // to P-Q, and set the expected_policy_set to {P-OID}. |
|
134 | - foreach ($poids as $poid) { |
|
135 | - $parent->addChild(new PolicyNode($poid, $pq, [$poid])); |
|
136 | - } |
|
137 | - break; |
|
138 | - } |
|
139 | - } |
|
140 | - // c. Delete the node of depth n with the |
|
141 | - // valid_policy anyPolicy. |
|
142 | - $node->remove(); |
|
143 | - } |
|
144 | - } |
|
145 | - // 4. If there is a node in the valid_policy_tree of depth n-1 or less |
|
146 | - // without any child nodes, delete that node. Repeat this step until |
|
147 | - // there are no nodes of depth n-1 or less without children. |
|
148 | - if (!$tree->_pruneTree($state->index() - 1)) { |
|
149 | - return $state->withoutValidPolicyTree(); |
|
150 | - } |
|
151 | - return $state->withValidPolicyTree($tree); |
|
152 | - } |
|
84 | + /** |
|
85 | + * Calculate policy intersection as specified in Wrap-Up Procedure 6.1.5.g. |
|
86 | + * |
|
87 | + * @param ValidatorState $state |
|
88 | + * @param array $policies |
|
89 | + * |
|
90 | + * @return ValidatorState |
|
91 | + */ |
|
92 | + public function calculateIntersection(ValidatorState $state, |
|
93 | + array $policies): ValidatorState |
|
94 | + { |
|
95 | + $tree = clone $this; |
|
96 | + $valid_policy_node_set = $tree->_validPolicyNodeSet(); |
|
97 | + // 2. If the valid_policy of any node in the valid_policy_node_set |
|
98 | + // is not in the user-initial-policy-set and is not anyPolicy, |
|
99 | + // delete this node and all its children. |
|
100 | + $valid_policy_node_set = array_filter($valid_policy_node_set, |
|
101 | + function (PolicyNode $node) use ($policies) { |
|
102 | + if ($node->isAnyPolicy()) { |
|
103 | + return true; |
|
104 | + } |
|
105 | + if (in_array($node->validPolicy(), $policies)) { |
|
106 | + return true; |
|
107 | + } |
|
108 | + $node->remove(); |
|
109 | + return false; |
|
110 | + }); |
|
111 | + // array of valid policy OIDs |
|
112 | + $valid_policy_set = array_map( |
|
113 | + function (PolicyNode $node) { |
|
114 | + return $node->validPolicy(); |
|
115 | + }, $valid_policy_node_set); |
|
116 | + // 3. If the valid_policy_tree includes a node of depth n with |
|
117 | + // the valid_policy anyPolicy and the user-initial-policy-set |
|
118 | + // is not any-policy |
|
119 | + foreach ($tree->_nodesAtDepth($state->index()) as $node) { |
|
120 | + if ($node->hasParent() && $node->isAnyPolicy()) { |
|
121 | + // a. Set P-Q to the qualifier_set in the node of depth n |
|
122 | + // with valid_policy anyPolicy. |
|
123 | + $pq = $node->qualifiers(); |
|
124 | + // b. For each P-OID in the user-initial-policy-set that is not |
|
125 | + // the valid_policy of a node in the valid_policy_node_set, |
|
126 | + // create a child node whose parent is the node of depth n-1 |
|
127 | + // with the valid_policy anyPolicy. |
|
128 | + $poids = array_diff($policies, $valid_policy_set); |
|
129 | + foreach ($tree->_nodesAtDepth($state->index() - 1) as $parent) { |
|
130 | + if ($parent->isAnyPolicy()) { |
|
131 | + // Set the values in the child node as follows: |
|
132 | + // set the valid_policy to P-OID, set the qualifier_set |
|
133 | + // to P-Q, and set the expected_policy_set to {P-OID}. |
|
134 | + foreach ($poids as $poid) { |
|
135 | + $parent->addChild(new PolicyNode($poid, $pq, [$poid])); |
|
136 | + } |
|
137 | + break; |
|
138 | + } |
|
139 | + } |
|
140 | + // c. Delete the node of depth n with the |
|
141 | + // valid_policy anyPolicy. |
|
142 | + $node->remove(); |
|
143 | + } |
|
144 | + } |
|
145 | + // 4. If there is a node in the valid_policy_tree of depth n-1 or less |
|
146 | + // without any child nodes, delete that node. Repeat this step until |
|
147 | + // there are no nodes of depth n-1 or less without children. |
|
148 | + if (!$tree->_pruneTree($state->index() - 1)) { |
|
149 | + return $state->withoutValidPolicyTree(); |
|
150 | + } |
|
151 | + return $state->withValidPolicyTree($tree); |
|
152 | + } |
|
153 | 153 | |
154 | - /** |
|
155 | - * Get policies at given policy tree depth. |
|
156 | - * |
|
157 | - * @param int $i Depth in range 1..n |
|
158 | - * |
|
159 | - * @return PolicyInformation[] |
|
160 | - */ |
|
161 | - public function policiesAtDepth(int $i): array |
|
162 | - { |
|
163 | - $policies = []; |
|
164 | - foreach ($this->_nodesAtDepth($i) as $node) { |
|
165 | - $policies[] = new PolicyInformation( |
|
166 | - $node->validPolicy(), ...$node->qualifiers()); |
|
167 | - } |
|
168 | - return $policies; |
|
169 | - } |
|
154 | + /** |
|
155 | + * Get policies at given policy tree depth. |
|
156 | + * |
|
157 | + * @param int $i Depth in range 1..n |
|
158 | + * |
|
159 | + * @return PolicyInformation[] |
|
160 | + */ |
|
161 | + public function policiesAtDepth(int $i): array |
|
162 | + { |
|
163 | + $policies = []; |
|
164 | + foreach ($this->_nodesAtDepth($i) as $node) { |
|
165 | + $policies[] = new PolicyInformation( |
|
166 | + $node->validPolicy(), ...$node->qualifiers()); |
|
167 | + } |
|
168 | + return $policies; |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * Process single policy information. |
|
173 | - * |
|
174 | - * @param PolicyInformation $policy |
|
175 | - * @param ValidatorState $state |
|
176 | - */ |
|
177 | - protected function _processPolicy(PolicyInformation $policy, |
|
178 | - ValidatorState $state): void |
|
179 | - { |
|
180 | - $p_oid = $policy->oid(); |
|
181 | - $i = $state->index(); |
|
182 | - $match_count = 0; |
|
183 | - // (d.1.i) for each node of depth i-1 in the valid_policy_tree... |
|
184 | - foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
185 | - // ...where P-OID is in the expected_policy_set |
|
186 | - if ($node->hasExpectedPolicy($p_oid)) { |
|
187 | - $node->addChild(new PolicyNode( |
|
188 | - $p_oid, $policy->qualifiers(), [$p_oid])); |
|
189 | - ++$match_count; |
|
190 | - } |
|
191 | - } |
|
192 | - // (d.1.ii) if there was no match in step (i)... |
|
193 | - if (!$match_count) { |
|
194 | - // ...and the valid_policy_tree includes a node of depth i-1 with |
|
195 | - // the valid_policy anyPolicy |
|
196 | - foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
197 | - if ($node->isAnyPolicy()) { |
|
198 | - $node->addChild(new PolicyNode( |
|
199 | - $p_oid, $policy->qualifiers(), [$p_oid])); |
|
200 | - } |
|
201 | - } |
|
202 | - } |
|
203 | - } |
|
171 | + /** |
|
172 | + * Process single policy information. |
|
173 | + * |
|
174 | + * @param PolicyInformation $policy |
|
175 | + * @param ValidatorState $state |
|
176 | + */ |
|
177 | + protected function _processPolicy(PolicyInformation $policy, |
|
178 | + ValidatorState $state): void |
|
179 | + { |
|
180 | + $p_oid = $policy->oid(); |
|
181 | + $i = $state->index(); |
|
182 | + $match_count = 0; |
|
183 | + // (d.1.i) for each node of depth i-1 in the valid_policy_tree... |
|
184 | + foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
185 | + // ...where P-OID is in the expected_policy_set |
|
186 | + if ($node->hasExpectedPolicy($p_oid)) { |
|
187 | + $node->addChild(new PolicyNode( |
|
188 | + $p_oid, $policy->qualifiers(), [$p_oid])); |
|
189 | + ++$match_count; |
|
190 | + } |
|
191 | + } |
|
192 | + // (d.1.ii) if there was no match in step (i)... |
|
193 | + if (!$match_count) { |
|
194 | + // ...and the valid_policy_tree includes a node of depth i-1 with |
|
195 | + // the valid_policy anyPolicy |
|
196 | + foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
197 | + if ($node->isAnyPolicy()) { |
|
198 | + $node->addChild(new PolicyNode( |
|
199 | + $p_oid, $policy->qualifiers(), [$p_oid])); |
|
200 | + } |
|
201 | + } |
|
202 | + } |
|
203 | + } |
|
204 | 204 | |
205 | - /** |
|
206 | - * Process anyPolicy policy information. |
|
207 | - * |
|
208 | - * @param PolicyInformation $policy |
|
209 | - * @param Certificate $cert |
|
210 | - * @param ValidatorState $state |
|
211 | - */ |
|
212 | - protected function _processAnyPolicy(PolicyInformation $policy, |
|
213 | - Certificate $cert, ValidatorState $state): void |
|
214 | - { |
|
215 | - $i = $state->index(); |
|
216 | - // if (a) inhibit_anyPolicy is greater than 0 or |
|
217 | - // (b) i<n and the certificate is self-issued |
|
218 | - if (!($state->inhibitAnyPolicy() > 0 || |
|
219 | - ($i < $state->pathLength() && $cert->isSelfIssued()))) { |
|
220 | - return; |
|
221 | - } |
|
222 | - // for each node in the valid_policy_tree of depth i-1 |
|
223 | - foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
224 | - // for each value in the expected_policy_set |
|
225 | - foreach ($node->expectedPolicies() as $p_oid) { |
|
226 | - // that does not appear in a child node |
|
227 | - if (!$node->hasChildWithValidPolicy($p_oid)) { |
|
228 | - $node->addChild(new PolicyNode( |
|
229 | - $p_oid, $policy->qualifiers(), [$p_oid])); |
|
230 | - } |
|
231 | - } |
|
232 | - } |
|
233 | - } |
|
205 | + /** |
|
206 | + * Process anyPolicy policy information. |
|
207 | + * |
|
208 | + * @param PolicyInformation $policy |
|
209 | + * @param Certificate $cert |
|
210 | + * @param ValidatorState $state |
|
211 | + */ |
|
212 | + protected function _processAnyPolicy(PolicyInformation $policy, |
|
213 | + Certificate $cert, ValidatorState $state): void |
|
214 | + { |
|
215 | + $i = $state->index(); |
|
216 | + // if (a) inhibit_anyPolicy is greater than 0 or |
|
217 | + // (b) i<n and the certificate is self-issued |
|
218 | + if (!($state->inhibitAnyPolicy() > 0 || |
|
219 | + ($i < $state->pathLength() && $cert->isSelfIssued()))) { |
|
220 | + return; |
|
221 | + } |
|
222 | + // for each node in the valid_policy_tree of depth i-1 |
|
223 | + foreach ($this->_nodesAtDepth($i - 1) as $node) { |
|
224 | + // for each value in the expected_policy_set |
|
225 | + foreach ($node->expectedPolicies() as $p_oid) { |
|
226 | + // that does not appear in a child node |
|
227 | + if (!$node->hasChildWithValidPolicy($p_oid)) { |
|
228 | + $node->addChild(new PolicyNode( |
|
229 | + $p_oid, $policy->qualifiers(), [$p_oid])); |
|
230 | + } |
|
231 | + } |
|
232 | + } |
|
233 | + } |
|
234 | 234 | |
235 | - /** |
|
236 | - * Apply policy mappings to the policy tree. |
|
237 | - * |
|
238 | - * @param Certificate $cert |
|
239 | - * @param ValidatorState $state |
|
240 | - */ |
|
241 | - protected function _applyMappings(Certificate $cert, ValidatorState $state): void |
|
242 | - { |
|
243 | - $policy_mappings = $cert->tbsCertificate()->extensions()->policyMappings(); |
|
244 | - // (6.1.4. b.1.) for each node in the valid_policy_tree of depth i... |
|
245 | - foreach ($policy_mappings->flattenedMappings() as $idp => $sdps) { |
|
246 | - $match_count = 0; |
|
247 | - foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
248 | - // ...where ID-P is the valid_policy |
|
249 | - if ($node->validPolicy() === $idp) { |
|
250 | - // set expected_policy_set to the set of subjectDomainPolicy |
|
251 | - // values that are specified as equivalent to ID-P by |
|
252 | - // the policy mappings extension |
|
253 | - $node->setExpectedPolicies(...$sdps); |
|
254 | - ++$match_count; |
|
255 | - } |
|
256 | - } |
|
257 | - // if no node of depth i in the valid_policy_tree has |
|
258 | - // a valid_policy of ID-P... |
|
259 | - if (!$match_count) { |
|
260 | - $this->_applyAnyPolicyMapping($cert, $state, $idp, $sdps); |
|
261 | - } |
|
262 | - } |
|
263 | - } |
|
235 | + /** |
|
236 | + * Apply policy mappings to the policy tree. |
|
237 | + * |
|
238 | + * @param Certificate $cert |
|
239 | + * @param ValidatorState $state |
|
240 | + */ |
|
241 | + protected function _applyMappings(Certificate $cert, ValidatorState $state): void |
|
242 | + { |
|
243 | + $policy_mappings = $cert->tbsCertificate()->extensions()->policyMappings(); |
|
244 | + // (6.1.4. b.1.) for each node in the valid_policy_tree of depth i... |
|
245 | + foreach ($policy_mappings->flattenedMappings() as $idp => $sdps) { |
|
246 | + $match_count = 0; |
|
247 | + foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
248 | + // ...where ID-P is the valid_policy |
|
249 | + if ($node->validPolicy() === $idp) { |
|
250 | + // set expected_policy_set to the set of subjectDomainPolicy |
|
251 | + // values that are specified as equivalent to ID-P by |
|
252 | + // the policy mappings extension |
|
253 | + $node->setExpectedPolicies(...$sdps); |
|
254 | + ++$match_count; |
|
255 | + } |
|
256 | + } |
|
257 | + // if no node of depth i in the valid_policy_tree has |
|
258 | + // a valid_policy of ID-P... |
|
259 | + if (!$match_count) { |
|
260 | + $this->_applyAnyPolicyMapping($cert, $state, $idp, $sdps); |
|
261 | + } |
|
262 | + } |
|
263 | + } |
|
264 | 264 | |
265 | - /** |
|
266 | - * Apply anyPolicy mapping to the policy tree as specified in 6.1.4 (b)(1). |
|
267 | - * |
|
268 | - * @param Certificate $cert |
|
269 | - * @param ValidatorState $state |
|
270 | - * @param string $idp OID of the issuer domain policy |
|
271 | - * @param array $sdps Array of subject domain policy OIDs |
|
272 | - */ |
|
273 | - protected function _applyAnyPolicyMapping(Certificate $cert, |
|
274 | - ValidatorState $state, string $idp, array $sdps): void |
|
275 | - { |
|
276 | - // (6.1.4. b.1.) ...but there is a node of depth i with |
|
277 | - // a valid_policy of anyPolicy |
|
278 | - foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
279 | - if ($node->isAnyPolicy()) { |
|
280 | - // then generate a child node of the node of depth i-1 |
|
281 | - // that has a valid_policy of anyPolicy as follows... |
|
282 | - foreach ($this->_nodesAtDepth($state->index() - 1) as $subnode) { |
|
283 | - if ($subnode->isAnyPolicy()) { |
|
284 | - // try to fetch qualifiers of anyPolicy certificate policy |
|
285 | - try { |
|
286 | - $qualifiers = $cert->tbsCertificate() |
|
287 | - ->extensions()->certificatePolicies() |
|
288 | - ->anyPolicy()->qualifiers(); |
|
289 | - } catch (\LogicException $e) { |
|
290 | - // if there's no policies or no qualifiers |
|
291 | - $qualifiers = []; |
|
292 | - } |
|
293 | - $subnode->addChild(new PolicyNode($idp, $qualifiers, $sdps)); |
|
294 | - // bail after first anyPolicy has been processed |
|
295 | - break; |
|
296 | - } |
|
297 | - } |
|
298 | - // bail after first anyPolicy has been processed |
|
299 | - break; |
|
300 | - } |
|
301 | - } |
|
302 | - } |
|
265 | + /** |
|
266 | + * Apply anyPolicy mapping to the policy tree as specified in 6.1.4 (b)(1). |
|
267 | + * |
|
268 | + * @param Certificate $cert |
|
269 | + * @param ValidatorState $state |
|
270 | + * @param string $idp OID of the issuer domain policy |
|
271 | + * @param array $sdps Array of subject domain policy OIDs |
|
272 | + */ |
|
273 | + protected function _applyAnyPolicyMapping(Certificate $cert, |
|
274 | + ValidatorState $state, string $idp, array $sdps): void |
|
275 | + { |
|
276 | + // (6.1.4. b.1.) ...but there is a node of depth i with |
|
277 | + // a valid_policy of anyPolicy |
|
278 | + foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
279 | + if ($node->isAnyPolicy()) { |
|
280 | + // then generate a child node of the node of depth i-1 |
|
281 | + // that has a valid_policy of anyPolicy as follows... |
|
282 | + foreach ($this->_nodesAtDepth($state->index() - 1) as $subnode) { |
|
283 | + if ($subnode->isAnyPolicy()) { |
|
284 | + // try to fetch qualifiers of anyPolicy certificate policy |
|
285 | + try { |
|
286 | + $qualifiers = $cert->tbsCertificate() |
|
287 | + ->extensions()->certificatePolicies() |
|
288 | + ->anyPolicy()->qualifiers(); |
|
289 | + } catch (\LogicException $e) { |
|
290 | + // if there's no policies or no qualifiers |
|
291 | + $qualifiers = []; |
|
292 | + } |
|
293 | + $subnode->addChild(new PolicyNode($idp, $qualifiers, $sdps)); |
|
294 | + // bail after first anyPolicy has been processed |
|
295 | + break; |
|
296 | + } |
|
297 | + } |
|
298 | + // bail after first anyPolicy has been processed |
|
299 | + break; |
|
300 | + } |
|
301 | + } |
|
302 | + } |
|
303 | 303 | |
304 | - /** |
|
305 | - * Delete nodes as specified in 6.1.4 (b)(2). |
|
306 | - * |
|
307 | - * @param Certificate $cert |
|
308 | - * @param ValidatorState $state |
|
309 | - */ |
|
310 | - protected function _deleteMappings(Certificate $cert, |
|
311 | - ValidatorState $state): void |
|
312 | - { |
|
313 | - $idps = $cert->tbsCertificate()->extensions() |
|
314 | - ->policyMappings()->issuerDomainPolicies(); |
|
315 | - // delete each node of depth i in the valid_policy_tree |
|
316 | - // where ID-P is the valid_policy |
|
317 | - foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
318 | - if (in_array($node->validPolicy(), $idps)) { |
|
319 | - $node->remove(); |
|
320 | - } |
|
321 | - } |
|
322 | - $this->_pruneTree($state->index() - 1); |
|
323 | - } |
|
304 | + /** |
|
305 | + * Delete nodes as specified in 6.1.4 (b)(2). |
|
306 | + * |
|
307 | + * @param Certificate $cert |
|
308 | + * @param ValidatorState $state |
|
309 | + */ |
|
310 | + protected function _deleteMappings(Certificate $cert, |
|
311 | + ValidatorState $state): void |
|
312 | + { |
|
313 | + $idps = $cert->tbsCertificate()->extensions() |
|
314 | + ->policyMappings()->issuerDomainPolicies(); |
|
315 | + // delete each node of depth i in the valid_policy_tree |
|
316 | + // where ID-P is the valid_policy |
|
317 | + foreach ($this->_nodesAtDepth($state->index()) as $node) { |
|
318 | + if (in_array($node->validPolicy(), $idps)) { |
|
319 | + $node->remove(); |
|
320 | + } |
|
321 | + } |
|
322 | + $this->_pruneTree($state->index() - 1); |
|
323 | + } |
|
324 | 324 | |
325 | - /** |
|
326 | - * Prune tree starting from given depth. |
|
327 | - * |
|
328 | - * @param int $depth |
|
329 | - * |
|
330 | - * @return int The number of nodes left in a tree |
|
331 | - */ |
|
332 | - protected function _pruneTree(int $depth): int |
|
333 | - { |
|
334 | - if (!$this->_root) { |
|
335 | - return 0; |
|
336 | - } |
|
337 | - for ($i = $depth; $i > 0; --$i) { |
|
338 | - foreach ($this->_nodesAtDepth($i) as $node) { |
|
339 | - if (!count($node)) { |
|
340 | - $node->remove(); |
|
341 | - } |
|
342 | - } |
|
343 | - } |
|
344 | - // if root has no children left |
|
345 | - if (!count($this->_root)) { |
|
346 | - $this->_root = null; |
|
347 | - return 0; |
|
348 | - } |
|
349 | - return $this->_root->nodeCount(); |
|
350 | - } |
|
325 | + /** |
|
326 | + * Prune tree starting from given depth. |
|
327 | + * |
|
328 | + * @param int $depth |
|
329 | + * |
|
330 | + * @return int The number of nodes left in a tree |
|
331 | + */ |
|
332 | + protected function _pruneTree(int $depth): int |
|
333 | + { |
|
334 | + if (!$this->_root) { |
|
335 | + return 0; |
|
336 | + } |
|
337 | + for ($i = $depth; $i > 0; --$i) { |
|
338 | + foreach ($this->_nodesAtDepth($i) as $node) { |
|
339 | + if (!count($node)) { |
|
340 | + $node->remove(); |
|
341 | + } |
|
342 | + } |
|
343 | + } |
|
344 | + // if root has no children left |
|
345 | + if (!count($this->_root)) { |
|
346 | + $this->_root = null; |
|
347 | + return 0; |
|
348 | + } |
|
349 | + return $this->_root->nodeCount(); |
|
350 | + } |
|
351 | 351 | |
352 | - /** |
|
353 | - * Get all nodes at given depth. |
|
354 | - * |
|
355 | - * @param int $i |
|
356 | - * |
|
357 | - * @return PolicyNode[] |
|
358 | - */ |
|
359 | - protected function _nodesAtDepth(int $i): array |
|
360 | - { |
|
361 | - if (!$this->_root) { |
|
362 | - return []; |
|
363 | - } |
|
364 | - $depth = 0; |
|
365 | - $nodes = [$this->_root]; |
|
366 | - while ($depth < $i) { |
|
367 | - $nodes = self::_gatherChildren(...$nodes); |
|
368 | - if (!count($nodes)) { |
|
369 | - break; |
|
370 | - } |
|
371 | - ++$depth; |
|
372 | - } |
|
373 | - return $nodes; |
|
374 | - } |
|
352 | + /** |
|
353 | + * Get all nodes at given depth. |
|
354 | + * |
|
355 | + * @param int $i |
|
356 | + * |
|
357 | + * @return PolicyNode[] |
|
358 | + */ |
|
359 | + protected function _nodesAtDepth(int $i): array |
|
360 | + { |
|
361 | + if (!$this->_root) { |
|
362 | + return []; |
|
363 | + } |
|
364 | + $depth = 0; |
|
365 | + $nodes = [$this->_root]; |
|
366 | + while ($depth < $i) { |
|
367 | + $nodes = self::_gatherChildren(...$nodes); |
|
368 | + if (!count($nodes)) { |
|
369 | + break; |
|
370 | + } |
|
371 | + ++$depth; |
|
372 | + } |
|
373 | + return $nodes; |
|
374 | + } |
|
375 | 375 | |
376 | - /** |
|
377 | - * Get the valid policy node set as specified in spec 6.1.5.(g)(iii)1. |
|
378 | - * |
|
379 | - * @return PolicyNode[] |
|
380 | - */ |
|
381 | - protected function _validPolicyNodeSet(): array |
|
382 | - { |
|
383 | - // 1. Determine the set of policy nodes whose parent nodes have |
|
384 | - // a valid_policy of anyPolicy. This is the valid_policy_node_set. |
|
385 | - $set = []; |
|
386 | - if (!$this->_root) { |
|
387 | - return $set; |
|
388 | - } |
|
389 | - // for each node in a tree |
|
390 | - $this->_root->walkNodes( |
|
391 | - function (PolicyNode $node) use (&$set) { |
|
392 | - $parents = $node->parents(); |
|
393 | - // node has parents |
|
394 | - if (count($parents)) { |
|
395 | - // check that each ancestor is an anyPolicy node |
|
396 | - foreach ($parents as $ancestor) { |
|
397 | - if (!$ancestor->isAnyPolicy()) { |
|
398 | - return; |
|
399 | - } |
|
400 | - } |
|
401 | - $set[] = $node; |
|
402 | - } |
|
403 | - }); |
|
404 | - return $set; |
|
405 | - } |
|
376 | + /** |
|
377 | + * Get the valid policy node set as specified in spec 6.1.5.(g)(iii)1. |
|
378 | + * |
|
379 | + * @return PolicyNode[] |
|
380 | + */ |
|
381 | + protected function _validPolicyNodeSet(): array |
|
382 | + { |
|
383 | + // 1. Determine the set of policy nodes whose parent nodes have |
|
384 | + // a valid_policy of anyPolicy. This is the valid_policy_node_set. |
|
385 | + $set = []; |
|
386 | + if (!$this->_root) { |
|
387 | + return $set; |
|
388 | + } |
|
389 | + // for each node in a tree |
|
390 | + $this->_root->walkNodes( |
|
391 | + function (PolicyNode $node) use (&$set) { |
|
392 | + $parents = $node->parents(); |
|
393 | + // node has parents |
|
394 | + if (count($parents)) { |
|
395 | + // check that each ancestor is an anyPolicy node |
|
396 | + foreach ($parents as $ancestor) { |
|
397 | + if (!$ancestor->isAnyPolicy()) { |
|
398 | + return; |
|
399 | + } |
|
400 | + } |
|
401 | + $set[] = $node; |
|
402 | + } |
|
403 | + }); |
|
404 | + return $set; |
|
405 | + } |
|
406 | 406 | |
407 | - /** |
|
408 | - * Gather all children of given nodes to a flattened array. |
|
409 | - * |
|
410 | - * @param PolicyNode ...$nodes |
|
411 | - * |
|
412 | - * @return PolicyNode[] |
|
413 | - */ |
|
414 | - private static function _gatherChildren(PolicyNode ...$nodes): array |
|
415 | - { |
|
416 | - $children = []; |
|
417 | - foreach ($nodes as $node) { |
|
418 | - $children = array_merge($children, $node->children()); |
|
419 | - } |
|
420 | - return $children; |
|
421 | - } |
|
407 | + /** |
|
408 | + * Gather all children of given nodes to a flattened array. |
|
409 | + * |
|
410 | + * @param PolicyNode ...$nodes |
|
411 | + * |
|
412 | + * @return PolicyNode[] |
|
413 | + */ |
|
414 | + private static function _gatherChildren(PolicyNode ...$nodes): array |
|
415 | + { |
|
416 | + $children = []; |
|
417 | + foreach ($nodes as $node) { |
|
418 | + $children = array_merge($children, $node->children()); |
|
419 | + } |
|
420 | + return $children; |
|
421 | + } |
|
422 | 422 | } |
@@ -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 | } |
@@ -20,242 +20,242 @@ |
||
20 | 20 | */ |
21 | 21 | class Certificate |
22 | 22 | { |
23 | - /** |
|
24 | - * "To be signed" certificate information. |
|
25 | - * |
|
26 | - * @var TBSCertificate |
|
27 | - */ |
|
28 | - protected $_tbsCertificate; |
|
23 | + /** |
|
24 | + * "To be signed" certificate information. |
|
25 | + * |
|
26 | + * @var TBSCertificate |
|
27 | + */ |
|
28 | + protected $_tbsCertificate; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Signature algorithm. |
|
32 | - * |
|
33 | - * @var SignatureAlgorithmIdentifier |
|
34 | - */ |
|
35 | - protected $_signatureAlgorithm; |
|
30 | + /** |
|
31 | + * Signature algorithm. |
|
32 | + * |
|
33 | + * @var SignatureAlgorithmIdentifier |
|
34 | + */ |
|
35 | + protected $_signatureAlgorithm; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Signature value. |
|
39 | - * |
|
40 | - * @var Signature |
|
41 | - */ |
|
42 | - protected $_signatureValue; |
|
37 | + /** |
|
38 | + * Signature value. |
|
39 | + * |
|
40 | + * @var Signature |
|
41 | + */ |
|
42 | + protected $_signatureValue; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Constructor. |
|
46 | - * |
|
47 | - * @param TBSCertificate $tbsCert |
|
48 | - * @param SignatureAlgorithmIdentifier $algo |
|
49 | - * @param Signature $signature |
|
50 | - */ |
|
51 | - public function __construct(TBSCertificate $tbsCert, |
|
52 | - SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
53 | - { |
|
54 | - $this->_tbsCertificate = $tbsCert; |
|
55 | - $this->_signatureAlgorithm = $algo; |
|
56 | - $this->_signatureValue = $signature; |
|
57 | - } |
|
44 | + /** |
|
45 | + * Constructor. |
|
46 | + * |
|
47 | + * @param TBSCertificate $tbsCert |
|
48 | + * @param SignatureAlgorithmIdentifier $algo |
|
49 | + * @param Signature $signature |
|
50 | + */ |
|
51 | + public function __construct(TBSCertificate $tbsCert, |
|
52 | + SignatureAlgorithmIdentifier $algo, Signature $signature) |
|
53 | + { |
|
54 | + $this->_tbsCertificate = $tbsCert; |
|
55 | + $this->_signatureAlgorithm = $algo; |
|
56 | + $this->_signatureValue = $signature; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Get certificate as a PEM formatted string. |
|
61 | - * |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - public function __toString(): string |
|
65 | - { |
|
66 | - return $this->toPEM()->string(); |
|
67 | - } |
|
59 | + /** |
|
60 | + * Get certificate as a PEM formatted string. |
|
61 | + * |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + public function __toString(): string |
|
65 | + { |
|
66 | + return $this->toPEM()->string(); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Initialize from ASN.1. |
|
71 | - * |
|
72 | - * @param Sequence $seq |
|
73 | - * |
|
74 | - * @return self |
|
75 | - */ |
|
76 | - public static function fromASN1(Sequence $seq): self |
|
77 | - { |
|
78 | - $tbsCert = TBSCertificate::fromASN1($seq->at(0)->asSequence()); |
|
79 | - $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
80 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
81 | - throw new \UnexpectedValueException( |
|
82 | - 'Unsupported signature algorithm ' . $algo->oid() . '.'); |
|
83 | - } |
|
84 | - $signature = Signature::fromSignatureData( |
|
85 | - $seq->at(2)->asBitString()->string(), $algo); |
|
86 | - return new self($tbsCert, $algo, $signature); |
|
87 | - } |
|
69 | + /** |
|
70 | + * Initialize from ASN.1. |
|
71 | + * |
|
72 | + * @param Sequence $seq |
|
73 | + * |
|
74 | + * @return self |
|
75 | + */ |
|
76 | + public static function fromASN1(Sequence $seq): self |
|
77 | + { |
|
78 | + $tbsCert = TBSCertificate::fromASN1($seq->at(0)->asSequence()); |
|
79 | + $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
|
80 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
81 | + throw new \UnexpectedValueException( |
|
82 | + 'Unsupported signature algorithm ' . $algo->oid() . '.'); |
|
83 | + } |
|
84 | + $signature = Signature::fromSignatureData( |
|
85 | + $seq->at(2)->asBitString()->string(), $algo); |
|
86 | + return new self($tbsCert, $algo, $signature); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Initialize from DER. |
|
91 | - * |
|
92 | - * @param string $data |
|
93 | - * |
|
94 | - * @return self |
|
95 | - */ |
|
96 | - public static function fromDER(string $data): self |
|
97 | - { |
|
98 | - return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
|
99 | - } |
|
89 | + /** |
|
90 | + * Initialize from DER. |
|
91 | + * |
|
92 | + * @param string $data |
|
93 | + * |
|
94 | + * @return self |
|
95 | + */ |
|
96 | + public static function fromDER(string $data): self |
|
97 | + { |
|
98 | + return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Initialize from PEM. |
|
103 | - * |
|
104 | - * @param PEM $pem |
|
105 | - * |
|
106 | - * @throws \UnexpectedValueException |
|
107 | - * |
|
108 | - * @return self |
|
109 | - */ |
|
110 | - public static function fromPEM(PEM $pem): self |
|
111 | - { |
|
112 | - if (PEM::TYPE_CERTIFICATE !== $pem->type()) { |
|
113 | - throw new \UnexpectedValueException('Invalid PEM type.'); |
|
114 | - } |
|
115 | - return self::fromDER($pem->data()); |
|
116 | - } |
|
101 | + /** |
|
102 | + * Initialize from PEM. |
|
103 | + * |
|
104 | + * @param PEM $pem |
|
105 | + * |
|
106 | + * @throws \UnexpectedValueException |
|
107 | + * |
|
108 | + * @return self |
|
109 | + */ |
|
110 | + public static function fromPEM(PEM $pem): self |
|
111 | + { |
|
112 | + if (PEM::TYPE_CERTIFICATE !== $pem->type()) { |
|
113 | + throw new \UnexpectedValueException('Invalid PEM type.'); |
|
114 | + } |
|
115 | + return self::fromDER($pem->data()); |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Get certificate information. |
|
120 | - * |
|
121 | - * @return TBSCertificate |
|
122 | - */ |
|
123 | - public function tbsCertificate(): TBSCertificate |
|
124 | - { |
|
125 | - return $this->_tbsCertificate; |
|
126 | - } |
|
118 | + /** |
|
119 | + * Get certificate information. |
|
120 | + * |
|
121 | + * @return TBSCertificate |
|
122 | + */ |
|
123 | + public function tbsCertificate(): TBSCertificate |
|
124 | + { |
|
125 | + return $this->_tbsCertificate; |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Get signature algorithm. |
|
130 | - * |
|
131 | - * @return SignatureAlgorithmIdentifier |
|
132 | - */ |
|
133 | - public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
134 | - { |
|
135 | - return $this->_signatureAlgorithm; |
|
136 | - } |
|
128 | + /** |
|
129 | + * Get signature algorithm. |
|
130 | + * |
|
131 | + * @return SignatureAlgorithmIdentifier |
|
132 | + */ |
|
133 | + public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
|
134 | + { |
|
135 | + return $this->_signatureAlgorithm; |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * Get signature value. |
|
140 | - * |
|
141 | - * @return Signature |
|
142 | - */ |
|
143 | - public function signatureValue(): Signature |
|
144 | - { |
|
145 | - return $this->_signatureValue; |
|
146 | - } |
|
138 | + /** |
|
139 | + * Get signature value. |
|
140 | + * |
|
141 | + * @return Signature |
|
142 | + */ |
|
143 | + public function signatureValue(): Signature |
|
144 | + { |
|
145 | + return $this->_signatureValue; |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * Check whether certificate is self-issued. |
|
150 | - * |
|
151 | - * @return bool |
|
152 | - */ |
|
153 | - public function isSelfIssued(): bool |
|
154 | - { |
|
155 | - return $this->_tbsCertificate->subject()->equals( |
|
156 | - $this->_tbsCertificate->issuer()); |
|
157 | - } |
|
148 | + /** |
|
149 | + * Check whether certificate is self-issued. |
|
150 | + * |
|
151 | + * @return bool |
|
152 | + */ |
|
153 | + public function isSelfIssued(): bool |
|
154 | + { |
|
155 | + return $this->_tbsCertificate->subject()->equals( |
|
156 | + $this->_tbsCertificate->issuer()); |
|
157 | + } |
|
158 | 158 | |
159 | - /** |
|
160 | - * Check whether certificate is semantically equal to another. |
|
161 | - * |
|
162 | - * @param Certificate $cert Certificate to compare to |
|
163 | - * |
|
164 | - * @return bool |
|
165 | - */ |
|
166 | - public function equals(Certificate $cert): bool |
|
167 | - { |
|
168 | - return $this->_hasEqualSerialNumber($cert) && |
|
169 | - $this->_hasEqualPublicKey($cert) && $this->_hasEqualSubject($cert); |
|
170 | - } |
|
159 | + /** |
|
160 | + * Check whether certificate is semantically equal to another. |
|
161 | + * |
|
162 | + * @param Certificate $cert Certificate to compare to |
|
163 | + * |
|
164 | + * @return bool |
|
165 | + */ |
|
166 | + public function equals(Certificate $cert): bool |
|
167 | + { |
|
168 | + return $this->_hasEqualSerialNumber($cert) && |
|
169 | + $this->_hasEqualPublicKey($cert) && $this->_hasEqualSubject($cert); |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * Generate ASN.1 structure. |
|
174 | - * |
|
175 | - * @return Sequence |
|
176 | - */ |
|
177 | - public function toASN1(): Sequence |
|
178 | - { |
|
179 | - return new Sequence($this->_tbsCertificate->toASN1(), |
|
180 | - $this->_signatureAlgorithm->toASN1(), |
|
181 | - $this->_signatureValue->bitString()); |
|
182 | - } |
|
172 | + /** |
|
173 | + * Generate ASN.1 structure. |
|
174 | + * |
|
175 | + * @return Sequence |
|
176 | + */ |
|
177 | + public function toASN1(): Sequence |
|
178 | + { |
|
179 | + return new Sequence($this->_tbsCertificate->toASN1(), |
|
180 | + $this->_signatureAlgorithm->toASN1(), |
|
181 | + $this->_signatureValue->bitString()); |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * Get certificate as a DER. |
|
186 | - * |
|
187 | - * @return string |
|
188 | - */ |
|
189 | - public function toDER(): string |
|
190 | - { |
|
191 | - return $this->toASN1()->toDER(); |
|
192 | - } |
|
184 | + /** |
|
185 | + * Get certificate as a DER. |
|
186 | + * |
|
187 | + * @return string |
|
188 | + */ |
|
189 | + public function toDER(): string |
|
190 | + { |
|
191 | + return $this->toASN1()->toDER(); |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
195 | - * Get certificate as a PEM. |
|
196 | - * |
|
197 | - * @return PEM |
|
198 | - */ |
|
199 | - public function toPEM(): PEM |
|
200 | - { |
|
201 | - return new PEM(PEM::TYPE_CERTIFICATE, $this->toDER()); |
|
202 | - } |
|
194 | + /** |
|
195 | + * Get certificate as a PEM. |
|
196 | + * |
|
197 | + * @return PEM |
|
198 | + */ |
|
199 | + public function toPEM(): PEM |
|
200 | + { |
|
201 | + return new PEM(PEM::TYPE_CERTIFICATE, $this->toDER()); |
|
202 | + } |
|
203 | 203 | |
204 | - /** |
|
205 | - * Verify certificate signature. |
|
206 | - * |
|
207 | - * @param PublicKeyInfo $pubkey_info Issuer's public key |
|
208 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
209 | - * |
|
210 | - * @return bool True if certificate signature is valid |
|
211 | - */ |
|
212 | - public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool |
|
213 | - { |
|
214 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
215 | - $data = $this->_tbsCertificate->toASN1()->toDER(); |
|
216 | - return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
217 | - $this->_signatureAlgorithm); |
|
218 | - } |
|
204 | + /** |
|
205 | + * Verify certificate signature. |
|
206 | + * |
|
207 | + * @param PublicKeyInfo $pubkey_info Issuer's public key |
|
208 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
209 | + * |
|
210 | + * @return bool True if certificate signature is valid |
|
211 | + */ |
|
212 | + public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool |
|
213 | + { |
|
214 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
215 | + $data = $this->_tbsCertificate->toASN1()->toDER(); |
|
216 | + return $crypto->verify($data, $this->_signatureValue, $pubkey_info, |
|
217 | + $this->_signatureAlgorithm); |
|
218 | + } |
|
219 | 219 | |
220 | - /** |
|
221 | - * Check whether certificate has serial number equal to another. |
|
222 | - * |
|
223 | - * @param Certificate $cert |
|
224 | - * |
|
225 | - * @return bool |
|
226 | - */ |
|
227 | - private function _hasEqualSerialNumber(Certificate $cert): bool |
|
228 | - { |
|
229 | - $sn1 = $this->_tbsCertificate->serialNumber(); |
|
230 | - $sn2 = $cert->_tbsCertificate->serialNumber(); |
|
231 | - return $sn1 === $sn2; |
|
232 | - } |
|
220 | + /** |
|
221 | + * Check whether certificate has serial number equal to another. |
|
222 | + * |
|
223 | + * @param Certificate $cert |
|
224 | + * |
|
225 | + * @return bool |
|
226 | + */ |
|
227 | + private function _hasEqualSerialNumber(Certificate $cert): bool |
|
228 | + { |
|
229 | + $sn1 = $this->_tbsCertificate->serialNumber(); |
|
230 | + $sn2 = $cert->_tbsCertificate->serialNumber(); |
|
231 | + return $sn1 === $sn2; |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * Check whether certificate has public key equal to another. |
|
236 | - * |
|
237 | - * @param Certificate $cert |
|
238 | - * |
|
239 | - * @return bool |
|
240 | - */ |
|
241 | - private function _hasEqualPublicKey(Certificate $cert): bool |
|
242 | - { |
|
243 | - $kid1 = $this->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
244 | - $kid2 = $cert->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
245 | - return $kid1 === $kid2; |
|
246 | - } |
|
234 | + /** |
|
235 | + * Check whether certificate has public key equal to another. |
|
236 | + * |
|
237 | + * @param Certificate $cert |
|
238 | + * |
|
239 | + * @return bool |
|
240 | + */ |
|
241 | + private function _hasEqualPublicKey(Certificate $cert): bool |
|
242 | + { |
|
243 | + $kid1 = $this->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
244 | + $kid2 = $cert->_tbsCertificate->subjectPublicKeyInfo()->keyIdentifier(); |
|
245 | + return $kid1 === $kid2; |
|
246 | + } |
|
247 | 247 | |
248 | - /** |
|
249 | - * Check whether certificate has subject equal to another. |
|
250 | - * |
|
251 | - * @param Certificate $cert |
|
252 | - * |
|
253 | - * @return bool |
|
254 | - */ |
|
255 | - private function _hasEqualSubject(Certificate $cert): bool |
|
256 | - { |
|
257 | - $dn1 = $this->_tbsCertificate->subject(); |
|
258 | - $dn2 = $cert->_tbsCertificate->subject(); |
|
259 | - return $dn1->equals($dn2); |
|
260 | - } |
|
248 | + /** |
|
249 | + * Check whether certificate has subject equal to another. |
|
250 | + * |
|
251 | + * @param Certificate $cert |
|
252 | + * |
|
253 | + * @return bool |
|
254 | + */ |
|
255 | + private function _hasEqualSubject(Certificate $cert): bool |
|
256 | + { |
|
257 | + $dn1 = $this->_tbsCertificate->subject(); |
|
258 | + $dn2 = $cert->_tbsCertificate->subject(); |
|
259 | + return $dn1->equals($dn2); |
|
260 | + } |
|
261 | 261 | } |
@@ -17,108 +17,108 @@ |
||
17 | 17 | */ |
18 | 18 | class Time |
19 | 19 | { |
20 | - use DateTimeHelper; |
|
20 | + use DateTimeHelper; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Datetime. |
|
24 | - * |
|
25 | - * @var \DateTimeImmutable |
|
26 | - */ |
|
27 | - protected $_dt; |
|
22 | + /** |
|
23 | + * Datetime. |
|
24 | + * |
|
25 | + * @var \DateTimeImmutable |
|
26 | + */ |
|
27 | + protected $_dt; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Time ASN.1 type tag. |
|
31 | - * |
|
32 | - * @var int |
|
33 | - */ |
|
34 | - protected $_type; |
|
29 | + /** |
|
30 | + * Time ASN.1 type tag. |
|
31 | + * |
|
32 | + * @var int |
|
33 | + */ |
|
34 | + protected $_type; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Constructor. |
|
38 | - * |
|
39 | - * @param \DateTimeImmutable $dt |
|
40 | - */ |
|
41 | - public function __construct(\DateTimeImmutable $dt) |
|
42 | - { |
|
43 | - $this->_dt = $dt; |
|
44 | - $this->_type = self::_determineType($dt); |
|
45 | - } |
|
36 | + /** |
|
37 | + * Constructor. |
|
38 | + * |
|
39 | + * @param \DateTimeImmutable $dt |
|
40 | + */ |
|
41 | + public function __construct(\DateTimeImmutable $dt) |
|
42 | + { |
|
43 | + $this->_dt = $dt; |
|
44 | + $this->_type = self::_determineType($dt); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Initialize from ASN.1. |
|
49 | - * |
|
50 | - * @param TimeType $el |
|
51 | - * |
|
52 | - * @return self |
|
53 | - */ |
|
54 | - public static function fromASN1(TimeType $el): self |
|
55 | - { |
|
56 | - $obj = new self($el->dateTime()); |
|
57 | - $obj->_type = $el->tag(); |
|
58 | - return $obj; |
|
59 | - } |
|
47 | + /** |
|
48 | + * Initialize from ASN.1. |
|
49 | + * |
|
50 | + * @param TimeType $el |
|
51 | + * |
|
52 | + * @return self |
|
53 | + */ |
|
54 | + public static function fromASN1(TimeType $el): self |
|
55 | + { |
|
56 | + $obj = new self($el->dateTime()); |
|
57 | + $obj->_type = $el->tag(); |
|
58 | + return $obj; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Initialize from date string. |
|
63 | - * |
|
64 | - * @param null|string $time |
|
65 | - * @param null|string $tz |
|
66 | - * |
|
67 | - * @return self |
|
68 | - */ |
|
69 | - public static function fromString(?string $time, ?string $tz = null): self |
|
70 | - { |
|
71 | - return new self(self::_createDateTime($time, $tz)); |
|
72 | - } |
|
61 | + /** |
|
62 | + * Initialize from date string. |
|
63 | + * |
|
64 | + * @param null|string $time |
|
65 | + * @param null|string $tz |
|
66 | + * |
|
67 | + * @return self |
|
68 | + */ |
|
69 | + public static function fromString(?string $time, ?string $tz = null): self |
|
70 | + { |
|
71 | + return new self(self::_createDateTime($time, $tz)); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Get datetime. |
|
76 | - * |
|
77 | - * @return \DateTimeImmutable |
|
78 | - */ |
|
79 | - public function dateTime(): \DateTimeImmutable |
|
80 | - { |
|
81 | - return $this->_dt; |
|
82 | - } |
|
74 | + /** |
|
75 | + * Get datetime. |
|
76 | + * |
|
77 | + * @return \DateTimeImmutable |
|
78 | + */ |
|
79 | + public function dateTime(): \DateTimeImmutable |
|
80 | + { |
|
81 | + return $this->_dt; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Generate ASN.1. |
|
86 | - * |
|
87 | - * @throws \UnexpectedValueException |
|
88 | - * |
|
89 | - * @return TimeType |
|
90 | - */ |
|
91 | - public function toASN1(): TimeType |
|
92 | - { |
|
93 | - $dt = $this->_dt; |
|
94 | - switch ($this->_type) { |
|
95 | - case Element::TYPE_UTC_TIME: |
|
96 | - return new UTCTime($dt); |
|
97 | - case Element::TYPE_GENERALIZED_TIME: |
|
98 | - // GeneralizedTime must not contain fractional seconds |
|
99 | - // (rfc5280 4.1.2.5.2) |
|
100 | - if (0 !== intval($dt->format('u'))) { |
|
101 | - // remove fractional seconds (round down) |
|
102 | - $dt = self::_roundDownFractionalSeconds($dt); |
|
103 | - } |
|
104 | - return new GeneralizedTime($dt); |
|
105 | - } |
|
106 | - throw new \UnexpectedValueException( |
|
107 | - 'Time type ' . Element::tagToName($this->_type) . ' not supported.'); |
|
108 | - } |
|
84 | + /** |
|
85 | + * Generate ASN.1. |
|
86 | + * |
|
87 | + * @throws \UnexpectedValueException |
|
88 | + * |
|
89 | + * @return TimeType |
|
90 | + */ |
|
91 | + public function toASN1(): TimeType |
|
92 | + { |
|
93 | + $dt = $this->_dt; |
|
94 | + switch ($this->_type) { |
|
95 | + case Element::TYPE_UTC_TIME: |
|
96 | + return new UTCTime($dt); |
|
97 | + case Element::TYPE_GENERALIZED_TIME: |
|
98 | + // GeneralizedTime must not contain fractional seconds |
|
99 | + // (rfc5280 4.1.2.5.2) |
|
100 | + if (0 !== intval($dt->format('u'))) { |
|
101 | + // remove fractional seconds (round down) |
|
102 | + $dt = self::_roundDownFractionalSeconds($dt); |
|
103 | + } |
|
104 | + return new GeneralizedTime($dt); |
|
105 | + } |
|
106 | + throw new \UnexpectedValueException( |
|
107 | + 'Time type ' . Element::tagToName($this->_type) . ' not supported.'); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Determine whether to use UTCTime or GeneralizedTime ASN.1 type. |
|
112 | - * |
|
113 | - * @param \DateTimeImmutable $dt |
|
114 | - * |
|
115 | - * @return int Type tag |
|
116 | - */ |
|
117 | - protected static function _determineType(\DateTimeImmutable $dt): int |
|
118 | - { |
|
119 | - if ($dt->format('Y') >= 2050) { |
|
120 | - return Element::TYPE_GENERALIZED_TIME; |
|
121 | - } |
|
122 | - return Element::TYPE_UTC_TIME; |
|
123 | - } |
|
110 | + /** |
|
111 | + * Determine whether to use UTCTime or GeneralizedTime ASN.1 type. |
|
112 | + * |
|
113 | + * @param \DateTimeImmutable $dt |
|
114 | + * |
|
115 | + * @return int Type tag |
|
116 | + */ |
|
117 | + protected static function _determineType(\DateTimeImmutable $dt): int |
|
118 | + { |
|
119 | + if ($dt->format('Y') >= 2050) { |
|
120 | + return Element::TYPE_GENERALIZED_TIME; |
|
121 | + } |
|
122 | + return Element::TYPE_UTC_TIME; |
|
123 | + } |
|
124 | 124 | } |
@@ -92,16 +92,16 @@ |
||
92 | 92 | { |
93 | 93 | $dt = $this->_dt; |
94 | 94 | switch ($this->_type) { |
95 | - case Element::TYPE_UTC_TIME: |
|
96 | - return new UTCTime($dt); |
|
97 | - case Element::TYPE_GENERALIZED_TIME: |
|
98 | - // GeneralizedTime must not contain fractional seconds |
|
99 | - // (rfc5280 4.1.2.5.2) |
|
100 | - if (0 !== intval($dt->format('u'))) { |
|
101 | - // remove fractional seconds (round down) |
|
102 | - $dt = self::_roundDownFractionalSeconds($dt); |
|
103 | - } |
|
104 | - return new GeneralizedTime($dt); |
|
95 | + case Element::TYPE_UTC_TIME: |
|
96 | + return new UTCTime($dt); |
|
97 | + case Element::TYPE_GENERALIZED_TIME: |
|
98 | + // GeneralizedTime must not contain fractional seconds |
|
99 | + // (rfc5280 4.1.2.5.2) |
|
100 | + if (0 !== intval($dt->format('u'))) { |
|
101 | + // remove fractional seconds (round down) |
|
102 | + $dt = self::_roundDownFractionalSeconds($dt); |
|
103 | + } |
|
104 | + return new GeneralizedTime($dt); |
|
105 | 105 | } |
106 | 106 | throw new \UnexpectedValueException( |
107 | 107 | 'Time type ' . Element::tagToName($this->_type) . ' not supported.'); |
@@ -27,622 +27,622 @@ |
||
27 | 27 | */ |
28 | 28 | class TBSCertificate |
29 | 29 | { |
30 | - // Certificate version enumerations |
|
31 | - const VERSION_1 = 0; |
|
32 | - const VERSION_2 = 1; |
|
33 | - const VERSION_3 = 2; |
|
34 | - |
|
35 | - /** |
|
36 | - * Certificate version. |
|
37 | - * |
|
38 | - * @var null|int |
|
39 | - */ |
|
40 | - protected $_version; |
|
41 | - |
|
42 | - /** |
|
43 | - * Serial number. |
|
44 | - * |
|
45 | - * @var null|string |
|
46 | - */ |
|
47 | - protected $_serialNumber; |
|
48 | - |
|
49 | - /** |
|
50 | - * Signature algorithm. |
|
51 | - * |
|
52 | - * @var null|SignatureAlgorithmIdentifier |
|
53 | - */ |
|
54 | - protected $_signature; |
|
55 | - |
|
56 | - /** |
|
57 | - * Certificate issuer. |
|
58 | - * |
|
59 | - * @var Name |
|
60 | - */ |
|
61 | - protected $_issuer; |
|
62 | - |
|
63 | - /** |
|
64 | - * Certificate validity period. |
|
65 | - * |
|
66 | - * @var Validity |
|
67 | - */ |
|
68 | - protected $_validity; |
|
69 | - |
|
70 | - /** |
|
71 | - * Certificate subject. |
|
72 | - * |
|
73 | - * @var Name |
|
74 | - */ |
|
75 | - protected $_subject; |
|
76 | - |
|
77 | - /** |
|
78 | - * Subject public key. |
|
79 | - * |
|
80 | - * @var PublicKeyInfo |
|
81 | - */ |
|
82 | - protected $_subjectPublicKeyInfo; |
|
83 | - |
|
84 | - /** |
|
85 | - * Issuer unique identifier. |
|
86 | - * |
|
87 | - * @var null|UniqueIdentifier |
|
88 | - */ |
|
89 | - protected $_issuerUniqueID; |
|
90 | - |
|
91 | - /** |
|
92 | - * Subject unique identifier. |
|
93 | - * |
|
94 | - * @var null|UniqueIdentifier |
|
95 | - */ |
|
96 | - protected $_subjectUniqueID; |
|
97 | - |
|
98 | - /** |
|
99 | - * Extensions. |
|
100 | - * |
|
101 | - * @var Extensions |
|
102 | - */ |
|
103 | - protected $_extensions; |
|
104 | - |
|
105 | - /** |
|
106 | - * Constructor. |
|
107 | - * |
|
108 | - * @param Name $subject Certificate subject |
|
109 | - * @param PublicKeyInfo $pki Subject public key |
|
110 | - * @param Name $issuer Certificate issuer |
|
111 | - * @param Validity $validity Validity period |
|
112 | - */ |
|
113 | - public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer, |
|
114 | - Validity $validity) |
|
115 | - { |
|
116 | - $this->_subject = $subject; |
|
117 | - $this->_subjectPublicKeyInfo = $pki; |
|
118 | - $this->_issuer = $issuer; |
|
119 | - $this->_validity = $validity; |
|
120 | - $this->_extensions = new Extensions(); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Initialize from ASN.1. |
|
125 | - * |
|
126 | - * @param Sequence $seq |
|
127 | - * |
|
128 | - * @return self |
|
129 | - */ |
|
130 | - public static function fromASN1(Sequence $seq): self |
|
131 | - { |
|
132 | - $idx = 0; |
|
133 | - if ($seq->hasTagged(0)) { |
|
134 | - ++$idx; |
|
135 | - $version = $seq->getTagged(0)->asExplicit()->asInteger()->intNumber(); |
|
136 | - } else { |
|
137 | - $version = self::VERSION_1; |
|
138 | - } |
|
139 | - $serial = $seq->at($idx++)->asInteger()->number(); |
|
140 | - $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
141 | - if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
142 | - throw new \UnexpectedValueException( |
|
143 | - 'Unsupported signature algorithm ' . $algo->name() . '.'); |
|
144 | - } |
|
145 | - $issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
146 | - $validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
|
147 | - $subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
148 | - $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
|
149 | - $tbs_cert = new self($subject, $pki, $issuer, $validity); |
|
150 | - $tbs_cert->_version = $version; |
|
151 | - $tbs_cert->_serialNumber = $serial; |
|
152 | - $tbs_cert->_signature = $algo; |
|
153 | - if ($seq->hasTagged(1)) { |
|
154 | - $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
155 | - $seq->getTagged(1)->asImplicit(Element::TYPE_BIT_STRING) |
|
156 | - ->asBitString()); |
|
157 | - } |
|
158 | - if ($seq->hasTagged(2)) { |
|
159 | - $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1( |
|
160 | - $seq->getTagged(2)->asImplicit(Element::TYPE_BIT_STRING) |
|
161 | - ->asBitString()); |
|
162 | - } |
|
163 | - if ($seq->hasTagged(3)) { |
|
164 | - $tbs_cert->_extensions = Extensions::fromASN1( |
|
165 | - $seq->getTagged(3)->asExplicit()->asSequence()); |
|
166 | - } |
|
167 | - return $tbs_cert; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Initialize from certification request. |
|
172 | - * |
|
173 | - * Note that signature is not verified and must be done by the caller. |
|
174 | - * |
|
175 | - * @param CertificationRequest $cr |
|
176 | - * |
|
177 | - * @return self |
|
178 | - */ |
|
179 | - public static function fromCSR(CertificationRequest $cr): self |
|
180 | - { |
|
181 | - $cri = $cr->certificationRequestInfo(); |
|
182 | - $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(), |
|
183 | - Validity::fromStrings(null, null)); |
|
184 | - // if CSR has Extension Request attribute |
|
185 | - if ($cri->hasAttributes()) { |
|
186 | - $attribs = $cri->attributes(); |
|
187 | - if ($attribs->hasExtensionRequest()) { |
|
188 | - $tbs_cert = $tbs_cert->withExtensions( |
|
189 | - $attribs->extensionRequest()->extensions()); |
|
190 | - } |
|
191 | - } |
|
192 | - // add Subject Key Identifier extension |
|
193 | - return $tbs_cert->withAdditionalExtensions( |
|
194 | - new SubjectKeyIdentifierExtension(false, |
|
195 | - $cri->subjectPKInfo()->keyIdentifier())); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Get self with fields set from the issuer's certificate. |
|
200 | - * |
|
201 | - * Issuer shall be set to issuing certificate's subject. |
|
202 | - * Authority key identifier extensions shall be added with a key identifier |
|
203 | - * set to issuing certificate's public key identifier. |
|
204 | - * |
|
205 | - * @param Certificate $cert Issuing party's certificate |
|
206 | - * |
|
207 | - * @return self |
|
208 | - */ |
|
209 | - public function withIssuerCertificate(Certificate $cert): self |
|
210 | - { |
|
211 | - $obj = clone $this; |
|
212 | - // set issuer DN from cert's subject |
|
213 | - $obj->_issuer = $cert->tbsCertificate()->subject(); |
|
214 | - // add authority key identifier extension |
|
215 | - $key_id = $cert->tbsCertificate()->subjectPublicKeyInfo()->keyIdentifier(); |
|
216 | - $obj->_extensions = $obj->_extensions->withExtensions( |
|
217 | - new AuthorityKeyIdentifierExtension(false, $key_id)); |
|
218 | - return $obj; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Get self with given version. |
|
223 | - * |
|
224 | - * If version is not set, appropriate version is automatically |
|
225 | - * determined during signing. |
|
226 | - * |
|
227 | - * @param int $version |
|
228 | - * |
|
229 | - * @return self |
|
230 | - */ |
|
231 | - public function withVersion(int $version): self |
|
232 | - { |
|
233 | - $obj = clone $this; |
|
234 | - $obj->_version = $version; |
|
235 | - return $obj; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Get self with given serial number. |
|
240 | - * |
|
241 | - * @param int|string $serial Base 10 number |
|
242 | - * |
|
243 | - * @return self |
|
244 | - */ |
|
245 | - public function withSerialNumber($serial): self |
|
246 | - { |
|
247 | - $obj = clone $this; |
|
248 | - $obj->_serialNumber = strval($serial); |
|
249 | - return $obj; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Get self with random positive serial number. |
|
254 | - * |
|
255 | - * @param int $size Number of random bytes |
|
256 | - * |
|
257 | - * @return self |
|
258 | - */ |
|
259 | - public function withRandomSerialNumber(int $size = 16): self |
|
260 | - { |
|
261 | - // ensure that first byte is always non-zero and having first bit unset |
|
262 | - $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
263 | - for ($i = 1; $i < $size; ++$i) { |
|
264 | - $num <<= 8; |
|
265 | - $num += mt_rand(0, 0xff); |
|
266 | - } |
|
267 | - return $this->withSerialNumber(gmp_strval($num, 10)); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Get self with given signature algorithm. |
|
272 | - * |
|
273 | - * @param SignatureAlgorithmIdentifier $algo |
|
274 | - * |
|
275 | - * @return self |
|
276 | - */ |
|
277 | - public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
278 | - { |
|
279 | - $obj = clone $this; |
|
280 | - $obj->_signature = $algo; |
|
281 | - return $obj; |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * Get self with given issuer. |
|
286 | - * |
|
287 | - * @param Name $issuer |
|
288 | - * |
|
289 | - * @return self |
|
290 | - */ |
|
291 | - public function withIssuer(Name $issuer): self |
|
292 | - { |
|
293 | - $obj = clone $this; |
|
294 | - $obj->_issuer = $issuer; |
|
295 | - return $obj; |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Get self with given validity. |
|
300 | - * |
|
301 | - * @param Validity $validity |
|
302 | - * |
|
303 | - * @return self |
|
304 | - */ |
|
305 | - public function withValidity(Validity $validity): self |
|
306 | - { |
|
307 | - $obj = clone $this; |
|
308 | - $obj->_validity = $validity; |
|
309 | - return $obj; |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * Get self with given subject. |
|
314 | - * |
|
315 | - * @param Name $subject |
|
316 | - * |
|
317 | - * @return self |
|
318 | - */ |
|
319 | - public function withSubject(Name $subject): self |
|
320 | - { |
|
321 | - $obj = clone $this; |
|
322 | - $obj->_subject = $subject; |
|
323 | - return $obj; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Get self with given subject public key info. |
|
328 | - * |
|
329 | - * @param PublicKeyInfo $pub_key_info |
|
330 | - * |
|
331 | - * @return self |
|
332 | - */ |
|
333 | - public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self |
|
334 | - { |
|
335 | - $obj = clone $this; |
|
336 | - $obj->_subjectPublicKeyInfo = $pub_key_info; |
|
337 | - return $obj; |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Get self with issuer unique ID. |
|
342 | - * |
|
343 | - * @param UniqueIdentifier $id |
|
344 | - * |
|
345 | - * @return self |
|
346 | - */ |
|
347 | - public function withIssuerUniqueID(UniqueIdentifier $id): self |
|
348 | - { |
|
349 | - $obj = clone $this; |
|
350 | - $obj->_issuerUniqueID = $id; |
|
351 | - return $obj; |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Get self with subject unique ID. |
|
356 | - * |
|
357 | - * @param UniqueIdentifier $id |
|
358 | - * |
|
359 | - * @return self |
|
360 | - */ |
|
361 | - public function withSubjectUniqueID(UniqueIdentifier $id): self |
|
362 | - { |
|
363 | - $obj = clone $this; |
|
364 | - $obj->_subjectUniqueID = $id; |
|
365 | - return $obj; |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Get self with given extensions. |
|
370 | - * |
|
371 | - * @param Extensions $extensions |
|
372 | - * |
|
373 | - * @return self |
|
374 | - */ |
|
375 | - public function withExtensions(Extensions $extensions): self |
|
376 | - { |
|
377 | - $obj = clone $this; |
|
378 | - $obj->_extensions = $extensions; |
|
379 | - return $obj; |
|
380 | - } |
|
381 | - |
|
382 | - /** |
|
383 | - * Get self with extensions added. |
|
384 | - * |
|
385 | - * @param Extension ...$exts One or more Extension objects |
|
386 | - * |
|
387 | - * @return self |
|
388 | - */ |
|
389 | - public function withAdditionalExtensions(Extension ...$exts): self |
|
390 | - { |
|
391 | - $obj = clone $this; |
|
392 | - $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
393 | - return $obj; |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Check whether version is set. |
|
398 | - * |
|
399 | - * @return bool |
|
400 | - */ |
|
401 | - public function hasVersion(): bool |
|
402 | - { |
|
403 | - return isset($this->_version); |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * Get certificate version. |
|
408 | - * |
|
409 | - * @throws \LogicException If not set |
|
410 | - * |
|
411 | - * @return int |
|
412 | - */ |
|
413 | - public function version(): int |
|
414 | - { |
|
415 | - if (!$this->hasVersion()) { |
|
416 | - throw new \LogicException('version not set.'); |
|
417 | - } |
|
418 | - return $this->_version; |
|
419 | - } |
|
420 | - |
|
421 | - /** |
|
422 | - * Check whether serial number is set. |
|
423 | - * |
|
424 | - * @return bool |
|
425 | - */ |
|
426 | - public function hasSerialNumber(): bool |
|
427 | - { |
|
428 | - return isset($this->_serialNumber); |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Get serial number. |
|
433 | - * |
|
434 | - * @throws \LogicException If not set |
|
435 | - * |
|
436 | - * @return string Base 10 integer |
|
437 | - */ |
|
438 | - public function serialNumber(): string |
|
439 | - { |
|
440 | - if (!$this->hasSerialNumber()) { |
|
441 | - throw new \LogicException('serialNumber not set.'); |
|
442 | - } |
|
443 | - return $this->_serialNumber; |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Check whether signature algorithm is set. |
|
448 | - * |
|
449 | - * @return bool |
|
450 | - */ |
|
451 | - public function hasSignature(): bool |
|
452 | - { |
|
453 | - return isset($this->_signature); |
|
454 | - } |
|
455 | - |
|
456 | - /** |
|
457 | - * Get signature algorithm. |
|
458 | - * |
|
459 | - * @throws \LogicException If not set |
|
460 | - * |
|
461 | - * @return SignatureAlgorithmIdentifier |
|
462 | - */ |
|
463 | - public function signature(): SignatureAlgorithmIdentifier |
|
464 | - { |
|
465 | - if (!$this->hasSignature()) { |
|
466 | - throw new \LogicException('signature not set.'); |
|
467 | - } |
|
468 | - return $this->_signature; |
|
469 | - } |
|
470 | - |
|
471 | - /** |
|
472 | - * Get issuer. |
|
473 | - * |
|
474 | - * @return Name |
|
475 | - */ |
|
476 | - public function issuer(): Name |
|
477 | - { |
|
478 | - return $this->_issuer; |
|
479 | - } |
|
480 | - |
|
481 | - /** |
|
482 | - * Get validity period. |
|
483 | - * |
|
484 | - * @return Validity |
|
485 | - */ |
|
486 | - public function validity(): Validity |
|
487 | - { |
|
488 | - return $this->_validity; |
|
489 | - } |
|
490 | - |
|
491 | - /** |
|
492 | - * Get subject. |
|
493 | - * |
|
494 | - * @return Name |
|
495 | - */ |
|
496 | - public function subject(): Name |
|
497 | - { |
|
498 | - return $this->_subject; |
|
499 | - } |
|
500 | - |
|
501 | - /** |
|
502 | - * Get subject public key. |
|
503 | - * |
|
504 | - * @return PublicKeyInfo |
|
505 | - */ |
|
506 | - public function subjectPublicKeyInfo(): PublicKeyInfo |
|
507 | - { |
|
508 | - return $this->_subjectPublicKeyInfo; |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * Whether issuer unique identifier is present. |
|
513 | - * |
|
514 | - * @return bool |
|
515 | - */ |
|
516 | - public function hasIssuerUniqueID(): bool |
|
517 | - { |
|
518 | - return isset($this->_issuerUniqueID); |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Get issuerUniqueID. |
|
523 | - * |
|
524 | - * @throws \LogicException If not set |
|
525 | - * |
|
526 | - * @return UniqueIdentifier |
|
527 | - */ |
|
528 | - public function issuerUniqueID(): UniqueIdentifier |
|
529 | - { |
|
530 | - if (!$this->hasIssuerUniqueID()) { |
|
531 | - throw new \LogicException('issuerUniqueID not set.'); |
|
532 | - } |
|
533 | - return $this->_issuerUniqueID; |
|
534 | - } |
|
535 | - |
|
536 | - /** |
|
537 | - * Whether subject unique identifier is present. |
|
538 | - * |
|
539 | - * @return bool |
|
540 | - */ |
|
541 | - public function hasSubjectUniqueID(): bool |
|
542 | - { |
|
543 | - return isset($this->_subjectUniqueID); |
|
544 | - } |
|
545 | - |
|
546 | - /** |
|
547 | - * Get subjectUniqueID. |
|
548 | - * |
|
549 | - * @throws \LogicException If not set |
|
550 | - * |
|
551 | - * @return UniqueIdentifier |
|
552 | - */ |
|
553 | - public function subjectUniqueID(): UniqueIdentifier |
|
554 | - { |
|
555 | - if (!$this->hasSubjectUniqueID()) { |
|
556 | - throw new \LogicException('subjectUniqueID not set.'); |
|
557 | - } |
|
558 | - return $this->_subjectUniqueID; |
|
559 | - } |
|
560 | - |
|
561 | - /** |
|
562 | - * Get extensions. |
|
563 | - * |
|
564 | - * @return Extensions |
|
565 | - */ |
|
566 | - public function extensions(): Extensions |
|
567 | - { |
|
568 | - return $this->_extensions; |
|
569 | - } |
|
570 | - |
|
571 | - /** |
|
572 | - * Generate ASN.1 structure. |
|
573 | - * |
|
574 | - * @return Sequence |
|
575 | - */ |
|
576 | - public function toASN1(): Sequence |
|
577 | - { |
|
578 | - $elements = []; |
|
579 | - $version = $this->version(); |
|
580 | - // if version is not default |
|
581 | - if (self::VERSION_1 !== $version) { |
|
582 | - $elements[] = new ExplicitlyTaggedType(0, new Integer($version)); |
|
583 | - } |
|
584 | - $serial = $this->serialNumber(); |
|
585 | - $signature = $this->signature(); |
|
586 | - // add required elements |
|
587 | - array_push($elements, new Integer($serial), $signature->toASN1(), |
|
588 | - $this->_issuer->toASN1(), $this->_validity->toASN1(), |
|
589 | - $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1()); |
|
590 | - if (isset($this->_issuerUniqueID)) { |
|
591 | - $elements[] = new ImplicitlyTaggedType(1, |
|
592 | - $this->_issuerUniqueID->toASN1()); |
|
593 | - } |
|
594 | - if (isset($this->_subjectUniqueID)) { |
|
595 | - $elements[] = new ImplicitlyTaggedType(2, |
|
596 | - $this->_subjectUniqueID->toASN1()); |
|
597 | - } |
|
598 | - if (count($this->_extensions)) { |
|
599 | - $elements[] = new ExplicitlyTaggedType(3, |
|
600 | - $this->_extensions->toASN1()); |
|
601 | - } |
|
602 | - return new Sequence(...$elements); |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * Create signed certificate. |
|
607 | - * |
|
608 | - * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
609 | - * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
610 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
611 | - * |
|
612 | - * @return Certificate |
|
613 | - */ |
|
614 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
615 | - PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): Certificate |
|
616 | - { |
|
617 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
618 | - $tbs_cert = clone $this; |
|
619 | - if (!isset($tbs_cert->_version)) { |
|
620 | - $tbs_cert->_version = $tbs_cert->_determineVersion(); |
|
621 | - } |
|
622 | - if (!isset($tbs_cert->_serialNumber)) { |
|
623 | - $tbs_cert->_serialNumber = strval(0); |
|
624 | - } |
|
625 | - $tbs_cert->_signature = $algo; |
|
626 | - $data = $tbs_cert->toASN1()->toDER(); |
|
627 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
628 | - return new Certificate($tbs_cert, $algo, $signature); |
|
629 | - } |
|
630 | - |
|
631 | - /** |
|
632 | - * Determine minimum version for the certificate. |
|
633 | - * |
|
634 | - * @return int |
|
635 | - */ |
|
636 | - protected function _determineVersion(): int |
|
637 | - { |
|
638 | - // if extensions are present |
|
639 | - if (count($this->_extensions)) { |
|
640 | - return self::VERSION_3; |
|
641 | - } |
|
642 | - // if UniqueIdentifier is present |
|
643 | - if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) { |
|
644 | - return self::VERSION_2; |
|
645 | - } |
|
646 | - return self::VERSION_1; |
|
647 | - } |
|
30 | + // Certificate version enumerations |
|
31 | + const VERSION_1 = 0; |
|
32 | + const VERSION_2 = 1; |
|
33 | + const VERSION_3 = 2; |
|
34 | + |
|
35 | + /** |
|
36 | + * Certificate version. |
|
37 | + * |
|
38 | + * @var null|int |
|
39 | + */ |
|
40 | + protected $_version; |
|
41 | + |
|
42 | + /** |
|
43 | + * Serial number. |
|
44 | + * |
|
45 | + * @var null|string |
|
46 | + */ |
|
47 | + protected $_serialNumber; |
|
48 | + |
|
49 | + /** |
|
50 | + * Signature algorithm. |
|
51 | + * |
|
52 | + * @var null|SignatureAlgorithmIdentifier |
|
53 | + */ |
|
54 | + protected $_signature; |
|
55 | + |
|
56 | + /** |
|
57 | + * Certificate issuer. |
|
58 | + * |
|
59 | + * @var Name |
|
60 | + */ |
|
61 | + protected $_issuer; |
|
62 | + |
|
63 | + /** |
|
64 | + * Certificate validity period. |
|
65 | + * |
|
66 | + * @var Validity |
|
67 | + */ |
|
68 | + protected $_validity; |
|
69 | + |
|
70 | + /** |
|
71 | + * Certificate subject. |
|
72 | + * |
|
73 | + * @var Name |
|
74 | + */ |
|
75 | + protected $_subject; |
|
76 | + |
|
77 | + /** |
|
78 | + * Subject public key. |
|
79 | + * |
|
80 | + * @var PublicKeyInfo |
|
81 | + */ |
|
82 | + protected $_subjectPublicKeyInfo; |
|
83 | + |
|
84 | + /** |
|
85 | + * Issuer unique identifier. |
|
86 | + * |
|
87 | + * @var null|UniqueIdentifier |
|
88 | + */ |
|
89 | + protected $_issuerUniqueID; |
|
90 | + |
|
91 | + /** |
|
92 | + * Subject unique identifier. |
|
93 | + * |
|
94 | + * @var null|UniqueIdentifier |
|
95 | + */ |
|
96 | + protected $_subjectUniqueID; |
|
97 | + |
|
98 | + /** |
|
99 | + * Extensions. |
|
100 | + * |
|
101 | + * @var Extensions |
|
102 | + */ |
|
103 | + protected $_extensions; |
|
104 | + |
|
105 | + /** |
|
106 | + * Constructor. |
|
107 | + * |
|
108 | + * @param Name $subject Certificate subject |
|
109 | + * @param PublicKeyInfo $pki Subject public key |
|
110 | + * @param Name $issuer Certificate issuer |
|
111 | + * @param Validity $validity Validity period |
|
112 | + */ |
|
113 | + public function __construct(Name $subject, PublicKeyInfo $pki, Name $issuer, |
|
114 | + Validity $validity) |
|
115 | + { |
|
116 | + $this->_subject = $subject; |
|
117 | + $this->_subjectPublicKeyInfo = $pki; |
|
118 | + $this->_issuer = $issuer; |
|
119 | + $this->_validity = $validity; |
|
120 | + $this->_extensions = new Extensions(); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Initialize from ASN.1. |
|
125 | + * |
|
126 | + * @param Sequence $seq |
|
127 | + * |
|
128 | + * @return self |
|
129 | + */ |
|
130 | + public static function fromASN1(Sequence $seq): self |
|
131 | + { |
|
132 | + $idx = 0; |
|
133 | + if ($seq->hasTagged(0)) { |
|
134 | + ++$idx; |
|
135 | + $version = $seq->getTagged(0)->asExplicit()->asInteger()->intNumber(); |
|
136 | + } else { |
|
137 | + $version = self::VERSION_1; |
|
138 | + } |
|
139 | + $serial = $seq->at($idx++)->asInteger()->number(); |
|
140 | + $algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
|
141 | + if (!$algo instanceof SignatureAlgorithmIdentifier) { |
|
142 | + throw new \UnexpectedValueException( |
|
143 | + 'Unsupported signature algorithm ' . $algo->name() . '.'); |
|
144 | + } |
|
145 | + $issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
146 | + $validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
|
147 | + $subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
|
148 | + $pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
|
149 | + $tbs_cert = new self($subject, $pki, $issuer, $validity); |
|
150 | + $tbs_cert->_version = $version; |
|
151 | + $tbs_cert->_serialNumber = $serial; |
|
152 | + $tbs_cert->_signature = $algo; |
|
153 | + if ($seq->hasTagged(1)) { |
|
154 | + $tbs_cert->_issuerUniqueID = UniqueIdentifier::fromASN1( |
|
155 | + $seq->getTagged(1)->asImplicit(Element::TYPE_BIT_STRING) |
|
156 | + ->asBitString()); |
|
157 | + } |
|
158 | + if ($seq->hasTagged(2)) { |
|
159 | + $tbs_cert->_subjectUniqueID = UniqueIdentifier::fromASN1( |
|
160 | + $seq->getTagged(2)->asImplicit(Element::TYPE_BIT_STRING) |
|
161 | + ->asBitString()); |
|
162 | + } |
|
163 | + if ($seq->hasTagged(3)) { |
|
164 | + $tbs_cert->_extensions = Extensions::fromASN1( |
|
165 | + $seq->getTagged(3)->asExplicit()->asSequence()); |
|
166 | + } |
|
167 | + return $tbs_cert; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Initialize from certification request. |
|
172 | + * |
|
173 | + * Note that signature is not verified and must be done by the caller. |
|
174 | + * |
|
175 | + * @param CertificationRequest $cr |
|
176 | + * |
|
177 | + * @return self |
|
178 | + */ |
|
179 | + public static function fromCSR(CertificationRequest $cr): self |
|
180 | + { |
|
181 | + $cri = $cr->certificationRequestInfo(); |
|
182 | + $tbs_cert = new self($cri->subject(), $cri->subjectPKInfo(), new Name(), |
|
183 | + Validity::fromStrings(null, null)); |
|
184 | + // if CSR has Extension Request attribute |
|
185 | + if ($cri->hasAttributes()) { |
|
186 | + $attribs = $cri->attributes(); |
|
187 | + if ($attribs->hasExtensionRequest()) { |
|
188 | + $tbs_cert = $tbs_cert->withExtensions( |
|
189 | + $attribs->extensionRequest()->extensions()); |
|
190 | + } |
|
191 | + } |
|
192 | + // add Subject Key Identifier extension |
|
193 | + return $tbs_cert->withAdditionalExtensions( |
|
194 | + new SubjectKeyIdentifierExtension(false, |
|
195 | + $cri->subjectPKInfo()->keyIdentifier())); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Get self with fields set from the issuer's certificate. |
|
200 | + * |
|
201 | + * Issuer shall be set to issuing certificate's subject. |
|
202 | + * Authority key identifier extensions shall be added with a key identifier |
|
203 | + * set to issuing certificate's public key identifier. |
|
204 | + * |
|
205 | + * @param Certificate $cert Issuing party's certificate |
|
206 | + * |
|
207 | + * @return self |
|
208 | + */ |
|
209 | + public function withIssuerCertificate(Certificate $cert): self |
|
210 | + { |
|
211 | + $obj = clone $this; |
|
212 | + // set issuer DN from cert's subject |
|
213 | + $obj->_issuer = $cert->tbsCertificate()->subject(); |
|
214 | + // add authority key identifier extension |
|
215 | + $key_id = $cert->tbsCertificate()->subjectPublicKeyInfo()->keyIdentifier(); |
|
216 | + $obj->_extensions = $obj->_extensions->withExtensions( |
|
217 | + new AuthorityKeyIdentifierExtension(false, $key_id)); |
|
218 | + return $obj; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Get self with given version. |
|
223 | + * |
|
224 | + * If version is not set, appropriate version is automatically |
|
225 | + * determined during signing. |
|
226 | + * |
|
227 | + * @param int $version |
|
228 | + * |
|
229 | + * @return self |
|
230 | + */ |
|
231 | + public function withVersion(int $version): self |
|
232 | + { |
|
233 | + $obj = clone $this; |
|
234 | + $obj->_version = $version; |
|
235 | + return $obj; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Get self with given serial number. |
|
240 | + * |
|
241 | + * @param int|string $serial Base 10 number |
|
242 | + * |
|
243 | + * @return self |
|
244 | + */ |
|
245 | + public function withSerialNumber($serial): self |
|
246 | + { |
|
247 | + $obj = clone $this; |
|
248 | + $obj->_serialNumber = strval($serial); |
|
249 | + return $obj; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Get self with random positive serial number. |
|
254 | + * |
|
255 | + * @param int $size Number of random bytes |
|
256 | + * |
|
257 | + * @return self |
|
258 | + */ |
|
259 | + public function withRandomSerialNumber(int $size = 16): self |
|
260 | + { |
|
261 | + // ensure that first byte is always non-zero and having first bit unset |
|
262 | + $num = gmp_init(mt_rand(1, 0x7f), 10); |
|
263 | + for ($i = 1; $i < $size; ++$i) { |
|
264 | + $num <<= 8; |
|
265 | + $num += mt_rand(0, 0xff); |
|
266 | + } |
|
267 | + return $this->withSerialNumber(gmp_strval($num, 10)); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Get self with given signature algorithm. |
|
272 | + * |
|
273 | + * @param SignatureAlgorithmIdentifier $algo |
|
274 | + * |
|
275 | + * @return self |
|
276 | + */ |
|
277 | + public function withSignature(SignatureAlgorithmIdentifier $algo): self |
|
278 | + { |
|
279 | + $obj = clone $this; |
|
280 | + $obj->_signature = $algo; |
|
281 | + return $obj; |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * Get self with given issuer. |
|
286 | + * |
|
287 | + * @param Name $issuer |
|
288 | + * |
|
289 | + * @return self |
|
290 | + */ |
|
291 | + public function withIssuer(Name $issuer): self |
|
292 | + { |
|
293 | + $obj = clone $this; |
|
294 | + $obj->_issuer = $issuer; |
|
295 | + return $obj; |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Get self with given validity. |
|
300 | + * |
|
301 | + * @param Validity $validity |
|
302 | + * |
|
303 | + * @return self |
|
304 | + */ |
|
305 | + public function withValidity(Validity $validity): self |
|
306 | + { |
|
307 | + $obj = clone $this; |
|
308 | + $obj->_validity = $validity; |
|
309 | + return $obj; |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * Get self with given subject. |
|
314 | + * |
|
315 | + * @param Name $subject |
|
316 | + * |
|
317 | + * @return self |
|
318 | + */ |
|
319 | + public function withSubject(Name $subject): self |
|
320 | + { |
|
321 | + $obj = clone $this; |
|
322 | + $obj->_subject = $subject; |
|
323 | + return $obj; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Get self with given subject public key info. |
|
328 | + * |
|
329 | + * @param PublicKeyInfo $pub_key_info |
|
330 | + * |
|
331 | + * @return self |
|
332 | + */ |
|
333 | + public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self |
|
334 | + { |
|
335 | + $obj = clone $this; |
|
336 | + $obj->_subjectPublicKeyInfo = $pub_key_info; |
|
337 | + return $obj; |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Get self with issuer unique ID. |
|
342 | + * |
|
343 | + * @param UniqueIdentifier $id |
|
344 | + * |
|
345 | + * @return self |
|
346 | + */ |
|
347 | + public function withIssuerUniqueID(UniqueIdentifier $id): self |
|
348 | + { |
|
349 | + $obj = clone $this; |
|
350 | + $obj->_issuerUniqueID = $id; |
|
351 | + return $obj; |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Get self with subject unique ID. |
|
356 | + * |
|
357 | + * @param UniqueIdentifier $id |
|
358 | + * |
|
359 | + * @return self |
|
360 | + */ |
|
361 | + public function withSubjectUniqueID(UniqueIdentifier $id): self |
|
362 | + { |
|
363 | + $obj = clone $this; |
|
364 | + $obj->_subjectUniqueID = $id; |
|
365 | + return $obj; |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Get self with given extensions. |
|
370 | + * |
|
371 | + * @param Extensions $extensions |
|
372 | + * |
|
373 | + * @return self |
|
374 | + */ |
|
375 | + public function withExtensions(Extensions $extensions): self |
|
376 | + { |
|
377 | + $obj = clone $this; |
|
378 | + $obj->_extensions = $extensions; |
|
379 | + return $obj; |
|
380 | + } |
|
381 | + |
|
382 | + /** |
|
383 | + * Get self with extensions added. |
|
384 | + * |
|
385 | + * @param Extension ...$exts One or more Extension objects |
|
386 | + * |
|
387 | + * @return self |
|
388 | + */ |
|
389 | + public function withAdditionalExtensions(Extension ...$exts): self |
|
390 | + { |
|
391 | + $obj = clone $this; |
|
392 | + $obj->_extensions = $obj->_extensions->withExtensions(...$exts); |
|
393 | + return $obj; |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Check whether version is set. |
|
398 | + * |
|
399 | + * @return bool |
|
400 | + */ |
|
401 | + public function hasVersion(): bool |
|
402 | + { |
|
403 | + return isset($this->_version); |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * Get certificate version. |
|
408 | + * |
|
409 | + * @throws \LogicException If not set |
|
410 | + * |
|
411 | + * @return int |
|
412 | + */ |
|
413 | + public function version(): int |
|
414 | + { |
|
415 | + if (!$this->hasVersion()) { |
|
416 | + throw new \LogicException('version not set.'); |
|
417 | + } |
|
418 | + return $this->_version; |
|
419 | + } |
|
420 | + |
|
421 | + /** |
|
422 | + * Check whether serial number is set. |
|
423 | + * |
|
424 | + * @return bool |
|
425 | + */ |
|
426 | + public function hasSerialNumber(): bool |
|
427 | + { |
|
428 | + return isset($this->_serialNumber); |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Get serial number. |
|
433 | + * |
|
434 | + * @throws \LogicException If not set |
|
435 | + * |
|
436 | + * @return string Base 10 integer |
|
437 | + */ |
|
438 | + public function serialNumber(): string |
|
439 | + { |
|
440 | + if (!$this->hasSerialNumber()) { |
|
441 | + throw new \LogicException('serialNumber not set.'); |
|
442 | + } |
|
443 | + return $this->_serialNumber; |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Check whether signature algorithm is set. |
|
448 | + * |
|
449 | + * @return bool |
|
450 | + */ |
|
451 | + public function hasSignature(): bool |
|
452 | + { |
|
453 | + return isset($this->_signature); |
|
454 | + } |
|
455 | + |
|
456 | + /** |
|
457 | + * Get signature algorithm. |
|
458 | + * |
|
459 | + * @throws \LogicException If not set |
|
460 | + * |
|
461 | + * @return SignatureAlgorithmIdentifier |
|
462 | + */ |
|
463 | + public function signature(): SignatureAlgorithmIdentifier |
|
464 | + { |
|
465 | + if (!$this->hasSignature()) { |
|
466 | + throw new \LogicException('signature not set.'); |
|
467 | + } |
|
468 | + return $this->_signature; |
|
469 | + } |
|
470 | + |
|
471 | + /** |
|
472 | + * Get issuer. |
|
473 | + * |
|
474 | + * @return Name |
|
475 | + */ |
|
476 | + public function issuer(): Name |
|
477 | + { |
|
478 | + return $this->_issuer; |
|
479 | + } |
|
480 | + |
|
481 | + /** |
|
482 | + * Get validity period. |
|
483 | + * |
|
484 | + * @return Validity |
|
485 | + */ |
|
486 | + public function validity(): Validity |
|
487 | + { |
|
488 | + return $this->_validity; |
|
489 | + } |
|
490 | + |
|
491 | + /** |
|
492 | + * Get subject. |
|
493 | + * |
|
494 | + * @return Name |
|
495 | + */ |
|
496 | + public function subject(): Name |
|
497 | + { |
|
498 | + return $this->_subject; |
|
499 | + } |
|
500 | + |
|
501 | + /** |
|
502 | + * Get subject public key. |
|
503 | + * |
|
504 | + * @return PublicKeyInfo |
|
505 | + */ |
|
506 | + public function subjectPublicKeyInfo(): PublicKeyInfo |
|
507 | + { |
|
508 | + return $this->_subjectPublicKeyInfo; |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * Whether issuer unique identifier is present. |
|
513 | + * |
|
514 | + * @return bool |
|
515 | + */ |
|
516 | + public function hasIssuerUniqueID(): bool |
|
517 | + { |
|
518 | + return isset($this->_issuerUniqueID); |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Get issuerUniqueID. |
|
523 | + * |
|
524 | + * @throws \LogicException If not set |
|
525 | + * |
|
526 | + * @return UniqueIdentifier |
|
527 | + */ |
|
528 | + public function issuerUniqueID(): UniqueIdentifier |
|
529 | + { |
|
530 | + if (!$this->hasIssuerUniqueID()) { |
|
531 | + throw new \LogicException('issuerUniqueID not set.'); |
|
532 | + } |
|
533 | + return $this->_issuerUniqueID; |
|
534 | + } |
|
535 | + |
|
536 | + /** |
|
537 | + * Whether subject unique identifier is present. |
|
538 | + * |
|
539 | + * @return bool |
|
540 | + */ |
|
541 | + public function hasSubjectUniqueID(): bool |
|
542 | + { |
|
543 | + return isset($this->_subjectUniqueID); |
|
544 | + } |
|
545 | + |
|
546 | + /** |
|
547 | + * Get subjectUniqueID. |
|
548 | + * |
|
549 | + * @throws \LogicException If not set |
|
550 | + * |
|
551 | + * @return UniqueIdentifier |
|
552 | + */ |
|
553 | + public function subjectUniqueID(): UniqueIdentifier |
|
554 | + { |
|
555 | + if (!$this->hasSubjectUniqueID()) { |
|
556 | + throw new \LogicException('subjectUniqueID not set.'); |
|
557 | + } |
|
558 | + return $this->_subjectUniqueID; |
|
559 | + } |
|
560 | + |
|
561 | + /** |
|
562 | + * Get extensions. |
|
563 | + * |
|
564 | + * @return Extensions |
|
565 | + */ |
|
566 | + public function extensions(): Extensions |
|
567 | + { |
|
568 | + return $this->_extensions; |
|
569 | + } |
|
570 | + |
|
571 | + /** |
|
572 | + * Generate ASN.1 structure. |
|
573 | + * |
|
574 | + * @return Sequence |
|
575 | + */ |
|
576 | + public function toASN1(): Sequence |
|
577 | + { |
|
578 | + $elements = []; |
|
579 | + $version = $this->version(); |
|
580 | + // if version is not default |
|
581 | + if (self::VERSION_1 !== $version) { |
|
582 | + $elements[] = new ExplicitlyTaggedType(0, new Integer($version)); |
|
583 | + } |
|
584 | + $serial = $this->serialNumber(); |
|
585 | + $signature = $this->signature(); |
|
586 | + // add required elements |
|
587 | + array_push($elements, new Integer($serial), $signature->toASN1(), |
|
588 | + $this->_issuer->toASN1(), $this->_validity->toASN1(), |
|
589 | + $this->_subject->toASN1(), $this->_subjectPublicKeyInfo->toASN1()); |
|
590 | + if (isset($this->_issuerUniqueID)) { |
|
591 | + $elements[] = new ImplicitlyTaggedType(1, |
|
592 | + $this->_issuerUniqueID->toASN1()); |
|
593 | + } |
|
594 | + if (isset($this->_subjectUniqueID)) { |
|
595 | + $elements[] = new ImplicitlyTaggedType(2, |
|
596 | + $this->_subjectUniqueID->toASN1()); |
|
597 | + } |
|
598 | + if (count($this->_extensions)) { |
|
599 | + $elements[] = new ExplicitlyTaggedType(3, |
|
600 | + $this->_extensions->toASN1()); |
|
601 | + } |
|
602 | + return new Sequence(...$elements); |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * Create signed certificate. |
|
607 | + * |
|
608 | + * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
609 | + * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
610 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
611 | + * |
|
612 | + * @return Certificate |
|
613 | + */ |
|
614 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
615 | + PrivateKeyInfo $privkey_info, ?Crypto $crypto = null): Certificate |
|
616 | + { |
|
617 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
618 | + $tbs_cert = clone $this; |
|
619 | + if (!isset($tbs_cert->_version)) { |
|
620 | + $tbs_cert->_version = $tbs_cert->_determineVersion(); |
|
621 | + } |
|
622 | + if (!isset($tbs_cert->_serialNumber)) { |
|
623 | + $tbs_cert->_serialNumber = strval(0); |
|
624 | + } |
|
625 | + $tbs_cert->_signature = $algo; |
|
626 | + $data = $tbs_cert->toASN1()->toDER(); |
|
627 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
628 | + return new Certificate($tbs_cert, $algo, $signature); |
|
629 | + } |
|
630 | + |
|
631 | + /** |
|
632 | + * Determine minimum version for the certificate. |
|
633 | + * |
|
634 | + * @return int |
|
635 | + */ |
|
636 | + protected function _determineVersion(): int |
|
637 | + { |
|
638 | + // if extensions are present |
|
639 | + if (count($this->_extensions)) { |
|
640 | + return self::VERSION_3; |
|
641 | + } |
|
642 | + // if UniqueIdentifier is present |
|
643 | + if (isset($this->_issuerUniqueID) || isset($this->_subjectUniqueID)) { |
|
644 | + return self::VERSION_2; |
|
645 | + } |
|
646 | + return self::VERSION_1; |
|
647 | + } |
|
648 | 648 | } |
@@ -14,135 +14,135 @@ |
||
14 | 14 | */ |
15 | 15 | class Targets implements \Countable, \IteratorAggregate |
16 | 16 | { |
17 | - /** |
|
18 | - * Target elements. |
|
19 | - * |
|
20 | - * @var Target[] |
|
21 | - */ |
|
22 | - protected $_targets; |
|
17 | + /** |
|
18 | + * Target elements. |
|
19 | + * |
|
20 | + * @var Target[] |
|
21 | + */ |
|
22 | + protected $_targets; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Constructor. |
|
26 | - * |
|
27 | - * @param Target ...$targets |
|
28 | - */ |
|
29 | - public function __construct(Target ...$targets) |
|
30 | - { |
|
31 | - $this->_targets = $targets; |
|
32 | - } |
|
24 | + /** |
|
25 | + * Constructor. |
|
26 | + * |
|
27 | + * @param Target ...$targets |
|
28 | + */ |
|
29 | + public function __construct(Target ...$targets) |
|
30 | + { |
|
31 | + $this->_targets = $targets; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Initialize from ASN.1. |
|
36 | - * |
|
37 | - * @param Sequence $seq |
|
38 | - * |
|
39 | - * @return self |
|
40 | - */ |
|
41 | - public static function fromASN1(Sequence $seq): self |
|
42 | - { |
|
43 | - $targets = array_map( |
|
44 | - function (UnspecifiedType $el) { |
|
45 | - return Target::fromASN1($el->asTagged()); |
|
46 | - }, $seq->elements()); |
|
47 | - return new self(...$targets); |
|
48 | - } |
|
34 | + /** |
|
35 | + * Initialize from ASN.1. |
|
36 | + * |
|
37 | + * @param Sequence $seq |
|
38 | + * |
|
39 | + * @return self |
|
40 | + */ |
|
41 | + public static function fromASN1(Sequence $seq): self |
|
42 | + { |
|
43 | + $targets = array_map( |
|
44 | + function (UnspecifiedType $el) { |
|
45 | + return Target::fromASN1($el->asTagged()); |
|
46 | + }, $seq->elements()); |
|
47 | + return new self(...$targets); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Get all targets. |
|
52 | - * |
|
53 | - * @return Target[] |
|
54 | - */ |
|
55 | - public function all(): array |
|
56 | - { |
|
57 | - return $this->_targets; |
|
58 | - } |
|
50 | + /** |
|
51 | + * Get all targets. |
|
52 | + * |
|
53 | + * @return Target[] |
|
54 | + */ |
|
55 | + public function all(): array |
|
56 | + { |
|
57 | + return $this->_targets; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Get all name targets. |
|
62 | - * |
|
63 | - * @return Target[] |
|
64 | - */ |
|
65 | - public function nameTargets(): array |
|
66 | - { |
|
67 | - return $this->_allOfType(Target::TYPE_NAME); |
|
68 | - } |
|
60 | + /** |
|
61 | + * Get all name targets. |
|
62 | + * |
|
63 | + * @return Target[] |
|
64 | + */ |
|
65 | + public function nameTargets(): array |
|
66 | + { |
|
67 | + return $this->_allOfType(Target::TYPE_NAME); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Get all group targets. |
|
72 | - * |
|
73 | - * @return Target[] |
|
74 | - */ |
|
75 | - public function groupTargets(): array |
|
76 | - { |
|
77 | - return $this->_allOfType(Target::TYPE_GROUP); |
|
78 | - } |
|
70 | + /** |
|
71 | + * Get all group targets. |
|
72 | + * |
|
73 | + * @return Target[] |
|
74 | + */ |
|
75 | + public function groupTargets(): array |
|
76 | + { |
|
77 | + return $this->_allOfType(Target::TYPE_GROUP); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Check whether given target is present. |
|
82 | - * |
|
83 | - * @param Target $target |
|
84 | - * |
|
85 | - * @return bool |
|
86 | - */ |
|
87 | - public function hasTarget(Target $target): bool |
|
88 | - { |
|
89 | - foreach ($this->_allOfType($target->type()) as $t) { |
|
90 | - if ($target->equals($t)) { |
|
91 | - return true; |
|
92 | - } |
|
93 | - } |
|
94 | - return false; |
|
95 | - } |
|
80 | + /** |
|
81 | + * Check whether given target is present. |
|
82 | + * |
|
83 | + * @param Target $target |
|
84 | + * |
|
85 | + * @return bool |
|
86 | + */ |
|
87 | + public function hasTarget(Target $target): bool |
|
88 | + { |
|
89 | + foreach ($this->_allOfType($target->type()) as $t) { |
|
90 | + if ($target->equals($t)) { |
|
91 | + return true; |
|
92 | + } |
|
93 | + } |
|
94 | + return false; |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Generate ASN.1 structure. |
|
99 | - * |
|
100 | - * @return Sequence |
|
101 | - */ |
|
102 | - public function toASN1(): Sequence |
|
103 | - { |
|
104 | - $elements = array_map( |
|
105 | - function (Target $target) { |
|
106 | - return $target->toASN1(); |
|
107 | - }, $this->_targets); |
|
108 | - return new Sequence(...$elements); |
|
109 | - } |
|
97 | + /** |
|
98 | + * Generate ASN.1 structure. |
|
99 | + * |
|
100 | + * @return Sequence |
|
101 | + */ |
|
102 | + public function toASN1(): Sequence |
|
103 | + { |
|
104 | + $elements = array_map( |
|
105 | + function (Target $target) { |
|
106 | + return $target->toASN1(); |
|
107 | + }, $this->_targets); |
|
108 | + return new Sequence(...$elements); |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * @see \Countable::count() |
|
113 | - * |
|
114 | - * @return int |
|
115 | - */ |
|
116 | - public function count(): int |
|
117 | - { |
|
118 | - return count($this->_targets); |
|
119 | - } |
|
111 | + /** |
|
112 | + * @see \Countable::count() |
|
113 | + * |
|
114 | + * @return int |
|
115 | + */ |
|
116 | + public function count(): int |
|
117 | + { |
|
118 | + return count($this->_targets); |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * Get iterator for targets. |
|
123 | - * |
|
124 | - * @see \IteratorAggregate::getIterator() |
|
125 | - * |
|
126 | - * @return \ArrayIterator |
|
127 | - */ |
|
128 | - public function getIterator(): \ArrayIterator |
|
129 | - { |
|
130 | - return new \ArrayIterator($this->_targets); |
|
131 | - } |
|
121 | + /** |
|
122 | + * Get iterator for targets. |
|
123 | + * |
|
124 | + * @see \IteratorAggregate::getIterator() |
|
125 | + * |
|
126 | + * @return \ArrayIterator |
|
127 | + */ |
|
128 | + public function getIterator(): \ArrayIterator |
|
129 | + { |
|
130 | + return new \ArrayIterator($this->_targets); |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * Get all targets of given type. |
|
135 | - * |
|
136 | - * @param int $type |
|
137 | - * |
|
138 | - * @return Target[] |
|
139 | - */ |
|
140 | - protected function _allOfType(int $type): array |
|
141 | - { |
|
142 | - return array_values( |
|
143 | - array_filter($this->_targets, |
|
144 | - function (Target $target) use ($type) { |
|
145 | - return $target->type() === $type; |
|
146 | - })); |
|
147 | - } |
|
133 | + /** |
|
134 | + * Get all targets of given type. |
|
135 | + * |
|
136 | + * @param int $type |
|
137 | + * |
|
138 | + * @return Target[] |
|
139 | + */ |
|
140 | + protected function _allOfType(int $type): array |
|
141 | + { |
|
142 | + return array_values( |
|
143 | + array_filter($this->_targets, |
|
144 | + function (Target $target) use ($type) { |
|
145 | + return $target->type() === $type; |
|
146 | + })); |
|
147 | + } |
|
148 | 148 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate\Extension\Target; |
6 | 6 | |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | public static function fromASN1(Sequence $seq): self |
42 | 42 | { |
43 | 43 | $targets = array_map( |
44 | - function (UnspecifiedType $el) { |
|
44 | + function(UnspecifiedType $el) { |
|
45 | 45 | return Target::fromASN1($el->asTagged()); |
46 | 46 | }, $seq->elements()); |
47 | 47 | return new self(...$targets); |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | public function toASN1(): Sequence |
103 | 103 | { |
104 | 104 | $elements = array_map( |
105 | - function (Target $target) { |
|
105 | + function(Target $target) { |
|
106 | 106 | return $target->toASN1(); |
107 | 107 | }, $this->_targets); |
108 | 108 | return new Sequence(...$elements); |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | { |
142 | 142 | return array_values( |
143 | 143 | array_filter($this->_targets, |
144 | - function (Target $target) use ($type) { |
|
144 | + function(Target $target) use ($type) { |
|
145 | 145 | return $target->type() === $type; |
146 | 146 | })); |
147 | 147 | } |
@@ -17,164 +17,164 @@ |
||
17 | 17 | */ |
18 | 18 | class PolicyMappingsExtension extends Extension implements \Countable, \IteratorAggregate |
19 | 19 | { |
20 | - /** |
|
21 | - * Policy mappings. |
|
22 | - * |
|
23 | - * @var PolicyMapping[] |
|
24 | - */ |
|
25 | - protected $_mappings; |
|
20 | + /** |
|
21 | + * Policy mappings. |
|
22 | + * |
|
23 | + * @var PolicyMapping[] |
|
24 | + */ |
|
25 | + protected $_mappings; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Constructor. |
|
29 | - * |
|
30 | - * @param bool $critical |
|
31 | - * @param PolicyMapping ...$mappings One or more PolicyMapping objects |
|
32 | - */ |
|
33 | - public function __construct(bool $critical, PolicyMapping ...$mappings) |
|
34 | - { |
|
35 | - parent::__construct(self::OID_POLICY_MAPPINGS, $critical); |
|
36 | - $this->_mappings = $mappings; |
|
37 | - } |
|
27 | + /** |
|
28 | + * Constructor. |
|
29 | + * |
|
30 | + * @param bool $critical |
|
31 | + * @param PolicyMapping ...$mappings One or more PolicyMapping objects |
|
32 | + */ |
|
33 | + public function __construct(bool $critical, PolicyMapping ...$mappings) |
|
34 | + { |
|
35 | + parent::__construct(self::OID_POLICY_MAPPINGS, $critical); |
|
36 | + $this->_mappings = $mappings; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Get all mappings. |
|
41 | - * |
|
42 | - * @return PolicyMapping[] |
|
43 | - */ |
|
44 | - public function mappings(): array |
|
45 | - { |
|
46 | - return $this->_mappings; |
|
47 | - } |
|
39 | + /** |
|
40 | + * Get all mappings. |
|
41 | + * |
|
42 | + * @return PolicyMapping[] |
|
43 | + */ |
|
44 | + public function mappings(): array |
|
45 | + { |
|
46 | + return $this->_mappings; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Get mappings flattened into a single array of arrays of subject domains |
|
51 | - * keyed by issuer domain. |
|
52 | - * |
|
53 | - * Eg. if policy mappings contains multiple mappings with the same issuer |
|
54 | - * domain policy, their corresponding subject domain policies are placed |
|
55 | - * under the same key. |
|
56 | - * |
|
57 | - * @return (string[])[] |
|
58 | - */ |
|
59 | - public function flattenedMappings(): array |
|
60 | - { |
|
61 | - $mappings = []; |
|
62 | - foreach ($this->_mappings as $mapping) { |
|
63 | - $idp = $mapping->issuerDomainPolicy(); |
|
64 | - if (!isset($mappings[$idp])) { |
|
65 | - $mappings[$idp] = []; |
|
66 | - } |
|
67 | - array_push($mappings[$idp], $mapping->subjectDomainPolicy()); |
|
68 | - } |
|
69 | - return $mappings; |
|
70 | - } |
|
49 | + /** |
|
50 | + * Get mappings flattened into a single array of arrays of subject domains |
|
51 | + * keyed by issuer domain. |
|
52 | + * |
|
53 | + * Eg. if policy mappings contains multiple mappings with the same issuer |
|
54 | + * domain policy, their corresponding subject domain policies are placed |
|
55 | + * under the same key. |
|
56 | + * |
|
57 | + * @return (string[])[] |
|
58 | + */ |
|
59 | + public function flattenedMappings(): array |
|
60 | + { |
|
61 | + $mappings = []; |
|
62 | + foreach ($this->_mappings as $mapping) { |
|
63 | + $idp = $mapping->issuerDomainPolicy(); |
|
64 | + if (!isset($mappings[$idp])) { |
|
65 | + $mappings[$idp] = []; |
|
66 | + } |
|
67 | + array_push($mappings[$idp], $mapping->subjectDomainPolicy()); |
|
68 | + } |
|
69 | + return $mappings; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Get all subject domain policy OIDs that are mapped to given issuer |
|
74 | - * domain policy OID. |
|
75 | - * |
|
76 | - * @param string $oid Issuer domain policy |
|
77 | - * |
|
78 | - * @return string[] List of OIDs in dotted format |
|
79 | - */ |
|
80 | - public function issuerMappings(string $oid): array |
|
81 | - { |
|
82 | - $oids = []; |
|
83 | - foreach ($this->_mappings as $mapping) { |
|
84 | - if ($mapping->issuerDomainPolicy() === $oid) { |
|
85 | - $oids[] = $mapping->subjectDomainPolicy(); |
|
86 | - } |
|
87 | - } |
|
88 | - return $oids; |
|
89 | - } |
|
72 | + /** |
|
73 | + * Get all subject domain policy OIDs that are mapped to given issuer |
|
74 | + * domain policy OID. |
|
75 | + * |
|
76 | + * @param string $oid Issuer domain policy |
|
77 | + * |
|
78 | + * @return string[] List of OIDs in dotted format |
|
79 | + */ |
|
80 | + public function issuerMappings(string $oid): array |
|
81 | + { |
|
82 | + $oids = []; |
|
83 | + foreach ($this->_mappings as $mapping) { |
|
84 | + if ($mapping->issuerDomainPolicy() === $oid) { |
|
85 | + $oids[] = $mapping->subjectDomainPolicy(); |
|
86 | + } |
|
87 | + } |
|
88 | + return $oids; |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * Get all mapped issuer domain policy OIDs. |
|
93 | - * |
|
94 | - * @return string[] |
|
95 | - */ |
|
96 | - public function issuerDomainPolicies(): array |
|
97 | - { |
|
98 | - $idps = array_map( |
|
99 | - function (PolicyMapping $mapping) { |
|
100 | - return $mapping->issuerDomainPolicy(); |
|
101 | - }, $this->_mappings); |
|
102 | - return array_values(array_unique($idps)); |
|
103 | - } |
|
91 | + /** |
|
92 | + * Get all mapped issuer domain policy OIDs. |
|
93 | + * |
|
94 | + * @return string[] |
|
95 | + */ |
|
96 | + public function issuerDomainPolicies(): array |
|
97 | + { |
|
98 | + $idps = array_map( |
|
99 | + function (PolicyMapping $mapping) { |
|
100 | + return $mapping->issuerDomainPolicy(); |
|
101 | + }, $this->_mappings); |
|
102 | + return array_values(array_unique($idps)); |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * Check whether policy mappings have anyPolicy mapped. |
|
107 | - * |
|
108 | - * RFC 5280 section 4.2.1.5 states that "Policies MUST NOT be mapped either |
|
109 | - * to or from the special value anyPolicy". |
|
110 | - * |
|
111 | - * @return bool |
|
112 | - */ |
|
113 | - public function hasAnyPolicyMapping(): bool |
|
114 | - { |
|
115 | - foreach ($this->_mappings as $mapping) { |
|
116 | - if (PolicyInformation::OID_ANY_POLICY === $mapping->issuerDomainPolicy()) { |
|
117 | - return true; |
|
118 | - } |
|
119 | - if (PolicyInformation::OID_ANY_POLICY === $mapping->subjectDomainPolicy()) { |
|
120 | - return true; |
|
121 | - } |
|
122 | - } |
|
123 | - return false; |
|
124 | - } |
|
105 | + /** |
|
106 | + * Check whether policy mappings have anyPolicy mapped. |
|
107 | + * |
|
108 | + * RFC 5280 section 4.2.1.5 states that "Policies MUST NOT be mapped either |
|
109 | + * to or from the special value anyPolicy". |
|
110 | + * |
|
111 | + * @return bool |
|
112 | + */ |
|
113 | + public function hasAnyPolicyMapping(): bool |
|
114 | + { |
|
115 | + foreach ($this->_mappings as $mapping) { |
|
116 | + if (PolicyInformation::OID_ANY_POLICY === $mapping->issuerDomainPolicy()) { |
|
117 | + return true; |
|
118 | + } |
|
119 | + if (PolicyInformation::OID_ANY_POLICY === $mapping->subjectDomainPolicy()) { |
|
120 | + return true; |
|
121 | + } |
|
122 | + } |
|
123 | + return false; |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * Get the number of mappings. |
|
128 | - * |
|
129 | - * @see \Countable::count() |
|
130 | - * |
|
131 | - * @return int |
|
132 | - */ |
|
133 | - public function count(): int |
|
134 | - { |
|
135 | - return count($this->_mappings); |
|
136 | - } |
|
126 | + /** |
|
127 | + * Get the number of mappings. |
|
128 | + * |
|
129 | + * @see \Countable::count() |
|
130 | + * |
|
131 | + * @return int |
|
132 | + */ |
|
133 | + public function count(): int |
|
134 | + { |
|
135 | + return count($this->_mappings); |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * Get iterator for policy mappings. |
|
140 | - * |
|
141 | - * @see \IteratorAggregate::getIterator() |
|
142 | - * |
|
143 | - * @return \ArrayIterator |
|
144 | - */ |
|
145 | - public function getIterator(): \ArrayIterator |
|
146 | - { |
|
147 | - return new \ArrayIterator($this->_mappings); |
|
148 | - } |
|
138 | + /** |
|
139 | + * Get iterator for policy mappings. |
|
140 | + * |
|
141 | + * @see \IteratorAggregate::getIterator() |
|
142 | + * |
|
143 | + * @return \ArrayIterator |
|
144 | + */ |
|
145 | + public function getIterator(): \ArrayIterator |
|
146 | + { |
|
147 | + return new \ArrayIterator($this->_mappings); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * {@inheritdoc} |
|
152 | - */ |
|
153 | - protected static function _fromDER(string $data, bool $critical): Extension |
|
154 | - { |
|
155 | - $mappings = array_map( |
|
156 | - function (UnspecifiedType $el) { |
|
157 | - return PolicyMapping::fromASN1($el->asSequence()); |
|
158 | - }, UnspecifiedType::fromDER($data)->asSequence()->elements()); |
|
159 | - if (!count($mappings)) { |
|
160 | - throw new \UnexpectedValueException( |
|
161 | - 'PolicyMappings must have at least one mapping.'); |
|
162 | - } |
|
163 | - return new self($critical, ...$mappings); |
|
164 | - } |
|
150 | + /** |
|
151 | + * {@inheritdoc} |
|
152 | + */ |
|
153 | + protected static function _fromDER(string $data, bool $critical): Extension |
|
154 | + { |
|
155 | + $mappings = array_map( |
|
156 | + function (UnspecifiedType $el) { |
|
157 | + return PolicyMapping::fromASN1($el->asSequence()); |
|
158 | + }, UnspecifiedType::fromDER($data)->asSequence()->elements()); |
|
159 | + if (!count($mappings)) { |
|
160 | + throw new \UnexpectedValueException( |
|
161 | + 'PolicyMappings must have at least one mapping.'); |
|
162 | + } |
|
163 | + return new self($critical, ...$mappings); |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * {@inheritdoc} |
|
168 | - */ |
|
169 | - protected function _valueASN1(): Element |
|
170 | - { |
|
171 | - if (!count($this->_mappings)) { |
|
172 | - throw new \LogicException('No mappings.'); |
|
173 | - } |
|
174 | - $elements = array_map( |
|
175 | - function (PolicyMapping $mapping) { |
|
176 | - return $mapping->toASN1(); |
|
177 | - }, $this->_mappings); |
|
178 | - return new Sequence(...$elements); |
|
179 | - } |
|
166 | + /** |
|
167 | + * {@inheritdoc} |
|
168 | + */ |
|
169 | + protected function _valueASN1(): Element |
|
170 | + { |
|
171 | + if (!count($this->_mappings)) { |
|
172 | + throw new \LogicException('No mappings.'); |
|
173 | + } |
|
174 | + $elements = array_map( |
|
175 | + function (PolicyMapping $mapping) { |
|
176 | + return $mapping->toASN1(); |
|
177 | + }, $this->_mappings); |
|
178 | + return new Sequence(...$elements); |
|
179 | + } |
|
180 | 180 | } |