jose 4.2.0 → 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.
- package/dist/browser/jwe/general/encrypt.js +3 -8
- package/dist/browser/jws/general/sign.js +6 -16
- package/dist/browser/lib/cek.js +5 -6
- package/dist/browser/lib/decrypt_key_management.js +1 -1
- package/dist/browser/lib/encrypt_key_management.js +1 -1
- package/dist/browser/lib/iv.js +4 -11
- package/dist/browser/runtime/aeskw.js +2 -1
- package/dist/browser/runtime/asn1.js +2 -1
- package/dist/browser/runtime/decrypt.js +2 -1
- package/dist/browser/runtime/ecdhes.js +5 -4
- package/dist/browser/runtime/encrypt.js +2 -1
- package/dist/browser/runtime/get_sign_verify_key.js +3 -2
- package/dist/browser/runtime/key_to_jwk.js +2 -1
- package/dist/browser/runtime/pbes2kw.js +2 -1
- package/dist/browser/runtime/rsaes.js +3 -2
- package/dist/node/cjs/jwe/general/encrypt.js +3 -8
- package/dist/node/cjs/jws/general/sign.js +6 -16
- package/dist/node/cjs/lib/cek.js +5 -6
- 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 +4 -11
- package/dist/node/cjs/runtime/aeskw.js +4 -3
- package/dist/node/cjs/runtime/asn1.js +2 -1
- package/dist/node/cjs/runtime/decrypt.js +5 -4
- package/dist/node/cjs/runtime/ecdhes.js +5 -4
- package/dist/node/cjs/runtime/encrypt.js +5 -4
- package/dist/node/cjs/runtime/get_named_curve.js +2 -1
- package/dist/node/cjs/runtime/get_sign_verify_key.js +3 -2
- package/dist/node/cjs/runtime/key_to_jwk.js +2 -1
- package/dist/node/cjs/runtime/pbes2kw.js +2 -1
- package/dist/node/cjs/runtime/rsaes.js +4 -3
- package/dist/node/esm/jwe/general/encrypt.js +3 -8
- package/dist/node/esm/jws/general/sign.js +6 -16
- package/dist/node/esm/lib/cek.js +5 -6
- package/dist/node/esm/lib/decrypt_key_management.js +1 -1
- package/dist/node/esm/lib/encrypt_key_management.js +1 -1
- package/dist/node/esm/lib/iv.js +4 -11
- package/dist/node/esm/runtime/aeskw.js +4 -3
- package/dist/node/esm/runtime/asn1.js +2 -1
- package/dist/node/esm/runtime/decrypt.js +5 -4
- package/dist/node/esm/runtime/ecdhes.js +5 -4
- package/dist/node/esm/runtime/encrypt.js +5 -4
- package/dist/node/esm/runtime/get_named_curve.js +2 -1
- package/dist/node/esm/runtime/get_sign_verify_key.js +3 -2
- package/dist/node/esm/runtime/key_to_jwk.js +2 -1
- package/dist/node/esm/runtime/pbes2kw.js +2 -1
- package/dist/node/esm/runtime/rsaes.js +4 -3
- package/package.json +1 -1
|
@@ -8,18 +8,13 @@ import validateCrit from '../../lib/validate_crit.js';
|
|
|
8
8
|
const recipientRef = new WeakMap();
|
|
9
9
|
class IndividualRecipient {
|
|
10
10
|
setUnprotectedHeader(unprotectedHeader) {
|
|
11
|
-
|
|
11
|
+
const ref = recipientRef.get(this);
|
|
12
|
+
if (ref.unprotectedHeader) {
|
|
12
13
|
throw new TypeError('setUnprotectedHeader can only be called once');
|
|
13
14
|
}
|
|
14
|
-
|
|
15
|
+
ref.unprotectedHeader = unprotectedHeader;
|
|
15
16
|
return this;
|
|
16
17
|
}
|
|
17
|
-
set _unprotectedHeader(value) {
|
|
18
|
-
recipientRef.get(this).unprotectedHeader = value;
|
|
19
|
-
}
|
|
20
|
-
get _unprotectedHeader() {
|
|
21
|
-
return recipientRef.get(this).unprotectedHeader;
|
|
22
|
-
}
|
|
23
18
|
}
|
|
24
19
|
export class GeneralEncrypt {
|
|
25
20
|
constructor(plaintext) {
|
|
@@ -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
|
-
|
|
6
|
+
const ref = signatureRef.get(this);
|
|
7
|
+
if (ref.protectedHeader) {
|
|
7
8
|
throw new TypeError('setProtectedHeader can only be called once');
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
+
ref.protectedHeader = protectedHeader;
|
|
10
11
|
return this;
|
|
11
12
|
}
|
|
12
13
|
setUnprotectedHeader(unprotectedHeader) {
|
|
13
|
-
|
|
14
|
+
const ref = signatureRef.get(this);
|
|
15
|
+
if (ref.unprotectedHeader) {
|
|
14
16
|
throw new TypeError('setUnprotectedHeader can only be called once');
|
|
15
17
|
}
|
|
16
|
-
|
|
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) {
|
package/dist/browser/lib/cek.js
CHANGED
|
@@ -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)
|
|
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)
|
|
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);
|
package/dist/browser/lib/iv.js
CHANGED
|
@@ -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
|
}
|
|
@@ -2,6 +2,7 @@ import bogusWebCrypto from './bogus.js';
|
|
|
2
2
|
import crypto, { isCryptoKey } from './webcrypto.js';
|
|
3
3
|
import { checkEncCryptoKey } from '../lib/crypto_key.js';
|
|
4
4
|
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
5
|
+
import { types } from './is_key_like.js';
|
|
5
6
|
function checkKeySize(key, alg) {
|
|
6
7
|
if (key.algorithm.length !== parseInt(alg.substr(1, 3), 10)) {
|
|
7
8
|
throw new TypeError(`Invalid key size for alg: ${alg}`);
|
|
@@ -15,7 +16,7 @@ function getCryptoKey(key, alg, usage) {
|
|
|
15
16
|
if (key instanceof Uint8Array) {
|
|
16
17
|
return crypto.subtle.importKey('raw', key, 'AES-KW', true, [usage]);
|
|
17
18
|
}
|
|
18
|
-
throw new TypeError(invalidKeyInput(key,
|
|
19
|
+
throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'));
|
|
19
20
|
}
|
|
20
21
|
export const wrap = async (alg, key, cek) => {
|
|
21
22
|
const cryptoKey = await getCryptoKey(key, alg, 'wrapKey');
|
|
@@ -4,9 +4,10 @@ import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
|
4
4
|
import { encodeBase64 } from './base64url.js';
|
|
5
5
|
import formatPEM from '../lib/format_pem.js';
|
|
6
6
|
import { JOSENotSupported } from '../util/errors.js';
|
|
7
|
+
import { types } from './is_key_like.js';
|
|
7
8
|
const genericExport = async (keyType, keyFormat, key) => {
|
|
8
9
|
if (!isCryptoKey(key)) {
|
|
9
|
-
throw new TypeError(invalidKeyInput(key,
|
|
10
|
+
throw new TypeError(invalidKeyInput(key, ...types));
|
|
10
11
|
}
|
|
11
12
|
if (!key.extractable) {
|
|
12
13
|
throw new TypeError('CryptoKey is not extractable');
|
|
@@ -6,6 +6,7 @@ import { JOSENotSupported, JWEDecryptionFailed } from '../util/errors.js';
|
|
|
6
6
|
import crypto, { isCryptoKey } from './webcrypto.js';
|
|
7
7
|
import { checkEncCryptoKey } from '../lib/crypto_key.js';
|
|
8
8
|
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
9
|
+
import { types } from './is_key_like.js';
|
|
9
10
|
async function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
|
|
10
11
|
if (!(cek instanceof Uint8Array)) {
|
|
11
12
|
throw new TypeError(invalidKeyInput(cek, 'Uint8Array'));
|
|
@@ -61,7 +62,7 @@ async function gcmDecrypt(enc, cek, ciphertext, iv, tag, aad) {
|
|
|
61
62
|
}
|
|
62
63
|
const decrypt = async (enc, cek, ciphertext, iv, tag, aad) => {
|
|
63
64
|
if (!isCryptoKey(cek) && !(cek instanceof Uint8Array)) {
|
|
64
|
-
throw new TypeError(invalidKeyInput(cek,
|
|
65
|
+
throw new TypeError(invalidKeyInput(cek, ...types, 'Uint8Array'));
|
|
65
66
|
}
|
|
66
67
|
checkIvLength(enc, iv);
|
|
67
68
|
switch (enc) {
|
|
@@ -3,13 +3,14 @@ import crypto, { isCryptoKey } from './webcrypto.js';
|
|
|
3
3
|
import { checkEncCryptoKey } from '../lib/crypto_key.js';
|
|
4
4
|
import digest from './digest.js';
|
|
5
5
|
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
6
|
+
import { types } from './is_key_like.js';
|
|
6
7
|
export const deriveKey = async (publicKey, privateKey, algorithm, keyLength, apu = new Uint8Array(0), apv = new Uint8Array(0)) => {
|
|
7
8
|
if (!isCryptoKey(publicKey)) {
|
|
8
|
-
throw new TypeError(invalidKeyInput(publicKey,
|
|
9
|
+
throw new TypeError(invalidKeyInput(publicKey, ...types));
|
|
9
10
|
}
|
|
10
11
|
checkEncCryptoKey(publicKey, 'ECDH-ES');
|
|
11
12
|
if (!isCryptoKey(privateKey)) {
|
|
12
|
-
throw new TypeError(invalidKeyInput(privateKey,
|
|
13
|
+
throw new TypeError(invalidKeyInput(privateKey, ...types));
|
|
13
14
|
}
|
|
14
15
|
checkEncCryptoKey(privateKey, 'ECDH-ES', 'deriveBits', 'deriveKey');
|
|
15
16
|
const value = concat(lengthAndInput(encoder.encode(algorithm)), lengthAndInput(apu), lengthAndInput(apv), uint32be(keyLength));
|
|
@@ -25,13 +26,13 @@ export const deriveKey = async (publicKey, privateKey, algorithm, keyLength, apu
|
|
|
25
26
|
};
|
|
26
27
|
export const generateEpk = async (key) => {
|
|
27
28
|
if (!isCryptoKey(key)) {
|
|
28
|
-
throw new TypeError(invalidKeyInput(key,
|
|
29
|
+
throw new TypeError(invalidKeyInput(key, ...types));
|
|
29
30
|
}
|
|
30
31
|
return (await crypto.subtle.generateKey({ name: 'ECDH', namedCurve: key.algorithm.namedCurve }, true, ['deriveBits'])).privateKey;
|
|
31
32
|
};
|
|
32
33
|
export const ecdhAllowed = (key) => {
|
|
33
34
|
if (!isCryptoKey(key)) {
|
|
34
|
-
throw new TypeError(invalidKeyInput(key,
|
|
35
|
+
throw new TypeError(invalidKeyInput(key, ...types));
|
|
35
36
|
}
|
|
36
37
|
return ['P-256', 'P-384', 'P-521'].includes(key.algorithm.namedCurve);
|
|
37
38
|
};
|
|
@@ -5,6 +5,7 @@ import crypto, { isCryptoKey } from './webcrypto.js';
|
|
|
5
5
|
import { checkEncCryptoKey } from '../lib/crypto_key.js';
|
|
6
6
|
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
7
7
|
import { JOSENotSupported } from '../util/errors.js';
|
|
8
|
+
import { types } from './is_key_like.js';
|
|
8
9
|
async function cbcEncrypt(enc, plaintext, cek, iv, aad) {
|
|
9
10
|
if (!(cek instanceof Uint8Array)) {
|
|
10
11
|
throw new TypeError(invalidKeyInput(cek, 'Uint8Array'));
|
|
@@ -44,7 +45,7 @@ async function gcmEncrypt(enc, plaintext, cek, iv, aad) {
|
|
|
44
45
|
}
|
|
45
46
|
const encrypt = async (enc, plaintext, cek, iv, aad) => {
|
|
46
47
|
if (!isCryptoKey(cek) && !(cek instanceof Uint8Array)) {
|
|
47
|
-
throw new TypeError(invalidKeyInput(cek,
|
|
48
|
+
throw new TypeError(invalidKeyInput(cek, ...types, 'Uint8Array'));
|
|
48
49
|
}
|
|
49
50
|
checkIvLength(enc, iv);
|
|
50
51
|
switch (enc) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import crypto, { isCryptoKey } from './webcrypto.js';
|
|
2
2
|
import { checkSigCryptoKey } from '../lib/crypto_key.js';
|
|
3
3
|
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
4
|
+
import { types } from './is_key_like.js';
|
|
4
5
|
export default function getCryptoKey(alg, key, usage) {
|
|
5
6
|
if (isCryptoKey(key)) {
|
|
6
7
|
checkSigCryptoKey(key, alg, usage);
|
|
@@ -8,9 +9,9 @@ export default function getCryptoKey(alg, key, usage) {
|
|
|
8
9
|
}
|
|
9
10
|
if (key instanceof Uint8Array) {
|
|
10
11
|
if (!alg.startsWith('HS')) {
|
|
11
|
-
throw new TypeError(invalidKeyInput(key,
|
|
12
|
+
throw new TypeError(invalidKeyInput(key, ...types));
|
|
12
13
|
}
|
|
13
14
|
return crypto.subtle.importKey('raw', key, { hash: `SHA-${alg.substr(-3)}`, name: 'HMAC' }, false, [usage]);
|
|
14
15
|
}
|
|
15
|
-
throw new TypeError(invalidKeyInput(key,
|
|
16
|
+
throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'));
|
|
16
17
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import crypto, { isCryptoKey } from './webcrypto.js';
|
|
2
2
|
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
3
3
|
import { encode as base64url } from './base64url.js';
|
|
4
|
+
import { types } from './is_key_like.js';
|
|
4
5
|
const keyToJWK = async (key) => {
|
|
5
6
|
if (key instanceof Uint8Array) {
|
|
6
7
|
return {
|
|
@@ -9,7 +10,7 @@ const keyToJWK = async (key) => {
|
|
|
9
10
|
};
|
|
10
11
|
}
|
|
11
12
|
if (!isCryptoKey(key)) {
|
|
12
|
-
throw new TypeError(invalidKeyInput(key,
|
|
13
|
+
throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'));
|
|
13
14
|
}
|
|
14
15
|
if (!key.extractable) {
|
|
15
16
|
throw new TypeError('non-extractable CryptoKey cannot be exported as a JWK');
|
|
@@ -6,6 +6,7 @@ import checkP2s from '../lib/check_p2s.js';
|
|
|
6
6
|
import crypto, { isCryptoKey } from './webcrypto.js';
|
|
7
7
|
import { checkEncCryptoKey } from '../lib/crypto_key.js';
|
|
8
8
|
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
9
|
+
import { types } from './is_key_like.js';
|
|
9
10
|
function getCryptoKey(key, alg) {
|
|
10
11
|
if (key instanceof Uint8Array) {
|
|
11
12
|
return crypto.subtle.importKey('raw', key, 'PBKDF2', false, ['deriveBits']);
|
|
@@ -14,7 +15,7 @@ function getCryptoKey(key, alg) {
|
|
|
14
15
|
checkEncCryptoKey(key, alg, 'deriveBits', 'deriveKey');
|
|
15
16
|
return key;
|
|
16
17
|
}
|
|
17
|
-
throw new TypeError(invalidKeyInput(key,
|
|
18
|
+
throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'));
|
|
18
19
|
}
|
|
19
20
|
async function deriveKey(p2s, alg, p2c, key) {
|
|
20
21
|
checkP2s(p2s);
|
|
@@ -4,9 +4,10 @@ import crypto, { isCryptoKey } from './webcrypto.js';
|
|
|
4
4
|
import { checkEncCryptoKey } from '../lib/crypto_key.js';
|
|
5
5
|
import checkKeyLength from './check_key_length.js';
|
|
6
6
|
import invalidKeyInput from '../lib/invalid_key_input.js';
|
|
7
|
+
import { types } from './is_key_like.js';
|
|
7
8
|
export const encrypt = async (alg, key, cek) => {
|
|
8
9
|
if (!isCryptoKey(key)) {
|
|
9
|
-
throw new TypeError(invalidKeyInput(key,
|
|
10
|
+
throw new TypeError(invalidKeyInput(key, ...types));
|
|
10
11
|
}
|
|
11
12
|
checkEncCryptoKey(key, alg, 'encrypt', 'wrapKey');
|
|
12
13
|
checkKeyLength(alg, key);
|
|
@@ -21,7 +22,7 @@ export const encrypt = async (alg, key, cek) => {
|
|
|
21
22
|
};
|
|
22
23
|
export const decrypt = async (alg, key, encryptedKey) => {
|
|
23
24
|
if (!isCryptoKey(key)) {
|
|
24
|
-
throw new TypeError(invalidKeyInput(key,
|
|
25
|
+
throw new TypeError(invalidKeyInput(key, ...types));
|
|
25
26
|
}
|
|
26
27
|
checkEncCryptoKey(key, alg, 'decrypt', 'unwrapKey');
|
|
27
28
|
checkKeyLength(alg, key);
|
|
@@ -11,18 +11,13 @@ const validate_crit_js_1 = require("../../lib/validate_crit.js");
|
|
|
11
11
|
const recipientRef = new WeakMap();
|
|
12
12
|
class IndividualRecipient {
|
|
13
13
|
setUnprotectedHeader(unprotectedHeader) {
|
|
14
|
-
|
|
14
|
+
const ref = recipientRef.get(this);
|
|
15
|
+
if (ref.unprotectedHeader) {
|
|
15
16
|
throw new TypeError('setUnprotectedHeader can only be called once');
|
|
16
17
|
}
|
|
17
|
-
|
|
18
|
+
ref.unprotectedHeader = unprotectedHeader;
|
|
18
19
|
return this;
|
|
19
20
|
}
|
|
20
|
-
set _unprotectedHeader(value) {
|
|
21
|
-
recipientRef.get(this).unprotectedHeader = value;
|
|
22
|
-
}
|
|
23
|
-
get _unprotectedHeader() {
|
|
24
|
-
return recipientRef.get(this).unprotectedHeader;
|
|
25
|
-
}
|
|
26
21
|
}
|
|
27
22
|
class GeneralEncrypt {
|
|
28
23
|
constructor(plaintext) {
|
|
@@ -6,31 +6,21 @@ const errors_js_1 = require("../../util/errors.js");
|
|
|
6
6
|
const signatureRef = new WeakMap();
|
|
7
7
|
class IndividualSignature {
|
|
8
8
|
setProtectedHeader(protectedHeader) {
|
|
9
|
-
|
|
9
|
+
const ref = signatureRef.get(this);
|
|
10
|
+
if (ref.protectedHeader) {
|
|
10
11
|
throw new TypeError('setProtectedHeader can only be called once');
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
+
ref.protectedHeader = protectedHeader;
|
|
13
14
|
return this;
|
|
14
15
|
}
|
|
15
16
|
setUnprotectedHeader(unprotectedHeader) {
|
|
16
|
-
|
|
17
|
+
const ref = signatureRef.get(this);
|
|
18
|
+
if (ref.unprotectedHeader) {
|
|
17
19
|
throw new TypeError('setUnprotectedHeader can only be called once');
|
|
18
20
|
}
|
|
19
|
-
|
|
21
|
+
ref.unprotectedHeader = unprotectedHeader;
|
|
20
22
|
return this;
|
|
21
23
|
}
|
|
22
|
-
set _protectedHeader(value) {
|
|
23
|
-
signatureRef.get(this).protectedHeader = value;
|
|
24
|
-
}
|
|
25
|
-
get _protectedHeader() {
|
|
26
|
-
return signatureRef.get(this).protectedHeader;
|
|
27
|
-
}
|
|
28
|
-
set _unprotectedHeader(value) {
|
|
29
|
-
signatureRef.get(this).unprotectedHeader = value;
|
|
30
|
-
}
|
|
31
|
-
get _unprotectedHeader() {
|
|
32
|
-
return signatureRef.get(this).unprotectedHeader;
|
|
33
|
-
}
|
|
34
24
|
}
|
|
35
25
|
class GeneralSign {
|
|
36
26
|
constructor(payload) {
|
package/dist/node/cjs/lib/cek.js
CHANGED
|
@@ -5,18 +5,17 @@ const errors_js_1 = require("../util/errors.js");
|
|
|
5
5
|
const random_js_1 = require("../runtime/random.js");
|
|
6
6
|
function bitLength(alg) {
|
|
7
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
8
|
case 'A128GCM':
|
|
15
9
|
return 128;
|
|
16
10
|
case 'A192GCM':
|
|
17
11
|
return 192;
|
|
18
12
|
case 'A256GCM':
|
|
13
|
+
case 'A128CBC-HS256':
|
|
19
14
|
return 256;
|
|
15
|
+
case 'A192CBC-HS384':
|
|
16
|
+
return 384;
|
|
17
|
+
case 'A256CBC-HS512':
|
|
18
|
+
return 512;
|
|
20
19
|
default:
|
|
21
20
|
throw new errors_js_1.JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
|
|
22
21
|
}
|
|
@@ -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,
|
|
45
|
+
const sharedSecret = await ECDH.deriveKey(epk, key, alg === 'ECDH-ES' ? joseHeader.enc : alg, alg === 'ECDH-ES' ? (0, cek_js_1.bitLength)(joseHeader.enc) : parseInt(alg.substr(-5, 3), 10), 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)
|
|
34
|
+
const sharedSecret = await ECDH.deriveKey(key, ephemeralKey, alg === 'ECDH-ES' ? enc : alg, alg === 'ECDH-ES' ? (0, cek_js_1.bitLength)(enc) : parseInt(alg.substr(-5, 3), 10), 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
|
@@ -5,24 +5,17 @@ const errors_js_1 = require("../util/errors.js");
|
|
|
5
5
|
const random_js_1 = require("../runtime/random.js");
|
|
6
6
|
function bitLength(alg) {
|
|
7
7
|
switch (alg) {
|
|
8
|
-
case 'A128CBC-HS256':
|
|
9
|
-
return 128;
|
|
10
8
|
case 'A128GCM':
|
|
11
|
-
return 96;
|
|
12
9
|
case 'A128GCMKW':
|
|
13
|
-
return 96;
|
|
14
|
-
case 'A192CBC-HS384':
|
|
15
|
-
return 128;
|
|
16
10
|
case 'A192GCM':
|
|
17
|
-
return 96;
|
|
18
11
|
case 'A192GCMKW':
|
|
19
|
-
return 96;
|
|
20
|
-
case 'A256CBC-HS512':
|
|
21
|
-
return 128;
|
|
22
12
|
case 'A256GCM':
|
|
23
|
-
return 96;
|
|
24
13
|
case 'A256GCMKW':
|
|
25
14
|
return 96;
|
|
15
|
+
case 'A128CBC-HS256':
|
|
16
|
+
case 'A192CBC-HS384':
|
|
17
|
+
case 'A256CBC-HS512':
|
|
18
|
+
return 128;
|
|
26
19
|
default:
|
|
27
20
|
throw new errors_js_1.JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
|
|
28
21
|
}
|
|
@@ -11,6 +11,7 @@ const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
|
11
11
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
12
12
|
const invalid_key_input_js_1 = require("../lib/invalid_key_input.js");
|
|
13
13
|
const ciphers_js_1 = require("./ciphers.js");
|
|
14
|
+
const is_key_like_js_1 = require("./is_key_like.js");
|
|
14
15
|
function checkKeySize(key, alg) {
|
|
15
16
|
if (key.symmetricKeySize << 3 !== parseInt(alg.substr(1, 3), 10)) {
|
|
16
17
|
throw new TypeError(`Invalid key size for alg: ${alg}`);
|
|
@@ -27,9 +28,9 @@ function ensureKeyObject(key, alg, usage) {
|
|
|
27
28
|
(0, crypto_key_js_1.checkEncCryptoKey)(key, alg, usage);
|
|
28
29
|
return crypto_1.KeyObject.from(key);
|
|
29
30
|
}
|
|
30
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(key,
|
|
31
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(key, ...is_key_like_js_1.types, 'Uint8Array'));
|
|
31
32
|
}
|
|
32
|
-
const wrap =
|
|
33
|
+
const wrap = (alg, key, cek) => {
|
|
33
34
|
const size = parseInt(alg.substr(1, 3), 10);
|
|
34
35
|
const algorithm = `aes${size}-wrap`;
|
|
35
36
|
if (!(0, ciphers_js_1.default)(algorithm)) {
|
|
@@ -41,7 +42,7 @@ const wrap = async (alg, key, cek) => {
|
|
|
41
42
|
return (0, buffer_utils_js_1.concat)(cipher.update(cek), cipher.final());
|
|
42
43
|
};
|
|
43
44
|
exports.wrap = wrap;
|
|
44
|
-
const unwrap =
|
|
45
|
+
const unwrap = (alg, key, encryptedKey) => {
|
|
45
46
|
const size = parseInt(alg.substr(1, 3), 10);
|
|
46
47
|
const algorithm = `aes${size}-wrap`;
|
|
47
48
|
if (!(0, ciphers_js_1.default)(algorithm)) {
|
|
@@ -6,6 +6,7 @@ const buffer_1 = require("buffer");
|
|
|
6
6
|
const webcrypto_js_1 = require("./webcrypto.js");
|
|
7
7
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
8
8
|
const invalid_key_input_js_1 = require("../lib/invalid_key_input.js");
|
|
9
|
+
const is_key_like_js_1 = require("./is_key_like.js");
|
|
9
10
|
const genericExport = (keyType, keyFormat, key) => {
|
|
10
11
|
let keyObject;
|
|
11
12
|
if ((0, webcrypto_js_1.isCryptoKey)(key)) {
|
|
@@ -18,7 +19,7 @@ const genericExport = (keyType, keyFormat, key) => {
|
|
|
18
19
|
keyObject = key;
|
|
19
20
|
}
|
|
20
21
|
else {
|
|
21
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(key,
|
|
22
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(key, ...is_key_like_js_1.types));
|
|
22
23
|
}
|
|
23
24
|
if (keyObject.type !== keyType) {
|
|
24
25
|
throw new TypeError(`key is not a ${keyType} key`);
|
|
@@ -12,7 +12,8 @@ const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
|
12
12
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
13
13
|
const invalid_key_input_js_1 = require("../lib/invalid_key_input.js");
|
|
14
14
|
const ciphers_js_1 = require("./ciphers.js");
|
|
15
|
-
|
|
15
|
+
const is_key_like_js_1 = require("./is_key_like.js");
|
|
16
|
+
function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
|
|
16
17
|
const keySize = parseInt(enc.substr(1, 3), 10);
|
|
17
18
|
if ((0, is_key_object_js_1.default)(cek)) {
|
|
18
19
|
cek = cek.export();
|
|
@@ -46,7 +47,7 @@ async function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
|
|
|
46
47
|
}
|
|
47
48
|
return plaintext;
|
|
48
49
|
}
|
|
49
|
-
|
|
50
|
+
function gcmDecrypt(enc, cek, ciphertext, iv, tag, aad) {
|
|
50
51
|
const keySize = parseInt(enc.substr(1, 3), 10);
|
|
51
52
|
const algorithm = `aes-${keySize}-gcm`;
|
|
52
53
|
if (!(0, ciphers_js_1.default)(algorithm)) {
|
|
@@ -66,7 +67,7 @@ async function gcmDecrypt(enc, cek, ciphertext, iv, tag, aad) {
|
|
|
66
67
|
throw new errors_js_1.JWEDecryptionFailed();
|
|
67
68
|
}
|
|
68
69
|
}
|
|
69
|
-
const decrypt =
|
|
70
|
+
const decrypt = (enc, cek, ciphertext, iv, tag, aad) => {
|
|
70
71
|
let key;
|
|
71
72
|
if ((0, webcrypto_js_1.isCryptoKey)(cek)) {
|
|
72
73
|
(0, crypto_key_js_1.checkEncCryptoKey)(cek, enc, 'decrypt');
|
|
@@ -76,7 +77,7 @@ const decrypt = async (enc, cek, ciphertext, iv, tag, aad) => {
|
|
|
76
77
|
key = cek;
|
|
77
78
|
}
|
|
78
79
|
else {
|
|
79
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(cek,
|
|
80
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(cek, ...is_key_like_js_1.types, 'Uint8Array'));
|
|
80
81
|
}
|
|
81
82
|
(0, check_cek_length_js_1.default)(enc, key);
|
|
82
83
|
(0, check_iv_length_js_1.default)(enc, iv);
|
|
@@ -11,8 +11,9 @@ const webcrypto_js_1 = require("./webcrypto.js");
|
|
|
11
11
|
const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
12
12
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
13
13
|
const invalid_key_input_js_1 = require("../lib/invalid_key_input.js");
|
|
14
|
+
const is_key_like_js_1 = require("./is_key_like.js");
|
|
14
15
|
const generateKeyPair = (0, util_1.promisify)(crypto_1.generateKeyPair);
|
|
15
|
-
const deriveKey =
|
|
16
|
+
const deriveKey = (publicKee, privateKee, algorithm, keyLength, apu = new Uint8Array(0), apv = new Uint8Array(0)) => {
|
|
16
17
|
let publicKey;
|
|
17
18
|
if ((0, webcrypto_js_1.isCryptoKey)(publicKee)) {
|
|
18
19
|
(0, crypto_key_js_1.checkEncCryptoKey)(publicKee, 'ECDH-ES');
|
|
@@ -22,7 +23,7 @@ const deriveKey = async (publicKee, privateKee, algorithm, keyLength, apu = new
|
|
|
22
23
|
publicKey = publicKee;
|
|
23
24
|
}
|
|
24
25
|
else {
|
|
25
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(publicKee,
|
|
26
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(publicKee, ...is_key_like_js_1.types));
|
|
26
27
|
}
|
|
27
28
|
let privateKey;
|
|
28
29
|
if ((0, webcrypto_js_1.isCryptoKey)(privateKee)) {
|
|
@@ -33,7 +34,7 @@ const deriveKey = async (publicKee, privateKee, algorithm, keyLength, apu = new
|
|
|
33
34
|
privateKey = privateKee;
|
|
34
35
|
}
|
|
35
36
|
else {
|
|
36
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(privateKee,
|
|
37
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(privateKee, ...is_key_like_js_1.types));
|
|
37
38
|
}
|
|
38
39
|
const value = (0, buffer_utils_js_1.concat)((0, buffer_utils_js_1.lengthAndInput)(buffer_utils_js_1.encoder.encode(algorithm)), (0, buffer_utils_js_1.lengthAndInput)(apu), (0, buffer_utils_js_1.lengthAndInput)(apv), (0, buffer_utils_js_1.uint32be)(keyLength));
|
|
39
40
|
const sharedSecret = (0, crypto_1.diffieHellman)({ privateKey, publicKey });
|
|
@@ -49,7 +50,7 @@ const generateEpk = async (kee) => {
|
|
|
49
50
|
key = kee;
|
|
50
51
|
}
|
|
51
52
|
else {
|
|
52
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(kee,
|
|
53
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(kee, ...is_key_like_js_1.types));
|
|
53
54
|
}
|
|
54
55
|
switch (key.asymmetricKeyType) {
|
|
55
56
|
case 'x25519':
|
|
@@ -11,7 +11,8 @@ const is_key_object_js_1 = require("./is_key_object.js");
|
|
|
11
11
|
const invalid_key_input_js_1 = require("../lib/invalid_key_input.js");
|
|
12
12
|
const errors_js_1 = require("../util/errors.js");
|
|
13
13
|
const ciphers_js_1 = require("./ciphers.js");
|
|
14
|
-
|
|
14
|
+
const is_key_like_js_1 = require("./is_key_like.js");
|
|
15
|
+
function cbcEncrypt(enc, plaintext, cek, iv, aad) {
|
|
15
16
|
const keySize = parseInt(enc.substr(1, 3), 10);
|
|
16
17
|
if ((0, is_key_object_js_1.default)(cek)) {
|
|
17
18
|
cek = cek.export();
|
|
@@ -28,7 +29,7 @@ async function cbcEncrypt(enc, plaintext, cek, iv, aad) {
|
|
|
28
29
|
const tag = (0, cbc_tag_js_1.default)(aad, iv, ciphertext, macSize, macKey, keySize);
|
|
29
30
|
return { ciphertext, tag };
|
|
30
31
|
}
|
|
31
|
-
|
|
32
|
+
function gcmEncrypt(enc, plaintext, cek, iv, aad) {
|
|
32
33
|
const keySize = parseInt(enc.substr(1, 3), 10);
|
|
33
34
|
const algorithm = `aes-${keySize}-gcm`;
|
|
34
35
|
if (!(0, ciphers_js_1.default)(algorithm)) {
|
|
@@ -43,7 +44,7 @@ async function gcmEncrypt(enc, plaintext, cek, iv, aad) {
|
|
|
43
44
|
const tag = cipher.getAuthTag();
|
|
44
45
|
return { ciphertext, tag };
|
|
45
46
|
}
|
|
46
|
-
const encrypt =
|
|
47
|
+
const encrypt = (enc, plaintext, cek, iv, aad) => {
|
|
47
48
|
let key;
|
|
48
49
|
if ((0, webcrypto_js_1.isCryptoKey)(cek)) {
|
|
49
50
|
(0, crypto_key_js_1.checkEncCryptoKey)(cek, enc, 'encrypt');
|
|
@@ -53,7 +54,7 @@ const encrypt = async (enc, plaintext, cek, iv, aad) => {
|
|
|
53
54
|
key = cek;
|
|
54
55
|
}
|
|
55
56
|
else {
|
|
56
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(cek,
|
|
57
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(cek, ...is_key_like_js_1.types, 'Uint8Array'));
|
|
57
58
|
}
|
|
58
59
|
(0, check_cek_length_js_1.default)(enc, key);
|
|
59
60
|
(0, check_iv_length_js_1.default)(enc, iv);
|
|
@@ -7,6 +7,7 @@ const errors_js_1 = require("../util/errors.js");
|
|
|
7
7
|
const webcrypto_js_1 = require("./webcrypto.js");
|
|
8
8
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
9
9
|
const invalid_key_input_js_1 = require("../lib/invalid_key_input.js");
|
|
10
|
+
const is_key_like_js_1 = require("./is_key_like.js");
|
|
10
11
|
const p256 = buffer_1.Buffer.from([42, 134, 72, 206, 61, 3, 1, 7]);
|
|
11
12
|
const p384 = buffer_1.Buffer.from([43, 129, 4, 0, 34]);
|
|
12
13
|
const p521 = buffer_1.Buffer.from([43, 129, 4, 0, 35]);
|
|
@@ -36,7 +37,7 @@ const getNamedCurve = (kee, raw) => {
|
|
|
36
37
|
key = kee;
|
|
37
38
|
}
|
|
38
39
|
else {
|
|
39
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(kee,
|
|
40
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(kee, ...is_key_like_js_1.types));
|
|
40
41
|
}
|
|
41
42
|
if (key.type === 'secret') {
|
|
42
43
|
throw new TypeError('only "private" or "public" type keys can be used for this operation');
|
|
@@ -5,10 +5,11 @@ const webcrypto_js_1 = require("./webcrypto.js");
|
|
|
5
5
|
const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
6
6
|
const secret_key_js_1 = require("./secret_key.js");
|
|
7
7
|
const invalid_key_input_js_1 = require("../lib/invalid_key_input.js");
|
|
8
|
+
const is_key_like_js_1 = require("./is_key_like.js");
|
|
8
9
|
function getSignVerifyKey(alg, key, usage) {
|
|
9
10
|
if (key instanceof Uint8Array) {
|
|
10
11
|
if (!alg.startsWith('HS')) {
|
|
11
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(key,
|
|
12
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(key, ...is_key_like_js_1.types));
|
|
12
13
|
}
|
|
13
14
|
return (0, secret_key_js_1.default)(key);
|
|
14
15
|
}
|
|
@@ -19,6 +20,6 @@ function getSignVerifyKey(alg, key, usage) {
|
|
|
19
20
|
(0, crypto_key_js_1.checkSigCryptoKey)(key, alg, usage);
|
|
20
21
|
return crypto_1.KeyObject.from(key);
|
|
21
22
|
}
|
|
22
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(key,
|
|
23
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(key, ...is_key_like_js_1.types, 'Uint8Array'));
|
|
23
24
|
}
|
|
24
25
|
exports.default = getSignVerifyKey;
|
|
@@ -8,6 +8,7 @@ const get_named_curve_js_1 = require("./get_named_curve.js");
|
|
|
8
8
|
const webcrypto_js_1 = require("./webcrypto.js");
|
|
9
9
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
10
10
|
const invalid_key_input_js_1 = require("../lib/invalid_key_input.js");
|
|
11
|
+
const is_key_like_js_1 = require("./is_key_like.js");
|
|
11
12
|
const [major, minor] = process.version
|
|
12
13
|
.substr(1)
|
|
13
14
|
.split('.')
|
|
@@ -31,7 +32,7 @@ const keyToJWK = (key) => {
|
|
|
31
32
|
};
|
|
32
33
|
}
|
|
33
34
|
else {
|
|
34
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(key,
|
|
35
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(key, ...is_key_like_js_1.types, 'Uint8Array'));
|
|
35
36
|
}
|
|
36
37
|
if (jwkExportSupported) {
|
|
37
38
|
return keyObject.export({ format: 'jwk' });
|
|
@@ -12,6 +12,7 @@ const webcrypto_js_1 = require("./webcrypto.js");
|
|
|
12
12
|
const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
13
13
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
14
14
|
const invalid_key_input_js_1 = require("../lib/invalid_key_input.js");
|
|
15
|
+
const is_key_like_js_1 = require("./is_key_like.js");
|
|
15
16
|
const pbkdf2 = (0, util_1.promisify)(crypto_1.pbkdf2);
|
|
16
17
|
function getPassword(key, alg) {
|
|
17
18
|
if ((0, is_key_object_js_1.default)(key)) {
|
|
@@ -24,7 +25,7 @@ function getPassword(key, alg) {
|
|
|
24
25
|
(0, crypto_key_js_1.checkEncCryptoKey)(key, alg, 'deriveBits', 'deriveKey');
|
|
25
26
|
return crypto_1.KeyObject.from(key).export();
|
|
26
27
|
}
|
|
27
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(key,
|
|
28
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(key, ...is_key_like_js_1.types, 'Uint8Array'));
|
|
28
29
|
}
|
|
29
30
|
const encrypt = async (alg, key, cek, p2c = Math.floor(Math.random() * 2049) + 2048, p2s = (0, random_js_1.default)(new Uint8Array(16))) => {
|
|
30
31
|
(0, check_p2s_js_1.default)(p2s);
|
|
@@ -7,6 +7,7 @@ const webcrypto_js_1 = require("./webcrypto.js");
|
|
|
7
7
|
const crypto_key_js_1 = require("../lib/crypto_key.js");
|
|
8
8
|
const is_key_object_js_1 = require("./is_key_object.js");
|
|
9
9
|
const invalid_key_input_js_1 = require("../lib/invalid_key_input.js");
|
|
10
|
+
const is_key_like_js_1 = require("./is_key_like.js");
|
|
10
11
|
const checkKey = (key, alg) => {
|
|
11
12
|
if (key.asymmetricKeyType !== 'rsa') {
|
|
12
13
|
throw new TypeError('Invalid key for this operation, its asymmetricKeyType must be rsa');
|
|
@@ -48,9 +49,9 @@ function ensureKeyObject(key, alg, ...usages) {
|
|
|
48
49
|
(0, crypto_key_js_1.checkEncCryptoKey)(key, alg, ...usages);
|
|
49
50
|
return crypto_1.KeyObject.from(key);
|
|
50
51
|
}
|
|
51
|
-
throw new TypeError((0, invalid_key_input_js_1.default)(key,
|
|
52
|
+
throw new TypeError((0, invalid_key_input_js_1.default)(key, ...is_key_like_js_1.types));
|
|
52
53
|
}
|
|
53
|
-
const encrypt =
|
|
54
|
+
const encrypt = (alg, key, cek) => {
|
|
54
55
|
const padding = resolvePadding(alg);
|
|
55
56
|
const oaepHash = resolveOaepHash(alg);
|
|
56
57
|
const keyObject = ensureKeyObject(key, alg, 'wrapKey', 'encrypt');
|
|
@@ -58,7 +59,7 @@ const encrypt = async (alg, key, cek) => {
|
|
|
58
59
|
return (0, crypto_1.publicEncrypt)({ key: keyObject, oaepHash, padding }, cek);
|
|
59
60
|
};
|
|
60
61
|
exports.encrypt = encrypt;
|
|
61
|
-
const decrypt =
|
|
62
|
+
const decrypt = (alg, key, encryptedKey) => {
|
|
62
63
|
const padding = resolvePadding(alg);
|
|
63
64
|
const oaepHash = resolveOaepHash(alg);
|
|
64
65
|
const keyObject = ensureKeyObject(key, alg, 'unwrapKey', 'decrypt');
|
|
@@ -8,18 +8,13 @@ import validateCrit from '../../lib/validate_crit.js';
|
|
|
8
8
|
const recipientRef = new WeakMap();
|
|
9
9
|
class IndividualRecipient {
|
|
10
10
|
setUnprotectedHeader(unprotectedHeader) {
|
|
11
|
-
|
|
11
|
+
const ref = recipientRef.get(this);
|
|
12
|
+
if (ref.unprotectedHeader) {
|
|
12
13
|
throw new TypeError('setUnprotectedHeader can only be called once');
|
|
13
14
|
}
|
|
14
|
-
|
|
15
|
+
ref.unprotectedHeader = unprotectedHeader;
|
|
15
16
|
return this;
|
|
16
17
|
}
|
|
17
|
-
set _unprotectedHeader(value) {
|
|
18
|
-
recipientRef.get(this).unprotectedHeader = value;
|
|
19
|
-
}
|
|
20
|
-
get _unprotectedHeader() {
|
|
21
|
-
return recipientRef.get(this).unprotectedHeader;
|
|
22
|
-
}
|
|
23
18
|
}
|
|
24
19
|
export class GeneralEncrypt {
|
|
25
20
|
constructor(plaintext) {
|
|
@@ -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
|
-
|
|
6
|
+
const ref = signatureRef.get(this);
|
|
7
|
+
if (ref.protectedHeader) {
|
|
7
8
|
throw new TypeError('setProtectedHeader can only be called once');
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
+
ref.protectedHeader = protectedHeader;
|
|
10
11
|
return this;
|
|
11
12
|
}
|
|
12
13
|
setUnprotectedHeader(unprotectedHeader) {
|
|
13
|
-
|
|
14
|
+
const ref = signatureRef.get(this);
|
|
15
|
+
if (ref.unprotectedHeader) {
|
|
14
16
|
throw new TypeError('setUnprotectedHeader can only be called once');
|
|
15
17
|
}
|
|
16
|
-
|
|
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) {
|
package/dist/node/esm/lib/cek.js
CHANGED
|
@@ -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)
|
|
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)
|
|
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);
|
package/dist/node/esm/lib/iv.js
CHANGED
|
@@ -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,
|
|
28
|
+
throw new TypeError(invalidKeyInput(key, ...types, 'Uint8Array'));
|
|
28
29
|
}
|
|
29
|
-
export const wrap =
|
|
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 =
|
|
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,
|
|
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
|
-
|
|
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();
|
|
@@ -44,7 +45,7 @@ async function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
|
|
|
44
45
|
}
|
|
45
46
|
return plaintext;
|
|
46
47
|
}
|
|
47
|
-
|
|
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)) {
|
|
@@ -64,7 +65,7 @@ async function gcmDecrypt(enc, cek, ciphertext, iv, tag, aad) {
|
|
|
64
65
|
throw new JWEDecryptionFailed();
|
|
65
66
|
}
|
|
66
67
|
}
|
|
67
|
-
const decrypt =
|
|
68
|
+
const decrypt = (enc, cek, ciphertext, iv, tag, aad) => {
|
|
68
69
|
let key;
|
|
69
70
|
if (isCryptoKey(cek)) {
|
|
70
71
|
checkEncCryptoKey(cek, enc, 'decrypt');
|
|
@@ -74,7 +75,7 @@ const decrypt = async (enc, cek, ciphertext, iv, tag, aad) => {
|
|
|
74
75
|
key = cek;
|
|
75
76
|
}
|
|
76
77
|
else {
|
|
77
|
-
throw new TypeError(invalidKeyInput(cek,
|
|
78
|
+
throw new TypeError(invalidKeyInput(cek, ...types, 'Uint8Array'));
|
|
78
79
|
}
|
|
79
80
|
checkCekLength(enc, key);
|
|
80
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 =
|
|
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,
|
|
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,
|
|
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,
|
|
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
|
-
|
|
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
|
-
|
|
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)) {
|
|
@@ -41,7 +42,7 @@ async function gcmEncrypt(enc, plaintext, cek, iv, aad) {
|
|
|
41
42
|
const tag = cipher.getAuthTag();
|
|
42
43
|
return { ciphertext, tag };
|
|
43
44
|
}
|
|
44
|
-
const encrypt =
|
|
45
|
+
const encrypt = (enc, plaintext, cek, iv, aad) => {
|
|
45
46
|
let key;
|
|
46
47
|
if (isCryptoKey(cek)) {
|
|
47
48
|
checkEncCryptoKey(cek, enc, 'encrypt');
|
|
@@ -51,7 +52,7 @@ const encrypt = async (enc, plaintext, cek, iv, aad) => {
|
|
|
51
52
|
key = cek;
|
|
52
53
|
}
|
|
53
54
|
else {
|
|
54
|
-
throw new TypeError(invalidKeyInput(cek,
|
|
55
|
+
throw new TypeError(invalidKeyInput(cek, ...types, 'Uint8Array'));
|
|
55
56
|
}
|
|
56
57
|
checkCekLength(enc, key);
|
|
57
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
49
|
+
throw new TypeError(invalidKeyInput(key, ...types));
|
|
49
50
|
}
|
|
50
|
-
export const encrypt =
|
|
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 =
|
|
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');
|