1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace X509\Certificate; |
4
|
|
|
|
5
|
|
|
use CryptoUtil\PEM\PEM; |
6
|
|
|
use CryptoUtil\PEM\PEMBundle; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Implements a list of certificates. |
11
|
|
|
*/ |
12
|
|
|
class CertificateBundle implements \Countable, \IteratorAggregate |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Certificates. |
16
|
|
|
* |
17
|
|
|
* @var Certificate[] $_certs |
18
|
|
|
*/ |
19
|
|
|
protected $_certs; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Mapping from public key id to array of certificates. |
23
|
|
|
* |
24
|
|
|
* @var (Certificate[])[] |
25
|
|
|
*/ |
26
|
|
|
private $_keyIdMap; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
* |
31
|
|
|
* @param Certificate ...$certs Certificate objects |
32
|
|
|
*/ |
33
|
17 |
|
public function __construct(Certificate ...$certs) { |
34
|
17 |
|
$this->_certs = $certs; |
35
|
17 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Reset internal cached variables on clone. |
39
|
|
|
*/ |
40
|
1 |
|
public function __clone() { |
41
|
1 |
|
$this->_keyIdMap = null; |
|
|
|
|
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Initialize from PEMs. |
46
|
|
|
* |
47
|
|
|
* @param PEM ...$pems PEM objects |
48
|
|
|
* @return self |
49
|
|
|
*/ |
50
|
2 |
|
public static function fromPEMs(PEM ...$pems) { |
51
|
2 |
|
$certs = array_map( |
52
|
2 |
|
function ($pem) { |
53
|
2 |
|
return Certificate::fromPEM($pem); |
54
|
2 |
|
}, $pems); |
55
|
2 |
|
return new self(...$certs); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Initialize from PEM bundle. |
60
|
|
|
* |
61
|
|
|
* @param PEMBundle $pem_bundle |
62
|
|
|
* @return self |
63
|
|
|
*/ |
64
|
1 |
|
public static function fromPEMBundle(PEMBundle $pem_bundle) { |
65
|
1 |
|
return self::fromPEMs(...$pem_bundle->all()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get self with certificates added. |
70
|
|
|
* |
71
|
|
|
* @param Certificate ...$cert |
72
|
|
|
* @return self |
73
|
|
|
*/ |
74
|
1 |
|
public function withCertificates(Certificate ...$cert) { |
75
|
1 |
|
$obj = clone $this; |
76
|
1 |
|
$obj->_certs = array_merge($obj->_certs, $cert); |
77
|
1 |
|
return $obj; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get self with certificates from PEMBundle added. |
82
|
|
|
* |
83
|
|
|
* @param PEMBundle $pem_bundle |
84
|
|
|
* @return self |
85
|
|
|
*/ |
86
|
1 |
|
public function withPEMBundle(PEMBundle $pem_bundle) { |
87
|
1 |
|
$certs = $this->_certs; |
88
|
1 |
|
foreach ($pem_bundle as $pem) { |
89
|
1 |
|
$certs[] = Certificate::fromPEM($pem); |
90
|
1 |
|
} |
91
|
1 |
|
return new self(...$certs); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get self with single certificate from PEM added. |
96
|
|
|
* |
97
|
|
|
* @param PEM $pem |
98
|
|
|
* @return self |
99
|
|
|
*/ |
100
|
1 |
|
public function withPEM(PEM $pem) { |
101
|
1 |
|
$certs = $this->_certs; |
102
|
1 |
|
$certs[] = Certificate::fromPEM($pem); |
103
|
1 |
|
return new self(...$certs); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check whether bundle contains a given certificate. |
108
|
|
|
* |
109
|
|
|
* @param Certificate $cert |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
3 |
|
public function contains(Certificate $cert) { |
113
|
3 |
|
$id = self::_getCertKeyId($cert); |
114
|
3 |
|
$map = $this->_getKeyIdMap(); |
115
|
3 |
|
if (!isset($map[$id])) { |
116
|
1 |
|
return false; |
117
|
|
|
} |
118
|
2 |
|
foreach ($map[$id] as $c) { |
119
|
|
|
/** @var Certificate $c */ |
120
|
2 |
|
if ($cert->equals($c)) { |
121
|
1 |
|
return true; |
122
|
|
|
} |
123
|
1 |
|
} |
124
|
1 |
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get all certificates that have given subject key identifier. |
129
|
|
|
* |
130
|
|
|
* @param string $id |
131
|
|
|
* @return Certificate[] |
132
|
|
|
*/ |
133
|
11 |
|
public function allBySubjectKeyIdentifier($id) { |
134
|
11 |
|
$map = $this->_getKeyIdMap(); |
135
|
11 |
|
if (!isset($map[$id])) { |
136
|
7 |
|
return array(); |
137
|
|
|
} |
138
|
9 |
|
return $map[$id]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get all certificates in a bundle. |
143
|
|
|
* |
144
|
|
|
* @return Certificate[] |
145
|
|
|
*/ |
146
|
1 |
|
public function all() { |
147
|
1 |
|
return $this->_certs; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get certificate mapping by public key id. |
152
|
|
|
* |
153
|
|
|
* @return (Certificate[])[] |
154
|
|
|
*/ |
155
|
14 |
|
private function _getKeyIdMap() { |
156
|
|
|
// lazily build mapping |
157
|
14 |
|
if (!isset($this->_keyIdMap)) { |
158
|
13 |
|
$this->_keyIdMap = array(); |
159
|
13 |
|
foreach ($this->_certs as $cert) { |
160
|
13 |
|
$id = self::_getCertKeyId($cert); |
161
|
13 |
|
if (!isset($this->_keyIdMap[$id])) { |
162
|
13 |
|
$this->_keyIdMap[$id] = array(); |
163
|
13 |
|
} |
164
|
13 |
|
array_push($this->_keyIdMap[$id], $cert); |
165
|
13 |
|
} |
166
|
13 |
|
} |
167
|
14 |
|
return $this->_keyIdMap; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get public key id for the certificate. |
172
|
|
|
* |
173
|
|
|
* @param Certificate $cert |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
13 |
|
private static function _getCertKeyId(Certificate $cert) { |
177
|
13 |
|
$exts = $cert->tbsCertificate()->extensions(); |
178
|
13 |
|
if ($exts->hasSubjectKeyIdentifier()) { |
179
|
11 |
|
return $exts->subjectKeyIdentifier()->keyIdentifier(); |
180
|
|
|
} |
181
|
2 |
|
return $cert->tbsCertificate() |
182
|
2 |
|
->subjectPublicKeyInfo() |
183
|
2 |
|
->keyIdentifier(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* |
188
|
|
|
* @see Countable::count() |
189
|
|
|
* @return int |
190
|
|
|
*/ |
191
|
4 |
|
public function count() { |
192
|
4 |
|
return count($this->_certs); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get iterator for certificates. |
197
|
|
|
* |
198
|
|
|
* @see IteratorAggregate::getIterator() |
199
|
|
|
* @return \ArrayIterator |
200
|
|
|
*/ |
201
|
1 |
|
public function getIterator() { |
202
|
1 |
|
return new \ArrayIterator($this->_certs); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..