-
Notifications
You must be signed in to change notification settings - Fork 11
/
acme-client.php
executable file
·432 lines (379 loc) · 10.9 KB
/
acme-client.php
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env php
<?php
class Config
{
// debug use
// public static $acme_url_base = 'https://acme-staging.api.letsencrypt.org';
// prod use
public static $acme_url_base = 'https://acme-v01.api.letsencrypt.org';
}
function printUsage($prog_name)
{
echo "usage: $prog_name ".
'-a <account_key_file> '.
'-r <csr_file> '.
'-d <domain_list(domain1;domain2...;domainN)> '.
'-c <http_challenge_dir> '.
'-o <output_cert_file>'.
'[-t <terms_of_service>]'.
"\n";
}
function urlbase64($bin)
{
return str_replace(
array('+', '/', '='),
array('-', '_', ''),
base64_encode($bin));
}
function loadAccountKey($account_key_file)
{
$key_file_content = file_get_contents($account_key_file);
if ($key_file_content === false) {
echo "can not open file: $account_key_file\n";
return false;
}
$key = openssl_pkey_get_private($key_file_content);
if ($key === false) {
echo "openssl failed: ".openssl_error_string()."\n";
return false;
}
return $key;
}
function loadCsrFile($csr_file)
{
$csr_file_content = file_get_contents($csr_file);
if ($csr_file_content === false) {
echo "can not open file: $csr_file_content\n";
return false;
}
$lines = explode("\n", $csr_file_content);
unset($lines[0]);
unset($lines[count($lines) - 1]);
return urlbase64(base64_decode(implode('', $lines)));
}
function getAccountKeyInfo($key)
{
$key_info = openssl_pkey_get_details($key);
if ($key_info === false) {
echo "openssl failed: ".openssl_error_string()."\n";
return false;
}
if (!isset($key_info['rsa'])) {
echo "account key file is not rsa private key\n";
return false;
}
return array(
'e' => urlbase64($key_info['rsa']['e']),
'n' => urlbase64($key_info['rsa']['n']),
);
}
function getThumbPrint($key)
{
$key_info = getAccountKeyInfo($key);
if ($key_info === false) {
return false;
}
$thumb_print = array(
'e' => $key_info['e'],
'kty' => 'RSA',
'n' => $key_info['n'],
);
$thumb_print = urlbase64(openssl_digest(
json_encode($thumb_print), "sha256", true));
return $thumb_print;
}
function signMessage($key, $message)
{
if (openssl_sign($message, $sign, $key, 'sha256') === false) {
return false;
}
return urlbase64($sign);
}
function httpRequest($url, $method, $post_data = '')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$method = strtolower($method);
if ($method === 'head') {
// head method
curl_setopt($ch, CURLOPT_NOBODY, true);
} else if ($method === 'get') {
// get method
} else if ($method === 'post') {
// post method
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
} else {
echo "curl_failed: invalid http method\n";
return false;
}
$output = curl_exec($ch);
if ($output === false) {
echo 'curl failed: '.curl_error($ch)."\n";
return false;
}
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($output, 0, $header_size);
$response = substr($output, $header_size);
curl_close($ch);
return array(
'http_code' => $http_code,
'header' => $header,
'response' => $response,
);
}
function getReplayNonce()
{
$ret = httpRequest(Config::$acme_url_base.'/directory', 'head');
if ($ret === false) {
return false;
}
preg_match('/^replay-nonce: (.*?)\r\n/sm', $ret['header'], $matches);
if (!isset($matches[1])) {
echo "curl failed: replay nonce header is missing\n";
return false;
}
return $matches[1];
}
function signedHttpRequest($key, $url, $payload)
{
$nonce = getReplayNonce();
if ($nonce === false) {
return false;
}
$key_info = getAccountKeyInfo($key);
if ($key_info === false) {
return false;
}
// header
$header = array(
'alg' => 'RS256',
'jwk' => array(
'kty' => 'RSA',
'e' => $key_info['e'],
'n' => $key_info['n'],
),
);
// protected
$protected = $header;
$protected['nonce'] = $nonce;
$payload64 = urlbase64(json_encode($payload));
$protected64 = urlbase64(json_encode($protected));
$sign = signMessage($key, $protected64.'.'.$payload64);
if ($sign === false) {
return false;
}
$request_data = array(
'header' => $header,
'protected' => $protected64,
'payload' => $payload64,
'signature' => $sign,
);
$request_data = json_encode($request_data);
return httpRequest($url, 'post', $request_data);
}
function registerAccount($key, $tos)
{
// get lastest tos
$ret = httpRequest(Config::$acme_url_base.'/terms', 'get');
if ($ret === false) {
return false;
}
preg_match('/^<a href="(.*?)">/sm', $ret['response'], $matches);
if (!isset($matches[1])) {
echo "acme/new-reg failed: get lastest tos failed\n";
return false;
}
$latest_tos = $matches[1];
if ($tos != '' && $tos != $latest_tos) {
echo "terms of service has changed: ".
"please modify your -t command option\n".
"new tos: $latest_tos\n";
return false;
}
// register account
$ret = signedHttpRequest($key,
Config::$acme_url_base.'/acme/new-reg', array(
'resource' => 'new-reg',
'agreement' => $latest_tos,
));
if ($ret === false) {
return false;
}
// 201 - register successfully
// 409 - already registered
if ($ret['http_code'] != 201 &&
$ret['http_code'] != 409) {
echo 'acme/new-reg failed: '.$ret['response']."\n";
return false;
}
return true;
}
function domainChallenge($key, $domain,
$http_challenge_dir, $thumb_print)
{
$ret = signedHttpRequest($key,
Config::$acme_url_base."/acme/new-authz", array(
'resource' => 'new-authz',
'identifier' => array(
'type' => 'dns',
'value' => $domain,
),
));
if ($ret === false) {
return false;
}
if ($ret['http_code'] != 201) {
echo 'acme/new-authz failed: '.$ret['response']."\n";
return false;
}
$response = json_decode($ret['response'], true);
$challenges = $response['challenges'];
foreach ($challenges as $challenge) {
if ($challenge['type'] != 'http-01') {
continue;
}
$challenge_token = $challenge['token'];
$challenge_uri = $challenge['uri'];
$challenge_key_auth = $challenge_token.'.'.$thumb_print;
if (file_put_contents(
"$http_challenge_dir/$challenge_token",
$challenge_key_auth) === false) {
return false;
}
$ret = signedHttpRequest($key,
$challenge_uri, array(
'resource' => 'challenge',
'keyAuthorization' => $challenge_key_auth,
));
if ($ret === false) {
return false;
}
if ($ret['http_code'] != 202) {
echo 'acme/challenge failed: '.$ret['response']."\n";
return false;
}
// wait to be verified
for (;;) {
$ret = httpRequest($challenge_uri, 'get');
if ($ret === false) {
return false;
}
$response = json_decode($ret['response'], true);
if (!isset($response['status']) ||
$response['status'] == 'invalid') {
echo 'acme/challenge failed: '.$ret['response']."\n";
return false;
}
if ($response['status'] == 'pending') {
sleep(2);
continue;
} else if ($response['status'] == 'valid') {
return true;
}
}
}
return true;
}
function issueCert($key, $csr, $output_cert_file)
{
// get cert
$ret = signedHttpRequest($key,
Config::$acme_url_base."/acme/new-cert", array(
'resource' => 'new-cert',
'csr' => $csr,
));
if ($ret === false) {
return false;
}
if ($ret['http_code'] != 201) {
echo 'acme/new-cert failed: '.$ret['response']."\n";
return false;
}
$cert = base64_encode($ret['response']);
// get intermediate cert
preg_match('/^link: <(.*?)>;rel="up"\r\n/sm', $ret['header'], $matches);
if (!isset($matches[1])) {
echo "acme/new-cert failed: can not get intermediate cert url\n";
return false;
}
$ret = httpRequest($matches[1], 'get');
if ($ret === false) {
return false;
}
if ($ret['http_code'] != 200) {
echo "acme/new-cert failed: can not get intermediate cert\n";
return false;
}
$intermediate_cert = base64_encode($ret['response']);
if (file_put_contents($output_cert_file,
"-----BEGIN CERTIFICATE-----\n".
chunk_split($cert, 64, "\n").
"-----END CERTIFICATE-----\n".
"-----BEGIN CERTIFICATE-----\n".
chunk_split($intermediate_cert, 64, "\n").
"-----END CERTIFICATE-----\n") === false) {
return false;
}
return true;
}
function main($argc, $argv)
{
$prog_name = basename($argv[0]);
$cmd_options = getopt('a:r:d:c:o:t:');
if (!isset($cmd_options['a']) ||
!isset($cmd_options['r']) ||
!isset($cmd_options['d']) ||
!isset($cmd_options['c']) ||
!isset($cmd_options['o'])) {
printUsage($prog_name);
return false;
}
$account_key_file = $cmd_options['a'];
$csr_file = $cmd_options['r'];
$domain_list = explode(";", $cmd_options['d']);
$http_challenge_dir = $cmd_options['c'];
$output_cert_file = $cmd_options['o'];
$tos = isset($cmd_options['t']) ? $cmd_options['t'] : '';
// load account key
$key = loadAccountKey($account_key_file);
if ($key === false) {
return false;
}
// load csr file
$csr = loadCsrFile($csr_file);
if ($csr === false) {
return false;
}
// register account
if (registerAccount($key, $tos) === false) {
return false;
}
// get thumb print
$thumb_print = getThumbPrint($key);
if ($thumb_print === false) {
return false;
}
// domain challenge
foreach ($domain_list as $domain) {
if (domainChallenge($key, $domain,
$http_challenge_dir, $thumb_print) === false) {
return false;
}
}
// issue cert
if (issueCert($key, $csr, $output_cert_file) === false) {
return false;
}
return true;
}
if (PHP_SAPI !== 'cli') {
exit(1);
}
if (main($argc, $argv) === false) {
exit(1);
}
exit(0);