@@ -384,6 +384,19 @@ async function testImportJwk(
384
384
385
385
const jwk = keyData [ size ] . jwk ;
386
386
387
+ let alg ;
388
+ switch ( name ) {
389
+ case 'RSA-PSS' :
390
+ alg = `PS${ hash === 'SHA-1' ? 1 : hash . substring ( 4 ) } ` ;
391
+ break ;
392
+ case 'RSA-OAEP' :
393
+ alg = `RSA-OAEP${ hash === 'SHA-1' ? '' : hash . substring ( 3 ) } ` ;
394
+ break ;
395
+ case 'RSASSA-PKCS1-v1_5' :
396
+ alg = `RS${ hash === 'SHA-1' ? 1 : hash . substring ( 4 ) } ` ;
397
+ break ;
398
+ }
399
+
387
400
const [
388
401
publicKey ,
389
402
privateKey ,
@@ -394,14 +407,14 @@ async function testImportJwk(
394
407
kty : jwk . kty ,
395
408
n : jwk . n ,
396
409
e : jwk . e ,
397
- alg : `PS ${ hash . substring ( 4 ) } `
410
+ alg,
398
411
} ,
399
412
{ name, hash } ,
400
413
extractable ,
401
414
publicUsages ) ,
402
415
subtle . importKey (
403
416
'jwk' ,
404
- { ...jwk , alg : `PS ${ hash . substring ( 4 ) } ` } ,
417
+ { ...jwk , alg } ,
405
418
{ name, hash } ,
406
419
extractable ,
407
420
privateUsages ) ,
@@ -435,6 +448,8 @@ async function testImportJwk(
435
448
436
449
assert . strictEqual ( pubJwk . kty , 'RSA' ) ;
437
450
assert . strictEqual ( pvtJwk . kty , 'RSA' ) ;
451
+ assert . strictEqual ( pubJwk . alg , alg ) ;
452
+ assert . strictEqual ( pvtJwk . alg , alg ) ;
438
453
assert . strictEqual ( pubJwk . n , jwk . n ) ;
439
454
assert . strictEqual ( pvtJwk . n , jwk . n ) ;
440
455
assert . strictEqual ( pubJwk . e , jwk . e ) ;
@@ -483,30 +498,71 @@ async function testImportJwk(
483
498
}
484
499
485
500
{
486
- let invalidAlg = name === 'RSA-OAEP' ? name : name === 'RSA-PSS' ? 'PS' : 'RS' ;
501
+ await assert . rejects (
502
+ subtle . importKey (
503
+ 'jwk' ,
504
+ { kty : jwk . kty , n : jwk . n , e : jwk . e , alg : alg . toLowerCase ( ) } ,
505
+ { name, hash } ,
506
+ extractable ,
507
+ publicUsages ) ,
508
+ { message : 'JWK "alg" does not match the requested algorithm' } ) ;
509
+ await assert . rejects (
510
+ subtle . importKey (
511
+ 'jwk' ,
512
+ { ...jwk , alg : alg . toLowerCase ( ) } ,
513
+ { name, hash } ,
514
+ extractable ,
515
+ privateUsages ) ,
516
+ { message : 'JWK "alg" does not match the requested algorithm' } ) ;
517
+ }
518
+
519
+ {
520
+ let invalidAlgHash = name === 'RSA-OAEP' ? name : name === 'RSA-PSS' ? 'PS' : 'RS' ;
487
521
switch ( name ) {
488
522
case 'RSA-OAEP' :
489
523
if ( hash === 'SHA-1' )
490
- invalidAlg += '-256' ;
524
+ invalidAlgHash += '-256' ;
491
525
break ;
492
526
default :
493
527
if ( hash === 'SHA-256' )
494
- invalidAlg += '384' ;
528
+ invalidAlgHash += '384' ;
495
529
else
496
- invalidAlg += '256' ;
530
+ invalidAlgHash += '256' ;
497
531
}
498
532
await assert . rejects (
499
533
subtle . importKey (
500
534
'jwk' ,
501
- { kty : jwk . kty , n : jwk . n , e : jwk . e , alg : invalidAlg } ,
535
+ { kty : jwk . kty , n : jwk . n , e : jwk . e , alg : invalidAlgHash } ,
502
536
{ name, hash } ,
503
537
extractable ,
504
538
publicUsages ) ,
505
539
{ message : 'JWK "alg" does not match the requested algorithm' } ) ;
506
540
await assert . rejects (
507
541
subtle . importKey (
508
542
'jwk' ,
509
- { ...jwk , alg : invalidAlg } ,
543
+ { ...jwk , alg : invalidAlgHash } ,
544
+ { name, hash } ,
545
+ extractable ,
546
+ privateUsages ) ,
547
+ { message : 'JWK "alg" does not match the requested algorithm' } ) ;
548
+ }
549
+
550
+ {
551
+ const invalidAlgType = name === 'RSA-PSS' ? `RS${ hash . substring ( 4 ) } ` : `PS${ hash . substring ( 4 ) } ` ;
552
+ await assert . rejects (
553
+ subtle . importKey (
554
+ 'jwk' ,
555
+ { kty : jwk . kty , n : jwk . n , e : jwk . e , alg : invalidAlgType } ,
556
+ { name, hash } ,
557
+ extractable ,
558
+ publicUsages ) ,
559
+ { message : 'JWK "alg" does not match the requested algorithm' } ) . catch ( ( e ) => {
560
+ throw e ;
561
+ } ) ;
562
+ await assert . rejects (
563
+ subtle . importKey (
564
+ 'jwk' ,
565
+ { ...jwk , alg : invalidAlgType } ,
510
566
{ name, hash } ,
511
567
extractable ,
512
568
privateUsages ) ,
0 commit comments