micro509 0.16.0 → 0.17.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 +75 -1
- package/dist/internal/jose/base64url.js +40 -0
- package/dist/internal/jose/base64url.js.map +1 -0
- package/dist/internal/jose/strict-json.d.ts +9 -0
- package/dist/internal/jose/strict-json.js +155 -0
- package/dist/internal/jose/strict-json.js.map +1 -0
- package/dist/jose/errors.d.ts +44 -0
- package/dist/jose/errors.js +11 -0
- package/dist/jose/errors.js.map +1 -0
- package/dist/jose/jwks.d.ts +93 -0
- package/dist/jose/jwks.js +193 -0
- package/dist/jose/jwks.js.map +1 -0
- package/dist/jose/jws.d.ts +158 -0
- package/dist/jose/jws.js +362 -0
- package/dist/jose/jws.js.map +1 -0
- package/dist/jose/jwt.d.ts +91 -0
- package/dist/jose/jwt.js +187 -0
- package/dist/jose/jwt.js.map +1 -0
- package/dist/jose/remote-jwks.d.ts +95 -0
- package/dist/jose/remote-jwks.js +298 -0
- package/dist/jose/remote-jwks.js.map +1 -0
- package/dist/jose.d.ts +7 -0
- package/dist/jose.js +5 -0
- package/dist/kdf/argon2.d.ts +78 -0
- package/dist/kdf/argon2.js +167 -0
- package/dist/kdf/argon2.js.map +1 -0
- package/dist/kdf.d.ts +2 -0
- package/dist/kdf.js +1 -0
- package/dist/ssh/ssh.d.ts +200 -0
- package/dist/ssh/ssh.js +648 -0
- package/dist/ssh/ssh.js.map +1 -0
- package/dist/ssh/wire.d.ts +3 -0
- package/dist/ssh/wire.js +105 -0
- package/dist/ssh/wire.js.map +1 -0
- package/dist/ssh.d.ts +3 -0
- package/dist/ssh.js +1 -0
- package/package.json +18 -4
package/dist/ssh/ssh.js
ADDED
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
import { failureResult as e, isResultError as t, rethrowIfInvariant as n, successResult as r, throwMicro509Error as i, unwrap as a } from "../result/result.js";
|
|
2
|
+
import { concatBytes as o } from "../internal/asn1/der.js";
|
|
3
|
+
import { toArrayBuffer as s } from "../internal/asn1/asn1.js";
|
|
4
|
+
import { getCrypto as c } from "../internal/crypto/webcrypto.js";
|
|
5
|
+
import { base64Decode as l, base64Encode as u } from "../internal/shared/base64.js";
|
|
6
|
+
import { pemDecodeOrThrow as d, pemEncode as f } from "../pem/pem.js";
|
|
7
|
+
import { exportPrivateJwk as p, exportPublicJwk as m, importPrivateJwkOrThrow as h, importPublicJwkOrThrow as g } from "../keys/keys.js";
|
|
8
|
+
import { asSshErrorCode as _, bigintToBytes as v, bytesEqual as y, bytesToBigint as b, createSshReader as x, decodeUtf8 as S, sshFail as C, sshMpint as w, sshString as T, sshTextString as E, sshUint32 as D } from "./wire.js";
|
|
9
|
+
// src/ssh/ssh.ts
|
|
10
|
+
/**
|
|
11
|
+
* OpenSSH public-key lines, unencrypted `openssh-key-v1` private keys, and
|
|
12
|
+
* `SHA256:` fingerprints for RSA, Ed25519, and ECDSA on P-256, P-384 and P-521.
|
|
13
|
+
*
|
|
14
|
+
* Keys move through the existing WebCrypto, JWK and PKCS#8/SPKI APIs of
|
|
15
|
+
* `micro509/keys`. Nothing here opens a connection, speaks the SSH transport,
|
|
16
|
+
* reads SSH certificates, evaluates `authorized_keys` options, or decrypts a
|
|
17
|
+
* private key.
|
|
18
|
+
*
|
|
19
|
+
* The `ssh-rsa` key type names the RSA public-key encoding (RFC 4253 §6.6). It
|
|
20
|
+
* does not select a signature hash: RFC 8332 §3 keeps the `ssh-rsa` key format
|
|
21
|
+
* for the `rsa-sha2-256` and `rsa-sha2-512` signature algorithms. Importing an
|
|
22
|
+
* `ssh-rsa` key enables no SHA-1 signature.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { generateKeyPair, unwrap } from 'micro509';
|
|
27
|
+
* import {
|
|
28
|
+
* exportOpenSshPrivateKey,
|
|
29
|
+
* exportSshPublicKey,
|
|
30
|
+
* importOpenSshPrivateKey,
|
|
31
|
+
* } from 'micro509/ssh';
|
|
32
|
+
*
|
|
33
|
+
* const keys = await generateKeyPair({ kind: 'ed25519' });
|
|
34
|
+
* const line = await exportSshPublicKey(keys.publicKey, { comment: 'me@host' });
|
|
35
|
+
* const pem = await exportOpenSshPrivateKey(keys.privateKey, { comment: 'me@host' });
|
|
36
|
+
* const imported = unwrap(await importOpenSshPrivateKey(pem));
|
|
37
|
+
* console.log(line, imported.fingerprint);
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @module
|
|
41
|
+
*/
|
|
42
|
+
const O = 32768, k = 2049, A = 8192, j = 16384, M = "OPENSSH PRIVATE KEY", N = Uint8Array.from("openssh-key-v1", (e) => e.charCodeAt(0)), P = [
|
|
43
|
+
"ssh-rsa",
|
|
44
|
+
"ssh-ed25519",
|
|
45
|
+
"ecdsa-sha2-nistp256",
|
|
46
|
+
"ecdsa-sha2-nistp384",
|
|
47
|
+
"ecdsa-sha2-nistp521"
|
|
48
|
+
];
|
|
49
|
+
function asSshKeyType(e) {
|
|
50
|
+
return P.find((t) => t === e);
|
|
51
|
+
}
|
|
52
|
+
const F = {
|
|
53
|
+
"P-256": {
|
|
54
|
+
identifier: "nistp256",
|
|
55
|
+
size: 32,
|
|
56
|
+
hash: "SHA-256",
|
|
57
|
+
prime: 2n ** 256n - 2n ** 224n + 2n ** 192n + 2n ** 96n - 1n,
|
|
58
|
+
b: 41058363725152142129326129780047268409114441015993725554835256314039467401291n
|
|
59
|
+
},
|
|
60
|
+
"P-384": {
|
|
61
|
+
identifier: "nistp384",
|
|
62
|
+
size: 48,
|
|
63
|
+
hash: "SHA-384",
|
|
64
|
+
prime: 2n ** 384n - 2n ** 128n - 2n ** 96n + 2n ** 32n - 1n,
|
|
65
|
+
b: 27580193559959705877849011840389048093056905856361568521428707301988689241309860865136260764883745107765439761230575n
|
|
66
|
+
},
|
|
67
|
+
"P-521": {
|
|
68
|
+
identifier: "nistp521",
|
|
69
|
+
size: 66,
|
|
70
|
+
hash: "SHA-512",
|
|
71
|
+
prime: 2n ** 521n - 1n,
|
|
72
|
+
b: 1093849038073734274511112390766805569936207598951683748994586394495953116150735016013708737573759623248592132296706313309438452531591012912142327488478985984n
|
|
73
|
+
}
|
|
74
|
+
}, I = [
|
|
75
|
+
"P-256",
|
|
76
|
+
"P-384",
|
|
77
|
+
"P-521"
|
|
78
|
+
], L = {
|
|
79
|
+
"ecdsa-sha2-nistp256": "P-256",
|
|
80
|
+
"ecdsa-sha2-nistp384": "P-384",
|
|
81
|
+
"ecdsa-sha2-nistp521": "P-521"
|
|
82
|
+
};
|
|
83
|
+
function resolveImportOptions(e) {
|
|
84
|
+
if (typeof e != "object" || !e || Array.isArray(e)) return C("invalid_options", "SSH import options must be an object");
|
|
85
|
+
let t = e.minRsaModulusBits === void 0 ? 2048 : e.minRsaModulusBits;
|
|
86
|
+
if (!Number.isSafeInteger(t) || t < 1 || t > j) return C("invalid_options", "minRsaModulusBits must be an integer from 1 to 16384");
|
|
87
|
+
let n = e.acceptCompressedEcPoints !== void 0 && e.acceptCompressedEcPoints;
|
|
88
|
+
if (typeof n != "boolean") return C("invalid_options", "acceptCompressedEcPoints must be a boolean");
|
|
89
|
+
let r = e.rsa === void 0 ? {} : e.rsa;
|
|
90
|
+
if (typeof r != "object" || !r || Array.isArray(r)) return C("invalid_options", "rsa must be an object");
|
|
91
|
+
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" ? C("invalid_options", "Unsupported RSA signature scheme") : a !== "SHA-256" && a !== "SHA-384" && a !== "SHA-512" ? C("invalid_options", "Unsupported RSA signature hash") : {
|
|
93
|
+
minRsaModulusBits: t,
|
|
94
|
+
acceptCompressedEcPoints: n,
|
|
95
|
+
rsaScheme: i,
|
|
96
|
+
rsaHash: a
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function unhandledKey(e) {
|
|
100
|
+
throw Error(`Unhandled SSH key: ${String(e)}`);
|
|
101
|
+
}
|
|
102
|
+
function keyTypeOf(e) {
|
|
103
|
+
switch (e.kind) {
|
|
104
|
+
case "rsa": return "ssh-rsa";
|
|
105
|
+
case "ed25519": return "ssh-ed25519";
|
|
106
|
+
case "ecdsa": return `ecdsa-sha2-${F[e.curve].identifier}`;
|
|
107
|
+
default: return unhandledKey(e);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function bitLength(e) {
|
|
111
|
+
return (e.length - 1) * 8 + 32 - Math.clz32(e[0] ?? 0);
|
|
112
|
+
}
|
|
113
|
+
function algorithmOf(e) {
|
|
114
|
+
switch (e.kind) {
|
|
115
|
+
case "rsa": return {
|
|
116
|
+
kind: "rsa",
|
|
117
|
+
modulusBits: bitLength(e.n)
|
|
118
|
+
};
|
|
119
|
+
case "ecdsa": return {
|
|
120
|
+
kind: "ecdsa",
|
|
121
|
+
curve: e.curve
|
|
122
|
+
};
|
|
123
|
+
case "ed25519": return { kind: "ed25519" };
|
|
124
|
+
default: return unhandledKey(e);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function toBase64Url(e) {
|
|
128
|
+
return u(e).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
129
|
+
}
|
|
130
|
+
function fromBase64Url(e) {
|
|
131
|
+
let t = e.replace(/-/g, "+").replace(/_/g, "/");
|
|
132
|
+
return l(t.padEnd(Math.ceil(t.length / 4) * 4, "="));
|
|
133
|
+
}
|
|
134
|
+
function modPow(e, t, n) {
|
|
135
|
+
let r = 1n, i = e % n, a = t;
|
|
136
|
+
for (; a > 0n;) (a & 1n) == 1n && (r = r * i % n), i = i * i % n, a >>= 1n;
|
|
137
|
+
return r;
|
|
138
|
+
}
|
|
139
|
+
function decompressPoint(e, t, n) {
|
|
140
|
+
let { prime: r, b: i, size: a } = F[e], o = b(n);
|
|
141
|
+
if (o >= r) return C("malformed", "ECDSA point coordinate is out of range");
|
|
142
|
+
let s = (((o * o - 3n) * o + i) % r + r) % r, c = modPow(s, (r + 1n) / 4n, r);
|
|
143
|
+
if (c * c % r !== s) return C("malformed", "ECDSA point is not on the curve");
|
|
144
|
+
let l = t === 3, u = (c & 1n) == 1n === l ? c : r - c;
|
|
145
|
+
return v(u, a);
|
|
146
|
+
}
|
|
147
|
+
function readPoint(e, t, n) {
|
|
148
|
+
let { size: r } = F[e], i = t[0];
|
|
149
|
+
if (i === 4 && t.length === 1 + 2 * r) return {
|
|
150
|
+
x: t.subarray(1, 1 + r),
|
|
151
|
+
y: t.subarray(1 + r)
|
|
152
|
+
};
|
|
153
|
+
if ((i === 2 || i === 3) && t.length === 1 + r) {
|
|
154
|
+
if (!n.acceptCompressedEcPoints) return C("unsupported", "Compressed ECDSA points need acceptCompressedEcPoints");
|
|
155
|
+
let r = t.subarray(1);
|
|
156
|
+
return {
|
|
157
|
+
x: r,
|
|
158
|
+
y: decompressPoint(e, i, r)
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
return C("malformed", "ECDSA point encoding is invalid");
|
|
162
|
+
}
|
|
163
|
+
function readRsaPublic(e, t, n) {
|
|
164
|
+
let r = e.mpint(k), i = e.mpint(k), [a, o] = t === "e-n" ? [r, i] : [i, r], s = bitLength(o);
|
|
165
|
+
if (s > j) return C("limit_exceeded", `RSA modulus of ${s} bits exceeds ${j}`);
|
|
166
|
+
if (s < n.minRsaModulusBits) return C("unsupported", `RSA modulus of ${s} bits is below ${n.minRsaModulusBits}`);
|
|
167
|
+
let c = b(a);
|
|
168
|
+
return c < 3n || (c & 1n) == 0n || c >= b(o) ? C("malformed", "RSA public exponent is invalid") : {
|
|
169
|
+
kind: "rsa",
|
|
170
|
+
e: a,
|
|
171
|
+
n: o
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function readEcdsaPublic(e, t, n) {
|
|
175
|
+
return e.name() === F[t].identifier ? {
|
|
176
|
+
kind: "ecdsa",
|
|
177
|
+
curve: t,
|
|
178
|
+
...readPoint(t, e.string(A), n)
|
|
179
|
+
} : C("key_mismatch", "ECDSA curve identifier disagrees with the key type");
|
|
180
|
+
}
|
|
181
|
+
function readEd25519Public(e) {
|
|
182
|
+
let t = e.string(A);
|
|
183
|
+
return t.length === 32 ? {
|
|
184
|
+
kind: "ed25519",
|
|
185
|
+
x: t
|
|
186
|
+
} : C("malformed", "Ed25519 public key is not 32 bytes");
|
|
187
|
+
}
|
|
188
|
+
function decodePublicBlob(e, t) {
|
|
189
|
+
let n = x(e), r = asSshKeyType(n.name());
|
|
190
|
+
if (r === void 0) return C("unsupported", "Unsupported SSH key type");
|
|
191
|
+
let i;
|
|
192
|
+
switch (r) {
|
|
193
|
+
case "ssh-rsa":
|
|
194
|
+
i = readRsaPublic(n, "e-n", t);
|
|
195
|
+
break;
|
|
196
|
+
case "ssh-ed25519":
|
|
197
|
+
i = readEd25519Public(n);
|
|
198
|
+
break;
|
|
199
|
+
case "ecdsa-sha2-nistp256":
|
|
200
|
+
case "ecdsa-sha2-nistp384":
|
|
201
|
+
case "ecdsa-sha2-nistp521":
|
|
202
|
+
i = readEcdsaPublic(n, L[r], t);
|
|
203
|
+
break;
|
|
204
|
+
default: return unhandledKey(r);
|
|
205
|
+
}
|
|
206
|
+
return n.remaining() === 0 ? i : C("malformed", "SSH public key has trailing data");
|
|
207
|
+
}
|
|
208
|
+
function ecPoint(e) {
|
|
209
|
+
return o([
|
|
210
|
+
Uint8Array.of(4),
|
|
211
|
+
e.x,
|
|
212
|
+
e.y
|
|
213
|
+
]);
|
|
214
|
+
}
|
|
215
|
+
function encodePublicBlob(e) {
|
|
216
|
+
let t = E(keyTypeOf(e));
|
|
217
|
+
switch (e.kind) {
|
|
218
|
+
case "rsa": return o([
|
|
219
|
+
t,
|
|
220
|
+
w(e.e),
|
|
221
|
+
w(e.n)
|
|
222
|
+
]);
|
|
223
|
+
case "ed25519": return o([t, T(e.x)]);
|
|
224
|
+
case "ecdsa": return o([
|
|
225
|
+
t,
|
|
226
|
+
E(F[e.curve].identifier),
|
|
227
|
+
T(ecPoint(e))
|
|
228
|
+
]);
|
|
229
|
+
default: return unhandledKey(e);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function samePublicMaterial(e, t) {
|
|
233
|
+
return y(encodePublicBlob(e), encodePublicBlob(t));
|
|
234
|
+
}
|
|
235
|
+
function publicJwk(e) {
|
|
236
|
+
switch (e.kind) {
|
|
237
|
+
case "rsa": return {
|
|
238
|
+
kty: "RSA",
|
|
239
|
+
n: toBase64Url(e.n),
|
|
240
|
+
e: toBase64Url(e.e)
|
|
241
|
+
};
|
|
242
|
+
case "ecdsa": return {
|
|
243
|
+
kty: "EC",
|
|
244
|
+
crv: e.curve,
|
|
245
|
+
x: toBase64Url(e.x),
|
|
246
|
+
y: toBase64Url(e.y)
|
|
247
|
+
};
|
|
248
|
+
case "ed25519": return {
|
|
249
|
+
kty: "OKP",
|
|
250
|
+
crv: "Ed25519",
|
|
251
|
+
x: toBase64Url(e.x)
|
|
252
|
+
};
|
|
253
|
+
default: return unhandledKey(e);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function privateJwk(e) {
|
|
257
|
+
switch (e.kind) {
|
|
258
|
+
case "rsa": {
|
|
259
|
+
let t = b(e.d), n = b(e.p), r = b(e.q);
|
|
260
|
+
return {
|
|
261
|
+
...publicJwk(e.pub),
|
|
262
|
+
d: toBase64Url(e.d),
|
|
263
|
+
p: toBase64Url(e.p),
|
|
264
|
+
q: toBase64Url(e.q),
|
|
265
|
+
dp: toBase64Url(v(t % (n - 1n))),
|
|
266
|
+
dq: toBase64Url(v(t % (r - 1n))),
|
|
267
|
+
qi: toBase64Url(e.iqmp)
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
case "ecdsa": return {
|
|
271
|
+
...publicJwk(e.pub),
|
|
272
|
+
d: toBase64Url(v(b(e.d), F[e.pub.curve].size))
|
|
273
|
+
};
|
|
274
|
+
case "ed25519": return {
|
|
275
|
+
...publicJwk(e.pub),
|
|
276
|
+
d: toBase64Url(e.seed)
|
|
277
|
+
};
|
|
278
|
+
default: return unhandledKey(e);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
function importInputFor(e, t) {
|
|
282
|
+
switch (e.kind) {
|
|
283
|
+
case "rsa": return {
|
|
284
|
+
kind: "rsa",
|
|
285
|
+
scheme: t.rsaScheme,
|
|
286
|
+
hash: t.rsaHash
|
|
287
|
+
};
|
|
288
|
+
case "ecdsa": return {
|
|
289
|
+
kind: "ecdsa",
|
|
290
|
+
curve: e.curve
|
|
291
|
+
};
|
|
292
|
+
case "ed25519": return { kind: "ed25519" };
|
|
293
|
+
default: return unhandledKey(e);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
function signParams(e, t) {
|
|
297
|
+
switch (e.kind) {
|
|
298
|
+
case "rsa": return t.rsaScheme === "pss" ? {
|
|
299
|
+
name: "RSA-PSS",
|
|
300
|
+
saltLength: 32
|
|
301
|
+
} : { name: "RSASSA-PKCS1-v1_5" };
|
|
302
|
+
case "ecdsa": return {
|
|
303
|
+
name: "ECDSA",
|
|
304
|
+
hash: F[e.curve].hash
|
|
305
|
+
};
|
|
306
|
+
case "ed25519": return { name: "Ed25519" };
|
|
307
|
+
default: return unhandledKey(e);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
async function importPublicMaterial(e, t) {
|
|
311
|
+
return await g(publicJwk(e), importInputFor(e, t));
|
|
312
|
+
}
|
|
313
|
+
async function importPrivateMaterial(e, t, r) {
|
|
314
|
+
try {
|
|
315
|
+
return await h(privateJwk(e), importInputFor(t, r));
|
|
316
|
+
} catch (e) {
|
|
317
|
+
return n(e), C("key_mismatch", "Private key material is invalid for the embedded public key");
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
async function assertKeyPairConsistent(e, t, n, r) {
|
|
321
|
+
let { subtle: i } = c(), a = c().getRandomValues(/* @__PURE__ */ new Uint8Array(32)), o = signParams(n, r), s = await i.sign(o, e, a);
|
|
322
|
+
await i.verify(o, t, s, a) || C("key_mismatch", "Private key does not match the embedded public key");
|
|
323
|
+
}
|
|
324
|
+
async function sha256Fingerprint(e) {
|
|
325
|
+
let t = new Uint8Array(await c().subtle.digest("SHA-256", s(e)));
|
|
326
|
+
return `SHA256:${u(t).replace(/=+$/, "")}`;
|
|
327
|
+
}
|
|
328
|
+
function textOf(e, t) {
|
|
329
|
+
return typeof e == "string" ? e.length > t || new TextEncoder().encode(e).length > t ? C("limit_exceeded", `SSH key input exceeds ${t} bytes`) : e : e.length > t ? C("limit_exceeded", `SSH key input exceeds ${t} bytes`) : S(e);
|
|
330
|
+
}
|
|
331
|
+
const R = /^([^ \t]+)[ \t]+([A-Za-z0-9+/]+={0,2})(?:[ \t]+(.*))?$/s;
|
|
332
|
+
async function decodePublicKeyLine(e, t) {
|
|
333
|
+
let n = textOf(e, 16384).replace(/\r?\n$/, "");
|
|
334
|
+
if (/[\r\n]/.test(n)) return C("malformed", "SSH public key must be a single line");
|
|
335
|
+
let r = R.exec(n);
|
|
336
|
+
if (r === null) return C("malformed", "SSH public key line is not \"<type> <base64> [comment]\"");
|
|
337
|
+
let [, i = "", a = "", o = ""] = r;
|
|
338
|
+
assertImportedComment(o);
|
|
339
|
+
let s = asSshKeyType(i);
|
|
340
|
+
if (s === void 0) return C("unsupported", "Unsupported SSH key type");
|
|
341
|
+
let c = decodePublicBlob(l(a), t);
|
|
342
|
+
return keyTypeOf(c) === s ? {
|
|
343
|
+
keyType: s,
|
|
344
|
+
algorithm: algorithmOf(c),
|
|
345
|
+
publicKey: await importPublicMaterial(c, t),
|
|
346
|
+
comment: o,
|
|
347
|
+
fingerprint: await sha256Fingerprint(encodePublicBlob(c))
|
|
348
|
+
} : C("key_mismatch", "Line key type disagrees with the key blob");
|
|
349
|
+
}
|
|
350
|
+
function readPrivateMaterial(e, t, n) {
|
|
351
|
+
if (e.name() !== t) return C("key_mismatch", "Private section key type disagrees with the public key");
|
|
352
|
+
switch (t) {
|
|
353
|
+
case "ssh-rsa": return {
|
|
354
|
+
kind: "rsa",
|
|
355
|
+
pub: readRsaPublic(e, "n-e", n),
|
|
356
|
+
d: e.mpint(k),
|
|
357
|
+
iqmp: e.mpint(k),
|
|
358
|
+
p: e.mpint(k),
|
|
359
|
+
q: e.mpint(k)
|
|
360
|
+
};
|
|
361
|
+
case "ssh-ed25519": {
|
|
362
|
+
let t = readEd25519Public(e), n = e.string(A);
|
|
363
|
+
return n.length === 64 ? y(n.subarray(32), t.x) ? {
|
|
364
|
+
kind: "ed25519",
|
|
365
|
+
pub: t,
|
|
366
|
+
seed: n.subarray(0, 32)
|
|
367
|
+
} : C("key_mismatch", "Ed25519 private key suffix differs from the public key") : C("malformed", "Ed25519 private key is not 64 bytes");
|
|
368
|
+
}
|
|
369
|
+
case "ecdsa-sha2-nistp256":
|
|
370
|
+
case "ecdsa-sha2-nistp384":
|
|
371
|
+
case "ecdsa-sha2-nistp521": {
|
|
372
|
+
let r = readEcdsaPublic(e, L[t], n), i = e.mpint(k);
|
|
373
|
+
return i.length > F[r.curve].size ? C("malformed", "ECDSA private scalar is larger than the curve order") : {
|
|
374
|
+
kind: "ecdsa",
|
|
375
|
+
pub: r,
|
|
376
|
+
d: i
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
default: return unhandledKey(t);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
function assertRsaConsistent(e) {
|
|
383
|
+
let t = b(e.pub.n), n = b(e.pub.e), r = b(e.d), i = b(e.p), a = b(e.q), o = b(e.iqmp);
|
|
384
|
+
(i < 3n || a < 3n || i * a !== t) && C("key_mismatch", "RSA primes do not multiply to the modulus"), (r >= t || r * n % (i - 1n) != 1n || r * n % (a - 1n) != 1n) && C("key_mismatch", "RSA private exponent does not match the public exponent"), (o >= i || o * a % i !== 1n) && C("key_mismatch", "RSA coefficient is not the inverse of q modulo p");
|
|
385
|
+
}
|
|
386
|
+
function assertPadding(e) {
|
|
387
|
+
let t = e.raw(e.remaining());
|
|
388
|
+
t.length >= 8 && C("malformed", "OpenSSH private key padding is too long"), t.forEach((e, t) => {
|
|
389
|
+
e !== t + 1 && C("malformed", "OpenSSH private key padding is invalid");
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
async function decodePrivateKey(e, t) {
|
|
393
|
+
let n = textOf(e, O), r = d(M, n);
|
|
394
|
+
try {
|
|
395
|
+
return await decodePrivateBinary(r, t);
|
|
396
|
+
} finally {
|
|
397
|
+
r.fill(0);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
async function decodePrivateBinary(e, t) {
|
|
401
|
+
let n = x(e), r = n.raw(N.length + 1);
|
|
402
|
+
if (!y(r.subarray(0, N.length), N) || r[N.length] !== 0) return C("malformed", "Missing openssh-key-v1 magic");
|
|
403
|
+
let i = n.name(), a = n.name();
|
|
404
|
+
if (i !== "none" || a !== "none") return C("unsupported", "Encrypted OpenSSH private keys are not supported");
|
|
405
|
+
if (n.string(A).length !== 0) return C("malformed", "KDF options must be empty for kdf none");
|
|
406
|
+
let o = n.uint32();
|
|
407
|
+
if (o === 0) return C("malformed", "OpenSSH private key file holds no keys");
|
|
408
|
+
if (o > 1) return C("unsupported", "OpenSSH private key files with several keys are not supported");
|
|
409
|
+
let s = decodePublicBlob(n.string(A), t), c = n.string(O);
|
|
410
|
+
if (n.remaining() !== 0) return C("malformed", "OpenSSH private key file has trailing data");
|
|
411
|
+
if (c.length === 0 || c.length % 8 != 0) return C("malformed", "OpenSSH private section is not a multiple of the block size");
|
|
412
|
+
let l = x(c);
|
|
413
|
+
if (l.uint32() !== l.uint32()) return C("malformed", "OpenSSH check integers differ");
|
|
414
|
+
let u = readPrivateMaterial(l, keyTypeOf(s), t), d = l.text(A);
|
|
415
|
+
if (assertImportedComment(d), assertPadding(l), !samePublicMaterial(s, u.pub)) return C("key_mismatch", "Private section public key differs from the file public key");
|
|
416
|
+
u.kind === "rsa" && assertRsaConsistent(u);
|
|
417
|
+
let f = await importPublicMaterial(s, t), p = await importPrivateMaterial(u, s, t);
|
|
418
|
+
return await assertKeyPairConsistent(p, f, s, t), {
|
|
419
|
+
keyType: keyTypeOf(s),
|
|
420
|
+
algorithm: algorithmOf(s),
|
|
421
|
+
publicKey: f,
|
|
422
|
+
privateKey: p,
|
|
423
|
+
comment: d,
|
|
424
|
+
fingerprint: await sha256Fingerprint(encodePublicBlob(s))
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
async function toSshResult(i) {
|
|
428
|
+
try {
|
|
429
|
+
return r(await i());
|
|
430
|
+
} catch (r) {
|
|
431
|
+
if (t(r)) {
|
|
432
|
+
let t = _(r.code);
|
|
433
|
+
if (t !== void 0) return e(t, r.error.message);
|
|
434
|
+
}
|
|
435
|
+
return n(r), e("malformed", r instanceof Error ? r.message : "Malformed SSH key");
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Import an OpenSSH public-key line (`<type> <base64> [comment]`).
|
|
440
|
+
*
|
|
441
|
+
* Reads `ssh-rsa`, `ssh-ed25519`, and `ecdsa-sha2-nistp256/384/521` keys as
|
|
442
|
+
* written by `ssh-keygen` into `.pub` files. The line key type must equal the
|
|
443
|
+
* type inside the blob (RFC 4253 §6.6), an ECDSA curve identifier must equal
|
|
444
|
+
* its key type suffix (RFC 5656 §3.1), and trailing data after the blob is
|
|
445
|
+
* rejected. Fields are separated by spaces or tabs, one trailing line break
|
|
446
|
+
* is allowed, and everything after the base64 field is the comment.
|
|
447
|
+
* `authorized_keys` options and SSH certificate types are not read.
|
|
448
|
+
*
|
|
449
|
+
* Input is capped at 16 KiB and RSA moduli at 16384 bits. `input` is never
|
|
450
|
+
* modified.
|
|
451
|
+
*
|
|
452
|
+
* @param input - The line as text or UTF-8 bytes
|
|
453
|
+
* @param options - Policy options; see {@linkcode SshImportOptions}
|
|
454
|
+
* @returns The WebCrypto verification key, its comment and its SHA-256 fingerprint
|
|
455
|
+
*
|
|
456
|
+
* @see {@linkcode importSshPublicKeyOrThrow} for the throwing variant
|
|
457
|
+
* @see {@linkcode exportSshPublicKey} for the inverse operation
|
|
458
|
+
*/
|
|
459
|
+
function importSshPublicKey(e, t = {}) {
|
|
460
|
+
return toSshResult(() => decodePublicKeyLine(e, resolveImportOptions(t)));
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Throwing variant of {@linkcode importSshPublicKey}.
|
|
464
|
+
*
|
|
465
|
+
* @throws {ResultError} Carrying an {@linkcode SshErrorCode} as `code`
|
|
466
|
+
*/
|
|
467
|
+
async function importSshPublicKeyOrThrow(e, t = {}) {
|
|
468
|
+
return a(await importSshPublicKey(e, t));
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Import an unencrypted single-key `openssh-key-v1` private key file.
|
|
472
|
+
*
|
|
473
|
+
* Accepts the `-----BEGIN OPENSSH PRIVATE KEY-----` PEM that `ssh-keygen`
|
|
474
|
+
* writes for RSA, Ed25519 and ECDSA P-256/P-384/P-521 keys. The cipher and
|
|
475
|
+
* KDF must both be `none` with empty KDF options; any other value returns
|
|
476
|
+
* `unsupported`, as does a file holding several keys. The check integers,
|
|
477
|
+
* the `1, 2, 3, ...` padding, the block alignment, and the absence of trailing
|
|
478
|
+
* bytes are verified, and RSA integers must be canonical positive `mpint`s.
|
|
479
|
+
*
|
|
480
|
+
* The public key embedded in the file must equal the public half of the
|
|
481
|
+
* private section and its key type, or the result is `key_mismatch`. The
|
|
482
|
+
* private material is then checked cryptographically: for RSA, `p·q = n`,
|
|
483
|
+
* `d·e ≡ 1` modulo both `p-1` and `q-1`, and `iqmp·q ≡ 1 (mod p)`; for every
|
|
484
|
+
* key, a signature made with the private key must verify under the embedded
|
|
485
|
+
* public key. For Ed25519 the 32-byte suffix of the private field must equal
|
|
486
|
+
* the public key.
|
|
487
|
+
*
|
|
488
|
+
* Both keys are returned extractable. The decoded binary copy of the file is
|
|
489
|
+
* zeroed before returning. JavaScript strings, including the PEM text and the
|
|
490
|
+
* JWK members used for the import, cannot be zeroed, and `input` is never
|
|
491
|
+
* modified.
|
|
492
|
+
*
|
|
493
|
+
* @param input - The PEM file as text or UTF-8 bytes, at most 32 KiB
|
|
494
|
+
* @param options - Policy options; see {@linkcode SshImportOptions}
|
|
495
|
+
*
|
|
496
|
+
* @see {@linkcode importOpenSshPrivateKeyOrThrow} for the throwing variant
|
|
497
|
+
* @see {@linkcode exportOpenSshPrivateKey} for the inverse operation
|
|
498
|
+
*/
|
|
499
|
+
function importOpenSshPrivateKey(e, t = {}) {
|
|
500
|
+
return toSshResult(() => decodePrivateKey(e, resolveImportOptions(t)));
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Throwing variant of {@linkcode importOpenSshPrivateKey}.
|
|
504
|
+
*
|
|
505
|
+
* @throws {ResultError} Carrying an {@linkcode SshErrorCode} as `code`
|
|
506
|
+
*/
|
|
507
|
+
async function importOpenSshPrivateKeyOrThrow(e, t = {}) {
|
|
508
|
+
return a(await importOpenSshPrivateKey(e, t));
|
|
509
|
+
}
|
|
510
|
+
function exportFail(e, t) {
|
|
511
|
+
return i(e, t);
|
|
512
|
+
}
|
|
513
|
+
function validCommentText(e) {
|
|
514
|
+
return typeof e == "string" && e.isWellFormed() && !/[\r\n\0]/.test(e);
|
|
515
|
+
}
|
|
516
|
+
function oversizedComment(e) {
|
|
517
|
+
return e.length > A || new TextEncoder().encode(e).length > A;
|
|
518
|
+
}
|
|
519
|
+
function assertImportedComment(e) {
|
|
520
|
+
validCommentText(e) || C("malformed", "Comment must be well-formed Unicode without CR, LF or NUL"), oversizedComment(e) && C("limit_exceeded", "Comment exceeds 8192 UTF-8 bytes");
|
|
521
|
+
}
|
|
522
|
+
function assertComment(e) {
|
|
523
|
+
(!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");
|
|
524
|
+
}
|
|
525
|
+
function assertExportable(e, t) {
|
|
526
|
+
e.type !== t && exportFail("invalid_key", `Expected a ${t} key`), e.extractable || exportFail("not_extractable", "The key must be extractable");
|
|
527
|
+
}
|
|
528
|
+
function jwkBytes(e, t) {
|
|
529
|
+
let n = e[t];
|
|
530
|
+
if (typeof n != "string") throw Error(`WebCrypto key export lacks JWK member ${t}`);
|
|
531
|
+
return fromBase64Url(n);
|
|
532
|
+
}
|
|
533
|
+
function publicMaterialFromJwk(e, t) {
|
|
534
|
+
if (e.kty === "RSA") return {
|
|
535
|
+
kind: "rsa",
|
|
536
|
+
e: jwkBytes(e, "e"),
|
|
537
|
+
n: jwkBytes(e, "n")
|
|
538
|
+
};
|
|
539
|
+
if (e.kty === "OKP" && e.crv === "Ed25519" && t === "Ed25519") return {
|
|
540
|
+
kind: "ed25519",
|
|
541
|
+
x: jwkBytes(e, "x")
|
|
542
|
+
};
|
|
543
|
+
if (e.kty === "EC" && t === "ECDSA") {
|
|
544
|
+
let t = I.find((t) => t === e.crv);
|
|
545
|
+
if (t !== void 0) return {
|
|
546
|
+
kind: "ecdsa",
|
|
547
|
+
curve: t,
|
|
548
|
+
x: jwkBytes(e, "x"),
|
|
549
|
+
y: jwkBytes(e, "y")
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
return exportFail("unsupported_algorithm", "Only RSA, ECDSA P-256/384/521 and Ed25519 are supported");
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Export a public key as an OpenSSH public-key line.
|
|
556
|
+
*
|
|
557
|
+
* Writes `<type> <base64> [comment]` with padded base64 and no trailing line
|
|
558
|
+
* break, the form `ssh-keygen` puts in `.pub` files. ECDSA points are written
|
|
559
|
+
* uncompressed. The key must be an extractable RSA, ECDSA (P-256, P-384,
|
|
560
|
+
* P-521) or Ed25519 public `CryptoKey`.
|
|
561
|
+
*
|
|
562
|
+
* @throws {ResultError} Carrying an {@linkcode SshExportErrorCode} as `code`
|
|
563
|
+
*
|
|
564
|
+
* @see {@linkcode importSshPublicKey} for the inverse operation
|
|
565
|
+
*/
|
|
566
|
+
async function exportSshPublicKey(e, t = {}) {
|
|
567
|
+
let n = t.comment === void 0 ? "" : t.comment;
|
|
568
|
+
assertComment(n), assertExportable(e, "public");
|
|
569
|
+
let r = publicMaterialFromJwk(await m(e), e.algorithm.name), i = `${keyTypeOf(r)} ${u(encodePublicBlob(r))}`;
|
|
570
|
+
return n === "" ? i : `${i} ${n}`;
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Export a private key as an unencrypted `openssh-key-v1` PEM file.
|
|
574
|
+
*
|
|
575
|
+
* The file holds one key, cipher `none` and KDF `none`, with the check
|
|
576
|
+
* integers drawn from the WebCrypto random source and the public key derived
|
|
577
|
+
* from the private key. `ssh-keygen` reads it directly. Check integers are
|
|
578
|
+
* random, so compare keys, not file bytes, between two exports.
|
|
579
|
+
*
|
|
580
|
+
* The private `CryptoKey` must be extractable. The binary encoding is zeroed
|
|
581
|
+
* after the PEM text is built; the returned string and the JWK used for the
|
|
582
|
+
* export cannot be zeroed.
|
|
583
|
+
*
|
|
584
|
+
* @throws {ResultError} Carrying an {@linkcode SshExportErrorCode} as `code`
|
|
585
|
+
*
|
|
586
|
+
* @see {@linkcode importOpenSshPrivateKey} for the inverse operation
|
|
587
|
+
*/
|
|
588
|
+
async function exportOpenSshPrivateKey(e, t = {}) {
|
|
589
|
+
let n = t.comment === void 0 ? "" : t.comment;
|
|
590
|
+
assertComment(n), assertExportable(e, "private");
|
|
591
|
+
let r = await p(e), i = publicMaterialFromJwk(r, e.algorithm.name), a = privateFields(i, r), s = c().getRandomValues(/* @__PURE__ */ new Uint32Array(1))[0] ?? 0, l = o([
|
|
592
|
+
D(s),
|
|
593
|
+
D(s),
|
|
594
|
+
E(keyTypeOf(i)),
|
|
595
|
+
a,
|
|
596
|
+
E(n)
|
|
597
|
+
]), u = (8 - l.length % 8) % 8, d = o([l, Uint8Array.from({ length: u }, (e, t) => t + 1)]), m = o([
|
|
598
|
+
N,
|
|
599
|
+
Uint8Array.of(0),
|
|
600
|
+
E("none"),
|
|
601
|
+
E("none"),
|
|
602
|
+
D(0),
|
|
603
|
+
D(1),
|
|
604
|
+
T(encodePublicBlob(i)),
|
|
605
|
+
T(d)
|
|
606
|
+
]);
|
|
607
|
+
try {
|
|
608
|
+
return f(M, m);
|
|
609
|
+
} finally {
|
|
610
|
+
l.fill(0), d.fill(0), m.fill(0), a.fill(0);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
function privateFields(e, t) {
|
|
614
|
+
switch (e.kind) {
|
|
615
|
+
case "rsa": return o([
|
|
616
|
+
w(e.n),
|
|
617
|
+
w(e.e),
|
|
618
|
+
w(jwkBytes(t, "d")),
|
|
619
|
+
w(jwkBytes(t, "qi")),
|
|
620
|
+
w(jwkBytes(t, "p")),
|
|
621
|
+
w(jwkBytes(t, "q"))
|
|
622
|
+
]);
|
|
623
|
+
case "ecdsa": return o([
|
|
624
|
+
E(F[e.curve].identifier),
|
|
625
|
+
T(ecPoint(e)),
|
|
626
|
+
w(jwkBytes(t, "d"))
|
|
627
|
+
]);
|
|
628
|
+
case "ed25519": return o([T(e.x), T(o([jwkBytes(t, "d"), e.x]))]);
|
|
629
|
+
default: return unhandledKey(e);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* The `SHA256:` fingerprint `ssh-keygen -l -E sha256` prints for a key.
|
|
634
|
+
*
|
|
635
|
+
* It is the SHA-256 digest of the SSH public-key blob (RFC 4253 §6.6 layout),
|
|
636
|
+
* base64-encoded without padding and prefixed with `SHA256:`. It differs from a
|
|
637
|
+
* certificate fingerprint and from a digest of the SPKI encoding. Pass a
|
|
638
|
+
* public or private `CryptoKey`, or the raw blob bytes (the decoded base64
|
|
639
|
+
* field of a public-key line). A private key must be extractable.
|
|
640
|
+
*
|
|
641
|
+
* @throws {ResultError} Carrying an {@linkcode SshExportErrorCode} when a `CryptoKey` cannot be encoded
|
|
642
|
+
*/
|
|
643
|
+
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 p(e) : await m(e), e.algorithm.name))));
|
|
645
|
+
}
|
|
646
|
+
export { exportOpenSshPrivateKey, exportSshPublicKey, importOpenSshPrivateKey, importOpenSshPrivateKeyOrThrow, importSshPublicKey, importSshPublicKeyOrThrow, sshFingerprintSha256 };
|
|
647
|
+
|
|
648
|
+
//# sourceMappingURL=ssh.js.map
|