@amritk/nish-aarch64-linux 0.49.0 → 0.50.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +233 -159
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +12 -9
  7. package/std/README.md +8 -5
  8. package/std/base64.ts +74 -1
  9. package/std/bytes.ts +201 -93
  10. package/std/compress/deflate.ts +20 -15
  11. package/std/crypto/base64url.ts +93 -66
  12. package/std/crypto/bcrypt.ts +25 -21
  13. package/std/crypto/hmac.ts +1 -1
  14. package/std/crypto/jwt.ts +32 -19
  15. package/std/crypto/p256.ts +9 -12
  16. package/std/crypto/pki-roots.ts +7 -4
  17. package/std/crypto/pki.ts +43 -55
  18. package/std/crypto/rsa-sign.ts +1632 -0
  19. package/std/crypto/rsa.ts +26 -4
  20. package/std/crypto/sha256.ts +135 -68
  21. package/std/crypto/x25519.ts +12 -11
  22. package/std/crypto/x509.ts +164 -40
  23. package/std/gpu/sys.ts +490 -0
  24. package/std/json-equal.ts +33 -18
  25. package/std/json-format.ts +39 -8
  26. package/std/json-number.ts +15 -3
  27. package/std/json-tokenizer.ts +4 -3
  28. package/std/json-typed.ts +29 -12
  29. package/std/json-value.ts +222 -104
  30. package/std/json.ts +81 -46
  31. package/std/net/dns-config.ts +18 -5
  32. package/std/net/dns-message.ts +60 -27
  33. package/std/net/dns-resolver.ts +35 -16
  34. package/std/net/dns.ts +3 -2
  35. package/std/net/fetch.ts +120 -38
  36. package/std/net/handler.ts +178 -66
  37. package/std/net/hpack.ts +192 -98
  38. package/std/net/http-compress.ts +83 -30
  39. package/std/net/http-fields.ts +327 -31
  40. package/std/net/http-server.ts +317 -20
  41. package/std/net/http1-client-codec.ts +137 -40
  42. package/std/net/http1-codec.ts +391 -202
  43. package/std/net/http1-server.ts +220 -38
  44. package/std/net/http2-cleartext.ts +10 -4
  45. package/std/net/http2-frame.ts +2 -2
  46. package/std/net/http2-tls.ts +3 -3
  47. package/std/net/http2.ts +69 -15
  48. package/std/net/http3-frame.ts +11 -14
  49. package/std/net/http3-server.ts +12 -15
  50. package/std/net/http3.ts +41 -29
  51. package/std/net/multipart.ts +592 -0
  52. package/std/net/outbound.ts +24 -8
  53. package/std/net/postgres-async.ts +92 -54
  54. package/std/net/postgres-carrier.ts +28 -4
  55. package/std/net/postgres.ts +166 -65
  56. package/std/net/prefork.ts +86 -5
  57. package/std/net/protobuf.ts +6 -2
  58. package/std/net/qpack.ts +66 -25
  59. package/std/net/quic-conn-ack.ts +27 -7
  60. package/std/net/quic-conn-cid.ts +13 -7
  61. package/std/net/quic-datagram.ts +6 -5
  62. package/std/net/quic-frame.ts +67 -23
  63. package/std/net/quic-listener.ts +19 -12
  64. package/std/net/quic-recovery.ts +11 -9
  65. package/std/net/quic-stream.ts +7 -5
  66. package/std/net/quic.ts +36 -31
  67. package/std/net/router.ts +136 -24
  68. package/std/net/serve.ts +59 -5
  69. package/std/net/static.ts +81 -33
  70. package/std/net/tls/record.ts +26 -13
  71. package/std/net/tls/ticket.ts +6 -5
  72. package/std/net/tls-tcp.ts +69 -30
  73. package/std/net/tls.ts +152 -38
  74. package/std/net/upstream.ts +65 -19
  75. package/std/net/url.ts +97 -78
  76. package/std/net/websocket.ts +33 -15
  77. package/std/net/webtransport.ts +28 -12
  78. package/std/regex.ts +600 -388
  79. package/std/sqlite.ts +37 -19
  80. package/std/sync/ring.ts +232 -115
  81. package/std/sync/shm.ts +11 -4
  82. package/std/temporal.ts +232 -133
  83. package/std/term-io.ts +12 -6
  84. package/std/term-pixels.ts +158 -22
  85. package/std/term-rate.ts +8 -3
  86. package/std/term.ts +4 -4
  87. package/std/text.ts +33 -11
  88. package/std/threads.ts +52 -8
  89. package/std/yaml-json.ts +238 -106
  90. package/std/yaml-lines.ts +47 -27
  91. package/std/yaml-write.ts +58 -24
  92. package/std/yaml.ts +808 -395
