@@ -327,36 +327,25 @@ async function exportKeySpki(key) {
327
327
case 'RSA-PSS' :
328
328
// Fall through
329
329
case 'RSA-OAEP' :
330
- if ( key . type === 'public' ) {
331
- return require ( 'internal/crypto/rsa' )
332
- . rsaExportKey ( key , kWebCryptoKeyFormatSPKI ) ;
333
- }
334
- break ;
330
+ return require ( 'internal/crypto/rsa' )
331
+ . rsaExportKey ( key , kWebCryptoKeyFormatSPKI ) ;
335
332
case 'ECDSA' :
336
333
// Fall through
337
334
case 'ECDH' :
338
- if ( key . type === 'public' ) {
339
- return require ( 'internal/crypto/ec' )
340
- . ecExportKey ( key , kWebCryptoKeyFormatSPKI ) ;
341
- }
342
- break ;
335
+ return require ( 'internal/crypto/ec' )
336
+ . ecExportKey ( key , kWebCryptoKeyFormatSPKI ) ;
343
337
case 'Ed25519' :
344
338
// Fall through
345
339
case 'Ed448' :
346
340
// Fall through
347
341
case 'X25519' :
348
342
// Fall through
349
343
case 'X448' :
350
- if ( key . type === 'public' ) {
351
- return require ( 'internal/crypto/cfrg' )
352
- . cfrgExportKey ( key , kWebCryptoKeyFormatSPKI ) ;
353
- }
354
- break ;
344
+ return require ( 'internal/crypto/cfrg' )
345
+ . cfrgExportKey ( key , kWebCryptoKeyFormatSPKI ) ;
346
+ default :
347
+ return undefined ;
355
348
}
356
-
357
- throw lazyDOMException (
358
- `Unable to export a raw ${ key . algorithm . name } ${ key . type } key` ,
359
- 'InvalidAccessError' ) ;
360
349
}
361
350
362
351
async function exportKeyPkcs8 ( key ) {
@@ -366,60 +355,50 @@ async function exportKeyPkcs8(key) {
366
355
case 'RSA-PSS' :
367
356
// Fall through
368
357
case 'RSA-OAEP' :
369
- if ( key . type === 'private' ) {
370
- return require ( 'internal/crypto/rsa' )
371
- . rsaExportKey ( key , kWebCryptoKeyFormatPKCS8 ) ;
372
- }
373
- break ;
358
+ return require ( 'internal/crypto/rsa' )
359
+ . rsaExportKey ( key , kWebCryptoKeyFormatPKCS8 ) ;
374
360
case 'ECDSA' :
375
361
// Fall through
376
362
case 'ECDH' :
377
- if ( key . type === 'private' ) {
378
- return require ( 'internal/crypto/ec' )
379
- . ecExportKey ( key , kWebCryptoKeyFormatPKCS8 ) ;
380
- }
381
- break ;
363
+ return require ( 'internal/crypto/ec' )
364
+ . ecExportKey ( key , kWebCryptoKeyFormatPKCS8 ) ;
382
365
case 'Ed25519' :
383
366
// Fall through
384
367
case 'Ed448' :
385
368
// Fall through
386
369
case 'X25519' :
387
370
// Fall through
388
371
case 'X448' :
389
- if ( key . type === 'private' ) {
390
- return require ( 'internal/crypto/cfrg' )
391
- . cfrgExportKey ( key , kWebCryptoKeyFormatPKCS8 ) ;
392
- }
393
- break ;
372
+ return require ( 'internal/crypto/cfrg' )
373
+ . cfrgExportKey ( key , kWebCryptoKeyFormatPKCS8 ) ;
374
+ default :
375
+ return undefined ;
394
376
}
395
-
396
- throw lazyDOMException (
397
- `Unable to export a pkcs8 ${ key . algorithm . name } ${ key . type } key` ,
398
- 'InvalidAccessError' ) ;
399
377
}
400
378
401
- async function exportKeyRaw ( key ) {
379
+ async function exportKeyRawPublic ( key ) {
402
380
switch ( key . algorithm . name ) {
403
381
case 'ECDSA' :
404
382
// Fall through
405
383
case 'ECDH' :
406
- if ( key . type === 'public' ) {
407
- return require ( 'internal/crypto/ec' )
408
- . ecExportKey ( key , kWebCryptoKeyFormatRaw ) ;
409
- }
410
- break ;
384
+ return require ( 'internal/crypto/ec' )
385
+ . ecExportKey ( key , kWebCryptoKeyFormatRaw ) ;
411
386
case 'Ed25519' :
412
387
// Fall through
413
388
case 'Ed448' :
414
389
// Fall through
415
390
case 'X25519' :
416
391
// Fall through
417
392
case 'X448' :
418
- if ( key . type === 'public' ) {
419
- return require ( 'internal/crypto/cfrg' )
420
- . cfrgExportKey ( key , kWebCryptoKeyFormatRaw ) ;
421
- }
422
- break ;
393
+ return require ( 'internal/crypto/cfrg' )
394
+ . cfrgExportKey ( key , kWebCryptoKeyFormatRaw ) ;
395
+ default :
396
+ return undefined ;
397
+ }
398
+ }
399
+
400
+ async function exportKeyRawSecret ( key ) {
401
+ switch ( key . algorithm . name ) {
423
402
case 'AES-CTR' :
424
403
// Fall through
425
404
case 'AES-CBC' :
@@ -429,71 +408,66 @@ async function exportKeyRaw(key) {
429
408
case 'AES-KW' :
430
409
// Fall through
431
410
case 'HMAC' :
432
- return key [ kKeyObject ] . export ( ) . buffer ;
411
+ return key [ kKeyObject ] [ kHandle ] . export ( ) . buffer ;
412
+ default :
413
+ return undefined ;
433
414
}
434
-
435
- throw lazyDOMException (
436
- `Unable to export a raw ${ key . algorithm . name } ${ key . type } key` ,
437
- 'InvalidAccessError' ) ;
438
415
}
439
416
440
417
async function exportKeyJWK ( key ) {
441
- const jwk = key [ kKeyObject ] [ kHandle ] . exportJwk ( {
418
+ const parameters = {
442
419
key_ops : key . usages ,
443
420
ext : key . extractable ,
444
- } , true ) ;
421
+ } ;
445
422
switch ( key . algorithm . name ) {
446
423
case 'RSASSA-PKCS1-v1_5' :
447
- jwk . alg = normalizeHashName (
424
+ parameters . alg = normalizeHashName (
448
425
key . algorithm . hash . name ,
449
426
normalizeHashName . kContextJwkRsa ) ;
450
- return jwk ;
427
+ break ;
451
428
case 'RSA-PSS' :
452
- jwk . alg = normalizeHashName (
429
+ parameters . alg = normalizeHashName (
453
430
key . algorithm . hash . name ,
454
431
normalizeHashName . kContextJwkRsaPss ) ;
455
- return jwk ;
432
+ break ;
456
433
case 'RSA-OAEP' :
457
- jwk . alg = normalizeHashName (
434
+ parameters . alg = normalizeHashName (
458
435
key . algorithm . hash . name ,
459
436
normalizeHashName . kContextJwkRsaOaep ) ;
460
- return jwk ;
437
+ break ;
461
438
case 'ECDSA' :
462
439
// Fall through
463
440
case 'ECDH' :
464
- jwk . crv ||= key . algorithm . namedCurve ;
465
- return jwk ;
441
+ // Fall through
466
442
case 'X25519' :
467
443
// Fall through
468
444
case 'X448' :
469
- jwk . crv ||= key . algorithm . name ;
470
- return jwk ;
445
+ break ;
471
446
case 'Ed25519' :
472
447
// Fall through
473
448
case 'Ed448' :
474
- jwk . crv ||= key . algorithm . name ;
475
- jwk . alg = key . algorithm . name ;
476
- return jwk ;
449
+ parameters . alg = key . algorithm . name ;
450
+ break ;
477
451
case 'AES-CTR' :
478
452
// Fall through
479
453
case 'AES-CBC' :
480
454
// Fall through
481
455
case 'AES-GCM' :
482
456
// Fall through
483
457
case 'AES-KW' :
484
- jwk . alg = require ( 'internal/crypto/aes' )
458
+ parameters . alg = require ( 'internal/crypto/aes' )
485
459
. getAlgorithmName ( key . algorithm . name , key . algorithm . length ) ;
486
- return jwk ;
460
+ break ;
487
461
case 'HMAC' :
488
- jwk . alg = normalizeHashName (
462
+ parameters . alg = normalizeHashName (
489
463
key . algorithm . hash . name ,
490
464
normalizeHashName . kContextJwkHmac ) ;
491
- return jwk ;
465
+ break ;
492
466
default :
493
- // Fall through
467
+ return undefined ;
494
468
}
495
469
496
- throw lazyDOMException ( 'Not yet supported' , 'NotSupportedError' ) ;
470
+ return key [ kKeyObject ] [ kHandle ] . exportJwk ( parameters , true ) ;
497
471
}
498
472
499
473
async function exportKey ( format , key ) {
@@ -511,17 +485,55 @@ async function exportKey(format, key) {
511
485
context : '2nd argument' ,
512
486
} ) ;
513
487
488
+ try {
489
+ normalizeAlgorithm ( key . algorithm , 'exportKey' ) ;
490
+ } catch {
491
+ throw lazyDOMException (
492
+ `${ key . algorithm . name } key export is not supported` , 'NotSupportedError' ) ;
493
+ }
494
+
514
495
if ( ! key . extractable )
515
496
throw lazyDOMException ( 'key is not extractable' , 'InvalidAccessException' ) ;
516
497
498
+ let result ;
517
499
switch ( format ) {
518
- case 'spki' : return exportKeySpki ( key ) ;
519
- case 'pkcs8' : return exportKeyPkcs8 ( key ) ;
520
- case 'jwk' : return exportKeyJWK ( key ) ;
521
- case 'raw' : return exportKeyRaw ( key ) ;
500
+ case 'spki' : {
501
+ if ( key . type === 'public' ) {
502
+ result = await exportKeySpki ( key ) ;
503
+ }
504
+ break ;
505
+ }
506
+ case 'pkcs8' : {
507
+ if ( key . type === 'private' ) {
508
+ result = await exportKeyPkcs8 ( key ) ;
509
+ }
510
+ break ;
511
+ }
512
+ case 'jwk' : {
513
+ result = await exportKeyJWK ( key ) ;
514
+ break ;
515
+ }
516
+ case 'raw' : {
517
+ if ( key . type === 'secret' ) {
518
+ result = await exportKeyRawSecret ( key ) ;
519
+ break ;
520
+ }
521
+
522
+ if ( key . type === 'public' ) {
523
+ result = await exportKeyRawPublic ( key ) ;
524
+ break ;
525
+ }
526
+ break ;
527
+ }
522
528
}
523
- throw lazyDOMException (
524
- 'Export format is unsupported' , 'NotSupportedError' ) ;
529
+
530
+ if ( ! result ) {
531
+ throw lazyDOMException (
532
+ `Unable to export ${ key . algorithm . name } ${ key . type } key using ${ format } format` ,
533
+ 'NotSupportedError' ) ;
534
+ }
535
+
536
+ return result ;
525
537
}
526
538
527
539
async function importKey (
@@ -608,8 +620,12 @@ async function importKey(
608
620
extractable ,
609
621
keyUsages ) ;
610
622
break ;
611
- default :
612
- throw lazyDOMException ( 'Unrecognized algorithm name' , 'NotSupportedError' ) ;
623
+ }
624
+
625
+ if ( ! result ) {
626
+ throw lazyDOMException (
627
+ `Unable to import ${ algorithm . name } using ${ format } format` ,
628
+ 'NotSupportedError' ) ;
613
629
}
614
630
615
631
if ( ( result . type === 'secret' || result . type === 'private' ) && result . usages . length === 0 ) {
0 commit comments