@@ -7,7 +7,8 @@ if (!common.hasCrypto)
77 common . skip ( 'missing crypto' ) ;
88
99const assert = require ( 'assert' ) ;
10- const { subtle } = require ( 'crypto' ) . webcrypto ;
10+ const crypto = require ( 'crypto' ) ;
11+ const { subtle } = crypto . webcrypto ;
1112
1213const sizes = [ 1024 , 2048 , 4096 ] ;
1314
@@ -521,3 +522,37 @@ const testVectors = [
521522 assert . strictEqual ( jwk . alg , 'PS256' ) ;
522523 } ) ( ) . then ( common . mustCall ( ) ) ;
523524}
525+
526+ {
527+ const ecPublic = crypto . createPublicKey (
528+ fixtures . readKey ( 'ec_p256_public.pem' ) ) ;
529+ const ecPrivate = crypto . createPrivateKey (
530+ fixtures . readKey ( 'ec_p256_private.pem' ) ) ;
531+
532+ for ( const [ name , [ publicUsage , privateUsage ] ] of Object . entries ( {
533+ 'RSA-PSS' : [ 'verify' , 'sign' ] ,
534+ 'RSASSA-PKCS1-v1_5' : [ 'verify' , 'sign' ] ,
535+ 'RSA-OAEP' : [ 'encrypt' , 'decrypt' ] ,
536+ } ) ) {
537+ assert . rejects ( subtle . importKey (
538+ 'node.keyObject' ,
539+ ecPublic ,
540+ { name, hash : 'SHA-256' } ,
541+ true , [ publicUsage ] ) , { message : / I n v a l i d k e y t y p e / } ) ;
542+ assert . rejects ( subtle . importKey (
543+ 'node.keyObject' ,
544+ ecPrivate ,
545+ { name, hash : 'SHA-256' } ,
546+ true , [ privateUsage ] ) , { message : / I n v a l i d k e y t y p e / } ) ;
547+ assert . rejects ( subtle . importKey (
548+ 'spki' ,
549+ ecPublic . export ( { format : 'der' , type : 'spki' } ) ,
550+ { name, hash : 'SHA-256' } ,
551+ true , [ publicUsage ] ) , { message : / I n v a l i d k e y t y p e / } ) ;
552+ assert . rejects ( subtle . importKey (
553+ 'pkcs8' ,
554+ ecPrivate . export ( { format : 'der' , type : 'pkcs8' } ) ,
555+ { name, hash : 'SHA-256' } ,
556+ true , [ privateUsage ] ) , { message : / I n v a l i d k e y t y p e / } ) ;
557+ }
558+ }
0 commit comments