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\Primitive\Integer; |
10
|
|
|
use Sop\ASN1\Type\Tagged\ImplicitlyTaggedType; |
11
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Implements 'Policy Constraints' certificate extensions. |
15
|
|
|
* |
16
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.11 |
17
|
|
|
*/ |
18
|
|
|
class PolicyConstraintsExtension extends Extension |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var null|int |
22
|
|
|
*/ |
23
|
|
|
protected $_requireExplicitPolicy; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var null|int |
27
|
|
|
*/ |
28
|
|
|
protected $_inhibitPolicyMapping; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param bool $critical |
34
|
|
|
* @param null|int $require_explicit_policy |
35
|
|
|
* @param null|int $inhibit_policy_mapping |
36
|
|
|
*/ |
37
|
12 |
|
public function __construct(bool $critical, |
38
|
|
|
?int $require_explicit_policy = null, ?int $inhibit_policy_mapping = null) |
39
|
|
|
{ |
40
|
12 |
|
parent::__construct(self::OID_POLICY_CONSTRAINTS, $critical); |
41
|
12 |
|
$this->_requireExplicitPolicy = $require_explicit_policy; |
42
|
12 |
|
$this->_inhibitPolicyMapping = $inhibit_policy_mapping; |
43
|
12 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Whether requireExplicitPolicy is present. |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
18 |
|
public function hasRequireExplicitPolicy(): bool |
51
|
|
|
{ |
52
|
18 |
|
return isset($this->_requireExplicitPolicy); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get requireExplicitPolicy. |
57
|
|
|
* |
58
|
|
|
* @throws \LogicException If not set |
59
|
|
|
* |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
18 |
|
public function requireExplicitPolicy(): int |
63
|
|
|
{ |
64
|
18 |
|
if (!$this->hasRequireExplicitPolicy()) { |
65
|
1 |
|
throw new \LogicException('requireExplicitPolicy not set.'); |
66
|
|
|
} |
67
|
17 |
|
return $this->_requireExplicitPolicy; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Whether inhibitPolicyMapping is present. |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
5 |
|
public function hasInhibitPolicyMapping(): bool |
76
|
|
|
{ |
77
|
5 |
|
return isset($this->_inhibitPolicyMapping); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get inhibitPolicyMapping. |
82
|
|
|
* |
83
|
|
|
* @throws \LogicException If not set |
84
|
|
|
* |
85
|
|
|
* @return int |
86
|
|
|
*/ |
87
|
4 |
|
public function inhibitPolicyMapping(): int |
88
|
|
|
{ |
89
|
4 |
|
if (!$this->hasInhibitPolicyMapping()) { |
90
|
1 |
|
throw new \LogicException('inhibitPolicyMapping not set.'); |
91
|
|
|
} |
92
|
3 |
|
return $this->_inhibitPolicyMapping; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
10 |
|
protected static function _fromDER(string $data, bool $critical): Extension |
99
|
|
|
{ |
100
|
10 |
|
$seq = UnspecifiedType::fromDER($data)->asSequence(); |
101
|
10 |
|
$require_explicit_policy = null; |
102
|
10 |
|
$inhibit_policy_mapping = null; |
103
|
10 |
|
if ($seq->hasTagged(0)) { |
104
|
9 |
|
$require_explicit_policy = $seq->getTagged(0) |
105
|
9 |
|
->asImplicit(Element::TYPE_INTEGER)->asInteger()->intNumber(); |
106
|
|
|
} |
107
|
10 |
|
if ($seq->hasTagged(1)) { |
108
|
9 |
|
$inhibit_policy_mapping = $seq->getTagged(1) |
109
|
9 |
|
->asImplicit(Element::TYPE_INTEGER)->asInteger()->intNumber(); |
110
|
|
|
} |
111
|
10 |
|
return new self($critical, $require_explicit_policy, $inhibit_policy_mapping); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
19 |
|
protected function _valueASN1(): Element |
118
|
|
|
{ |
119
|
19 |
|
$elements = []; |
120
|
19 |
|
if (isset($this->_requireExplicitPolicy)) { |
121
|
18 |
|
$elements[] = new ImplicitlyTaggedType(0, |
122
|
18 |
|
new Integer($this->_requireExplicitPolicy)); |
123
|
|
|
} |
124
|
19 |
|
if (isset($this->_inhibitPolicyMapping)) { |
125
|
16 |
|
$elements[] = new ImplicitlyTaggedType(1, |
126
|
16 |
|
new Integer($this->_inhibitPolicyMapping)); |
127
|
|
|
} |
128
|
19 |
|
return new Sequence(...$elements); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|