-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.js
560 lines (452 loc) · 11.9 KB
/
lib.js
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
// Copyright (c) 2018, Ben Noordhuis <[email protected]>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
'use strict'
const {crypto_sign_open} = require('tweetnacl').lowlevel
const {createHash, randomBytes} = require('crypto')
const {createSocket} = require('dgram')
const CERT = 0x54524543
const DELE = 0x454c4544
const INDX = 0x58444e49
const MAXT = 0x5458414d
const MIDP = 0x5044494d
const MINT = 0x544e494d
const NONC = 0x434e4f4e
const PAD = 0xff444150
const PATH = 0x48544150
const PUBK = 0x4b425550
const RADI = 0x49444152
const ROOT = 0x544f4f52
const SIG = 0x00474953
const SREP = 0x50455253
const knownHosts = {
'roughtime.cloudflare.com:2002': unbase64('gD63hSj3ScS+wuOeGrubXlq35N1c5Lby/S+T7MNTjxo='),
'roughtime.sandbox.google.com:2002': unbase64('etPaaIxcBMY1oUeGpwvPMCJMwlRVNxv51KK/tktoJTQ='),
}
Object.setPrototypeOf(knownHosts, null)
const certificateContext = bytes('RoughTime v1 delegation signature--\x00')
const signedResponseContext = bytes('RoughTime v1 response signature\x00')
const zero = Uint8Array.from([0])
const one = Uint8Array.from([1])
const scratch0 = new Uint8Array(2048)
const scratch1 = new Uint8Array(2048)
// |b| is a Buffer instead of a Uint8Array because dgram.Socket#send() in
// older Node.js versions only accepts Buffers.
const b = Buffer.alloc ? Buffer.alloc(1024) : new Buffer(1024)
b[0] = 2 // 2 tags; NONC and PAD
b[4] = 64 // 2nd tag (PAD) starts 64 bytes after header
b[8] = 78; b[9] = 79; b[10] = 78; b[11] = 67 // tag 'NONC'
b[12] = 80; b[13] = 65; b[14] = 68; b[15] = 255 // tag 'PAD\xff'
function roughtime(options, cb) {
if (typeof cb !== 'function') {
throw new TypeError('callback required')
}
if (typeof options === 'string') {
options = {host: options}
}
let {host, nonce, port, pubkey, socket} = options
port = port>>>0 || 2002
pubkey = pubkey || knownHosts[host + ':' + port]
if (nonce != null && nonce.length !== 64) {
throw new TypeError('nonce must be 64 bytes')
}
if (pubkey == null) {
throw new TypeError('server pubkey required')
}
if (pubkey.length !== 32) {
throw new TypeError('pubkey must be 32 bytes')
}
if (nonce) {
next(null, nonce)
} else {
randomBytes(64, next)
}
function next(err, nonce) {
if (err) {
return cb(err)
}
for (let i = 0; i < 64; i++) {
b[i+16] = nonce[i]
}
const ownsocket = (socket == null)
if (ownsocket) {
const {createSocket} = module.exports
socket = createSocket('udp4')
}
socket.send(b, port, host)
socket.once('message', b => {
if (ownsocket) {
socket.close()
}
const [midpoint, radius, err] = parse(pubkey, nonce, b)
cb(err, {midpoint, radius})
})
}
}
roughtime.promise = function(options) {
return new Promise((resolve, reject) => {
roughtime(options, (err, result) => {
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
}
function parse(pubkey, nonce, b) {
let CERT_tagstart = -1
let CERT_tagend = -1
let INDX_tagstart = -1
let INDX_tagend = -1
let PATH_tagstart = -1
let PATH_tagend = -1
let SIG_tagstart = -1
let SIG_tagend = -1
let SREP_tagstart = -1
let SREP_tagend = -1
let CERT_DELE_tagstart = -1
let CERT_DELE_tagend = -1
let CERT_SIG_tagstart = -1
let CERT_SIG_tagend = -1
let CERT_DELE_MAXT_tagstart = -1
let CERT_DELE_MAXT_tagend = -1
let CERT_DELE_MINT_tagstart = -1
let CERT_DELE_MINT_tagend = -1
let CERT_DELE_PUBK_tagstart = -1
let CERT_DELE_PUBK_tagend = -1
let SREP_MIDP_tagstart = -1
let SREP_MIDP_tagend = -1
let SREP_RADI_tagstart = -1
let SREP_RADI_tagend = -1
let SREP_ROOT_tagstart = -1
let SREP_ROOT_tagend = -1
let s = 0
let i = 0
let n = b.length
if (n%4 > 0) {
return reject(b, 'short message')
}
done:
for (;;) {
if (i+4 > n) {
return reject(b, 'short message')
}
const ntags = uint32(b, i)
i += 4
if (ntags === 0) {
return reject(b, 'no tags') // not technically illegal but...
}
const firstoffset = i
i += 4*(ntags-1)
const lastoffset = i
const firsttag = i
i += 4*ntags
const lasttag = i
const firstdatabyte = i
if (i > n) {
return reject(b, 'short message')
}
for (let last = -1, j = firstoffset; j < lastoffset; j += 4) {
const offset = uint32(b, j)
if (offset < last) {
return reject(b, 'illegal offset (order)')
}
last = offset
if (offset%4 > 0) {
return reject(b, 'illegal offset (alignment)')
}
if (offset+firstdatabyte > n) {
return reject(b, 'illegal offset (range)')
}
}
let tagstart = firstdatabyte
let tagend = -1
for (let j = firsttag; j < lasttag; j += 4, tagstart = tagend) {
tagend = n
if (j+4 < lasttag) {
tagend = firstdatabyte + uint32(b, j - firsttag + firstoffset)
}
const tag = uint32(b, j)
switch (s) {
case 0: // toplevel
switch (tag) {
case CERT:
CERT_tagstart = tagstart
CERT_tagend = tagend
break
case INDX:
INDX_tagstart = tagstart
INDX_tagend = tagend
break
case PATH:
PATH_tagstart = tagstart
PATH_tagend = tagend
break
case SIG:
SIG_tagstart = tagstart
SIG_tagend = tagend
break
case SREP:
SREP_tagstart = tagstart
SREP_tagend = tagend
break
}
break
case 1: // CERT
switch (tag) {
case DELE:
CERT_DELE_tagstart = tagstart
CERT_DELE_tagend = tagend
break
case SIG:
CERT_SIG_tagstart = tagstart
CERT_SIG_tagend = tagend
break
}
break
case 2: // CERT_DELE
switch (tag) {
case MAXT:
CERT_DELE_MAXT_tagstart = tagstart
CERT_DELE_MAXT_tagend = tagend
break
case MINT:
CERT_DELE_MINT_tagstart = tagstart
CERT_DELE_MINT_tagend = tagend
break
case PUBK:
CERT_DELE_PUBK_tagstart = tagstart
CERT_DELE_PUBK_tagend = tagend
break
}
break
case 3: // SREP
switch (tag) {
case MIDP:
SREP_MIDP_tagstart = tagstart
SREP_MIDP_tagend = tagend
break
case RADI:
SREP_RADI_tagstart = tagstart
SREP_RADI_tagend = tagend
break
case ROOT:
SREP_ROOT_tagstart = tagstart
SREP_ROOT_tagend = tagend
break
}
break
}
}
switch (s) {
case 0: // toplevel
if (CERT_tagstart < 0) {
return reject(b, 'no CERT tag')
}
if (INDX_tagstart < 0) {
return reject(b, 'no INDX tag')
}
if (INDX_tagend-INDX_tagstart !== 4) {
return reject(b, 'bad INDX tag')
}
if (PATH_tagstart < 0) {
return reject(b, 'no PATH tag')
}
if ((PATH_tagend-PATH_tagstart)%64 !== 0) {
return reject(b, 'bad PATH tag')
}
if (SIG_tagstart < 0) {
return reject(b, 'no SIG tag')
}
if (SIG_tagend-SIG_tagstart !== 64) {
return reject(b, 'bad SIG tag')
}
if (SREP_tagstart < 0) {
return reject(b, 'no SREP tag')
}
i = CERT_tagstart
n = CERT_tagend
s++ // CERT
break
case 1: // CERT
if (CERT_DELE_tagstart < 0) {
return reject(b, 'no CERT.DELE tag')
}
if (CERT_SIG_tagstart < 0) {
return reject(b, 'no CERT.SIG tag')
}
if (CERT_SIG_tagend-CERT_SIG_tagstart !== 64) {
return reject(b, 'bad CERT.SIG tag')
}
i = CERT_DELE_tagstart
n = CERT_DELE_tagend
s++ // CERT_DELE
break
case 2: // CERT_DELE
if (CERT_DELE_MAXT_tagstart < 0) {
return reject(b, 'no CERT.DELE.MAXT tag')
}
if (CERT_DELE_MAXT_tagend-CERT_DELE_MAXT_tagstart !== 8) {
return reject(b, 'bad CERT.DELE.MAXT tag')
}
if (CERT_DELE_MINT_tagstart < 0) {
return reject(b, 'no CERT.DELE.MINT tag')
}
if (CERT_DELE_MINT_tagend-CERT_DELE_MINT_tagstart !== 8) {
return reject(b, 'bad CERT.DELE.MAXT tag')
}
if (CERT_DELE_PUBK_tagstart < 0) {
return reject(b, 'no CERT.DELE.PUBK tag')
}
if (CERT_DELE_PUBK_tagend-CERT_DELE_PUBK_tagstart !== 32) {
return reject(b, 'bad CERT.DELE.PUBK')
}
i = SREP_tagstart
n = SREP_tagend
s++ // SREP
break
case 3: // SREP
if (SREP_MIDP_tagstart < 0) {
return reject(b, 'no SREP.MIDP tag')
}
if (SREP_MIDP_tagend-SREP_MIDP_tagstart !== 8) {
return reject(b, 'bad SREP.MIDP tag')
}
if (SREP_RADI_tagstart < 0) {
return reject(b, 'no SREP.RADI tag')
}
if (SREP_RADI_tagend-SREP_RADI_tagstart !== 4) {
return reject(b, 'bad SREP.RADI tag')
}
if (SREP_ROOT_tagstart < 0) {
return reject(b, 'no SREP.ROOT tag')
}
if (SREP_ROOT_tagend-SREP_ROOT_tagstart !== 64) {
return reject(b, 'bad SREP.ROOT tag')
}
break done
}
}
{
const sig = b.subarray(CERT_SIG_tagstart, CERT_SIG_tagend)
if (!verify(sig, certificateContext, b, CERT_DELE_tagstart, CERT_DELE_tagend, pubkey)) {
return reject(b, 'CERT.DELE does not verify')
}
}
{
const sig = b.subarray(SIG_tagstart, SIG_tagend)
const key = b.subarray(CERT_DELE_PUBK_tagstart, CERT_DELE_PUBK_tagend)
if (!verify(sig, signedResponseContext, b, SREP_tagstart, SREP_tagend, key)) {
return reject(b, 'SREP does not verify')
}
}
let h = createHash('sha512').
update(zero).
update(nonce).
digest()
const pathlen = PATH_tagend - PATH_tagstart
if (pathlen > 0) {
let index = uint32(b, INDX_tagstart)
for (let j = 0; j < pathlen; j += 64) {
let l = b.subarray(PATH_tagstart+j, PATH_tagstart+j+64)
let r = h
if (index&1 === 0) {
[l, r] = [r, l]
}
h = createHash('sha512').
update(one).
update(l).
update(r).
digest()
index >>>= 1
}
}
{
let i = 0
for (let j = 0; j < 64; j++) {
i ^= h[j]
i ^= b[j+SREP_ROOT_tagstart]
}
if (i !== 0) {
return reject(b, 'ROOT does not verify')
}
}
let midpoint
let mintime
let maxtime
let ok
// TODO(bnoordhuis) switch to BigInt before 2255 AD
if ([midpoint, ok] = uint64(b, SREP_MIDP_tagstart), !ok) {
return reject(b, 'deep future midpoint')
}
if ([mintime, ok] = uint64(b, CERT_DELE_MINT_tagstart), !ok) {
return reject(b, 'deep future mintime')
}
if ([maxtime, ok] = uint64(b, CERT_DELE_MAXT_tagstart), !ok) {
return reject(b, 'deep future maxtime')
}
if (maxtime < mintime) {
return reject(b, 'maxtime < mintime')
}
if (midpoint < mintime) {
return reject(b, 'midpoint < mintime')
}
if (midpoint > maxtime) {
return reject(b, 'midpoint > maxtime')
}
const radius = uint32(b, SREP_RADI_tagstart)
return [midpoint, radius, null]
}
function reject(b, s) {
const err = new Error(s)
err.reply = b
return [0, 0, err]
}
function verify(sig, prefix, b, start, end, pubkey) {
const prefixlen = prefix.length
const msglen = end-start
const len = 64 + prefixlen + msglen
if (len > scratch0.length) {
return false
}
let k = 0
for (let i = 0; i < 64; i++, k++) {
scratch0[k] = sig[i]
}
for (let i = 0; i < prefixlen; i++, k++) {
scratch0[k] = prefix[i]
}
for (let i = start; i < end; i++, k++) {
scratch0[k] = b[i]
}
return 0 <= crypto_sign_open(scratch1, scratch0, len, pubkey)
}
function unbase64(s) {
return Uint8Array.from(Buffer.from(s, 'base64'))
}
function bytes(s) {
return Uint8Array.from(s.split('').map(c => c.charCodeAt(0)))
}
function uint32(b, i) {
return b[i+0] + 256*b[i+1] + 65536*b[i+2] + 16777216*b[i+3]
}
function uint64(b, i) {
const lo = uint32(b, i+0)
const hi = uint32(b, i+4)
if (hi > 2097152) {
return [-1, false]
}
return [4294967296*hi + lo, true]
}
module.exports = {createSocket, knownHosts, parse, roughtime, unbase64}