jose 4.1.3 → 4.2.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.
Files changed (68) hide show
  1. package/README.md +4 -1
  2. package/dist/browser/index.js +1 -0
  3. package/dist/browser/jwe/flattened/encrypt.js +15 -4
  4. package/dist/browser/jwe/general/decrypt.js +3 -0
  5. package/dist/browser/jwe/general/encrypt.js +156 -0
  6. package/dist/browser/jws/general/sign.js +6 -16
  7. package/dist/browser/key/import.js +2 -1
  8. package/dist/browser/lib/cek.js +5 -6
  9. package/dist/browser/lib/decrypt_key_management.js +1 -1
  10. package/dist/browser/lib/encrypt_key_management.js +1 -1
  11. package/dist/browser/lib/iv.js +4 -11
  12. package/dist/browser/runtime/aeskw.js +2 -1
  13. package/dist/browser/runtime/asn1.js +2 -1
  14. package/dist/browser/runtime/check_key_length.js +0 -7
  15. package/dist/browser/runtime/decrypt.js +2 -1
  16. package/dist/browser/runtime/ecdhes.js +5 -4
  17. package/dist/browser/runtime/encrypt.js +2 -1
  18. package/dist/browser/runtime/get_sign_verify_key.js +3 -2
  19. package/dist/browser/runtime/key_to_jwk.js +2 -1
  20. package/dist/browser/runtime/pbes2kw.js +2 -1
  21. package/dist/browser/runtime/rsaes.js +3 -2
  22. package/dist/node/cjs/index.js +9 -7
  23. package/dist/node/cjs/jwe/flattened/encrypt.js +16 -5
  24. package/dist/node/cjs/jwe/general/decrypt.js +3 -0
  25. package/dist/node/cjs/jwe/general/encrypt.js +160 -0
  26. package/dist/node/cjs/jws/general/sign.js +6 -16
  27. package/dist/node/cjs/key/import.js +2 -1
  28. package/dist/node/cjs/lib/cek.js +5 -6
  29. package/dist/node/cjs/lib/decrypt_key_management.js +1 -1
  30. package/dist/node/cjs/lib/encrypt_key_management.js +1 -1
  31. package/dist/node/cjs/lib/iv.js +4 -11
  32. package/dist/node/cjs/runtime/aeskw.js +4 -3
  33. package/dist/node/cjs/runtime/asn1.js +2 -1
  34. package/dist/node/cjs/runtime/decrypt.js +13 -10
  35. package/dist/node/cjs/runtime/ecdhes.js +5 -4
  36. package/dist/node/cjs/runtime/encrypt.js +7 -5
  37. package/dist/node/cjs/runtime/get_named_curve.js +2 -1
  38. package/dist/node/cjs/runtime/get_sign_verify_key.js +3 -2
  39. package/dist/node/cjs/runtime/key_to_jwk.js +2 -1
  40. package/dist/node/cjs/runtime/pbes2kw.js +2 -1
  41. package/dist/node/cjs/runtime/rsaes.js +4 -3
  42. package/dist/node/cjs/runtime/sign.js +0 -4
  43. package/dist/node/esm/index.js +1 -0
  44. package/dist/node/esm/jwe/flattened/encrypt.js +15 -4
  45. package/dist/node/esm/jwe/general/decrypt.js +3 -0
  46. package/dist/node/esm/jwe/general/encrypt.js +156 -0
  47. package/dist/node/esm/jws/general/sign.js +6 -16
  48. package/dist/node/esm/key/import.js +2 -1
  49. package/dist/node/esm/lib/cek.js +5 -6
  50. package/dist/node/esm/lib/decrypt_key_management.js +1 -1
  51. package/dist/node/esm/lib/encrypt_key_management.js +1 -1
  52. package/dist/node/esm/lib/iv.js +4 -11
  53. package/dist/node/esm/runtime/aeskw.js +4 -3
  54. package/dist/node/esm/runtime/asn1.js +2 -1
  55. package/dist/node/esm/runtime/decrypt.js +13 -10
  56. package/dist/node/esm/runtime/ecdhes.js +5 -4
  57. package/dist/node/esm/runtime/encrypt.js +7 -5
  58. package/dist/node/esm/runtime/get_named_curve.js +2 -1
  59. package/dist/node/esm/runtime/get_sign_verify_key.js +3 -2
  60. package/dist/node/esm/runtime/key_to_jwk.js +2 -1
  61. package/dist/node/esm/runtime/pbes2kw.js +2 -1
  62. package/dist/node/esm/runtime/rsaes.js +4 -3
  63. package/dist/node/esm/runtime/sign.js +0 -4
  64. package/dist/types/index.d.ts +3 -1
  65. package/dist/types/jwe/flattened/encrypt.d.ts +1 -0
  66. package/dist/types/jwe/general/encrypt.d.ts +17 -0
  67. package/dist/types/types.d.ts +2 -1
  68. package/package.json +1 -1
