Skip to content

Commit 6edf04e

Browse files
jasnelladuh95
authored andcommitted
test: move crypto related common utilities in common/crypto
Since `common/crypto` already exists, it makes sense to keep crypto-related utilities there. The only exception being common.hasCrypto which is needed up front to determine if tests should be skipped. Eliminate the redundant check in hasFipsCrypto and just use crypto.getFips() directly where needed. PR-URL: #56714 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent c7a1322 commit 6edf04e

89 files changed

Lines changed: 505 additions & 288 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎test/addons/openssl-providers/providers.cjs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use strict';
22

33
constcommon=require('../../common');
4-
if(!common.hasCrypto)
4+
if(!common.hasCrypto){
55
common.skip('missing crypto');
6+
}
7+
const{ hasOpenSSL3 }=require('../../common/crypto');
68

7-
if(!common.hasOpenSSL3)
9+
if(!hasOpenSSL3){
810
common.skip('this test requires OpenSSL 3.x');
11+
}
912
constassert=require('node:assert');
1013
const{ createHash, getCiphers, getHashes }=require('node:crypto');
1114
const{ debuglog }=require('node:util');

‎test/benchmark/test-benchmark-crypto.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ const common = require('../common');
55
if(!common.hasCrypto)
66
common.skip('missing crypto');
77

8-
if(common.hasFipsCrypto)
8+
const{ getFips }=require('crypto');
9+
10+
if(getFips()){
911
common.skip('some benchmarks are FIPS-incompatible');
12+
}
1013

1114
construnBenchmark=require('../common/benchmark');
1215

‎test/common/README.md‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,6 @@ The TTY file descriptor is assumed to be capable of being writable.
226226

227227
Indicates whether OpenSSL is available.
228228

229-
### `hasFipsCrypto`
230-
231-
*[\<boolean>][<boolean>]
232-
233-
Indicates that Node.js has been linked with a FIPS compatible OpenSSL library,
234-
and that FIPS as been enabled using `--enable-fips`.
235-
236-
To only detect if the OpenSSL library is FIPS compatible, regardless if it has
237-
been enabled or not, then `process.config.variables.openssl_is_fips` can be
238-
used to determine that situation.
239-
240229
### `hasIntl`
241230

242231
*[\<boolean>][<boolean>]
@@ -417,12 +406,6 @@ Returns `true` if the exit code `exitCode` and/or signal name `signal` represent
417406
the exit code and/or signal name of a node process that aborted, `false`
418407
otherwise.
419408

420-
### `opensslCli`
421-
422-
*[\<boolean>][<boolean>]
423-
424-
Indicates whether 'opensslCli' is supported.
425-
426409
### `platformTimeout(ms)`
427410

428411
*`ms`[\<number>][<number>] | [\<bigint>][<bigint>]

‎test/common/crypto.js‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
constcommon=require('../common');
4-
if(!common.hasCrypto)
4+
if(!common.hasCrypto){
55
common.skip('missing crypto');
6+
}
67

78
constassert=require('assert');
89
constcrypto=require('crypto');
@@ -98,6 +99,27 @@ const pkcs8EncExp = getRegExpForPEM('ENCRYPTED PRIVATE KEY');
9899
constsec1Exp=getRegExpForPEM('EC PRIVATE KEY');
99100
constsec1EncExp=(cipher)=>getRegExpForPEM('EC PRIVATE KEY',cipher);
100101

