@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/crypto/x509.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* PEM, and mint the short-lived self-signed ECDSA P-256 certificate a browser
|
|
5
5
|
* pins by its SHA-256.
|
|
6
6
|
*
|
|
7
|
+
* import { Secret, wipe } from "nish:secret";
|
|
7
8
|
* import { x509MintSelfSigned, x509CertificateHash, derToPem } from "nish/crypto/x509";
|
|
8
9
|
* // Native only: a program that imports this does not compile for wasm32, which has no entropy.
|
|
9
10
|
* import { x509DrawP256Key, x509DrawSerial } from "nish/crypto/x509-random";
|
|
@@ -28,17 +29,18 @@
|
|
|
28
29
|
* must be definite and minimal (no `0x80`, no long form below 128, no leading
|
|
29
30
|
* zero length octet), an INTEGER must be minimal in two's complement, a tag
|
|
30
31
|
* must fit in one octet, every element must end inside its parent, and a
|
|
31
|
-
* certificate must end where its outer SEQUENCE does.
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
32
|
+
* certificate must end where its outer SEQUENCE does.
|
|
33
|
+
*
|
|
34
|
+
* Anything else answers `null`, never a panic. The reader takes the version,
|
|
35
|
+
* serial, algorithms, the issuer and subject Names (their RDNs checked, their
|
|
36
|
+
* bytes kept whole), validity, the subject public key and the signature, checks
|
|
37
|
+
* the order of what follows the key (`[1]`, `[2]`, `[3]`), and decodes the
|
|
38
|
+
* extensions a path validator needs: subjectAltName, basicConstraints,
|
|
39
|
+
* keyUsage, extKeyUsage, nameConstraints and the two key identifiers. A
|
|
40
|
+
* duplicate or malformed one refuses the certificate, and any other critical
|
|
41
|
+
* one sets `unknownCritical`. It parses and does not validate: applying these
|
|
42
|
+
* to a chain is RFC 5280 §6 path validation, which `nish/crypto/pki`'s
|
|
43
|
+
* `pkiVerifyServer` does. The mint writes two extensions of its own (below).
|
|
42
44
|
*
|
|
43
45
|
* **PEM is RFC 7468's strict grammar**, with standard base64 (`+` and `/`,
|
|
44
46
|
* padded with `=`), not `base64url`: 64 characters a line and a shorter last
|
|
@@ -51,44 +53,47 @@
|
|
|
51
53
|
* `serverCertificateHashes` accepts (W3C WebTransport, "custom certificate
|
|
52
54
|
* requirements"): X.509 v3, an ECDSA P-256 key signed with ecdsa-with-SHA256,
|
|
53
55
|
* and a validity period of at most fourteen days — `notAfter - notBefore`,
|
|
54
|
-
* which is what Chromium compares, so `days` is 1 to 14. Subject and issuer
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
56
|
+
* which is what Chromium compares, so `days` is 1 to 14. Subject and issuer are
|
|
57
|
+
* the same single common name, as a UTF8String. It carries **two extensions,
|
|
58
|
+
* both critical and both constant**: basicConstraints with cA FALSE and
|
|
59
|
+
* keyUsage with digitalSignature alone.
|
|
60
|
+
*
|
|
61
|
+
* The W3C text requires no extension, but Chromium does: the verifier behind
|
|
62
|
+
* `serverCertificateHashes`, quiche's `WebTransportFingerprintProofVerifier`,
|
|
63
|
+
* parses the certificate with `CertificateView::ParseSingleCertificate` before
|
|
64
|
+
* it compares the hash, and that answers null when `[3] extensions` is absent
|
|
65
|
+
* (`quiche/quic/core/crypto/certificate_view.cc:395`, at the quiche revision
|
|
66
|
+
* 62826931 Chromium 141 pins), so the handshake fails with
|
|
67
|
+
* CERTIFICATE_VERIFY_FAILED. It also holds every extension to `SEQUENCE { OID,
|
|
68
|
+
* BOOLEAN OPTIONAL, OCTET STRING }` (`:413`–`:432`) and reads subjectAltName
|
|
69
|
+
* only when one is present (`:434`), so no name is needed there.
|
|
70
|
+
*
|
|
71
|
+
* The two chosen say what the certificate is, an end entity whose key only
|
|
72
|
+
* signs handshakes, so a verifier that does build a path need not fall back on
|
|
73
|
+
* its defaults; OpenSSL verifies it as its own trust anchor
|
|
74
|
+
* (`tests/link/crypto_x509`'s header has the commands; X509-9 in
|
|
75
|
+
* docs/security/crypto-x509.md). The key, the serial and the time are
|
|
76
|
+
* parameters so the golden certificate is deterministic; a caller passes
|
|
77
|
+
* `Date.now()` and, for the key and the serial, bytes from
|
|
78
|
+
* `crypto.getRandomValues`. This module does not draw them itself, because a
|
|
79
|
+
* call to the operating system would stop it compiling for wasm32;
|
|
80
|
+
* `nish/crypto/x509-random`, which this module does not import, draws both for
|
|
81
|
+
* a native program (X509-6).
|
|
78
82
|
*
|
|
79
83
|
* **Constant time.** A private key is the one secret that passes through here:
|
|
80
84
|
* it is base64-decoded from PEM, lifted out of its DER, and handed to
|
|
81
85
|
* `nish/crypto/p256`. Base64 is `nish/crypto/base64url`'s `base64StdPadDecode`,
|
|
82
86
|
* which maps a character to its sextet by arithmetic on range masks and folds a
|
|
83
87
|
* bad character into one word read after the whole text, so neither a branch
|
|
84
|
-
* nor a table index depends on the key's bytes.
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* the
|
|
88
|
+
* nor a table index depends on the key's bytes.
|
|
89
|
+
*
|
|
90
|
+
* PEM's framing is found by comparing the start of each line with the fixed
|
|
91
|
+
* `-----BEGIN ` and `-----END `, which no base64 character matches, so those
|
|
92
|
+
* comparisons end on a line's first byte whatever the key is. The DER reader
|
|
93
|
+
* branches on tags and lengths, which are the key's public framing, and copies
|
|
94
|
+
* its 32 octets with a fixed loop. Signing is `p256SignSha256`, and checking a
|
|
95
|
+
* key against the public key stored beside it compares public values. None of
|
|
96
|
+
* this is under the disassembly check; it is discipline.
|
|
92
97
|
*
|
|
93
98
|
* **The private key is a `Secret`** (`nish:secret`, docs/LANGUAGE.md
|
|
94
99
|
* "Secrets"). `x509ParseP256PrivateKey` answers one and `x509MintSelfSigned`
|
|
@@ -214,6 +219,7 @@ export class X509GeneralSubtree {
|
|
|
214
219
|
/** For a directoryName, the Name's DER, whole. */
|
|
215
220
|
dirName: u8[]
|
|
216
221
|
|
|
222
|
+
/** A subtree with no form and no name; `x509ParseCertificate` fills each one it reads. */
|
|
217
223
|
constructor() {
|
|
218
224
|
this.ip = []
|
|
219
225
|
this.mask = []
|
|
@@ -302,6 +308,11 @@ export class X509Certificate {
|
|
|
302
308
|
/** Whether a critical extension is one this module does not decode: RFC 5280 §4.2 has a verifier refuse it. */
|
|
303
309
|
unknownCritical: boolean = false
|
|
304
310
|
|
|
311
|
+
/**
|
|
312
|
+
* A certificate with every field empty, zero, `false` or `null`, and
|
|
313
|
+
* `bcPathLen` and `keyUsage` at -1 for absent; `x509ParseCertificate` fills
|
|
314
|
+
* one in.
|
|
315
|
+
*/
|
|
305
316
|
constructor() {
|
|
306
317
|
this.der = []
|
|
307
318
|
this.tbs = []
|
|
@@ -323,10 +334,13 @@ export class X509Certificate {
|
|
|
323
334
|
|
|
324
335
|
/** One element's identifier octet and where its contents lie: `der[start .. end)`. */
|
|
325
336
|
class X509DerElement {
|
|
337
|
+
/** @internal */
|
|
326
338
|
tag: i32
|
|
327
|
-
/** Where the identifier octet is, so the whole element can be copied. */
|
|
339
|
+
/** @internal Where the identifier octet is, so the whole element can be copied. */
|
|
328
340
|
at: i32
|
|
341
|
+
/** @internal */
|
|
329
342
|
start: i32
|
|
343
|
+
/** @internal */
|
|
330
344
|
end: i32
|
|
331
345
|
|
|
332
346
|
constructor(tag: i32, at: i32, start: i32, end: i32) {
|
|
@@ -755,8 +769,11 @@ const x509DerHashAlgorithm = (der: u8[], start: i32, end: i32): i32 => {
|
|
|
755
769
|
|
|
756
770
|
/** The signature algorithm `X509Certificate` reports, and for RSASSA-PSS its hash and salt length. */
|
|
757
771
|
class X509SigAlg {
|
|
772
|
+
/** @internal */
|
|
758
773
|
alg: i32 = 0
|
|
774
|
+
/** @internal */
|
|
759
775
|
hash: i32 = 0
|
|
776
|
+
/** @internal */
|
|
760
777
|
salt: i32 = 0
|
|
761
778
|
}
|
|
762
779
|
|
|
@@ -899,6 +916,10 @@ const x509DerUnsignedInto = (der: u8[], e: X509DerElement, out: u8[], off: i32,
|
|
|
899
916
|
* 2^(8 * width), and `width` is 32 (P-256) or 48 (P-384). This is the form a
|
|
900
917
|
* TLS `CertificateVerify` or an X.509 signature carries; that each value is
|
|
901
918
|
* below the curve's order is the curve's check.
|
|
919
|
+
*
|
|
920
|
+
* @param sig The DER signature, read and not kept.
|
|
921
|
+
* @param width The octets each of `r` and `s` is padded to: 32 for P-256, 48 for P-384.
|
|
922
|
+
* @returns A fresh array of `2 * width` octets, `r` then `s`, or `null`.
|
|
902
923
|
*/
|
|
903
924
|
export const x509DerSignatureRSWidth = (sig: u8[], width: i32): u8[] | null => {
|
|
904
925
|
if (width !== 32 && width !== 48) {
|
|
@@ -927,6 +948,9 @@ export const x509DerSignatureRSWidth = (sig: u8[], width: i32): u8[] | null => {
|
|
|
927
948
|
/**
|
|
928
949
|
* `x509DerSignatureRSWidth` for P-256: `r || s`, 32 octets each, the form
|
|
929
950
|
* `nish/crypto/p256` takes.
|
|
951
|
+
*
|
|
952
|
+
* @param sig The DER signature, read and not kept.
|
|
953
|
+
* @returns A fresh 64-octet array, `r` then `s`, or `null`.
|
|
930
954
|
*/
|
|
931
955
|
export const x509DerSignatureRS = (sig: u8[]): u8[] | null => x509DerSignatureRSWidth(sig, 32)
|
|
932
956
|
|
|
@@ -1117,6 +1141,10 @@ const x509EndsWith = (text: string, suffix: string): boolean => {
|
|
|
1117
1141
|
* `der` as a PEM block (RFC 7468 §2): `-----BEGIN label-----`, the base64 of
|
|
1118
1142
|
* the DER in lines of 64 characters, and `-----END label-----`, each line
|
|
1119
1143
|
* ending in a line feed.
|
|
1144
|
+
*
|
|
1145
|
+
* @param der The bytes to encode; any length, empty included.
|
|
1146
|
+
* @param label The text between `BEGIN ` and the dashes, such as `CERTIFICATE`; written as given, unchecked.
|
|
1147
|
+
* @returns The PEM text, ending in a line feed.
|
|
1120
1148
|
*/
|
|
1121
1149
|
export const derToPem = (der: u8[], label: string): string => {
|
|
1122
1150
|
const body: string = base64StdPadEncode(der)
|
|
@@ -1136,7 +1164,12 @@ export const derToPem = (der: u8[], label: string): string => {
|
|
|
1136
1164
|
* an unterminated block, an END whose label is not its BEGIN's, a line longer
|
|
1137
1165
|
* than 64 characters or a short line before the last, a header line, or
|
|
1138
1166
|
* base64 that is not canonical. Blocks with other labels must be as well
|
|
1139
|
-
* formed, and are skipped; so is text outside the blocks
|
|
1167
|
+
* formed, and are skipped; so is text outside the blocks, though an
|
|
1168
|
+
* `-----END` line outside a block answers `null`. A line may end in CRLF.
|
|
1169
|
+
*
|
|
1170
|
+
* @param pem The PEM text.
|
|
1171
|
+
* @param label The label to collect, compared byte for byte.
|
|
1172
|
+
* @returns A fresh array holding each matching block's DER, freshly decoded, or `null`.
|
|
1140
1173
|
*/
|
|
1141
1174
|
export const pemToDer = (pem: string, label: string): u8[][] | null => {
|
|
1142
1175
|
const out: u8[][] = []
|
|
@@ -1352,6 +1385,9 @@ const x509WipeBlocks = (blocks: u8[][] | null): void => {
|
|
|
1352
1385
|
* and answers `null`. Every block decoded from `pem` is wiped before this
|
|
1353
1386
|
* returns, whichever answer it gives; `pem` itself is the caller's string,
|
|
1354
1387
|
* which nothing can wipe (X509-7 in docs/security/crypto-x509.md).
|
|
1388
|
+
*
|
|
1389
|
+
* @param pem The PEM text holding the key.
|
|
1390
|
+
* @returns The 32-byte scalar, big-endian, in a fresh `Secret` the caller wipes, or `null`.
|
|
1355
1391
|
*/
|
|
1356
1392
|
export const x509ParseP256PrivateKey = (pem: string): Secret<u8[]> | null => {
|
|
1357
1393
|
const sec1: u8[][] | null = pemToDer(pem, "EC PRIVATE KEY")
|
|
@@ -1415,6 +1451,10 @@ const x509DerNameSound = (der: u8[], e: X509DerElement): boolean => {
|
|
|
1415
1451
|
* each attribute — is not implemented (X509-12 in docs/security/crypto-x509.md),
|
|
1416
1452
|
* so two Names spelled differently that it would match answer `false`. That
|
|
1417
1453
|
* fails closed: a chain whose issuer and subject differ only so is not built.
|
|
1454
|
+
*
|
|
1455
|
+
* @param a One Name's DER, whole, as `X509Certificate.issuer` or `subject` holds it.
|
|
1456
|
+
* @param b The other Name's DER.
|
|
1457
|
+
* @returns Whether the two are byte for byte equal.
|
|
1418
1458
|
*/
|
|
1419
1459
|
export const x509NameEqual = (a: u8[], b: u8[]): boolean => x509BytesEqual(a, b)
|
|
1420
1460
|
|
|
@@ -1424,6 +1464,10 @@ export const x509NameEqual = (a: u8[], b: u8[]): boolean => x509BytesEqual(a, b)
|
|
|
1424
1464
|
* `name`'s RDN at the same place, byte for byte — how a directoryName
|
|
1425
1465
|
* constraint (RFC 5280 §4.2.1.10) is matched. An empty `base` is a prefix of
|
|
1426
1466
|
* every Name. `false` when either is not a Name.
|
|
1467
|
+
*
|
|
1468
|
+
* @param base The constraint's Name, whole DER.
|
|
1469
|
+
* @param name The Name tested against it, whole DER.
|
|
1470
|
+
* @returns Whether `base` is a prefix of `name`.
|
|
1427
1471
|
*/
|
|
1428
1472
|
export const x509NameIsPrefix = (base: u8[], name: u8[]): boolean => {
|
|
1429
1473
|
const b: X509DerElement | null = x509DerExpect(base, 0, toI32(base.length), X509_TAG_SEQUENCE)
|
|
@@ -1976,17 +2020,19 @@ const x509ReadExtensions = (der: u8[], e: X509DerElement, out: X509Certificate):
|
|
|
1976
2020
|
}
|
|
1977
2021
|
|
|
1978
2022
|
/**
|
|
1979
|
-
* `der` read as one Certificate (RFC 5280 §4.1), or `null` unless it is
|
|
1980
|
-
*
|
|
1981
|
-
*
|
|
1982
|
-
*
|
|
1983
|
-
*
|
|
1984
|
-
*
|
|
1985
|
-
*
|
|
1986
|
-
*
|
|
1987
|
-
*
|
|
1988
|
-
* and
|
|
1989
|
-
* `
|
|
2023
|
+
* `der` read as one Certificate (RFC 5280 §4.1), or `null` unless it is exactly
|
|
2024
|
+
* one in DER: `SEQUENCE { tbsCertificate, signatureAlgorithm, signatureValue
|
|
2025
|
+
* }`, where tbsCertificate holds an explicit version of v2 or v3 or none (DER
|
|
2026
|
+
* omits v1, the default), a minimal serial, an algorithm equal to the outer one
|
|
2027
|
+
* byte for byte (§4.1.1.2), issuer and subject Names, a Validity of two valid
|
|
2028
|
+
* times, a SubjectPublicKeyInfo whose key is whole octets, then only the
|
|
2029
|
+
* optional `[1]` and `[2]` unique identifiers (v2 and v3) and `[3]` extensions
|
|
2030
|
+
* (v3), in that order.
|
|
2031
|
+
*
|
|
2032
|
+
* Issuer and subject must be sound Names. The signature algorithm is named in
|
|
2033
|
+
* `sigAlg` (with `pssHash` and `pssSaltLen` for RSA-PSS) and the key in
|
|
2034
|
+
* `keyType`; `p256` and `ecdsaSha256` say whether they are the ones
|
|
2035
|
+
* `x509VerifySignature` checks.
|
|
1990
2036
|
*
|
|
1991
2037
|
* Extensions are read: subjectAltName, basicConstraints, keyUsage,
|
|
1992
2038
|
* extKeyUsage, nameConstraints and the two key identifiers are decoded into
|
|
@@ -1998,7 +2044,10 @@ const x509ReadExtensions = (der: u8[], e: X509DerElement, out: X509Certificate):
|
|
|
1998
2044
|
* and what its extensions say. It does not check the signature
|
|
1999
2045
|
* (`x509VerifySignature` does), that `notBefore` is not after `notAfter` or
|
|
2000
2046
|
* that either is near any clock, or what the extensions mean for a chain,
|
|
2001
|
-
* which is a path validator's to apply
|
|
2047
|
+
* which is a path validator's to apply: `nish/crypto/pki`'s `pkiVerifyServer`.
|
|
2048
|
+
*
|
|
2049
|
+
* @param der One certificate's DER, read and not kept.
|
|
2050
|
+
* @returns A new certificate whose byte fields are copies of `der`'s, or `null`.
|
|
2002
2051
|
*/
|
|
2003
2052
|
export const x509ParseCertificate = (der: u8[]): X509Certificate | null => {
|
|
2004
2053
|
const total: i32 = toI32(der.length)
|
|
@@ -2143,7 +2192,10 @@ export const x509ParseCertificate = (der: u8[]): X509Certificate | null => {
|
|
|
2143
2192
|
* clock, nor what each one's basicConstraints, keyUsage and name constraints
|
|
2144
2193
|
* allow, nor a trust anchor. Nothing here does RFC 5280 §6's path
|
|
2145
2194
|
* validation, and a chain this answers is no evidence that anyone
|
|
2146
|
-
* vouches for the leaf.
|
|
2195
|
+
* vouches for the leaf; `nish/crypto/pki`'s `pkiVerifyServer` is what does.
|
|
2196
|
+
*
|
|
2197
|
+
* @param pem The PEM text, such as a server's chain file.
|
|
2198
|
+
* @returns A fresh array of new certificates, or `null`.
|
|
2147
2199
|
*/
|
|
2148
2200
|
export const x509ParseChain = (pem: string): X509Certificate[] | null => {
|
|
2149
2201
|
const blocks: u8[][] | null = pemToDer(pem, "CERTIFICATE")
|
|
@@ -2169,6 +2221,10 @@ export const x509ParseChain = (pem: string): X509Certificate[] | null => {
|
|
|
2169
2221
|
* `issuer.subject` and that the times hold are fields the caller compares, and
|
|
2170
2222
|
* whether `issuer` may issue at all (`bcCa`, `keyUsage`) is a path
|
|
2171
2223
|
* validator's to decide, so a `true` here is not path validation.
|
|
2224
|
+
*
|
|
2225
|
+
* @param cert The certificate whose signature is checked.
|
|
2226
|
+
* @param issuer The certificate whose key is meant to have made it; `cert` itself for a self-signed one.
|
|
2227
|
+
* @returns Whether the signature verifies.
|
|
2172
2228
|
*/
|
|
2173
2229
|
export const x509VerifySignature = (cert: X509Certificate, issuer: X509Certificate): boolean => {
|
|
2174
2230
|
if (!cert.ecdsaSha256 || !issuer.p256) {
|
|
@@ -2211,14 +2267,22 @@ const x509MintExtensions = (): u8[] => [
|
|
|
2211
2267
|
* and issuer `CN=commonName`, valid from `notBeforeMs` (milliseconds since
|
|
2212
2268
|
* 1970, rounded down to the second) for exactly `days` days, serial number
|
|
2213
2269
|
* `serial`, carrying `x509MintExtensions`' basicConstraints and keyUsage, and
|
|
2214
|
-
* signed with ecdsa-with-SHA256 — see the module comment for why Chromium
|
|
2215
|
-
*
|
|
2216
|
-
*
|
|
2217
|
-
*
|
|
2218
|
-
*
|
|
2219
|
-
*
|
|
2220
|
-
*
|
|
2221
|
-
*
|
|
2270
|
+
* signed with ecdsa-with-SHA256 — see the module comment for why Chromium needs
|
|
2271
|
+
* an extension at all.
|
|
2272
|
+
*
|
|
2273
|
+
* `null` when `days` is below 1 or above `X509_MAX_DAYS`, `notBeforeMs` is
|
|
2274
|
+
* negative or the validity would end after 9999, `commonName` is empty, longer
|
|
2275
|
+
* than 64 bytes, not UTF-8 or holds a NUL (it is written as a UTF8String),
|
|
2276
|
+
* `priv` is not a P-256 private key, or `serial` — big-endian, leading zeros
|
|
2277
|
+
* ignored — is zero or needs more than `X509_MAX_SERIAL` octets as a positive
|
|
2278
|
+
* INTEGER (clear the top bit of 20 random bytes, or use fewer).
|
|
2279
|
+
*
|
|
2280
|
+
* @param priv The 32-byte P-256 private scalar, big-endian; read, and left for the caller to wipe.
|
|
2281
|
+
* @param commonName The subject's and issuer's common name, 1 to 64 bytes of UTF-8 without a NUL.
|
|
2282
|
+
* @param notBeforeMs When validity starts, in milliseconds since 1970-01-01 UTC.
|
|
2283
|
+
* @param days How long it is valid, 1 to `X509_MAX_DAYS`.
|
|
2284
|
+
* @param serial The serial number's octets, big-endian.
|
|
2285
|
+
* @returns The certificate's DER, a fresh array, or `null`.
|
|
2222
2286
|
*/
|
|
2223
2287
|
export const x509MintSelfSigned = (
|
|
2224
2288
|
priv: Secret<u8[]>,
|
|
@@ -2295,5 +2359,8 @@ export const x509MintSelfSigned = (
|
|
|
2295
2359
|
* The SHA-256 of a certificate's DER: the value WebTransport's
|
|
2296
2360
|
* `serverCertificateHashes` pins, and what `openssl x509 -outform DER |
|
|
2297
2361
|
* sha256sum` prints.
|
|
2362
|
+
*
|
|
2363
|
+
* @param der The certificate's DER, whole.
|
|
2364
|
+
* @returns The 32-byte digest, a fresh array.
|
|
2298
2365
|
*/
|
|
2299
2366
|
export const x509CertificateHash = (der: u8[]): u8[] => sha256(der)
|
package/std/host/ring.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `nish/host/ring` — a single-producer, single-consumer ring of `i32` words
|
|
3
|
+
* over module storage, shared by a wasm module and the page that drives it
|
|
4
|
+
* (`docs/wp50-wasm-host.md` §11).
|
|
5
|
+
*
|
|
6
|
+
* The ring is an `Int32Array` the program declares as module storage, so the
|
|
7
|
+
* page reaches the same words through the `Layout` the loader hands it:
|
|
8
|
+
*
|
|
9
|
+
* import { ringShift, ringSize } from "nish/host/ring";
|
|
10
|
+
*
|
|
11
|
+
* const INPUT = new Int32Array(2 + 64); // head, tail, then 64 slots
|
|
12
|
+
*
|
|
13
|
+
* export function tick(): void {
|
|
14
|
+
* while (ringSize(INPUT) > 0) {
|
|
15
|
+
* steer(ringShift(INPUT));
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* Word 0 is the **head** and word 1 the **tail**, each a count of words taken
|
|
20
|
+
* and put kept modulo twice the capacity (the length less two), so neither
|
|
21
|
+
* overflows and `tail - head` (modulo the same) is the number waiting: 0 is
|
|
22
|
+
* empty and the capacity is full. Slot `count % capacity` holds the word put
|
|
23
|
+
* at that count. One side only advances the tail and the other only the head:
|
|
24
|
+
* that is the whole protocol, and it needs no atomics because the page and the
|
|
25
|
+
* module run on one thread and never at once. `nish/page`'s `ringOver` is the
|
|
26
|
+
* page's half, over the same layout and arithmetic.
|
|
27
|
+
*
|
|
28
|
+
* A zero-filled ring (what module storage starts as) is empty. A ring whose
|
|
29
|
+
* counts a buggy page wrote out of protocol is still never read out of
|
|
30
|
+
* bounds: each count is reduced modulo twice the capacity before it is used,
|
|
31
|
+
* and a size past the capacity reads as full, so `ringPush` refuses and
|
|
32
|
+
* `ringShift` takes words until the counts agree again.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/** Head, tail: the two words in front of the slots. */
|
|
36
|
+
const RING_HEADER: i32 = 2
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The number of slots, the ring's length less its header.
|
|
40
|
+
*
|
|
41
|
+
* @param ring - the ring's storage: head, tail, then its slots
|
|
42
|
+
* @returns the slot count, or 0 for a ring too short to have one
|
|
43
|
+
*/
|
|
44
|
+
export const ringCapacity = (ring: Int32Array): i32 => {
|
|
45
|
+
const length: i32 = toI32(ring.length)
|
|
46
|
+
return length > RING_HEADER ? length - RING_HEADER : 0
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** `count` brought into `[0, 2 * capacity)`, whatever a page wrote there. */
|
|
50
|
+
const ringCount = (count: i32, capacity: i32): i32 => {
|
|
51
|
+
const m: i32 = capacity * 2
|
|
52
|
+
const r: i32 = count % m
|
|
53
|
+
return r < 0 ? r + m : r
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The number of words put and not yet taken.
|
|
58
|
+
*
|
|
59
|
+
* @param ring - the ring's storage: head, tail, then its slots
|
|
60
|
+
* @returns the words waiting, at most the capacity
|
|
61
|
+
*/
|
|
62
|
+
export const ringSize = (ring: Int32Array): i32 => {
|
|
63
|
+
const capacity: i32 = ringCapacity(ring)
|
|
64
|
+
if (capacity === 0) {
|
|
65
|
+
return 0
|
|
66
|
+
}
|
|
67
|
+
const size: i32 = ringCount(ringCount(ring[1], capacity) - ringCount(ring[0], capacity), capacity)
|
|
68
|
+
return size > capacity ? capacity : size
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The number of words `ringPush` would still take.
|
|
73
|
+
*
|
|
74
|
+
* @param ring - the ring's storage: head, tail, then its slots
|
|
75
|
+
* @returns the free slots
|
|
76
|
+
*/
|
|
77
|
+
export const ringFree = (ring: Int32Array): i32 => ringCapacity(ring) - ringSize(ring)
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Puts `word` at the tail, or changes nothing when the ring is full.
|
|
81
|
+
*
|
|
82
|
+
* @param ring - the ring's storage: head, tail, then its slots
|
|
83
|
+
* @param word - the word to put
|
|
84
|
+
* @returns true when the word was put, false when the ring was full
|
|
85
|
+
*/
|
|
86
|
+
export const ringPush = (ring: Int32Array, word: i32): boolean => {
|
|
87
|
+
const capacity: i32 = ringCapacity(ring)
|
|
88
|
+
if (capacity === 0 || ringSize(ring) >= capacity) {
|
|
89
|
+
return false
|
|
90
|
+
}
|
|
91
|
+
const tail: i32 = ringCount(ring[1], capacity)
|
|
92
|
+
ring[RING_HEADER + (tail % capacity)] = word
|
|
93
|
+
ring[1] = ringCount(tail + 1, capacity)
|
|
94
|
+
return true
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Takes the word at the head. On an empty ring it answers 0 and changes
|
|
99
|
+
* nothing, so a caller that cannot tell 0 from empty asks `ringSize` first.
|
|
100
|
+
*
|
|
101
|
+
* @param ring - the ring's storage: head, tail, then its slots
|
|
102
|
+
* @returns the word taken, or 0 on an empty ring
|
|
103
|
+
*/
|
|
104
|
+
export const ringShift = (ring: Int32Array): i32 => {
|
|
105
|
+
const capacity: i32 = ringCapacity(ring)
|
|
106
|
+
if (capacity === 0 || ringSize(ring) === 0) {
|
|
107
|
+
return 0
|
|
108
|
+
}
|
|
109
|
+
const head: i32 = ringCount(ring[0], capacity)
|
|
110
|
+
const word: i32 = ring[RING_HEADER + (head % capacity)]
|
|
111
|
+
ring[0] = ringCount(head + 1, capacity)
|
|
112
|
+
return word
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Empties the ring: the head catches up with the tail, and the slots keep their stale words.
|
|
117
|
+
*
|
|
118
|
+
* @param ring - the ring's storage: head, tail, then its slots
|
|
119
|
+
*/
|
|
120
|
+
export const ringClear = (ring: Int32Array): void => {
|
|
121
|
+
if (toI32(ring.length) >= RING_HEADER) {
|
|
122
|
+
ring[0] = ring[1]
|
|
123
|
+
}
|
|
124
|
+
}
|
package/std/json-equal.ts
CHANGED
|
@@ -25,17 +25,18 @@
|
|
|
25
25
|
* built with `push` and `set` deeper than any parser allows compares as well
|
|
26
26
|
* as a flat one, and nothing recurses.
|
|
27
27
|
*
|
|
28
|
-
* **What a call costs.** `jsonEqual` stops at the first difference. Up to
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* the first repeat and keeps
|
|
28
|
+
* **What a call costs.** `jsonEqual` stops at the first difference. Up to there
|
|
29
|
+
* it is linear in the values' size, except that each pair of objects reads
|
|
30
|
+
* every key (one string each) and sorts them, `n log n` for `n` members; it
|
|
31
|
+
* also allocates one string per string value, its stack, and whatever
|
|
32
|
+
* `jsonNumberCompare` allocates per number pair. `jsonEqualCanonical` builds
|
|
33
|
+
* the canonical text, about the size of the value written as JSON, and sorts
|
|
34
|
+
* keys the same way. `jsonEqualHash` is FNV-1a of that text's units: its bytes
|
|
35
|
+
* natively, its code units under Node.
|
|
36
|
+
*
|
|
37
|
+
* Every order and length below is in those units too. `jsonEqualFirstDuplicate`
|
|
38
|
+
* makes the canonical text of every element up to the first repeat and keeps
|
|
39
|
+
* them in a `Map`.
|
|
39
40
|
*
|
|
40
41
|
* **The hash is not keyed.** FNV-1a (public domain) is the same in every
|
|
41
42
|
* process, so an attacker can make many values that share a hash, and a
|
|
@@ -144,6 +145,12 @@ const jsonEqualNumbers = (a: string, b: string): boolean => {
|
|
|
144
145
|
* JSON Schema's rule (the module header): numbers by value, objects whatever
|
|
145
146
|
* their member order. The two may be one document. `false` when either
|
|
146
147
|
* handle names no value. Iterative, so any depth compares.
|
|
148
|
+
*
|
|
149
|
+
* @param da - the document that holds `a`.
|
|
150
|
+
* @param a - a value handle from `da`.
|
|
151
|
+
* @param db - the document that holds `b`, which may be `da` itself.
|
|
152
|
+
* @param b - a value handle from `db`.
|
|
153
|
+
* @returns true when the two values are equal as JSON Schema compares them.
|
|
147
154
|
*/
|
|
148
155
|
export const jsonEqual = (da: JsonDocument, a: i32, db: JsonDocument, b: i32): boolean => {
|
|
149
156
|
const left: i32[] = [a]
|
|
@@ -281,6 +288,11 @@ const jsonEqualCanonicalText = (doc: JsonDocument, v: i32): string => {
|
|
|
281
288
|
* members in key order, each key a length, `:` and the key (orders and lengths
|
|
282
289
|
* as the module header says).
|
|
283
290
|
* `""` for a handle that names no value.
|
|
291
|
+
*
|
|
292
|
+
* @param doc - the document that holds `v`.
|
|
293
|
+
* @param v - the value to write out.
|
|
294
|
+
* @returns the canonical text, which two values share exactly when `jsonEqual`
|
|
295
|
+
* calls them equal.
|
|
284
296
|
*/
|
|
285
297
|
export const jsonEqualCanonical = (doc: JsonDocument, v: i32): string => jsonEqualCanonicalText(doc, v)
|
|
286
298
|
|
|
@@ -290,6 +302,10 @@ export const jsonEqualCanonical = (doc: JsonDocument, v: i32): string => jsonEqu
|
|
|
290
302
|
* Node, so a string past U+00FF hashes differently there), unkeyed (the
|
|
291
303
|
* module header says what that means for a table). The offset basis for a handle that names no
|
|
292
304
|
* value.
|
|
305
|
+
*
|
|
306
|
+
* @param doc - the document that holds `v`.
|
|
307
|
+
* @param v - the value to hash.
|
|
308
|
+
* @returns the hash, the same for any two values `jsonEqual` calls equal.
|
|
293
309
|
*/
|
|
294
310
|
export const jsonEqualHash = (doc: JsonDocument, v: i32): u64 => {
|
|
295
311
|
const text: string = jsonEqualCanonicalText(doc, v)
|
|
@@ -317,6 +333,10 @@ export const jsonEqualHash = (doc: JsonDocument, v: i32): u64 => {
|
|
|
317
333
|
* earlier one, or `-1` when every element is distinct or `v` is not an array:
|
|
318
334
|
* JSON Schema's `uniqueItems` holds exactly when this is `-1`. Linear in the
|
|
319
335
|
* elements read, through a `Map` of their canonical texts.
|
|
336
|
+
*
|
|
337
|
+
* @param doc - the document that holds `v`.
|
|
338
|
+
* @param v - the array to search.
|
|
339
|
+
* @returns the index of the first repeated element, or `-1` when there is none.
|
|
320
340
|
*/
|
|
321
341
|
export const jsonEqualFirstDuplicate = (doc: JsonDocument, v: i32): i32 => {
|
|
322
342
|
if (doc.kind(v) !== JSON_VALUE_ARRAY) {
|