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.
Completed
Branch php72 (a7f01e)
by Joni
04:53
created

AuthorityKeyIdentifierExtension::_fromDER()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 23
cts 23
cp 1
rs 8.8177
c 0
b 0
f 0
cc 6
nc 6
nop 2
crap 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A AuthorityKeyIdentifierExtension::hasKeyIdentifier() 0 3 1
A AuthorityKeyIdentifierExtension::hasIssuer() 0 3 1
A AuthorityKeyIdentifierExtension::keyIdentifier() 0 6 2
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
     */
68 1
    public static function fromPublicKeyInfo(PublicKeyInfo $pki): self
69
    {
70 1
        return new self(false, $pki->keyIdentifier());
71
    }
72
73
    /**
74
     * Whether key identifier is present.
75
     *
76
     * @return bool
77
     */
78 12
    public function hasKeyIdentifier(): bool
79
    {
80 12
        return isset($this->_keyIdentifier);
81
    }
82
83
    /**
84
     * Get key identifier.
85
     *
86
     * @throws \LogicException If not set
87
     *
88
     * @return string
89
     */
90 12
    public function keyIdentifier(): string
91
    {
92 12
        if (!$this->hasKeyIdentifier()) {
93 1
            throw new \LogicException('keyIdentifier not set.');
94
        }
95 11
        return $this->_keyIdentifier;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->_keyIdentifier could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
96
    }
97
98
    /**
99
     * Whether issuer is present.
100
     *
101
     * @return bool
102
     */
103 5
    public function hasIssuer(): bool
104
    {
105 5
        return isset($this->_authorityCertIssuer);
106
    }
107
108
    /**
109
     * Get issuer.
110
     *
111
     * @throws \LogicException If not set
112
     *
113
     * @return GeneralNames
114
     */
115 3
    public function issuer(): GeneralNames
116
    {
117 3
        if (!$this->hasIssuer()) {
118 1
            throw new \LogicException('authorityCertIssuer not set.');
119
        }
120 2
        return $this->_authorityCertIssuer;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->_authorityCertIssuer could return the type null which is incompatible with the type-hinted return Sop\X509\GeneralName\GeneralNames. Consider adding an additional type-check to rule them out.
Loading history...
121
    }
122
123
    /**
124
     * Get serial number.
125
     *
126
     * @throws \LogicException If not set
127
     *
128
     * @return string Base 10 integer string
129
     */
130 3
    public function serial(): string
131
    {
132
        // both issuer and serial must be present or both absent
133 3
        if (!$this->hasIssuer()) {
134 1
            throw new \LogicException('authorityCertSerialNumber not set.');
135
        }
136 2
        return $this->_authorityCertSerialNumber;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->_authorityCertSerialNumber could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 20
    protected static function _fromDER(string $data, bool $critical): Extension
143
    {
144 20
        $seq = UnspecifiedType::fromDER($data)->asSequence();
145 20
        $keyIdentifier = null;
146 20
        $issuer = null;
147 20
        $serial = null;
148 20
        if ($seq->hasTagged(0)) {
149 20
            $keyIdentifier = $seq->getTagged(0)
150 20
                ->asImplicit(Element::TYPE_OCTET_STRING)
151 20
                ->asOctetString()->string();
152
        }
153 20
        if ($seq->hasTagged(1) || $seq->hasTagged(2)) {
154 10
            if (!$seq->hasTagged(1) || !$seq->hasTagged(2)) {
155 1
                throw new \UnexpectedValueException(
156
                    'AuthorityKeyIdentifier must have both' .
157
                        ' authorityCertIssuer and authorityCertSerialNumber' .
158 1
                        ' present or both absent.');
159
            }
160 9
            $issuer = GeneralNames::fromASN1($seq->getTagged(1)
161 9
                ->asImplicit(Element::TYPE_SEQUENCE)->asSequence());
162 9
            $serial = $seq->getTagged(2)->asImplicit(Element::TYPE_INTEGER)
163 9
                ->asInteger()->number();
164
        }
165 19
        return new self($critical, $keyIdentifier, $issuer, $serial);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 49
    protected function _valueASN1(): Element
172
    {
173 49
        $elements = [];
174 49
        if (isset($this->_keyIdentifier)) {
175 49
            $elements[] = new ImplicitlyTaggedType(0,
176 49
                new OctetString($this->_keyIdentifier));
1 ignored issue
show
Bug introduced by
It seems like $this->_keyIdentifier can also be of type null; however, parameter $string of Sop\ASN1\Type\Primitive\OctetString::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

176
                new OctetString(/** @scrutinizer ignore-type */ $this->_keyIdentifier));
Loading history...
177
        }
178
        // if either issuer or serial is set, both must be set
179 49
        if (isset($this->_authorityCertIssuer) ||
180 49
             isset($this->_authorityCertSerialNumber)) {
181 16
            if (!isset($this->_authorityCertIssuer,
182 16
                $this->_authorityCertSerialNumber)) {
183 1
                throw new \LogicException(
184
                    'AuthorityKeyIdentifier must have both' .
185
                        ' authorityCertIssuer and authorityCertSerialNumber' .
186 1
                        ' present or both absent.');
187
            }
188 15
            $elements[] = new ImplicitlyTaggedType(1,
189 15
                $this->_authorityCertIssuer->toASN1());
1 ignored issue
show
Bug introduced by
The method toASN1() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

189
                $this->_authorityCertIssuer->/** @scrutinizer ignore-call */ 
190
                                             toASN1());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
190 15
            $elements[] = new ImplicitlyTaggedType(2,
191 15
                new Integer($this->_authorityCertSerialNumber));
192
        }
193 48
        return new Sequence(...$elements);
194
    }
195
}
196