|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace X509\Certificate\Extension\NameConstraints; |
|
6
|
|
|
|
|
7
|
|
|
use ASN1\Element; |
|
8
|
|
|
use ASN1\Type\Constructed\Sequence; |
|
9
|
|
|
use ASN1\Type\Primitive\Integer; |
|
10
|
|
|
use ASN1\Type\Tagged\ImplicitlyTaggedType; |
|
11
|
|
|
use X509\GeneralName\GeneralName; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Implements <i>GeneralSubtree</i> ASN.1 type used by |
|
15
|
|
|
* 'Name Constraints' certificate extension. |
|
16
|
|
|
* |
|
17
|
|
|
* @link 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 $_min |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $_min; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Not used, must be null. |
|
37
|
|
|
* |
|
38
|
|
|
* @var int|null $_max |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $_max; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param GeneralName $base |
|
46
|
|
|
* @param int $min |
|
47
|
|
|
* @param int|null $max |
|
48
|
|
|
*/ |
|
49
|
17 |
|
public function __construct(GeneralName $base, int $min = 0, $max = null) |
|
50
|
|
|
{ |
|
51
|
17 |
|
$this->_base = $base; |
|
52
|
17 |
|
$this->_min = $min; |
|
53
|
17 |
|
$this->_max = $max; |
|
54
|
17 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Initialize from ASN.1. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Sequence $seq |
|
60
|
|
|
* @return self |
|
61
|
|
|
*/ |
|
62
|
12 |
|
public static function fromASN1(Sequence $seq): self |
|
63
|
|
|
{ |
|
64
|
12 |
|
$base = GeneralName::fromASN1($seq->at(0)->asTagged()); |
|
65
|
12 |
|
$min = 0; |
|
66
|
12 |
|
$max = null; |
|
67
|
12 |
|
if ($seq->hasTagged(0)) { |
|
68
|
1 |
|
$min = $seq->getTagged(0) |
|
69
|
1 |
|
->asImplicit(Element::TYPE_INTEGER) |
|
70
|
1 |
|
->asInteger() |
|
71
|
1 |
|
->intNumber(); |
|
72
|
|
|
} |
|
73
|
12 |
|
if ($seq->hasTagged(1)) { |
|
74
|
1 |
|
$max = $seq->getTagged(1) |
|
75
|
1 |
|
->asImplicit(Element::TYPE_INTEGER) |
|
76
|
1 |
|
->asInteger() |
|
77
|
1 |
|
->intNumber(); |
|
78
|
|
|
} |
|
79
|
12 |
|
return new self($base, $min, $max); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get constraint. |
|
84
|
|
|
* |
|
85
|
|
|
* @return GeneralName |
|
86
|
|
|
*/ |
|
87
|
5 |
|
public function base(): GeneralName |
|
88
|
|
|
{ |
|
89
|
5 |
|
return $this->_base; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Generate ASN.1 structure. |
|
94
|
|
|
* |
|
95
|
|
|
* @return Sequence |
|
96
|
|
|
*/ |
|
97
|
19 |
|
public function toASN1(): Sequence |
|
98
|
|
|
{ |
|
99
|
19 |
|
$elements = array($this->_base->toASN1()); |
|
100
|
19 |
|
if (isset($this->_min) && $this->_min != 0) { |
|
101
|
1 |
|
$elements[] = new ImplicitlyTaggedType(0, new Integer($this->_min)); |
|
102
|
|
|
} |
|
103
|
19 |
|
if (isset($this->_max)) { |
|
104
|
1 |
|
$elements[] = new ImplicitlyTaggedType(1, new Integer($this->_max)); |
|
105
|
|
|
} |
|
106
|
19 |
|
return new Sequence(...$elements); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|