@amritk/nish-aarch64-linux 0.49.0 → 0.50.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +1 -1
- package/bin/nish +0 -0
- package/docs/AI.md +233 -159
- package/package.json +1 -1
- package/runtime/gfx/nish-gfx.h +373 -61
- package/runtime/nish.d.ts +12 -9
- package/std/README.md +8 -5
- package/std/base64.ts +74 -1
- package/std/bytes.ts +201 -93
- package/std/compress/deflate.ts +20 -15
- package/std/crypto/base64url.ts +93 -66
- package/std/crypto/bcrypt.ts +25 -21
- package/std/crypto/hmac.ts +1 -1
- package/std/crypto/jwt.ts +32 -19
- package/std/crypto/p256.ts +9 -12
- package/std/crypto/pki-roots.ts +7 -4
- package/std/crypto/pki.ts +43 -55
- package/std/crypto/rsa-sign.ts +1632 -0
- package/std/crypto/rsa.ts +26 -4
- package/std/crypto/sha256.ts +135 -68
- package/std/crypto/x25519.ts +12 -11
- package/std/crypto/x509.ts +164 -40
- package/std/gpu/sys.ts +490 -0
- package/std/json-equal.ts +33 -18
- package/std/json-format.ts +39 -8
- package/std/json-number.ts +15 -3
- package/std/json-tokenizer.ts +4 -3
- package/std/json-typed.ts +29 -12
- package/std/json-value.ts +222 -104
- package/std/json.ts +81 -46
- package/std/net/dns-config.ts +18 -5
- package/std/net/dns-message.ts +60 -27
- package/std/net/dns-resolver.ts +35 -16
- package/std/net/dns.ts +3 -2
- package/std/net/fetch.ts +120 -38
- package/std/net/handler.ts +178 -66
- package/std/net/hpack.ts +192 -98
- package/std/net/http-compress.ts +83 -30
- package/std/net/http-fields.ts +327 -31
- package/std/net/http-server.ts +317 -20
- package/std/net/http1-client-codec.ts +137 -40
- package/std/net/http1-codec.ts +391 -202
- package/std/net/http1-server.ts +220 -38
- package/std/net/http2-cleartext.ts +10 -4
- package/std/net/http2-frame.ts +2 -2
- package/std/net/http2-tls.ts +3 -3
- package/std/net/http2.ts +69 -15
- package/std/net/http3-frame.ts +11 -14
- package/std/net/http3-server.ts +12 -15
- package/std/net/http3.ts +41 -29
- package/std/net/multipart.ts +592 -0
- package/std/net/outbound.ts +24 -8
- package/std/net/postgres-async.ts +92 -54
- package/std/net/postgres-carrier.ts +28 -4
- package/std/net/postgres.ts +166 -65
- package/std/net/prefork.ts +86 -5
- package/std/net/protobuf.ts +6 -2
- package/std/net/qpack.ts +66 -25
- package/std/net/quic-conn-ack.ts +27 -7
- package/std/net/quic-conn-cid.ts +13 -7
- package/std/net/quic-datagram.ts +6 -5
- package/std/net/quic-frame.ts +67 -23
- package/std/net/quic-listener.ts +19 -12
- package/std/net/quic-recovery.ts +11 -9
- package/std/net/quic-stream.ts +7 -5
- package/std/net/quic.ts +36 -31
- package/std/net/router.ts +136 -24
- package/std/net/serve.ts +59 -5
- package/std/net/static.ts +81 -33
- package/std/net/tls/record.ts +26 -13
- package/std/net/tls/ticket.ts +6 -5
- package/std/net/tls-tcp.ts +69 -30
- package/std/net/tls.ts +152 -38
- package/std/net/upstream.ts +65 -19
- package/std/net/url.ts +97 -78
- package/std/net/websocket.ts +33 -15
- package/std/net/webtransport.ts +28 -12
- package/std/regex.ts +600 -388
- package/std/sqlite.ts +37 -19
- package/std/sync/ring.ts +232 -115
- package/std/sync/shm.ts +11 -4
- package/std/temporal.ts +232 -133
- package/std/term-io.ts +12 -6
- package/std/term-pixels.ts +158 -22
- package/std/term-rate.ts +8 -3
- package/std/term.ts +4 -4
- package/std/text.ts +33 -11
- package/std/threads.ts +52 -8
- package/std/yaml-json.ts +238 -106
- package/std/yaml-lines.ts +47 -27
- package/std/yaml-write.ts +58 -24
- package/std/yaml.ts +808 -395
package/std/net/static.ts
CHANGED
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
*/
|
|
61
61
|
|
|
62
62
|
import { realpathSync, statMtimeSync } from "nish:fs"
|
|
63
|
-
import {
|
|
63
|
+
import { tryCopyBytes } from "nish/bytes"
|
|
64
64
|
import { HANDLER_SENT, HttpHandler, HttpRequest } from "nish/net/handler"
|
|
65
65
|
import { HTTP_DATE_SIZE, httpDateWrite, httpFieldBytes } from "nish/net/http-fields"
|
|
66
66
|
import { HttpServer, HttpServerRequest } from "nish/net/http-server"
|
|
@@ -162,13 +162,34 @@ const staticValue = (r: HttpRequest, i: i32, start: boolean): Result<i32, string
|
|
|
162
162
|
const staticByte = (b: u8[], i: i32): i32 => (i >= 0 && i < toI32(b.length) ? toI32(b[i]) : -1)
|
|
163
163
|
|
|
164
164
|
/**
|
|
165
|
-
* `a + b` for file offsets and sizes,
|
|
166
|
-
*
|
|
165
|
+
* `a + b` for file offsets and sizes, or -1 when either is below 0 or the
|
|
166
|
+
* sum is past the last `i64`. Every offset and size it is given is from 0 to
|
|
167
|
+
* `STATIC_MAX_FILE`, 2^31 - 1, as `staticPlan` refuses a larger file, so the
|
|
168
|
+
* -1 is never answered. Two values from 0 to 2^63 - 1 sum in `u64` without
|
|
169
|
+
* wrapping, and the sum is turned back only once it is known to fit.
|
|
167
170
|
*/
|
|
168
|
-
const staticAdd64 = (a: i64, b: i64): i64 =>
|
|
171
|
+
const staticAdd64 = (a: i64, b: i64): i64 => {
|
|
172
|
+
if (a < 0 || b < 0) {
|
|
173
|
+
return -1
|
|
174
|
+
}
|
|
175
|
+
const sum: u64 = toU64(a) + toU64(b)
|
|
176
|
+
const top: u64 = (toU64(1) << toU64(63)) - toU64(1)
|
|
177
|
+
if (sum > top) {
|
|
178
|
+
return -1
|
|
179
|
+
}
|
|
180
|
+
return toI64(sum)
|
|
181
|
+
}
|
|
169
182
|
|
|
170
|
-
/**
|
|
171
|
-
|
|
183
|
+
/**
|
|
184
|
+
* `a - b` for the same offsets as `staticAdd64`, or -1 when either is below
|
|
185
|
+
* 0, which none is: the difference of two such values always fits.
|
|
186
|
+
*/
|
|
187
|
+
const staticSub64 = (a: i64, b: i64): i64 => {
|
|
188
|
+
if (a < 0 || b < 0) {
|
|
189
|
+
return -1
|
|
190
|
+
}
|
|
191
|
+
return a - b
|
|
192
|
+
}
|
|
172
193
|
|
|
173
194
|
/**
|
|
174
195
|
* Writes `v` to `out[i]` and answers `i + 1`, the next write's index; -1,
|
|
@@ -213,12 +234,16 @@ const staticCap = (size: i32): i32 => {
|
|
|
213
234
|
|
|
214
235
|
/** Reads `size` bytes of `fd` from its start into `into`: false when a read fails or `into` is short. */
|
|
215
236
|
const staticReadAll = (fd: i32, into: u8[], size: i32): boolean => {
|
|
216
|
-
if (size > toI32(into.length)) {
|
|
217
|
-
return false
|
|
218
|
-
}
|
|
219
237
|
let got: i32 = 0
|
|
220
238
|
while (got < size) {
|
|
221
|
-
|
|
239
|
+
// `got` only grows from 0, by reads short of `size - got`, so the exit
|
|
240
|
+
// is taken only for a short `into`; it is where the prover reads both.
|
|
241
|
+
const at: i32 = got
|
|
242
|
+
const pos: i64 = toI64(at)
|
|
243
|
+
if (at < 0 || at > size || size > toI32(into.length)) {
|
|
244
|
+
return false
|
|
245
|
+
}
|
|
246
|
+
const n: i32 = fileReadAt(fd, into, at, size - at, pos)
|
|
222
247
|
if (n <= 0) {
|
|
223
248
|
return false
|
|
224
249
|
}
|
|
@@ -435,18 +460,27 @@ const staticDate = (b: u8[], start: i32, end: i32): i64 => {
|
|
|
435
460
|
} else {
|
|
436
461
|
year = year - 1
|
|
437
462
|
}
|
|
438
|
-
// The prover does not carry `year` through the branches,
|
|
439
|
-
//
|
|
463
|
+
// The prover does not carry `year` through the branches, and it bounds
|
|
464
|
+
// only values of 0 or more, so `365 * year` is worked from `year + 1`,
|
|
465
|
+
// 0 to 10000 by the bounds above: this test never holds.
|
|
440
466
|
const y: i32 = year
|
|
467
|
+
const shiftedYear: i32 = staticAdd(y, 1)
|
|
468
|
+
if (shiftedYear < 0 || shiftedYear > 10000) {
|
|
469
|
+
return -1
|
|
470
|
+
}
|
|
441
471
|
const dayOfYear: i32 = (153 * mp + 2) / 5 + day - 1
|
|
442
472
|
const leaps: i32 = staticAdd(staticSub(y / 4, y / 100), y / 400)
|
|
443
|
-
const
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
//
|
|
448
|
-
//
|
|
449
|
-
|
|
473
|
+
const yearDays: i32 = shiftedYear * 365 - 365
|
|
474
|
+
const days: i32 = staticSub(staticAdd(staticAdd(yearDays, leaps), dayOfYear), 719468)
|
|
475
|
+
// A year from -1 to 9999 puts `days` from about -720,000 to 2,940,000, so
|
|
476
|
+
// `days + 1000000` is from 0 to 5000000 and this test never holds; it is
|
|
477
|
+
// what proves the seconds, worked from that shifted count and taken back
|
|
478
|
+
// by 1000000 days' seconds at the end, cannot overflow.
|
|
479
|
+
const shiftedDays: i32 = staticAdd(days, 1000000)
|
|
480
|
+
if (shiftedDays < 0 || shiftedDays > 5000000) {
|
|
481
|
+
return -1
|
|
482
|
+
}
|
|
483
|
+
return toI64(shiftedDays) * 86400 + toI64(3600 * hour + 60 * minute + second) - 86400000000
|
|
450
484
|
}
|
|
451
485
|
|
|
452
486
|
/**
|
|
@@ -457,17 +491,17 @@ const staticNumber = (b: u8[], start: i32, end: i32): i64 => {
|
|
|
457
491
|
if (start < 0 || end <= start || staticSub(end, start) > 18) {
|
|
458
492
|
return -1
|
|
459
493
|
}
|
|
460
|
-
|
|
494
|
+
// The value stays `u64` throughout: eighteen digits are below 10^18, so
|
|
495
|
+
// the sum never wraps and its value fits an `i64` exactly.
|
|
496
|
+
let n: u64 = 0
|
|
461
497
|
for (let k: i32 = start; k < end && k < toI32(b.length); k++) {
|
|
462
498
|
const d: i32 = toI32(b[k]) - 48
|
|
463
499
|
if (d < 0 || d > 9) {
|
|
464
500
|
return -1
|
|
465
501
|
}
|
|
466
|
-
|
|
467
|
-
// one: eighteen digits are below 10^18, so it never wraps.
|
|
468
|
-
n = toI64(toU64(n) * toU64(10) + toU64(d))
|
|
502
|
+
n = n * 10 + toU64(d)
|
|
469
503
|
}
|
|
470
|
-
return n
|
|
504
|
+
return toI64(n)
|
|
471
505
|
}
|
|
472
506
|
|
|
473
507
|
/**
|
|
@@ -1198,8 +1232,10 @@ export class StaticFiles {
|
|
|
1198
1232
|
const last: i64 = this.staticSecond()
|
|
1199
1233
|
// A date validates only a strong Last-Modified, a second or more before
|
|
1200
1234
|
// now (RFC 9110 §13.1.5): two versions within one second share the date.
|
|
1201
|
-
// `last
|
|
1202
|
-
|
|
1235
|
+
// `(last + 1) * 1000 <= now` is `last + 1 <= floor(now / 1000)`, as
|
|
1236
|
+
// `last + 1` is whole, and so `last < now / 1000`; for a negative `now`
|
|
1237
|
+
// both are false, as `last` is at least 0. Dividing cannot overflow.
|
|
1238
|
+
return at >= 0 && at === last && last < toI64(Date.now()) / 1000
|
|
1203
1239
|
}
|
|
1204
1240
|
|
|
1205
1241
|
/**
|
|
@@ -1344,7 +1380,10 @@ export class StaticFiles {
|
|
|
1344
1380
|
) {
|
|
1345
1381
|
return
|
|
1346
1382
|
}
|
|
1347
|
-
|
|
1383
|
+
// A key shorter than the path leaves the entry unset, so the file streams.
|
|
1384
|
+
if (!tryCopyBytes(keys[at], 0, path, 0, m)) {
|
|
1385
|
+
return
|
|
1386
|
+
}
|
|
1348
1387
|
keyLength[at] = m
|
|
1349
1388
|
keyCoding[at] = this.coding
|
|
1350
1389
|
sizes[at] = this.size
|
|
@@ -1363,9 +1402,16 @@ export class StaticFiles {
|
|
|
1363
1402
|
if (this.slot >= 0) {
|
|
1364
1403
|
return toI32(left)
|
|
1365
1404
|
}
|
|
1366
|
-
|
|
1367
|
-
|
|
1405
|
+
// `chunk` is `STATIC_CHUNK` bytes from the constructor; were it shorter,
|
|
1406
|
+
// the file would go in shorter pieces. `pos` is short of `to` in every
|
|
1407
|
+
// caller, so `left` is not negative.
|
|
1368
1408
|
const chunk: u8[] = this.chunk
|
|
1409
|
+
const room: i32 = toI32(chunk.length)
|
|
1410
|
+
const n: i32 = left > toI64(room) ? room : toI32(left)
|
|
1411
|
+
if (n < 0 || n > toI32(chunk.length)) {
|
|
1412
|
+
return 0
|
|
1413
|
+
}
|
|
1414
|
+
const got: i32 = fileReadAt(this.fd, chunk, 0, n, pos)
|
|
1369
1415
|
for (let k: i32 = 0; k < n && k < toI32(chunk.length); k++) {
|
|
1370
1416
|
if (k >= got) {
|
|
1371
1417
|
chunk[k] = 0
|
|
@@ -1403,8 +1449,9 @@ export class StaticFiles {
|
|
|
1403
1449
|
// A size below 2^31 (`STATIC_MAX_FILE`) has 10 digits, which
|
|
1404
1450
|
// `scratch` holds; -1 would send the 416 without the field, which
|
|
1405
1451
|
// RFC 9110 §15.5.17 only recommends.
|
|
1406
|
-
const at: i32 =
|
|
1407
|
-
|
|
1452
|
+
const at: i32 = tryCopyBytes(scratch, 0, httpFieldBytes("bytes */"), 0, 8)
|
|
1453
|
+
? staticDigits(scratch, 8, this.size, 10)
|
|
1454
|
+
: -1
|
|
1408
1455
|
if (at >= 0) {
|
|
1409
1456
|
names.push(httpFieldBytes("content-range"))
|
|
1410
1457
|
values.push(staticCut(scratch, at))
|
|
@@ -1445,11 +1492,12 @@ export class StaticFiles {
|
|
|
1445
1492
|
values.push(httpFieldBytes(this.coding === STATIC_BR ? "br" : "gzip"))
|
|
1446
1493
|
}
|
|
1447
1494
|
if (status === 206) {
|
|
1448
|
-
copyBytes(scratch, 0, httpFieldBytes("bytes "), 0, 6)
|
|
1449
1495
|
// Three numbers below 2^31 (`STATIC_MAX_FILE`) and the words around
|
|
1450
1496
|
// them, 38 bytes at most, fit `scratch`'s 64, so the field is never
|
|
1451
1497
|
// dropped: a 206 without it would break RFC 9110 §15.3.7.
|
|
1452
|
-
let at: i32 =
|
|
1498
|
+
let at: i32 = tryCopyBytes(scratch, 0, httpFieldBytes("bytes "), 0, 6)
|
|
1499
|
+
? staticDigits(scratch, 6, this.from, 10)
|
|
1500
|
+
: -1
|
|
1453
1501
|
at = staticPut(scratch, at, 45)
|
|
1454
1502
|
at = staticDigits(scratch, at, staticSub64(this.to, 1), 10)
|
|
1455
1503
|
at = staticPut(scratch, at, 47)
|
package/std/net/tls/record.ts
CHANGED
|
@@ -144,6 +144,23 @@ export const TLS_KEY_UPDATE_RECORDS: i64 = 16777216
|
|
|
144
144
|
*/
|
|
145
145
|
export const TLS_LAST_SEQUENCE: i64 = 9007199254740991
|
|
146
146
|
|
|
147
|
+
/**
|
|
148
|
+
* The sequence number after `sequence`, or -1 when no record may use
|
|
149
|
+
* `sequence`: below 0, or at `TLS_LAST_SEQUENCE`, where §5.3's counter would
|
|
150
|
+
* have to wrap. Counted down from `TLS_LAST_SEQUENCE`, so every step is a sum
|
|
151
|
+
* `src/bounds.ts` proves, never a wrap.
|
|
152
|
+
*/
|
|
153
|
+
const tlsNextSequence = (sequence: i64): i64 => {
|
|
154
|
+
if (sequence < 0) {
|
|
155
|
+
return -1
|
|
156
|
+
}
|
|
157
|
+
const left: i64 = TLS_LAST_SEQUENCE - sequence
|
|
158
|
+
if (left < 1) {
|
|
159
|
+
return -1
|
|
160
|
+
}
|
|
161
|
+
return TLS_LAST_SEQUENCE - (left - 1)
|
|
162
|
+
}
|
|
163
|
+
|
|
147
164
|
/** A typed zero for the offsets below: a bare literal is an `f64` under `--number-mode f64`. */
|
|
148
165
|
const TLS_RECORD_FROM: i32 = 0
|
|
149
166
|
|
|
@@ -742,10 +759,9 @@ export class TlsRecordProtection {
|
|
|
742
759
|
}
|
|
743
760
|
return TLS_RECORD_HEADER_SIZE + len
|
|
744
761
|
}
|
|
745
|
-
// The number never passes `TLS_LAST_SEQUENCE` (2^53 - 1) and starts at 0: the guard is that rule
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
if (padding > TLS_MAX_INNER_PLAINTEXT - 1 - len || sequence < 0 || sequence >= 9007199254740991) {
|
|
762
|
+
// The number never passes `TLS_LAST_SEQUENCE` (2^53 - 1) and starts at 0: the guard is that rule.
|
|
763
|
+
const next: i64 = tlsNextSequence(this.sequence)
|
|
764
|
+
if (padding > TLS_MAX_INNER_PLAINTEXT - 1 - len || next < 0) {
|
|
749
765
|
return -TLS_ALERT_INTERNAL_ERROR
|
|
750
766
|
}
|
|
751
767
|
const inner: i32 = len + 1 + padding
|
|
@@ -786,8 +802,7 @@ export class TlsRecordProtection {
|
|
|
786
802
|
out[k] = ciphertext[i]
|
|
787
803
|
k++
|
|
788
804
|
}
|
|
789
|
-
|
|
790
|
-
this.sequence = toI64(toU64(sequence) + toU64(1))
|
|
805
|
+
this.sequence = next
|
|
791
806
|
return TLS_RECORD_HEADER_SIZE + body
|
|
792
807
|
}
|
|
793
808
|
|
|
@@ -874,10 +889,9 @@ export class TlsRecordProtection {
|
|
|
874
889
|
if (body < TLS_RECORD_TAG_SIZE + 1) {
|
|
875
890
|
return -TLS_ALERT_BAD_RECORD_MAC
|
|
876
891
|
}
|
|
877
|
-
// The number never passes `TLS_LAST_SEQUENCE` (2^53 - 1) and starts at 0: the guard is that rule
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
if (sequence < 0 || sequence >= 9007199254740991) {
|
|
892
|
+
// The number never passes `TLS_LAST_SEQUENCE` (2^53 - 1) and starts at 0: the guard is that rule.
|
|
893
|
+
const next: i64 = tlsNextSequence(this.sequence)
|
|
894
|
+
if (next < 0) {
|
|
881
895
|
return -TLS_ALERT_INTERNAL_ERROR
|
|
882
896
|
}
|
|
883
897
|
// `body` is at most 2^14 + 256, so it is a u16 as it is.
|
|
@@ -908,9 +922,8 @@ export class TlsRecordProtection {
|
|
|
908
922
|
}
|
|
909
923
|
const inner: u8[] = opened
|
|
910
924
|
// The record authenticated, so it is the peer's, and its number is spent
|
|
911
|
-
// whatever its content turns out to be.
|
|
912
|
-
|
|
913
|
-
this.sequence = toI64(toU64(sequence) + toU64(1))
|
|
925
|
+
// whatever its content turns out to be.
|
|
926
|
+
this.sequence = next
|
|
914
927
|
const innerLength: i32 = toI32(inner.length)
|
|
915
928
|
if (innerLength > TLS_MAX_INNER_PLAINTEXT) {
|
|
916
929
|
return -TLS_ALERT_RECORD_OVERFLOW
|
package/std/net/tls/ticket.ts
CHANGED
|
@@ -86,6 +86,7 @@ import {
|
|
|
86
86
|
} from "nish/crypto/chacha20poly1305"
|
|
87
87
|
import { tlsSuiteHashLength } from "nish/net/tls/schedule"
|
|
88
88
|
import { Secret, exposeWith, secret, wipe } from "nish:secret"
|
|
89
|
+
import { wrappingAdd } from "nish:unsafe"
|
|
89
90
|
|
|
90
91
|
/** A ticket key's id, sent in the clear at the head of each ticket it seals. */
|
|
91
92
|
export const TLS_TICKET_KEY_ID_SIZE: i32 = 16
|
|
@@ -134,21 +135,21 @@ const TLS_TICKET_NAME_AT: i32 = 22
|
|
|
134
135
|
|
|
135
136
|
/**
|
|
136
137
|
* `a + b`, held at the `i64` range's ends rather than past them. Two values of one
|
|
137
|
-
* sign sum within 2^64 of zero, so
|
|
138
|
-
*
|
|
138
|
+
* sign sum within 2^64 of zero, so `wrappingAdd` crosses the sign bit only when the
|
|
139
|
+
* true sum has left the range: the wrap is how that is seen, and the sum is held instead.
|
|
139
140
|
*/
|
|
140
141
|
const tlsTicketAdd = (a: i64, b: i64): i64 => {
|
|
141
142
|
if (a >= 0) {
|
|
142
143
|
if (b < 0) {
|
|
143
144
|
return a + b
|
|
144
145
|
}
|
|
145
|
-
const sum: i64 =
|
|
146
|
-
return sum >= 0 ? sum : toI64(
|
|
146
|
+
const sum: i64 = wrappingAdd(a, b)
|
|
147
|
+
return sum >= 0 ? sum : toI64(-1) >>> toI64(1)
|
|
147
148
|
}
|
|
148
149
|
if (b >= 0) {
|
|
149
150
|
return a + b
|
|
150
151
|
}
|
|
151
|
-
const sum: i64 =
|
|
152
|
+
const sum: i64 = wrappingAdd(a, b)
|
|
152
153
|
return sum < 0 ? sum : toI64(toU64(1) << toU64(63))
|
|
153
154
|
}
|
|
154
155
|
|
package/std/net/tls-tcp.ts
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* if (slot >= 0) { pollAdd(loop, tls.fd(slot), 1, slot); }
|
|
15
15
|
* // when a connection's token comes back:
|
|
16
16
|
* let wants: i32 = (events & 5) !== 0 ? tls.readable(slot) : tls.writable(slot);
|
|
17
|
-
* if ((wants & TLS_RECORD_SIGN) !== 0) { wants = tls.
|
|
17
|
+
* if ((wants & TLS_RECORD_SIGN) !== 0) { wants = tls.signKey(slot, leafKey); }
|
|
18
18
|
* if ((wants & TLS_RECORD_DATA) !== 0) { tls.read(slot, buf, 0, buf.length); ... }
|
|
19
19
|
* if ((wants & TLS_RECORD_DONE) !== 0) { tls.close(slot); }
|
|
20
20
|
* else { pollModify(loop, tls.fd(slot), wants & 3, slot); }
|
|
@@ -59,10 +59,14 @@
|
|
|
59
59
|
* the slot's copy of the key is wiped once the ServerHello is written, when the
|
|
60
60
|
* handshake fails, or when the slot is closed.
|
|
61
61
|
*
|
|
62
|
-
* **Signing is the caller's**, as `TlsServer`'s is: `
|
|
63
|
-
* CertificateVerify with
|
|
64
|
-
* for the call
|
|
65
|
-
*
|
|
62
|
+
* **Signing is the caller's**, as `TlsServer`'s is: `signKey` signs the
|
|
63
|
+
* CertificateVerify with the key the caller holds as a `Secret` and borrows
|
|
64
|
+
* for the call (P-256 or RSA, as the configuration's `signatureScheme`
|
|
65
|
+
* says), and `signatureInput` / `sign` hand the input out and take any
|
|
66
|
+
* signature back. Nothing checks that the key, the leaf certificate and
|
|
67
|
+
* `signatureScheme` agree: the signer refuses only a key not of its
|
|
68
|
+
* scheme's shape, and a key of the right shape that is not the leaf's signs,
|
|
69
|
+
* so the client's check of the CertificateVerify is what fails.
|
|
66
70
|
*
|
|
67
71
|
* **Resumption is opt-in, and its keys are the caller's.** After
|
|
68
72
|
* `enableTickets`, a slot whose interest says `TLS_RECORD_TICKET` waits on
|
|
@@ -117,7 +121,7 @@ import {
|
|
|
117
121
|
tcpConnect,
|
|
118
122
|
} from "nish:net"
|
|
119
123
|
import { monotonicNanos, wallClockNanos } from "nish:process"
|
|
120
|
-
import {
|
|
124
|
+
import { TlsKeySigner, TlsServer, TlsServerConfig } from "nish/net/tls"
|
|
121
125
|
import { TlsClient, TlsClientConfig } from "nish/net/tls/client"
|
|
122
126
|
import { TLS_RECORD_AUTH, TlsRecordClient } from "nish/net/tls/record-client"
|
|
123
127
|
import { TlsClientTrust, tlsClientAuthenticate, tlsClientSystemTrust } from "nish/net/tls/verify"
|
|
@@ -224,8 +228,11 @@ export class TlsTcpServer {
|
|
|
224
228
|
private scratch: u8[]
|
|
225
229
|
/** The last accepted peer's address, in `nish:net`'s 18-byte form. */
|
|
226
230
|
peer: u8[]
|
|
227
|
-
/**
|
|
228
|
-
|
|
231
|
+
/**
|
|
232
|
+
* Signs every slot's CertificateVerify in `signKey`, in the configuration's
|
|
233
|
+
* scheme: one for the pool, keeping nothing per signature.
|
|
234
|
+
*/
|
|
235
|
+
private signer: TlsKeySigner
|
|
229
236
|
/**
|
|
230
237
|
* Each slot's own copy of its connection's server random: `TlsServer`
|
|
231
238
|
* keeps the array it is given and writes it into the ServerHello only once
|
|
@@ -290,7 +297,7 @@ export class TlsTcpServer {
|
|
|
290
297
|
this.pools = new TlsRecordPools(this.slots)
|
|
291
298
|
this.scratch = new Array<u8>(TLS_MAX_RECORD)
|
|
292
299
|
this.peer = new Array<u8>(TLS_TCP_ADDRESS_SIZE)
|
|
293
|
-
this.signer = new
|
|
300
|
+
this.signer = new TlsKeySigner(config.signatureScheme)
|
|
294
301
|
}
|
|
295
302
|
|
|
296
303
|
/**
|
|
@@ -754,17 +761,18 @@ export class TlsTcpServer {
|
|
|
754
761
|
}
|
|
755
762
|
|
|
756
763
|
/**
|
|
757
|
-
* Signs `slot`'s CertificateVerify with
|
|
758
|
-
*
|
|
759
|
-
*
|
|
760
|
-
* the `
|
|
761
|
-
* `
|
|
764
|
+
* Signs `slot`'s CertificateVerify with `key` in the configuration's
|
|
765
|
+
* `signatureScheme` — a P-256 key for `ecdsa_secp256r1_sha256`, an RSA key
|
|
766
|
+
* for `rsa_pss_rsae_sha256` — and hands it over, as `sign` does, through
|
|
767
|
+
* the pool's `TlsKeySigner`, so a handshake's signature keeps nothing. The
|
|
768
|
+
* key is borrowed: the caller made the `Secret`, and wipes it. A key of the
|
|
769
|
+
* wrong kind or a malformed one fails the connection with `internal_error`.
|
|
762
770
|
*
|
|
763
771
|
* @param slot The slot whose interest says `TLS_RECORD_SIGN`.
|
|
764
|
-
* @param key The certificate's
|
|
772
|
+
* @param key The certificate's private key; borrowed, still the caller's to wipe.
|
|
765
773
|
* @returns The slot's interest after sending; the current interest when the slot wants no signature.
|
|
766
774
|
*/
|
|
767
|
-
|
|
775
|
+
signKey(slot: i32, key: Secret<u8[]>): i32 {
|
|
768
776
|
const input: u8[] | null = this.signatureInput(slot)
|
|
769
777
|
if (input === null) {
|
|
770
778
|
return this.interest(slot)
|
|
@@ -781,6 +789,18 @@ export class TlsTcpServer {
|
|
|
781
789
|
return this.sign(slot, signature)
|
|
782
790
|
}
|
|
783
791
|
|
|
792
|
+
/**
|
|
793
|
+
* `signKey`, under the name it had when P-256 was the only scheme: it signs
|
|
794
|
+
* in whatever scheme the configuration names.
|
|
795
|
+
*
|
|
796
|
+
* @param slot The slot whose interest says `TLS_RECORD_SIGN`.
|
|
797
|
+
* @param key The certificate's private key; borrowed, still the caller's to wipe.
|
|
798
|
+
* @returns The slot's interest after sending.
|
|
799
|
+
*/
|
|
800
|
+
signP256(slot: i32, key: Secret<u8[]>): i32 {
|
|
801
|
+
return this.signKey(slot, key)
|
|
802
|
+
}
|
|
803
|
+
|
|
784
804
|
/**
|
|
785
805
|
* Turns session resumption on for every slot (`TlsServer.enableTickets`):
|
|
786
806
|
* from the next `accept`, a connection offering a ticket, or one that may
|
|
@@ -1337,13 +1357,16 @@ export class TlsTcpClient {
|
|
|
1337
1357
|
let loop: i32 = -1
|
|
1338
1358
|
let result: i32 = 0
|
|
1339
1359
|
while (true) {
|
|
1340
|
-
if (done < len) {
|
|
1341
|
-
//
|
|
1342
|
-
//
|
|
1343
|
-
|
|
1344
|
-
const
|
|
1360
|
+
if (len > 0 && done >= 0 && done < len) {
|
|
1361
|
+
// `done` runs from 0 up to `len`, so the rest is exact; an
|
|
1362
|
+
// `off + done` past `i32` is -1, a window `write` refuses with -22.
|
|
1363
|
+
const rest: i32 = len - done
|
|
1364
|
+
const from: i64 = toI64(off) + toI64(done)
|
|
1365
|
+
const n: i32 = this.write(data, from > toI64(2147483647) ? toI32(-1) : toI32(from), rest)
|
|
1345
1366
|
if (n >= 0) {
|
|
1346
|
-
|
|
1367
|
+
// `write` takes at most `rest`; counted down from `len`.
|
|
1368
|
+
const left: i32 = rest >= 0 ? rest - n : 0
|
|
1369
|
+
done = left > 0 ? len - left : len
|
|
1347
1370
|
} else if (n !== TLS_RECORD_AGAIN) {
|
|
1348
1371
|
result = n
|
|
1349
1372
|
break
|
|
@@ -1504,20 +1527,36 @@ export class TlsTcpClient {
|
|
|
1504
1527
|
}
|
|
1505
1528
|
}
|
|
1506
1529
|
|
|
1530
|
+
/** 2^62 - 1, where a deadline stops: centuries past any clock reading. */
|
|
1531
|
+
const TLS_TCP_CLOCK_MAX: i64 = 1073741824 * 4294967296 - 1
|
|
1532
|
+
|
|
1507
1533
|
/**
|
|
1508
|
-
* The monotonic clock `timeoutMs` milliseconds from now.
|
|
1509
|
-
*
|
|
1510
|
-
*
|
|
1534
|
+
* The monotonic clock `timeoutMs` milliseconds from now. The clock counts up
|
|
1535
|
+
* from boot, far below 2^62, and an `i32` of milliseconds is below 2^52
|
|
1536
|
+
* nanoseconds, so the sum is exact; it is taken below `TLS_TCP_CLOCK_MAX`,
|
|
1537
|
+
* which it would stop at. A negative timeout is a deadline already passed: now.
|
|
1511
1538
|
*/
|
|
1512
|
-
const tlsTcpDeadline = (timeoutMs: i32): i64 =>
|
|
1513
|
-
|
|
1539
|
+
const tlsTcpDeadline = (timeoutMs: i32): i64 => {
|
|
1540
|
+
const now: i64 = monotonicNanos()
|
|
1541
|
+
const span: i64 = toI64(timeoutMs) * TLS_TCP_NANOS_PER_MS
|
|
1542
|
+
if (now < 0 || span < 0) {
|
|
1543
|
+
return now
|
|
1544
|
+
}
|
|
1545
|
+
const room: i64 = TLS_TCP_CLOCK_MAX - now
|
|
1546
|
+
if (room < 0) {
|
|
1547
|
+
return TLS_TCP_CLOCK_MAX
|
|
1548
|
+
}
|
|
1549
|
+
const left: i64 = room - span
|
|
1550
|
+
return left > 0 ? TLS_TCP_CLOCK_MAX - left : TLS_TCP_CLOCK_MAX
|
|
1551
|
+
}
|
|
1514
1552
|
|
|
1515
1553
|
/**
|
|
1516
1554
|
* How many output bytes wait between `start` and `end`, for a loop that runs
|
|
1517
|
-
* while `end > start`.
|
|
1518
|
-
*
|
|
1555
|
+
* while `end > start`. A record layer's output start is never negative; a
|
|
1556
|
+
* pair out of that order has none waiting.
|
|
1519
1557
|
*/
|
|
1520
|
-
const tlsTcpPending = (start: i32, end: i32): i32 =>
|
|
1558
|
+
const tlsTcpPending = (start: i32, end: i32): i32 =>
|
|
1559
|
+
start >= 0 && end >= 0 && end >= start ? end - start : 0
|
|
1521
1560
|
|
|
1522
1561
|
/**
|
|
1523
1562
|
* Opens a TLS 1.3 connection to the server at `addr` (`nish:net`'s 18-byte
|