1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyRSA; |
4
|
|
|
|
5
|
|
|
class Wrapper |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Main location of easyrsa scripts |
9
|
|
|
* @var string |
10
|
|
|
*/ |
11
|
|
|
private $_scripts; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Path to folder with certificates |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $_certs; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Wrapper constructor, need configuration for normal usage |
21
|
|
|
* |
22
|
|
|
* @param Config $config |
23
|
|
|
* @throws \RuntimeException |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Config $config) |
26
|
|
|
{ |
27
|
|
|
// Create folders for certificates |
28
|
|
|
$this->_certs = $config->getCerts(); |
29
|
|
|
putenv("EASYRSA_PKI={$this->_certs}"); |
30
|
|
|
if (@mkdir($this->_certs, 0755, true) || is_dir($this->_certs)) { |
31
|
|
|
error_log("Folder '{$this->_certs}' created"); |
32
|
|
|
} else { |
33
|
|
|
throw new \RuntimeException("Folder {$this->_certs} can't be created"); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->_scripts = $config->getScripts(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Show content of certificate file |
41
|
|
|
* |
42
|
|
|
* @param string $filename Only name of file must be set, without path |
43
|
|
|
* @return bool|string |
44
|
|
|
*/ |
45
|
|
|
public function getContent(string $filename) |
46
|
|
|
{ |
47
|
|
|
switch ($filename) { |
48
|
|
|
case 'ca.crt': |
49
|
|
|
case 'dh.pem': |
50
|
|
|
$path = $this->_certs . '/' . $filename; |
51
|
|
|
break; |
52
|
|
|
default: |
53
|
|
|
$ext = pathinfo($filename, PATHINFO_EXTENSION); |
54
|
|
|
switch ($ext) { |
55
|
|
|
case 'crt': |
56
|
|
|
$path = $this->_certs . '/issued/' . $filename; |
57
|
|
|
break; |
58
|
|
|
case 'key': |
59
|
|
|
$path = $this->_certs . '/private/' . $filename; |
60
|
|
|
break; |
61
|
|
|
case 'req': |
62
|
|
|
$path = $this->_certs . '/reqs/' . $filename; |
63
|
|
|
break; |
64
|
|
|
default: |
65
|
|
|
$path = false; |
66
|
|
|
break; |
67
|
|
|
} |
68
|
|
|
break; |
69
|
|
|
} |
70
|
|
|
return file_get_contents($path); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Execute some command and return result |
75
|
|
|
* |
76
|
|
|
* @param string $cmd |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
private function exec(string $cmd): array |
80
|
|
|
{ |
81
|
|
|
chdir($this->_certs); |
82
|
|
|
exec($this->_scripts . '/easyrsa3/easyrsa --batch ' . $cmd, $result); |
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function init_pki(): array |
87
|
|
|
{ |
88
|
|
|
return $this->exec('init-pki'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function build_ca(bool $nopass = false): array |
92
|
|
|
{ |
93
|
|
|
$param = $nopass ? 'nopass' : ''; |
94
|
|
|
return $this->exec("build-ca $param"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function gen_dh(): array |
98
|
|
|
{ |
99
|
|
|
return $this->exec('gen-dh'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function gen_req(string $name, bool $nopass = false): array |
103
|
|
|
{ |
104
|
|
|
$param = $nopass ? 'nopass' : ''; |
105
|
|
|
return $this->exec("gen-req $name $param"); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function sign_req_client(string $filename): array |
109
|
|
|
{ |
110
|
|
|
return $this->exec("sign-req server $filename"); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function sign_req_server(string $filename): array |
114
|
|
|
{ |
115
|
|
|
return $this->exec("sign-req client $filename"); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function build_client_full(string $name, bool $nopass = false): array |
119
|
|
|
{ |
120
|
|
|
$param = $nopass ? 'nopass' : ''; |
121
|
|
|
return $this->exec("build-client-full $name $param"); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function build_server_full(string $name, bool $nopass = false): array |
125
|
|
|
{ |
126
|
|
|
$param = $nopass ? 'nopass' : ''; |
127
|
|
|
return $this->exec("build-server-full $name $param"); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function revoke(string $filename): array |
131
|
|
|
{ |
132
|
|
|
return $this->exec("revoke $filename"); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function gen_crl(): array |
136
|
|
|
{ |
137
|
|
|
return $this->exec('gen-crl'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function update_db(): array |
141
|
|
|
{ |
142
|
|
|
return $this->exec('update-db'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function show_req(string $filename): array |
146
|
|
|
{ |
147
|
|
|
return $this->exec("show-req $filename"); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function show_cert(string $filename): array |
151
|
|
|
{ |
152
|
|
|
return $this->exec("show-cert $filename"); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function import_req(string $request_file_path, string $short_basename): array |
156
|
|
|
{ |
157
|
|
|
return $this->exec("import-req $request_file_path $short_basename"); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function export_p7(string $filename): array |
161
|
|
|
{ |
162
|
|
|
return $this->exec("export-p7 $filename"); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function export_p12(string $filename): array |
166
|
|
|
{ |
167
|
|
|
return $this->exec("export-p12 $filename"); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function set_rsa_pass(string $filename): array |
171
|
|
|
{ |
172
|
|
|
return $this->exec("set-rsa-pass $filename"); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function set_ec_pass(string $filename): array |
176
|
|
|
{ |
177
|
|
|
return $this->exec("set-ec-pass $filename"); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|