jose 4.0.0 → 4.0.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.
Files changed (54) hide show
  1. package/dist/browser/lib/cek.js +18 -16
  2. package/dist/browser/lib/check_iv_length.js +2 -2
  3. package/dist/browser/lib/crypto_key.js +160 -0
  4. package/dist/browser/lib/decrypt_key_management.js +2 -2
  5. package/dist/browser/lib/encrypt_key_management.js +2 -2
  6. package/dist/browser/lib/iv.js +24 -19
  7. package/dist/browser/runtime/aeskw.js +5 -3
  8. package/dist/browser/runtime/check_cek_length.js +4 -34
  9. package/dist/browser/runtime/decrypt.js +18 -6
  10. package/dist/browser/runtime/ecdhes.js +3 -0
  11. package/dist/browser/runtime/encrypt.js +18 -6
  12. package/dist/browser/runtime/get_sign_verify_key.js +2 -0
  13. package/dist/browser/runtime/pbes2kw.js +5 -3
  14. package/dist/browser/runtime/rsaes.js +3 -0
  15. package/dist/node/cjs/lib/cek.js +20 -17
  16. package/dist/node/cjs/lib/check_iv_length.js +1 -1
  17. package/dist/node/cjs/lib/crypto_key.js +165 -0
  18. package/dist/node/cjs/lib/decrypt_key_management.js +1 -1
  19. package/dist/node/cjs/lib/encrypt_key_management.js +1 -1
  20. package/dist/node/cjs/lib/iv.js +26 -20
  21. package/dist/node/cjs/runtime/aeskw.js +3 -1
  22. package/dist/node/cjs/runtime/asn1.js +1 -1
  23. package/dist/node/cjs/runtime/decrypt.js +3 -1
  24. package/dist/node/cjs/runtime/ecdhes.js +6 -3
  25. package/dist/node/cjs/runtime/encrypt.js +3 -1
  26. package/dist/node/cjs/runtime/fetch_jwks.js +13 -9
  27. package/dist/node/cjs/runtime/get_named_curve.js +1 -1
  28. package/dist/node/cjs/runtime/get_sign_verify_key.js +5 -3
  29. package/dist/node/cjs/runtime/global.js +11 -0
  30. package/dist/node/cjs/runtime/key_to_jwk.js +1 -1
  31. package/dist/node/cjs/runtime/pbes2kw.js +3 -1
  32. package/dist/node/cjs/runtime/rsaes.js +3 -1
  33. package/dist/node/cjs/runtime/webcrypto.js +1 -149
  34. package/dist/node/esm/lib/cek.js +18 -16
  35. package/dist/node/esm/lib/check_iv_length.js +2 -2
  36. package/dist/node/esm/lib/crypto_key.js +160 -0
  37. package/dist/node/esm/lib/decrypt_key_management.js +2 -2
  38. package/dist/node/esm/lib/encrypt_key_management.js +2 -2
  39. package/dist/node/esm/lib/iv.js +24 -19
  40. package/dist/node/esm/runtime/aeskw.js +5 -3
  41. package/dist/node/esm/runtime/asn1.js +3 -3
  42. package/dist/node/esm/runtime/decrypt.js +5 -3
  43. package/dist/node/esm/runtime/ecdhes.js +8 -5
  44. package/dist/node/esm/runtime/encrypt.js +5 -3
  45. package/dist/node/esm/runtime/fetch_jwks.js +13 -9
  46. package/dist/node/esm/runtime/get_named_curve.js +3 -3
  47. package/dist/node/esm/runtime/get_sign_verify_key.js +6 -4
  48. package/dist/node/esm/runtime/global.js +6 -0
  49. package/dist/node/esm/runtime/key_to_jwk.js +3 -3
  50. package/dist/node/esm/runtime/pbes2kw.js +5 -3
  51. package/dist/node/esm/runtime/rsaes.js +5 -3
  52. package/dist/node/esm/runtime/webcrypto.js +0 -147
  53. package/dist/types/index.d.ts +17 -0
  54. package/package.json +1 -1
@@ -1,19 +1,21 @@
1
1
  import { JOSENotSupported } from '../util/errors.js';
2
2
  import random from '../runtime/random.js';
