GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( 405cf3...79c9ba )
by Joni
04:48
created

AuthorityKeyIdentifierExtension::hasSerial()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\Integer;
10
use Sop\ASN1\Type\Primitive\OctetString;
11
use Sop\ASN1\Type\Tagged\ImplicitlyTaggedType;
12
use Sop\ASN1\Type\UnspecifiedType;
13
use Sop\CryptoTypes\Asymmetric\PublicKeyInfo;
14
use Sop\X509\GeneralName\GeneralNames;
15
16
/**
17
 * Implements 'Authority Key Identifier' certificate extension.
18
 *
19
 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.1
20
 */
21
class AuthorityKeyIdentifierExtension extends Extension
22
{
23
    /**
24
     * Key identifier.
25
     *
26
     * @var null|string
27
     */
28
    protected $_keyIdentifier;
29
30
    /**
31
     * Issuer name.
32
     *
33
     * @var null|GeneralNames
34
     */
35
    protected $_authorityCertIssuer;
36
37
    /**
38
     * Issuer serial number as a base 10 integer.
39
     *
40
     * @var null|string
41
     */
42
    protected $_authorityCertSerialNumber;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param bool              $critical      Conforming CA's must mark as non-critical (false)
48
     * @param null|string       $keyIdentifier Key identifier
49
     * @param null|GeneralNames $issuer        Issuer name
50
     * @param null|int|string   $serial        Issuer serial number as a base 10 integer
51
     */
52 27
    public function __construct(bool $critical, ?string $keyIdentifier,
53
        ?GeneralNames $issuer = null, $serial = null)
54
    {
55 27
        parent::__construct(self::OID_AUTHORITY_KEY_IDENTIFIER, $critical);
56 27
        $this->_keyIdentifier = $keyIdentifier;
57 27
        $this->_authorityCertIssuer = $issuer;
58 27
        $this->_authorityCertSerialNumber = isset($serial) ? strval($serial) : null;
59 27
    }
60
61
    /**
62
     * Create from public key info.
63
     *
64
     * @param PublicKeyInfo $pki
65
     *
66
     * @return AuthorityKeyIdentifierExtension
67 1
     */
68
    public static function fromPublicKeyInfo(PublicKeyInfo $pki): self
69 1
    {
70
        return new self(false, $pki->keyIdentifier());
71
    }
72
73
    /**
74
     * Whether key identifier is present.
75
     *
76
     * @return bool
77 20
     */
78
    public function hasKeyIdentifier(): bool
79 20
    {
80 20
        return isset($this->_keyIdentifier);
81 20
    }
82 20
83 20
    /**
84 20
     * Get key identifier.
85 20
     *
86 20
     * @throws \LogicException If not set
87 20
     *
88
     * @return string
89 20
     */
90 10
    public function keyIdentifier(): string
91 1
    {
92
        if (!$this->hasKeyIdentifier()) {
93
            throw new \LogicException('keyIdentifier not set.');
94 1
        }
95
        return $this->_keyIdentifier;
96 9
    }
97 9
98 9
    /**
99 9
     * Whether issuer is present.
100 9
     *
101 9
     * @return bool
102 9
     */
103 9
    public function hasIssuer(): bool
104
    {
105 19
        return isset($this->_authorityCertIssuer);
106
    }
107
108
    /**
109
     * Get issuer.
110
     *
111
     * @throws \LogicException If not set
112
     *
113 12
     * @return GeneralNames
114
     */
115 12
    public function issuer(): GeneralNames
116
    {
117
        if (!$this->hasIssuer()) {
118
            throw new \LogicException('authorityCertIssuer not set.');
119
        }
120
        return $this->_authorityCertIssuer;
121
    }
122
123
    /**
124 12
     * Whether serial is present.
125
     *
126 12
     * @return bool
127 1
     */
128
    public function hasSerial(): bool
129 11
    {
130
        return isset($this->_authorityCertSerialNumber);
131
    }
132
133
    /**
134
     * Get serial number.
135
     *
136
     * @throws \LogicException If not set
137 5
     *
138
     * @return string Base 10 integer string
139 5
     */
140
    public function serial(): string
141
    {
142
        if (!$this->hasSerial()) {
143
            throw new \LogicException('authorityCertSerialNumber not set.');
144
        }
145
        return $this->_authorityCertSerialNumber;
146
    }
147
148 3
    /**
149
     * {@inheritdoc}
150 3
     */
151 1
    protected static function _fromDER(string $data, bool $critical): Extension
152
    {
153 2
        $seq = UnspecifiedType::fromDER($data)->asSequence();
154
        $keyIdentifier = null;
155
        $issuer = null;
156
        $serial = null;
157
        if ($seq->hasTagged(0)) {
158
            $keyIdentifier = $seq->getTagged(0)
159
                ->asImplicit(Element::TYPE_OCTET_STRING)
160
                ->asOctetString()->string();
161
        }
162 3
        if ($seq->hasTagged(1) || $seq->hasTagged(2)) {
163
            if (!$seq->hasTagged(1) || !$seq->hasTagged(2)) {
164
                throw new \UnexpectedValueException(
165 3
                    'AuthorityKeyIdentifier must have both' .
166 1
                        ' authorityCertIssuer and authorityCertSerialNumber' .
167
                        ' present or both absent.');
168 2
            }
169
            $issuer = GeneralNames::fromASN1($seq->getTagged(1)
170
                ->asImplicit(Element::TYPE_SEQUENCE)->asSequence());
171
            $serial = $seq->getTagged(2)->asImplicit(Element::TYPE_INTEGER)
172
                ->asInteger()->number();
173
        }
174
        return new self($critical, $keyIdentifier, $issuer, $serial);
175
    }
176 49
177
    /**
178 49
     * {@inheritdoc}
179 49
     */
180 49
    protected function _valueASN1(): Element
181 49
    {
182
        $elements = [];
183
        if (isset($this->_keyIdentifier)) {
184 49
            $elements[] = new ImplicitlyTaggedType(0,
185 49
                new OctetString($this->_keyIdentifier));
186 16
        }
187 16
        // if either issuer or serial is set, both must be set
188 1
        if (isset($this->_authorityCertIssuer) ||
189
             isset($this->_authorityCertSerialNumber)) {
190
            if (!isset($this->_authorityCertIssuer,
191 1
                $this->_authorityCertSerialNumber)) {
192
                throw new \LogicException(
193 15
                    'AuthorityKeyIdentifier must have both' .
194 15
                        ' authorityCertIssuer and authorityCertSerialNumber' .
195 15
                        ' present or both absent.');
196 15
            }
197
            $elements[] = new ImplicitlyTaggedType(1,
198 48
                $this->_authorityCertIssuer->toASN1());
199
            $elements[] = new ImplicitlyTaggedType(2,
200
                new Integer($this->_authorityCertSerialNumber));
201
        }
202
        return new Sequence(...$elements);
203
    }
204
}
205