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\UnspecifiedType; |
9
|
|
|
use Sop\ASN1\Util\Flags; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Implements 'Key Usage' certificate extension. |
13
|
|
|
* |
14
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.3 |
15
|
|
|
*/ |
16
|
|
|
class KeyUsageExtension extends Extension |
17
|
|
|
{ |
18
|
|
|
const DIGITAL_SIGNATURE = 0x100; |
19
|
|
|
const NON_REPUDIATION = 0x080; |
20
|
|
|
const KEY_ENCIPHERMENT = 0x040; |
21
|
|
|
const DATA_ENCIPHERMENT = 0x020; |
22
|
|
|
const KEY_AGREEMENT = 0x010; |
23
|
|
|
const KEY_CERT_SIGN = 0x008; |
24
|
|
|
const CRL_SIGN = 0x004; |
25
|
|
|
const ENCIPHER_ONLY = 0x002; |
26
|
|
|
const DECIPHER_ONLY = 0x001; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Key usage flags. |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $_keyUsage; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
* |
38
|
|
|
* @param bool $critical |
39
|
|
|
* @param int $keyUsage |
40
|
|
|
*/ |
41
|
20 |
|
public function __construct(bool $critical, int $keyUsage) |
42
|
|
|
{ |
43
|
20 |
|
parent::__construct(self::OID_KEY_USAGE, $critical); |
44
|
20 |
|
$this->_keyUsage = $keyUsage; |
45
|
20 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Check whether digitalSignature flag is set. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
4 |
|
public function isDigitalSignature(): bool |
53
|
|
|
{ |
54
|
4 |
|
return $this->_flagSet(self::DIGITAL_SIGNATURE); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Check whether nonRepudiation/contentCommitment flag is set. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
2 |
|
public function isNonRepudiation(): bool |
63
|
|
|
{ |
64
|
2 |
|
return $this->_flagSet(self::NON_REPUDIATION); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check whether keyEncipherment flag is set. |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
3 |
|
public function isKeyEncipherment(): bool |
73
|
|
|
{ |
74
|
3 |
|
return $this->_flagSet(self::KEY_ENCIPHERMENT); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Check whether dataEncipherment flag is set. |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
2 |
|
public function isDataEncipherment(): bool |
83
|
|
|
{ |
84
|
2 |
|
return $this->_flagSet(self::DATA_ENCIPHERMENT); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Check whether keyAgreement flag is set. |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
2 |
|
public function isKeyAgreement(): bool |
93
|
|
|
{ |
94
|
2 |
|
return $this->_flagSet(self::KEY_AGREEMENT); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check whether keyCertSign flag is set. |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
23 |
|
public function isKeyCertSign(): bool |
103
|
|
|
{ |
104
|
23 |
|
return $this->_flagSet(self::KEY_CERT_SIGN); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Check whether cRLSign flag is set. |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
2 |
|
public function isCRLSign(): bool |
113
|
|
|
{ |
114
|
2 |
|
return $this->_flagSet(self::CRL_SIGN); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Check whether encipherOnly flag is set. |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
2 |
|
public function isEncipherOnly(): bool |
123
|
|
|
{ |
124
|
2 |
|
return $this->_flagSet(self::ENCIPHER_ONLY); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Check whether decipherOnly flag is set. |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
2 |
|
public function isDecipherOnly(): bool |
133
|
|
|
{ |
134
|
2 |
|
return $this->_flagSet(self::DECIPHER_ONLY); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Check whether given flag is set. |
139
|
|
|
* |
140
|
|
|
* @param int $flag |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
31 |
|
protected function _flagSet(int $flag): bool |
145
|
|
|
{ |
146
|
31 |
|
return (bool) ($this->_keyUsage & $flag); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
16 |
|
protected static function _fromDER(string $data, bool $critical): Extension |
153
|
|
|
{ |
154
|
16 |
|
return new self($critical, |
155
|
16 |
|
Flags::fromBitString( |
156
|
16 |
|
UnspecifiedType::fromDER($data)->asBitString(), 9)->intNumber()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
27 |
|
protected function _valueASN1(): Element |
163
|
|
|
{ |
164
|
27 |
|
$flags = new Flags($this->_keyUsage, 9); |
165
|
27 |
|
return $flags->bitString()->withoutTrailingZeroes(); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|