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
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get self with certificates from PEMBundle added. |
68
|
|
|
* |
69
|
|
|
* @param PEMBundle $pem_bundle |
70
|
|
|
* @return self |
71
|
|
|
*/ |
72
|
1 |
|
public function withPEMBundle(PEMBundle $pem_bundle) { |
73
|
1 |
|
$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
|
1 |
|
public function withPEM(PEM $pem) { |
87
|
1 |
|
$certs = $this->_certs; |
88
|
1 |
|
$certs[] = Certificate::fromPEM($pem); |
89
|
1 |
|
return new self(...$certs); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get all certificates that have given subject key identifier. |
94
|
|
|
* |
95
|
|
|
* @param string $id |
96
|
|
|
* @return Certificate[] |
97
|
|
|
*/ |
98
|
11 |
|
public function allBySubjectKeyIdentifier($id) { |
99
|
11 |
|
$certs = array(); |
100
|
11 |
|
foreach ($this->_certs as $cert) { |
101
|
11 |
|
$extensions = $cert->tbsCertificate()->extensions(); |
102
|
11 |
|
if (!$extensions->hasSubjectKeyIdentifier()) { |
103
|
1 |
|
continue; |
104
|
|
|
} |
105
|
10 |
|
if ($id === $extensions->subjectKeyIdentifier()->keyIdentifier()) { |
106
|
9 |
|
$certs[] = $cert; |
107
|
9 |
|
} |
108
|
11 |
|
} |
109
|
11 |
|
return $certs; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get all certificates in a bundle. |
114
|
|
|
* |
115
|
|
|
* @return Certificate[] |
116
|
|
|
*/ |
117
|
1 |
|
public function all() { |
118
|
1 |
|
return $this->_certs; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* |
123
|
|
|
* @see Countable::count() |
124
|
|
|
* @return int |
125
|
|
|
*/ |
126
|
4 |
|
public function count() { |
127
|
4 |
|
return count($this->_certs); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get iterator for certificates. |
132
|
|
|
* |
133
|
|
|
* @see IteratorAggregate::getIterator() |
134
|
|
|
* @return \ArrayIterator |
135
|
|
|
*/ |
136
|
1 |
|
public function getIterator() { |
137
|
1 |
|
return new \ArrayIterator($this->_certs); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|