1 | <?php |
||
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 |
||
297 |