unjwt 0.4.0 → 0.5.1
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 +445 -190
- package/dist/index.d.mts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.mjs +5 -5
- package/dist/jwe.d.mts +3 -3
- package/dist/jwe.d.ts +3 -3
- package/dist/jwe.mjs +4 -3
- package/dist/jwk.d.mts +2 -2
- package/dist/jwk.d.ts +2 -2
- package/dist/jwk.mjs +2 -2
- package/dist/jws.d.mts +3 -3
- package/dist/jws.d.ts +3 -3
- package/dist/jws.mjs +4 -4
- package/dist/shared/unjwt.1gbuWbc6.mjs +1507 -0
- package/dist/shared/unjwt.B0XDPf8a.d.ts +189 -0
- package/dist/shared/unjwt.B4fFiy1c.d.mts +52 -0
- package/dist/shared/unjwt.BG_R52ey.d.mts +100 -0
- package/dist/shared/unjwt.BISJ5yt1.d.ts +52 -0
- package/dist/shared/unjwt.BxqF-n1S.mjs +167 -0
- package/dist/shared/unjwt.C9Xhi7hE.mjs +101 -0
- package/dist/shared/unjwt.CVKbKOFy.d.ts +100 -0
- package/dist/shared/unjwt.Cd9nTEWg.d.mts +52 -0
- package/dist/shared/unjwt.Ce72NmIz.mjs +453 -0
- package/dist/shared/unjwt.CxhGjEAX.d.mts +202 -0
- package/dist/shared/unjwt.CxhGjEAX.d.ts +202 -0
- package/dist/shared/unjwt.D1J1DelP.d.mts +198 -0
- package/dist/shared/unjwt.DHdDAH0L.mjs +83 -0
- package/dist/shared/unjwt.DQIYSlHj.d.mts +189 -0
- package/dist/shared/unjwt.DqmNvXMd.d.ts +52 -0
- package/dist/shared/unjwt.WvWb2zNM.d.ts +198 -0
- package/dist/utils.d.mts +2 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.mjs +1 -0
- package/package.json +8 -4
- package/dist/shared/unjwt.4uzJv0cA.d.mts +0 -110
- package/dist/shared/unjwt.A7HKFx3n.d.mts +0 -52
- package/dist/shared/unjwt.BdjvpIUY.d.mts +0 -79
- package/dist/shared/unjwt.BmjmfFWo.d.ts +0 -79
- package/dist/shared/unjwt.C9ELsKeK.mjs +0 -120
- package/dist/shared/unjwt.CVUxLNoy.d.ts +0 -52
- package/dist/shared/unjwt.ChP7VhqI.d.ts +0 -110
- package/dist/shared/unjwt.DD0p18Gd.mjs +0 -81
- package/dist/shared/unjwt.DGsIvgyn.d.ts +0 -106
- package/dist/shared/unjwt.DPTwlVpK.mjs +0 -28
- package/dist/shared/unjwt.Dr-mxtq9.d.mts +0 -106
- package/dist/shared/unjwt.KOd0vXyx.mjs +0 -90
- package/dist/shared/unjwt._GivNhs2.mjs +0 -213
- package/dist/shared/unjwt.zb2wBNCJ.d.mts +0 -82
- package/dist/shared/unjwt.zb2wBNCJ.d.ts +0 -82
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { K as KEY_WRAPPING_ALGORITHMS, C as CONTENT_ENCRYPTION_ALGORITHMS, J as JoseHeaderParameters } from './unjwt.ChP7VhqI.js';
|
|
2
|
-
|
|
3
|
-
type KeyWrappingAlgorithmType = keyof typeof KEY_WRAPPING_ALGORITHMS;
|
|
4
|
-
type ContentEncryptionAlgorithmType = keyof typeof CONTENT_ENCRYPTION_ALGORITHMS;
|
|
5
|
-
interface JWEHeaderParameters extends JoseHeaderParameters {
|
|
6
|
-
/**
|
|
7
|
-
* `alg` (Algorithm): Header Parameter
|
|
8
|
-
*
|
|
9
|
-
* @default "PBES2-HS256+A128KW"
|
|
10
|
-
*/
|
|
11
|
-
alg?: KeyWrappingAlgorithmType;
|
|
12
|
-
/**
|
|
13
|
-
* `enc` (Encryption Algorithm): Header Parameter
|
|
14
|
-
*
|
|
15
|
-
* @default "A256GCM"
|
|
16
|
-
*/
|
|
17
|
-
enc?: ContentEncryptionAlgorithmType;
|
|
18
|
-
/**
|
|
19
|
-
* `p2c` (PBES2 Count): Header Parameter
|
|
20
|
-
*
|
|
21
|
-
* @default 2048
|
|
22
|
-
*/
|
|
23
|
-
p2c?: number;
|
|
24
|
-
/**
|
|
25
|
-
* `p2s` (PBES2 Salt): Header Parameter
|
|
26
|
-
*/
|
|
27
|
-
p2s?: string;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* JWE (JSON Web Encryption) options
|
|
31
|
-
*/
|
|
32
|
-
interface JWEOptions {
|
|
33
|
-
/**
|
|
34
|
-
* Number of iterations for PBES2 key derivation
|
|
35
|
-
* Also accessible as `protectedHeader.p2c`
|
|
36
|
-
*
|
|
37
|
-
* @default 2048
|
|
38
|
-
*/
|
|
39
|
-
iterations?: number;
|
|
40
|
-
/**
|
|
41
|
-
* Size of the salt for PBES2 key derivation
|
|
42
|
-
*
|
|
43
|
-
* @default 16
|
|
44
|
-
*/
|
|
45
|
-
saltSize?: number;
|
|
46
|
-
/**
|
|
47
|
-
* Additional protected header parameters
|
|
48
|
-
*/
|
|
49
|
-
protectedHeader?: JWEHeaderParameters;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** The default settings. */
|
|
53
|
-
declare const JWE_DEFAULTS: Readonly<{
|
|
54
|
-
saltSize: 16;
|
|
55
|
-
iterations: 2048;
|
|
56
|
-
alg: KeyWrappingAlgorithmType;
|
|
57
|
-
enc: ContentEncryptionAlgorithmType;
|
|
58
|
-
}>;
|
|
59
|
-
/**
|
|
60
|
-
* Seal (encrypt) data using JWE with configurable algorithms
|
|
61
|
-
* @param data The data to encrypt
|
|
62
|
-
* @param password The password to use for encryption
|
|
63
|
-
* @param options Optional parameters
|
|
64
|
-
* @returns Promise resolving to the compact JWE token
|
|
65
|
-
*/
|
|
66
|
-
declare function seal(data: string | Uint8Array, password: string | Uint8Array, options?: JWEOptions): Promise<string>;
|
|
67
|
-
/**
|
|
68
|
-
* Decrypts a JWE (JSON Web Encryption) token
|
|
69
|
-
* @param token The JWE token string in compact serialization format
|
|
70
|
-
* @param password The password used to derive the encryption key
|
|
71
|
-
* @returns The decrypted content as a string
|
|
72
|
-
*/
|
|
73
|
-
declare function unseal(token: string, password: string | Uint8Array): Promise<string>;
|
|
74
|
-
/**
|
|
75
|
-
* Decrypts a JWE (JSON Web Encryption) token
|
|
76
|
-
* @param token The JWE token string in compact serialization format
|
|
77
|
-
* @param password The password used to derive the encryption key
|
|
78
|
-
* @param options Decryption options
|
|
79
|
-
* @returns The decrypted content as a string
|
|
80
|
-
*/
|
|
81
|
-
declare function unseal(token: string, password: string | Uint8Array, options: {
|
|
82
|
-
textOutput: true;
|
|
83
|
-
}): Promise<string>;
|
|
84
|
-
/**
|
|
85
|
-
* Decrypts a JWE (JSON Web Encryption) token
|
|
86
|
-
* @param token The JWE token string in compact serialization format
|
|
87
|
-
* @param password The password used to derive the encryption key
|
|
88
|
-
* @param options Decryption options
|
|
89
|
-
* @returns The decrypted content as a Uint8Array
|
|
90
|
-
*/
|
|
91
|
-
declare function unseal(token: string, password: string | Uint8Array, options: {
|
|
92
|
-
textOutput: false;
|
|
93
|
-
}): Promise<Uint8Array>;
|
|
94
|
-
|
|
95
|
-
declare const jwe_JWE_DEFAULTS: typeof JWE_DEFAULTS;
|
|
96
|
-
declare const jwe_seal: typeof seal;
|
|
97
|
-
declare const jwe_unseal: typeof unseal;
|
|
98
|
-
declare namespace jwe {
|
|
99
|
-
export {
|
|
100
|
-
jwe_JWE_DEFAULTS as JWE_DEFAULTS,
|
|
101
|
-
jwe_seal as seal,
|
|
102
|
-
jwe_unseal as unseal,
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export { JWE_DEFAULTS as J, jwe as j, seal as s, unseal as u };
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
const textEncoder = /* @__PURE__ */ new TextEncoder();
|
|
2
|
-
const textDecoder = /* @__PURE__ */ new TextDecoder();
|
|
3
|
-
function base64UrlEncode(data) {
|
|
4
|
-
return btoa(String.fromCodePoint(...data)).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
5
|
-
}
|
|
6
|
-
function base64UrlDecode(str) {
|
|
7
|
-
if (!str) {
|
|
8
|
-
return new Uint8Array(0);
|
|
9
|
-
}
|
|
10
|
-
str = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
11
|
-
while (str.length % 4) str += "=";
|
|
12
|
-
return Uint8Array.from(atob(str), (b) => b.codePointAt(0));
|
|
13
|
-
}
|
|
14
|
-
function randomBytes(length) {
|
|
15
|
-
return crypto.getRandomValues(new Uint8Array(length));
|
|
16
|
-
}
|
|
17
|
-
function concatUint8Arrays(...arrays) {
|
|
18
|
-
const totalLength = arrays.reduce((length, arr) => length + arr.length, 0);
|
|
19
|
-
const result = new Uint8Array(totalLength);
|
|
20
|
-
let offset = 0;
|
|
21
|
-
for (const arr of arrays) {
|
|
22
|
-
result.set(arr, offset);
|
|
23
|
-
offset += arr.length;
|
|
24
|
-
}
|
|
25
|
-
return result;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export { textDecoder as a, base64UrlEncode as b, base64UrlDecode as c, concatUint8Arrays as d, randomBytes as r, textEncoder as t };
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { K as KEY_WRAPPING_ALGORITHMS, C as CONTENT_ENCRYPTION_ALGORITHMS, J as JoseHeaderParameters } from './unjwt.4uzJv0cA.mjs';
|
|
2
|
-
|
|
3
|
-
type KeyWrappingAlgorithmType = keyof typeof KEY_WRAPPING_ALGORITHMS;
|
|
4
|
-
type ContentEncryptionAlgorithmType = keyof typeof CONTENT_ENCRYPTION_ALGORITHMS;
|
|
5
|
-
interface JWEHeaderParameters extends JoseHeaderParameters {
|
|
6
|
-
/**
|
|
7
|
-
* `alg` (Algorithm): Header Parameter
|
|
8
|
-
*
|
|
9
|
-
* @default "PBES2-HS256+A128KW"
|
|
10
|
-
*/
|
|
11
|
-
alg?: KeyWrappingAlgorithmType;
|
|
12
|
-
/**
|
|
13
|
-
* `enc` (Encryption Algorithm): Header Parameter
|
|
14
|
-
*
|
|
15
|
-
* @default "A256GCM"
|
|
16
|
-
*/
|
|
17
|
-
enc?: ContentEncryptionAlgorithmType;
|
|
18
|
-
/**
|
|
19
|
-
* `p2c` (PBES2 Count): Header Parameter
|
|
20
|
-
*
|
|
21
|
-
* @default 2048
|
|
22
|
-
*/
|
|
23
|
-
p2c?: number;
|
|
24
|
-
/**
|
|
25
|
-
* `p2s` (PBES2 Salt): Header Parameter
|
|
26
|
-
*/
|
|
27
|
-
p2s?: string;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* JWE (JSON Web Encryption) options
|
|
31
|
-
*/
|
|
32
|
-
interface JWEOptions {
|
|
33
|
-
/**
|
|
34
|
-
* Number of iterations for PBES2 key derivation
|
|
35
|
-
* Also accessible as `protectedHeader.p2c`
|
|
36
|
-
*
|
|
37
|
-
* @default 2048
|
|
38
|
-
*/
|
|
39
|
-
iterations?: number;
|
|
40
|
-
/**
|
|
41
|
-
* Size of the salt for PBES2 key derivation
|
|
42
|
-
*
|
|
43
|
-
* @default 16
|
|
44
|
-
*/
|
|
45
|
-
saltSize?: number;
|
|
46
|
-
/**
|
|
47
|
-
* Additional protected header parameters
|
|
48
|
-
*/
|
|
49
|
-
protectedHeader?: JWEHeaderParameters;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** The default settings. */
|
|
53
|
-
declare const JWE_DEFAULTS: Readonly<{
|
|
54
|
-
saltSize: 16;
|
|
55
|
-
iterations: 2048;
|
|
56
|
-
alg: KeyWrappingAlgorithmType;
|
|
57
|
-
enc: ContentEncryptionAlgorithmType;
|
|
58
|
-
}>;
|
|
59
|
-
/**
|
|
60
|
-
* Seal (encrypt) data using JWE with configurable algorithms
|
|
61
|
-
* @param data The data to encrypt
|
|
62
|
-
* @param password The password to use for encryption
|
|
63
|
-
* @param options Optional parameters
|
|
64
|
-
* @returns Promise resolving to the compact JWE token
|
|
65
|
-
*/
|
|
66
|
-
declare function seal(data: string | Uint8Array, password: string | Uint8Array, options?: JWEOptions): Promise<string>;
|
|
67
|
-
/**
|
|
68
|
-
* Decrypts a JWE (JSON Web Encryption) token
|
|
69
|
-
* @param token The JWE token string in compact serialization format
|
|
70
|
-
* @param password The password used to derive the encryption key
|
|
71
|
-
* @returns The decrypted content as a string
|
|
72
|
-
*/
|
|
73
|
-
declare function unseal(token: string, password: string | Uint8Array): Promise<string>;
|
|
74
|
-
/**
|
|
75
|
-
* Decrypts a JWE (JSON Web Encryption) token
|
|
76
|
-
* @param token The JWE token string in compact serialization format
|
|
77
|
-
* @param password The password used to derive the encryption key
|
|
78
|
-
* @param options Decryption options
|
|
79
|
-
* @returns The decrypted content as a string
|
|
80
|
-
*/
|
|
81
|
-
declare function unseal(token: string, password: string | Uint8Array, options: {
|
|
82
|
-
textOutput: true;
|
|
83
|
-
}): Promise<string>;
|
|
84
|
-
/**
|
|
85
|
-
* Decrypts a JWE (JSON Web Encryption) token
|
|
86
|
-
* @param token The JWE token string in compact serialization format
|
|
87
|
-
* @param password The password used to derive the encryption key
|
|
88
|
-
* @param options Decryption options
|
|
89
|
-
* @returns The decrypted content as a Uint8Array
|
|
90
|
-
*/
|
|
91
|
-
declare function unseal(token: string, password: string | Uint8Array, options: {
|
|
92
|
-
textOutput: false;
|
|
93
|
-
}): Promise<Uint8Array>;
|
|
94
|
-
|
|
95
|
-
declare const jwe_JWE_DEFAULTS: typeof JWE_DEFAULTS;
|
|
96
|
-
declare const jwe_seal: typeof seal;
|
|
97
|
-
declare const jwe_unseal: typeof unseal;
|
|
98
|
-
declare namespace jwe {
|
|
99
|
-
export {
|
|
100
|
-
jwe_JWE_DEFAULTS as JWE_DEFAULTS,
|
|
101
|
-
jwe_seal as seal,
|
|
102
|
-
jwe_unseal as unseal,
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export { JWE_DEFAULTS as J, jwe as j, seal as s, unseal as u };
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { b as base64UrlEncode, t as textEncoder, a as textDecoder, c as base64UrlDecode } from './unjwt.DPTwlVpK.mjs';
|
|
2
|
-
import { l as lookupAlgorithm, J as JWS_SYMMETRIC_ALGORITHMS } from './unjwt.DD0p18Gd.mjs';
|
|
3
|
-
import { i as importKey } from './unjwt.C9ELsKeK.mjs';
|
|
4
|
-
|
|
5
|
-
const JWS_DEFAULTS = /* @__PURE__ */ Object.freeze({
|
|
6
|
-
alg: "HS256"
|
|
7
|
-
});
|
|
8
|
-
async function sign(payload, secret, options = {}) {
|
|
9
|
-
if (!secret) {
|
|
10
|
-
throw new Error("Missing secret key");
|
|
11
|
-
}
|
|
12
|
-
const protectedHeader = options.protectedHeader || {};
|
|
13
|
-
const alg = protectedHeader.alg || JWS_DEFAULTS.alg;
|
|
14
|
-
const algConfig = validateJwsAlgorithm(alg);
|
|
15
|
-
const header = {
|
|
16
|
-
alg,
|
|
17
|
-
typ: "JWT",
|
|
18
|
-
...protectedHeader
|
|
19
|
-
};
|
|
20
|
-
const encodedHeader = base64UrlEncode(
|
|
21
|
-
textEncoder.encode(JSON.stringify(header))
|
|
22
|
-
);
|
|
23
|
-
const encodedPayload = base64UrlEncode(
|
|
24
|
-
typeof payload === "string" ? textEncoder.encode(payload) : payload
|
|
25
|
-
);
|
|
26
|
-
const signingInput = textEncoder.encode(`${encodedHeader}.${encodedPayload}`);
|
|
27
|
-
const cryptoKey = await importKey(
|
|
28
|
-
secret,
|
|
29
|
-
{ name: "HMAC", hash: algConfig.hash },
|
|
30
|
-
false,
|
|
31
|
-
["sign"]
|
|
32
|
-
);
|
|
33
|
-
const signature = await crypto.subtle.sign(
|
|
34
|
-
{ name: "HMAC" },
|
|
35
|
-
cryptoKey,
|
|
36
|
-
signingInput
|
|
37
|
-
);
|
|
38
|
-
const encodedSignature = base64UrlEncode(new Uint8Array(signature));
|
|
39
|
-
return `${encodedHeader}.${encodedPayload}.${encodedSignature}`;
|
|
40
|
-
}
|
|
41
|
-
async function verify(token, secret, options = {}) {
|
|
42
|
-
if (!token) {
|
|
43
|
-
throw new Error("Missing JWS token");
|
|
44
|
-
}
|
|
45
|
-
if (!secret) {
|
|
46
|
-
throw new Error("Missing secret key");
|
|
47
|
-
}
|
|
48
|
-
const textOutput = options.textOutput !== false;
|
|
49
|
-
const parts = token.split(".");
|
|
50
|
-
if (parts.length !== 3) {
|
|
51
|
-
throw new Error("Invalid JWS token format");
|
|
52
|
-
}
|
|
53
|
-
const [encodedHeader, encodedPayload, encodedSignature] = parts;
|
|
54
|
-
const header = JSON.parse(
|
|
55
|
-
textDecoder.decode(base64UrlDecode(encodedHeader))
|
|
56
|
-
);
|
|
57
|
-
const alg = header.alg;
|
|
58
|
-
const algConfig = validateJwsAlgorithm(alg);
|
|
59
|
-
const signingInput = textEncoder.encode(`${encodedHeader}.${encodedPayload}`);
|
|
60
|
-
const signature = base64UrlDecode(encodedSignature);
|
|
61
|
-
const cryptoKey = await importKey(
|
|
62
|
-
secret,
|
|
63
|
-
{ name: "HMAC", hash: algConfig.hash },
|
|
64
|
-
false,
|
|
65
|
-
["verify"]
|
|
66
|
-
);
|
|
67
|
-
const isValid = await crypto.subtle.verify(
|
|
68
|
-
{ name: "HMAC" },
|
|
69
|
-
cryptoKey,
|
|
70
|
-
signature,
|
|
71
|
-
signingInput
|
|
72
|
-
);
|
|
73
|
-
if (!isValid) {
|
|
74
|
-
throw new Error("Signature verification failed");
|
|
75
|
-
}
|
|
76
|
-
const payloadBytes = base64UrlDecode(encodedPayload);
|
|
77
|
-
return textOutput ? textDecoder.decode(payloadBytes) : payloadBytes;
|
|
78
|
-
}
|
|
79
|
-
function validateJwsAlgorithm(alg) {
|
|
80
|
-
return lookupAlgorithm(alg, JWS_SYMMETRIC_ALGORITHMS, "JWS");
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const jws = {
|
|
84
|
-
__proto__: null,
|
|
85
|
-
JWS_DEFAULTS: JWS_DEFAULTS,
|
|
86
|
-
sign: sign,
|
|
87
|
-
verify: verify
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export { JWS_DEFAULTS as J, jws as j, sign as s, verify as v };
|
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
import { t as textEncoder, r as randomBytes, b as base64UrlEncode, a as textDecoder, c as base64UrlDecode, d as concatUint8Arrays } from './unjwt.DPTwlVpK.mjs';
|
|
2
|
-
import { l as lookupAlgorithm, K as KEY_WRAPPING_ALGORITHMS, C as CONTENT_ENCRYPTION_ALGORITHMS } from './unjwt.DD0p18Gd.mjs';
|
|
3
|
-
|
|
4
|
-
const JWE_DEFAULTS = /* @__PURE__ */ Object.freeze({
|
|
5
|
-
saltSize: 16,
|
|
6
|
-
iterations: 2048,
|
|
7
|
-
alg: "PBES2-HS256+A128KW",
|
|
8
|
-
enc: "A256GCM"
|
|
9
|
-
});
|
|
10
|
-
async function seal(data, password, options = {}) {
|
|
11
|
-
const protectedHeader = options.protectedHeader || {};
|
|
12
|
-
const iterations = protectedHeader.p2c || options.iterations || JWE_DEFAULTS.iterations;
|
|
13
|
-
const saltSize = options.saltSize || JWE_DEFAULTS.saltSize;
|
|
14
|
-
const alg = protectedHeader.alg || JWE_DEFAULTS.alg;
|
|
15
|
-
const enc = protectedHeader.enc || JWE_DEFAULTS.enc;
|
|
16
|
-
validateKeyWrappingAlgorithm(alg);
|
|
17
|
-
const encConfig = validateContentEncryptionAlgorithm(enc);
|
|
18
|
-
const plaintext = typeof data === "string" ? textEncoder.encode(data) : data;
|
|
19
|
-
const saltInput = randomBytes(saltSize);
|
|
20
|
-
const header = {
|
|
21
|
-
alg,
|
|
22
|
-
enc,
|
|
23
|
-
p2s: base64UrlEncode(saltInput),
|
|
24
|
-
p2c: iterations,
|
|
25
|
-
...protectedHeader
|
|
26
|
-
};
|
|
27
|
-
const encodedHeader = base64UrlEncode(
|
|
28
|
-
textEncoder.encode(JSON.stringify(header))
|
|
29
|
-
);
|
|
30
|
-
const derivedKey = await deriveKeyFromPassword(
|
|
31
|
-
password,
|
|
32
|
-
saltInput,
|
|
33
|
-
iterations,
|
|
34
|
-
alg
|
|
35
|
-
);
|
|
36
|
-
const {
|
|
37
|
-
wrappedKey,
|
|
38
|
-
rawCek: _,
|
|
39
|
-
cek
|
|
40
|
-
} = await generateAndWrapCEK(derivedKey, encConfig);
|
|
41
|
-
const iv = randomBytes(encConfig.ivLength);
|
|
42
|
-
let ciphertext;
|
|
43
|
-
let tag;
|
|
44
|
-
if (encConfig.type === "gcm") {
|
|
45
|
-
const result = await encryptGCM(
|
|
46
|
-
plaintext,
|
|
47
|
-
cek,
|
|
48
|
-
iv,
|
|
49
|
-
textEncoder.encode(encodedHeader),
|
|
50
|
-
encConfig
|
|
51
|
-
);
|
|
52
|
-
ciphertext = result.ciphertext;
|
|
53
|
-
tag = result.tag;
|
|
54
|
-
} else {
|
|
55
|
-
throw new Error(`Unsupported encryption type: ${encConfig.type}`);
|
|
56
|
-
}
|
|
57
|
-
return [
|
|
58
|
-
encodedHeader,
|
|
59
|
-
base64UrlEncode(new Uint8Array(wrappedKey)),
|
|
60
|
-
base64UrlEncode(iv),
|
|
61
|
-
base64UrlEncode(ciphertext),
|
|
62
|
-
base64UrlEncode(tag)
|
|
63
|
-
].join(".");
|
|
64
|
-
}
|
|
65
|
-
async function unseal(token, password, options = {}) {
|
|
66
|
-
if (!token) {
|
|
67
|
-
throw new Error("Missing JWE token");
|
|
68
|
-
}
|
|
69
|
-
const textOutput = options.textOutput !== false;
|
|
70
|
-
const [
|
|
71
|
-
encodedHeader,
|
|
72
|
-
encryptedKey,
|
|
73
|
-
encodedIv,
|
|
74
|
-
encodedCiphertext,
|
|
75
|
-
encodedTag
|
|
76
|
-
] = token.split(".");
|
|
77
|
-
const header = JSON.parse(textDecoder.decode(base64UrlDecode(encodedHeader)));
|
|
78
|
-
const alg = header.alg;
|
|
79
|
-
const enc = header.enc;
|
|
80
|
-
validateKeyWrappingAlgorithm(alg);
|
|
81
|
-
const encConfig = validateContentEncryptionAlgorithm(enc);
|
|
82
|
-
const iterations = header.p2c;
|
|
83
|
-
const saltInput = base64UrlDecode(header.p2s);
|
|
84
|
-
const derivedKey = await deriveKeyFromPassword(
|
|
85
|
-
password,
|
|
86
|
-
saltInput,
|
|
87
|
-
iterations,
|
|
88
|
-
alg
|
|
89
|
-
);
|
|
90
|
-
const wrappedKey = base64UrlDecode(encryptedKey);
|
|
91
|
-
const iv = base64UrlDecode(encodedIv);
|
|
92
|
-
const ciphertext = base64UrlDecode(encodedCiphertext);
|
|
93
|
-
const tag = base64UrlDecode(encodedTag);
|
|
94
|
-
const cek = await unwrapCEK(wrappedKey, derivedKey, encConfig);
|
|
95
|
-
let decrypted;
|
|
96
|
-
if (encConfig.type === "gcm") {
|
|
97
|
-
decrypted = await decryptGCM(
|
|
98
|
-
ciphertext,
|
|
99
|
-
tag,
|
|
100
|
-
cek,
|
|
101
|
-
iv,
|
|
102
|
-
textEncoder.encode(encodedHeader)
|
|
103
|
-
);
|
|
104
|
-
} else {
|
|
105
|
-
throw new Error(`Unsupported encryption type: ${encConfig.type}`);
|
|
106
|
-
}
|
|
107
|
-
return textOutput ? textDecoder.decode(decrypted) : decrypted;
|
|
108
|
-
}
|
|
109
|
-
async function deriveKeyFromPassword(password, saltInput, iterations, alg) {
|
|
110
|
-
if (!password) {
|
|
111
|
-
throw new Error("Missing password");
|
|
112
|
-
}
|
|
113
|
-
const algConfig = validateKeyWrappingAlgorithm(alg);
|
|
114
|
-
const baseKey = await crypto.subtle.importKey(
|
|
115
|
-
"raw",
|
|
116
|
-
typeof password === "string" ? textEncoder.encode(password) : password,
|
|
117
|
-
{ name: "PBKDF2" },
|
|
118
|
-
false,
|
|
119
|
-
["deriveKey"]
|
|
120
|
-
);
|
|
121
|
-
const salt = concatUint8Arrays(
|
|
122
|
-
textEncoder.encode(alg),
|
|
123
|
-
new Uint8Array([0]),
|
|
124
|
-
saltInput
|
|
125
|
-
);
|
|
126
|
-
return crypto.subtle.deriveKey(
|
|
127
|
-
{
|
|
128
|
-
name: "PBKDF2",
|
|
129
|
-
hash: algConfig.hash,
|
|
130
|
-
salt,
|
|
131
|
-
iterations
|
|
132
|
-
},
|
|
133
|
-
baseKey,
|
|
134
|
-
{ name: "AES-KW", length: algConfig.keyLength },
|
|
135
|
-
false,
|
|
136
|
-
["wrapKey", "unwrapKey"]
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
function validateKeyWrappingAlgorithm(alg) {
|
|
140
|
-
return lookupAlgorithm(alg, KEY_WRAPPING_ALGORITHMS, "key wrapping");
|
|
141
|
-
}
|
|
142
|
-
function validateContentEncryptionAlgorithm(enc) {
|
|
143
|
-
return lookupAlgorithm(
|
|
144
|
-
enc,
|
|
145
|
-
CONTENT_ENCRYPTION_ALGORITHMS,
|
|
146
|
-
"content encryption"
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
async function generateAndWrapCEK(derivedKey, encConfig) {
|
|
150
|
-
if (encConfig.type === "gcm") {
|
|
151
|
-
const cek = await crypto.subtle.generateKey(
|
|
152
|
-
{ name: "AES-GCM", length: encConfig.keyLength },
|
|
153
|
-
true,
|
|
154
|
-
["encrypt", "decrypt", "wrapKey", "unwrapKey"]
|
|
155
|
-
);
|
|
156
|
-
const wrappedKey = await crypto.subtle.wrapKey("raw", cek, derivedKey, {
|
|
157
|
-
name: "AES-KW"
|
|
158
|
-
});
|
|
159
|
-
return { wrappedKey, rawCek: null, cek };
|
|
160
|
-
}
|
|
161
|
-
throw new Error(`Unsupported encryption type: ${encConfig.type}`);
|
|
162
|
-
}
|
|
163
|
-
async function unwrapCEK(wrappedKey, derivedKey, encConfig) {
|
|
164
|
-
if (encConfig.type === "gcm") {
|
|
165
|
-
return crypto.subtle.unwrapKey(
|
|
166
|
-
"raw",
|
|
167
|
-
wrappedKey,
|
|
168
|
-
derivedKey,
|
|
169
|
-
{ name: "AES-KW" },
|
|
170
|
-
{ name: "AES-GCM", length: encConfig.keyLength },
|
|
171
|
-
false,
|
|
172
|
-
["decrypt"]
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
throw new Error(`Unsupported encryption type: ${encConfig.type}`);
|
|
176
|
-
}
|
|
177
|
-
async function encryptGCM(plaintext, cek, iv, aad, encConfig) {
|
|
178
|
-
const ciphertext = await crypto.subtle.encrypt(
|
|
179
|
-
{
|
|
180
|
-
name: "AES-GCM",
|
|
181
|
-
iv,
|
|
182
|
-
additionalData: aad
|
|
183
|
-
},
|
|
184
|
-
cek,
|
|
185
|
-
plaintext
|
|
186
|
-
);
|
|
187
|
-
const encrypted = new Uint8Array(ciphertext);
|
|
188
|
-
const tag = encrypted.slice(-encConfig.tagLength);
|
|
189
|
-
const ciphertextOutput = encrypted.slice(0, -encConfig.tagLength);
|
|
190
|
-
return { ciphertext: ciphertextOutput, tag };
|
|
191
|
-
}
|
|
192
|
-
async function decryptGCM(ciphertext, tag, cek, iv, aad) {
|
|
193
|
-
const encryptedData = concatUint8Arrays(ciphertext, tag);
|
|
194
|
-
const decrypted = await crypto.subtle.decrypt(
|
|
195
|
-
{
|
|
196
|
-
name: "AES-GCM",
|
|
197
|
-
iv,
|
|
198
|
-
additionalData: aad
|
|
199
|
-
},
|
|
200
|
-
cek,
|
|
201
|
-
encryptedData
|
|
202
|
-
);
|
|
203
|
-
return new Uint8Array(decrypted);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
const jwe = {
|
|
207
|
-
__proto__: null,
|
|
208
|
-
JWE_DEFAULTS: JWE_DEFAULTS,
|
|
209
|
-
seal: seal,
|
|
210
|
-
unseal: unseal
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
export { JWE_DEFAULTS as J, jwe as j, seal as s, unseal as u };
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
Based on https://github.com/panva/jose/tree/v6.0.10
|
|
3
|
-
Copyright (c) 2018 Filip Skokan.
|
|
4
|
-
https://github.com/panva/jose/blob/v6.0.10/LICENSE.md
|
|
5
|
-
*/
|
|
6
|
-
/** Generic JSON Web Key Parameters. */
|
|
7
|
-
interface JWKParameters {
|
|
8
|
-
/** JWK "kty" (Key Type) Parameter */
|
|
9
|
-
kty?: string;
|
|
10
|
-
/**
|
|
11
|
-
* JWK "alg" (Algorithm) Parameter
|
|
12
|
-
*
|
|
13
|
-
* @see {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements}
|
|
14
|
-
*/
|
|
15
|
-
alg?: string;
|
|
16
|
-
/** JWK "key_ops" (Key Operations) Parameter */
|
|
17
|
-
key_ops?: string[];
|
|
18
|
-
/** JWK "ext" (Extractable) Parameter */
|
|
19
|
-
ext?: boolean;
|
|
20
|
-
/** JWK "use" (Public Key Use) Parameter */
|
|
21
|
-
use?: string;
|
|
22
|
-
/** JWK "x5c" (X.509 Certificate Chain) Parameter */
|
|
23
|
-
x5c?: string[];
|
|
24
|
-
/** JWK "x5t" (X.509 Certificate SHA-1 Thumbprint) Parameter */
|
|
25
|
-
x5t?: string;
|
|
26
|
-
/** JWK "x5t#S256" (X.509 Certificate SHA-256 Thumbprint) Parameter */
|
|
27
|
-
"x5t#S256"?: string;
|
|
28
|
-
/** JWK "x5u" (X.509 URL) Parameter */
|
|
29
|
-
x5u?: string;
|
|
30
|
-
/** JWK "kid" (Key ID) Parameter */
|
|
31
|
-
kid?: string;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* JSON Web Key ({@link https://www.rfc-editor.org/rfc/rfc7517 JWK}). "RSA", "EC", "OKP", and "oct"
|
|
35
|
-
* key types are supported.
|
|
36
|
-
*
|
|
37
|
-
* @see {@link JWK_OKP_Public}
|
|
38
|
-
* @see {@link JWK_OKP_Private}
|
|
39
|
-
* @see {@link JWK_EC_Public}
|
|
40
|
-
* @see {@link JWK_EC_Private}
|
|
41
|
-
* @see {@link JWK_RSA_Public}
|
|
42
|
-
* @see {@link JWK_RSA_Private}
|
|
43
|
-
* @see {@link JWK_oct}
|
|
44
|
-
*/
|
|
45
|
-
interface JWK extends JWKParameters {
|
|
46
|
-
/**
|
|
47
|
-
* - EC JWK "crv" (Curve) Parameter
|
|
48
|
-
* - OKP JWK "crv" (The Subtype of Key Pair) Parameter
|
|
49
|
-
*/
|
|
50
|
-
crv?: string;
|
|
51
|
-
/**
|
|
52
|
-
* - Private RSA JWK "d" (Private Exponent) Parameter
|
|
53
|
-
* - Private EC JWK "d" (ECC Private Key) Parameter
|
|
54
|
-
* - Private OKP JWK "d" (The Private Key) Parameter
|
|
55
|
-
*/
|
|
56
|
-
d?: string;
|
|
57
|
-
/** Private RSA JWK "dp" (First Factor CRT Exponent) Parameter */
|
|
58
|
-
dp?: string;
|
|
59
|
-
/** Private RSA JWK "dq" (Second Factor CRT Exponent) Parameter */
|
|
60
|
-
dq?: string;
|
|
61
|
-
/** RSA JWK "e" (Exponent) Parameter */
|
|
62
|
-
e?: string;
|
|
63
|
-
/** Oct JWK "k" (Key Value) Parameter */
|
|
64
|
-
k?: string;
|
|
65
|
-
/** RSA JWK "n" (Modulus) Parameter */
|
|
66
|
-
n?: string;
|
|
67
|
-
/** Private RSA JWK "p" (First Prime Factor) Parameter */
|
|
68
|
-
p?: string;
|
|
69
|
-
/** Private RSA JWK "q" (Second Prime Factor) Parameter */
|
|
70
|
-
q?: string;
|
|
71
|
-
/** Private RSA JWK "qi" (First CRT Coefficient) Parameter */
|
|
72
|
-
qi?: string;
|
|
73
|
-
/**
|
|
74
|
-
* - EC JWK "x" (X Coordinate) Parameter
|
|
75
|
-
* - OKP JWK "x" (The public key) Parameter
|
|
76
|
-
*/
|
|
77
|
-
x?: string;
|
|
78
|
-
/** EC JWK "y" (Y Coordinate) Parameter */
|
|
79
|
-
y?: string;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export type { JWK as J };
|