jose 3.20.1 → 3.20.4
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/browser/jwe/flattened/decrypt.js +7 -11
- package/dist/browser/jwe/flattened/encrypt.js +2 -5
- package/dist/browser/jws/flattened/sign.js +1 -2
- package/dist/browser/jws/flattened/verify.js +2 -4
- package/dist/browser/lib/cek.js +3 -2
- package/dist/browser/lib/decrypt_key_management.js +4 -1
- package/dist/browser/lib/encrypt_key_management.js +1 -3
- package/dist/browser/lib/iv.js +3 -2
- package/dist/browser/runtime/aesgcmkw.js +1 -3
- package/dist/browser/runtime/random.js +1 -2
- package/dist/browser/util/errors.js +66 -34
- package/dist/node/cjs/jwe/compact/decrypt.js +1 -1
- package/dist/node/cjs/jwe/flattened/decrypt.js +18 -22
- package/dist/node/cjs/jwe/flattened/encrypt.js +13 -16
- package/dist/node/cjs/jwe/general/decrypt.js +2 -2
- package/dist/node/cjs/jwk/embedded.js +2 -2
- package/dist/node/cjs/jwk/from_key_like.js +1 -1
- package/dist/node/cjs/jwk/parse.js +1 -1
- package/dist/node/cjs/jwk/thumbprint.js +2 -2
- package/dist/node/cjs/jwks/remote.js +3 -3
- package/dist/node/cjs/jws/compact/verify.js +1 -1
- package/dist/node/cjs/jws/flattened/sign.js +8 -9
- package/dist/node/cjs/jws/flattened/verify.js +11 -13
- package/dist/node/cjs/jws/general/verify.js +2 -2
- package/dist/node/cjs/jwt/decrypt.js +2 -2
- package/dist/node/cjs/jwt/produce.js +4 -4
- package/dist/node/cjs/jwt/unsecured.js +1 -1
- package/dist/node/cjs/jwt/verify.js +2 -2
- package/dist/node/cjs/key/export.js +3 -3
- package/dist/node/cjs/key/import.js +10 -10
- package/dist/node/cjs/lib/cek.js +4 -3
- package/dist/node/cjs/lib/check_key_type.js +4 -4
- package/dist/node/cjs/lib/decrypt_key_management.js +16 -13
- package/dist/node/cjs/lib/encrypt_key_management.js +14 -16
- package/dist/node/cjs/lib/iv.js +4 -3
- package/dist/node/cjs/lib/jwt_claims_set.js +4 -4
- package/dist/node/cjs/runtime/aesgcmkw.js +4 -6
- package/dist/node/cjs/runtime/aeskw.js +11 -11
- package/dist/node/cjs/runtime/asn1.js +6 -6
- package/dist/node/cjs/runtime/cbc_tag.js +2 -2
- package/dist/node/cjs/runtime/check_cek_length.js +1 -1
- package/dist/node/cjs/runtime/ciphers.js +1 -1
- package/dist/node/cjs/runtime/decrypt.js +15 -15
- package/dist/node/cjs/runtime/digest.js +1 -1
- package/dist/node/cjs/runtime/ecdhes.js +18 -18
- package/dist/node/cjs/runtime/encrypt.js +14 -14
- package/dist/node/cjs/runtime/fetch_jwks.js +2 -2
- package/dist/node/cjs/runtime/generate.js +4 -4
- package/dist/node/cjs/runtime/get_named_curve.js +5 -5
- package/dist/node/cjs/runtime/get_sign_verify_key.js +5 -5
- package/dist/node/cjs/runtime/invalid_key_input.js +2 -2
- package/dist/node/cjs/runtime/is_key_like.js +1 -1
- package/dist/node/cjs/runtime/jwk_to_key.js +11 -11
- package/dist/node/cjs/runtime/key_to_jwk.js +27 -27
- package/dist/node/cjs/runtime/node_key.js +4 -4
- package/dist/node/cjs/runtime/pbes2kw.js +13 -13
- package/dist/node/cjs/runtime/random.js +3 -2
- package/dist/node/cjs/runtime/rsaes.js +7 -7
- package/dist/node/cjs/runtime/secret_key.js +1 -1
- package/dist/node/cjs/runtime/sign.js +4 -4
- package/dist/node/cjs/runtime/verify.js +5 -5
- package/dist/node/cjs/runtime/zlib.js +2 -2
- package/dist/node/cjs/util/decode_protected_header.js +2 -2
- package/dist/node/cjs/util/errors.js +69 -37
- package/dist/node/cjs/util/generate_key_pair.js +1 -1
- package/dist/node/cjs/util/generate_secret.js +1 -1
- package/dist/node/esm/jwe/flattened/decrypt.js +7 -11
- package/dist/node/esm/jwe/flattened/encrypt.js +2 -5
- package/dist/node/esm/jws/flattened/sign.js +1 -2
- package/dist/node/esm/jws/flattened/verify.js +2 -4
- package/dist/node/esm/lib/cek.js +3 -2
- package/dist/node/esm/lib/decrypt_key_management.js +4 -1
- package/dist/node/esm/lib/encrypt_key_management.js +1 -3
- package/dist/node/esm/lib/iv.js +3 -2
- package/dist/node/esm/runtime/aesgcmkw.js +1 -3
- package/dist/node/esm/runtime/random.js +1 -2
- package/dist/node/esm/util/errors.js +66 -34
- package/dist/types/types.d.ts +1 -0
- package/dist/types/util/errors.d.ts +21 -18
- package/package.json +2 -1
|
@@ -9,10 +9,10 @@ async function EmbeddedJWK(protectedHeader, token) {
|
|
|
9
9
|
...protectedHeader,
|
|
10
10
|
...token.header,
|
|
11
11
|
};
|
|
12
|
-
if (!
|
|
12
|
+
if (!is_object_js_1.default(joseHeader.jwk)) {
|
|
13
13
|
throw new errors_js_1.JWSInvalid('"jwk" (JSON Web Key) Header Parameter must be a JSON object');
|
|
14
14
|
}
|
|
15
|
-
const key = await
|
|
15
|
+
const key = await import_js_1.importJWK({ ...joseHeader.jwk, ext: true }, joseHeader.alg, true);
|
|
16
16
|
if (key instanceof Uint8Array || key.type !== 'public') {
|
|
17
17
|
throw new errors_js_1.JWSInvalid('"jwk" (JSON Web Key) Header Parameter must be a public key');
|
|
18
18
|
}
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.fromKeyLike = void 0;
|
|
4
4
|
const export_js_1 = require("../key/export.js");
|
|
5
5
|
async function fromKeyLike(key) {
|
|
6
|
-
return
|
|
6
|
+
return export_js_1.exportJWK(key);
|
|
7
7
|
}
|
|
8
8
|
exports.fromKeyLike = fromKeyLike;
|
|
9
9
|
exports.default = fromKeyLike;
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.parseJwk = void 0;
|
|
4
4
|
const import_js_1 = require("../key/import.js");
|
|
5
5
|
async function parseJwk(jwk, alg, octAsKeyObject) {
|
|
6
|
-
return
|
|
6
|
+
return import_js_1.importJWK(jwk, alg, octAsKeyObject);
|
|
7
7
|
}
|
|
8
8
|
exports.parseJwk = parseJwk;
|
|
9
9
|
exports.default = parseJwk;
|
|
@@ -12,7 +12,7 @@ const check = (value, description) => {
|
|
|
12
12
|
}
|
|
13
13
|
};
|
|
14
14
|
async function calculateThumbprint(jwk, digestAlgorithm = 'sha256') {
|
|
15
|
-
if (!
|
|
15
|
+
if (!is_object_js_1.default(jwk)) {
|
|
16
16
|
throw new TypeError('JWK must be an object');
|
|
17
17
|
}
|
|
18
18
|
let components;
|
|
@@ -41,7 +41,7 @@ async function calculateThumbprint(jwk, digestAlgorithm = 'sha256') {
|
|
|
41
41
|
throw new errors_js_1.JOSENotSupported('"kty" (Key Type) Parameter missing or unsupported');
|
|
42
42
|
}
|
|
43
43
|
const data = buffer_utils_js_1.encoder.encode(JSON.stringify(components));
|
|
44
|
-
return
|
|
44
|
+
return base64url_js_1.encode(await digest_js_1.default(digestAlgorithm, data));
|
|
45
45
|
}
|
|
46
46
|
exports.calculateThumbprint = calculateThumbprint;
|
|
47
47
|
exports.default = calculateThumbprint;
|
|
@@ -19,7 +19,7 @@ function getKtyFromAlg(alg) {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
function isJWKLike(key) {
|
|
22
|
-
return
|
|
22
|
+
return is_object_js_1.default(key);
|
|
23
23
|
}
|
|
24
24
|
class RemoteJWKSet {
|
|
25
25
|
constructor(url, options) {
|
|
@@ -93,7 +93,7 @@ class RemoteJWKSet {
|
|
|
93
93
|
}
|
|
94
94
|
const cached = this._cached.get(jwk) || this._cached.set(jwk, {}).get(jwk);
|
|
95
95
|
if (cached[protectedHeader.alg] === undefined) {
|
|
96
|
-
const keyObject = await
|
|
96
|
+
const keyObject = await import_js_1.importJWK({ ...jwk, ext: true }, protectedHeader.alg);
|
|
97
97
|
if (keyObject instanceof Uint8Array || keyObject.type !== 'public') {
|
|
98
98
|
throw new errors_js_1.JWKSInvalid('JSON Web Key Set members must be public keys');
|
|
99
99
|
}
|
|
@@ -103,7 +103,7 @@ class RemoteJWKSet {
|
|
|
103
103
|
}
|
|
104
104
|
async reload() {
|
|
105
105
|
if (!this._pendingFetch) {
|
|
106
|
-
this._pendingFetch =
|
|
106
|
+
this._pendingFetch = fetch_jwks_js_1.default(this._url, this._timeoutDuration, this._options)
|
|
107
107
|
.then((json) => {
|
|
108
108
|
if (typeof json !== 'object' ||
|
|
109
109
|
!json ||
|
|
@@ -15,7 +15,7 @@ async function compactVerify(jws, key, options) {
|
|
|
15
15
|
if (length !== 3) {
|
|
16
16
|
throw new errors_js_1.JWSInvalid('Invalid Compact JWS');
|
|
17
17
|
}
|
|
18
|
-
const verified = await
|
|
18
|
+
const verified = await verify_js_1.default({
|
|
19
19
|
payload: (payload || undefined),
|
|
20
20
|
protected: protectedHeader || undefined,
|
|
21
21
|
signature: (signature || undefined),
|
|
@@ -8,7 +8,6 @@ const errors_js_1 = require("../../util/errors.js");
|
|
|
8
8
|
const buffer_utils_js_1 = require("../../lib/buffer_utils.js");
|
|
9
9
|
const check_key_type_js_1 = require("../../lib/check_key_type.js");
|
|
10
10
|
const validate_crit_js_1 = require("../../lib/validate_crit.js");
|
|
11
|
-
const checkExtensions = validate_crit_js_1.default.bind(undefined, errors_js_1.JWSInvalid, new Map([['b64', true]]));
|
|
12
11
|
class FlattenedSign {
|
|
13
12
|
constructor(payload) {
|
|
14
13
|
if (!(payload instanceof Uint8Array)) {
|
|
@@ -34,14 +33,14 @@ class FlattenedSign {
|
|
|
34
33
|
if (!this._protectedHeader && !this._unprotectedHeader) {
|
|
35
34
|
throw new errors_js_1.JWSInvalid('either setProtectedHeader or setUnprotectedHeader must be called before #sign()');
|
|
36
35
|
}
|
|
37
|
-
if (!
|
|
36
|
+
if (!is_disjoint_js_1.default(this._protectedHeader, this._unprotectedHeader)) {
|
|
38
37
|
throw new errors_js_1.JWSInvalid('JWS Protected and JWS Unprotected Header Parameter names must be disjoint');
|
|
39
38
|
}
|
|
40
39
|
const joseHeader = {
|
|
41
40
|
...this._protectedHeader,
|
|
42
41
|
...this._unprotectedHeader,
|
|
43
42
|
};
|
|
44
|
-
const extensions =
|
|
43
|
+
const extensions = validate_crit_js_1.default(errors_js_1.JWSInvalid, new Map([['b64', true]]), options === null || options === void 0 ? void 0 : options.crit, this._protectedHeader, joseHeader);
|
|
45
44
|
let b64 = true;
|
|
46
45
|
if (extensions.has('b64')) {
|
|
47
46
|
b64 = this._protectedHeader.b64;
|
|
@@ -53,22 +52,22 @@ class FlattenedSign {
|
|
|
53
52
|
if (typeof alg !== 'string' || !alg) {
|
|
54
53
|
throw new errors_js_1.JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
|
55
54
|
}
|
|
56
|
-
|
|
55
|
+
check_key_type_js_1.default(alg, key, 'sign');
|
|
57
56
|
let payload = this._payload;
|
|
58
57
|
if (b64) {
|
|
59
|
-
payload = buffer_utils_js_1.encoder.encode(
|
|
58
|
+
payload = buffer_utils_js_1.encoder.encode(base64url_js_1.encode(payload));
|
|
60
59
|
}
|
|
61
60
|
let protectedHeader;
|
|
62
61
|
if (this._protectedHeader) {
|
|
63
|
-
protectedHeader = buffer_utils_js_1.encoder.encode(
|
|
62
|
+
protectedHeader = buffer_utils_js_1.encoder.encode(base64url_js_1.encode(JSON.stringify(this._protectedHeader)));
|
|
64
63
|
}
|
|
65
64
|
else {
|
|
66
65
|
protectedHeader = buffer_utils_js_1.encoder.encode('');
|
|
67
66
|
}
|
|
68
|
-
const data =
|
|
69
|
-
const signature = await
|
|
67
|
+
const data = buffer_utils_js_1.concat(protectedHeader, buffer_utils_js_1.encoder.encode('.'), payload);
|
|
68
|
+
const signature = await sign_js_1.default(alg, key, data);
|
|
70
69
|
const jws = {
|
|
71
|
-
signature:
|
|
70
|
+
signature: base64url_js_1.encode(signature),
|
|
72
71
|
payload: '',
|
|
73
72
|
};
|
|
74
73
|
if (b64) {
|
|
@@ -10,11 +10,9 @@ const is_object_js_1 = require("../../lib/is_object.js");
|
|
|
10
10
|
const check_key_type_js_1 = require("../../lib/check_key_type.js");
|
|
11
11
|
const validate_crit_js_1 = require("../../lib/validate_crit.js");
|
|
12
12
|
const validate_algorithms_js_1 = require("../../lib/validate_algorithms.js");
|
|
13
|
-
const checkExtensions = validate_crit_js_1.default.bind(undefined, errors_js_1.JWSInvalid, new Map([['b64', true]]));
|
|
14
|
-
const checkAlgOption = validate_algorithms_js_1.default.bind(undefined, 'algorithms');
|
|
15
13
|
async function flattenedVerify(jws, key, options) {
|
|
16
14
|
var _a;
|
|
17
|
-
if (!
|
|
15
|
+
if (!is_object_js_1.default(jws)) {
|
|
18
16
|
throw new errors_js_1.JWSInvalid('Flattened JWS must be an object');
|
|
19
17
|
}
|
|
20
18
|
if (jws.protected === undefined && jws.header === undefined) {
|
|
@@ -29,12 +27,12 @@ async function flattenedVerify(jws, key, options) {
|
|
|
29
27
|
if (typeof jws.signature !== 'string') {
|
|
30
28
|
throw new errors_js_1.JWSInvalid('JWS Signature missing or incorrect type');
|
|
31
29
|
}
|
|
32
|
-
if (jws.header !== undefined && !
|
|
30
|
+
if (jws.header !== undefined && !is_object_js_1.default(jws.header)) {
|
|
33
31
|
throw new errors_js_1.JWSInvalid('JWS Unprotected Header incorrect type');
|
|
34
32
|
}
|
|
35
33
|
let parsedProt = {};
|
|
36
34
|
if (jws.protected) {
|
|
37
|
-
const protectedHeader =
|
|
35
|
+
const protectedHeader = base64url_js_1.decode(jws.protected);
|
|
38
36
|
try {
|
|
39
37
|
parsedProt = JSON.parse(buffer_utils_js_1.decoder.decode(protectedHeader));
|
|
40
38
|
}
|
|
@@ -42,14 +40,14 @@ async function flattenedVerify(jws, key, options) {
|
|
|
42
40
|
throw new errors_js_1.JWSInvalid('JWS Protected Header is invalid');
|
|
43
41
|
}
|
|
44
42
|
}
|
|
45
|
-
if (!
|
|
43
|
+
if (!is_disjoint_js_1.default(parsedProt, jws.header)) {
|
|
46
44
|
throw new errors_js_1.JWSInvalid('JWS Protected and JWS Unprotected Header Parameter names must be disjoint');
|
|
47
45
|
}
|
|
48
46
|
const joseHeader = {
|
|
49
47
|
...parsedProt,
|
|
50
48
|
...jws.header,
|
|
51
49
|
};
|
|
52
|
-
const extensions =
|
|
50
|
+
const extensions = validate_crit_js_1.default(errors_js_1.JWSInvalid, new Map([['b64', true]]), options === null || options === void 0 ? void 0 : options.crit, parsedProt, joseHeader);
|
|
53
51
|
let b64 = true;
|
|
54
52
|
if (extensions.has('b64')) {
|
|
55
53
|
b64 = parsedProt.b64;
|
|
@@ -61,7 +59,7 @@ async function flattenedVerify(jws, key, options) {
|
|
|
61
59
|
if (typeof alg !== 'string' || !alg) {
|
|
62
60
|
throw new errors_js_1.JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
|
63
61
|
}
|
|
64
|
-
const algorithms = options &&
|
|
62
|
+
const algorithms = options && validate_algorithms_js_1.default('algorithms', options.algorithms);
|
|
65
63
|
if (algorithms && !algorithms.has(alg)) {
|
|
66
64
|
throw new errors_js_1.JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter not allowed');
|
|
67
65
|
}
|
|
@@ -78,16 +76,16 @@ async function flattenedVerify(jws, key, options) {
|
|
|
78
76
|
key = await key(parsedProt, jws);
|
|
79
77
|
resolvedKey = true;
|
|
80
78
|
}
|
|
81
|
-
|
|
82
|
-
const data =
|
|
83
|
-
const signature =
|
|
84
|
-
const verified = await
|
|
79
|
+
check_key_type_js_1.default(alg, key, 'verify');
|
|
80
|
+
const data = buffer_utils_js_1.concat(buffer_utils_js_1.encoder.encode((_a = jws.protected) !== null && _a !== void 0 ? _a : ''), buffer_utils_js_1.encoder.encode('.'), typeof jws.payload === 'string' ? buffer_utils_js_1.encoder.encode(jws.payload) : jws.payload);
|
|
81
|
+
const signature = base64url_js_1.decode(jws.signature);
|
|
82
|
+
const verified = await verify_js_1.default(alg, key, signature, data);
|
|
85
83
|
if (!verified) {
|
|
86
84
|
throw new errors_js_1.JWSSignatureVerificationFailed();
|
|
87
85
|
}
|
|
88
86
|
let payload;
|
|
89
87
|
if (b64) {
|
|
90
|
-
payload =
|
|
88
|
+
payload = base64url_js_1.decode(jws.payload);
|
|
91
89
|
}
|
|
92
90
|
else if (typeof jws.payload === 'string') {
|
|
93
91
|
payload = buffer_utils_js_1.encoder.encode(jws.payload);
|
|
@@ -5,7 +5,7 @@ const verify_js_1 = require("../flattened/verify.js");
|
|
|
5
5
|
const errors_js_1 = require("../../util/errors.js");
|
|
6
6
|
const is_object_js_1 = require("../../lib/is_object.js");
|
|
7
7
|
async function generalVerify(jws, key, options) {
|
|
8
|
-
if (!
|
|
8
|
+
if (!is_object_js_1.default(jws)) {
|
|
9
9
|
throw new errors_js_1.JWSInvalid('General JWS must be an object');
|
|
10
10
|
}
|
|
11
11
|
if (!Array.isArray(jws.signatures) || !jws.signatures.every(is_object_js_1.default)) {
|
|
@@ -13,7 +13,7 @@ async function generalVerify(jws, key, options) {
|
|
|
13
13
|
}
|
|
14
14
|
for (const signature of jws.signatures) {
|
|
15
15
|
try {
|
|
16
|
-
return await
|
|
16
|
+
return await verify_js_1.default({
|
|
17
17
|
header: signature.header,
|
|
18
18
|
payload: jws.payload,
|
|
19
19
|
protected: signature.protected,
|
|
@@ -5,8 +5,8 @@ const decrypt_js_1 = require("../jwe/compact/decrypt.js");
|
|
|
5
5
|
const jwt_claims_set_js_1 = require("../lib/jwt_claims_set.js");
|
|
6
6
|
const errors_js_1 = require("../util/errors.js");
|
|
7
7
|
async function jwtDecrypt(jwt, key, options) {
|
|
8
|
-
const decrypted = await
|
|
9
|
-
const payload =
|
|
8
|
+
const decrypted = await decrypt_js_1.default(jwt, key, options);
|
|
9
|
+
const payload = jwt_claims_set_js_1.default(decrypted.protectedHeader, decrypted.plaintext, options);
|
|
10
10
|
const { protectedHeader } = decrypted;
|
|
11
11
|
if (protectedHeader.iss !== undefined && protectedHeader.iss !== payload.iss) {
|
|
12
12
|
throw new errors_js_1.JWTClaimValidationFailed('replicated "iss" claim header parameter mismatch', 'iss', 'mismatch');
|
|
@@ -6,7 +6,7 @@ const is_object_js_1 = require("../lib/is_object.js");
|
|
|
6
6
|
const secs_js_1 = require("../lib/secs.js");
|
|
7
7
|
class ProduceJWT {
|
|
8
8
|
constructor(payload) {
|
|
9
|
-
if (!
|
|
9
|
+
if (!is_object_js_1.default(payload)) {
|
|
10
10
|
throw new TypeError('JWT Claims Set MUST be an object');
|
|
11
11
|
}
|
|
12
12
|
this._payload = payload;
|
|
@@ -32,7 +32,7 @@ class ProduceJWT {
|
|
|
32
32
|
this._payload = { ...this._payload, nbf: input };
|
|
33
33
|
}
|
|
34
34
|
else {
|
|
35
|
-
this._payload = { ...this._payload, nbf:
|
|
35
|
+
this._payload = { ...this._payload, nbf: epoch_js_1.default(new Date()) + secs_js_1.default(input) };
|
|
36
36
|
}
|
|
37
37
|
return this;
|
|
38
38
|
}
|
|
@@ -41,13 +41,13 @@ class ProduceJWT {
|
|
|
41
41
|
this._payload = { ...this._payload, exp: input };
|
|
42
42
|
}
|
|
43
43
|
else {
|
|
44
|
-
this._payload = { ...this._payload, exp:
|
|
44
|
+
this._payload = { ...this._payload, exp: epoch_js_1.default(new Date()) + secs_js_1.default(input) };
|
|
45
45
|
}
|
|
46
46
|
return this;
|
|
47
47
|
}
|
|
48
48
|
setIssuedAt(input) {
|
|
49
49
|
if (typeof input === 'undefined') {
|
|
50
|
-
this._payload = { ...this._payload, iat:
|
|
50
|
+
this._payload = { ...this._payload, iat: epoch_js_1.default(new Date()) };
|
|
51
51
|
}
|
|
52
52
|
else {
|
|
53
53
|
this._payload = { ...this._payload, iat: input };
|
|
@@ -29,7 +29,7 @@ class UnsecuredJWT extends produce_js_1.ProduceJWT {
|
|
|
29
29
|
catch {
|
|
30
30
|
throw new errors_js_1.JWTInvalid('Invalid Unsecured JWT');
|
|
31
31
|
}
|
|
32
|
-
const payload =
|
|
32
|
+
const payload = jwt_claims_set_js_1.default(header, base64url.decode(encodedPayload), options);
|
|
33
33
|
return { payload, header };
|
|
34
34
|
}
|
|
35
35
|
}
|
|
@@ -6,11 +6,11 @@ const jwt_claims_set_js_1 = require("../lib/jwt_claims_set.js");
|
|
|
6
6
|
const errors_js_1 = require("../util/errors.js");
|
|
7
7
|
async function jwtVerify(jwt, key, options) {
|
|
8
8
|
var _a;
|
|
9
|
-
const verified = await
|
|
9
|
+
const verified = await verify_js_1.default(jwt, key, options);
|
|
10
10
|
if (((_a = verified.protectedHeader.crit) === null || _a === void 0 ? void 0 : _a.includes('b64')) && verified.protectedHeader.b64 === false) {
|
|
11
11
|
throw new errors_js_1.JWTInvalid('JWTs MUST NOT use unencoded payload');
|
|
12
12
|
}
|
|
13
|
-
const payload =
|
|
13
|
+
const payload = jwt_claims_set_js_1.default(verified.protectedHeader, verified.payload, options);
|
|
14
14
|
const result = { payload, protectedHeader: verified.protectedHeader };
|
|
15
15
|
if (typeof key === 'function') {
|
|
16
16
|
return { ...result, key: verified.key };
|
|
@@ -5,14 +5,14 @@ const asn1_js_1 = require("../runtime/asn1.js");
|
|
|
5
5
|
const asn1_js_2 = require("../runtime/asn1.js");
|
|
6
6
|
const key_to_jwk_js_1 = require("../runtime/key_to_jwk.js");
|
|
7
7
|
async function exportSPKI(key) {
|
|
8
|
-
return
|
|
8
|
+
return asn1_js_1.toSPKI(key);
|
|
9
9
|
}
|
|
10
10
|
exports.exportSPKI = exportSPKI;
|
|
11
11
|
async function exportPKCS8(key) {
|
|
12
|
-
return
|
|
12
|
+
return asn1_js_2.toPKCS8(key);
|
|
13
13
|
}
|
|
14
14
|
exports.exportPKCS8 = exportPKCS8;
|
|
15
15
|
async function exportJWK(key) {
|
|
16
|
-
return
|
|
16
|
+
return key_to_jwk_js_1.default(key);
|
|
17
17
|
}
|
|
18
18
|
exports.exportJWK = exportJWK;
|
|
@@ -65,18 +65,18 @@ function parseElement(bytes) {
|
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
67
|
function spkiFromX509(buf) {
|
|
68
|
-
return
|
|
68
|
+
return base64url_js_1.encodeBase64(getElement(getElement(parseElement(buf).contents)[0].contents)[6].raw);
|
|
69
69
|
}
|
|
70
70
|
function getSPKI(x509) {
|
|
71
71
|
const pem = x509.replace(/(?:-----(?:BEGIN|END) CERTIFICATE-----|\s)/g, '');
|
|
72
|
-
const raw =
|
|
73
|
-
return
|
|
72
|
+
const raw = base64url_js_1.decodeBase64(pem);
|
|
73
|
+
return format_pem_js_1.default(spkiFromX509(raw), 'PUBLIC KEY');
|
|
74
74
|
}
|
|
75
75
|
async function importSPKI(spki, alg, options) {
|
|
76
76
|
if (typeof spki !== 'string' || spki.indexOf('-----BEGIN PUBLIC KEY-----') !== 0) {
|
|
77
77
|
throw new TypeError('"spki" must be SPKI formatted string');
|
|
78
78
|
}
|
|
79
|
-
return
|
|
79
|
+
return asn1_js_1.fromSPKI(spki, alg, options);
|
|
80
80
|
}
|
|
81
81
|
exports.importSPKI = importSPKI;
|
|
82
82
|
async function importX509(x509, alg, options) {
|
|
@@ -84,18 +84,18 @@ async function importX509(x509, alg, options) {
|
|
|
84
84
|
throw new TypeError('"x509" must be X.509 formatted string');
|
|
85
85
|
}
|
|
86
86
|
const spki = getSPKI(x509);
|
|
87
|
-
return
|
|
87
|
+
return asn1_js_1.fromSPKI(spki, alg, options);
|
|
88
88
|
}
|
|
89
89
|
exports.importX509 = importX509;
|
|
90
90
|
async function importPKCS8(pkcs8, alg, options) {
|
|
91
91
|
if (typeof pkcs8 !== 'string' || pkcs8.indexOf('-----BEGIN PRIVATE KEY-----') !== 0) {
|
|
92
92
|
throw new TypeError('"pkcs8" must be PCKS8 formatted string');
|
|
93
93
|
}
|
|
94
|
-
return
|
|
94
|
+
return asn1_js_2.fromPKCS8(pkcs8, alg, options);
|
|
95
95
|
}
|
|
96
96
|
exports.importPKCS8 = importPKCS8;
|
|
97
97
|
async function importJWK(jwk, alg, octAsKeyObject) {
|
|
98
|
-
if (!
|
|
98
|
+
if (!is_object_js_1.default(jwk)) {
|
|
99
99
|
throw new TypeError('JWK must be an object');
|
|
100
100
|
}
|
|
101
101
|
alg || (alg = jwk.alg);
|
|
@@ -109,16 +109,16 @@ async function importJWK(jwk, alg, octAsKeyObject) {
|
|
|
109
109
|
}
|
|
110
110
|
octAsKeyObject !== null && octAsKeyObject !== void 0 ? octAsKeyObject : (octAsKeyObject = jwk.ext !== true);
|
|
111
111
|
if (octAsKeyObject) {
|
|
112
|
-
return
|
|
112
|
+
return jwk_to_key_js_1.default({ ...jwk, alg, ext: false });
|
|
113
113
|
}
|
|
114
|
-
return
|
|
114
|
+
return base64url_js_1.decode(jwk.k);
|
|
115
115
|
case 'RSA':
|
|
116
116
|
if (jwk.oth !== undefined) {
|
|
117
117
|
throw new errors_js_1.JOSENotSupported('RSA JWK "oth" (Other Primes Info) Parameter value is not supported');
|
|
118
118
|
}
|
|
119
119
|
case 'EC':
|
|
120
120
|
case 'OKP':
|
|
121
|
-
return
|
|
121
|
+
return jwk_to_key_js_1.default({ ...jwk, alg });
|
|
122
122
|
default:
|
|
123
123
|
throw new errors_js_1.JOSENotSupported('Unsupported "kty" (Key Type) Parameter value');
|
|
124
124
|
}
|
package/dist/node/cjs/lib/cek.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.bitLengths = void 0;
|
|
4
4
|
const errors_js_1 = require("../util/errors.js");
|
|
5
|
+
const random_js_1 = require("../runtime/random.js");
|
|
5
6
|
const bitLengths = new Map([
|
|
6
7
|
['A128CBC-HS256', 256],
|
|
7
8
|
['A128GCM', 128],
|
|
@@ -11,11 +12,11 @@ const bitLengths = new Map([
|
|
|
11
12
|
['A256GCM', 256],
|
|
12
13
|
]);
|
|
13
14
|
exports.bitLengths = bitLengths;
|
|
14
|
-
const
|
|
15
|
+
const generateCek = (alg) => {
|
|
15
16
|
const bitLength = bitLengths.get(alg);
|
|
16
17
|
if (!bitLength) {
|
|
17
18
|
throw new errors_js_1.JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
|
|
18
19
|
}
|
|
19
|
-
return
|
|
20
|
+
return random_js_1.default(new Uint8Array(bitLength >> 3));
|
|
20
21
|
};
|
|
21
|
-
exports.default =
|
|
22
|
+
exports.default = generateCek;
|
|
@@ -5,16 +5,16 @@ const is_key_like_js_1 = require("../runtime/is_key_like.js");
|
|
|
5
5
|
const symmetricTypeCheck = (key) => {
|
|
6
6
|
if (key instanceof Uint8Array)
|
|
7
7
|
return;
|
|
8
|
-
if (!
|
|
9
|
-
throw new TypeError(
|
|
8
|
+
if (!is_key_like_js_1.default(key)) {
|
|
9
|
+
throw new TypeError(invalid_key_input_js_1.default(key, ...is_key_like_js_1.types, 'Uint8Array'));
|
|
10
10
|
}
|
|
11
11
|
if (key.type !== 'secret') {
|
|
12
12
|
throw new TypeError(`${is_key_like_js_1.types.join(' or ')} instances for symmetric algorithms must be of type "secret"`);
|
|
13
13
|
}
|
|
14
14
|
};
|
|
15
15
|
const asymmetricTypeCheck = (key, usage) => {
|
|
16
|
-
if (!
|
|
17
|
-
throw new TypeError(
|
|
16
|
+
if (!is_key_like_js_1.default(key)) {
|
|
17
|
+
throw new TypeError(invalid_key_input_js_1.default(key, ...is_key_like_js_1.types));
|
|
18
18
|
}
|
|
19
19
|
if (key.type === 'secret') {
|
|
20
20
|
throw new TypeError(`${is_key_like_js_1.types.join(' or ')} instances for asymmetric algorithms must not be of type "secret"`);
|
|
@@ -11,8 +11,8 @@ const cek_js_1 = require("../lib/cek.js");
|
|
|
11
11
|
const import_js_1 = require("../key/import.js");
|
|
12
12
|
const check_key_type_js_1 = require("./check_key_type.js");
|
|
13
13
|
const is_object_js_1 = require("./is_object.js");
|
|
14
|
-
async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
|
|
15
|
-
|
|
14
|
+
async function decryptKeyManagement(alg, key, encryptedKey, joseHeader, options) {
|
|
15
|
+
check_key_type_js_1.default(alg, key, 'decrypt');
|
|
16
16
|
switch (alg) {
|
|
17
17
|
case 'dir': {
|
|
18
18
|
if (encryptedKey !== undefined)
|
|
@@ -25,29 +25,29 @@ async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
|
|
|
25
25
|
case 'ECDH-ES+A128KW':
|
|
26
26
|
case 'ECDH-ES+A192KW':
|
|
27
27
|
case 'ECDH-ES+A256KW': {
|
|
28
|
-
if (!
|
|
28
|
+
if (!is_object_js_1.default(joseHeader.epk))
|
|
29
29
|
throw new errors_js_1.JWEInvalid(`JOSE Header "epk" (Ephemeral Public Key) missing or invalid`);
|
|
30
30
|
if (!ECDH.ecdhAllowed(key))
|
|
31
31
|
throw new errors_js_1.JOSENotSupported('ECDH-ES with the provided key is not allowed or not supported by your javascript runtime');
|
|
32
|
-
const epk = await
|
|
32
|
+
const epk = await import_js_1.importJWK(joseHeader.epk, alg);
|
|
33
33
|
let partyUInfo;
|
|
34
34
|
let partyVInfo;
|
|
35
35
|
if (joseHeader.apu !== undefined) {
|
|
36
36
|
if (typeof joseHeader.apu !== 'string')
|
|
37
37
|
throw new errors_js_1.JWEInvalid(`JOSE Header "apu" (Agreement PartyUInfo) invalid`);
|
|
38
|
-
partyUInfo =
|
|
38
|
+
partyUInfo = base64url_js_1.decode(joseHeader.apu);
|
|
39
39
|
}
|
|
40
40
|
if (joseHeader.apv !== undefined) {
|
|
41
41
|
if (typeof joseHeader.apv !== 'string')
|
|
42
42
|
throw new errors_js_1.JWEInvalid(`JOSE Header "apv" (Agreement PartyVInfo) invalid`);
|
|
43
|
-
partyVInfo =
|
|
43
|
+
partyVInfo = base64url_js_1.decode(joseHeader.apv);
|
|
44
44
|
}
|
|
45
45
|
const sharedSecret = await ECDH.deriveKey(epk, key, alg === 'ECDH-ES' ? joseHeader.enc : alg, parseInt(alg.substr(-5, 3), 10) || cek_js_1.bitLengths.get(joseHeader.enc), partyUInfo, partyVInfo);
|
|
46
46
|
if (alg === 'ECDH-ES')
|
|
47
47
|
return sharedSecret;
|
|
48
48
|
if (encryptedKey === undefined)
|
|
49
49
|
throw new errors_js_1.JWEInvalid('JWE Encrypted Key missing');
|
|
50
|
-
return
|
|
50
|
+
return aeskw_js_1.unwrap(alg.substr(-6), sharedSecret, encryptedKey);
|
|
51
51
|
}
|
|
52
52
|
case 'RSA1_5':
|
|
53
53
|
case 'RSA-OAEP':
|
|
@@ -56,7 +56,7 @@ async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
|
|
|
56
56
|
case 'RSA-OAEP-512': {
|
|
57
57
|
if (encryptedKey === undefined)
|
|
58
58
|
throw new errors_js_1.JWEInvalid('JWE Encrypted Key missing');
|
|
59
|
-
return
|
|
59
|
+
return rsaes_js_1.decrypt(alg, key, encryptedKey);
|
|
60
60
|
}
|
|
61
61
|
case 'PBES2-HS256+A128KW':
|
|
62
62
|
case 'PBES2-HS384+A192KW':
|
|
@@ -65,16 +65,19 @@ async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
|
|
|
65
65
|
throw new errors_js_1.JWEInvalid('JWE Encrypted Key missing');
|
|
66
66
|
if (typeof joseHeader.p2c !== 'number')
|
|
67
67
|
throw new errors_js_1.JWEInvalid(`JOSE Header "p2c" (PBES2 Count) missing or invalid`);
|
|
68
|
+
const p2cLimit = (options === null || options === void 0 ? void 0 : options.maxPBES2Count) || 10000;
|
|
69
|
+
if (joseHeader.p2c > p2cLimit)
|
|
70
|
+
throw new errors_js_1.JWEInvalid(`JOSE Header "p2c" (PBES2 Count) out is of acceptable bounds`);
|
|
68
71
|
if (typeof joseHeader.p2s !== 'string')
|
|
69
72
|
throw new errors_js_1.JWEInvalid(`JOSE Header "p2s" (PBES2 Salt) missing or invalid`);
|
|
70
|
-
return
|
|
73
|
+
return pbes2kw_js_1.decrypt(alg, key, encryptedKey, joseHeader.p2c, base64url_js_1.decode(joseHeader.p2s));
|
|
71
74
|
}
|
|
72
75
|
case 'A128KW':
|
|
73
76
|
case 'A192KW':
|
|
74
77
|
case 'A256KW': {
|
|
75
78
|
if (encryptedKey === undefined)
|
|
76
79
|
throw new errors_js_1.JWEInvalid('JWE Encrypted Key missing');
|
|
77
|
-
return
|
|
80
|
+
return aeskw_js_1.unwrap(alg, key, encryptedKey);
|
|
78
81
|
}
|
|
79
82
|
case 'A128GCMKW':
|
|
80
83
|
case 'A192GCMKW':
|
|
@@ -85,9 +88,9 @@ async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
|
|
|
85
88
|
throw new errors_js_1.JWEInvalid(`JOSE Header "iv" (Initialization Vector) missing or invalid`);
|
|
86
89
|
if (typeof joseHeader.tag !== 'string')
|
|
87
90
|
throw new errors_js_1.JWEInvalid(`JOSE Header "tag" (Authentication Tag) missing or invalid`);
|
|
88
|
-
const iv =
|
|
89
|
-
const tag =
|
|
90
|
-
return
|
|
91
|
+
const iv = base64url_js_1.decode(joseHeader.iv);
|
|
92
|
+
const tag = base64url_js_1.decode(joseHeader.tag);
|
|
93
|
+
return aesgcmkw_js_1.unwrap(alg, key, encryptedKey, iv, tag);
|
|
91
94
|
}
|
|
92
95
|
default: {
|
|
93
96
|
throw new errors_js_1.JOSENotSupported('Invalid or unsupported "alg" (JWE Algorithm) header value');
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const random_js_1 = require("../runtime/random.js");
|
|
4
3
|
const aeskw_js_1 = require("../runtime/aeskw.js");
|
|
5
4
|
const ECDH = require("../runtime/ecdhes.js");
|
|
6
5
|
const pbes2kw_js_1 = require("../runtime/pbes2kw.js");
|
|
@@ -11,12 +10,11 @@ const cek_js_1 = require("../lib/cek.js");
|
|
|
11
10
|
const errors_js_1 = require("../util/errors.js");
|
|
12
11
|
const export_js_1 = require("../key/export.js");
|
|
13
12
|
const check_key_type_js_1 = require("./check_key_type.js");
|
|
14
|
-
const generateCek = (0, cek_js_1.default)(random_js_1.default);
|
|
15
13
|
async function encryptKeyManagement(alg, enc, key, providedCek, providedParameters = {}) {
|
|
16
14
|
let encryptedKey;
|
|
17
15
|
let parameters;
|
|
18
16
|
let cek;
|
|
19
|
-
|
|
17
|
+
check_key_type_js_1.default(alg, key, 'encrypt');
|
|
20
18
|
switch (alg) {
|
|
21
19
|
case 'dir': {
|
|
22
20
|
cek = key;
|
|
@@ -32,20 +30,20 @@ async function encryptKeyManagement(alg, enc, key, providedCek, providedParamete
|
|
|
32
30
|
const { apu, apv } = providedParameters;
|
|
33
31
|
let { epk: ephemeralKey } = providedParameters;
|
|
34
32
|
ephemeralKey || (ephemeralKey = await ECDH.generateEpk(key));
|
|
35
|
-
const { x, y, crv, kty } = await
|
|
33
|
+
const { x, y, crv, kty } = await export_js_1.exportJWK(ephemeralKey);
|
|
36
34
|
const sharedSecret = await ECDH.deriveKey(key, ephemeralKey, alg === 'ECDH-ES' ? enc : alg, parseInt(alg.substr(-5, 3), 10) || cek_js_1.bitLengths.get(enc), apu, apv);
|
|
37
35
|
parameters = { epk: { x, y, crv, kty } };
|
|
38
36
|
if (apu)
|
|
39
|
-
parameters.apu =
|
|
37
|
+
parameters.apu = base64url_js_1.encode(apu);
|
|
40
38
|
if (apv)
|
|
41
|
-
parameters.apv =
|
|
39
|
+
parameters.apv = base64url_js_1.encode(apv);
|
|
42
40
|
if (alg === 'ECDH-ES') {
|
|
43
41
|
cek = sharedSecret;
|
|
44
42
|
break;
|
|
45
43
|
}
|
|
46
|
-
cek = providedCek ||
|
|
44
|
+
cek = providedCek || cek_js_1.default(enc);
|
|
47
45
|
const kwAlg = alg.substr(-6);
|
|
48
|
-
encryptedKey = await
|
|
46
|
+
encryptedKey = await aeskw_js_1.wrap(kwAlg, sharedSecret, cek);
|
|
49
47
|
break;
|
|
50
48
|
}
|
|
51
49
|
case 'RSA1_5':
|
|
@@ -53,31 +51,31 @@ async function encryptKeyManagement(alg, enc, key, providedCek, providedParamete
|
|
|
53
51
|
case 'RSA-OAEP-256':
|
|
54
52
|
case 'RSA-OAEP-384':
|
|
55
53
|
case 'RSA-OAEP-512': {
|
|
56
|
-
cek = providedCek ||
|
|
57
|
-
encryptedKey = await
|
|
54
|
+
cek = providedCek || cek_js_1.default(enc);
|
|
55
|
+
encryptedKey = await rsaes_js_1.encrypt(alg, key, cek);
|
|
58
56
|
break;
|
|
59
57
|
}
|
|
60
58
|
case 'PBES2-HS256+A128KW':
|
|
61
59
|
case 'PBES2-HS384+A192KW':
|
|
62
60
|
case 'PBES2-HS512+A256KW': {
|
|
63
|
-
cek = providedCek ||
|
|
61
|
+
cek = providedCek || cek_js_1.default(enc);
|
|
64
62
|
const { p2c, p2s } = providedParameters;
|
|
65
|
-
({ encryptedKey, ...parameters } = await
|
|
63
|
+
({ encryptedKey, ...parameters } = await pbes2kw_js_1.encrypt(alg, key, cek, p2c, p2s));
|
|
66
64
|
break;
|
|
67
65
|
}
|
|
68
66
|
case 'A128KW':
|
|
69
67
|
case 'A192KW':
|
|
70
68
|
case 'A256KW': {
|
|
71
|
-
cek = providedCek ||
|
|
72
|
-
encryptedKey = await
|
|
69
|
+
cek = providedCek || cek_js_1.default(enc);
|
|
70
|
+
encryptedKey = await aeskw_js_1.wrap(alg, key, cek);
|
|
73
71
|
break;
|
|
74
72
|
}
|
|
75
73
|
case 'A128GCMKW':
|
|
76
74
|
case 'A192GCMKW':
|
|
77
75
|
case 'A256GCMKW': {
|
|
78
|
-
cek = providedCek ||
|
|
76
|
+
cek = providedCek || cek_js_1.default(enc);
|
|
79
77
|
const { iv } = providedParameters;
|
|
80
|
-
({ encryptedKey, ...parameters } = await
|
|
78
|
+
({ encryptedKey, ...parameters } = await aesgcmkw_js_1.wrap(alg, key, cek, iv));
|
|
81
79
|
break;
|
|
82
80
|
}
|
|
83
81
|
default: {
|
package/dist/node/cjs/lib/iv.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.bitLengths = void 0;
|
|
4
4
|
const errors_js_1 = require("../util/errors.js");
|
|
5
|
+
const random_js_1 = require("../runtime/random.js");
|
|
5
6
|
const bitLengths = new Map([
|
|
6
7
|
['A128CBC-HS256', 128],
|
|
7
8
|
['A128GCM', 96],
|
|
@@ -14,11 +15,11 @@ const bitLengths = new Map([
|
|
|
14
15
|
['A256GCMKW', 96],
|
|
15
16
|
]);
|
|
16
17
|
exports.bitLengths = bitLengths;
|
|
17
|
-
const
|
|
18
|
+
const generateIv = (alg) => {
|
|
18
19
|
const bitLength = bitLengths.get(alg);
|
|
19
20
|
if (!bitLength) {
|
|
20
21
|
throw new errors_js_1.JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
|
|
21
22
|
}
|
|
22
|
-
return
|
|
23
|
+
return random_js_1.default(new Uint8Array(bitLength >> 3));
|
|
23
24
|
};
|
|
24
|
-
exports.default =
|
|
25
|
+
exports.default = generateIv;
|