Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate\Extension;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use IteratorAggregate;
11 use SpomkyLabs\Pki\ASN1\Element;
12 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
13 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
14 use SpomkyLabs\Pki\X509\Certificate\Extension\Target\Target;
15 use SpomkyLabs\Pki\X509\Certificate\Extension\Target\Targets;
16
17 /**
18 * Implements 'AC Targeting' certificate extension.
19 *
20 * NOTE**: Syntax is *SEQUENCE OF Targets*, but only one *Targets* element must be used. Multiple *Targets* elements
21 * shall be merged into single *Targets*.
22 *
23 * @see https://tools.ietf.org/html/rfc5755#section-4.3.2
24 */
25 final class TargetInformationExtension extends Extension implements Countable, IteratorAggregate
26 {
27 /**
28 * Targets elements.
29 *
30 * @var Targets[]
31 */
32 protected array $targets;
33
34 /**
35 * Targets[] merged to single Targets.
36 */
37 private ?Targets $merged = null;
38
39 private function __construct(bool $critical, Targets ...$targets)
40 {
41 parent::__construct(self::OID_TARGET_INFORMATION, $critical);
42 $this->targets = $targets;
43 }
44
45 /**
46 * Reset internal state on clone.
47 */
48 public function __clone()
49 {
50 $this->merged = null;
51 }
52
53 public static function create(bool $critical, Targets ...$targets): self
54 {
55 return new self($critical, ...$targets);
56 }
57
58 /**
59 * Initialize from one or more Target objects.
60 *
61 * Extension criticality shall be set to true as specified by RFC 5755.
62 */
63 public static function fromTargets(Target ...$target): self
64 {
65 return self::create(true, Targets::create(...$target));
66 }
67
68 /**
69 * Get all targets.
70 */
71 public function targets(): Targets
72 {
73 if ($this->merged === null) {
74 $a = [];
75 foreach ($this->targets as $targets) {
76 $a = array_merge($a, $targets->all());
77 }
78 $this->merged = Targets::create(...$a);
79 }
80 return $this->merged;
81 }
82
83 /**
84 * Get all name targets.
85 *
86 * @return Target[]
87 */
88 public function names(): array
89 {
90 return $this->targets()
91 ->nameTargets();
92 }
93
94 /**
95 * Get all group targets.
96 *
97 * @return Target[]
98 */
99 public function groups(): array
100 {
101 return $this->targets()
102 ->groupTargets();
103 }
104
105 /**
106 * @see \Countable::count()
107 */
108 public function count(): int
109 {
110 return count($this->targets());
111 }
112
113 /**
114 * Get iterator for targets.
115 *
116 * @see \IteratorAggregate::getIterator()
117 */
118 public function getIterator(): ArrayIterator
119 {
120 return new ArrayIterator($this->targets()->all());
121 }
122
123 protected static function fromDER(string $data, bool $critical): static
124 {
125 $targets = array_map(
126 static fn (UnspecifiedType $el) => Targets::fromASN1($el->asSequence()),
127 UnspecifiedType::fromDER($data)->asSequence()->elements()
128 );
129 return self::create($critical, ...$targets);
130 }
131
132 protected function valueASN1(): Element
133 {
134 $elements = array_map(static fn (Targets $targets) => $targets->toASN1(), $this->targets);
135 return Sequence::create(...$elements);
136 }
137 }