Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/AttributeCertificate/AttributeCertificateInfo.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\AttributeCertificate;
6
7 use Brick\Math\BigInteger;
8 use function count;
9 use LogicException;
10 use SpomkyLabs\Pki\ASN1\Element;
11 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
12 use SpomkyLabs\Pki\ASN1\Type\Primitive\Integer;
13 use SpomkyLabs\Pki\CryptoBridge\Crypto;
14 use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier;
15 use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier;
16 use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PrivateKeyInfo;
17 use SpomkyLabs\Pki\X509\Certificate\Extension\Extension;
18 use SpomkyLabs\Pki\X509\Certificate\Extensions;
19 use SpomkyLabs\Pki\X509\Certificate\UniqueIdentifier;
20 use function strval;
21 use UnexpectedValueException;
22
23 /**
24 * Implements *AttributeCertificateInfo* ASN.1 type.
25 *
26 * @see https://tools.ietf.org/html/rfc5755#section-4.1
27 */
28 final class AttributeCertificateInfo
29 {
30 final public const VERSION_2 = 1;
31
32 /**
33 * AC version.
34 */
35 private readonly int $version;
36
37 /**
38 * Signature algorithm identifier.
39 */
40 private ?SignatureAlgorithmIdentifier $signature = null;
41
42 /**
43 * AC serial number as a base 10 integer.
44 */
45 private ?string $serialNumber = null;
46
47 /**
48 * Issuer unique identifier.
49 */
50 private ?UniqueIdentifier $issuerUniqueID = null;
51
52 /**
53 * Extensions.
54 */
55 private Extensions $extensions;
56
57 /**
58 * @param Holder $holder AC holder
59 * @param AttCertIssuer $issuer AC issuer
60 * @param AttCertValidityPeriod $attrCertValidityPeriod Validity
61 * @param Attributes $attributes Attributes
62 */
63 private function __construct(
64 private Holder $holder,
65 private AttCertIssuer $issuer,
66 private AttCertValidityPeriod $attrCertValidityPeriod,
67 private Attributes $attributes
68 ) {
69 $this->version = self::VERSION_2;
70 $this->extensions = Extensions::create();
71 }
72
73 public static function create(
74 Holder $holder,
75 AttCertIssuer $issuer,
76 AttCertValidityPeriod $attrCertValidityPeriod,
77 Attributes $attributes
78 ): self {
79 return new self($holder, $issuer, $attrCertValidityPeriod, $attributes);
80 }
81
82 /**
83 * Initialize from ASN.1.
84 */
85 public static function fromASN1(Sequence $seq): self
86 {
87 $idx = 0;
88 $version = $seq->at($idx++)
89 ->asInteger()
90 ->intNumber();
91 if ($version !== self::VERSION_2) {
92 throw new UnexpectedValueException('Version must be 2.');
93 }
94 $holder = Holder::fromASN1($seq->at($idx++)->asSequence());
95 $issuer = AttCertIssuer::fromASN1($seq->at($idx++));
96 $signature = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence());
97 if (! $signature instanceof SignatureAlgorithmIdentifier) {
98 throw new UnexpectedValueException('Unsupported signature algorithm ' . $signature->oid() . '.');
99 }
100 $serial = $seq->at($idx++)
101 ->asInteger()
102 ->number();
103 $validity = AttCertValidityPeriod::fromASN1($seq->at($idx++)->asSequence());
104 $attribs = Attributes::fromASN1($seq->at($idx++)->asSequence());
105 $obj = self::create($holder, $issuer, $validity, $attribs);
106 $obj->signature = $signature;
107 $obj->serialNumber = $serial;
108 if ($seq->has($idx, Element::TYPE_BIT_STRING)) {
109 $obj->issuerUniqueID = UniqueIdentifier::fromASN1($seq->at($idx++)->asBitString());
110 }
111 if ($seq->has($idx, Element::TYPE_SEQUENCE)) {
112 $obj->extensions = Extensions::fromASN1($seq->at($idx++)->asSequence());
113 }
114 return $obj;
115 }
116
117 /**
118 * Get self with holder.
119 */
120 public function withHolder(Holder $holder): self
121 {
122 $obj = clone $this;
123 $obj->holder = $holder;
124 return $obj;
125 }
126
127 /**
128 * Get self with issuer.
129 */
130 public function withIssuer(AttCertIssuer $issuer): self
131 {
132 $obj = clone $this;
133 $obj->issuer = $issuer;
134 return $obj;
135 }
136
137 /**
138 * Get self with signature algorithm identifier.
139 */
140 public function withSignature(SignatureAlgorithmIdentifier $algo): self
141 {
142 $obj = clone $this;
143 $obj->signature = $algo;
144 return $obj;
145 }
146
147 /**
148 * Get self with serial number.
149 *
150 * @param int|string $serial Base 10 serial number
151 */
152 public function withSerialNumber(int|string $serial): self
153 {
154 $obj = clone $this;
155 $obj->serialNumber = strval($serial);
156 return $obj;
157 }
158
159 /**
160 * Get self with random positive serial number.
161 *
162 * @param int $size Number of random bytes
163 */
164 public function withRandomSerialNumber(int $size): self
165 {
166 // ensure that first byte is always non-zero and having first bit unset
167 $num = BigInteger::of(random_int(1, 0x7f));
168 for ($i = 1; $i < $size; ++$i) {
169 $num = $num->shiftedLeft(8);
170 $num = $num->plus(random_int(0, 0xff));
171 }
172 return $this->withSerialNumber($num->toBase(10));
173 }
174
175 /**
176 * Get self with validity period.
177 */
178 public function withValidity(AttCertValidityPeriod $validity): self
179 {
180 $obj = clone $this;
181 $obj->attrCertValidityPeriod = $validity;
182 return $obj;
183 }
184
185 /**
186 * Get self with attributes.
187 */
188 public function withAttributes(Attributes $attribs): self
189 {
190 $obj = clone $this;
191 $obj->attributes = $attribs;
192 return $obj;
193 }
194
195 /**
196 * Get self with issuer unique identifier.
197 */
198 public function withIssuerUniqueID(UniqueIdentifier $uid): self
199 {
200 $obj = clone $this;
201 $obj->issuerUniqueID = $uid;
202 return $obj;
203 }
204
205 /**
206 * Get self with extensions.
207 */
208 public function withExtensions(Extensions $extensions): self
209 {
210 $obj = clone $this;
211 $obj->extensions = $extensions;
212 return $obj;
213 }
214
215 /**
216 * Get self with extensions added.
217 *
218 * @param Extension ...$exts One or more Extension objects
219 */
220 public function withAdditionalExtensions(Extension ...$exts): self
221 {
222 $obj = clone $this;
223 $obj->extensions = $obj->extensions->withExtensions(...$exts);
224 return $obj;
225 }
226
227 public function version(): int
228 {
229 return $this->version;
230 }
231
232 /**
233 * Get AC holder.
234 */
235 public function holder(): Holder
236 {
237 return $this->holder;
238 }
239
240 /**
241 * Get AC issuer.
242 */
243 public function issuer(): AttCertIssuer
244 {
245 return $this->issuer;
246 }
247
248 /**
249 * Check whether signature is set.
250 */
251 public function hasSignature(): bool
252 {
253 return $this->signature !== null;
254 }
255
256 /**
257 * Get signature algorithm identifier.
258 */
259 public function signature(): SignatureAlgorithmIdentifier
260 {
261 if (! $this->hasSignature()) {
262 throw new LogicException('signature not set.');
263 }
264 return $this->signature;
265 }
266
267 /**
268 * Check whether serial number is present.
269 */
270 public function hasSerialNumber(): bool
271 {
272 return isset($this->serialNumber);
273 }
274
275 /**
276 * Get AC serial number as a base 10 integer.
277 */
278 public function serialNumber(): string
279 {
280 if (! $this->hasSerialNumber()) {
281 throw new LogicException('serialNumber not set.');
282 }
283 return $this->serialNumber;
284 }
285
286 /**
287 * Get validity period.
288 */
289 public function validityPeriod(): AttCertValidityPeriod
290 {
291 return $this->attrCertValidityPeriod;
292 }
293
294 public function attributes(): Attributes
295 {
296 return $this->attributes;
297 }
298
299 /**
300 * Check whether issuer unique identifier is present.
301 */
302 public function hasIssuerUniqueID(): bool
303 {
304 return isset($this->issuerUniqueID);
305 }
306
307 /**
308 * Get issuer unique identifier.
309 */
310 public function issuerUniqueID(): UniqueIdentifier
311 {
312 if (! $this->hasIssuerUniqueID()) {
313 throw new LogicException('issuerUniqueID not set.');
314 }
315 return $this->issuerUniqueID;
316 }
317
318 public function extensions(): Extensions
319 {
320 return $this->extensions;
321 }
322
323 /**
324 * Get ASN.1 structure.
325 */
326 public function toASN1(): Sequence
327 {
328 $elements = [Integer::create($this->version), $this->holder->toASN1(),
329 $this->issuer->toASN1(), $this->signature()
330 ->toASN1(),
331 Integer::create($this->serialNumber()),
332 $this->attrCertValidityPeriod->toASN1(),
333 $this->attributes->toASN1(), ];
334 if (isset($this->issuerUniqueID)) {
335 $elements[] = $this->issuerUniqueID->toASN1();
336 }
337 if (count($this->extensions) !== 0) {
338 $elements[] = $this->extensions->toASN1();
339 }
340 return Sequence::create(...$elements);
341 }
342
343 /**
344 * Create signed attribute certificate.
345 *
346 * @param SignatureAlgorithmIdentifier $algo Signature algorithm
347 * @param PrivateKeyInfo $privkey_info Private key
348 * @param null|Crypto $crypto Crypto engine, use default if not set
349 */
350 public function sign(
351 SignatureAlgorithmIdentifier $algo,
352 PrivateKeyInfo $privkey_info,
353 ?Crypto $crypto = null
354 ): AttributeCertificate {
355 $crypto ??= Crypto::getDefault();
356 $aci = clone $this;
357 if (! isset($aci->serialNumber)) {
358 $aci->serialNumber = '0';
359 }
360 $aci->signature = $algo;
361 $data = $aci->toASN1()
362 ->toDER();
363 $signature = $crypto->sign($data, $privkey_info, $algo);
364 return AttributeCertificate::create($aci, $algo, $signature);
365 }
366 }