jose 4.0.3 → 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.
- package/dist/browser/lib/cek.js +18 -16
- package/dist/browser/lib/check_iv_length.js +2 -2
- package/dist/browser/lib/crypto_key.js +160 -0
- package/dist/browser/lib/decrypt_key_management.js +2 -2
- package/dist/browser/lib/encrypt_key_management.js +2 -2
- package/dist/browser/lib/iv.js +24 -19
- package/dist/browser/runtime/aeskw.js +3 -2
- package/dist/browser/runtime/check_cek_length.js +7 -0
- package/dist/browser/runtime/decrypt.js +8 -2
- package/dist/browser/runtime/ecdhes.js +4 -3
- package/dist/browser/runtime/encrypt.js +8 -2
- package/dist/browser/runtime/get_sign_verify_key.js +3 -2
- package/dist/browser/runtime/pbes2kw.js +3 -2
- package/dist/browser/runtime/rsaes.js +4 -3
- package/dist/browser/runtime/webcrypto.js +0 -145
- 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/crypto_key.js +165 -0
- package/dist/node/cjs/lib/decrypt_key_management.js +1 -1
- package/dist/node/cjs/lib/encrypt_key_management.js +1 -1
- package/dist/node/cjs/lib/iv.js +26 -20
- package/dist/node/cjs/runtime/aeskw.js +3 -1
- package/dist/node/cjs/runtime/asn1.js +1 -1
- package/dist/node/cjs/runtime/decrypt.js +3 -1
- package/dist/node/cjs/runtime/ecdhes.js +6 -3
- package/dist/node/cjs/runtime/encrypt.js +3 -1
- package/dist/node/cjs/runtime/get_named_curve.js +1 -1
- package/dist/node/cjs/runtime/get_sign_verify_key.js +5 -3
- package/dist/node/cjs/runtime/global.js +11 -0
- package/dist/node/cjs/runtime/key_to_jwk.js +1 -1
- package/dist/node/cjs/runtime/pbes2kw.js +3 -1
- package/dist/node/cjs/runtime/rsaes.js +3 -1
- package/dist/node/cjs/runtime/webcrypto.js +1 -152
- 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/crypto_key.js +160 -0
- package/dist/node/esm/lib/decrypt_key_management.js +2 -2
- package/dist/node/esm/lib/encrypt_key_management.js +2 -2
- package/dist/node/esm/lib/iv.js +24 -19
- package/dist/node/esm/runtime/aeskw.js +5 -3
- package/dist/node/esm/runtime/asn1.js +3 -3
- package/dist/node/esm/runtime/decrypt.js +5 -3
- package/dist/node/esm/runtime/ecdhes.js +8 -5
- package/dist/node/esm/runtime/encrypt.js +5 -3
- package/dist/node/esm/runtime/get_named_curve.js +3 -3
- package/dist/node/esm/runtime/get_sign_verify_key.js +6 -4
- package/dist/node/esm/runtime/global.js +6 -0
- package/dist/node/esm/runtime/key_to_jwk.js +3 -3
- package/dist/node/esm/runtime/pbes2kw.js +5 -3
- package/dist/node/esm/runtime/rsaes.js +5 -3
- package/dist/node/esm/runtime/webcrypto.js +0 -149
- package/package.json +1 -1
package/dist/node/cjs/lib/cek.js
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.bitLength = void 0;
|
|
4
4
|
const errors_js_1 = require("../util/errors.js");
|
|
5
5
|
const random_js_1 = require("../runtime/random.js");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
6
|
+
function bitLength(alg) {
|
|
7
|
+
switch (alg) {
|
|
8
|
+
case 'A128CBC-HS256':
|
|
9
|
+
return 256;
|
|
10
|
+
case 'A192CBC-HS384':
|
|
11
|
+
return 384;
|
|
12
|
+
case 'A256CBC-HS512':
|
|
13
|
+
return 512;
|
|
14
|
+
case 'A128GCM':
|
|
15
|
+
return 128;
|
|
16
|
+
case 'A192GCM':
|
|
17
|
+
return 192;
|
|
18
|
+
case 'A256GCM':
|
|
19
|
+
return 256;
|
|
20
|
+
default:
|
|
21
|
+
throw new errors_js_1.JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
|
|
19
22
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
exports.default =
|
|
23
|
+
}
|
|
24
|
+
exports.bitLength = bitLength;
|
|
25
|
+
exports.default = (alg) => (0, random_js_1.default)(new Uint8Array(bitLength(alg) >> 3));
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const errors_js_1 = require("../util/errors.js");
|
|
4
4
|
const iv_js_1 = require("./iv.js");
|
|
5
5
|
const checkIvLength = (enc, iv) => {
|
|
6
|
-
if (iv.length << 3 !== iv_js_1.
|
|
6
|
+
if (iv.length << 3 !== (0, iv_js_1.bitLength)(enc)) {
|
|
7
7
|
throw new errors_js_1.JWEInvalid('Invalid Initialization Vector length');
|
|
8
8
|
}
|
|
9
9
|
};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkEncCryptoKey = exports.checkSigCryptoKey = void 0;
|
|
4
|
+
const global_js_1 = require("../runtime/global.js");
|
|
5
|
+
function getHashLength(hash) {
|
|
6
|
+
return parseInt(hash === null || hash === void 0 ? void 0 : hash.name.substr(4), 10);
|
|
7
|
+
}
|
|
8
|
+
function getNamedCurve(alg) {
|
|
9
|
+
switch (alg) {
|
|
10
|
+
case 'ES256':
|
|
11
|
+
return 'P-256';
|
|
12
|
+
case 'ES384':
|
|
13
|
+
return 'P-384';
|
|
14
|
+
case 'ES512':
|
|
15
|
+
return 'P-521';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function checkUsage(key, usages) {
|
|
19
|
+
if (usages.length && !usages.some((expected) => key.usages.includes(expected))) {
|
|
20
|
+
let msg = 'CryptoKey does not support this operation, its usages must include ';
|
|
21
|
+
if (usages.length > 2) {
|
|
22
|
+
const last = usages.pop();
|
|
23
|
+
msg += `one of ${usages.join(', ')}, or ${last}.`;
|
|
24
|
+
}
|
|
25
|
+
else if (usages.length === 2) {
|
|
26
|
+
msg += `one of ${usages[0]} or ${usages[1]}.`;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
msg += `${usages[0]}.`;
|
|
30
|
+
}
|
|
31
|
+
throw new TypeError(msg);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function checkSigCryptoKey(key, alg, ...usages) {
|
|
35
|
+
switch (alg) {
|
|
36
|
+
case 'HS256':
|
|
37
|
+
case 'HS384':
|
|
38
|
+
case 'HS512': {
|
|
39
|
+
if (key.algorithm.name !== 'HMAC') {
|
|
40
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be HMAC.`);
|
|
41
|
+
}
|
|
42
|
+
const expected = parseInt(alg.substr(2), 10);
|
|
43
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
44
|
+
if (actual !== expected) {
|
|
45
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.hash must be SHA-${expected}.`);
|
|
46
|
+
}
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
case 'RS256':
|
|
50
|
+
case 'RS384':
|
|
51
|
+
case 'RS512': {
|
|
52
|
+
if (key.algorithm.name !== 'RSASSA-PKCS1-v1_5') {
|
|
53
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be RSASSA-PKCS1-v1_5.`);
|
|
54
|
+
}
|
|
55
|
+
const expected = parseInt(alg.substr(2), 10);
|
|
56
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
57
|
+
if (actual !== expected) {
|
|
58
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.hash must be SHA-${expected}.`);
|
|
59
|
+
}
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case 'PS256':
|
|
63
|
+
case 'PS384':
|
|
64
|
+
case 'PS512': {
|
|
65
|
+
if (key.algorithm.name !== 'RSA-PSS') {
|
|
66
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be RSA-PSS.`);
|
|
67
|
+
}
|
|
68
|
+
const expected = parseInt(alg.substr(2), 10);
|
|
69
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
70
|
+
if (actual !== expected) {
|
|
71
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.hash must be SHA-${expected}.`);
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
case (0, global_js_1.isNodeJs)() && 'EdDSA': {
|
|
76
|
+
if (key.algorithm.name !== 'NODE-ED25519' && key.algorithm.name !== 'NODE-ED448') {
|
|
77
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be NODE-ED25519 or NODE-ED448.`);
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
case (0, global_js_1.isCloudflareWorkers)() && 'EdDSA': {
|
|
82
|
+
if (key.algorithm.name !== 'NODE-ED25519') {
|
|
83
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be NODE-ED25519.`);
|
|
84
|
+
}
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
case 'ES256':
|
|
88
|
+
case 'ES384':
|
|
89
|
+
case 'ES512': {
|
|
90
|
+
if (key.algorithm.name !== 'ECDSA') {
|
|
91
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be ECDSA.`);
|
|
92
|
+
}
|
|
93
|
+
const expected = getNamedCurve(alg);
|
|
94
|
+
const actual = key.algorithm.namedCurve;
|
|
95
|
+
if (actual !== expected) {
|
|
96
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.namedCurve must be ${expected}.`);
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
default:
|
|
101
|
+
throw new TypeError('CryptoKey does not support this operation');
|
|
102
|
+
}
|
|
103
|
+
checkUsage(key, usages);
|
|
104
|
+
}
|
|
105
|
+
exports.checkSigCryptoKey = checkSigCryptoKey;
|
|
106
|
+
function checkEncCryptoKey(key, alg, ...usages) {
|
|
107
|
+
switch (alg) {
|
|
108
|
+
case 'A128GCM':
|
|
109
|
+
case 'A192GCM':
|
|
110
|
+
case 'A256GCM': {
|
|
111
|
+
if (key.algorithm.name !== 'AES-GCM') {
|
|
112
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be AES-GCM.`);
|
|
113
|
+
}
|
|
114
|
+
const expected = parseInt(alg.substr(1, 3), 10);
|
|
115
|
+
const actual = key.algorithm.length;
|
|
116
|
+
if (actual !== expected) {
|
|
117
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.length must be ${expected}.`);
|
|
118
|
+
}
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
case 'A128KW':
|
|
122
|
+
case 'A192KW':
|
|
123
|
+
case 'A256KW': {
|
|
124
|
+
if (key.algorithm.name !== 'AES-KW') {
|
|
125
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be AES-KW.`);
|
|
126
|
+
}
|
|
127
|
+
const expected = parseInt(alg.substr(1, 3), 10);
|
|
128
|
+
const actual = key.algorithm.length;
|
|
129
|
+
if (actual !== expected) {
|
|
130
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.length must be ${expected}.`);
|
|
131
|
+
}
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case 'ECDH-ES':
|
|
135
|
+
if (key.algorithm.name !== 'ECDH') {
|
|
136
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be ECDH.`);
|
|
137
|
+
}
|
|
138
|
+
break;
|
|
139
|
+
case 'PBES2-HS256+A128KW':
|
|
140
|
+
case 'PBES2-HS384+A192KW':
|
|
141
|
+
case 'PBES2-HS512+A256KW':
|
|
142
|
+
if (key.algorithm.name !== 'PBKDF2') {
|
|
143
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be PBKDF2.`);
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
case 'RSA-OAEP':
|
|
147
|
+
case 'RSA-OAEP-256':
|
|
148
|
+
case 'RSA-OAEP-384':
|
|
149
|
+
case 'RSA-OAEP-512': {
|
|
150
|
+
if (key.algorithm.name !== 'RSA-OAEP') {
|
|
151
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be RSA-OAEP.`);
|
|
152
|
+
}
|
|
153
|
+
const expected = parseInt(alg.substr(9), 10) || 1;
|
|
154
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
155
|
+
if (actual !== expected) {
|
|
156
|
+
throw new TypeError(`CryptoKey does not support this operation, its algorithm.hash must be SHA-${expected}.`);
|
|
157
|
+
}
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
default:
|
|
161
|
+
throw new TypeError('CryptoKey does not support this operation');
|
|
162
|
+
}
|
|
163
|
+
checkUsage(key, usages);
|
|
164
|
+
}
|
|
165
|
+
exports.checkEncCryptoKey = checkEncCryptoKey;
|
|
@@ -42,7 +42,7 @@ async function decryptKeyManagement(alg, key, encryptedKey, joseHeader) {
|
|
|
42
42
|
throw new errors_js_1.JWEInvalid(`JOSE Header "apv" (Agreement PartyVInfo) invalid`);
|
|
43
43
|
partyVInfo = (0, base64url_js_1.decode)(joseHeader.apv);
|
|
44
44
|
}
|
|
45
|
-
const sharedSecret = await ECDH.deriveKey(epk, key, alg === 'ECDH-ES' ? joseHeader.enc : alg, parseInt(alg.substr(-5, 3), 10) || cek_js_1.
|
|
45
|
+
const sharedSecret = await ECDH.deriveKey(epk, key, alg === 'ECDH-ES' ? joseHeader.enc : alg, parseInt(alg.substr(-5, 3), 10) || (0, cek_js_1.bitLength)(joseHeader.enc), partyUInfo, partyVInfo);
|
|
46
46
|
if (alg === 'ECDH-ES')
|
|
47
47
|
return sharedSecret;
|
|
48
48
|
if (encryptedKey === undefined)
|
|
@@ -31,7 +31,7 @@ async function encryptKeyManagement(alg, enc, key, providedCek, providedParamete
|
|
|
31
31
|
let { epk: ephemeralKey } = providedParameters;
|
|
32
32
|
ephemeralKey || (ephemeralKey = await ECDH.generateEpk(key));
|
|
33
33
|
const { x, y, crv, kty } = await (0, export_js_1.exportJWK)(ephemeralKey);
|
|
34
|
-
const sharedSecret = await ECDH.deriveKey(key, ephemeralKey, alg === 'ECDH-ES' ? enc : alg, parseInt(alg.substr(-5, 3), 10) || cek_js_1.
|
|
34
|
+
const sharedSecret = await ECDH.deriveKey(key, ephemeralKey, alg === 'ECDH-ES' ? enc : alg, parseInt(alg.substr(-5, 3), 10) || (0, cek_js_1.bitLength)(enc), apu, apv);
|
|
35
35
|
parameters = { epk: { x, y, crv, kty } };
|
|
36
36
|
if (apu)
|
|
37
37
|
parameters.apu = (0, base64url_js_1.encode)(apu);
|
package/dist/node/cjs/lib/iv.js
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.bitLength = void 0;
|
|
4
4
|
const errors_js_1 = require("../util/errors.js");
|
|
5
5
|
const random_js_1 = require("../runtime/random.js");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
6
|
+
function bitLength(alg) {
|
|
7
|
+
switch (alg) {
|
|
8
|
+
case 'A128CBC-HS256':
|
|
9
|
+
return 128;
|
|
10
|
+
case 'A128GCM':
|
|
11
|
+
return 96;
|
|
12
|
+
case 'A128GCMKW':
|
|
13
|
+
return 96;
|
|
14
|
+
case 'A192CBC-HS384':
|
|
15
|
+
return 128;
|
|
16
|
+
case 'A192GCM':
|
|
17
|
+
return 96;
|
|
18
|
+
case 'A192GCMKW':
|
|
19
|
+
return 96;
|
|
20
|
+
case 'A256CBC-HS512':
|
|
21
|
+
return 128;
|
|
22
|
+
case 'A256GCM':
|
|
23
|
+
return 96;
|
|
24
|
+
case 'A256GCMKW':
|
|
25
|
+
return 96;
|
|
26
|
+
default:
|
|
27
|
+
throw new errors_js_1.JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
|
|
22
28
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
exports.default =
|
|
29
|
+
}
|
|
30
|
+
exports.bitLength = bitLength;
|
|
31
|
+
exports.default = (alg) => (0, random_js_1.default)(new Uint8Array(bitLength(alg) >> 3));
|
|
@@ -7,6 +7,7 @@ const errors_js_1 = require("../util/errors.js");
|
|
|
7
7
|
const buffer_utils_js_1 = require("../lib/buffer_utils.js");
|
|
8
8
|
const secret_key_js_1 = require("./secret_key.js");
|
|
9
9
|
const webcrypto_js_1 = require("./webcrypto.js");
|
|
10
|
+
const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
10
11
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
11
12
|
const invalid_key_input_js_1 = require("./invalid_key_input.js");
|
|
12
13
|
const ciphers_js_1 = require("./ciphers.js");
|
|
@@ -23,7 +24,8 @@ function ensureKeyObject(key, alg, usage) {
|
|
|
23
24
|
return (0, secret_key_js_1.default)(key);
|
|
24
25
|
}
|
|
25
26
|
if ((0, webcrypto_js_1.isCryptoKey)(key)) {
|
|
26
|
-
|
|
27
|
+
(0, crypto_key_js_1.checkEncCryptoKey)(key, alg, usage);
|
|
28
|
+
return crypto_1.KeyObject.from(key);
|
|
27
29
|
}
|
|
28
30
|
throw new TypeError((0, invalid_key_input_js_1.default)(key, 'KeyObject', 'CryptoKey', 'Uint8Array'));
|
|
29
31
|
}
|
|
@@ -12,7 +12,7 @@ const genericExport = (keyType, keyFormat, key) => {
|
|
|
12
12
|
if (!key.extractable) {
|
|
13
13
|
throw new TypeError('CryptoKey is not extractable');
|
|
14
14
|
}
|
|
15
|
-
keyObject =
|
|
15
|
+
keyObject = crypto_1.KeyObject.from(key);
|
|
16
16
|
}
|
|
17
17
|
else if ((0, is_key_object_js_1.default)(key)) {
|
|
18
18
|
keyObject = key;
|
|
@@ -8,6 +8,7 @@ const errors_js_1 = require("../util/errors.js");
|
|
|
8
8
|
const timing_safe_equal_js_1 = require("./timing_safe_equal.js");
|
|
9
9
|
const cbc_tag_js_1 = require("./cbc_tag.js");
|
|
10
10
|
const webcrypto_js_1 = require("./webcrypto.js");
|
|
11
|
+
const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
11
12
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
12
13
|
const invalid_key_input_js_1 = require("./invalid_key_input.js");
|
|
13
14
|
const ciphers_js_1 = require("./ciphers.js");
|
|
@@ -66,7 +67,8 @@ async function gcmDecrypt(enc, cek, ciphertext, iv, tag, aad) {
|
|
|
66
67
|
const decrypt = async (enc, cek, ciphertext, iv, tag, aad) => {
|
|
67
68
|
let key;
|
|
68
69
|
if ((0, webcrypto_js_1.isCryptoKey)(cek)) {
|
|
69
|
-
|
|
70
|
+
(0, crypto_key_js_1.checkEncCryptoKey)(cek, enc, 'decrypt');
|
|
71
|
+
key = crypto_1.KeyObject.from(cek);
|
|
70
72
|
}
|
|
71
73
|
else if (cek instanceof Uint8Array || (0, is_key_object_js_1.default)(cek)) {
|
|
72
74
|
key = cek;
|
|
@@ -8,13 +8,15 @@ const buffer_utils_js_1 = require("../lib/buffer_utils.js");
|
|
|
8
8
|
const digest_js_1 = require("./digest.js");
|
|
9
9
|
const errors_js_1 = require("../util/errors.js");
|
|
10
10
|
const webcrypto_js_1 = require("./webcrypto.js");
|
|
11
|
+
const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
11
12
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
12
13
|
const invalid_key_input_js_1 = require("./invalid_key_input.js");
|
|
13
14
|
const generateKeyPair = (0, util_1.promisify)(crypto_1.generateKeyPair);
|
|
14
15
|
const deriveKey = async (publicKee, privateKee, algorithm, keyLength, apu = new Uint8Array(0), apv = new Uint8Array(0)) => {
|
|
15
16
|
let publicKey;
|
|
16
17
|
if ((0, webcrypto_js_1.isCryptoKey)(publicKee)) {
|
|
17
|
-
|
|
18
|
+
(0, crypto_key_js_1.checkEncCryptoKey)(publicKee, 'ECDH-ES');
|
|
19
|
+
publicKey = crypto_1.KeyObject.from(publicKee);
|
|
18
20
|
}
|
|
19
21
|
else if ((0, is_key_object_js_1.default)(publicKee)) {
|
|
20
22
|
publicKey = publicKee;
|
|
@@ -24,7 +26,8 @@ const deriveKey = async (publicKee, privateKee, algorithm, keyLength, apu = new
|
|
|
24
26
|
}
|
|
25
27
|
let privateKey;
|
|
26
28
|
if ((0, webcrypto_js_1.isCryptoKey)(privateKee)) {
|
|
27
|
-
|
|
29
|
+
(0, crypto_key_js_1.checkEncCryptoKey)(privateKee, 'ECDH-ES', 'deriveBits', 'deriveKey');
|
|
30
|
+
privateKey = crypto_1.KeyObject.from(privateKee);
|
|
28
31
|
}
|
|
29
32
|
else if ((0, is_key_object_js_1.default)(privateKee)) {
|
|
30
33
|
privateKey = privateKee;
|
|
@@ -40,7 +43,7 @@ exports.deriveKey = deriveKey;
|
|
|
40
43
|
const generateEpk = async (kee) => {
|
|
41
44
|
let key;
|
|
42
45
|
if ((0, webcrypto_js_1.isCryptoKey)(kee)) {
|
|
43
|
-
key =
|
|
46
|
+
key = crypto_1.KeyObject.from(kee);
|
|
44
47
|
}
|
|
45
48
|
else if ((0, is_key_object_js_1.default)(kee)) {
|
|
46
49
|
key = kee;
|
|
@@ -6,6 +6,7 @@ const check_cek_length_js_1 = require("./check_cek_length.js");
|
|
|
6
6
|
const buffer_utils_js_1 = require("../lib/buffer_utils.js");
|
|
7
7
|
const cbc_tag_js_1 = require("./cbc_tag.js");
|
|
8
8
|
const webcrypto_js_1 = require("./webcrypto.js");
|
|
9
|
+
const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
9
10
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
10
11
|
const invalid_key_input_js_1 = require("./invalid_key_input.js");
|
|
11
12
|
const errors_js_1 = require("../util/errors.js");
|
|
@@ -44,7 +45,8 @@ async function gcmEncrypt(enc, plaintext, cek, iv, aad) {
|
|
|
44
45
|
const encrypt = async (enc, plaintext, cek, iv, aad) => {
|
|
45
46
|
let key;
|
|
46
47
|
if ((0, webcrypto_js_1.isCryptoKey)(cek)) {
|
|
47
|
-
|
|
48
|
+
(0, crypto_key_js_1.checkEncCryptoKey)(cek, enc, 'encrypt');
|
|
49
|
+
key = crypto_1.KeyObject.from(cek);
|
|
48
50
|
}
|
|
49
51
|
else if (cek instanceof Uint8Array || (0, is_key_object_js_1.default)(cek)) {
|
|
50
52
|
key = cek;
|
|
@@ -30,7 +30,7 @@ const getNamedCurve = (kee, raw) => {
|
|
|
30
30
|
var _a;
|
|
31
31
|
let key;
|
|
32
32
|
if ((0, webcrypto_js_1.isCryptoKey)(kee)) {
|
|
33
|
-
key =
|
|
33
|
+
key = crypto_1.KeyObject.from(kee);
|
|
34
34
|
}
|
|
35
35
|
else if ((0, is_key_object_js_1.default)(kee)) {
|
|
36
36
|
key = kee;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
3
|
+
const crypto_1 = require("crypto");
|
|
4
4
|
const webcrypto_js_1 = require("./webcrypto.js");
|
|
5
|
+
const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
5
6
|
const secret_key_js_1 = require("./secret_key.js");
|
|
6
7
|
const invalid_key_input_js_1 = require("./invalid_key_input.js");
|
|
7
8
|
function getSignVerifyKey(alg, key, usage) {
|
|
@@ -11,11 +12,12 @@ function getSignVerifyKey(alg, key, usage) {
|
|
|
11
12
|
}
|
|
12
13
|
return (0, secret_key_js_1.default)(key);
|
|
13
14
|
}
|
|
14
|
-
if (key instanceof
|
|
15
|
+
if (key instanceof crypto_1.KeyObject) {
|
|
15
16
|
return key;
|
|
16
17
|
}
|
|
17
18
|
if ((0, webcrypto_js_1.isCryptoKey)(key)) {
|
|
18
|
-
|
|
19
|
+
(0, crypto_key_js_1.checkSigCryptoKey)(key, alg, usage);
|
|
20
|
+
return crypto_1.KeyObject.from(key);
|
|
19
21
|
}
|
|
20
22
|
throw new TypeError((0, invalid_key_input_js_1.default)(key, 'KeyObject', 'CryptoKey', 'Uint8Array'));
|
|
21
23
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isNodeJs = exports.isCloudflareWorkers = void 0;
|
|
4
|
+
function isCloudflareWorkers() {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
exports.isCloudflareWorkers = isCloudflareWorkers;
|
|
8
|
+
function isNodeJs() {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
exports.isNodeJs = isNodeJs;
|
|
@@ -19,7 +19,7 @@ const keyToJWK = (key) => {
|
|
|
19
19
|
if (!key.extractable) {
|
|
20
20
|
throw new TypeError('CryptoKey is not extractable');
|
|
21
21
|
}
|
|
22
|
-
keyObject =
|
|
22
|
+
keyObject = crypto_1.KeyObject.from(key);
|
|
23
23
|
}
|
|
24
24
|
else if ((0, is_key_object_js_1.default)(key)) {
|
|
25
25
|
keyObject = key;
|
|
@@ -9,6 +9,7 @@ const base64url_js_1 = require("./base64url.js");
|
|
|
9
9
|
const aeskw_js_1 = require("./aeskw.js");
|
|
10
10
|
const check_p2s_js_1 = require("../lib/check_p2s.js");
|
|
11
11
|
const webcrypto_js_1 = require("./webcrypto.js");
|
|
12
|
+
const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
12
13
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
13
14
|
const invalid_key_input_js_1 = require("./invalid_key_input.js");
|
|
14
15
|
const pbkdf2 = (0, util_1.promisify)(crypto_1.pbkdf2);
|
|
@@ -20,7 +21,8 @@ function getPassword(key, alg) {
|
|
|
20
21
|
return key;
|
|
21
22
|
}
|
|
22
23
|
if ((0, webcrypto_js_1.isCryptoKey)(key)) {
|
|
23
|
-
|
|
24
|
+
(0, crypto_key_js_1.checkEncCryptoKey)(key, alg, 'deriveBits', 'deriveKey');
|
|
25
|
+
return crypto_1.KeyObject.from(key).export();
|
|
24
26
|
}
|
|
25
27
|
throw new TypeError((0, invalid_key_input_js_1.default)(key, 'KeyObject', 'CryptoKey', 'Uint8Array'));
|
|
26
28
|
}
|
|
@@ -4,6 +4,7 @@ exports.decrypt = exports.encrypt = void 0;
|
|
|
4
4
|
const crypto_1 = require("crypto");
|
|
5
5
|
const check_modulus_length_js_1 = require("./check_modulus_length.js");
|
|
6
6
|
const webcrypto_js_1 = require("./webcrypto.js");
|
|
7
|
+
const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
7
8
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
8
9
|
const invalid_key_input_js_1 = require("./invalid_key_input.js");
|
|
9
10
|
const checkKey = (key, alg) => {
|
|
@@ -44,7 +45,8 @@ function ensureKeyObject(key, alg, ...usages) {
|
|
|
44
45
|
return key;
|
|
45
46
|
}
|
|
46
47
|
if ((0, webcrypto_js_1.isCryptoKey)(key)) {
|
|
47
|
-
|
|
48
|
+
(0, crypto_key_js_1.checkEncCryptoKey)(key, alg, ...usages);
|
|
49
|
+
return crypto_1.KeyObject.from(key);
|
|
48
50
|
}
|
|
49
51
|
throw new TypeError((0, invalid_key_input_js_1.default)(key, 'KeyObject', 'CryptoKey'));
|
|
50
52
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.isCryptoKey = void 0;
|
|
4
4
|
const crypto = require("crypto");
|
|
5
5
|
const util = require("util");
|
|
6
6
|
const webcrypto = crypto.webcrypto;
|
|
@@ -9,154 +9,3 @@ exports.isCryptoKey = util.types.isCryptoKey
|
|
|
9
9
|
? (obj) => util.types.isCryptoKey(obj)
|
|
10
10
|
:
|
|
11
11
|
(obj) => false;
|
|
12
|
-
function getHashLength(hash) {
|
|
13
|
-
return parseInt(hash === null || hash === void 0 ? void 0 : hash.name.substr(4), 10);
|
|
14
|
-
}
|
|
15
|
-
function getNamedCurve(alg) {
|
|
16
|
-
switch (alg) {
|
|
17
|
-
case 'ES256':
|
|
18
|
-
return 'P-256';
|
|
19
|
-
case 'ES384':
|
|
20
|
-
return 'P-384';
|
|
21
|
-
case 'ES512':
|
|
22
|
-
return 'P-521';
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
function checkCryptoKey(key, alg, ...usages) {
|
|
26
|
-
switch (alg) {
|
|
27
|
-
case undefined:
|
|
28
|
-
break;
|
|
29
|
-
case 'HS256':
|
|
30
|
-
case 'HS384':
|
|
31
|
-
case 'HS512': {
|
|
32
|
-
if (key.algorithm.name !== 'HMAC') {
|
|
33
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be HMAC.`);
|
|
34
|
-
}
|
|
35
|
-
const expected = parseInt(alg.substr(2), 10);
|
|
36
|
-
const actual = getHashLength(key.algorithm.hash);
|
|
37
|
-
if (actual !== expected) {
|
|
38
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.hash must be SHA-${expected}.`);
|
|
39
|
-
}
|
|
40
|
-
break;
|
|
41
|
-
}
|
|
42
|
-
case 'RS256':
|
|
43
|
-
case 'RS384':
|
|
44
|
-
case 'RS512': {
|
|
45
|
-
if (key.algorithm.name !== 'RSASSA-PKCS1-v1_5') {
|
|
46
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be RSASSA-PKCS1-v1_5.`);
|
|
47
|
-
}
|
|
48
|
-
const expected = parseInt(alg.substr(2), 10);
|
|
49
|
-
const actual = getHashLength(key.algorithm.hash);
|
|
50
|
-
if (actual !== expected) {
|
|
51
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.hash must be SHA-${expected}.`);
|
|
52
|
-
}
|
|
53
|
-
break;
|
|
54
|
-
}
|
|
55
|
-
case 'PS256':
|
|
56
|
-
case 'PS384':
|
|
57
|
-
case 'PS512': {
|
|
58
|
-
if (key.algorithm.name !== 'RSA-PSS') {
|
|
59
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be RSA-PSS.`);
|
|
60
|
-
}
|
|
61
|
-
const expected = parseInt(alg.substr(2), 10);
|
|
62
|
-
const actual = getHashLength(key.algorithm.hash);
|
|
63
|
-
if (actual !== expected) {
|
|
64
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.hash must be SHA-${expected}.`);
|
|
65
|
-
}
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
case 'EdDSA': {
|
|
69
|
-
if (key.algorithm.name !== 'NODE-ED25519' && key.algorithm.name !== 'NODE-ED448') {
|
|
70
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be NODE-ED25519 or NODE-ED448.`);
|
|
71
|
-
}
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
case 'ES256':
|
|
75
|
-
case 'ES384':
|
|
76
|
-
case 'ES512': {
|
|
77
|
-
if (key.algorithm.name !== 'ECDSA') {
|
|
78
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be ECDSA.`);
|
|
79
|
-
}
|
|
80
|
-
const expected = getNamedCurve(alg);
|
|
81
|
-
const actual = key.algorithm.namedCurve;
|
|
82
|
-
if (actual !== expected) {
|
|
83
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.namedCurve must be ${expected}.`);
|
|
84
|
-
}
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
case 'A128GCM':
|
|
88
|
-
case 'A192GCM':
|
|
89
|
-
case 'A256GCM': {
|
|
90
|
-
if (key.algorithm.name !== 'AES-GCM') {
|
|
91
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be AES-GCM.`);
|
|
92
|
-
}
|
|
93
|
-
const expected = parseInt(alg.substr(1, 3), 10);
|
|
94
|
-
const actual = key.algorithm.length;
|
|
95
|
-
if (actual !== expected) {
|
|
96
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.length must be ${expected}.`);
|
|
97
|
-
}
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
case 'A128KW':
|
|
101
|
-
case 'A192KW':
|
|
102
|
-
case 'A256KW': {
|
|
103
|
-
if (key.algorithm.name !== 'AES-KW') {
|
|
104
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be AES-KW.`);
|
|
105
|
-
}
|
|
106
|
-
const expected = parseInt(alg.substr(1, 3), 10);
|
|
107
|
-
const actual = key.algorithm.length;
|
|
108
|
-
if (actual !== expected) {
|
|
109
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.length must be ${expected}.`);
|
|
110
|
-
}
|
|
111
|
-
break;
|
|
112
|
-
}
|
|
113
|
-
case 'ECDH-ES':
|
|
114
|
-
if (key.algorithm.name !== 'ECDH') {
|
|
115
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be ECDH.`);
|
|
116
|
-
}
|
|
117
|
-
break;
|
|
118
|
-
case 'PBES2-HS256+A128KW':
|
|
119
|
-
case 'PBES2-HS384+A192KW':
|
|
120
|
-
case 'PBES2-HS512+A256KW':
|
|
121
|
-
if (key.algorithm.name !== 'PBKDF2') {
|
|
122
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be PBKDF2.`);
|
|
123
|
-
}
|
|
124
|
-
break;
|
|
125
|
-
case 'RSA-OAEP':
|
|
126
|
-
case 'RSA-OAEP-256':
|
|
127
|
-
case 'RSA-OAEP-384':
|
|
128
|
-
case 'RSA-OAEP-512': {
|
|
129
|
-
if (key.algorithm.name !== 'RSA-OAEP') {
|
|
130
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.name must be RSA-OAEP.`);
|
|
131
|
-
}
|
|
132
|
-
const expected = parseInt(alg.substr(9), 10) || 1;
|
|
133
|
-
const actual = getHashLength(key.algorithm.hash);
|
|
134
|
-
if (actual !== expected) {
|
|
135
|
-
throw new TypeError(`CryptoKey does not support this operation, its algorithm.hash must be SHA-${expected}.`);
|
|
136
|
-
}
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
default:
|
|
140
|
-
throw new TypeError('CryptoKey does not support this operation');
|
|
141
|
-
}
|
|
142
|
-
if (usages.length && !usages.some((expected) => key.usages.includes(expected))) {
|
|
143
|
-
let msg = 'CryptoKey does not support this operation, its usages must include ';
|
|
144
|
-
if (usages.length > 2) {
|
|
145
|
-
const last = usages.pop();
|
|
146
|
-
msg += `one of ${usages.join(', ')}, or ${last}.`;
|
|
147
|
-
}
|
|
148
|
-
else if (usages.length === 2) {
|
|
149
|
-
msg += `one of ${usages[0]} or ${usages[1]}.`;
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
msg += `${usages[0]}.`;
|
|
153
|
-
}
|
|
154
|
-
throw new TypeError(msg);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
exports.checkCryptoKey = checkCryptoKey;
|
|
158
|
-
function getKeyObject(key, alg, ...usages) {
|
|
159
|
-
checkCryptoKey(key, alg, ...usages);
|
|
160
|
-
return crypto.KeyObject.from(key);
|
|
161
|
-
}
|
|
162
|
-
exports.getKeyObject = getKeyObject;
|