@@ -59,6 +59,47 @@ const { hasOpenSSL } = require('../common/crypto');
5959 }
6060}
6161
62+ // Raw public keys cannot be imported as private keys.
63+ {
64+ const rawPublicKeys = [
65+ [ 'ec' , 'ec_p256_public.pem' , { namedCurve : 'P-256' } ] ,
66+ [ 'ed25519' , 'ed25519_public.pem' ] ,
67+ [ 'x25519' , 'x25519_public.pem' ] ,
68+ ] ;
69+
70+ if ( ! process . features . openssl_is_boringssl ) {
71+ rawPublicKeys . push (
72+ [ 'ed448' , 'ed448_public.pem' ] ,
73+ [ 'x448' , 'x448_public.pem' ] ,
74+ ) ;
75+ } else {
76+ common . printSkipMessage ( 'Skipping unsupported ed448/x448 test cases' ) ;
77+ }
78+
79+ if ( hasOpenSSL ( 3 , 5 ) || process . features . openssl_is_boringssl ) {
80+ rawPublicKeys . push (
81+ [ 'ml-dsa-44' , 'ml_dsa_44_public.pem' ] ,
82+ [ 'ml-kem-768' , 'ml_kem_768_public.pem' ] ,
83+ ) ;
84+ }
85+
86+ if ( hasOpenSSL ( 3 , 5 ) ) {
87+ rawPublicKeys . push (
88+ [ 'slh-dsa-sha2-128f' , 'slh_dsa_sha2_128f_public.pem' ] ,
89+ ) ;
90+ }
91+
92+ for ( const [ asymmetricKeyType , fixture , options = { } ] of rawPublicKeys ) {
93+ const publicKey = crypto . createPublicKey ( fixtures . readKey ( fixture , 'ascii' ) ) ;
94+ assert . throws ( ( ) => crypto . createPrivateKey ( {
95+ key : publicKey . export ( { format : 'raw-public' } ) ,
96+ format : 'raw-public' ,
97+ asymmetricKeyType,
98+ ...options ,
99+ } ) , { code : 'ERR_INVALID_ARG_VALUE' } ) ;
100+ }
101+ }
102+
62103// Raw seed imports do not support strings.
63104if ( hasOpenSSL ( 3 , 5 ) ) {
64105 const privKeyObj = crypto . createPrivateKey (
@@ -113,7 +154,11 @@ if (hasOpenSSL(3, 5)) {
113154 assert . throws ( ( ) => privKeyObj . export ( { format : 'raw-private' } ) ,
114155 { code : 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' } ) ;
115156
116- for ( const format of [ 'raw-public' , 'raw-private' , 'raw-seed' ] ) {
157+ assert . throws ( ( ) => crypto . createPrivateKey ( {
158+ key : Buffer . alloc ( 32 ) , format : 'raw-public' , asymmetricKeyType : 'dh' ,
159+ } ) , { code : 'ERR_INVALID_ARG_VALUE' } ) ;
160+
161+ for ( const format of [ 'raw-private' , 'raw-seed' ] ) {
117162 assert . throws ( ( ) => crypto . createPrivateKey ( {
118163 key : Buffer . alloc ( 32 ) , format, asymmetricKeyType : 'dh' ,
119164 } ) , { code : 'ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS' } ) ;
0 commit comments