@@ -12,8 +12,8 @@ if (isBoringSSL)
1212const assert = require ( 'assert' ) ;
1313const spawnSync = require ( 'child_process' ) . spawnSync ;
1414const path = require ( 'path' ) ;
15- const { spawnSyncAndAssert } = require ( '../common/child_process' ) ;
1615const fixtures = require ( '../common/fixtures' ) ;
16+ const { spawnSyncAndAssert } = require ( '../common/child_process' ) ;
1717const { internalBinding } = require ( 'internal/test/binding' ) ;
1818const { testFipsCrypto } = internalBinding ( 'crypto' ) ;
1919
@@ -78,32 +78,66 @@ function testHelper(stream, args, expectedStatus, expectedOutput, cmd, env) {
7878 responseHandler ( child [ stream ] , expectedOutput ) ;
7979}
8080
81- // --enable-fips should raise an error if OpenSSL is not FIPS enabled.
82- testHelper (
83- testFipsCrypto ( ) ? 'stdout' : 'stderr' ,
84- [ '--enable-fips' ] ,
85- testFipsCrypto ( ) ? kNoFailure : kGenericUserError ,
86- testFipsCrypto ( ) ? FIPS_ENABLED : FIPS_ENABLE_ERROR_STRING ,
87- 'require("crypto").getFips()' ,
88- process . env ) ;
81+ if ( hasOpenSSL ( 3 ) ) {
82+ const fipsFlags = [ '--enable-fips' , '--force-fips' , '--force-fips=provider' ] ;
83+ if ( hasOpenSSL ( 3 , 4 ) )
84+ fipsFlags . push ( '--force-fips=strict' ) ;
85+
86+ // The flags change the default property query without requiring a provider
87+ // named "fips". This configuration supplies the RNG needed during startup,
88+ // but has no cryptographic implementations matching fips=yes.
89+ for ( const flag of fipsFlags ) {
90+ spawnSyncAndAssert ( process . execPath , [
91+ flag ,
92+ `--openssl-config=${ fixtures . path ( 'openssl3-conf' , 'fips_no_provider.cnf' ) } ` ,
93+ '-e' , `
94+ const assert = require('assert');
95+ const crypto = require('crypto');
96+ assert.strictEqual(crypto.getFips(), 1);
97+ assert.throws(() => crypto.createHash('sha256'), {
98+ code: 'ERR_OSSL_EVP_UNSUPPORTED',
99+ });
100+ if (${ flag === '--enable-fips' } ) {
101+ crypto.setFips(false);
102+ assert.strictEqual(crypto.getFips(), 0);
103+ crypto.createHash('sha256');
104+ } else {
105+ assert.throws(() => crypto.setFips(false), {
106+ code: 'ERR_CRYPTO_FIPS_FORCED',
107+ });
108+ assert.strictEqual(crypto.getFips(), 1);
109+ }
110+ ` ,
111+ ] , { stdout : '' , stderr : '' } ) ;
112+ }
113+ } else {
114+ // --enable-fips should raise an error if OpenSSL is not FIPS enabled.
115+ testHelper (
116+ testFipsCrypto ( ) ? 'stdout' : 'stderr' ,
117+ [ '--enable-fips' ] ,
118+ testFipsCrypto ( ) ? kNoFailure : kGenericUserError ,
119+ testFipsCrypto ( ) ? FIPS_ENABLED : FIPS_ENABLE_ERROR_STRING ,
120+ 'require("crypto").getFips()' ,
121+ process . env ) ;
89122
90- // --force-fips should raise an error if OpenSSL is not FIPS enabled.
91- testHelper (
92- testFipsCrypto ( ) ? 'stdout' : 'stderr' ,
93- [ '--force-fips' ] ,
94- testFipsCrypto ( ) ? kNoFailure : kGenericUserError ,
95- testFipsCrypto ( ) ? FIPS_ENABLED : FIPS_FORCE_ERROR_STRING ,
96- 'require("crypto").getFips()' ,
97- process . env ) ;
123+ // --force-fips should raise an error if OpenSSL is not FIPS enabled.
124+ testHelper (
125+ testFipsCrypto ( ) ? 'stdout' : 'stderr' ,
126+ [ '--force-fips' ] ,
127+ testFipsCrypto ( ) ? kNoFailure : kGenericUserError ,
128+ testFipsCrypto ( ) ? FIPS_ENABLED : FIPS_FORCE_ERROR_STRING ,
129+ 'require("crypto").getFips()' ,
130+ process . env ) ;
98131
99- // Explicit provider mode should preserve the behavior of bare --force-fips.
100- testHelper (
101- testFipsCrypto ( ) ? 'stdout' : 'stderr' ,
102- [ '--force-fips=provider' ] ,
103- testFipsCrypto ( ) ? kNoFailure : kGenericUserError ,
104- testFipsCrypto ( ) ? FIPS_ENABLED : FIPS_FORCE_ERROR_STRING ,
105- 'require("crypto").getFips()' ,
106- process . env ) ;
132+ // Explicit provider mode should preserve the behavior of bare --force-fips.
133+ testHelper (
134+ testFipsCrypto ( ) ? 'stdout' : 'stderr' ,
135+ [ '--force-fips=provider' ] ,
136+ testFipsCrypto ( ) ? kNoFailure : kGenericUserError ,
137+ testFipsCrypto ( ) ? FIPS_ENABLED : FIPS_FORCE_ERROR_STRING ,
138+ 'require("crypto").getFips()' ,
139+ process . env ) ;
140+ }
107141
108142{
109143 spawnSyncAndAssert (
@@ -113,15 +147,7 @@ testHelper(
113147 } ) ;
114148}
115149
116- if ( hasOpenSSL ( 3 , 4 ) ) {
117- testHelper (
118- testFipsCrypto ( ) ? 'stdout' : 'stderr' ,
119- [ '--force-fips=strict' ] ,
120- testFipsCrypto ( ) ? kNoFailure : kGenericUserError ,
121- testFipsCrypto ( ) ? FIPS_ENABLED : FIPS_FORCE_ERROR_STRING ,
122- 'require("crypto").getFips()' ,
123- process . env ) ;
124- } else {
150+ if ( ! hasOpenSSL ( 3 , 4 ) ) {
125151 spawnSyncAndAssert (
126152 process . execPath , [ '--enable-fips-indicator-events' , '-e' , '0' ] , {
127153 status : 9 ,
0 commit comments