@@ -111,6 +111,7 @@
111
111
  import { Secret, secret, wipe } from "nish:secret"
112
112
  import { base64StdPadDecode, base64StdPadEncode } from "nish/crypto/base64url"
113
113
  import { p256PublicKey, p256SignSha256, p256VerifySha256 } from "nish/crypto/p256"
114
+ import { rsaParsePrivateKey } from "nish/crypto/rsa-sign"
114
115
  import { sha256 } from "nish/crypto/sha256"
115
116
 
116
117
  /** The longest validity `x509MintSelfSigned` writes, in days: WebTransport's two weeks. */
@@ -398,6 +399,28 @@ const x509DerRead = (der: u8[], at: i32, limit: i32): X509DerElement | null => {
398
399
  return e
399
400
  }
400
401
 
402
+ /**
403
+ * `a + b` when neither is negative and the sum is at most `limit`, else `-1`:
404
+ * an offset past a length the parser has checked. It is summed as differences
405
+ * of ordered values that are not negative, which src/bounds.ts proves where it
406
+ * cannot carry `b <= limit - a` into `a + b`; past the first two checks, the
407
+ * others are always false and only state that order for the proof.
408
+ */
409
+ const x509Sum = (a: i32, b: i32, limit: i32): i32 => {
410
+ if (a < 0 || b < 0 || a > limit) {
411
+ return -1
412
+ }
413
+ const room: i32 = limit - a
414
+ if (b > room) {
415
+ return -1
416
+ }
417
+ const gap: i32 = room - b
418
+ if (gap < 0 || gap > limit) {
419
+ return -1
420
+ }
421
+ return limit - gap
422
+ }
423
+
401
424
  /**
402
425
  * `x509DerRead` into `into`, answering whether it read one: what a loop uses
403
426
  * so a pass allocates nothing it leaves behind. `into` is unspecified on `false`.
@@ -417,13 +440,14 @@ const x509DerReadInto = (der: u8[], at: i32, limit: i32, into: X509DerElement):
417
440
  if (length >= 0x80) {
418
441
  const count: i32 = length & 0x7f
419
442
  // 0x80 alone is BER's indefinite length, which DER forbids.
420
- if (count < 1 || count > 4 || count > limit - start) {
443
+ // `start < der.length` follows from the first check; spelled out because
444
+ // it is what proves the sum below.
445
+ if (count < 1 || count > 4 || count > limit - start || start >= toI32(der.length)) {
421
446
  return false
422
447
  }
423
- // Unsigned: `count <= limit - start`, so the sum cannot wrap.
424
- const stop: i32 = toI32(toU32(start) + toU32(count))
425
- // `start < stop <= limit <= der.length`, spelled out for the same reason.
426
- if (stop > toI32(der.length) || start >= toI32(der.length)) {
448
+ const stop: i32 = x509Sum(start, count, limit)
449
+ // `stop <= limit <= der.length`, spelled out for the same reason.
450
+ if (stop < 0 || stop > toI32(der.length)) {
427
451
  return false
428
452
  }
429
453
  if (toI32(der[start]) === 0 || (count === 4 && toI32(der[start]) >= 0x80)) {
@@ -438,15 +462,15 @@ const x509DerReadInto = (der: u8[], at: i32, limit: i32, into: X509DerElement):
438
462
  }
439
463
  start = stop
440
464
  }
441
- // Unsigned: `start <= limit` on both paths, so the difference cannot wrap.
442
- if (toU32(length) > toU32(limit) - toU32(start)) {
465
+ // `-1` for a length past the limit, or one read from four octets as negative.
466
+ const end: i32 = x509Sum(start, length, limit)
467
+ if (end < 0) {
443
468
  return false
444
469
  }
445
470
  into.tag = tag
446
471
  into.at = at
447
472
  into.start = start
448
- // Unsigned: `length <= limit - start`, so the sum cannot wrap.
449
- into.end = toI32(toU32(start) + toU32(length))
473
+ into.end = end
450
474
  return true
451
475
  }
452
476
 
@@ -485,8 +509,7 @@ const x509DerSize = (der: u8[], e: X509DerElement): i32 => {
485
509
  if (start < 0 || end < start || end > toI32(der.length)) {
486
510
  return -1
487
511
  }
488
- // Unsigned: `start <= end`, so the difference cannot wrap.
489
- return toI32(toU32(end) - toU32(start))
512
+ return end - start
490
513
  }
491
514
 
492
515
  /**
@@ -499,8 +522,7 @@ const x509DerOctet = (der: u8[], e: X509DerElement, k: i32): i32 => {
499
522
  if (k < 0 || k >= n) {
500
523
  return -1
501
524
  }
502
- // Unsigned: `k < end - start`, so the sum cannot wrap.
503
- const i: i32 = toI32(toU32(e.start) + toU32(k))
525
+ const i: i32 = x509Sum(e.start, k, toI32(der.length))
504
526
  if (i < 0 || i >= toI32(der.length)) {
505
527
  return -1
506
528
  }
@@ -615,8 +637,13 @@ const x509DerBitStringOctets = (der: u8[], e: X509DerElement): u8[] | null => {
615
637
  if (e.tag !== X509_TAG_BIT_STRING || !x509DerBitStringSound(der, e) || x509DerOctet(der, e, 0) !== 0) {
616
638
  return null
617
639
  }
618
- // Unsigned: a sound BIT STRING has an octet at `e.start`, so the sum cannot wrap.
619
- return x509DerSlice(der, toI32(toU32(e.start) + 1), e.end)
640
+ const start: i32 = e.start
641
+ // A sound BIT STRING has an octet at `start`, so it is below the DER's length;
642
+ // spelled out because that is what proves `start + 1`.
643
+ if (start < 0 || start >= toI32(der.length)) {
644
+ return null
645
+ }
646
+ return x509DerSlice(der, start + 1, e.end)
620
647
  }
621
648
 
622
649
  /**
@@ -666,8 +693,13 @@ const x509DerTime = (der: u8[], e: X509DerElement): i64 => {
666
693
  if (e.tag === X509_TAG_UTC_TIME && n === 13) {
667
694
  year = x509DerDigits(der, e, 0, 2)
668
695
  if (year >= 0) {
669
- // Unsigned: two digits, so the sum cannot wrap.
670
- year = toI32(toU32(year) + (year >= 50 ? 1900 : 2000))
696
+ // The mask keeps what two digits hold (at most 99) and lets
697
+ // src/bounds.ts prove the sum.
698
+ if (year >= 50) {
699
+ year = (year & 127) + 1900
700
+ } else {
701
+ year = (year & 127) + 2000
702
+ }
671
703
  }
672
704
  at = 2
673
705
  } else if (e.tag === X509_TAG_GENERALIZED_TIME && n === 15) {
@@ -697,14 +729,13 @@ const x509DerTime = (der: u8[], e: X509DerElement): i64 => {
697
729
  // itself. 1969 * 365 + 492 - 19 + 4 is the same count for 1970.
698
730
  const y: i32 = year - 1
699
731
  const years: i32 = 365 * y + y / 4 - y / 100 + y / 400 - 719162
700
- // Unsigned: at most 365 days, so the sum cannot wrap.
701
- let yday: u32 = toU32(day - 1)
732
+ // At most 365 days, which the mask keeps: it lets src/bounds.ts prove the
733
+ // sum, as `years` is bounded by the year's checks above.
734
+ let yday: i32 = day - 1
702
735
  for (let m: i32 = 1; m < month; m++) {
703
- yday = yday + toU32(x509DaysInMonth(year, m))
736
+ yday = (yday & 511) + (x509DaysInMonth(year, m) & 31)
704
737
  }
705
- // Two's complement: the true sum lies well inside i32 (the year is at most
706
- // 9999), so the unsigned add gives it exactly, negative or not.
707
- const days: i32 = toI32(toU32(years) + yday)
738
+ const days: i32 = years + (yday & 511)
708
739
  const seconds: i32 = hour * 3600 + minute * 60 + second
709
740
  return toI64(days) * 86400000 + toI64(seconds) * 1000
710
741
  }
@@ -1165,31 +1196,36 @@ const x509DerTimeAt = (seconds: i64): u8[] => {
1165
1196
  let days: i32 = toI32(seconds / 86400)
1166
1197
  const rest: i32 = toI32(seconds % 86400)
1167
1198
  let year: i32 = 1970
1168
- while (year < 9999) {
1199
+ // `days >= 0` changes nothing (a negative count is below any length and
1200
+ // stops the loop as before); it is what proves the subtraction.
1201
+ while (year < 9999 && days >= 0) {
1169
1202
  const length: i32 = x509IsLeapYear(year) ? 366 : 365
1170
1203
  if (days < length) {
1171
1204
  break
1172
1205
  }
1173
- // Unsigned: `days` is at least `length` here, so the difference cannot wrap.
1174
- days = toI32(toU32(days) - toU32(length))
1206
+ // Not negative (the loop's test) less the year's length, which the mask
1207
+ // keeps: src/bounds.ts proves the difference.
1208
+ days = days - (length & 511)
1175
1209
  year++
1176
1210
  }
1177
1211
  let month: i32 = 1
1178
- while (month < 12) {
1212
+ while (month < 12 && days >= 0) {
1179
1213
  const length: i32 = x509DaysInMonth(year, month)
1180
1214
  if (days < length) {
1181
1215
  break
1182
1216
  }
1183
- // Unsigned: `days` is at least `length` here, so the difference cannot wrap.
1184
- days = toI32(toU32(days) - toU32(length))
1217
+ // Not negative (the loop's test) less the month's length, which the mask
1218
+ // keeps: src/bounds.ts proves the difference.
1219
+ days = days - (length & 31)
1185
1220
  month++
1186
1221
  }
1187
1222
  const text: u8[] = []
1188
1223
  const utc: boolean = year < 2050
1189
1224
  x509PushDigits(text, year, !utc)
1190
1225
  x509PushDigits(text, month, false)
1191
- // Unsigned: `days` is below its month's length here, so the sum cannot wrap.
1192
- x509PushDigits(text, toI32(toU32(days) + 1), false)
1226
+ // `days` is below its month's length for any time up to 9999-12-31, which the
1227
+ // mask keeps: it lets src/bounds.ts prove the sum.
1228
+ x509PushDigits(text, (days & 31) + 1, false)
1193
1229
  x509PushDigits(text, rest / 3600, false)
1194
1230
  x509PushDigits(text, (rest / 60) % 60, false)
1195
1231
  x509PushDigits(text, rest % 60, false)
@@ -1326,8 +1362,12 @@ export const pemToDer = (pem: string, label: string): u8[][] | null => {
1326
1362
  while (lineEnd >= 0 && lineEnd < toI32(pem.length) && toI32(pem.charCodeAt(lineEnd)) !== 10) {
1327
1363
  lineEnd = lineEnd + 1
1328
1364
  }
1329
- // Unsigned: `lineEnd` is at most the text's length, so the sum cannot wrap.
1330
- const next: i32 = toI32(toU32(lineEnd) + 1)
1365
+ // Past the newline; at the text's end, the end itself, which stops the loop
1366
+ // just as one past it did.
1367
+ let next: i32 = n
1368
+ if (lineEnd >= 0 && lineEnd < n) {
1369
+ next = lineEnd + 1
1370
+ }
1331
1371
  // `lineEnd` is in the text, said again for the bounds proof.
1332
1372
  if (
1333
1373
  lineEnd > lineStart &&
@@ -1568,6 +1608,80 @@ export const x509ParseP256PrivateKey = (pem: string): Secret<u8[]> | null => {
1568
1608
  return key
1569
1609
  }
1570
1610
 
1611
+ /**
1612
+ * The RSA key of a PKCS#8 PrivateKeyInfo (RFC 5208 §5) or OneAsymmetricKey
1613
+ * (RFC 5958 §2): version 0 or 1, the algorithm rsaEncryption with NULL
1614
+ * parameters (RFC 3279 §2.3.1), and a PKCS#1 RSAPrivateKey filling the OCTET
1615
+ * STRING, which `rsaParsePrivateKey` reads and tries. The optional `[0]`
1616
+ * attributes are skipped; anything after them, a version 1 public key
1617
+ * included, is refused, since nothing here would check it against the key.
1618
+ */
1619
+ const x509Pkcs8RsaKey = (der: u8[]): Secret<u8[]> | null => {
1620
+ const total: i32 = toI32(der.length)
1621
+ const seq: X509DerElement | null = x509DerExpect(der, 0, total, X509_TAG_SEQUENCE)
1622
+ if (seq === null || seq.end !== total) {
1623
+ return null
1624
+ }
1625
+ const version: X509DerElement | null = x509DerRead(der, seq.start, seq.end)
1626
+ if (version === null) {
1627
+ return null
1628
+ }
1629
+ const v: i32 = x509DerSmallInteger(der, version)
1630
+ if (v !== 0 && v !== 1) {
1631
+ return null
1632
+ }
1633
+ const alg: X509DerElement | null = x509DerRead(der, version.end, seq.end)
1634
+ if (alg === null || !x509DerIsAlgorithmNull(der, alg, x509OidPkcs1(1))) {
1635
+ return null
1636
+ }
1637
+ const key: X509DerElement | null = x509DerExpect(der, alg.end, seq.end, X509_TAG_OCTET_STRING)
1638
+ if (key === null) {
1639
+ return null
1640
+ }
1641
+ let at: i32 = key.end
1642
+ if (x509DerPeek(der, at, seq.end) === 0xa0) {
1643
+ const attributes: X509DerElement | null = x509DerRead(der, at, seq.end)
1644
+ if (attributes === null) {
1645
+ return null
1646
+ }
1647
+ at = attributes.end
1648
+ }
1649
+ if (at !== seq.end) {
1650
+ return null
1651
+ }
1652
+ return rsaParsePrivateKey(der, key.start, key.end)
1653
+ }
1654
+
1655
+ /**
1656
+ * The RSA private key in `pem`, as the `Secret` `nish/crypto/rsa-sign` signs
1657
+ * with: one `RSA PRIVATE KEY` block (PKCS#1, RFC 8017 §A.1.2) or one `PRIVATE
1658
+ * KEY` block (unencrypted PKCS#8 with rsaEncryption, RFC 5208 or RFC 5958),
1659
+ * and not both. The key must be 2048 to 4096 bits, with an odd modulus and
1660
+ * a private exponent that signs under its modulus (`rsaParsePrivateKey`).
1661
+ * `null` for no such block or more than one, for a smaller or larger key, for
1662
+ * an EC or any other key, and for anything malformed. Every block decoded
1663
+ * from `pem` is wiped before this returns, whichever answer it gives; `pem`
1664
+ * itself is the caller's string, which nothing can wipe (X509-7 in
1665
+ * docs/security/crypto-x509.md).
1666
+ *
1667
+ * @param pem The PEM text holding the key.
1668
+ * @returns The key in `rsaParsePrivateKey`'s layout, in a fresh `Secret` the caller wipes, or `null`.
1669
+ */
1670
+ export const x509ParseRsaPrivateKey = (pem: string): Secret<u8[]> | null => {
1671
+ const pkcs1: u8[][] | null = pemToDer(pem, "RSA PRIVATE KEY")
1672
+ const pkcs8: u8[][] | null = pemToDer(pem, "PRIVATE KEY")
1673
+ let key: Secret<u8[]> | null = null
1674
+ if (pkcs1 !== null && pkcs8 === null && toI32(pkcs1.length) === 1) {
1675
+ const der: u8[] = x509FirstBlock(pkcs1)
1676
+ key = rsaParsePrivateKey(der, 0, toI32(der.length))
1677
+ } else if (pkcs8 !== null && pkcs1 === null && toI32(pkcs8.length) === 1) {
1678
+ key = x509Pkcs8RsaKey(x509FirstBlock(pkcs8))
1679
+ }
1680
+ x509WipeBlocks(pkcs1)
1681
+ x509WipeBlocks(pkcs8)
1682
+ return key
1683
+ }
1684
+
1571
1685
  // ---------------------------------------------------------------------------
