1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\CryptoEncoding; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Container for multiple PEM objects. |
9
|
|
|
* |
10
|
|
|
* The order of PEMs shall be retained, eg. when read from a file. |
11
|
|
|
*/ |
12
|
|
|
class PEMBundle implements \Countable, \IteratorAggregate |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Array of PEM objects. |
16
|
|
|
* |
17
|
|
|
* @var PEM[] $_pems |
18
|
|
|
*/ |
19
|
|
|
protected $_pems; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor. |
23
|
|
|
* |
24
|
|
|
* @param PEM ...$pems |
25
|
|
|
*/ |
26
|
|
|
public function __construct(PEM ...$pems) |
27
|
|
|
{ |
28
|
|
|
$this->_pems = $pems; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initialize from a string. |
33
|
|
|
* |
34
|
|
|
* @param string $str |
35
|
|
|
* @throws \UnexpectedValueException |
36
|
|
|
* @return self |
37
|
|
|
*/ |
38
|
|
|
public static function fromString(string $str): self |
39
|
|
|
{ |
40
|
|
|
if (!preg_match_all(PEM::PEM_REGEX, $str, $matches, PREG_SET_ORDER)) { |
41
|
|
|
throw new \UnexpectedValueException("No PEM blocks."); |
42
|
|
|
} |
43
|
|
|
$pems = array_map( |
44
|
|
|
function ($match) { |
45
|
|
|
$payload = preg_replace('/\s+/', "", $match[2]); |
46
|
|
|
$data = base64_decode($payload, true); |
47
|
|
|
if (false === $data) { |
48
|
|
|
throw new \UnexpectedValueException( |
49
|
|
|
"Failed to decode PEM data."); |
50
|
|
|
} |
51
|
|
|
return new PEM($match[1], $data); |
52
|
|
|
}, $matches); |
53
|
|
|
return new self(...$pems); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Initialize from a file. |
58
|
|
|
* |
59
|
|
|
* @param string $filename |
60
|
|
|
* @throws \RuntimeException If file reading fails |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
|
|
public static function fromFile($filename): self |
64
|
|
|
{ |
65
|
|
|
if (!is_readable($filename) || |
66
|
|
|
false === ($str = file_get_contents($filename))) { |
67
|
|
|
throw new \RuntimeException("Failed to read $filename."); |
68
|
|
|
} |
69
|
|
|
return self::fromString($str); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get self with PEM objects appended. |
74
|
|
|
* |
75
|
|
|
* @param PEM ...$pems |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
|
|
public function withPEMs(PEM ...$pems): self |
79
|
|
|
{ |
80
|
|
|
$obj = clone $this; |
81
|
|
|
$obj->_pems = array_merge($obj->_pems, $pems); |
82
|
|
|
return $obj; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get all PEMs in a bundle. |
87
|
|
|
* |
88
|
|
|
* @return PEM[] |
89
|
|
|
*/ |
90
|
|
|
public function all(): array |
91
|
|
|
{ |
92
|
|
|
return $this->_pems; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the first PEM in a bundle. |
97
|
|
|
* |
98
|
|
|
* @throws \LogicException If bundle contains no PEM objects |
99
|
|
|
* @return PEM |
100
|
|
|
*/ |
101
|
|
|
public function first(): PEM |
102
|
|
|
{ |
103
|
|
|
if (!count($this->_pems)) { |
104
|
|
|
throw new \LogicException("No PEMs."); |
105
|
|
|
} |
106
|
|
|
return $this->_pems[0]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* |
111
|
|
|
* @see \Countable::count() |
112
|
|
|
* @return int |
113
|
|
|
*/ |
114
|
|
|
public function count(): int |
115
|
|
|
{ |
116
|
|
|
return count($this->_pems); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get iterator for PEMs. |
121
|
|
|
* |
122
|
|
|
* @see \IteratorAggregate::getIterator() |
123
|
|
|
* @return \ArrayIterator |
124
|
|
|
*/ |
125
|
|
|
public function getIterator(): \ArrayIterator |
126
|
|
|
{ |
127
|
|
|
return new \ArrayIterator($this->_pems); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Encode bundle to a string of contiguous PEM blocks. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function string(): string |
136
|
|
|
{ |
137
|
|
|
return implode("\n", |
138
|
|
|
array_map( |
139
|
|
|
function (PEM $pem) { |
140
|
|
|
return $pem->string(); |
141
|
|
|
}, $this->_pems)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function __toString() |
149
|
|
|
{ |
150
|
|
|
return $this->string(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|