1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EasyRSA; |
6
|
|
|
|
7
|
|
|
use EasyRSA\Interfaces\ConfigInterface; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
class Wrapper |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Main location of EasyRSA scripts |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private string $scripts; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Path to folder with certificates |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private string $certs; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* If need to make certificate without password |
28
|
|
|
*/ |
29
|
|
|
private const NOPASS = 'nopass'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* If need to enable debug mode |
33
|
|
|
*/ |
34
|
|
|
public bool $dryRun = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Wrapper constructor, need configuration for normal usage |
38
|
|
|
* |
39
|
|
|
* @param \EasyRSA\Interfaces\ConfigInterface $config |
40
|
|
|
* |
41
|
|
|
* @throws \RuntimeException |
42
|
|
|
*/ |
43
|
|
|
public function __construct(ConfigInterface $config) |
44
|
|
|
{ |
45
|
|
|
// Create folders for certificates |
46
|
|
|
$this->certs = $config->getCerts(); |
47
|
|
|
putenv("EASYRSA_PKI={$this->certs}"); |
48
|
|
|
if (!mkdir($this->certs, 0755, true) || is_dir($this->certs)) { |
49
|
|
|
throw new RuntimeException("Folder {$this->certs} can't be created"); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->scripts = $config->getScripts(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Show content of certificate file |
57
|
|
|
* |
58
|
|
|
* @param string $filename Only name of file must be set, without path |
59
|
|
|
* |
60
|
|
|
* @return string|null |
61
|
|
|
*/ |
62
|
|
|
public function getContent(string $filename): ?string |
63
|
|
|
{ |
64
|
|
|
switch ($filename) { |
65
|
|
|
case 'ca.crt': |
66
|
|
|
case 'dh.pem': |
67
|
|
|
$path = $this->certs . '/' . $filename; |
68
|
|
|
break; |
69
|
|
|
default: |
70
|
|
|
$ext = pathinfo($filename, PATHINFO_EXTENSION); |
71
|
|
|
switch ($ext) { |
72
|
|
|
case 'crt': |
73
|
|
|
$path = $this->certs . '/issued/' . $filename; |
74
|
|
|
break; |
75
|
|
|
case 'key': |
76
|
|
|
$path = $this->certs . '/private/' . $filename; |
77
|
|
|
break; |
78
|
|
|
case 'req': |
79
|
|
|
$path = $this->certs . '/reqs/' . $filename; |
80
|
|
|
break; |
81
|
|
|
default: |
82
|
|
|
return null; |
83
|
|
|
break; |
84
|
|
|
} |
85
|
|
|
break; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return file_get_contents($path); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Execute some command and return result |
93
|
|
|
* |
94
|
|
|
* @param string $cmd |
95
|
|
|
* |
96
|
|
|
* @return array<string> |
97
|
|
|
*/ |
98
|
|
|
private function exec(string $cmd): array |
99
|
|
|
{ |
100
|
|
|
$command = $this->scripts . '/easyrsa3/easyrsa --batch ' . $cmd; |
101
|
|
|
|
102
|
|
|
// In dry run mode need just return command without real execution |
103
|
|
|
if ($this->dryRun) { |
104
|
|
|
$result = [$command]; |
105
|
|
|
} else { |
106
|
|
|
chdir($this->certs); |
107
|
|
|
exec($this->scripts . '/easyrsa3/easyrsa --batch ' . $cmd, $result); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $result; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array<string> |
115
|
|
|
*/ |
116
|
|
|
public function init_pki(): array |
117
|
|
|
{ |
118
|
|
|
return $this->exec('init-pki'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param bool $nopass |
123
|
|
|
* |
124
|
|
|
* @return array<string> |
125
|
|
|
*/ |
126
|
|
|
public function build_ca(bool $nopass = false): array |
127
|
|
|
{ |
128
|
|
|
$param = $nopass ? self::NOPASS : ''; |
129
|
|
|
|
130
|
|
|
return $this->exec("build-ca $param"); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array<string> |
135
|
|
|
*/ |
136
|
|
|
public function gen_dh(): array |
137
|
|
|
{ |
138
|
|
|
return $this->exec('gen-dh'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $name |
143
|
|
|
* @param bool $nopass |
144
|
|
|
* |
145
|
|
|
* @return array<string> |
146
|
|
|
*/ |
147
|
|
|
public function gen_req(string $name, bool $nopass = false): array |
148
|
|
|
{ |
149
|
|
|
$param = $nopass ? self::NOPASS : ''; |
150
|
|
|
|
151
|
|
|
return $this->exec("gen-req $name $param"); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $filename |
156
|
|
|
* |
157
|
|
|
* @return array<string> |
158
|
|
|
*/ |
159
|
|
|
public function sign_req_client(string $filename): array |
160
|
|
|
{ |
161
|
|
|
return $this->exec("sign-req server $filename"); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $filename |
166
|
|
|
* |
167
|
|
|
* @return array<string> |
168
|
|
|
*/ |
169
|
|
|
public function sign_req_server(string $filename): array |
170
|
|
|
{ |
171
|
|
|
return $this->exec("sign-req client $filename"); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $name |
176
|
|
|
* @param bool $nopass |
177
|
|
|
* |
178
|
|
|
* @return array<string> |
179
|
|
|
*/ |
180
|
|
|
public function build_client_full(string $name, bool $nopass = false): array |
181
|
|
|
{ |
182
|
|
|
$param = $nopass ? self::NOPASS : ''; |
183
|
|
|
|
184
|
|
|
return $this->exec("build-client-full $name $param"); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $name |
189
|
|
|
* @param bool $nopass |
190
|
|
|
* |
191
|
|
|
* @return array<string> |
192
|
|
|
*/ |
193
|
|
|
public function build_server_full(string $name, bool $nopass = false): array |
194
|
|
|
{ |
195
|
|
|
$param = $nopass ? self::NOPASS : ''; |
196
|
|
|
|
197
|
|
|
return $this->exec("build-server-full $name $param"); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $filename |
202
|
|
|
* |
203
|
|
|
* @return array<string> |
204
|
|
|
*/ |
205
|
|
|
public function revoke(string $filename): array |
206
|
|
|
{ |
207
|
|
|
return $this->exec("revoke $filename"); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return array<string> |
212
|
|
|
*/ |
213
|
|
|
public function gen_crl(): array |
214
|
|
|
{ |
215
|
|
|
return $this->exec('gen-crl'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return array<string> |
220
|
|
|
*/ |
221
|
|
|
public function update_db(): array |
222
|
|
|
{ |
223
|
|
|
return $this->exec('update-db'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param string $filename |
228
|
|
|
* |
229
|
|
|
* @return array<string> |
230
|
|
|
*/ |
231
|
|
|
public function show_req(string $filename): array |
232
|
|
|
{ |
233
|
|
|
return $this->exec("show-req $filename"); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param string $filename |
238
|
|
|
* |
239
|
|
|
* @return array<string> |
240
|
|
|
*/ |
241
|
|
|
public function show_cert(string $filename): array |
242
|
|
|
{ |
243
|
|
|
return $this->exec("show-cert $filename"); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param string $request_file_path |
248
|
|
|
* @param string $short_basename |
249
|
|
|
* |
250
|
|
|
* @return array<string> |
251
|
|
|
*/ |
252
|
|
|
public function import_req(string $request_file_path, string $short_basename): array |
253
|
|
|
{ |
254
|
|
|
return $this->exec("import-req $request_file_path $short_basename"); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param string $filename |
259
|
|
|
* |
260
|
|
|
* @return array<string> |
261
|
|
|
*/ |
262
|
|
|
public function export_p7(string $filename): array |
263
|
|
|
{ |
264
|
|
|
return $this->exec("export-p7 $filename"); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param string $filename |
269
|
|
|
* |
270
|
|
|
* @return array<string> |
271
|
|
|
*/ |
272
|
|
|
public function export_p12(string $filename): array |
273
|
|
|
{ |
274
|
|
|
return $this->exec("export-p12 $filename"); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param string $filename |
279
|
|
|
* |
280
|
|
|
* @return array<string> |
281
|
|
|
*/ |
282
|
|
|
public function set_rsa_pass(string $filename): array |
283
|
|
|
{ |
284
|
|
|
return $this->exec("set-rsa-pass $filename"); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param string $filename |
289
|
|
|
* |
290
|
|
|
* @return array<string> |
291
|
|
|
*/ |
292
|
|
|
public function set_ec_pass(string $filename): array |
293
|
|
|
{ |
294
|
|
|
return $this->exec("set-ec-pass $filename"); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|