@amritk/nish-x86_64-linux 0.38.0 → 0.40.1
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 +74 -129
- package/bin/nish +0 -0
- package/docs/AI.md +455 -65
- package/package.json +1 -1
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +320 -33
- package/runtime/nish.h +84 -10
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +97 -12
- package/runtime/runtime-async.c +43 -10
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-net.c +64 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +94 -10
- package/runtime/runtime-sandbox.c +404 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-wasm.c +437 -0
- package/runtime/runtime.c +5 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +42 -8
- package/std/README.md +78 -91
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1629 -2732
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +151 -13
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +120 -10
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +344 -111
- package/std/json-typed.ts +256 -12
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/handler.ts +130 -16
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +636 -162
- package/std/net/http1-server.ts +569 -42
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +131 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/postgres-async.ts +1304 -86
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +122 -7
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +373 -27
- package/std/net/quic-stream.ts +195 -37
- package/std/net/quic.ts +516 -22
- package/std/net/router.ts +407 -38
- package/std/net/serve.ts +1022 -110
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls/verify.ts +19 -1
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +129 -19
- package/std/net/webtransport.ts +185 -1
- package/std/raster.ts +837 -0
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/release.ts +593 -0
- package/std/secret.ts +26 -59
- package/std/synth.ts +143 -0
- package/std/text.ts +233 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
package/std/net/tls.ts
CHANGED
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
* space (RFC 9001 §4.1.3). Neither needs anything else from here.
|
|
23
23
|
*
|
|
24
24
|
* **Sans-IO.** Nothing here reads a socket, a clock or the random device. The
|
|
25
|
-
* server random and the ephemeral
|
|
26
|
-
* arguments, so a test injects RFC 8448's and a server draws them with
|
|
25
|
+
* server random and the ephemeral private key (32 bytes, the x25519 key or,
|
|
26
|
+
* its top bit cleared, the secp256r1 scalar) are constructor arguments, so a test injects RFC 8448's and a server draws them with
|
|
27
27
|
* `crypto.getRandomValues`. Signing is handed off the same way: once the
|
|
28
28
|
* Certificate is written the server stops at `TLS_STATE_WAIT_SIGNATURE`, the
|
|
29
29
|
* caller signs `signatureInput()` with its own key (`tlsSignEcdsaP256` for a
|
|
@@ -33,10 +33,13 @@
|
|
|
33
33
|
*
|
|
34
34
|
* **What is negotiated.** TLS 1.3 only. The suites `TLS_AES_128_GCM_SHA256`,
|
|
35
35
|
* `TLS_CHACHA20_POLY1305_SHA256` and `TLS_AES_256_GCM_SHA384`, the first of
|
|
36
|
-
* them the client lists; the group x25519
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
36
|
+
* them the client lists; the group x25519 whenever the client lists it, and
|
|
37
|
+
* secp256r1 for a client that lists only that; the configuration's one
|
|
38
|
+
* signature scheme. A client that lists the chosen group in `supported_groups`
|
|
39
|
+
* without a share for it gets one HelloRetryRequest (§4.1.4), and a second
|
|
40
|
+
* ClientHello still without the share is refused. A secp256r1 share must be
|
|
41
|
+
* an uncompressed point on the curve (§4.2.8.2); any other is
|
|
42
|
+
* `illegal_parameter`. `server_name` is read and exposed as `serverName`.
|
|
40
43
|
* ALPN picks the first of the configuration's protocols the client offers.
|
|
41
44
|
* `quic_transport_parameters` is read and written only when the configuration
|
|
42
45
|
* says the carrier is QUIC. 0-RTT and client authentication are not here:
|
|
@@ -91,7 +94,14 @@ import { HkdfScratch } from "nish/crypto/hkdf"
|
|
|
91
94
|
import { hmacSha256, hmacSha384 } from "nish/crypto/hmac"
|
|
92
95
|
import { SHA256_SIZE } from "nish/crypto/sha256"
|
|
93
96
|
import { SHA384_SIZE } from "nish/crypto/sha512"
|
|
94
|
-
import {
|
|
97
|
+
import {
|
|
98
|
+
P256SignScratch,
|
|
99
|
+
P256_SIGNATURE_SIZE,
|
|
100
|
+
p256Ecdh,
|
|
101
|
+
p256PublicKey,
|
|
102
|
+
p256SignSha256,
|
|
103
|
+
p256SignSha256Into,
|
|
104
|
+
} from "nish/crypto/p256"
|
|
95
105
|
import { timingSafeEqual, timingSafeEqualAt } from "nish/crypto/ct"
|
|
96
106
|
import { X25519_SIZE, x25519, x25519Base } from "nish/crypto/x25519"
|
|
97
107
|
import {
|
|
@@ -104,11 +114,15 @@ import {
|
|
|
104
114
|
TLS_ALERT_NO_APPLICATION_PROTOCOL,
|
|
105
115
|
TLS_ALERT_PROTOCOL_VERSION,
|
|
106
116
|
TLS_ALERT_UNEXPECTED_MESSAGE,
|
|
117
|
+
TLS_GROUP_SECP256R1,
|
|
107
118
|
TLS_GROUP_X25519,
|
|
108
119
|
TLS_HANDSHAKE_CLIENT_HELLO,
|
|
109
120
|
TLS_HANDSHAKE_FINISHED,
|
|
121
|
+
TLS_EXT_RECORD_SIZE_LIMIT,
|
|
110
122
|
TLS_RANDOM_SIZE,
|
|
123
|
+
TLS_RECORD_SIZE_LIMIT_MAX,
|
|
111
124
|
TLS_VERSION_13,
|
|
125
|
+
TLS_P256_SHARE_SIZE,
|
|
112
126
|
TLS_X25519_SHARE_SIZE,
|
|
113
127
|
TlsClientHelloView,
|
|
114
128
|
tlsHexInto,
|
|
@@ -206,8 +220,10 @@ export interface TlsServerConfig {
|
|
|
206
220
|
quicTransportParameters: u8[]
|
|
207
221
|
/**
|
|
208
222
|
* Extensions this module does not interpret, already encoded (each a type,
|
|
209
|
-
* a length and a body), written first in EncryptedExtensions
|
|
210
|
-
*
|
|
223
|
+
* a length and a body), written first in EncryptedExtensions. Empty for
|
|
224
|
+
* none. Not for ALPN, `server_name` or `quic_transport_parameters`, which
|
|
225
|
+
* the server writes itself: a client refuses a repeated extension. A
|
|
226
|
+
* `record_size_limit` here replaces the one the server would write.
|
|
211
227
|
*/
|
|
212
228
|
extraExtensions: u8[]
|
|
213
229
|
/**
|
|
@@ -246,10 +262,11 @@ const tlsConfigFits = (config: TlsServerConfig): boolean => {
|
|
|
246
262
|
certificate = certificate + 5 + n
|
|
247
263
|
}
|
|
248
264
|
// ALPN's type, lengths and the one name (at most 4 + 2 + 1 + 255), the
|
|
249
|
-
// server_name acknowledgement (4),
|
|
250
|
-
// length (4), beside what the caller passes
|
|
265
|
+
// server_name acknowledgement (4), the transport parameters' type and
|
|
266
|
+
// length (4), and `record_size_limit` (6), beside what the caller passes
|
|
267
|
+
// through.
|
|
251
268
|
const extensions: i32 =
|
|
252
|
-
|
|
269
|
+
272 + toI32(config.extraExtensions.length) + toI32(config.quicTransportParameters.length)
|
|
253
270
|
for (const protocol of config.alpn) {
|
|
254
271
|
const n: i32 = toI32(protocol.length)
|
|
255
272
|
if (n < 1 || n > 255) {
|
|
@@ -259,6 +276,23 @@ const tlsConfigFits = (config: TlsServerConfig): boolean => {
|
|
|
259
276
|
return extensions <= 65535
|
|
260
277
|
}
|
|
261
278
|
|
|
279
|
+
/**
|
|
280
|
+
* Whether the encoded extensions `extensions` (each a type, a 16-bit length
|
|
281
|
+
* and a body) carry one of `type`. A length that runs past the end stops the
|
|
282
|
+
* walk.
|
|
283
|
+
*/
|
|
284
|
+
const tlsExtensionsHave = (extensions: u8[], type: i32): boolean => {
|
|
285
|
+
const end: i32 = toI32(extensions.length)
|
|
286
|
+
let at: i32 = 0
|
|
287
|
+
while (at >= 0 && at + 3 < end) {
|
|
288
|
+
if (((toI32(extensions[at]) << 8) | toI32(extensions[at + 1])) === type) {
|
|
289
|
+
return true
|
|
290
|
+
}
|
|
291
|
+
at = at + 4 + ((toI32(extensions[at + 2]) << 8) | toI32(extensions[at + 3]))
|
|
292
|
+
}
|
|
293
|
+
return false
|
|
294
|
+
}
|
|
295
|
+
|
|
262
296
|
/**
|
|
263
297
|
* Whether the bytes `data[at .. at + length)` spell `text`, one character a
|
|
264
298
|
* byte: ALPN's byte-wise comparison (RFC 7301 §3.2), and the one a host name
|
|
@@ -517,10 +551,12 @@ export const tlsHeldCopyOver = (from: u8[], to: Secret<u8[]>): i32 => exposeWith
|
|
|
517
551
|
// ---- The exchange, in a scope of its own --------------------------------------
|
|
518
552
|
|
|
519
553
|
/**
|
|
520
|
-
* The
|
|
521
|
-
* ephemeral private key and the client's share in
|
|
522
|
-
*
|
|
523
|
-
*
|
|
554
|
+
* The ECDHE exchange's inputs and answers as big-endian 64-bit words: the
|
|
555
|
+
* group, the ephemeral private key and the client's share in (32 bytes for
|
|
556
|
+
* x25519, a 65-byte point for secp256r1, so nine share words), the server's
|
|
557
|
+
* public key and the handshake secret out, and the alert. It is a class of
|
|
558
|
+
* numbers and nothing else on purpose. `x25519` and `p256Ecdh` answer their
|
|
559
|
+
* shared secret as a `Secret`,
|
|
524
560
|
* which is an allocation it stores, so neither an automatic arena scope nor a
|
|
525
561
|
* `using a = arena()` block may be put around a call to it (NL2424). A
|
|
526
562
|
* function whose every parameter is an object of numbers can be handed no
|
|
@@ -546,10 +582,20 @@ class TlsExchangeWords {
|
|
|
546
582
|
share1: u64 = 0
|
|
547
583
|
share2: u64 = 0
|
|
548
584
|
share3: u64 = 0
|
|
585
|
+
share4: u64 = 0
|
|
586
|
+
share5: u64 = 0
|
|
587
|
+
share6: u64 = 0
|
|
588
|
+
share7: u64 = 0
|
|
589
|
+
share8: u64 = 0
|
|
549
590
|
public0: u64 = 0
|
|
550
591
|
public1: u64 = 0
|
|
551
592
|
public2: u64 = 0
|
|
552
593
|
public3: u64 = 0
|
|
594
|
+
public4: u64 = 0
|
|
595
|
+
public5: u64 = 0
|
|
596
|
+
public6: u64 = 0
|
|
597
|
+
public7: u64 = 0
|
|
598
|
+
public8: u64 = 0
|
|
553
599
|
secret0: u64 = 0
|
|
554
600
|
secret1: u64 = 0
|
|
555
601
|
secret2: u64 = 0
|
|
@@ -574,6 +620,8 @@ class TlsExchangeWords {
|
|
|
574
620
|
salt4: u64 = 0
|
|
575
621
|
/** @internal Read by the free functions of this module; `TlsServer.exchangeKeys` writes it. */
|
|
576
622
|
salt5: u64 = 0
|
|
623
|
+
/** `TLS_GROUP_X25519` or `TLS_GROUP_SECP256R1`: which exchange to run. */
|
|
624
|
+
group: i32 = 0
|
|
577
625
|
/** 32 or 48: which handshake secret to extract. */
|
|
578
626
|
hashLength: i32 = 0
|
|
579
627
|
}
|
|
@@ -610,11 +658,14 @@ const tlsHandshakeFromWords = (held: u8[], w: TlsExchangeWords): i32 => {
|
|
|
610
658
|
}
|
|
611
659
|
|
|
612
660
|
/**
|
|
613
|
-
* The exchange of RFC 8446 §7.4.2 on `w`: the server's public
|
|
614
|
-
* handshake secret from the ephemeral key and the client's share,
|
|
615
|
-
* written back as words; the caller wipes the words. Answers 0
|
|
616
|
-
* `illegal_parameter` for
|
|
617
|
-
* RFC 7748 §6.1)
|
|
661
|
+
* The exchange of RFC 8446 §7.4.2 on `w`, in `w.group`: the server's public
|
|
662
|
+
* key and the handshake secret from the ephemeral key and the client's share,
|
|
663
|
+
* both written back as words; the caller wipes the words. Answers 0;
|
|
664
|
+
* `illegal_parameter` for an x25519 share whose shared secret is all zeros
|
|
665
|
+
* (§7.4.2, RFC 7748 §6.1) or a secp256r1 share that is not a point on the
|
|
666
|
+
* curve (§4.2.8.2, which `p256Ecdh` checks); or `internal_error` when the
|
|
667
|
+
* server's own public key cannot be made. A secp256r1 key is the same 32
|
|
668
|
+
* bytes with the top bit cleared, so the scalar is below the group order. The
|
|
618
669
|
* copy of the key and the ECDHE secret are `Secret`s wiped on every path, and
|
|
619
670
|
* the handshake secret's bytes with `secureZero` once they are words. The
|
|
620
671
|
* compiler brackets this function with an arena scope of its own (see
|
|
@@ -629,30 +680,43 @@ const tlsExchange = (w: TlsExchangeWords): i32 => {
|
|
|
629
680
|
tlsWordInto(salt, 24, w.salt3)
|
|
630
681
|
tlsWordInto(salt, 32, w.salt4)
|
|
631
682
|
tlsWordInto(salt, 40, w.salt5)
|
|
683
|
+
const p256: boolean = w.group === TLS_GROUP_SECP256R1
|
|
632
684
|
const raw: u8[] = new Array<u8>(X25519_SIZE)
|
|
633
685
|
tlsWordInto(raw, 0, w.key0)
|
|
634
686
|
tlsWordInto(raw, 8, w.key1)
|
|
635
687
|
tlsWordInto(raw, 16, w.key2)
|
|
636
688
|
tlsWordInto(raw, 24, w.key3)
|
|
637
|
-
|
|
689
|
+
if (p256) {
|
|
690
|
+
// Below 2^255, so below the order n: only all zeros is refused.
|
|
691
|
+
raw[0] = raw[0] & toU8(0x7f)
|
|
692
|
+
}
|
|
693
|
+
const share: u8[] = new Array<u8>(p256 ? TLS_P256_SHARE_SIZE : X25519_SIZE)
|
|
638
694
|
tlsWordInto(share, 0, w.share0)
|
|
639
695
|
tlsWordInto(share, 8, w.share1)
|
|
640
696
|
tlsWordInto(share, 16, w.share2)
|
|
641
697
|
tlsWordInto(share, 24, w.share3)
|
|
698
|
+
tlsWordInto(share, 32, w.share4)
|
|
699
|
+
tlsWordInto(share, 40, w.share5)
|
|
700
|
+
tlsWordInto(share, 48, w.share6)
|
|
701
|
+
tlsWordInto(share, 56, w.share7)
|
|
702
|
+
tlsWordInto(share, 64, w.share8)
|
|
642
703
|
const key: Secret<u8[]> = secret(raw)
|
|
643
|
-
const serverPublic: u8[] | null = x25519Base(key)
|
|
644
|
-
const shared: Secret<u8[]> | null = x25519(key, share)
|
|
704
|
+
const serverPublic: u8[] | null = p256 ? p256PublicKey(key) : x25519Base(key)
|
|
705
|
+
const shared: Secret<u8[]> | null = p256 ? p256Ecdh(key, share) : x25519(key, share)
|
|
645
706
|
wipe(key)
|
|
646
707
|
let alert: i32 = 0
|
|
647
708
|
if (shared === null) {
|
|
648
|
-
|
|
709
|
+
// x25519 answers for every share; `p256Ecdh` refuses a point off the
|
|
710
|
+
// curve (§4.2.8.2), or a key `p256PublicKey` refused too.
|
|
711
|
+
alert = p256 && serverPublic !== null ? TLS_ALERT_ILLEGAL_PARAMETER : TLS_ALERT_INTERNAL_ERROR
|
|
649
712
|
} else {
|
|
650
713
|
// RFC 7748 §6.1 leaves the all-zero check to the protocol, and RFC 8446
|
|
651
714
|
// §7.4.2 makes it: a low-order client share gives the zero secret, which
|
|
652
715
|
// anybody can compute. The secret is secret, so the compare reads it all.
|
|
716
|
+
// RFC 8446 asks it of x25519 alone: a secp256r1 share is checked as a point.
|
|
653
717
|
if (serverPublic === null) {
|
|
654
718
|
alert = TLS_ALERT_INTERNAL_ERROR
|
|
655
|
-
} else if (expose(shared, tlsIsZeroSecret)) {
|
|
719
|
+
} else if (!p256 && expose(shared, tlsIsZeroSecret)) {
|
|
656
720
|
alert = TLS_ALERT_ILLEGAL_PARAMETER
|
|
657
721
|
} else {
|
|
658
722
|
const handshake: u8[] =
|
|
@@ -663,6 +727,11 @@ const tlsExchange = (w: TlsExchangeWords): i32 => {
|
|
|
663
727
|
w.public1 = tlsWordAt(serverPublic, 8)
|
|
664
728
|
w.public2 = tlsWordAt(serverPublic, 16)
|
|
665
729
|
w.public3 = tlsWordAt(serverPublic, 24)
|
|
730
|
+
w.public4 = tlsWordAt(serverPublic, 32)
|
|
731
|
+
w.public5 = tlsWordAt(serverPublic, 40)
|
|
732
|
+
w.public6 = tlsWordAt(serverPublic, 48)
|
|
733
|
+
w.public7 = tlsWordAt(serverPublic, 56)
|
|
734
|
+
w.public8 = tlsWordAt(serverPublic, 64)
|
|
666
735
|
w.secret0 = tlsWordAt(handshake, 0)
|
|
667
736
|
w.secret1 = tlsWordAt(handshake, 8)
|
|
668
737
|
w.secret2 = tlsWordAt(handshake, 16)
|
|
@@ -683,7 +752,7 @@ const tlsExchange = (w: TlsExchangeWords): i32 => {
|
|
|
683
752
|
|
|
684
753
|
/** The first input buffer: room for the ClientHello of every common client. It grows only for a longer one. */
|
|
685
754
|
const TLS_INPUT_START: i32 = 2048
|
|
686
|
-
/** The Initial output: a HelloRetryRequest and a ServerHello
|
|
755
|
+
/** The Initial output: a HelloRetryRequest and a ServerHello with a 32-byte session id, a 65-byte secp256r1 share and a PSK, about 250 bytes; it grows for more. */
|
|
687
756
|
const TLS_INITIAL_OUTPUT_SIZE: i32 = 256
|
|
688
757
|
/** The longest DER ECDSA P-256 signature, what the flight is first sized for: two 33-byte integers and their headers. */
|
|
689
758
|
const TLS_ECDSA_SIGNATURE_MAX: i32 = 72
|
|
@@ -852,7 +921,8 @@ const tlsFlightCapacity = (config: TlsServerConfig): i32 => {
|
|
|
852
921
|
config.extraExtensions,
|
|
853
922
|
longest,
|
|
854
923
|
true,
|
|
855
|
-
config.quic ? config.quicTransportParameters : null
|
|
924
|
+
config.quic ? config.quicTransportParameters : null,
|
|
925
|
+
config.quic ? 0 : TLS_RECORD_SIZE_LIMIT_MAX
|
|
856
926
|
)
|
|
857
927
|
const certificate: i32 = tlsWriteCertificate(none, TLS_FROM, config.certificateChain)
|
|
858
928
|
return extensions + certificate + 8 + TLS_ECDSA_SIGNATURE_MAX + 4 + SHA384_SIZE
|
|
@@ -958,7 +1028,7 @@ export class TlsServer {
|
|
|
958
1028
|
/** The configuration this server was built with: its certificate chain, ALPN list and QUIC transport parameters. */
|
|
959
1029
|
config: TlsServerConfig
|
|
960
1030
|
private serverRandom: u8[]
|
|
961
|
-
/** The connection's
|
|
1031
|
+
/** The connection's 32-byte ephemeral private key (x25519, or the secp256r1 scalar with its top bit cleared), copied in by `restart` and wiped by the record layer once the ServerHello is written. */
|
|
962
1032
|
readonly ephemeral: Secret<u8[]>
|
|
963
1033
|
/** The client's `host_name`, or empty. */
|
|
964
1034
|
serverName: string = ""
|
|
@@ -997,8 +1067,15 @@ export class TlsServer {
|
|
|
997
1067
|
private kdf: HkdfScratch
|
|
998
1068
|
/** The exchange's words (`tlsExchange`). */
|
|
999
1069
|
exchange: TlsExchangeWords
|
|
1000
|
-
/**
|
|
1070
|
+
/**
|
|
1071
|
+
* The server's public key for the ServerHello, unpacked from the
|
|
1072
|
+
* exchange's words: `publicX25519` or `publicP256`, whichever `group` names.
|
|
1073
|
+
*/
|
|
1001
1074
|
private serverPublic: u8[]
|
|
1075
|
+
/** Room for an x25519 public key, 32 bytes, made once. */
|
|
1076
|
+
private publicX25519: u8[]
|
|
1077
|
+
/** Room for a secp256r1 public key, a 65-byte uncompressed point, made once. */
|
|
1078
|
+
private publicP256: u8[]
|
|
1002
1079
|
/** A transcript hash on its way into the schedule: HashLen bytes of it. */
|
|
1003
1080
|
private hashes: u8[]
|
|
1004
1081
|
/**
|
|
@@ -1048,10 +1125,17 @@ export class TlsServer {
|
|
|
1048
1125
|
outputHandshakeLength: i32 = 0
|
|
1049
1126
|
/** @internal Read by `net/tls/record-server.ts`. */
|
|
1050
1127
|
outputApplicationLength: i32 = 0
|
|
1128
|
+
/**
|
|
1129
|
+
* The key exchange group: `TLS_GROUP_X25519` or `TLS_GROUP_SECP256R1` once
|
|
1130
|
+
* the ClientHello chose it or a HelloRetryRequest asked for it, 0 before.
|
|
1131
|
+
*/
|
|
1132
|
+
group: i32 = 0
|
|
1051
1133
|
/** Whether a HelloRetryRequest was sent, so a second miss is fatal. */
|
|
1052
1134
|
retried: boolean = false
|
|
1053
1135
|
/** Whether `config` passed `tlsConfigFits`, decided once by the constructor. */
|
|
1054
1136
|
private configFits: boolean = false
|
|
1137
|
+
/** Whether `config.extraExtensions` carries its own `record_size_limit`, which the server then does not write. */
|
|
1138
|
+
private extraRecordSizeLimit: boolean = false
|
|
1055
1139
|
/** Whether the carrier turned resumption on with `enableTickets`; kept across `restart`. */
|
|
1056
1140
|
tickets: boolean = false
|
|
1057
1141
|
/**
|
|
@@ -1070,17 +1154,24 @@ export class TlsServer {
|
|
|
1070
1154
|
issued: boolean = false
|
|
1071
1155
|
/** Whether this connection resumed a ticket (row 1 of the record's table). */
|
|
1072
1156
|
resumed: boolean = false
|
|
1157
|
+
/**
|
|
1158
|
+
* The client's `record_size_limit` (RFC 8449): the most plaintext, content
|
|
1159
|
+
* type and padding included, a protected record to it may carry, from
|
|
1160
|
+
* `TLS_RECORD_SIZE_LIMIT_MIN` to `TLS_RECORD_SIZE_LIMIT_MAX`. 0 when the
|
|
1161
|
+
* client sent none, and always over QUIC, which has no TLS records.
|
|
1162
|
+
*/
|
|
1163
|
+
peerRecordSizeLimit: i32 = 0
|
|
1073
1164
|
|
|
1074
1165
|
/**
|
|
1075
1166
|
* A server for one connection under `config`, with the 32-byte server
|
|
1076
|
-
* random and the 32-byte
|
|
1167
|
+
* random and the 32-byte ephemeral private key drawn for it. A random or key of
|
|
1077
1168
|
* another length, or a configuration `tlsConfigFits` refuses, leaves the
|
|
1078
1169
|
* server failed with `internal_error` before it reads anything.
|
|
1079
1170
|
*
|
|
1080
1171
|
* @param config What the server offers and answers with; held, not copied, and checked by `tlsConfigFits`.
|
|
1081
1172
|
* @param serverRandom The 32 random bytes of the ServerHello's `random`; the array itself is kept, so the
|
|
1082
1173
|
* caller hands over a fresh one.
|
|
1083
|
-
* @param ephemeralPrivate The 32-byte
|
|
1174
|
+
* @param ephemeralPrivate The 32-byte ephemeral private key, for x25519 or secp256r1; copied into the server's `Secret` and then zeroed
|
|
1084
1175
|
* in the caller's array, as `restart` does.
|
|
1085
1176
|
*/
|
|
1086
1177
|
constructor(config: TlsServerConfig, serverRandom: u8[], ephemeralPrivate: u8[]) {
|
|
@@ -1092,6 +1183,7 @@ export class TlsServer {
|
|
|
1092
1183
|
this.clientParameters = []
|
|
1093
1184
|
this.transcript = new TlsTranscript(0)
|
|
1094
1185
|
this.configFits = tlsConfigFits(config)
|
|
1186
|
+
this.extraRecordSizeLimit = tlsExtensionsHave(config.extraExtensions, TLS_EXT_RECORD_SIZE_LIMIT)
|
|
1095
1187
|
this.input = new Array<u8>(TLS_INPUT_START)
|
|
1096
1188
|
this.outputInitial = new Array<u8>(TLS_INITIAL_OUTPUT_SIZE)
|
|
1097
1189
|
this.outputHandshake = new Array<u8>(this.configFits ? tlsFlightCapacity(config) : TLS_FROM)
|
|
@@ -1100,7 +1192,9 @@ export class TlsServer {
|
|
|
1100
1192
|
this.hello = new TlsClientHelloView()
|
|
1101
1193
|
this.kdf = new HkdfScratch()
|
|
1102
1194
|
this.exchange = new TlsExchangeWords()
|
|
1103
|
-
this.
|
|
1195
|
+
this.publicX25519 = new Array<u8>(X25519_SIZE)
|
|
1196
|
+
this.publicP256 = new Array<u8>(TLS_P256_SHARE_SIZE)
|
|
1197
|
+
this.serverPublic = this.publicX25519
|
|
1104
1198
|
this.hashes = new Array<u8>(SHA384_SIZE)
|
|
1105
1199
|
this.salt = new Array<u8>(SHA384_SIZE)
|
|
1106
1200
|
this.binderHash = new Array<u8>(SHA384_SIZE)
|
|
@@ -1115,7 +1209,7 @@ export class TlsServer {
|
|
|
1115
1209
|
|
|
1116
1210
|
/**
|
|
1117
1211
|
* Starts the next connection in this server's buffers, with its own
|
|
1118
|
-
* 32-byte random and
|
|
1212
|
+
* 32-byte random and ephemeral private key, as a fresh `TlsServer` would:
|
|
1119
1213
|
* every secret the last connection left is wiped (`wipe`) and none is
|
|
1120
1214
|
* held. The random is kept, as the constructor keeps it; the key is copied
|
|
1121
1215
|
* into `ephemeral` and the caller's array zeroed with `secureZero`, so the
|
|
@@ -1124,7 +1218,7 @@ export class TlsServer {
|
|
|
1124
1218
|
* failed with `internal_error`.
|
|
1125
1219
|
*
|
|
1126
1220
|
* @param serverRandom The next connection's 32 random bytes; the array itself is kept.
|
|
1127
|
-
* @param ephemeralPrivate The next connection's 32-byte
|
|
1221
|
+
* @param ephemeralPrivate The next connection's 32-byte ephemeral private key; zeroed once copied in.
|
|
1128
1222
|
*/
|
|
1129
1223
|
restart(serverRandom: u8[], ephemeralPrivate: u8[]): void {
|
|
1130
1224
|
const keyLength: i32 = toI32(ephemeralPrivate.length)
|
|
@@ -1138,8 +1232,10 @@ export class TlsServer {
|
|
|
1138
1232
|
this.suite = 0
|
|
1139
1233
|
this.hashLength = 0
|
|
1140
1234
|
this.retried = false
|
|
1235
|
+
this.group = 0
|
|
1141
1236
|
this.serverName = ""
|
|
1142
1237
|
this.alpn = ""
|
|
1238
|
+
this.peerRecordSizeLimit = 0
|
|
1143
1239
|
this.clientTransportParameters = this.none
|
|
1144
1240
|
this.inputLength = 0
|
|
1145
1241
|
this.outputInitialLength = 0
|
|
@@ -1428,6 +1524,16 @@ export class TlsServer {
|
|
|
1428
1524
|
return 0
|
|
1429
1525
|
}
|
|
1430
1526
|
|
|
1527
|
+
/**
|
|
1528
|
+
* The `record_size_limit` the server sends in EncryptedExtensions: the
|
|
1529
|
+
* TLS 1.3 maximum, since this server reads any record the protocol allows,
|
|
1530
|
+
* and only when the client sent one (RFC 8449 §4) and the configuration's
|
|
1531
|
+
* `extraExtensions` do not carry their own; 0 for none.
|
|
1532
|
+
*/
|
|
1533
|
+
private ownRecordSizeLimit(): i32 {
|
|
1534
|
+
return this.peerRecordSizeLimit !== 0 && !this.extraRecordSizeLimit ? TLS_RECORD_SIZE_LIMIT_MAX : 0
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1431
1537
|
/**
|
|
1432
1538
|
* ALPN (RFC 7301 §3.2): the first configured protocol the client offers.
|
|
1433
1539
|
* Answers 0 with `alpn` set, or empty when nothing is negotiated, or
|
|
@@ -1485,6 +1591,9 @@ export class TlsServer {
|
|
|
1485
1591
|
if (hello.alert !== 0) {
|
|
1486
1592
|
return hello.alert
|
|
1487
1593
|
}
|
|
1594
|
+
// RFC 8449 §4: taken from the hello the server answers, so a second
|
|
1595
|
+
// ClientHello's replaces the first's.
|
|
1596
|
+
this.peerRecordSizeLimit = this.config.quic ? 0 : hello.recordSizeLimit
|
|
1488
1597
|
// §4.2.1: the version is negotiated by `supported_versions` alone, and
|
|
1489
1598
|
// `legacy_version` is not consulted for it. The one exception is SSL 3.0
|
|
1490
1599
|
// or below, which §D.5 lets a server refuse outright. Without
|
|
@@ -1525,7 +1634,11 @@ export class TlsServer {
|
|
|
1525
1634
|
if (this.config.quic && !hello.hasQuicTransportParameters) {
|
|
1526
1635
|
return TLS_ALERT_MISSING_EXTENSION
|
|
1527
1636
|
}
|
|
1528
|
-
|
|
1637
|
+
// x25519 first, then secp256r1: a client that lists neither shares no
|
|
1638
|
+
// group with this server (§4.1.1).
|
|
1639
|
+
const offersX25519: boolean = tlsWindowHasU16(data, hello.groupsAt, hello.groupsLength, TLS_GROUP_X25519)
|
|
1640
|
+
const offersP256: boolean = tlsWindowHasU16(data, hello.groupsAt, hello.groupsLength, TLS_GROUP_SECP256R1)
|
|
1641
|
+
if (!offersX25519 && !offersP256) {
|
|
1529
1642
|
return TLS_ALERT_HANDSHAKE_FAILURE
|
|
1530
1643
|
}
|
|
1531
1644
|
if (
|
|
@@ -1549,13 +1662,23 @@ export class TlsServer {
|
|
|
1549
1662
|
} else {
|
|
1550
1663
|
this.transcript.update(data, TLS_FROM, 4 + length)
|
|
1551
1664
|
}
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1665
|
+
// The group is the server's choice, not the client's: x25519 whenever the
|
|
1666
|
+
// client lists it, secp256r1 only for a client that does not. A hello
|
|
1667
|
+
// without a share of that group gets one HelloRetryRequest asking for it
|
|
1668
|
+
// (§4.1.4), and the second hello must carry that share or is
|
|
1669
|
+
// `illegal_parameter`; after a retry `group` is fixed already.
|
|
1670
|
+
const preferred: i32 = offersX25519 ? TLS_GROUP_X25519 : TLS_GROUP_SECP256R1
|
|
1671
|
+
const group: i32 = this.retried ? this.group : preferred
|
|
1672
|
+
const p256Group: boolean = group === TLS_GROUP_SECP256R1
|
|
1673
|
+
const hasShare: boolean = p256Group
|
|
1674
|
+
? offersP256 && hello.hasP256Share
|
|
1675
|
+
: offersX25519 && hello.hasX25519Share
|
|
1676
|
+
if (!hasShare) {
|
|
1555
1677
|
if (this.retried) {
|
|
1556
1678
|
return TLS_ALERT_ILLEGAL_PARAMETER
|
|
1557
1679
|
}
|
|
1558
1680
|
this.retried = true
|
|
1681
|
+
this.group = group
|
|
1559
1682
|
const none: u8[] = []
|
|
1560
1683
|
this.reserveOutput(
|
|
1561
1684
|
TLS_LEVEL_INITIAL,
|
|
@@ -1566,7 +1689,7 @@ export class TlsServer {
|
|
|
1566
1689
|
hello.sessionIdAt,
|
|
1567
1690
|
hello.sessionIdLength,
|
|
1568
1691
|
suite,
|
|
1569
|
-
|
|
1692
|
+
this.group
|
|
1570
1693
|
)
|
|
1571
1694
|
)
|
|
1572
1695
|
using _scope = arena()
|
|
@@ -1579,12 +1702,14 @@ export class TlsServer {
|
|
|
1579
1702
|
hello.sessionIdAt,
|
|
1580
1703
|
hello.sessionIdLength,
|
|
1581
1704
|
this.suite,
|
|
1582
|
-
|
|
1705
|
+
this.group
|
|
1583
1706
|
)
|
|
1584
1707
|
this.sent(TLS_LEVEL_INITIAL, at, end)
|
|
1585
1708
|
return 0
|
|
1586
1709
|
}
|
|
1587
|
-
|
|
1710
|
+
this.group = group
|
|
1711
|
+
const shareLength: i32 = p256Group ? hello.p256ShareLength : hello.x25519ShareLength
|
|
1712
|
+
if (shareLength !== (p256Group ? TLS_P256_SHARE_SIZE : TLS_X25519_SHARE_SIZE)) {
|
|
1588
1713
|
return TLS_ALERT_ILLEGAL_PARAMETER
|
|
1589
1714
|
}
|
|
1590
1715
|
const alpnAlert: i32 = this.chooseAlpn(hello)
|
|
@@ -1884,7 +2009,8 @@ export class TlsServer {
|
|
|
1884
2009
|
this.config.extraExtensions,
|
|
1885
2010
|
this.alpn,
|
|
1886
2011
|
hello.hasServerName,
|
|
1887
|
-
this.config.quic ? this.config.quicTransportParameters : null
|
|
2012
|
+
this.config.quic ? this.config.quicTransportParameters : null,
|
|
2013
|
+
this.ownRecordSizeLimit()
|
|
1888
2014
|
)
|
|
1889
2015
|
this.reserveOutput(TLS_LEVEL_HANDSHAKE, extensionsEnd + 4 + this.hashLength)
|
|
1890
2016
|
// The secrets' pool is the slot's, chosen before the blocks open.
|
|
@@ -1924,14 +2050,15 @@ export class TlsServer {
|
|
|
1924
2050
|
this.config.extraExtensions,
|
|
1925
2051
|
this.alpn,
|
|
1926
2052
|
hello.hasServerName,
|
|
1927
|
-
this.config.quic ? this.config.quicTransportParameters : null
|
|
2053
|
+
this.config.quic ? this.config.quicTransportParameters : null,
|
|
2054
|
+
this.ownRecordSizeLimit()
|
|
1928
2055
|
)
|
|
1929
2056
|
this.sent(TLS_LEVEL_HANDSHAKE, extensionsAt, written)
|
|
1930
2057
|
}
|
|
1931
2058
|
|
|
1932
2059
|
/**
|
|
1933
|
-
* Runs the
|
|
1934
|
-
* share (`tlsExchange`), outside any arena block since its scope is its
|
|
2060
|
+
* Runs the exchange in `group` on the caller's ephemeral key and the
|
|
2061
|
+
* client's share (`tlsExchange`), outside any arena block since its scope is its
|
|
1935
2062
|
* own, and unpacks the server's public key and the handshake secret from
|
|
1936
2063
|
* the words. The words, the ephemeral key's and the handshake secret's
|
|
1937
2064
|
* among them, are wiped with one `wipe` after the branch on the alert, so
|
|
@@ -1943,7 +2070,11 @@ export class TlsServer {
|
|
|
1943
2070
|
private exchangeKeys(): i32 {
|
|
1944
2071
|
const w: TlsExchangeWords = this.exchange
|
|
1945
2072
|
const share: u8[] = this.input
|
|
1946
|
-
const
|
|
2073
|
+
const p256: boolean = this.group === TLS_GROUP_SECP256R1
|
|
2074
|
+
const at: i32 = p256 ? this.hello.p256ShareAt : this.hello.x25519ShareAt
|
|
2075
|
+
// A 65-byte secp256r1 point fills nine words, an x25519 share four; the
|
|
2076
|
+
// rest stay zero from the last `wipe`.
|
|
2077
|
+
w.group = this.group
|
|
1947
2078
|
w.key0 = exposeWith(this.ephemeral, TLS_FROM, tlsWordAt)
|
|
1948
2079
|
w.key1 = exposeWith(this.ephemeral, TLS_FROM + 8, tlsWordAt)
|
|
1949
2080
|
w.key2 = exposeWith(this.ephemeral, TLS_FROM + 16, tlsWordAt)
|
|
@@ -1952,6 +2083,13 @@ export class TlsServer {
|
|
|
1952
2083
|
w.share1 = tlsWordAt(share, at + 8)
|
|
1953
2084
|
w.share2 = tlsWordAt(share, at + 16)
|
|
1954
2085
|
w.share3 = tlsWordAt(share, at + 24)
|
|
2086
|
+
if (p256) {
|
|
2087
|
+
w.share4 = tlsWordAt(share, at + 32)
|
|
2088
|
+
w.share5 = tlsWordAt(share, at + 40)
|
|
2089
|
+
w.share6 = tlsWordAt(share, at + 48)
|
|
2090
|
+
w.share7 = tlsWordAt(share, at + 56)
|
|
2091
|
+
w.share8 = tlsWordAt(share, at + 64)
|
|
2092
|
+
}
|
|
1955
2093
|
w.hashLength = this.hashLength
|
|
1956
2094
|
w.salt0 = tlsWordAt(this.salt, 0)
|
|
1957
2095
|
w.salt1 = tlsWordAt(this.salt, 8)
|
|
@@ -1962,11 +2100,17 @@ export class TlsServer {
|
|
|
1962
2100
|
secureZero(this.salt)
|
|
1963
2101
|
const alert: i32 = tlsExchange(w)
|
|
1964
2102
|
if (alert === 0) {
|
|
1965
|
-
const pub: u8[] = this.
|
|
2103
|
+
const pub: u8[] = p256 ? this.publicP256 : this.publicX25519
|
|
1966
2104
|
tlsWordInto(pub, 0, w.public0)
|
|
1967
2105
|
tlsWordInto(pub, 8, w.public1)
|
|
1968
2106
|
tlsWordInto(pub, 16, w.public2)
|
|
1969
2107
|
tlsWordInto(pub, 24, w.public3)
|
|
2108
|
+
tlsWordInto(pub, 32, w.public4)
|
|
2109
|
+
tlsWordInto(pub, 40, w.public5)
|
|
2110
|
+
tlsWordInto(pub, 48, w.public6)
|
|
2111
|
+
tlsWordInto(pub, 56, w.public7)
|
|
2112
|
+
tlsWordInto(pub, 64, w.public8)
|
|
2113
|
+
this.serverPublic = pub
|
|
1970
2114
|
exposeWith(this.secrets().handshake, w, tlsHandshakeFromWords)
|
|
1971
2115
|
this.held = this.held | (1 << TLS_SECRET_HANDSHAKE)
|
|
1972
2116
|
}
|
|
@@ -2005,7 +2149,8 @@ export class TlsServer {
|
|
|
2005
2149
|
this.config.extraExtensions,
|
|
2006
2150
|
this.alpn,
|
|
2007
2151
|
hello.hasServerName,
|
|
2008
|
-
this.config.quic ? this.config.quicTransportParameters : null
|
|
2152
|
+
this.config.quic ? this.config.quicTransportParameters : null,
|
|
2153
|
+
this.ownRecordSizeLimit()
|
|
2009
2154
|
)
|
|
2010
2155
|
this.reserveOutput(
|
|
2011
2156
|
TLS_LEVEL_HANDSHAKE,
|
|
@@ -2041,7 +2186,8 @@ export class TlsServer {
|
|
|
2041
2186
|
this.config.extraExtensions,
|
|
2042
2187
|
this.alpn,
|
|
2043
2188
|
hello.hasServerName,
|
|
2044
|
-
this.config.quic ? this.config.quicTransportParameters : null
|
|
2189
|
+
this.config.quic ? this.config.quicTransportParameters : null,
|
|
2190
|
+
this.ownRecordSizeLimit()
|
|
2045
2191
|
)
|
|
2046
2192
|
this.sent(TLS_LEVEL_HANDSHAKE, extensionsAt, written)
|
|
2047
2193
|
const certificateEnd: i32 = tlsWriteCertificate(
|