102+
// Synthesize OPENSSL_VERSION_NUMBER format with the layout 0xMNN00PPSL
103+
constopensslVersionNumber=(major=0,minor=0,patch=0)=>{
104+
assert(major>=0&&major<=0xf);
105+
assert(minor>=0&&minor<=0xff);
106+
assert(patch>=0&&patch<=0xff);
107+
return(major<<28)|(minor<<20)|(patch<<4);
108+
};
109+
110+
letOPENSSL_VERSION_NUMBER;
111+
consthasOpenSSL=(major=0,minor=0,patch=0)=>{
112+
if(!common.hasCrypto)returnfalse;
113+
if(OPENSSL_VERSION_NUMBER===undefined){
114+
constregexp=/(?<m>\d+)\.(?<n>\d+)\.(?<p>\d+)/;
115+
const{ m, n, p }=process.versions.openssl.match(regexp).groups;
116+
OPENSSL_VERSION_NUMBER=opensslVersionNumber(m,n,p);
117+
}
118+
returnOPENSSL_VERSION_NUMBER>=opensslVersionNumber(major,minor,patch);
119+
};
120+
121+
letopensslCli=null;
122+
101123
module.exports={
102124
modp2buf,
103125
assertApproximateSize,
@@ -111,4 +133,32 @@ module.exports = {
111133
pkcs8EncExp,// used once
112134
sec1Exp,
113135
sec1EncExp,
136+
hasOpenSSL,
137+
gethasOpenSSL3(){
138+
returnhasOpenSSL(3);
139+
},
140+
// opensslCli defined lazily to reduce overhead of spawnSync
141+
getopensslCli(){
142+
if(opensslCli!==null)returnopensslCli;
143+
144+
if(process.config.variables.node_shared_openssl){
145+
// Use external command
146+
opensslCli='openssl';
147+
}else{
148+
constpath=require('path');
149+
// Use command built from sources included in Node.js repository
150+
opensslCli=path.join(path.dirname(process.execPath),'openssl-cli');
151+
}
152+
153+
if(exports.isWindows)opensslCli+='.exe';
154+
155+
const{ spawnSync }=require('child_process');
156+
157+
constopensslCmd=spawnSync(opensslCli,['version']);
158+
if(opensslCmd.status!==0||opensslCmd.error!==undefined){
159+
// OpenSSL command cannot be executed
160+
opensslCli=false;
161+
}
162+
returnopensslCli;
163+
},
114164
};

‎test/common/index.js‎

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
/* eslint-disable node-core/crypto-check */
2322
'use strict';
2423
constprocess=global.process;// Some tests tamper with the process global.
2524

@@ -57,25 +56,6 @@ const noop = () => {};
5756
consthasCrypto=Boolean(process.versions.openssl)&&
5857
!process.env.NODE_SKIP_CRYPTO;
5958

60-
// Synthesize OPENSSL_VERSION_NUMBER format with the layout 0xMNN00PPSL
61-
constopensslVersionNumber=(major=0,minor=0,patch=0)=>{
62-
assert(major>=0&&major<=0xf);
63-
assert(minor>=0&&minor<=0xff);
64-
assert(patch>=0&&patch<=0xff);
65-
return(major<<28)|(minor<<20)|(patch<<4);
66-
};
67-
68-
letOPENSSL_VERSION_NUMBER;
69-
consthasOpenSSL=(major=0,minor=0,patch=0)=>{
70-
if(!hasCrypto)returnfalse;
71-
if(OPENSSL_VERSION_NUMBER===undefined){
72-
constregexp=/(?<m>\d+)\.(?<n>\d+)\.(?<p>\d+)/;
73-
const{ m, n, p }=process.versions.openssl.match(regexp).groups;
74-
OPENSSL_VERSION_NUMBER=opensslVersionNumber(m,n,p);
75-
}
76-
returnOPENSSL_VERSION_NUMBER>=opensslVersionNumber(major,minor,patch);
77-
};
78-
7959
consthasQuic=hasCrypto&&!!process.config.variables.openssl_quic;
8060

8161
functionparseTestFlags(filename=process.argv[1]){
@@ -220,7 +200,6 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) {
220200
}).enable();
221201
}
222202

223-
letopensslCli=null;
224203
letinFreeBSDJail=null;
225204
letlocalhostIPv4=null;
226205

@@ -985,7 +964,6 @@ const common = {
985964
getTTYfd,
986965
hasIntl,
987966
hasCrypto,
988-
hasOpenSSL,
989967
hasQuic,
990968
hasMultiLocalhost,
991969
invalidArgTypeHelper,
@@ -1027,10 +1005,6 @@ const common = {
10271005
returnrequire('os').totalmem()>0x70000000;/* 1.75 Gb */
10281006
},
10291007

1030-
gethasFipsCrypto(){
1031-
returnhasCrypto&&require('crypto').getFips();
1032-
},
1033-
10341008
gethasIPv6(){
10351009
constiFaces=require('os').networkInterfaces();
10361010
letre;
@@ -1047,10 +1021,6 @@ const common = {
10471021
});
10481022
},
10491023

