|
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\Boolean; |
|
10
|
|
|
use Sop\ASN1\Type\Primitive\Integer; |
|
11
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Implements 'Basic Constraints' certificate extension. |
|
15
|
|
|
* |
|
16
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.9 |
|
17
|
|
|
*/ |
|
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
|
40 |
|
public function isCA(): bool |
|
54
|
|
|
{ |
|
55
|
40 |
|
return $this->_ca; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Whether path length is present. |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
40 |
|
public function hasPathLen(): bool |
|
64
|
|
|
{ |
|
65
|
40 |
|
return isset($this->_pathLen); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get path length. |
|
70
|
|
|
* |
|
71
|
|
|
* @throws \LogicException If not set |
|
72
|
|
|
* |
|
73
|
|
|
* @return int |
|
74
|
|
|
*/ |
|
75
|
29 |
|
public function pathLen(): int |
|
76
|
|
|
{ |
|
77
|
29 |
|
if (!$this->hasPathLen()) { |
|
78
|
1 |
|
throw new \LogicException('pathLenConstraint not set.'); |
|
79
|
|
|
} |
|
80
|
28 |
|
return $this->_pathLen; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
15 |
|
protected static function _fromDER(string $data, bool $critical): Extension |
|
87
|
|
|
{ |
|
88
|
15 |
|
$seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
89
|
15 |
|
$ca = false; |
|
90
|
15 |
|
$path_len = null; |
|
91
|
15 |
|
$idx = 0; |
|
92
|
15 |
|
if ($seq->has($idx, Element::TYPE_BOOLEAN)) { |
|
93
|
14 |
|
$ca = $seq->at($idx++)->asBoolean()->value(); |
|
94
|
|
|
} |
|
95
|
15 |
|
if ($seq->has($idx, Element::TYPE_INTEGER)) { |
|
96
|
14 |
|
$path_len = $seq->at($idx)->asInteger()->intNumber(); |
|
97
|
|
|
} |
|
98
|
15 |
|
return new self($critical, $ca, $path_len); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
47 |
|
protected function _valueASN1(): Element |
|
105
|
|
|
{ |
|
106
|
47 |
|
$elements = []; |
|
107
|
47 |
|
if ($this->_ca) { |
|
108
|
42 |
|
$elements[] = new Boolean(true); |
|
109
|
|
|
} |
|
110
|
47 |
|
if (isset($this->_pathLen)) { |
|
111
|
30 |
|
$elements[] = new Integer($this->_pathLen); |
|
112
|
|
|
} |
|
113
|
47 |
|
return new Sequence(...$elements); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|