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

AttributeCertificate::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\AttributeCertificate;
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\Asymmetric\PublicKeyInfo;
14
use Sop\CryptoTypes\Signature\Signature;
15
use Sop\X509\Certificate\Certificate;
16
17
/**
18
 * Implements <i>AttributeCertificate</i> ASN.1 type.
19
 *
20
 * @see https://tools.ietf.org/html/rfc5755#section-4.1
21
 */
22
class AttributeCertificate
23
{
24
    /**
25
     * Attribute certificate info.
26
     *
27
     * @var AttributeCertificateInfo
28
     */
29
    protected $_acinfo;
30
31
    /**
32
     * Signature algorithm identifier.
33
     *
34
     * @var SignatureAlgorithmIdentifier
35
     */
36
    protected $_signatureAlgorithm;
37
38
    /**
39
     * Signature value.
40
     *
41
     * @var Signature
42
     */
43
    protected $_signatureValue;
44
45
    /**
46
     * Constructor.
47
     *
48
     * @param AttributeCertificateInfo     $acinfo
49
     * @param SignatureAlgorithmIdentifier $algo
50
     * @param Signature                    $signature
51
     */
52 5
    public function __construct(AttributeCertificateInfo $acinfo,
53
        SignatureAlgorithmIdentifier $algo, Signature $signature)
54
    {
55 5
        $this->_acinfo = $acinfo;
56 5
        $this->_signatureAlgorithm = $algo;
57 5
        $this->_signatureValue = $signature;
58 5
    }
59
60
    /**
61
     * Get attribute certificate as a PEM formatted string.
62
     *
63
     * @return string
64
     */
65 1
    public function __toString(): string
66
    {
67 1
        return $this->toPEM()->string();
68
    }
69
70
    /**
71
     * Initialize from ASN.1.
72
     *
73
     * @param Sequence $seq
74
     *
75
     * @return self
76
     */
77 4
    public static function fromASN1(Sequence $seq): self
78
    {
79 4
        $acinfo = AttributeCertificateInfo::fromASN1($seq->at(0)->asSequence());
80 4
        $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence());
81 4
        if (!$algo instanceof SignatureAlgorithmIdentifier) {
82 1
            throw new \UnexpectedValueException(
83 1
                'Unsupported signature algorithm ' . $algo->oid() . '.');
84
        }
85 3
        $signature = Signature::fromSignatureData(
86 3
            $seq->at(2)->asBitString()->string(), $algo);
87 3
        return new self($acinfo, $algo, $signature);
88
    }
89
90
    /**
91
     * Initialize from DER data.
92
     *
93
     * @param string $data
94
     *
95
     * @return self
96
     */
97 1
    public static function fromDER(string $data): self
98
    {
99 1
        return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence());
100
    }
101
102
    /**
103
     * Initialize from PEM.
104
     *
105
     * @param PEM $pem
106
     *
107
     * @throws \UnexpectedValueException
108
     *
109
     * @return self
110
     */
111 2
    public static function fromPEM(PEM $pem): self
112
    {
113 2
        if (PEM::TYPE_ATTRIBUTE_CERTIFICATE !== $pem->type()) {
114 1
            throw new \UnexpectedValueException('Invalid PEM type.');
115
        }
116 1
        return self::fromDER($pem->data());
117
    }
118
119
    /**
120
     * Get attribute certificate info.
121
     *
122
     * @return AttributeCertificateInfo
123
     */
124 7
    public function acinfo(): AttributeCertificateInfo
125
    {
126 7
        return $this->_acinfo;
127
    }
128
129
    /**
130
     * Get signature algorithm identifier.
131
     *
132
     * @return SignatureAlgorithmIdentifier
133
     */
134 2
    public function signatureAlgorithm(): SignatureAlgorithmIdentifier
135
    {
136 2
        return $this->_signatureAlgorithm;
137
    }
138
139
    /**
140
     * Get signature value.
141
     *
142
     * @return Signature
143
     */
144 1
    public function signatureValue(): Signature
145
    {
146 1
        return $this->_signatureValue;
147
    }
148
149
    /**
150
     * Get ASN.1 structure.
151
     *
152
     * @return Sequence
153
     */
154 4
    public function toASN1(): Sequence
155
    {
156 4
        return new Sequence($this->_acinfo->toASN1(),
157 4
            $this->_signatureAlgorithm->toASN1(),
158 4
            $this->_signatureValue->bitString());
159
    }
160
161
    /**
162
     * Get attribute certificate as a DER.
163
     *
164
     * @return string
165
     */
166 2
    public function toDER(): string
167
    {
168 2
        return $this->toASN1()->toDER();
169
    }
170
171
    /**
172
     * Get attribute certificate as a PEM.
173
     *
174
     * @return PEM
175
     */
176 2
    public function toPEM(): PEM
177
    {
178 2
        return new PEM(PEM::TYPE_ATTRIBUTE_CERTIFICATE, $this->toDER());
179
    }
180
181
    /**
182
     * Check whether attribute certificate is issued to the subject identified
183
     * by given public key certificate.
184
     *
185
     * @param Certificate $cert Certificate
186
     *
187
     * @return bool
188
     */
189 13
    public function isHeldBy(Certificate $cert): bool
190
    {
191 13
        if (!$this->_acinfo->holder()->identifiesPKC($cert)) {
192 2
            return false;
193
        }
194 11
        return true;
195
    }
196
197
    /**
198
     * Check whether attribute certificate is issued by given public key
199
     * certificate.
200
     *
201
     * @param Certificate $cert Certificate
202
     *
203
     * @return bool
204
     */
205 11
    public function isIssuedBy(Certificate $cert): bool
206
    {
207 11
        if (!$this->_acinfo->issuer()->identifiesPKC($cert)) {
208 2
            return false;
209
        }
210 9
        return true;
211
    }
212
213
    /**
214
     * Verify signature.
215
     *
216
     * @param PublicKeyInfo $pubkey_info Signer's public key
217
     * @param null|Crypto   $crypto      Crypto engine, use default if not set
218
     *
219
     * @return bool
220
     */
221 10
    public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool
222
    {
223 10
        $crypto = $crypto ?? Crypto::getDefault();
224 10
        $data = $this->_acinfo->toASN1()->toDER();
225 10
        return $crypto->verify($data, $this->_signatureValue, $pubkey_info,
226 10
            $this->_signatureAlgorithm);
227
    }
228
}
229