Skip to content
This repository was archived by the owner on Aug 31, 2018. It is now read-only.

Commit 19dc5c8

Browse files
jasnellQard
authored andcommitted
crypto: migrate setFipsCrypto to internal/errors
With the exception of ThrowCryptoError, use internal/errors to report fips unavailable or forced PR-URL: nodejs/node#16428 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 40de606 commit 19dc5c8

File tree

6 files changed

+65
-19
lines changed

6 files changed

+65
-19
lines changed

doc/api/errors.md

+12
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,17 @@ Used when an invalid value for the `format` argument has been passed to the
643643
Used when an invalid crypto engine identifier is passed to
644644
[`require('crypto').setEngine()`][].
645645

646+
<a id="ERR_CRYPTO_FIPS_FORCED"></a>
647+
### ERR_CRYPTO_FIPS_FORCED
648+
649+
Used when trying to enable or disable FIPS mode in the crypto module and
650+
the [`--force-fips`][] command-line argument is used.
651+
652+
<a id="ERR_CRYPTO_FIPS_UNAVAILABLE"></a>
653+
### ERR_CRYPTO_FIPS_UNAVAILABLE
654+
655+
Used when trying to enable or disable FIPS mode when FIPS is not available.
656+
646657
<a id="ERR_CRYPTO_HASH_DIGEST_NO_UTF16"></a>
647658
### ERR_CRYPTO_HASH_DIGEST_NO_UTF16
648659

@@ -1440,6 +1451,7 @@ Used when a given value is out of the accepted range.
14401451
Used when an attempt is made to use a `zlib` object after it has already been
14411452
closed.
14421453

