unjwt 0.5.16 → 0.5.17
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/README.md +0 -8
- package/dist/index.d.mts +5 -5
- package/dist/index.mjs +4 -5
- package/dist/jwe.d.mts +2 -2
- package/dist/jwe.mjs +3 -4
- package/dist/jwk.d.mts +2 -2
- package/dist/jwk.mjs +2 -2
- package/dist/jws.d.mts +2 -2
- package/dist/jws.mjs +3 -4
- package/dist/shared/{unjwt.D1MC63Yb.mjs → unjwt.A5jxFsjh.mjs} +578 -44
- package/dist/shared/{unjwt.BkUfaQRR.d.mts → unjwt.BQUETRUD.d.mts} +25 -8
- package/dist/shared/{unjwt.u3h40Nr-.mjs → unjwt.Bui8we0H.mjs} +2 -3
- package/dist/shared/{unjwt.OzI0aGB_.d.mts → unjwt.ClaeEBcy.d.mts} +20 -8
- package/dist/shared/unjwt.D5J5ziZr.mjs +247 -0
- package/dist/shared/{unjwt.CbxAdgsb.d.mts → unjwt.Djt8pPsc.d.mts} +17 -2
- package/dist/shared/{unjwt.DUVOOOvO.d.mts → unjwt.DuLUtSpD.d.mts} +1 -1
- package/dist/shared/{unjwt.r8lbwWSJ.d.mts → unjwt.fumhLslo.d.mts} +4 -2
- package/dist/shared/{unjwt.CRHfsi7v.mjs → unjwt.tM5bsBSH.mjs} +1 -1
- package/dist/utils.d.mts +2 -2
- package/dist/utils.mjs +1 -1
- package/package.json +3 -3
- package/dist/shared/unjwt.BZ3B0Ak8.mjs +0 -395
- package/dist/shared/unjwt.BpusjhPR.mjs +0 -101
|
@@ -1,395 +0,0 @@
|
|
|
1
|
-
import { a as checkEncCryptoKey, u as uint32be, b as generateCEK, e as encryptIV, w as wrap, k as keyToJWK, d as bitLengthCEK, f as jwkTokey, i as importKey, h as generateIV, l as encrypt$1, m as unwrapKey, n as decrypt$1 } from './unjwt.D1MC63Yb.mjs';
|
|
2
|
-
import { k as concatUint8Arrays, t as textEncoder, l as assertCryptoKey, d as base64UrlEncode, m as isCryptoKey, a as isJWK, s as sanitizeObject, f as base64UrlDecode, r as randomBytes, b as applyTypCtyDefaults, c as computeJwtTimeClaims, n as getPlaintextBytes, o as decodePayloadFromBytes, p as validateCriticalHeadersJWE, j as validateJwtClaims } from './unjwt.CRHfsi7v.mjs';
|
|
3
|
-
import { c as checkKeyLength } from './unjwt.BpusjhPR.mjs';
|
|
4
|
-
|
|
5
|
-
function lengthAndInput(input) {
|
|
6
|
-
return concatUint8Arrays(uint32be(input.length), input);
|
|
7
|
-
}
|
|
8
|
-
async function concatKdf(secret, bits, value) {
|
|
9
|
-
const iterations = Math.ceil((bits >> 3) / 32);
|
|
10
|
-
const res = new Uint8Array(iterations * 32);
|
|
11
|
-
for (let iter = 0; iter < iterations; iter++) {
|
|
12
|
-
const buf = new Uint8Array(4 + secret.length + value.length);
|
|
13
|
-
buf.set(uint32be(iter + 1));
|
|
14
|
-
buf.set(secret, 4);
|
|
15
|
-
buf.set(value, 4 + secret.length);
|
|
16
|
-
res.set(await digest("sha256", buf), iter * 32);
|
|
17
|
-
}
|
|
18
|
-
return res.slice(0, bits >> 3);
|
|
19
|
-
}
|
|
20
|
-
async function deriveECDHESKey(publicKey, privateKey, algorithm, keyLength, apu = new Uint8Array(0), apv = new Uint8Array(0)) {
|
|
21
|
-
checkEncCryptoKey(publicKey, "ECDH");
|
|
22
|
-
checkEncCryptoKey(privateKey, "ECDH", "deriveBits");
|
|
23
|
-
const value = concatUint8Arrays(
|
|
24
|
-
lengthAndInput(textEncoder.encode(algorithm)),
|
|
25
|
-
lengthAndInput(apu),
|
|
26
|
-
lengthAndInput(apv),
|
|
27
|
-
uint32be(keyLength)
|
|
28
|
-
);
|
|
29
|
-
const length = publicKey.algorithm.name === "X25519" ? 256 : Math.ceil(
|
|
30
|
-
Number.parseInt(
|
|
31
|
-
publicKey.algorithm.namedCurve.slice(-3),
|
|
32
|
-
10
|
|
33
|
-
) / 8
|
|
34
|
-
) << 3;
|
|
35
|
-
const sharedSecret = new Uint8Array(
|
|
36
|
-
await crypto.subtle.deriveBits(
|
|
37
|
-
{
|
|
38
|
-
name: publicKey.algorithm.name,
|
|
39
|
-
public: publicKey
|
|
40
|
-
},
|
|
41
|
-
privateKey,
|
|
42
|
-
length
|
|
43
|
-
)
|
|
44
|
-
);
|
|
45
|
-
return concatKdf(sharedSecret, keyLength, value);
|
|
46
|
-
}
|
|
47
|
-
function allowed(key) {
|
|
48
|
-
switch (key.algorithm.namedCurve) {
|
|
49
|
-
case "P-256":
|
|
50
|
-
case "P-384":
|
|
51
|
-
case "P-521": {
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
default: {
|
|
55
|
-
return key.algorithm.name === "X25519";
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
async function digest(algorithm, data) {
|
|
60
|
-
const subtleDigest = `SHA-${algorithm.slice(-3)}`;
|
|
61
|
-
return new Uint8Array(await crypto.subtle.digest(subtleDigest, data));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const subtleAlgorithm = (alg) => {
|
|
65
|
-
switch (alg) {
|
|
66
|
-
case "RSA-OAEP":
|
|
67
|
-
case "RSA-OAEP-256":
|
|
68
|
-
case "RSA-OAEP-384":
|
|
69
|
-
case "RSA-OAEP-512": {
|
|
70
|
-
return "RSA-OAEP";
|
|
71
|
-
}
|
|
72
|
-
default: {
|
|
73
|
-
throw new Error(
|
|
74
|
-
`alg ${alg} is not supported either by JOSE or your javascript runtime`
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
async function encryptRSAES(alg, key, cek) {
|
|
80
|
-
checkEncCryptoKey(key, alg, "encrypt");
|
|
81
|
-
checkKeyLength(alg, key);
|
|
82
|
-
return new Uint8Array(
|
|
83
|
-
await crypto.subtle.encrypt(subtleAlgorithm(alg), key, cek)
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* This is a fork of Jose library's utility functions.
|
|
89
|
-
* @source https://github.com/panva/jose/tree/69b7960c67e05be55fa2ec31c74b987696c20c60/src/lib/encrypt_key_management.ts
|
|
90
|
-
* @license MIT https://github.com/panva/jose/blob/69b7960c67e05be55fa2ec31c74b987696c20c60/LICENSE.md
|
|
91
|
-
*/
|
|
92
|
-
async function encryptKey(alg, enc, key, providedCek, providedParameters = {}) {
|
|
93
|
-
let encryptedKey;
|
|
94
|
-
let parameters = {};
|
|
95
|
-
let cek;
|
|
96
|
-
switch (alg) {
|
|
97
|
-
case "dir": {
|
|
98
|
-
cek = key;
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
case "ECDH-ES":
|
|
102
|
-
case "ECDH-ES+A128KW":
|
|
103
|
-
case "ECDH-ES+A192KW":
|
|
104
|
-
case "ECDH-ES+A256KW": {
|
|
105
|
-
assertCryptoKey(key);
|
|
106
|
-
if (!allowed(key)) {
|
|
107
|
-
throw new Error(
|
|
108
|
-
"ECDH with the provided key is not allowed or not supported by your javascript runtime"
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
const { apu, apv } = providedParameters;
|
|
112
|
-
const ephemeralKey = providedParameters.epk ? await normalizeKey(providedParameters.epk, alg) : (await crypto.subtle.generateKey(
|
|
113
|
-
key.algorithm,
|
|
114
|
-
true,
|
|
115
|
-
["deriveBits"]
|
|
116
|
-
)).privateKey;
|
|
117
|
-
const { x, y, crv, kty } = await keyToJWK(ephemeralKey);
|
|
118
|
-
const sharedSecret = await deriveECDHESKey(
|
|
119
|
-
key,
|
|
120
|
-
ephemeralKey,
|
|
121
|
-
alg === "ECDH-ES" ? enc : alg,
|
|
122
|
-
alg === "ECDH-ES" ? bitLengthCEK(enc) : Number.parseInt(alg.slice(-5, -2), 10),
|
|
123
|
-
apu,
|
|
124
|
-
apv
|
|
125
|
-
);
|
|
126
|
-
parameters = { epk: { x, y, crv, kty } };
|
|
127
|
-
if (apu) parameters.apu = base64UrlEncode(apu);
|
|
128
|
-
if (apv) parameters.apv = base64UrlEncode(apv);
|
|
129
|
-
if (alg === "ECDH-ES") {
|
|
130
|
-
cek = sharedSecret;
|
|
131
|
-
break;
|
|
132
|
-
}
|
|
133
|
-
cek = providedCek || generateCEK(enc);
|
|
134
|
-
const kwAlg = alg.slice(-6);
|
|
135
|
-
encryptedKey = (await wrap(kwAlg, sharedSecret, cek)).encryptedKey;
|
|
136
|
-
break;
|
|
137
|
-
}
|
|
138
|
-
case "RSA-OAEP":
|
|
139
|
-
case "RSA-OAEP-256":
|
|
140
|
-
case "RSA-OAEP-384":
|
|
141
|
-
case "RSA-OAEP-512": {
|
|
142
|
-
cek = providedCek || generateCEK(enc);
|
|
143
|
-
assertCryptoKey(key);
|
|
144
|
-
encryptedKey = await encryptRSAES(alg, key, cek);
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
case "PBES2-HS256+A128KW":
|
|
148
|
-
case "PBES2-HS384+A192KW":
|
|
149
|
-
case "PBES2-HS512+A256KW": {
|
|
150
|
-
cek = providedCek || generateCEK(enc);
|
|
151
|
-
const { p2c, p2s } = providedParameters;
|
|
152
|
-
({ encryptedKey, ...parameters } = await wrap(alg, key, cek, p2c, p2s));
|
|
153
|
-
break;
|
|
154
|
-
}
|
|
155
|
-
case "A128KW":
|
|
156
|
-
case "A192KW":
|
|
157
|
-
case "A256KW": {
|
|
158
|
-
cek = providedCek || generateCEK(enc);
|
|
159
|
-
encryptedKey = (await wrap(alg, key, cek)).encryptedKey;
|
|
160
|
-
break;
|
|
161
|
-
}
|
|
162
|
-
case "A128GCMKW":
|
|
163
|
-
case "A192GCMKW":
|
|
164
|
-
case "A256GCMKW": {
|
|
165
|
-
cek = providedCek || generateCEK(enc);
|
|
166
|
-
const { iv } = providedParameters;
|
|
167
|
-
({ encryptedKey, ...parameters } = await encryptIV(alg, key, cek, iv));
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
default: {
|
|
171
|
-
throw new Error(
|
|
172
|
-
'Invalid or unsupported "alg" (JWE Algorithm) header value'
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
return { cek, encryptedKey, parameters };
|
|
177
|
-
}
|
|
178
|
-
async function normalizeKey(key, alg) {
|
|
179
|
-
if (key instanceof Uint8Array) {
|
|
180
|
-
return key;
|
|
181
|
-
}
|
|
182
|
-
if (isCryptoKey(key)) {
|
|
183
|
-
return key;
|
|
184
|
-
}
|
|
185
|
-
if (isJWK(key)) {
|
|
186
|
-
const safeJwk = sanitizeObject(key);
|
|
187
|
-
if ("k" in safeJwk && safeJwk.k) {
|
|
188
|
-
return base64UrlDecode(safeJwk.k, false);
|
|
189
|
-
}
|
|
190
|
-
return jwkTokey({ ...safeJwk, alg });
|
|
191
|
-
}
|
|
192
|
-
throw new Error("unreachable");
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
async function encrypt(payload, key, options = {}) {
|
|
196
|
-
const {
|
|
197
|
-
protectedHeader: additionalProtectedHeader,
|
|
198
|
-
cek: providedCek,
|
|
199
|
-
contentEncryptionIV: providedContentIV,
|
|
200
|
-
keyManagementIV,
|
|
201
|
-
p2s = randomBytes(16),
|
|
202
|
-
p2c = 2048,
|
|
203
|
-
ecdhPartyUInfo,
|
|
204
|
-
ecdhPartyVInfo
|
|
205
|
-
} = options;
|
|
206
|
-
let { alg, enc } = options;
|
|
207
|
-
if (!alg) {
|
|
208
|
-
if (typeof key === "string" || key instanceof Uint8Array) {
|
|
209
|
-
alg = "PBES2-HS256+A128KW";
|
|
210
|
-
} else if (isJWK(key)) {
|
|
211
|
-
alg = key.alg;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
if (!alg) {
|
|
215
|
-
throw new TypeError(
|
|
216
|
-
'JWE "alg" (Key Management Algorithm) must be provided in options or inferable from the key'
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
if (!enc) {
|
|
220
|
-
enc = isJWK(key) && "enc" in key ? key.enc : "A128GCM";
|
|
221
|
-
}
|
|
222
|
-
const jweKeyManagementParams = {};
|
|
223
|
-
if (keyManagementIV) jweKeyManagementParams.iv = keyManagementIV;
|
|
224
|
-
if (p2s) jweKeyManagementParams.p2s = p2s;
|
|
225
|
-
if (p2c) jweKeyManagementParams.p2c = p2c;
|
|
226
|
-
if (ecdhPartyUInfo) jweKeyManagementParams.apu = ecdhPartyUInfo;
|
|
227
|
-
if (ecdhPartyVInfo) jweKeyManagementParams.apv = ecdhPartyVInfo;
|
|
228
|
-
if (additionalProtectedHeader?.epk instanceof CryptoKey) {
|
|
229
|
-
jweKeyManagementParams.epk = additionalProtectedHeader.epk;
|
|
230
|
-
}
|
|
231
|
-
const wrappingKeyMaterial = await importKey(key, alg);
|
|
232
|
-
const {
|
|
233
|
-
cek: finalCek,
|
|
234
|
-
// This is the CEK (CryptoKey | Uint8Array) to be used for content encryption
|
|
235
|
-
encryptedKey: jweEncryptedKey,
|
|
236
|
-
// This is the JWE Encrypted Key (Uint8Array | undefined)
|
|
237
|
-
parameters: keyManagementHeaderParams
|
|
238
|
-
// JWE header params from key encryption (e.g., epk, p2s, iv, tag)
|
|
239
|
-
} = await encryptKey(
|
|
240
|
-
alg,
|
|
241
|
-
enc,
|
|
242
|
-
wrappingKeyMaterial,
|
|
243
|
-
providedCek,
|
|
244
|
-
jweKeyManagementParams
|
|
245
|
-
);
|
|
246
|
-
const baseProtectedHeader = { ...additionalProtectedHeader };
|
|
247
|
-
delete baseProtectedHeader.alg;
|
|
248
|
-
delete baseProtectedHeader.enc;
|
|
249
|
-
const jweProtectedHeader = sanitizeObject({
|
|
250
|
-
...baseProtectedHeader,
|
|
251
|
-
...keyManagementHeaderParams,
|
|
252
|
-
alg,
|
|
253
|
-
enc
|
|
254
|
-
});
|
|
255
|
-
const protectedHeader = applyTypCtyDefaults(jweProtectedHeader, payload);
|
|
256
|
-
const computedPayload = computeJwtTimeClaims(
|
|
257
|
-
payload,
|
|
258
|
-
protectedHeader.typ,
|
|
259
|
-
options.expiresIn,
|
|
260
|
-
options.currentDate
|
|
261
|
-
);
|
|
262
|
-
const plaintextBytes = getPlaintextBytes(computedPayload || payload);
|
|
263
|
-
const protectedHeaderSerialized = JSON.stringify(protectedHeader);
|
|
264
|
-
const protectedHeaderEncoded = base64UrlEncode(protectedHeaderSerialized);
|
|
265
|
-
const aadBytes = textEncoder.encode(protectedHeaderEncoded);
|
|
266
|
-
const contentIVBytes = providedContentIV ?? generateIV(enc);
|
|
267
|
-
const {
|
|
268
|
-
ciphertext: contentCiphertext,
|
|
269
|
-
tag: contentAuthTag,
|
|
270
|
-
iv: actualContentIV
|
|
271
|
-
} = await encrypt$1(
|
|
272
|
-
enc,
|
|
273
|
-
plaintextBytes,
|
|
274
|
-
finalCek,
|
|
275
|
-
contentIVBytes,
|
|
276
|
-
aadBytes
|
|
277
|
-
);
|
|
278
|
-
if (!actualContentIV) {
|
|
279
|
-
throw new Error("Content encryption IV was not generated or returned.");
|
|
280
|
-
}
|
|
281
|
-
if (!contentAuthTag) {
|
|
282
|
-
throw new Error(
|
|
283
|
-
"Content encryption auth tag was not generated or returned."
|
|
284
|
-
);
|
|
285
|
-
}
|
|
286
|
-
const encryptedKeyEncoded = jweEncryptedKey ? base64UrlEncode(jweEncryptedKey) : "";
|
|
287
|
-
const jweParts = [
|
|
288
|
-
protectedHeaderEncoded,
|
|
289
|
-
encryptedKeyEncoded,
|
|
290
|
-
base64UrlEncode(actualContentIV),
|
|
291
|
-
base64UrlEncode(contentCiphertext),
|
|
292
|
-
base64UrlEncode(contentAuthTag)
|
|
293
|
-
];
|
|
294
|
-
return jweParts.join(".");
|
|
295
|
-
}
|
|
296
|
-
async function decrypt(jwe, key, options = {}) {
|
|
297
|
-
const parts = jwe.split(".");
|
|
298
|
-
if (parts.length !== 5) {
|
|
299
|
-
throw new Error(
|
|
300
|
-
"Invalid JWE: Must contain five sections (RFC7516, section-3)."
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
const [
|
|
304
|
-
protectedHeaderEncoded,
|
|
305
|
-
encryptedKeyEncoded,
|
|
306
|
-
ivEncoded,
|
|
307
|
-
ciphertextEncoded,
|
|
308
|
-
authTagEncoded
|
|
309
|
-
] = parts;
|
|
310
|
-
let protectedHeader;
|
|
311
|
-
try {
|
|
312
|
-
const protectedHeaderJson = base64UrlDecode(protectedHeaderEncoded);
|
|
313
|
-
protectedHeader = sanitizeObject(
|
|
314
|
-
JSON.parse(protectedHeaderJson)
|
|
315
|
-
);
|
|
316
|
-
} catch (error_) {
|
|
317
|
-
throw new Error(
|
|
318
|
-
`Invalid JWE: Protected header is not valid Base64URL or JSON (${error_ instanceof Error ? error_.message : error_})`
|
|
319
|
-
);
|
|
320
|
-
}
|
|
321
|
-
if (!protectedHeader || typeof protectedHeader !== "object" || !protectedHeader.alg || !protectedHeader.enc) {
|
|
322
|
-
throw new Error(
|
|
323
|
-
'Invalid JWE: Protected header must be an object with "alg" and "enc" properties.'
|
|
324
|
-
);
|
|
325
|
-
}
|
|
326
|
-
const alg = protectedHeader.alg;
|
|
327
|
-
const enc = protectedHeader.enc;
|
|
328
|
-
if (options?.algorithms && !options.algorithms.includes(alg)) {
|
|
329
|
-
throw new Error(`Key management algorithm not allowed: ${alg}`);
|
|
330
|
-
}
|
|
331
|
-
if (options?.encryptionAlgorithms && !options.encryptionAlgorithms.includes(enc)) {
|
|
332
|
-
throw new Error(`Content encryption algorithm not allowed: ${enc}`);
|
|
333
|
-
}
|
|
334
|
-
const resolvedKeyMaterial = typeof key === "function" ? await key(protectedHeader, jwe) : key;
|
|
335
|
-
const unwrappingKey = await importKey(resolvedKeyMaterial, alg);
|
|
336
|
-
const encryptedKeyBytes = base64UrlDecode(encryptedKeyEncoded, false);
|
|
337
|
-
const contentIVBytes = base64UrlDecode(ivEncoded, false);
|
|
338
|
-
const contentAuthTagBytes = base64UrlDecode(authTagEncoded, false);
|
|
339
|
-
const ciphertextBytes = base64UrlDecode(ciphertextEncoded, false);
|
|
340
|
-
const unwrapKeyOpts = {
|
|
341
|
-
iv: protectedHeader.iv,
|
|
342
|
-
tag: protectedHeader.tag,
|
|
343
|
-
p2s: protectedHeader.p2s,
|
|
344
|
-
p2c: protectedHeader.p2c,
|
|
345
|
-
epk: protectedHeader.epk,
|
|
346
|
-
apu: protectedHeader.apu,
|
|
347
|
-
apv: protectedHeader.apv,
|
|
348
|
-
unwrappedKeyAlgorithm: options?.unwrappedKeyAlgorithm,
|
|
349
|
-
keyUsage: options?.keyUsage,
|
|
350
|
-
extractable: options?.extractable,
|
|
351
|
-
returnAs: false
|
|
352
|
-
// Returning CEK as Uint8Array<ArrayBuffer>
|
|
353
|
-
};
|
|
354
|
-
const cekBytes = await unwrapKey(
|
|
355
|
-
alg,
|
|
356
|
-
encryptedKeyBytes,
|
|
357
|
-
unwrappingKey,
|
|
358
|
-
unwrapKeyOpts
|
|
359
|
-
);
|
|
360
|
-
const aadBytes = textEncoder.encode(protectedHeaderEncoded);
|
|
361
|
-
const plaintextBytes = await decrypt$1(
|
|
362
|
-
enc,
|
|
363
|
-
cekBytes,
|
|
364
|
-
ciphertextBytes,
|
|
365
|
-
contentIVBytes,
|
|
366
|
-
contentAuthTagBytes,
|
|
367
|
-
aadBytes
|
|
368
|
-
);
|
|
369
|
-
const payload = decodePayloadFromBytes(
|
|
370
|
-
plaintextBytes,
|
|
371
|
-
protectedHeader,
|
|
372
|
-
options?.forceUint8Array
|
|
373
|
-
);
|
|
374
|
-
validateCriticalHeadersJWE(protectedHeader, [
|
|
375
|
-
...options?.critical || [],
|
|
376
|
-
...options?.requiredHeaders || []
|
|
377
|
-
]);
|
|
378
|
-
if (payload && typeof payload === "object" && options.validateJWT !== false && (options.validateJWT === true || protectedHeader.typ?.toLowerCase().includes("jwt")) && !options.forceUint8Array && !(payload instanceof Uint8Array)) {
|
|
379
|
-
validateJwtClaims(payload, options);
|
|
380
|
-
}
|
|
381
|
-
return {
|
|
382
|
-
payload,
|
|
383
|
-
protectedHeader,
|
|
384
|
-
cek: cekBytes,
|
|
385
|
-
aad: aadBytes
|
|
386
|
-
};
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
const jwe = {
|
|
390
|
-
__proto__: null,
|
|
391
|
-
decrypt: decrypt,
|
|
392
|
-
encrypt: encrypt
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
export { decrypt as d, encrypt as e, jwe as j };
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { c as checkSigCryptoKey } from './unjwt.D1MC63Yb.mjs';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* This is a fork of Jose library's utility functions.
|
|
5
|
-
* @source https://github.com/panva/jose/tree/69b7960c67e05be55fa2ec31c74b987696c20c60/src/lib/sign.ts
|
|
6
|
-
* @source https://github.com/panva/jose/tree/69b7960c67e05be55fa2ec31c74b987696c20c60/src/lib/verify.ts
|
|
7
|
-
* @license MIT https://github.com/panva/jose/blob/69b7960c67e05be55fa2ec31c74b987696c20c60/LICENSE.md
|
|
8
|
-
*/
|
|
9
|
-
async function sign(alg, key, data) {
|
|
10
|
-
const cryptoKey = await getSignVerifyKey(alg, key, "sign");
|
|
11
|
-
checkKeyLength(alg, cryptoKey);
|
|
12
|
-
const signature = await crypto.subtle.sign(
|
|
13
|
-
subtleAlgorithm(alg, cryptoKey.algorithm),
|
|
14
|
-
cryptoKey,
|
|
15
|
-
data
|
|
16
|
-
);
|
|
17
|
-
return new Uint8Array(signature);
|
|
18
|
-
}
|
|
19
|
-
async function verify(alg, key, signature, data) {
|
|
20
|
-
const cryptoKey = await getSignVerifyKey(alg, key, "verify");
|
|
21
|
-
checkKeyLength(alg, cryptoKey);
|
|
22
|
-
const algorithm = subtleAlgorithm(alg, cryptoKey.algorithm);
|
|
23
|
-
try {
|
|
24
|
-
return await crypto.subtle.verify(algorithm, cryptoKey, signature, data);
|
|
25
|
-
} catch {
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
function checkKeyLength(alg, key) {
|
|
30
|
-
if (alg.startsWith("RS") || alg.startsWith("PS")) {
|
|
31
|
-
const { modulusLength } = key.algorithm;
|
|
32
|
-
if (typeof modulusLength !== "number" || modulusLength < 2048) {
|
|
33
|
-
throw new TypeError(
|
|
34
|
-
`${alg} requires key modulusLength to be 2048 bits or larger`
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
async function getSignVerifyKey(alg, key, usage) {
|
|
40
|
-
if (key instanceof Uint8Array) {
|
|
41
|
-
if (!alg.startsWith("HS")) {
|
|
42
|
-
throw new TypeError(
|
|
43
|
-
`Key must be ${key} of type: CryptoKey or JSON Web Key`
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
return crypto.subtle.importKey(
|
|
47
|
-
"raw",
|
|
48
|
-
key,
|
|
49
|
-
{ hash: `SHA-${alg.slice(-3)}`, name: "HMAC" },
|
|
50
|
-
false,
|
|
51
|
-
[usage]
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
checkSigCryptoKey(key, alg, usage);
|
|
55
|
-
return key;
|
|
56
|
-
}
|
|
57
|
-
function subtleAlgorithm(alg, algorithm) {
|
|
58
|
-
const hash = `SHA-${alg.slice(-3)}`;
|
|
59
|
-
switch (alg) {
|
|
60
|
-
case "HS256":
|
|
61
|
-
case "HS384":
|
|
62
|
-
case "HS512": {
|
|
63
|
-
return { hash, name: "HMAC" };
|
|
64
|
-
}
|
|
65
|
-
case "PS256":
|
|
66
|
-
case "PS384":
|
|
67
|
-
case "PS512": {
|
|
68
|
-
return {
|
|
69
|
-
hash,
|
|
70
|
-
name: "RSA-PSS",
|
|
71
|
-
saltLength: Number.parseInt(alg.slice(-3), 10) >> 3
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
case "RS256":
|
|
75
|
-
case "RS384":
|
|
76
|
-
case "RS512": {
|
|
77
|
-
return { hash, name: "RSASSA-PKCS1-v1_5" };
|
|
78
|
-
}
|
|
79
|
-
case "ES256":
|
|
80
|
-
case "ES384":
|
|
81
|
-
case "ES512": {
|
|
82
|
-
return {
|
|
83
|
-
hash,
|
|
84
|
-
name: "ECDSA",
|
|
85
|
-
namedCurve: algorithm.namedCurve
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
case "Ed25519":
|
|
89
|
-
// Fall through
|
|
90
|
-
case "EdDSA": {
|
|
91
|
-
return { name: "Ed25519" };
|
|
92
|
-
}
|
|
93
|
-
default: {
|
|
94
|
-
throw new Error(
|
|
95
|
-
`alg ${alg} is not supported either by JOSE or your javascript runtime`
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export { checkKeyLength as c, sign as s, verify as v };
|