1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\Certificate\Extension; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
8
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
9
|
|
|
use Sop\ASN1\Type\Primitive\Integer; |
10
|
|
|
use Sop\ASN1\Type\Primitive\OctetString; |
11
|
|
|
use Sop\ASN1\Type\Tagged\ImplicitlyTaggedType; |
12
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
13
|
|
|
use Sop\CryptoTypes\Asymmetric\PublicKeyInfo; |
14
|
|
|
use Sop\X509\GeneralName\GeneralNames; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Implements 'Authority Key Identifier' certificate extension. |
18
|
|
|
* |
19
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.1 |
20
|
|
|
*/ |
21
|
|
|
class AuthorityKeyIdentifierExtension extends Extension |
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
|
27 |
|
public function __construct(bool $critical, ?string $keyIdentifier, |
53
|
|
|
?GeneralNames $issuer = null, $serial = null) |
54
|
|
|
{ |
55
|
27 |
|
parent::__construct(self::OID_AUTHORITY_KEY_IDENTIFIER, $critical); |
56
|
27 |
|
$this->_keyIdentifier = $keyIdentifier; |
57
|
27 |
|
$this->_authorityCertIssuer = $issuer; |
58
|
27 |
|
$this->_authorityCertSerialNumber = isset($serial) ? strval($serial) : null; |
59
|
27 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create from public key info. |
63
|
|
|
* |
64
|
|
|
* @param PublicKeyInfo $pki |
65
|
|
|
* |
66
|
|
|
* @return AuthorityKeyIdentifierExtension |
67
|
|
|
*/ |
68
|
1 |
|
public static function fromPublicKeyInfo(PublicKeyInfo $pki): self |
69
|
|
|
{ |
70
|
1 |
|
return new self(false, $pki->keyIdentifier()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Whether key identifier is present. |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
12 |
|
public function hasKeyIdentifier(): bool |
79
|
|
|
{ |
80
|
12 |
|
return isset($this->_keyIdentifier); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get key identifier. |
85
|
|
|
* |
86
|
|
|
* @throws \LogicException If not set |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
12 |
|
public function keyIdentifier(): string |
91
|
|
|
{ |
92
|
12 |
|
if (!$this->hasKeyIdentifier()) { |
93
|
1 |
|
throw new \LogicException('keyIdentifier not set.'); |
94
|
|
|
} |
95
|
11 |
|
return $this->_keyIdentifier; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Whether issuer is present. |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
3 |
|
public function hasIssuer(): bool |
104
|
|
|
{ |
105
|
3 |
|
return isset($this->_authorityCertIssuer); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get issuer. |
110
|
|
|
* |
111
|
|
|
* @throws \LogicException If not set |
112
|
|
|
* |
113
|
|
|
* @return GeneralNames |
114
|
|
|
*/ |
115
|
3 |
|
public function issuer(): GeneralNames |
116
|
|
|
{ |
117
|
3 |
|
if (!$this->hasIssuer()) { |
118
|
1 |
|
throw new \LogicException('authorityCertIssuer not set.'); |
119
|
|
|
} |
120
|
2 |
|
return $this->_authorityCertIssuer; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Whether serial is present. |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
3 |
|
public function hasSerial(): bool |
129
|
|
|
{ |
130
|
3 |
|
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
|
3 |
|
public function serial(): string |
141
|
|
|
{ |
142
|
3 |
|
if (!$this->hasSerial()) { |
143
|
1 |
|
throw new \LogicException('authorityCertSerialNumber not set.'); |
144
|
|
|
} |
145
|
2 |
|
return $this->_authorityCertSerialNumber; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
20 |
|
protected static function _fromDER(string $data, bool $critical): Extension |
152
|
|
|
{ |
153
|
20 |
|
$seq = UnspecifiedType::fromDER($data)->asSequence(); |
154
|
20 |
|
$keyIdentifier = null; |
155
|
20 |
|
$issuer = null; |
156
|
20 |
|
$serial = null; |
157
|
20 |
|
if ($seq->hasTagged(0)) { |
158
|
20 |
|
$keyIdentifier = $seq->getTagged(0) |
159
|
20 |
|
->asImplicit(Element::TYPE_OCTET_STRING) |
160
|
20 |
|
->asOctetString()->string(); |
161
|
|
|
} |
162
|
20 |
|
if ($seq->hasTagged(1) || $seq->hasTagged(2)) { |
163
|
10 |
|
if (!$seq->hasTagged(1) || !$seq->hasTagged(2)) { |
164
|
1 |
|
throw new \UnexpectedValueException( |
165
|
|
|
'AuthorityKeyIdentifier must have both' . |
166
|
|
|
' authorityCertIssuer and authorityCertSerialNumber' . |
167
|
1 |
|
' present or both absent.'); |
168
|
|
|
} |
169
|
9 |
|
$issuer = GeneralNames::fromASN1($seq->getTagged(1) |
170
|
9 |
|
->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
171
|
9 |
|
$serial = $seq->getTagged(2)->asImplicit(Element::TYPE_INTEGER) |
172
|
9 |
|
->asInteger()->number(); |
173
|
|
|
} |
174
|
19 |
|
return new self($critical, $keyIdentifier, $issuer, $serial); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
49 |
|
protected function _valueASN1(): Element |
181
|
|
|
{ |
182
|
49 |
|
$elements = []; |
183
|
49 |
|
if (isset($this->_keyIdentifier)) { |
184
|
49 |
|
$elements[] = new ImplicitlyTaggedType(0, |
185
|
49 |
|
new OctetString($this->_keyIdentifier)); |
186
|
|
|
} |
187
|
|
|
// if either issuer or serial is set, both must be set |
188
|
49 |
|
if (isset($this->_authorityCertIssuer) || |
189
|
49 |
|
isset($this->_authorityCertSerialNumber)) { |
190
|
16 |
|
if (!isset($this->_authorityCertIssuer, |
191
|
16 |
|
$this->_authorityCertSerialNumber)) { |
192
|
1 |
|
throw new \LogicException( |
193
|
|
|
'AuthorityKeyIdentifier must have both' . |
194
|
|
|
' authorityCertIssuer and authorityCertSerialNumber' . |
195
|
1 |
|
' present or both absent.'); |
196
|
|
|
} |
197
|
15 |
|
$elements[] = new ImplicitlyTaggedType(1, |
198
|
15 |
|
$this->_authorityCertIssuer->toASN1()); |
199
|
15 |
|
$elements[] = new ImplicitlyTaggedType(2, |
200
|
15 |
|
new Integer($this->_authorityCertSerialNumber)); |
201
|
|
|
} |
202
|
48 |
|
return new Sequence(...$elements); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|