Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/Certificate/Extension/AuthorityKeyIdentifierExtension.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate\Extension;
6
7 use LogicException;
8 use SpomkyLabs\Pki\ASN1\Element;
9 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
10 use SpomkyLabs\Pki\ASN1\Type\Primitive\Integer;
11 use SpomkyLabs\Pki\ASN1\Type\Primitive\OctetString;
12 use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType;
13 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
14 use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PublicKeyInfo;
15 use SpomkyLabs\Pki\X509\GeneralName\GeneralNames;
16 use UnexpectedValueException;
17
18 /**
19 * Implements 'Authority Key Identifier' certificate extension.
20 *
21 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.1
22 */
23 final class AuthorityKeyIdentifierExtension extends Extension
24 {
25 private function __construct(
26 bool $critical,
27 private readonly ?string $keyIdentifier,
28 private readonly ?GeneralNames $authorityCertIssuer,
29 private readonly null|string $authorityCertSerialNumber
30 ) {
31 parent::__construct(self::OID_AUTHORITY_KEY_IDENTIFIER, $critical);
32 }
33
34 /**
35 * @param bool $critical Conforming CA's must mark as non-critical (false)
36 * @param null|string $keyIdentifier Key identifier
37 * @param null|GeneralNames $authorityCertIssuer Issuer name
38 */
39 public static function create(
40 bool $critical,
41 ?string $keyIdentifier,
42 ?GeneralNames $authorityCertIssuer = null,
43 null|string $authorityCertSerialNumber = null
44 ): self {
45 return new self($critical, $keyIdentifier, $authorityCertIssuer, $authorityCertSerialNumber);
46 }
47
48 /**
49 * Create from public key info.
50 */
51 public static function fromPublicKeyInfo(PublicKeyInfo $pki): self
52 {
53 return self::create(false, $pki->keyIdentifier());
54 }
55
56 /**
57 * Whether key identifier is present.
58 */
59 public function hasKeyIdentifier(): bool
60 {
61 return isset($this->keyIdentifier);
62 }
63
64 /**
65 * Get key identifier.
66 */
67 public function keyIdentifier(): string
68 {
69 if (! $this->hasKeyIdentifier()) {
70 throw new LogicException('keyIdentifier not set.');
71 }
72 return $this->keyIdentifier;
73 }
74
75 /**
76 * Whether issuer is present.
77 */
78 public function hasIssuer(): bool
79 {
80 return isset($this->authorityCertIssuer);
81 }
82
83 public function issuer(): GeneralNames
84 {
85 if (! $this->hasIssuer()) {
86 throw new LogicException('authorityCertIssuer not set.');
87 }
88 return $this->authorityCertIssuer;
89 }
90
91 /**
92 * Whether serial is present.
93 */
94 public function hasSerial(): bool
95 {
96 return isset($this->authorityCertSerialNumber);
97 }
98
99 /**
100 * Get serial number.
101 *
102 * @return string Base 10 integer string
103 */
104 public function serial(): string
105 {
106 if (! $this->hasSerial()) {
107 throw new LogicException('authorityCertSerialNumber not set.');
108 }
109 return $this->authorityCertSerialNumber;
110 }
111
112 protected static function fromDER(string $data, bool $critical): static
113 {
114 $seq = UnspecifiedType::fromDER($data)->asSequence();
115 $keyIdentifier = null;
116 $issuer = null;
117 $serial = null;
118 if ($seq->hasTagged(0)) {
119 $keyIdentifier = $seq->getTagged(0)
120 ->asImplicit(Element::TYPE_OCTET_STRING)
121 ->asOctetString()
122 ->string();
123 }
124 if ($seq->hasTagged(1) || $seq->hasTagged(2)) {
125 if (! $seq->hasTagged(1) || ! $seq->hasTagged(2)) {
126 throw new UnexpectedValueException(
127 'AuthorityKeyIdentifier must have both' .
128 ' authorityCertIssuer and authorityCertSerialNumber' .
129 ' present or both absent.'
130 );
131 }
132 $issuer = GeneralNames::fromASN1($seq->getTagged(1)->asImplicit(Element::TYPE_SEQUENCE)->asSequence());
133 $serial = $seq->getTagged(2)
134 ->asImplicit(Element::TYPE_INTEGER)
135 ->asInteger()
136 ->number();
137 }
138 return self::create($critical, $keyIdentifier, $issuer, $serial);
139 }
140
141 protected function valueASN1(): Element
142 {
143 $elements = [];
144 if (isset($this->keyIdentifier)) {
145 $elements[] = ImplicitlyTaggedType::create(0, OctetString::create($this->keyIdentifier));
146 }
147 // if either issuer or serial is set, both must be set
148 if (isset($this->authorityCertIssuer) ||
149 isset($this->authorityCertSerialNumber)) {
150 if (! isset($this->authorityCertIssuer,
151 $this->authorityCertSerialNumber)) {
152 throw new LogicException(
153 'AuthorityKeyIdentifier must have both' .
154 ' authorityCertIssuer and authorityCertSerialNumber' .
155 ' present or both absent.'
156 );
157 }
158 $elements[] = ImplicitlyTaggedType::create(1, $this->authorityCertIssuer->toASN1());
159 $elements[] = ImplicitlyTaggedType::create(2, Integer::create($this->authorityCertSerialNumber));
160 }
161 return Sequence::create(...$elements);
162 }
163 }