Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\AttributeCertificate\Attribute;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use IteratorAggregate;
11 use LogicException;
12 use SpomkyLabs\Pki\ASN1\Element;
13 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
14 use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType;
15 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
16 use SpomkyLabs\Pki\X501\ASN1\AttributeValue\AttributeValue;
17 use SpomkyLabs\Pki\X501\MatchingRule\BinaryMatch;
18 use SpomkyLabs\Pki\X501\MatchingRule\MatchingRule;
19 use SpomkyLabs\Pki\X509\GeneralName\GeneralNames;
20
21 /**
22 * Base class implementing *IetfAttrSyntax* ASN.1 type used by attribute certificate attribute values.
23 *
24 * @see https://tools.ietf.org/html/rfc5755#section-4.4
25 */
26 abstract class IetfAttrSyntax extends AttributeValue implements Countable, IteratorAggregate
27 {
28 /**
29 * Policy authority.
30 */
31 protected ?GeneralNames $_policyAuthority;
32
33 /**
34 * Values.
35 *
36 * @var IetfAttrValue[]
37 */
38 protected array $_values;
39
40 protected function __construct(string $oid, IetfAttrValue ...$values)
41 {
42 parent::__construct($oid);
43 $this->_policyAuthority = null;
44 $this->_values = $values;
45 }
46
47 abstract public static function create(IetfAttrValue ...$values): self;
48
49 /**
50 * @return self
51 */
52 public static function fromASN1(UnspecifiedType $el): AttributeValue
53 {
54 $seq = $el->asSequence();
55 $authority = null;
56 $idx = 0;
57 if ($seq->hasTagged(0)) {
58 $authority = GeneralNames::fromASN1(
59 $seq->getTagged(0)
60 ->asImplicit(Element::TYPE_SEQUENCE)
61 ->asSequence()
62 );
63 ++$idx;
64 }
65 $values = array_map(IetfAttrValue::fromASN1(...), $seq->at($idx) ->asSequence() ->elements());
66 $obj = static::create(...$values);
67 $obj->_policyAuthority = $authority;
68 return $obj;
69 }
70
71 /**
72 * Get self with policy authority.
73 */
74 public function withPolicyAuthority(GeneralNames $names): self
75 {
76 $obj = clone $this;
77 $obj->_policyAuthority = $names;
78 return $obj;
79 }
80
81 /**
82 * Check whether policy authority is present.
83 */
84 public function hasPolicyAuthority(): bool
85 {
86 return isset($this->_policyAuthority);
87 }
88
89 /**
90 * Get policy authority.
91 */
92 public function policyAuthority(): GeneralNames
93 {
94 if (! $this->hasPolicyAuthority()) {
95 throw new LogicException('policyAuthority not set.');
96 }
97 return $this->_policyAuthority;
98 }
99
100 /**
101 * Get values.
102 *
103 * @return IetfAttrValue[]
104 */
105 public function values(): array
106 {
107 return $this->_values;
108 }
109
110 /**
111 * Get first value.
112 */
113 public function first(): IetfAttrValue
114 {
115 if (count($this->_values) === 0) {
116 throw new LogicException('No values.');
117 }
118 return $this->_values[0];
119 }
120
121 public function toASN1(): Element
122 {
123 $elements = [];
124 if (isset($this->_policyAuthority)) {
125 $elements[] = ImplicitlyTaggedType::create(0, $this->_policyAuthority->toASN1());
126 }
127 $values = array_map(static fn (IetfAttrValue $val) => $val->toASN1(), $this->_values);
128 $elements[] = Sequence::create(...$values);
129 return Sequence::create(...$elements);
130 }
131
132 public function stringValue(): string
133 {
134 return '#' . bin2hex($this->toASN1()->toDER());
135 }
136
137 public function equalityMatchingRule(): MatchingRule
138 {
139 return new BinaryMatch();
140 }
141
142 public function rfc2253String(): string
143 {
144 return $this->stringValue();
145 }
146
147 /**
148 * Get number of values.
149 *
150 * @see \Countable::count()
151 */
152 public function count(): int
153 {
154 return count($this->_values);
155 }
156
157 /**
158 * Get iterator for values.
159 *
160 * @see \IteratorAggregate::getIterator()
161 */
162 public function getIterator(): ArrayIterator
163 {
164 return new ArrayIterator($this->_values);
165 }
166
167 protected function _transcodedString(): string
168 {
169 return $this->stringValue();
170 }
171 }