Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/Certificate/Extension/KeyUsageExtension.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate\Extension;
6
7 use SpomkyLabs\Pki\ASN1\Element;
8 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
9 use SpomkyLabs\Pki\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 final class KeyUsageExtension extends Extension
17 {
18 final public const DIGITAL_SIGNATURE = 0x100;
19
20 final public const NON_REPUDIATION = 0x080;
21
22 final public const KEY_ENCIPHERMENT = 0x040;
23
24 final public const DATA_ENCIPHERMENT = 0x020;
25
26 final public const KEY_AGREEMENT = 0x010;
27
28 final public const KEY_CERT_SIGN = 0x008;
29
30 final public const CRL_SIGN = 0x004;
31
32 final public const ENCIPHER_ONLY = 0x002;
33
34 final public const DECIPHER_ONLY = 0x001;
35
36 private function __construct(
37 bool $critical,
38 private readonly int $keyUsage
39 ) {
40 parent::__construct(self::OID_KEY_USAGE, $critical);
41 }
42
43 public static function create(bool $critical, int $keyUsage): self
44 {
45 return new self($critical, $keyUsage);
46 }
47
48 /**
49 * Check whether digitalSignature flag is set.
50 */
51 public function isDigitalSignature(): bool
52 {
53 return $this->_flagSet(self::DIGITAL_SIGNATURE);
54 }
55
56 /**
57 * Check whether nonRepudiation/contentCommitment flag is set.
58 */
59 public function isNonRepudiation(): bool
60 {
61 return $this->_flagSet(self::NON_REPUDIATION);
62 }
63
64 /**
65 * Check whether keyEncipherment flag is set.
66 */
67 public function isKeyEncipherment(): bool
68 {
69 return $this->_flagSet(self::KEY_ENCIPHERMENT);
70 }
71
72 /**
73 * Check whether dataEncipherment flag is set.
74 */
75 public function isDataEncipherment(): bool
76 {
77 return $this->_flagSet(self::DATA_ENCIPHERMENT);
78 }
79
80 /**
81 * Check whether keyAgreement flag is set.
82 */
83 public function isKeyAgreement(): bool
84 {
85 return $this->_flagSet(self::KEY_AGREEMENT);
86 }
87
88 /**
89 * Check whether keyCertSign flag is set.
90 */
91 public function isKeyCertSign(): bool
92 {
93 return $this->_flagSet(self::KEY_CERT_SIGN);
94 }
95
96 /**
97 * Check whether cRLSign flag is set.
98 */
99 public function isCRLSign(): bool
100 {
101 return $this->_flagSet(self::CRL_SIGN);
102 }
103
104 /**
105 * Check whether encipherOnly flag is set.
106 */
107 public function isEncipherOnly(): bool
108 {
109 return $this->_flagSet(self::ENCIPHER_ONLY);
110 }
111
112 /**
113 * Check whether decipherOnly flag is set.
114 */
115 public function isDecipherOnly(): bool
116 {
117 return $this->_flagSet(self::DECIPHER_ONLY);
118 }
119
120 /**
121 * Check whether given flag is set.
122 */
123 protected function _flagSet(int $flag): bool
124 {
125 return (bool) ($this->keyUsage & $flag);
126 }
127
128 protected static function fromDER(string $data, bool $critical): static
129 {
130 return self::create(
131 $critical,
132 Flags::fromBitString(UnspecifiedType::fromDER($data)->asBitString(), 9)->intNumber()
133 );
134 }
135
136 protected function valueASN1(): Element
137 {
138 $flags = Flags::create($this->keyUsage, 9);
139 return $flags->bitString()
140 ->withoutTrailingZeroes();
141 }
142 }