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

CertificationRequest::toPEM()   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 *CertificationRequest* 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
    public function __toString(): string
64 4
    {
65
        return $this->toPEM()->string();
66 4
    }
67 4
68 4
    /**
69 1
     * Initialize from ASN.1.
70 1
     *
71
     * @param Sequence $seq
72 3
     *
73 3
     * @return self
74 3
     */
75 3
    public static function fromASN1(Sequence $seq): self
76 3
    {
77
        $info = CertificationRequestInfo::fromASN1($seq->at(0)->asSequence());
78
        $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence());
79
        if (!$algo instanceof SignatureAlgorithmIdentifier) {
80
            throw new \UnexpectedValueException(
81
                'Unsupported signature algorithm ' . $algo->oid() . '.');
82
        }
83
        $signature = Signature::fromSignatureData(
84
            $seq->at(2)->asBitString()->string(), $algo);
85 2
        return new self($info, $algo, $signature);
86
    }
87 2
88
    /**
89
     * Initialize from DER.
90
     *
91
     * @param string $data
92
     *
93
     * @return self
94
     */
95
    public static function fromDER(string $data): self
96
    {
97 3
        return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence());
98
    }
99 3
100 1
    /**
101
     * Initialize from PEM.
102 2
     *
103
     * @param PEM $pem
104
     *
105
     * @throws \UnexpectedValueException
106
     *
107
     * @return self
108
     */
109
    public static function fromPEM(PEM $pem): self
110 3
    {
111
        if (PEM::TYPE_CERTIFICATE_REQUEST !== $pem->type()) {
112 3
            throw new \UnexpectedValueException('Invalid PEM type.');
113
        }
114
        return self::fromDER($pem->data());
115
    }
116
117
    /**
118
     * Get certification request info.
119
     *
120 2
     * @return CertificationRequestInfo
121
     */
122 2
    public function certificationRequestInfo(): CertificationRequestInfo
123
    {
124
        return $this->_certificationRequestInfo;
125
    }
126
127
    /**
128
     * Get signature algorithm.
129
     *
130 2
     * @return SignatureAlgorithmIdentifier
131
     */
132 2
    public function signatureAlgorithm(): SignatureAlgorithmIdentifier
133
    {
134
        return $this->_signatureAlgorithm;
135
    }
136
137
    /**
138
     * Get signature.
139
     *
140 4
     * @return Signature
141
     */
142 4
    public function signature(): Signature
143 4
    {
144
        return $this->_signature;
145
    }
146
147
    /**
148
     * Generate ASN.1 structure.
149
     *
150
     * @return Sequence
151 2
     */
152
    public function toASN1(): Sequence
153 2
    {
154
        return new Sequence($this->_certificationRequestInfo->toASN1(),
155
            $this->_signatureAlgorithm->toASN1(), $this->_signature->bitString());
156
    }
157
158
    /**
159
     * Get certification request as a DER.
160
     *
161 2
     * @return string
162
     */
163 2
    public function toDER(): string
164
    {
165
        return $this->toASN1()->toDER();
166
    }
167
168
    /**
169
     * Get certification request as a PEM.
170
     *
171
     * @return PEM
172 1
     */
173
    public function toPEM(): PEM
174 1
    {
175 1
        return new PEM(PEM::TYPE_CERTIFICATE_REQUEST, $this->toDER());
176 1
    }
177 1
178 1
    /**
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
    public function verify(?Crypto $crypto = null): bool
186 1
    {
187
        $crypto = $crypto ?? Crypto::getDefault();
188 1
        $data = $this->_certificationRequestInfo->toASN1()->toDER();
189
        $pk_info = $this->_certificationRequestInfo->subjectPKInfo();
190
        return $crypto->verify($data, $this->_signature, $pk_info,
191
            $this->_signatureAlgorithm);
192
    }
193
}
194