-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.ts
More file actions
735 lines (663 loc) · 16.6 KB
/
lib.ts
File metadata and controls
735 lines (663 loc) · 16.6 KB
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
import crypto from "node:crypto";
import {
fetch,
ProxyAgent,
setGlobalDispatcher,
} from "undici";
import {
type AuthResponse,
type Challenge,
type DirectoryResponse,
type OrderResponse,
type FinalizeResponse,
type ValidOrderResponse,
} from "./types";
import { extract_openssl_hex } from "./openssl_hex";
const FETCH_HEADERS = {
"Content-Type": "application/jose+json",
};
function get_proxy(): undefined | string {
for (let key of ["HTTPS_PROXY", "https_proxy"]) {
if (process.env[key]) {
return process.env[key];
}
}
return undefined;
}
export const proxy_url: undefined | string = get_proxy();
if (proxy_url) {
const agent = new ProxyAgent(proxy_url);
setGlobalDispatcher(agent);
}
// always empty payload
const CHALLENGE_PAYLOAD = b64("{}");
const RECHECK_AUTH_PAYLOAD_B64 = "";
const RECHECK_ORDER_PAYLOAD_B64 = "";
const CERT_PAYLOAD_B64 = "";
const DIRECTORY_URL = "https://acme-v02.api.letsencrypt.org/directory";
async function sha256Async(bytes) {
const hash = await crypto.subtle.digest("SHA-256", bytes);
return new Uint8Array(hash);
}
/**
* @param token the same as file-based content
*/
export async function gen_dnstoken(token: string) {
const len = token.length,
bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = token.charCodeAt(i);
}
const hash = await sha256Async(bytes);
return b64(hash);
}
function b64(data) {
return Buffer.from(data).toString("base64url");
}
function hex2b64(openssl_output: string) {
let hex = extract_openssl_hex(openssl_output);
if (hex === null) {
return null;
}
var bytes: number[] = [];
for (let i = 0; i < hex.length; i += 2) {
bytes.push(parseInt(hex.substring(i, i + 2), 16));
}
return b64(new Uint8Array(bytes));
}
function cachebuster() {
return "cachebuster=" + b64(crypto.getRandomValues(new Uint8Array(8)));
}
async function getNonceAsync(dir: DirectoryResponse) {
const response = await fetch(`${dir.newNonce}?${cachebuster()}`);
if (!response.ok)
throw "Failed to fetch nonce from Let's Encrypt";
return response.headers.get("Replay-Nonce");
}
export async function populateDirectoryAsync() {
const res = await fetch(`${DIRECTORY_URL}?${cachebuster()}`)
if (!res.ok) {
console.log("❌ Error: Let's Encrypt appears to be down. Please try again later.");
process.exit(1);
}
return res.json() as Promise<DirectoryResponse>;
}
interface Account {
pubkey: string;
alg: string;
jwk: crypto.JsonWebKey;
thumbprint: string;
registration_payload_json: {
termsOfServiceAgreed: boolean;
};
registration_payload_b64: string;
update_payload_json: {
contact: string[];
};
update_payload_b64: string;
}
export async function createAccountAsync({
email,
pubkey,
}: {
email: string;
pubkey: string;
}): Promise<Account> {
// validate email
const email_re = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
if (!email_re.test(email)) {
throw "Account email doesn't look valid.";
}
// parse account public key
if (pubkey === "") {
throw "You need to include an account public key.";
}
const keyObj = crypto.createPublicKey(pubkey);
const { e, kty, n } = keyObj.export({ format: "jwk" });
const jwk = { e, kty, n };
const jwk_json_str = JSON.stringify(jwk);
const jwk_bytes = new Uint8Array(jwk_json_str.length);
for (let i = 0; i < jwk_json_str.length; i++) {
jwk_bytes[i] = jwk_json_str.charCodeAt(i);
}
const hash = await sha256Async(jwk_bytes);
let registration_payload_json = {
termsOfServiceAgreed: true,
};
let update_payload_json = {
contact: [ "mailto:" + email ],
};
return {
pubkey,
alg: "RS256",
jwk,
thumbprint: b64(hash),
registration_payload_json,
registration_payload_b64: b64(JSON.stringify(registration_payload_json)),
update_payload_json,
update_payload_b64: b64(JSON.stringify(update_payload_json)),
};
}
export function createOrder({
domains,
}: {
domains: string[];
}) {
const order_payload_json = {
identifiers: domains.map(value => ({
type: "dns",
value,
})),
};
return {
order_payload_json,
order_payload_b64: b64(JSON.stringify(order_payload_json)),
};
}
/**
* Generate message to be signed by openssl
*/
export async function createOrderCmdAsync({
directory,
account,
}: {
directory: DirectoryResponse;
account: Account;
}) {
const nonce = await getNonceAsync(directory);
const registration_protected_json = {
url: directory.newAccount,
alg: account.alg,
nonce,
jwk: account.jwk,
};
const registration_protected_b64 = b64(JSON.stringify(registration_protected_json));
return {
registration_protected_b64,
cmd: `${registration_protected_b64}.${account.registration_payload_b64}`,
}
}
/**
* Step: 3a
*/
export async function validateRegistrationAsync({
account,
directory,
openssl_order_signature,
registration_protected_b64,
}: {
account: Account,
directory: DirectoryResponse,
openssl_order_signature: string;
registration_protected_b64: string,
}) {
const signature = hex2b64(openssl_order_signature);
if (signature === null) {
throw "null signature";
}
const response_newaccount = await fetch(directory.newAccount, {
method: "POST",
headers: FETCH_HEADERS,
body: JSON.stringify({
protected: registration_protected_b64,
payload: account.registration_payload_b64,
signature,
}),
});
if (!([200, 201, 204].includes(response_newaccount.status))) {
console.log(response_newaccount.status);
console.log(await response_newaccount.json());
throw "Account registration failed. Please start back at Step 1. ";
}
const account_uri = response_newaccount.headers.get("Location");
if (!account_uri) {
throw "account_uri not found";
}
const nonce = await getNonceAsync(directory);
const update_protected_json = {
url: account_uri,
alg: account.alg,
nonce,
kid: account_uri,
};
const update_protected_b64 = b64(JSON.stringify(update_protected_json));
return {
account_uri,
update_protected_b64,
cmd: `${update_protected_b64}.${account.update_payload_b64}`
};
}
// Step: 3b
export async function validateUpdateAsync({
account,
account_uri,
directory,
openssl_registration_signature,
order,
update_protected_b64,
}: {
account: Account;
account_uri: string;
directory: DirectoryResponse;
openssl_registration_signature: string;
order: ReturnType<typeof createOrder>;
update_protected_b64: string;
}) {
const signature = hex2b64(openssl_registration_signature);
const response = await fetch(account_uri, {
method: "POST",
headers: FETCH_HEADERS,
body: JSON.stringify({
protected: update_protected_b64,
payload: account.update_payload_b64,
signature,
}),
});
if (response.status != 200) {
throw "Account contact update failed. Please start back at Step 1.";
}
const nonce = await getNonceAsync(directory);
const order_protected_json = {
url: directory.newOrder,
alg: account.alg,
nonce,
kid: account_uri,
};
const order_protected_b64 = b64(JSON.stringify(order_protected_json));
return {
order_protected_b64,
cmd: `${order_protected_b64}.${order.order_payload_b64}`,
};
}
/*
* Step 3c: Create New Order (POST /newOrder)
*/
export async function validateOrderAsync({
directory,
openssl_validate_order_signature,
order,
order_protected_b64,
}: {
directory: DirectoryResponse;
openssl_validate_order_signature: string;
order: ReturnType<typeof createOrder>;
order_protected_b64: string;
}) {
const signature = hex2b64(openssl_validate_order_signature);
const response = await fetch(directory.newOrder, {
method: "POST",
headers: FETCH_HEADERS,
body: JSON.stringify({
protected: order_protected_b64,
payload: order.order_payload_b64,
signature,
}),
});
if (!([200, 201].includes(response.status))) {
const json = await response.json();
console.log(response.status, json);
throw "Order failed. Please start back at Step 1.";
}
const order_uri = response.headers.get("Location");
if (!order_uri) {
throw "order_uri not found";
}
const order_response = (await response.json()) as OrderResponse;
return {
order_uri,
order_response,
}
}
export async function buildAuthorizationAsync({
idx,
//status,
directory,
order_response,
account,
account_uri,
auth_payload_b64,
}: {
idx: number;
//status: any;
directory: DirectoryResponse;
order_response: OrderResponse;
account: Account;
account_uri: string;
auth_payload_b64: string;
}) {
const auth_url = order_response.authorizations[idx];
const nonce = await getNonceAsync(directory);
const protected_json = {
url: auth_url,
alg: account.alg,
nonce,
kid: account_uri,
};
const protected_b64 = b64(JSON.stringify(protected_json));
return {
auth_url,
auth_protected_json: protected_json,
auth_protected_b64: protected_b64,
cmd: `${protected_b64}.${auth_payload_b64}`,
};
}
// 4b load challenges
export async function validateAuthorizationAsync({
auth_url,
auth_protected_b64,
auth_payload_b64 = "", // To be checked, "" or "e30" (b64("{}"))
openssl_auth_sig,
}: {
auth_url: string;
auth_protected_b64: string;
auth_payload_b64?: string;
openssl_auth_sig: string;
}) {
const signature = hex2b64(openssl_auth_sig);
const response = await fetch(auth_url, {
method: "POST",
headers: FETCH_HEADERS,
body: JSON.stringify({
protected: auth_protected_b64,
payload: auth_payload_b64,
signature,
}),
});
if (response.status !== 200) {
console.log(response.status);
console.log(await response.json());
throw "Loading challenges failed. Please start back at Step 1.";
}
const auth_obj = (await response.json()) as AuthResponse;
return auth_obj;
}
// 4c
export async function confirmChallengeAsync({
account,
account_uri,
challenge,
directory,
}: {
account: Account;
account_uri: string;
challenge: Challenge;
directory: DirectoryResponse;
}) {
const nonce = await getNonceAsync(directory);
const protected_json = {
url: challenge.url,
alg: account.alg,
nonce,
kid: account_uri,
};
const challenge_protected_b64 = b64(JSON.stringify(protected_json));
return {
challenge_protected_b64,
cmd: `${challenge_protected_b64}.${CHALLENGE_PAYLOAD}`,
}
}
// 4d
export async function validateChallengeAsync({
account,
account_uri,
challenge,
challenge_protected_b64,
directory,
openssl_challenge_signature,
}: {
account: Account;
account_uri: string;
challenge: Challenge;
challenge_protected_b64: string;
directory: DirectoryResponse;
openssl_challenge_signature: string;
}) {
const signature = hex2b64(openssl_challenge_signature);
const response = await fetch(challenge.url, {
method: "POST",
headers: FETCH_HEADERS,
body: JSON.stringify({
protected: challenge_protected_b64,
payload: CHALLENGE_PAYLOAD,
signature,
}),
});
if (response.status !== 200) {
// TODO FIXME
throw "Challenge submission failed. Please start back at Step 1.";
}
// @ts-ignore TODO check response
const challenge_response = await response.json();
const nonce = await getNonceAsync(directory);
const recheck_auth_protected_json = {
url: challenge.url,
alg: account.alg,
nonce,
kid: account_uri,
};
const recheck_auth_protected_b64 = b64(JSON.stringify(recheck_auth_protected_json));
return {
recheck_auth_protected_b64,
cmd: `${recheck_auth_protected_b64}.${RECHECK_AUTH_PAYLOAD_B64}`,
};
}
// 4e
export async function checkAuthorizationAsync({
account,
account_uri,
challenge,
directory,
openssl_validate_challenge_signature,
recheck_auth_protected_b64,
}: {
account: Account;
account_uri: string;
challenge: Challenge;
directory: DirectoryResponse;
openssl_validate_challenge_signature: string;
recheck_auth_protected_b64: string;
}) {
const signature = hex2b64(openssl_validate_challenge_signature);
const response = await fetch(challenge.url, {
method: "POST",
headers: FETCH_HEADERS,
body: JSON.stringify({
protected: recheck_auth_protected_b64,
payload: RECHECK_AUTH_PAYLOAD_B64,
signature,
}),
});
if (response.status !== 200) {
console.log(response.status);
console.log(await response.json());
throw "Loading challenge status failed. Please start back at Step 1."
}
const auth_obj = (await response.json()) as AuthResponse;
const status = auth_obj.status;
if (status === "pending") {
console.log(auth_obj);
const nonce = await getNonceAsync(directory);
const recheck_auth_protected_json = {
url: challenge.url,
alg: account.alg,
nonce,
kid: account_uri,
};
const recheck_auth_protected_b64 = b64(JSON.stringify(recheck_auth_protected_json));
return {
recheck_auth_protected_b64,
cmd: `${recheck_auth_protected_b64}.${RECHECK_AUTH_PAYLOAD_B64}`,
};
} else if (status === "valid") {
return {
status,
};
} else {
// @ts-ignore
console.log(`\n${auth_obj.error.detail}`);
console.log("Loading challenge status failed. Please start back at Step 1.");
process.exit(1);
}
}
export async function finalizeOrderAsync({
account,
account_uri,
directory,
order,
pem,
}: {
account: Account;
account_uri: string;
directory: DirectoryResponse;
order: OrderResponse;
pem: string;
}) {
const nonce = await getNonceAsync(directory);
const finalize_protected_json = {
url: order.finalize,
alg: account.alg,
nonce,
kid: account_uri,
};
const finalize_protected_b64 = b64(JSON.stringify(finalize_protected_json));
const unarmor = /-----BEGIN CERTIFICATE REQUEST-----([A-Za-z0-9+\/=\s]+)-----END CERTIFICATE REQUEST-----/;
const matches = unarmor.exec(pem);
if (!matches || matches.length < 1) {
throw "wrong cert format";
}
const csr_der = matches[1].replace(/\r?\n/g, '');
const finalize_payload_json = {
csr: Buffer.from(csr_der, "base64").toString("base64url"),
};
const finalize_payload_b64 = b64(JSON.stringify(finalize_payload_json));
return {
finalize_payload_b64,
finalize_protected_b64,
cmd: `${finalize_protected_b64}.${finalize_payload_b64}`,
};
}
// 4f
export async function validateFinalizeAsync({
account,
account_uri,
directory,
finalize_payload_b64,
finalize_protected_b64,
openssl_finalize_order_signature,
order,
order_uri,
}: {
account: Account;
account_uri: string;
directory: DirectoryResponse;
finalize_payload_b64: string;
finalize_protected_b64: string;
openssl_finalize_order_signature: string;
order: OrderResponse;
order_uri: string;
}) {
const signature = hex2b64(openssl_finalize_order_signature);
const response = await fetch(order.finalize, {
method: "POST",
headers: FETCH_HEADERS,
body: JSON.stringify({
protected: finalize_protected_b64,
payload: finalize_payload_b64,
signature,
}),
});
if (response.status !== 200) {
console.log(response.status);
console.log(await response.json());
throw "Finalizing failed. Please start back at Step 1.";
}
// @ts-ignore TODO check response
const finalize_response = (await response.json()) as FinalizeResponse;
const nonce = await getNonceAsync(directory);
const recheck_order_protected_b64 = b64(JSON.stringify({
url: order_uri,
alg: account.alg,
nonce,
kid: account_uri,
}));
return {
recheck_order_protected_b64,
cmd: `${recheck_order_protected_b64}.${RECHECK_ORDER_PAYLOAD_B64}`,
};
}
// 4g
export async function recheckOrderAsync({
account,
account_uri,
directory,
openssl_recheck_order_signature,
order_uri,
recheck_order_protected_b64,
}: {
account: Account;
account_uri: string;
directory: DirectoryResponse;
openssl_recheck_order_signature: string;
order_uri: string;
recheck_order_protected_b64: string;
}) {
const signature = hex2b64(openssl_recheck_order_signature);
const request = await fetch(order_uri, {
method: "POST",
headers: FETCH_HEADERS,
body: JSON.stringify({
protected: recheck_order_protected_b64,
payload: RECHECK_ORDER_PAYLOAD_B64,
signature,
}),
});
if (request.status !== 200) {
throw "TODO";
}
// TODO type
const order = (await request.json()) as ValidOrderResponse;
if (["pending", "processing", "ready"].includes(order.status)) {
throw "TODO";
} else if (order.status === "valid") {
const nonce = await getNonceAsync(directory);
const cert_protected_json = {
url: order.certificate,
alg: account.alg,
nonce,
kid: account_uri,
};
const cert_protected_b64 = b64(JSON.stringify(cert_protected_json));
return {
cert_uri: order.certificate,
cert_protected_b64,
cmd: `${cert_protected_b64}.${CERT_PAYLOAD_B64}`,
};
} else {
throw "TODO";
}
}
// 4h
export async function getCertificateAsync({
cert_uri,
cert_protected_b64,
openssl_cert_signature,
}: {
cert_uri: string;
cert_protected_b64: string;
openssl_cert_signature: string;
}) {
const signature = hex2b64(openssl_cert_signature);
const response = await fetch(cert_uri, {
method: "POST",
headers: FETCH_HEADERS,
body: JSON.stringify({
protected: cert_protected_b64,
payload: CERT_PAYLOAD_B64,
signature,
}),
});
if (response.status !== 200) {
throw "TODO";
}
return await response.text();
}