@amritk/nish-x86_64-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/tls.ts
CHANGED
|
@@ -102,6 +102,7 @@ import {
|
|
|
102
102
|
p256SignSha256,
|
|
103
103
|
p256SignSha256Into,
|
|
104
104
|
} from "nish/crypto/p256"
|
|
105
|
+
import { RsaSignScratch, rsaPrivateKeyBits, rsaSignPssSha256Into } from "nish/crypto/rsa-sign"
|
|
105
106
|
import { timingSafeEqual, timingSafeEqualAt } from "nish/crypto/ct"
|
|
106
107
|
import { X25519_SIZE, x25519, x25519Base } from "nish/crypto/x25519"
|
|
107
108
|
import {
|
|
@@ -121,6 +122,8 @@ import {
|
|
|
121
122
|
TLS_EXT_RECORD_SIZE_LIMIT,
|
|
122
123
|
TLS_RANDOM_SIZE,
|
|
123
124
|
TLS_RECORD_SIZE_LIMIT_MAX,
|
|
125
|
+
TLS_SIGNATURE_ECDSA_SECP256R1_SHA256,
|
|
126
|
+
TLS_SIGNATURE_RSA_PSS_RSAE_SHA256,
|
|
124
127
|
TLS_VERSION_13,
|
|
125
128
|
TLS_P256_SHARE_SIZE,
|
|
126
129
|
TLS_X25519_SHARE_SIZE,
|
|
@@ -229,7 +232,9 @@ export interface TlsServerConfig {
|
|
|
229
232
|
/**
|
|
230
233
|
* The signature scheme the caller's key signs with, sent in
|
|
231
234
|
* CertificateVerify: `TLS_SIGNATURE_ECDSA_SECP256R1_SHA256` for a P-256
|
|
232
|
-
* key
|
|
235
|
+
* key, `TLS_SIGNATURE_RSA_PSS_RSAE_SHA256` for an RSA key of 2048 to 4096
|
|
236
|
+
* bits (`TlsKeySigner` signs either). A client that does not list it is
|
|
237
|
+
* refused with `handshake_failure`.
|
|
233
238
|
*/
|
|
234
239
|
signatureScheme: i32
|
|
235
240
|
/** Whether the carrier is QUIC, which turns `quic_transport_parameters` on both ways. */
|
|
@@ -296,8 +301,8 @@ const tlsExtensionsHave = (extensions: u8[], type: i32): boolean => {
|
|
|
296
301
|
if (length > end - at - 4) {
|
|
297
302
|
return false
|
|
298
303
|
}
|
|
299
|
-
// At most `end
|
|
300
|
-
at =
|
|
304
|
+
// At most `end` by the check above; -1 past 2^31 ends the walk.
|
|
305
|
+
at = tlsSum(tlsSum(at, 4), length)
|
|
301
306
|
}
|
|
302
307
|
return false
|
|
303
308
|
}
|
|
@@ -378,8 +383,8 @@ const tlsDerIntegerInto = (out: u8[], to: i32, rs: u8[], at: i32, length: i32):
|
|
|
378
383
|
j++
|
|
379
384
|
from++
|
|
380
385
|
}
|
|
381
|
-
// Inside `out` by the check above
|
|
382
|
-
return
|
|
386
|
+
// Inside `out` by the check above; -1 past 2^31.
|
|
387
|
+
return tlsSum(tlsSum(to, 2), length)
|
|
383
388
|
}
|
|
384
389
|
|
|
385
390
|
/**
|
|
@@ -502,6 +507,103 @@ export class TlsP256Signer {
|
|
|
502
507
|
}
|
|
503
508
|
}
|
|
504
509
|
|
|
510
|
+
/**
|
|
511
|
+
* Signs CertificateVerify inputs for `rsa_pss_rsae_sha256` (RFC 8446
|
|
512
|
+
* §4.2.3) with nothing kept per signature: `rsaSignPssSha256Into` hashes in
|
|
513
|
+
* this signer's scratch, releases what it allocates, and writes the
|
|
514
|
+
* signature into this signer's own array of the key's length, which
|
|
515
|
+
* `TlsServer.sign` stores as it stores a P-256 one. The array is made by the
|
|
516
|
+
* first signature with a key of a new length and kept, so a server with one
|
|
517
|
+
* key allocates it once. A carrier keeps one signer, not one per slot.
|
|
518
|
+
*/
|
|
519
|
+
export class TlsRsaSigner {
|
|
520
|
+
/** The hashers every signature hashes in. */
|
|
521
|
+
private scratch: RsaSignScratch
|
|
522
|
+
/** The last signature, as long as the key's modulus; empty before the first. */
|
|
523
|
+
private out: u8[]
|
|
524
|
+
|
|
525
|
+
/** Makes the hashers; the signature array waits for the first key. */
|
|
526
|
+
constructor() {
|
|
527
|
+
this.scratch = new RsaSignScratch()
|
|
528
|
+
this.out = []
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* The `rsa_pss_rsae_sha256` signature of `content` under `priv`, in this
|
|
533
|
+
* signer's own array; `null` for a `Secret` that is not an RSA key from
|
|
534
|
+
* `rsaParsePrivateKey` or `x509ParseRsaPrivateKey`, and for a signature
|
|
535
|
+
* that fails the check against the public exponent. The key is borrowed.
|
|
536
|
+
*
|
|
537
|
+
* @param priv The server's RSA private key, borrowed.
|
|
538
|
+
* @param content The CertificateVerify input, as `TlsServer.signatureInput` answers it.
|
|
539
|
+
* @returns This signer's own array, which the next `sign` overwrites, or `null`.
|
|
540
|
+
*/
|
|
541
|
+
sign(priv: Secret<u8[]>, content: u8[]): u8[] | null {
|
|
542
|
+
const bits: i32 = rsaPrivateKeyBits(priv)
|
|
543
|
+
if (bits < 2048 || bits > 4096) {
|
|
544
|
+
return null
|
|
545
|
+
}
|
|
546
|
+
const k: i32 = (bits + 7) >> 3
|
|
547
|
+
if (k < 256 || k > 512) {
|
|
548
|
+
return null
|
|
549
|
+
}
|
|
550
|
+
if (toI32(this.out.length) !== k) {
|
|
551
|
+
this.out = new Array<u8>(k)
|
|
552
|
+
}
|
|
553
|
+
if (
|
|
554
|
+
!rsaSignPssSha256Into(this.scratch, priv, content, TLS_FROM, toI32(content.length), this.out, TLS_FROM)
|
|
555
|
+
) {
|
|
556
|
+
return null
|
|
557
|
+
}
|
|
558
|
+
return this.out
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* The CertificateVerify signer for a configuration's `signatureScheme`: a
|
|
564
|
+
* `TlsP256Signer` for `TLS_SIGNATURE_ECDSA_SECP256R1_SHA256`, a
|
|
565
|
+
* `TlsRsaSigner` for `TLS_SIGNATURE_RSA_PSS_RSAE_SHA256`, and for any other
|
|
566
|
+
* scheme none, so every signature answers `null` and the connection fails
|
|
567
|
+
* with `internal_error`. Only the one the scheme names is made.
|
|
568
|
+
*/
|
|
569
|
+
export class TlsKeySigner {
|
|
570
|
+
/** The P-256 signer, with that scheme. */
|
|
571
|
+
private p256: TlsP256Signer | null
|
|
572
|
+
/** The RSA-PSS signer, with that scheme. */
|
|
573
|
+
private rsa: TlsRsaSigner | null
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Makes the signer `scheme` names.
|
|
577
|
+
*
|
|
578
|
+
* @param scheme The configuration's `signatureScheme`.
|
|
579
|
+
*/
|
|
580
|
+
constructor(scheme: i32) {
|
|
581
|
+
this.p256 = scheme === TLS_SIGNATURE_ECDSA_SECP256R1_SHA256 ? new TlsP256Signer() : null
|
|
582
|
+
this.rsa = scheme === TLS_SIGNATURE_RSA_PSS_RSAE_SHA256 ? new TlsRsaSigner() : null
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* `content` signed under `priv` in the scheme this signer was made for,
|
|
587
|
+
* in the inner signer's own array; `null` for a key that is not of that
|
|
588
|
+
* kind, a signature that fails, or a scheme with no signer.
|
|
589
|
+
*
|
|
590
|
+
* @param priv The server's private key, borrowed.
|
|
591
|
+
* @param content The CertificateVerify input, as `TlsServer.signatureInput` answers it.
|
|
592
|
+
* @returns The inner signer's own array, or `null`.
|
|
593
|
+
*/
|
|
594
|
+
sign(priv: Secret<u8[]>, content: u8[]): u8[] | null {
|
|
595
|
+
const p256: TlsP256Signer | null = this.p256
|
|
596
|
+
if (p256 !== null) {
|
|
597
|
+
return p256.sign(priv, content)
|
|
598
|
+
}
|
|
599
|
+
const rsa: TlsRsaSigner | null = this.rsa
|
|
600
|
+
if (rsa !== null) {
|
|
601
|
+
return rsa.sign(priv, content)
|
|
602
|
+
}
|
|
603
|
+
return null
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
505
607
|
/**
|
|
506
608
|
* Whether an x25519 shared secret is all zeros, reading every byte (run inside `expose`).
|
|
507
609
|
*
|
|
@@ -669,6 +771,19 @@ class TlsExchangeWords {
|
|
|
669
771
|
hashLength: i32 = 0
|
|
670
772
|
}
|
|
671
773
|
|
|
774
|
+
/**
|
|
775
|
+
* `a + b` for two offsets or sizes, or -1 when either is negative or the sum
|
|
776
|
+
* passes 2^31 - 1. The sum is taken in i64, where it cannot overflow; -1 is
|
|
777
|
+
* an offset outside every array and a size no buffer grows to.
|
|
778
|
+
*/
|
|
779
|
+
const tlsSum = (a: i32, b: i32): i32 => {
|
|
780
|
+
if (a < 0 || b < 0) {
|
|
781
|
+
return -1
|
|
782
|
+
}
|
|
783
|
+
const sum: i64 = toI64(a) + toI64(b)
|
|
784
|
+
return sum > toI64(2147483647) ? -1 : toI32(sum)
|
|
785
|
+
}
|
|
786
|
+
|
|
672
787
|
/**
|
|
673
788
|
* `n` zero bytes; none for a negative `n`. A length a `u16` holds is one
|
|
674
789
|
* allocation; a longer one is pushed a byte at a time.
|
|
@@ -699,11 +814,10 @@ const tlsWordAt = (b: u8[], at: i32): u64 => {
|
|
|
699
814
|
}
|
|
700
815
|
|
|
701
816
|
/**
|
|
702
|
-
* `tlsWordAt(b, at + skip)` for a share's later words.
|
|
703
|
-
*
|
|
704
|
-
* the offset lands below zero and the word reads as zero, outside `b`.
|
|
817
|
+
* `tlsWordAt(b, at + skip)` for a share's later words. Where the sum would
|
|
818
|
+
* pass 2^31 the offset is -1, and the word reads as zero, outside `b`.
|
|
705
819
|
*/
|
|
706
|
-
const tlsWordAfter = (b: u8[], at: i32, skip: i32): u64 => tlsWordAt(b,
|
|
820
|
+
const tlsWordAfter = (b: u8[], at: i32, skip: i32): u64 => tlsWordAt(b, tlsSum(at, skip))
|
|
707
821
|
|
|
708
822
|
/** `v` big-endian into `b[at .. at + 8)`; a byte outside `b` is dropped. */
|
|
709
823
|
const tlsWordInto = (b: u8[], at: i32, v: u64): void => {
|
|
@@ -1002,8 +1116,8 @@ const tlsFlightCapacity = (config: TlsServerConfig): i32 => {
|
|
|
1002
1116
|
config.quic ? 0 : TLS_RECORD_SIZE_LIMIT_MAX
|
|
1003
1117
|
)
|
|
1004
1118
|
const certificate: i32 = tlsWriteCertificate(none, TLS_FROM, config.certificateChain)
|
|
1005
|
-
//
|
|
1006
|
-
return
|
|
1119
|
+
// `tlsConfigFits` keeps the sum far below 2^31; past it, -1 sizes nothing.
|
|
1120
|
+
return tlsSum(tlsSum(extensions, certificate), 8 + TLS_ECDSA_SIGNATURE_MAX + 4 + SHA384_SIZE)
|
|
1007
1121
|
}
|
|
1008
1122
|
|
|
1009
1123
|
// ---- Resumption ------------------------------------------------------------------
|
|
@@ -1289,10 +1403,8 @@ export class TlsServer {
|
|
|
1289
1403
|
this.opened = new TlsTicketState()
|
|
1290
1404
|
this.issuing = new TlsTicketState()
|
|
1291
1405
|
this.ticketScratch = tlsZeros(tlsServerTicketCapacity(this.issuing))
|
|
1292
|
-
// A few hundred bytes and a constant
|
|
1293
|
-
this.outputApplication = tlsZeros(
|
|
1294
|
-
toI32(toU32(toI32(this.ticketScratch.length)) + toU32(TLS_SERVER_TICKET_OVERHEAD))
|
|
1295
|
-
)
|
|
1406
|
+
// A few hundred bytes and a constant.
|
|
1407
|
+
this.outputApplication = tlsZeros(tlsSum(toI32(this.ticketScratch.length), TLS_SERVER_TICKET_OVERHEAD))
|
|
1296
1408
|
this.ticketNonce = new Array<u8>(1)
|
|
1297
1409
|
this.ageAddBytes = new Array<u8>(4)
|
|
1298
1410
|
this.restart(serverRandom, ephemeralPrivate)
|
|
@@ -1470,9 +1582,9 @@ export class TlsServer {
|
|
|
1470
1582
|
}
|
|
1471
1583
|
const most: i32 = TLS_MAX_HANDSHAKE_MESSAGE + 4
|
|
1472
1584
|
let size: i32 = capacity
|
|
1473
|
-
// Stopping at `most` gives the same clamped size;
|
|
1585
|
+
// Stopping at `most` gives the same clamped size; an empty buffer starts there.
|
|
1474
1586
|
while (size < need && size < most) {
|
|
1475
|
-
size =
|
|
1587
|
+
size = size > 0 ? tlsSum(size, size) : most
|
|
1476
1588
|
}
|
|
1477
1589
|
const grown: u8[] = tlsZeros(size > most ? most : size)
|
|
1478
1590
|
for (
|
|
@@ -1573,12 +1685,13 @@ export class TlsServer {
|
|
|
1573
1685
|
return 0
|
|
1574
1686
|
}
|
|
1575
1687
|
const type: i32 = toI32(this.input[0])
|
|
1576
|
-
|
|
1688
|
+
// Three bytes, 0 to 2^24 - 1; the mask is the bound the proof reads.
|
|
1689
|
+
const length: i32 =
|
|
1690
|
+
((toI32(this.input[1]) << 16) | (toI32(this.input[2]) << 8) | toI32(this.input[3])) & 0xffffff
|
|
1577
1691
|
if (length > TLS_MAX_HANDSHAKE_MESSAGE) {
|
|
1578
1692
|
return this.fail(TLS_ALERT_DECODE_ERROR)
|
|
1579
1693
|
}
|
|
1580
|
-
|
|
1581
|
-
const whole: i32 = toI32(toU32(4) + toU32(length))
|
|
1694
|
+
const whole: i32 = 4 + length
|
|
1582
1695
|
if (this.inputLength < whole) {
|
|
1583
1696
|
return 0
|
|
1584
1697
|
}
|
|
@@ -1623,8 +1736,8 @@ export class TlsServer {
|
|
|
1623
1736
|
if (tlsSuiteHashLength(suite) > 0) {
|
|
1624
1737
|
return suite
|
|
1625
1738
|
}
|
|
1626
|
-
//
|
|
1627
|
-
k =
|
|
1739
|
+
// `k` is below the data's length less one, so the sum is in range.
|
|
1740
|
+
k = k + 2
|
|
1628
1741
|
}
|
|
1629
1742
|
return 0
|
|
1630
1743
|
}
|
|
@@ -1653,12 +1766,12 @@ export class TlsServer {
|
|
|
1653
1766
|
let at: i32 = hello.alpnAt
|
|
1654
1767
|
while (toI64(at) < end && at >= 0 && at < toI32(data.length)) {
|
|
1655
1768
|
const n: i32 = toI32(data[at])
|
|
1656
|
-
// `at` is inside `data` and `n` a byte
|
|
1657
|
-
if (tlsBytesSpell(data,
|
|
1769
|
+
// `at` is inside `data` and `n` a byte.
|
|
1770
|
+
if (tlsBytesSpell(data, at + 1, n, protocol)) {
|
|
1658
1771
|
this.alpn = protocol
|
|
1659
1772
|
return 0
|
|
1660
1773
|
}
|
|
1661
|
-
at =
|
|
1774
|
+
at = tlsSum(tlsSum(at, 1), n)
|
|
1662
1775
|
}
|
|
1663
1776
|
}
|
|
1664
1777
|
return TLS_ALERT_NO_APPLICATION_PROTOCOL
|
|
@@ -1768,8 +1881,8 @@ export class TlsServer {
|
|
|
1768
1881
|
// difference is the i32 it always was.
|
|
1769
1882
|
this.transcript.update(data, prefix, toI32(toI64(4) + toI64(length) - toI64(prefix)))
|
|
1770
1883
|
} else {
|
|
1771
|
-
//
|
|
1772
|
-
this.transcript.update(data, TLS_FROM,
|
|
1884
|
+
// `length` is three bytes and inside the message.
|
|
1885
|
+
this.transcript.update(data, TLS_FROM, tlsSum(4, length))
|
|
1773
1886
|
}
|
|
1774
1887
|
// The group is the server's choice, not the client's: x25519 whenever the
|
|
1775
1888
|
// client lists it, secp256r1 only for a client that does not. A hello
|
|
@@ -2113,8 +2226,8 @@ export class TlsServer {
|
|
|
2113
2226
|
this.config.quic ? this.config.quicTransportParameters : null,
|
|
2114
2227
|
this.ownRecordSizeLimit()
|
|
2115
2228
|
)
|
|
2116
|
-
// A message's end inside the output and a hash length
|
|
2117
|
-
this.reserveOutput(TLS_LEVEL_HANDSHAKE,
|
|
2229
|
+
// A message's end inside the output and a hash length.
|
|
2230
|
+
this.reserveOutput(TLS_LEVEL_HANDSHAKE, tlsSum(tlsSum(extensionsEnd, 4), this.hashLength))
|
|
2118
2231
|
// The secrets' pool is the slot's, chosen before the blocks open.
|
|
2119
2232
|
const pool: TlsSecretPool = this.secrets()
|
|
2120
2233
|
this.writeResumedHello(pool, index)
|
|
@@ -2351,10 +2464,11 @@ export class TlsServer {
|
|
|
2351
2464
|
return this.fail(TLS_ALERT_INTERNAL_ERROR)
|
|
2352
2465
|
}
|
|
2353
2466
|
const h: i32 = this.hashLength
|
|
2354
|
-
// A length inside the output,
|
|
2467
|
+
// A length inside the output, the two headers (8 and 4 bytes), at most
|
|
2468
|
+
// 65535 and a hash length.
|
|
2355
2469
|
this.reserveOutput(
|
|
2356
2470
|
TLS_LEVEL_HANDSHAKE,
|
|
2357
|
-
|
|
2471
|
+
tlsSum(tlsSum(this.outputHandshakeLength, 12), tlsSum(signatureLength, h))
|
|
2358
2472
|
)
|
|
2359
2473
|
// The secrets' pool is the slot's, chosen before the blocks open.
|
|
2360
2474
|
const pool: TlsSecretPool = this.secrets()
|
|
@@ -2387,8 +2501,8 @@ export class TlsServer {
|
|
|
2387
2501
|
const h: i32 = this.hashLength
|
|
2388
2502
|
using _scope = arena()
|
|
2389
2503
|
const at: i32 = this.outputHandshakeLength
|
|
2390
|
-
// Where the body starts: `at` is inside the output
|
|
2391
|
-
const body: i32 =
|
|
2504
|
+
// Where the body starts: `at` is inside the output.
|
|
2505
|
+
const body: i32 = tlsSum(at, 4)
|
|
2392
2506
|
this.transcript.hashInto(this.hashes, TLS_FROM)
|
|
2393
2507
|
const serverHandshake: u8[] = expose(pool.serverHandshake, tlsHeldClone)
|
|
2394
2508
|
tlsFinishedVerifyDataInto(this.kdf, h, serverHandshake, this.hashes, TLS_FROM, this.outputHandshake, body)
|
|
@@ -2421,8 +2535,8 @@ export class TlsServer {
|
|
|
2421
2535
|
// The only client Finished `handleFinished` accepts is the one just
|
|
2422
2536
|
// computed, so the transcript through it is known now (RFC 8446 §4.6.1),
|
|
2423
2537
|
// and the resumption master secret is derived while the master secret is.
|
|
2424
|
-
// A hash length and its header
|
|
2425
|
-
const clientFinished: u8[] = tlsZeros(
|
|
2538
|
+
// A hash length and its header.
|
|
2539
|
+
const clientFinished: u8[] = tlsZeros(tlsSum(4, h))
|
|
2426
2540
|
tlsWriteFinished(clientFinished, TLS_FROM, finished, TLS_FROM, h)
|
|
2427
2541
|
this.transcript.update(clientFinished, TLS_FROM, toI32(clientFinished.length))
|
|
2428
2542
|
this.transcript.hashInto(this.hashes, TLS_FROM)
|
|
@@ -2551,9 +2665,9 @@ export class TlsServer {
|
|
|
2551
2665
|
* level's buffer, goes into the transcript and stays on the output.
|
|
2552
2666
|
*/
|
|
2553
2667
|
private sent(level: i32, at: i32, end: i32): void {
|
|
2554
|
-
// Both are offsets inside the buffer, `end` past `at
|
|
2555
|
-
//
|
|
2556
|
-
const length: i32 =
|
|
2668
|
+
// Both are offsets inside the buffer, `end` past `at`; anything else
|
|
2669
|
+
// records nothing.
|
|
2670
|
+
const length: i32 = at >= 0 && end >= 0 && end >= at ? end - at : 0
|
|
2557
2671
|
if (level === TLS_LEVEL_INITIAL) {
|
|
2558
2672
|
this.transcript.update(this.outputInitial, at, length)
|
|
2559
2673
|
this.outputInitialLength = end
|
package/std/net/upstream.ts
CHANGED
|
@@ -297,8 +297,9 @@ export class Upstream {
|
|
|
297
297
|
if (connections < 1 || slots < 1 || limits.maxHead < 1 || limits.timeoutMs < 1) {
|
|
298
298
|
panic("Upstream: connections, calls, maxHead and timeoutMs must each be at least 1")
|
|
299
299
|
}
|
|
300
|
-
//
|
|
301
|
-
|
|
300
|
+
// The test's 16 is HANDLER_MAX_WATCHED: past it the sum is past it too,
|
|
301
|
+
// and short of it `firstId + connections` is at most 32.
|
|
302
|
+
if (firstId < 0 || firstId > 16 || connections > 16 || firstId + connections > HANDLER_MAX_WATCHED) {
|
|
302
303
|
panic(
|
|
303
304
|
`Upstream: ${connections} watch ids from ${firstId} are not all inside 0 to ${HANDLER_MAX_WATCHED - 1}`
|
|
304
305
|
)
|
|
@@ -311,15 +312,15 @@ export class Upstream {
|
|
|
311
312
|
this.connections = []
|
|
312
313
|
for (let k: i32 = 0; k < connections; k += 1) {
|
|
313
314
|
// `firstId + k` is below `firstId + connections`, which the check above holds to HANDLER_MAX_WATCHED.
|
|
314
|
-
const id: i32 =
|
|
315
|
+
const id: i32 = firstId + k
|
|
315
316
|
this.connections.push(new UpstreamConnection(id, upstreamParser(limits)))
|
|
316
317
|
}
|
|
317
318
|
const placeholder: HttpRequest = new HttpRequest(-1)
|
|
318
319
|
this.calls = []
|
|
319
320
|
const free: i32[] = upstreamSlots(slots)
|
|
320
321
|
for (let k: i32 = 0; k < slots && k < toI32(free.length); k += 1) {
|
|
321
|
-
// `k` is below `slots`, so `slots - 1 - k` is in 0 .. slots - 1
|
|
322
|
-
free[k] =
|
|
322
|
+
// `k` is below `slots`, so `slots - 1 - k` is in 0 .. slots - 1.
|
|
323
|
+
free[k] = slots - 1 - k
|
|
323
324
|
}
|
|
324
325
|
for (let k: i32 = 0; k < slots; k += 1) {
|
|
325
326
|
this.calls.push(new UpstreamResponse(k, limits.maxHead, placeholder))
|
|
@@ -425,9 +426,9 @@ export class Upstream {
|
|
|
425
426
|
r.length = 0
|
|
426
427
|
r.retried = false
|
|
427
428
|
// The clock counts nanoseconds since boot and the span is under 2^52 ns, so the sum is far inside
|
|
428
|
-
// i64
|
|
429
|
+
// i64 and `upstreamLater` never saturates it.
|
|
429
430
|
const span: i64 = toI64(this.limits.timeoutMs) * UPSTREAM_NS_PER_MS
|
|
430
|
-
r.deadline =
|
|
431
|
+
r.deadline = upstreamLater(monotonicNanos(), span)
|
|
431
432
|
if (this.error !== UPSTREAM_E_NONE) {
|
|
432
433
|
this.end(r, this.error)
|
|
433
434
|
return UPSTREAM_E_NONE
|
|
@@ -504,8 +505,13 @@ export class Upstream {
|
|
|
504
505
|
* @returns The count of slots in use.
|
|
505
506
|
*/
|
|
506
507
|
pending(): i32 {
|
|
507
|
-
// `freeCount` stays in 0 .. free.length, so the
|
|
508
|
-
|
|
508
|
+
// `freeCount` stays in 0 .. free.length, so the test never fires.
|
|
509
|
+
const size: i32 = toI32(this.free.length)
|
|
510
|
+
const free: i32 = this.freeCount
|
|
511
|
+
if (free < 0 || size < free) {
|
|
512
|
+
return 0
|
|
513
|
+
}
|
|
514
|
+
return size - free
|
|
509
515
|
}
|
|
510
516
|
|
|
511
517
|
/**
|
|
@@ -661,14 +667,19 @@ export class Upstream {
|
|
|
661
667
|
const head: u8[] = r.head
|
|
662
668
|
const end: i32 = r.headLength
|
|
663
669
|
// `get` wrote `end` bytes into `head` and `sent` only grows to `end`, so this never fires.
|
|
664
|
-
if (c.sent < 0 || end > toI32(head.length)) {
|
|
670
|
+
if (c.sent < 0 || end < 0 || end > toI32(head.length)) {
|
|
665
671
|
this.finish(c, UPSTREAM_E_REQUEST)
|
|
666
672
|
return
|
|
667
673
|
}
|
|
668
674
|
let sent: i32 = c.sent
|
|
669
675
|
while (sent < end) {
|
|
670
|
-
//
|
|
671
|
-
|
|
676
|
+
// `sent` starts at least 0 and only grows, so this never fires; it is
|
|
677
|
+
// what proves `end - sent` fits.
|
|
678
|
+
if (sent < 0) {
|
|
679
|
+
this.finish(c, UPSTREAM_E_REQUEST)
|
|
680
|
+
return
|
|
681
|
+
}
|
|
682
|
+
const left: i32 = end - sent
|
|
672
683
|
const n: i32 = netWrite(c.fd, head, sent, left)
|
|
673
684
|
if (n === UPSTREAM_WOULD_BLOCK) {
|
|
674
685
|
this.interest(c, UPSTREAM_WRITE)
|
|
@@ -678,8 +689,17 @@ export class Upstream {
|
|
|
678
689
|
this.finish(c, UPSTREAM_E_CLOSED)
|
|
679
690
|
return
|
|
680
691
|
}
|
|
681
|
-
//
|
|
682
|
-
sent
|
|
692
|
+
// `netWrite` writes at most the `left` bytes it was asked for, so `gap`
|
|
693
|
+
// is never negative and `sent` becomes `sent + n`, at most `end`. A
|
|
694
|
+
// larger answer would count the head as all sent.
|
|
695
|
+
let next: i32 = end
|
|
696
|
+
if (left >= 0) {
|
|
697
|
+
const gap: i32 = left - n
|
|
698
|
+
if (gap >= 0) {
|
|
699
|
+
next = end - gap
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
sent = next
|
|
683
703
|
c.sent = sent
|
|
684
704
|
}
|
|
685
705
|
this.interest(c, UPSTREAM_READ)
|
|
@@ -862,10 +882,33 @@ const upstreamSlots = (n: i32): i32[] => {
|
|
|
862
882
|
|
|
863
883
|
/**
|
|
864
884
|
* The poll token the handler's loop reports for watch id `id`: `-2 - id`.
|
|
865
|
-
* Watch ids are in 0 .. HANDLER_MAX_WATCHED,
|
|
866
|
-
*
|
|
885
|
+
* Watch ids are in 0 .. HANDLER_MAX_WATCHED, which the constructor checks,
|
|
886
|
+
* so the difference is a small negative number and the test, whose
|
|
887
|
+
* 2147483646 is the largest id `-2 - id` holds, never fires; past it the
|
|
888
|
+
* answer is the one the unsigned difference gave.
|
|
889
|
+
*/
|
|
890
|
+
const upstreamToken = (id: i32): i32 => {
|
|
891
|
+
if (id > 2147483646) {
|
|
892
|
+
return 2147483647
|
|
893
|
+
}
|
|
894
|
+
return -2 - id
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
/**
|
|
898
|
+
* The `monotonicNanos` instant `span` nanoseconds after `now`, or the last
|
|
899
|
+
* `i64` when the sum would pass it, which no clock reading does. Both are at
|
|
900
|
+
* least 0, so they are summed in `u64`, where two values below 2^63 cannot
|
|
901
|
+
* wrap, and the sum is turned back only once it is known to fit. A negative
|
|
902
|
+
* `now` or `span`, which no caller passes, answers `now`.
|
|
867
903
|
*/
|
|
868
|
-
const
|
|
904
|
+
const upstreamLater = (now: i64, span: i64): i64 => {
|
|
905
|
+
if (now < 0 || span < 0) {
|
|
906
|
+
return now
|
|
907
|
+
}
|
|
908
|
+
const sum: u64 = toU64(now) + toU64(span)
|
|
909
|
+
const top: u64 = (toU64(1) << toU64(63)) - toU64(1)
|
|
910
|
+
return sum > top ? toI64(top) : toI64(sum)
|
|
911
|
+
}
|
|
869
912
|
|
|
870
913
|
/**
|
|
871
914
|
* A ring of call slot indexes, one place per slot. Each slot sits in at most
|
|
@@ -889,9 +932,12 @@ class UpstreamQueue {
|
|
|
889
932
|
if (this.start < 0 || this.start >= n || offset < 0 || offset >= n) {
|
|
890
933
|
return -1
|
|
891
934
|
}
|
|
892
|
-
// Both are below `n`, under 2^31, so the unsigned sum cannot wrap, and one lap brings it back under `n
|
|
935
|
+
// Both are below `n`, under 2^31, so the unsigned sum cannot wrap, and one lap brings it back under `n`:
|
|
936
|
+
// worked in `u32` throughout, the place is at most `n - 1` and turns back exactly.
|
|
937
|
+
const size: u32 = toU32(n)
|
|
893
938
|
const sum: u32 = toU32(this.start) + toU32(offset)
|
|
894
|
-
|
|
939
|
+
const place: u32 = sum < size ? sum : sum - size
|
|
940
|
+
return toI32(place)
|
|
895
941
|
}
|
|
896
942
|
|
|
897
943
|
/** Puts `index` last. */
|