1454+
[`--force-fips`]: cli.html#cli_force_fips
14431455
[`crypto.timingSafeEqual()`]: crypto.html#crypto_crypto_timingsafeequal_a_b
14441456
[`dgram.createSocket()`]: dgram.html#dgram_dgram_createsocket_options_callback
14451457
[`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE

lib/crypto.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ const {
3030
} = require('internal/util');
3131
assertCrypto();
3232

33+
const errors = require('internal/errors');
3334
const constants = process.binding('constants').crypto;
35+
const {
36+
fipsMode,
37+
fipsForced
38+
} = process.binding('config');
3439
const {
3540
getFipsCrypto,
3641
setFipsCrypto,
@@ -173,10 +178,29 @@ module.exports = exports = {
173178
Verify
174179
};
175180

181+
function setFipsDisabled() {
182+
throw new errors.Error('ERR_CRYPTO_FIPS_UNAVAILABLE');
183+
}
184+
185+
function setFipsForced(val) {
186+
if (val) return;
187+
throw new errors.Error('ERR_CRYPTO_FIPS_FORCED');
188+
}
189+
190+
function getFipsDisabled() {
191+
return 0;
192+
}
193+
194+
function getFipsForced() {
195+
return 1;
196+
}
197+
176198
Object.defineProperties(exports, {
177199
fips: {
178-
get: getFipsCrypto,
179-
set: setFipsCrypto
200+
get: !fipsMode ? getFipsDisabled :
201+
fipsForced ? getFipsForced : getFipsCrypto,
202+
set: !fipsMode ? setFipsDisabled :
203+
fipsForced ? setFipsForced : setFipsCrypto
180204
},
181205
DEFAULT_ENCODING: {
182206
enumerable: true,

lib/internal/errors.js

+3
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ E('ERR_CONSOLE_WRITABLE_STREAM',
156156
E('ERR_CPU_USAGE', 'Unable to obtain cpu usage %s');
157157
E('ERR_CRYPTO_ECDH_INVALID_FORMAT', 'Invalid ECDH format: %s');
158158
E('ERR_CRYPTO_ENGINE_UNKNOWN', 'Engine "%s" was not found');
159+
E('ERR_CRYPTO_FIPS_FORCED',
160+
'Cannot set FIPS mode, it was forced with --force-fips at startup.');
161+
E('ERR_CRYPTO_FIPS_UNAVAILABLE', 'Cannot set FIPS mode in a non-FIPS build.');
159162
E('ERR_CRYPTO_HASH_DIGEST_NO_UTF16', 'hash.digest() does not support UTF-16');
160163
E('ERR_CRYPTO_HASH_FINALIZED', 'Digest already called');
161164
E('ERR_CRYPTO_HASH_UPDATE_FAILED', 'Hash update failed');

src/node_config.cc

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ static void InitConfig(Local<Object> target,
4444
Environment* env = Environment::GetCurrent(context);
4545
Isolate* isolate = env->isolate();
4646

47+
#ifdef NODE_FIPS_MODE
48+
READONLY_BOOLEAN_PROPERTY("fipsMode");
49+
if (force_fips_crypto)
50+
READONLY_BOOLEAN_PROPERTY("fipsForced");
51+
#endif
52+
4753
#ifdef NODE_HAVE_I18N_SUPPORT
4854

4955
READONLY_BOOLEAN_PROPERTY("hasIntl");

src/node_crypto.cc

+9-13
Original file line numberDiff line numberDiff line change
@@ -5962,32 +5962,24 @@ void SetEngine(const FunctionCallbackInfo<Value>& args) {
59625962
}
59635963
#endif // !OPENSSL_NO_ENGINE
59645964

5965+
#ifdef NODE_FIPS_MODE
59655966
void GetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
5966-
if (FIPS_mode()) {
5967-
args.GetReturnValue().Set(1);
5968-
} else {
5969-
args.GetReturnValue().Set(0);
5970-
}
5967+
args.GetReturnValue().Set(FIPS_mode() ? 1 : 0);
59715968
}
59725969

59735970
void SetFipsCrypto(const FunctionCallbackInfo<Value>& args) {
5971+
CHECK(!force_fips_crypto);
59745972
Environment* env = Environment::GetCurrent(args);
5975-
#ifdef NODE_FIPS_MODE
59765973
const bool enabled = FIPS_mode();
59775974
const bool enable = args[0]->BooleanValue();
59785975
if (enable == enabled)
59795976
return; // No action needed.
5980-
if (force_fips_crypto) {
5981-
return env->ThrowError(
5982-
"Cannot set FIPS mode, it was forced with --force-fips at startup.");
5983-
} else if (!FIPS_mode_set(enable)) {
5977+
if (!FIPS_mode_set(enable)) {
59845978
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
59855979
return ThrowCryptoError(env, err);
59865980
}
5987-
#else
5988-
return env->ThrowError("Cannot set FIPS mode in a non-FIPS build.");
5989-
#endif /* NODE_FIPS_MODE */
59905981
}
5982+
#endif /* NODE_FIPS_MODE */
59915983

59925984
void InitCrypto(Local<Object> target,
59935985
Local<Value> unused,
@@ -6013,8 +6005,12 @@ void InitCrypto(Local<Object> target,
60136005
#ifndef OPENSSL_NO_ENGINE
60146006
env->SetMethod(target, "setEngine", SetEngine);
60156007
#endif // !OPENSSL_NO_ENGINE
6008+
6009+
#ifdef NODE_FIPS_MODE
60166010
env->SetMethod(target, "getFipsCrypto", GetFipsCrypto);
60176011
env->SetMethod(target, "setFipsCrypto", SetFipsCrypto);
6012+
#endif
6013+
60186014
env->SetMethod(target, "PBKDF2", PBKDF2);
60196015
env->SetMethod(target, "randomBytes", RandomBytes);
60206016
env->SetMethod(target, "randomFill", RandomBytesBuffer);

test/parallel/test-crypto-fips.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ const fixtures = require('../common/fixtures');
1010

1111
const FIPS_ENABLED = 1;
1212
const FIPS_DISABLED = 0;
13-
const FIPS_ERROR_STRING = 'Error: Cannot set FIPS mode';
13+
const FIPS_ERROR_STRING =
14+
'Error [ERR_CRYPTO_FIPS_UNAVAILABLE]: Cannot set FIPS mode in a ' +
15+
'non-FIPS build.';
16+
const FIPS_ERROR_STRING2 =
17+
'Error [ERR_CRYPTO_FIPS_FORCED]: Cannot set FIPS mode, it was forced with ' +
18+
'--force-fips at startup.';
1419
const OPTION_ERROR_STRING = 'bad option';
1520

1621
const CNF_FIPS_ON = fixtures.path('openssl_fips_enabled.cnf');
@@ -208,7 +213,7 @@ testHelper(
208213
testHelper(
209214
'stderr',
210215
['--force-fips'],
211-
compiledWithFips() ? FIPS_ERROR_STRING : OPTION_ERROR_STRING,
216+
compiledWithFips() ? FIPS_ERROR_STRING2 : OPTION_ERROR_STRING,
212217
'require("crypto").fips = false',
213218
process.env);
214219

@@ -225,14 +230,14 @@ testHelper(
225230
testHelper(
226231
'stderr',
227232
['--force-fips', '--enable-fips'],
228-
compiledWithFips() ? FIPS_ERROR_STRING : OPTION_ERROR_STRING,
233+
compiledWithFips() ? FIPS_ERROR_STRING2 : OPTION_ERROR_STRING,
229234
'require("crypto").fips = false',
230235
process.env);
231236

232237
//--enable-fips and --force-fips order does not matter
233238
testHelper(
234239
'stderr',
235240
['--enable-fips', '--force-fips'],
236-
compiledWithFips() ? FIPS_ERROR_STRING : OPTION_ERROR_STRING,
241+
compiledWithFips() ? FIPS_ERROR_STRING2 : OPTION_ERROR_STRING,
237242
'require("crypto").fips = false',
238243
process.env);

0 commit comments

Comments
 (0)