micro509 0.13.0 → 0.15.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 +2064 -0
- package/README.md +111 -183
- package/dist/crypto/crypto.d.ts +104 -0
- package/dist/crypto/crypto.js +105 -0
- package/dist/crypto/crypto.js.map +1 -0
- package/dist/crypto.d.ts +4 -0
- package/dist/crypto.js +1 -0
- package/dist/der/der.d.ts +41 -33
- package/dist/der/der.js +26 -24
- package/dist/der/der.js.map +1 -1
- package/dist/index.d.ts +18 -14
- package/dist/index.js +19 -17
- package/dist/internal/asn1/asn1.d.ts +3 -6
- package/dist/internal/asn1/asn1.js +355 -44
- package/dist/internal/asn1/asn1.js.map +1 -1
- package/dist/internal/asn1/ber.js +146 -0
- package/dist/internal/asn1/ber.js.map +1 -0
- package/dist/internal/asn1/decode-refusal.js +29 -0
- package/dist/internal/asn1/decode-refusal.js.map +1 -0
- package/dist/internal/asn1/der.d.ts +42 -43
- package/dist/internal/asn1/der.js +148 -56
- package/dist/internal/asn1/der.js.map +1 -1
- package/dist/internal/asn1/oids.js +90 -54
- package/dist/internal/asn1/oids.js.map +1 -1
- package/dist/internal/asn1/oids2.js +1 -2
- package/dist/internal/asn1/oids2.js.map +1 -1
- package/dist/internal/crypto/algorithm-names.js +5 -2
- package/dist/internal/crypto/algorithm-names.js.map +1 -1
- package/dist/internal/crypto/ecdsa.js +13 -8
- package/dist/internal/crypto/ecdsa.js.map +1 -1
- package/dist/internal/crypto/hash.js +1 -2
- package/dist/internal/crypto/hash.js.map +1 -1
- package/dist/internal/crypto/pbes2.d.ts +51 -0
- package/dist/internal/crypto/pbes2.js +86 -34
- package/dist/internal/crypto/pbes2.js.map +1 -1
- package/dist/internal/crypto/rsa-pss.js +48 -44
- package/dist/internal/crypto/rsa-pss.js.map +1 -1
- package/dist/internal/crypto/sig-verify.d.ts +28 -0
- package/dist/internal/crypto/sig-verify.js +111 -62
- package/dist/internal/crypto/sig-verify.js.map +1 -1
- package/dist/internal/crypto/signing.d.ts +1 -4
- package/dist/internal/crypto/signing.js +28 -29
- package/dist/internal/crypto/signing.js.map +1 -1
- package/dist/internal/crypto/webcrypto.js +1 -2
- package/dist/internal/crypto/webcrypto.js.map +1 -1
- package/dist/internal/shared/base64.js +8 -3
- package/dist/internal/shared/base64.js.map +1 -1
- package/dist/internal/shared/dn.d.ts +13 -0
- package/dist/internal/shared/dn.js +101 -48
- package/dist/internal/shared/dn.js.map +1 -1
- package/dist/internal/shared/idna-tables.js +7217 -0
- package/dist/internal/shared/idna-tables.js.map +1 -0
- package/dist/internal/shared/idna.js +296 -0
- package/dist/internal/shared/idna.js.map +1 -0
- package/dist/internal/shared/ip.d.ts +66 -0
- package/dist/internal/shared/ip.js +19 -3
- package/dist/internal/shared/ip.js.map +1 -1
- package/dist/internal/shared/mailbox.js +26 -0
- package/dist/internal/shared/mailbox.js.map +1 -0
- package/dist/internal/shared/punycode.js +101 -0
- package/dist/internal/shared/punycode.js.map +1 -0
- package/dist/internal/shared/rfc3454-tables.js +1 -2
- package/dist/internal/shared/rfc3454-tables.js.map +1 -1
- package/dist/internal/shared/rfc4518-tables.js +119 -0
- package/dist/internal/shared/rfc4518-tables.js.map +1 -0
- package/dist/internal/shared/uri-host.js +102 -0
- package/dist/internal/shared/uri-host.js.map +1 -0
- package/dist/internal/verify/name-constraints-engine.js +246 -131
- package/dist/internal/verify/name-constraints-engine.js.map +1 -1
- package/dist/internal/verify/policy-engine.js +6 -21
- package/dist/internal/verify/policy-engine.js.map +1 -1
- package/dist/internal/verify/verify-path.d.ts +4 -0
- package/dist/internal/verify/verify-path.js +264 -128
- package/dist/internal/verify/verify-path.js.map +1 -1
- package/dist/internal/x509/directory-name.js +23 -0
- package/dist/internal/x509/directory-name.js.map +1 -0
- package/dist/internal/x509/extension-bits.js +15 -13
- package/dist/internal/x509/extension-bits.js.map +1 -1
- package/dist/internal/x509/extension-errors.d.ts +3 -0
- package/dist/internal/x509/extension-errors.js +19 -0
- package/dist/internal/x509/extension-errors.js.map +1 -0
- package/dist/internal/x509/extension-registry.js +157 -81
- package/dist/internal/x509/extension-registry.js.map +1 -1
- package/dist/internal/x509/general-name-profile.js +129 -0
- package/dist/internal/x509/general-name-profile.js.map +1 -0
- package/dist/internal/x509/general-name-tags.js +27 -0
- package/dist/internal/x509/general-name-tags.js.map +1 -0
- package/dist/internal/x509/general-name.js +183 -0
- package/dist/internal/x509/general-name.js.map +1 -0
- package/dist/internal/x509/name-fields.js +21 -12
- package/dist/internal/x509/name-fields.js.map +1 -1
- package/dist/internal/x509/or-address.js +217 -0
- package/dist/internal/x509/or-address.js.map +1 -0
- package/dist/keys/keys.d.ts +124 -95
- package/dist/keys/keys.js +362 -214
- package/dist/keys/keys.js.map +1 -1
- package/dist/keys.d.ts +3 -2
- package/dist/keys.js +3 -2
- package/dist/pem/pem.d.ts +17 -19
- package/dist/pem/pem.js +122 -41
- package/dist/pem/pem.js.map +1 -1
- package/dist/pem.js +1 -2
- package/dist/pkcs/pfx.d.ts +57 -32
- package/dist/pkcs/pfx.js +209 -173
- package/dist/pkcs/pfx.js.map +1 -1
- package/dist/pkcs/pkcs12-mac.d.ts +104 -27
- package/dist/pkcs/pkcs12-mac.js +266 -79
- package/dist/pkcs/pkcs12-mac.js.map +1 -1
- package/dist/pkcs/pkcs7.d.ts +123 -53
- package/dist/pkcs/pkcs7.js +314 -238
- package/dist/pkcs/pkcs7.js.map +1 -1
- package/dist/pkcs.d.ts +5 -4
- package/dist/result/result.d.ts +43 -28
- package/dist/result/result.js +27 -14
- package/dist/result/result.js.map +1 -1
- package/dist/result.d.ts +2 -2
- package/dist/result.js +1 -2
- package/dist/revocation/chain.d.ts +101 -23
- package/dist/revocation/chain.js +456 -206
- package/dist/revocation/chain.js.map +1 -1
- package/dist/revocation/crl.d.ts +140 -65
- package/dist/revocation/crl.js +691 -391
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.d.ts +145 -69
- package/dist/revocation/ocsp.js +347 -297
- package/dist/revocation/ocsp.js.map +1 -1
- package/dist/revocation/revocation.d.ts +48 -36
- package/dist/revocation/revocation.js +112 -63
- package/dist/revocation/revocation.js.map +1 -1
- package/dist/revocation.d.ts +5 -5
- package/dist/revocation.js +5 -5
- package/dist/verify/identity.d.ts +18 -20
- package/dist/verify/identity.js +120 -90
- package/dist/verify/identity.js.map +1 -1
- package/dist/verify/name-constraints.d.ts +1 -4
- package/dist/verify/policy.d.ts +15 -8
- package/dist/verify/verify.d.ts +86 -46
- package/dist/verify/verify.js +309 -143
- package/dist/verify/verify.js.map +1 -1
- package/dist/verify.d.ts +3 -2
- package/dist/verify.js +4 -3
- package/dist/x509/certificate.d.ts +50 -28
- package/dist/x509/certificate.js +69 -36
- package/dist/x509/certificate.js.map +1 -1
- package/dist/x509/csr.d.ts +9 -8
- package/dist/x509/csr.js +10 -11
- package/dist/x509/csr.js.map +1 -1
- package/dist/x509/extensions.d.ts +207 -82
- package/dist/x509/extensions.js +561 -128
- package/dist/x509/extensions.js.map +1 -1
- package/dist/x509/fingerprint.d.ts +4 -7
- package/dist/x509/fingerprint.js +1 -2
- package/dist/x509/fingerprint.js.map +1 -1
- package/dist/x509/name-text.d.ts +7 -9
- package/dist/x509/name-text.js +20 -4
- package/dist/x509/name-text.js.map +1 -1
- package/dist/x509/name.d.ts +15 -14
- package/dist/x509/name.js +23 -19
- package/dist/x509/name.js.map +1 -1
- package/dist/x509/parse.d.ts +165 -64
- package/dist/x509/parse.js +444 -459
- package/dist/x509/parse.js.map +1 -1
- package/dist/x509.d.ts +8 -5
- package/dist/x509.js +10 -8
- package/package.json +90 -65
package/dist/keys/keys.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
1
|
+
import { failureResult as e, rethrowIfInvariant as t, successResult as n } from "../result/result.js";
|
|
2
|
+
import { DECODE_LIMIT_CODES as r, decodeFailureResult as i, rethrowDecodeRefusal as a } from "../internal/asn1/decode-refusal.js";
|
|
3
|
+
import { explicitContext as o, integer as s, nullValue as c, objectIdentifier as l, octetString as u, readRootElement as d, readSequenceChildren as f, sequence as p, tlv as m } from "../internal/asn1/der.js";
|
|
4
|
+
import { decodeObjectIdentifier as h, extractBitStringValue as g, hexToBytes as _, toArrayBuffer as v, toHex as y } from "../internal/asn1/asn1.js";
|
|
5
|
+
import { OIDS as b } from "../internal/asn1/oids2.js";
|
|
6
|
+
import { getCrypto as x } from "../internal/crypto/webcrypto.js";
|
|
7
|
+
import { md5 as S } from "../internal/crypto/hash.js";
|
|
8
|
+
import { createKdfBudget as C, decryptPbes2 as w, encryptPbes2 as T, isKdfIterationLimitError as E, isWrongPasswordError as D, parsePbes2AlgorithmIdentifier as O, wrongPasswordError as k } from "../internal/crypto/pbes2.js";
|
|
9
|
+
import { base64Decode as A, base64Encode as j } from "../internal/shared/base64.js";
|
|
10
|
+
import { parseTraditionalPemOrThrow as M, pemDecodeOrThrow as N, pemEncode as P, trimLwsp as F } from "../pem/pem.js";
|
|
11
|
+
// src/keys/keys.ts
|
|
11
12
|
/**
|
|
12
13
|
* WebCrypto key generation plus import/export for PKCS#1, PKCS#8, SEC1, SPKI, and JWK.
|
|
13
14
|
*
|
|
@@ -49,7 +50,7 @@ async function generateKeyPair(e = {
|
|
|
49
50
|
kind: "ecdsa",
|
|
50
51
|
curve: "P-256"
|
|
51
52
|
}) {
|
|
52
|
-
let t =
|
|
53
|
+
let t = x().subtle, n = isRsaOaepInput(e) ? ["encrypt", "decrypt"] : ["sign", "verify"], r = await t.generateKey(toGenerateKeyAlgorithm(e), !0, n);
|
|
53
54
|
if (!("publicKey" in r) || !("privateKey" in r)) throw Error("Expected an asymmetric key pair");
|
|
54
55
|
return wrapKeyPair(r.publicKey, r.privateKey);
|
|
55
56
|
}
|
|
@@ -58,22 +59,22 @@ function wrapKeyPair(e, t) {
|
|
|
58
59
|
publicKey: e,
|
|
59
60
|
privateKey: t,
|
|
60
61
|
async exportSpkiDer() {
|
|
61
|
-
return new Uint8Array(await
|
|
62
|
+
return new Uint8Array(await x().subtle.exportKey("spki", e));
|
|
62
63
|
},
|
|
63
64
|
async exportSpkiPem() {
|
|
64
|
-
return
|
|
65
|
+
return P("PUBLIC KEY", await this.exportSpkiDer());
|
|
65
66
|
},
|
|
66
67
|
async exportPkcs8Der() {
|
|
67
|
-
return new Uint8Array(await
|
|
68
|
+
return new Uint8Array(await x().subtle.exportKey("pkcs8", t));
|
|
68
69
|
},
|
|
69
70
|
async exportPkcs8Pem() {
|
|
70
|
-
return
|
|
71
|
+
return P("PRIVATE KEY", await this.exportPkcs8Der());
|
|
71
72
|
},
|
|
72
73
|
exportPublicJwk() {
|
|
73
|
-
return
|
|
74
|
+
return x().subtle.exportKey("jwk", e);
|
|
74
75
|
},
|
|
75
76
|
exportPrivateJwk() {
|
|
76
|
-
return
|
|
77
|
+
return x().subtle.exportKey("jwk", t);
|
|
77
78
|
}
|
|
78
79
|
};
|
|
79
80
|
}
|
|
@@ -84,7 +85,7 @@ function wrapKeyPair(e, t) {
|
|
|
84
85
|
* @see {@linkcode exportSpkiPem} for PEM output
|
|
85
86
|
*/
|
|
86
87
|
async function exportSpkiDer(e) {
|
|
87
|
-
return new Uint8Array(await
|
|
88
|
+
return new Uint8Array(await x().subtle.exportKey("spki", e));
|
|
88
89
|
}
|
|
89
90
|
/**
|
|
90
91
|
* Export a private key as DER-encoded PKCS#8 PrivateKeyInfo.
|
|
@@ -94,7 +95,7 @@ async function exportSpkiDer(e) {
|
|
|
94
95
|
* @see {@linkcode exportEncryptedPkcs8Der} for password-protected export
|
|
95
96
|
*/
|
|
96
97
|
async function exportPkcs8Der(e) {
|
|
97
|
-
return new Uint8Array(await
|
|
98
|
+
return new Uint8Array(await x().subtle.exportKey("pkcs8", e));
|
|
98
99
|
}
|
|
99
100
|
/**
|
|
100
101
|
* Export a public key as a JSON Web Key.
|
|
@@ -106,7 +107,7 @@ async function exportPkcs8Der(e) {
|
|
|
106
107
|
* ```
|
|
107
108
|
*/
|
|
108
109
|
function exportPublicJwk(e) {
|
|
109
|
-
return
|
|
110
|
+
return x().subtle.exportKey("jwk", e);
|
|
110
111
|
}
|
|
111
112
|
/**
|
|
112
113
|
* Export a private key as a JSON Web Key.
|
|
@@ -115,7 +116,7 @@ function exportPublicJwk(e) {
|
|
|
115
116
|
* @see {@linkcode exportPublicJwk} for public key export
|
|
116
117
|
*/
|
|
117
118
|
function exportPrivateJwk(e) {
|
|
118
|
-
return
|
|
119
|
+
return x().subtle.exportKey("jwk", e);
|
|
119
120
|
}
|
|
120
121
|
/**
|
|
121
122
|
* Export a private key as PEM-encoded PKCS#8 PrivateKeyInfo.
|
|
@@ -133,7 +134,7 @@ function exportPrivateJwk(e) {
|
|
|
133
134
|
* @see {@linkcode exportEncryptedPkcs8Pem} for password-protected export
|
|
134
135
|
*/
|
|
135
136
|
async function exportPkcs8Pem(e) {
|
|
136
|
-
return
|
|
137
|
+
return P("PRIVATE KEY", await exportPkcs8Der(e));
|
|
137
138
|
}
|
|
138
139
|
/**
|
|
139
140
|
* Export a private key as DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
|
|
@@ -143,12 +144,14 @@ async function exportPkcs8Pem(e) {
|
|
|
143
144
|
* @param privateKey - The private key to export
|
|
144
145
|
* @param options - Encryption options including password and optional algorithm settings
|
|
145
146
|
*
|
|
147
|
+
* @throws {RangeError} If `options.iterations` is not an integer from 1 to 4294967295
|
|
148
|
+
*
|
|
146
149
|
* @see {@linkcode importEncryptedPkcs8Der} for the inverse operation
|
|
147
150
|
* @see {@linkcode exportEncryptedPkcs8Pem} for PEM output
|
|
148
151
|
*/
|
|
149
152
|
async function exportEncryptedPkcs8Der(e, t) {
|
|
150
|
-
let n = await
|
|
151
|
-
return
|
|
153
|
+
let n = await exportPkcs8Der(e), r = await T(n, t);
|
|
154
|
+
return p([r.algorithmIdentifierDer, u(r.encryptedData)]);
|
|
152
155
|
}
|
|
153
156
|
/**
|
|
154
157
|
* Export a private key as PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
|
|
@@ -165,7 +168,7 @@ async function exportEncryptedPkcs8Der(e, t) {
|
|
|
165
168
|
* @see {@linkcode importEncryptedPkcs8Pem} for the inverse operation
|
|
166
169
|
*/
|
|
167
170
|
async function exportEncryptedPkcs8Pem(e, t) {
|
|
168
|
-
return
|
|
171
|
+
return P("ENCRYPTED PRIVATE KEY", await exportEncryptedPkcs8Der(e, t));
|
|
169
172
|
}
|
|
170
173
|
/**
|
|
171
174
|
* Export an RSA private key as DER-encoded PKCS#1 RSAPrivateKey.
|
|
@@ -180,7 +183,7 @@ async function exportEncryptedPkcs8Pem(e, t) {
|
|
|
180
183
|
*/
|
|
181
184
|
async function exportPkcs1Der(e) {
|
|
182
185
|
let t = parsePkcs8PrivateKey(await exportPkcs8Der(e));
|
|
183
|
-
if (t.algorithmOid !==
|
|
186
|
+
if (t.algorithmOid !== b.rsaEncryption) throw Error("PKCS#1 export requires an RSA private key");
|
|
184
187
|
return t.privateKeyDer;
|
|
185
188
|
}
|
|
186
189
|
/**
|
|
@@ -192,7 +195,7 @@ async function exportPkcs1Der(e) {
|
|
|
192
195
|
* @see {@linkcode exportEncryptedPkcs1Pem} for password-protected export
|
|
193
196
|
*/
|
|
194
197
|
async function exportPkcs1Pem(e) {
|
|
195
|
-
return
|
|
198
|
+
return P("RSA PRIVATE KEY", await exportPkcs1Der(e));
|
|
196
199
|
}
|
|
197
200
|
/**
|
|
198
201
|
* Export an RSA private key as legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1).
|
|
@@ -224,7 +227,7 @@ async function exportEncryptedPkcs1Pem(e, t) {
|
|
|
224
227
|
*/
|
|
225
228
|
async function exportSec1Der(e) {
|
|
226
229
|
let t = parsePkcs8PrivateKey(await exportPkcs8Der(e));
|
|
227
|
-
if (t.algorithmOid !==
|
|
230
|
+
if (t.algorithmOid !== b.ecPublicKey) throw Error("SEC1 export requires an EC private key");
|
|
228
231
|
return ensureSec1NamedCurveParameters(t.privateKeyDer, t.parametersOid);
|
|
229
232
|
}
|
|
230
233
|
/**
|
|
@@ -236,7 +239,7 @@ async function exportSec1Der(e) {
|
|
|
236
239
|
* @see {@linkcode exportEncryptedSec1Pem} for password-protected export
|
|
237
240
|
*/
|
|
238
241
|
async function exportSec1Pem(e) {
|
|
239
|
-
return
|
|
242
|
+
return P("EC PRIVATE KEY", await exportSec1Der(e));
|
|
240
243
|
}
|
|
241
244
|
/**
|
|
242
245
|
* Export an EC private key as legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1).
|
|
@@ -261,7 +264,7 @@ async function exportEncryptedSec1Pem(e, t) {
|
|
|
261
264
|
* ```
|
|
262
265
|
*/
|
|
263
266
|
async function exportSpkiPem(e) {
|
|
264
|
-
return
|
|
267
|
+
return P("PUBLIC KEY", await exportSpkiDer(e));
|
|
265
268
|
}
|
|
266
269
|
/**
|
|
267
270
|
* Derive the matching public key from an imported (or generated) private key.
|
|
@@ -296,7 +299,7 @@ async function exportSpkiPem(e) {
|
|
|
296
299
|
async function derivePublicKey(e) {
|
|
297
300
|
if (e.type !== "private") throw Error("derivePublicKey requires a private CryptoKey");
|
|
298
301
|
if (!e.extractable) throw Error("Cannot derive public key from a non-extractable private key");
|
|
299
|
-
let t =
|
|
302
|
+
let t = x().subtle, n = e.algorithm.name === "RSA-OAEP" ? "encrypt" : "verify", r = await t.exportKey("jwk", e);
|
|
300
303
|
return t.importKey("jwk", toPublicJwk(r, n), e.algorithm, !0, [n]);
|
|
301
304
|
}
|
|
302
305
|
/**
|
|
@@ -311,8 +314,8 @@ async function derivePublicKey(e) {
|
|
|
311
314
|
* @see {@linkcode importPkcs8Base64} for private key import
|
|
312
315
|
*/
|
|
313
316
|
async function exportBinaryBase64(e) {
|
|
314
|
-
if (e.type === "public") return
|
|
315
|
-
if (e.type === "private") return
|
|
317
|
+
if (e.type === "public") return j(await exportSpkiDer(e));
|
|
318
|
+
if (e.type === "private") return j(await exportPkcs8Der(e));
|
|
316
319
|
throw Error("Cannot export secret/symmetric CryptoKey");
|
|
317
320
|
}
|
|
318
321
|
/**
|
|
@@ -336,25 +339,25 @@ async function importSpkiDerOrThrow(e, t) {
|
|
|
336
339
|
let n = parseSpkiDer(e), r;
|
|
337
340
|
t === void 0 ? r = inferKeyImportInput(n, "Unsupported SubjectPublicKeyInfo algorithm") : (assertSpkiMatchesRequestedAlgorithm(n, t), r = t);
|
|
338
341
|
try {
|
|
339
|
-
return await
|
|
342
|
+
return await x().subtle.importKey("spki", new Uint8Array(e), toImportAlgorithm(r), !0, publicKeyUsages(r));
|
|
340
343
|
} catch {
|
|
341
344
|
throw Error("Malformed SubjectPublicKeyInfo");
|
|
342
345
|
}
|
|
343
346
|
}
|
|
344
|
-
/** Runs a throwing key import and maps an EXPECTED failure to a `'malformed'` result; invariants rethrow. */
|
|
347
|
+
/** Runs a throwing key import and maps an EXPECTED failure to a `'malformed'` or `'limit_exceeded'` result; invariants rethrow. */
|
|
345
348
|
async function importResult(e) {
|
|
346
349
|
try {
|
|
347
|
-
return
|
|
350
|
+
return n(await e());
|
|
348
351
|
} catch (e) {
|
|
349
|
-
return
|
|
352
|
+
return t(e), i(e, r, "Malformed key");
|
|
350
353
|
}
|
|
351
354
|
}
|
|
352
355
|
/** Like {@link importResult} but distinguishes a typed wrong-password failure (`'invalid_password'`). */
|
|
353
|
-
async function encryptedImportResult(
|
|
356
|
+
async function encryptedImportResult(a) {
|
|
354
357
|
try {
|
|
355
|
-
return
|
|
356
|
-
} catch (
|
|
357
|
-
return
|
|
358
|
+
return n(await a());
|
|
359
|
+
} catch (n) {
|
|
360
|
+
return t(n), D(n) ? e("invalid_password", n.message) : E(n) ? e("kdf_iterations_exceeded", n.message) : i(n, r, "Malformed encrypted key");
|
|
358
361
|
}
|
|
359
362
|
}
|
|
360
363
|
/**
|
|
@@ -379,7 +382,7 @@ function importSpkiDer(e, t) {
|
|
|
379
382
|
* @see {@linkcode exportSpkiPem} for the inverse operation
|
|
380
383
|
*/
|
|
381
384
|
function importSpkiPemOrThrow(e, t) {
|
|
382
|
-
return importSpkiDerOrThrow(
|
|
385
|
+
return importSpkiDerOrThrow(N("PUBLIC KEY", e), t);
|
|
383
386
|
}
|
|
384
387
|
/**
|
|
385
388
|
* Import a public key from PEM-encoded SubjectPublicKeyInfo.
|
|
@@ -398,7 +401,7 @@ function importSpkiPem(e, t) {
|
|
|
398
401
|
function importSpkiBase64OrThrow(e, t) {
|
|
399
402
|
let n;
|
|
400
403
|
try {
|
|
401
|
-
n =
|
|
404
|
+
n = A(e);
|
|
402
405
|
} catch {
|
|
403
406
|
throw Error("Invalid base64 SubjectPublicKeyInfo");
|
|
404
407
|
}
|
|
@@ -420,11 +423,17 @@ function importSpkiBase64(e, t) {
|
|
|
420
423
|
* keys whose type isn't known ahead of time. Pass `algorithm` to additionally
|
|
421
424
|
* assert that the DER matches an expected algorithm.
|
|
422
425
|
*
|
|
426
|
+
* An RFC 5958 v2 `OneAsymmetricKey` carrying `attributes [0]` or `publicKey [1]`
|
|
427
|
+
* is reduced to its v1 fields for the platform import, which Bun 1.3.14 and
|
|
428
|
+
* earlier rejects outright
|
|
429
|
+
* ([oven-sh/bun#35432](https://github.com/oven-sh/bun/issues/35432)). A
|
|
430
|
+
* `publicKey [1]` field is checked against the private key it accompanies.
|
|
431
|
+
*
|
|
423
432
|
* @param der - DER-encoded PKCS#8 PrivateKeyInfo bytes
|
|
424
433
|
* @param algorithm - Optional expected algorithm; must match key contents when given
|
|
425
434
|
* @returns Extractable CryptoKey with `sign` usage
|
|
426
435
|
*
|
|
427
|
-
* @throws {Error} If DER is malformed, encodes an unsupported algorithm,
|
|
436
|
+
* @throws {Error} If DER is malformed, encodes an unsupported algorithm, doesn't match `algorithm`, or carries a `publicKey [1]` that is not the private key's own
|
|
428
437
|
*
|
|
429
438
|
* @see {@linkcode exportPkcs8Der} for the inverse operation
|
|
430
439
|
* @see {@linkcode importPkcs8Pem} for PEM input
|
|
@@ -434,16 +443,33 @@ async function importPkcs8DerOrThrow(e, t) {
|
|
|
434
443
|
let n;
|
|
435
444
|
try {
|
|
436
445
|
n = parsePkcs8PrivateKey(e);
|
|
437
|
-
} catch {
|
|
438
|
-
throw Error("Malformed PKCS#8 private key");
|
|
446
|
+
} catch (e) {
|
|
447
|
+
throw a(e, r), Error("Malformed PKCS#8 private key");
|
|
439
448
|
}
|
|
440
|
-
let
|
|
441
|
-
t === void 0 ?
|
|
449
|
+
let i;
|
|
450
|
+
t === void 0 ? i = inferKeyImportInput(n, "Unsupported PKCS#8 private key algorithm") : (assertPkcs8MatchesRequestedAlgorithm(n, t), i = t);
|
|
451
|
+
let o;
|
|
442
452
|
try {
|
|
443
|
-
|
|
453
|
+
o = await x().subtle.importKey("pkcs8", new Uint8Array(n.v1Der), toImportAlgorithm(i), !0, privateKeyUsages(i));
|
|
444
454
|
} catch {
|
|
445
455
|
throw Error("Malformed PKCS#8 private key");
|
|
446
456
|
}
|
|
457
|
+
let s = n.declaredPublicKey;
|
|
458
|
+
return s !== void 0 && await assertDeclaredPublicKeyMatches(o, s), o;
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Reject a OneAsymmetricKey whose `publicKey [1]` field is not the public key of
|
|
462
|
+
* its own `privateKey`.
|
|
463
|
+
*
|
|
464
|
+
* RFC 8410 Appendix A: "Key mismatch errors: If a public key is provided, it may
|
|
465
|
+
* not agree with the private key because either it is wrong or the wrong
|
|
466
|
+
* algorithm was used."
|
|
467
|
+
*/
|
|
468
|
+
async function assertDeclaredPublicKeyMatches(e, t) {
|
|
469
|
+
let n = await exportSpkiDer(await derivePublicKey(e)), r = f(n)[1];
|
|
470
|
+
if (r === void 0 || r.tag !== 3) throw Error("Malformed PKCS#8 private key");
|
|
471
|
+
let i = r.value.slice(1);
|
|
472
|
+
if (i.length !== t.length || i.some((e, n) => e !== t[n])) throw Error("PKCS#8 publicKey does not match the private key");
|
|
447
473
|
}
|
|
448
474
|
/**
|
|
449
475
|
* Import a private key from DER-encoded PKCS#8 PrivateKeyInfo.
|
|
@@ -466,7 +492,7 @@ function importPkcs8Der(e, t) {
|
|
|
466
492
|
* ```
|
|
467
493
|
*/
|
|
468
494
|
function importPkcs8PemOrThrow(e, t) {
|
|
469
|
-
return importPkcs8DerOrThrow(
|
|
495
|
+
return importPkcs8DerOrThrow(N("PRIVATE KEY", e), t);
|
|
470
496
|
}
|
|
471
497
|
/**
|
|
472
498
|
* Import a private key from PEM-encoded PKCS#8 PrivateKeyInfo.
|
|
@@ -487,35 +513,52 @@ function importPkcs8Pem(e, t) {
|
|
|
487
513
|
* @param der - DER-encoded EncryptedPrivateKeyInfo bytes
|
|
488
514
|
* @param password - Decryption password
|
|
489
515
|
* @param algorithm - Optional expected algorithm; must match decrypted key when given
|
|
516
|
+
* @param options - KDF work-factor limit
|
|
490
517
|
*
|
|
491
|
-
* @throws {Error} If DER is malformed, password is wrong,
|
|
518
|
+
* @throws {Error} If DER is malformed, password is wrong, the PBKDF2 iteration
|
|
519
|
+
* count exceeds `options.maxKdfIterations`, or algorithm doesn't match
|
|
492
520
|
*
|
|
493
521
|
* @see {@linkcode exportEncryptedPkcs8Der} for the inverse operation
|
|
494
522
|
*/
|
|
495
|
-
async function importEncryptedPkcs8DerOrThrow(e, t, n) {
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
let
|
|
523
|
+
async function importEncryptedPkcs8DerOrThrow(e, t, n, r) {
|
|
524
|
+
return await importEncryptedPkcs8WithBudget(e, t, n, C(r));
|
|
525
|
+
}
|
|
526
|
+
async function importEncryptedPkcs8WithBudget(e, t, n, r) {
|
|
527
|
+
let i = readEncryptedPkcs8Envelope(e), a = await w(i.algorithmIdentifierDer, i.encryptedData, t, r);
|
|
528
|
+
return assertDecryptedPrivateKey(() => parsePkcs8PrivateKey(a), "Invalid password or encrypted content"), importPkcs8DerOrThrow(a, n);
|
|
529
|
+
}
|
|
530
|
+
/** Splits a DER `EncryptedPrivateKeyInfo` into its AlgorithmIdentifier DER and encryptedData bytes. */
|
|
531
|
+
function readEncryptedPkcs8Envelope(e) {
|
|
532
|
+
let t;
|
|
505
533
|
try {
|
|
506
|
-
|
|
507
|
-
} catch {
|
|
508
|
-
throw
|
|
534
|
+
t = f(e);
|
|
535
|
+
} catch (e) {
|
|
536
|
+
throw a(e, r), Error("Malformed EncryptedPrivateKeyInfo");
|
|
509
537
|
}
|
|
510
|
-
|
|
538
|
+
let n = t[0], i = t[1];
|
|
539
|
+
if (t.length !== 2 || n === void 0 || i === void 0 || i.tag !== 4) throw Error("Malformed EncryptedPrivateKeyInfo");
|
|
540
|
+
return {
|
|
541
|
+
algorithmIdentifierDer: e.slice(n.start - n.headerLength, n.end),
|
|
542
|
+
encryptedData: i.value
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Reads the PBES2 encryption parameters of a DER PKCS#8
|
|
547
|
+
* `EncryptedPrivateKeyInfo` (RFC 5958 §3) without the password: PBKDF2
|
|
548
|
+
* iteration count, salt, and PRF, plus the AES-CBC variant and IV.
|
|
549
|
+
* Throws on malformed DER, a non-PBES2 encryption algorithm, or a PBKDF2
|
|
550
|
+
* iteration count outside 1 to 4294967295.
|
|
551
|
+
*/
|
|
552
|
+
function inspectEncryptedPkcs8Der(e) {
|
|
553
|
+
return O(readEncryptedPkcs8Envelope(e).algorithmIdentifierDer);
|
|
511
554
|
}
|
|
512
555
|
/**
|
|
513
556
|
* Import a private key from DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
|
|
514
557
|
*
|
|
515
558
|
* @see `importEncryptedPkcs8DerOrThrow` for the throwing variant
|
|
516
559
|
*/
|
|
517
|
-
function importEncryptedPkcs8Der(e, t, n) {
|
|
518
|
-
return encryptedImportResult(() => importEncryptedPkcs8DerOrThrow(e, t, n));
|
|
560
|
+
function importEncryptedPkcs8Der(e, t, n, r) {
|
|
561
|
+
return encryptedImportResult(() => importEncryptedPkcs8DerOrThrow(e, t, n, r));
|
|
519
562
|
}
|
|
520
563
|
/**
|
|
521
564
|
* Import a private key from PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
|
|
@@ -529,16 +572,17 @@ function importEncryptedPkcs8Der(e, t, n) {
|
|
|
529
572
|
* const inferred = await importEncryptedPkcs8PemOrThrow(pem, 'secret');
|
|
530
573
|
* ```
|
|
531
574
|
*/
|
|
532
|
-
function importEncryptedPkcs8PemOrThrow(e, t, n) {
|
|
533
|
-
|
|
575
|
+
async function importEncryptedPkcs8PemOrThrow(e, t, n, r) {
|
|
576
|
+
let i = C(r);
|
|
577
|
+
return await importEncryptedPkcs8WithBudget(N("ENCRYPTED PRIVATE KEY", e), t, n, i);
|
|
534
578
|
}
|
|
535
579
|
/**
|
|
536
580
|
* Import a private key from PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
|
|
537
581
|
*
|
|
538
582
|
* @see `importEncryptedPkcs8PemOrThrow` for the throwing variant
|
|
539
583
|
*/
|
|
540
|
-
function importEncryptedPkcs8Pem(e, t, n) {
|
|
541
|
-
return encryptedImportResult(() => importEncryptedPkcs8PemOrThrow(e, t, n));
|
|
584
|
+
function importEncryptedPkcs8Pem(e, t, n, r) {
|
|
585
|
+
return encryptedImportResult(() => importEncryptedPkcs8PemOrThrow(e, t, n, r));
|
|
542
586
|
}
|
|
543
587
|
/**
|
|
544
588
|
* Import an RSA private key from DER-encoded PKCS#1 RSAPrivateKey.
|
|
@@ -568,7 +612,7 @@ function importPkcs1Der(e, t = { kind: "rsa" }) {
|
|
|
568
612
|
* @see {@linkcode importEncryptedPkcs1Pem} for encrypted PEM
|
|
569
613
|
*/
|
|
570
614
|
function importPkcs1PemOrThrow(e, t = { kind: "rsa" }) {
|
|
571
|
-
return importPkcs1DerOrThrow(
|
|
615
|
+
return importPkcs1DerOrThrow(N("RSA PRIVATE KEY", e), t);
|
|
572
616
|
}
|
|
573
617
|
/**
|
|
574
618
|
* Import an RSA private key from PEM-encoded PKCS#1 RSAPrivateKey.
|
|
@@ -587,7 +631,8 @@ function importPkcs1Pem(e, t = { kind: "rsa" }) {
|
|
|
587
631
|
* @see {@linkcode importEncryptedPkcs8Pem} for modern PBES2 encryption
|
|
588
632
|
*/
|
|
589
633
|
async function importEncryptedPkcs1PemOrThrow(e, t, n = { kind: "rsa" }) {
|
|
590
|
-
|
|
634
|
+
let r = await decryptTraditionalPem("RSA PRIVATE KEY", e, t);
|
|
635
|
+
return assertDecryptedPrivateKey(() => parsePkcs1PrivateKey(r), "Invalid password or encrypted PEM content"), importPkcs1DerOrThrow(r, n);
|
|
591
636
|
}
|
|
592
637
|
/**
|
|
593
638
|
* Import an RSA private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1).
|
|
@@ -606,7 +651,7 @@ function importEncryptedPkcs1Pem(e, t, n = { kind: "rsa" }) {
|
|
|
606
651
|
function importPkcs8Base64OrThrow(e, t) {
|
|
607
652
|
let n;
|
|
608
653
|
try {
|
|
609
|
-
n =
|
|
654
|
+
n = A(e);
|
|
610
655
|
} catch {
|
|
611
656
|
throw Error("Invalid base64 PKCS#8 private key");
|
|
612
657
|
}
|
|
@@ -641,11 +686,11 @@ async function importSec1DerOrThrow(e, t) {
|
|
|
641
686
|
let n;
|
|
642
687
|
try {
|
|
643
688
|
n = parseSec1PrivateKey(e);
|
|
644
|
-
} catch {
|
|
645
|
-
throw Error("Malformed SEC 1 private key");
|
|
689
|
+
} catch (e) {
|
|
690
|
+
throw a(e, r), Error("Malformed SEC 1 private key");
|
|
646
691
|
}
|
|
647
|
-
let
|
|
648
|
-
return t === void 0 ?
|
|
692
|
+
let i;
|
|
693
|
+
return t === void 0 ? i = inferSec1ImportInput(n) : (assertSec1MatchesRequestedAlgorithm(n, t), i = t), await importPkcs8DerOrThrow(wrapSec1InPkcs8(e, i.curve), i);
|
|
649
694
|
}
|
|
650
695
|
/**
|
|
651
696
|
* Import an EC private key from DER-encoded SEC 1 ECPrivateKey.
|
|
@@ -666,7 +711,7 @@ function importSec1Der(e, t) {
|
|
|
666
711
|
* @see {@linkcode importEncryptedSec1Pem} for encrypted PEM
|
|
667
712
|
*/
|
|
668
713
|
function importSec1PemOrThrow(e, t) {
|
|
669
|
-
return importSec1DerOrThrow(
|
|
714
|
+
return importSec1DerOrThrow(N("EC PRIVATE KEY", e), t);
|
|
670
715
|
}
|
|
671
716
|
/**
|
|
672
717
|
* Import an EC private key from PEM-encoded SEC 1 ECPrivateKey.
|
|
@@ -685,7 +730,8 @@ function importSec1Pem(e, t) {
|
|
|
685
730
|
* @see {@linkcode importEncryptedPkcs8Pem} for modern PBES2 encryption
|
|
686
731
|
*/
|
|
687
732
|
async function importEncryptedSec1PemOrThrow(e, t, n) {
|
|
688
|
-
|
|
733
|
+
let r = await decryptTraditionalPem("EC PRIVATE KEY", e, t);
|
|
734
|
+
return assertDecryptedPrivateKey(() => parseSec1PrivateKey(r), "Invalid password or encrypted PEM content"), importSec1DerOrThrow(r, n);
|
|
689
735
|
}
|
|
690
736
|
/**
|
|
691
737
|
* Import an EC private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1).
|
|
@@ -715,7 +761,7 @@ async function importPublicJwkOrThrow(e, t) {
|
|
|
715
761
|
let n = t ?? inferJwkImportInput(e);
|
|
716
762
|
assertPublicJwkMatchesRequestedAlgorithm(e, n);
|
|
717
763
|
try {
|
|
718
|
-
return await
|
|
764
|
+
return await x().subtle.importKey("jwk", e, toImportAlgorithm(n), !0, publicKeyUsages(n));
|
|
719
765
|
} catch {
|
|
720
766
|
throw Error("Malformed public JWK");
|
|
721
767
|
}
|
|
@@ -754,7 +800,7 @@ async function importPrivateJwkOrThrow(e, t) {
|
|
|
754
800
|
let n = t ?? inferJwkImportInput(e);
|
|
755
801
|
assertPrivateJwkMatchesRequestedAlgorithm(e, n);
|
|
756
802
|
try {
|
|
757
|
-
return await
|
|
803
|
+
return await x().subtle.importKey("jwk", e, toImportAlgorithm(n), !0, privateKeyUsages(n));
|
|
758
804
|
} catch {
|
|
759
805
|
throw Error("Malformed private JWK");
|
|
760
806
|
}
|
|
@@ -794,13 +840,13 @@ function importPrivateJwk(e, t) {
|
|
|
794
840
|
* @see {@linkcode decryptRsaOaepOrThrow} for the inverse operation
|
|
795
841
|
* @see `encryptRsaOaep` for the Result-returning variant
|
|
796
842
|
*/
|
|
797
|
-
async function encryptRsaOaepOrThrow(e,
|
|
798
|
-
let
|
|
799
|
-
if (
|
|
843
|
+
async function encryptRsaOaepOrThrow(e, n, r = {}) {
|
|
844
|
+
let i = validateRsaOaepKey(e, "public", "encrypt");
|
|
845
|
+
if (i !== void 0) throw Error(i);
|
|
800
846
|
try {
|
|
801
|
-
return new Uint8Array(await
|
|
847
|
+
return new Uint8Array(await x().subtle.encrypt(toRsaOaepParams(r), e, v(n)));
|
|
802
848
|
} catch (e) {
|
|
803
|
-
throw
|
|
849
|
+
throw t(e), Error("Plaintext exceeds the RSA-OAEP capacity of the key (modulus bytes − 2 × hash bytes − 2)");
|
|
804
850
|
}
|
|
805
851
|
}
|
|
806
852
|
/**
|
|
@@ -819,13 +865,13 @@ async function encryptRsaOaepOrThrow(e, t, n = {}) {
|
|
|
819
865
|
*
|
|
820
866
|
* @see `encryptRsaOaepOrThrow` for the throwing variant
|
|
821
867
|
*/
|
|
822
|
-
async function encryptRsaOaep(
|
|
823
|
-
let
|
|
824
|
-
if (
|
|
868
|
+
async function encryptRsaOaep(r, i, a = {}) {
|
|
869
|
+
let o = validateRsaOaepKey(r, "public", "encrypt");
|
|
870
|
+
if (o !== void 0) return e("invalid_key", o);
|
|
825
871
|
try {
|
|
826
|
-
return
|
|
827
|
-
} catch (
|
|
828
|
-
return
|
|
872
|
+
return n(await encryptRsaOaepOrThrow(r, i, a));
|
|
873
|
+
} catch (n) {
|
|
874
|
+
return t(n), e("message_too_long", n instanceof Error ? n.message : "Plaintext exceeds the RSA-OAEP capacity");
|
|
829
875
|
}
|
|
830
876
|
}
|
|
831
877
|
/**
|
|
@@ -850,13 +896,13 @@ async function encryptRsaOaep(e, t, n = {}) {
|
|
|
850
896
|
* @see {@linkcode encryptRsaOaepOrThrow} for the inverse operation
|
|
851
897
|
* @see `decryptRsaOaep` for the Result-returning variant
|
|
852
898
|
*/
|
|
853
|
-
async function decryptRsaOaepOrThrow(e,
|
|
854
|
-
let
|
|
855
|
-
if (
|
|
899
|
+
async function decryptRsaOaepOrThrow(e, n, r = {}) {
|
|
900
|
+
let i = validateRsaOaepKey(e, "private", "decrypt");
|
|
901
|
+
if (i !== void 0) throw Error(i);
|
|
856
902
|
try {
|
|
857
|
-
return new Uint8Array(await
|
|
903
|
+
return new Uint8Array(await x().subtle.decrypt(toRsaOaepParams(r), e, v(n)));
|
|
858
904
|
} catch (e) {
|
|
859
|
-
throw
|
|
905
|
+
throw t(e), Error("RSA-OAEP decryption failed: wrong key, wrong label, or corrupted ciphertext");
|
|
860
906
|
}
|
|
861
907
|
}
|
|
862
908
|
/**
|
|
@@ -874,13 +920,13 @@ async function decryptRsaOaepOrThrow(e, t, n = {}) {
|
|
|
874
920
|
*
|
|
875
921
|
* @see `decryptRsaOaepOrThrow` for the throwing variant
|
|
876
922
|
*/
|
|
877
|
-
async function decryptRsaOaep(
|
|
878
|
-
let
|
|
879
|
-
if (
|
|
923
|
+
async function decryptRsaOaep(r, i, a = {}) {
|
|
924
|
+
let o = validateRsaOaepKey(r, "private", "decrypt");
|
|
925
|
+
if (o !== void 0) return e("invalid_key", o);
|
|
880
926
|
try {
|
|
881
|
-
return
|
|
882
|
-
} catch (
|
|
883
|
-
return
|
|
927
|
+
return n(await decryptRsaOaepOrThrow(r, i, a));
|
|
928
|
+
} catch (n) {
|
|
929
|
+
return t(n), e("decryption_failed", n instanceof Error ? n.message : "RSA-OAEP decryption failed");
|
|
884
930
|
}
|
|
885
931
|
}
|
|
886
932
|
/** Map a {@linkcode KeyAlgorithmInput} to the WebCrypto `generateKey` algorithm parameter. */
|
|
@@ -897,6 +943,7 @@ function toGenerateKeyAlgorithm(e) {
|
|
|
897
943
|
namedCurve: e.curve ?? "P-256"
|
|
898
944
|
};
|
|
899
945
|
case "ed25519": return { name: "Ed25519" };
|
|
946
|
+
default: throw Error(`Unhandled KeyAlgorithmInput kind: ${String(e)}`);
|
|
900
947
|
}
|
|
901
948
|
}
|
|
902
949
|
/** Map a {@linkcode PublicKeyImportInput} to the WebCrypto `importKey` algorithm parameter. */
|
|
@@ -911,37 +958,152 @@ function toImportAlgorithm(e) {
|
|
|
911
958
|
namedCurve: e.curve
|
|
912
959
|
};
|
|
913
960
|
case "ed25519": return { name: "Ed25519" };
|
|
961
|
+
default: throw Error(`Unhandled PublicKeyImportInput kind: ${String(e)}`);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* Rejects decrypted plaintext that is not the private-key structure it should be.
|
|
966
|
+
*
|
|
967
|
+
* PBES2 and traditional PEM both encrypt with unauthenticated AES-CBC, where a
|
|
968
|
+
* wrong key clears the padding check roughly once in every 256 attempts and
|
|
969
|
+
* yields random plaintext. Structure is the only integrity signal left, so
|
|
970
|
+
* plaintext that fails to parse means the password was wrong or the ciphertext
|
|
971
|
+
* was corrupted, not that the enclosing input was malformed.
|
|
972
|
+
*/
|
|
973
|
+
function assertDecryptedPrivateKey(e, t) {
|
|
974
|
+
try {
|
|
975
|
+
e();
|
|
976
|
+
} catch (e) {
|
|
977
|
+
throw a(e, r), k(t);
|
|
914
978
|
}
|
|
915
979
|
}
|
|
980
|
+
/** Structural check for a PKCS#1 RSAPrivateKey, including optional multiprime fields. */
|
|
981
|
+
function parsePkcs1PrivateKey(e) {
|
|
982
|
+
let t = f(e), n = t[0];
|
|
983
|
+
if (n === void 0 || n.tag !== 2 || n.value.length !== 1 || !t.slice(1, 9).every((e) => e.tag === 2)) throw Error("Malformed PKCS#1 private key");
|
|
984
|
+
let r = n.value[0];
|
|
985
|
+
if (r === 0 && t.length === 9) return;
|
|
986
|
+
let i = t[9];
|
|
987
|
+
if (r !== 1 || t.length !== 10 || i === void 0 || !isOtherPrimeInfosShape(e, i)) throw Error("Malformed PKCS#1 private key");
|
|
988
|
+
}
|
|
989
|
+
/** RFC 8017 Appendix A.1.2: OtherPrimeInfos is a non-empty SEQUENCE OF three-INTEGER entries. */
|
|
990
|
+
function isOtherPrimeInfosShape(e, t) {
|
|
991
|
+
if (t.tag !== 48) return !1;
|
|
992
|
+
let n = e.slice(t.start - t.headerLength, t.end), r = f(n);
|
|
993
|
+
return r.length !== 0 && r.every((e) => {
|
|
994
|
+
if (e.tag !== 48) return !1;
|
|
995
|
+
let t = f(n.slice(e.start - e.headerLength, e.end));
|
|
996
|
+
return t.length === 3 && t.every((e) => e.tag === 2);
|
|
997
|
+
});
|
|
998
|
+
}
|
|
916
999
|
/** Extract algorithm OID and inner key bytes from a PKCS#8 PrivateKeyInfo envelope. */
|
|
917
1000
|
function parsePkcs8PrivateKey(e) {
|
|
918
|
-
let t =
|
|
919
|
-
if (t.length < 3 ||
|
|
920
|
-
let
|
|
921
|
-
if (
|
|
922
|
-
let c =
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
1001
|
+
let t = f(e), n = t[0], r = t[1], i = t[2];
|
|
1002
|
+
if (t.length < 3 || n === void 0 || n.tag !== 2 || r === void 0 || r.tag !== 48 || i === void 0 || i.tag !== 4) throw Error("Malformed PKCS#8 private key");
|
|
1003
|
+
let a = validateOneAsymmetricKeyTail(t.slice(3));
|
|
1004
|
+
if (readPkcs8Version(n.value) !== (a === void 0 ? 0 : 1)) throw Error("Malformed PKCS#8 private key");
|
|
1005
|
+
let o = e.slice(r.start - r.headerLength, r.end), c = f(o), l = c[0];
|
|
1006
|
+
if (l === void 0 || l.tag !== 6 || c.length < 1 || c.length > 2) throw Error("Malformed PKCS#8 private key");
|
|
1007
|
+
let d = c[1], m = h(l.value);
|
|
1008
|
+
return I.includes(m) && assertCurvePrivateKey(i.value), {
|
|
1009
|
+
algorithmOid: m,
|
|
1010
|
+
...d === void 0 ? {} : { parametersTag: d.tag },
|
|
1011
|
+
...d?.tag === 6 ? { parametersOid: h(d.value) } : {},
|
|
1012
|
+
privateKeyDer: i.value,
|
|
1013
|
+
v1Der: t.length === 3 ? e : p([
|
|
1014
|
+
s(Uint8Array.of(0)),
|
|
1015
|
+
o,
|
|
1016
|
+
u(i.value)
|
|
1017
|
+
]),
|
|
1018
|
+
...a === void 0 ? {} : { declaredPublicKey: a }
|
|
928
1019
|
};
|
|
929
1020
|
}
|
|
1021
|
+
/** Algorithms whose `privateKey` field holds an RFC 8410 §7 `CurvePrivateKey`. */
|
|
1022
|
+
const I = [
|
|
1023
|
+
b.x25519,
|
|
1024
|
+
b.x448,
|
|
1025
|
+
b.ed25519,
|
|
1026
|
+
b.ed448
|
|
1027
|
+
];
|
|
1028
|
+
/**
|
|
1029
|
+
* Reject a `privateKey` field that does not hold exactly one DER `CurvePrivateKey`.
|
|
1030
|
+
*
|
|
1031
|
+
* RFC 8410 §7: "when encoding a OneAsymmetricKey object, the private key is
|
|
1032
|
+
* wrapped in a CurvePrivateKey object and wrapped by the OCTET STRING of the
|
|
1033
|
+
* 'privateKey' field", and `CurvePrivateKey ::= OCTET STRING`.
|
|
1034
|
+
*/
|
|
1035
|
+
function assertCurvePrivateKey(e) {
|
|
1036
|
+
if (d(e).tag !== 4) throw Error("Malformed PKCS#8 private key");
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* Validate the OneAsymmetricKey tail after `privateKey` and return the
|
|
1040
|
+
* `publicKey [1]` octets when the field is present.
|
|
1041
|
+
*
|
|
1042
|
+
* RFC 5958 §2 encodes `attributes [0]` as an IMPLICIT constructed `SET OF`
|
|
1043
|
+
* (tag `A0`); RFC 8410 §7 adds `publicKey [1]` as an IMPLICIT primitive
|
|
1044
|
+
* `BIT STRING` (tag `81`) after it. Each appears at most once and in that order.
|
|
1045
|
+
* The type's two X.680 extensibility markers admit later additions, which take
|
|
1046
|
+
* the next context-specific numbers, so `[2]` and above are tolerated and every
|
|
1047
|
+
* other class is malformed.
|
|
1048
|
+
*/
|
|
1049
|
+
function validateOneAsymmetricKeyTail(e) {
|
|
1050
|
+
let t = !1, n, r = !1;
|
|
1051
|
+
for (let i of e) {
|
|
1052
|
+
let e = (i.tag & 192) == 128 ? i.tagNumber : -1;
|
|
1053
|
+
if (e === 0) {
|
|
1054
|
+
if (t || n !== void 0 || r || i.tag !== 160) throw Error("Malformed PKCS#8 private key");
|
|
1055
|
+
assertOneAsymmetricKeyAttributes(i.value), t = !0;
|
|
1056
|
+
} else if (e === 1) {
|
|
1057
|
+
if (n !== void 0 || r || i.tag !== 129) throw Error("Malformed PKCS#8 private key");
|
|
1058
|
+
n = readPublicKeyBitString(i.value);
|
|
1059
|
+
} else if (e >= 2) r = !0;
|
|
1060
|
+
else throw Error("Malformed PKCS#8 private key");
|
|
1061
|
+
}
|
|
1062
|
+
return n;
|
|
1063
|
+
}
|
|
1064
|
+
/**
|
|
1065
|
+
* Reject an `attributes [0]` field that does not hold a `SET OF Attribute`.
|
|
1066
|
+
*
|
|
1067
|
+
* RFC 5958 §2 types the field `Attributes ::= SET OF Attribute`, over the
|
|
1068
|
+
* `Attribute ::= SEQUENCE { attrType OBJECT IDENTIFIER, attrValues SET OF
|
|
1069
|
+
* AttributeValue }` of RFC 5652 §5.3. The implicit tag has replaced the SET OF
|
|
1070
|
+
* tag, so the members are read back under a universal constructed one.
|
|
1071
|
+
*/
|
|
1072
|
+
function assertOneAsymmetricKeyAttributes(e) {
|
|
1073
|
+
let t = m(48, e);
|
|
1074
|
+
for (let e of f(t)) {
|
|
1075
|
+
if (e.tag !== 48) throw Error("Malformed PKCS#8 private key");
|
|
1076
|
+
let n = f(t.slice(e.start - e.headerLength, e.end)), r = n[0], i = n[1];
|
|
1077
|
+
if (n.length !== 2 || r === void 0 || r.tag !== 6 || i === void 0 || i.tag !== 49) throw Error("Malformed PKCS#8 private key");
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
/** Reject a `publicKey [1]` BIT STRING that is not octet-aligned or carries no key octets. */
|
|
1081
|
+
function readPublicKeyBitString(e) {
|
|
1082
|
+
if (e.length < 2 || e[0] !== 0) throw Error("Malformed PKCS#8 private key");
|
|
1083
|
+
return e.slice(1);
|
|
1084
|
+
}
|
|
1085
|
+
/** Decode the canonical RFC 5958 version INTEGER content, `v1(0)` or `v2(1)`. */
|
|
1086
|
+
function readPkcs8Version(e) {
|
|
1087
|
+
let t = e[0];
|
|
1088
|
+
if (e.length !== 1 || t !== 0 && t !== 1) throw Error("Malformed PKCS#8 private key");
|
|
1089
|
+
return t;
|
|
1090
|
+
}
|
|
930
1091
|
/**
|
|
931
1092
|
* Extract the optional `parameters [0]` curve identifier from a SEC 1 ECPrivateKey.
|
|
932
1093
|
*
|
|
933
|
-
* RFC 5915: `ECPrivateKey ::= SEQUENCE { version INTEGER
|
|
934
|
-
*
|
|
1094
|
+
* RFC 5915 §3: `ECPrivateKey ::= SEQUENCE { version INTEGER { ecPrivkeyVer1(1) }
|
|
1095
|
+
* (ecPrivkeyVer1), privateKey OCTET STRING, parameters [0] ECParameters
|
|
1096
|
+
* {{ NamedCurve }} OPTIONAL, publicKey [1] BIT STRING OPTIONAL }`.
|
|
935
1097
|
*/
|
|
936
1098
|
function parseSec1PrivateKey(e) {
|
|
937
|
-
let t =
|
|
938
|
-
if (t.length < 2 || t.length > 4 || n === void 0 || n.tag !== 2 || r === void 0 || r.tag !== 4 ||
|
|
939
|
-
let
|
|
940
|
-
if (
|
|
941
|
-
let
|
|
1099
|
+
let t = f(e), n = t[0], r = t[1], i = t[2], a = t[3];
|
|
1100
|
+
if (t.length < 2 || t.length > 4 || n === void 0 || n.tag !== 2 || n.value.length !== 1 || n.value[0] !== 1 || r === void 0 || r.tag !== 4 || i !== void 0 && i.tag !== 160 && i.tag !== 161 || a !== void 0 && (i?.tag !== 160 || a.tag !== 161)) throw Error("Malformed SEC 1 private key");
|
|
1101
|
+
let o = i?.tag === 160 ? i : void 0;
|
|
1102
|
+
if (o === void 0) return {};
|
|
1103
|
+
let s = d(o.value);
|
|
942
1104
|
return {
|
|
943
|
-
parametersTag:
|
|
944
|
-
...
|
|
1105
|
+
parametersTag: s.tag,
|
|
1106
|
+
...s.tag === 6 ? { parametersOid: h(s.value) } : {}
|
|
945
1107
|
};
|
|
946
1108
|
}
|
|
947
1109
|
/**
|
|
@@ -951,31 +1113,31 @@ function parseSec1PrivateKey(e) {
|
|
|
951
1113
|
* standalone SEC 1 — and OpenSSL always writes it. Injecting it keeps exports
|
|
952
1114
|
* self-describing, so they re-import without an explicit curve.
|
|
953
1115
|
*/
|
|
954
|
-
function ensureSec1NamedCurveParameters(
|
|
955
|
-
let
|
|
956
|
-
if (
|
|
957
|
-
if (
|
|
958
|
-
let raw = (
|
|
959
|
-
return
|
|
960
|
-
...
|
|
961
|
-
|
|
962
|
-
...
|
|
1116
|
+
function ensureSec1NamedCurveParameters(e, t) {
|
|
1117
|
+
let n = f(e);
|
|
1118
|
+
if (n.some((e) => e.tag === 160)) return new Uint8Array(e);
|
|
1119
|
+
if (t === void 0) throw Error("SEC1 export requires a named curve in the PKCS#8 privateKeyAlgorithm");
|
|
1120
|
+
let raw = (t) => e.slice(t.start - t.headerLength, t.end);
|
|
1121
|
+
return p([
|
|
1122
|
+
...n.slice(0, 2).map(raw),
|
|
1123
|
+
o(0, l(t)),
|
|
1124
|
+
...n.slice(2).map(raw)
|
|
963
1125
|
]);
|
|
964
1126
|
}
|
|
965
1127
|
/** Wrap a PKCS#1 RSAPrivateKey in a PKCS#8 PrivateKeyInfo envelope for WebCrypto import. */
|
|
966
1128
|
function wrapPkcs1InPkcs8(e) {
|
|
967
|
-
return
|
|
1129
|
+
return p([
|
|
968
1130
|
Uint8Array.of(2, 1, 0),
|
|
969
|
-
|
|
970
|
-
|
|
1131
|
+
p([l(b.rsaEncryption), c()]),
|
|
1132
|
+
u(new Uint8Array(e))
|
|
971
1133
|
]);
|
|
972
1134
|
}
|
|
973
1135
|
/** Wrap a SEC 1 ECPrivateKey in a PKCS#8 PrivateKeyInfo envelope for WebCrypto import. */
|
|
974
1136
|
function wrapSec1InPkcs8(e, t) {
|
|
975
|
-
return
|
|
1137
|
+
return p([
|
|
976
1138
|
Uint8Array.of(2, 1, 0),
|
|
977
|
-
|
|
978
|
-
|
|
1139
|
+
p([l(b.ecPublicKey), l(curveToOid(t))]),
|
|
1140
|
+
u(new Uint8Array(e))
|
|
979
1141
|
]);
|
|
980
1142
|
}
|
|
981
1143
|
/** Project a private JWK onto its public-only members, discarding private key material. */
|
|
@@ -998,19 +1160,19 @@ function toPublicJwk(e, t) {
|
|
|
998
1160
|
* Single source of truth for the NIST curve ↔ named-curve OID mapping, read in
|
|
999
1161
|
* both directions by {@linkcode curveToOid} and {@linkcode oidToCurve}.
|
|
1000
1162
|
*/
|
|
1001
|
-
const
|
|
1002
|
-
["P-256",
|
|
1003
|
-
["P-384",
|
|
1004
|
-
["P-521",
|
|
1163
|
+
const L = [
|
|
1164
|
+
["P-256", b.prime256v1],
|
|
1165
|
+
["P-384", b.secp384r1],
|
|
1166
|
+
["P-521", b.secp521r1]
|
|
1005
1167
|
];
|
|
1006
1168
|
/** Map a curve name to its ASN.1 OID string. */
|
|
1007
1169
|
function curveToOid(e) {
|
|
1008
|
-
for (let [t, n] of
|
|
1170
|
+
for (let [t, n] of L) if (t === e) return n;
|
|
1009
1171
|
throw Error(`Unsupported EC curve: ${e}`);
|
|
1010
1172
|
}
|
|
1011
1173
|
/** Map an ASN.1 curve OID string back to its curve name, or `undefined` if unrecognized. */
|
|
1012
1174
|
function oidToCurve(e) {
|
|
1013
|
-
for (let [t, n] of
|
|
1175
|
+
for (let [t, n] of L) if (n === e) return t;
|
|
1014
1176
|
}
|
|
1015
1177
|
/** Map an {@linkcode RsaScheme} to the WebCrypto algorithm name string. */
|
|
1016
1178
|
function rsaSchemeToWebCryptoAlgorithmName(e) {
|
|
@@ -1045,53 +1207,63 @@ function validateRsaOaepKey(e, t, n) {
|
|
|
1045
1207
|
function toRsaOaepParams(e) {
|
|
1046
1208
|
return {
|
|
1047
1209
|
name: "RSA-OAEP",
|
|
1048
|
-
...e.label === void 0 ? {} : { label:
|
|
1210
|
+
...e.label === void 0 ? {} : { label: v(e.label) }
|
|
1049
1211
|
};
|
|
1050
1212
|
}
|
|
1051
1213
|
/** Encrypt DER key material as an OpenSSL-style `Proc-Type: 4,ENCRYPTED` PEM block. */
|
|
1052
1214
|
async function encryptTraditionalPem(e, t, n) {
|
|
1053
|
-
let r = n.iv ??
|
|
1215
|
+
let r = n.iv ?? x().getRandomValues(/* @__PURE__ */ new Uint8Array(16));
|
|
1054
1216
|
if (r.length !== 16) throw Error("Traditional PEM encryption requires a 16-byte IV");
|
|
1055
|
-
let i = n.cipher ?? "AES-256-CBC", a = await importTraditionalPemAesKey(n.password, r.slice(0, 8), i, ["encrypt"]), o =
|
|
1217
|
+
let i = n.cipher ?? "AES-256-CBC", a = await importTraditionalPemAesKey(n.password, r.slice(0, 8), i, ["encrypt"]), o = new Uint8Array(await x().subtle.encrypt({
|
|
1056
1218
|
name: "AES-CBC",
|
|
1057
|
-
iv:
|
|
1058
|
-
}, a,
|
|
1219
|
+
iv: v(r)
|
|
1220
|
+
}, a, v(t))), s = j(o).match(/.{1,64}/g)?.join("\n") ?? "";
|
|
1059
1221
|
return [
|
|
1060
1222
|
`-----BEGIN ${e}-----`,
|
|
1061
1223
|
"Proc-Type: 4,ENCRYPTED",
|
|
1062
|
-
`DEK-Info: ${i},${
|
|
1224
|
+
`DEK-Info: ${i},${y(r).toUpperCase()}`,
|
|
1063
1225
|
"",
|
|
1064
|
-
|
|
1226
|
+
s,
|
|
1065
1227
|
`-----END ${e}-----`
|
|
1066
1228
|
].join("\n");
|
|
1067
1229
|
}
|
|
1068
|
-
/** Decrypt
|
|
1230
|
+
/** Decrypt a `Proc-Type: 4,ENCRYPTED` PEM block, RFC 1421 §4.6 headers, to plaintext DER. */
|
|
1069
1231
|
async function decryptTraditionalPem(e, t, n) {
|
|
1070
|
-
let r =
|
|
1232
|
+
let r = M(t);
|
|
1071
1233
|
if (r.label !== e) throw Error(`Expected ${e} PEM block`);
|
|
1072
|
-
let i = r.headers.get("DEK-Info");
|
|
1073
|
-
if (
|
|
1074
|
-
let [
|
|
1075
|
-
if (!isTraditionalPemCipher(
|
|
1076
|
-
if (!isTraditionalPemIvHex(
|
|
1077
|
-
let
|
|
1234
|
+
let i = r.headers.get("DEK-Info"), a = r.headers.get("Proc-Type");
|
|
1235
|
+
if (a === void 0 || !isEncryptedProcType(a) || i === void 0) throw Error("Traditional PEM encryption headers missing");
|
|
1236
|
+
let [o, s] = i.split(",").map(F);
|
|
1237
|
+
if (!isTraditionalPemCipher(o) || s === void 0) throw Error("Only AES-128-CBC, AES-192-CBC, and AES-256-CBC traditional PEM encryption is supported");
|
|
1238
|
+
if (!isTraditionalPemIvHex(s)) throw Error("Traditional PEM encryption requires a 16-byte IV encoded as 32 hex characters");
|
|
1239
|
+
let c = _(s), l = await importTraditionalPemAesKey(n, c.slice(0, 8), o, ["decrypt"]);
|
|
1078
1240
|
try {
|
|
1079
|
-
return new Uint8Array(await
|
|
1241
|
+
return new Uint8Array(await x().subtle.decrypt({
|
|
1080
1242
|
name: "AES-CBC",
|
|
1081
|
-
iv:
|
|
1082
|
-
},
|
|
1243
|
+
iv: v(c)
|
|
1244
|
+
}, l, v(A(r.base64Body))));
|
|
1083
1245
|
} catch {
|
|
1084
|
-
throw
|
|
1246
|
+
throw k("Invalid password or encrypted PEM content");
|
|
1085
1247
|
}
|
|
1086
1248
|
}
|
|
1087
1249
|
/** Derive and import an AES-CBC key for legacy PEM encryption using OpenSSL `EVP_BytesToKey`. */
|
|
1088
1250
|
function importTraditionalPemAesKey(e, t, n, r) {
|
|
1089
1251
|
let i = traditionalPemCipherKeyLength(n), a = opensslBytesToKey(e, t, i / 8);
|
|
1090
|
-
return
|
|
1252
|
+
return x().subtle.importKey("raw", v(a), {
|
|
1091
1253
|
name: "AES-CBC",
|
|
1092
1254
|
length: i
|
|
1093
1255
|
}, !1, r);
|
|
1094
1256
|
}
|
|
1257
|
+
/**
|
|
1258
|
+
* RFC 1421 §4.6.1.1 `Proc-Type: 4,ENCRYPTED`.
|
|
1259
|
+
*
|
|
1260
|
+
* Fields are trimmed because RFC 822 §3.1.1 unfolding leaves an LWSP-char
|
|
1261
|
+
* wherever the header was folded.
|
|
1262
|
+
*/
|
|
1263
|
+
function isEncryptedProcType(e) {
|
|
1264
|
+
let t = e.split(",");
|
|
1265
|
+
return t.length === 2 && F(t[0] ?? "") === "4" && F(t[1] ?? "") === "ENCRYPTED";
|
|
1266
|
+
}
|
|
1095
1267
|
/** Type guard for the three AES-CBC ciphers supported by legacy PEM encryption. */
|
|
1096
1268
|
function isTraditionalPemCipher(e) {
|
|
1097
1269
|
return e === "AES-128-CBC" || e === "AES-192-CBC" || e === "AES-256-CBC";
|
|
@@ -1105,6 +1277,7 @@ function traditionalPemCipherKeyLength(e) {
|
|
|
1105
1277
|
case "AES-128-CBC": return 128;
|
|
1106
1278
|
case "AES-192-CBC": return 192;
|
|
1107
1279
|
case "AES-256-CBC": return 256;
|
|
1280
|
+
default: throw Error(`Unhandled traditional PEM cipher: ${String(e)}`);
|
|
1108
1281
|
}
|
|
1109
1282
|
}
|
|
1110
1283
|
/** OpenSSL `EVP_BytesToKey` with MD5 — derives a fixed-length key from password + salt. */
|
|
@@ -1112,7 +1285,7 @@ function opensslBytesToKey(e, t, n) {
|
|
|
1112
1285
|
let r = new TextEncoder().encode(e), i = [], a = /* @__PURE__ */ new Uint8Array(), o = 0;
|
|
1113
1286
|
for (; o < n;) {
|
|
1114
1287
|
let e = new Uint8Array(a.length + r.length + t.length);
|
|
1115
|
-
e.set(a, 0), e.set(r, a.length), e.set(t, a.length + r.length), a =
|
|
1288
|
+
e.set(a, 0), e.set(r, a.length), e.set(t, a.length + r.length), a = S(e), i.push(a), o += a.length;
|
|
1116
1289
|
}
|
|
1117
1290
|
let s = new Uint8Array(n), c = 0;
|
|
1118
1291
|
for (let e of i) {
|
|
@@ -1121,49 +1294,23 @@ function opensslBytesToKey(e, t, n) {
|
|
|
1121
1294
|
}
|
|
1122
1295
|
return s;
|
|
1123
1296
|
}
|
|
1124
|
-
/** Parse a PEM block into its label, RFC 1421 headers, and base64 body. */
|
|
1125
|
-
function parseTraditionalPem(e) {
|
|
1126
|
-
let t = e.replace(/\r/g, "").trim().split("\n"), n = t[0], r = t[t.length - 1];
|
|
1127
|
-
if (n === void 0 || r === void 0 || !n.startsWith("-----BEGIN ") || !r.startsWith("-----END ")) throw Error("Invalid PEM block");
|
|
1128
|
-
let i = n.slice(11, -5);
|
|
1129
|
-
if (r !== `-----END ${i}-----`) throw Error("PEM boundaries do not match");
|
|
1130
|
-
let a = /* @__PURE__ */ new Map(), o = 1;
|
|
1131
|
-
for (; o < t.length - 1;) {
|
|
1132
|
-
let e = t[o];
|
|
1133
|
-
if (e === void 0) break;
|
|
1134
|
-
if (e.length === 0) {
|
|
1135
|
-
o += 1;
|
|
1136
|
-
break;
|
|
1137
|
-
}
|
|
1138
|
-
let n = e.indexOf(": ");
|
|
1139
|
-
if (n === -1) break;
|
|
1140
|
-
let r = e.slice(0, n);
|
|
1141
|
-
if (a.has(r)) throw Error(`Duplicate PEM header: ${r}`);
|
|
1142
|
-
a.set(r, e.slice(n + 2)), o += 1;
|
|
1143
|
-
}
|
|
1144
|
-
return {
|
|
1145
|
-
label: i,
|
|
1146
|
-
headers: a,
|
|
1147
|
-
base64Body: t.slice(o, t.length - 1).join("")
|
|
1148
|
-
};
|
|
1149
|
-
}
|
|
1150
1297
|
function parseSpkiDer(e) {
|
|
1151
1298
|
try {
|
|
1152
|
-
let t =
|
|
1299
|
+
let t = f(e);
|
|
1153
1300
|
if (t.length !== 2) throw Error("Malformed SubjectPublicKeyInfo");
|
|
1154
1301
|
let n = t[0], r = t[1];
|
|
1155
1302
|
if (n === void 0 || n.tag !== 48 || r === void 0 || r.tag !== 3) throw Error("Malformed SubjectPublicKeyInfo");
|
|
1156
|
-
|
|
1157
|
-
let i =
|
|
1158
|
-
if (
|
|
1159
|
-
let
|
|
1303
|
+
g(r);
|
|
1304
|
+
let i = f(e.slice(n.start - n.headerLength, n.end)), a = i[0];
|
|
1305
|
+
if (a === void 0 || a.tag !== 6 || i.length < 1 || i.length > 2) throw Error("Malformed SubjectPublicKeyInfo");
|
|
1306
|
+
let o = i[1];
|
|
1160
1307
|
return {
|
|
1161
|
-
algorithmOid:
|
|
1162
|
-
...
|
|
1163
|
-
...
|
|
1308
|
+
algorithmOid: h(a.value),
|
|
1309
|
+
...o === void 0 ? {} : { parametersTag: o.tag },
|
|
1310
|
+
...o?.tag === 6 ? { parametersOid: h(o.value) } : {}
|
|
1164
1311
|
};
|
|
1165
|
-
} catch {
|
|
1166
|
-
throw Error("Malformed SubjectPublicKeyInfo");
|
|
1312
|
+
} catch (e) {
|
|
1313
|
+
throw a(e, r), Error("Malformed SubjectPublicKeyInfo");
|
|
1167
1314
|
}
|
|
1168
1315
|
}
|
|
1169
1316
|
/**
|
|
@@ -1176,10 +1323,10 @@ function parseSpkiDer(e) {
|
|
|
1176
1323
|
*/
|
|
1177
1324
|
function inferKeyImportInput(e, t) {
|
|
1178
1325
|
switch (e.algorithmOid) {
|
|
1179
|
-
case
|
|
1326
|
+
case b.rsaEncryption:
|
|
1180
1327
|
if (e.parametersTag === void 0 || e.parametersTag === 5) return { kind: "rsa" };
|
|
1181
1328
|
break;
|
|
1182
|
-
case
|
|
1329
|
+
case b.ecPublicKey: {
|
|
1183
1330
|
let t = e.parametersTag === 6 && e.parametersOid !== void 0 ? oidToCurve(e.parametersOid) : void 0;
|
|
1184
1331
|
if (t !== void 0) return {
|
|
1185
1332
|
kind: "ecdsa",
|
|
@@ -1187,9 +1334,7 @@ function inferKeyImportInput(e, t) {
|
|
|
1187
1334
|
};
|
|
1188
1335
|
break;
|
|
1189
1336
|
}
|
|
1190
|
-
case
|
|
1191
|
-
if (e.parametersTag === void 0) return { kind: "ed25519" };
|
|
1192
|
-
break;
|
|
1337
|
+
case b.ed25519: if (e.parametersTag === void 0) return { kind: "ed25519" };
|
|
1193
1338
|
}
|
|
1194
1339
|
throw Error(t);
|
|
1195
1340
|
}
|
|
@@ -1274,27 +1419,29 @@ function rsaImportInputFromJwkAlg(e) {
|
|
|
1274
1419
|
function assertSpkiMatchesRequestedAlgorithm(e, t) {
|
|
1275
1420
|
switch (t.kind) {
|
|
1276
1421
|
case "rsa":
|
|
1277
|
-
if (e.algorithmOid !==
|
|
1422
|
+
if (e.algorithmOid !== b.rsaEncryption || e.parametersTag !== void 0 && e.parametersTag !== 5) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
|
|
1278
1423
|
return;
|
|
1279
1424
|
case "ecdsa":
|
|
1280
|
-
if (e.algorithmOid !==
|
|
1425
|
+
if (e.algorithmOid !== b.ecPublicKey || e.parametersTag !== 6 || e.parametersOid !== curveToOid(t.curve)) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
|
|
1281
1426
|
return;
|
|
1282
1427
|
case "ed25519":
|
|
1283
|
-
if (e.algorithmOid !==
|
|
1428
|
+
if (e.algorithmOid !== b.ed25519 || e.parametersTag !== void 0) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
|
|
1284
1429
|
return;
|
|
1430
|
+
default: throw Error(`Unhandled PublicKeyImportInput kind: ${String(t)}`);
|
|
1285
1431
|
}
|
|
1286
1432
|
}
|
|
1287
1433
|
function assertPkcs8MatchesRequestedAlgorithm(e, t) {
|
|
1288
1434
|
switch (t.kind) {
|
|
1289
1435
|
case "rsa":
|
|
1290
|
-
if (e.algorithmOid !==
|
|
1436
|
+
if (e.algorithmOid !== b.rsaEncryption || e.parametersTag !== void 0 && e.parametersTag !== 5) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
|
|
1291
1437
|
return;
|
|
1292
1438
|
case "ecdsa":
|
|
1293
|
-
if (e.algorithmOid !==
|
|
1439
|
+
if (e.algorithmOid !== b.ecPublicKey || e.parametersTag !== 6 || e.parametersOid !== curveToOid(t.curve)) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
|
|
1294
1440
|
return;
|
|
1295
1441
|
case "ed25519":
|
|
1296
|
-
if (e.algorithmOid !==
|
|
1442
|
+
if (e.algorithmOid !== b.ed25519 || e.parametersTag !== void 0) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
|
|
1297
1443
|
return;
|
|
1444
|
+
default: throw Error(`Unhandled PrivateKeyImportInput kind: ${String(t)}`);
|
|
1298
1445
|
}
|
|
1299
1446
|
}
|
|
1300
1447
|
function assertSec1MatchesRequestedAlgorithm(e, t) {
|
|
@@ -1312,6 +1459,7 @@ function assertPublicJwkMatchesRequestedAlgorithm(e, t) {
|
|
|
1312
1459
|
case "ed25519":
|
|
1313
1460
|
if (e.kty !== "OKP" || e.crv !== "Ed25519" || typeof e.x != "string") throw Error("Public JWK algorithm does not match requested import algorithm");
|
|
1314
1461
|
return;
|
|
1462
|
+
default: throw Error(`Unhandled PublicKeyImportInput kind: ${String(t)}`);
|
|
1315
1463
|
}
|
|
1316
1464
|
}
|
|
1317
1465
|
function assertPrivateJwkMatchesRequestedAlgorithm(e, t) {
|
|
@@ -1327,9 +1475,9 @@ function assertPrivateJwkMatchesRequestedAlgorithm(e, t) {
|
|
|
1327
1475
|
case "ed25519":
|
|
1328
1476
|
if (e.kty !== "OKP" || e.crv !== "Ed25519" || typeof e.x != "string") throw Error("Private JWK algorithm does not match requested import algorithm");
|
|
1329
1477
|
return;
|
|
1478
|
+
default: throw Error(`Unhandled PrivateKeyImportInput kind: ${String(t)}`);
|
|
1330
1479
|
}
|
|
1331
1480
|
}
|
|
1332
|
-
|
|
1333
|
-
export { decryptRsaOaep, decryptRsaOaepOrThrow, derivePublicKey, encryptRsaOaep, encryptRsaOaepOrThrow, exportBinaryBase64, exportEncryptedPkcs1Pem, exportEncryptedPkcs8Der, exportEncryptedPkcs8Pem, exportEncryptedSec1Pem, exportPkcs1Der, exportPkcs1Pem, exportPkcs8Der, exportPkcs8Pem, exportPrivateJwk, exportPublicJwk, exportSec1Der, exportSec1Pem, exportSpkiDer, exportSpkiPem, generateKeyPair, importEncryptedPkcs1Pem, importEncryptedPkcs1PemOrThrow, importEncryptedPkcs8Der, importEncryptedPkcs8DerOrThrow, importEncryptedPkcs8Pem, importEncryptedPkcs8PemOrThrow, importEncryptedSec1Pem, importEncryptedSec1PemOrThrow, importPkcs1Der, importPkcs1DerOrThrow, importPkcs1Pem, importPkcs1PemOrThrow, importPkcs8Base64, importPkcs8Base64OrThrow, importPkcs8Der, importPkcs8DerOrThrow, importPkcs8Pem, importPkcs8PemOrThrow, importPrivateJwk, importPrivateJwkOrThrow, importPublicJwk, importPublicJwkOrThrow, importSec1Der, importSec1DerOrThrow, importSec1Pem, importSec1PemOrThrow, importSpkiBase64, importSpkiBase64OrThrow, importSpkiDer, importSpkiDerOrThrow, importSpkiPem, importSpkiPemOrThrow };
|
|
1481
|
+
export { decryptRsaOaep, decryptRsaOaepOrThrow, derivePublicKey, encryptRsaOaep, encryptRsaOaepOrThrow, exportBinaryBase64, exportEncryptedPkcs1Pem, exportEncryptedPkcs8Der, exportEncryptedPkcs8Pem, exportEncryptedSec1Pem, exportPkcs1Der, exportPkcs1Pem, exportPkcs8Der, exportPkcs8Pem, exportPrivateJwk, exportPublicJwk, exportSec1Der, exportSec1Pem, exportSpkiDer, exportSpkiPem, generateKeyPair, importEncryptedPkcs1Pem, importEncryptedPkcs1PemOrThrow, importEncryptedPkcs8Der, importEncryptedPkcs8DerOrThrow, importEncryptedPkcs8Pem, importEncryptedPkcs8PemOrThrow, importEncryptedSec1Pem, importEncryptedSec1PemOrThrow, importPkcs1Der, importPkcs1DerOrThrow, importPkcs1Pem, importPkcs1PemOrThrow, importPkcs8Base64, importPkcs8Base64OrThrow, importPkcs8Der, importPkcs8DerOrThrow, importPkcs8Pem, importPkcs8PemOrThrow, importPrivateJwk, importPrivateJwkOrThrow, importPublicJwk, importPublicJwkOrThrow, importSec1Der, importSec1DerOrThrow, importSec1Pem, importSec1PemOrThrow, importSpkiBase64, importSpkiBase64OrThrow, importSpkiDer, importSpkiDerOrThrow, importSpkiPem, importSpkiPemOrThrow, inspectEncryptedPkcs8Der, O as parsePbes2AlgorithmIdentifier };
|
|
1334
1482
|
|
|
1335
1483
|
//# sourceMappingURL=keys.js.map
|