Skip to content

Commit e499ea8

Browse files
stefanmbmhdawson
authored andcommitted
test: skip/replace weak crypto tests in FIPS mode
FIPS 140-2 does not permit the use of MD5 and RC4, skip or tests that use them, or substitute with stronger crypto where applicable. PR-URL: #3757 Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: James Snell <[email protected]> Reviewed-By: Shigeki Ohtsu <[email protected]>
1 parent 5900d14 commit e499ea8

9 files changed

+89
-66
lines changed

test/parallel/test-crypto-binary-default.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,13 @@ var rfc2202_sha1 = [
324324
];
325325

326326
for (var i = 0, l = rfc2202_md5.length; i < l; i++) {
327-
assert.equal(rfc2202_md5[i]['hmac'],
328-
crypto.createHmac('md5', rfc2202_md5[i]['key'])
329-
.update(rfc2202_md5[i]['data'])
330-
.digest('hex'),
331-
'Test HMAC-MD5 : Test case ' + (i + 1) + ' rfc 2202');
327+
if (!common.hasFipsCrypto) {
328+
assert.equal(rfc2202_md5[i]['hmac'],
329+
crypto.createHmac('md5', rfc2202_md5[i]['key'])
330+
.update(rfc2202_md5[i]['data'])
331+
.digest('hex'),
332+
'Test HMAC-MD5 : Test case ' + (i + 1) + ' rfc 2202');
333+
}
332334
}
333335
for (var i = 0, l = rfc2202_sha1.length; i < l; i++) {
334336
assert.equal(rfc2202_sha1[i]['hmac'],
@@ -339,15 +341,19 @@ for (var i = 0, l = rfc2202_sha1.length; i < l; i++) {
339341
}
340342

341343
// Test hashing
342-
var a0 = crypto.createHash('sha1').update('Test123').digest('hex');
343-
var a1 = crypto.createHash('md5').update('Test123').digest('binary');
344+
var a1 = crypto.createHash('sha1').update('Test123').digest('hex');
344345
var a2 = crypto.createHash('sha256').update('Test123').digest('base64');
345346
var a3 = crypto.createHash('sha512').update('Test123').digest(); // binary
346347
var a4 = crypto.createHash('sha1').update('Test123').digest('buffer');
347348

348-
assert.equal(a0, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'Test SHA1');
349-
assert.equal(a1, 'h\u00ea\u00cb\u0097\u00d8o\fF!\u00fa+\u000e\u0017\u00ca' +
350-
'\u00bd\u008c', 'Test MD5 as binary');
349+
if (!common.hasFipsCrypto) {
350+
var a0 = crypto.createHash('md5').update('Test123').digest('binary');
351+
assert.equal(a0, 'h\u00ea\u00cb\u0097\u00d8o\fF!\u00fa+\u000e\u0017\u00ca' +
352+
'\u00bd\u008c', 'Test MD5 as binary');
353+
}
354+
355+
assert.equal(a1, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'Test SHA1');
356+
351357
assert.equal(a2, '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=',
352358
'Test SHA256 as base64');
353359

test/parallel/test-crypto-hash.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ if (!common.hasCrypto) {
1111
var crypto = require('crypto');
1212

1313
// Test hashing
14-
var a0 = crypto.createHash('sha1').update('Test123').digest('hex');
15-
var a1 = crypto.createHash('md5').update('Test123').digest('binary');
14+
var a1 = crypto.createHash('sha1').update('Test123').digest('hex');
1615
var a2 = crypto.createHash('sha256').update('Test123').digest('base64');
1716
var a3 = crypto.createHash('sha512').update('Test123').digest(); // binary
1817
var a4 = crypto.createHash('sha1').update('Test123').digest('buffer');
@@ -38,9 +37,12 @@ a8.write('');
3837
a8.end();
3938
a8 = a8.read();
4039

41-
assert.equal(a0, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'Test SHA1');
42-
assert.equal(a1, 'h\u00ea\u00cb\u0097\u00d8o\fF!\u00fa+\u000e\u0017\u00ca' +
43-
'\u00bd\u008c', 'Test MD5 as binary');
40+
if (!common.hasFipsCrypto) {
41+
var a0 = crypto.createHash('md5').update('Test123').digest('binary');
42+
assert.equal(a0, 'h\u00ea\u00cb\u0097\u00d8o\fF!\u00fa+\u000e\u0017\u00ca' +
43+
'\u00bd\u008c', 'Test MD5 as binary');
44+
}
45+
assert.equal(a1, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'Test SHA1');
4446
assert.equal(a2, '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=',
4547
'Test SHA256 as base64');
4648
assert.deepEqual(

test/parallel/test-crypto-hmac.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ var wikipedia = [
6161

6262
for (var i = 0, l = wikipedia.length; i < l; i++) {
6363
for (var hash in wikipedia[i]['hmac']) {
64+
// FIPS does not support MD5.
65+
if (common.hasFipsCrypto && hash == 'md5' )
66+
continue;
6467
var result = crypto.createHmac(hash, wikipedia[i]['key'])
6568
.update(wikipedia[i]['data'])
6669
.digest('hex');
@@ -346,12 +349,14 @@ var rfc2202_sha1 = [
346349
}
347350
];
348351

349-
for (var i = 0, l = rfc2202_md5.length; i < l; i++) {
350-
assert.equal(rfc2202_md5[i]['hmac'],
351-
crypto.createHmac('md5', rfc2202_md5[i]['key'])
352-
.update(rfc2202_md5[i]['data'])
353-
.digest('hex'),
354-
'Test HMAC-MD5 : Test case ' + (i + 1) + ' rfc 2202');
352+
if (!common.hasFipsCrypto) {
353+
for (var i = 0, l = rfc2202_md5.length; i < l; i++) {
354+
assert.equal(rfc2202_md5[i]['hmac'],
355+
crypto.createHmac('md5', rfc2202_md5[i]['key'])
356+
.update(rfc2202_md5[i]['data'])
357+
.digest('hex'),
358+
'Test HMAC-MD5 : Test case ' + (i + 1) + ' rfc 2202');
359+
}
355360
}
356361
for (var i = 0, l = rfc2202_sha1.length; i < l; i++) {
357362
assert.equal(rfc2202_sha1[i]['hmac'],

test/parallel/test-crypto-stream.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@ Stream2buffer.prototype._write = function(data, encodeing, done) {
2626
return done(null);
2727
};
2828

29-
// Create an md5 hash of "Hallo world"
30-
var hasher1 = crypto.createHash('md5');
31-
hasher1.pipe(new Stream2buffer(common.mustCall(function end(err, hash) {
32-
assert.equal(err, null);
33-
assert.equal(hash.toString('hex'), '06460dadb35d3d503047ce750ceb2d07');
34-
})));
35-
hasher1.end('Hallo world');
36-
37-
// Simpler check for unpipe, setEncoding, pause and resume
38-
crypto.createHash('md5').unpipe({});
39-
crypto.createHash('md5').setEncoding('utf8');
40-
crypto.createHash('md5').pause();
41-
crypto.createHash('md5').resume();
29+
if (!common.hasFipsCrypto) {
30+
// Create an md5 hash of "Hallo world"
31+
var hasher1 = crypto.createHash('md5');
32+
hasher1.pipe(new Stream2buffer(common.mustCall(function end(err, hash) {
33+
assert.equal(err, null);
34+
assert.equal(hash.toString('hex'), '06460dadb35d3d503047ce750ceb2d07');
35+
})));
36+
hasher1.end('Hallo world');
37+
38+
// Simpler check for unpipe, setEncoding, pause and resume
39+
crypto.createHash('md5').unpipe({});
40+
crypto.createHash('md5').setEncoding('utf8');
41+
crypto.createHash('md5').pause();
42+
crypto.createHash('md5').resume();
43+
}
4244

4345
// Decipher._flush() should emit an error event, not an exception.
4446
var key = new Buffer('48fb56eb10ffeb13fc0ef551bbca3b1b', 'hex'),

test/parallel/test-tls-ecdh.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var fs = require('fs');
1414
var options = {
1515
key: fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem'),
1616
cert: fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem'),
17-
ciphers: '-ALL:ECDHE-RSA-RC4-SHA',
17+
ciphers: '-ALL:ECDHE-RSA-AES128-SHA256',
1818
ecdhCurve: 'prime256v1'
1919
};
2020

test/parallel/test-tls-getcipher.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ if (!common.hasCrypto) {
99
var tls = require('tls');
1010

1111
var fs = require('fs');
12-
var cipher_list = ['RC4-SHA', 'AES256-SHA'];
12+
var cipher_list = ['AES128-SHA256', 'AES256-SHA256'];
1313
var cipher_version_pattern = /TLS|SSL/;
1414
var options = {
1515
key: fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem'),

test/parallel/test-tls-set-ciphers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var fs = require('fs');
1919
var options = {
2020
key: fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem'),
2121
cert: fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem'),
22-
ciphers: 'RC4-MD5'
22+
ciphers: 'DES-CBC3-SHA'
2323
};
2424

2525
var reply = 'I AM THE WALRUS'; // something recognizable

test/pummel/test-crypto-dh.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,30 @@ assert.throws(function() {
2020
});
2121

2222
var hashes = {
23-
modp1 : 'b4b330a6ffeacfbd861e7fe2135b4431',
24-
modp2 : '7c3c5cad8b9f378d88f1dd64a4b6413a',
25-
modp5 : 'b1d2acc22c542e08669a5c5ae812694d',
26-
modp14 : '8d041538cecc1a7d915ba4b718f8ad20',
27-
modp15 : 'dc3b93def24e078c4fbf92d5e14ba69b',
28-
modp16 : 'a273487f46f699461f613b3878d9dfd9',
29-
modp17 : 'dc76e09935310348c492de9bd82014d0',
30-
modp18 : 'db08973bfd2371758a69db180871c993'
23+
modp1 : '630e9acd2cc63f7e80d8507624ba60ac0757201a',
24+
modp2 : '18f7aa964484137f57bca64b21917a385b6a0b60',
25+
modp5 : 'c0a8eec0c2c8a5ec2f9c26f9661eb339a010ec61',
26+
modp14 : 'af5455606fe74cec49782bb374e4c63c9b1d132c',
27+
modp15 : '7bdd39e5cdbb9748113933e5c2623b559c534e74',
28+
modp16 : 'daea5277a7ad0116e734a8e0d2f297ef759d1161',
29+
modp17 : '3b62aaf0142c2720f0bf26a9589b0432c00eadc1',
30+
modp18 : 'a870b491bbbec9b131ae9878d07449d32e54f160'
3131
};
3232

3333
for (var name in hashes) {
3434
var group = crypto.getDiffieHellman(name);
3535
var private_key = group.getPrime('hex');
3636
var hash1 = hashes[name];
37-
var hash2 = crypto.createHash('md5')
37+
var hash2 = crypto.createHash('sha1')
3838
.update(private_key.toUpperCase()).digest('hex');
3939
assert.equal(hash1, hash2);
4040
assert.equal(group.getGenerator('hex'), '02');
4141
}
4242

4343
for (var name in hashes) {
44+
// modp1 is 768 bits, FIPS requires >= 1024
45+
if (name == 'modp1' && common.hasFipsCrypto)
46+
continue;
4447
var group1 = crypto.getDiffieHellman(name);
4548
var group2 = crypto.getDiffieHellman(name);
4649
group1.generateKeys();

test/sequential/test-tls-honorcipherorder.js

+25-20
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ var tls = require('tls');
1010

1111
var fs = require('fs');
1212
var nconns = 0;
13-
// test only in TLSv1 to use DES which is no longer supported TLSv1.2
14-
// to be safe when the default method is updated in the future
15-
var SSL_Method = 'TLSv1_method';
13+
14+
// We explicitly set TLS version to 1.2 so as to be safe when the
15+
// default method is updated in the future
16+
var SSL_Method = 'TLSv1_2_method';
1617
var localhost = '127.0.0.1';
1718

1819
process.on('exit', function() {
@@ -24,7 +25,8 @@ function test(honorCipherOrder, clientCipher, expectedCipher, cb) {
2425
secureProtocol: SSL_Method,
2526
key: fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem'),
2627
cert: fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem'),
27-
ciphers: 'DES-CBC-SHA:AES256-SHA:RC4-SHA:ECDHE-RSA-AES256-SHA',
28+
ciphers: 'AES256-SHA256:AES128-GCM-SHA256:AES128-SHA256:' +
29+
'ECDHE-RSA-AES128-GCM-SHA256',
2830
honorCipherOrder: !!honorCipherOrder
2931
};
3032

@@ -57,37 +59,40 @@ test1();
5759

5860
function test1() {
5961
// Client has the preference of cipher suites by default
60-
test(false, 'AES256-SHA:DES-CBC-SHA:RC4-SHA', 'AES256-SHA', test2);
62+
test(false, 'AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256',
63+
'AES128-GCM-SHA256', test2);
6164
}
6265

6366
function test2() {
64-
// Server has the preference of cipher suites where DES-CBC-SHA is in
65-
// the first.
66-
test(true, 'AES256-SHA:DES-CBC-SHA:RC4-SHA', 'DES-CBC-SHA', test3);
67+
// Server has the preference of cipher suites, and AES256-SHA256 is
68+
// the server's top choice.
69+
test(true, 'AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256',
70+
'AES256-SHA256', test3);
6771
}
6872

6973
function test3() {
70-
// Server has the preference of cipher suites. RC4-SHA is given
71-
// higher priority over DES-CBC-SHA among client cipher suites.
72-
test(true, 'RC4-SHA:AES256-SHA', 'AES256-SHA', test4);
74+
// Server has the preference of cipher suites. AES128-GCM-SHA256 is given
75+
// higher priority over AES128-SHA256 among client cipher suites.
76+
test(true, 'AES128-SHA256:AES128-GCM-SHA256', 'AES128-GCM-SHA256', test4);
77+
7378
}
7479

7580
function test4() {
76-
// As client has only one cipher, server has no choice in regardless
81+
// As client has only one cipher, server has no choice, irrespective
7782
// of honorCipherOrder.
78-
test(true, 'RC4-SHA', 'RC4-SHA', test5);
83+
test(true, 'AES128-SHA256', 'AES128-SHA256', test5);
7984
}
8085

8186
function test5() {
82-
// Client did not explicitly set ciphers. Ensure that client defaults to
83-
// sane ciphers. Even though server gives top priority to DES-CBC-SHA
84-
// it should not be negotiated because it's not in default client ciphers.
85-
test(true, null, 'AES256-SHA', test6);
87+
// Client did not explicitly set ciphers and client offers
88+
// tls.DEFAULT_CIPHERS. All ciphers of the server are included in the
89+
// default list so the negotiated cipher is selected according to the
90+
// server's top preference of AES256-SHA256.
91+
test(true, null, 'AES256-SHA256', test6);
8692
}
8793

8894
function test6() {
8995
// Ensure that `tls.DEFAULT_CIPHERS` is used
90-
SSL_Method = 'TLSv1_2_method';
91-
tls.DEFAULT_CIPHERS = 'ECDHE-RSA-AES256-SHA';
92-
test(true, null, 'ECDHE-RSA-AES256-SHA');
96+
tls.DEFAULT_CIPHERS = 'ECDHE-RSA-AES128-GCM-SHA256';
97+
test(true, null, 'ECDHE-RSA-AES128-GCM-SHA256');
9398
}

0 commit comments

Comments
 (0)