-
Notifications
You must be signed in to change notification settings - Fork 15
/
request.html
201 lines (155 loc) · 5.42 KB
/
request.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/local/bin/php
<?php
namespace DirectAdmin\LetsEncrypt\Users\Actions;
use Crypt_RSA;
use DirectAdmin\LetsEncrypt\Lib\HTTPSocket;
use DirectAdmin\LetsEncrypt\Lib\Logger;
use Kelunik\Acme\AcmeClient;
use Kelunik\Acme\AcmeService;
use Kelunik\Acme\KeyPair;
// Variables from POST/GET //
/* @var String $domain */
/* @var String $email */
// End variables from POST/GET //
/* @var String $accountPath */
/* @var String $accountSettingsPath */
/* @var String $domainPath */
/* @var String $domainSettingsPath */
require_once dirname(dirname(__DIR__)) . '/includes/bootstrap.php';
$log = new Logger();
$log->short('Successfully applied SSL certificate for '.$domain);
/**
* Private/public certificates
*/
$publicKeyPath = $accountSettingsPath . DIRECTORY_SEPARATOR . 'public.key';
$privateKeyPath = $accountSettingsPath . DIRECTORY_SEPARATOR . 'private.key';
if (!file_exists($publicKeyPath) || !file_exists($privateKeyPath)) {
$log->setErrorShort('Error while creating account keys');
$rsa = new Crypt_RSA();
$keys = $rsa->createKey(4096);
if ($keys['partialkey'] === false) {
$keyPair = new KeyPair($keys['privatekey'], $keys['publickey']);
file_put_contents($publicKeyPath, $keyPair->getPublic());
file_put_contents($privateKeyPath, $keyPair->getPrivate());
} else {
$log->error('CPU was to slow, we\'ve not yet coded this part.');
}
$acme = new AcmeService(new AcmeClient('https://acme-staging.api.letsencrypt.org/directory', $keyPair), $keyPair);
try {
// Register, because we've just created a key pair.
$acme->register($email);
} catch(\Exception $e) {
$log->error($e->getMessage());
}
$log->info('Created a new pair of RSA keys for you and registrered them at Let\'s Encrypt.');
} else {
$log->setErrorShort('Error while receiving account keys');
$publicKey = file_get_contents($publicKeyPath);
$privateKey = file_get_contents($privateKeyPath);
$keyPair = new KeyPair($privateKey, $publicKey);
$acme = new AcmeService(new AcmeClient('https://acme-staging.api.letsencrypt.org/directory', $keyPair), $keyPair);
$log->info('Reused the pair of existing RSA keys.');
}
/**
* Request and complete challenges for domain
*/
$log->setErrorShort('Error while completing domain challenges');
try {
$response = $acme->requestChallenges($domain);
} catch(\Exception $e) {
$log->error($e->getMessage());
}
$goodChallenges = [];
foreach ($response[1]->challenges as $i => $challenge) {
if ($challenge->type === "http-01") {
$goodChallenges[] = $i;
}
}
foreach ($goodChallenges as $i => $challenge) {
if (!in_array([$challenge], $response[1]->combinations)) {
unset($goodChallenges[$i]);
}
}
if (empty($goodChallenges)) {
$log->error('We didn\'t receive any challenge we can solve.');
}
$challenge = $response[1]->challenges[reset($goodChallenges)];
$token = $challenge->token;
try {
$payload = $acme->generateHttp01Payload($token);
} catch(\Exception $e) {
$log->error($e->getMessage());
}
$challengePath = $domainPath . DIRECTORY_SEPARATOR . 'public_html' . DIRECTORY_SEPARATOR . '.well-known';
if (!file_exists($challengePath)) {
mkdir($challengePath);
}
$challengePath .= DIRECTORY_SEPARATOR . 'acme-challenge';
if (!file_exists($challengePath)) {
mkdir($challengePath);
}
file_put_contents($challengePath . DIRECTORY_SEPARATOR . $token, $payload);
try {
$acme->selfVerify($domain, $token, $payload);
$acme->answerChallenge($challenge->uri, $payload);
$acme->pollForChallenge($response[0]);
} catch(\Exception $e) {
$log->error($e->getMessage());
}
$log->info('Successfully completed challenge for ' . $domain . '.');
/**
* Request certificate
*/
$log->setErrorShort('Error while requesting SSL certificates');
$rsa = new Crypt_RSA();
$keys = $rsa->createKey(4096);
if ($keys['partialkey'] === false) {
$domainKeys = new KeyPair($keys['privatekey'], $keys['publickey']);
} else {
$log->error('CPU was to slow, we\'ve not yet coded this part.');
}
try {
$location = $acme->requestCertificate($domainKeys, [$domain]);
$certificates = $acme->pollForCertificate($location);
$certificate = $domainKeys->getPrivate() . "\n" . array_shift($certificates);
} catch(\Exception $e) {
$log->error($e->getMessage());
}
$log->info('Successfully received certificate from Let\'s Encrypt.');
/**
* Save certificate in DirectAdmin
*/
$log->setErrorShort('Error while saving SSL certificates to DirectAdmin');
$sock = new HTTPSocket();
$sock->connect('127.0.0.1', 2222);
$sock->set_login('admin');
$sock->set_method('POST');
$sock->query('/CMD_API_SSL', [
'domain' => $domain,
'action' => 'save',
'type' => 'paste',
'certificate' => $certificate,
'submit' => 'Save'
]);
$result = $sock->fetch_parsed_body();
if ($result['error'] != 0) {
$log->error('Error while executing API request: ' . $result['details']);
}
$sock = new HTTPSocket();
$sock->connect('127.0.0.1', 2222);
$sock->set_login('admin');
$sock->set_method('POST');
$sock->query('/CMD_API_SSL', [
'domain' => $domain,
'action' => 'save',
'type' => 'cacert',
'active' => 'yes',
'cacert' => implode("\n", $certificates),
'submit' => 'Save'
]);
$result = $sock->fetch_parsed_body();
if ($result['error'] != 0) {
$log->error('Error while executing API request: ' . $result['details']);
}
$log->info('Successfully saved new certificate and CA certificates in DirectAdmin.');
$log->output();