@@ -20,185 +20,185 @@ |
||
20 | 20 | */ |
21 | 21 | class AuthorityKeyIdentifierExtension extends Extension |
22 | 22 | { |
23 | - /** |
|
24 | - * Key identifier. |
|
25 | - * |
|
26 | - * @var null|string |
|
27 | - */ |
|
28 | - protected $_keyIdentifier; |
|
29 | - |
|
30 | - /** |
|
31 | - * Issuer name. |
|
32 | - * |
|
33 | - * @var null|GeneralNames |
|
34 | - */ |
|
35 | - protected $_authorityCertIssuer; |
|
36 | - |
|
37 | - /** |
|
38 | - * Issuer serial number as a base 10 integer. |
|
39 | - * |
|
40 | - * @var null|string |
|
41 | - */ |
|
42 | - protected $_authorityCertSerialNumber; |
|
43 | - |
|
44 | - /** |
|
45 | - * Constructor. |
|
46 | - * |
|
47 | - * @param bool $critical Conforming CA's must mark as non-critical (false) |
|
48 | - * @param null|string $keyIdentifier Key identifier |
|
49 | - * @param null|GeneralNames $issuer Issuer name |
|
50 | - * @param null|int|string $serial Issuer serial number as a base 10 integer |
|
51 | - */ |
|
52 | - public function __construct(bool $critical, ?string $keyIdentifier, |
|
53 | - ?GeneralNames $issuer = null, $serial = null) |
|
54 | - { |
|
55 | - parent::__construct(self::OID_AUTHORITY_KEY_IDENTIFIER, $critical); |
|
56 | - $this->_keyIdentifier = $keyIdentifier; |
|
57 | - $this->_authorityCertIssuer = $issuer; |
|
58 | - $this->_authorityCertSerialNumber = isset($serial) ? strval($serial) : null; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Create from public key info. |
|
63 | - * |
|
64 | - * @param PublicKeyInfo $pki |
|
65 | - * |
|
66 | - * @return AuthorityKeyIdentifierExtension |
|
67 | - */ |
|
68 | - public static function fromPublicKeyInfo(PublicKeyInfo $pki): self |
|
69 | - { |
|
70 | - return new self(false, $pki->keyIdentifier()); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Whether key identifier is present. |
|
75 | - * |
|
76 | - * @return bool |
|
77 | - */ |
|
78 | - public function hasKeyIdentifier(): bool |
|
79 | - { |
|
80 | - return isset($this->_keyIdentifier); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Get key identifier. |
|
85 | - * |
|
86 | - * @throws \LogicException If not set |
|
87 | - * |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - public function keyIdentifier(): string |
|
91 | - { |
|
92 | - if (!$this->hasKeyIdentifier()) { |
|
93 | - throw new \LogicException('keyIdentifier not set.'); |
|
94 | - } |
|
95 | - return $this->_keyIdentifier; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Whether issuer is present. |
|
100 | - * |
|
101 | - * @return bool |
|
102 | - */ |
|
103 | - public function hasIssuer(): bool |
|
104 | - { |
|
105 | - return isset($this->_authorityCertIssuer); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Get issuer. |
|
110 | - * |
|
111 | - * @throws \LogicException If not set |
|
112 | - * |
|
113 | - * @return GeneralNames |
|
114 | - */ |
|
115 | - public function issuer(): GeneralNames |
|
116 | - { |
|
117 | - if (!$this->hasIssuer()) { |
|
118 | - throw new \LogicException('authorityCertIssuer not set.'); |
|
119 | - } |
|
120 | - return $this->_authorityCertIssuer; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Whether serial is present. |
|
125 | - * |
|
126 | - * @return bool |
|
127 | - */ |
|
128 | - public function hasSerial(): bool |
|
129 | - { |
|
130 | - return isset($this->_authorityCertSerialNumber); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Get serial number. |
|
135 | - * |
|
136 | - * @throws \LogicException If not set |
|
137 | - * |
|
138 | - * @return string Base 10 integer string |
|
139 | - */ |
|
140 | - public function serial(): string |
|
141 | - { |
|
142 | - if (!$this->hasSerial()) { |
|
143 | - throw new \LogicException('authorityCertSerialNumber not set.'); |
|
144 | - } |
|
145 | - return $this->_authorityCertSerialNumber; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * {@inheritdoc} |
|
150 | - */ |
|
151 | - protected static function _fromDER(string $data, bool $critical): Extension |
|
152 | - { |
|
153 | - $seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
154 | - $keyIdentifier = null; |
|
155 | - $issuer = null; |
|
156 | - $serial = null; |
|
157 | - if ($seq->hasTagged(0)) { |
|
158 | - $keyIdentifier = $seq->getTagged(0) |
|
159 | - ->asImplicit(Element::TYPE_OCTET_STRING) |
|
160 | - ->asOctetString()->string(); |
|
161 | - } |
|
162 | - if ($seq->hasTagged(1) || $seq->hasTagged(2)) { |
|
163 | - if (!$seq->hasTagged(1) || !$seq->hasTagged(2)) { |
|
164 | - throw new \UnexpectedValueException( |
|
165 | - 'AuthorityKeyIdentifier must have both' . |
|
166 | - ' authorityCertIssuer and authorityCertSerialNumber' . |
|
167 | - ' present or both absent.'); |
|
168 | - } |
|
169 | - $issuer = GeneralNames::fromASN1($seq->getTagged(1) |
|
170 | - ->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
|
171 | - $serial = $seq->getTagged(2)->asImplicit(Element::TYPE_INTEGER) |
|
172 | - ->asInteger()->number(); |
|
173 | - } |
|
174 | - return new self($critical, $keyIdentifier, $issuer, $serial); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * {@inheritdoc} |
|
179 | - */ |
|
180 | - protected function _valueASN1(): Element |
|
181 | - { |
|
182 | - $elements = []; |
|
183 | - if (isset($this->_keyIdentifier)) { |
|
184 | - $elements[] = new ImplicitlyTaggedType(0, |
|
185 | - new OctetString($this->_keyIdentifier)); |
|
186 | - } |
|
187 | - // if either issuer or serial is set, both must be set |
|
188 | - if (isset($this->_authorityCertIssuer) || |
|
189 | - isset($this->_authorityCertSerialNumber)) { |
|
190 | - if (!isset($this->_authorityCertIssuer, |
|
191 | - $this->_authorityCertSerialNumber)) { |
|
192 | - throw new \LogicException( |
|
193 | - 'AuthorityKeyIdentifier must have both' . |
|
194 | - ' authorityCertIssuer and authorityCertSerialNumber' . |
|
195 | - ' present or both absent.'); |
|
196 | - } |
|
197 | - $elements[] = new ImplicitlyTaggedType(1, |
|
198 | - $this->_authorityCertIssuer->toASN1()); |
|
199 | - $elements[] = new ImplicitlyTaggedType(2, |
|
200 | - new Integer($this->_authorityCertSerialNumber)); |
|
201 | - } |
|
202 | - return new Sequence(...$elements); |
|
203 | - } |
|
23 | + /** |
|
24 | + * Key identifier. |
|
25 | + * |
|
26 | + * @var null|string |
|
27 | + */ |
|
28 | + protected $_keyIdentifier; |
|
29 | + |
|
30 | + /** |
|
31 | + * Issuer name. |
|
32 | + * |
|
33 | + * @var null|GeneralNames |
|
34 | + */ |
|
35 | + protected $_authorityCertIssuer; |
|
36 | + |
|
37 | + /** |
|
38 | + * Issuer serial number as a base 10 integer. |
|
39 | + * |
|
40 | + * @var null|string |
|
41 | + */ |
|
42 | + protected $_authorityCertSerialNumber; |
|
43 | + |
|
44 | + /** |
|
45 | + * Constructor. |
|
46 | + * |
|
47 | + * @param bool $critical Conforming CA's must mark as non-critical (false) |
|
48 | + * @param null|string $keyIdentifier Key identifier |
|
49 | + * @param null|GeneralNames $issuer Issuer name |
|
50 | + * @param null|int|string $serial Issuer serial number as a base 10 integer |
|
51 | + */ |
|
52 | + public function __construct(bool $critical, ?string $keyIdentifier, |
|
53 | + ?GeneralNames $issuer = null, $serial = null) |
|
54 | + { |
|
55 | + parent::__construct(self::OID_AUTHORITY_KEY_IDENTIFIER, $critical); |
|
56 | + $this->_keyIdentifier = $keyIdentifier; |
|
57 | + $this->_authorityCertIssuer = $issuer; |
|
58 | + $this->_authorityCertSerialNumber = isset($serial) ? strval($serial) : null; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Create from public key info. |
|
63 | + * |
|
64 | + * @param PublicKeyInfo $pki |
|
65 | + * |
|
66 | + * @return AuthorityKeyIdentifierExtension |
|
67 | + */ |
|
68 | + public static function fromPublicKeyInfo(PublicKeyInfo $pki): self |
|
69 | + { |
|
70 | + return new self(false, $pki->keyIdentifier()); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Whether key identifier is present. |
|
75 | + * |
|
76 | + * @return bool |
|
77 | + */ |
|
78 | + public function hasKeyIdentifier(): bool |
|
79 | + { |
|
80 | + return isset($this->_keyIdentifier); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Get key identifier. |
|
85 | + * |
|
86 | + * @throws \LogicException If not set |
|
87 | + * |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + public function keyIdentifier(): string |
|
91 | + { |
|
92 | + if (!$this->hasKeyIdentifier()) { |
|
93 | + throw new \LogicException('keyIdentifier not set.'); |
|
94 | + } |
|
95 | + return $this->_keyIdentifier; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Whether issuer is present. |
|
100 | + * |
|
101 | + * @return bool |
|
102 | + */ |
|
103 | + public function hasIssuer(): bool |
|
104 | + { |
|
105 | + return isset($this->_authorityCertIssuer); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Get issuer. |
|
110 | + * |
|
111 | + * @throws \LogicException If not set |
|
112 | + * |
|
113 | + * @return GeneralNames |
|
114 | + */ |
|
115 | + public function issuer(): GeneralNames |
|
116 | + { |
|
117 | + if (!$this->hasIssuer()) { |
|
118 | + throw new \LogicException('authorityCertIssuer not set.'); |
|
119 | + } |
|
120 | + return $this->_authorityCertIssuer; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Whether serial is present. |
|
125 | + * |
|
126 | + * @return bool |
|
127 | + */ |
|
128 | + public function hasSerial(): bool |
|
129 | + { |
|
130 | + return isset($this->_authorityCertSerialNumber); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Get serial number. |
|
135 | + * |
|
136 | + * @throws \LogicException If not set |
|
137 | + * |
|
138 | + * @return string Base 10 integer string |
|
139 | + */ |
|
140 | + public function serial(): string |
|
141 | + { |
|
142 | + if (!$this->hasSerial()) { |
|
143 | + throw new \LogicException('authorityCertSerialNumber not set.'); |
|
144 | + } |
|
145 | + return $this->_authorityCertSerialNumber; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * {@inheritdoc} |
|
150 | + */ |
|
151 | + protected static function _fromDER(string $data, bool $critical): Extension |
|
152 | + { |
|
153 | + $seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
154 | + $keyIdentifier = null; |
|
155 | + $issuer = null; |
|
156 | + $serial = null; |
|
157 | + if ($seq->hasTagged(0)) { |
|
158 | + $keyIdentifier = $seq->getTagged(0) |
|
159 | + ->asImplicit(Element::TYPE_OCTET_STRING) |
|
160 | + ->asOctetString()->string(); |
|
161 | + } |
|
162 | + if ($seq->hasTagged(1) || $seq->hasTagged(2)) { |
|
163 | + if (!$seq->hasTagged(1) || !$seq->hasTagged(2)) { |
|
164 | + throw new \UnexpectedValueException( |
|
165 | + 'AuthorityKeyIdentifier must have both' . |
|
166 | + ' authorityCertIssuer and authorityCertSerialNumber' . |
|
167 | + ' present or both absent.'); |
|
168 | + } |
|
169 | + $issuer = GeneralNames::fromASN1($seq->getTagged(1) |
|
170 | + ->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
|
171 | + $serial = $seq->getTagged(2)->asImplicit(Element::TYPE_INTEGER) |
|
172 | + ->asInteger()->number(); |
|
173 | + } |
|
174 | + return new self($critical, $keyIdentifier, $issuer, $serial); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * {@inheritdoc} |
|
179 | + */ |
|
180 | + protected function _valueASN1(): Element |
|
181 | + { |
|
182 | + $elements = []; |
|
183 | + if (isset($this->_keyIdentifier)) { |
|
184 | + $elements[] = new ImplicitlyTaggedType(0, |
|
185 | + new OctetString($this->_keyIdentifier)); |
|
186 | + } |
|
187 | + // if either issuer or serial is set, both must be set |
|
188 | + if (isset($this->_authorityCertIssuer) || |
|
189 | + isset($this->_authorityCertSerialNumber)) { |
|
190 | + if (!isset($this->_authorityCertIssuer, |
|
191 | + $this->_authorityCertSerialNumber)) { |
|
192 | + throw new \LogicException( |
|
193 | + 'AuthorityKeyIdentifier must have both' . |
|
194 | + ' authorityCertIssuer and authorityCertSerialNumber' . |
|
195 | + ' present or both absent.'); |
|
196 | + } |
|
197 | + $elements[] = new ImplicitlyTaggedType(1, |
|
198 | + $this->_authorityCertIssuer->toASN1()); |
|
199 | + $elements[] = new ImplicitlyTaggedType(2, |
|
200 | + new Integer($this->_authorityCertSerialNumber)); |
|
201 | + } |
|
202 | + return new Sequence(...$elements); |
|
203 | + } |
|
204 | 204 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\X509\Certificate\Extension; |
6 | 6 |