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

CertificationPath::fromCertificateChain()   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 1
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\X509\CertificationPath;
6
7
use Sop\CryptoBridge\Crypto;
8
use Sop\X509\Certificate\Certificate;
9
use Sop\X509\Certificate\CertificateBundle;
10
use Sop\X509\Certificate\CertificateChain;
11
use Sop\X509\CertificationPath\PathBuilding\CertificationPathBuilder;
12
use Sop\X509\CertificationPath\PathValidation\PathValidationConfig;
13
use Sop\X509\CertificationPath\PathValidation\PathValidationResult;
14
use Sop\X509\CertificationPath\PathValidation\PathValidator;
15
16
/**
17
 * Implements certification path structure.
18
 *
19
 * Certification path is a list of certificates from the trust anchor to
20
 * the end entity certificate, possibly spanning over multiple intermediate
21
 * certificates.
22
 *
23
 * @see https://tools.ietf.org/html/rfc5280#section-3.2
24
 */
25
class CertificationPath implements \Countable, \IteratorAggregate
26
{
27
    /**
28
     * Certification path.
29
     *
30
     * @var Certificate[]
31
     */
32
    protected $_certificates;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param Certificate ...$certificates Certificates from the trust anchor
38
     *                                     to the target end-entity certificate
39 38
     */
40
    public function __construct(Certificate ...$certificates)
41 38
    {
42 38
        $this->_certificates = $certificates;
43
    }
44
45
    /**
46
     * Initialize from a certificate chain.
47
     *
48
     * @param CertificateChain $chain
49
     *
50 2
     * @return self
51
     */
52 2
    public static function fromCertificateChain(CertificateChain $chain): self
53
    {
54
        return new self(...array_reverse($chain->certificates(), false));
55
    }
56
57
    /**
58
     * Build certification path to given target.
59
     *
60
     * @param Certificate            $target        Target end-entity certificate
61
     * @param CertificateBundle      $trust_anchors List of trust anchors
62
     * @param null|CertificateBundle $intermediate  Optional intermediate certificates
63
     *
64 2
     * @return self
65
     */
66
    public static function toTarget(Certificate $target,
67 2
        CertificateBundle $trust_anchors, ?CertificateBundle $intermediate = null): self
68 2
    {
69
        $builder = new CertificationPathBuilder($trust_anchors);
70
        return $builder->shortestPathToTarget($target, $intermediate);
71
    }
72
73
    /**
74
     * Build certification path from given trust anchor to target certificate,
75
     * using intermediate certificates from given bundle.
76
     *
77
     * @param Certificate            $trust_anchor Trust anchor certificate
78
     * @param Certificate            $target       Target end-entity certificate
79
     * @param null|CertificateBundle $intermediate Optional intermediate certificates
80
     *
81 2
     * @return self
82
     */
83
    public static function fromTrustAnchorToTarget(Certificate $trust_anchor,
84 2
        Certificate $target, ?CertificateBundle $intermediate = null): self
85 2
    {
86
        return self::toTarget($target, new CertificateBundle($trust_anchor),
87
            $intermediate);
88
    }
89
90
    /**
91
     * Get certificates.
92
     *
93 5
     * @return Certificate[]
94
     */
95 5
    public function certificates(): array
96
    {
97
        return $this->_certificates;
98
    }
99
100
    /**
101
     * Get the trust anchor certificate from the path.
102
     *
103
     * @throws \LogicException If path is empty
104 2
     *
105
     * @return Certificate
106 2
     */
107 1
    public function trustAnchorCertificate(): Certificate
108
    {
109 1
        if (!count($this->_certificates)) {
110
            throw new \LogicException('No certificates.');
111
        }
112
        return $this->_certificates[0];
113
    }
114
115
    /**
116
     * Get the end-entity certificate from the path.
117
     *
118 2
     * @throws \LogicException If path is empty
119
     *
120 2
     * @return Certificate
121 1
     */
122
    public function endEntityCertificate(): Certificate
123 1
    {
124
        if (!count($this->_certificates)) {
125
            throw new \LogicException('No certificates.');
126
        }
127
        return $this->_certificates[count($this->_certificates) - 1];
128
    }
129
130
    /**
131 1
     * Get certification path as a certificate chain.
132
     *
133 1
     * @return CertificateChain
134 1
     */
135
    public function certificateChain(): CertificateChain
136
    {
137
        return new CertificateChain(...array_reverse($this->_certificates, false));
138
    }
139
140
    /**
141
     * Check whether certification path starts with one ore more given
142
     * certificates in parameter order.
143
     *
144 5
     * @param Certificate ...$certs Certificates
145
     *
146 5
     * @return bool
147 5
     */
148 1
    public function startsWith(Certificate ...$certs): bool
149
    {
150 4
        $n = count($certs);
151 4
        if ($n > count($this->_certificates)) {
152 1
            return false;
153
        }
154
        for ($i = 0; $i < $n; ++$i) {
155 3
            if (!$certs[$i]->equals($this->_certificates[$i])) {
156
                return false;
157
            }
158
        }
159
        return true;
160
    }
161
162
    /**
163
     * Validate certification path.
164
     *
165
     * @param PathValidationConfig $config
166 43
     * @param null|Crypto          $crypto Crypto engine, use default if not set
167
     *
168 43
     * @throws Exception\PathValidationException
169 43
     *
170 43
     * @return PathValidationResult
171
     */
172
    public function validate(PathValidationConfig $config,
173
        ?Crypto $crypto = null): PathValidationResult
174
    {
175
        $crypto = $crypto ?? Crypto::getDefault();
176
        $validator = new PathValidator($crypto, $config, ...$this->_certificates);
177
        return $validator->validate();
178 18
    }
179
180 18
    /**
181
     * @see \Countable::count()
182
     *
183
     * @return int
184
     */
185
    public function count(): int
186
    {
187
        return count($this->_certificates);
188
    }
189 1
190
    /**
191 1
     * Get iterator for certificates.
192
     *
193
     * @see \IteratorAggregate::getIterator()
194
     *
195
     * @return \ArrayIterator
196
     */
197
    public function getIterator(): \ArrayIterator
198
    {
199
        return new \ArrayIterator($this->_certificates);
200
    }
201
}
202