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

CertificationPath::count()   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\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
     */
40 38
    public function __construct(Certificate ...$certificates)
41
    {
42 38
        $this->_certificates = $certificates;
43 38
    }
44
45
    /**
46
     * Initialize from a certificate chain.
47
     *
48
     * @param CertificateChain $chain
49
     *
50
     * @return self
51
     */
52 2
    public static function fromCertificateChain(CertificateChain $chain): self
53
    {
54 2
        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
     * @return self
65
     */
66 2
    public static function toTarget(Certificate $target,
67
        CertificateBundle $trust_anchors, ?CertificateBundle $intermediate = null): self
68
    {
69 2
        $builder = new CertificationPathBuilder($trust_anchors);
70 2
        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
     * @return self
82
     */
83 2
    public static function fromTrustAnchorToTarget(Certificate $trust_anchor,
84
        Certificate $target, ?CertificateBundle $intermediate = null): self
85
    {
86 2
        return self::toTarget($target, new CertificateBundle($trust_anchor),
87 2
            $intermediate);
88
    }
89
90
    /**
91
     * Get certificates.
92
     *
93
     * @return Certificate[]
94
     */
95 5
    public function certificates(): array
96
    {
97 5
        return $this->_certificates;
98
    }
99
100
    /**
101
     * Get the trust anchor certificate from the path.
102
     *
103
     * @throws \LogicException If path is empty
104
     *
105
     * @return Certificate
106
     */
107 2
    public function trustAnchorCertificate(): Certificate
108
    {
109 2
        if (!count($this->_certificates)) {
110 1
            throw new \LogicException('No certificates.');
111
        }
112 1
        return $this->_certificates[0];
113
    }
114
115
    /**
116
     * Get the end-entity certificate from the path.
117
     *
118
     * @throws \LogicException If path is empty
119
     *
120
     * @return Certificate
121
     */
122 2
    public function endEntityCertificate(): Certificate
123
    {
124 2
        if (!count($this->_certificates)) {
125 1
            throw new \LogicException('No certificates.');
126
        }
127 1
        return $this->_certificates[count($this->_certificates) - 1];
128
    }
129
130
    /**
131
     * Get certification path as a certificate chain.
132
     *
133
     * @return CertificateChain
134
     */
135 1
    public function certificateChain(): CertificateChain
136
    {
137 1
        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
     * @param Certificate ...$certs Certificates
145
     *
146
     * @return true
147
     */
148 5
    public function startsWith(Certificate ...$certs): bool
149
    {
150 5
        $n = count($certs);
151 5
        if ($n > count($this->_certificates)) {
152 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type true.
Loading history...
153
        }
154 4
        for ($i = 0; $i < $n; ++$i) {
155 4
            if (!$certs[$i]->equals($this->_certificates[$i])) {
156 1
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type true.
Loading history...
157
            }
158
        }
159 3
        return true;
160
    }
161
162
    /**
163
     * Validate certification path.
164
     *
165
     * @param PathValidationConfig $config
166
     * @param null|Crypto          $crypto Crypto engine, use default if not set
167
     *
168
     * @throws Exception\PathValidationException
169
     *
170
     * @return PathValidationResult
171
     */
172 43
    public function validate(PathValidationConfig $config,
173
        ?Crypto $crypto = null): PathValidationResult
174
    {
175 43
        $crypto = $crypto ?? Crypto::getDefault();
176 43
        $validator = new PathValidator($crypto, $config, ...$this->_certificates);
177 43
        return $validator->validate();
178
    }
179
180
    /**
181
     * @see \Countable::count()
182
     *
183
     * @return int
184
     */
185 18
    public function count(): int
186
    {
187 18
        return count($this->_certificates);
188
    }
189
190
    /**
191
     * Get iterator for certificates.
192
     *
193
     * @see \IteratorAggregate::getIterator()
194
     *
195
     * @return \ArrayIterator
196
     */
197 1
    public function getIterator(): \ArrayIterator
198
    {
199 1
        return new \ArrayIterator($this->_certificates);
200
    }
201
}
202