|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace X509\Certificate\Extension; |
|
6
|
|
|
|
|
7
|
|
|
use ASN1\Type\UnspecifiedType; |
|
8
|
|
|
use ASN1\Type\Constructed\Sequence; |
|
9
|
|
|
use X509\Certificate\Extension\CertificatePolicy\PolicyInformation; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Implements 'Certificate Policies' certificate extension. |
|
13
|
|
|
* |
|
14
|
|
|
* @link https://tools.ietf.org/html/rfc5280#section-4.2.1.4 |
|
15
|
|
|
*/ |
|
16
|
|
|
class CertificatePoliciesExtension extends Extension implements |
|
17
|
|
|
\Countable, |
|
18
|
|
|
\IteratorAggregate |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Policy information terms. |
|
22
|
|
|
* |
|
23
|
|
|
* @var PolicyInformation[] $_policies |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $_policies; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param bool $critical |
|
31
|
|
|
* @param PolicyInformation ...$policies |
|
32
|
|
|
*/ |
|
33
|
13 |
|
public function __construct(bool $critical, PolicyInformation ...$policies) |
|
34
|
|
|
{ |
|
35
|
13 |
|
parent::__construct(Extension::OID_CERTIFICATE_POLICIES, $critical); |
|
36
|
13 |
|
$this->_policies = []; |
|
37
|
13 |
|
foreach ($policies as $policy) { |
|
38
|
12 |
|
$this->_policies[$policy->oid()] = $policy; |
|
39
|
|
|
} |
|
40
|
13 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
* @return self |
|
46
|
|
|
*/ |
|
47
|
10 |
|
protected static function _fromDER(string $data, bool $critical): self |
|
48
|
|
|
{ |
|
49
|
10 |
|
$policies = array_map( |
|
50
|
10 |
|
function (UnspecifiedType $el) { |
|
51
|
9 |
|
return PolicyInformation::fromASN1($el->asSequence()); |
|
52
|
10 |
|
}, UnspecifiedType::fromDER($data)->asSequence()->elements()); |
|
53
|
10 |
|
if (!count($policies)) { |
|
54
|
1 |
|
throw new \UnexpectedValueException( |
|
55
|
|
|
"certificatePolicies must contain" . |
|
56
|
1 |
|
" at least one PolicyInformation."); |
|
57
|
|
|
} |
|
58
|
9 |
|
return new self($critical, ...$policies); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Check whether policy information by OID is present. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $oid |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
7 |
|
public function has(string $oid): bool |
|
68
|
|
|
{ |
|
69
|
7 |
|
return isset($this->_policies[$oid]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get policy information by OID. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $oid |
|
76
|
|
|
* @throws \LogicException |
|
77
|
|
|
* @return PolicyInformation |
|
78
|
|
|
*/ |
|
79
|
4 |
|
public function get(string $oid): PolicyInformation |
|
80
|
|
|
{ |
|
81
|
4 |
|
if (!$this->has($oid)) { |
|
82
|
1 |
|
throw new \LogicException("Not certificate policy by OID $oid."); |
|
83
|
|
|
} |
|
84
|
3 |
|
return $this->_policies[$oid]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Check whether anyPolicy is present. |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
4 |
|
public function hasAnyPolicy(): bool |
|
93
|
|
|
{ |
|
94
|
4 |
|
return $this->has(PolicyInformation::OID_ANY_POLICY); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get anyPolicy information. |
|
99
|
|
|
* |
|
100
|
|
|
* @throws \LogicException If anyPolicy is not present. |
|
101
|
|
|
* @return PolicyInformation |
|
102
|
|
|
*/ |
|
103
|
3 |
|
public function anyPolicy(): PolicyInformation |
|
104
|
|
|
{ |
|
105
|
3 |
|
if (!$this->hasAnyPolicy()) { |
|
106
|
2 |
|
throw new \LogicException("No anyPolicy."); |
|
107
|
|
|
} |
|
108
|
1 |
|
return $this->get(PolicyInformation::OID_ANY_POLICY); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
* @return Sequence |
|
115
|
|
|
*/ |
|
116
|
30 |
|
protected function _valueASN1(): Sequence |
|
117
|
|
|
{ |
|
118
|
30 |
|
if (!count($this->_policies)) { |
|
119
|
1 |
|
throw new \LogicException("No policies."); |
|
120
|
|
|
} |
|
121
|
29 |
|
$elements = array_map( |
|
122
|
29 |
|
function (PolicyInformation $pi) { |
|
123
|
29 |
|
return $pi->toASN1(); |
|
124
|
29 |
|
}, array_values($this->_policies)); |
|
125
|
29 |
|
return new Sequence(...$elements); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get the number of policies. |
|
130
|
|
|
* |
|
131
|
|
|
* @see \Countable::count() |
|
132
|
|
|
* @return int |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function count(): int |
|
135
|
|
|
{ |
|
136
|
1 |
|
return count($this->_policies); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get iterator for policy information terms. |
|
141
|
|
|
* |
|
142
|
|
|
* @see \IteratorAggregate::getIterator() |
|
143
|
|
|
* @return \ArrayIterator |
|
144
|
|
|
*/ |
|
145
|
15 |
|
public function getIterator(): \ArrayIterator |
|
146
|
|
|
{ |
|
147
|
15 |
|
return new \ArrayIterator($this->_policies); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|