micro509 0.9.0 → 0.10.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/dist/index.d.ts +3 -3
- package/dist/index.js +15 -15
- package/dist/internal/asn1/asn1.js +145 -186
- package/dist/internal/asn1/asn1.js.map +1 -0
- package/dist/internal/asn1/der.js +210 -227
- package/dist/internal/asn1/der.js.map +1 -0
- package/dist/internal/asn1/oids.js +297 -101
- package/dist/internal/asn1/oids.js.map +1 -0
- package/dist/internal/asn1/oids2.js +69 -0
- package/dist/internal/asn1/oids2.js.map +1 -0
- package/dist/internal/crypto/algorithm-names.js +37 -35
- package/dist/internal/crypto/algorithm-names.js.map +1 -0
- package/dist/internal/crypto/ecdsa.js +25 -28
- package/dist/internal/crypto/ecdsa.js.map +1 -0
- package/dist/internal/crypto/hash.js +56 -117
- package/dist/internal/crypto/hash.js.map +1 -0
- package/dist/internal/crypto/pbes2.js +122 -133
- package/dist/internal/crypto/pbes2.js.map +1 -0
- package/dist/internal/crypto/rsa-pss.js +118 -124
- package/dist/internal/crypto/rsa-pss.js.map +1 -0
- package/dist/internal/crypto/sig-verify.js +87 -99
- package/dist/internal/crypto/sig-verify.js.map +1 -0
- package/dist/internal/crypto/signing.d.ts +12 -8
- package/dist/internal/crypto/signing.js +53 -55
- package/dist/internal/crypto/signing.js.map +1 -0
- package/dist/internal/crypto/webcrypto.js +5 -3
- package/dist/internal/crypto/webcrypto.js.map +1 -0
- package/dist/internal/shared/base64.js +10 -9
- package/dist/internal/shared/base64.js.map +1 -0
- package/dist/internal/shared/dn.js +50 -57
- package/dist/internal/shared/dn.js.map +1 -0
- package/dist/internal/shared/ip.js +43 -53
- package/dist/internal/shared/ip.js.map +1 -0
- package/dist/internal/verify/name-constraints-engine.js +211 -239
- package/dist/internal/verify/name-constraints-engine.js.map +1 -0
- package/dist/internal/verify/policy-engine.js +228 -247
- package/dist/internal/verify/policy-engine.js.map +1 -0
- package/dist/internal/verify/verify-path.js +213 -228
- package/dist/internal/verify/verify-path.js.map +1 -0
- package/dist/internal/x509/extension-bits.js +62 -59
- package/dist/internal/x509/extension-bits.js.map +1 -0
- package/dist/internal/x509/extension-registry.js +133 -164
- package/dist/internal/x509/extension-registry.js.map +1 -0
- package/dist/internal/x509/name-fields.js +35 -41
- package/dist/internal/x509/name-fields.js.map +1 -0
- package/dist/keys/keys.d.ts +583 -547
- package/dist/keys/keys.js +519 -431
- package/dist/keys/keys.js.map +1 -0
- package/dist/keys.js +2 -2
- package/dist/pem/pem.d.ts +46 -46
- package/dist/pem/pem.js +62 -81
- package/dist/pem/pem.js.map +1 -0
- package/dist/pem.js +2 -2
- package/dist/pkcs/pfx.d.ts +96 -80
- package/dist/pkcs/pfx.js +191 -231
- package/dist/pkcs/pfx.js.map +1 -0
- package/dist/pkcs/pkcs12-mac.d.ts +20 -18
- package/dist/pkcs/pkcs12-mac.js +90 -111
- package/dist/pkcs/pkcs12-mac.js.map +1 -0
- package/dist/pkcs/pkcs7.d.ts +118 -69
- package/dist/pkcs/pkcs7.js +401 -443
- package/dist/pkcs/pkcs7.js.map +1 -0
- package/dist/pkcs.d.ts +2 -2
- package/dist/pkcs.js +4 -4
- package/dist/result/result.d.ts +37 -32
- package/dist/result/result.js +46 -44
- package/dist/result/result.js.map +1 -0
- package/dist/result.js +2 -2
- package/dist/revocation/chain.d.ts +138 -119
- package/dist/revocation/chain.js +248 -281
- package/dist/revocation/chain.js.map +1 -0
- package/dist/revocation/crl.d.ts +125 -123
- package/dist/revocation/crl.js +653 -745
- package/dist/revocation/crl.js.map +1 -0
- package/dist/revocation/ocsp.d.ts +183 -177
- package/dist/revocation/ocsp.js +579 -636
- package/dist/revocation/ocsp.js.map +1 -0
- package/dist/revocation/revocation.d.ts +26 -26
- package/dist/revocation/revocation.js +104 -125
- package/dist/revocation/revocation.js.map +1 -0
- package/dist/revocation.js +5 -5
- package/dist/verify/identity.d.ts +42 -42
- package/dist/verify/identity.js +158 -167
- package/dist/verify/identity.js.map +1 -0
- package/dist/verify/name-constraints.d.ts +7 -7
- package/dist/verify/policy.d.ts +10 -10
- package/dist/verify/verify.d.ts +163 -163
- package/dist/verify/verify.js +396 -392
- package/dist/verify/verify.js.map +1 -0
- package/dist/verify.js +3 -3
- package/dist/x509/certificate.d.ts +117 -117
- package/dist/x509/certificate.js +51 -60
- package/dist/x509/certificate.js.map +1 -0
- package/dist/x509/csr.d.ts +25 -25
- package/dist/x509/csr.js +26 -30
- package/dist/x509/csr.js.map +1 -0
- package/dist/x509/extensions.d.ts +228 -184
- package/dist/x509/extensions.js +175 -200
- package/dist/x509/extensions.js.map +1 -0
- package/dist/x509/name.d.ts +94 -93
- package/dist/x509/name.js +30 -29
- package/dist/x509/name.js.map +1 -0
- package/dist/x509/parse.d.ts +185 -185
- package/dist/x509/parse.js +638 -676
- package/dist/x509/parse.js.map +1 -0
- package/dist/x509.js +6 -6
- package/package.json +79 -41
package/dist/keys/keys.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { nullValue, objectIdentifier, octetString, readRootElement, readSequenceChildren, sequence } from "../internal/asn1/der.js";
|
|
2
|
-
import { decodeObjectIdentifier, extractBitStringValue, hexToBytes, toArrayBuffer, toHex } from "../internal/asn1/asn1.js";
|
|
3
|
-
import { OIDS } from "../internal/asn1/
|
|
4
|
-
import { md5 } from "../internal/crypto/hash.js";
|
|
5
|
-
import { getCrypto } from "../internal/crypto/webcrypto.js";
|
|
6
|
-
import { decryptPbes2, encryptPbes2, isWrongPasswordError, wrongPasswordError } from "../internal/crypto/pbes2.js";
|
|
7
|
-
import { base64Decode, base64Encode } from "../internal/shared/base64.js";
|
|
8
|
-
import { failureResult, rethrowIfInvariant, successResult } from "../result/result.js";
|
|
9
|
-
import { pemDecodeOrThrow, pemEncode } from "../pem/pem.js";
|
|
1
|
+
import { explicitContext as e, nullValue as t, objectIdentifier as n, octetString as r, readRootElement as i, readSequenceChildren as a, sequence as o } from "../internal/asn1/der.js";
|
|
2
|
+
import { decodeObjectIdentifier as s, extractBitStringValue as c, hexToBytes as l, toArrayBuffer as u, toHex as d } from "../internal/asn1/asn1.js";
|
|
3
|
+
import { OIDS as f } from "../internal/asn1/oids2.js";
|
|
4
|
+
import { md5 as p } from "../internal/crypto/hash.js";
|
|
5
|
+
import { getCrypto as m } from "../internal/crypto/webcrypto.js";
|
|
6
|
+
import { decryptPbes2 as h, encryptPbes2 as g, isWrongPasswordError as _, wrongPasswordError as v } from "../internal/crypto/pbes2.js";
|
|
7
|
+
import { base64Decode as y, base64Encode as b } from "../internal/shared/base64.js";
|
|
8
|
+
import { failureResult as x, rethrowIfInvariant as S, successResult as C } from "../result/result.js";
|
|
9
|
+
import { pemDecodeOrThrow as w, pemEncode as T } from "../pem/pem.js";
|
|
10
10
|
//#region src/keys/keys.ts
|
|
11
11
|
/**
|
|
12
12
|
* WebCrypto key generation plus import/export for PKCS#1, PKCS#8, SEC1, SPKI, and JWK.
|
|
@@ -45,37 +45,35 @@ import { pemDecodeOrThrow, pemEncode } from "../pem/pem.js";
|
|
|
45
45
|
* const pem = await keys.exportPkcs8Pem();
|
|
46
46
|
* ```
|
|
47
47
|
*/
|
|
48
|
-
async function generateKeyPair(
|
|
48
|
+
async function generateKeyPair(e = {
|
|
49
49
|
kind: "ecdsa",
|
|
50
50
|
curve: "P-256"
|
|
51
51
|
}) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (!("publicKey" in generated) || !("privateKey" in generated)) throw new Error("Expected an asymmetric key pair");
|
|
56
|
-
return wrapKeyPair(generated.publicKey, generated.privateKey);
|
|
52
|
+
let t = m().subtle, n = isRsaOaepInput(e) ? ["encrypt", "decrypt"] : ["sign", "verify"], r = await t.generateKey(toGenerateKeyAlgorithm(e), !0, n);
|
|
53
|
+
if (!("publicKey" in r) || !("privateKey" in r)) throw Error("Expected an asymmetric key pair");
|
|
54
|
+
return wrapKeyPair(r.publicKey, r.privateKey);
|
|
57
55
|
}
|
|
58
|
-
function wrapKeyPair(
|
|
56
|
+
function wrapKeyPair(e, t) {
|
|
59
57
|
return {
|
|
60
|
-
publicKey,
|
|
61
|
-
privateKey,
|
|
58
|
+
publicKey: e,
|
|
59
|
+
privateKey: t,
|
|
62
60
|
async exportSpkiDer() {
|
|
63
|
-
return new Uint8Array(await
|
|
61
|
+
return new Uint8Array(await m().subtle.exportKey("spki", e));
|
|
64
62
|
},
|
|
65
63
|
async exportSpkiPem() {
|
|
66
|
-
return
|
|
64
|
+
return T("PUBLIC KEY", await this.exportSpkiDer());
|
|
67
65
|
},
|
|
68
66
|
async exportPkcs8Der() {
|
|
69
|
-
return new Uint8Array(await
|
|
67
|
+
return new Uint8Array(await m().subtle.exportKey("pkcs8", t));
|
|
70
68
|
},
|
|
71
69
|
async exportPkcs8Pem() {
|
|
72
|
-
return
|
|
70
|
+
return T("PRIVATE KEY", await this.exportPkcs8Der());
|
|
73
71
|
},
|
|
74
72
|
exportPublicJwk() {
|
|
75
|
-
return
|
|
73
|
+
return m().subtle.exportKey("jwk", e);
|
|
76
74
|
},
|
|
77
75
|
exportPrivateJwk() {
|
|
78
|
-
return
|
|
76
|
+
return m().subtle.exportKey("jwk", t);
|
|
79
77
|
}
|
|
80
78
|
};
|
|
81
79
|
}
|
|
@@ -85,8 +83,8 @@ function wrapKeyPair(publicKey, privateKey) {
|
|
|
85
83
|
* @see {@linkcode importSpkiDer} for the inverse operation
|
|
86
84
|
* @see {@linkcode exportSpkiPem} for PEM output
|
|
87
85
|
*/
|
|
88
|
-
async function exportSpkiDer(
|
|
89
|
-
return new Uint8Array(await
|
|
86
|
+
async function exportSpkiDer(e) {
|
|
87
|
+
return new Uint8Array(await m().subtle.exportKey("spki", e));
|
|
90
88
|
}
|
|
91
89
|
/**
|
|
92
90
|
* Export a private key as DER-encoded PKCS#8 PrivateKeyInfo.
|
|
@@ -95,8 +93,8 @@ async function exportSpkiDer(publicKey) {
|
|
|
95
93
|
* @see {@linkcode exportPkcs8Pem} for PEM output
|
|
96
94
|
* @see {@linkcode exportEncryptedPkcs8Der} for password-protected export
|
|
97
95
|
*/
|
|
98
|
-
async function exportPkcs8Der(
|
|
99
|
-
return new Uint8Array(await
|
|
96
|
+
async function exportPkcs8Der(e) {
|
|
97
|
+
return new Uint8Array(await m().subtle.exportKey("pkcs8", e));
|
|
100
98
|
}
|
|
101
99
|
/**
|
|
102
100
|
* Export a public key as a JSON Web Key.
|
|
@@ -107,8 +105,8 @@ async function exportPkcs8Der(privateKey) {
|
|
|
107
105
|
* const jwk = await exportPublicJwk(keys.publicKey);
|
|
108
106
|
* ```
|
|
109
107
|
*/
|
|
110
|
-
function exportPublicJwk(
|
|
111
|
-
return
|
|
108
|
+
function exportPublicJwk(e) {
|
|
109
|
+
return m().subtle.exportKey("jwk", e);
|
|
112
110
|
}
|
|
113
111
|
/**
|
|
114
112
|
* Export a private key as a JSON Web Key.
|
|
@@ -116,8 +114,8 @@ function exportPublicJwk(publicKey) {
|
|
|
116
114
|
* @see {@linkcode importPrivateJwk} for the inverse operation
|
|
117
115
|
* @see {@linkcode exportPublicJwk} for public key export
|
|
118
116
|
*/
|
|
119
|
-
function exportPrivateJwk(
|
|
120
|
-
return
|
|
117
|
+
function exportPrivateJwk(e) {
|
|
118
|
+
return m().subtle.exportKey("jwk", e);
|
|
121
119
|
}
|
|
122
120
|
/**
|
|
123
121
|
* Export a private key as PEM-encoded PKCS#8 PrivateKeyInfo.
|
|
@@ -134,8 +132,8 @@ function exportPrivateJwk(privateKey) {
|
|
|
134
132
|
* @see {@linkcode importPkcs8Pem} for the inverse operation
|
|
135
133
|
* @see {@linkcode exportEncryptedPkcs8Pem} for password-protected export
|
|
136
134
|
*/
|
|
137
|
-
async function exportPkcs8Pem(
|
|
138
|
-
return
|
|
135
|
+
async function exportPkcs8Pem(e) {
|
|
136
|
+
return T("PRIVATE KEY", await exportPkcs8Der(e));
|
|
139
137
|
}
|
|
140
138
|
/**
|
|
141
139
|
* Export a private key as DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
|
|
@@ -148,9 +146,9 @@ async function exportPkcs8Pem(privateKey) {
|
|
|
148
146
|
* @see {@linkcode importEncryptedPkcs8Der} for the inverse operation
|
|
149
147
|
* @see {@linkcode exportEncryptedPkcs8Pem} for PEM output
|
|
150
148
|
*/
|
|
151
|
-
async function exportEncryptedPkcs8Der(
|
|
152
|
-
|
|
153
|
-
return
|
|
149
|
+
async function exportEncryptedPkcs8Der(e, t) {
|
|
150
|
+
let n = await g(await exportPkcs8Der(e), t);
|
|
151
|
+
return o([n.algorithmIdentifierDer, r(n.encryptedData)]);
|
|
154
152
|
}
|
|
155
153
|
/**
|
|
156
154
|
* Export a private key as PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
|
|
@@ -166,8 +164,8 @@ async function exportEncryptedPkcs8Der(privateKey, options) {
|
|
|
166
164
|
*
|
|
167
165
|
* @see {@linkcode importEncryptedPkcs8Pem} for the inverse operation
|
|
168
166
|
*/
|
|
169
|
-
async function exportEncryptedPkcs8Pem(
|
|
170
|
-
return
|
|
167
|
+
async function exportEncryptedPkcs8Pem(e, t) {
|
|
168
|
+
return T("ENCRYPTED PRIVATE KEY", await exportEncryptedPkcs8Der(e, t));
|
|
171
169
|
}
|
|
172
170
|
/**
|
|
173
171
|
* Export an RSA private key as DER-encoded PKCS#1 RSAPrivateKey.
|
|
@@ -180,10 +178,10 @@ async function exportEncryptedPkcs8Pem(privateKey, options) {
|
|
|
180
178
|
* @see {@linkcode importPkcs1Der} for the inverse operation
|
|
181
179
|
* @see {@linkcode exportPkcs1Pem} for PEM output
|
|
182
180
|
*/
|
|
183
|
-
async function exportPkcs1Der(
|
|
184
|
-
|
|
185
|
-
if (
|
|
186
|
-
return
|
|
181
|
+
async function exportPkcs1Der(e) {
|
|
182
|
+
let t = parsePkcs8PrivateKey(await exportPkcs8Der(e));
|
|
183
|
+
if (t.algorithmOid !== f.rsaEncryption) throw Error("PKCS#1 export requires an RSA private key");
|
|
184
|
+
return t.privateKeyDer;
|
|
187
185
|
}
|
|
188
186
|
/**
|
|
189
187
|
* Export an RSA private key as PEM-encoded PKCS#1 RSAPrivateKey.
|
|
@@ -193,8 +191,8 @@ async function exportPkcs1Der(privateKey) {
|
|
|
193
191
|
* @see {@linkcode importPkcs1Pem} for the inverse operation
|
|
194
192
|
* @see {@linkcode exportEncryptedPkcs1Pem} for password-protected export
|
|
195
193
|
*/
|
|
196
|
-
async function exportPkcs1Pem(
|
|
197
|
-
return
|
|
194
|
+
async function exportPkcs1Pem(e) {
|
|
195
|
+
return T("RSA PRIVATE KEY", await exportPkcs1Der(e));
|
|
198
196
|
}
|
|
199
197
|
/**
|
|
200
198
|
* Export an RSA private key as legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1).
|
|
@@ -206,8 +204,8 @@ async function exportPkcs1Pem(privateKey) {
|
|
|
206
204
|
*
|
|
207
205
|
* @see {@linkcode importEncryptedPkcs1Pem} for the inverse operation
|
|
208
206
|
*/
|
|
209
|
-
async function exportEncryptedPkcs1Pem(
|
|
210
|
-
return encryptTraditionalPem("RSA PRIVATE KEY", await exportPkcs1Der(
|
|
207
|
+
async function exportEncryptedPkcs1Pem(e, t) {
|
|
208
|
+
return encryptTraditionalPem("RSA PRIVATE KEY", await exportPkcs1Der(e), t);
|
|
211
209
|
}
|
|
212
210
|
/**
|
|
213
211
|
* Export an EC private key as DER-encoded SEC 1 ECPrivateKey.
|
|
@@ -215,15 +213,19 @@ async function exportEncryptedPkcs1Pem(privateKey, options) {
|
|
|
215
213
|
* SEC 1 is the legacy EC-only format. For algorithm-agnostic export, use
|
|
216
214
|
* {@linkcode exportPkcs8Der}.
|
|
217
215
|
*
|
|
216
|
+
* The output always carries the RFC 5915 `parameters [0]` named curve
|
|
217
|
+
* (matching OpenSSL), so it re-imports via {@linkcode importSec1Der} without
|
|
218
|
+
* an explicit curve.
|
|
219
|
+
*
|
|
218
220
|
* @throws {Error} If the key is not an EC key
|
|
219
221
|
*
|
|
220
222
|
* @see {@linkcode importSec1Der} for the inverse operation
|
|
221
223
|
* @see {@linkcode exportSec1Pem} for PEM output
|
|
222
224
|
*/
|
|
223
|
-
async function exportSec1Der(
|
|
224
|
-
|
|
225
|
-
if (
|
|
226
|
-
return
|
|
225
|
+
async function exportSec1Der(e) {
|
|
226
|
+
let t = parsePkcs8PrivateKey(await exportPkcs8Der(e));
|
|
227
|
+
if (t.algorithmOid !== f.ecPublicKey) throw Error("SEC1 export requires an EC private key");
|
|
228
|
+
return ensureSec1NamedCurveParameters(t.privateKeyDer, t.parametersOid);
|
|
227
229
|
}
|
|
228
230
|
/**
|
|
229
231
|
* Export an EC private key as PEM-encoded SEC 1 ECPrivateKey.
|
|
@@ -233,8 +235,8 @@ async function exportSec1Der(privateKey) {
|
|
|
233
235
|
* @see {@linkcode importSec1Pem} for the inverse operation
|
|
234
236
|
* @see {@linkcode exportEncryptedSec1Pem} for password-protected export
|
|
235
237
|
*/
|
|
236
|
-
async function exportSec1Pem(
|
|
237
|
-
return
|
|
238
|
+
async function exportSec1Pem(e) {
|
|
239
|
+
return T("EC PRIVATE KEY", await exportSec1Der(e));
|
|
238
240
|
}
|
|
239
241
|
/**
|
|
240
242
|
* Export an EC private key as legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1).
|
|
@@ -246,8 +248,8 @@ async function exportSec1Pem(privateKey) {
|
|
|
246
248
|
*
|
|
247
249
|
* @see {@linkcode importEncryptedSec1Pem} for the inverse operation
|
|
248
250
|
*/
|
|
249
|
-
async function exportEncryptedSec1Pem(
|
|
250
|
-
return encryptTraditionalPem("EC PRIVATE KEY", await exportSec1Der(
|
|
251
|
+
async function exportEncryptedSec1Pem(e, t) {
|
|
252
|
+
return encryptTraditionalPem("EC PRIVATE KEY", await exportSec1Der(e), t);
|
|
251
253
|
}
|
|
252
254
|
/**
|
|
253
255
|
* Export a public key as PEM-encoded SubjectPublicKeyInfo.
|
|
@@ -258,8 +260,8 @@ async function exportEncryptedSec1Pem(privateKey, options) {
|
|
|
258
260
|
* const pem = await exportSpkiPem(keys.publicKey);
|
|
259
261
|
* ```
|
|
260
262
|
*/
|
|
261
|
-
async function exportSpkiPem(
|
|
262
|
-
return
|
|
263
|
+
async function exportSpkiPem(e) {
|
|
264
|
+
return T("PUBLIC KEY", await exportSpkiDer(e));
|
|
263
265
|
}
|
|
264
266
|
/**
|
|
265
267
|
* Derive the matching public key from an imported (or generated) private key.
|
|
@@ -291,13 +293,11 @@ async function exportSpkiPem(publicKey) {
|
|
|
291
293
|
*
|
|
292
294
|
* @see {@linkcode exportSpkiDer} for exporting the derived key
|
|
293
295
|
*/
|
|
294
|
-
async function derivePublicKey(
|
|
295
|
-
if (
|
|
296
|
-
if (!
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
const privateJwk = await subtle.exportKey("jwk", privateKey);
|
|
300
|
-
return subtle.importKey("jwk", toPublicJwk(privateJwk, usage), privateKey.algorithm, true, [usage]);
|
|
296
|
+
async function derivePublicKey(e) {
|
|
297
|
+
if (e.type !== "private") throw Error("derivePublicKey requires a private CryptoKey");
|
|
298
|
+
if (!e.extractable) throw Error("Cannot derive public key from a non-extractable private key");
|
|
299
|
+
let t = m().subtle, n = e.algorithm.name === "RSA-OAEP" ? "encrypt" : "verify", r = await t.exportKey("jwk", e);
|
|
300
|
+
return t.importKey("jwk", toPublicJwk(r, n), e.algorithm, !0, [n]);
|
|
301
301
|
}
|
|
302
302
|
/**
|
|
303
303
|
* Export a key as raw base64 (no PEM headers).
|
|
@@ -310,10 +310,10 @@ async function derivePublicKey(privateKey) {
|
|
|
310
310
|
* @see {@linkcode importSpkiBase64} for public key import
|
|
311
311
|
* @see {@linkcode importPkcs8Base64} for private key import
|
|
312
312
|
*/
|
|
313
|
-
async function exportBinaryBase64(
|
|
314
|
-
if (
|
|
315
|
-
if (
|
|
316
|
-
throw
|
|
313
|
+
async function exportBinaryBase64(e) {
|
|
314
|
+
if (e.type === "public") return b(await exportSpkiDer(e));
|
|
315
|
+
if (e.type === "private") return b(await exportPkcs8Der(e));
|
|
316
|
+
throw Error("Cannot export secret/symmetric CryptoKey");
|
|
317
317
|
}
|
|
318
318
|
/**
|
|
319
319
|
* Import a public key from DER-encoded SubjectPublicKeyInfo.
|
|
@@ -332,37 +332,29 @@ async function exportBinaryBase64(key) {
|
|
|
332
332
|
* @see {@linkcode exportSpkiDer} for the inverse operation
|
|
333
333
|
* @see {@linkcode importSpkiPem} for PEM input
|
|
334
334
|
*/
|
|
335
|
-
async function importSpkiDerOrThrow(
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
if (algorithm === void 0) importInput = inferPublicKeyImportInput(parsedSpki);
|
|
339
|
-
else {
|
|
340
|
-
assertSpkiMatchesRequestedAlgorithm(parsedSpki, algorithm);
|
|
341
|
-
importInput = algorithm;
|
|
342
|
-
}
|
|
335
|
+
async function importSpkiDerOrThrow(e, t) {
|
|
336
|
+
let n = parseSpkiDer(e), r;
|
|
337
|
+
t === void 0 ? r = inferKeyImportInput(n, "Unsupported SubjectPublicKeyInfo algorithm") : (assertSpkiMatchesRequestedAlgorithm(n, t), r = t);
|
|
343
338
|
try {
|
|
344
|
-
return await
|
|
339
|
+
return await m().subtle.importKey("spki", new Uint8Array(e), toImportAlgorithm(r), !0, publicKeyUsages(r));
|
|
345
340
|
} catch {
|
|
346
|
-
throw
|
|
341
|
+
throw Error("Malformed SubjectPublicKeyInfo");
|
|
347
342
|
}
|
|
348
343
|
}
|
|
349
344
|
/** Runs a throwing key import and maps an EXPECTED failure to a `'malformed'` result; invariants rethrow. */
|
|
350
|
-
async function importResult(
|
|
345
|
+
async function importResult(e) {
|
|
351
346
|
try {
|
|
352
|
-
return
|
|
353
|
-
} catch (
|
|
354
|
-
|
|
355
|
-
return failureResult("malformed", error instanceof Error ? error.message : "Malformed key");
|
|
347
|
+
return C(await e());
|
|
348
|
+
} catch (e) {
|
|
349
|
+
return S(e), x("malformed", e instanceof Error ? e.message : "Malformed key");
|
|
356
350
|
}
|
|
357
351
|
}
|
|
358
352
|
/** Like {@link importResult} but distinguishes a typed wrong-password failure (`'invalid_password'`). */
|
|
359
|
-
async function encryptedImportResult(
|
|
353
|
+
async function encryptedImportResult(e) {
|
|
360
354
|
try {
|
|
361
|
-
return
|
|
362
|
-
} catch (
|
|
363
|
-
|
|
364
|
-
if (isWrongPasswordError(error)) return failureResult("invalid_password", error.message);
|
|
365
|
-
return failureResult("malformed", error instanceof Error ? error.message : "Malformed encrypted key");
|
|
355
|
+
return C(await e());
|
|
356
|
+
} catch (e) {
|
|
357
|
+
return S(e), _(e) ? x("invalid_password", e.message) : x("malformed", e instanceof Error ? e.message : "Malformed encrypted key");
|
|
366
358
|
}
|
|
367
359
|
}
|
|
368
360
|
/**
|
|
@@ -370,8 +362,8 @@ async function encryptedImportResult(run) {
|
|
|
370
362
|
*
|
|
371
363
|
* @see `importSpkiDerOrThrow` for the throwing variant
|
|
372
364
|
*/
|
|
373
|
-
function importSpkiDer(
|
|
374
|
-
return importResult(() => importSpkiDerOrThrow(
|
|
365
|
+
function importSpkiDer(e, t) {
|
|
366
|
+
return importResult(() => importSpkiDerOrThrow(e, t));
|
|
375
367
|
}
|
|
376
368
|
/**
|
|
377
369
|
* Import a public key from PEM-encoded SubjectPublicKeyInfo.
|
|
@@ -386,16 +378,16 @@ function importSpkiDer(der, algorithm) {
|
|
|
386
378
|
*
|
|
387
379
|
* @see {@linkcode exportSpkiPem} for the inverse operation
|
|
388
380
|
*/
|
|
389
|
-
function importSpkiPemOrThrow(
|
|
390
|
-
return importSpkiDerOrThrow(
|
|
381
|
+
function importSpkiPemOrThrow(e, t) {
|
|
382
|
+
return importSpkiDerOrThrow(w("PUBLIC KEY", e), t);
|
|
391
383
|
}
|
|
392
384
|
/**
|
|
393
385
|
* Import a public key from PEM-encoded SubjectPublicKeyInfo.
|
|
394
386
|
*
|
|
395
387
|
* @see `importSpkiPemOrThrow` for the throwing variant
|
|
396
388
|
*/
|
|
397
|
-
function importSpkiPem(
|
|
398
|
-
return importResult(() => importSpkiPemOrThrow(
|
|
389
|
+
function importSpkiPem(e, t) {
|
|
390
|
+
return importResult(() => importSpkiPemOrThrow(e, t));
|
|
399
391
|
}
|
|
400
392
|
/**
|
|
401
393
|
* Import a public key from base64-encoded SubjectPublicKeyInfo (no PEM headers).
|
|
@@ -403,48 +395,54 @@ function importSpkiPem(pem, algorithm) {
|
|
|
403
395
|
* @see {@linkcode exportBinaryBase64} for the inverse operation
|
|
404
396
|
* @see {@linkcode importSpkiPem} for PEM input with headers
|
|
405
397
|
*/
|
|
406
|
-
function importSpkiBase64OrThrow(
|
|
407
|
-
let
|
|
398
|
+
function importSpkiBase64OrThrow(e, t) {
|
|
399
|
+
let n;
|
|
408
400
|
try {
|
|
409
|
-
|
|
401
|
+
n = y(e);
|
|
410
402
|
} catch {
|
|
411
|
-
throw
|
|
403
|
+
throw Error("Invalid base64 SubjectPublicKeyInfo");
|
|
412
404
|
}
|
|
413
|
-
return importSpkiDerOrThrow(
|
|
405
|
+
return importSpkiDerOrThrow(n, t);
|
|
414
406
|
}
|
|
415
407
|
/**
|
|
416
408
|
* Import a public key from base64-encoded SubjectPublicKeyInfo (no PEM headers).
|
|
417
409
|
*
|
|
418
410
|
* @see `importSpkiBase64OrThrow` for the throwing variant
|
|
419
411
|
*/
|
|
420
|
-
function importSpkiBase64(
|
|
421
|
-
return importResult(() => importSpkiBase64OrThrow(
|
|
412
|
+
function importSpkiBase64(e, t) {
|
|
413
|
+
return importResult(() => importSpkiBase64OrThrow(e, t));
|
|
422
414
|
}
|
|
423
415
|
/**
|
|
424
416
|
* Import a private key from DER-encoded PKCS#8 PrivateKeyInfo.
|
|
425
417
|
*
|
|
418
|
+
* When `algorithm` is omitted, the algorithm (and, for EC keys, the curve) is
|
|
419
|
+
* inferred from the PrivateKeyInfo's own `privateKeyAlgorithm` — useful for
|
|
420
|
+
* keys whose type isn't known ahead of time. Pass `algorithm` to additionally
|
|
421
|
+
* assert that the DER matches an expected algorithm.
|
|
422
|
+
*
|
|
426
423
|
* @param der - DER-encoded PKCS#8 PrivateKeyInfo bytes
|
|
427
|
-
* @param algorithm -
|
|
424
|
+
* @param algorithm - Optional expected algorithm; must match key contents when given
|
|
428
425
|
* @returns Extractable CryptoKey with `sign` usage
|
|
429
426
|
*
|
|
430
|
-
* @throws {Error} If DER is malformed
|
|
427
|
+
* @throws {Error} If DER is malformed, encodes an unsupported algorithm, or doesn't match `algorithm`
|
|
431
428
|
*
|
|
432
429
|
* @see {@linkcode exportPkcs8Der} for the inverse operation
|
|
433
430
|
* @see {@linkcode importPkcs8Pem} for PEM input
|
|
434
431
|
* @see {@linkcode importEncryptedPkcs8Der} for encrypted PKCS#8
|
|
435
432
|
*/
|
|
436
|
-
async function importPkcs8DerOrThrow(
|
|
437
|
-
let
|
|
433
|
+
async function importPkcs8DerOrThrow(e, t) {
|
|
434
|
+
let n;
|
|
438
435
|
try {
|
|
439
|
-
|
|
436
|
+
n = parsePkcs8PrivateKey(e);
|
|
440
437
|
} catch {
|
|
441
|
-
throw
|
|
438
|
+
throw Error("Malformed PKCS#8 private key");
|
|
442
439
|
}
|
|
443
|
-
|
|
440
|
+
let r;
|
|
441
|
+
t === void 0 ? r = inferKeyImportInput(n, "Unsupported PKCS#8 private key algorithm") : (assertPkcs8MatchesRequestedAlgorithm(n, t), r = t);
|
|
444
442
|
try {
|
|
445
|
-
return await
|
|
443
|
+
return await m().subtle.importKey("pkcs8", new Uint8Array(e), toImportAlgorithm(r), !0, privateKeyUsages(r));
|
|
446
444
|
} catch {
|
|
447
|
-
throw
|
|
445
|
+
throw Error("Malformed PKCS#8 private key");
|
|
448
446
|
}
|
|
449
447
|
}
|
|
450
448
|
/**
|
|
@@ -452,85 +450,95 @@ async function importPkcs8DerOrThrow(der, algorithm) {
|
|
|
452
450
|
*
|
|
453
451
|
* @see `importPkcs8DerOrThrow` for the throwing variant
|
|
454
452
|
*/
|
|
455
|
-
function importPkcs8Der(
|
|
456
|
-
return importResult(() => importPkcs8DerOrThrow(
|
|
453
|
+
function importPkcs8Der(e, t) {
|
|
454
|
+
return importResult(() => importPkcs8DerOrThrow(e, t));
|
|
457
455
|
}
|
|
458
456
|
/**
|
|
459
457
|
* Import a private key from PEM-encoded PKCS#8 PrivateKeyInfo.
|
|
460
458
|
*
|
|
459
|
+
* When `algorithm` is omitted, it is inferred from the key's own
|
|
460
|
+
* `privateKeyAlgorithm` (see {@linkcode importPkcs8DerOrThrow}).
|
|
461
|
+
*
|
|
461
462
|
* @example
|
|
462
463
|
* ```ts
|
|
463
464
|
* const key = await importPkcs8PemOrThrow(pemString, { kind: 'ecdsa', curve: 'P-256' });
|
|
465
|
+
* const inferred = await importPkcs8PemOrThrow(pemString);
|
|
464
466
|
* ```
|
|
465
467
|
*/
|
|
466
|
-
function importPkcs8PemOrThrow(
|
|
467
|
-
return importPkcs8DerOrThrow(
|
|
468
|
+
function importPkcs8PemOrThrow(e, t) {
|
|
469
|
+
return importPkcs8DerOrThrow(w("PRIVATE KEY", e), t);
|
|
468
470
|
}
|
|
469
471
|
/**
|
|
470
472
|
* Import a private key from PEM-encoded PKCS#8 PrivateKeyInfo.
|
|
471
473
|
*
|
|
472
474
|
* @see `importPkcs8PemOrThrow` for the throwing variant
|
|
473
475
|
*/
|
|
474
|
-
function importPkcs8Pem(
|
|
475
|
-
return importResult(() => importPkcs8PemOrThrow(
|
|
476
|
+
function importPkcs8Pem(e, t) {
|
|
477
|
+
return importResult(() => importPkcs8PemOrThrow(e, t));
|
|
476
478
|
}
|
|
477
479
|
/**
|
|
478
480
|
* Import a private key from DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
|
|
479
481
|
*
|
|
480
482
|
* Decrypts the PBES2 envelope using the provided password, then imports the key.
|
|
481
483
|
*
|
|
484
|
+
* When `algorithm` is omitted, it is inferred from the decrypted key's own
|
|
485
|
+
* `privateKeyAlgorithm` (see {@linkcode importPkcs8DerOrThrow}).
|
|
486
|
+
*
|
|
482
487
|
* @param der - DER-encoded EncryptedPrivateKeyInfo bytes
|
|
483
488
|
* @param password - Decryption password
|
|
484
|
-
* @param algorithm -
|
|
489
|
+
* @param algorithm - Optional expected algorithm; must match decrypted key when given
|
|
485
490
|
*
|
|
486
491
|
* @throws {Error} If DER is malformed, password is wrong, or algorithm doesn't match
|
|
487
492
|
*
|
|
488
493
|
* @see {@linkcode exportEncryptedPkcs8Der} for the inverse operation
|
|
489
494
|
*/
|
|
490
|
-
async function importEncryptedPkcs8DerOrThrow(
|
|
491
|
-
let
|
|
495
|
+
async function importEncryptedPkcs8DerOrThrow(e, t, n) {
|
|
496
|
+
let r;
|
|
492
497
|
try {
|
|
493
|
-
|
|
498
|
+
r = a(e);
|
|
494
499
|
} catch {
|
|
495
|
-
throw
|
|
500
|
+
throw Error("Malformed EncryptedPrivateKeyInfo");
|
|
496
501
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
const decrypted = await decryptPbes2(der.slice(algorithmIdentifier.start - algorithmIdentifier.headerLength, algorithmIdentifier.end), encryptedData.value, password);
|
|
502
|
+
let i = r[0], o = r[1];
|
|
503
|
+
if (r.length !== 2 || i === void 0 || o === void 0 || o.tag !== 4) throw Error("Malformed EncryptedPrivateKeyInfo");
|
|
504
|
+
let s = await h(e.slice(i.start - i.headerLength, i.end), o.value, t);
|
|
501
505
|
try {
|
|
502
|
-
parsePkcs8PrivateKey(
|
|
506
|
+
parsePkcs8PrivateKey(s);
|
|
503
507
|
} catch {
|
|
504
|
-
throw
|
|
508
|
+
throw v("Invalid password or encrypted content");
|
|
505
509
|
}
|
|
506
|
-
return importPkcs8DerOrThrow(
|
|
510
|
+
return importPkcs8DerOrThrow(s, n);
|
|
507
511
|
}
|
|
508
512
|
/**
|
|
509
513
|
* Import a private key from DER-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
|
|
510
514
|
*
|
|
511
515
|
* @see `importEncryptedPkcs8DerOrThrow` for the throwing variant
|
|
512
516
|
*/
|
|
513
|
-
function importEncryptedPkcs8Der(
|
|
514
|
-
return encryptedImportResult(() => importEncryptedPkcs8DerOrThrow(
|
|
517
|
+
function importEncryptedPkcs8Der(e, t, n) {
|
|
518
|
+
return encryptedImportResult(() => importEncryptedPkcs8DerOrThrow(e, t, n));
|
|
515
519
|
}
|
|
516
520
|
/**
|
|
517
521
|
* Import a private key from PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
|
|
518
522
|
*
|
|
523
|
+
* When `algorithm` is omitted, it is inferred from the decrypted key's own
|
|
524
|
+
* `privateKeyAlgorithm` (see {@linkcode importPkcs8DerOrThrow}).
|
|
525
|
+
*
|
|
519
526
|
* @example
|
|
520
527
|
* ```ts
|
|
521
528
|
* const key = await importEncryptedPkcs8PemOrThrow(pem, 'secret', { kind: 'rsa' });
|
|
529
|
+
* const inferred = await importEncryptedPkcs8PemOrThrow(pem, 'secret');
|
|
522
530
|
* ```
|
|
523
531
|
*/
|
|
524
|
-
function importEncryptedPkcs8PemOrThrow(
|
|
525
|
-
return importEncryptedPkcs8DerOrThrow(
|
|
532
|
+
function importEncryptedPkcs8PemOrThrow(e, t, n) {
|
|
533
|
+
return importEncryptedPkcs8DerOrThrow(w("ENCRYPTED PRIVATE KEY", e), t, n);
|
|
526
534
|
}
|
|
527
535
|
/**
|
|
528
536
|
* Import a private key from PEM-encoded PBES2-encrypted PKCS#8 EncryptedPrivateKeyInfo.
|
|
529
537
|
*
|
|
530
538
|
* @see `importEncryptedPkcs8PemOrThrow` for the throwing variant
|
|
531
539
|
*/
|
|
532
|
-
function importEncryptedPkcs8Pem(
|
|
533
|
-
return encryptedImportResult(() => importEncryptedPkcs8PemOrThrow(
|
|
540
|
+
function importEncryptedPkcs8Pem(e, t, n) {
|
|
541
|
+
return encryptedImportResult(() => importEncryptedPkcs8PemOrThrow(e, t, n));
|
|
534
542
|
}
|
|
535
543
|
/**
|
|
536
544
|
* Import an RSA private key from DER-encoded PKCS#1 RSAPrivateKey.
|
|
@@ -540,16 +548,16 @@ function importEncryptedPkcs8Pem(pem, password, algorithm) {
|
|
|
540
548
|
* @see {@linkcode exportPkcs1Der} for the inverse operation
|
|
541
549
|
* @see {@linkcode importPkcs1Pem} for PEM input
|
|
542
550
|
*/
|
|
543
|
-
function importPkcs1DerOrThrow(
|
|
544
|
-
return importPkcs8DerOrThrow(wrapPkcs1InPkcs8(
|
|
551
|
+
function importPkcs1DerOrThrow(e, t = { kind: "rsa" }) {
|
|
552
|
+
return importPkcs8DerOrThrow(wrapPkcs1InPkcs8(e), t);
|
|
545
553
|
}
|
|
546
554
|
/**
|
|
547
555
|
* Import an RSA private key from DER-encoded PKCS#1 RSAPrivateKey.
|
|
548
556
|
*
|
|
549
557
|
* @see `importPkcs1DerOrThrow` for the throwing variant
|
|
550
558
|
*/
|
|
551
|
-
function importPkcs1Der(
|
|
552
|
-
return importResult(() => importPkcs1DerOrThrow(
|
|
559
|
+
function importPkcs1Der(e, t = { kind: "rsa" }) {
|
|
560
|
+
return importResult(() => importPkcs1DerOrThrow(e, t));
|
|
553
561
|
}
|
|
554
562
|
/**
|
|
555
563
|
* Import an RSA private key from PEM-encoded PKCS#1 RSAPrivateKey.
|
|
@@ -559,16 +567,16 @@ function importPkcs1Der(der, algorithm = { kind: "rsa" }) {
|
|
|
559
567
|
* @see {@linkcode exportPkcs1Pem} for the inverse operation
|
|
560
568
|
* @see {@linkcode importEncryptedPkcs1Pem} for encrypted PEM
|
|
561
569
|
*/
|
|
562
|
-
function importPkcs1PemOrThrow(
|
|
563
|
-
return importPkcs1DerOrThrow(
|
|
570
|
+
function importPkcs1PemOrThrow(e, t = { kind: "rsa" }) {
|
|
571
|
+
return importPkcs1DerOrThrow(w("RSA PRIVATE KEY", e), t);
|
|
564
572
|
}
|
|
565
573
|
/**
|
|
566
574
|
* Import an RSA private key from PEM-encoded PKCS#1 RSAPrivateKey.
|
|
567
575
|
*
|
|
568
576
|
* @see `importPkcs1PemOrThrow` for the throwing variant
|
|
569
577
|
*/
|
|
570
|
-
function importPkcs1Pem(
|
|
571
|
-
return importResult(() => importPkcs1PemOrThrow(
|
|
578
|
+
function importPkcs1Pem(e, t = { kind: "rsa" }) {
|
|
579
|
+
return importResult(() => importPkcs1PemOrThrow(e, t));
|
|
572
580
|
}
|
|
573
581
|
/**
|
|
574
582
|
* Import an RSA private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1).
|
|
@@ -578,16 +586,16 @@ function importPkcs1Pem(pem, algorithm = { kind: "rsa" }) {
|
|
|
578
586
|
* @see {@linkcode exportEncryptedPkcs1Pem} for the inverse operation
|
|
579
587
|
* @see {@linkcode importEncryptedPkcs8Pem} for modern PBES2 encryption
|
|
580
588
|
*/
|
|
581
|
-
async function importEncryptedPkcs1PemOrThrow(
|
|
582
|
-
return importPkcs1DerOrThrow(await decryptTraditionalPem("RSA PRIVATE KEY",
|
|
589
|
+
async function importEncryptedPkcs1PemOrThrow(e, t, n = { kind: "rsa" }) {
|
|
590
|
+
return importPkcs1DerOrThrow(await decryptTraditionalPem("RSA PRIVATE KEY", e, t), n);
|
|
583
591
|
}
|
|
584
592
|
/**
|
|
585
593
|
* Import an RSA private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (PKCS#1).
|
|
586
594
|
*
|
|
587
595
|
* @see `importEncryptedPkcs1PemOrThrow` for the throwing variant
|
|
588
596
|
*/
|
|
589
|
-
function importEncryptedPkcs1Pem(
|
|
590
|
-
return encryptedImportResult(() => importEncryptedPkcs1PemOrThrow(
|
|
597
|
+
function importEncryptedPkcs1Pem(e, t, n = { kind: "rsa" }) {
|
|
598
|
+
return encryptedImportResult(() => importEncryptedPkcs1PemOrThrow(e, t, n));
|
|
591
599
|
}
|
|
592
600
|
/**
|
|
593
601
|
* Import a private key from base64-encoded PKCS#8 PrivateKeyInfo (no PEM headers).
|
|
@@ -595,22 +603,22 @@ function importEncryptedPkcs1Pem(pem, password, algorithm = { kind: "rsa" }) {
|
|
|
595
603
|
* @see {@linkcode exportBinaryBase64} for the inverse operation
|
|
596
604
|
* @see {@linkcode importPkcs8Pem} for PEM input with headers
|
|
597
605
|
*/
|
|
598
|
-
function importPkcs8Base64OrThrow(
|
|
599
|
-
let
|
|
606
|
+
function importPkcs8Base64OrThrow(e, t) {
|
|
607
|
+
let n;
|
|
600
608
|
try {
|
|
601
|
-
|
|
609
|
+
n = y(e);
|
|
602
610
|
} catch {
|
|
603
|
-
throw
|
|
611
|
+
throw Error("Invalid base64 PKCS#8 private key");
|
|
604
612
|
}
|
|
605
|
-
return importPkcs8DerOrThrow(
|
|
613
|
+
return importPkcs8DerOrThrow(n, t);
|
|
606
614
|
}
|
|
607
615
|
/**
|
|
608
616
|
* Import a private key from base64-encoded PKCS#8 PrivateKeyInfo (no PEM headers).
|
|
609
617
|
*
|
|
610
618
|
* @see `importPkcs8Base64OrThrow` for the throwing variant
|
|
611
619
|
*/
|
|
612
|
-
function importPkcs8Base64(
|
|
613
|
-
return importResult(() => importPkcs8Base64OrThrow(
|
|
620
|
+
function importPkcs8Base64(e, t) {
|
|
621
|
+
return importResult(() => importPkcs8Base64OrThrow(e, t));
|
|
614
622
|
}
|
|
615
623
|
/**
|
|
616
624
|
* Import an EC private key from DER-encoded SEC 1 ECPrivateKey.
|
|
@@ -620,47 +628,53 @@ function importPkcs8Base64(base64, algorithm) {
|
|
|
620
628
|
* (OpenSSL always writes it), its named-curve OID must match `algorithm.curve`;
|
|
621
629
|
* when the field is absent, the caller-supplied curve is trusted.
|
|
622
630
|
*
|
|
623
|
-
*
|
|
631
|
+
* When `algorithm` is omitted, the curve is inferred from the embedded
|
|
632
|
+
* `parameters [0]` field; a key without a supported named curve then fails.
|
|
633
|
+
*
|
|
634
|
+
* @throws {Error} If DER is not an ECPrivateKey, its embedded curve doesn't
|
|
635
|
+
* match `algorithm`, or no curve is available (neither embedded nor supplied)
|
|
624
636
|
*
|
|
625
637
|
* @see {@linkcode exportSec1Der} for the inverse operation
|
|
626
638
|
* @see {@linkcode importSec1Pem} for PEM input
|
|
627
639
|
*/
|
|
628
|
-
async function importSec1DerOrThrow(
|
|
629
|
-
let
|
|
640
|
+
async function importSec1DerOrThrow(e, t) {
|
|
641
|
+
let n;
|
|
630
642
|
try {
|
|
631
|
-
|
|
643
|
+
n = parseSec1PrivateKey(e);
|
|
632
644
|
} catch {
|
|
633
|
-
throw
|
|
645
|
+
throw Error("Malformed SEC 1 private key");
|
|
634
646
|
}
|
|
635
|
-
|
|
636
|
-
return await importPkcs8DerOrThrow(wrapSec1InPkcs8(
|
|
647
|
+
let r;
|
|
648
|
+
return t === void 0 ? r = inferSec1ImportInput(n) : (assertSec1MatchesRequestedAlgorithm(n, t), r = t), await importPkcs8DerOrThrow(wrapSec1InPkcs8(e, r.curve), r);
|
|
637
649
|
}
|
|
638
650
|
/**
|
|
639
651
|
* Import an EC private key from DER-encoded SEC 1 ECPrivateKey.
|
|
640
652
|
*
|
|
641
653
|
* @see `importSec1DerOrThrow` for the throwing variant
|
|
642
654
|
*/
|
|
643
|
-
function importSec1Der(
|
|
644
|
-
return importResult(() => importSec1DerOrThrow(
|
|
655
|
+
function importSec1Der(e, t) {
|
|
656
|
+
return importResult(() => importSec1DerOrThrow(e, t));
|
|
645
657
|
}
|
|
646
658
|
/**
|
|
647
659
|
* Import an EC private key from PEM-encoded SEC 1 ECPrivateKey.
|
|
648
660
|
*
|
|
649
|
-
* Expects the `-----BEGIN EC PRIVATE KEY-----` PEM label.
|
|
661
|
+
* Expects the `-----BEGIN EC PRIVATE KEY-----` PEM label. When `algorithm` is
|
|
662
|
+
* omitted, the curve is inferred from the embedded `parameters [0]` field
|
|
663
|
+
* (see {@linkcode importSec1DerOrThrow}).
|
|
650
664
|
*
|
|
651
665
|
* @see {@linkcode exportSec1Pem} for the inverse operation
|
|
652
666
|
* @see {@linkcode importEncryptedSec1Pem} for encrypted PEM
|
|
653
667
|
*/
|
|
654
|
-
function importSec1PemOrThrow(
|
|
655
|
-
return importSec1DerOrThrow(
|
|
668
|
+
function importSec1PemOrThrow(e, t) {
|
|
669
|
+
return importSec1DerOrThrow(w("EC PRIVATE KEY", e), t);
|
|
656
670
|
}
|
|
657
671
|
/**
|
|
658
672
|
* Import an EC private key from PEM-encoded SEC 1 ECPrivateKey.
|
|
659
673
|
*
|
|
660
674
|
* @see `importSec1PemOrThrow` for the throwing variant
|
|
661
675
|
*/
|
|
662
|
-
function importSec1Pem(
|
|
663
|
-
return importResult(() => importSec1PemOrThrow(
|
|
676
|
+
function importSec1Pem(e, t) {
|
|
677
|
+
return importResult(() => importSec1PemOrThrow(e, t));
|
|
664
678
|
}
|
|
665
679
|
/**
|
|
666
680
|
* Import an EC private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1).
|
|
@@ -670,34 +684,40 @@ function importSec1Pem(pem, algorithm) {
|
|
|
670
684
|
* @see {@linkcode exportEncryptedSec1Pem} for the inverse operation
|
|
671
685
|
* @see {@linkcode importEncryptedPkcs8Pem} for modern PBES2 encryption
|
|
672
686
|
*/
|
|
673
|
-
async function importEncryptedSec1PemOrThrow(
|
|
674
|
-
return importSec1DerOrThrow(await decryptTraditionalPem("EC PRIVATE KEY",
|
|
687
|
+
async function importEncryptedSec1PemOrThrow(e, t, n) {
|
|
688
|
+
return importSec1DerOrThrow(await decryptTraditionalPem("EC PRIVATE KEY", e, t), n);
|
|
675
689
|
}
|
|
676
690
|
/**
|
|
677
691
|
* Import an EC private key from legacy `Proc-Type: 4,ENCRYPTED` PEM (SEC 1).
|
|
678
692
|
*
|
|
679
693
|
* @see `importEncryptedSec1PemOrThrow` for the throwing variant
|
|
680
694
|
*/
|
|
681
|
-
function importEncryptedSec1Pem(
|
|
682
|
-
return encryptedImportResult(() => importEncryptedSec1PemOrThrow(
|
|
695
|
+
function importEncryptedSec1Pem(e, t, n) {
|
|
696
|
+
return encryptedImportResult(() => importEncryptedSec1PemOrThrow(e, t, n));
|
|
683
697
|
}
|
|
684
698
|
/**
|
|
685
699
|
* Import a public verification key from a JSON Web Key.
|
|
686
700
|
*
|
|
701
|
+
* When `algorithm` is omitted, it is inferred from the JWK's own `kty`, `crv`,
|
|
702
|
+
* and `alg` members (e.g. `PS256` → RSA-PSS/SHA-256, `RSA-OAEP-256` →
|
|
703
|
+
* RSA-OAEP/SHA-256; an RSA JWK without `alg` defaults to PKCS#1 v1.5 with
|
|
704
|
+
* SHA-256). Pass `algorithm` to additionally assert an expected algorithm.
|
|
705
|
+
*
|
|
687
706
|
* @param jwk - JSON Web Key object with public key components
|
|
688
|
-
* @param algorithm -
|
|
707
|
+
* @param algorithm - Optional expected algorithm; must match JWK's `kty` and `crv` when given
|
|
689
708
|
* @returns Extractable CryptoKey with `verify` usage
|
|
690
709
|
*
|
|
691
|
-
* @throws {Error} If JWK is malformed
|
|
710
|
+
* @throws {Error} If JWK is malformed, encodes an unsupported algorithm, or doesn't match `algorithm`
|
|
692
711
|
*
|
|
693
712
|
* @see {@linkcode exportPublicJwk} for the inverse operation
|
|
694
713
|
*/
|
|
695
|
-
async function importPublicJwkOrThrow(
|
|
696
|
-
|
|
714
|
+
async function importPublicJwkOrThrow(e, t) {
|
|
715
|
+
let n = t ?? inferJwkImportInput(e);
|
|
716
|
+
assertPublicJwkMatchesRequestedAlgorithm(e, n);
|
|
697
717
|
try {
|
|
698
|
-
return await
|
|
718
|
+
return await m().subtle.importKey("jwk", e, toImportAlgorithm(n), !0, publicKeyUsages(n));
|
|
699
719
|
} catch {
|
|
700
|
-
throw
|
|
720
|
+
throw Error("Malformed public JWK");
|
|
701
721
|
}
|
|
702
722
|
}
|
|
703
723
|
/**
|
|
@@ -705,32 +725,38 @@ async function importPublicJwkOrThrow(jwk, algorithm) {
|
|
|
705
725
|
*
|
|
706
726
|
* @see `importPublicJwkOrThrow` for the throwing variant
|
|
707
727
|
*/
|
|
708
|
-
function importPublicJwk(
|
|
709
|
-
return importResult(() => importPublicJwkOrThrow(
|
|
728
|
+
function importPublicJwk(e, t) {
|
|
729
|
+
return importResult(() => importPublicJwkOrThrow(e, t));
|
|
710
730
|
}
|
|
711
731
|
/**
|
|
712
732
|
* Import a private signing key from a JSON Web Key.
|
|
713
733
|
*
|
|
734
|
+
* When `algorithm` is omitted, it is inferred from the JWK's own `kty`, `crv`,
|
|
735
|
+
* and `alg` members (see {@linkcode importPublicJwkOrThrow}).
|
|
736
|
+
*
|
|
714
737
|
* @param jwk - JSON Web Key object with private key components
|
|
715
|
-
* @param algorithm -
|
|
738
|
+
* @param algorithm - Optional expected algorithm; must match JWK's `kty` and `crv` when given
|
|
716
739
|
* @returns Extractable CryptoKey with `sign` usage
|
|
717
740
|
*
|
|
718
|
-
* @throws {Error} If JWK is malformed, lacks private key material,
|
|
741
|
+
* @throws {Error} If JWK is malformed, lacks private key material, encodes an
|
|
742
|
+
* unsupported algorithm, or doesn't match `algorithm`
|
|
719
743
|
*
|
|
720
744
|
* @example
|
|
721
745
|
* ```ts
|
|
722
746
|
* const jwk = { kty: 'EC', crv: 'P-256', x: '...', y: '...', d: '...' };
|
|
723
747
|
* const key = await importPrivateJwkOrThrow(jwk, { kind: 'ecdsa', curve: 'P-256' });
|
|
748
|
+
* const inferred = await importPrivateJwkOrThrow(jwk);
|
|
724
749
|
* ```
|
|
725
750
|
*
|
|
726
751
|
* @see {@linkcode exportPrivateJwk} for the inverse operation
|
|
727
752
|
*/
|
|
728
|
-
async function importPrivateJwkOrThrow(
|
|
729
|
-
|
|
753
|
+
async function importPrivateJwkOrThrow(e, t) {
|
|
754
|
+
let n = t ?? inferJwkImportInput(e);
|
|
755
|
+
assertPrivateJwkMatchesRequestedAlgorithm(e, n);
|
|
730
756
|
try {
|
|
731
|
-
return await
|
|
757
|
+
return await m().subtle.importKey("jwk", e, toImportAlgorithm(n), !0, privateKeyUsages(n));
|
|
732
758
|
} catch {
|
|
733
|
-
throw
|
|
759
|
+
throw Error("Malformed private JWK");
|
|
734
760
|
}
|
|
735
761
|
}
|
|
736
762
|
/**
|
|
@@ -738,8 +764,8 @@ async function importPrivateJwkOrThrow(jwk, algorithm) {
|
|
|
738
764
|
*
|
|
739
765
|
* @see `importPrivateJwkOrThrow` for the throwing variant
|
|
740
766
|
*/
|
|
741
|
-
function importPrivateJwk(
|
|
742
|
-
return importResult(() => importPrivateJwkOrThrow(
|
|
767
|
+
function importPrivateJwk(e, t) {
|
|
768
|
+
return importResult(() => importPrivateJwkOrThrow(e, t));
|
|
743
769
|
}
|
|
744
770
|
/**
|
|
745
771
|
* Encrypt a small message with an RSA-OAEP public key.
|
|
@@ -768,14 +794,13 @@ function importPrivateJwk(jwk, algorithm) {
|
|
|
768
794
|
* @see {@linkcode decryptRsaOaepOrThrow} for the inverse operation
|
|
769
795
|
* @see `encryptRsaOaep` for the Result-returning variant
|
|
770
796
|
*/
|
|
771
|
-
async function encryptRsaOaepOrThrow(
|
|
772
|
-
|
|
773
|
-
if (
|
|
797
|
+
async function encryptRsaOaepOrThrow(e, t, n = {}) {
|
|
798
|
+
let r = validateRsaOaepKey(e, "public", "encrypt");
|
|
799
|
+
if (r !== void 0) throw Error(r);
|
|
774
800
|
try {
|
|
775
|
-
return new Uint8Array(await
|
|
776
|
-
} catch (
|
|
777
|
-
|
|
778
|
-
throw new Error("Plaintext exceeds the RSA-OAEP capacity of the key (modulus bytes − 2 × hash bytes − 2)");
|
|
801
|
+
return new Uint8Array(await m().subtle.encrypt(toRsaOaepParams(n), e, u(t)));
|
|
802
|
+
} catch (e) {
|
|
803
|
+
throw S(e), Error("Plaintext exceeds the RSA-OAEP capacity of the key (modulus bytes − 2 × hash bytes − 2)");
|
|
779
804
|
}
|
|
780
805
|
}
|
|
781
806
|
/**
|
|
@@ -794,14 +819,13 @@ async function encryptRsaOaepOrThrow(publicKey, plaintext, options = {}) {
|
|
|
794
819
|
*
|
|
795
820
|
* @see `encryptRsaOaepOrThrow` for the throwing variant
|
|
796
821
|
*/
|
|
797
|
-
async function encryptRsaOaep(
|
|
798
|
-
|
|
799
|
-
if (
|
|
822
|
+
async function encryptRsaOaep(e, t, n = {}) {
|
|
823
|
+
let r = validateRsaOaepKey(e, "public", "encrypt");
|
|
824
|
+
if (r !== void 0) return x("invalid_key", r);
|
|
800
825
|
try {
|
|
801
|
-
return
|
|
802
|
-
} catch (
|
|
803
|
-
|
|
804
|
-
return failureResult("message_too_long", error instanceof Error ? error.message : "Plaintext exceeds the RSA-OAEP capacity");
|
|
826
|
+
return C(await encryptRsaOaepOrThrow(e, t, n));
|
|
827
|
+
} catch (e) {
|
|
828
|
+
return S(e), x("message_too_long", e instanceof Error ? e.message : "Plaintext exceeds the RSA-OAEP capacity");
|
|
805
829
|
}
|
|
806
830
|
}
|
|
807
831
|
/**
|
|
@@ -826,14 +850,13 @@ async function encryptRsaOaep(publicKey, plaintext, options = {}) {
|
|
|
826
850
|
* @see {@linkcode encryptRsaOaepOrThrow} for the inverse operation
|
|
827
851
|
* @see `decryptRsaOaep` for the Result-returning variant
|
|
828
852
|
*/
|
|
829
|
-
async function decryptRsaOaepOrThrow(
|
|
830
|
-
|
|
831
|
-
if (
|
|
853
|
+
async function decryptRsaOaepOrThrow(e, t, n = {}) {
|
|
854
|
+
let r = validateRsaOaepKey(e, "private", "decrypt");
|
|
855
|
+
if (r !== void 0) throw Error(r);
|
|
832
856
|
try {
|
|
833
|
-
return new Uint8Array(await
|
|
834
|
-
} catch (
|
|
835
|
-
|
|
836
|
-
throw new Error("RSA-OAEP decryption failed: wrong key, wrong label, or corrupted ciphertext");
|
|
857
|
+
return new Uint8Array(await m().subtle.decrypt(toRsaOaepParams(n), e, u(t)));
|
|
858
|
+
} catch (e) {
|
|
859
|
+
throw S(e), Error("RSA-OAEP decryption failed: wrong key, wrong label, or corrupted ciphertext");
|
|
837
860
|
}
|
|
838
861
|
}
|
|
839
862
|
/**
|
|
@@ -851,61 +874,57 @@ async function decryptRsaOaepOrThrow(privateKey, ciphertext, options = {}) {
|
|
|
851
874
|
*
|
|
852
875
|
* @see `decryptRsaOaepOrThrow` for the throwing variant
|
|
853
876
|
*/
|
|
854
|
-
async function decryptRsaOaep(
|
|
855
|
-
|
|
856
|
-
if (
|
|
877
|
+
async function decryptRsaOaep(e, t, n = {}) {
|
|
878
|
+
let r = validateRsaOaepKey(e, "private", "decrypt");
|
|
879
|
+
if (r !== void 0) return x("invalid_key", r);
|
|
857
880
|
try {
|
|
858
|
-
return
|
|
859
|
-
} catch (
|
|
860
|
-
|
|
861
|
-
return failureResult("decryption_failed", error instanceof Error ? error.message : "RSA-OAEP decryption failed");
|
|
881
|
+
return C(await decryptRsaOaepOrThrow(e, t, n));
|
|
882
|
+
} catch (e) {
|
|
883
|
+
return S(e), x("decryption_failed", e instanceof Error ? e.message : "RSA-OAEP decryption failed");
|
|
862
884
|
}
|
|
863
885
|
}
|
|
864
886
|
/** Map a {@linkcode KeyAlgorithmInput} to the WebCrypto `generateKey` algorithm parameter. */
|
|
865
|
-
function toGenerateKeyAlgorithm(
|
|
866
|
-
switch (
|
|
887
|
+
function toGenerateKeyAlgorithm(e) {
|
|
888
|
+
switch (e.kind) {
|
|
867
889
|
case "rsa": return {
|
|
868
|
-
name: rsaSchemeToWebCryptoAlgorithmName(
|
|
869
|
-
modulusLength:
|
|
890
|
+
name: rsaSchemeToWebCryptoAlgorithmName(e.scheme),
|
|
891
|
+
modulusLength: e.modulusLength ?? 2048,
|
|
870
892
|
publicExponent: Uint8Array.of(1, 0, 1),
|
|
871
|
-
hash:
|
|
893
|
+
hash: e.hash ?? "SHA-256"
|
|
872
894
|
};
|
|
873
895
|
case "ecdsa": return {
|
|
874
896
|
name: "ECDSA",
|
|
875
|
-
namedCurve:
|
|
897
|
+
namedCurve: e.curve ?? "P-256"
|
|
876
898
|
};
|
|
877
899
|
case "ed25519": return { name: "Ed25519" };
|
|
878
900
|
}
|
|
879
901
|
}
|
|
880
902
|
/** Map a {@linkcode PublicKeyImportInput} to the WebCrypto `importKey` algorithm parameter. */
|
|
881
|
-
function toImportAlgorithm(
|
|
882
|
-
switch (
|
|
903
|
+
function toImportAlgorithm(e) {
|
|
904
|
+
switch (e.kind) {
|
|
883
905
|
case "rsa": return {
|
|
884
|
-
name: rsaSchemeToWebCryptoAlgorithmName(
|
|
885
|
-
hash:
|
|
906
|
+
name: rsaSchemeToWebCryptoAlgorithmName(e.scheme),
|
|
907
|
+
hash: e.hash ?? "SHA-256"
|
|
886
908
|
};
|
|
887
909
|
case "ecdsa": return {
|
|
888
910
|
name: "ECDSA",
|
|
889
|
-
namedCurve:
|
|
911
|
+
namedCurve: e.curve
|
|
890
912
|
};
|
|
891
913
|
case "ed25519": return { name: "Ed25519" };
|
|
892
914
|
}
|
|
893
915
|
}
|
|
894
916
|
/** Extract algorithm OID and inner key bytes from a PKCS#8 PrivateKeyInfo envelope. */
|
|
895
|
-
function parsePkcs8PrivateKey(
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
if (
|
|
900
|
-
|
|
901
|
-
const algorithmOid = algorithmChildren[0];
|
|
902
|
-
if (algorithmOid === void 0 || algorithmOid.tag !== 6 || algorithmChildren.length < 1 || algorithmChildren.length > 2) throw new Error("Malformed PKCS#8 private key");
|
|
903
|
-
const parameters = algorithmChildren[1];
|
|
917
|
+
function parsePkcs8PrivateKey(e) {
|
|
918
|
+
let t = a(e), n = t[1], r = t[2];
|
|
919
|
+
if (t.length < 3 || t.length > 4 || n === void 0 || n.tag !== 48 || r === void 0 || r.tag !== 4) throw Error("Malformed PKCS#8 private key");
|
|
920
|
+
let i = a(e.slice(n.start - n.headerLength, n.end)), o = i[0];
|
|
921
|
+
if (o === void 0 || o.tag !== 6 || i.length < 1 || i.length > 2) throw Error("Malformed PKCS#8 private key");
|
|
922
|
+
let c = i[1];
|
|
904
923
|
return {
|
|
905
|
-
algorithmOid:
|
|
906
|
-
...
|
|
907
|
-
...
|
|
908
|
-
privateKeyDer:
|
|
924
|
+
algorithmOid: s(o.value),
|
|
925
|
+
...c === void 0 ? {} : { parametersTag: c.tag },
|
|
926
|
+
...c?.tag === 6 ? { parametersOid: s(c.value) } : {},
|
|
927
|
+
privateKeyDer: r.value
|
|
909
928
|
};
|
|
910
929
|
}
|
|
911
930
|
/**
|
|
@@ -914,42 +933,56 @@ function parsePkcs8PrivateKey(der) {
|
|
|
914
933
|
* RFC 5915: `ECPrivateKey ::= SEQUENCE { version INTEGER, privateKey OCTET STRING,
|
|
915
934
|
* parameters [0] ECParameters OPTIONAL, publicKey [1] BIT STRING OPTIONAL }`.
|
|
916
935
|
*/
|
|
917
|
-
function parseSec1PrivateKey(
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
if (children.length < 2 || children.length > 4 || version === void 0 || version.tag !== 2 || privateKey === void 0 || privateKey.tag !== 4 || third !== void 0 && third.tag !== 160 && third.tag !== 161 || fourth !== void 0 && (third?.tag !== 160 || fourth.tag !== 161)) throw new Error("Malformed SEC 1 private key");
|
|
924
|
-
const parameters = third?.tag === 160 ? third : void 0;
|
|
925
|
-
if (parameters === void 0) return {};
|
|
926
|
-
const ecParameters = readRootElement(parameters.value);
|
|
936
|
+
function parseSec1PrivateKey(e) {
|
|
937
|
+
let t = a(e), n = t[0], r = t[1], o = t[2], c = t[3];
|
|
938
|
+
if (t.length < 2 || t.length > 4 || n === void 0 || n.tag !== 2 || r === void 0 || r.tag !== 4 || o !== void 0 && o.tag !== 160 && o.tag !== 161 || c !== void 0 && (o?.tag !== 160 || c.tag !== 161)) throw Error("Malformed SEC 1 private key");
|
|
939
|
+
let l = o?.tag === 160 ? o : void 0;
|
|
940
|
+
if (l === void 0) return {};
|
|
941
|
+
let u = i(l.value);
|
|
927
942
|
return {
|
|
928
|
-
parametersTag:
|
|
929
|
-
...
|
|
943
|
+
parametersTag: u.tag,
|
|
944
|
+
...u.tag === 6 ? { parametersOid: s(u.value) } : {}
|
|
930
945
|
};
|
|
931
946
|
}
|
|
947
|
+
/**
|
|
948
|
+
* Ensure a SEC 1 ECPrivateKey carries the RFC 5915 `parameters [0]` named
|
|
949
|
+
* curve. WebCrypto's PKCS#8 export omits it from the inner ECPrivateKey (the
|
|
950
|
+
* curve lives in the envelope's privateKeyAlgorithm), but RFC 5915 wants it in
|
|
951
|
+
* standalone SEC 1 — and OpenSSL always writes it. Injecting it keeps exports
|
|
952
|
+
* self-describing, so they re-import without an explicit curve.
|
|
953
|
+
*/
|
|
954
|
+
function ensureSec1NamedCurveParameters(t, r) {
|
|
955
|
+
let i = a(t);
|
|
956
|
+
if (i.some((e) => e.tag === 160)) return new Uint8Array(t);
|
|
957
|
+
if (r === void 0) throw Error("SEC1 export requires a named curve in the PKCS#8 privateKeyAlgorithm");
|
|
958
|
+
let raw = (e) => t.slice(e.start - e.headerLength, e.end);
|
|
959
|
+
return o([
|
|
960
|
+
...i.slice(0, 2).map(raw),
|
|
961
|
+
e(0, n(r)),
|
|
962
|
+
...i.slice(2).map(raw)
|
|
963
|
+
]);
|
|
964
|
+
}
|
|
932
965
|
/** Wrap a PKCS#1 RSAPrivateKey in a PKCS#8 PrivateKeyInfo envelope for WebCrypto import. */
|
|
933
|
-
function wrapPkcs1InPkcs8(
|
|
934
|
-
return
|
|
966
|
+
function wrapPkcs1InPkcs8(e) {
|
|
967
|
+
return o([
|
|
935
968
|
Uint8Array.of(2, 1, 0),
|
|
936
|
-
|
|
937
|
-
|
|
969
|
+
o([n(f.rsaEncryption), t()]),
|
|
970
|
+
r(new Uint8Array(e))
|
|
938
971
|
]);
|
|
939
972
|
}
|
|
940
973
|
/** Wrap a SEC 1 ECPrivateKey in a PKCS#8 PrivateKeyInfo envelope for WebCrypto import. */
|
|
941
|
-
function wrapSec1InPkcs8(
|
|
942
|
-
return
|
|
974
|
+
function wrapSec1InPkcs8(e, t) {
|
|
975
|
+
return o([
|
|
943
976
|
Uint8Array.of(2, 1, 0),
|
|
944
|
-
|
|
945
|
-
|
|
977
|
+
o([n(f.ecPublicKey), n(curveToOid(t))]),
|
|
978
|
+
r(new Uint8Array(e))
|
|
946
979
|
]);
|
|
947
980
|
}
|
|
948
981
|
/** Project a private JWK onto its public-only members, discarding private key material. */
|
|
949
|
-
function toPublicJwk(
|
|
950
|
-
if (
|
|
951
|
-
|
|
952
|
-
for (
|
|
982
|
+
function toPublicJwk(e, t) {
|
|
983
|
+
if (e.kty !== "RSA" && e.kty !== "EC" && e.kty !== "OKP") throw Error(`Cannot derive public key: unsupported JWK key type ${String(e.kty)}`);
|
|
984
|
+
let n = { ...e };
|
|
985
|
+
for (let e of [
|
|
953
986
|
"d",
|
|
954
987
|
"p",
|
|
955
988
|
"q",
|
|
@@ -958,46 +991,42 @@ function toPublicJwk(jwk, usage) {
|
|
|
958
991
|
"qi",
|
|
959
992
|
"oth",
|
|
960
993
|
"k"
|
|
961
|
-
]) delete
|
|
962
|
-
|
|
963
|
-
publicJwk.ext = true;
|
|
964
|
-
return publicJwk;
|
|
994
|
+
]) delete n[e];
|
|
995
|
+
return n.key_ops = [t], n.ext = !0, n;
|
|
965
996
|
}
|
|
966
997
|
/**
|
|
967
998
|
* Single source of truth for the NIST curve ↔ named-curve OID mapping, read in
|
|
968
999
|
* both directions by {@linkcode curveToOid} and {@linkcode oidToCurve}.
|
|
969
1000
|
*/
|
|
970
|
-
const
|
|
971
|
-
["P-256",
|
|
972
|
-
["P-384",
|
|
973
|
-
["P-521",
|
|
1001
|
+
const E = [
|
|
1002
|
+
["P-256", f.prime256v1],
|
|
1003
|
+
["P-384", f.secp384r1],
|
|
1004
|
+
["P-521", f.secp521r1]
|
|
974
1005
|
];
|
|
975
1006
|
/** Map a curve name to its ASN.1 OID string. */
|
|
976
|
-
function curveToOid(
|
|
977
|
-
for (
|
|
978
|
-
throw
|
|
1007
|
+
function curveToOid(e) {
|
|
1008
|
+
for (let [t, n] of E) if (t === e) return n;
|
|
1009
|
+
throw Error(`Unsupported EC curve: ${e}`);
|
|
979
1010
|
}
|
|
980
1011
|
/** Map an ASN.1 curve OID string back to its curve name, or `undefined` if unrecognized. */
|
|
981
|
-
function oidToCurve(
|
|
982
|
-
for (
|
|
1012
|
+
function oidToCurve(e) {
|
|
1013
|
+
for (let [t, n] of E) if (n === e) return t;
|
|
983
1014
|
}
|
|
984
1015
|
/** Map an {@linkcode RsaScheme} to the WebCrypto algorithm name string. */
|
|
985
|
-
function rsaSchemeToWebCryptoAlgorithmName(
|
|
986
|
-
|
|
987
|
-
if (scheme === "oaep") return "RSA-OAEP";
|
|
988
|
-
return "RSASSA-PKCS1-v1_5";
|
|
1016
|
+
function rsaSchemeToWebCryptoAlgorithmName(e) {
|
|
1017
|
+
return e === "pss" ? "RSA-PSS" : e === "oaep" ? "RSA-OAEP" : "RSASSA-PKCS1-v1_5";
|
|
989
1018
|
}
|
|
990
1019
|
/** `true` when the algorithm input selects RSA-OAEP, whose keys carry encrypt/decrypt usages. */
|
|
991
|
-
function isRsaOaepInput(
|
|
992
|
-
return
|
|
1020
|
+
function isRsaOaepInput(e) {
|
|
1021
|
+
return e.kind === "rsa" && e.scheme === "oaep";
|
|
993
1022
|
}
|
|
994
1023
|
/** Key usages for an imported public key: `verify`, or `encrypt` for RSA-OAEP. */
|
|
995
|
-
function publicKeyUsages(
|
|
996
|
-
return isRsaOaepInput(
|
|
1024
|
+
function publicKeyUsages(e) {
|
|
1025
|
+
return isRsaOaepInput(e) ? ["encrypt"] : ["verify"];
|
|
997
1026
|
}
|
|
998
1027
|
/** Key usages for an imported private key: `sign`, or `decrypt` for RSA-OAEP. */
|
|
999
|
-
function privateKeyUsages(
|
|
1000
|
-
return isRsaOaepInput(
|
|
1028
|
+
function privateKeyUsages(e) {
|
|
1029
|
+
return isRsaOaepInput(e) ? ["decrypt"] : ["sign"];
|
|
1001
1030
|
}
|
|
1002
1031
|
/**
|
|
1003
1032
|
* Diagnose a key unfit for the given RSA-OAEP operation.
|
|
@@ -1006,242 +1035,301 @@ function privateKeyUsages(algorithm) {
|
|
|
1006
1035
|
* usable — keeping key-level failures (`'invalid_key'`) separable from
|
|
1007
1036
|
* ciphertext-level failures before WebCrypto is ever called.
|
|
1008
1037
|
*/
|
|
1009
|
-
function validateRsaOaepKey(
|
|
1010
|
-
|
|
1011
|
-
if (
|
|
1012
|
-
if (
|
|
1013
|
-
if (!
|
|
1038
|
+
function validateRsaOaepKey(e, t, n) {
|
|
1039
|
+
let r = n === "encrypt" ? "encryption" : "decryption";
|
|
1040
|
+
if (e.type !== t) return `RSA-OAEP ${r} requires a ${t} CryptoKey`;
|
|
1041
|
+
if (e.algorithm.name !== "RSA-OAEP") return `RSA-OAEP ${r} requires an RSA-OAEP key (got ${e.algorithm.name}); generate or import the key with scheme: 'oaep'`;
|
|
1042
|
+
if (!e.usages.includes(n)) return `RSA-OAEP ${r} requires a key with '${n}' usage`;
|
|
1014
1043
|
}
|
|
1015
1044
|
/** Build the WebCrypto RSA-OAEP params, attaching the optional label. */
|
|
1016
|
-
function toRsaOaepParams(
|
|
1045
|
+
function toRsaOaepParams(e) {
|
|
1017
1046
|
return {
|
|
1018
1047
|
name: "RSA-OAEP",
|
|
1019
|
-
...
|
|
1048
|
+
...e.label === void 0 ? {} : { label: u(e.label) }
|
|
1020
1049
|
};
|
|
1021
1050
|
}
|
|
1022
1051
|
/** Encrypt DER key material as an OpenSSL-style `Proc-Type: 4,ENCRYPTED` PEM block. */
|
|
1023
|
-
async function encryptTraditionalPem(
|
|
1024
|
-
|
|
1025
|
-
if (
|
|
1026
|
-
|
|
1027
|
-
const key = await importTraditionalPemAesKey(options.password, iv.slice(0, 8), cipher, ["encrypt"]);
|
|
1028
|
-
const body = base64Encode(new Uint8Array(await getCrypto().subtle.encrypt({
|
|
1052
|
+
async function encryptTraditionalPem(e, t, n) {
|
|
1053
|
+
let r = n.iv ?? m().getRandomValues(/* @__PURE__ */ new Uint8Array(16));
|
|
1054
|
+
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 = b(new Uint8Array(await m().subtle.encrypt({
|
|
1029
1056
|
name: "AES-CBC",
|
|
1030
|
-
iv:
|
|
1031
|
-
},
|
|
1057
|
+
iv: u(r)
|
|
1058
|
+
}, a, u(t)))).match(/.{1,64}/g)?.join("\n") ?? "";
|
|
1032
1059
|
return [
|
|
1033
|
-
`-----BEGIN ${
|
|
1060
|
+
`-----BEGIN ${e}-----`,
|
|
1034
1061
|
"Proc-Type: 4,ENCRYPTED",
|
|
1035
|
-
`DEK-Info: ${
|
|
1062
|
+
`DEK-Info: ${i},${d(r).toUpperCase()}`,
|
|
1036
1063
|
"",
|
|
1037
|
-
|
|
1038
|
-
`-----END ${
|
|
1064
|
+
o,
|
|
1065
|
+
`-----END ${e}-----`
|
|
1039
1066
|
].join("\n");
|
|
1040
1067
|
}
|
|
1041
1068
|
/** Decrypt an OpenSSL-style `Proc-Type: 4,ENCRYPTED` PEM block back to plaintext DER. */
|
|
1042
|
-
async function decryptTraditionalPem(
|
|
1043
|
-
|
|
1044
|
-
if (
|
|
1045
|
-
|
|
1046
|
-
if (
|
|
1047
|
-
|
|
1048
|
-
if (!isTraditionalPemCipher(
|
|
1049
|
-
if (!isTraditionalPemIvHex(
|
|
1050
|
-
|
|
1051
|
-
const key = await importTraditionalPemAesKey(password, iv.slice(0, 8), cipher, ["decrypt"]);
|
|
1069
|
+
async function decryptTraditionalPem(e, t, n) {
|
|
1070
|
+
let r = parseTraditionalPem(t);
|
|
1071
|
+
if (r.label !== e) throw Error(`Expected ${e} PEM block`);
|
|
1072
|
+
let i = r.headers.get("DEK-Info");
|
|
1073
|
+
if (r.headers.get("Proc-Type") !== "4,ENCRYPTED" || i === void 0) throw Error("Traditional PEM encryption headers missing");
|
|
1074
|
+
let [a, o] = i.split(",");
|
|
1075
|
+
if (!isTraditionalPemCipher(a) || o === void 0) throw Error("Only AES-128-CBC, AES-192-CBC, and AES-256-CBC traditional PEM encryption is supported");
|
|
1076
|
+
if (!isTraditionalPemIvHex(o)) throw Error("Traditional PEM encryption requires a 16-byte IV encoded as 32 hex characters");
|
|
1077
|
+
let s = l(o), c = await importTraditionalPemAesKey(n, s.slice(0, 8), a, ["decrypt"]);
|
|
1052
1078
|
try {
|
|
1053
|
-
return new Uint8Array(await
|
|
1079
|
+
return new Uint8Array(await m().subtle.decrypt({
|
|
1054
1080
|
name: "AES-CBC",
|
|
1055
|
-
iv:
|
|
1056
|
-
},
|
|
1081
|
+
iv: u(s)
|
|
1082
|
+
}, c, u(y(r.base64Body))));
|
|
1057
1083
|
} catch {
|
|
1058
|
-
throw
|
|
1084
|
+
throw v("Invalid password or encrypted PEM content");
|
|
1059
1085
|
}
|
|
1060
1086
|
}
|
|
1061
1087
|
/** Derive and import an AES-CBC key for legacy PEM encryption using OpenSSL `EVP_BytesToKey`. */
|
|
1062
|
-
function importTraditionalPemAesKey(
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
return getCrypto().subtle.importKey("raw", toArrayBuffer(keyBytes), {
|
|
1088
|
+
function importTraditionalPemAesKey(e, t, n, r) {
|
|
1089
|
+
let i = traditionalPemCipherKeyLength(n), a = opensslBytesToKey(e, t, i / 8);
|
|
1090
|
+
return m().subtle.importKey("raw", u(a), {
|
|
1066
1091
|
name: "AES-CBC",
|
|
1067
|
-
length:
|
|
1068
|
-
},
|
|
1092
|
+
length: i
|
|
1093
|
+
}, !1, r);
|
|
1069
1094
|
}
|
|
1070
1095
|
/** Type guard for the three AES-CBC ciphers supported by legacy PEM encryption. */
|
|
1071
|
-
function isTraditionalPemCipher(
|
|
1072
|
-
return
|
|
1096
|
+
function isTraditionalPemCipher(e) {
|
|
1097
|
+
return e === "AES-128-CBC" || e === "AES-192-CBC" || e === "AES-256-CBC";
|
|
1073
1098
|
}
|
|
1074
|
-
function isTraditionalPemIvHex(
|
|
1075
|
-
return
|
|
1099
|
+
function isTraditionalPemIvHex(e) {
|
|
1100
|
+
return e.length === 32 && /^[0-9A-Fa-f]+$/.test(e);
|
|
1076
1101
|
}
|
|
1077
1102
|
/** Return the AES key size in bits for a given cipher name. */
|
|
1078
|
-
function traditionalPemCipherKeyLength(
|
|
1079
|
-
switch (
|
|
1103
|
+
function traditionalPemCipherKeyLength(e) {
|
|
1104
|
+
switch (e) {
|
|
1080
1105
|
case "AES-128-CBC": return 128;
|
|
1081
1106
|
case "AES-192-CBC": return 192;
|
|
1082
1107
|
case "AES-256-CBC": return 256;
|
|
1083
1108
|
}
|
|
1084
1109
|
}
|
|
1085
1110
|
/** OpenSSL `EVP_BytesToKey` with MD5 — derives a fixed-length key from password + salt. */
|
|
1086
|
-
function opensslBytesToKey(
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
while (total < length) {
|
|
1092
|
-
const input = new Uint8Array(previous.length + passwordBytes.length + salt.length);
|
|
1093
|
-
input.set(previous, 0);
|
|
1094
|
-
input.set(passwordBytes, previous.length);
|
|
1095
|
-
input.set(salt, previous.length + passwordBytes.length);
|
|
1096
|
-
previous = md5(input);
|
|
1097
|
-
chunks.push(previous);
|
|
1098
|
-
total += previous.length;
|
|
1111
|
+
function opensslBytesToKey(e, t, n) {
|
|
1112
|
+
let r = new TextEncoder().encode(e), i = [], a = /* @__PURE__ */ new Uint8Array(), o = 0;
|
|
1113
|
+
for (; o < n;) {
|
|
1114
|
+
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 = p(e), i.push(a), o += a.length;
|
|
1099
1116
|
}
|
|
1100
|
-
|
|
1101
|
-
let
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
out.set(slice, offset);
|
|
1105
|
-
offset += slice.length;
|
|
1106
|
-
if (offset >= length) break;
|
|
1117
|
+
let s = new Uint8Array(n), c = 0;
|
|
1118
|
+
for (let e of i) {
|
|
1119
|
+
let t = e.slice(0, Math.min(e.length, n - c));
|
|
1120
|
+
if (s.set(t, c), c += t.length, c >= n) break;
|
|
1107
1121
|
}
|
|
1108
|
-
return
|
|
1122
|
+
return s;
|
|
1109
1123
|
}
|
|
1110
1124
|
/** Parse a PEM block into its label, RFC 1421 headers, and base64 body. */
|
|
1111
|
-
function parseTraditionalPem(
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
if (
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
if (line === void 0) break;
|
|
1123
|
-
if (line.length === 0) {
|
|
1124
|
-
index += 1;
|
|
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;
|
|
1125
1136
|
break;
|
|
1126
1137
|
}
|
|
1127
|
-
|
|
1128
|
-
if (
|
|
1129
|
-
|
|
1130
|
-
if (
|
|
1131
|
-
|
|
1132
|
-
index += 1;
|
|
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;
|
|
1133
1143
|
}
|
|
1134
1144
|
return {
|
|
1135
|
-
label,
|
|
1136
|
-
headers,
|
|
1137
|
-
base64Body:
|
|
1145
|
+
label: i,
|
|
1146
|
+
headers: a,
|
|
1147
|
+
base64Body: t.slice(o, t.length - 1).join("")
|
|
1138
1148
|
};
|
|
1139
1149
|
}
|
|
1140
|
-
function parseSpkiDer(
|
|
1150
|
+
function parseSpkiDer(e) {
|
|
1141
1151
|
try {
|
|
1142
|
-
|
|
1143
|
-
if (
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
if (algorithmOid === void 0 || algorithmOid.tag !== 6 || algorithmChildren.length < 1 || algorithmChildren.length > 2) throw new Error("Malformed SubjectPublicKeyInfo");
|
|
1151
|
-
const parameters = algorithmChildren[1];
|
|
1152
|
+
let t = a(e);
|
|
1153
|
+
if (t.length !== 2) throw Error("Malformed SubjectPublicKeyInfo");
|
|
1154
|
+
let n = t[0], r = t[1];
|
|
1155
|
+
if (n === void 0 || n.tag !== 48 || r === void 0 || r.tag !== 3) throw Error("Malformed SubjectPublicKeyInfo");
|
|
1156
|
+
c(r);
|
|
1157
|
+
let i = a(e.slice(n.start - n.headerLength, n.end)), o = i[0];
|
|
1158
|
+
if (o === void 0 || o.tag !== 6 || i.length < 1 || i.length > 2) throw Error("Malformed SubjectPublicKeyInfo");
|
|
1159
|
+
let l = i[1];
|
|
1152
1160
|
return {
|
|
1153
|
-
algorithmOid:
|
|
1154
|
-
...
|
|
1155
|
-
...
|
|
1161
|
+
algorithmOid: s(o.value),
|
|
1162
|
+
...l === void 0 ? {} : { parametersTag: l.tag },
|
|
1163
|
+
...l?.tag === 6 ? { parametersOid: s(l.value) } : {}
|
|
1156
1164
|
};
|
|
1157
1165
|
} catch {
|
|
1158
|
-
throw
|
|
1166
|
+
throw Error("Malformed SubjectPublicKeyInfo");
|
|
1159
1167
|
}
|
|
1160
1168
|
}
|
|
1161
1169
|
/**
|
|
1162
|
-
* Derive the import algorithm from a
|
|
1170
|
+
* Derive the import algorithm from a key envelope's own AlgorithmIdentifier
|
|
1171
|
+
* (SPKI `algorithm` or PKCS#8 `privateKeyAlgorithm` — both share the shape).
|
|
1163
1172
|
*
|
|
1164
1173
|
* RSA keys default to the `pkcs1-v1_5`/`SHA-256` import parameters (a plain
|
|
1165
|
-
* `rsaEncryption`
|
|
1166
|
-
* their curve in the DER; Ed25519 is fully determined by its OID.
|
|
1174
|
+
* `rsaEncryption` AlgorithmIdentifier does not encode padding scheme or hash);
|
|
1175
|
+
* EC keys carry their curve in the DER; Ed25519 is fully determined by its OID.
|
|
1167
1176
|
*/
|
|
1168
|
-
function
|
|
1169
|
-
switch (
|
|
1170
|
-
case
|
|
1171
|
-
if (
|
|
1177
|
+
function inferKeyImportInput(e, t) {
|
|
1178
|
+
switch (e.algorithmOid) {
|
|
1179
|
+
case f.rsaEncryption:
|
|
1180
|
+
if (e.parametersTag === void 0 || e.parametersTag === 5) return { kind: "rsa" };
|
|
1172
1181
|
break;
|
|
1173
|
-
case
|
|
1174
|
-
|
|
1175
|
-
if (
|
|
1182
|
+
case f.ecPublicKey: {
|
|
1183
|
+
let t = e.parametersTag === 6 && e.parametersOid !== void 0 ? oidToCurve(e.parametersOid) : void 0;
|
|
1184
|
+
if (t !== void 0) return {
|
|
1176
1185
|
kind: "ecdsa",
|
|
1177
|
-
curve
|
|
1186
|
+
curve: t
|
|
1178
1187
|
};
|
|
1179
1188
|
break;
|
|
1180
1189
|
}
|
|
1181
|
-
case
|
|
1182
|
-
if (
|
|
1190
|
+
case f.ed25519:
|
|
1191
|
+
if (e.parametersTag === void 0) return { kind: "ed25519" };
|
|
1183
1192
|
break;
|
|
1184
1193
|
}
|
|
1185
|
-
throw
|
|
1194
|
+
throw Error(t);
|
|
1195
|
+
}
|
|
1196
|
+
/**
|
|
1197
|
+
* Derive the import curve from a SEC 1 ECPrivateKey's own RFC 5915
|
|
1198
|
+
* `parameters [0]` field. SEC 1 encodes no algorithm family (it is EC by
|
|
1199
|
+
* definition), so only the named curve needs recovering — and it may be
|
|
1200
|
+
* absent, in which case the caller must supply it.
|
|
1201
|
+
*/
|
|
1202
|
+
function inferSec1ImportInput(e) {
|
|
1203
|
+
let t = e.parametersTag === 6 && e.parametersOid !== void 0 ? oidToCurve(e.parametersOid) : void 0;
|
|
1204
|
+
if (t === void 0) throw Error("SEC 1 private key does not encode a supported named curve; pass the algorithm explicitly");
|
|
1205
|
+
return {
|
|
1206
|
+
kind: "ecdsa",
|
|
1207
|
+
curve: t
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
/**
|
|
1211
|
+
* Derive the import algorithm from a JWK's own `kty`, `crv`, and `alg` members.
|
|
1212
|
+
*
|
|
1213
|
+
* `alg` disambiguates the RSA scheme and hash (`RS*`, `PS*`, `RSA-OAEP-*`) —
|
|
1214
|
+
* WebCrypto rejects a JWK whose `alg` disagrees with the import algorithm, so
|
|
1215
|
+
* it must be honored rather than defaulted over. An RSA JWK without `alg`
|
|
1216
|
+
* defaults to `pkcs1-v1_5`/`SHA-256`, mirroring SPKI inference.
|
|
1217
|
+
*/
|
|
1218
|
+
function inferJwkImportInput(e) {
|
|
1219
|
+
if (e.kty === "RSA") return rsaImportInputFromJwkAlg(e.alg);
|
|
1220
|
+
if (e.kty === "EC") {
|
|
1221
|
+
if (e.crv === "P-256" || e.crv === "P-384" || e.crv === "P-521") return {
|
|
1222
|
+
kind: "ecdsa",
|
|
1223
|
+
curve: e.crv
|
|
1224
|
+
};
|
|
1225
|
+
throw Error(`Unsupported EC JWK curve: ${String(e.crv)}`);
|
|
1226
|
+
}
|
|
1227
|
+
if (e.kty === "OKP" && e.crv === "Ed25519") return { kind: "ed25519" };
|
|
1228
|
+
throw Error("Unsupported JWK key type");
|
|
1229
|
+
}
|
|
1230
|
+
/** Map a JWA `alg` identifier to the RSA scheme + hash it pins down. */
|
|
1231
|
+
function rsaImportInputFromJwkAlg(e) {
|
|
1232
|
+
switch (e) {
|
|
1233
|
+
case void 0:
|
|
1234
|
+
case "RS256": return { kind: "rsa" };
|
|
1235
|
+
case "RS384": return {
|
|
1236
|
+
kind: "rsa",
|
|
1237
|
+
hash: "SHA-384"
|
|
1238
|
+
};
|
|
1239
|
+
case "RS512": return {
|
|
1240
|
+
kind: "rsa",
|
|
1241
|
+
hash: "SHA-512"
|
|
1242
|
+
};
|
|
1243
|
+
case "PS256": return {
|
|
1244
|
+
kind: "rsa",
|
|
1245
|
+
scheme: "pss"
|
|
1246
|
+
};
|
|
1247
|
+
case "PS384": return {
|
|
1248
|
+
kind: "rsa",
|
|
1249
|
+
scheme: "pss",
|
|
1250
|
+
hash: "SHA-384"
|
|
1251
|
+
};
|
|
1252
|
+
case "PS512": return {
|
|
1253
|
+
kind: "rsa",
|
|
1254
|
+
scheme: "pss",
|
|
1255
|
+
hash: "SHA-512"
|
|
1256
|
+
};
|
|
1257
|
+
case "RSA-OAEP-256": return {
|
|
1258
|
+
kind: "rsa",
|
|
1259
|
+
scheme: "oaep"
|
|
1260
|
+
};
|
|
1261
|
+
case "RSA-OAEP-384": return {
|
|
1262
|
+
kind: "rsa",
|
|
1263
|
+
scheme: "oaep",
|
|
1264
|
+
hash: "SHA-384"
|
|
1265
|
+
};
|
|
1266
|
+
case "RSA-OAEP-512": return {
|
|
1267
|
+
kind: "rsa",
|
|
1268
|
+
scheme: "oaep",
|
|
1269
|
+
hash: "SHA-512"
|
|
1270
|
+
};
|
|
1271
|
+
default: throw Error(`Unsupported RSA JWK alg: ${e}`);
|
|
1272
|
+
}
|
|
1186
1273
|
}
|
|
1187
|
-
function assertSpkiMatchesRequestedAlgorithm(
|
|
1188
|
-
switch (
|
|
1274
|
+
function assertSpkiMatchesRequestedAlgorithm(e, t) {
|
|
1275
|
+
switch (t.kind) {
|
|
1189
1276
|
case "rsa":
|
|
1190
|
-
if (
|
|
1277
|
+
if (e.algorithmOid !== f.rsaEncryption || e.parametersTag !== void 0 && e.parametersTag !== 5) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
|
|
1191
1278
|
return;
|
|
1192
1279
|
case "ecdsa":
|
|
1193
|
-
if (
|
|
1280
|
+
if (e.algorithmOid !== f.ecPublicKey || e.parametersTag !== 6 || e.parametersOid !== curveToOid(t.curve)) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
|
|
1194
1281
|
return;
|
|
1195
1282
|
case "ed25519":
|
|
1196
|
-
if (
|
|
1283
|
+
if (e.algorithmOid !== f.ed25519 || e.parametersTag !== void 0) throw Error("SubjectPublicKeyInfo algorithm does not match requested import algorithm");
|
|
1197
1284
|
return;
|
|
1198
1285
|
}
|
|
1199
1286
|
}
|
|
1200
|
-
function assertPkcs8MatchesRequestedAlgorithm(
|
|
1201
|
-
switch (
|
|
1287
|
+
function assertPkcs8MatchesRequestedAlgorithm(e, t) {
|
|
1288
|
+
switch (t.kind) {
|
|
1202
1289
|
case "rsa":
|
|
1203
|
-
if (
|
|
1290
|
+
if (e.algorithmOid !== f.rsaEncryption || e.parametersTag !== void 0 && e.parametersTag !== 5) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
|
|
1204
1291
|
return;
|
|
1205
1292
|
case "ecdsa":
|
|
1206
|
-
if (
|
|
1293
|
+
if (e.algorithmOid !== f.ecPublicKey || e.parametersTag !== 6 || e.parametersOid !== curveToOid(t.curve)) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
|
|
1207
1294
|
return;
|
|
1208
1295
|
case "ed25519":
|
|
1209
|
-
if (
|
|
1296
|
+
if (e.algorithmOid !== f.ed25519 || e.parametersTag !== void 0) throw Error("PKCS#8 private key algorithm does not match requested import algorithm");
|
|
1210
1297
|
return;
|
|
1211
1298
|
}
|
|
1212
1299
|
}
|
|
1213
|
-
function assertSec1MatchesRequestedAlgorithm(
|
|
1214
|
-
if (
|
|
1215
|
-
if (parsedSec1.parametersTag !== 6 || parsedSec1.parametersOid !== curveToOid(algorithm.curve)) throw new Error("SEC 1 private key curve does not match requested import algorithm");
|
|
1300
|
+
function assertSec1MatchesRequestedAlgorithm(e, t) {
|
|
1301
|
+
if (e.parametersTag !== void 0 && (e.parametersTag !== 6 || e.parametersOid !== curveToOid(t.curve))) throw Error("SEC 1 private key curve does not match requested import algorithm");
|
|
1216
1302
|
}
|
|
1217
|
-
function assertPublicJwkMatchesRequestedAlgorithm(
|
|
1218
|
-
if (
|
|
1219
|
-
switch (
|
|
1303
|
+
function assertPublicJwkMatchesRequestedAlgorithm(e, t) {
|
|
1304
|
+
if (e.k !== void 0 || e.d !== void 0 || e.p !== void 0 || e.q !== void 0 || e.dp !== void 0 || e.dq !== void 0 || e.qi !== void 0 || e.oth !== void 0) throw Error("Public JWK must not contain private key material");
|
|
1305
|
+
switch (t.kind) {
|
|
1220
1306
|
case "rsa":
|
|
1221
|
-
if (
|
|
1307
|
+
if (e.kty !== "RSA" || typeof e.n != "string" || typeof e.e != "string") throw Error("Public JWK algorithm does not match requested import algorithm");
|
|
1222
1308
|
return;
|
|
1223
1309
|
case "ecdsa":
|
|
1224
|
-
if (
|
|
1310
|
+
if (e.kty !== "EC" || e.crv !== t.curve || typeof e.x != "string" || typeof e.y != "string") throw Error("Public JWK algorithm does not match requested import algorithm");
|
|
1225
1311
|
return;
|
|
1226
1312
|
case "ed25519":
|
|
1227
|
-
if (
|
|
1313
|
+
if (e.kty !== "OKP" || e.crv !== "Ed25519" || typeof e.x != "string") throw Error("Public JWK algorithm does not match requested import algorithm");
|
|
1228
1314
|
return;
|
|
1229
1315
|
}
|
|
1230
1316
|
}
|
|
1231
|
-
function assertPrivateJwkMatchesRequestedAlgorithm(
|
|
1232
|
-
if (
|
|
1233
|
-
if (typeof
|
|
1234
|
-
switch (
|
|
1317
|
+
function assertPrivateJwkMatchesRequestedAlgorithm(e, t) {
|
|
1318
|
+
if (e.k !== void 0 || e.oth !== void 0) throw Error("Private JWK must not contain symmetric or multi-prime key material");
|
|
1319
|
+
if (typeof e.d != "string") throw Error("Private JWK must contain private key material");
|
|
1320
|
+
switch (t.kind) {
|
|
1235
1321
|
case "rsa":
|
|
1236
|
-
if (
|
|
1322
|
+
if (e.kty !== "RSA" || typeof e.n != "string" || typeof e.e != "string" || typeof e.p != "string" || typeof e.q != "string" || typeof e.dp != "string" || typeof e.dq != "string" || typeof e.qi != "string") throw Error("Private JWK algorithm does not match requested import algorithm");
|
|
1237
1323
|
return;
|
|
1238
1324
|
case "ecdsa":
|
|
1239
|
-
if (
|
|
1325
|
+
if (e.kty !== "EC" || e.crv !== t.curve || typeof e.x != "string" || typeof e.y != "string") throw Error("Private JWK algorithm does not match requested import algorithm");
|
|
1240
1326
|
return;
|
|
1241
1327
|
case "ed25519":
|
|
1242
|
-
if (
|
|
1328
|
+
if (e.kty !== "OKP" || e.crv !== "Ed25519" || typeof e.x != "string") throw Error("Private JWK algorithm does not match requested import algorithm");
|
|
1243
1329
|
return;
|
|
1244
1330
|
}
|
|
1245
1331
|
}
|
|
1246
1332
|
//#endregion
|
|
1247
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 };
|
|
1334
|
+
|
|
1335
|
+
//# sourceMappingURL=keys.js.map
|