@@ -919,6 +919,14 @@ async function wrapKey(format, key, wrappingKey, algorithm) {
919919 } catch {
920920 algorithm = normalizeAlgorithm ( algorithm , 'encrypt' ) ;
921921 }
922+
923+ if ( algorithm . name !== wrappingKey [ kAlgorithm ] . name ||
924+ ! ArrayPrototypeIncludes ( wrappingKey [ kKeyUsages ] , 'wrapKey' ) ) {
925+ throw lazyDOMException (
926+ 'The requested operation is not valid for the provided key' ,
927+ 'InvalidAccessError' ) ;
928+ }
929+
922930 let keyData = await FunctionPrototypeCall ( exportKey , this , format , key ) ;
923931
924932 if ( format === 'jwk' ) {
@@ -997,6 +1005,13 @@ async function unwrapKey(
9971005
9981006 unwrappedKeyAlgo = normalizeAlgorithm ( unwrappedKeyAlgo , 'importKey' ) ;
9991007
1008+ if ( unwrapAlgo . name !== unwrappingKey [ kAlgorithm ] . name ||
1009+ ! ArrayPrototypeIncludes ( unwrappingKey [ kKeyUsages ] , 'unwrapKey' ) ) {
1010+ throw lazyDOMException (
1011+ 'The requested operation is not valid for the provided key' ,
1012+ 'InvalidAccessError' ) ;
1013+ }
1014+
10001015 let keyData = await cipherOrWrap (
10011016 kWebCryptoCipherDecrypt ,
10021017 unwrapAlgo ,
@@ -1030,12 +1045,12 @@ async function signVerify(algorithm, key, data, signature) {
10301045 }
10311046 algorithm = normalizeAlgorithm ( algorithm , usage ) ;
10321047
1033- if ( ! ArrayPrototypeIncludes ( key [ kKeyUsages ] , usage ) ||
1034- algorithm . name !== key [ kAlgorithm ] . name ) {
1048+ if ( algorithm . name !== key [ kAlgorithm ] . name )
1049+ throw lazyDOMException ( 'Key algorithm mismatch' , 'InvalidAccessError' ) ;
1050+
1051+ if ( ! ArrayPrototypeIncludes ( key [ kKeyUsages ] , usage ) )
10351052 throw lazyDOMException (
1036- `Unable to use this key to ${ usage } ` ,
1037- 'InvalidAccessError' ) ;
1038- }
1053+ `Unable to use this key to ${ usage } ` , 'InvalidAccessError' ) ;
10391054
10401055 switch ( algorithm . name ) {
10411056 case 'RSA-PSS' :
@@ -1120,18 +1135,6 @@ async function verify(algorithm, key, signature, data) {
11201135}
11211136
11221137async function cipherOrWrap ( mode , algorithm , key , data , op ) {
1123- // We use a Node.js style error here instead of a DOMException because
1124- // the WebCrypto spec is not specific what kind of error is to be thrown
1125- // in this case. Both Firefox and Chrome throw simple TypeErrors here.
1126- // The key algorithm and cipher algorithm must match, and the
1127- // key must have the proper usage.
1128- if ( key [ kAlgorithm ] . name !== algorithm . name ||
1129- ! ArrayPrototypeIncludes ( key [ kKeyUsages ] , op ) ) {
1130- throw lazyDOMException (
1131- 'The requested operation is not valid for the provided key' ,
1132- 'InvalidAccessError' ) ;
1133- }
1134-
11351138 // While WebCrypto allows for larger input buffer sizes, we limit
11361139 // those to sizes that can fit within uint32_t because of limitations
11371140 // in the OpenSSL API.
@@ -1182,6 +1185,14 @@ async function encrypt(algorithm, key, data) {
11821185 } ) ;
11831186
11841187 algorithm = normalizeAlgorithm ( algorithm , 'encrypt' ) ;
1188+
1189+ if ( algorithm . name !== key [ kAlgorithm ] . name ||
1190+ ! ArrayPrototypeIncludes ( key [ kKeyUsages ] , 'encrypt' ) ) {
1191+ throw lazyDOMException (
1192+ 'The requested operation is not valid for the provided key' ,
1193+ 'InvalidAccessError' ) ;
1194+ }
1195+
11851196 return await cipherOrWrap (
11861197 kWebCryptoCipherEncrypt ,
11871198 algorithm ,
@@ -1211,6 +1222,14 @@ async function decrypt(algorithm, key, data) {
12111222 } ) ;
12121223
12131224 algorithm = normalizeAlgorithm ( algorithm , 'decrypt' ) ;
1225+
1226+ if ( algorithm . name !== key [ kAlgorithm ] . name ||
1227+ ! ArrayPrototypeIncludes ( key [ kKeyUsages ] , 'decrypt' ) ) {
1228+ throw lazyDOMException (
1229+ 'The requested operation is not valid for the provided key' ,
1230+ 'InvalidAccessError' ) ;
1231+ }
1232+
12141233 return await cipherOrWrap (
12151234 kWebCryptoCipherDecrypt ,
12161235 algorithm ,
0 commit comments