@@ -170,6 +170,23 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
170170 assert . strictEqual ( derivedPublicKey . asymmetricKeyType , 'rsa' ) ;
171171 assert . strictEqual ( derivedPublicKey . symmetricKeySize , undefined ) ;
172172
173+ // The private key should not be extractable from the derived public key.
174+ assert . throws ( ( ) => derivedPublicKey . export ( { format : 'pem' , type : 'pkcs8' } ) ,
175+ { code : 'ERR_INVALID_ARG_VALUE' } ) ;
176+ assert . throws ( ( ) => derivedPublicKey . export ( { format : 'der' , type : 'pkcs8' } ) ,
177+ { code : 'ERR_INVALID_ARG_VALUE' } ) ;
178+ // JWK export should only contain public components, no 'd'.
179+ {
180+ const jwkExport = derivedPublicKey . export ( { format : 'jwk' } ) ;
181+ assert . strictEqual ( jwkExport . kty , 'RSA' ) ;
182+ assert . strictEqual ( jwkExport . d , undefined ) ;
183+ assert . strictEqual ( jwkExport . dp , undefined ) ;
184+ assert . strictEqual ( jwkExport . dq , undefined ) ;
185+ assert . strictEqual ( jwkExport . qi , undefined ) ;
186+ assert . strictEqual ( jwkExport . p , undefined ) ;
187+ assert . strictEqual ( jwkExport . q , undefined ) ;
188+ }
189+
173190 const publicKeyFromJwk = createPublicKey ( { key : publicJwk , format : 'jwk' } ) ;
174191 assert . strictEqual ( publicKeyFromJwk . type , 'public' ) ;
175192 assert . strictEqual ( publicKeyFromJwk . toString ( ) , '[object KeyObject]' ) ;
@@ -533,6 +550,16 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
533550 delete jwk . d ;
534551 assert . deepStrictEqual (
535552 key . export ( { format : 'jwk' } ) , jwk ) ;
553+
554+ // Private key material must not be extractable from a derived public key.
555+ assert . throws ( ( ) => key . export ( { format : 'pem' , type : 'pkcs8' } ) ,
556+ { code : 'ERR_INVALID_ARG_VALUE' } ) ;
557+ assert . throws ( ( ) => key . export ( { format : 'pem' , type : 'sec1' } ) ,
558+ { code : 'ERR_INVALID_ARG_VALUE' } ) ;
559+ assert . throws ( ( ) => key . export ( { format : 'der' , type : 'pkcs8' } ) ,
560+ { code : 'ERR_INVALID_ARG_VALUE' } ) ;
561+ assert . throws ( ( ) => key . export ( { format : 'der' , type : 'sec1' } ) ,
562+ { code : 'ERR_INVALID_ARG_VALUE' } ) ;
536563 }
537564 }
538565
0 commit comments