Skip to content

Commit 35c9777

Browse files
fix(sdk): Enable support for more KAS key types (#624)
1 parent 08c8651 commit 35c9777

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

lib/src/access.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,44 @@ export async function fetchWrappedKey(
3939
);
4040
}
4141

42-
export type KasPublicKeyAlgorithm = 'ec:secp256r1' | 'rsa:2048';
42+
export type KasPublicKeyAlgorithm =
43+
| 'ec:secp256r1'
44+
| 'ec:secp384r1'
45+
| 'ec:secp521r1'
46+
| 'rsa:2048'
47+
| 'rsa:4096';
4348

4449
export const isPublicKeyAlgorithm = (a: string): a is KasPublicKeyAlgorithm => {
4550
return a === 'ec:secp256r1' || a === 'rsa:2048';
4651
};
4752

48-
export const keyAlgorithmToPublicKeyAlgorithm = (a: KeyAlgorithm): KasPublicKeyAlgorithm => {
53+
export const keyAlgorithmToPublicKeyAlgorithm = (k: CryptoKey): KasPublicKeyAlgorithm => {
54+
const a = k.algorithm;
4955
if (a.name === 'ECDSA' || a.name === 'ECDH') {
5056
const eca = a as EcKeyAlgorithm;
51-
if (eca.namedCurve === 'P-256') {
52-
return 'ec:secp256r1';
57+
switch (eca.namedCurve) {
58+
case 'P-256':
59+
return 'ec:secp256r1';
60+
case 'P-384':
61+
return 'ec:secp384r1';
62+
case 'P-521':
63+
return 'ec:secp521r1';
64+
default:
65+
throw new Error(`unsupported EC curve: ${eca.namedCurve}`);
5366
}
54-
throw new Error(`unsupported EC curve: ${eca.namedCurve}`);
5567
}
56-
if (a.name === 'RSA-OAEP') {
68+
if (a.name === 'RSA-OAEP' || a.name === 'RSASSA-PKCS1-v1_5') {
5769
const rsaa = a as RsaHashedKeyAlgorithm;
58-
if (rsaa.modulusLength === 2048) {
59-
// if (rsaa.hash.name !== 'RSASSA-PKCS1-v1_5') {
60-
// throw new Error(`unsupported RSA hash: ${rsaa.hash.name}`);
61-
// }
62-
if (rsaa.publicExponent.toString() !== '1,0,1') {
63-
throw new Error(`unsupported RSA public exponent: ${rsaa.publicExponent}`);
64-
}
65-
return 'rsa:2048';
70+
if (rsaa.publicExponent.toString() !== '1,0,1') {
71+
throw new Error(`unsupported RSA public exponent: ${rsaa.publicExponent}`);
72+
}
73+
switch (rsaa.modulusLength) {
74+
case 2048:
75+
return 'rsa:2048';
76+
case 4096:
77+
return 'rsa:4096';
78+
default:
79+
throw new Error(`unsupported RSA modulus length: ${rsaa.modulusLength}`);
6680
}
6781
}
6882
throw new Error(`unsupported key algorithm: ${a.name}`);
@@ -74,6 +88,14 @@ export const publicKeyAlgorithmToJwa = (a: KasPublicKeyAlgorithm): string => {
7488
return 'ES256';
7589
case 'rsa:2048':
7690
return 'RS256';
91+
case 'rsa:4096':
92+
return 'RS512';
93+
case 'ec:secp384r1':
94+
return 'ES384';
95+
case 'ec:secp521r1':
96+
return 'ES512';
97+
default:
98+
throw new Error(`unsupported public key algorithm: ${a}`);
7799
}
78100
};
79101

lib/src/nanotdf-crypto/keyAgreement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export async function keyAgreement(
7171
}
7272
): Promise<CryptoKey> {
7373
for (const k of [privateKey, publicKey]) {
74-
const mechanism = keyAlgorithmToPublicKeyAlgorithm(k.algorithm);
74+
const mechanism = keyAlgorithmToPublicKeyAlgorithm(k);
7575
if (mechanism !== 'ec:secp256r1') {
7676
throw new ConfigurationError(
7777
`${k.type} CryptoKey is expected to be of type ECDSA or ECDH, not [${k.algorithm?.name}]`

lib/tdf3/src/client/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const resolveKasInfo = async (
7979
kid?: string
8080
): Promise<KasPublicKeyInfo> => {
8181
const k: CryptoKey = await pemToCryptoPublicKey(pem);
82-
const algorithm = keyAlgorithmToPublicKeyAlgorithm(k.algorithm);
82+
const algorithm = keyAlgorithmToPublicKeyAlgorithm(k);
8383
return {
8484
key: Promise.resolve(k),
8585
publicKey: pem,

0 commit comments

Comments
 (0)