1572
1686
  // Certificates (RFC 5280 §4.1)
1573
1687
  // ---------------------------------------------------------------------------
@@ -1766,8 +1880,13 @@ const x509ReadSubjectAltName = (der: u8[], start: i32, end: i32, out: X509Certif
1766
1880
  } else if (kind === X509_GN_IP && (length === 4 || length === 16)) {
1767
1881
  out.sanIp.push(x509DerSlice(der, name.start, name.end))
1768
1882
  } else if (kind === X509_GN_DIR || kind === X509_GN_OTHER) {
1769
- // Unsigned: one per name read, fewer than the DER's octets, so the sum cannot wrap.
1770
- out.sanOther = toI32(toU32(out.sanOther) + 1)
1883
+ const other: i32 = out.sanOther
1884
+ // One per name read, so fewer than the DER's octets: always false, and
1885
+ // what proves the count's step.
1886
+ if (other < 0 || other >= toI32(der.length)) {
1887
+ return false
1888
+ }
1889
+ out.sanOther = other + 1
1771
1890
  } else {
1772
1891
  return false
1773
1892
  }
@@ -1971,8 +2090,10 @@ const x509ReadSubtrees = (
1971
2090
  if (length !== 8 && length !== 32) {
1972
2091
  return false
1973
2092
  }
1974
- // Unsigned: the half is in the element, so the sum cannot wrap.
1975
- const half: i32 = toI32(toU32(base.start) + toU32(length / 2))
2093
+ const half: i32 = x509Sum(base.start, length / 2, base.end)
2094
+ if (half < 0) {
2095
+ return false
2096
+ }
1976
2097
  subtree.ip = x509DerSlice(der, base.start, half)
1977
2098
  subtree.mask = x509DerSlice(der, half, base.end)
1978
2099
  if (!x509MaskContiguous(subtree.mask)) {
@@ -2564,12 +2685,15 @@ export const x509MintSelfSigned = (
2564
2685
  if (start < 0 || start > 253402300799) {
2565
2686
  return null
2566
2687
  }
2567
- // Unsigned: `start` is below 2^38 and `days` at most `X509_MAX_DAYS`, so the sum cannot wrap.
2568
- const end: i64 = toI64(toU64(start) + toU64(days) * 86400)
2688
+ // In `u64`: `start` is below 2^38 and `days` at most `X509_MAX_DAYS`, so the
2689
+ // sum comes nowhere near 2^64.
2690
+ const stop: u64 = toU64(start) + toU64(days) * toU64(86400)
2569
2691
  // 9999-12-31T23:59:59Z, the last second RFC 5280's times can spell.
2570
- if (end > 253402300799) {
2692
+ const last: i64 = 253402300799
2693
+ if (stop > toU64(last)) {
2571
2694
  return null
2572
2695
  }
2696
+ const end: i64 = toI64(stop)
2573
2697
  const name: u8[] = x509Bytes(commonName)
2574
2698
  const nameLength: i32 = toI32(name.length)
2575
2699
  if (nameLength < 1 || nameLength > X509_MAX_COMMON_NAME || !x509IsUtf8Name(name)) {