Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

tls: make server not use DHE in less than 1024bits #25514

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions doc/api/tls.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,12 @@ automatically set as a listener for the [secureConnection][] event. The

Defaults to `prime256v1`. Consult [RFC 4492] for more details.

- `dhparam`: DH parameter file to use for DHE key agreement. Use
`openssl dhparam` command to create it. If the file is invalid to
load, it is silently discarded.
- `dhparam`: A string or `Buffer` containing Diffie Hellman parameters,
required for Perfect Forward Secrecy. Use `openssl dhparam` to create it.
Its key length should be greater than or equal to 1024 bits, otherwise
it throws an error. It is strongly recommended to use 2048 bits or
more for stronger security. If omitted or invalid, it is silently
discarded and DHE ciphers won't be available.

- `handshakeTimeout`: Abort the connection if the SSL/TLS handshake does not
finish in this many milliseconds. The default is 120 seconds.
Expand Down
6 changes: 5 additions & 1 deletion lib/_tls_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ exports.createSecureContext = function createSecureContext(options, context) {
else if (options.ecdhCurve)
c.context.setECDHCurve(options.ecdhCurve);

if (options.dhparam) c.context.setDHParam(options.dhparam);
if (options.dhparam) {
var warning = c.context.setDHParam(options.dhparam);
if (warning)
console.trace(warning);
}

if (options.crl) {
if (util.isArray(options.crl)) {
Expand Down
9 changes: 9 additions & 0 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,16 @@ void SecureContext::SetDHParam(const FunctionCallbackInfo<Value>& args) {
if (dh == NULL)
return;

const int size = BN_num_bits(dh->p);
if (size < 1024) {
return env->ThrowError("DH parameter is less than 1024 bits");
} else if (size < 2048) {
args.GetReturnValue().Set(FIXED_ONE_BYTE_STRING(
env->isolate(), "WARNING: DH parameter is less than 2048 bits"));
}

SSL_CTX_set_options(sc->ctx_, SSL_OP_SINGLE_DH_USE);

int r = SSL_CTX_set_tmp_dh(sc->ctx_, dh);
DH_free(dh);

Expand Down
10 changes: 6 additions & 4 deletions test/simple/test-tls-dhe.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ function test(keylen, expectedCipher, cb) {
}

function test512() {
test(512, 'DHE-RSA-AES128-SHA256', test1024);
ntests++;
assert.throws(function() {
test(512, 'DHE-RSA-AES128-SHA256', null);
}, /DH parameter is less than 1024 bits/);
}

function test1024() {
Expand All @@ -96,12 +97,13 @@ function test2048() {
}

function testError() {
test('error', 'ECDHE-RSA-AES128-SHA256', null);
test('error', 'ECDHE-RSA-AES128-SHA256', test512);
ntests++;
}

test512();
test1024();

process.on('exit', function() {
assert.equal(ntests, nsuccess);
assert.equal(ntests, 3);
});