micro509 0.17.1 → 0.19.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/CHANGELOG.md +165 -6
- package/dist/crypto/crypto.d.ts +12 -4
- package/dist/crypto/crypto.js +30 -17
- package/dist/crypto/crypto.js.map +1 -1
- package/dist/crypto.d.ts +8 -2
- package/dist/der/der.d.ts +14 -4
- package/dist/der/der.js +19 -19
- package/dist/der/der.js.map +1 -1
- package/dist/der.d.ts +26 -2
- package/dist/der.js +4 -4
- package/dist/index.d.ts +84 -16
- package/dist/index.js +19 -19
- package/dist/internal/asn1/asn1.d.ts +1 -2
- package/dist/internal/asn1/asn1.js +10 -2
- package/dist/internal/asn1/asn1.js.map +1 -1
- package/dist/internal/asn1/ber.js.map +1 -1
- package/dist/internal/asn1/decode-refusal.js.map +1 -1
- package/dist/internal/asn1/der.d.ts +1 -2
- package/dist/internal/asn1/der.js.map +1 -1
- package/dist/internal/asn1/oids.js +71 -353
- package/dist/internal/asn1/oids.js.map +1 -1
- package/dist/internal/asn1/oids.json.js +357 -0
- package/dist/internal/asn1/oids.json.js.map +1 -0
- package/dist/internal/crypto/algorithm-names.js +1 -1
- package/dist/internal/crypto/algorithm-names.js.map +1 -1
- package/dist/internal/crypto/ecdsa.js.map +1 -1
- package/dist/internal/crypto/hash.js.map +1 -1
- package/dist/internal/crypto/pbes2.d.ts +1 -2
- package/dist/internal/crypto/pbes2.js +70 -70
- package/dist/internal/crypto/pbes2.js.map +1 -1
- package/dist/internal/crypto/prime-ec.js +37 -0
- package/dist/internal/crypto/prime-ec.js.map +1 -0
- package/dist/internal/crypto/rsa-pss.js +50 -50
- package/dist/internal/crypto/rsa-pss.js.map +1 -1
- package/dist/internal/crypto/sig-verify.d.ts +41 -2
- package/dist/internal/crypto/sig-verify.js +125 -38
- package/dist/internal/crypto/sig-verify.js.map +1 -1
- package/dist/internal/crypto/signing.d.ts +1 -2
- package/dist/internal/crypto/signing.js +26 -26
- package/dist/internal/crypto/signing.js.map +1 -1
- package/dist/internal/jose/base64url.js.map +1 -1
- package/dist/internal/jose/json-data.js +108 -0
- package/dist/internal/jose/json-data.js.map +1 -0
- package/dist/internal/jose/strict-json.d.ts +1 -2
- package/dist/internal/jose/strict-json.js.map +1 -1
- package/dist/internal/shared/base64.js.map +1 -1
- package/dist/internal/shared/dn.d.ts +1 -2
- package/dist/internal/shared/dn.js +1 -1
- package/dist/internal/shared/dn.js.map +1 -1
- package/dist/internal/shared/idna.js.map +1 -1
- package/dist/internal/shared/ip.d.ts +1 -2
- package/dist/internal/shared/ip.js.map +1 -1
- package/dist/internal/shared/mailbox.js.map +1 -1
- package/dist/internal/shared/punycode.js.map +1 -1
- package/dist/internal/shared/uri-host.js.map +1 -1
- package/dist/internal/verify/name-constraints-engine.js +64 -64
- package/dist/internal/verify/name-constraints-engine.js.map +1 -1
- package/dist/internal/verify/policy-engine.js +1 -1
- package/dist/internal/verify/policy-engine.js.map +1 -1
- package/dist/internal/verify/spki-distrust.js.map +1 -1
- package/dist/internal/verify/verify-path.d.ts +1 -2
- package/dist/internal/verify/verify-path.js +234 -128
- package/dist/internal/verify/verify-path.js.map +1 -1
- package/dist/internal/x509/directory-name.js +10 -10
- package/dist/internal/x509/directory-name.js.map +1 -1
- package/dist/internal/x509/extension-bits.js.map +1 -1
- package/dist/internal/x509/extension-errors.d.ts +1 -2
- package/dist/internal/x509/extension-errors.js.map +1 -1
- package/dist/internal/x509/extension-registry.js +18 -18
- package/dist/internal/x509/extension-registry.js.map +1 -1
- package/dist/internal/x509/general-name-profile.js +9 -9
- package/dist/internal/x509/general-name-profile.js.map +1 -1
- package/dist/internal/x509/general-name.js +32 -32
- package/dist/internal/x509/general-name.js.map +1 -1
- package/dist/internal/x509/name-fields.js +1 -1
- package/dist/internal/x509/name-fields.js.map +1 -1
- package/dist/internal/x509/or-address.js.map +1 -1
- package/dist/jose/base64url.d.ts +4 -0
- package/dist/jose/base64url.js +14 -0
- package/dist/jose/base64url.js.map +1 -0
- package/dist/jose/errors.d.ts +9 -3
- package/dist/jose/errors.js.map +1 -1
- package/dist/jose/jwe.d.ts +64 -0
- package/dist/jose/jwe.js +188 -0
- package/dist/jose/jwe.js.map +1 -0
- package/dist/jose/jwks.d.ts +38 -10
- package/dist/jose/jwks.js +156 -96
- package/dist/jose/jwks.js.map +1 -1
- package/dist/jose/jws.d.ts +8 -6
- package/dist/jose/jws.js +216 -170
- package/dist/jose/jws.js.map +1 -1
- package/dist/jose/jwt.d.ts +8 -2
- package/dist/jose/jwt.js +98 -98
- package/dist/jose/jwt.js.map +1 -1
- package/dist/jose/remote-jwks.d.ts +1 -2
- package/dist/jose/remote-jwks.js +106 -108
- package/dist/jose/remote-jwks.js.map +1 -1
- package/dist/jose.d.ts +15 -4
- package/dist/jose.js +7 -5
- package/dist/kdf/argon2-arithmetic.js.map +1 -1
- package/dist/kdf/argon2-core.js.map +1 -1
- package/dist/kdf/argon2-memory.js.map +1 -1
- package/dist/kdf/argon2-portable.js.map +1 -1
- package/dist/kdf/argon2.d.ts +7 -2
- package/dist/kdf/argon2.js.map +1 -1
- package/dist/kdf.d.ts +6 -0
- package/dist/keys/keys.d.ts +31 -6
- package/dist/keys/keys.js +315 -233
- package/dist/keys/keys.js.map +1 -1
- package/dist/keys.d.ts +11 -2
- package/dist/keys.js +2 -2
- package/dist/pem/pem.d.ts +1 -2
- package/dist/pem/pem.js +40 -25
- package/dist/pem/pem.js.map +1 -1
- package/dist/pem.d.ts +6 -0
- package/dist/pkcs/pfx.d.ts +2 -3
- package/dist/pkcs/pfx.js +142 -142
- package/dist/pkcs/pfx.js.map +1 -1
- package/dist/pkcs/pkcs12-mac.d.ts +1 -2
- package/dist/pkcs/pkcs12-mac.js +122 -122
- package/dist/pkcs/pkcs12-mac.js.map +1 -1
- package/dist/pkcs/pkcs7.d.ts +24 -3
- package/dist/pkcs/pkcs7.js +294 -256
- package/dist/pkcs/pkcs7.js.map +1 -1
- package/dist/pkcs.d.ts +10 -1
- package/dist/pkcs.js +4 -4
- package/dist/result/result.d.ts +1 -2
- package/dist/result/result.js.map +1 -1
- package/dist/result.d.ts +6 -0
- package/dist/revocation/chain.d.ts +69 -16
- package/dist/revocation/chain.js +431 -303
- package/dist/revocation/chain.js.map +1 -1
- package/dist/revocation/crl.d.ts +67 -14
- package/dist/revocation/crl.js +656 -366
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.d.ts +78 -24
- package/dist/revocation/ocsp.js +622 -436
- package/dist/revocation/ocsp.js.map +1 -1
- package/dist/revocation/revocation.d.ts +68 -14
- package/dist/revocation/revocation.js +195 -74
- package/dist/revocation/revocation.js.map +1 -1
- package/dist/revocation.d.ts +14 -5
- package/dist/revocation.js +4 -4
- package/dist/ssh/ssh.d.ts +2 -3
- package/dist/ssh/ssh.js +165 -170
- package/dist/ssh/ssh.js.map +1 -1
- package/dist/ssh/wire.d.ts +1 -2
- package/dist/ssh/wire.js +10 -10
- package/dist/ssh/wire.js.map +1 -1
- package/dist/ssh.d.ts +8 -0
- package/dist/verify/identity.d.ts +3 -4
- package/dist/verify/identity.js +33 -33
- package/dist/verify/identity.js.map +1 -1
- package/dist/verify/name-constraints.d.ts +9 -2
- package/dist/verify/policy.d.ts +9 -2
- package/dist/verify/verify.d.ts +85 -34
- package/dist/verify/verify.js +276 -226
- package/dist/verify/verify.js.map +1 -1
- package/dist/verify.d.ts +12 -2
- package/dist/x509/certificate.d.ts +4 -5
- package/dist/x509/certificate.js +40 -40
- package/dist/x509/certificate.js.map +1 -1
- package/dist/x509/csr.d.ts +3 -4
- package/dist/x509/csr.js +15 -15
- package/dist/x509/csr.js.map +1 -1
- package/dist/x509/extensions.d.ts +1 -2
- package/dist/x509/extensions.js +165 -165
- package/dist/x509/extensions.js.map +1 -1
- package/dist/x509/fingerprint.d.ts +1 -2
- package/dist/x509/fingerprint.js.map +1 -1
- package/dist/x509/name-text.d.ts +1 -2
- package/dist/x509/name-text.js.map +1 -1
- package/dist/x509/name.d.ts +1 -2
- package/dist/x509/name.js +15 -15
- package/dist/x509/name.js.map +1 -1
- package/dist/x509/parse.d.ts +9 -6
- package/dist/x509/parse.js +394 -394
- package/dist/x509/parse.js.map +1 -1
- package/dist/x509.d.ts +17 -4
- package/dist/x509.js +8 -8
- package/package.json +32 -33
- package/dist/internal/asn1/oids2.js +0 -75
- package/dist/internal/asn1/oids2.js.map +0 -1
package/dist/ssh/ssh.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { concatBytes as
|
|
3
|
-
import {
|
|
4
|
-
import { getCrypto as
|
|
5
|
-
import { base64Decode as
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
1
|
+
import { toArrayBuffer as e } from "../internal/asn1/asn1.js";
|
|
2
|
+
import { concatBytes as t } from "../internal/asn1/der.js";
|
|
3
|
+
import { PRIME_EC_CURVES as n } from "../internal/crypto/prime-ec.js";
|
|
4
|
+
import { getCrypto as r } from "../internal/crypto/webcrypto.js";
|
|
5
|
+
import { base64Decode as i, base64Encode as a } from "../internal/shared/base64.js";
|
|
6
|
+
import { exportPrivateJwk as o, exportPublicJwk as s, importPrivateJwkOrThrow as c, importPublicJwkOrThrow as l } from "../keys/keys.js";
|
|
7
|
+
import { pemDecodeOrThrow as u, pemEncode as d } from "../pem/pem.js";
|
|
8
|
+
import { failureResult as f, isResultError as p, rethrowIfInvariant as m, successResult as h, throwMicro509Error as g, unwrap as _ } from "../result/result.js";
|
|
9
|
+
import { asSshErrorCode as v, bigintToBytes as y, bytesEqual as b, bytesToBigint as x, createSshReader as S, decodeUtf8 as C, sshFail as w, sshMpint as T, sshString as E, sshTextString as D, sshUint32 as O } from "./wire.js";
|
|
9
10
|
// src/ssh/ssh.ts
|
|
10
11
|
/**
|
|
11
12
|
* OpenSSH public-key lines, unencrypted `openssh-key-v1` private keys, and
|
|
@@ -39,7 +40,7 @@ import { asSshErrorCode as _, bigintToBytes as v, bytesEqual as y, bytesToBigint
|
|
|
39
40
|
*
|
|
40
41
|
* @module
|
|
41
42
|
*/
|
|
42
|
-
const
|
|
43
|
+
const k = 32768, A = 2049, j = 8192, M = 16384, N = "OPENSSH PRIVATE KEY", P = Uint8Array.from("openssh-key-v1", (e) => e.charCodeAt(0)), F = [
|
|
43
44
|
"ssh-rsa",
|
|
44
45
|
"ssh-ed25519",
|
|
45
46
|
"ecdsa-sha2-nistp256",
|
|
@@ -47,49 +48,43 @@ const O = 32768, k = 2049, A = 8192, j = 16384, M = "OPENSSH PRIVATE KEY", N = U
|
|
|
47
48
|
"ecdsa-sha2-nistp521"
|
|
48
49
|
];
|
|
49
50
|
function asSshKeyType(e) {
|
|
50
|
-
return
|
|
51
|
+
return F.find((t) => t === e);
|
|
51
52
|
}
|
|
52
|
-
const
|
|
53
|
+
const I = {
|
|
53
54
|
"P-256": {
|
|
54
55
|
identifier: "nistp256",
|
|
55
|
-
size: 32,
|
|
56
56
|
hash: "SHA-256",
|
|
57
|
-
|
|
58
|
-
b: 41058363725152142129326129780047268409114441015993725554835256314039467401291n
|
|
57
|
+
...n["P-256"]
|
|
59
58
|
},
|
|
60
59
|
"P-384": {
|
|
61
60
|
identifier: "nistp384",
|
|
62
|
-
size: 48,
|
|
63
61
|
hash: "SHA-384",
|
|
64
|
-
|
|
65
|
-
b: 27580193559959705877849011840389048093056905856361568521428707301988689241309860865136260764883745107765439761230575n
|
|
62
|
+
...n["P-384"]
|
|
66
63
|
},
|
|
67
64
|
"P-521": {
|
|
68
65
|
identifier: "nistp521",
|
|
69
|
-
size: 66,
|
|
70
66
|
hash: "SHA-512",
|
|
71
|
-
|
|
72
|
-
b: 1093849038073734274511112390766805569936207598951683748994586394495953116150735016013708737573759623248592132296706313309438452531591012912142327488478985984n
|
|
67
|
+
...n["P-521"]
|
|
73
68
|
}
|
|
74
|
-
},
|
|
69
|
+
}, L = [
|
|
75
70
|
"P-256",
|
|
76
71
|
"P-384",
|
|
77
72
|
"P-521"
|
|
78
|
-
],
|
|
73
|
+
], R = {
|
|
79
74
|
"ecdsa-sha2-nistp256": "P-256",
|
|
80
75
|
"ecdsa-sha2-nistp384": "P-384",
|
|
81
76
|
"ecdsa-sha2-nistp521": "P-521"
|
|
82
77
|
};
|
|
83
78
|
function resolveImportOptions(e) {
|
|
84
|
-
if (typeof e != "object" || !e || Array.isArray(e)) return
|
|
79
|
+
if (typeof e != "object" || !e || Array.isArray(e)) return w("invalid_options", "SSH import options must be an object");
|
|
85
80
|
let t = e.minRsaModulusBits === void 0 ? 2048 : e.minRsaModulusBits;
|
|
86
|
-
if (!Number.isSafeInteger(t) || t < 1 || t >
|
|
81
|
+
if (!Number.isSafeInteger(t) || t < 1 || t > M) return w("invalid_options", "minRsaModulusBits must be an integer from 1 to 16384");
|
|
87
82
|
let n = e.acceptCompressedEcPoints !== void 0 && e.acceptCompressedEcPoints;
|
|
88
|
-
if (typeof n != "boolean") return
|
|
83
|
+
if (typeof n != "boolean") return w("invalid_options", "acceptCompressedEcPoints must be a boolean");
|
|
89
84
|
let r = e.rsa === void 0 ? {} : e.rsa;
|
|
90
|
-
if (typeof r != "object" || !r || Array.isArray(r)) return
|
|
85
|
+
if (typeof r != "object" || !r || Array.isArray(r)) return w("invalid_options", "rsa must be an object");
|
|
91
86
|
let i = r.scheme === void 0 ? "pkcs1-v1_5" : r.scheme, a = r.hash === void 0 ? "SHA-256" : r.hash;
|
|
92
|
-
return i !== "pkcs1-v1_5" && i !== "pss" ?
|
|
87
|
+
return i !== "pkcs1-v1_5" && i !== "pss" ? w("invalid_options", "Unsupported RSA signature scheme") : a !== "SHA-256" && a !== "SHA-384" && a !== "SHA-512" ? w("invalid_options", "Unsupported RSA signature hash") : {
|
|
93
88
|
minRsaModulusBits: t,
|
|
94
89
|
acceptCompressedEcPoints: n,
|
|
95
90
|
rsaScheme: i,
|
|
@@ -103,7 +98,7 @@ function keyTypeOf(e) {
|
|
|
103
98
|
switch (e.kind) {
|
|
104
99
|
case "rsa": return "ssh-rsa";
|
|
105
100
|
case "ed25519": return "ssh-ed25519";
|
|
106
|
-
case "ecdsa": return `ecdsa-sha2-${
|
|
101
|
+
case "ecdsa": return `ecdsa-sha2-${I[e.curve].identifier}`;
|
|
107
102
|
default: return unhandledKey(e);
|
|
108
103
|
}
|
|
109
104
|
}
|
|
@@ -125,11 +120,11 @@ function algorithmOf(e) {
|
|
|
125
120
|
}
|
|
126
121
|
}
|
|
127
122
|
function toBase64Url(e) {
|
|
128
|
-
return
|
|
123
|
+
return a(e).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
129
124
|
}
|
|
130
125
|
function fromBase64Url(e) {
|
|
131
126
|
let t = e.replace(/-/g, "+").replace(/_/g, "/");
|
|
132
|
-
return
|
|
127
|
+
return i(t.padEnd(Math.ceil(t.length / 4) * 4, "="));
|
|
133
128
|
}
|
|
134
129
|
function modPow(e, t, n) {
|
|
135
130
|
let r = 1n, i = e % n, a = t;
|
|
@@ -137,57 +132,57 @@ function modPow(e, t, n) {
|
|
|
137
132
|
return r;
|
|
138
133
|
}
|
|
139
134
|
function decompressPoint(e, t, n) {
|
|
140
|
-
let { prime: r, b: i, size: a } =
|
|
141
|
-
if (o >= r) return
|
|
135
|
+
let { prime: r, b: i, size: a } = I[e], o = x(n);
|
|
136
|
+
if (o >= r) return w("malformed", "ECDSA point coordinate is out of range");
|
|
142
137
|
let s = (((o * o - 3n) * o + i) % r + r) % r, c = modPow(s, (r + 1n) / 4n, r);
|
|
143
|
-
if (c * c % r !== s) return
|
|
138
|
+
if (c * c % r !== s) return w("malformed", "ECDSA point is not on the curve");
|
|
144
139
|
let l = t === 3, u = (c & 1n) == 1n === l ? c : r - c;
|
|
145
|
-
return
|
|
140
|
+
return y(u, a);
|
|
146
141
|
}
|
|
147
142
|
function readPoint(e, t, n) {
|
|
148
|
-
let { size: r } =
|
|
143
|
+
let { size: r } = I[e], i = t[0];
|
|
149
144
|
if (i === 4 && t.length === 1 + 2 * r) return {
|
|
150
145
|
x: t.subarray(1, 1 + r),
|
|
151
146
|
y: t.subarray(1 + r)
|
|
152
147
|
};
|
|
153
148
|
if ((i === 2 || i === 3) && t.length === 1 + r) {
|
|
154
|
-
if (!n.acceptCompressedEcPoints) return
|
|
149
|
+
if (!n.acceptCompressedEcPoints) return w("unsupported", "Compressed ECDSA points need acceptCompressedEcPoints");
|
|
155
150
|
let r = t.subarray(1);
|
|
156
151
|
return {
|
|
157
152
|
x: r,
|
|
158
153
|
y: decompressPoint(e, i, r)
|
|
159
154
|
};
|
|
160
155
|
}
|
|
161
|
-
return
|
|
156
|
+
return w("malformed", "ECDSA point encoding is invalid");
|
|
162
157
|
}
|
|
163
158
|
function readRsaPublic(e, t, n) {
|
|
164
|
-
let r = e.mpint(
|
|
165
|
-
if (s >
|
|
166
|
-
if (s < n.minRsaModulusBits) return
|
|
167
|
-
let c =
|
|
168
|
-
return c < 3n || (c & 1n) == 0n || c >=
|
|
159
|
+
let r = e.mpint(A), i = e.mpint(A), [a, o] = t === "e-n" ? [r, i] : [i, r], s = bitLength(o);
|
|
160
|
+
if (s > M) return w("limit_exceeded", `RSA modulus of ${s} bits exceeds ${M}`);
|
|
161
|
+
if (s < n.minRsaModulusBits) return w("unsupported", `RSA modulus of ${s} bits is below ${n.minRsaModulusBits}`);
|
|
162
|
+
let c = x(a);
|
|
163
|
+
return c < 3n || (c & 1n) == 0n || c >= x(o) ? w("malformed", "RSA public exponent is invalid") : {
|
|
169
164
|
kind: "rsa",
|
|
170
165
|
e: a,
|
|
171
166
|
n: o
|
|
172
167
|
};
|
|
173
168
|
}
|
|
174
169
|
function readEcdsaPublic(e, t, n) {
|
|
175
|
-
return e.name() ===
|
|
170
|
+
return e.name() === I[t].identifier ? {
|
|
176
171
|
kind: "ecdsa",
|
|
177
172
|
curve: t,
|
|
178
|
-
...readPoint(t, e.string(
|
|
179
|
-
} :
|
|
173
|
+
...readPoint(t, e.string(j), n)
|
|
174
|
+
} : w("key_mismatch", "ECDSA curve identifier disagrees with the key type");
|
|
180
175
|
}
|
|
181
176
|
function readEd25519Public(e) {
|
|
182
|
-
let t = e.string(
|
|
177
|
+
let t = e.string(j);
|
|
183
178
|
return t.length === 32 ? {
|
|
184
179
|
kind: "ed25519",
|
|
185
180
|
x: t
|
|
186
|
-
} :
|
|
181
|
+
} : w("malformed", "Ed25519 public key is not 32 bytes");
|
|
187
182
|
}
|
|
188
183
|
function decodePublicBlob(e, t) {
|
|
189
|
-
let n =
|
|
190
|
-
if (r === void 0) return
|
|
184
|
+
let n = S(e), r = asSshKeyType(n.name());
|
|
185
|
+
if (r === void 0) return w("unsupported", "Unsupported SSH key type");
|
|
191
186
|
let i;
|
|
192
187
|
switch (r) {
|
|
193
188
|
case "ssh-rsa":
|
|
@@ -199,38 +194,38 @@ function decodePublicBlob(e, t) {
|
|
|
199
194
|
case "ecdsa-sha2-nistp256":
|
|
200
195
|
case "ecdsa-sha2-nistp384":
|
|
201
196
|
case "ecdsa-sha2-nistp521":
|
|
202
|
-
i = readEcdsaPublic(n,
|
|
197
|
+
i = readEcdsaPublic(n, R[r], t);
|
|
203
198
|
break;
|
|
204
199
|
default: return unhandledKey(r);
|
|
205
200
|
}
|
|
206
|
-
return n.remaining() === 0 ? i :
|
|
201
|
+
return n.remaining() === 0 ? i : w("malformed", "SSH public key has trailing data");
|
|
207
202
|
}
|
|
208
203
|
function ecPoint(e) {
|
|
209
|
-
return
|
|
204
|
+
return t([
|
|
210
205
|
Uint8Array.of(4),
|
|
211
206
|
e.x,
|
|
212
207
|
e.y
|
|
213
208
|
]);
|
|
214
209
|
}
|
|
215
210
|
function encodePublicBlob(e) {
|
|
216
|
-
let
|
|
211
|
+
let n = D(keyTypeOf(e));
|
|
217
212
|
switch (e.kind) {
|
|
218
|
-
case "rsa": return
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
213
|
+
case "rsa": return t([
|
|
214
|
+
n,
|
|
215
|
+
T(e.e),
|
|
216
|
+
T(e.n)
|
|
222
217
|
]);
|
|
223
|
-
case "ed25519": return
|
|
224
|
-
case "ecdsa": return
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
218
|
+
case "ed25519": return t([n, E(e.x)]);
|
|
219
|
+
case "ecdsa": return t([
|
|
220
|
+
n,
|
|
221
|
+
D(I[e.curve].identifier),
|
|
222
|
+
E(ecPoint(e))
|
|
228
223
|
]);
|
|
229
224
|
default: return unhandledKey(e);
|
|
230
225
|
}
|
|
231
226
|
}
|
|
232
227
|
function samePublicMaterial(e, t) {
|
|
233
|
-
return
|
|
228
|
+
return b(encodePublicBlob(e), encodePublicBlob(t));
|
|
234
229
|
}
|
|
235
230
|
function publicJwk(e) {
|
|
236
231
|
switch (e.kind) {
|
|
@@ -256,20 +251,20 @@ function publicJwk(e) {
|
|
|
256
251
|
function privateJwk(e) {
|
|
257
252
|
switch (e.kind) {
|
|
258
253
|
case "rsa": {
|
|
259
|
-
let t =
|
|
254
|
+
let t = x(e.d), n = x(e.p), r = x(e.q);
|
|
260
255
|
return {
|
|
261
256
|
...publicJwk(e.pub),
|
|
262
257
|
d: toBase64Url(e.d),
|
|
263
258
|
p: toBase64Url(e.p),
|
|
264
259
|
q: toBase64Url(e.q),
|
|
265
|
-
dp: toBase64Url(
|
|
266
|
-
dq: toBase64Url(
|
|
260
|
+
dp: toBase64Url(y(t % (n - 1n))),
|
|
261
|
+
dq: toBase64Url(y(t % (r - 1n))),
|
|
267
262
|
qi: toBase64Url(e.iqmp)
|
|
268
263
|
};
|
|
269
264
|
}
|
|
270
265
|
case "ecdsa": return {
|
|
271
266
|
...publicJwk(e.pub),
|
|
272
|
-
d: toBase64Url(
|
|
267
|
+
d: toBase64Url(y(x(e.d), I[e.pub.curve].size))
|
|
273
268
|
};
|
|
274
269
|
case "ed25519": return {
|
|
275
270
|
...publicJwk(e.pub),
|
|
@@ -301,76 +296,76 @@ function signParams(e, t) {
|
|
|
301
296
|
} : { name: "RSASSA-PKCS1-v1_5" };
|
|
302
297
|
case "ecdsa": return {
|
|
303
298
|
name: "ECDSA",
|
|
304
|
-
hash:
|
|
299
|
+
hash: I[e.curve].hash
|
|
305
300
|
};
|
|
306
301
|
case "ed25519": return { name: "Ed25519" };
|
|
307
302
|
default: return unhandledKey(e);
|
|
308
303
|
}
|
|
309
304
|
}
|
|
310
305
|
async function importPublicMaterial(e, t) {
|
|
311
|
-
return await
|
|
306
|
+
return await l(publicJwk(e), importInputFor(e, t));
|
|
312
307
|
}
|
|
313
|
-
async function importPrivateMaterial(e, t,
|
|
308
|
+
async function importPrivateMaterial(e, t, n) {
|
|
314
309
|
try {
|
|
315
|
-
return await
|
|
310
|
+
return await c(privateJwk(e), importInputFor(t, n));
|
|
316
311
|
} catch (e) {
|
|
317
|
-
return
|
|
312
|
+
return m(e), w("key_mismatch", "Private key material is invalid for the embedded public key");
|
|
318
313
|
}
|
|
319
314
|
}
|
|
320
|
-
async function assertKeyPairConsistent(e, t, n,
|
|
321
|
-
let { subtle:
|
|
322
|
-
await
|
|
315
|
+
async function assertKeyPairConsistent(e, t, n, i) {
|
|
316
|
+
let { subtle: a } = r(), o = r().getRandomValues(/* @__PURE__ */ new Uint8Array(32)), s = signParams(n, i), c = await a.sign(s, e, o);
|
|
317
|
+
await a.verify(s, t, c, o) || w("key_mismatch", "Private key does not match the embedded public key");
|
|
323
318
|
}
|
|
324
|
-
async function sha256Fingerprint(
|
|
325
|
-
let
|
|
326
|
-
return `SHA256:${
|
|
319
|
+
async function sha256Fingerprint(t) {
|
|
320
|
+
let n = new Uint8Array(await r().subtle.digest("SHA-256", e(t)));
|
|
321
|
+
return `SHA256:${a(n).replace(/=+$/, "")}`;
|
|
327
322
|
}
|
|
328
323
|
function textOf(e, t) {
|
|
329
|
-
return typeof e == "string" ? e.length > t || new TextEncoder().encode(e).length > t ?
|
|
324
|
+
return typeof e == "string" ? e.length > t || new TextEncoder().encode(e).length > t ? w("limit_exceeded", `SSH key input exceeds ${t} bytes`) : e : e.length > t ? w("limit_exceeded", `SSH key input exceeds ${t} bytes`) : C(e);
|
|
330
325
|
}
|
|
331
|
-
const
|
|
326
|
+
const z = /^([^ \t]+)[ \t]+([A-Za-z0-9+/]+={0,2})(?:[ \t]+(.*))?$/s;
|
|
332
327
|
async function decodePublicKeyLine(e, t) {
|
|
333
328
|
let n = textOf(e, 16384).replace(/\r?\n$/, "");
|
|
334
|
-
if (/[\r\n]/.test(n)) return
|
|
335
|
-
let r =
|
|
336
|
-
if (r === null) return
|
|
337
|
-
let [,
|
|
338
|
-
assertImportedComment(
|
|
339
|
-
let
|
|
340
|
-
if (
|
|
341
|
-
let
|
|
342
|
-
return keyTypeOf(
|
|
343
|
-
keyType:
|
|
344
|
-
algorithm: algorithmOf(
|
|
345
|
-
publicKey: await importPublicMaterial(
|
|
346
|
-
comment:
|
|
347
|
-
fingerprint: await sha256Fingerprint(encodePublicBlob(
|
|
348
|
-
} :
|
|
329
|
+
if (/[\r\n]/.test(n)) return w("malformed", "SSH public key must be a single line");
|
|
330
|
+
let r = z.exec(n);
|
|
331
|
+
if (r === null) return w("malformed", "SSH public key line is not \"<type> <base64> [comment]\"");
|
|
332
|
+
let [, a = "", o = "", s = ""] = r;
|
|
333
|
+
assertImportedComment(s);
|
|
334
|
+
let c = asSshKeyType(a);
|
|
335
|
+
if (c === void 0) return w("unsupported", "Unsupported SSH key type");
|
|
336
|
+
let l = decodePublicBlob(i(o), t);
|
|
337
|
+
return keyTypeOf(l) === c ? {
|
|
338
|
+
keyType: c,
|
|
339
|
+
algorithm: algorithmOf(l),
|
|
340
|
+
publicKey: await importPublicMaterial(l, t),
|
|
341
|
+
comment: s,
|
|
342
|
+
fingerprint: await sha256Fingerprint(encodePublicBlob(l))
|
|
343
|
+
} : w("key_mismatch", "Line key type disagrees with the key blob");
|
|
349
344
|
}
|
|
350
345
|
function readPrivateMaterial(e, t, n) {
|
|
351
|
-
if (e.name() !== t) return
|
|
346
|
+
if (e.name() !== t) return w("key_mismatch", "Private section key type disagrees with the public key");
|
|
352
347
|
switch (t) {
|
|
353
348
|
case "ssh-rsa": return {
|
|
354
349
|
kind: "rsa",
|
|
355
350
|
pub: readRsaPublic(e, "n-e", n),
|
|
356
|
-
d: e.mpint(
|
|
357
|
-
iqmp: e.mpint(
|
|
358
|
-
p: e.mpint(
|
|
359
|
-
q: e.mpint(
|
|
351
|
+
d: e.mpint(A),
|
|
352
|
+
iqmp: e.mpint(A),
|
|
353
|
+
p: e.mpint(A),
|
|
354
|
+
q: e.mpint(A)
|
|
360
355
|
};
|
|
361
356
|
case "ssh-ed25519": {
|
|
362
|
-
let t = readEd25519Public(e), n = e.string(
|
|
363
|
-
return n.length === 64 ?
|
|
357
|
+
let t = readEd25519Public(e), n = e.string(j);
|
|
358
|
+
return n.length === 64 ? b(n.subarray(32), t.x) ? {
|
|
364
359
|
kind: "ed25519",
|
|
365
360
|
pub: t,
|
|
366
361
|
seed: n.subarray(0, 32)
|
|
367
|
-
} :
|
|
362
|
+
} : w("key_mismatch", "Ed25519 private key suffix differs from the public key") : w("malformed", "Ed25519 private key is not 64 bytes");
|
|
368
363
|
}
|
|
369
364
|
case "ecdsa-sha2-nistp256":
|
|
370
365
|
case "ecdsa-sha2-nistp384":
|
|
371
366
|
case "ecdsa-sha2-nistp521": {
|
|
372
|
-
let r = readEcdsaPublic(e,
|
|
373
|
-
return i.length >
|
|
367
|
+
let r = readEcdsaPublic(e, R[t], n), i = e.mpint(A);
|
|
368
|
+
return i.length > I[r.curve].size ? w("malformed", "ECDSA private scalar is larger than the curve order") : {
|
|
374
369
|
kind: "ecdsa",
|
|
375
370
|
pub: r,
|
|
376
371
|
d: i
|
|
@@ -380,17 +375,17 @@ function readPrivateMaterial(e, t, n) {
|
|
|
380
375
|
}
|
|
381
376
|
}
|
|
382
377
|
function assertRsaConsistent(e) {
|
|
383
|
-
let t =
|
|
384
|
-
(i < 3n || a < 3n || i * a !== t) &&
|
|
378
|
+
let t = x(e.pub.n), n = x(e.pub.e), r = x(e.d), i = x(e.p), a = x(e.q), o = x(e.iqmp);
|
|
379
|
+
(i < 3n || a < 3n || i * a !== t) && w("key_mismatch", "RSA primes do not multiply to the modulus"), (r >= t || r * n % (i - 1n) != 1n || r * n % (a - 1n) != 1n) && w("key_mismatch", "RSA private exponent does not match the public exponent"), (o >= i || o * a % i !== 1n) && w("key_mismatch", "RSA coefficient is not the inverse of q modulo p");
|
|
385
380
|
}
|
|
386
381
|
function assertPadding(e) {
|
|
387
382
|
let t = e.raw(e.remaining());
|
|
388
|
-
t.length >= 8 &&
|
|
389
|
-
e !== t + 1 &&
|
|
383
|
+
t.length >= 8 && w("malformed", "OpenSSH private key padding is too long"), t.forEach((e, t) => {
|
|
384
|
+
e !== t + 1 && w("malformed", "OpenSSH private key padding is invalid");
|
|
390
385
|
});
|
|
391
386
|
}
|
|
392
387
|
async function decodePrivateKey(e, t) {
|
|
393
|
-
let n = textOf(e,
|
|
388
|
+
let n = textOf(e, k), r = u(N, n);
|
|
394
389
|
try {
|
|
395
390
|
return await decodePrivateBinary(r, t);
|
|
396
391
|
} finally {
|
|
@@ -398,21 +393,21 @@ async function decodePrivateKey(e, t) {
|
|
|
398
393
|
}
|
|
399
394
|
}
|
|
400
395
|
async function decodePrivateBinary(e, t) {
|
|
401
|
-
let n =
|
|
402
|
-
if (!
|
|
396
|
+
let n = S(e), r = n.raw(P.length + 1);
|
|
397
|
+
if (!b(r.subarray(0, P.length), P) || r[P.length] !== 0) return w("malformed", "Missing openssh-key-v1 magic");
|
|
403
398
|
let i = n.name(), a = n.name();
|
|
404
|
-
if (i !== "none" || a !== "none") return
|
|
405
|
-
if (n.string(
|
|
399
|
+
if (i !== "none" || a !== "none") return w("unsupported", "Encrypted OpenSSH private keys are not supported");
|
|
400
|
+
if (n.string(j).length !== 0) return w("malformed", "KDF options must be empty for kdf none");
|
|
406
401
|
let o = n.uint32();
|
|
407
|
-
if (o === 0) return
|
|
408
|
-
if (o > 1) return
|
|
409
|
-
let s = decodePublicBlob(n.string(
|
|
410
|
-
if (n.remaining() !== 0) return
|
|
411
|
-
if (c.length === 0 || c.length % 8 != 0) return
|
|
412
|
-
let l =
|
|
413
|
-
if (l.uint32() !== l.uint32()) return
|
|
414
|
-
let u = readPrivateMaterial(l, keyTypeOf(s), t), d = l.text(
|
|
415
|
-
if (assertImportedComment(d), assertPadding(l), !samePublicMaterial(s, u.pub)) return
|
|
402
|
+
if (o === 0) return w("malformed", "OpenSSH private key file holds no keys");
|
|
403
|
+
if (o > 1) return w("unsupported", "OpenSSH private key files with several keys are not supported");
|
|
404
|
+
let s = decodePublicBlob(n.string(j), t), c = n.string(k);
|
|
405
|
+
if (n.remaining() !== 0) return w("malformed", "OpenSSH private key file has trailing data");
|
|
406
|
+
if (c.length === 0 || c.length % 8 != 0) return w("malformed", "OpenSSH private section is not a multiple of the block size");
|
|
407
|
+
let l = S(c);
|
|
408
|
+
if (l.uint32() !== l.uint32()) return w("malformed", "OpenSSH check integers differ");
|
|
409
|
+
let u = readPrivateMaterial(l, keyTypeOf(s), t), d = l.text(j);
|
|
410
|
+
if (assertImportedComment(d), assertPadding(l), !samePublicMaterial(s, u.pub)) return w("key_mismatch", "Private section public key differs from the file public key");
|
|
416
411
|
u.kind === "rsa" && assertRsaConsistent(u);
|
|
417
412
|
let f = await importPublicMaterial(s, t), p = await importPrivateMaterial(u, s, t);
|
|
418
413
|
return await assertKeyPairConsistent(p, f, s, t), {
|
|
@@ -424,15 +419,15 @@ async function decodePrivateBinary(e, t) {
|
|
|
424
419
|
fingerprint: await sha256Fingerprint(encodePublicBlob(s))
|
|
425
420
|
};
|
|
426
421
|
}
|
|
427
|
-
async function toSshResult(
|
|
422
|
+
async function toSshResult(e) {
|
|
428
423
|
try {
|
|
429
|
-
return
|
|
430
|
-
} catch (
|
|
431
|
-
if (
|
|
432
|
-
let t =
|
|
433
|
-
if (t !== void 0) return
|
|
424
|
+
return h(await e());
|
|
425
|
+
} catch (e) {
|
|
426
|
+
if (p(e)) {
|
|
427
|
+
let t = v(e.code);
|
|
428
|
+
if (t !== void 0) return f(t, e.error.message);
|
|
434
429
|
}
|
|
435
|
-
return
|
|
430
|
+
return m(e), f("malformed", e instanceof Error ? e.message : "Malformed SSH key");
|
|
436
431
|
}
|
|
437
432
|
}
|
|
438
433
|
/**
|
|
@@ -465,7 +460,7 @@ function importSshPublicKey(e, t = {}) {
|
|
|
465
460
|
* @throws {ResultError} Carrying an {@linkcode SshErrorCode} as `code`
|
|
466
461
|
*/
|
|
467
462
|
async function importSshPublicKeyOrThrow(e, t = {}) {
|
|
468
|
-
return
|
|
463
|
+
return _(await importSshPublicKey(e, t));
|
|
469
464
|
}
|
|
470
465
|
/**
|
|
471
466
|
* Import an unencrypted single-key `openssh-key-v1` private key file.
|
|
@@ -505,19 +500,19 @@ function importOpenSshPrivateKey(e, t = {}) {
|
|
|
505
500
|
* @throws {ResultError} Carrying an {@linkcode SshErrorCode} as `code`
|
|
506
501
|
*/
|
|
507
502
|
async function importOpenSshPrivateKeyOrThrow(e, t = {}) {
|
|
508
|
-
return
|
|
503
|
+
return _(await importOpenSshPrivateKey(e, t));
|
|
509
504
|
}
|
|
510
505
|
function exportFail(e, t) {
|
|
511
|
-
return
|
|
506
|
+
return g(e, t);
|
|
512
507
|
}
|
|
513
508
|
function validCommentText(e) {
|
|
514
509
|
return typeof e == "string" && e.isWellFormed() && !/[\r\n\0]/.test(e);
|
|
515
510
|
}
|
|
516
511
|
function oversizedComment(e) {
|
|
517
|
-
return e.length >
|
|
512
|
+
return e.length > j || new TextEncoder().encode(e).length > j;
|
|
518
513
|
}
|
|
519
514
|
function assertImportedComment(e) {
|
|
520
|
-
validCommentText(e) ||
|
|
515
|
+
validCommentText(e) || w("malformed", "Comment must be well-formed Unicode without CR, LF or NUL"), oversizedComment(e) && w("limit_exceeded", "Comment exceeds 8192 UTF-8 bytes");
|
|
521
516
|
}
|
|
522
517
|
function assertComment(e) {
|
|
523
518
|
(!validCommentText(e) || oversizedComment(e)) && exportFail("invalid_comment", "Comment must be well-formed Unicode without CR, LF or NUL and at most 8192 UTF-8 bytes");
|
|
@@ -541,7 +536,7 @@ function publicMaterialFromJwk(e, t) {
|
|
|
541
536
|
x: jwkBytes(e, "x")
|
|
542
537
|
};
|
|
543
538
|
if (e.kty === "EC" && t === "ECDSA") {
|
|
544
|
-
let t =
|
|
539
|
+
let t = L.find((t) => t === e.crv);
|
|
545
540
|
if (t !== void 0) return {
|
|
546
541
|
kind: "ecdsa",
|
|
547
542
|
curve: t,
|
|
@@ -566,7 +561,7 @@ function publicMaterialFromJwk(e, t) {
|
|
|
566
561
|
async function exportSshPublicKey(e, t = {}) {
|
|
567
562
|
let n = t.comment === void 0 ? "" : t.comment;
|
|
568
563
|
assertComment(n), assertExportable(e, "public");
|
|
569
|
-
let r = publicMaterialFromJwk(await
|
|
564
|
+
let r = publicMaterialFromJwk(await s(e), e.algorithm.name), i = `${keyTypeOf(r)} ${a(encodePublicBlob(r))}`;
|
|
570
565
|
return n === "" ? i : `${i} ${n}`;
|
|
571
566
|
}
|
|
572
567
|
/**
|
|
@@ -585,47 +580,47 @@ async function exportSshPublicKey(e, t = {}) {
|
|
|
585
580
|
*
|
|
586
581
|
* @see {@linkcode importOpenSshPrivateKey} for the inverse operation
|
|
587
582
|
*/
|
|
588
|
-
async function exportOpenSshPrivateKey(e,
|
|
589
|
-
let
|
|
590
|
-
assertComment(
|
|
591
|
-
let
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
]),
|
|
598
|
-
|
|
583
|
+
async function exportOpenSshPrivateKey(e, n = {}) {
|
|
584
|
+
let i = n.comment === void 0 ? "" : n.comment;
|
|
585
|
+
assertComment(i), assertExportable(e, "private");
|
|
586
|
+
let a = await o(e), s = publicMaterialFromJwk(a, e.algorithm.name), c = privateFields(s, a), l = r().getRandomValues(/* @__PURE__ */ new Uint32Array(1))[0] ?? 0, u = t([
|
|
587
|
+
O(l),
|
|
588
|
+
O(l),
|
|
589
|
+
D(keyTypeOf(s)),
|
|
590
|
+
c,
|
|
591
|
+
D(i)
|
|
592
|
+
]), f = (8 - u.length % 8) % 8, p = t([u, Uint8Array.from({ length: f }, (e, t) => t + 1)]), m = t([
|
|
593
|
+
P,
|
|
599
594
|
Uint8Array.of(0),
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
595
|
+
D("none"),
|
|
596
|
+
D("none"),
|
|
597
|
+
O(0),
|
|
598
|
+
O(1),
|
|
599
|
+
E(encodePublicBlob(s)),
|
|
600
|
+
E(p)
|
|
606
601
|
]);
|
|
607
602
|
try {
|
|
608
|
-
return
|
|
603
|
+
return d(N, m);
|
|
609
604
|
} finally {
|
|
610
|
-
|
|
605
|
+
u.fill(0), p.fill(0), m.fill(0), c.fill(0);
|
|
611
606
|
}
|
|
612
607
|
}
|
|
613
|
-
function privateFields(e,
|
|
608
|
+
function privateFields(e, n) {
|
|
614
609
|
switch (e.kind) {
|
|
615
|
-
case "rsa": return
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
610
|
+
case "rsa": return t([
|
|
611
|
+
T(e.n),
|
|
612
|
+
T(e.e),
|
|
613
|
+
T(jwkBytes(n, "d")),
|
|
614
|
+
T(jwkBytes(n, "qi")),
|
|
615
|
+
T(jwkBytes(n, "p")),
|
|
616
|
+
T(jwkBytes(n, "q"))
|
|
622
617
|
]);
|
|
623
|
-
case "ecdsa": return
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
618
|
+
case "ecdsa": return t([
|
|
619
|
+
D(I[e.curve].identifier),
|
|
620
|
+
E(ecPoint(e)),
|
|
621
|
+
T(jwkBytes(n, "d"))
|
|
627
622
|
]);
|
|
628
|
-
case "ed25519": return
|
|
623
|
+
case "ed25519": return t([E(e.x), E(t([jwkBytes(n, "d"), e.x]))]);
|
|
629
624
|
default: return unhandledKey(e);
|
|
630
625
|
}
|
|
631
626
|
}
|
|
@@ -641,7 +636,7 @@ function privateFields(e, t) {
|
|
|
641
636
|
* @throws {ResultError} Carrying an {@linkcode SshExportErrorCode} when a `CryptoKey` cannot be encoded
|
|
642
637
|
*/
|
|
643
638
|
async function sshFingerprintSha256(e) {
|
|
644
|
-
return e instanceof Uint8Array ? await sha256Fingerprint(e) : (e.type === "secret" && exportFail("invalid_key", "Expected a public or private key"), assertExportable(e, e.type), await sha256Fingerprint(encodePublicBlob(publicMaterialFromJwk(e.type === "private" ? await
|
|
639
|
+
return e instanceof Uint8Array ? await sha256Fingerprint(e) : (e.type === "secret" && exportFail("invalid_key", "Expected a public or private key"), assertExportable(e, e.type), await sha256Fingerprint(encodePublicBlob(publicMaterialFromJwk(e.type === "private" ? await o(e) : await s(e), e.algorithm.name))));
|
|
645
640
|
}
|
|
646
641
|
export { exportOpenSshPrivateKey, exportSshPublicKey, importOpenSshPrivateKey, importOpenSshPrivateKeyOrThrow, importSshPublicKey, importSshPublicKeyOrThrow, sshFingerprintSha256 };
|
|
647
642
|
|