jose 6.2.0 → 6.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/webapi/jwe/flattened/decrypt.js +11 -36
- package/dist/webapi/jwe/flattened/encrypt.js +10 -22
- package/dist/webapi/jwe/general/decrypt.js +1 -1
- package/dist/webapi/jwe/general/encrypt.js +8 -16
- package/dist/webapi/jwk/embedded.js +1 -1
- package/dist/webapi/jwk/thumbprint.js +2 -2
- package/dist/webapi/jwks/local.js +1 -1
- package/dist/webapi/jwks/remote.js +2 -2
- package/dist/webapi/jws/flattened/sign.js +5 -8
- package/dist/webapi/jws/flattened/verify.js +6 -16
- package/dist/webapi/jws/general/sign.js +3 -6
- package/dist/webapi/jws/general/verify.js +1 -1
- package/dist/webapi/jwt/encrypt.js +5 -12
- package/dist/webapi/key/import.js +1 -1
- package/dist/webapi/lib/aesgcmkw.js +1 -2
- package/dist/webapi/lib/check_key_type.js +1 -1
- package/dist/webapi/lib/content_encryption.js +217 -0
- package/dist/webapi/lib/crypto_key.js +9 -16
- package/dist/webapi/lib/ecdhes.js +1 -1
- package/dist/webapi/lib/helpers.js +19 -0
- package/dist/webapi/lib/jwk_to_key.js +9 -11
- package/dist/webapi/lib/jwt_claims_set.js +1 -1
- package/dist/webapi/lib/key_management.js +186 -0
- package/dist/webapi/lib/normalize_key.js +10 -20
- package/dist/webapi/lib/rsaes.js +1 -1
- package/dist/webapi/lib/signing.js +68 -0
- package/dist/webapi/lib/type_checks.js +40 -0
- package/dist/webapi/util/decode_jwt.js +1 -1
- package/dist/webapi/util/decode_protected_header.js +1 -1
- package/package.json +1 -1
- package/dist/webapi/lib/cek.js +0 -19
- package/dist/webapi/lib/check_cek_length.js +0 -7
- package/dist/webapi/lib/check_iv_length.js +0 -7
- package/dist/webapi/lib/check_key_length.js +0 -8
- package/dist/webapi/lib/decrypt.js +0 -106
- package/dist/webapi/lib/decrypt_key_management.js +0 -127
- package/dist/webapi/lib/digest.js +0 -4
- package/dist/webapi/lib/encrypt.js +0 -74
- package/dist/webapi/lib/encrypt_key_management.js +0 -92
- package/dist/webapi/lib/get_sign_verify_key.js +0 -12
- package/dist/webapi/lib/is_disjoint.js +0 -21
- package/dist/webapi/lib/is_jwk.js +0 -6
- package/dist/webapi/lib/is_object.js +0 -14
- package/dist/webapi/lib/iv.js +0 -19
- package/dist/webapi/lib/private_symbols.js +0 -1
- package/dist/webapi/lib/sign.js +0 -9
- package/dist/webapi/lib/subtle_dsa.js +0 -31
- package/dist/webapi/lib/verify.js +0 -14
|
@@ -3,6 +3,11 @@ const isAlgorithm = (algorithm, name) => algorithm.name === name;
|
|
|
3
3
|
function getHashLength(hash) {
|
|
4
4
|
return parseInt(hash.name.slice(4), 10);
|
|
5
5
|
}
|
|
6
|
+
function checkHashLength(algorithm, expected) {
|
|
7
|
+
const actual = getHashLength(algorithm.hash);
|
|
8
|
+
if (actual !== expected)
|
|
9
|
+
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
|
10
|
+
}
|
|
6
11
|
function getNamedCurve(alg) {
|
|
7
12
|
switch (alg) {
|
|
8
13
|
case 'ES256':
|
|
@@ -27,10 +32,7 @@ export function checkSigCryptoKey(key, alg, usage) {
|
|
|
27
32
|
case 'HS512': {
|
|
28
33
|
if (!isAlgorithm(key.algorithm, 'HMAC'))
|
|
29
34
|
throw unusable('HMAC');
|
|
30
|
-
|
|
31
|
-
const actual = getHashLength(key.algorithm.hash);
|
|
32
|
-
if (actual !== expected)
|
|
33
|
-
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
|
35
|
+
checkHashLength(key.algorithm, parseInt(alg.slice(2), 10));
|
|
34
36
|
break;
|
|
35
37
|
}
|
|
36
38
|
case 'RS256':
|
|
@@ -38,10 +40,7 @@ export function checkSigCryptoKey(key, alg, usage) {
|
|
|
38
40
|
case 'RS512': {
|
|
39
41
|
if (!isAlgorithm(key.algorithm, 'RSASSA-PKCS1-v1_5'))
|
|
40
42
|
throw unusable('RSASSA-PKCS1-v1_5');
|
|
41
|
-
|
|
42
|
-
const actual = getHashLength(key.algorithm.hash);
|
|
43
|
-
if (actual !== expected)
|
|
44
|
-
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
|
43
|
+
checkHashLength(key.algorithm, parseInt(alg.slice(2), 10));
|
|
45
44
|
break;
|
|
46
45
|
}
|
|
47
46
|
case 'PS256':
|
|
@@ -49,10 +48,7 @@ export function checkSigCryptoKey(key, alg, usage) {
|
|
|
49
48
|
case 'PS512': {
|
|
50
49
|
if (!isAlgorithm(key.algorithm, 'RSA-PSS'))
|
|
51
50
|
throw unusable('RSA-PSS');
|
|
52
|
-
|
|
53
|
-
const actual = getHashLength(key.algorithm.hash);
|
|
54
|
-
if (actual !== expected)
|
|
55
|
-
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
|
51
|
+
checkHashLength(key.algorithm, parseInt(alg.slice(2), 10));
|
|
56
52
|
break;
|
|
57
53
|
}
|
|
58
54
|
case 'Ed25519':
|
|
@@ -130,10 +126,7 @@ export function checkEncCryptoKey(key, alg, usage) {
|
|
|
130
126
|
case 'RSA-OAEP-512': {
|
|
131
127
|
if (!isAlgorithm(key.algorithm, 'RSA-OAEP'))
|
|
132
128
|
throw unusable('RSA-OAEP');
|
|
133
|
-
|
|
134
|
-
const actual = getHashLength(key.algorithm.hash);
|
|
135
|
-
if (actual !== expected)
|
|
136
|
-
throw unusable(`SHA-${expected}`, 'algorithm.hash');
|
|
129
|
+
checkHashLength(key.algorithm, parseInt(alg.slice(9), 10) || 1);
|
|
137
130
|
break;
|
|
138
131
|
}
|
|
139
132
|
default:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { encode, concat, uint32be } from './buffer_utils.js';
|
|
2
2
|
import { checkEncCryptoKey } from './crypto_key.js';
|
|
3
|
-
import { digest } from './
|
|
3
|
+
import { digest } from './helpers.js';
|
|
4
4
|
function lengthAndInput(input) {
|
|
5
5
|
return concat(uint32be(input.length), input);
|
|
6
6
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { decode } from '../util/base64url.js';
|
|
2
|
+
export const unprotected = Symbol();
|
|
3
|
+
export function assertNotSet(value, name) {
|
|
4
|
+
if (value) {
|
|
5
|
+
throw new TypeError(`${name} can only be called once`);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function decodeBase64url(value, label, ErrorClass) {
|
|
9
|
+
try {
|
|
10
|
+
return decode(value);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
throw new ErrorClass(`Failed to base64url decode the ${label}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export async function digest(algorithm, data) {
|
|
17
|
+
const subtleDigest = `SHA-${algorithm.slice(-3)}`;
|
|
18
|
+
return new Uint8Array(await crypto.subtle.digest(subtleDigest, data));
|
|
19
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { JOSENotSupported } from '../util/errors.js';
|
|
2
|
+
const unsupportedAlg = 'Invalid or unsupported JWK "alg" (Algorithm) Parameter value';
|
|
2
3
|
function subtleMapping(jwk) {
|
|
3
4
|
let algorithm;
|
|
4
5
|
let keyUsages;
|
|
@@ -12,7 +13,7 @@ function subtleMapping(jwk) {
|
|
|
12
13
|
keyUsages = jwk.priv ? ['sign'] : ['verify'];
|
|
13
14
|
break;
|
|
14
15
|
default:
|
|
15
|
-
throw new JOSENotSupported(
|
|
16
|
+
throw new JOSENotSupported(unsupportedAlg);
|
|
16
17
|
}
|
|
17
18
|
break;
|
|
18
19
|
}
|
|
@@ -41,22 +42,19 @@ function subtleMapping(jwk) {
|
|
|
41
42
|
keyUsages = jwk.d ? ['decrypt', 'unwrapKey'] : ['encrypt', 'wrapKey'];
|
|
42
43
|
break;
|
|
43
44
|
default:
|
|
44
|
-
throw new JOSENotSupported(
|
|
45
|
+
throw new JOSENotSupported(unsupportedAlg);
|
|
45
46
|
}
|
|
46
47
|
break;
|
|
47
48
|
}
|
|
48
49
|
case 'EC': {
|
|
49
50
|
switch (jwk.alg) {
|
|
50
51
|
case 'ES256':
|
|
51
|
-
algorithm = { name: 'ECDSA', namedCurve: 'P-256' };
|
|
52
|
-
keyUsages = jwk.d ? ['sign'] : ['verify'];
|
|
53
|
-
break;
|
|
54
52
|
case 'ES384':
|
|
55
|
-
algorithm = { name: 'ECDSA', namedCurve: 'P-384' };
|
|
56
|
-
keyUsages = jwk.d ? ['sign'] : ['verify'];
|
|
57
|
-
break;
|
|
58
53
|
case 'ES512':
|
|
59
|
-
algorithm = {
|
|
54
|
+
algorithm = {
|
|
55
|
+
name: 'ECDSA',
|
|
56
|
+
namedCurve: { ES256: 'P-256', ES384: 'P-384', ES512: 'P-521' }[jwk.alg],
|
|
57
|
+
};
|
|
60
58
|
keyUsages = jwk.d ? ['sign'] : ['verify'];
|
|
61
59
|
break;
|
|
62
60
|
case 'ECDH-ES':
|
|
@@ -67,7 +65,7 @@ function subtleMapping(jwk) {
|
|
|
67
65
|
keyUsages = jwk.d ? ['deriveBits'] : [];
|
|
68
66
|
break;
|
|
69
67
|
default:
|
|
70
|
-
throw new JOSENotSupported(
|
|
68
|
+
throw new JOSENotSupported(unsupportedAlg);
|
|
71
69
|
}
|
|
72
70
|
break;
|
|
73
71
|
}
|
|
@@ -86,7 +84,7 @@ function subtleMapping(jwk) {
|
|
|
86
84
|
keyUsages = jwk.d ? ['deriveBits'] : [];
|
|
87
85
|
break;
|
|
88
86
|
default:
|
|
89
|
-
throw new JOSENotSupported(
|
|
87
|
+
throw new JOSENotSupported(unsupportedAlg);
|
|
90
88
|
}
|
|
91
89
|
break;
|
|
92
90
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { JWTClaimValidationFailed, JWTExpired, JWTInvalid } from '../util/errors.js';
|
|
2
2
|
import { encoder, decoder } from './buffer_utils.js';
|
|
3
|
-
import { isObject } from './
|
|
3
|
+
import { isObject } from './type_checks.js';
|
|
4
4
|
const epoch = (date) => Math.floor(date.getTime() / 1000);
|
|
5
5
|
const minute = 60;
|
|
6
6
|
const hour = minute * 60;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import * as aeskw from './aeskw.js';
|
|
2
|
+
import * as ecdhes from './ecdhes.js';
|
|
3
|
+
import * as pbes2kw from './pbes2kw.js';
|
|
4
|
+
import * as rsaes from './rsaes.js';
|
|
5
|
+
import { encode as b64u } from '../util/base64url.js';
|
|
6
|
+
import { normalizeKey } from './normalize_key.js';
|
|
7
|
+
import { JOSENotSupported, JWEInvalid } from '../util/errors.js';
|
|
8
|
+
import { decodeBase64url } from './helpers.js';
|
|
9
|
+
import { generateCek, cekLength } from './content_encryption.js';
|
|
10
|
+
import { importJWK } from '../key/import.js';
|
|
11
|
+
import { exportJWK } from '../key/export.js';
|
|
12
|
+
import { isObject } from './type_checks.js';
|
|
13
|
+
import { wrap as aesGcmKwWrap, unwrap as aesGcmKwUnwrap } from './aesgcmkw.js';
|
|
14
|
+
import { assertCryptoKey } from './is_key_like.js';
|
|
15
|
+
const unsupportedAlgHeader = 'Invalid or unsupported "alg" (JWE Algorithm) header value';
|
|
16
|
+
function assertEncryptedKey(encryptedKey) {
|
|
17
|
+
if (encryptedKey === undefined)
|
|
18
|
+
throw new JWEInvalid('JWE Encrypted Key missing');
|
|
19
|
+
}
|
|
20
|
+
export async function decryptKeyManagement(alg, key, encryptedKey, joseHeader, options) {
|
|
21
|
+
switch (alg) {
|
|
22
|
+
case 'dir': {
|
|
23
|
+
if (encryptedKey !== undefined)
|
|
24
|
+
throw new JWEInvalid('Encountered unexpected JWE Encrypted Key');
|
|
25
|
+
return key;
|
|
26
|
+
}
|
|
27
|
+
case 'ECDH-ES':
|
|
28
|
+
if (encryptedKey !== undefined)
|
|
29
|
+
throw new JWEInvalid('Encountered unexpected JWE Encrypted Key');
|
|
30
|
+
case 'ECDH-ES+A128KW':
|
|
31
|
+
case 'ECDH-ES+A192KW':
|
|
32
|
+
case 'ECDH-ES+A256KW': {
|
|
33
|
+
if (!isObject(joseHeader.epk))
|
|
34
|
+
throw new JWEInvalid(`JOSE Header "epk" (Ephemeral Public Key) missing or invalid`);
|
|
35
|
+
assertCryptoKey(key);
|
|
36
|
+
if (!ecdhes.allowed(key))
|
|
37
|
+
throw new JOSENotSupported('ECDH with the provided key is not allowed or not supported by your javascript runtime');
|
|
38
|
+
const epk = await importJWK(joseHeader.epk, alg);
|
|
39
|
+
assertCryptoKey(epk);
|
|
40
|
+
let partyUInfo;
|
|
41
|
+
let partyVInfo;
|
|
42
|
+
if (joseHeader.apu !== undefined) {
|
|
43
|
+
if (typeof joseHeader.apu !== 'string')
|
|
44
|
+
throw new JWEInvalid(`JOSE Header "apu" (Agreement PartyUInfo) invalid`);
|
|
45
|
+
partyUInfo = decodeBase64url(joseHeader.apu, 'apu', JWEInvalid);
|
|
46
|
+
}
|
|
47
|
+
if (joseHeader.apv !== undefined) {
|
|
48
|
+
if (typeof joseHeader.apv !== 'string')
|
|
49
|
+
throw new JWEInvalid(`JOSE Header "apv" (Agreement PartyVInfo) invalid`);
|
|
50
|
+
partyVInfo = decodeBase64url(joseHeader.apv, 'apv', JWEInvalid);
|
|
51
|
+
}
|
|
52
|
+
const sharedSecret = await ecdhes.deriveKey(epk, key, alg === 'ECDH-ES' ? joseHeader.enc : alg, alg === 'ECDH-ES' ? cekLength(joseHeader.enc) : parseInt(alg.slice(-5, -2), 10), partyUInfo, partyVInfo);
|
|
53
|
+
if (alg === 'ECDH-ES')
|
|
54
|
+
return sharedSecret;
|
|
55
|
+
assertEncryptedKey(encryptedKey);
|
|
56
|
+
return aeskw.unwrap(alg.slice(-6), sharedSecret, encryptedKey);
|
|
57
|
+
}
|
|
58
|
+
case 'RSA-OAEP':
|
|
59
|
+
case 'RSA-OAEP-256':
|
|
60
|
+
case 'RSA-OAEP-384':
|
|
61
|
+
case 'RSA-OAEP-512': {
|
|
62
|
+
assertEncryptedKey(encryptedKey);
|
|
63
|
+
assertCryptoKey(key);
|
|
64
|
+
return rsaes.decrypt(alg, key, encryptedKey);
|
|
65
|
+
}
|
|
66
|
+
case 'PBES2-HS256+A128KW':
|
|
67
|
+
case 'PBES2-HS384+A192KW':
|
|
68
|
+
case 'PBES2-HS512+A256KW': {
|
|
69
|
+
assertEncryptedKey(encryptedKey);
|
|
70
|
+
if (typeof joseHeader.p2c !== 'number')
|
|
71
|
+
throw new JWEInvalid(`JOSE Header "p2c" (PBES2 Count) missing or invalid`);
|
|
72
|
+
const p2cLimit = options?.maxPBES2Count || 10_000;
|
|
73
|
+
if (joseHeader.p2c > p2cLimit)
|
|
74
|
+
throw new JWEInvalid(`JOSE Header "p2c" (PBES2 Count) out is of acceptable bounds`);
|
|
75
|
+
if (typeof joseHeader.p2s !== 'string')
|
|
76
|
+
throw new JWEInvalid(`JOSE Header "p2s" (PBES2 Salt) missing or invalid`);
|
|
77
|
+
let p2s;
|
|
78
|
+
p2s = decodeBase64url(joseHeader.p2s, 'p2s', JWEInvalid);
|
|
79
|
+
return pbes2kw.unwrap(alg, key, encryptedKey, joseHeader.p2c, p2s);
|
|
80
|
+
}
|
|
81
|
+
case 'A128KW':
|
|
82
|
+
case 'A192KW':
|
|
83
|
+
case 'A256KW': {
|
|
84
|
+
assertEncryptedKey(encryptedKey);
|
|
85
|
+
return aeskw.unwrap(alg, key, encryptedKey);
|
|
86
|
+
}
|
|
87
|
+
case 'A128GCMKW':
|
|
88
|
+
case 'A192GCMKW':
|
|
89
|
+
case 'A256GCMKW': {
|
|
90
|
+
assertEncryptedKey(encryptedKey);
|
|
91
|
+
if (typeof joseHeader.iv !== 'string')
|
|
92
|
+
throw new JWEInvalid(`JOSE Header "iv" (Initialization Vector) missing or invalid`);
|
|
93
|
+
if (typeof joseHeader.tag !== 'string')
|
|
94
|
+
throw new JWEInvalid(`JOSE Header "tag" (Authentication Tag) missing or invalid`);
|
|
95
|
+
let iv;
|
|
96
|
+
iv = decodeBase64url(joseHeader.iv, 'iv', JWEInvalid);
|
|
97
|
+
let tag;
|
|
98
|
+
tag = decodeBase64url(joseHeader.tag, 'tag', JWEInvalid);
|
|
99
|
+
return aesGcmKwUnwrap(alg, key, encryptedKey, iv, tag);
|
|
100
|
+
}
|
|
101
|
+
default: {
|
|
102
|
+
throw new JOSENotSupported(unsupportedAlgHeader);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
export async function encryptKeyManagement(alg, enc, key, providedCek, providedParameters = {}) {
|
|
107
|
+
let encryptedKey;
|
|
108
|
+
let parameters;
|
|
109
|
+
let cek;
|
|
110
|
+
switch (alg) {
|
|
111
|
+
case 'dir': {
|
|
112
|
+
cek = key;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
case 'ECDH-ES':
|
|
116
|
+
case 'ECDH-ES+A128KW':
|
|
117
|
+
case 'ECDH-ES+A192KW':
|
|
118
|
+
case 'ECDH-ES+A256KW': {
|
|
119
|
+
assertCryptoKey(key);
|
|
120
|
+
if (!ecdhes.allowed(key)) {
|
|
121
|
+
throw new JOSENotSupported('ECDH with the provided key is not allowed or not supported by your javascript runtime');
|
|
122
|
+
}
|
|
123
|
+
const { apu, apv } = providedParameters;
|
|
124
|
+
let ephemeralKey;
|
|
125
|
+
if (providedParameters.epk) {
|
|
126
|
+
ephemeralKey = (await normalizeKey(providedParameters.epk, alg));
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
ephemeralKey = (await crypto.subtle.generateKey(key.algorithm, true, ['deriveBits'])).privateKey;
|
|
130
|
+
}
|
|
131
|
+
const { x, y, crv, kty } = await exportJWK(ephemeralKey);
|
|
132
|
+
const sharedSecret = await ecdhes.deriveKey(key, ephemeralKey, alg === 'ECDH-ES' ? enc : alg, alg === 'ECDH-ES' ? cekLength(enc) : parseInt(alg.slice(-5, -2), 10), apu, apv);
|
|
133
|
+
parameters = { epk: { x, crv, kty } };
|
|
134
|
+
if (kty === 'EC')
|
|
135
|
+
parameters.epk.y = y;
|
|
136
|
+
if (apu)
|
|
137
|
+
parameters.apu = b64u(apu);
|
|
138
|
+
if (apv)
|
|
139
|
+
parameters.apv = b64u(apv);
|
|
140
|
+
if (alg === 'ECDH-ES') {
|
|
141
|
+
cek = sharedSecret;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
cek = providedCek || generateCek(enc);
|
|
145
|
+
const kwAlg = alg.slice(-6);
|
|
146
|
+
encryptedKey = await aeskw.wrap(kwAlg, sharedSecret, cek);
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
case 'RSA-OAEP':
|
|
150
|
+
case 'RSA-OAEP-256':
|
|
151
|
+
case 'RSA-OAEP-384':
|
|
152
|
+
case 'RSA-OAEP-512': {
|
|
153
|
+
cek = providedCek || generateCek(enc);
|
|
154
|
+
assertCryptoKey(key);
|
|
155
|
+
encryptedKey = await rsaes.encrypt(alg, key, cek);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case 'PBES2-HS256+A128KW':
|
|
159
|
+
case 'PBES2-HS384+A192KW':
|
|
160
|
+
case 'PBES2-HS512+A256KW': {
|
|
161
|
+
cek = providedCek || generateCek(enc);
|
|
162
|
+
const { p2c, p2s } = providedParameters;
|
|
163
|
+
({ encryptedKey, ...parameters } = await pbes2kw.wrap(alg, key, cek, p2c, p2s));
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
case 'A128KW':
|
|
167
|
+
case 'A192KW':
|
|
168
|
+
case 'A256KW': {
|
|
169
|
+
cek = providedCek || generateCek(enc);
|
|
170
|
+
encryptedKey = await aeskw.wrap(alg, key, cek);
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
case 'A128GCMKW':
|
|
174
|
+
case 'A192GCMKW':
|
|
175
|
+
case 'A256GCMKW': {
|
|
176
|
+
cek = providedCek || generateCek(enc);
|
|
177
|
+
const { iv } = providedParameters;
|
|
178
|
+
({ encryptedKey, ...parameters } = await aesGcmKwWrap(alg, key, cek, iv));
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
default: {
|
|
182
|
+
throw new JOSENotSupported(unsupportedAlgHeader);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return { cek, encryptedKey, parameters };
|
|
186
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { isJWK } from './
|
|
1
|
+
import { isJWK } from './type_checks.js';
|
|
2
2
|
import { decode } from '../util/base64url.js';
|
|
3
3
|
import { jwkToKey } from './jwk_to_key.js';
|
|
4
4
|
import { isCryptoKey, isKeyObject } from './is_key_like.js';
|
|
5
|
+
const unusableForAlg = 'given KeyObject instance cannot be used for this algorithm';
|
|
5
6
|
let cache;
|
|
6
7
|
const handleJWK = async (key, jwk, alg, freeze = false) => {
|
|
7
8
|
cache ||= new WeakMap();
|
|
@@ -37,13 +38,13 @@ const handleKeyObject = (keyObject, alg) => {
|
|
|
37
38
|
case 'ECDH-ES+A256KW':
|
|
38
39
|
break;
|
|
39
40
|
default:
|
|
40
|
-
throw new TypeError(
|
|
41
|
+
throw new TypeError(unusableForAlg);
|
|
41
42
|
}
|
|
42
43
|
cryptoKey = keyObject.toCryptoKey(keyObject.asymmetricKeyType, extractable, isPublic ? [] : ['deriveBits']);
|
|
43
44
|
}
|
|
44
45
|
if (keyObject.asymmetricKeyType === 'ed25519') {
|
|
45
46
|
if (alg !== 'EdDSA' && alg !== 'Ed25519') {
|
|
46
|
-
throw new TypeError(
|
|
47
|
+
throw new TypeError(unusableForAlg);
|
|
47
48
|
}
|
|
48
49
|
cryptoKey = keyObject.toCryptoKey(keyObject.asymmetricKeyType, extractable, [
|
|
49
50
|
isPublic ? 'verify' : 'sign',
|
|
@@ -54,7 +55,7 @@ const handleKeyObject = (keyObject, alg) => {
|
|
|
54
55
|
case 'ml-dsa-65':
|
|
55
56
|
case 'ml-dsa-87': {
|
|
56
57
|
if (alg !== keyObject.asymmetricKeyType.toUpperCase()) {
|
|
57
|
-
throw new TypeError(
|
|
58
|
+
throw new TypeError(unusableForAlg);
|
|
58
59
|
}
|
|
59
60
|
cryptoKey = keyObject.toCryptoKey(keyObject.asymmetricKeyType, extractable, [
|
|
60
61
|
isPublic ? 'verify' : 'sign',
|
|
@@ -83,7 +84,7 @@ const handleKeyObject = (keyObject, alg) => {
|
|
|
83
84
|
hash = 'SHA-512';
|
|
84
85
|
break;
|
|
85
86
|
default:
|
|
86
|
-
throw new TypeError(
|
|
87
|
+
throw new TypeError(unusableForAlg);
|
|
87
88
|
}
|
|
88
89
|
if (alg.startsWith('RSA-OAEP')) {
|
|
89
90
|
return keyObject.toCryptoKey({
|
|
@@ -104,21 +105,10 @@ const handleKeyObject = (keyObject, alg) => {
|
|
|
104
105
|
]);
|
|
105
106
|
const namedCurve = nist.get(keyObject.asymmetricKeyDetails?.namedCurve);
|
|
106
107
|
if (!namedCurve) {
|
|
107
|
-
throw new TypeError(
|
|
108
|
+
throw new TypeError(unusableForAlg);
|
|
108
109
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
name: 'ECDSA',
|
|
112
|
-
namedCurve,
|
|
113
|
-
}, extractable, [isPublic ? 'verify' : 'sign']);
|
|
114
|
-
}
|
|
115
|
-
if (alg === 'ES384' && namedCurve === 'P-384') {
|
|
116
|
-
cryptoKey = keyObject.toCryptoKey({
|
|
117
|
-
name: 'ECDSA',
|
|
118
|
-
namedCurve,
|
|
119
|
-
}, extractable, [isPublic ? 'verify' : 'sign']);
|
|
120
|
-
}
|
|
121
|
-
if (alg === 'ES512' && namedCurve === 'P-521') {
|
|
110
|
+
const expectedCurve = { ES256: 'P-256', ES384: 'P-384', ES512: 'P-521' };
|
|
111
|
+
if (expectedCurve[alg] && namedCurve === expectedCurve[alg]) {
|
|
122
112
|
cryptoKey = keyObject.toCryptoKey({
|
|
123
113
|
name: 'ECDSA',
|
|
124
114
|
namedCurve,
|
|
@@ -132,7 +122,7 @@ const handleKeyObject = (keyObject, alg) => {
|
|
|
132
122
|
}
|
|
133
123
|
}
|
|
134
124
|
if (!cryptoKey) {
|
|
135
|
-
throw new TypeError(
|
|
125
|
+
throw new TypeError(unusableForAlg);
|
|
136
126
|
}
|
|
137
127
|
if (!cached) {
|
|
138
128
|
cache.set(keyObject, { [alg]: cryptoKey });
|
package/dist/webapi/lib/rsaes.js
CHANGED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { JOSENotSupported } from '../util/errors.js';
|
|
2
|
+
import { checkSigCryptoKey } from './crypto_key.js';
|
|
3
|
+
import { invalidKeyInput } from './invalid_key_input.js';
|
|
4
|
+
export function checkKeyLength(alg, key) {
|
|
5
|
+
if (alg.startsWith('RS') || alg.startsWith('PS')) {
|
|
6
|
+
const { modulusLength } = key.algorithm;
|
|
7
|
+
if (typeof modulusLength !== 'number' || modulusLength < 2048) {
|
|
8
|
+
throw new TypeError(`${alg} requires key modulusLength to be 2048 bits or larger`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function subtleAlgorithm(alg, algorithm) {
|
|
13
|
+
const hash = `SHA-${alg.slice(-3)}`;
|
|
14
|
+
switch (alg) {
|
|
15
|
+
case 'HS256':
|
|
16
|
+
case 'HS384':
|
|
17
|
+
case 'HS512':
|
|
18
|
+
return { hash, name: 'HMAC' };
|
|
19
|
+
case 'PS256':
|
|
20
|
+
case 'PS384':
|
|
21
|
+
case 'PS512':
|
|
22
|
+
return { hash, name: 'RSA-PSS', saltLength: parseInt(alg.slice(-3), 10) >> 3 };
|
|
23
|
+
case 'RS256':
|
|
24
|
+
case 'RS384':
|
|
25
|
+
case 'RS512':
|
|
26
|
+
return { hash, name: 'RSASSA-PKCS1-v1_5' };
|
|
27
|
+
case 'ES256':
|
|
28
|
+
case 'ES384':
|
|
29
|
+
case 'ES512':
|
|
30
|
+
return { hash, name: 'ECDSA', namedCurve: algorithm.namedCurve };
|
|
31
|
+
case 'Ed25519':
|
|
32
|
+
case 'EdDSA':
|
|
33
|
+
return { name: 'Ed25519' };
|
|
34
|
+
case 'ML-DSA-44':
|
|
35
|
+
case 'ML-DSA-65':
|
|
36
|
+
case 'ML-DSA-87':
|
|
37
|
+
return { name: alg };
|
|
38
|
+
default:
|
|
39
|
+
throw new JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function getSigKey(alg, key, usage) {
|
|
43
|
+
if (key instanceof Uint8Array) {
|
|
44
|
+
if (!alg.startsWith('HS')) {
|
|
45
|
+
throw new TypeError(invalidKeyInput(key, 'CryptoKey', 'KeyObject', 'JSON Web Key'));
|
|
46
|
+
}
|
|
47
|
+
return crypto.subtle.importKey('raw', key, { hash: `SHA-${alg.slice(-3)}`, name: 'HMAC' }, false, [usage]);
|
|
48
|
+
}
|
|
49
|
+
checkSigCryptoKey(key, alg, usage);
|
|
50
|
+
return key;
|
|
51
|
+
}
|
|
52
|
+
export async function sign(alg, key, data) {
|
|
53
|
+
const cryptoKey = await getSigKey(alg, key, 'sign');
|
|
54
|
+
checkKeyLength(alg, cryptoKey);
|
|
55
|
+
const signature = await crypto.subtle.sign(subtleAlgorithm(alg, cryptoKey.algorithm), cryptoKey, data);
|
|
56
|
+
return new Uint8Array(signature);
|
|
57
|
+
}
|
|
58
|
+
export async function verify(alg, key, signature, data) {
|
|
59
|
+
const cryptoKey = await getSigKey(alg, key, 'verify');
|
|
60
|
+
checkKeyLength(alg, cryptoKey);
|
|
61
|
+
const algorithm = subtleAlgorithm(alg, cryptoKey.algorithm);
|
|
62
|
+
try {
|
|
63
|
+
return await crypto.subtle.verify(algorithm, cryptoKey, signature, data);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const isObjectLike = (value) => typeof value === 'object' && value !== null;
|
|
2
|
+
export function isObject(input) {
|
|
3
|
+
if (!isObjectLike(input) || Object.prototype.toString.call(input) !== '[object Object]') {
|
|
4
|
+
return false;
|
|
5
|
+
}
|
|
6
|
+
if (Object.getPrototypeOf(input) === null) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
let proto = input;
|
|
10
|
+
while (Object.getPrototypeOf(proto) !== null) {
|
|
11
|
+
proto = Object.getPrototypeOf(proto);
|
|
12
|
+
}
|
|
13
|
+
return Object.getPrototypeOf(input) === proto;
|
|
14
|
+
}
|
|
15
|
+
export function isDisjoint(...headers) {
|
|
16
|
+
const sources = headers.filter(Boolean);
|
|
17
|
+
if (sources.length === 0 || sources.length === 1) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
let acc;
|
|
21
|
+
for (const header of sources) {
|
|
22
|
+
const parameters = Object.keys(header);
|
|
23
|
+
if (!acc || acc.size === 0) {
|
|
24
|
+
acc = new Set(parameters);
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
for (const parameter of parameters) {
|
|
28
|
+
if (acc.has(parameter)) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
acc.add(parameter);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
export const isJWK = (key) => isObject(key) && typeof key.kty === 'string';
|
|
37
|
+
export const isPrivateJWK = (key) => key.kty !== 'oct' &&
|
|
38
|
+
((key.kty === 'AKP' && typeof key.priv === 'string') || typeof key.d === 'string');
|
|
39
|
+
export const isPublicJWK = (key) => key.kty !== 'oct' && key.d === undefined && key.priv === undefined;
|
|
40
|
+
export const isSecretJWK = (key) => key.kty === 'oct' && typeof key.k === 'string';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { decode as b64u } from './base64url.js';
|
|
2
2
|
import { decoder } from '../lib/buffer_utils.js';
|
|
3
|
-
import { isObject } from '../lib/
|
|
3
|
+
import { isObject } from '../lib/type_checks.js';
|
|
4
4
|
import { JWTInvalid } from './errors.js';
|
|
5
5
|
export function decodeJwt(jwt) {
|
|
6
6
|
if (typeof jwt !== 'string')
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { decode as b64u } from './base64url.js';
|
|
2
2
|
import { decoder } from '../lib/buffer_utils.js';
|
|
3
|
-
import { isObject } from '../lib/
|
|
3
|
+
import { isObject } from '../lib/type_checks.js';
|
|
4
4
|
export function decodeProtectedHeader(token) {
|
|
5
5
|
let protectedB64u;
|
|
6
6
|
if (typeof token === 'string') {
|
package/package.json
CHANGED
package/dist/webapi/lib/cek.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { JOSENotSupported } from '../util/errors.js';
|
|
2
|
-
export function cekLength(alg) {
|
|
3
|
-
switch (alg) {
|
|
4
|
-
case 'A128GCM':
|
|
5
|
-
return 128;
|
|
6
|
-
case 'A192GCM':
|
|
7
|
-
return 192;
|
|
8
|
-
case 'A256GCM':
|
|
9
|
-
case 'A128CBC-HS256':
|
|
10
|
-
return 256;
|
|
11
|
-
case 'A192CBC-HS384':
|
|
12
|
-
return 384;
|
|
13
|
-
case 'A256CBC-HS512':
|
|
14
|
-
return 512;
|
|
15
|
-
default:
|
|
16
|
-
throw new JOSENotSupported(`Unsupported JWE Algorithm: ${alg}`);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
export const generateCek = (alg) => crypto.getRandomValues(new Uint8Array(cekLength(alg) >> 3));
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { JWEInvalid } from '../util/errors.js';
|
|
2
|
-
export function checkCekLength(cek, expected) {
|
|
3
|
-
const actual = cek.byteLength << 3;
|
|
4
|
-
if (actual !== expected) {
|
|
5
|
-
throw new JWEInvalid(`Invalid Content Encryption Key length. Expected ${expected} bits, got ${actual} bits`);
|
|
6
|
-
}
|
|
7
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export function checkKeyLength(alg, key) {
|
|
2
|
-
if (alg.startsWith('RS') || alg.startsWith('PS')) {
|
|
3
|
-
const { modulusLength } = key.algorithm;
|
|
4
|
-
if (typeof modulusLength !== 'number' || modulusLength < 2048) {
|
|
5
|
-
throw new TypeError(`${alg} requires key modulusLength to be 2048 bits or larger`);
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
}
|