|
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
|
|
|
* Execute some command and return result |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $cmd |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
private function exec(string $cmd): array |
|
46
|
|
|
{ |
|
47
|
|
|
chdir($this->_certs); |
|
48
|
|
|
exec($this->_scripts . '/easyrsa3/easyrsa --batch ' . $cmd, $result); |
|
49
|
|
|
return $result; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function init_pki(): array |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->exec('init-pki'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function build_ca(bool $nopass = false): array |
|
58
|
|
|
{ |
|
59
|
|
|
$param = $nopass ? 'nopass' : ''; |
|
60
|
|
|
return $this->exec("build-ca $param"); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function gen_dh(): array |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->exec('gen-dh'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function gen_req(string $name, bool $nopass = false): array |
|
69
|
|
|
{ |
|
70
|
|
|
$param = $nopass ? 'nopass' : ''; |
|
71
|
|
|
return $this->exec("gen-req $name $param"); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function sign_req_client(string $filename): array |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->exec("sign-req server $filename"); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function sign_req_server(string $filename): array |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->exec("sign-req client $filename"); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function build_client_full(string $name, bool $nopass = false): array |
|
85
|
|
|
{ |
|
86
|
|
|
$param = $nopass ? 'nopass' : ''; |
|
87
|
|
|
return $this->exec("build-client-full $name $param"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function build_server_full(string $name, bool $nopass = false): array |
|
91
|
|
|
{ |
|
92
|
|
|
$param = $nopass ? 'nopass' : ''; |
|
93
|
|
|
return $this->exec("build-server-full $name $param"); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function revoke(string $filename): array |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->exec("revoke $filename"); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function gen_crl(): array |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->exec('gen-crl'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function update_db(): array |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->exec('update-db'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function show_req(string $filename): array |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->exec("show-req $filename"); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function show_cert(string $filename): array |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->exec("show-cert $filename"); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function import_req(string $request_file_path, string $short_basename): array |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->exec("import-req $request_file_path $short_basename"); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function export_p7(string $filename): array |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->exec("export-p7 $filename"); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function export_p12(string $filename): array |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->exec("export-p12 $filename"); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function set_rsa_pass(string $filename): array |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->exec("set-rsa-pass $filename"); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function set_ec_pass(string $filename): array |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->exec("set-ec-pass $filename"); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|