3
- const bitLengths = new Map([
4
- ['A128CBC-HS256', 256],
5
- ['A128GCM', 128],
6
- ['A192CBC-HS384', 384],
7
- ['A192GCM', 192],
8
- ['A256CBC-HS512', 512],
9
- ['A256GCM', 256],
10
- ]);
11
- const generateCek = (alg) => {
12
- const bitLength = bitLengths.get(alg);
13
- if (!bitLength) {
14
- throw new JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
3
+ export function bitLength(alg) {
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
+ case 'A128GCM':
12
+ return 128;
13
+ case 'A192GCM':
14
+ return 192;
15
+ case 'A256GCM':
16
+ return 256;
17
+ default:
18
+ throw new JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
15
19
  }
16
- return random(new Uint8Array(bitLength >> 3));
17
- };
18
- export default generateCek;
19
- export { bitLengths };
20
+ }
21
+ export default (alg) => random(new Uint8Array(bitLength(alg) >> 3));
@@ -1,7 +1,7 @@
1
1
  import { JWEInvalid } from '../util/errors.js';
2
- import { bitLengths } from './iv.js';
2
+ import { bitLength } from './iv.js';
3
3
  const checkIvLength = (enc, iv) => {
4
- if (iv.length << 3 !== bitLengths.get(enc)) {
4
+ if (iv.length << 3 !== bitLength(enc)) {
5
5
  throw new JWEInvalid('Invalid Initialization Vector length');
6
6
  }
7
7
  };
@@ -0,0 +1,160 @@
1
+ import { isCloudflareWorkers, isNodeJs } from '../runtime/global.js';
2
+ function getHashLength(hash) {
3
+ return parseInt(hash === null || hash === void 0 ? void 0 : hash.name.substr(4), 10);
4
+ }
5
+ function getNamedCurve(alg) {
6
+ switch (alg) {
7
+ case 'ES256':
8
+ return 'P-256';
9
+ case 'ES384':
10
+ return 'P-384';
11
+ case 'ES512':
12
+ return 'P-521';
13
+ }
14
+ }
15
+ function checkUsage(key, usages) {
16
+ if (usages.length && !usages.some((expected) => key.usages.includes(expected))) {
17
+ let msg = 'CryptoKey does not support this operation, its usages must include ';
18
+ if (usages.length > 2) {
19
+ const last = usages.pop();
20
+ msg += `one of ${usages.join(', ')}, or ${last}.`;
21
+ }
22
+ else if (usages.length === 2) {
23
+ msg += `one of ${usages[0]} or ${usages[1]}.`;
24
+ }
25
+ else {
26
+ msg += `${usages[0]}.`;
27
+ }
28
+ throw new TypeError(msg);
29
+ }
30
+ }
31
+ export function checkSigCryptoKey(key, alg, ...usages) {
32
+ switch (alg) {
33
+ case 'HS256':
34
+ case 'HS384':
35
+ case 'HS512': {
36
+ if (key.algorithm.name !== 'HMAC') {
37
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be HMAC.`);
38
+ }
39
+ const expected = parseInt(alg.substr(2), 10);
40
+ const actual = getHashLength(key.algorithm.hash);
41
+ if (actual !== expected) {
42
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.hash must be SHA-${expected}.`);
43
+ }
44
+ break;
45
+ }
46
+ case 'RS256':
47
+ case 'RS384':
48
+ case 'RS512': {
49
+ if (key.algorithm.name !== 'RSASSA-PKCS1-v1_5') {
50
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be RSASSA-PKCS1-v1_5.`);
51
+ }
52
+ const expected = parseInt(alg.substr(2), 10);
53
+ const actual = getHashLength(key.algorithm.hash);
54
+ if (actual !== expected) {
55
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.hash must be SHA-${expected}.`);
56
+ }
57
+ break;
58
+ }
59
+ case 'PS256':
60
+ case 'PS384':
61
+ case 'PS512': {
62
+ if (key.algorithm.name !== 'RSA-PSS') {
63
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be RSA-PSS.`);
64
+ }
65
+ const expected = parseInt(alg.substr(2), 10);
66
+ const actual = getHashLength(key.algorithm.hash);
67
+ if (actual !== expected) {
68
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.hash must be SHA-${expected}.`);
69
+ }
70
+ break;
71
+ }
72
+ case isNodeJs() && 'EdDSA': {
73
+ if (key.algorithm.name !== 'NODE-ED25519' && key.algorithm.name !== 'NODE-ED448') {
74
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be NODE-ED25519 or NODE-ED448.`);
75
+ }
76
+ break;
77
+ }
78
+ case isCloudflareWorkers() && 'EdDSA': {
79
+ if (key.algorithm.name !== 'NODE-ED25519') {
80
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be NODE-ED25519.`);
81
+ }
82
+ break;
83
+ }
84
+ case 'ES256':
85
+ case 'ES384':
86
+ case 'ES512': {
87
+ if (key.algorithm.name !== 'ECDSA') {
88
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be ECDSA.`);
89
+ }
90
+ const expected = getNamedCurve(alg);
91
+ const actual = key.algorithm.namedCurve;
92
+ if (actual !== expected) {
93
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.namedCurve must be ${expected}.`);
94
+ }
95
+ break;
96
+ }
97
+ default:
98
+ throw new TypeError('CryptoKey does not support this operation');
99
+ }
100
+ checkUsage(key, usages);
101
+ }
102
+ export function checkEncCryptoKey(key, alg, ...usages) {
103
+ switch (alg) {
104
+ case 'A128GCM':
105
+ case 'A192GCM':
106
+ case 'A256GCM': {
107
+ if (key.algorithm.name !== 'AES-GCM') {
108
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be AES-GCM.`);
109
+ }
110
+ const expected = parseInt(alg.substr(1, 3), 10);
111
+ const actual = key.algorithm.length;
112
+ if (actual !== expected) {
113
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.length must be ${expected}.`);
114
+ }
115
+ break;
116
+ }
117
+ case 'A128KW':
118
+ case 'A192KW':
119
+ case 'A256KW': {
120
+ if (key.algorithm.name !== 'AES-KW') {
121
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be AES-KW.`);
122
+ }
123
+ const expected = parseInt(alg.substr(1, 3), 10);
124
+ const actual = key.algorithm.length;
125
+ if (actual !== expected) {
126
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.length must be ${expected}.`);
127
+ }
128
+ break;
129
+ }
130
+ case 'ECDH-ES':
131
+ if (key.algorithm.name !== 'ECDH') {
132
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be ECDH.`);
133
+ }
134
+ break;
135
+ case 'PBES2-HS256+A128KW':
136
+ case 'PBES2-HS384+A192KW':
137
+ case 'PBES2-HS512+A256KW':
138
+ if (key.algorithm.name !== 'PBKDF2') {
139
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be PBKDF2.`);
140
+ }
141
+ break;
142
+ case 'RSA-OAEP':
143
+ case 'RSA-OAEP-256':
144
+ case 'RSA-OAEP-384':
145
+ case 'RSA-OAEP-512': {
146
+ if (key.algorithm.name !== 'RSA-OAEP') {
147
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be RSA-OAEP.`);
148
+ }
149
+ const expected = parseInt(alg.substr(9), 10) || 1;
150
+ const actual = getHashLength(key.algorithm.hash);
151
+ if (actual !== expected) {
152
+ throw new TypeError(`CryptoKey does not support this operation, its algorithm.hash must be SHA-${expected}.`);
153
+ }
154
+ break;
155
+ }
156
+ default:
157
+ throw new TypeError('CryptoKey does not support this operation');
158
+ }
159
+ checkUsage(key, usages);
160
+ }
@@ -5,7 +5,7 @@ import { decrypt as rsaEs } from '../runtime/rsaes.js';
5
5
  import { unwrap as aesGcmKw } from '../runtime/aesgcmkw.js';
6
6
  import { decode as base64url } from '../runtime/base64url.js';
7
7
  import { JOSENotSupported, JWEInvalid } from '../util/errors.js';
8
- import { bitLengths as cekLengths } from '../lib/cek.js';
8
+ import { bitLength as cekLength } from '../lib/cek.js';
9
9
  import { importJWK } from '../key/import.js';
10
10
  import checkKeyType from './check_key_type.js';
11
11
  import isObject from './is_object.js';
@@ -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) || cekLengths.get(joseHeader.enc), partyUInfo, partyVInfo);
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);
44
44
  if (alg === 'ECDH-ES')
45
45
  return sharedSecret;
46
46
  if (encryptedKey === undefined)
@@ -4,7 +4,7 @@ import { encrypt as pbes2Kw } from '../runtime/pbes2kw.js';
4
4
  import { encrypt as rsaEs } from '../runtime/rsaes.js';
5
5
  import { wrap as aesGcmKw } from '../runtime/aesgcmkw.js';
6
6
  import { encode as base64url } from '../runtime/base64url.js';
7
- import generateCek, { bitLengths as cekLengths } from '../lib/cek.js';
7
+ import generateCek, { bitLength as cekLength } from '../lib/cek.js';
8
8
  import { JOSENotSupported } from '../util/errors.js';
9
9
  import { exportJWK } from '../key/export.js';
10
10
  import checkKeyType from './check_key_type.js';
@@ -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) || cekLengths.get(enc), apu, apv);
32
+ const sharedSecret = await ECDH.deriveKey(key, ephemeralKey, alg === 'ECDH-ES' ? enc : alg, parseInt(alg.substr(-5, 3), 10) || cekLength(enc), apu, apv);
33
33
  parameters = { epk: { x, y, crv, kty } };
34
34
  if (apu)
35
35
  parameters.apu = base64url(apu);
@@ -1,22 +1,27 @@
1
1
  import { JOSENotSupported } from '../util/errors.js';
2
2
  import random from '../runtime/random.js';
3
- const bitLengths = new Map([
4
- ['A128CBC-HS256', 128],
5
- ['A128GCM', 96],
6
- ['A128GCMKW', 96],
7
- ['A192CBC-HS384', 128],
8
- ['A192GCM', 96],
9
- ['A192GCMKW', 96],
10
- ['A256CBC-HS512', 128],
11
- ['A256GCM', 96],
12
- ['A256GCMKW', 96],
13
- ]);
14
- const generateIv = (alg) => {
15
- const bitLength = bitLengths.get(alg);
16
- if (!bitLength) {
17
- throw new JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
3
+ export function bitLength(alg) {
4
+ switch (alg) {
5
+ case 'A128CBC-HS256':
6
+ return 128;
7
+ case 'A128GCM':
8
+ return 96;
9
+ case 'A128GCMKW':
10
+ return 96;
11
+ case 'A192CBC-HS384':
12
+ return 128;
13
+ case 'A192GCM':
14
+ return 96;
15
+ case 'A192GCMKW':
16
+ return 96;
17
+ case 'A256CBC-HS512':
18
+ return 128;
19
+ case 'A256GCM':
20
+ return 96;
21
+ case 'A256GCMKW':
22
+ return 96;
23
+ default:
24
+ throw new JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
18
25
  }
19
- return random(new Uint8Array(bitLength >> 3));
20
- };
21
- export default generateIv;
22
- export { bitLengths };
26
+ }
27
+ export default (alg) => random(new Uint8Array(bitLength(alg) >> 3));
@@ -1,9 +1,10 @@
1
1
  import { Buffer } from 'buffer';
2
- import { createDecipheriv, createCipheriv } from 'crypto';
2
+ import { KeyObject, createDecipheriv, createCipheriv } from 'crypto';
3
3
  import { JOSENotSupported } from '../util/errors.js';
4
4
  import { concat } from '../lib/buffer_utils.js';
5
5
  import getSecretKey from './secret_key.js';
6
- import { isCryptoKey, getKeyObject } from './webcrypto.js';
6
+ import { isCryptoKey } from './webcrypto.js';
7
+ import { checkEncCryptoKey } from '../lib/crypto_key.js';
7
8
  import isKeyObject from './is_key_object.js';
8
9
  import invalidKeyInput from './invalid_key_input.js';
9
10
  import supported from './ciphers.js';
@@ -20,7 +21,8 @@ function ensureKeyObject(key, alg, usage) {
20
21
  return getSecretKey(key);
21
22
  }
22
23
  if (isCryptoKey(key)) {
23
- return getKeyObject(key, alg, new Set([usage]));
24
+ checkEncCryptoKey(key, alg, usage);
25
+ return KeyObject.from(key);
24
26
  }
25
27
  throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey', 'Uint8Array'));
26
28
  }
@@ -1,6 +1,6 @@
1
- import { createPrivateKey, createPublicKey } from 'crypto';
1
+ import { createPrivateKey, createPublicKey, KeyObject } from 'crypto';
2
2
  import { Buffer } from 'buffer';
3
- import { isCryptoKey, getKeyObject } from './webcrypto.js';
3
+ import { isCryptoKey } from './webcrypto.js';
4
4
  import isKeyObject from './is_key_object.js';
5
5
  import invalidKeyInput from './invalid_key_input.js';
6
6
  const genericExport = (keyType, keyFormat, key) => {
@@ -9,7 +9,7 @@ const genericExport = (keyType, keyFormat, key) => {
9
9
  if (!key.extractable) {
10
10
  throw new TypeError('CryptoKey is not extractable');
11
11
  }
12
- keyObject = getKeyObject(key);
12
+ keyObject = KeyObject.from(key);
13
13
  }
14
14
  else if (isKeyObject(key)) {
15
15
  keyObject = key;
@@ -1,11 +1,12 @@
1
- import { createDecipheriv } from 'crypto';
1
+ import { createDecipheriv, KeyObject } from 'crypto';
2
2
  import checkIvLength from '../lib/check_iv_length.js';
3
3
  import checkCekLength from './check_cek_length.js';
4
4
  import { concat } from '../lib/buffer_utils.js';
5
5
  import { JOSENotSupported, JWEDecryptionFailed } from '../util/errors.js';
6
6
  import timingSafeEqual from './timing_safe_equal.js';
7
7
  import cbcTag from './cbc_tag.js';
8
- import { isCryptoKey, getKeyObject } from './webcrypto.js';
8
+ import { isCryptoKey } from './webcrypto.js';
9
+ import { checkEncCryptoKey } from '../lib/crypto_key.js';
9
10
  import isKeyObject from './is_key_object.js';
10
11
  import invalidKeyInput from './invalid_key_input.js';
11
12
  import supported from './ciphers.js';
@@ -64,7 +65,8 @@ async function gcmDecrypt(enc, cek, ciphertext, iv, tag, aad) {
64
65
  const decrypt = async (enc, cek, ciphertext, iv, tag, aad) => {
65
66
  let key;
66
67
  if (isCryptoKey(cek)) {
67
- key = getKeyObject(cek, enc, new Set(['decrypt']));
68
+ checkEncCryptoKey(cek, enc, 'decrypt');
69
+ key = KeyObject.from(cek);
68
70
  }
69
71
  else if (cek instanceof Uint8Array || isKeyObject(cek)) {
70
72
  key = cek;
@@ -1,17 +1,19 @@
1
- import { diffieHellman, generateKeyPair as generateKeyPairCb } from 'crypto';
1
+ import { diffieHellman, generateKeyPair as generateKeyPairCb, KeyObject } from 'crypto';
2
2
  import { promisify } from 'util';
3
3
  import getNamedCurve from './get_named_curve.js';
4
4
  import { encoder, concat, uint32be, lengthAndInput, concatKdf } from '../lib/buffer_utils.js';
5
5
  import digest from './digest.js';
6
6
  import { JOSENotSupported } from '../util/errors.js';
7
- import { isCryptoKey, getKeyObject } from './webcrypto.js';
7
+ import { isCryptoKey } from './webcrypto.js';
8
+ import { checkEncCryptoKey } from '../lib/crypto_key.js';
8
9
  import isKeyObject from './is_key_object.js';
9
10
  import invalidKeyInput from './invalid_key_input.js';
10
11
  const generateKeyPair = promisify(generateKeyPairCb);
11
12
  export const deriveKey = async (publicKee, privateKee, algorithm, keyLength, apu = new Uint8Array(0), apv = new Uint8Array(0)) => {
12
13
  let publicKey;
13
14
  if (isCryptoKey(publicKee)) {
14
- publicKey = getKeyObject(publicKee, 'ECDH-ES');
15
+ checkEncCryptoKey(publicKee, 'ECDH-ES');
16
+ publicKey = KeyObject.from(publicKee);
15
17
  }
16
18
  else if (isKeyObject(publicKee)) {
17
19
  publicKey = publicKee;
@@ -21,7 +23,8 @@ export const deriveKey = async (publicKee, privateKee, algorithm, keyLength, apu
21
23
  }
22
24
  let privateKey;
23
25
  if (isCryptoKey(privateKee)) {
24
- privateKey = getKeyObject(privateKee, 'ECDH-ES', new Set(['deriveBits', 'deriveKey']));
26
+ checkEncCryptoKey(privateKee, 'ECDH-ES', 'deriveBits', 'deriveKey');
27
+ privateKey = KeyObject.from(privateKee);
25
28
  }
26
29
  else if (isKeyObject(privateKee)) {
27
30
  privateKey = privateKee;
@@ -36,7 +39,7 @@ export const deriveKey = async (publicKee, privateKee, algorithm, keyLength, apu
36
39
  export const generateEpk = async (kee) => {
37
40
  let key;
38
41
  if (isCryptoKey(kee)) {
39
- key = getKeyObject(kee);
42
+ key = KeyObject.from(kee);
40
43
  }
41
44
  else if (isKeyObject(kee)) {
42
45
  key = kee;
@@ -1,9 +1,10 @@
1
- import { createCipheriv } from 'crypto';
1
+ import { createCipheriv, KeyObject } from 'crypto';
2
2
  import checkIvLength from '../lib/check_iv_length.js';
3
3
  import checkCekLength from './check_cek_length.js';
4
4
  import { concat } from '../lib/buffer_utils.js';
5
5
  import cbcTag from './cbc_tag.js';
6
- import { isCryptoKey, getKeyObject } from './webcrypto.js';
6
+ import { isCryptoKey } from './webcrypto.js';
7
+ import { checkEncCryptoKey } from '../lib/crypto_key.js';
7
8
  import isKeyObject from './is_key_object.js';
8
9
  import invalidKeyInput from './invalid_key_input.js';
9
10
  import { JOSENotSupported } from '../util/errors.js';
@@ -42,7 +43,8 @@ async function gcmEncrypt(enc, plaintext, cek, iv, aad) {
42
43
  const encrypt = async (enc, plaintext, cek, iv, aad) => {
43
44
  let key;
44
45
  if (isCryptoKey(cek)) {
45
- key = getKeyObject(cek, enc, new Set(['encrypt']));
46
+ checkEncCryptoKey(cek, enc, 'encrypt');
47
+ key = KeyObject.from(cek);
46
48
  }
47
49
  else if (cek instanceof Uint8Array || isKeyObject(cek)) {
48
50
  key = cek;
@@ -1,18 +1,22 @@
1
- import { get as http } from 'http';
2
- import { get as https } from 'https';
1
+ import * as http from 'http';
2
+ import * as https from 'https';
3
3
  import { once } from 'events';
4
4
  import { JOSEError, JWKSTimeout } from '../util/errors.js';
5
5
  import { concat, decoder } from '../lib/buffer_utils.js';
6
- const protocols = {
7
- 'https:': https,
8
- 'http:': http,
9
- };
10
6
  const fetchJwks = async (url, timeout, options) => {
11
- if (protocols[url.protocol] === undefined) {
12
- throw new TypeError('Unsupported URL protocol.');
7
+ let get;
8
+ switch (url.protocol) {
9
+ case 'https:':
10
+ get = https.get;
11
+ break;
12
+ case 'http:':
13
+ get = http.get;
14
+ break;
15
+ default:
16
+ throw new TypeError('Unsupported URL protocol.');
13
17
  }
14
18
  const { agent } = options;
15
- const req = protocols[url.protocol](url.href, {
19
+ const req = get(url.href, {
16
20
  agent,
17
21
  timeout,
18
22
  });
@@ -1,7 +1,7 @@
1
1
  import { Buffer } from 'buffer';
2
- import { createPublicKey } from 'crypto';
2
+ import { createPublicKey, KeyObject } from 'crypto';
3
3
  import { JOSENotSupported } from '../util/errors.js';
4
- import { isCryptoKey, getKeyObject } from './webcrypto.js';
4
+ import { isCryptoKey } from './webcrypto.js';
5
5
  import isKeyObject from './is_key_object.js';
6
6
  import invalidKeyInput from './invalid_key_input.js';
7
7
  const p256 = Buffer.from([42, 134, 72, 206, 61, 3, 1, 7]);
@@ -27,7 +27,7 @@ const getNamedCurve = (kee, raw) => {
27
27
  var _a;
28
28
  let key;
29
29
  if (isCryptoKey(kee)) {
30
- key = getKeyObject(kee);
30
+ key = KeyObject.from(kee);
31
31
  }
32
32
  else if (isKeyObject(kee)) {
33
33
  key = kee;
@@ -1,5 +1,6 @@
1
- import * as crypto from 'crypto';
2
- import { isCryptoKey, getKeyObject } from './webcrypto.js';
1
+ import { KeyObject } from 'crypto';
2
+ import { isCryptoKey } from './webcrypto.js';
3
+ import { checkSigCryptoKey } from '../lib/crypto_key.js';
3
4
  import getSecretKey from './secret_key.js';
4
5
  import invalidKeyInput from './invalid_key_input.js';
5
6
  export default function getSignVerifyKey(alg, key, usage) {
@@ -9,11 +10,12 @@ export default function getSignVerifyKey(alg, key, usage) {
9
10
  }
10
11
  return getSecretKey(key);
11
12
  }
12
- if (key instanceof crypto.KeyObject) {
13
+ if (key instanceof KeyObject) {
13
14
  return key;
14
15
  }
15
16
  if (isCryptoKey(key)) {
16
- return getKeyObject(key, alg, new Set([usage]));
17
+ checkSigCryptoKey(key, alg, usage);
18
+ return KeyObject.from(key);
17
19
  }
18
20
  throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey', 'Uint8Array'));
19
21
  }
@@ -0,0 +1,6 @@
1
+ export function isCloudflareWorkers() {
2
+ return false;
3
+ }
4
+ export function isNodeJs() {
5
+ return true;
6
+ }
@@ -1,9 +1,9 @@
1
- import { createPublicKey } from 'crypto';
1
+ import { KeyObject, createPublicKey } from 'crypto';
2
2
  import { encode as base64url } from './base64url.js';
3
3
  import Asn1SequenceDecoder from './asn1_sequence_decoder.js';
4
4
  import { JOSENotSupported } from '../util/errors.js';
5
5
  import getNamedCurve from './get_named_curve.js';
6
- import { isCryptoKey, getKeyObject } from './webcrypto.js';
6
+ import { isCryptoKey } from './webcrypto.js';
7
7
  import isKeyObject from './is_key_object.js';
8
8
  import invalidKeyInput from './invalid_key_input.js';
9
9
  const [major, minor] = process.version
@@ -17,7 +17,7 @@ const keyToJWK = (key) => {
17
17
  if (!key.extractable) {
18
18
  throw new TypeError('CryptoKey is not extractable');
19
19
  }
20
- keyObject = getKeyObject(key);
20
+ keyObject = KeyObject.from(key);
21
21
  }
22
22
  else if (isKeyObject(key)) {
23
23
  keyObject = key;
@@ -1,11 +1,12 @@
1
1
  import { promisify } from 'util';
2
- import { pbkdf2 as pbkdf2cb } from 'crypto';
2
+ import { KeyObject, pbkdf2 as pbkdf2cb } from 'crypto';
3
3
  import random from './random.js';
4
4
  import { p2s as concatSalt } from '../lib/buffer_utils.js';
5
5
  import { encode as base64url } from './base64url.js';
6
6
  import { wrap, unwrap } from './aeskw.js';
7
7
  import checkP2s from '../lib/check_p2s.js';
8
- import { isCryptoKey, getKeyObject } from './webcrypto.js';
8
+ import { isCryptoKey } from './webcrypto.js';
9
+ import { checkEncCryptoKey } from '../lib/crypto_key.js';
9
10
  import isKeyObject from './is_key_object.js';
10
11
  import invalidKeyInput from './invalid_key_input.js';
11
12
  const pbkdf2 = promisify(pbkdf2cb);
@@ -17,7 +18,8 @@ function getPassword(key, alg) {
17
18
  return key;
18
19
  }
19
20
  if (isCryptoKey(key)) {
20
- return getKeyObject(key, alg, new Set(['deriveBits', 'deriveKey'])).export();
21
+ checkEncCryptoKey(key, alg, 'deriveBits', 'deriveKey');
22
+ return KeyObject.from(key).export();
21
23
  }
22
24
  throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey', 'Uint8Array'));
23
25
  }
@@ -1,6 +1,7 @@
1
- import { publicEncrypt, constants, privateDecrypt } from 'crypto';
1
+ import { KeyObject, publicEncrypt, constants, privateDecrypt } from 'crypto';
2
2
  import checkModulusLength from './check_modulus_length.js';
3
- import { isCryptoKey, getKeyObject } from './webcrypto.js';
3
+ import { isCryptoKey } from './webcrypto.js';
4
+ import { checkEncCryptoKey } from '../lib/crypto_key.js';
4
5
  import isKeyObject from './is_key_object.js';
5
6
  import invalidKeyInput from './invalid_key_input.js';
6
7
  const checkKey = (key, alg) => {
@@ -41,7 +42,8 @@ function ensureKeyObject(key, alg, ...usages) {
41
42
  return key;
42
43
  }
43
44
  if (isCryptoKey(key)) {
44
- return getKeyObject(key, alg, new Set(usages));
45
+ checkEncCryptoKey(key, alg, ...usages);
46
+ return KeyObject.from(key);
45
47
  }
46
48
  throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey'));
47
49
  }