1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\Certificate\Extension; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
8
|
|
|
use Sop\ASN1\Type\Constructed\Sequence; |
9
|
|
|
use Sop\ASN1\Type\Tagged\ImplicitlyTaggedType; |
10
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
11
|
|
|
use Sop\X509\Certificate\Extension\NameConstraints\GeneralSubtrees; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Implements 'Name Constraints' certificate extension. |
15
|
|
|
* |
16
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.10 |
17
|
|
|
*/ |
18
|
|
|
class NameConstraintsExtension extends Extension |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Permitted subtrees. |
22
|
|
|
* |
23
|
|
|
* @var null|GeneralSubtrees |
24
|
|
|
*/ |
25
|
|
|
protected $_permitted; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Excluded subtrees. |
29
|
|
|
* |
30
|
|
|
* @var null|GeneralSubtrees |
31
|
|
|
*/ |
32
|
|
|
protected $_excluded; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param bool $critical |
38
|
|
|
* @param GeneralSubtrees $permitted |
39
|
|
|
* @param GeneralSubtrees $excluded |
40
|
|
|
*/ |
41
|
12 |
|
public function __construct(bool $critical, ?GeneralSubtrees $permitted = null, |
42
|
|
|
?GeneralSubtrees $excluded = null) |
43
|
|
|
{ |
44
|
12 |
|
parent::__construct(self::OID_NAME_CONSTRAINTS, $critical); |
45
|
12 |
|
$this->_permitted = $permitted; |
46
|
12 |
|
$this->_excluded = $excluded; |
47
|
12 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Whether permitted subtrees are present. |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
10 |
|
public function hasPermittedSubtrees(): bool |
55
|
|
|
{ |
56
|
10 |
|
return isset($this->_permitted); |
57
|
10 |
|
} |
58
|
10 |
|
|
59
|
10 |
|
/** |
60
|
9 |
|
* Get permitted subtrees. |
61
|
9 |
|
* |
62
|
9 |
|
* @throws \LogicException If not set |
63
|
9 |
|
* |
64
|
|
|
* @return GeneralSubtrees |
65
|
10 |
|
*/ |
66
|
1 |
|
public function permittedSubtrees(): GeneralSubtrees |
67
|
1 |
|
{ |
68
|
1 |
|
if (!$this->hasPermittedSubtrees()) { |
69
|
1 |
|
throw new \LogicException('No permitted subtrees.'); |
70
|
|
|
} |
71
|
10 |
|
return $this->_permitted; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Whether excluded subtrees are present. |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
3 |
|
public function hasExcludedSubtrees(): bool |
80
|
|
|
{ |
81
|
3 |
|
return isset($this->_excluded); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get excluded subtrees. |
86
|
|
|
* |
87
|
|
|
* @throws \LogicException If not set |
88
|
|
|
* |
89
|
|
|
* @return GeneralSubtrees |
90
|
3 |
|
*/ |
91
|
|
|
public function excludedSubtrees(): GeneralSubtrees |
92
|
3 |
|
{ |
93
|
1 |
|
if (!$this->hasExcludedSubtrees()) { |
94
|
|
|
throw new \LogicException('No excluded subtrees.'); |
95
|
2 |
|
} |
96
|
|
|
return $this->_excluded; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
protected static function _fromDER(string $data, bool $critical): Extension |
103
|
2 |
|
{ |
104
|
|
|
$seq = UnspecifiedType::fromDER($data)->asSequence(); |
105
|
2 |
|
$permitted = null; |
106
|
|
|
$excluded = null; |
107
|
|
|
if ($seq->hasTagged(0)) { |
108
|
|
|
$permitted = GeneralSubtrees::fromASN1( |
109
|
|
|
$seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
110
|
|
|
} |
111
|
|
|
if ($seq->hasTagged(1)) { |
112
|
|
|
$excluded = GeneralSubtrees::fromASN1( |
113
|
|
|
$seq->getTagged(1)->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
114
|
2 |
|
} |
115
|
|
|
return new self($critical, $permitted, $excluded); |
116
|
2 |
|
} |
117
|
1 |
|
|
118
|
|
|
/** |
119
|
1 |
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
protected function _valueASN1(): Element |
122
|
|
|
{ |
123
|
|
|
$elements = []; |
124
|
|
|
if (isset($this->_permitted)) { |
125
|
|
|
$elements[] = new ImplicitlyTaggedType(0, $this->_permitted->toASN1()); |
126
|
|
|
} |
127
|
17 |
|
if (isset($this->_excluded)) { |
128
|
|
|
$elements[] = new ImplicitlyTaggedType(1, $this->_excluded->toASN1()); |
129
|
17 |
|
} |
130
|
17 |
|
return new Sequence(...$elements); |
131
|
16 |
|
} |
132
|
|
|
} |
133
|
|
|
|