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

CertificationRequest::signatureAlgorithm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
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\CertificationRequest;
6
7
use Sop\ASN1\Type\Constructed\Sequence;
8
use Sop\ASN1\Type\UnspecifiedType;
9
use Sop\CryptoBridge\Crypto;
10
use Sop\CryptoEncoding\PEM;
11
use Sop\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier;
12
use Sop\CryptoTypes\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier;
13
use Sop\CryptoTypes\Signature\Signature;
14
15
/**
16
 * Implements <i>CertificationRequest</i> ASN.1 type.
17
 *
18
 * @see https://tools.ietf.org/html/rfc2986#section-4
19
 */
20
class CertificationRequest
21
{
22
    /**
23
     * Certification request info.
24
     *
25
     * @var CertificationRequestInfo
26
     */
27
    protected $_certificationRequestInfo;
28
29
    /**
30
     * Signature algorithm.
31
     *
32
     * @var SignatureAlgorithmIdentifier
33
     */
34
    protected $_signatureAlgorithm;
35
36
    /**
37
     * Signature.
38
     *
39
     * @var Signature
40
     */
41
    protected $_signature;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param CertificationRequestInfo     $info
47
     * @param SignatureAlgorithmIdentifier $algo
48
     * @param Signature                    $signature
49
     */
50 6
    public function __construct(CertificationRequestInfo $info,
51
        SignatureAlgorithmIdentifier $algo, Signature $signature)
52
    {
53 6
        $this->_certificationRequestInfo = $info;
54 6
        $this->_signatureAlgorithm = $algo;
55 6
        $this->_signature = $signature;
56 6
    }
57
58
    /**
59
     * Get certification request as a PEM formatted string.
60
     *
61
     * @return string
62
     */
63 1
    public function __toString(): string
64
    {
65 1
        return $this->toPEM()->string();
66
    }
67
68
    /**
69
     * Initialize from ASN.1.
70
     *
71
     * @param Sequence $seq
72
     *
73
     * @return self
74
     */
75 4
    public static function fromASN1(Sequence $seq): self
76
    {
77 4
        $info = CertificationRequestInfo::fromASN1($seq->at(0)->asSequence());
78 4
        $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence());
79 4
        if (!$algo instanceof SignatureAlgorithmIdentifier) {
80 1
            throw new \UnexpectedValueException(
81 1
                'Unsupported signature algorithm ' . $algo->oid() . '.');
82
        }
83 3
        $signature = Signature::fromSignatureData(
84 3
            $seq->at(2)->asBitString()->string(), $algo);
85 3
        return new self($info, $algo, $signature);
86
    }
87
88
    /**
89
     * Initialize from DER.
90
     *
91
     * @param string $data
92
     *
93
     * @return self
94
     */
95 2
    public static function fromDER(string $data): self
96
    {
97 2
        return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence());
98
    }
99
100
    /**
101
     * Initialize from PEM.
102
     *
103
     * @param PEM $pem
104
     *
105
     * @throws \UnexpectedValueException
106
     *
107
     * @return self
108
     */
109 3
    public static function fromPEM(PEM $pem): self
110
    {
111 3
        if (PEM::TYPE_CERTIFICATE_REQUEST !== $pem->type()) {
112 1
            throw new \UnexpectedValueException('Invalid PEM type.');
113
        }
114 2
        return self::fromDER($pem->data());
115
    }
116
117
    /**
118
     * Get certification request info.
119
     *
120
     * @return CertificationRequestInfo
121
     */
122 3
    public function certificationRequestInfo(): CertificationRequestInfo
123
    {
124 3
        return $this->_certificationRequestInfo;
125
    }
126
127
    /**
128
     * Get signature algorithm.
129
     *
130
     * @return SignatureAlgorithmIdentifier
131
     */
132 2
    public function signatureAlgorithm(): SignatureAlgorithmIdentifier
133
    {
134 2
        return $this->_signatureAlgorithm;
135
    }
136
137
    /**
138
     * Get signature.
139
     *
140
     * @return Signature
141
     */
142 2
    public function signature(): Signature
143
    {
144 2
        return $this->_signature;
145
    }
146
147
    /**
148
     * Generate ASN.1 structure.
149
     *
150
     * @return Sequence
151
     */
152 4
    public function toASN1(): Sequence
153
    {
154 4
        return new Sequence($this->_certificationRequestInfo->toASN1(),
155 4
            $this->_signatureAlgorithm->toASN1(), $this->_signature->bitString());
156
    }
157
158
    /**
159
     * Get certification request as a DER.
160
     *
161
     * @return string
162
     */
163 2
    public function toDER(): string
164
    {
165 2
        return $this->toASN1()->toDER();
166
    }
167
168
    /**
169
     * Get certification request as a PEM.
170
     *
171
     * @return PEM
172
     */
173 2
    public function toPEM(): PEM
174
    {
175 2
        return new PEM(PEM::TYPE_CERTIFICATE_REQUEST, $this->toDER());
176
    }
177
178
    /**
179
     * Verify certification request signature.
180
     *
181
     * @param null|Crypto $crypto Crypto engine, use default if not set
182
     *
183
     * @return bool True if signature matches
184
     */
185 1
    public function verify(?Crypto $crypto = null): bool
186
    {
187 1
        $crypto = $crypto ?? Crypto::getDefault();
188 1
        $data = $this->_certificationRequestInfo->toASN1()->toDER();
189 1
        $pk_info = $this->_certificationRequestInfo->subjectPKInfo();
190 1
        return $crypto->verify($data, $this->_signature, $pk_info,
191 1
            $this->_signatureAlgorithm);
192
    }
193
}
194