1 | <?php |
||
18 | class BasicConstraintsExtension extends Extension |
||
19 | { |
||
20 | /** |
||
21 | * Whether certificate is a CA. |
||
22 | * |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $_ca; |
||
26 | |||
27 | /** |
||
28 | * Maximum certification path length. |
||
29 | * |
||
30 | * @var null|int |
||
31 | */ |
||
32 | protected $_pathLen; |
||
33 | |||
34 | /** |
||
35 | * Constructor. |
||
36 | * |
||
37 | * @param bool $critical |
||
38 | * @param bool $ca |
||
39 | * @param null|int $path_len |
||
40 | */ |
||
41 | 23 | public function __construct(bool $critical, bool $ca, ?int $path_len = null) |
|
42 | { |
||
43 | 23 | parent::__construct(self::OID_BASIC_CONSTRAINTS, $critical); |
|
44 | 23 | $this->_ca = $ca; |
|
45 | 23 | $this->_pathLen = $path_len; |
|
46 | 23 | } |
|
47 | |||
48 | /** |
||
49 | * Whether certificate is a CA. |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | 15 | public function isCA(): bool |
|
54 | { |
||
55 | 15 | return $this->_ca; |
|
56 | 15 | } |
|
57 | 15 | ||
58 | 15 | /** |
|
59 | 15 | * Whether path length is present. |
|
60 | 14 | * |
|
61 | 14 | * @return bool |
|
62 | 14 | */ |
|
63 | public function hasPathLen(): bool |
||
66 | 14 | } |
|
67 | 14 | ||
68 | /** |
||
69 | 15 | * Get path length. |
|
70 | * |
||
71 | * @throws \LogicException If not set |
||
72 | * |
||
73 | * @return int |
||
74 | */ |
||
75 | public function pathLen(): int |
||
76 | { |
||
77 | 40 | if (!$this->hasPathLen()) { |
|
78 | throw new \LogicException('pathLenConstraint not set.'); |
||
79 | 40 | } |
|
80 | return $this->_pathLen; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | protected static function _fromDER(string $data, bool $critical): Extension |
||
87 | 40 | { |
|
88 | $seq = UnspecifiedType::fromDER($data)->asSequence(); |
||
89 | 40 | $ca = false; |
|
90 | $path_len = null; |
||
91 | $idx = 0; |
||
92 | if ($seq->has($idx, Element::TYPE_BOOLEAN)) { |
||
93 | $ca = $seq->at($idx++)->asBoolean()->value(); |
||
94 | } |
||
95 | if ($seq->has($idx, Element::TYPE_INTEGER)) { |
||
96 | $path_len = $seq->at($idx)->asInteger()->intNumber(); |
||
97 | } |
||
98 | 29 | return new self($critical, $ca, $path_len); |
|
99 | } |
||
100 | 29 | ||
101 | 1 | /** |
|
102 | * {@inheritdoc} |
||
103 | 28 | */ |
|
104 | protected function _valueASN1(): Element |
||
114 | 47 | } |
|
115 | } |
||
116 |