|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\X509\Certificate\Extension\NameConstraints; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
|
8
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
|
9
|
|
|
use Sop\ASN1\Type\Primitive\Integer; |
|
10
|
|
|
use Sop\ASN1\Type\Tagged\ImplicitlyTaggedType; |
|
11
|
|
|
use Sop\X509\GeneralName\GeneralName; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Implements *GeneralSubtree* ASN.1 type used by 'Name Constraints' |
|
15
|
|
|
* certificate extension. |
|
16
|
|
|
* |
|
17
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.10 |
|
18
|
|
|
*/ |
|
19
|
|
|
class GeneralSubtree |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Constraint. |
|
23
|
|
|
* |
|
24
|
|
|
* @var GeneralName |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $_base; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Not used, must be zero. |
|
30
|
|
|
* |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $_min; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Not used, must be null. |
|
37
|
|
|
* |
|
38
|
|
|
* @var null|int |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $_max; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param GeneralName $base |
|
46
|
|
|
* @param int $min |
|
47
|
|
|
* @param null|int $max |
|
48
|
|
|
*/ |
|
49
|
18 |
|
public function __construct(GeneralName $base, int $min = 0, ?int $max = null) |
|
50
|
|
|
{ |
|
51
|
18 |
|
$this->_base = $base; |
|
52
|
18 |
|
$this->_min = $min; |
|
53
|
18 |
|
$this->_max = $max; |
|
54
|
18 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Initialize from ASN.1. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Sequence $seq |
|
60
|
|
|
* |
|
61
|
|
|
* @return self |
|
62
|
|
|
*/ |
|
63
|
13 |
|
public static function fromASN1(Sequence $seq): self |
|
64
|
|
|
{ |
|
65
|
13 |
|
$base = GeneralName::fromASN1($seq->at(0)->asTagged()); |
|
66
|
13 |
|
$min = 0; |
|
67
|
13 |
|
$max = null; |
|
68
|
|
|
// GeneralName is a CHOICE, which may be tagged as otherName [0] |
|
69
|
|
|
// or rfc822Name [1]. As minimum and maximum are also implicitly tagged, |
|
70
|
|
|
// we have to iterate the remaining elements instead of just checking |
|
71
|
|
|
// for tagged types. |
|
72
|
13 |
|
for ($i = 1; $i < count($seq); ++$i) { |
|
|
|
|
|
|
73
|
1 |
|
$el = $seq->at($i)->expectTagged(); |
|
74
|
1 |
|
switch ($el->tag()) { |
|
75
|
1 |
|
case 0: |
|
76
|
1 |
|
$min = $el->asImplicit(Element::TYPE_INTEGER) |
|
77
|
1 |
|
->asInteger()->intNumber(); |
|
78
|
1 |
|
break; |
|
79
|
1 |
|
case 1: |
|
80
|
1 |
|
$max = $el->asImplicit(Element::TYPE_INTEGER) |
|
81
|
1 |
|
->asInteger()->intNumber(); |
|
82
|
1 |
|
break; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
13 |
|
return new self($base, $min, $max); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get constraint. |
|
90
|
|
|
* |
|
91
|
|
|
* @return GeneralName |
|
92
|
|
|
*/ |
|
93
|
5 |
|
public function base(): GeneralName |
|
94
|
|
|
{ |
|
95
|
5 |
|
return $this->_base; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Generate ASN.1 structure. |
|
100
|
|
|
* |
|
101
|
|
|
* @return Sequence |
|
102
|
|
|
*/ |
|
103
|
20 |
|
public function toASN1(): Sequence |
|
104
|
|
|
{ |
|
105
|
20 |
|
$elements = [$this->_base->toASN1()]; |
|
106
|
20 |
|
if (isset($this->_min) && 0 !== $this->_min) { |
|
107
|
1 |
|
$elements[] = new ImplicitlyTaggedType(0, new Integer($this->_min)); |
|
108
|
|
|
} |
|
109
|
20 |
|
if (isset($this->_max)) { |
|
110
|
1 |
|
$elements[] = new ImplicitlyTaggedType(1, new Integer($this->_max)); |
|
111
|
|
|
} |
|
112
|
20 |
|
return new Sequence(...$elements); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: