@@ -3,12 +3,14 @@ const common = require('../common');
33if ( ! common . hasCrypto )
44 common . skip ( 'missing crypto' ) ;
55
6- const { hasOpenSSL3 } = require ( '../common/crypto' ) ;
6+ const { hasOpenSSL , hasFIPS } = require ( '../common/crypto' ) ;
77const assert = require ( 'assert' ) ;
88const util = require ( 'util' ) ;
99const crypto = require ( 'crypto' ) ;
1010const fixtures = require ( '../common/fixtures' ) ;
1111
12+ const fips3 = hasFIPS ( 3 ) ;
13+
1214function test (
1315 publicFixture ,
1416 privateFixture ,
@@ -65,6 +67,15 @@ function test(
6567 }
6668}
6769
70+ function testSignFailure ( privateFixture , algorithm , options , code ) {
71+ const key = { key : fixtures . readKey ( privateFixture ) , ...options } ;
72+ const data = Buffer . from ( 'Hello world' ) ;
73+ assert . throws ( ( ) => crypto . sign ( algorithm , data , key ) , { code } ) ;
74+ crypto . sign ( algorithm , data , key , common . mustCall ( ( err ) => {
75+ assert . strictEqual ( err ?. code , code ) ;
76+ } ) ) ;
77+ }
78+
6879// RSA w/ default padding
6980test ( 'rsa_public.pem' , 'rsa_private.pem' , 'sha256' , true ) ;
7081test ( 'rsa_public.pem' , 'rsa_private.pem' , 'sha256' , true ,
@@ -94,14 +105,19 @@ if (!process.features.openssl_is_boringssl) {
94105 test ( 'ed448_public.pem' , 'ed448_private.pem' , undefined , true ) ;
95106
96107 // ECDSA w/ der signature encoding
97- test ( 'ec_secp256k1_public.pem' , 'ec_secp256k1_private.pem' , 'sha384' ,
98- false ) ;
99- test ( 'ec_secp256k1_public.pem' , 'ec_secp256k1_private.pem' , 'sha384' ,
100- false , { dsaEncoding : 'der' } ) ;
101-
102- // ECDSA w/ ieee-p1363 signature encoding
103- test ( 'ec_secp256k1_public.pem' , 'ec_secp256k1_private.pem' , 'sha384' , false ,
104- { dsaEncoding : 'ieee-p1363' } ) ;
108+ if ( fips3 ) {
109+ testSignFailure ( 'ec_secp256k1_private.pem' , 'sha384' , { } ,
110+ 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE' ) ;
111+ } else {
112+ test ( 'ec_secp256k1_public.pem' , 'ec_secp256k1_private.pem' , 'sha384' ,
113+ false ) ;
114+ test ( 'ec_secp256k1_public.pem' , 'ec_secp256k1_private.pem' , 'sha384' ,
115+ false , { dsaEncoding : 'der' } ) ;
116+
117+ // ECDSA w/ ieee-p1363 signature encoding
118+ test ( 'ec_secp256k1_public.pem' , 'ec_secp256k1_private.pem' , 'sha384' , false ,
119+ { dsaEncoding : 'ieee-p1363' } ) ;
120+ }
105121
106122 // DSA w/ der signature encoding
107123 test ( 'dsa_public.pem' , 'dsa_private.pem' , 'sha256' ,
@@ -157,7 +173,7 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
157173
158174 let expected = / n o d e f a u l t d i g e s t / ;
159175 let expectedCode = 'ERR_OSSL_EVP_NO_DEFAULT_DIGEST' ;
160- if ( hasOpenSSL3 || process . features . openssl_is_boringssl ) {
176+ if ( hasOpenSSL ( 3 ) || process . features . openssl_is_boringssl ) {
161177 expected = / o p e r a t i o n [ \s _ ] n o t [ \s _ ] s u p p o r t e d [ \s _ ] f o r [ \s _ ] t h i s [ \s _ ] k e y t y p e / i;
162178 expectedCode = 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE' ;
163179 }
@@ -170,12 +186,21 @@ MCowBQYDK2VuAyEA6pwGRbadNQAI/tYN8+/p/0/hbsdHfOEGr1ADiLVk/Gc=
170186}
171187
172188{
173- const { privateKey } = crypto . generateKeyPairSync ( 'rsa' , {
174- modulusLength : 512
175- } ) ;
176- crypto . sign ( 'sha512' , 'message' , privateKey , common . mustCall ( ( err ) => {
177- assert . ok ( err ) ;
178- assert . match ( err . message , / d i g e s t [ \s _ ] t o o [ \s _ ] b i g [ \s _ ] f o r [ \s _ ] r s a [ \s _ ] k e y / i) ;
179- assert . match ( err . code , / ^ E R R _ O S S L _ .* D I G E S T _ T O O _ B I G _ F O R _ R S A _ K E Y $ / ) ;
180- } ) ) ;
189+ if ( fips3 ) {
190+ crypto . generateKeyPair ( 'rsa' , { modulusLength : 512 } ,
191+ common . mustCall ( ( err ) => {
192+ assert . strictEqual (
193+ err ?. code , 'ERR_OSSL_RSA_INVALID_MODULUS' ) ;
194+ } ) ) ;
195+ } else {
196+ const { privateKey } = crypto . generateKeyPairSync ( 'rsa' , {
197+ modulusLength : 512
198+ } ) ;
199+ crypto . sign ( 'sha512' , 'message' , privateKey , common . mustCall ( ( err ) => {
200+ assert . ok ( err ) ;
201+ assert . match (
202+ err . message , / d i g e s t [ \s _ ] t o o [ \s _ ] b i g [ \s _ ] f o r [ \s _ ] r s a [ \s _ ] k e y / i) ;
203+ assert . match ( err . code , / ^ E R R _ O S S L _ .* D I G E S T _ T O O _ B I G _ F O R _ R S A _ K E Y $ / ) ;
204+ } ) ) ;
205+ }
181206}
0 commit comments