|
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
|
|
|
* Constructor |
|
23
|
|
|
* |
|
24
|
|
|
* @param Certificate ...$certs Certificate objects |
|
25
|
|
|
*/ |
|
26
|
15 |
|
public function __construct(Certificate ...$certs) { |
|
27
|
15 |
|
$this->_certs = $certs; |
|
28
|
15 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Initialize from PEMs. |
|
32
|
|
|
* |
|
33
|
|
|
* @param PEM ...$pems PEM objects |
|
34
|
|
|
* @return self |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public static function fromPEMs(PEM ...$pems) { |
|
37
|
2 |
|
$certs = array_map( |
|
38
|
2 |
|
function ($pem) { |
|
39
|
2 |
|
return Certificate::fromPEM($pem); |
|
40
|
2 |
|
}, $pems); |
|
41
|
2 |
|
return new self(...$certs); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Initialize from PEM bundle. |
|
46
|
|
|
* |
|
47
|
|
|
* @param PEMBundle $pem_bundle |
|
48
|
|
|
* @return self |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public static function fromPEMBundle(PEMBundle $pem_bundle) { |
|
51
|
1 |
|
return self::fromPEMs(...$pem_bundle->all()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get self with certificates added. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Certificate ...$cert |
|
58
|
|
|
* @return self |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function withCertificates(Certificate ...$cert) { |
|
61
|
1 |
|
$obj = clone $this; |
|
62
|
1 |
|
$obj->_certs = array_merge($obj->_certs, $cert); |
|
63
|
1 |
|
return $obj; |
|
64
|
1 |
|
} |
|
65
|
1 |
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get self with certificates from PEMBundle added. |
|
68
|
|
|
* |
|
69
|
|
|
* @param PEMBundle $pem_bundle |
|
70
|
|
|
* @return self |
|
71
|
|
|
*/ |
|
72
|
|
|
public function withPEMBundle(PEMBundle $pem_bundle) { |
|
73
|
|
|
$certs = $this->_certs; |
|
74
|
1 |
|
foreach ($pem_bundle as $pem) { |
|
75
|
1 |
|
$certs[] = Certificate::fromPEM($pem); |
|
76
|
1 |
|
} |
|
77
|
1 |
|
return new self(...$certs); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get self with single certificate from PEM added. |
|
82
|
|
|
* |
|
83
|
|
|
* @param PEM $pem |
|
84
|
|
|
* @return self |
|
85
|
|
|
*/ |
|
86
|
11 |
|
public function withPEM(PEM $pem) { |
|
87
|
11 |
|
$certs = $this->_certs; |
|
88
|
11 |
|
$certs[] = Certificate::fromPEM($pem); |
|
89
|
11 |
|
return new self(...$certs); |
|
90
|
11 |
|
} |
|
91
|
1 |
|
|
|
92
|
|
|
/** |
|
93
|
10 |
|
* Get all certificates that have given subject key identifier. |
|
94
|
9 |
|
* |
|
95
|
9 |
|
* @param string $id |
|
96
|
11 |
|
* @return Certificate[] |
|
97
|
11 |
|
*/ |
|
98
|
|
|
public function allBySubjectKeyIdentifier($id) { |
|
99
|
|
|
$certs = array(); |
|
100
|
|
|
foreach ($this->_certs as $cert) { |
|
101
|
|
|
$extensions = $cert->tbsCertificate()->extensions(); |
|
102
|
|
|
if (!$extensions->hasSubjectKeyIdentifier()) { |
|
103
|
|
|
continue; |
|
104
|
|
|
} |
|
105
|
1 |
|
if ($id === $extensions->subjectKeyIdentifier()->keyIdentifier()) { |
|
106
|
1 |
|
$certs[] = $cert; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
return $certs; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get all certificates in a bundle. |
|
114
|
3 |
|
* |
|
115
|
3 |
|
* @return Certificate[] |
|
116
|
|
|
*/ |
|
117
|
|
|
public function all() { |
|
118
|
|
|
return $this->_certs; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* |
|
123
|
|
|
* @see Countable::count() |
|
124
|
1 |
|
* @return int |
|
125
|
1 |
|
*/ |
|
126
|
|
|
public function count() { |
|
127
|
|
|
return count($this->_certs); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get iterator for certificates. |
|
132
|
|
|
* |
|
133
|
|
|
* @see IteratorAggregate::getIterator() |
|
134
|
|
|
* @return \ArrayIterator |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getIterator() { |
|
137
|
|
|
return new \ArrayIterator($this->_certs); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|