jose 4.0.3 → 4.1.2
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/{runtime → lib}/aesgcmkw.js +8 -8
- package/dist/browser/lib/cek.js +18 -16
- package/dist/browser/lib/check_iv_length.js +2 -2
- package/dist/browser/lib/check_key_type.js +1 -1
- package/dist/browser/lib/crypto_key.js +150 -0
- package/dist/browser/lib/decrypt_key_management.js +3 -3
- package/dist/browser/lib/encrypt_key_management.js +3 -3
- package/dist/browser/{runtime → lib}/invalid_key_input.js +0 -0
- package/dist/browser/lib/iv.js +24 -19
- package/dist/browser/runtime/aeskw.js +4 -3
- package/dist/browser/runtime/asn1.js +4 -4
- package/dist/browser/runtime/bogus.js +1 -1
- package/dist/browser/runtime/check_cek_length.js +7 -0
- package/dist/browser/runtime/decrypt.js +10 -4
- package/dist/browser/runtime/ecdhes.js +5 -4
- package/dist/browser/runtime/encrypt.js +10 -4
- package/dist/browser/runtime/generate.js +4 -4
- package/dist/browser/runtime/get_sign_verify_key.js +5 -4
- package/dist/browser/runtime/jwk_to_key.js +4 -4
- package/dist/browser/runtime/key_to_jwk.js +1 -1
- package/dist/browser/runtime/pbes2kw.js +14 -36
- package/dist/browser/runtime/rsaes.js +5 -4
- package/dist/browser/runtime/subtle_dsa.js +7 -26
- package/dist/browser/runtime/webcrypto.js +0 -145
- package/dist/node/cjs/{runtime → lib}/aesgcmkw.js +8 -8
- package/dist/node/cjs/lib/cek.js +20 -17
- package/dist/node/cjs/lib/check_iv_length.js +1 -1
- package/dist/node/cjs/lib/check_key_type.js +1 -1
- package/dist/node/cjs/lib/crypto_key.js +155 -0
- package/dist/node/cjs/lib/decrypt_key_management.js +2 -2
- package/dist/node/cjs/lib/encrypt_key_management.js +2 -2
- package/dist/node/cjs/{runtime → lib}/invalid_key_input.js +0 -11
- package/dist/node/cjs/lib/iv.js +26 -20
- package/dist/node/cjs/runtime/aeskw.js +4 -2
- package/dist/node/cjs/runtime/asn1.js +2 -2
- package/dist/node/cjs/runtime/decrypt.js +4 -2
- package/dist/node/cjs/runtime/ecdhes.js +7 -4
- package/dist/node/cjs/runtime/encrypt.js +4 -2
- package/dist/node/cjs/runtime/get_named_curve.js +2 -2
- package/dist/node/cjs/runtime/get_sign_verify_key.js +6 -4
- package/dist/node/cjs/runtime/global.js +11 -0
- package/dist/node/cjs/runtime/key_to_jwk.js +2 -2
- package/dist/node/cjs/runtime/node_key.js +6 -10
- package/dist/node/cjs/runtime/pbes2kw.js +4 -2
- package/dist/node/cjs/runtime/rsaes.js +4 -2
- package/dist/node/cjs/runtime/webcrypto.js +1 -152
- package/dist/node/esm/{runtime → lib}/aesgcmkw.js +8 -8
- package/dist/node/esm/lib/cek.js +18 -16
- package/dist/node/esm/lib/check_iv_length.js +2 -2
- package/dist/node/esm/lib/check_key_type.js +1 -1
- package/dist/node/esm/lib/crypto_key.js +150 -0
- package/dist/node/esm/lib/decrypt_key_management.js +3 -3
- package/dist/node/esm/lib/encrypt_key_management.js +3 -3
- package/dist/node/esm/{runtime → lib}/invalid_key_input.js +0 -11
- package/dist/node/esm/lib/iv.js +24 -19
- package/dist/node/esm/runtime/aeskw.js +6 -4
- package/dist/node/esm/runtime/asn1.js +4 -4
- package/dist/node/esm/runtime/decrypt.js +6 -4
- package/dist/node/esm/runtime/ecdhes.js +9 -6
- package/dist/node/esm/runtime/encrypt.js +6 -4
- package/dist/node/esm/runtime/get_named_curve.js +4 -4
- package/dist/node/esm/runtime/get_sign_verify_key.js +7 -5
- package/dist/node/esm/runtime/global.js +6 -0
- package/dist/node/esm/runtime/key_to_jwk.js +4 -4
- package/dist/node/esm/runtime/node_key.js +6 -10
- package/dist/node/esm/runtime/pbes2kw.js +6 -4
- package/dist/node/esm/runtime/rsaes.js +6 -4
- package/dist/node/esm/runtime/webcrypto.js +0 -149
- package/dist/types/jwks/remote.d.ts +4 -0
- package/package.json +4 -1
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { isCloudflareWorkers, isNodeJs } from '../runtime/global.js';
|
|
2
|
+
function unusable(name, prop = 'algorithm.name') {
|
|
3
|
+
return new TypeError(`CryptoKey does not support this operation, its ${prop} must be ${name}`);
|
|
4
|
+
}
|
|
5
|
+
function isAlgorithm(algorithm, name) {
|
|
6
|
+
return algorithm.name === name;
|
|
7
|
+
}
|
|
8
|
+
function getHashLength(hash) {
|
|
9
|
+
return parseInt(hash.name.substr(4), 10);
|
|
10
|
+
}
|
|
11
|
+
function getNamedCurve(alg) {
|
|
12
|
+
switch (alg) {
|
|
13
|
+
case 'ES256':
|
|
14
|
+
return 'P-256';
|
|
15
|
+
case 'ES384':
|
|
16
|
+
return 'P-384';
|
|
17
|
+
case 'ES512':
|
|
18
|
+
return 'P-521';
|
|
19
|
+
default:
|
|
20
|
+
throw new Error('unreachable');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function checkUsage(key, usages) {
|
|
24
|
+
if (usages.length && !usages.some((expected) => key.usages.includes(expected))) {
|
|
25
|
+
let msg = 'CryptoKey does not support this operation, its usages must include ';
|
|
26
|
+
if (usages.length > 2) {
|
|
27
|
+
const last = usages.pop();
|
|
28
|
+
msg += `one of ${usages.join(', ')}, or ${last}.`;
|
|
29
|
+
}
|
|
30
|
+
else if (usages.length === 2) {
|
|
31
|
+
msg += `one of ${usages[0]} or ${usages[1]}.`;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
msg += `${usages[0]}.`;
|
|
35
|
+
}
|
|
36
|
+
throw new TypeError(msg);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export function checkSigCryptoKey(key, alg, ...usages) {
|
|
40
|
+
switch (alg) {
|
|
41
|
+
case 'HS256':
|
|
42
|
+
case 'HS384':
|
|
43
|
+
case 'HS512': {
|
|
44
|
+
if (!isAlgorithm(key.algorithm, 'HMAC'))
|
|
45
|
+
throw unusable('HMAC');
|
|
46
|
+
const expected = parseInt(alg.substr(2), 10);
|
|
47
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
48
|
+
if (actual !== expected)
|
|
49
|
+
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
case 'RS256':
|
|
53
|
+
case 'RS384':
|
|
54
|
+
case 'RS512': {
|
|
55
|
+
if (!isAlgorithm(key.algorithm, 'RSASSA-PKCS1-v1_5'))
|
|
56
|
+
throw unusable('RSASSA-PKCS1-v1_5');
|
|
57
|
+
const expected = parseInt(alg.substr(2), 10);
|
|
58
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
59
|
+
if (actual !== expected)
|
|
60
|
+
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
case 'PS256':
|
|
64
|
+
case 'PS384':
|
|
65
|
+
case 'PS512': {
|
|
66
|
+
if (!isAlgorithm(key.algorithm, 'RSA-PSS'))
|
|
67
|
+
throw unusable('RSA-PSS');
|
|
68
|
+
const expected = parseInt(alg.substr(2), 10);
|
|
69
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
70
|
+
if (actual !== expected)
|
|
71
|
+
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
case isNodeJs() && 'EdDSA': {
|
|
75
|
+
if (key.algorithm.name !== 'NODE-ED25519' && key.algorithm.name !== 'NODE-ED448')
|
|
76
|
+
throw unusable('NODE-ED25519 or NODE-ED448');
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
case isCloudflareWorkers() && 'EdDSA': {
|
|
80
|
+
if (!isAlgorithm(key.algorithm, 'NODE-ED25519'))
|
|
81
|
+
throw unusable('NODE-ED25519');
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
case 'ES256':
|
|
85
|
+
case 'ES384':
|
|
86
|
+
case 'ES512': {
|
|
87
|
+
if (!isAlgorithm(key.algorithm, 'ECDSA'))
|
|
88
|
+
throw unusable('ECDSA');
|
|
89
|
+
const expected = getNamedCurve(alg);
|
|
90
|
+
const actual = key.algorithm.namedCurve;
|
|
91
|
+
if (actual !== expected)
|
|
92
|
+
throw unusable(expected, 'algorithm.namedCurve');
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
default:
|
|
96
|
+
throw new TypeError('CryptoKey does not support this operation');
|
|
97
|
+
}
|
|
98
|
+
checkUsage(key, usages);
|
|
99
|
+
}
|
|
100
|
+
export function checkEncCryptoKey(key, alg, ...usages) {
|
|
101
|
+
switch (alg) {
|
|
102
|
+
case 'A128GCM':
|
|
103
|
+
case 'A192GCM':
|
|
104
|
+
case 'A256GCM': {
|
|
105
|
+
if (!isAlgorithm(key.algorithm, 'AES-GCM'))
|
|
106
|
+
throw unusable('AES-GCM');
|
|
107
|
+
const expected = parseInt(alg.substr(1, 3), 10);
|
|
108
|
+
const actual = key.algorithm.length;
|
|
109
|
+
if (actual !== expected)
|
|
110
|
+
throw unusable(expected, 'algorithm.length');
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case 'A128KW':
|
|
114
|
+
case 'A192KW':
|
|
115
|
+
case 'A256KW': {
|
|
116
|
+
if (!isAlgorithm(key.algorithm, 'AES-KW'))
|
|
117
|
+
throw unusable('AES-KW');
|
|
118
|
+
const expected = parseInt(alg.substr(1, 3), 10);
|
|
119
|
+
const actual = key.algorithm.length;
|
|
120
|
+
if (actual !== expected)
|
|
121
|
+
throw unusable(expected, 'algorithm.length');
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
case 'ECDH-ES':
|
|
125
|
+
if (!isAlgorithm(key.algorithm, 'ECDH'))
|
|
126
|
+
throw unusable('ECDH');
|
|
127
|
+
break;
|
|
128
|
+
case 'PBES2-HS256+A128KW':
|
|
129
|
+
case 'PBES2-HS384+A192KW':
|
|
130
|
+
case 'PBES2-HS512+A256KW':
|
|
131
|
+
if (!isAlgorithm(key.algorithm, 'PBKDF2'))
|
|
132
|
+
throw unusable('PBKDF2');
|
|
133
|
+
break;
|
|
134
|
+
case 'RSA-OAEP':
|
|
135
|
+
case 'RSA-OAEP-256':
|
|
136
|
+
case 'RSA-OAEP-384':
|
|
137
|
+
case 'RSA-OAEP-512': {
|
|
138
|
+
if (!isAlgorithm(key.algorithm, 'RSA-OAEP'))
|
|
139
|
+
throw unusable('RSA-OAEP');
|
|
140
|
+
const expected = parseInt(alg.substr(9), 10) || 1;
|
|
141
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
142
|
+
if (actual !== expected)
|
|
143
|
+
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
default:
|
|
147
|
+
throw new TypeError('CryptoKey does not support this operation');
|
|
148
|
+
}
|
|
149
|
+
checkUsage(key, usages);
|
|
150
|
+
}
|
|
@@ -2,13 +2,13 @@ import { unwrap as aesKw } from '../runtime/aeskw.js';
|
|
|
2
2
|
import * as ECDH from '../runtime/ecdhes.js';
|
|
3
3
|
import { decrypt as pbes2Kw } from '../runtime/pbes2kw.js';
|
|
4
4
|
import { decrypt as rsaEs } from '../runtime/rsaes.js';
|
|
5
|
-
import { unwrap as aesGcmKw } from '../runtime/aesgcmkw.js';
|
|
6
5
|
import { decode as base64url } from '../runtime/base64url.js';
|
|
7
6
|
import { JOSENotSupported, JWEInvalid } from '../util/errors.js';
|
|
8
|
-
import {
|
|
7
|
+
import { bitLength as cekLength } from '../lib/cek.js';
|
|
9
8
|
import { importJWK } from '../key/import.js';
|
|
10
9
|
import checkKeyType from './check_key_type.js';
|
|
11
10
|
import isObject from './is_object.js';
|
|
11
|
+
import { unwrap as aesGcmKw } from './aesgcmkw.js';
|
|
12
12
|
async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
|
|
13
13
|
checkKeyType(alg, key, 'decrypt');
|
|
14
14
|
switch (alg) {
|
|
@@ -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) ||
|
|
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)
|
|
@@ -2,12 +2,12 @@ import { wrap as aesKw } from '../runtime/aeskw.js';
|
|
|
2
2
|
import * as ECDH from '../runtime/ecdhes.js';
|
|
3
3
|
import { encrypt as pbes2Kw } from '../runtime/pbes2kw.js';
|
|
4
4
|
import { encrypt as rsaEs } from '../runtime/rsaes.js';
|
|
5
|
-
import { wrap as aesGcmKw } from '../runtime/aesgcmkw.js';
|
|
6
5
|
import { encode as base64url } from '../runtime/base64url.js';
|
|
7
|
-
import generateCek, {
|
|
6
|
+
import generateCek, { bitLength as cekLength } from '../lib/cek.js';
|
|
8
7
|
import { JOSENotSupported } from '../util/errors.js';
|
|
9
8
|
import { exportJWK } from '../key/export.js';
|
|
10
9
|
import checkKeyType from './check_key_type.js';
|
|
10
|
+
import { wrap as aesGcmKw } from './aesgcmkw.js';
|
|
11
11
|
async function encryptKeyManagement(alg, enc, key, providedCek, providedParameters = {}) {
|
|
12
12
|
let encryptedKey;
|
|
13
13
|
let parameters;
|
|
@@ -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) ||
|
|
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,4 +1,3 @@
|
|
|
1
|
-
import { inspect } from 'util';
|
|
2
1
|
export default (actual, ...types) => {
|
|
3
2
|
let msg = 'Key must be ';
|
|
4
3
|
if (types.length > 2) {
|
|
@@ -21,16 +20,6 @@ export default (actual, ...types) => {
|
|
|
21
20
|
if (actual.constructor && actual.constructor.name) {
|
|
22
21
|
msg += ` Received an instance of ${actual.constructor.name}`;
|
|
23
22
|
}
|
|
24
|
-
else {
|
|
25
|
-
const inspected = inspect(actual, { depth: -1 });
|
|
26
|
-
msg += ` Received ${inspected}`;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
let inspected = inspect(actual, { colors: false });
|
|
31
|
-
if (inspected.length > 25)
|
|
32
|
-
inspected = `${inspected.slice(0, 25)}...`;
|
|
33
|
-
msg += ` Received type ${typeof actual} (${inspected})`;
|
|
34
23
|
}
|
|
35
24
|
return msg;
|
|
36
25
|
};
|
package/dist/node/esm/lib/iv.js
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
import { JOSENotSupported } from '../util/errors.js';
|
|
2
2
|
import random from '../runtime/random.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
export default generateIv;
|
|
22
|
-
export { bitLengths };
|
|
26
|
+
}
|
|
27
|
+
export default (alg) => random(new Uint8Array(bitLength(alg) >> 3));
|
|
@@ -1,11 +1,12 @@
|
|
|
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
|
|
6
|
+
import { isCryptoKey } from './webcrypto.js';
|
|
7
|
+
import { checkEncCryptoKey } from '../lib/crypto_key.js';
|
|
7
8
|
import isKeyObject from './is_key_object.js';
|
|
8
|
-
import invalidKeyInput from '
|
|
9
|
+
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
9
10
|
import supported from './ciphers.js';
|
|
10
11
|
function checkKeySize(key, alg) {
|
|
11
12
|
if (key.symmetricKeySize << 3 !== parseInt(alg.substr(1, 3), 10)) {
|
|
@@ -20,7 +21,8 @@ function ensureKeyObject(key, alg, usage) {
|
|
|
20
21
|
return getSecretKey(key);
|
|
21
22
|
}
|
|
22
23
|
if (isCryptoKey(key)) {
|
|
23
|
-
|
|
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,15 +1,15 @@
|
|
|
1
|
-
import { createPrivateKey, createPublicKey } from 'crypto';
|
|
1
|
+
import { createPrivateKey, createPublicKey, KeyObject } from 'crypto';
|
|
2
2
|
import { Buffer } from 'buffer';
|
|
3
|
-
import { isCryptoKey
|
|
3
|
+
import { isCryptoKey } from './webcrypto.js';
|
|
4
4
|
import isKeyObject from './is_key_object.js';
|
|
5
|
-
import invalidKeyInput from '
|
|
5
|
+
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
6
6
|
const genericExport = (keyType, keyFormat, key) => {
|
|
7
7
|
let keyObject;
|
|
8
8
|
if (isCryptoKey(key)) {
|
|
9
9
|
if (!key.extractable) {
|
|
10
10
|
throw new TypeError('CryptoKey is not extractable');
|
|
11
11
|
}
|
|
12
|
-
keyObject =
|
|
12
|
+
keyObject = KeyObject.from(key);
|
|
13
13
|
}
|
|
14
14
|
else if (isKeyObject(key)) {
|
|
15
15
|
keyObject = key;
|
|
@@ -1,13 +1,14 @@
|
|
|
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
|
|
8
|
+
import { isCryptoKey } from './webcrypto.js';
|
|
9
|
+
import { checkEncCryptoKey } from '../lib/crypto_key.js';
|
|
9
10
|
import isKeyObject from './is_key_object.js';
|
|
10
|
-
import invalidKeyInput from '
|
|
11
|
+
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
11
12
|
import supported from './ciphers.js';
|
|
12
13
|
async function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
|
|
13
14
|
const keySize = parseInt(enc.substr(1, 3), 10);
|
|
@@ -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
|
-
|
|
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
|
|
7
|
+
import { isCryptoKey } from './webcrypto.js';
|
|
8
|
+
import { checkEncCryptoKey } from '../lib/crypto_key.js';
|
|
8
9
|
import isKeyObject from './is_key_object.js';
|
|
9
|
-
import invalidKeyInput from '
|
|
10
|
+
import invalidKeyInput from '../lib/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
|
-
|
|
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
|
-
|
|
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 =
|
|
42
|
+
key = KeyObject.from(kee);
|
|
40
43
|
}
|
|
41
44
|
else if (isKeyObject(kee)) {
|
|
42
45
|
key = kee;
|
|
@@ -1,11 +1,12 @@
|
|
|
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
|
|
6
|
+
import { isCryptoKey } from './webcrypto.js';
|
|
7
|
+
import { checkEncCryptoKey } from '../lib/crypto_key.js';
|
|
7
8
|
import isKeyObject from './is_key_object.js';
|
|
8
|
-
import invalidKeyInput from '
|
|
9
|
+
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
9
10
|
import { JOSENotSupported } from '../util/errors.js';
|
|
10
11
|
import supported from './ciphers.js';
|
|
11
12
|
async function cbcEncrypt(enc, plaintext, cek, iv, aad) {
|
|
@@ -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
|
-
|
|
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,9 +1,9 @@
|
|
|
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
|
|
4
|
+
import { isCryptoKey } from './webcrypto.js';
|
|
5
5
|
import isKeyObject from './is_key_object.js';
|
|
6
|
-
import invalidKeyInput from '
|
|
6
|
+
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
7
7
|
const p256 = Buffer.from([42, 134, 72, 206, 61, 3, 1, 7]);
|
|
8
8
|
const p384 = Buffer.from([43, 129, 4, 0, 34]);
|
|
9
9
|
const p521 = Buffer.from([43, 129, 4, 0, 35]);
|
|
@@ -27,7 +27,7 @@ const getNamedCurve = (kee, raw) => {
|
|
|
27
27
|
var _a;
|
|
28
28
|
let key;
|
|
29
29
|
if (isCryptoKey(kee)) {
|
|
30
|
-
key =
|
|
30
|
+
key = KeyObject.from(kee);
|
|
31
31
|
}
|
|
32
32
|
else if (isKeyObject(kee)) {
|
|
33
33
|
key = kee;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { isCryptoKey
|
|
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
|
-
import invalidKeyInput from '
|
|
5
|
+
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
5
6
|
export default function getSignVerifyKey(alg, key, usage) {
|
|
6
7
|
if (key instanceof Uint8Array) {
|
|
7
8
|
if (!alg.startsWith('HS')) {
|
|
@@ -9,11 +10,12 @@ export default function getSignVerifyKey(alg, key, usage) {
|
|
|
9
10
|
}
|
|
10
11
|
return getSecretKey(key);
|
|
11
12
|
}
|
|
12
|
-
if (key instanceof
|
|
13
|
+
if (key instanceof KeyObject) {
|
|
13
14
|
return key;
|
|
14
15
|
}
|
|
15
16
|
if (isCryptoKey(key)) {
|
|
16
|
-
|
|
17
|
+
checkSigCryptoKey(key, alg, usage);
|
|
18
|
+
return KeyObject.from(key);
|
|
17
19
|
}
|
|
18
20
|
throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey', 'Uint8Array'));
|
|
19
21
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
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
|
|
6
|
+
import { isCryptoKey } from './webcrypto.js';
|
|
7
7
|
import isKeyObject from './is_key_object.js';
|
|
8
|
-
import invalidKeyInput from '
|
|
8
|
+
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
9
9
|
const [major, minor] = process.version
|
|
10
10
|
.substr(1)
|
|
11
11
|
.split('.')
|
|
@@ -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 =
|
|
20
|
+
keyObject = KeyObject.from(key);
|
|
21
21
|
}
|
|
22
22
|
else if (isKeyObject(key)) {
|
|
23
23
|
keyObject = key;
|
|
@@ -7,6 +7,10 @@ const [major, minor] = process.version
|
|
|
7
7
|
.split('.')
|
|
8
8
|
.map((str) => parseInt(str, 10));
|
|
9
9
|
const rsaPssParams = major >= 17 || (major === 16 && minor >= 9);
|
|
10
|
+
const PSS = {
|
|
11
|
+
padding: constants.RSA_PKCS1_PSS_PADDING,
|
|
12
|
+
saltLength: constants.RSA_PSS_SALTLEN_DIGEST,
|
|
13
|
+
};
|
|
10
14
|
const ecCurveAlgMap = new Map([
|
|
11
15
|
['ES256', 'P-256'],
|
|
12
16
|
['ES256K', 'secp256k1'],
|
|
@@ -46,11 +50,7 @@ export default function keyForCrypto(alg, key) {
|
|
|
46
50
|
throw new TypeError('Invalid key for this operation, its asymmetricKeyType must be rsa or rsa-pss');
|
|
47
51
|
}
|
|
48
52
|
checkModulusLength(key, alg);
|
|
49
|
-
return {
|
|
50
|
-
key,
|
|
51
|
-
padding: constants.RSA_PKCS1_PSS_PADDING,
|
|
52
|
-
saltLength: constants.RSA_PSS_SALTLEN_DIGEST,
|
|
53
|
-
};
|
|
53
|
+
return { key, ...PSS };
|
|
54
54
|
case !rsaPssParams && 'PS256':
|
|
55
55
|
case !rsaPssParams && 'PS384':
|
|
56
56
|
case !rsaPssParams && 'PS512':
|
|
@@ -58,11 +58,7 @@ export default function keyForCrypto(alg, key) {
|
|
|
58
58
|
throw new TypeError('Invalid key for this operation, its asymmetricKeyType must be rsa');
|
|
59
59
|
}
|
|
60
60
|
checkModulusLength(key, alg);
|
|
61
|
-
return {
|
|
62
|
-
key,
|
|
63
|
-
padding: constants.RSA_PKCS1_PSS_PADDING,
|
|
64
|
-
saltLength: constants.RSA_PSS_SALTLEN_DIGEST,
|
|
65
|
-
};
|
|
61
|
+
return { key, ...PSS };
|
|
66
62
|
case 'ES256':
|
|
67
63
|
case 'ES256K':
|
|
68
64
|
case 'ES384':
|
|
@@ -1,13 +1,14 @@
|
|
|
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
|
|
8
|
+
import { isCryptoKey } from './webcrypto.js';
|
|
9
|
+
import { checkEncCryptoKey } from '../lib/crypto_key.js';
|
|
9
10
|
import isKeyObject from './is_key_object.js';
|
|
10
|
-
import invalidKeyInput from '
|
|
11
|
+
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
11
12
|
const pbkdf2 = promisify(pbkdf2cb);
|
|
12
13
|
function getPassword(key, alg) {
|
|
13
14
|
if (isKeyObject(key)) {
|
|
@@ -17,7 +18,8 @@ function getPassword(key, alg) {
|
|
|
17
18
|
return key;
|
|
18
19
|
}
|
|
19
20
|
if (isCryptoKey(key)) {
|
|
20
|
-
|
|
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,8 +1,9 @@
|
|
|
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
|
|
3
|
+
import { isCryptoKey } from './webcrypto.js';
|
|
4
|
+
import { checkEncCryptoKey } from '../lib/crypto_key.js';
|
|
4
5
|
import isKeyObject from './is_key_object.js';
|
|
5
|
-
import invalidKeyInput from '
|
|
6
|
+
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
6
7
|
const checkKey = (key, alg) => {
|
|
7
8
|
if (key.asymmetricKeyType !== 'rsa') {
|
|
8
9
|
throw new TypeError('Invalid key for this operation, its asymmetricKeyType must be rsa');
|
|
@@ -41,7 +42,8 @@ function ensureKeyObject(key, alg, ...usages) {
|
|
|
41
42
|
return key;
|
|
42
43
|
}
|
|
43
44
|
if (isCryptoKey(key)) {
|
|
44
|
-
|
|
45
|
+
checkEncCryptoKey(key, alg, ...usages);
|
|
46
|
+
return KeyObject.from(key);
|
|
45
47
|
}
|
|
46
48
|
throw new TypeError(invalidKeyInput(key, 'KeyObject', 'CryptoKey'));
|
|
47
49
|
}
|