Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\AttributeCertificate;
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\X509\Certificate\Certificate;
12 use SpomkyLabs\Pki\X509\Certificate\UniqueIdentifier;
13 use SpomkyLabs\Pki\X509\GeneralName\DirectoryName;
14 use SpomkyLabs\Pki\X509\GeneralName\GeneralNames;
15
16 /**
17 * Implements *IssuerSerial* ASN.1 type.
18 *
19 * @see https://tools.ietf.org/html/rfc5755#section-4.1
20 */
21 final class IssuerSerial
22 {
23 private function __construct(
24 private readonly GeneralNames $issuer,
25 private readonly string $serial,
26 private readonly ?UniqueIdentifier $issuerUID
27 ) {
28 }
29
30 public static function create(GeneralNames $issuer, string $serial, ?UniqueIdentifier $issuerUID = null): self
31 {
32 return new self($issuer, $serial, $issuerUID);
33 }
34
35 /**
36 * Initialize from ASN.1.
37 */
38 public static function fromASN1(Sequence $seq): self
39 {
40 $issuer = GeneralNames::fromASN1($seq->at(0)->asSequence());
41 $serial = $seq->at(1)
42 ->asInteger()
43 ->number();
44 $uid = null;
45 if ($seq->has(2, Element::TYPE_BIT_STRING)) {
46 $uid = UniqueIdentifier::fromASN1($seq->at(2)->asBitString());
47 }
48 return self::create($issuer, $serial, $uid);
49 }
50
51 /**
52 * Initialize from a public key certificate.
53 */
54 public static function fromPKC(Certificate $cert): self
55 {
56 $tbsCert = $cert->tbsCertificate();
57 $issuer = GeneralNames::create(DirectoryName::create($tbsCert->issuer()));
58 $serial = $tbsCert->serialNumber();
59 $uid = $tbsCert->hasIssuerUniqueID() ? $tbsCert->issuerUniqueID() : null;
60 return self::create($issuer, $serial, $uid);
61 }
62
63 /**
64 * Get issuer name.
65 */
66 public function issuer(): GeneralNames
67 {
68 return $this->issuer;
69 }
70
71 /**
72 * Get serial number.
73 */
74 public function serial(): string
75 {
76 return $this->serial;
77 }
78
79 /**
80 * Check whether issuer unique identifier is present.
81 */
82 public function hasIssuerUID(): bool
83 {
84 return isset($this->issuerUID);
85 }
86
87 /**
88 * Get issuer unique identifier.
89 */
90 public function issuerUID(): UniqueIdentifier
91 {
92 if (! $this->hasIssuerUID()) {
93 throw new LogicException('issuerUID not set.');
94 }
95 return $this->issuerUID;
96 }
97
98 /**
99 * Generate ASN.1 structure.
100 */
101 public function toASN1(): Sequence
102 {
103 $elements = [$this->issuer->toASN1(), Integer::create($this->serial)];
104 if (isset($this->issuerUID)) {
105 $elements[] = $this->issuerUID->toASN1();
106 }
107 return Sequence::create(...$elements);
108 }
109
110 /**
111 * Check whether this IssuerSerial identifies given certificate.
112 */
113 public function identifiesPKC(Certificate $cert): bool
114 {
115 $tbs = $cert->tbsCertificate();
116 if (! $tbs->issuer()->equals($this->issuer->firstDN())) {
117 return false;
118 }
119 if ($tbs->serialNumber() !== $this->serial) {
120 return false;
121 }
122 if ($this->issuerUID !== null && ! $this->_checkUniqueID($cert)) {
123 return false;
124 }
125 return true;
126 }
127
128 /**
129 * Check whether issuerUID matches given certificate.
130 */
131 private function _checkUniqueID(Certificate $cert): bool
132 {
133 if (! $cert->tbsCertificate()->hasIssuerUniqueID()) {
134 return false;
135 }
136 $uid = $cert->tbsCertificate()
137 ->issuerUniqueID()
138 ->string();
139 return $this->issuerUID?->string() === $uid;
140 }
141 }