|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\X509\Certificate\Extension\NameConstraints; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
|
8
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Implements *GeneralSubtrees* ASN.1 type used by 'Name Constraints' |
|
12
|
|
|
* certificate extension. |
|
13
|
|
|
* |
|
14
|
|
|
* @see @link https://tools.ietf.org/html/rfc5280#section-4.2.1.10 |
|
15
|
|
|
*/ |
|
16
|
|
|
class GeneralSubtrees implements \Countable, \IteratorAggregate |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Subtrees. |
|
20
|
|
|
* |
|
21
|
|
|
* @var GeneralSubtree[] |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $_subtrees; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param GeneralSubtree ...$subtrees |
|
29
|
|
|
*/ |
|
30
|
14 |
|
public function __construct(GeneralSubtree ...$subtrees) |
|
31
|
|
|
{ |
|
32
|
14 |
|
$this->_subtrees = $subtrees; |
|
33
|
14 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Initialize from ASN.1. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Sequence $seq |
|
39
|
|
|
* |
|
40
|
|
|
* @return self |
|
41
|
|
|
*/ |
|
42
|
11 |
|
public static function fromASN1(Sequence $seq): self |
|
43
|
|
|
{ |
|
44
|
11 |
|
$subtrees = array_map( |
|
45
|
|
|
function (UnspecifiedType $el) { |
|
46
|
10 |
|
return GeneralSubtree::fromASN1($el->asSequence()); |
|
47
|
11 |
|
}, $seq->elements()); |
|
48
|
11 |
|
if (!count($subtrees)) { |
|
49
|
1 |
|
throw new \UnexpectedValueException( |
|
50
|
1 |
|
'GeneralSubtrees must contain at least one GeneralSubtree.'); |
|
51
|
|
|
} |
|
52
|
10 |
|
return new self(...$subtrees); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get all subtrees. |
|
57
|
|
|
* |
|
58
|
|
|
* @return GeneralSubtree[] |
|
59
|
|
|
*/ |
|
60
|
5 |
|
public function all(): array |
|
61
|
|
|
{ |
|
62
|
5 |
|
return $this->_subtrees; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Generate ASN.1 structure. |
|
67
|
|
|
* |
|
68
|
|
|
* @return Sequence |
|
69
|
|
|
*/ |
|
70
|
18 |
|
public function toASN1(): Sequence |
|
71
|
|
|
{ |
|
72
|
18 |
|
if (!count($this->_subtrees)) { |
|
73
|
1 |
|
throw new \LogicException('No subtrees.'); |
|
74
|
|
|
} |
|
75
|
17 |
|
$elements = array_map( |
|
76
|
|
|
function (GeneralSubtree $gs) { |
|
77
|
17 |
|
return $gs->toASN1(); |
|
78
|
17 |
|
}, $this->_subtrees); |
|
79
|
17 |
|
return new Sequence(...$elements); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @see \Countable::count() |
|
84
|
|
|
* |
|
85
|
|
|
* @return int |
|
86
|
|
|
*/ |
|
87
|
2 |
|
public function count(): int |
|
88
|
|
|
{ |
|
89
|
2 |
|
return count($this->_subtrees); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get iterator for subtrees. |
|
94
|
|
|
* |
|
95
|
|
|
* @see \IteratorAggregate::getIterator() |
|
96
|
|
|
* |
|
97
|
|
|
* @return \ArrayIterator |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function getIterator(): \ArrayIterator |
|
100
|
|
|
{ |
|
101
|
2 |
|
return new \ArrayIterator($this->_subtrees); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|