nish 0.35.0 → 0.37.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/docs/AI.md +50 -4
- package/docs/INSTALL.md +73 -59
- package/package.json +19 -5
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/scripts/fuzz.sh +1 -0
- package/scripts/undocumented.txt +14 -0
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/net/tls/codec.ts
CHANGED
|
@@ -2,30 +2,48 @@
|
|
|
2
2
|
* `nish/net/tls/codec` — the TLS 1.3 handshake messages a server and a
|
|
3
3
|
* client read and write (RFC 8446 §4), as bytes.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* it in the hello's `alert` field (`decode_error` for a malformed or truncated
|
|
10
|
-
* one, `illegal_parameter` for a duplicate), never a panic, since every byte
|
|
11
|
-
* of it is the peer's. What the fields *mean* — which version, which suite,
|
|
12
|
-
* whether a share is missing — is the server's to decide, in `nish/net/tls`.
|
|
5
|
+
* import {
|
|
6
|
+
* TLS_VERSION_13, TlsClientHelloView, tlsCopyWindow, tlsEncodeServerHello,
|
|
7
|
+
* tlsReadClientHello, tlsWindowHasU16
|
|
8
|
+
* } from "nish/net/tls/codec";
|
|
13
9
|
*
|
|
14
|
-
* `
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
10
|
+
* // `body` is a ClientHello without its four-byte handshake header;
|
|
11
|
+
* // `random`, `suite` and `x25519Public` are the server's own.
|
|
12
|
+
* const hello = new TlsClientHelloView()
|
|
13
|
+
* tlsReadClientHello(hello, body, 0, toI32(body.length))
|
|
14
|
+
* if (hello.alert !== 0) {
|
|
15
|
+
* // send hello.alert, such as TLS_ALERT_DECODE_ERROR (50), and close
|
|
16
|
+
* } else if (tlsWindowHasU16(hello.data, hello.versionsAt, hello.versionsLength, TLS_VERSION_13)) {
|
|
17
|
+
* const sessionId: u8[] = tlsCopyWindow(hello.data, hello.sessionIdAt, hello.sessionIdLength)
|
|
18
|
+
* const reply: u8[] = tlsEncodeServerHello(random, sessionId, suite, x25519Public)
|
|
19
|
+
* }
|
|
18
20
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
21
|
+
* **One reader and eight writers.** `tlsParseClientHello` reads a ClientHello
|
|
22
|
+
* body into a `TlsClientHello`, checking its structure: every length against
|
|
23
|
+
* the bytes it claims, every vector against its RFC 8446 bounds, and no
|
|
24
|
+
* extension twice (§4.2). A message that breaks one answers the alert a server
|
|
25
|
+
* sends for it in the hello's `alert` field (`decode_error` for a malformed or
|
|
26
|
+
* truncated one, `illegal_parameter` for a duplicate), never a panic, since
|
|
27
|
+
* every byte of it is the peer's. What the fields *mean* — which version,
|
|
28
|
+
* which suite, whether a share is missing — is the server's to decide, in
|
|
29
|
+
* `nish/net/tls`.
|
|
27
30
|
*
|
|
28
|
-
*
|
|
31
|
+
* **Reading without copying.** `tlsReadClientHello` is the same reader for a
|
|
32
|
+
* server that keeps its state in a slot: it reads into a `TlsClientHelloView`
|
|
33
|
+
* made once, whose fields are windows into the message rather than copies of
|
|
34
|
+
* it, so a ClientHello read allocates nothing, and `tlsParseClientHello`
|
|
35
|
+
* copies a view's windows out.
|
|
36
|
+
*
|
|
37
|
+
* **The writers trust their inputs.** Each answers one whole handshake
|
|
38
|
+
* message, header included: ServerHello, a resumed ServerHello that names the
|
|
39
|
+
* PSK it accepts, HelloRetryRequest, EncryptedExtensions, Certificate,
|
|
40
|
+
* CertificateVerify, Finished and NewSessionTicket. Their inputs come from the
|
|
41
|
+
* server and the caller's configuration rather than from the peer, so they
|
|
42
|
+
* check nothing. Each is a `tlsWrite*` function that writes into the caller's
|
|
43
|
+
* array at an offset — run over an empty array, it measures — and a
|
|
44
|
+
* `tlsEncode*` one that answers the message in a fresh array.
|
|
45
|
+
*
|
|
46
|
+
* **The client's side is the mirror**: `tlsWriteClientHello` and
|
|
29
47
|
* `tlsEncodeClientHello` write the hello `nish/net/tls/client` sends, and five
|
|
30
48
|
* readers — `tlsReadServerHello` (a HelloRetryRequest too),
|
|
31
49
|
* `tlsReadEncryptedExtensions`, `tlsReadCertificate`,
|
|
@@ -35,14 +53,15 @@
|
|
|
35
53
|
* are the peer's just as a client's are, so these readers never panic either
|
|
36
54
|
* (the `tls-server-flight` fuzz target).
|
|
37
55
|
*
|
|
38
|
-
* The
|
|
39
|
-
* `signature_algorithms`, `supported_versions` and
|
|
40
|
-
*
|
|
41
|
-
* `quic_transport_parameters` (RFC 9001 §8.2), and
|
|
42
|
-
* `psk_key_exchange_modes` (RFC 8446 §4.2.11, §4.2.9),
|
|
43
|
-
* binders the reader checks against their syntax but
|
|
44
|
-
* and its binder are the server's to judge. Every
|
|
45
|
-
* which is what RFC 8446 §4.2 asks of a server
|
|
56
|
+
* **Extensions.** The ones read are `server_name` (RFC 6066 §3),
|
|
57
|
+
* `supported_groups`, `signature_algorithms`, `supported_versions` and
|
|
58
|
+
* `key_share` (RFC 8446 §4.2), `application_layer_protocol_negotiation`
|
|
59
|
+
* (RFC 7301 §3.1) and `quic_transport_parameters` (RFC 9001 §8.2), and
|
|
60
|
+
* `pre_shared_key` and `psk_key_exchange_modes` (RFC 8446 §4.2.11, §4.2.9),
|
|
61
|
+
* whose identities and binders the reader checks against their syntax but
|
|
62
|
+
* does not open: a ticket and its binder are the server's to judge. Every
|
|
63
|
+
* other extension is skipped, which is what RFC 8446 §4.2 asks of a server
|
|
64
|
+
* that does not know it.
|
|
46
65
|
*
|
|
47
66
|
* Written from the RFCs, not ported from another implementation. Private
|
|
48
67
|
* names carry the `tls` prefix because a `std/` module's private functions
|
|
@@ -52,18 +71,29 @@
|
|
|
52
71
|
|
|
53
72
|
// ---- Handshake message types (RFC 8446 §4) --------------------------------
|
|
54
73
|
|
|
74
|
+
/**
|
|
75
|
+
* `client_hello` (§4.1.2): the first message of a handshake, and the client's second after a
|
|
76
|
+
* HelloRetryRequest.
|
|
77
|
+
*/
|
|
55
78
|
export const TLS_HANDSHAKE_CLIENT_HELLO: i32 = 1
|
|
79
|
+
/** `server_hello` (§4.1.3), which a HelloRetryRequest shares, told apart by its `random`. */
|
|
56
80
|
export const TLS_HANDSHAKE_SERVER_HELLO: i32 = 2
|
|
81
|
+
/** `new_session_ticket` (§4.6.1): a ticket the server sends after the handshake, for resumption. */
|
|
57
82
|
export const TLS_HANDSHAKE_NEW_SESSION_TICKET: i32 = 4
|
|
83
|
+
/** `encrypted_extensions` (§4.3.1): the server's extensions that do not belong in its ServerHello. */
|
|
58
84
|
export const TLS_HANDSHAKE_ENCRYPTED_EXTENSIONS: i32 = 8
|
|
85
|
+
/** `certificate` (§4.4.2): the server's chain, leaf first. */
|
|
59
86
|
export const TLS_HANDSHAKE_CERTIFICATE: i32 = 11
|
|
87
|
+
/** `certificate_verify` (§4.4.3): a signature over the transcript under the leaf's key. */
|
|
60
88
|
export const TLS_HANDSHAKE_CERTIFICATE_VERIFY: i32 = 15
|
|
89
|
+
/** `finished` (§4.4.4): the MAC over the transcript that closes a side's flight. */
|
|
61
90
|
export const TLS_HANDSHAKE_FINISHED: i32 = 20
|
|
62
91
|
/** The synthetic message a HelloRetryRequest puts in the transcript in place of the first ClientHello (§4.4.1). */
|
|
63
92
|
export const TLS_HANDSHAKE_MESSAGE_HASH: i32 = 254
|
|
64
93
|
|
|
65
94
|
// ---- Extension types (RFC 8446 §4.2, RFC 7301, RFC 9001) ------------------
|
|
66
95
|
|
|
96
|
+
/** `server_name` (RFC 6066 §3): the host name the client asks for, or the server's empty acknowledgement. */
|
|
67
97
|
export const TLS_EXT_SERVER_NAME: i32 = 0
|
|
68
98
|
/** `pre_shared_key` (§4.2.11): the client's identities and binders, which must be the last extension. */
|
|
69
99
|
export const TLS_EXT_PRE_SHARED_KEY: i32 = 41
|
|
@@ -71,11 +101,17 @@ export const TLS_EXT_PRE_SHARED_KEY: i32 = 41
|
|
|
71
101
|
export const TLS_EXT_EARLY_DATA: i32 = 42
|
|
72
102
|
/** `psk_key_exchange_modes` (§4.2.9): which modes the client will resume in. */
|
|
73
103
|
export const TLS_EXT_PSK_KEY_EXCHANGE_MODES: i32 = 45
|
|
104
|
+
/** `supported_groups` (§4.2.7): the key-exchange groups the client offers. */
|
|
74
105
|
export const TLS_EXT_SUPPORTED_GROUPS: i32 = 10
|
|
106
|
+
/** `signature_algorithms` (§4.2.3): the schemes a CertificateVerify may be signed with. */
|
|
75
107
|
export const TLS_EXT_SIGNATURE_ALGORITHMS: i32 = 13
|
|
108
|
+
/** `application_layer_protocol_negotiation` (RFC 7301 §3.1): the protocols offered, or the one chosen. */
|
|
76
109
|
export const TLS_EXT_ALPN: i32 = 16
|
|
110
|
+
/** `supported_versions` (§4.2.1): where TLS 1.3 is offered and chosen, since `legacy_version` stays 1.2. */
|
|
77
111
|
export const TLS_EXT_SUPPORTED_VERSIONS: i32 = 43
|
|
112
|
+
/** `key_share` (§4.2.8): the (EC)DHE shares, or a HelloRetryRequest's `selected_group`. */
|
|
78
113
|
export const TLS_EXT_KEY_SHARE: i32 = 51
|
|
114
|
+
/** `quic_transport_parameters` (RFC 9001 §8.2): QUIC's own parameters, opaque to TLS. */
|
|
79
115
|
export const TLS_EXT_QUIC_TRANSPORT_PARAMETERS: i32 = 57
|
|
80
116
|
/** `cookie` (§4.2.2): what a HelloRetryRequest may carry and the client's second ClientHello echoes. */
|
|
81
117
|
export const TLS_EXT_COOKIE: i32 = 44
|
|
@@ -154,9 +190,13 @@ const TLS_CODEC_FROM: i32 = 0
|
|
|
154
190
|
* reader was opened on.
|
|
155
191
|
*/
|
|
156
192
|
class TlsReader {
|
|
193
|
+
/** @internal Read by the free functions of this module. */
|
|
157
194
|
data: u8[]
|
|
195
|
+
/** @internal Read by the free functions of this module. */
|
|
158
196
|
at: i32
|
|
197
|
+
/** @internal Read by the free functions of this module. */
|
|
159
198
|
end: i32
|
|
199
|
+
/** @internal Read by the free functions of this module. */
|
|
160
200
|
failed: boolean = false
|
|
161
201
|
|
|
162
202
|
constructor(data: u8[], at: i32, end: i32) {
|
|
@@ -233,54 +273,85 @@ const tlsReadSkipTo = (r: TlsReader, to: i32): void => {
|
|
|
233
273
|
* in a slot reads its ClientHellos without moving the arena.
|
|
234
274
|
*/
|
|
235
275
|
export class TlsClientHelloView {
|
|
236
|
-
/**
|
|
276
|
+
/**
|
|
277
|
+
* The buffer the windows point into: the one the last hello was read from.
|
|
278
|
+
*
|
|
279
|
+
* @internal Read by `net/tls.ts`.
|
|
280
|
+
*/
|
|
237
281
|
data: u8[]
|
|
238
|
-
/**
|
|
282
|
+
/**
|
|
283
|
+
* One bit per extension type read, for the duplicate check (§4.2); cleared by each read.
|
|
284
|
+
*
|
|
285
|
+
* @internal Read by the free functions of this module.
|
|
286
|
+
*/
|
|
239
287
|
extensionSeen: u32[]
|
|
240
|
-
/**
|
|
288
|
+
/**
|
|
289
|
+
* One bit per group a share was offered for, for the duplicate check (§4.2.8).
|
|
290
|
+
*
|
|
291
|
+
* @internal Read by the free functions of this module.
|
|
292
|
+
*/
|
|
241
293
|
shareSeen: u32[]
|
|
242
|
-
/**
|
|
294
|
+
/**
|
|
295
|
+
* The reader each read runs, kept so that a read allocates none.
|
|
296
|
+
*
|
|
297
|
+
* @internal Read by the free functions of this module.
|
|
298
|
+
*/
|
|
243
299
|
reader: TlsReader
|
|
244
300
|
/** 0 when the message parsed, or the alert it earns: `decode_error` or `illegal_parameter`. */
|
|
245
301
|
alert: i32 = 0
|
|
302
|
+
/** `legacy_version` as sent; TLS 1.3 fixes it at 0x0303 and negotiates the real version in `supported_versions` (§4.1.2). */
|
|
246
303
|
legacyVersion: i32 = 0
|
|
304
|
+
/** Where the client's 32-byte `random` starts in `data`; its length is always 32. */
|
|
247
305
|
randomAt: i32 = 0
|
|
248
306
|
/** `legacy_session_id`, which a server echoes (§4.1.3). */
|
|
249
307
|
sessionIdAt: i32 = 0
|
|
308
|
+
/** The `legacy_session_id`'s length in bytes, 0 to 32. */
|
|
250
309
|
sessionIdLength: i32 = 0
|
|
251
310
|
/** `cipher_suites`, 16-bit values in the client's order. */
|
|
252
311
|
suitesAt: i32 = 0
|
|
312
|
+
/** The `cipher_suites` window's length in bytes, two a suite. */
|
|
253
313
|
suitesLength: i32 = 0
|
|
254
314
|
/** `supported_versions`, 16-bit values. */
|
|
255
315
|
versionsAt: i32 = 0
|
|
316
|
+
/** The `supported_versions` window's length in bytes, two a version; 0 when the extension was absent. */
|
|
256
317
|
versionsLength: i32 = 0
|
|
257
318
|
/** `supported_groups`, 16-bit values. */
|
|
258
319
|
groupsAt: i32 = 0
|
|
320
|
+
/** The `supported_groups` window's length in bytes, two a group; 0 when the extension was absent. */
|
|
259
321
|
groupsLength: i32 = 0
|
|
260
322
|
/** `signature_algorithms`, 16-bit values. */
|
|
261
323
|
schemesAt: i32 = 0
|
|
324
|
+
/** The `signature_algorithms` window's length in bytes, two a scheme; 0 when the extension was absent. */
|
|
262
325
|
schemesLength: i32 = 0
|
|
263
326
|
/** The `client_shares` list of `key_share`, every entry in it. */
|
|
264
327
|
sharesAt: i32 = 0
|
|
328
|
+
/** The `client_shares` window's length in bytes; 0 when `key_share` was absent. */
|
|
265
329
|
sharesLength: i32 = 0
|
|
266
330
|
/** The x25519 share's key_exchange, as sent; its length is the server's to check. */
|
|
267
331
|
x25519ShareAt: i32 = 0
|
|
332
|
+
/** The x25519 share's length in bytes as sent, 0 when none was offered; a usable share is 32. */
|
|
268
333
|
x25519ShareLength: i32 = 0
|
|
269
334
|
/** The `host_name` of `server_name`, or empty. */
|
|
270
335
|
serverNameAt: i32 = 0
|
|
336
|
+
/** The `host_name`'s length in bytes, 0 when `server_name` was absent. */
|
|
271
337
|
serverNameLength: i32 = 0
|
|
272
338
|
/** ALPN's `protocol_name_list`, each name a length byte and its bytes (RFC 7301 §3.1). */
|
|
273
339
|
alpnAt: i32 = 0
|
|
340
|
+
/** The ALPN list's length in bytes, its length bytes included; 0 when ALPN was absent. */
|
|
274
341
|
alpnLength: i32 = 0
|
|
275
342
|
/** The client's `quic_transport_parameters`, opaque (RFC 9001 §8.2). */
|
|
276
343
|
quicAt: i32 = 0
|
|
344
|
+
/** The QUIC transport parameters' length in bytes, 0 when the extension was absent. */
|
|
277
345
|
quicLength: i32 = 0
|
|
278
346
|
/** The extensions block, every extension in it. */
|
|
279
347
|
extensionsAt: i32 = 0
|
|
348
|
+
/** The extensions block's length in bytes, 0 for a hello with none. */
|
|
280
349
|
extensionsLength: i32 = 0
|
|
281
350
|
/** `pre_shared_key`'s `identities` list (§4.2.11): each an identity<1..2^16-1> and a 32-bit obfuscated age. */
|
|
282
351
|
pskIdentitiesAt: i32 = 0
|
|
352
|
+
/** The `identities` window's length in bytes, 0 when `pre_shared_key` was absent. */
|
|
283
353
|
pskIdentitiesLength: i32 = 0
|
|
354
|
+
/** How many identities `identities` holds; the binder count must match it (§4.2.11). */
|
|
284
355
|
pskIdentityCount: i32 = 0
|
|
285
356
|
/**
|
|
286
357
|
* Where `binders`' two-byte length starts: the ClientHello up to here, its
|
|
@@ -290,33 +361,56 @@ export class TlsClientHelloView {
|
|
|
290
361
|
pskBindersPrefixAt: i32 = 0
|
|
291
362
|
/** `pre_shared_key`'s `binders` list: each a PskBinderEntry<32..255>. */
|
|
292
363
|
pskBindersAt: i32 = 0
|
|
364
|
+
/** The `binders` window's length in bytes, 0 when `pre_shared_key` was absent. */
|
|
293
365
|
pskBindersLength: i32 = 0
|
|
366
|
+
/** How many binders `binders` holds, one an identity. */
|
|
294
367
|
pskBinderCount: i32 = 0
|
|
295
368
|
/** `psk_key_exchange_modes`' `ke_modes`, one byte a mode (§4.2.9). */
|
|
296
369
|
pskModesAt: i32 = 0
|
|
370
|
+
/** The `ke_modes` window's length in bytes, one a mode; 0 when the extension was absent. */
|
|
297
371
|
pskModesLength: i32 = 0
|
|
298
372
|
/** The identity `tlsSelectPskIdentity` last chose: its bytes and its obfuscated ticket age. */
|
|
299
373
|
pskIdentityAt: i32 = 0
|
|
374
|
+
/** The chosen identity's length in bytes, 0 until `tlsSelectPskIdentity` chooses one. */
|
|
300
375
|
pskIdentityLength: i32 = 0
|
|
376
|
+
/** The chosen identity's `obfuscated_ticket_age`, as sent: the ticket's age in milliseconds plus its `ticket_age_add`, modulo 2^32 (§4.2.11.1). */
|
|
301
377
|
pskTicketAge: u32 = 0
|
|
302
378
|
/** That identity's binder, the entry of the same index in `binders`. */
|
|
303
379
|
pskBinderAt: i32 = 0
|
|
380
|
+
/**
|
|
381
|
+
* The chosen binder's length in bytes, 32 to 255: `tlsReadPreSharedKey` refuses a shorter one
|
|
382
|
+
* when it parses the extension. It must equal the suite's hash length.
|
|
383
|
+
*/
|
|
304
384
|
pskBinderLength: i32 = 0
|
|
305
385
|
/** Whether `ke_modes` lists `psk_dhe_ke`, the one mode the server resumes in. */
|
|
306
386
|
pskDheKe: boolean = false
|
|
307
387
|
/** Whether `legacy_compression_methods` is the single null method TLS 1.3 requires (§4.1.2). */
|
|
308
388
|
nullCompressionOnly: boolean = false
|
|
389
|
+
/** Whether the hello carried `supported_versions`: without it the client does not offer TLS 1.3 (§4.2.1). */
|
|
309
390
|
hasSupportedVersions: boolean = false
|
|
391
|
+
/** Whether the hello carried `supported_groups`. */
|
|
310
392
|
hasSupportedGroups: boolean = false
|
|
393
|
+
/** Whether the hello carried `key_share`, even an empty one. */
|
|
311
394
|
hasKeyShare: boolean = false
|
|
395
|
+
/** Whether `key_share` offered an x25519 share, so `x25519ShareAt` names one. */
|
|
312
396
|
hasX25519Share: boolean = false
|
|
397
|
+
/** Whether the hello carried `signature_algorithms`. */
|
|
313
398
|
hasSignatureAlgorithms: boolean = false
|
|
399
|
+
/** Whether the hello carried `server_name` with a `host_name` in it. */
|
|
314
400
|
hasServerName: boolean = false
|
|
401
|
+
/** Whether the hello carried ALPN. */
|
|
315
402
|
hasAlpn: boolean = false
|
|
403
|
+
/** Whether the hello carried `quic_transport_parameters`, which a QUIC handshake requires (RFC 9001 §8.2). */
|
|
316
404
|
hasQuicTransportParameters: boolean = false
|
|
405
|
+
/** Whether the hello carried `pre_shared_key`, which must be its last extension (§4.2.11). */
|
|
317
406
|
hasPreSharedKey: boolean = false
|
|
407
|
+
/** Whether the hello carried `psk_key_exchange_modes`, without which a server must not resume (§4.2.9). */
|
|
318
408
|
hasPskModes: boolean = false
|
|
319
409
|
|
|
410
|
+
/**
|
|
411
|
+
* Makes an empty view, every window empty and every flag false, for
|
|
412
|
+
* `tlsReadClientHello` to fill.
|
|
413
|
+
*/
|
|
320
414
|
constructor() {
|
|
321
415
|
this.data = []
|
|
322
416
|
this.extensionSeen = new Array<u32>(TLS_U16_BITMAP_WORDS)
|
|
@@ -324,7 +418,11 @@ export class TlsClientHelloView {
|
|
|
324
418
|
this.reader = new TlsReader(this.data, TLS_CODEC_FROM, TLS_CODEC_FROM)
|
|
325
419
|
}
|
|
326
420
|
|
|
327
|
-
/**
|
|
421
|
+
/**
|
|
422
|
+
* Forgets the last hello: every window empty, every flag down, both bitmaps clear.
|
|
423
|
+
*
|
|
424
|
+
* @internal Read by the free functions of this module.
|
|
425
|
+
*/
|
|
328
426
|
clear(): void {
|
|
329
427
|
this.alert = 0
|
|
330
428
|
this.legacyVersion = 0
|
|
@@ -423,10 +521,27 @@ const tlsReadU16Vector = (r: TlsReader, width: i32, min: i32, max: i32): i32 =>
|
|
|
423
521
|
return end
|
|
424
522
|
}
|
|
425
523
|
|
|
426
|
-
/**
|
|
524
|
+
/**
|
|
525
|
+
* Whether `list` holds `value`.
|
|
526
|
+
*
|
|
527
|
+
* @param list The values to look in, such as a `TlsClientHello`'s `cipherSuites` or `groups`.
|
|
528
|
+
* @param value The code point to look for, such as a suite or a group.
|
|
529
|
+
* @returns true when `value` is somewhere in `list`.
|
|
530
|
+
*/
|
|
427
531
|
export const tlsListHas = (list: i32[], value: i32): boolean => list.indexOf(value) >= 0
|
|
428
532
|
|
|
429
|
-
/**
|
|
533
|
+
/**
|
|
534
|
+
* Whether the 16-bit values `data[at .. at + length)` hold `value`: how a
|
|
535
|
+
* server asks a `TlsClientHelloView`'s windows what it offers without copying
|
|
536
|
+
* them. The values are big-endian pairs; a last odd byte, and any part of the
|
|
537
|
+
* window outside `data`, is not read.
|
|
538
|
+
*
|
|
539
|
+
* @param data The buffer the window points into, such as a view's `data`.
|
|
540
|
+
* @param at Where the window starts in `data`, in bytes.
|
|
541
|
+
* @param length How many bytes the window holds, two a value.
|
|
542
|
+
* @param value The 16-bit code point to look for, such as `TLS_VERSION_13`.
|
|
543
|
+
* @returns true when one of the window's values is `value`.
|
|
544
|
+
*/
|
|
430
545
|
export const tlsWindowHasU16 = (data: u8[], at: i32, length: i32, value: i32): boolean => {
|
|
431
546
|
for (
|
|
432
547
|
let k: i32 = at;
|
|
@@ -627,19 +742,26 @@ const tlsReadExtension = (r: TlsReader, type: i32, end: i32, hello: TlsClientHel
|
|
|
627
742
|
}
|
|
628
743
|
|
|
629
744
|
/**
|
|
630
|
-
* Reads the ClientHello body `data[off .. off + len)` — the message without
|
|
631
|
-
*
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
*
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
*
|
|
745
|
+
* Reads the ClientHello body `data[off .. off + len)` — the message without its
|
|
746
|
+
* four-byte handshake header — into `hello`, which forgets the last one first.
|
|
747
|
+
* Its `alert` is 0 when the body parsed; otherwise the first fault decides it:
|
|
748
|
+
* `decode_error` for a length past its bytes, a vector out of its bounds or
|
|
749
|
+
* bytes left over, and `illegal_parameter` for an extension sent twice (§4.2),
|
|
750
|
+
* a `pre_shared_key` that is not the last extension (§4.2.11), two shares for
|
|
751
|
+
* one group, or a bad `server_name`. A window outside `data` is `decode_error`
|
|
752
|
+
* too, rather than a panic.
|
|
753
|
+
*
|
|
754
|
+
* A hello that parses but offers `pre_shared_key` with a different number of
|
|
755
|
+
* identities and binders is `illegal_parameter`, and one that offers it without
|
|
640
756
|
* `psk_key_exchange_modes` is `missing_extension` (§4.2.9): rows 10 to 13 of
|
|
641
|
-
* the fallback table in `docs/security/tls.md`, "Resumption". Nothing is
|
|
642
|
-
* point into `data`, which must outlive the use made of
|
|
757
|
+
* the fallback table in `docs/security/tls.md`, "Resumption". Nothing is
|
|
758
|
+
* copied: the windows point into `data`, which must outlive the use made of
|
|
759
|
+
* them.
|
|
760
|
+
*
|
|
761
|
+
* @param hello The view to read into, cleared first; its `alert` is the verdict.
|
|
762
|
+
* @param data The buffer that holds the message, which the view's windows then point into.
|
|
763
|
+
* @param off Where the body starts in `data`, in bytes, just past the handshake header.
|
|
764
|
+
* @param len How many bytes the body is, from `off`.
|
|
643
765
|
*/
|
|
644
766
|
export const tlsReadClientHello = (hello: TlsClientHelloView, data: u8[], off: i32, len: i32): void => {
|
|
645
767
|
hello.clear()
|
|
@@ -718,6 +840,10 @@ export const tlsReadClientHello = (hello: TlsClientHelloView, data: u8[], off: i
|
|
|
718
840
|
* answers whether there is one: false for an index out of range, or a hello
|
|
719
841
|
* that offered no `pre_shared_key` or did not parse. It walks the lists
|
|
720
842
|
* `tlsReadClientHello` checked, and allocates nothing.
|
|
843
|
+
*
|
|
844
|
+
* @param hello A view `tlsReadClientHello` has read into; its PSK windows are cleared first either way.
|
|
845
|
+
* @param index Which identity to choose, from 0, in the client's order.
|
|
846
|
+
* @returns true when the identity and its binder were found and the windows point at them.
|
|
721
847
|
*/
|
|
722
848
|
export const tlsSelectPskIdentity = (hello: TlsClientHelloView, index: i32): boolean => {
|
|
723
849
|
hello.pskIdentityAt = 0
|
|
@@ -774,9 +900,11 @@ export const tlsSelectPskIdentity = (hello: TlsClientHelloView, index: i32): boo
|
|
|
774
900
|
* says so.
|
|
775
901
|
*/
|
|
776
902
|
export class TlsClientHello {
|
|
903
|
+
/** The client's 32-byte `random`. */
|
|
777
904
|
random: u8[]
|
|
778
905
|
/** `legacy_session_id`, which a server echoes (§4.1.3). */
|
|
779
906
|
sessionId: u8[]
|
|
907
|
+
/** `cipher_suites`, 16-bit values in the client's order. */
|
|
780
908
|
cipherSuites: i32[]
|
|
781
909
|
/** `supported_versions`, in the client's order. */
|
|
782
910
|
versions: i32[]
|
|
@@ -798,6 +926,7 @@ export class TlsClientHello {
|
|
|
798
926
|
extensionTypes: i32[]
|
|
799
927
|
/** `pre_shared_key`'s identities, in the client's order, and each one's obfuscated ticket age (§4.2.11). */
|
|
800
928
|
pskIdentities: u8[][]
|
|
929
|
+
/** Each identity's `obfuscated_ticket_age`, as sent: entry `i` belongs to `pskIdentities[i]`. */
|
|
801
930
|
pskTicketAges: u32[]
|
|
802
931
|
/** `pre_shared_key`'s binders, one an identity. */
|
|
803
932
|
pskBinders: u8[][]
|
|
@@ -811,22 +940,37 @@ export class TlsClientHello {
|
|
|
811
940
|
pskBindersPrefix: i32 = 0
|
|
812
941
|
/** 0 when the message parsed, or the alert it earns: `decode_error` or `illegal_parameter`. */
|
|
813
942
|
alert: i32 = 0
|
|
943
|
+
/** `legacy_version` as sent; TLS 1.3 fixes it at 0x0303 and negotiates the real version in `supported_versions` (§4.1.2). */
|
|
814
944
|
legacyVersion: i32 = 0
|
|
815
945
|
/** Whether `legacy_compression_methods` is the single null method TLS 1.3 requires (§4.1.2). */
|
|
816
946
|
nullCompressionOnly: boolean = false
|
|
947
|
+
/** Whether the hello carried `supported_versions`: without it the client does not offer TLS 1.3 (§4.2.1). */
|
|
817
948
|
hasSupportedVersions: boolean = false
|
|
949
|
+
/** Whether the hello carried `supported_groups`. */
|
|
818
950
|
hasSupportedGroups: boolean = false
|
|
951
|
+
/** Whether the hello carried `key_share`, even an empty one. */
|
|
819
952
|
hasKeyShare: boolean = false
|
|
953
|
+
/** Whether `key_share` offered an x25519 share, so `x25519Share` holds one. */
|
|
820
954
|
hasX25519Share: boolean = false
|
|
955
|
+
/** Whether the hello carried `signature_algorithms`. */
|
|
821
956
|
hasSignatureAlgorithms: boolean = false
|
|
957
|
+
/** Whether the hello carried `server_name` with a `host_name` in it. */
|
|
822
958
|
hasServerName: boolean = false
|
|
959
|
+
/** Whether the hello carried ALPN. */
|
|
823
960
|
hasAlpn: boolean = false
|
|
961
|
+
/** Whether the hello carried `quic_transport_parameters`, which a QUIC handshake requires (RFC 9001 §8.2). */
|
|
824
962
|
hasQuicTransportParameters: boolean = false
|
|
963
|
+
/** Whether the hello carried `pre_shared_key`, which must be its last extension (§4.2.11). */
|
|
825
964
|
hasPreSharedKey: boolean = false
|
|
965
|
+
/** Whether the hello carried `psk_key_exchange_modes`, without which a server must not resume (§4.2.9). */
|
|
826
966
|
hasPskModes: boolean = false
|
|
827
967
|
/** Whether `psk_key_exchange_modes` lists `psk_dhe_ke`. */
|
|
828
968
|
pskDheKe: boolean = false
|
|
829
969
|
|
|
970
|
+
/**
|
|
971
|
+
* Makes an empty hello, every field empty and every flag false, for
|
|
972
|
+
* `tlsParseClientHello` to fill.
|
|
973
|
+
*/
|
|
830
974
|
constructor() {
|
|
831
975
|
this.random = []
|
|
832
976
|
this.sessionId = []
|
|
@@ -846,7 +990,15 @@ export class TlsClientHello {
|
|
|
846
990
|
}
|
|
847
991
|
}
|
|
848
992
|
|
|
849
|
-
/**
|
|
993
|
+
/**
|
|
994
|
+
* A copy of `data[at .. at + length)`, which is how a window of a view becomes
|
|
995
|
+
* an array of its own that outlives the buffer it was read from.
|
|
996
|
+
*
|
|
997
|
+
* @param data The buffer the window points into.
|
|
998
|
+
* @param at Where the window starts in `data`, in bytes.
|
|
999
|
+
* @param length How many bytes to copy; a negative one copies none.
|
|
1000
|
+
* @returns A fresh array of `length` bytes; nothing from outside `data` is copied into it.
|
|
1001
|
+
*/
|
|
850
1002
|
export const tlsCopyWindow = (data: u8[], at: i32, length: i32): u8[] => {
|
|
851
1003
|
const out: u8[] = new Array<u8>(length < 0 ? TLS_CODEC_FROM : length)
|
|
852
1004
|
for (let k: i32 = 0; k < toI32(out.length) && at + k >= 0 && at + k < toI32(data.length); k++) {
|
|
@@ -868,7 +1020,15 @@ const tlsCopyU16Window = (data: u8[], at: i32, length: i32): i32[] => {
|
|
|
868
1020
|
return out
|
|
869
1021
|
}
|
|
870
1022
|
|
|
871
|
-
/**
|
|
1023
|
+
/**
|
|
1024
|
+
* The bytes of `data[at .. at + length)` as a string of one character a byte:
|
|
1025
|
+
* ASCII here, a host name.
|
|
1026
|
+
*
|
|
1027
|
+
* @param data The buffer the window points into.
|
|
1028
|
+
* @param at Where the window starts in `data`, in bytes.
|
|
1029
|
+
* @param length How many bytes the window holds; any part outside `data` is left out.
|
|
1030
|
+
* @returns A fresh string, one character for each byte read.
|
|
1031
|
+
*/
|
|
872
1032
|
export const tlsWindowString = (data: u8[], at: i32, length: i32): string => {
|
|
873
1033
|
const parts: string[] = []
|
|
874
1034
|
for (let k: i32 = at; k < at + length && k >= 0 && k < toI32(data.length); k++) {
|
|
@@ -881,6 +1041,11 @@ export const tlsWindowString = (data: u8[], at: i32, length: i32): string => {
|
|
|
881
1041
|
* Reads the ClientHello body `data[off .. off + len)` — the message without
|
|
882
1042
|
* its four-byte handshake header — into a fresh `TlsClientHello`, with
|
|
883
1043
|
* `tlsReadClientHello`'s checks and its `alert`, every field copied out.
|
|
1044
|
+
*
|
|
1045
|
+
* @param data The buffer that holds the message; nothing in the answer points into it.
|
|
1046
|
+
* @param off Where the body starts in `data`, in bytes, just past the handshake header.
|
|
1047
|
+
* @param len How many bytes the body is, from `off`.
|
|
1048
|
+
* @returns A fresh hello whose `alert` is 0 when the body parsed, or the alert it earns.
|
|
884
1049
|
*/
|
|
885
1050
|
export const tlsParseClientHello = (data: u8[], off: i32, len: i32): TlsClientHello => {
|
|
886
1051
|
const view = new TlsClientHelloView()
|
|
@@ -1036,6 +1201,11 @@ const tlsHexNibble = (c: i32): i32 => (c <= 57 ? c - 48 : c - 87)
|
|
|
1036
1201
|
* Writes the bytes the lowercase hex constant `text` spells at `out[at]` and
|
|
1037
1202
|
* answers where they end: how this stack's fixed values, written out where
|
|
1038
1203
|
* they are defined, become bytes without an array of their own.
|
|
1204
|
+
*
|
|
1205
|
+
* @param text Lowercase hex digits, two a byte; a last odd digit is ignored, and nothing else is checked.
|
|
1206
|
+
* @param out The array to write into; a byte that would land outside it is dropped.
|
|
1207
|
+
* @param at Where the first byte goes in `out`.
|
|
1208
|
+
* @returns `at` plus the number of bytes `text` spells, written or not.
|
|
1039
1209
|
*/
|
|
1040
1210
|
export const tlsHexInto = (text: string, out: u8[], at: i32): i32 => {
|
|
1041
1211
|
const length: i32 = toI32(text.length) >> 1
|
|
@@ -1116,6 +1286,16 @@ const tlsPutServerHello = (
|
|
|
1116
1286
|
* `sessionId[sessionIdOff .. sessionIdOff + sessionIdLength)`, and carrying
|
|
1117
1287
|
* the server's x25519 share. The extensions are `key_share` then
|
|
1118
1288
|
* `supported_versions`, the order RFC 8448 §3 shows.
|
|
1289
|
+
*
|
|
1290
|
+
* @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
|
|
1291
|
+
* @param at Where the message, handshake header first, starts in `out`.
|
|
1292
|
+
* @param random The server's 32-byte `random` (§4.1.3), written as it is.
|
|
1293
|
+
* @param sessionId The buffer that holds the client's `legacy_session_id`, such as the hello's `data`.
|
|
1294
|
+
* @param sessionIdOff Where the session id starts in `sessionId`.
|
|
1295
|
+
* @param sessionIdLength How many bytes the session id is, 0 to 32.
|
|
1296
|
+
* @param suite The cipher suite the server chose, such as `nish/net/tls/schedule`'s `TLS_AES_128_GCM_SHA256`.
|
|
1297
|
+
* @param x25519Public The server's x25519 public key, the share's `key_exchange`.
|
|
1298
|
+
* @returns Where the message ends in `out`: `at` plus its length, written or not.
|
|
1119
1299
|
*/
|
|
1120
1300
|
export const tlsWriteServerHello = (
|
|
1121
1301
|
out: u8[],
|
|
@@ -1135,6 +1315,18 @@ export const tlsWriteServerHello = (
|
|
|
1135
1315
|
* `pre_shared_key` that names the client's identity number
|
|
1136
1316
|
* `selectedIdentity` (a `uint16`) ahead of `key_share`, since the server
|
|
1137
1317
|
* resumes only in `psk_dhe_ke` and so always sends its share.
|
|
1318
|
+
*
|
|
1319
|
+
* @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
|
|
1320
|
+
* @param at Where the message, handshake header first, starts in `out`.
|
|
1321
|
+
* @param random The server's 32-byte `random` (§4.1.3), written as it is.
|
|
1322
|
+
* @param sessionId The buffer that holds the client's `legacy_session_id`, such as the hello's `data`.
|
|
1323
|
+
* @param sessionIdOff Where the session id starts in `sessionId`.
|
|
1324
|
+
* @param sessionIdLength How many bytes the session id is, 0 to 32.
|
|
1325
|
+
* @param suite The cipher suite the server chose.
|
|
1326
|
+
* @param x25519Public The server's x25519 public key, the share's `key_exchange`.
|
|
1327
|
+
* @param selectedIdentity The index, from 0, of the client's PSK identity the server accepts; only its low
|
|
1328
|
+
* 16 bits are written.
|
|
1329
|
+
* @returns Where the message ends in `out`: `at` plus its length, written or not.
|
|
1138
1330
|
*/
|
|
1139
1331
|
export const tlsWriteServerHelloPsk = (
|
|
1140
1332
|
out: u8[],
|
|
@@ -1164,6 +1356,15 @@ export const tlsWriteServerHelloPsk = (
|
|
|
1164
1356
|
* a ServerHello whose `random` is the fixed HelloRetryRequest value, choosing
|
|
1165
1357
|
* `suite`, echoing the session id, and asking for a share of `group` in
|
|
1166
1358
|
* `key_share`. No cookie is sent.
|
|
1359
|
+
*
|
|
1360
|
+
* @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
|
|
1361
|
+
* @param at Where the message, handshake header first, starts in `out`.
|
|
1362
|
+
* @param sessionId The buffer that holds the client's `legacy_session_id`, such as the hello's `data`.
|
|
1363
|
+
* @param sessionIdOff Where the session id starts in `sessionId`.
|
|
1364
|
+
* @param sessionIdLength How many bytes the session id is, 0 to 32.
|
|
1365
|
+
* @param suite The cipher suite the server chose, which the second ServerHello must choose again.
|
|
1366
|
+
* @param group The group the client is asked to send a share for, such as `TLS_GROUP_X25519`.
|
|
1367
|
+
* @returns Where the message ends in `out`: `at` plus its length, written or not.
|
|
1167
1368
|
*/
|
|
1168
1369
|
export const tlsWriteHelloRetryRequest = (
|
|
1169
1370
|
out: u8[],
|
|
@@ -1193,6 +1394,14 @@ export const tlsWriteHelloRetryRequest = (
|
|
|
1193
1394
|
* a list of exactly one), an empty `server_name` when `acknowledgeServerName`
|
|
1194
1395
|
* (RFC 6066 §3's acknowledgement), and the server's
|
|
1195
1396
|
* `quic_transport_parameters` when `quicTransportParameters` is not `null`.
|
|
1397
|
+
*
|
|
1398
|
+
* @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
|
|
1399
|
+
* @param at Where the message, handshake header first, starts in `out`.
|
|
1400
|
+
* @param extra Whole extensions, type and length included, written first and unchecked; empty for none.
|
|
1401
|
+
* @param alpn The protocol the server chose, one character a byte, at most 255; empty to send no ALPN.
|
|
1402
|
+
* @param acknowledgeServerName Whether to send the empty `server_name` that says the server used the name.
|
|
1403
|
+
* @param quicTransportParameters The server's QUIC transport parameters, opaque; `null` to send none.
|
|
1404
|
+
* @returns Where the message ends in `out`: `at` plus its length, written or not.
|
|
1196
1405
|
*/
|
|
1197
1406
|
export const tlsWriteEncryptedExtensions = (
|
|
1198
1407
|
out: u8[],
|
|
@@ -1232,6 +1441,11 @@ export const tlsWriteEncryptedExtensions = (
|
|
|
1232
1441
|
* Writes a server's Certificate (§4.4.2) at `out[at]` and answers where it
|
|
1233
1442
|
* ends: an empty request context, then each DER certificate of `chain`, leaf
|
|
1234
1443
|
* first, with no extensions of its own.
|
|
1444
|
+
*
|
|
1445
|
+
* @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
|
|
1446
|
+
* @param at Where the message, handshake header first, starts in `out`.
|
|
1447
|
+
* @param chain The DER certificates, leaf first, each written as it is.
|
|
1448
|
+
* @returns Where the message ends in `out`: `at` plus its length, written or not.
|
|
1235
1449
|
*/
|
|
1236
1450
|
export const tlsWriteCertificate = (out: u8[], at: i32, chain: u8[][]): i32 => {
|
|
1237
1451
|
const list: i32 = tlsPutU8(out, at + 4, 0) + 3
|
|
@@ -1247,7 +1461,16 @@ export const tlsWriteCertificate = (out: u8[], at: i32, chain: u8[][]): i32 => {
|
|
|
1247
1461
|
return p
|
|
1248
1462
|
}
|
|
1249
1463
|
|
|
1250
|
-
/**
|
|
1464
|
+
/**
|
|
1465
|
+
* Writes a CertificateVerify (§4.4.3) at `out[at]`: the signature scheme and
|
|
1466
|
+
* the signature as it goes on the wire.
|
|
1467
|
+
*
|
|
1468
|
+
* @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
|
|
1469
|
+
* @param at Where the message, handshake header first, starts in `out`.
|
|
1470
|
+
* @param scheme The signature scheme, such as `TLS_SIGNATURE_ECDSA_SECP256R1_SHA256`.
|
|
1471
|
+
* @param signature The signature over `tlsCertificateVerifyContent`, in the scheme's wire encoding.
|
|
1472
|
+
* @returns Where the message ends in `out`: `at` plus its length, written or not.
|
|
1473
|
+
*/
|
|
1251
1474
|
export const tlsWriteCertificateVerify = (out: u8[], at: i32, scheme: i32, signature: u8[]): i32 => {
|
|
1252
1475
|
const body: i32 = tlsPutU16(out, at + 4, scheme) + 2
|
|
1253
1476
|
const p: i32 = tlsPutBytes(out, body, signature)
|
|
@@ -1256,7 +1479,16 @@ export const tlsWriteCertificateVerify = (out: u8[], at: i32, scheme: i32, signa
|
|
|
1256
1479
|
return p
|
|
1257
1480
|
}
|
|
1258
1481
|
|
|
1259
|
-
/**
|
|
1482
|
+
/**
|
|
1483
|
+
* Writes a Finished (§4.4.4) at `out[at]`, carrying `verifyData[off .. off + len)`.
|
|
1484
|
+
*
|
|
1485
|
+
* @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
|
|
1486
|
+
* @param at Where the message, handshake header first, starts in `out`.
|
|
1487
|
+
* @param verifyData The buffer that holds the `verify_data`, the HMAC the key schedule computed.
|
|
1488
|
+
* @param off Where the `verify_data` starts in `verifyData`.
|
|
1489
|
+
* @param len How many bytes it is: the suite's hash length, 32 or 48.
|
|
1490
|
+
* @returns Where the message ends in `out`: `at` plus its length, written or not.
|
|
1491
|
+
*/
|
|
1260
1492
|
export const tlsWriteFinished = (out: u8[], at: i32, verifyData: u8[], off: i32, len: i32): i32 => {
|
|
1261
1493
|
const p: i32 = tlsPutWindow(out, at + 4, verifyData, off, len)
|
|
1262
1494
|
tlsPutHeader(out, at, TLS_HANDSHAKE_FINISHED, p)
|
|
@@ -1270,6 +1502,19 @@ export const tlsWriteFinished = (out: u8[], at: i32, verifyData: u8[], off: i32,
|
|
|
1270
1502
|
* `ticket[ticketOff .. ticketOff + ticketLength)`, and `extra` as the
|
|
1271
1503
|
* extensions, encoded by the caller and written as they are. The server sends
|
|
1272
1504
|
* none; RFC 8448 §3's ticket carries `early_data`.
|
|
1505
|
+
*
|
|
1506
|
+
* @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
|
|
1507
|
+
* @param at Where the message, handshake header first, starts in `out`.
|
|
1508
|
+
* @param lifetime How long the ticket may be used, in seconds; RFC 8446 §4.6.1 caps it at seven days.
|
|
1509
|
+
* @param ageAdd The `ticket_age_add` that obfuscates the ticket age, which §4.6.1 wants fresh and random.
|
|
1510
|
+
* @param nonce The buffer that holds the `ticket_nonce`.
|
|
1511
|
+
* @param nonceOff Where the nonce starts in `nonce`.
|
|
1512
|
+
* @param nonceLength How many bytes the nonce is, 0 to 255.
|
|
1513
|
+
* @param ticket The buffer that holds the ticket the client will send back.
|
|
1514
|
+
* @param ticketOff Where the ticket starts in `ticket`.
|
|
1515
|
+
* @param ticketLength How many bytes the ticket is: §4.6.1 allows 1 to 65535, not checked here.
|
|
1516
|
+
* @param extra Whole extensions, type and length included, written unchecked; empty for none.
|
|
1517
|
+
* @returns Where the message ends in `out`: `at` plus its length, written or not.
|
|
1273
1518
|
*/
|
|
1274
1519
|
export const tlsWriteNewSessionTicket = (
|
|
1275
1520
|
out: u8[],
|
|
@@ -1301,6 +1546,13 @@ export const tlsWriteNewSessionTicket = (
|
|
|
1301
1546
|
* spaces, the context string "TLS 1.3, server CertificateVerify", a zero byte,
|
|
1302
1547
|
* and the transcript hash `transcriptHash[off .. off + len)` through
|
|
1303
1548
|
* Certificate.
|
|
1549
|
+
*
|
|
1550
|
+
* @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
|
|
1551
|
+
* @param at Where the content starts in `out`. It has no handshake header: it is signed, not sent.
|
|
1552
|
+
* @param transcriptHash The buffer that holds the transcript hash through the server's Certificate.
|
|
1553
|
+
* @param off Where the hash starts in `transcriptHash`.
|
|
1554
|
+
* @param len How many bytes the hash is: the suite's hash length.
|
|
1555
|
+
* @returns Where the content ends in `out`: `at` plus 98 plus `len`, written or not.
|
|
1304
1556
|
*/
|
|
1305
1557
|
export const tlsWriteCertificateVerifyContent = (
|
|
1306
1558
|
out: u8[],
|
|
@@ -1325,6 +1577,12 @@ const tlsBytesString = (name: u8[]): string => tlsWindowString(name, TLS_CODEC_F
|
|
|
1325
1577
|
* A ServerHello (§4.1.3) choosing `suite` and TLS 1.3, echoing the client's
|
|
1326
1578
|
* `sessionId`, and carrying the server's x25519 share, in a fresh array
|
|
1327
1579
|
* (`tlsWriteServerHello`).
|
|
1580
|
+
*
|
|
1581
|
+
* @param random The server's 32-byte `random` (§4.1.3), written as it is.
|
|
1582
|
+
* @param sessionId The client's whole `legacy_session_id`, 0 to 32 bytes, echoed.
|
|
1583
|
+
* @param suite The cipher suite the server chose.
|
|
1584
|
+
* @param x25519Public The server's x25519 public key, the share's `key_exchange`.
|
|
1585
|
+
* @returns The whole message, handshake header included.
|
|
1328
1586
|
*/
|
|
1329
1587
|
export const tlsEncodeServerHello = (random: u8[], sessionId: u8[], suite: i32, x25519Public: u8[]): u8[] => {
|
|
1330
1588
|
const none: u8[] = []
|
|
@@ -1354,7 +1612,18 @@ export const tlsEncodeServerHello = (random: u8[], sessionId: u8[], suite: i32,
|
|
|
1354
1612
|
return out
|
|
1355
1613
|
}
|
|
1356
1614
|
|
|
1357
|
-
/**
|
|
1615
|
+
/**
|
|
1616
|
+
* A resumed handshake's ServerHello naming `selectedIdentity`, in a fresh
|
|
1617
|
+
* array (`tlsWriteServerHelloPsk`).
|
|
1618
|
+
*
|
|
1619
|
+
* @param random The server's 32-byte `random` (§4.1.3), written as it is.
|
|
1620
|
+
* @param sessionId The client's whole `legacy_session_id`, 0 to 32 bytes, echoed.
|
|
1621
|
+
* @param suite The cipher suite the server chose.
|
|
1622
|
+
* @param x25519Public The server's x25519 public key, the share's `key_exchange`.
|
|
1623
|
+
* @param selectedIdentity The index, from 0, of the client's PSK identity the server accepts; only its low
|
|
1624
|
+
* 16 bits are written.
|
|
1625
|
+
* @returns The whole message, handshake header included.
|
|
1626
|
+
*/
|
|
1358
1627
|
export const tlsEncodeServerHelloPsk = (
|
|
1359
1628
|
random: u8[],
|
|
1360
1629
|
sessionId: u8[],
|
|
@@ -1391,7 +1660,15 @@ export const tlsEncodeServerHelloPsk = (
|
|
|
1391
1660
|
return out
|
|
1392
1661
|
}
|
|
1393
1662
|
|
|
1394
|
-
/**
|
|
1663
|
+
/**
|
|
1664
|
+
* A HelloRetryRequest (§4.1.4) choosing `suite`, echoing `sessionId` and
|
|
1665
|
+
* asking for `group`, in a fresh array (`tlsWriteHelloRetryRequest`).
|
|
1666
|
+
*
|
|
1667
|
+
* @param sessionId The client's whole `legacy_session_id`, 0 to 32 bytes, echoed.
|
|
1668
|
+
* @param suite The cipher suite the server chose, which the second ServerHello must choose again.
|
|
1669
|
+
* @param group The group the client is asked to send a share for, such as `TLS_GROUP_X25519`.
|
|
1670
|
+
* @returns The whole message, handshake header included.
|
|
1671
|
+
*/
|
|
1395
1672
|
export const tlsEncodeHelloRetryRequest = (sessionId: u8[], suite: i32, group: i32): u8[] => {
|
|
1396
1673
|
const none: u8[] = []
|
|
1397
1674
|
const sessionIdLength: i32 = toI32(sessionId.length)
|
|
@@ -1405,6 +1682,12 @@ export const tlsEncodeHelloRetryRequest = (sessionId: u8[], suite: i32, group: i
|
|
|
1405
1682
|
/**
|
|
1406
1683
|
* EncryptedExtensions (§4.3.1) in a fresh array (`tlsWriteEncryptedExtensions`),
|
|
1407
1684
|
* the ALPN protocol given as its bytes.
|
|
1685
|
+
*
|
|
1686
|
+
* @param extra Whole extensions, type and length included, written first and unchecked; empty for none.
|
|
1687
|
+
* @param alpn The protocol the server chose, as its bytes, at most 255; empty to send no ALPN.
|
|
1688
|
+
* @param acknowledgeServerName Whether to send the empty `server_name` that says the server used the name.
|
|
1689
|
+
* @param quicTransportParameters The server's QUIC transport parameters, opaque; `null` to send none.
|
|
1690
|
+
* @returns The whole message, handshake header included.
|
|
1408
1691
|
*/
|
|
1409
1692
|
export const tlsEncodeEncryptedExtensions = (
|
|
1410
1693
|
extra: u8[],
|
|
@@ -1435,7 +1718,12 @@ export const tlsEncodeEncryptedExtensions = (
|
|
|
1435
1718
|
return out
|
|
1436
1719
|
}
|
|
1437
1720
|
|
|
1438
|
-
/**
|
|
1721
|
+
/**
|
|
1722
|
+
* A server's Certificate (§4.4.2) in a fresh array (`tlsWriteCertificate`).
|
|
1723
|
+
*
|
|
1724
|
+
* @param chain The DER certificates, leaf first, each written as it is.
|
|
1725
|
+
* @returns The whole message, handshake header included.
|
|
1726
|
+
*/
|
|
1439
1727
|
export const tlsEncodeCertificate = (chain: u8[][]): u8[] => {
|
|
1440
1728
|
const none: u8[] = []
|
|
1441
1729
|
const out: u8[] = new Array<u8>(tlsWriteCertificate(none, TLS_CODEC_FROM, chain))
|
|
@@ -1443,7 +1731,13 @@ export const tlsEncodeCertificate = (chain: u8[][]): u8[] => {
|
|
|
1443
1731
|
return out
|
|
1444
1732
|
}
|
|
1445
1733
|
|
|
1446
|
-
/**
|
|
1734
|
+
/**
|
|
1735
|
+
* A CertificateVerify (§4.4.3) in a fresh array (`tlsWriteCertificateVerify`).
|
|
1736
|
+
*
|
|
1737
|
+
* @param scheme The signature scheme, such as `TLS_SIGNATURE_ECDSA_SECP256R1_SHA256`.
|
|
1738
|
+
* @param signature The signature over `tlsCertificateVerifyContent`, in the scheme's wire encoding.
|
|
1739
|
+
* @returns The whole message, handshake header included.
|
|
1740
|
+
*/
|
|
1447
1741
|
export const tlsEncodeCertificateVerify = (scheme: i32, signature: u8[]): u8[] => {
|
|
1448
1742
|
const none: u8[] = []
|
|
1449
1743
|
const out: u8[] = new Array<u8>(tlsWriteCertificateVerify(none, TLS_CODEC_FROM, scheme, signature))
|
|
@@ -1451,7 +1745,12 @@ export const tlsEncodeCertificateVerify = (scheme: i32, signature: u8[]): u8[] =
|
|
|
1451
1745
|
return out
|
|
1452
1746
|
}
|
|
1453
1747
|
|
|
1454
|
-
/**
|
|
1748
|
+
/**
|
|
1749
|
+
* A Finished (§4.4.4) carrying `verifyData`, in a fresh array (`tlsWriteFinished`).
|
|
1750
|
+
*
|
|
1751
|
+
* @param verifyData The whole `verify_data`, the HMAC the key schedule computed: 32 or 48 bytes.
|
|
1752
|
+
* @returns The whole message, handshake header included.
|
|
1753
|
+
*/
|
|
1455
1754
|
export const tlsEncodeFinished = (verifyData: u8[]): u8[] => {
|
|
1456
1755
|
const none: u8[] = []
|
|
1457
1756
|
const length: i32 = toI32(verifyData.length)
|
|
@@ -1460,7 +1759,16 @@ export const tlsEncodeFinished = (verifyData: u8[]): u8[] => {
|
|
|
1460
1759
|
return out
|
|
1461
1760
|
}
|
|
1462
1761
|
|
|
1463
|
-
/**
|
|
1762
|
+
/**
|
|
1763
|
+
* A NewSessionTicket (§4.6.1) in a fresh array (`tlsWriteNewSessionTicket`).
|
|
1764
|
+
*
|
|
1765
|
+
* @param lifetime How long the ticket may be used, in seconds; RFC 8446 §4.6.1 caps it at seven days.
|
|
1766
|
+
* @param ageAdd The `ticket_age_add` that obfuscates the ticket age, which §4.6.1 wants fresh and random.
|
|
1767
|
+
* @param nonce The whole `ticket_nonce`, 0 to 255 bytes.
|
|
1768
|
+
* @param ticket The whole ticket the client will send back: §4.6.1 allows 1 to 65535 bytes, not checked here.
|
|
1769
|
+
* @param extra Whole extensions, type and length included, written unchecked; empty for none.
|
|
1770
|
+
* @returns The whole message, handshake header included.
|
|
1771
|
+
*/
|
|
1464
1772
|
export const tlsEncodeNewSessionTicket = (
|
|
1465
1773
|
lifetime: u32,
|
|
1466
1774
|
ageAdd: u32,
|
|
@@ -1502,7 +1810,13 @@ export const tlsEncodeNewSessionTicket = (
|
|
|
1502
1810
|
return out
|
|
1503
1811
|
}
|
|
1504
1812
|
|
|
1505
|
-
/**
|
|
1813
|
+
/**
|
|
1814
|
+
* What a server's CertificateVerify signs (§4.4.3), in a fresh array
|
|
1815
|
+
* (`tlsWriteCertificateVerifyContent`).
|
|
1816
|
+
*
|
|
1817
|
+
* @param transcriptHash The whole transcript hash through the server's Certificate.
|
|
1818
|
+
* @returns The content to sign: 98 bytes of prefix, then the hash. It has no handshake header.
|
|
1819
|
+
*/
|
|
1506
1820
|
export const tlsCertificateVerifyContent = (transcriptHash: u8[]): u8[] => {
|
|
1507
1821
|
const none: u8[] = []
|
|
1508
1822
|
const length: i32 = toI32(transcriptHash.length)
|
|
@@ -1526,7 +1840,13 @@ const tlsPutClientSchemes = (out: u8[], at: i32): i32 => {
|
|
|
1526
1840
|
return tlsPutU16(out, p, TLS_SIGNATURE_RSA_PSS_RSAE_SHA512)
|
|
1527
1841
|
}
|
|
1528
1842
|
|
|
1529
|
-
/**
|
|
1843
|
+
/**
|
|
1844
|
+
* Whether `scheme` is one the client's `signature_algorithms` offers, the
|
|
1845
|
+
* only ones a server's CertificateVerify may use (§4.4.3).
|
|
1846
|
+
*
|
|
1847
|
+
* @param scheme The scheme a server's CertificateVerify names.
|
|
1848
|
+
* @returns true for ECDSA P-256 or P-384, or RSA-PSS (rsae) with SHA-256, SHA-384 or SHA-512.
|
|
1849
|
+
*/
|
|
1530
1850
|
export const tlsClientOffersScheme = (scheme: i32): boolean =>
|
|
1531
1851
|
scheme === TLS_SIGNATURE_ECDSA_SECP256R1_SHA256 ||
|
|
1532
1852
|
scheme === TLS_SIGNATURE_ECDSA_SECP384R1_SHA384 ||
|
|
@@ -1543,6 +1863,19 @@ export const tlsClientOffersScheme = (scheme: i32): boolean =>
|
|
|
1543
1863
|
* `supported_versions` (TLS 1.3 alone), a `key_share` with the one entry
|
|
1544
1864
|
* `shareGroup`/`sharePublic`, and `cookie` when `cookie` is not empty (the
|
|
1545
1865
|
* echo of a HelloRetryRequest's, §4.2.2).
|
|
1866
|
+
*
|
|
1867
|
+
* @param out The array to write into; a byte that would land outside it is dropped, so an empty one measures.
|
|
1868
|
+
* @param at Where the message, handshake header first, starts in `out`.
|
|
1869
|
+
* @param random The client's 32-byte `random` (§4.1.2), written as it is.
|
|
1870
|
+
* @param sessionId The `legacy_session_id`, 0 to 32 bytes; the client sends 32 for middlebox compatibility.
|
|
1871
|
+
* @param suites The cipher suites offered, in order of preference.
|
|
1872
|
+
* @param groups The groups `supported_groups` offers, in order of preference.
|
|
1873
|
+
* @param shareGroup The group of the one key share sent, such as `TLS_GROUP_X25519`.
|
|
1874
|
+
* @param sharePublic That share's public key, its `key_exchange`.
|
|
1875
|
+
* @param serverName The host name for `server_name`, ASCII; empty to leave the extension out.
|
|
1876
|
+
* @param alpn The ALPN protocols offered, one character a byte, in order; empty to leave ALPN out.
|
|
1877
|
+
* @param cookie A HelloRetryRequest's `cookie` to echo; empty to send none.
|
|
1878
|
+
* @returns Where the message ends in `out`: `at` plus its length, written or not.
|
|
1546
1879
|
*/
|
|
1547
1880
|
export const tlsWriteClientHello = (
|
|
1548
1881
|
out: u8[],
|
|
@@ -1631,7 +1964,20 @@ export const tlsWriteClientHello = (
|
|
|
1631
1964
|
return p
|
|
1632
1965
|
}
|
|
1633
1966
|
|
|
1634
|
-
/**
|
|
1967
|
+
/**
|
|
1968
|
+
* A ClientHello (§4.1.2) in a fresh array (`tlsWriteClientHello`).
|
|
1969
|
+
*
|
|
1970
|
+
* @param random The client's 32-byte `random` (§4.1.2), written as it is.
|
|
1971
|
+
* @param sessionId The `legacy_session_id`, 0 to 32 bytes; the client sends 32 for middlebox compatibility.
|
|
1972
|
+
* @param suites The cipher suites offered, in order of preference.
|
|
1973
|
+
* @param groups The groups `supported_groups` offers, in order of preference.
|
|
1974
|
+
* @param shareGroup The group of the one key share sent, such as `TLS_GROUP_X25519`.
|
|
1975
|
+
* @param sharePublic That share's public key, its `key_exchange`.
|
|
1976
|
+
* @param serverName The host name for `server_name`, ASCII; empty to leave the extension out.
|
|
1977
|
+
* @param alpn The ALPN protocols offered, one character a byte, in order; empty to leave ALPN out.
|
|
1978
|
+
* @param cookie A HelloRetryRequest's `cookie` to echo; empty to send none.
|
|
1979
|
+
* @returns The whole message, handshake header included.
|
|
1980
|
+
*/
|
|
1635
1981
|
export const tlsEncodeClientHello = (
|
|
1636
1982
|
random: u8[],
|
|
1637
1983
|
sessionId: u8[],
|
|
@@ -1693,59 +2039,98 @@ export const tlsEncodeClientHello = (
|
|
|
1693
2039
|
* TLS 1.2 server sends.
|
|
1694
2040
|
*/
|
|
1695
2041
|
export class TlsServerFlightView {
|
|
1696
|
-
/**
|
|
2042
|
+
/**
|
|
2043
|
+
* The buffer the windows point into: the one the last message was read from.
|
|
2044
|
+
*
|
|
2045
|
+
* @internal Read by `net/tls/client.ts`.
|
|
2046
|
+
*/
|
|
1697
2047
|
data: u8[]
|
|
1698
|
-
/**
|
|
2048
|
+
/**
|
|
2049
|
+
* One bit per extension type read, for the duplicate check (§4.2).
|
|
2050
|
+
*
|
|
2051
|
+
* @internal Read by the free functions of this module.
|
|
2052
|
+
*/
|
|
1699
2053
|
extensionSeen: u32[]
|
|
1700
|
-
/**
|
|
2054
|
+
/**
|
|
2055
|
+
* The reader each read runs, kept so that a read allocates none.
|
|
2056
|
+
*
|
|
2057
|
+
* @internal Read by the free functions of this module.
|
|
2058
|
+
*/
|
|
1701
2059
|
reader: TlsReader
|
|
1702
2060
|
/** 0 when the message parsed, else `decode_error`. */
|
|
1703
2061
|
alert: i32 = 0
|
|
1704
2062
|
/** 0 when the extensions are ones this message may carry, else `illegal_parameter` or `unsupported_extension`. */
|
|
1705
2063
|
extensionAlert: i32 = 0
|
|
2064
|
+
/** ServerHello's `legacy_version` as sent; a TLS 1.3 server sends 0x0303 and names 1.3 in `supported_versions` (§4.1.3). */
|
|
1706
2065
|
legacyVersion: i32 = 0
|
|
2066
|
+
/** Where the server's 32-byte `random` starts in `data`; the client checks it for the downgrade sentinel (§4.1.3). */
|
|
1707
2067
|
randomAt: i32 = 0
|
|
2068
|
+
/** `legacy_session_id_echo`, which must equal the session id the client sent (§4.1.3). */
|
|
1708
2069
|
sessionIdAt: i32 = 0
|
|
2070
|
+
/** The echoed session id's length in bytes, 0 to 32. */
|
|
1709
2071
|
sessionIdLength: i32 = 0
|
|
2072
|
+
/** The cipher suite the server chose, a 16-bit value. */
|
|
1710
2073
|
suite: i32 = 0
|
|
2074
|
+
/** `legacy_compression_method`, which must be 0 (§4.1.3). */
|
|
1711
2075
|
compression: i32 = 0
|
|
2076
|
+
/** `supported_versions`' `selected_version`, 0 when the extension was absent; 0x0304 for TLS 1.3. */
|
|
1712
2077
|
selectedVersion: i32 = 0
|
|
1713
2078
|
/** `key_share`: a ServerHello's entry, or a HelloRetryRequest's `selected_group` with an empty share. */
|
|
1714
2079
|
shareGroup: i32 = 0
|
|
2080
|
+
/** Where the ServerHello's key_exchange starts in `data`. */
|
|
1715
2081
|
shareAt: i32 = 0
|
|
2082
|
+
/** The key_exchange's length in bytes as sent, 0 for a HelloRetryRequest; an x25519 share is 32. */
|
|
1716
2083
|
shareLength: i32 = 0
|
|
1717
2084
|
/** A HelloRetryRequest's `cookie`, which the second ClientHello echoes (§4.2.2). */
|
|
1718
2085
|
cookieAt: i32 = 0
|
|
2086
|
+
/** The cookie's length in bytes, 0 when none was sent. */
|
|
1719
2087
|
cookieLength: i32 = 0
|
|
1720
2088
|
/** EncryptedExtensions' ALPN: the one protocol the server chose (RFC 7301 §3.1). */
|
|
1721
2089
|
alpnAt: i32 = 0
|
|
2090
|
+
/** The chosen protocol name's length in bytes, 0 when ALPN was absent. */
|
|
1722
2091
|
alpnLength: i32 = 0
|
|
1723
2092
|
/** Certificate's `certificate_request_context`, which must be empty for a server's (§4.4.2). */
|
|
1724
2093
|
contextLength: i32 = 0
|
|
1725
2094
|
/** Certificate's `certificate_list`, every entry in it, and how many there are. */
|
|
1726
2095
|
certificatesAt: i32 = 0
|
|
2096
|
+
/** The `certificate_list` window's length in bytes. */
|
|
1727
2097
|
certificatesLength: i32 = 0
|
|
2098
|
+
/** How many entries `certificate_list` holds; the first is the server's own certificate. */
|
|
1728
2099
|
certificateCount: i32 = 0
|
|
1729
2100
|
/** CertificateVerify's scheme and signature (§4.4.3). */
|
|
1730
2101
|
scheme: i32 = 0
|
|
2102
|
+
/** Where CertificateVerify's signature starts in `data`. */
|
|
1731
2103
|
signatureAt: i32 = 0
|
|
2104
|
+
/** The signature's length in bytes. */
|
|
1732
2105
|
signatureLength: i32 = 0
|
|
1733
2106
|
/** A ServerHello whose `random` is the HelloRetryRequest value (§4.1.3). */
|
|
1734
2107
|
isRetry: boolean = false
|
|
2108
|
+
/** Whether the message carried `supported_versions`: a TLS 1.3 ServerHello must (§4.2.1). */
|
|
1735
2109
|
hasSupportedVersions: boolean = false
|
|
2110
|
+
/** Whether the message carried `key_share`. */
|
|
1736
2111
|
hasKeyShare: boolean = false
|
|
2112
|
+
/** Whether a HelloRetryRequest carried `cookie`. */
|
|
1737
2113
|
hasCookie: boolean = false
|
|
2114
|
+
/** Whether EncryptedExtensions carried ALPN. */
|
|
1738
2115
|
hasAlpn: boolean = false
|
|
1739
2116
|
/** EncryptedExtensions' empty `server_name`, the acknowledgement of RFC 6066 §3. */
|
|
1740
2117
|
serverNameAcknowledged: boolean = false
|
|
1741
2118
|
|
|
2119
|
+
/**
|
|
2120
|
+
* Makes an empty view, every window empty and every flag false, for the
|
|
2121
|
+
* `tlsRead*` functions to fill.
|
|
2122
|
+
*/
|
|
1742
2123
|
constructor() {
|
|
1743
2124
|
this.data = []
|
|
1744
2125
|
this.extensionSeen = new Array<u32>(TLS_U16_BITMAP_WORDS)
|
|
1745
2126
|
this.reader = new TlsReader(this.data, TLS_CODEC_FROM, TLS_CODEC_FROM)
|
|
1746
2127
|
}
|
|
1747
2128
|
|
|
1748
|
-
/**
|
|
2129
|
+
/**
|
|
2130
|
+
* Forgets the last message: every window empty, every flag down, the bitmap clear.
|
|
2131
|
+
*
|
|
2132
|
+
* @internal Read by the free functions of this module.
|
|
2133
|
+
*/
|
|
1749
2134
|
clear(): void {
|
|
1750
2135
|
this.alert = 0
|
|
1751
2136
|
this.extensionAlert = 0
|
|
@@ -1938,6 +2323,11 @@ const tlsWindowIsHex = (data: u8[], at: i32, text: string): boolean => {
|
|
|
1938
2323
|
* header — into `view`, which tells a HelloRetryRequest from it by its
|
|
1939
2324
|
* `random` (§4.1.3). An absent extensions block parses: it is how a TLS 1.2
|
|
1940
2325
|
* or older server answers, which the client refuses for its version.
|
|
2326
|
+
*
|
|
2327
|
+
* @param view The view to read into, cleared first; its `alert` and `extensionAlert` are the verdicts.
|
|
2328
|
+
* @param data The buffer that holds the message, which the view's windows then point into.
|
|
2329
|
+
* @param off Where the ServerHello body starts in `data`, in bytes, just past the handshake header.
|
|
2330
|
+
* @param len How many bytes the body is, from `off`.
|
|
1941
2331
|
*/
|
|
1942
2332
|
export const tlsReadServerHello = (view: TlsServerFlightView, data: u8[], off: i32, len: i32): void => {
|
|
1943
2333
|
const r: TlsReader | null = tlsFlightOpen(view, data, off, len)
|
|
@@ -1968,13 +2358,25 @@ const TLS_DOWNGRADE_11: string = "444f574e47524400"
|
|
|
1968
2358
|
* Whether the ServerHello in `view` carries a downgrade sentinel in the last
|
|
1969
2359
|
* 8 bytes of its random (§4.1.3), which a TLS 1.3 client must refuse with
|
|
1970
2360
|
* `illegal_parameter`.
|
|
2361
|
+
*
|
|
2362
|
+
* @param view A view `tlsReadServerHello` has just read into.
|
|
2363
|
+
* @returns true when the random ends in the TLS 1.2 or the TLS 1.1 sentinel.
|
|
1971
2364
|
*/
|
|
1972
2365
|
export const tlsServerHelloDowngrade = (view: TlsServerFlightView): boolean => {
|
|
1973
2366
|
const at: i32 = view.randomAt + TLS_RANDOM_SIZE - 8
|
|
1974
2367
|
return tlsWindowIsHex(view.data, at, TLS_DOWNGRADE_12) || tlsWindowIsHex(view.data, at, TLS_DOWNGRADE_11)
|
|
1975
2368
|
}
|
|
1976
2369
|
|
|
1977
|
-
/**
|
|
2370
|
+
/**
|
|
2371
|
+
* Reads an EncryptedExtensions body (§4.3.1) into `view`: ALPN, an empty
|
|
2372
|
+
* `server_name` and `supported_groups` may be there, and any other extension
|
|
2373
|
+
* sets `extensionAlert`.
|
|
2374
|
+
*
|
|
2375
|
+
* @param view The view to read into, cleared first; its `alert` and `extensionAlert` are the verdicts.
|
|
2376
|
+
* @param data The buffer that holds the message, which the view's windows then point into.
|
|
2377
|
+
* @param off Where the EncryptedExtensions body starts in `data`, in bytes, just past the handshake header.
|
|
2378
|
+
* @param len How many bytes the body is, from `off`.
|
|
2379
|
+
*/
|
|
1978
2380
|
export const tlsReadEncryptedExtensions = (
|
|
1979
2381
|
view: TlsServerFlightView,
|
|
1980
2382
|
data: u8[],
|
|
@@ -1995,6 +2397,11 @@ export const tlsReadEncryptedExtensions = (
|
|
|
1995
2397
|
* entries. An empty list is `decode_error`, as §4.4.2.4 has a client answer
|
|
1996
2398
|
* it; an entry with extensions is `unsupported_extension`, because the
|
|
1997
2399
|
* client offers none that a CertificateEntry may carry.
|
|
2400
|
+
*
|
|
2401
|
+
* @param view The view to read into, cleared first; its `alert` and `extensionAlert` are the verdicts.
|
|
2402
|
+
* @param data The buffer that holds the message, which the view's windows then point into.
|
|
2403
|
+
* @param off Where the Certificate body starts in `data`, in bytes, just past the handshake header.
|
|
2404
|
+
* @param len How many bytes the body is, from `off`.
|
|
1998
2405
|
*/
|
|
1999
2406
|
export const tlsReadCertificate = (view: TlsServerFlightView, data: u8[], off: i32, len: i32): void => {
|
|
2000
2407
|
const r: TlsReader | null = tlsFlightOpen(view, data, off, len)
|
|
@@ -2021,7 +2428,15 @@ export const tlsReadCertificate = (view: TlsServerFlightView, data: u8[], off: i
|
|
|
2021
2428
|
tlsFlightClose(view, r, off + len)
|
|
2022
2429
|
}
|
|
2023
2430
|
|
|
2024
|
-
/**
|
|
2431
|
+
/**
|
|
2432
|
+
* Reads a CertificateVerify body (§4.4.3) into `view`: the scheme and the
|
|
2433
|
+
* signature, neither of them checked here.
|
|
2434
|
+
*
|
|
2435
|
+
* @param view The view to read into, cleared first; its `alert` and `extensionAlert` are the verdicts.
|
|
2436
|
+
* @param data The buffer that holds the message, which the view's windows then point into.
|
|
2437
|
+
* @param off Where the CertificateVerify body starts in `data`, in bytes, just past the handshake header.
|
|
2438
|
+
* @param len How many bytes the body is, from `off`.
|
|
2439
|
+
*/
|
|
2025
2440
|
export const tlsReadCertificateVerify = (view: TlsServerFlightView, data: u8[], off: i32, len: i32): void => {
|
|
2026
2441
|
const r: TlsReader | null = tlsFlightOpen(view, data, off, len)
|
|
2027
2442
|
if (r === null) {
|
|
@@ -2039,6 +2454,11 @@ export const tlsReadCertificateVerify = (view: TlsServerFlightView, data: u8[],
|
|
|
2039
2454
|
* Checks a NewSessionTicket body's structure (§4.6.1) into `view`'s `alert`:
|
|
2040
2455
|
* the client keeps no tickets, so it reads one only to discard it, but a
|
|
2041
2456
|
* malformed one is still `decode_error`.
|
|
2457
|
+
*
|
|
2458
|
+
* @param view The view to read into, cleared first; its `alert` and `extensionAlert` are the verdicts.
|
|
2459
|
+
* @param data The buffer that holds the message, which the view's windows then point into.
|
|
2460
|
+
* @param off Where the NewSessionTicket body starts in `data`, in bytes, just past the handshake header.
|
|
2461
|
+
* @param len How many bytes the body is, from `off`.
|
|
2042
2462
|
*/
|
|
2043
2463
|
export const tlsReadNewSessionTicket = (view: TlsServerFlightView, data: u8[], off: i32, len: i32): void => {
|
|
2044
2464
|
const r: TlsReader | null = tlsFlightOpen(view, data, off, len)
|
|
@@ -2052,7 +2472,13 @@ export const tlsReadNewSessionTicket = (view: TlsServerFlightView, data: u8[], o
|
|
|
2052
2472
|
tlsFlightClose(view, r, off + len)
|
|
2053
2473
|
}
|
|
2054
2474
|
|
|
2055
|
-
/**
|
|
2475
|
+
/**
|
|
2476
|
+
* The DER certificates of the Certificate `tlsReadCertificate` last read into
|
|
2477
|
+
* `view`, leaf first, each a fresh array.
|
|
2478
|
+
*
|
|
2479
|
+
* @param view A view `tlsReadCertificate` has just read into.
|
|
2480
|
+
* @returns The chain, copied out of `view.data`; empty if the read failed or another message was read since.
|
|
2481
|
+
*/
|
|
2056
2482
|
export const tlsCopyCertificates = (view: TlsServerFlightView): u8[][] => {
|
|
2057
2483
|
const chain: u8[][] = []
|
|
2058
2484
|
const r: TlsReader = new TlsReader(
|