@@ -18,13 +18,6 @@ const {
18
18
} = internalBinding ( 'crypto' ) ;
19
19
20
20
const {
21
- codes : {
22
- ERR_MISSING_OPTION ,
23
- }
24
- } = require ( 'internal/errors' ) ;
25
-
26
- const {
27
- getArrayBufferOrView,
28
21
getUsagesUnion,
29
22
hasAnyNotIn,
30
23
jobPromise,
@@ -86,7 +79,6 @@ function verifyAcceptableEcKeyUse(name, type, usages) {
86
79
87
80
function createECPublicKeyRaw ( namedCurve , keyData ) {
88
81
const handle = new KeyObjectHandle ( ) ;
89
- keyData = getArrayBufferOrView ( keyData , 'keyData' ) ;
90
82
91
83
if ( ! handle . initECRaw ( kNamedCurveAliases [ namedCurve ] , keyData ) ) {
92
84
throw lazyDOMException ( 'Invalid keyData' , 'DataError' ) ;
@@ -215,12 +207,14 @@ async function ecImportKey(
215
207
break ;
216
208
}
217
209
case 'jwk' : {
218
- if ( keyData == null || typeof keyData !== 'object' )
219
- throw lazyDOMException ( 'Invalid JWK keyData' , 'DataError' ) ;
210
+ if ( ! keyData . kty )
211
+ throw lazyDOMException ( 'Invalid keyData' , 'DataError' ) ;
220
212
if ( keyData . kty !== 'EC' )
221
- throw lazyDOMException ( 'Invalid key type ' , 'DataError' ) ;
213
+ throw lazyDOMException ( 'Invalid JWK "kty" Parameter ' , 'DataError' ) ;
222
214
if ( keyData . crv !== namedCurve )
223
- throw lazyDOMException ( 'Named curve mismatch' , 'DataError' ) ;
215
+ throw lazyDOMException (
216
+ 'JWK "crv" does not match the requested algorithm' ,
217
+ 'DataError' ) ;
224
218
225
219
if ( keyData . d !== undefined ) {
226
220
verifyAcceptableEcKeyUse ( name , 'private' , usagesSet ) ;
@@ -229,37 +223,38 @@ async function ecImportKey(
229
223
}
230
224
231
225
if ( usagesSet . size > 0 && keyData . use !== undefined ) {
232
- if ( algorithm . name === 'ECDSA' && keyData . use !== 'sig' )
233
- throw lazyDOMException ( 'Invalid use type' , 'DataError' ) ;
234
- if ( algorithm . name === 'ECDH' && keyData . use !== 'enc' )
235
- throw lazyDOMException ( 'Invalid use type' , 'DataError' ) ;
226
+ const checkUse = name === 'ECDH' ? 'enc' : 'sig' ;
227
+ if ( keyData . use !== checkUse )
228
+ throw lazyDOMException ( 'Invalid JWK "use" Parameter' , 'DataError' ) ;
236
229
}
237
230
238
231
validateKeyOps ( keyData . key_ops , usagesSet ) ;
239
232
240
233
if ( keyData . ext !== undefined &&
241
234
keyData . ext === false &&
242
235
extractable === true ) {
243
- throw lazyDOMException ( 'JWK is not extractable' , 'DataError' ) ;
236
+ throw lazyDOMException (
237
+ 'JWK "ext" Parameter and extractable mismatch' ,
238
+ 'DataError' ) ;
244
239
}
245
240
246
241
if ( algorithm . name === 'ECDSA' && keyData . alg !== undefined ) {
247
- if ( typeof keyData . alg !== 'string' )
248
- throw lazyDOMException ( 'Invalid alg' , 'DataError' ) ;
249
242
let algNamedCurve ;
250
243
switch ( keyData . alg ) {
251
244
case 'ES256' : algNamedCurve = 'P-256' ; break ;
252
245
case 'ES384' : algNamedCurve = 'P-384' ; break ;
253
246
case 'ES512' : algNamedCurve = 'P-521' ; break ;
254
247
}
255
248
if ( algNamedCurve !== namedCurve )
256
- throw lazyDOMException ( 'Named curve mismatch' , 'DataError' ) ;
249
+ throw lazyDOMException (
250
+ 'JWK "alg" does not match the requested algorithm' ,
251
+ 'DataError' ) ;
257
252
}
258
253
259
254
const handle = new KeyObjectHandle ( ) ;
260
255
const type = handle . initJwk ( keyData , namedCurve ) ;
261
256
if ( type === undefined )
262
- throw lazyDOMException ( 'Invalid JWK keyData ' , 'DataError' ) ;
257
+ throw lazyDOMException ( 'Invalid JWK' , 'DataError' ) ;
263
258
keyObject = type === kKeyTypePrivate ?
264
259
new PrivateKeyObject ( handle ) :
265
260
new PublicKeyObject ( handle ) ;
@@ -301,8 +296,6 @@ function ecdsaSignVerify(key, data, { name, hash }, signature) {
301
296
if ( key . type !== type )
302
297
throw lazyDOMException ( `Key must be a ${ type } key` , 'InvalidAccessError' ) ;
303
298
304
- if ( hash === undefined )
305
- throw new ERR_MISSING_OPTION ( 'algorithm.hash' ) ;
306
299
const hashname = normalizeHashName ( hash . name ) ;
307
300
308
301
return jobPromise ( ( ) => new SignJob (
0 commit comments