|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* eduVPN - End-user friendly VPN. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright: 2016-2017, The Commons Conservancy eduVPN Programme |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SURFnet\VPN\Server\CA; |
|
11
|
|
|
|
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
use SURFnet\VPN\Common\Config; |
|
14
|
|
|
use SURFnet\VPN\Common\FileIO; |
|
15
|
|
|
use SURFnet\VPN\Server\CA\Exception\CaException; |
|
16
|
|
|
|
|
17
|
|
|
class EasyRsaCa implements CaInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
private $easyRsaDir; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $easyRsaDataDir; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct($easyRsaDir, $easyRsaDataDir) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->easyRsaDir = $easyRsaDir; |
|
28
|
|
|
$this->easyRsaDataDir = $easyRsaDataDir; |
|
29
|
|
|
FileIO::createDir($this->easyRsaDataDir, 0700); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Initialize the CA. |
|
34
|
|
|
* |
|
35
|
|
|
* @param \SURFnet\VPN\Common\Config $config the CA configuration |
|
36
|
|
|
*/ |
|
37
|
|
|
public function init(Config $config) |
|
38
|
|
|
{ |
|
39
|
|
|
// only initialize when unitialized, prevent destroying existing CA |
|
40
|
|
|
if (!@file_exists(sprintf('%s/vars', $this->easyRsaDataDir))) { |
|
41
|
|
|
$configData = [ |
|
42
|
|
|
sprintf('set_var EASYRSA "%s"', $this->easyRsaDir), |
|
43
|
|
|
sprintf('set_var EASYRSA_PKI "%s/pki"', $this->easyRsaDataDir), |
|
44
|
|
|
sprintf('set_var EASYRSA_KEY_SIZE %d', $config->getSection('CA')->getItem('key_size')), |
|
45
|
|
|
sprintf('set_var EASYRSA_CA_EXPIRE %d', $config->getSection('CA')->getItem('ca_expire')), |
|
46
|
|
|
sprintf('set_var EASYRSA_CERT_EXPIRE %d', $config->getSection('CA')->getItem('cert_expire')), |
|
47
|
|
|
sprintf('set_var EASYRSA_REQ_CN "%s"', $config->getSection('CA')->getItem('ca_cn')), |
|
48
|
|
|
'set_var EASYRSA_BATCH "1"', |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
FileIO::writeFile( |
|
52
|
|
|
sprintf('%s/vars', $this->easyRsaDataDir), |
|
53
|
|
|
implode(PHP_EOL, $configData).PHP_EOL, |
|
54
|
|
|
0600 |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$this->execEasyRsa(['init-pki']); |
|
58
|
|
|
$this->execEasyRsa(['build-ca', 'nopass']); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get the CA root certificate. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string the CA certificate in PEM format |
|
66
|
|
|
*/ |
|
67
|
|
|
public function caCert() |
|
68
|
|
|
{ |
|
69
|
|
|
$certFile = sprintf('%s/pki/ca.crt', $this->easyRsaDataDir); |
|
70
|
|
|
|
|
71
|
|
|
return $this->readCertificate($certFile); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Generate a certificate for the VPN server. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $commonName |
|
78
|
|
|
* |
|
79
|
|
|
* @return array the certificate, key in array with keys |
|
80
|
|
|
* 'cert', 'key', 'valid_from' and 'valid_to' |
|
81
|
|
|
*/ |
|
82
|
|
View Code Duplication |
public function serverCert($commonName) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->hasCert($commonName)) { |
|
85
|
|
|
throw new CaException(sprintf('certificate with commonName "%s" already exists', $commonName)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->execEasyRsa(['build-server-full', $commonName, 'nopass']); |
|
89
|
|
|
|
|
90
|
|
|
return $this->certInfo($commonName); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Generate a certificate for a VPN client. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $commonName |
|
97
|
|
|
* |
|
98
|
|
|
* @return array the certificate and key in array with keys 'cert', 'key', |
|
99
|
|
|
* 'valid_from' and 'valid_to' |
|
100
|
|
|
*/ |
|
101
|
|
View Code Duplication |
public function clientCert($commonName) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->hasCert($commonName)) { |
|
104
|
|
|
throw new CaException(sprintf('certificate with commonName "%s" already exists', $commonName)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->execEasyRsa(['build-client-full', $commonName, 'nopass']); |
|
108
|
|
|
|
|
109
|
|
|
return $this->certInfo($commonName); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function certInfo($commonName) |
|
113
|
|
|
{ |
|
114
|
|
|
$certData = $this->readCertificate(sprintf('%s/pki/issued/%s.crt', $this->easyRsaDataDir, $commonName)); |
|
115
|
|
|
$keyData = $this->readKey(sprintf('%s/pki/private/%s.key', $this->easyRsaDataDir, $commonName)); |
|
116
|
|
|
|
|
117
|
|
|
$parsedCert = openssl_x509_parse($certData); |
|
118
|
|
|
|
|
119
|
|
|
return [ |
|
120
|
|
|
'certificate' => $certData, |
|
121
|
|
|
'private_key' => $keyData, |
|
122
|
|
|
'valid_from' => $parsedCert['validFrom_time_t'], |
|
123
|
|
|
'valid_to' => $parsedCert['validTo_time_t'], |
|
124
|
|
|
]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
private function readCertificate($certFile) |
|
128
|
|
|
{ |
|
129
|
|
|
// strip junk before and after actual certificate |
|
130
|
|
|
$pattern = '/(-----BEGIN CERTIFICATE-----.*-----END CERTIFICATE-----)/msU'; |
|
131
|
|
|
if (1 !== preg_match($pattern, FileIO::readFile($certFile), $matches)) { |
|
132
|
|
|
throw new CaException('unable to extract certificate'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $matches[1]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private function readKey($keyFile) |
|
139
|
|
|
{ |
|
140
|
|
|
// strip whitespace before and after actual key |
|
141
|
|
|
return trim( |
|
142
|
|
|
FileIO::readFile($keyFile) |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
private function hasCert($commonName) |
|
147
|
|
|
{ |
|
148
|
|
|
return @file_exists( |
|
149
|
|
|
sprintf( |
|
150
|
|
|
'%s/pki/issued/%s.crt', |
|
151
|
|
|
$this->easyRsaDataDir, |
|
152
|
|
|
$commonName |
|
153
|
|
|
) |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
View Code Duplication |
private function execEasyRsa(array $argv) |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
$command = sprintf( |
|
160
|
|
|
'%s/easyrsa --vars=%s/vars %s >/dev/null 2>/dev/null', |
|
161
|
|
|
$this->easyRsaDir, |
|
162
|
|
|
$this->easyRsaDataDir, |
|
163
|
|
|
implode(' ', $argv) |
|
164
|
|
|
); |
|
165
|
|
|
|
|
166
|
|
|
exec( |
|
167
|
|
|
$command, |
|
168
|
|
|
$commandOutput, |
|
169
|
|
|
$returnValue |
|
170
|
|
|
); |
|
171
|
|
|
|
|
172
|
|
|
if (0 !== $returnValue) { |
|
173
|
|
|
throw new RuntimeException( |
|
174
|
|
|
sprintf('command "%s" did not complete successfully: "%s"', $command, $commandOutput) |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
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.