Skip to content

Commit da3cc16

Browse files
jasnellBridgeAR
authored andcommitted
crypto: docs-only deprecate crypto.fips, replace
Docs-only deprecate the getter/setter crypto.fips and replace with crypto.setFips() and crypto.getFips() This is specifically in preparation for ESM module support PR-URL: nodejs#18335 Refs: nodejs#18131 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 5d92fed commit da3cc16

File tree

4 files changed

+65
-30
lines changed

4 files changed

+65
-30
lines changed

doc/api/crypto.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,11 +1288,15 @@ become deprecated in a future Node.js release.
12881288
### crypto.fips
12891289
<!-- YAML
12901290
added: v6.0.0
1291+
deprecated: REPLACEME
12911292
-->
12921293

12931294
Property for checking and controlling whether a FIPS compliant crypto provider
12941295
is currently in use. Setting to true requires a FIPS build of Node.js.
12951296

1297+
This property is deprecated. Please use `crypto.setFips()` and
1298+
`crypto.getFips()` instead.
1299+
12961300
### crypto.createCipher(algorithm, password[, options])
12971301
<!-- YAML
12981302
added: v0.1.94
@@ -1665,6 +1669,14 @@ const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
16651669
console.log(aliceSecret === bobSecret);
16661670
```
16671671

1672+
### crypto.getFips()
1673+
<!-- YAML
1674+
added: REPLACEME
1675+
-->
1676+
1677+
Returns `true` if and only if a FIPS compliant crypto provider is
1678+
currently in use.
1679+
16681680
### crypto.getHashes()
16691681
<!-- YAML
16701682
added: v0.9.3
@@ -2086,6 +2098,15 @@ is a bit field taking one of or a mix of the following flags (defined in
20862098
* `crypto.constants.ENGINE_METHOD_ALL`
20872099
* `crypto.constants.ENGINE_METHOD_NONE`
20882100

2101+
### crypto.setFips(bool)
2102+
<!-- YAML
2103+
added: REPLACEME
2104+
-->
2105+
* `bool` {boolean} `true` to enable FIPS mode.
2106+
2107+
Enables the FIPS compliant crypto provider in a FIPS-enabled Node.js build.
2108+
Throws an error if FIPS mode is not available.
2109+
20892110
### crypto.timingSafeEqual(a, b)
20902111
<!-- YAML
20912112
added: v6.6.0

doc/api/deprecations.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ is strongly recommended:
8282

8383
* [`Buffer.alloc(size[, fill[, encoding]])`][alloc] - Create a `Buffer` with
8484
*initialized* memory.
85-
* [`Buffer.allocUnsafe(size)`][alloc_unsafe_size] - Create a `Buffer` with
85+
* [`Buffer.allocUnsafe(size)`][alloc_unsafe_size] - Create a `Buffer` with
8686
*uninitialized* memory.
8787
* [`Buffer.allocUnsafeSlow(size)`][] - Create a `Buffer` with *uninitialized*
8888
memory.
8989
* [`Buffer.from(array)`][] - Create a `Buffer` with a copy of `array`
90-
* [`Buffer.from(arrayBuffer[, byteOffset[, length]])`][from_arraybuffer] -
90+
* [`Buffer.from(arrayBuffer[, byteOffset[, length]])`][from_arraybuffer] -
9191
Create a `Buffer` that wraps the given `arrayBuffer`.
9292
* [`Buffer.from(buffer)`][] - Create a `Buffer` that copies `buffer`.
93-
* [`Buffer.from(string[, encoding])`][from_string_encoding] - Create a `Buffer`
93+
* [`Buffer.from(string[, encoding])`][from_string_encoding] - Create a `Buffer`
9494
that copies `string`.
9595

9696
<a id="DEP0006"></a>
@@ -784,6 +784,14 @@ Importing assert directly is not recommended as the exposed functions will use
784784
loose equality checks. Use `require('assert').strict` instead. The API is the
785785
same as the legacy assert but it will always use strict equality checks.
786786
787+
<a id="DEP0093"></a>
788+
### DEP0093: crypto.fips is deprecated and replaced.
789+
790+
Type: Documentation-only
791+
792+
The [`crypto.fips`][] property is deprecated. Please use `crypto.setFips()`
793+
and `crypto.getFips()` instead.
794+
787795
<a id="DEP0098"></a>
788796
### DEP0098: AsyncHooks Embedder AsyncResource.emit{Before,After} APIs
789797
@@ -820,6 +828,7 @@ expose values under these names.
820828
[`console.error()`]: console.html#console_console_error_data_args
821829
[`console.log()`]: console.html#console_console_log_data_args
822830
[`crypto.createCredentials()`]: crypto.html#crypto_crypto_createcredentials_details
831+
[`crypto.fips`]: crypto.html#crypto_crypto_fips
823832
[`crypto.pbkdf2()`]: crypto.html#crypto_crypto_pbkdf2_password_salt_iterations_keylen_digest_callback
824833
[`domain`]: domain.html
825834
[`ecdh.setPublicKey()`]: crypto.html#crypto_ecdh_setpublickey_publickey_encoding

lib/crypto.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ module.exports = exports = {
162162
rng: randomBytes,
163163
setEngine,
164164
timingSafeEqual,
165+
getFips: !fipsMode ? getFipsDisabled :
166+
fipsForced ? getFipsForced : getFipsCrypto,
167+
setFips: !fipsMode ? setFipsDisabled :
168+
fipsForced ? setFipsForced : setFipsCrypto,
165169

166170
// Classes
167171
Certificate,
@@ -196,6 +200,7 @@ function getFipsForced() {
196200
}
197201

198202
Object.defineProperties(exports, {
203+
// crypto.fips is deprecated. DEP00XX. Use crypto.getFips()/crypto.setFips()
199204
fips: {
200205
get: !fipsMode ? getFipsDisabled :
201206
fipsForced ? getFipsForced : getFipsCrypto,

test/parallel/test-crypto-fips.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,23 @@ testHelper(
6767
'stdout',
6868
[],
6969
FIPS_DISABLED,
70-
'require("crypto").fips',
70+
'require("crypto").getFips()',
7171
Object.assign({}, process.env, { 'OPENSSL_CONF': '' }));
7272

7373
// --enable-fips should turn FIPS mode on
7474
testHelper(
7575
compiledWithFips() ? 'stdout' : 'stderr',
7676
['--enable-fips'],
7777
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
78-
'require("crypto").fips',
78+
'require("crypto").getFips()',
7979
process.env);
8080

8181
//--force-fips should turn FIPS mode on
8282
testHelper(
8383
compiledWithFips() ? 'stdout' : 'stderr',
8484
['--force-fips'],
8585
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
86-
'require("crypto").fips',
86+
'require("crypto").getFips()',
8787
process.env);
8888

8989
// If Node was configured using --shared-openssl fips support might be
@@ -104,140 +104,140 @@ if (!sharedOpenSSL()) {
104104
'stdout',
105105
[`--openssl-config=${CNF_FIPS_ON}`],
106106
compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED,
107-
'require("crypto").fips',
107+
'require("crypto").getFips()',
108108
process.env);
109109

110110
// OPENSSL_CONF should be able to turn on FIPS mode
111111
testHelper(
112112
'stdout',
113113
[],
114114
compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED,
115-
'require("crypto").fips',
115+
'require("crypto").getFips()',
116116
Object.assign({}, process.env, { 'OPENSSL_CONF': CNF_FIPS_ON }));
117117

118118
// --openssl-config option should override OPENSSL_CONF
119119
testHelper(
120120
'stdout',
121121
[`--openssl-config=${CNF_FIPS_ON}`],
122122
compiledWithFips() ? FIPS_ENABLED : FIPS_DISABLED,
123-
'require("crypto").fips',
123+
'require("crypto").getFips()',
124124
Object.assign({}, process.env, { 'OPENSSL_CONF': CNF_FIPS_OFF }));
125125
}
126126

127127
testHelper(
128128
'stdout',
129129
[`--openssl-config=${CNF_FIPS_OFF}`],
130130
FIPS_DISABLED,
131-
'require("crypto").fips',
131+
'require("crypto").getFips()',
132132
Object.assign({}, process.env, { 'OPENSSL_CONF': CNF_FIPS_ON }));
133133

134134
// --enable-fips should take precedence over OpenSSL config file
135135
testHelper(
136136
compiledWithFips() ? 'stdout' : 'stderr',
137137
['--enable-fips', `--openssl-config=${CNF_FIPS_OFF}`],
138138
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
139-
'require("crypto").fips',
139+
'require("crypto").getFips()',
140140
process.env);
141141

142142
// OPENSSL_CONF should _not_ make a difference to --enable-fips
143143
testHelper(
144144
compiledWithFips() ? 'stdout' : 'stderr',
145145
['--enable-fips'],
146146
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
147-
'require("crypto").fips',
147+
'require("crypto").getFips()',
148148
Object.assign({}, process.env, { 'OPENSSL_CONF': CNF_FIPS_OFF }));
149149

150150
// --force-fips should take precedence over OpenSSL config file
151151
testHelper(
152152
compiledWithFips() ? 'stdout' : 'stderr',
153153
['--force-fips', `--openssl-config=${CNF_FIPS_OFF}`],
154154
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
155-
'require("crypto").fips',
155+
'require("crypto").getFips()',
156156
process.env);
157157

158158
// Using OPENSSL_CONF should not make a difference to --force-fips
159159
testHelper(
160160
compiledWithFips() ? 'stdout' : 'stderr',
161161
['--force-fips'],
162162
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
163-
'require("crypto").fips',
163+
'require("crypto").getFips()',
164164
Object.assign({}, process.env, { 'OPENSSL_CONF': CNF_FIPS_OFF }));
165165

166166
// setFipsCrypto should be able to turn FIPS mode on
167167
testHelper(
168168
compiledWithFips() ? 'stdout' : 'stderr',
169169
[],
170170
compiledWithFips() ? FIPS_ENABLED : FIPS_ERROR_STRING,
171-
'(require("crypto").fips = true,' +
172-
'require("crypto").fips)',
171+
'(require("crypto").setFips(true),' +
172+
'require("crypto").getFips())',
173173
process.env);
174174

175175
// setFipsCrypto should be able to turn FIPS mode on and off
176176
testHelper(
177177
compiledWithFips() ? 'stdout' : 'stderr',
178178
[],
179179
compiledWithFips() ? FIPS_DISABLED : FIPS_ERROR_STRING,
180-
'(require("crypto").fips = true,' +
181-
'require("crypto").fips = false,' +
182-
'require("crypto").fips)',
180+
'(require("crypto").setFips(true),' +
181+
'require("crypto").setFips(false),' +
182+
'require("crypto").getFips())',
183183
process.env);
184184

185185
// setFipsCrypto takes precedence over OpenSSL config file, FIPS on
186186
testHelper(
187187
compiledWithFips() ? 'stdout' : 'stderr',
188188
[`--openssl-config=${CNF_FIPS_OFF}`],
189189
compiledWithFips() ? FIPS_ENABLED : FIPS_ERROR_STRING,
190-
'(require("crypto").fips = true,' +
191-
'require("crypto").fips)',
190+
'(require("crypto").setFips(true),' +
191+
'require("crypto").getFips())',
192192
process.env);
193193

194194
// setFipsCrypto takes precedence over OpenSSL config file, FIPS off
195195
testHelper(
196196
compiledWithFips() ? 'stdout' : 'stderr',
197197
[`--openssl-config=${CNF_FIPS_ON}`],
198198
compiledWithFips() ? FIPS_DISABLED : FIPS_ERROR_STRING,
199-
'(require("crypto").fips = false,' +
200-
'require("crypto").fips)',
199+
'(require("crypto").setFips(false),' +
200+
'require("crypto").getFips())',
201201
process.env);
202202

203203
// --enable-fips does not prevent use of setFipsCrypto API
204204
testHelper(
205205
compiledWithFips() ? 'stdout' : 'stderr',
206206
['--enable-fips'],
207207
compiledWithFips() ? FIPS_DISABLED : OPTION_ERROR_STRING,
208-
'(require("crypto").fips = false,' +
209-
'require("crypto").fips)',
208+
'(require("crypto").setFips(false),' +
209+
'require("crypto").getFips())',
210210
process.env);
211211

212212
// --force-fips prevents use of setFipsCrypto API
213213
testHelper(
214214
'stderr',
215215
['--force-fips'],
216216
compiledWithFips() ? FIPS_ERROR_STRING2 : OPTION_ERROR_STRING,
217-
'require("crypto").fips = false',
217+
'require("crypto").setFips(false)',
218218
process.env);
219219

220220
// --force-fips makes setFipsCrypto enable a no-op (FIPS stays on)
221221
testHelper(
222222
compiledWithFips() ? 'stdout' : 'stderr',
223223
['--force-fips'],
224224
compiledWithFips() ? FIPS_ENABLED : OPTION_ERROR_STRING,
225-
'(require("crypto").fips = true,' +
226-
'require("crypto").fips)',
225+
'(require("crypto").setFips(true),' +
226+
'require("crypto").getFips())',
227227
process.env);
228228

229229
// --force-fips and --enable-fips order does not matter
230230
testHelper(
231231
'stderr',
232232
['--force-fips', '--enable-fips'],
233233
compiledWithFips() ? FIPS_ERROR_STRING2 : OPTION_ERROR_STRING,
234-
'require("crypto").fips = false',
234+
'require("crypto").setFips(false)',
235235
process.env);
236236

237237
//--enable-fips and --force-fips order does not matter
238238
testHelper(
239239
'stderr',
240240
['--enable-fips', '--force-fips'],
241241
compiledWithFips() ? FIPS_ERROR_STRING2 : OPTION_ERROR_STRING,
242-
'require("crypto").fips = false',
242+
'require("crypto").setFips(false)',
243243
process.env);

0 commit comments

Comments
 (0)