|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace X509\CertificationPath; |
|
4
|
|
|
|
|
5
|
|
|
use CryptoUtil\Crypto\Crypto; |
|
6
|
|
|
use X509\Certificate\Certificate; |
|
7
|
|
|
use X509\Certificate\CertificateBundle; |
|
8
|
|
|
use X509\Certificate\CertificateChain; |
|
9
|
|
|
use X509\CertificationPath\Exception\PathValidationException; |
|
10
|
|
|
use X509\CertificationPath\PathBuilding\CertificationPathBuilder; |
|
11
|
|
|
use X509\CertificationPath\PathValidation\PathValidationConfig; |
|
12
|
|
|
use X509\CertificationPath\PathValidation\PathValidationResult; |
|
13
|
|
|
use X509\CertificationPath\PathValidation\PathValidator; |
|
14
|
|
|
|
|
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
|
|
|
* @link 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[] $_certificates |
|
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
|
30 |
|
public function __construct(Certificate ...$certificates) { |
|
41
|
30 |
|
$this->_certificates = $certificates; |
|
42
|
30 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Initialize from a certificate chain. |
|
46
|
|
|
* |
|
47
|
|
|
* @param CertificateChain $chain |
|
48
|
|
|
* @return self |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public static function fromCertificateChain(CertificateChain $chain) { |
|
51
|
2 |
|
return new self(...array_reverse($chain->certificates(), false)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Build certification path to given target. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Certificate $target Target end-entity certificate |
|
58
|
|
|
* @param CertificateBundle $trust_anchors List of trust anchors |
|
59
|
|
|
* @param CertificateBundle|null $intermediate Optional intermediate |
|
60
|
|
|
* certificates |
|
61
|
|
|
* @return self |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public static function toTarget(Certificate $target, |
|
64
|
|
|
CertificateBundle $trust_anchors, |
|
65
|
|
|
CertificateBundle $intermediate = null) { |
|
66
|
2 |
|
$builder = new CertificationPathBuilder($trust_anchors); |
|
67
|
2 |
|
return $builder->shortestPathToTarget($target, $intermediate); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Build certification path from given trust anchor to target certificate, |
|
72
|
|
|
* using intermediate certificates from given bundle. |
|
73
|
|
|
* |
|
74
|
|
|
* @param Certificate $trust_anchor Trust anchor certificate |
|
75
|
|
|
* @param Certificate $target Target end-entity certificate |
|
76
|
|
|
* @param CertificateBundle|null $intermediate Optional intermediate |
|
77
|
|
|
* certificates |
|
78
|
|
|
* @return self |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public static function fromTrustAnchorToTarget(Certificate $trust_anchor, |
|
81
|
|
|
Certificate $target, CertificateBundle $intermediate = null) { |
|
82
|
2 |
|
return self::toTarget($target, new CertificateBundle($trust_anchor), |
|
83
|
2 |
|
$intermediate); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get certificates. |
|
88
|
|
|
* |
|
89
|
|
|
* @return Certificate[] |
|
90
|
|
|
*/ |
|
91
|
5 |
|
public function certificates() { |
|
92
|
5 |
|
return $this->_certificates; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the trust anchor certificate from the path. |
|
97
|
|
|
* |
|
98
|
|
|
* @throws \LogicException If path is empty |
|
99
|
|
|
* @return Certificate |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function trustAnchorCertificate() { |
|
102
|
2 |
|
if (!count($this->_certificates)) { |
|
103
|
1 |
|
throw new \LogicException("No certificates."); |
|
104
|
|
|
} |
|
105
|
1 |
|
return $this->_certificates[0]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the end-entity certificate from the path. |
|
110
|
|
|
* |
|
111
|
|
|
* @throws \LogicException If path is empty |
|
112
|
|
|
* @return Certificate |
|
113
|
|
|
*/ |
|
114
|
2 |
|
public function endEntityCertificate() { |
|
115
|
2 |
|
if (!count($this->_certificates)) { |
|
116
|
1 |
|
throw new \LogicException("No certificates."); |
|
117
|
|
|
} |
|
118
|
1 |
|
return $this->_certificates[count($this->_certificates) - 1]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Check whether certification path starts with one ore more given |
|
123
|
|
|
* certificates in parameter order. |
|
124
|
|
|
* |
|
125
|
|
|
* @param Certificate ...$certs Certificates |
|
126
|
|
|
* @return true |
|
127
|
|
|
*/ |
|
128
|
5 |
|
public function startsWith(Certificate ...$certs) { |
|
129
|
5 |
|
$n = count($certs); |
|
130
|
5 |
|
if ($n > count($this->_certificates)) { |
|
131
|
1 |
|
return false; |
|
132
|
|
|
} |
|
133
|
4 |
|
for ($i = 0; $i < $n; ++$i) { |
|
134
|
4 |
|
if (!$certs[$i]->equals($this->_certificates[$i])) { |
|
135
|
1 |
|
return false; |
|
136
|
|
|
} |
|
137
|
3 |
|
} |
|
138
|
3 |
|
return true; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Validate certification path. |
|
143
|
|
|
* |
|
144
|
|
|
* @param Crypto $crypto |
|
145
|
|
|
* @param PathValidationConfig $config |
|
146
|
|
|
* @throws PathValidationException |
|
147
|
|
|
* @return PathValidationResult |
|
148
|
|
|
*/ |
|
149
|
35 |
|
public function validate(Crypto $crypto, PathValidationConfig $config) { |
|
150
|
35 |
|
$validator = new PathValidator($crypto, $config, ...$this->_certificates); |
|
151
|
35 |
|
return $validator->validate(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* |
|
156
|
|
|
* @see Countable::count() |
|
157
|
|
|
* @return int |
|
158
|
|
|
*/ |
|
159
|
18 |
|
public function count() { |
|
160
|
18 |
|
return count($this->_certificates); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Get iterator for certificates. |
|
165
|
|
|
* |
|
166
|
|
|
* @see IteratorAggregate::getIterator() |
|
167
|
|
|
* @return \ArrayIterator |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function getIterator() { |
|
170
|
1 |
|
return new \ArrayIterator($this->_certificates); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|