@amritk/nish-aarch64-linux 0.50.0 → 0.51.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.
Files changed (54) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +4 -1
  3. package/package.json +1 -1
  4. package/runtime/nish.d.ts +82 -2
  5. package/runtime/nish.h +5 -0
  6. package/runtime/runtime-host.c +21 -5
  7. package/std/README.md +7 -7
  8. package/std/base64.ts +61 -95
  9. package/std/bytes.ts +6 -4
  10. package/std/cli-args.ts +150 -107
  11. package/std/compress/checksum.ts +68 -10
  12. package/std/compress/deflate.ts +193 -100
  13. package/std/compress/gzip.ts +124 -49
  14. package/std/compress/zlib.ts +57 -15
  15. package/std/crypto/aes.ts +51 -9
  16. package/std/crypto/ed25519.ts +12 -10
  17. package/std/crypto/hkdf.ts +405 -283
  18. package/std/crypto/hmac.ts +483 -148
  19. package/std/crypto/jwt.ts +19 -21
  20. package/std/crypto/p256.ts +5 -7
  21. package/std/crypto/pki-roots.ts +28 -8
  22. package/std/crypto/rsa-sign.ts +5 -2
  23. package/std/crypto/sha256.ts +81 -26
  24. package/std/crypto/sha512.ts +135 -49
  25. package/std/json-value.ts +14 -15
  26. package/std/net/buffer-pool.ts +25 -14
  27. package/std/net/fetch.ts +108 -27
  28. package/std/net/handler.ts +11 -22
  29. package/std/net/hpack.ts +38 -19
  30. package/std/net/http-compress.ts +21 -12
  31. package/std/net/http-server.ts +9 -4
  32. package/std/net/http1-client-codec.ts +22 -15
  33. package/std/net/http1-codec.ts +37 -36
  34. package/std/net/http3-server.ts +127 -45
  35. package/std/net/http3.ts +99 -50
  36. package/std/net/postgres-async.ts +5 -10
  37. package/std/net/qpack.ts +83 -17
  38. package/std/net/quic-listener.ts +42 -27
  39. package/std/net/quic-packet.ts +91 -50
  40. package/std/net/quic-stream.ts +29 -22
  41. package/std/net/quic.ts +48 -23
  42. package/std/net/static.ts +5 -3
  43. package/std/net/tls/client.ts +81 -34
  44. package/std/net/tls/codec.ts +3 -4
  45. package/std/net/tls/record.ts +54 -22
  46. package/std/net/tls/schedule.ts +339 -149
  47. package/std/net/tls.ts +42 -12
  48. package/std/net/upstream.ts +18 -19
  49. package/std/net/url.ts +78 -21
  50. package/std/net/websocket.ts +3 -11
  51. package/std/net/webtransport.ts +95 -21
  52. package/std/raster.ts +12 -2
  53. package/std/term-pixels.ts +18 -2
  54. package/std/text.ts +5 -5
@@ -863,8 +863,7 @@ export class TlsClient {
863
863
  if (length < 0 || length > 65536) {
864
864
  return false
865
865
  }
866
- this.transcript.update(this.input, TLS_CLIENT_FROM, 4 + length)
867
- return true
866
+ return this.transcript.tryUpdate(this.input, TLS_CLIENT_FROM, 4 + length)
868
867
  }
869
868
 
870
869
  /** A ServerHello or HelloRetryRequest, `input[0 .. 4 + length)`. */