1050-
gethasOpenSSL3(){
1051-
returnhasOpenSSL(3);
1052-
},
1053-
10541024
getinFreeBSDJail(){
10551025
if(inFreeBSDJail!==null)returninFreeBSDJail;
10561026

@@ -1100,28 +1070,6 @@ const common = {
11001070
returnlocalhostIPv4;
11011071
},
11021072

1103-
// opensslCli defined lazily to reduce overhead of spawnSync
1104-
getopensslCli(){
1105-
if(opensslCli!==null)returnopensslCli;
1106-
1107-
if(process.config.variables.node_shared_openssl){
1108-
// Use external command
1109-
opensslCli='openssl';
1110-
}else{
1111-
// Use command built from sources included in Node.js repository
1112-
opensslCli=path.join(path.dirname(process.execPath),'openssl-cli');
1113-
}
1114-
1115-
if(exports.isWindows)opensslCli+='.exe';
1116-
1117-
constopensslCmd=spawnSync(opensslCli,['version']);
1118-
if(opensslCmd.status!==0||opensslCmd.error!==undefined){
1119-
// OpenSSL command cannot be executed
1120-
opensslCli=false;
1121-
}
1122-
returnopensslCli;
1123-
},
1124-
11251073
getPORT(){
11261074
if(+process.env.TEST_PARALLEL){
11271075
thrownewError('common.PORT cannot be used in a parallelized test');

‎test/common/index.mjs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const {
4141
mustNotMutateObjectDeep,
4242
mustSucceed,
4343
nodeProcessAborted,
44-
opensslCli,
4544
parseTestFlags,
4645
PIPE,
4746
platformTimeout,
@@ -97,7 +96,6 @@ export {
9796
mustNotMutateObjectDeep,
9897
mustSucceed,
9998
nodeProcessAborted,
100-
opensslCli,
10199
parseTestFlags,
102100
PIPE,
103101
platformTimeout,

‎test/parallel/test-cli-node-options.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const { Worker } = require('worker_threads');
1212

1313
constfixtures=require('../common/fixtures');
1414
consttmpdir=require('../common/tmpdir');
15+
const{ hasOpenSSL3 }=require('../common/crypto');
1516
tmpdir.refresh();
1617

1718
constprintA=path.relative(tmpdir.path,fixtures.path('printA.js'));
@@ -64,7 +65,7 @@ if (common.isLinux) {
6465
if(common.hasCrypto){
6566
expectNoWorker('--use-openssl-ca','B\n');
6667
expectNoWorker('--use-bundled-ca','B\n');
67-
if(!common.hasOpenSSL3)
68+
if(!hasOpenSSL3)
6869
expectNoWorker('--openssl-config=_ossl_cfg','B\n');
6970
}
7071

‎test/parallel/test-crypto-authenticated.js‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
// Flags: --no-warnings
2222
'use strict';
2323
constcommon=require('../common');
24-
if(!common.hasCrypto)
24+
if(!common.hasCrypto){
2525
common.skip('missing crypto');
26+
}
2627

2728
constassert=require('assert');
2829
constcrypto=require('crypto');
2930
const{ inspect }=require('util');
3031
constfixtures=require('../common/fixtures');
32+
const{ hasOpenSSL3 }=require('../common/crypto');
33+
34+
constisFipsEnabled=crypto.getFips();
3135

3236
//
3337
// Test authenticated encryption modes.
@@ -53,7 +57,7 @@ for (const test of TEST_CASES) {
5357
continue;
5458
}
5559

56-
if(common.hasFipsCrypto&&test.iv.length<24){
60+
if(isFipsEnabled&&test.iv.length<24){
5761
common.printSkipMessage('IV len < 12 bytes unsupported in FIPS mode');
5862
continue;
5963
}
@@ -95,7 +99,7 @@ for (const test of TEST_CASES) {
9599
}
96100

97101
{
98-
if(isCCM&&common.hasFipsCrypto){
102+
if(isCCM&&isFipsEnabled){
99103
assert.throws(()=>{
100104
crypto.createDecipheriv(test.algo,
101105
Buffer.from(test.key,'hex'),
@@ -286,7 +290,7 @@ for (const test of TEST_CASES) {
286290
});
287291
},errMessages.authTagLength);
288292

289-
if(!common.hasFipsCrypto){
293+
if(!isFipsEnabled){
290294
assert.throws(()=>{
291295
crypto.createDecipheriv('aes-256-ccm',
292296
'FxLKsqdmv0E9xrQhp0b1ZgI0K7JFZJM8',
@@ -312,7 +316,7 @@ for (const test of TEST_CASES) {
312316
});
313317

314318
// CCM decryption and create(De|C)ipher are unsupported in FIPS mode.
315-
if(!common.hasFipsCrypto){
319+
if(!isFipsEnabled){
316320
assert.throws(()=>{
317321
crypto.createDecipheriv(`aes-256-${mode}`,
318322
'FxLKsqdmv0E9xrQhp0b1ZgI0K7JFZJM8',
@@ -388,7 +392,7 @@ for (const test of TEST_CASES) {
388392
cipher.setAAD(Buffer.from('0123456789','hex'));
389393
},/options\.plaintextLengthrequiredforCCMmodewithAAD/);
390394

391-
if(!common.hasFipsCrypto){
395+
if(!isFipsEnabled){
392396
assert.throws(()=>{
393397
constcipher=crypto.createDecipheriv('aes-256-ccm',
394398
'FxLKsqdmv0E9xrQhp0b1ZgI0K7JFZJM8',
@@ -403,7 +407,7 @@ for (const test of TEST_CASES) {
403407

404408
// Test that final() throws in CCM mode when no authentication tag is provided.
405409
{
406-
if(!common.hasFipsCrypto){
410+
if(!isFipsEnabled){
407411
constkey=Buffer.from('1ed2233fa2223ef5d7df08546049406c','hex');
408412
constiv=Buffer.from('7305220bca40d4c90e1791e9','hex');
409413
constct=Buffer.from('8beba09d4d4d861f957d51c0794f4abf8030848e','hex');
@@ -562,7 +566,7 @@ for (const test of TEST_CASES) {
562566
]){
563567
assert.throws(()=>{
564568
cipher.final();
565-
},common.hasOpenSSL3 ? {
569+
},hasOpenSSL3 ? {
566570
code: 'ERR_OSSL_TAG_NOT_SET'
567571
} : {
568572
message: /Unsupportedstate/

‎test/parallel/test-crypto-cipheriv-decipheriv.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ if (!common.hasCrypto)
55

66
constassert=require('assert');
77
constcrypto=require('crypto');
8+
const{ hasOpenSSL3 }=require('../common/crypto');
9+
constisFipsEnabled=crypto.getFips();
810

911
functiontestCipher1(key,iv){
1012
// Test encryption and decryption with explicit key and iv
@@ -150,7 +152,7 @@ testCipher1(Buffer.from('0123456789abcd0123456789'), '12345678');
150152
testCipher1(Buffer.from('0123456789abcd0123456789'),Buffer.from('12345678'));
151153
testCipher2(Buffer.from('0123456789abcd0123456789'),Buffer.from('12345678'));
152154

153-
if(!common.hasFipsCrypto){
155+
if(!isFipsEnabled){
154156
testCipher3(Buffer.from('000102030405060708090A0B0C0D0E0F','hex'),
155157
Buffer.from('A6A6A6A6A6A6A6A6','hex'));
156158
}
@@ -193,10 +195,10 @@ assert.throws(
193195
errMessage);
194196

195197
// But all other IV lengths should be accepted.
196-
constminIvLength=common.hasOpenSSL3 ? 8 : 1;
197-
constmaxIvLength=common.hasOpenSSL3 ? 64 : 256;
198+
constminIvLength=hasOpenSSL3 ? 8 : 1;
199+
constmaxIvLength=hasOpenSSL3 ? 64 : 256;
198200
for(letn=minIvLength;n<maxIvLength;n+=1){
199-
if(common.hasFipsCrypto&&n<12)continue;
201+
if(isFipsEnabled&&n<12)continue;
200202
crypto.createCipheriv('aes-128-gcm',Buffer.alloc(16),Buffer.alloc(n));
201203
}
202204

0 commit comments

Comments
 (0)