jose 4.9.1 → 4.9.3

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.
@@ -95,10 +95,10 @@ export async function flattenedDecrypt(jwe, key, options) {
95
95
  }
96
96
  let cek;
97
97
  try {
98
- cek = await decryptKeyManagement(alg, key, encryptedKey, joseHeader);
98
+ cek = await decryptKeyManagement(alg, key, encryptedKey, joseHeader, options);
99
99
  }
100
100
  catch (err) {
101
- if (err instanceof TypeError) {
101
+ if (err instanceof TypeError || err instanceof JWEInvalid || err instanceof JOSENotSupported) {
102
102
  throw err;
103
103
  }
104
104
  cek = generateCek(enc);
@@ -1,18 +1,18 @@
1
- import invalidKeyInput from './invalid_key_input.js';
1
+ import { withAlg as invalidKeyInput } from './invalid_key_input.js';
2
2
  import isKeyLike, { types } from '../runtime/is_key_like.js';
3
- const symmetricTypeCheck = (key) => {
3
+ const symmetricTypeCheck = (alg, key) => {
4
4
  if (key instanceof Uint8Array)
5
5
  return;
6
6
  if (!isKeyLike(key)) {
7
- throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'));
7
+ throw new TypeError(invalidKeyInput(alg, key, ...types, 'Uint8Array'));
8
8
  }
9
9
  if (key.type !== 'secret') {
10
10
  throw new TypeError(`${types.join(' or ')} instances for symmetric algorithms must be of type "secret"`);
11
11
  }
12
12
  };
13
- const asymmetricTypeCheck = (key, usage) => {
13
+ const asymmetricTypeCheck = (alg, key, usage) => {
14
14
  if (!isKeyLike(key)) {
15
- throw new TypeError(invalidKeyInput(key, ...types));
15
+ throw new TypeError(invalidKeyInput(alg, key, ...types));
16
16
  }
17
17
  if (key.type === 'secret') {
18
18
  throw new TypeError(`${types.join(' or ')} instances for asymmetric algorithms must not be of type "secret"`);
@@ -36,10 +36,10 @@ const checkKeyType = (alg, key, usage) => {
36
36
  alg.startsWith('PBES2') ||
37
37
  /^A\d{3}(?:GCM)?KW$/.test(alg);
38
38
  if (symmetric) {
39
- symmetricTypeCheck(key);
39
+ symmetricTypeCheck(alg, key);
40
40
  }
41
41
  else {
42
- asymmetricTypeCheck(key, usage);
42
+ asymmetricTypeCheck(alg, key, usage);
43
43
  }
44
44
  };
45
45
  export default checkKeyType;
@@ -9,7 +9,7 @@ import { importJWK } from '../key/import.js';
9
9
  import checkKeyType from './check_key_type.js';
10
10
  import isObject from './is_object.js';
11
11
  import { unwrap as aesGcmKw } from './aesgcmkw.js';
12
- async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
12
+ async function decryptKeyManagement(alg, key, encryptedKey, joseHeader, options) {
13
13
  checkKeyType(alg, key, 'decrypt');
14
14
  switch (alg) {
15
15
  case 'dir': {
@@ -63,6 +63,9 @@ async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
63
63
  throw new JWEInvalid('JWE Encrypted Key missing');
64
64
  if (typeof joseHeader.p2c !== 'number')
65
65
  throw new JWEInvalid(`JOSE Header "p2c" (PBES2 Count) missing or invalid`);
66
+ const p2cLimit = (options === null || options === void 0 ? void 0 : options.maxPBES2Count) || 10000;
67
+ if (joseHeader.p2c > p2cLimit)
68
+ throw new JWEInvalid(`JOSE Header "p2c" (PBES2 Count) out is of acceptable bounds`);
66
69
  if (typeof joseHeader.p2s !== 'string')
67
70
  throw new JWEInvalid(`JOSE Header "p2s" (PBES2 Salt) missing or invalid`);
68
71
  return pbes2Kw(alg, key, encryptedKey, joseHeader.p2c, base64url(joseHeader.p2s));
@@ -1,5 +1,4 @@
1
- export default (actual, ...types) => {
2
- let msg = 'Key must be ';
1
+ function message(msg, actual, ...types) {
3
2
  if (types.length > 2) {
4
3
  const last = types.pop();
5
4
  msg += `one of type ${types.join(', ')}, or ${last}.`;
@@ -22,4 +21,10 @@ export default (actual, ...types) => {
22
21
  }
23
22
  }
24
23
  return msg;
24
+ }
25
+ export default (actual, ...types) => {
26
+ return message('Key must be ', actual, ...types);
25
27
  };
28
+ export function withAlg(alg, actual, ...types) {
29
+ return message(`Key for the ${alg} algorithm must be `, actual, ...types);
30
+ }
@@ -1,7 +1,8 @@
1
1
  import { JWEInvalid } from '../util/errors.js';
2
2
  const checkCekLength = (cek, expected) => {
3
- if (cek.length << 3 !== expected) {
4
- throw new JWEInvalid('Invalid Content Encryption Key length');
3
+ const actual = cek.byteLength << 3;
4
+ if (actual !== expected) {
5
+ throw new JWEInvalid(`Invalid Content Encryption Key length. Expected ${expected} bits, got ${actual} bits`);
5
6
  }
6
7
  };
7
8
  export default checkCekLength;
@@ -98,10 +98,10 @@ async function flattenedDecrypt(jwe, key, options) {
98
98
  }
99
99
  let cek;
100
100
  try {
101
- cek = await (0, decrypt_key_management_js_1.default)(alg, key, encryptedKey, joseHeader);
101
+ cek = await (0, decrypt_key_management_js_1.default)(alg, key, encryptedKey, joseHeader, options);
102
102
  }
103
103
  catch (err) {
104
- if (err instanceof TypeError) {
104
+ if (err instanceof TypeError || err instanceof errors_js_1.JWEInvalid || err instanceof errors_js_1.JOSENotSupported) {
105
105
  throw err;
106
106
  }
107
107
  cek = (0, cek_js_1.default)(enc);
@@ -2,19 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const invalid_key_input_js_1 = require("./invalid_key_input.js");
4
4
  const is_key_like_js_1 = require("../runtime/is_key_like.js");
5
- const symmetricTypeCheck = (key) => {
5
+ const symmetricTypeCheck = (alg, key) => {
6
6
  if (key instanceof Uint8Array)
7
7
  return;
8
8
  if (!(0, is_key_like_js_1.default)(key)) {
9
- throw new TypeError((0, invalid_key_input_js_1.default)(key, ...is_key_like_js_1.types, 'Uint8Array'));
9
+ throw new TypeError((0, invalid_key_input_js_1.withAlg)(alg, 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
- const asymmetricTypeCheck = (key, usage) => {
15
+ const asymmetricTypeCheck = (alg, key, usage) => {
16
16
  if (!(0, is_key_like_js_1.default)(key)) {
17
- throw new TypeError((0, invalid_key_input_js_1.default)(key, ...is_key_like_js_1.types));
17
+ throw new TypeError((0, invalid_key_input_js_1.withAlg)(alg, 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"`);
@@ -38,10 +38,10 @@ const checkKeyType = (alg, key, usage) => {
38
38
  alg.startsWith('PBES2') ||
39
39
  /^A\d{3}(?:GCM)?KW$/.test(alg);
40
40
  if (symmetric) {
41
- symmetricTypeCheck(key);
41
+ symmetricTypeCheck(alg, key);
42
42
  }
43
43
  else {
44
- asymmetricTypeCheck(key, usage);
44
+ asymmetricTypeCheck(alg, key, usage);
45
45
  }
46
46
  };
47
47
  exports.default = checkKeyType;
@@ -11,7 +11,7 @@ const import_js_1 = require("../key/import.js");
11
11
  const check_key_type_js_1 = require("./check_key_type.js");
12
12
  const is_object_js_1 = require("./is_object.js");
13
13
  const aesgcmkw_js_1 = require("./aesgcmkw.js");
14
- async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
14
+ async function decryptKeyManagement(alg, key, encryptedKey, joseHeader, options) {
15
15
  (0, check_key_type_js_1.default)(alg, key, 'decrypt');
16
16
  switch (alg) {
17
17
  case 'dir': {
@@ -65,6 +65,9 @@ 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
73
  return (0, pbes2kw_js_1.decrypt)(alg, key, encryptedKey, joseHeader.p2c, (0, base64url_js_1.decode)(joseHeader.p2s));
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = (actual, ...types) => {
4
- let msg = 'Key must be ';
3
+ exports.withAlg = void 0;
4
+ function message(msg, actual, ...types) {
5
5
  if (types.length > 2) {
6
6
  const last = types.pop();
7
7
  msg += `one of type ${types.join(', ')}, or ${last}.`;
@@ -24,4 +24,11 @@ exports.default = (actual, ...types) => {
24
24
  }
25
25
  }
26
26
  return msg;
27
+ }
28
+ exports.default = (actual, ...types) => {
29
+ return message('Key must be ', actual, ...types);
27
30
  };
31
+ function withAlg(alg, actual, ...types) {
32
+ return message(`Key for the ${alg} algorithm must be `, actual, ...types);
33
+ }
34
+ exports.withAlg = withAlg;
@@ -19,14 +19,16 @@ const checkCekLength = (enc, cek) => {
19
19
  throw new errors_js_1.JOSENotSupported(`Content Encryption Algorithm ${enc} is not supported either by JOSE or your javascript runtime`);
20
20
  }
21
21
  if (cek instanceof Uint8Array) {
22
- if (cek.length << 3 !== expected) {
23
- throw new errors_js_1.JWEInvalid('Invalid Content Encryption Key length');
22
+ const actual = cek.byteLength << 3;
23
+ if (actual !== expected) {
24
+ throw new errors_js_1.JWEInvalid(`Invalid Content Encryption Key length. Expected ${expected} bits, got ${actual} bits`);
24
25
  }
25
26
  return;
26
27
  }
27
28
  if ((0, is_key_object_js_1.default)(cek) && cek.type === 'secret') {
28
- if (cek.symmetricKeySize << 3 !== expected) {
29
- throw new errors_js_1.JWEInvalid('Invalid Content Encryption Key length');
29
+ const actual = cek.symmetricKeySize << 3;
30
+ if (actual !== expected) {
31
+ throw new errors_js_1.JWEInvalid(`Invalid Content Encryption Key length. Expected ${expected} bits, got ${actual} bits`);
30
32
  }
31
33
  return;
32
34
  }
@@ -95,10 +95,10 @@ export async function flattenedDecrypt(jwe, key, options) {
95
95
  }
96
96
  let cek;
97
97
  try {
98
- cek = await decryptKeyManagement(alg, key, encryptedKey, joseHeader);
98
+ cek = await decryptKeyManagement(alg, key, encryptedKey, joseHeader, options);
99
99
  }
100
100
  catch (err) {
101
- if (err instanceof TypeError) {
101
+ if (err instanceof TypeError || err instanceof JWEInvalid || err instanceof JOSENotSupported) {
102
102
  throw err;
103
103
  }
104
104
  cek = generateCek(enc);
@@ -1,18 +1,18 @@
1
- import invalidKeyInput from './invalid_key_input.js';
1
+ import { withAlg as invalidKeyInput } from './invalid_key_input.js';
2
2
  import isKeyLike, { types } from '../runtime/is_key_like.js';
3
- const symmetricTypeCheck = (key) => {
3
+ const symmetricTypeCheck = (alg, key) => {
4
4
  if (key instanceof Uint8Array)
5
5
  return;
6
6
  if (!isKeyLike(key)) {
7
- throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'));
7
+ throw new TypeError(invalidKeyInput(alg, key, ...types, 'Uint8Array'));
8
8
  }
9
9
  if (key.type !== 'secret') {
10
10
  throw new TypeError(`${types.join(' or ')} instances for symmetric algorithms must be of type "secret"`);
11
11
  }
12
12
  };
13
- const asymmetricTypeCheck = (key, usage) => {
13
+ const asymmetricTypeCheck = (alg, key, usage) => {
14
14
  if (!isKeyLike(key)) {
15
- throw new TypeError(invalidKeyInput(key, ...types));
15
+ throw new TypeError(invalidKeyInput(alg, key, ...types));
16
16
  }
17
17
  if (key.type === 'secret') {
18
18
  throw new TypeError(`${types.join(' or ')} instances for asymmetric algorithms must not be of type "secret"`);
@@ -36,10 +36,10 @@ const checkKeyType = (alg, key, usage) => {
36
36
  alg.startsWith('PBES2') ||
37
37
  /^A\d{3}(?:GCM)?KW$/.test(alg);
38
38
  if (symmetric) {
39
- symmetricTypeCheck(key);
39
+ symmetricTypeCheck(alg, key);
40
40
  }
41
41
  else {
42
- asymmetricTypeCheck(key, usage);
42
+ asymmetricTypeCheck(alg, key, usage);
43
43
  }
44
44
  };
45
45
  export default checkKeyType;
@@ -9,7 +9,7 @@ import { importJWK } from '../key/import.js';
9
9
  import checkKeyType from './check_key_type.js';
10
10
  import isObject from './is_object.js';
11
11
  import { unwrap as aesGcmKw } from './aesgcmkw.js';
12
- async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
12
+ async function decryptKeyManagement(alg, key, encryptedKey, joseHeader, options) {
13
13
  checkKeyType(alg, key, 'decrypt');
14
14
  switch (alg) {
15
15
  case 'dir': {
@@ -63,6 +63,9 @@ async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
63
63
  throw new JWEInvalid('JWE Encrypted Key missing');
64
64
  if (typeof joseHeader.p2c !== 'number')
65
65
  throw new JWEInvalid(`JOSE Header "p2c" (PBES2 Count) missing or invalid`);
66
+ const p2cLimit = (options === null || options === void 0 ? void 0 : options.maxPBES2Count) || 10000;
67
+ if (joseHeader.p2c > p2cLimit)
68
+ throw new JWEInvalid(`JOSE Header "p2c" (PBES2 Count) out is of acceptable bounds`);
66
69
  if (typeof joseHeader.p2s !== 'string')
67
70
  throw new JWEInvalid(`JOSE Header "p2s" (PBES2 Salt) missing or invalid`);
68
71
  return pbes2Kw(alg, key, encryptedKey, joseHeader.p2c, base64url(joseHeader.p2s));
@@ -1,5 +1,4 @@
1
- export default (actual, ...types) => {
2
- let msg = 'Key must be ';
1
+ function message(msg, actual, ...types) {
3
2
  if (types.length > 2) {
4
3
  const last = types.pop();
5
4
  msg += `one of type ${types.join(', ')}, or ${last}.`;
@@ -22,4 +21,10 @@ export default (actual, ...types) => {
22
21
  }
23
22
  }
24
23
  return msg;
24
+ }
25
+ export default (actual, ...types) => {
26
+ return message('Key must be ', actual, ...types);
25
27
  };
28
+ export function withAlg(alg, actual, ...types) {
29
+ return message(`Key for the ${alg} algorithm must be `, actual, ...types);
30
+ }
@@ -17,14 +17,16 @@ const checkCekLength = (enc, cek) => {
17
17
  throw new JOSENotSupported(`Content Encryption Algorithm ${enc} is not supported either by JOSE or your javascript runtime`);
18
18
  }
19
19
  if (cek instanceof Uint8Array) {
20
- if (cek.length << 3 !== expected) {
21
- throw new JWEInvalid('Invalid Content Encryption Key length');
20
+ const actual = cek.byteLength << 3;
21
+ if (actual !== expected) {
22
+ throw new JWEInvalid(`Invalid Content Encryption Key length. Expected ${expected} bits, got ${actual} bits`);
22
23
  }
23
24
  return;
24
25
  }
25
26
  if (isKeyObject(cek) && cek.type === 'secret') {
26
- if (cek.symmetricKeySize << 3 !== expected) {
27
- throw new JWEInvalid('Invalid Content Encryption Key length');
27
+ const actual = cek.symmetricKeySize << 3;
28
+ if (actual !== expected) {
29
+ throw new JWEInvalid(`Invalid Content Encryption Key length. Expected ${expected} bits, got ${actual} bits`);
28
30
  }
29
31
  return;
30
32
  }
@@ -20,8 +20,8 @@
20
20
  * obtain a KeyObject from your existing key material.
21
21
  *
22
22
  * [CryptoKey](https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey) is a representation of a
23
- * key/secret available in the Browser and Deno runtimes. In addition to the import functions of
24
- * this library you may use the
23
+ * key/secret available in the Browser and Web-interoperable runtimes. In addition to the import
24
+ * functions of this library you may use the
25
25
  * [SubtleCrypto.importKey](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey)
26
26
  * API to obtain a CryptoKey from your existing key material.
27
27
  *
@@ -401,6 +401,13 @@ export interface DecryptOptions extends CritOption {
401
401
  * with compressed plaintext.
402
402
  */
403
403
  inflateRaw?: InflateFunction
404
+
405
+ /**
406
+ * (PBES2 Key Management Algorithms only) Maximum allowed "p2c" (PBES2 Count) Header Parameter
407
+ * value. The PBKDF2 iteration count defines the algorithm's computational expense. By default
408
+ * this value is set to 10000.
409
+ */
410
+ maxPBES2Count?: number
404
411
  }
405
412
 
406
413
  /** JWE Deflate option. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jose",
3
- "version": "4.9.1",
3
+ "version": "4.9.3",
4
4
  "description": "'JSON Web Almost Everything' - JWA, JWS, JWE, JWT, JWK, JWKS with no dependencies using runtime's native crypto",
5
5
  "keywords": [
6
6
  "browser",