@@ -16,10 +16,6 @@ else {
16
16
  const sign = async (alg, key, data) => {
17
17
  const keyObject = (0, get_sign_verify_key_js_1.default)(alg, key, 'sign');
18
18
  if (alg.startsWith('HS')) {
19
- const bitlen = parseInt(alg.substr(-3), 10);
20
- if (!keyObject.symmetricKeySize || keyObject.symmetricKeySize << 3 < bitlen) {
21
- throw new TypeError(`${alg} requires symmetric keys to be ${bitlen} bits or larger`);
22
- }
23
19
  const hmac = crypto.createHmac((0, hmac_digest_js_1.default)(alg), keyObject);
24
20
  hmac.update(data);
25
21
  return hmac.digest();
@@ -1,6 +1,7 @@
1
1
  export { compactDecrypt } from './jwe/compact/decrypt.js';
2
2
  export { flattenedDecrypt } from './jwe/flattened/decrypt.js';
3
3
  export { generalDecrypt } from './jwe/general/decrypt.js';
4
+ export { GeneralEncrypt } from './jwe/general/encrypt.js';
4
5
  export { compactVerify } from './jws/compact/verify.js';
5
6
  export { flattenedVerify } from './jws/flattened/verify.js';
6
7
  export { generalVerify } from './jws/general/verify.js';
@@ -7,6 +7,7 @@ import { JOSENotSupported, JWEInvalid } from '../../util/errors.js';
7
7
  import isDisjoint from '../../lib/is_disjoint.js';
8
8
  import { encoder, decoder, concat } from '../../lib/buffer_utils.js';
9
9
  import validateCrit from '../../lib/validate_crit.js';
10
+ export const unprotected = Symbol();
10
11
  export class FlattenedEncrypt {
11
12
  constructor(plaintext) {
12
13
  if (!(plaintext instanceof Uint8Array)) {
@@ -65,7 +66,7 @@ export class FlattenedEncrypt {
65
66
  throw new JWEInvalid('either setProtectedHeader, setUnprotectedHeader, or sharedUnprotectedHeader must be called before #encrypt()');
66
67
  }
67
68
  if (!isDisjoint(this._protectedHeader, this._unprotectedHeader, this._sharedUnprotectedHeader)) {
68
- throw new JWEInvalid('JWE Shared Protected, JWE Shared Unprotected and JWE Per-Recipient Header Parameter names must be disjoint');
69
+ throw new JWEInvalid('JWE Protected, JWE Shared Unprotected and JWE Per-Recipient Header Parameter names must be disjoint');
69
70
  }
70
71
  const joseHeader = {
71
72
  ...this._protectedHeader,
@@ -104,11 +105,21 @@ export class FlattenedEncrypt {
104
105
  let parameters;
105
106
  ({ cek, encryptedKey, parameters } = await encryptKeyManagement(alg, enc, key, this._cek, this._keyManagementParameters));
106
107
  if (parameters) {
107
- if (!this._protectedHeader) {
108
- this.setProtectedHeader(parameters);
108
+ if (options && unprotected in options) {
109
+ if (!this._unprotectedHeader) {
110
+ this.setUnprotectedHeader(parameters);
111
+ }
112
+ else {
113
+ this._unprotectedHeader = { ...this._unprotectedHeader, ...parameters };
114
+ }
109
115
  }
110
116
  else {
111
- this._protectedHeader = { ...this._protectedHeader, ...parameters };
117
+ if (!this._protectedHeader) {
118
+ this.setProtectedHeader(parameters);
119
+ }
120
+ else {
121
+ this._protectedHeader = { ...this._protectedHeader, ...parameters };
122
+ }
112
123
  }
113
124
  }
114
125
  }
@@ -8,6 +8,9 @@ export async function generalDecrypt(jwe, key, options) {
8
8
  if (!Array.isArray(jwe.recipients) || !jwe.recipients.every(isObject)) {
9
9
  throw new JWEInvalid('JWE Recipients missing or incorrect type');
10
10
  }
11
+ if (!jwe.recipients.length) {
12
+ throw new JWEInvalid('JWE Recipients has no members');
13
+ }
11
14
  for (const recipient of jwe.recipients) {
12
15
  try {
13
16
  return await flattenedDecrypt({
@@ -0,0 +1,156 @@
1
+ import { FlattenedEncrypt, unprotected } from '../flattened/encrypt.js';
2
+ import { JWEInvalid } from '../../util/errors.js';
3
+ import generateCek from '../../lib/cek.js';
4
+ import isDisjoint from '../../lib/is_disjoint.js';
5
+ import encryptKeyManagement from '../../lib/encrypt_key_management.js';
6
+ import { encode as base64url } from '../../runtime/base64url.js';
7
+ import validateCrit from '../../lib/validate_crit.js';
8
+ const recipientRef = new WeakMap();
9
+ class IndividualRecipient {
10
+ setUnprotectedHeader(unprotectedHeader) {
11
+ const ref = recipientRef.get(this);
12
+ if (ref.unprotectedHeader) {
13
+ throw new TypeError('setUnprotectedHeader can only be called once');
14
+ }
15
+ ref.unprotectedHeader = unprotectedHeader;
16
+ return this;
17
+ }
18
+ }
19
+ export class GeneralEncrypt {
20
+ constructor(plaintext) {
21
+ this._recipients = [];
22
+ this._plaintext = plaintext;
23
+ }
24
+ addRecipient(key, options) {
25
+ const recipient = new IndividualRecipient();
26
+ recipientRef.set(recipient, { key, options: { crit: options === null || options === void 0 ? void 0 : options.crit } });
27
+ this._recipients.push(recipient);
28
+ return recipient;
29
+ }
30
+ setProtectedHeader(protectedHeader) {
31
+ if (this._protectedHeader) {
32
+ throw new TypeError('setProtectedHeader can only be called once');
33
+ }
34
+ this._protectedHeader = protectedHeader;
35
+ return this;
36
+ }
37
+ setSharedUnprotectedHeader(sharedUnprotectedHeader) {
38
+ if (this._unprotectedHeader) {
39
+ throw new TypeError('setSharedUnprotectedHeader can only be called once');
40
+ }
41
+ this._unprotectedHeader = sharedUnprotectedHeader;
42
+ return this;
43
+ }
44
+ setAdditionalAuthenticatedData(aad) {
45
+ this._aad = aad;
46
+ return this;
47
+ }
48
+ async encrypt(options) {
49
+ var _a, _b;
50
+ if (!this._recipients.length) {
51
+ throw new JWEInvalid('at least one recipient must be added');
52
+ }
53
+ options = { deflateRaw: options === null || options === void 0 ? void 0 : options.deflateRaw };
54
+ if (this._recipients.length === 1) {
55
+ const { unprotectedHeader, options: recipientOpts, key, } = recipientRef.get(this._recipients[0]);
56
+ const flattened = await new FlattenedEncrypt(this._plaintext)
57
+ .setAdditionalAuthenticatedData(this._aad)
58
+ .setProtectedHeader(this._protectedHeader)
59
+ .setSharedUnprotectedHeader(this._unprotectedHeader)
60
+ .setUnprotectedHeader(unprotectedHeader)
61
+ .encrypt(key, { ...recipientOpts, ...options });
62
+ let jwe = {
63
+ ciphertext: flattened.ciphertext,
64
+ iv: flattened.iv,
65
+ recipients: [{}],
66
+ tag: flattened.tag,
67
+ };
68
+ if (flattened.aad)
69
+ jwe.aad = flattened.aad;
70
+ if (flattened.protected)
71
+ jwe.protected = flattened.protected;
72
+ if (flattened.unprotected)
73
+ jwe.unprotected = flattened.unprotected;
74
+ if (flattened.encrypted_key)
75
+ jwe.recipients[0].encrypted_key = flattened.encrypted_key;
76
+ if (flattened.header)
77
+ jwe.recipients[0].header = flattened.header;
78
+ return jwe;
79
+ }
80
+ let enc;
81
+ for (let i = 0; i < this._recipients.length; i++) {
82
+ const recipient = this._recipients[i];
83
+ const { unprotectedHeader, options: recipientOpts } = recipientRef.get(recipient);
84
+ if (!isDisjoint(this._protectedHeader, this._unprotectedHeader, unprotectedHeader)) {
85
+ throw new JWEInvalid('JWE Protected, JWE Shared Unprotected and JWE Per-Recipient Header Parameter names must be disjoint');
86
+ }
87
+ const joseHeader = {
88
+ ...this._protectedHeader,
89
+ ...this._unprotectedHeader,
90
+ ...unprotectedHeader,
91
+ };
92
+ const { alg } = joseHeader;
93
+ if (typeof alg !== 'string' || !alg) {
94
+ throw new JWEInvalid('JWE "alg" (Algorithm) Header Parameter missing or invalid');
95
+ }
96
+ if (alg === 'dir' || alg === 'ECDH-ES') {
97
+ throw new JWEInvalid('"dir" and "ECDH-ES" alg may only be used with a single recipient');
98
+ }
99
+ if (typeof joseHeader.enc !== 'string' || !joseHeader.enc) {
100
+ throw new JWEInvalid('JWE "enc" (Encryption Algorithm) Header Parameter missing or invalid');
101
+ }
102
+ if (!enc) {
103
+ enc = joseHeader.enc;
104
+ }
105
+ else if (enc !== joseHeader.enc) {
106
+ throw new JWEInvalid('JWE "enc" (Encryption Algorithm) Header Parameter must be the same for all recipients');
107
+ }
108
+ validateCrit(JWEInvalid, new Map(), recipientOpts === null || recipientOpts === void 0 ? void 0 : recipientOpts.crit, this._protectedHeader, joseHeader);
109
+ if (joseHeader.zip !== undefined) {
110
+ if (!this._protectedHeader || !this._protectedHeader.zip) {
111
+ throw new JWEInvalid('JWE "zip" (Compression Algorithm) Header MUST be integrity protected');
112
+ }
113
+ }
114
+ }
115
+ const cek = generateCek(enc);
116
+ let jwe = {
117
+ ciphertext: '',
118
+ iv: '',
119
+ recipients: [],
120
+ tag: '',
121
+ };
122
+ for (let i = 0; i < this._recipients.length; i++) {
123
+ const recipient = this._recipients[i];
124
+ const target = {};
125
+ jwe.recipients.push(target);
126
+ const { unprotectedHeader, options: recipientOpts, key } = recipientRef.get(recipient);
127
+ if (i === 0) {
128
+ const flattened = await new FlattenedEncrypt(this._plaintext)
129
+ .setAdditionalAuthenticatedData(this._aad)
130
+ .setContentEncryptionKey(cek)
131
+ .setProtectedHeader(this._protectedHeader)
132
+ .setSharedUnprotectedHeader(this._unprotectedHeader)
133
+ .setUnprotectedHeader(unprotectedHeader)
134
+ .encrypt(key, { ...recipientOpts, ...options, [unprotected]: true });
135
+ jwe.ciphertext = flattened.ciphertext;
136
+ jwe.iv = flattened.iv;
137
+ jwe.tag = flattened.tag;
138
+ if (flattened.aad)
139
+ jwe.aad = flattened.aad;
140
+ if (flattened.protected)
141
+ jwe.protected = flattened.protected;
142
+ if (flattened.unprotected)
143
+ jwe.unprotected = flattened.unprotected;
144
+ target.encrypted_key = flattened.encrypted_key;
145
+ if (flattened.header)
146
+ target.header = flattened.header;
147
+ continue;
148
+ }
149
+ const { encryptedKey, parameters } = await encryptKeyManagement((unprotectedHeader === null || unprotectedHeader === void 0 ? void 0 : unprotectedHeader.alg) || ((_a = this._protectedHeader) === null || _a === void 0 ? void 0 : _a.alg) || ((_b = this._unprotectedHeader) === null || _b === void 0 ? void 0 : _b.alg), enc, key, cek);
150
+ target.encrypted_key = base64url(encryptedKey);
151
+ if (unprotectedHeader || parameters)
152
+ target.header = { ...unprotectedHeader, ...parameters };
153
+ }
154
+ return jwe;
155
+ }
156
+ }
@@ -3,31 +3,21 @@ import { JWSInvalid } from '../../util/errors.js';
3
3
  const signatureRef = new WeakMap();
4
4
  class IndividualSignature {
5
5
  setProtectedHeader(protectedHeader) {
6
- if (this._protectedHeader) {
6
+ const ref = signatureRef.get(this);
7
+ if (ref.protectedHeader) {
7
8
  throw new TypeError('setProtectedHeader can only be called once');
8
9
  }
9
- this._protectedHeader = protectedHeader;
10
+ ref.protectedHeader = protectedHeader;
10
11
  return this;
11
12
  }
12
13
  setUnprotectedHeader(unprotectedHeader) {
13
- if (this._unprotectedHeader) {
14
+ const ref = signatureRef.get(this);
15
+ if (ref.unprotectedHeader) {
14
16
  throw new TypeError('setUnprotectedHeader can only be called once');
15
17
  }
16
- this._unprotectedHeader = unprotectedHeader;
18
+ ref.unprotectedHeader = unprotectedHeader;
17
19
  return this;
18
20
  }
19
- set _protectedHeader(value) {
20
- signatureRef.get(this).protectedHeader = value;
21
- }
22
- get _protectedHeader() {
23
- return signatureRef.get(this).protectedHeader;
24
- }
25
- set _unprotectedHeader(value) {
26
- signatureRef.get(this).unprotectedHeader = value;
27
- }
28
- get _unprotectedHeader() {
29
- return signatureRef.get(this).unprotectedHeader;
30
- }
31
21
  }
32
22
  export class GeneralSign {
33
23
  constructor(payload) {
@@ -62,7 +62,8 @@ function parseElement(bytes) {
62
62
  };
63
63
  }
64
64
  function spkiFromX509(buf) {
65
- return encodeBase64(getElement(getElement(parseElement(buf).contents)[0].contents)[6].raw);
65
+ const tbsCertificate = getElement(getElement(parseElement(buf).contents)[0].contents);
66
+ return encodeBase64(tbsCertificate[tbsCertificate[0].raw[0] === 0xa0 ? 6 : 5].raw);
66
67
  }
67
68
  function getSPKI(x509) {
68
69
  const pem = x509.replace(/(?:-----(?:BEGIN|END) CERTIFICATE-----|\s)/g, '');
@@ -2,18 +2,17 @@ import { JOSENotSupported } from '../util/errors.js';
2
2
  import random from '../runtime/random.js';
3
3
  export function bitLength(alg) {
4
4
  switch (alg) {
5
- case 'A128CBC-HS256':
6
- return 256;
7
- case 'A192CBC-HS384':
8
- return 384;
9
- case 'A256CBC-HS512':
10
- return 512;
11
5
  case 'A128GCM':
12
6
  return 128;
13
7
  case 'A192GCM':
14
8
  return 192;
15
9
  case 'A256GCM':
10
+ case 'A128CBC-HS256':
16
11
  return 256;
12
+ case 'A192CBC-HS384':
13
+ return 384;
14
+ case 'A256CBC-HS512':
15
+ return 512;
17
16
  default:
18
17
  throw new JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
19
18
  }
@@ -40,7 +40,7 @@ async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
40
40
  throw new JWEInvalid(`JOSE Header "apv" (Agreement PartyVInfo) invalid`);
41
41
  partyVInfo = base64url(joseHeader.apv);
42
42
  }
43
- const sharedSecret = await ECDH.deriveKey(epk, key, alg === 'ECDH-ES' ? joseHeader.enc : alg, parseInt(alg.substr(-5, 3), 10) || cekLength(joseHeader.enc), partyUInfo, partyVInfo);
43
+ const sharedSecret = await ECDH.deriveKey(epk, key, alg === 'ECDH-ES' ? joseHeader.enc : alg, alg === 'ECDH-ES' ? cekLength(joseHeader.enc) : parseInt(alg.substr(-5, 3), 10), partyUInfo, partyVInfo);
44
44
  if (alg === 'ECDH-ES')
45
45
  return sharedSecret;
46
46
  if (encryptedKey === undefined)
@@ -29,7 +29,7 @@ async function encryptKeyManagement(alg, enc, key, providedCek, providedParamete
29
29
  let { epk: ephemeralKey } = providedParameters;
30
30
  ephemeralKey || (ephemeralKey = await ECDH.generateEpk(key));
31
31
  const { x, y, crv, kty } = await exportJWK(ephemeralKey);
32
- const sharedSecret = await ECDH.deriveKey(key, ephemeralKey, alg === 'ECDH-ES' ? enc : alg, parseInt(alg.substr(-5, 3), 10) || cekLength(enc), apu, apv);
32
+ const sharedSecret = await ECDH.deriveKey(key, ephemeralKey, alg === 'ECDH-ES' ? enc : alg, alg === 'ECDH-ES' ? cekLength(enc) : parseInt(alg.substr(-5, 3), 10), apu, apv);
33
33
  parameters = { epk: { x, y, crv, kty } };
34
34
  if (apu)
35
35
  parameters.apu = base64url(apu);
@@ -2,24 +2,17 @@ import { JOSENotSupported } from '../util/errors.js';
2
2
  import random from '../runtime/random.js';
3
3
  export function bitLength(alg) {
4
4
  switch (alg) {
5
- case 'A128CBC-HS256':
6
- return 128;
7
5
  case 'A128GCM':
8
- return 96;
9
6
  case 'A128GCMKW':
10
- return 96;
11
- case 'A192CBC-HS384':
12
- return 128;
13
7
  case 'A192GCM':
14
- return 96;
15
8
  case 'A192GCMKW':
16
- return 96;
17
- case 'A256CBC-HS512':
18
- return 128;
19
9
  case 'A256GCM':
20
- return 96;
21
10
  case 'A256GCMKW':
22
11
  return 96;
12
+ case 'A128CBC-HS256':
13
+ case 'A192CBC-HS384':
14
+ case 'A256CBC-HS512':
15
+ return 128;
23
16
  default:
24
17
  throw new JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
25
18
  }
@@ -8,6 +8,7 @@ import { checkEncCryptoKey } from '../lib/crypto_key.js';
8
8
  import isKeyObject from './is_key_object.js';
9
9
  import invalidKeyInput from '../lib/invalid_key_input.js';
10
10
  import supported from './ciphers.js';
11
+ import { types } from './is_key_like.js';
11
12
  function checkKeySize(key, alg) {
12
13
  if (key.symmetricKeySize << 3 !== parseInt(alg.substr(1, 3), 10)) {
13
14
  throw new TypeError(`Invalid key size for alg: ${alg}`);
@@ -24,9 +25,9 @@ function ensureKeyObject(key, alg, usage) {
24
25
  checkEncCryptoKey(key, alg, usage);
25
26
  return KeyObject.from(key);
26
27
  }
27
- throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey', 'Uint8Array'));
28
+ throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'));
28
29
  }
29
- export const wrap = async (alg, key, cek) => {
30
+ export const wrap = (alg, key, cek) => {
30
31
  const size = parseInt(alg.substr(1, 3), 10);
31
32
  const algorithm = `aes${size}-wrap`;
32
33
  if (!supported(algorithm)) {
@@ -37,7 +38,7 @@ export const wrap = async (alg, key, cek) => {
37
38
  const cipher = createCipheriv(algorithm, keyObject, Buffer.alloc(8, 0xa6));
38
39
  return concat(cipher.update(cek), cipher.final());
39
40
  };
40
- export const unwrap = async (alg, key, encryptedKey) => {
41
+ export const unwrap = (alg, key, encryptedKey) => {
41
42
  const size = parseInt(alg.substr(1, 3), 10);
42
43
  const algorithm = `aes${size}-wrap`;
43
44
  if (!supported(algorithm)) {
@@ -3,6 +3,7 @@ import { Buffer } from 'buffer';
3
3
  import { isCryptoKey } from './webcrypto.js';
4
4
  import isKeyObject from './is_key_object.js';
5
5
  import invalidKeyInput from '../lib/invalid_key_input.js';
6
+ import { types } from './is_key_like.js';
6
7
  const genericExport = (keyType, keyFormat, key) => {
7
8
  let keyObject;
8
9
  if (isCryptoKey(key)) {
@@ -15,7 +16,7 @@ const genericExport = (keyType, keyFormat, key) => {
15
16
  keyObject = key;
16
17
  }
17
18
  else {
18
- throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey'));
19
+ throw new TypeError(invalidKeyInput(key, ...types));
19
20
  }
20
21
  if (keyObject.type !== keyType) {
21
22
  throw new TypeError(`key is not a ${keyType} key`);
@@ -10,7 +10,8 @@ import { checkEncCryptoKey } from '../lib/crypto_key.js';
10
10
  import isKeyObject from './is_key_object.js';
11
11
  import invalidKeyInput from '../lib/invalid_key_input.js';
12
12
  import supported from './ciphers.js';
13
- async function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
13
+ import { types } from './is_key_like.js';
14
+ function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
14
15
  const keySize = parseInt(enc.substr(1, 3), 10);
15
16
  if (isKeyObject(cek)) {
16
17
  cek = cek.export();
@@ -34,8 +35,8 @@ async function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
34
35
  }
35
36
  let plaintext;
36
37
  try {
37
- const cipher = createDecipheriv(algorithm, encKey, iv);
38
- plaintext = concat(cipher.update(ciphertext), cipher.final());
38
+ const decipher = createDecipheriv(algorithm, encKey, iv);
39
+ plaintext = concat(decipher.update(ciphertext), decipher.final());
39
40
  }
40
41
  catch {
41
42
  }
@@ -44,25 +45,27 @@ async function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
44
45
  }
45
46
  return plaintext;
46
47
  }
47
- async function gcmDecrypt(enc, cek, ciphertext, iv, tag, aad) {
48
+ function gcmDecrypt(enc, cek, ciphertext, iv, tag, aad) {
48
49
  const keySize = parseInt(enc.substr(1, 3), 10);
49
50
  const algorithm = `aes-${keySize}-gcm`;
50
51
  if (!supported(algorithm)) {
51
52
  throw new JOSENotSupported(`alg ${enc} is not supported by your javascript runtime`);
52
53
  }
53
54
  try {
54
- const cipher = createDecipheriv(algorithm, cek, iv, { authTagLength: 16 });
55
- cipher.setAuthTag(tag);
55
+ const decipher = createDecipheriv(algorithm, cek, iv, { authTagLength: 16 });
56
+ decipher.setAuthTag(tag);
56
57
  if (aad.byteLength) {
57
- cipher.setAAD(aad, { plaintextLength: ciphertext.length });
58
+ decipher.setAAD(aad, { plaintextLength: ciphertext.length });
58
59
  }
59
- return concat(cipher.update(ciphertext), cipher.final());
60
+ const plaintext = decipher.update(ciphertext);
61
+ decipher.final();
62
+ return plaintext;
60
63
  }
61
64
  catch {
62
65
  throw new JWEDecryptionFailed();
63
66
  }
64
67
  }
65
- const decrypt = async (enc, cek, ciphertext, iv, tag, aad) => {
68
+ const decrypt = (enc, cek, ciphertext, iv, tag, aad) => {
66
69
  let key;
67
70
  if (isCryptoKey(cek)) {
68
71
  checkEncCryptoKey(cek, enc, 'decrypt');
@@ -72,7 +75,7 @@ const decrypt = async (enc, cek, ciphertext, iv, tag, aad) => {
72
75
  key = cek;
73
76
  }
74
77
  else {
75
- throw new TypeError(invalidKeyInput(cek, 'KeyObject', 'CryptoKey', 'Uint8Array'));
78
+ throw new TypeError(invalidKeyInput(cek, ...types, 'Uint8Array'));
76
79
  }
77
80
  checkCekLength(enc, key);
78
81
  checkIvLength(enc, iv);
@@ -8,8 +8,9 @@ import { isCryptoKey } from './webcrypto.js';
8
8
  import { checkEncCryptoKey } from '../lib/crypto_key.js';
9
9
  import isKeyObject from './is_key_object.js';
10
10
  import invalidKeyInput from '../lib/invalid_key_input.js';
11
+ import { types } from './is_key_like.js';
11
12
  const generateKeyPair = promisify(generateKeyPairCb);
12
- export const deriveKey = async (publicKee, privateKee, algorithm, keyLength, apu = new Uint8Array(0), apv = new Uint8Array(0)) => {
13
+ export const deriveKey = (publicKee, privateKee, algorithm, keyLength, apu = new Uint8Array(0), apv = new Uint8Array(0)) => {
13
14
  let publicKey;
14
15
  if (isCryptoKey(publicKee)) {
15
16
  checkEncCryptoKey(publicKee, 'ECDH-ES');
@@ -19,7 +20,7 @@ export const deriveKey = async (publicKee, privateKee, algorithm, keyLength, apu
19
20
  publicKey = publicKee;
20
21
  }
21
22
  else {
22
- throw new TypeError(invalidKeyInput(publicKee, 'KeyObject', 'CryptoKey'));
23
+ throw new TypeError(invalidKeyInput(publicKee, ...types));
23
24
  }
24
25
  let privateKey;
25
26
  if (isCryptoKey(privateKee)) {
@@ -30,7 +31,7 @@ export const deriveKey = async (publicKee, privateKee, algorithm, keyLength, apu
30
31
  privateKey = privateKee;
31
32
  }
32
33
  else {
33
- throw new TypeError(invalidKeyInput(privateKee, 'KeyObject', 'CryptoKey'));
34
+ throw new TypeError(invalidKeyInput(privateKee, ...types));
34
35
  }
35
36
  const value = concat(lengthAndInput(encoder.encode(algorithm)), lengthAndInput(apu), lengthAndInput(apv), uint32be(keyLength));
36
37
  const sharedSecret = diffieHellman({ privateKey, publicKey });
@@ -45,7 +46,7 @@ export const generateEpk = async (kee) => {
45
46
  key = kee;
46
47
  }
47
48
  else {
48
- throw new TypeError(invalidKeyInput(kee, 'KeyObject', 'CryptoKey'));
49
+ throw new TypeError(invalidKeyInput(kee, ...types));
49
50
  }
50
51
  switch (key.asymmetricKeyType) {
51
52
  case 'x25519':
@@ -9,7 +9,8 @@ import isKeyObject from './is_key_object.js';
9
9
  import invalidKeyInput from '../lib/invalid_key_input.js';
10
10
  import { JOSENotSupported } from '../util/errors.js';
11
11
  import supported from './ciphers.js';
12
- async function cbcEncrypt(enc, plaintext, cek, iv, aad) {
12
+ import { types } from './is_key_like.js';
13
+ function cbcEncrypt(enc, plaintext, cek, iv, aad) {
13
14
  const keySize = parseInt(enc.substr(1, 3), 10);
14
15
  if (isKeyObject(cek)) {
15
16
  cek = cek.export();
@@ -26,7 +27,7 @@ async function cbcEncrypt(enc, plaintext, cek, iv, aad) {
26
27
  const tag = cbcTag(aad, iv, ciphertext, macSize, macKey, keySize);
27
28
  return { ciphertext, tag };
28
29
  }
29
- async function gcmEncrypt(enc, plaintext, cek, iv, aad) {
30
+ function gcmEncrypt(enc, plaintext, cek, iv, aad) {
30
31
  const keySize = parseInt(enc.substr(1, 3), 10);
31
32
  const algorithm = `aes-${keySize}-gcm`;
32
33
  if (!supported(algorithm)) {
@@ -36,11 +37,12 @@ async function gcmEncrypt(enc, plaintext, cek, iv, aad) {
36
37
  if (aad.byteLength) {
37
38
  cipher.setAAD(aad, { plaintextLength: plaintext.length });
38
39
  }
39
- const ciphertext = concat(cipher.update(plaintext), cipher.final());
40
+ const ciphertext = cipher.update(plaintext);
41
+ cipher.final();
40
42
  const tag = cipher.getAuthTag();
41
43
  return { ciphertext, tag };
42
44
  }
43
- const encrypt = async (enc, plaintext, cek, iv, aad) => {
45
+ const encrypt = (enc, plaintext, cek, iv, aad) => {
44
46
  let key;
45
47
  if (isCryptoKey(cek)) {
46
48
  checkEncCryptoKey(cek, enc, 'encrypt');
@@ -50,7 +52,7 @@ const encrypt = async (enc, plaintext, cek, iv, aad) => {
50
52
  key = cek;
51
53
  }
52
54
  else {
53
- throw new TypeError(invalidKeyInput(cek, 'KeyObject', 'CryptoKey', 'Uint8Array'));
55
+ throw new TypeError(invalidKeyInput(cek, ...types, 'Uint8Array'));
54
56
  }
55
57
  checkCekLength(enc, key);
56
58
  checkIvLength(enc, iv);
@@ -4,6 +4,7 @@ import { JOSENotSupported } from '../util/errors.js';
4
4
  import { isCryptoKey } from './webcrypto.js';
5
5
  import isKeyObject from './is_key_object.js';
6
6
  import invalidKeyInput from '../lib/invalid_key_input.js';
7
+ import { types } from './is_key_like.js';
7
8
  const p256 = Buffer.from([42, 134, 72, 206, 61, 3, 1, 7]);
8
9
  const p384 = Buffer.from([43, 129, 4, 0, 34]);
9
10
  const p521 = Buffer.from([43, 129, 4, 0, 35]);
@@ -33,7 +34,7 @@ const getNamedCurve = (kee, raw) => {
33
34
  key = kee;
34
35
  }
35
36
  else {
36
- throw new TypeError(invalidKeyInput(kee, 'KeyObject', 'CryptoKey'));
37
+ throw new TypeError(invalidKeyInput(kee, ...types));
37
38
  }
38
39
  if (key.type === 'secret') {
39
40
  throw new TypeError('only "private" or "public" type keys can be used for this operation');
@@ -3,10 +3,11 @@ import { isCryptoKey } from './webcrypto.js';
3
3
  import { checkSigCryptoKey } from '../lib/crypto_key.js';
4
4
  import getSecretKey from './secret_key.js';
5
5
  import invalidKeyInput from '../lib/invalid_key_input.js';
6
+ import { types } from './is_key_like.js';
6
7
  export default function getSignVerifyKey(alg, key, usage) {
7
8
  if (key instanceof Uint8Array) {
8
9
  if (!alg.startsWith('HS')) {
9
- throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey'));
10
+ throw new TypeError(invalidKeyInput(key, ...types));
10
11
  }
11
12
  return getSecretKey(key);
12
13
  }
@@ -17,5 +18,5 @@ export default function getSignVerifyKey(alg, key, usage) {
17
18
  checkSigCryptoKey(key, alg, usage);
18
19
  return KeyObject.from(key);
19
20
  }
20
- throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey', 'Uint8Array'));
21
+ throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'));
21
22
  }
@@ -6,6 +6,7 @@ import getNamedCurve from './get_named_curve.js';
6
6
  import { isCryptoKey } from './webcrypto.js';
7
7
  import isKeyObject from './is_key_object.js';
8
8
  import invalidKeyInput from '../lib/invalid_key_input.js';
9
+ import { types } from './is_key_like.js';
9
10
  const [major, minor] = process.version
10
11
  .substr(1)
11
12
  .split('.')
@@ -29,7 +30,7 @@ const keyToJWK = (key) => {
29
30
  };
30
31
  }
31
32
  else {
32
- throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey', 'Uint8Array'));
33
+ throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'));
33
34
  }
34
35
  if (jwkExportSupported) {
35
36
  return keyObject.export({ format: 'jwk' });
@@ -9,6 +9,7 @@ import { isCryptoKey } from './webcrypto.js';
9
9
  import { checkEncCryptoKey } from '../lib/crypto_key.js';
10
10
  import isKeyObject from './is_key_object.js';
11
11
  import invalidKeyInput from '../lib/invalid_key_input.js';
12
+ import { types } from './is_key_like.js';
12
13
  const pbkdf2 = promisify(pbkdf2cb);
13
14
  function getPassword(key, alg) {
14
15
  if (isKeyObject(key)) {
@@ -21,7 +22,7 @@ function getPassword(key, alg) {
21
22
  checkEncCryptoKey(key, alg, 'deriveBits', 'deriveKey');
22
23
  return KeyObject.from(key).export();
23
24
  }
24
- throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey', 'Uint8Array'));
25
+ throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'));
25
26
  }
26
27
  export const encrypt = async (alg, key, cek, p2c = Math.floor(Math.random() * 2049) + 2048, p2s = random(new Uint8Array(16))) => {
27
28
  checkP2s(p2s);
@@ -4,6 +4,7 @@ import { isCryptoKey } from './webcrypto.js';
4
4
  import { checkEncCryptoKey } from '../lib/crypto_key.js';
5
5
  import isKeyObject from './is_key_object.js';
6
6
  import invalidKeyInput from '../lib/invalid_key_input.js';
7
+ import { types } from './is_key_like.js';
7
8
  const checkKey = (key, alg) => {
8
9
  if (key.asymmetricKeyType !== 'rsa') {
9
10
  throw new TypeError('Invalid key for this operation, its asymmetricKeyType must be rsa');
@@ -45,16 +46,16 @@ function ensureKeyObject(key, alg, ...usages) {
45
46
  checkEncCryptoKey(key, alg, ...usages);
46
47
  return KeyObject.from(key);
47
48
  }
48
- throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey'));
49
+ throw new TypeError(invalidKeyInput(key, ...types));
49
50
  }
50
- export const encrypt = async (alg, key, cek) => {
51
+ export const encrypt = (alg, key, cek) => {
51
52
  const padding = resolvePadding(alg);
52
53
  const oaepHash = resolveOaepHash(alg);
53
54
  const keyObject = ensureKeyObject(key, alg, 'wrapKey', 'encrypt');
54
55
  checkKey(keyObject, alg);
55
56
  return publicEncrypt({ key: keyObject, oaepHash, padding }, cek);
56
57
  };
57
- export const decrypt = async (alg, key, encryptedKey) => {
58
+ export const decrypt = (alg, key, encryptedKey) => {
58
59
  const padding = resolvePadding(alg);
59
60
  const oaepHash = resolveOaepHash(alg);
60
61
  const keyObject = ensureKeyObject(key, alg, 'unwrapKey', 'decrypt');