KeyUsageExtension::isNonRepudiation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\Certificate\Extension;
6
7
use ASN1\Type\UnspecifiedType;
8
use ASN1\Type\Primitive\BitString;
9
use ASN1\Util\Flags;
10
11
/**
12
 * Implements 'Key Usage' certificate extension.
13
 *
14
 * @link 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 $_keyUsage
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
     *
49
     * {@inheritdoc}
50
     * @return self
51
     */
52 16
    protected static function _fromDER(string $data, bool $critical): self
53
    {
54 16
        return new self($critical,
55 16
            Flags::fromBitString(UnspecifiedType::fromDER($data)->asBitString(),
56 16
                9)->intNumber());
57
    }
58
    
59
    /**
60
     * Check whether digitalSignature flag is set.
61
     *
62
     * @return bool
63
     */
64 4
    public function isDigitalSignature(): bool
65
    {
66 4
        return $this->_flagSet(self::DIGITAL_SIGNATURE);
67
    }
68
    
69
    /**
70
     * Check whether nonRepudiation/contentCommitment flag is set.
71
     *
72
     * @return bool
73
     */
74 2
    public function isNonRepudiation(): bool
75
    {
76 2
        return $this->_flagSet(self::NON_REPUDIATION);
77
    }
78
    
79
    /**
80
     * Check whether keyEncipherment flag is set.
81
     *
82
     * @return bool
83
     */
84 3
    public function isKeyEncipherment(): bool
85
    {
86 3
        return $this->_flagSet(self::KEY_ENCIPHERMENT);
87
    }
88
    
89
    /**
90
     * Check whether dataEncipherment flag is set.
91
     *
92
     * @return bool
93
     */
94 2
    public function isDataEncipherment(): bool
95
    {
96 2
        return $this->_flagSet(self::DATA_ENCIPHERMENT);
97
    }
98
    
99
    /**
100
     * Check whether keyAgreement flag is set.
101
     *
102
     * @return bool
103
     */
104 2
    public function isKeyAgreement(): bool
105
    {
106 2
        return $this->_flagSet(self::KEY_AGREEMENT);
107
    }
108
    
109
    /**
110
     * Check whether keyCertSign flag is set.
111
     *
112
     * @return bool
113
     */
114 23
    public function isKeyCertSign(): bool
115
    {
116 23
        return $this->_flagSet(self::KEY_CERT_SIGN);
117
    }
118
    
119
    /**
120
     * Check whether cRLSign flag is set.
121
     *
122
     * @return bool
123
     */
124 2
    public function isCRLSign(): bool
125
    {
126 2
        return $this->_flagSet(self::CRL_SIGN);
127
    }
128
    
129
    /**
130
     * Check whether encipherOnly flag is set.
131
     *
132
     * @return bool
133
     */
134 2
    public function isEncipherOnly(): bool
135
    {
136 2
        return $this->_flagSet(self::ENCIPHER_ONLY);
137
    }
138
    
139
    /**
140
     * Check whether decipherOnly flag is set.
141
     *
142
     * @return bool
143
     */
144 2
    public function isDecipherOnly(): bool
145
    {
146 2
        return $this->_flagSet(self::DECIPHER_ONLY);
147
    }
148
    
149
    /**
150
     * Check whether given flag is set.
151
     *
152
     * @param int $flag
153
     * @return boolean
154
     */
155 31
    protected function _flagSet(int $flag): bool
156
    {
157 31
        return (bool) ($this->_keyUsage & $flag);
158
    }
159
    
160
    /**
161
     *
162
     * {@inheritdoc}
163
     * @return BitString
164
     */
165 27
    protected function _valueASN1(): BitString
166
    {
167 27
        $flags = new Flags($this->_keyUsage, 9);
168 27
        return $flags->bitString()->withoutTrailingZeroes();
169
    }
170
}
171