|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EasyRSA; |
|
6
|
|
|
|
|
7
|
|
|
use EasyRSA\Interfaces\ConfigInterface; |
|
8
|
|
|
use EasyRSA\Interfaces\WorkerInterface; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
|
|
11
|
|
|
class Worker implements WorkerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \EasyRSA\Interfaces\ConfigInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
protected ConfigInterface $config; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* If need to enable debug mode |
|
20
|
|
|
*/ |
|
21
|
|
|
public bool $dryRun = false; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Wrapper constructor, need configuration for normal usage |
|
25
|
|
|
* |
|
26
|
|
|
* @param \EasyRSA\Interfaces\ConfigInterface|array $config |
|
27
|
|
|
* |
|
28
|
|
|
* @throws \RuntimeException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($config = []) |
|
31
|
|
|
{ |
|
32
|
|
|
if (is_array($config)) { |
|
33
|
|
|
$config = new Config($config); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if ($config instanceof ConfigInterface) { |
|
|
|
|
|
|
37
|
|
|
$this->config = $config; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// Must be set to enable ability execute shell scripts in background |
|
41
|
|
|
putenv("EASYRSA_PKI={$this->config->get('certs')}"); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return \EasyRSA\Interfaces\ConfigInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getConfig(): ConfigInterface |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->config; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getContent(string $filename): ?string |
|
56
|
|
|
{ |
|
57
|
|
|
switch ($filename) { |
|
58
|
|
|
case 'ca.crt': |
|
59
|
|
|
case 'dh.pem': |
|
60
|
|
|
$path = $this->config->get('certs') . '/' . $filename; |
|
61
|
|
|
break; |
|
62
|
|
|
default: |
|
63
|
|
|
$ext = pathinfo($filename, PATHINFO_EXTENSION); |
|
64
|
|
|
switch ($ext) { |
|
65
|
|
|
case 'crt': |
|
66
|
|
|
$path = $this->config->get('certs') . '/issued/' . $filename; |
|
67
|
|
|
break; |
|
68
|
|
|
case 'key': |
|
69
|
|
|
$path = $this->config->get('certs') . '/private/' . $filename; |
|
70
|
|
|
break; |
|
71
|
|
|
case 'req': |
|
72
|
|
|
$path = $this->config->get('certs') . '/reqs/' . $filename; |
|
73
|
|
|
break; |
|
74
|
|
|
default: |
|
75
|
|
|
return null; |
|
76
|
|
|
break; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
break; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return file_get_contents($path); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritdoc} |
|
86
|
|
|
* |
|
87
|
|
|
* @throws \RuntimeException Of folder with certs unable to create or if is not a folder |
|
88
|
|
|
*/ |
|
89
|
|
|
public function exec(string $cmd): array |
|
90
|
|
|
{ |
|
91
|
|
|
$command = $this->config->get('scripts') . '/easyrsa3/easyrsa --batch ' . $cmd; |
|
92
|
|
|
|
|
93
|
|
|
// In dry run mode need just return command without real execution |
|
94
|
|
|
if ($this->dryRun) { |
|
95
|
|
|
$result = [$command]; |
|
96
|
|
|
} else { |
|
97
|
|
|
// Create folder if not exist |
|
98
|
|
|
if (!mkdir($this->config->get('certs'), 0755, true) && !is_dir($this->config->get('certs'))) { |
|
99
|
|
|
throw new RuntimeException("Folder \"{$this->config->get('certs')}\" can't be created"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
chdir($this->config->get('certs')); |
|
103
|
|
|
exec($command, $result); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $result; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|