@@ -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
4449export 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
0 commit comments