Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate\Extension\DistributionPoint;
6
7 use LogicException;
8 use SpomkyLabs\Pki\ASN1\Element;
9 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
10 use SpomkyLabs\Pki\ASN1\Type\Tagged\ExplicitlyTaggedType;
11 use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType;
12 use SpomkyLabs\Pki\X509\GeneralName\GeneralNames;
13
14 /**
15 * Implements *DistributionPoint* ASN.1 type used by 'CRL Distribution Points' certificate extension.
16 *
17 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.13
18 */
19 final class DistributionPoint
20 {
21 private function __construct(
22 private readonly ?DistributionPointName $distributionPoint,
23 private readonly ?ReasonFlags $reasons,
24 private readonly ?GeneralNames $issuer
25 ) {
26 }
27
28 public static function create(
29 ?DistributionPointName $distributionPoint = null,
30 ?ReasonFlags $reasons = null,
31 ?GeneralNames $issuer = null
32 ): self {
33 return new self($distributionPoint, $reasons, $issuer);
34 }
35
36 /**
37 * Initialize from ASN.1.
38 */
39 public static function fromASN1(Sequence $seq): self
40 {
41 $name = null;
42 $reasons = null;
43 $issuer = null;
44 if ($seq->hasTagged(0)) {
45 // promoted to explicit tagging because underlying type is CHOICE
46 $name = DistributionPointName::fromTaggedType($seq->getTagged(0)->asExplicit()->asTagged());
47 }
48 if ($seq->hasTagged(1)) {
49 $reasons = ReasonFlags::fromASN1(
50 $seq->getTagged(1)
51 ->asImplicit(Element::TYPE_BIT_STRING)
52 ->asBitString()
53 );
54 }
55 if ($seq->hasTagged(2)) {
56 $issuer = GeneralNames::fromASN1(
57 $seq->getTagged(2)
58 ->asImplicit(Element::TYPE_SEQUENCE)
59 ->asSequence()
60 );
61 }
62 return self::create($name, $reasons, $issuer);
63 }
64
65 /**
66 * Check whether distribution point name is set.
67 */
68 public function hasDistributionPointName(): bool
69 {
70 return isset($this->distributionPoint);
71 }
72
73 /**
74 * Get distribution point name.
75 */
76 public function distributionPointName(): DistributionPointName
77 {
78 if (! $this->hasDistributionPointName()) {
79 throw new LogicException('distributionPoint not set.');
80 }
81 return $this->distributionPoint;
82 }
83
84 /**
85 * Check whether distribution point name is set, and it's a full name.
86 */
87 public function hasFullName(): bool
88 {
89 return $this->distributionPointName()
90 ->tag() ===
91 DistributionPointName::TAG_FULL_NAME;
92 }
93
94 /**
95 * Get full distribution point name.
96 */
97 public function fullName(): FullName
98 {
99 if (! $this->distributionPoint instanceof FullName || ! $this->hasFullName()) {
100 throw new LogicException('fullName not set.');
101 }
102 return $this->distributionPoint;
103 }
104
105 /**
106 * Check whether distribution point name is set and it's a relative name.
107 */
108 public function hasRelativeName(): bool
109 {
110 return $this->distributionPointName()
111 ->tag() ===
112 DistributionPointName::TAG_RDN;
113 }
114
115 /**
116 * Get relative distribution point name.
117 */
118 public function relativeName(): RelativeName
119 {
120 if (! $this->distributionPoint instanceof RelativeName || ! $this->hasRelativeName()) {
121 throw new LogicException('nameRelativeToCRLIssuer not set.');
122 }
123 return $this->distributionPoint;
124 }
125
126 /**
127 * Check whether reasons flags is set.
128 */
129 public function hasReasons(): bool
130 {
131 return isset($this->reasons);
132 }
133
134 /**
135 * Get revocation reason flags.
136 */
137 public function reasons(): ReasonFlags
138 {
139 if (! $this->hasReasons()) {
140 throw new LogicException('reasons not set.');
141 }
142 return $this->reasons;
143 }
144
145 /**
146 * Check whether cRLIssuer is set.
147 */
148 public function hasCRLIssuer(): bool
149 {
150 return isset($this->issuer);
151 }
152
153 /**
154 * Get CRL issuer.
155 */
156 public function crlIssuer(): GeneralNames
157 {
158 if (! $this->hasCRLIssuer()) {
159 throw new LogicException('crlIssuer not set.');
160 }
161 return $this->issuer;
162 }
163
164 /**
165 * Generate ASN.1 structure.
166 */
167 public function toASN1(): Sequence
168 {
169 $elements = [];
170 if (isset($this->distributionPoint)) {
171 $elements[] = ExplicitlyTaggedType::create(0, $this->distributionPoint->toASN1());
172 }
173 if (isset($this->reasons)) {
174 $elements[] = ImplicitlyTaggedType::create(1, $this->reasons->toASN1());
175 }
176 if (isset($this->issuer)) {
177 $elements[] = ImplicitlyTaggedType::create(2, $this->issuer->toASN1());
178 }
179 return Sequence::create(...$elements);
180 }
181 }