@@ -892,8 +891,8 @@ export class TlsClient {
892
891
  if (view.shareLength !== shareSize) {
893
892
  return TLS_ALERT_ILLEGAL_PARAMETER
894
893
  }
895
- if (!this.retried) {
896
- this.startTranscript(view.suite)
894
+ if (!this.retried && !this.startTranscript(view.suite)) {
895
+ return TLS_ALERT_INTERNAL_ERROR
897
896
  }
898
897
  if (!this.hashMessage(length)) {
899
898
  return TLS_ALERT_INTERNAL_ERROR
@@ -905,8 +904,26 @@ export class TlsClient {
905
904
  const pool: TlsSecretPool = this.secrets()
906
905
  const h: i32 = this.hashLength
907
906
  this.transcript.hashInto(this.hashes, TLS_CLIENT_FROM)
908
- tlsDeriveSecretHeld(h, pool.handshake, "c hs traffic", this.hashes, TLS_CLIENT_FROM, pool.clientHandshake)
909
- tlsDeriveSecretHeld(h, pool.handshake, "s hs traffic", this.hashes, TLS_CLIENT_FROM, pool.serverHandshake)
907
+ if (
908
+ !tlsDeriveSecretHeld(
909
+ h,
910
+ pool.handshake,
911
+ "c hs traffic",
912
+ this.hashes,
913
+ TLS_CLIENT_FROM,
914
+ pool.clientHandshake
915
+ ) ||
916
+ !tlsDeriveSecretHeld(
917
+ h,
918
+ pool.handshake,
919
+ "s hs traffic",
920
+ this.hashes,
921
+ TLS_CLIENT_FROM,
922
+ pool.serverHandshake
923
+ )
924
+ ) {
925
+ return TLS_ALERT_INTERNAL_ERROR
926
+ }
910
927
  this.held =
911
928
  this.held |
912
929
  (1 << TLS_SECRET_HANDSHAKE) |
@@ -916,12 +933,16 @@ export class TlsClient {
916
933
  return 0
917
934
  }
918
935
 
919
- /** Starts the transcript over `suite`'s hash, from the first ClientHello. */
920
- private startTranscript(suite: i32): void {
936
+ /**
937
+ * Starts the transcript over `suite`'s hash, from the first ClientHello;
938
+ * false when the hash refuses the hello's window, so the caller fails the
939
+ * handshake.
940
+ */
941
+ private startTranscript(suite: i32): boolean {
921
942
  this.suite = suite
922
943
  this.hashLength = tlsSuiteHashLength(suite)
923
944
  this.transcript.reset(this.hashLength)
924
- this.transcript.update(this.firstHello, TLS_CLIENT_FROM, toI32(this.firstHello.length))
945
+ return this.transcript.tryUpdate(this.firstHello, TLS_CLIENT_FROM, toI32(this.firstHello.length))
925
946
  }
926
947
 
927
948
  /**
@@ -944,8 +965,12 @@ export class TlsClient {
944
965
  return TLS_ALERT_ILLEGAL_PARAMETER
945
966
  }
946
967
  this.retried = true
947
- this.startTranscript(view.suite)
948
- this.transcript.restartWithMessageHash()
968
+ if (!this.startTranscript(view.suite)) {
969
+ return TLS_ALERT_INTERNAL_ERROR
970
+ }
971
+ if (!this.transcript.tryRestartWithMessageHash()) {
972
+ return TLS_ALERT_INTERNAL_ERROR
973
+ }
949
974
  if (!this.hashMessage(length)) {
950
975
  return TLS_ALERT_INTERNAL_ERROR
951
976
  }
@@ -1002,10 +1027,10 @@ export class TlsClient {
1002
1027
  }
1003
1028
  const h: i32 = this.hashLength
1004
1029
  const early: Secret<u8[]> = tlsEarlySecret(h)
1005
- tlsHandshakeSecretHeld(h, early, shared, this.secrets().handshake)
1030
+ const derived: boolean = tlsHandshakeSecretHeld(h, early, shared, this.secrets().handshake)
1006
1031
  wipe(early)
1007
1032
  wipe(shared)
1008
- return 0
1033
+ return derived ? 0 : TLS_ALERT_INTERNAL_ERROR
1009
1034
  }
1010
1035
 
1011
1036
  /** EncryptedExtensions (§4.3.1): ALPN, if any, must be one the client offered, and `server_name` acknowledged only if sent. */
@@ -1095,7 +1120,15 @@ export class TlsClient {
1095
1120
  }
1096
1121
  const pool: TlsSecretPool = this.secrets()
1097
1122
  this.transcript.hashInto(this.hashes, TLS_CLIENT_FROM)
1098
- const expected: u8[] = tlsFinishedVerifyDataHeld(h, pool.serverHandshake, this.hashes, TLS_CLIENT_FROM)
1123
+ const expected: u8[] | null = tlsFinishedVerifyDataHeld(
1124
+ h,
1125
+ pool.serverHandshake,
1126
+ this.hashes,
1127
+ TLS_CLIENT_FROM
1128
+ )
1129
+ if (expected === null) {
1130
+ return TLS_ALERT_INTERNAL_ERROR
1131
+ }
1099
1132
  const matches: boolean = timingSafeEqualAt(this.input, 4, expected, TLS_CLIENT_FROM, h)
1100
1133
  secureZero(expected)
1101
1134
  if (!matches) {
@@ -1107,26 +1140,38 @@ export class TlsClient {
1107
1140
  this.transcript.hashInto(this.hashes, TLS_CLIENT_FROM)
1108
1141
  // The master secret is derived over the handshake secret, which has no
1109
1142
  // other use left.
1110
- tlsMasterSecretHeld(h, pool.handshake, pool.handshake)
1111
- tlsDeriveSecretHeld(
1112
- h,
1113
- pool.handshake,
1114
- "c ap traffic",
1115
- this.hashes,
1116
- TLS_CLIENT_FROM,
1117
- pool.clientApplication
1118
- )
1119
- tlsDeriveSecretHeld(
1143
+ if (
1144
+ !tlsMasterSecretHeld(h, pool.handshake, pool.handshake) ||
1145
+ !tlsDeriveSecretHeld(
1146
+ h,
1147
+ pool.handshake,
1148
+ "c ap traffic",
1149
+ this.hashes,
1150
+ TLS_CLIENT_FROM,
1151
+ pool.clientApplication
1152
+ ) ||
1153
+ !tlsDeriveSecretHeld(
1154
+ h,
1155
+ pool.handshake,
1156
+ "s ap traffic",
1157
+ this.hashes,
1158
+ TLS_CLIENT_FROM,
1159
+ pool.serverApplication
1160
+ ) ||
1161
+ !tlsDeriveSecretHeld(h, pool.handshake, "exp master", this.hashes, TLS_CLIENT_FROM, pool.exporter)
1162
+ ) {
1163
+ return TLS_ALERT_INTERNAL_ERROR
1164
+ }
1165
+ pool.zero(TLS_SECRET_HANDSHAKE)
1166
+ const verifyData: u8[] | null = tlsFinishedVerifyDataHeld(
1120
1167
  h,
1121
- pool.handshake,
1122
- "s ap traffic",
1168
+ pool.clientHandshake,
1123
1169
  this.hashes,
1124
- TLS_CLIENT_FROM,
1125
- pool.serverApplication
1170
+ TLS_CLIENT_FROM
1126
1171
  )
1127
- tlsDeriveSecretHeld(h, pool.handshake, "exp master", this.hashes, TLS_CLIENT_FROM, pool.exporter)
1128
- pool.zero(TLS_SECRET_HANDSHAKE)
1129
- const verifyData: u8[] = tlsFinishedVerifyDataHeld(h, pool.clientHandshake, this.hashes, TLS_CLIENT_FROM)
1172
+ if (verifyData === null) {
1173
+ return TLS_ALERT_INTERNAL_ERROR
1174
+ }
1130
1175
  const at: i32 = this.outputHandshakeLength
1131
1176
  const end: i32 = tlsWriteFinished(this.outputHandshake, at, verifyData, TLS_CLIENT_FROM, h)
1132
1177
  secureZero(verifyData)
@@ -1255,12 +1300,14 @@ export class TlsClient {
1255
1300
  return false
1256
1301
  }
1257
1302
  if (level === TLS_LEVEL_INITIAL) {
1258
- if (this.hashLength !== 0) {
1259
- this.transcript.update(this.outputInitial, at, end - at)
1303
+ if (this.hashLength !== 0 && !this.transcript.tryUpdate(this.outputInitial, at, end - at)) {
1304
+ return false
1260
1305
  }
1261
1306
  this.outputInitialLength = end
1262
1307
  } else {
1263
- this.transcript.update(this.outputHandshake, at, end - at)
1308
+ if (!this.transcript.tryUpdate(this.outputHandshake, at, end - at)) {
1309
+ return false
1310
+ }
1264
1311
  this.outputHandshakeLength = end
1265
1312
  }
1266
1313
  return true
@@ -1847,11 +1847,10 @@ export const tlsWriteCertificateVerifyContent = (
1847
1847
  * bytes or with a vector its length field cannot hold, gives an empty one, which the writer then leaves empty.
1848
1848
  */
1849
1849
  const tlsZeros = (n: i32): u8[] => {
1850
- const out: u8[] = []
1851
- for (let k: i32 = 0; k < n; k++) {
1852
- out.push(0)
1850
+ if (n <= 0) {
1851
+ return []
1853
1852
  }
1854
- return out
1853
+ return new Array<u8>(n)
1855
1854
  }
1856
1855
 
1857
1856
  /** The bytes of `name` as a string of one character a byte, which `tlsWriteEncryptedExtensions` writes back as they were. */
@@ -71,10 +71,11 @@ import {
71
71
  TLS_LEGACY_VERSION,
72
72
  } from "nish/net/tls/codec"
73
73
  import {
74
+ HkdfHash,
74
75
  HkdfScratch,
75
- hkdfExpandLabelInto,
76
- hkdfExpandLabelSha256Held,
77
- hkdfExpandLabelSha384Held,
76
+ tryHkdfExpandLabelInto,
77
+ tryHkdfExpandLabelSha256Held,
78
+ tryHkdfExpandLabelSha384Held,
78
79
  } from "nish/crypto/hkdf"
79
80
  import { SHA384_SIZE } from "nish/crypto/sha512"
80
81
  import {
@@ -172,9 +173,9 @@ const TLS_RECORD_FROM: i32 = 0
172
173
  *
173
174
  * @param hashLength The hash length of the connection's suite, 32 or 48, and so how many bytes are derived.
174
175
  * @param trafficSecret The direction's current traffic secret. This function does not wipe it.
175
- * @returns A fresh `Secret` holding the next traffic secret, which the caller owns and wipes.
176
+ * @returns A fresh `Secret` holding the next traffic secret, which the caller owns and wipes, or `null` when `hashLength` is outside 0 to 255, so no `HkdfLabel` holds it, or the secret is shorter than the hash it names (SHA-384 for 48, SHA-256 for any other).
176
177
  */
177
- export const tlsNextTrafficSecret = (hashLength: i32, trafficSecret: Secret<u8[]>): Secret<u8[]> => {
178
+ export const tlsNextTrafficSecret = (hashLength: i32, trafficSecret: Secret<u8[]>): Secret<u8[]> | null => {
178
179
  const none: u8[] = []
179
180
  return tlsExpandLabel(hashLength, trafficSecret, "traffic upd", none, hashLength)
180
181
  }
@@ -325,13 +326,15 @@ export class TlsRecordReader {
325
326
  /**
326
327
  * Copies as many of `data[off .. off + len)` as fit and answers how many,
327
328
  * after moving what is held to the front. A window outside `data` is
328
- * `-TLS_ALERT_INTERNAL_ERROR`, and nothing is copied.
329
+ * `-TLS_ALERT_INTERNAL_ERROR`, and nothing is copied; so is a first byte
330
+ * that finds a shared pool (`borrowFrom`) with every buffer lent.
329
331
  *
330
332
  * @param data The array that holds the bytes read from the stream.
331
333
  * @param off Where in `data` the new bytes start.
332
334
  * @param len How many bytes, from `off`, to offer.
333
335
  * @returns How many of the bytes were taken, from 0 (the buffer is full) up to `len`; the caller offers the
334
- * rest again once a record is consumed. `-TLS_ALERT_INTERNAL_ERROR` for a window outside `data`.
336
+ * rest again once a record is consumed. `-TLS_ALERT_INTERNAL_ERROR` for a window outside `data`
337
+ * or a spent shared pool.
335
338
  */
336
339
  push(data: u8[], off: i32, len: i32): i32 {
337
340
  if (!tlsRecordWindowFits(toI32(data.length), off, len)) {
@@ -341,7 +344,12 @@ export class TlsRecordReader {
341
344
  return 0
342
345
  }
343
346
  if (this.buffer.length === 0) {
344
- this.buffer = this.pool.take()
347
+ // A shared pool with every buffer lent leaves this reader nothing to read into.
348
+ const taken: u8[] | null = this.pool.tryTake()
349
+ if (taken === null) {
350
+ return -TLS_ALERT_INTERNAL_ERROR
351
+ }
352
+ this.buffer = taken
345
353
  }
346
354
  const buffer: u8[] = this.buffer
347
355
  let start: i32 = this.start
@@ -545,7 +553,9 @@ export class TlsRecordProtection {
545
553
  return false
546
554
  }
547
555
  this.reserve(suite)
548
- this.derive(suite, secret)
556
+ if (!this.derive(suite, secret)) {
557
+ return false
558
+ }
549
559
  this.suite = suite
550
560
  return true
551
561
  }
@@ -564,15 +574,21 @@ export class TlsRecordProtection {
564
574
  * direction's own HKDF scratch, which is wiped after, and the schedule
565
575
  * expanded into this direction's `AesKey` by `aesKeyInto`. All of it runs
566
576
  * inside an arena block, so a key install leaves the arena where it was.
567
- * ChaCha20 has no schedule.
577
+ * ChaCha20 has no schedule. False, with no schedule expanded, when
578
+ * `tlsTrafficKeysInto` refuses the secret or the arrays; whatever it wrote
579
+ * of the key and IV is zeroed by `clear` before it answers.
568
580
  */
569
- private derive(suite: i32, secret: u8[]): void {
581
+ private derive(suite: i32, secret: u8[]): boolean {
570
582
  using _scope = arena()
571
- tlsTrafficKeysInto(this.kdf, suite, secret, this.key, this.iv)
583
+ if (!tlsTrafficKeysInto(this.kdf, suite, secret, this.key, this.iv)) {
584
+ this.clear()
585
+ return false
586
+ }
572
587
  const aes: AesKey | null = this.aes
573
588
  if (aes !== null && suite !== TLS_CHACHA20_POLY1305_SHA256) {
574
589
  aesKeyInto(this.key, aes)
575
590
  }
591
+ return true
576
592
  }
577
593
 
578
594
  /**
@@ -592,7 +608,7 @@ export class TlsRecordProtection {
592
608
  * keys are. False, as `install` answers, when the keys do not install.
593
609
  *
594
610
  * @param secret The direction's current traffic secret, plain, which is overwritten with the next one. It
595
- * stays the caller's to wipe. One shorter than the suite's hash length panics in HKDF.
611
+ * stays the caller's to wipe. One not of the suite's hash length is left as it was, and answers `false`.
596
612
  * @returns `true` when the next keys are installed; `false` for a cleartext direction, or when the install
597
613
  * fails and the direction is left cleartext.
598
614
  */
@@ -603,12 +619,13 @@ export class TlsRecordProtection {
603
619
  return false
604
620
  }
605
621
  const next: u8[] = this.next
622
+ let stepped: boolean = false
606
623
  {
607
624
  using _scope = arena()
608
625
  const none: u8[] = []
609
- hkdfExpandLabelInto(
626
+ stepped = tryHkdfExpandLabelInto(
610
627
  this.kdf,
611
- hashLength,
628
+ hashLength === SHA384_SIZE ? HkdfHash.Sha384 : HkdfHash.Sha256,
612
629
  secret,
613
630
  "traffic upd",
614
631
  none,
@@ -619,10 +636,18 @@ export class TlsRecordProtection {
619
636
  hashLength
620
637
  )
621
638
  }
622
- for (let k: i32 = 0; k < toI32(secret.length) && k < hashLength && k < toI32(next.length); k++) {
639
+ for (
640
+ let k: i32 = 0;
641
+ stepped && k < toI32(secret.length) && k < hashLength && k < toI32(next.length);
642
+ k++
643
+ ) {
623
644
  secret[k] = next[k]
624
645
  }
625
646
  secureZero(next)
647
+ if (!stepped) {
648
+ this.clear()
649
+ return false
650
+ }
626
651
  return this.install(suite, secret)
627
652
  }
628
653
 
@@ -644,7 +669,10 @@ export class TlsRecordProtection {
644
669
  return false
645
670
  }
646
671
  this.reserve(suite)
647
- tlsTrafficKeysFromHeld(suite, secret, this.key, this.iv)
672
+ if (!tlsTrafficKeysFromHeld(suite, secret, this.key, this.iv)) {
673
+ this.clear()
674
+ return false
675
+ }
648
676
  const aes: AesKey | null = this.aes
649
677
  if (aes !== null && suite !== TLS_CHACHA20_POLY1305_SHA256) {
650
678
  using _scope = arena()
@@ -672,13 +700,17 @@ export class TlsRecordProtection {
672
700
  if (hashLength === 0) {
673
701
  return false
674
702
  }
703
+ let stepped: boolean = true
675
704
  if (expose(secret, (bytes: u8[]): i32 => toI32(bytes.length)) === hashLength) {
676
705
  const none: u8[] = []
677
- if (hashLength === SHA384_SIZE) {
678
- hkdfExpandLabelSha384Held(secret, secret, "traffic upd", none)
679
- } else {
680
- hkdfExpandLabelSha256Held(secret, secret, "traffic upd", none)
681
- }
706
+ stepped =
707
+ hashLength === SHA384_SIZE
708
+ ? tryHkdfExpandLabelSha384Held(secret, secret, "traffic upd", none)
709
+ : tryHkdfExpandLabelSha256Held(secret, secret, "traffic upd", none)
710
+ }
711
+ if (!stepped) {
712
+ this.clear()
713
+ return false
682
714
  }
683
715
  return this.installHeld(suite, secret)
684
716
  }