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