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