@amritk/nish-aarch64-linux 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/INSTALL.md +73 -59
- package/bin/nish +0 -0
- package/docs/AI.md +50 -4
- package/package.json +1 -1
- 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/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/client.ts
CHANGED
|
@@ -141,7 +141,12 @@ export const TLS_CLIENT_STATE_START: i32 = 0
|
|
|
141
141
|
export const TLS_CLIENT_STATE_WAIT_SERVER_HELLO: i32 = 1
|
|
142
142
|
/** The handshake secrets are known; EncryptedExtensions comes next, at the Handshake level. */
|
|
143
143
|
export const TLS_CLIENT_STATE_WAIT_ENCRYPTED_EXTENSIONS: i32 = 2
|
|
144
|
+
/**
|
|
145
|
+
* EncryptedExtensions is read; the server's Certificate comes next, at the Handshake level. A
|
|
146
|
+
* CertificateRequest here is `unexpected_message`: this client does not authenticate itself.
|
|
147
|
+
*/
|
|
144
148
|
export const TLS_CLIENT_STATE_WAIT_CERTIFICATE: i32 = 3
|
|
149
|
+
/** The server's chain is read; its CertificateVerify comes next, at the Handshake level. */
|
|
145
150
|
export const TLS_CLIENT_STATE_WAIT_CERTIFICATE_VERIFY: i32 = 4
|
|
146
151
|
/**
|
|
147
152
|
* The server's chain and signature are read and wait for the caller's
|
|
@@ -153,6 +158,7 @@ export const TLS_CLIENT_STATE_WAIT_AUTH: i32 = 5
|
|
|
153
158
|
export const TLS_CLIENT_STATE_WAIT_FINISHED: i32 = 6
|
|
154
159
|
/** Both Finished are done and the application secrets known. */
|
|
155
160
|
export const TLS_CLIENT_STATE_CONNECTED: i32 = 7
|
|
161
|
+
/** The handshake failed or was wiped: every secret is zeroed, and `alert` says why. No state follows it. */
|
|
156
162
|
export const TLS_CLIENT_STATE_FAILED: i32 = 8
|
|
157
163
|
|
|
158
164
|
/** A typed zero for the offsets below: a bare literal is an `f64` under `--number-mode f64`. */
|
|
@@ -181,6 +187,15 @@ export class TlsClientConfig {
|
|
|
181
187
|
/** The group of the one key share the first ClientHello carries; one of `groups`. */
|
|
182
188
|
shareGroup: i32
|
|
183
189
|
|
|
190
|
+
/**
|
|
191
|
+
* A configuration for `serverName` offering `alpn`, with every suite this tree negotiates and an x25519
|
|
192
|
+
* share. Nothing is checked here: `TlsClient`'s constructor checks the whole configuration.
|
|
193
|
+
*
|
|
194
|
+
* @param serverName The host to reach: at most 253 ASCII bytes, sent as `server_name` unless it is an
|
|
195
|
+
* IP literal, and the name the caller authenticates the certificate against.
|
|
196
|
+
* @param alpn The ALPN protocols to offer, most preferred first, each 1 to 255 ASCII bytes; empty sends
|
|
197
|
+
* no ALPN. The array is kept, not copied.
|
|
198
|
+
*/
|
|
184
199
|
constructor(serverName: string, alpn: string[]) {
|
|
185
200
|
this.serverName = serverName
|
|
186
201
|
this.alpn = alpn
|
|
@@ -201,6 +216,10 @@ const tlsClientKnowsGroup = (group: i32): boolean =>
|
|
|
201
216
|
* Whether `name` is an IP literal, which RFC 6066 §3 keeps out of
|
|
202
217
|
* `server_name`: dotted decimal (digits and dots only), or anything with a
|
|
203
218
|
* colon, which no DNS name has.
|
|
219
|
+
*
|
|
220
|
+
* @param name A host name as the caller gave it, such as `TlsClientConfig.serverName`.
|
|
221
|
+
* @returns True when `name` is non-empty and all digits and dots, or holds a colon anywhere; such a
|
|
222
|
+
* name is not sent as `server_name`.
|
|
204
223
|
*/
|
|
205
224
|
export const tlsClientIsIpLiteral = (name: string): boolean => {
|
|
206
225
|
let digits: boolean = true
|
|
@@ -264,57 +283,78 @@ const tlsClientConfigFits = (config: TlsClientConfig): boolean => {
|
|
|
264
283
|
return toI32(config.serverName.length) <= TLS_CLIENT_SERVER_NAME_MAX && tlsClientIsAscii(config.serverName)
|
|
265
284
|
}
|
|
266
285
|
|
|
286
|
+
/**
|
|
287
|
+
* The client side of one TLS 1.3 handshake, sans-IO: it takes the server's handshake messages through
|
|
288
|
+
* `receive`, writes its own for `takeOutput`, and derives each secret into `secrets()` as it becomes
|
|
289
|
+
* known. It never judges the server's certificate itself: at `TLS_CLIENT_STATE_WAIT_AUTH` it waits for
|
|
290
|
+
* the caller's `authenticate` or `reject`. One instance serves one connection; once it fails or is
|
|
291
|
+
* wiped it stays failed.
|
|
292
|
+
*/
|
|
267
293
|
export class TlsClient {
|
|
294
|
+
/** @internal Read by `net/tls/verify.ts`. */
|
|
268
295
|
config: TlsClientConfig
|
|
269
|
-
clientRandom: u8[]
|
|
296
|
+
private clientRandom: u8[]
|
|
270
297
|
/** The x25519 private key, copied in by the constructor; wiped once the server picks a group, unless it picked x25519, then once the exchange is done. */
|
|
271
|
-
readonly x25519Key: Secret<u8[]>
|
|
298
|
+
private readonly x25519Key: Secret<u8[]>
|
|
272
299
|
/** The P-256 private key, held the same way. */
|
|
273
|
-
readonly p256Key: Secret<u8[]>
|
|
300
|
+
private readonly p256Key: Secret<u8[]>
|
|
274
301
|
/** The legacy_session_id sent, which the ServerHello must echo. */
|
|
275
302
|
sessionId: u8[]
|
|
276
|
-
transcript: TlsTranscript
|
|
303
|
+
private transcript: TlsTranscript
|
|
277
304
|
/**
|
|
278
305
|
* The first ClientHello, kept plain until the ServerHello names the hash:
|
|
279
306
|
* the transcript feeds one hash only, so it starts from this copy.
|
|
280
307
|
*/
|
|
281
|
-
firstHello: u8[]
|
|
308
|
+
private firstHello: u8[]
|
|
282
309
|
/** The bytes received at the current level and not yet read: `input[0 .. inputLength)`, whole messages and the start of the next. */
|
|
283
|
-
input: u8[]
|
|
284
|
-
/**
|
|
310
|
+
private input: u8[]
|
|
311
|
+
/**
|
|
312
|
+
* What the client has written at the Initial level and not handed on.
|
|
313
|
+
*
|
|
314
|
+
* @internal Read by `net/tls/record-client.ts`.
|
|
315
|
+
*/
|
|
285
316
|
outputInitial: u8[]
|
|
286
|
-
/**
|
|
317
|
+
/**
|
|
318
|
+
* What the client has written at the Handshake level and not handed on: its Finished.
|
|
319
|
+
*
|
|
320
|
+
* @internal Read by `net/tls/record-client.ts`.
|
|
321
|
+
*/
|
|
287
322
|
outputHandshake: u8[]
|
|
288
|
-
secrets256: TlsSecretPool
|
|
289
|
-
secrets384: TlsSecretPool
|
|
323
|
+
private secrets256: TlsSecretPool
|
|
324
|
+
private secrets384: TlsSecretPool
|
|
290
325
|
/** The server message last read, as windows into `input`. */
|
|
291
|
-
view: TlsServerFlightView
|
|
326
|
+
private view: TlsServerFlightView
|
|
292
327
|
/** The HelloRetryRequest's cookie, echoed in the second ClientHello; empty if it sent none. */
|
|
293
|
-
cookie: u8[]
|
|
328
|
+
private cookie: u8[]
|
|
294
329
|
/** The server's DER certificates, leaf first, once its Certificate is read. */
|
|
295
|
-
certificates: u8[][]
|
|
330
|
+
private certificates: u8[][]
|
|
296
331
|
/** What the server's CertificateVerify signs (§4.4.3), once its Certificate is read. */
|
|
297
|
-
content: u8[]
|
|
332
|
+
private content: u8[]
|
|
298
333
|
/** The CertificateVerify signature, as it came on the wire. */
|
|
299
|
-
signature: u8[]
|
|
334
|
+
private signature: u8[]
|
|
300
335
|
/** A transcript hash on its way into the schedule: HashLen bytes of it. */
|
|
301
|
-
hashes: u8[]
|
|
336
|
+
private hashes: u8[]
|
|
302
337
|
/** The ALPN protocol the server chose, one of the configuration's, or empty. */
|
|
303
338
|
alpnChosen: string = ""
|
|
304
339
|
/** Which of the negotiated pool's secrets are known: bit `1 << TLS_SECRET_*` for each, as `TlsServer.held`. */
|
|
305
|
-
held: i32 = 0
|
|
340
|
+
private held: i32 = 0
|
|
341
|
+
/** Where the handshake is: one of the `TLS_CLIENT_STATE_*` values, `TLS_CLIENT_STATE_CONNECTED` once it has finished and `TLS_CLIENT_STATE_FAILED` once `alert` is set. */
|
|
306
342
|
state: i32 = 0
|
|
307
343
|
/** The alert the client failed with, or 0. */
|
|
308
344
|
alert: i32 = 0
|
|
309
345
|
/** The negotiated cipher suite, or 0 before the ServerHello (or HelloRetryRequest) is read. */
|
|
310
346
|
suite: i32 = 0
|
|
347
|
+
/** @internal Read by `net/tls/record-client.ts`. */
|
|
311
348
|
hashLength: i32 = 0
|
|
312
349
|
/** The group of the key share the last ClientHello carried. */
|
|
313
350
|
shareGroup: i32 = 0
|
|
314
351
|
/** The CertificateVerify scheme. */
|
|
315
352
|
scheme: i32 = 0
|
|
353
|
+
/** @internal Read by `net/tls/record-client.ts`. */
|
|
316
354
|
inputLength: i32 = 0
|
|
355
|
+
/** @internal Read by `net/tls/record-client.ts`. */
|
|
317
356
|
outputInitialLength: i32 = 0
|
|
357
|
+
/** @internal Read by `net/tls/record-client.ts`. */
|
|
318
358
|
outputHandshakeLength: i32 = 0
|
|
319
359
|
/** Whether a HelloRetryRequest was read, so a second is fatal. */
|
|
320
360
|
retried: boolean = false
|
|
@@ -327,6 +367,14 @@ export class TlsClient {
|
|
|
327
367
|
* keys are copied in and the caller's arrays zeroed. A random or key of
|
|
328
368
|
* another length, or a configuration it cannot offer, leaves the client
|
|
329
369
|
* failed with `internal_error` before it writes anything.
|
|
370
|
+
*
|
|
371
|
+
* @param config What to offer; kept, not copied, so it must not change while the handshake runs.
|
|
372
|
+
* @param clientRandom 32 fresh random bytes for the ClientHello's random; copied, so the array is the
|
|
373
|
+
* caller's again.
|
|
374
|
+
* @param x25519Private 32 fresh random bytes for the x25519 key share; copied into the client's own
|
|
375
|
+
* `Secret`, and this array zeroed whatever its length.
|
|
376
|
+
* @param p256Private 32 fresh random bytes for a P-256 key share, used only if the share or a
|
|
377
|
+
* HelloRetryRequest names secp256r1; copied and zeroed the same way.
|
|
330
378
|
*/
|
|
331
379
|
constructor(config: TlsClientConfig, clientRandom: u8[], x25519Private: u8[], p256Private: u8[]) {
|
|
332
380
|
const x25519Length: i32 = toI32(x25519Private.length)
|
|
@@ -369,7 +417,13 @@ export class TlsClient {
|
|
|
369
417
|
}
|
|
370
418
|
}
|
|
371
419
|
|
|
372
|
-
/**
|
|
420
|
+
/**
|
|
421
|
+
* Writes the first ClientHello at the Initial level. Answers 0, or the alert the client failed with;
|
|
422
|
+
* a second call is `internal_error`.
|
|
423
|
+
*
|
|
424
|
+
* @returns 0 once the ClientHello waits in `takeOutput(TLS_LEVEL_INITIAL)`, or the TLS alert code
|
|
425
|
+
* (1 to 255) the client failed with.
|
|
426
|
+
*/
|
|
373
427
|
start(): i32 {
|
|
374
428
|
if (this.state === TLS_CLIENT_STATE_FAILED) {
|
|
375
429
|
return this.alert
|
|
@@ -394,7 +448,7 @@ export class TlsClient {
|
|
|
394
448
|
* pass the 65,535 bytes their length can say (a HelloRetryRequest's cookie
|
|
395
449
|
* near that size).
|
|
396
450
|
*/
|
|
397
|
-
writeHello(): i32 {
|
|
451
|
+
private writeHello(): i32 {
|
|
398
452
|
const share: u8[] | null =
|
|
399
453
|
this.shareGroup === TLS_GROUP_X25519 ? x25519Base(this.x25519Key) : p256PublicKey(this.p256Key)
|
|
400
454
|
if (share === null) {
|
|
@@ -441,24 +495,33 @@ export class TlsClient {
|
|
|
441
495
|
}
|
|
442
496
|
|
|
443
497
|
/** Fails the handshake with `alert` and answers it, wiping every secret the client holds. */
|
|
444
|
-
fail(alert: i32): i32 {
|
|
498
|
+
private fail(alert: i32): i32 {
|
|
445
499
|
this.wipe()
|
|
446
500
|
this.state = TLS_CLIENT_STATE_FAILED
|
|
447
501
|
this.alert = alert
|
|
448
502
|
return alert
|
|
449
503
|
}
|
|
450
504
|
|
|
451
|
-
/**
|
|
505
|
+
/**
|
|
506
|
+
* The pool of the negotiated hash: SHA-384's for a SHA-384 suite, SHA-256's otherwise.
|
|
507
|
+
*
|
|
508
|
+
* @returns The client's own pool, one named field per secret; only the secrets the handshake has
|
|
509
|
+
* reached hold a value. It is the client's own, not a copy, and `wipe` zeroes it.
|
|
510
|
+
*/
|
|
452
511
|
secrets(): TlsSecretPool {
|
|
453
512
|
return this.hashLength === SHA384_SIZE ? this.secrets384 : this.secrets256
|
|
454
513
|
}
|
|
455
514
|
|
|
456
515
|
/** Whether the handshake has derived secret `index` (`TLS_SECRET_*`). */
|
|
457
|
-
known(index: i32): boolean {
|
|
516
|
+
private known(index: i32): boolean {
|
|
458
517
|
return index >= 0 && index < 31 && (this.held & (1 << index)) !== 0
|
|
459
518
|
}
|
|
460
519
|
|
|
461
|
-
/**
|
|
520
|
+
/**
|
|
521
|
+
* Whether the secret the client *reads* `level` under — the server's, `serverHandshake` or `serverApplication` — is known yet.
|
|
522
|
+
*
|
|
523
|
+
* @internal Read by `net/tls/record-client.ts`.
|
|
524
|
+
*/
|
|
462
525
|
readKnown(level: i32): boolean {
|
|
463
526
|
if (level === TLS_LEVEL_HANDSHAKE) {
|
|
464
527
|
return this.known(TLS_SECRET_SERVER_HANDSHAKE)
|
|
@@ -466,7 +529,11 @@ export class TlsClient {
|
|
|
466
529
|
return level === TLS_LEVEL_APPLICATION && this.known(TLS_SECRET_SERVER_APPLICATION)
|
|
467
530
|
}
|
|
468
531
|
|
|
469
|
-
/**
|
|
532
|
+
/**
|
|
533
|
+
* Whether the secret the client *writes* `level` under — `clientHandshake` or `clientApplication` — is known yet.
|
|
534
|
+
*
|
|
535
|
+
* @internal Read by `net/tls/record-client.ts`.
|
|
536
|
+
*/
|
|
470
537
|
writeKnown(level: i32): boolean {
|
|
471
538
|
if (level === TLS_LEVEL_HANDSHAKE) {
|
|
472
539
|
return this.known(TLS_SECRET_CLIENT_HANDSHAKE)
|
|
@@ -474,7 +541,11 @@ export class TlsClient {
|
|
|
474
541
|
return level === TLS_LEVEL_APPLICATION && this.known(TLS_SECRET_CLIENT_APPLICATION)
|
|
475
542
|
}
|
|
476
543
|
|
|
477
|
-
/**
|
|
544
|
+
/**
|
|
545
|
+
* Zeroes secret `index` of `secrets()` once a carrier has taken what it needs from it; it stays known.
|
|
546
|
+
*
|
|
547
|
+
* @internal Read by `net/tls/record-client.ts`.
|
|
548
|
+
*/
|
|
478
549
|
wipeSecret(index: i32): void {
|
|
479
550
|
this.secrets().zero(index)
|
|
480
551
|
}
|
|
@@ -497,7 +568,7 @@ export class TlsClient {
|
|
|
497
568
|
}
|
|
498
569
|
|
|
499
570
|
/** The level the server's next bytes must arrive at, or -1 when the state expects none. */
|
|
500
|
-
expectedLevel(): i32 {
|
|
571
|
+
private expectedLevel(): i32 {
|
|
501
572
|
switch (this.state) {
|
|
502
573
|
case TLS_CLIENT_STATE_WAIT_SERVER_HELLO:
|
|
503
574
|
return TLS_LEVEL_INITIAL
|
|
@@ -515,7 +586,7 @@ export class TlsClient {
|
|
|
515
586
|
}
|
|
516
587
|
|
|
517
588
|
/** Makes `input` hold at least `need` bytes, keeping the ones it holds; it doubles, and never grows past one largest message. */
|
|
518
|
-
reserveInput(need: i32): void {
|
|
589
|
+
private reserveInput(need: i32): void {
|
|
519
590
|
const capacity: i32 = toI32(this.input.length)
|
|
520
591
|
if (need <= capacity) {
|
|
521
592
|
return
|
|
@@ -533,7 +604,7 @@ export class TlsClient {
|
|
|
533
604
|
}
|
|
534
605
|
|
|
535
606
|
/** Makes the Initial output hold at least `need` bytes, keeping the ones it holds. */
|
|
536
|
-
reserveOutput(need: i32): void {
|
|
607
|
+
private reserveOutput(need: i32): void {
|
|
537
608
|
const capacity: i32 = toI32(this.outputInitial.length)
|
|
538
609
|
if (need <= capacity) {
|
|
539
610
|
return
|
|
@@ -558,6 +629,13 @@ export class TlsClient {
|
|
|
558
629
|
* until `authenticate`. Once connected, the Application level takes
|
|
559
630
|
* NewSessionTickets, which are checked and dropped; anything else there is
|
|
560
631
|
* `unexpected_message`, KeyUpdate being the record layer's.
|
|
632
|
+
*
|
|
633
|
+
* @param level The level the bytes arrived at: `TLS_LEVEL_INITIAL`, `TLS_LEVEL_HANDSHAKE` or
|
|
634
|
+
* `TLS_LEVEL_APPLICATION`, from `nish/net/tls`.
|
|
635
|
+
* @param data The buffer holding the server's handshake bytes; they are copied, not kept.
|
|
636
|
+
* @param off Where in `data` the bytes start.
|
|
637
|
+
* @param len How many bytes, from `off`, to take; 0 takes nothing.
|
|
638
|
+
* @returns 0, or the TLS alert code (1 to 255) the client failed with.
|
|
561
639
|
*/
|
|
562
640
|
receive(level: i32, data: u8[], off: i32, len: i32): i32 {
|
|
563
641
|
if (this.state === TLS_CLIENT_STATE_FAILED) {
|
|
@@ -608,7 +686,7 @@ export class TlsClient {
|
|
|
608
686
|
* read, until a partial one is left or the state waits on the caller.
|
|
609
687
|
* Answers 0 or the alert.
|
|
610
688
|
*/
|
|
611
|
-
drain(): i32 {
|
|
689
|
+
private drain(): i32 {
|
|
612
690
|
while (this.state !== TLS_CLIENT_STATE_WAIT_AUTH && this.inputLength >= 4) {
|
|
613
691
|
const length: i32 = (toI32(this.input[1]) << 16) | (toI32(this.input[2]) << 8) | toI32(this.input[3])
|
|
614
692
|
if (length > TLS_MAX_HANDSHAKE_MESSAGE) {
|
|
@@ -633,7 +711,7 @@ export class TlsClient {
|
|
|
633
711
|
}
|
|
634
712
|
|
|
635
713
|
/** Drops the first `count` bytes of `input`, moving the rest to its front. */
|
|
636
|
-
consume(count: i32): void {
|
|
714
|
+
private consume(count: i32): void {
|
|
637
715
|
const rest: i32 = this.inputLength - count
|
|
638
716
|
for (let k: i32 = 0; k < rest && count + k < toI32(this.input.length); k++) {
|
|
639
717
|
this.input[k] = this.input[count + k]
|
|
@@ -642,7 +720,7 @@ export class TlsClient {
|
|
|
642
720
|
}
|
|
643
721
|
|
|
644
722
|
/** Dispatches one whole message, `input[0 .. 4 + length)`, of type `type`, by state. */
|
|
645
|
-
handleMessage(type: i32, length: i32): i32 {
|
|
723
|
+
private handleMessage(type: i32, length: i32): i32 {
|
|
646
724
|
switch (this.state) {
|
|
647
725
|
case TLS_CLIENT_STATE_WAIT_SERVER_HELLO:
|
|
648
726
|
return type === TLS_HANDSHAKE_SERVER_HELLO
|
|
@@ -678,7 +756,7 @@ export class TlsClient {
|
|
|
678
756
|
* extensions, the echoed session id, null compression and an offered
|
|
679
757
|
* suite. Answers 0 or the alert.
|
|
680
758
|
*/
|
|
681
|
-
checkHello(view: TlsServerFlightView): i32 {
|
|
759
|
+
private checkHello(view: TlsServerFlightView): i32 {
|
|
682
760
|
if (view.alert !== 0) {
|
|
683
761
|
return view.alert
|
|
684
762
|
}
|
|
@@ -717,7 +795,7 @@ export class TlsClient {
|
|
|
717
795
|
}
|
|
718
796
|
|
|
719
797
|
/** A ServerHello or HelloRetryRequest, `input[0 .. 4 + length)`. */
|
|
720
|
-
handleServerHello(length: i32): i32 {
|
|
798
|
+
private handleServerHello(length: i32): i32 {
|
|
721
799
|
const view: TlsServerFlightView = this.view
|
|
722
800
|
tlsReadServerHello(view, this.input, 4, length)
|
|
723
801
|
if (view.isRetry && this.retried) {
|
|
@@ -764,7 +842,7 @@ export class TlsClient {
|
|
|
764
842
|
}
|
|
765
843
|
|
|
766
844
|
/** Starts the transcript over `suite`'s hash, from the first ClientHello. */
|
|
767
|
-
startTranscript(suite: i32): void {
|
|
845
|
+
private startTranscript(suite: i32): void {
|
|
768
846
|
this.suite = suite
|
|
769
847
|
this.hashLength = tlsSuiteHashLength(suite)
|
|
770
848
|
this.transcript.reset(this.hashLength)
|
|
@@ -779,7 +857,7 @@ export class TlsClient {
|
|
|
779
857
|
* the new group and echoes the cookie. The key of the group not picked is
|
|
780
858
|
* wiped now.
|
|
781
859
|
*/
|
|
782
|
-
handleRetry(length: i32): i32 {
|
|
860
|
+
private handleRetry(length: i32): i32 {
|
|
783
861
|
const view: TlsServerFlightView = this.view
|
|
784
862
|
if (!view.hasKeyShare && !view.hasCookie) {
|
|
785
863
|
return TLS_ALERT_ILLEGAL_PARAMETER
|
|
@@ -810,13 +888,13 @@ export class TlsClient {
|
|
|
810
888
|
}
|
|
811
889
|
|
|
812
890
|
/** Zeroes both private keys, once the exchange is done or cannot be. */
|
|
813
|
-
wipeKeys(): void {
|
|
891
|
+
private wipeKeys(): void {
|
|
814
892
|
exposeWith(this.x25519Key, TLS_CLIENT_FROM, tlsZeroHeld)
|
|
815
893
|
exposeWith(this.p256Key, TLS_CLIENT_FROM, tlsZeroHeld)
|
|
816
894
|
}
|
|
817
895
|
|
|
818
896
|
/** Zeroes the private key of the group the client's share is not of. */
|
|
819
|
-
wipeUnpicked(): void {
|
|
897
|
+
private wipeUnpicked(): void {
|
|
820
898
|
if (this.shareGroup === TLS_GROUP_X25519) {
|
|
821
899
|
exposeWith(this.p256Key, TLS_CLIENT_FROM, tlsZeroHeld)
|
|
822
900
|
} else {
|
|
@@ -832,7 +910,7 @@ export class TlsClient {
|
|
|
832
910
|
* a secp256r1 share that is not an uncompressed point on the curve
|
|
833
911
|
* (§4.2.8.2); its shared secret is the product's x, which may be zero.
|
|
834
912
|
*/
|
|
835
|
-
exchangeKeys(): i32 {
|
|
913
|
+
private exchangeKeys(): i32 {
|
|
836
914
|
const view: TlsServerFlightView = this.view
|
|
837
915
|
const peer: u8[] = tlsCopyWindow(view.data, view.shareAt, view.shareLength)
|
|
838
916
|
const isX25519: boolean = this.shareGroup === TLS_GROUP_X25519
|
|
@@ -854,7 +932,7 @@ export class TlsClient {
|
|
|
854
932
|
}
|
|
855
933
|
|
|
856
934
|
/** EncryptedExtensions (§4.3.1): ALPN, if any, must be one the client offered, and `server_name` acknowledged only if sent. */
|
|
857
|
-
handleEncryptedExtensions(length: i32): i32 {
|
|
935
|
+
private handleEncryptedExtensions(length: i32): i32 {
|
|
858
936
|
const view: TlsServerFlightView = this.view
|
|
859
937
|
tlsReadEncryptedExtensions(view, this.input, 4, length)
|
|
860
938
|
if (view.alert !== 0) {
|
|
@@ -885,7 +963,7 @@ export class TlsClient {
|
|
|
885
963
|
}
|
|
886
964
|
|
|
887
965
|
/** The server's Certificate (§4.4.2): its request context must be empty. The chain is copied out, and what CertificateVerify signs computed. */
|
|
888
|
-
handleCertificate(length: i32): i32 {
|
|
966
|
+
private handleCertificate(length: i32): i32 {
|
|
889
967
|
const view: TlsServerFlightView = this.view
|
|
890
968
|
tlsReadCertificate(view, this.input, 4, length)
|
|
891
969
|
if (view.alert !== 0) {
|
|
@@ -905,7 +983,7 @@ export class TlsClient {
|
|
|
905
983
|
}
|
|
906
984
|
|
|
907
985
|
/** CertificateVerify (§4.4.3): a scheme the client offered. The signature is kept, unchecked, for the caller; the client then waits on it. */
|
|
908
|
-
handleCertificateVerify(length: i32): i32 {
|
|
986
|
+
private handleCertificateVerify(length: i32): i32 {
|
|
909
987
|
const view: TlsServerFlightView = this.view
|
|
910
988
|
tlsReadCertificateVerify(view, this.input, 4, length)
|
|
911
989
|
if (view.alert !== 0) {
|
|
@@ -927,7 +1005,7 @@ export class TlsClient {
|
|
|
927
1005
|
* master secret, the application and exporter secrets, and the client's
|
|
928
1006
|
* Finished at the Handshake level; the handshake secret is wiped.
|
|
929
1007
|
*/
|
|
930
|
-
handleFinished(length: i32): i32 {
|
|
1008
|
+
private handleFinished(length: i32): i32 {
|
|
931
1009
|
const h: i32 = this.hashLength
|
|
932
1010
|
if (length !== h) {
|
|
933
1011
|
return TLS_ALERT_DECODE_ERROR
|
|
@@ -979,32 +1057,53 @@ export class TlsClient {
|
|
|
979
1057
|
}
|
|
980
1058
|
|
|
981
1059
|
/** A NewSessionTicket (§4.6.1) once connected: checked for its structure and dropped, since this client does not resume. */
|
|
982
|
-
handleTicket(length: i32): i32 {
|
|
1060
|
+
private handleTicket(length: i32): i32 {
|
|
983
1061
|
tlsReadNewSessionTicket(this.view, this.input, 4, length)
|
|
984
1062
|
return this.view.alert
|
|
985
1063
|
}
|
|
986
1064
|
|
|
987
|
-
/**
|
|
1065
|
+
/**
|
|
1066
|
+
* The server's DER certificates, leaf first; empty before its Certificate is read.
|
|
1067
|
+
*
|
|
1068
|
+
* @returns The client's own list, not a copy, for the caller to check before `authenticate`.
|
|
1069
|
+
*/
|
|
988
1070
|
peerCertificates(): u8[][] {
|
|
989
1071
|
return this.certificates
|
|
990
1072
|
}
|
|
991
1073
|
|
|
992
|
-
/**
|
|
1074
|
+
/**
|
|
1075
|
+
* The CertificateVerify scheme (`TLS_SIGNATURE_*`); 0 before it is read.
|
|
1076
|
+
*
|
|
1077
|
+
* @returns The signature scheme code point the server signed with, or 0.
|
|
1078
|
+
*/
|
|
993
1079
|
verifyScheme(): i32 {
|
|
994
1080
|
return this.scheme
|
|
995
1081
|
}
|
|
996
1082
|
|
|
997
|
-
/**
|
|
1083
|
+
/**
|
|
1084
|
+
* What the CertificateVerify signature is over (§4.4.3): 64 spaces, the server's context string, a
|
|
1085
|
+
* zero byte and the transcript hash.
|
|
1086
|
+
*
|
|
1087
|
+
* @returns The client's own bytes, not a copy; empty before the server's Certificate is read.
|
|
1088
|
+
*/
|
|
998
1089
|
verifyContent(): u8[] {
|
|
999
1090
|
return this.content
|
|
1000
1091
|
}
|
|
1001
1092
|
|
|
1002
|
-
/**
|
|
1093
|
+
/**
|
|
1094
|
+
* The CertificateVerify signature as sent: DER for ECDSA, raw for RSA-PSS.
|
|
1095
|
+
*
|
|
1096
|
+
* @returns The client's own bytes, not a copy; empty before the CertificateVerify is read.
|
|
1097
|
+
*/
|
|
1003
1098
|
verifySignature(): u8[] {
|
|
1004
1099
|
return this.signature
|
|
1005
1100
|
}
|
|
1006
1101
|
|
|
1007
|
-
/**
|
|
1102
|
+
/**
|
|
1103
|
+
* The ALPN protocol the server chose, or empty when it chose none.
|
|
1104
|
+
*
|
|
1105
|
+
* @returns One of the configuration's `alpn` protocols, or the empty string.
|
|
1106
|
+
*/
|
|
1008
1107
|
alpn(): string {
|
|
1009
1108
|
return this.alpnChosen
|
|
1010
1109
|
}
|
|
@@ -1015,6 +1114,10 @@ export class TlsClient {
|
|
|
1015
1114
|
* reading what was held meanwhile; `false` is `reject(bad_certificate)`.
|
|
1016
1115
|
* Answers 0 or the alert; in any other state the client fails with
|
|
1017
1116
|
* `internal_error`.
|
|
1117
|
+
*
|
|
1118
|
+
* @param ok True when the caller has checked the chain against its trust and the signature over
|
|
1119
|
+
* `verifyContent()`, and accepts the server.
|
|
1120
|
+
* @returns 0, or the TLS alert code the client failed with.
|
|
1018
1121
|
*/
|
|
1019
1122
|
authenticate(ok: boolean): i32 {
|
|
1020
1123
|
if (!ok) {
|
|
@@ -1040,6 +1143,10 @@ export class TlsClient {
|
|
|
1040
1143
|
* `unknown_ca`, `decrypt_error`…): the client fails with it and answers it.
|
|
1041
1144
|
* An alert outside 1 to 255 is taken as `bad_certificate`. In any other
|
|
1042
1145
|
* state the client fails with `internal_error`.
|
|
1146
|
+
*
|
|
1147
|
+
* @param alert The TLS alert code to fail with, 1 to 255 (a `TLS_ALERT_*`).
|
|
1148
|
+
* @returns The alert the client failed with: `alert`, `bad_certificate`, `internal_error`, or the
|
|
1149
|
+
* earlier alert of a client that had already failed.
|
|
1043
1150
|
*/
|
|
1044
1151
|
reject(alert: i32): i32 {
|
|
1045
1152
|
if (this.state === TLS_CLIENT_STATE_FAILED) {
|
|
@@ -1052,7 +1159,7 @@ export class TlsClient {
|
|
|
1052
1159
|
}
|
|
1053
1160
|
|
|
1054
1161
|
/** A message the client wrote at `level`, `output[at .. end)`, goes into the transcript once its hash is known and stays on the output. */
|
|
1055
|
-
sent(level: i32, at: i32, end: i32): void {
|
|
1162
|
+
private sent(level: i32, at: i32, end: i32): void {
|
|
1056
1163
|
if (level === TLS_LEVEL_INITIAL) {
|
|
1057
1164
|
if (this.hashLength !== 0) {
|
|
1058
1165
|
this.transcript.update(this.outputInitial, at, end - at)
|
|
@@ -1064,7 +1171,12 @@ export class TlsClient {
|
|
|
1064
1171
|
}
|
|
1065
1172
|
}
|
|
1066
1173
|
|
|
1067
|
-
/**
|
|
1174
|
+
/**
|
|
1175
|
+
* The handshake bytes written at `level` since the last call, in a fresh array, and forgets them.
|
|
1176
|
+
*
|
|
1177
|
+
* @param level `TLS_LEVEL_INITIAL` for ClientHellos or `TLS_LEVEL_HANDSHAKE` for the Finished.
|
|
1178
|
+
* @returns A copy the caller owns, empty when nothing waits or for any other level.
|
|
1179
|
+
*/
|
|
1068
1180
|
takeOutput(level: i32): u8[] {
|
|
1069
1181
|
const none: u8[] = []
|
|
1070
1182
|
switch (level) {
|
|
@@ -1083,7 +1195,11 @@ export class TlsClient {
|
|
|
1083
1195
|
}
|
|
1084
1196
|
}
|
|
1085
1197
|
|
|
1086
|
-
/**
|
|
1198
|
+
/**
|
|
1199
|
+
* Forgets the handshake bytes written at `level`, which a carrier has read out of the buffer itself.
|
|
1200
|
+
*
|
|
1201
|
+
* @internal Read by `net/tls/record-client.ts`.
|
|
1202
|
+
*/
|
|
1087
1203
|
clearOutput(level: i32): void {
|
|
1088
1204
|
if (level === TLS_LEVEL_INITIAL) {
|
|
1089
1205
|
this.outputInitialLength = 0
|