|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\X509\AttributeCertificate; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
|
8
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
|
9
|
|
|
use Sop\ASN1\Type\Primitive\Integer; |
|
10
|
|
|
use Sop\X509\Certificate\Certificate; |
|
11
|
|
|
use Sop\X509\Certificate\UniqueIdentifier; |
|
12
|
|
|
use Sop\X509\GeneralName\DirectoryName; |
|
13
|
|
|
use Sop\X509\GeneralName\GeneralNames; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Implements *IssuerSerial* ASN.1 type. |
|
17
|
|
|
* |
|
18
|
|
|
* @see https://tools.ietf.org/html/rfc5755#section-4.1 |
|
19
|
|
|
*/ |
|
20
|
|
|
class IssuerSerial |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Issuer name. |
|
24
|
|
|
* |
|
25
|
|
|
* @var GeneralNames |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $_issuer; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Serial number as a base 10 integer. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $_serial; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Issuer unique ID. |
|
38
|
|
|
* |
|
39
|
|
|
* @var null|UniqueIdentifier |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $_issuerUID; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param GeneralNames $issuer |
|
47
|
|
|
* @param int|string $serial |
|
48
|
|
|
* @param null|UniqueIdentifier $uid |
|
49
|
|
|
*/ |
|
50
|
19 |
|
public function __construct(GeneralNames $issuer, $serial, |
|
51
|
|
|
?UniqueIdentifier $uid = null) |
|
52
|
|
|
{ |
|
53
|
19 |
|
$this->_issuer = $issuer; |
|
54
|
19 |
|
$this->_serial = strval($serial); |
|
55
|
19 |
|
$this->_issuerUID = $uid; |
|
56
|
19 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Initialize from ASN.1. |
|
60
|
|
|
* |
|
61
|
|
|
* @param Sequence $seq |
|
62
|
|
|
* |
|
63
|
|
|
* @return self |
|
64
|
|
|
*/ |
|
65
|
9 |
|
public static function fromASN1(Sequence $seq): IssuerSerial |
|
66
|
|
|
{ |
|
67
|
9 |
|
$issuer = GeneralNames::fromASN1($seq->at(0)->asSequence()); |
|
68
|
9 |
|
$serial = $seq->at(1)->asInteger()->number(); |
|
69
|
9 |
|
$uid = null; |
|
70
|
9 |
|
if ($seq->has(2, Element::TYPE_BIT_STRING)) { |
|
71
|
1 |
|
$uid = UniqueIdentifier::fromASN1($seq->at(2)->asBitString()); |
|
72
|
|
|
} |
|
73
|
9 |
|
return new self($issuer, $serial, $uid); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Initialize from a public key certificate. |
|
78
|
|
|
* |
|
79
|
|
|
* @param Certificate $cert |
|
80
|
|
|
* |
|
81
|
|
|
* @return self |
|
82
|
|
|
*/ |
|
83
|
3 |
|
public static function fromPKC(Certificate $cert): IssuerSerial |
|
84
|
|
|
{ |
|
85
|
3 |
|
$tbsCert = $cert->tbsCertificate(); |
|
86
|
3 |
|
$issuer = new GeneralNames(new DirectoryName($tbsCert->issuer())); |
|
87
|
3 |
|
$serial = $tbsCert->serialNumber(); |
|
88
|
3 |
|
$uid = $tbsCert->hasIssuerUniqueID() ? $tbsCert->issuerUniqueID() : null; |
|
89
|
3 |
|
return new self($issuer, $serial, $uid); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get issuer name. |
|
94
|
|
|
* |
|
95
|
|
|
* @return GeneralNames |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function issuer(): GeneralNames |
|
98
|
|
|
{ |
|
99
|
2 |
|
return $this->_issuer; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get serial number. |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function serial(): string |
|
108
|
|
|
{ |
|
109
|
2 |
|
return $this->_serial; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Check whether issuer unique identifier is present. |
|
114
|
|
|
* |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
2 |
|
public function hasIssuerUID(): bool |
|
118
|
|
|
{ |
|
119
|
2 |
|
return isset($this->_issuerUID); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get issuer unique identifier. |
|
124
|
|
|
* |
|
125
|
|
|
* @throws \LogicException If not set |
|
126
|
|
|
* |
|
127
|
|
|
* @return UniqueIdentifier |
|
128
|
|
|
*/ |
|
129
|
2 |
|
public function issuerUID(): UniqueIdentifier |
|
130
|
|
|
{ |
|
131
|
2 |
|
if (!$this->hasIssuerUID()) { |
|
132
|
1 |
|
throw new \LogicException('issuerUID not set.'); |
|
133
|
|
|
} |
|
134
|
1 |
|
return $this->_issuerUID; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Generate ASN.1 structure. |
|
139
|
|
|
* |
|
140
|
|
|
* @return Sequence |
|
141
|
|
|
*/ |
|
142
|
23 |
|
public function toASN1(): Sequence |
|
143
|
|
|
{ |
|
144
|
23 |
|
$elements = [$this->_issuer->toASN1(), new Integer($this->_serial)]; |
|
145
|
23 |
|
if (isset($this->_issuerUID)) { |
|
146
|
1 |
|
$elements[] = $this->_issuerUID->toASN1(); |
|
147
|
|
|
} |
|
148
|
23 |
|
return new Sequence(...$elements); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Check whether this IssuerSerial identifies given certificate. |
|
153
|
|
|
* |
|
154
|
|
|
* @param Certificate $cert |
|
155
|
|
|
* |
|
156
|
|
|
* @return bool |
|
157
|
|
|
*/ |
|
158
|
19 |
|
public function identifiesPKC(Certificate $cert): bool |
|
159
|
|
|
{ |
|
160
|
19 |
|
$tbs = $cert->tbsCertificate(); |
|
161
|
19 |
|
if (!$tbs->issuer()->equals($this->_issuer->firstDN())) { |
|
162
|
3 |
|
return false; |
|
163
|
|
|
} |
|
164
|
16 |
|
if ($tbs->serialNumber() !== $this->_serial) { |
|
165
|
1 |
|
return false; |
|
166
|
|
|
} |
|
167
|
15 |
|
if ($this->_issuerUID && !$this->_checkUniqueID($cert)) { |
|
168
|
2 |
|
return false; |
|
169
|
|
|
} |
|
170
|
13 |
|
return true; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Check whether issuerUID matches given certificate. |
|
175
|
|
|
* |
|
176
|
|
|
* @param Certificate $cert |
|
177
|
|
|
* |
|
178
|
|
|
* @return bool |
|
179
|
|
|
*/ |
|
180
|
3 |
|
private function _checkUniqueID(Certificate $cert): bool |
|
181
|
|
|
{ |
|
182
|
3 |
|
if (!$cert->tbsCertificate()->hasIssuerUniqueID()) { |
|
183
|
1 |
|
return false; |
|
184
|
|
|
} |
|
185
|
2 |
|
$uid = $cert->tbsCertificate()->issuerUniqueID()->string(); |
|
186
|
2 |
|
if ($this->_issuerUID->string() !== $uid) { |
|
187
|
1 |
|
return false; |
|
188
|
|
|
} |
|
189
|
1 |
|
return true; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|