|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace SURFnet\VPN\Server\CA; |
|
20
|
|
|
|
|
21
|
|
|
use SURFnet\VPN\Common\Config; |
|
22
|
|
|
use SURFnet\VPN\Common\FileIO; |
|
23
|
|
|
use SURFnet\VPN\Server\CA\Exception\CaException; |
|
24
|
|
|
use RuntimeException; |
|
25
|
|
|
|
|
26
|
|
|
class EasyRsaCa implements CaInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
private $easyRsaDir; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
private $easyRsaDataDir; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct($easyRsaDir, $easyRsaDataDir) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->easyRsaDir = $easyRsaDir; |
|
37
|
|
|
$this->easyRsaDataDir = $easyRsaDataDir; |
|
38
|
|
|
FileIO::createDir($this->easyRsaDataDir, 0700); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Initialize the CA. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \SURFnet\VPN\Common\Config $config the CA configuration |
|
45
|
|
|
*/ |
|
46
|
|
|
public function init(Config $config) |
|
47
|
|
|
{ |
|
48
|
|
|
// only initialize when unitialized, prevent destroying existing CA |
|
49
|
|
|
if (!@file_exists(sprintf('%s/vars', $this->easyRsaDataDir))) { |
|
50
|
|
|
$configData = [ |
|
51
|
|
|
sprintf('set_var EASYRSA "%s"', $this->easyRsaDir), |
|
52
|
|
|
sprintf('set_var EASYRSA_PKI "%s/pki"', $this->easyRsaDataDir), |
|
53
|
|
|
sprintf('set_var EASYRSA_KEY_SIZE %d', $config->i('CA', 'key_size')), |
|
54
|
|
|
sprintf('set_var EASYRSA_CA_EXPIRE %d', $config->i('CA', 'ca_expire')), |
|
55
|
|
|
sprintf('set_var EASYRSA_CERT_EXPIRE %d', $config->i('CA', 'cert_expire')), |
|
56
|
|
|
sprintf('set_var EASYRSA_REQ_CN "%s"', $config->s('CA', 'ca_cn')), |
|
57
|
|
|
'set_var EASYRSA_BATCH "1"', |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
FileIO::writeFile( |
|
61
|
|
|
sprintf('%s/vars', $this->easyRsaDataDir), |
|
62
|
|
|
implode(PHP_EOL, $configData).PHP_EOL, |
|
63
|
|
|
0600 |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$this->execEasyRsa(['init-pki']); |
|
67
|
|
|
$this->execEasyRsa(['build-ca', 'nopass']); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get the CA root certificate. |
|
73
|
|
|
* |
|
74
|
|
|
* @return string the CA certificate in PEM format |
|
75
|
|
|
*/ |
|
76
|
|
|
public function caCert() |
|
77
|
|
|
{ |
|
78
|
|
|
$certFile = sprintf('%s/pki/ca.crt', $this->easyRsaDataDir); |
|
79
|
|
|
|
|
80
|
|
|
return $this->readCertificate($certFile); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Generate a certificate for the VPN server. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $commonName |
|
87
|
|
|
* |
|
88
|
|
|
* @return array the certificate, key in array with keys |
|
89
|
|
|
* 'cert', 'key', 'valid_from' and 'valid_to' |
|
90
|
|
|
*/ |
|
91
|
|
View Code Duplication |
public function serverCert($commonName) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
if ($this->hasCert($commonName)) { |
|
94
|
|
|
throw new CaException(sprintf('certificate with commonName "%s" already exists', $commonName)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->execEasyRsa(['build-server-full', $commonName, 'nopass']); |
|
98
|
|
|
|
|
99
|
|
|
return $this->certInfo($commonName); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Generate a certificate for a VPN client. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $commonName |
|
106
|
|
|
* |
|
107
|
|
|
* @return array the certificate and key in array with keys 'cert', 'key', |
|
108
|
|
|
* 'valid_from' and 'valid_to' |
|
109
|
|
|
*/ |
|
110
|
|
View Code Duplication |
public function clientCert($commonName) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
if ($this->hasCert($commonName)) { |
|
113
|
|
|
throw new CaException(sprintf('certificate with commonName "%s" already exists', $commonName)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$this->execEasyRsa(['build-client-full', $commonName, 'nopass']); |
|
117
|
|
|
|
|
118
|
|
|
return $this->certInfo($commonName); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function certInfo($commonName) |
|
122
|
|
|
{ |
|
123
|
|
|
$certData = $this->readCertificate(sprintf('%s/pki/issued/%s.crt', $this->easyRsaDataDir, $commonName)); |
|
124
|
|
|
$keyData = $this->readKey(sprintf('%s/pki/private/%s.key', $this->easyRsaDataDir, $commonName)); |
|
125
|
|
|
|
|
126
|
|
|
$parsedCert = openssl_x509_parse($certData); |
|
127
|
|
|
|
|
128
|
|
|
return [ |
|
129
|
|
|
'certificate' => $certData, |
|
130
|
|
|
'private_key' => $keyData, |
|
131
|
|
|
'valid_from' => $parsedCert['validFrom_time_t'], |
|
132
|
|
|
'valid_to' => $parsedCert['validTo_time_t'], |
|
133
|
|
|
]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
private function readCertificate($certFile) |
|
137
|
|
|
{ |
|
138
|
|
|
// strip junk before and after actual certificate |
|
139
|
|
|
$pattern = '/(-----BEGIN CERTIFICATE-----.*-----END CERTIFICATE-----)/msU'; |
|
140
|
|
|
if (1 !== preg_match($pattern, FileIO::readFile($certFile), $matches)) { |
|
141
|
|
|
throw new CaException('unable to extract certificate'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $matches[1]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
private function readKey($keyFile) |
|
148
|
|
|
{ |
|
149
|
|
|
// strip whitespace before and after actual key |
|
150
|
|
|
return trim( |
|
151
|
|
|
FileIO::readFile($keyFile) |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
private function hasCert($commonName) |
|
156
|
|
|
{ |
|
157
|
|
|
return @file_exists( |
|
158
|
|
|
sprintf( |
|
159
|
|
|
'%s/pki/issued/%s.crt', |
|
160
|
|
|
$this->easyRsaDataDir, |
|
161
|
|
|
$commonName |
|
162
|
|
|
) |
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
View Code Duplication |
private function execEasyRsa(array $argv) |
|
|
|
|
|
|
167
|
|
|
{ |
|
168
|
|
|
$command = sprintf( |
|
169
|
|
|
'%s/easyrsa --vars=%s/vars %s >/dev/null 2>/dev/null', |
|
170
|
|
|
$this->easyRsaDir, |
|
171
|
|
|
$this->easyRsaDataDir, |
|
172
|
|
|
implode(' ', $argv) |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
exec( |
|
176
|
|
|
$command, |
|
177
|
|
|
$commandOutput, |
|
178
|
|
|
$returnValue |
|
179
|
|
|
); |
|
180
|
|
|
|
|
181
|
|
|
if (0 !== $returnValue) { |
|
182
|
|
|
throw new RuntimeException( |
|
183
|
|
|
sprintf('command "%s" did not complete successfully: "%s"', $command, $commandOutput) |
|
184
|
|
|
); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.