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\CertificationPath\Exception\PathValidationException; |
9
|
|
|
use X509\CertificationPath\PathBuilding\CertificationPathBuilder; |
10
|
|
|
use X509\CertificationPath\PathValidation\PathValidationConfig; |
11
|
|
|
use X509\CertificationPath\PathValidation\PathValidationResult; |
12
|
|
|
use X509\CertificationPath\PathValidation\PathValidator; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Implements certification path structure. |
17
|
|
|
* |
18
|
|
|
* Certification path is a list of certificates from the trust anchor to |
19
|
|
|
* the end entity certificate, possibly spanning over multiple intermediate |
20
|
|
|
* certificates. |
21
|
|
|
* |
22
|
|
|
* @link https://tools.ietf.org/html/rfc5280#section-3.2 |
23
|
|
|
*/ |
24
|
|
|
class CertificationPath implements \Countable, \IteratorAggregate |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Certification path. |
28
|
|
|
* |
29
|
|
|
* @var Certificate[] $_certificates |
30
|
|
|
*/ |
31
|
|
|
protected $_certificates; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor |
35
|
|
|
* |
36
|
|
|
* @param Certificate ...$certificates Certificates from the trust anchor |
37
|
|
|
* to the target end-entity certificate |
38
|
|
|
*/ |
39
|
26 |
|
public function __construct(Certificate ...$certificates) { |
40
|
26 |
|
$this->_certificates = $certificates; |
41
|
26 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Build certification path to given target. |
45
|
|
|
* |
46
|
|
|
* @param Certificate $target Target end-entity certificate |
47
|
|
|
* @param CertificateBundle $trust_anchors List of trust anchors |
48
|
|
|
* @param CertificateBundle|null $intermediate Optional intermediate |
49
|
|
|
* certificates |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
2 |
|
public static function toTarget(Certificate $target, |
53
|
|
|
CertificateBundle $trust_anchors, |
54
|
|
|
CertificateBundle $intermediate = null) { |
55
|
2 |
|
$builder = new CertificationPathBuilder($trust_anchors); |
56
|
2 |
|
return $builder->shortestPathToTarget($target, $intermediate); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Build certification path from given trust anchor to target certificate, |
61
|
|
|
* using intermediate certificates from given bundle. |
62
|
|
|
* |
63
|
|
|
* @param Certificate $trust_anchor Trust anchor certificate |
64
|
|
|
* @param Certificate $target Target end-entity certificate |
65
|
|
|
* @param CertificateBundle|null $intermediate Optional intermediate |
66
|
|
|
* certificates |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
2 |
|
public static function fromTrustAnchorToTarget(Certificate $trust_anchor, |
70
|
|
|
Certificate $target, CertificateBundle $intermediate = null) { |
71
|
2 |
|
return self::toTarget($target, new CertificateBundle($trust_anchor), |
72
|
2 |
|
$intermediate); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get certificates. |
77
|
|
|
* |
78
|
|
|
* @return Certificate[] |
79
|
|
|
*/ |
80
|
5 |
|
public function certificates() { |
81
|
5 |
|
return $this->_certificates; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check whether certification path starts with one ore more given |
86
|
|
|
* certificates in parameter order. |
87
|
|
|
* |
88
|
|
|
* @param Certificate ...$certs Certificates |
89
|
|
|
* @return true |
90
|
|
|
*/ |
91
|
5 |
|
public function startsWith(Certificate ...$certs) { |
92
|
5 |
|
$n = count($certs); |
93
|
5 |
|
if ($n > count($this->_certificates)) { |
94
|
1 |
|
return false; |
95
|
|
|
} |
96
|
4 |
|
for ($i = 0; $i < $n; ++$i) { |
97
|
5 |
|
if (!$certs[$i]->equals($this->_certificates[$i])) { |
98
|
1 |
|
return false; |
99
|
|
|
} |
100
|
3 |
|
} |
101
|
3 |
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Validate certification path. |
106
|
|
|
* |
107
|
|
|
* @param Crypto $crypto |
108
|
|
|
* @param PathValidationConfig $config |
109
|
|
|
* @throws PathValidationException |
110
|
|
|
* @return PathValidationResult |
111
|
|
|
*/ |
112
|
35 |
|
public function validate(Crypto $crypto, PathValidationConfig $config) { |
113
|
35 |
|
$validator = new PathValidator($crypto, $config, ...$this->_certificates); |
114
|
35 |
|
return $validator->validate(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* |
119
|
|
|
* @see Countable::count() |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
18 |
|
public function count() { |
123
|
18 |
|
return count($this->_certificates); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get iterator for certificates. |
128
|
|
|
* |
129
|
|
|
* @see IteratorAggregate::getIterator() |
130
|
|
|
* @return \ArrayIterator |
131
|
|
|
*/ |
132
|
1 |
|
public function getIterator() { |
133
|
1 |
|
return new \ArrayIterator($this->_certificates); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|