Skip to content

Commit 0dfedb7

Browse files
Shigeki Ohtsuindutny
authored andcommitted
tls, crypto: add DHE support
In case of an invalid DH parameter file, it is sliently discarded. To use auto DH parameter in a server and DHE key length check in a client, we need to wait for the next release of OpenSSL-1.0.2. Reviewed-By: Fedor Indutny <fedor@indutny.com>
1 parent 6e453fa commit 0dfedb7

11 files changed

Lines changed: 178 additions & 1 deletion

File tree

doc/api/tls.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ automatically set as a listener for the [secureConnection][] event. The
165165

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

168+
- `dhparam`: DH parameter file to use for DHE key agreement. Use
169+
`openssl dhparam` command to create it. If the file is invalid to
170+
load, it is silently discarded.
171+
168172
- `handshakeTimeout`: Abort the connection if the SSL/TLS handshake does not
169173
finish in this many milliseconds. The default is 120 seconds.
170174

lib/_tls_common.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ exports.createSecureContext = function createSecureContext(options, context) {
9797
else if (options.ecdhCurve)
9898
c.context.setECDHCurve(options.ecdhCurve);
9999

100+
if (options.dhparam) c.context.setDHParam(options.dhparam);
101+
100102
if (options.crl) {
101103
if (util.isArray(options.crl)) {
102104
for (var i = 0, len = options.crl.length; i < len; i++) {

lib/_tls_wrap.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ function Server(/* [options], listener */) {
600600
ca: self.ca,
601601
ciphers: self.ciphers,
602602
ecdhCurve: self.ecdhCurve,
603+
dhparam: self.dhparam,
603604
secureProtocol: self.secureProtocol,
604605
secureOptions: self.secureOptions,
605606
honorCipherOrder: self.honorCipherOrder,
@@ -718,6 +719,7 @@ Server.prototype.setOptions = function(options) {
718719
if (options.ciphers) this.ciphers = options.ciphers;
719720
if (!util.isUndefined(options.ecdhCurve))
720721
this.ecdhCurve = options.ecdhCurve;
722+
if (options.dhparam) this.dhparam = options.dhparam;
721723
if (options.sessionTimeout) this.sessionTimeout = options.sessionTimeout;
722724
if (options.ticketKeys) this.ticketKeys = options.ticketKeys;
723725
var secureOptions = options.secureOptions || 0;

src/node_crypto.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ void SecureContext::Initialize(Environment* env, Handle<Object> target) {
270270
NODE_SET_PROTOTYPE_METHOD(t, "addRootCerts", SecureContext::AddRootCerts);
271271
NODE_SET_PROTOTYPE_METHOD(t, "setCiphers", SecureContext::SetCiphers);
272272
NODE_SET_PROTOTYPE_METHOD(t, "setECDHCurve", SecureContext::SetECDHCurve);
273+
NODE_SET_PROTOTYPE_METHOD(t, "setDHParam", SecureContext::SetDHParam);
273274
NODE_SET_PROTOTYPE_METHOD(t, "setOptions", SecureContext::SetOptions);
274275
NODE_SET_PROTOTYPE_METHOD(t, "setSessionIdContext",
275276
SecureContext::SetSessionIdContext);
@@ -746,6 +747,37 @@ void SecureContext::SetECDHCurve(const FunctionCallbackInfo<Value>& args) {
746747
}
747748

748749

750+
void SecureContext::SetDHParam(const FunctionCallbackInfo<Value>& args) {
751+
HandleScope scope(args.GetIsolate());
752+
753+
SecureContext* sc = Unwrap<SecureContext>(args.This());
754+
Environment* env = sc->env();
755+
756+
// Auto DH is not supported in openssl 1.0.1, so dhparam needs
757+
// to be specifed explicitly
758+
if (args.Length() != 1)
759+
return env->ThrowTypeError("Bad parameter");
760+
761+
// Invalid dhparam is silently discarded and DHE is no longer used.
762+
BIO* bio = LoadBIO(env, args[0]);
763+
if (!bio)
764+
return;
765+
766+
DH* dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
767+
BIO_free_all(bio);
768+
769+
if (dh == NULL)
770+
return;
771+
772+
SSL_CTX_set_options(sc->ctx_, SSL_OP_SINGLE_DH_USE);
773+
int r = SSL_CTX_set_tmp_dh(sc->ctx_, dh);
774+
DH_free(dh);
775+
776+
if (!r)
777+
return env->ThrowTypeError("Error setting temp DH parameter");
778+
}
779+
780+
749781
void SecureContext::SetOptions(const FunctionCallbackInfo<Value>& args) {
750782
HandleScope scope(args.GetIsolate());
751783

src/node_crypto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class SecureContext : public BaseObject {
9595
static void AddRootCerts(const v8::FunctionCallbackInfo<v8::Value>& args);
9696
static void SetCiphers(const v8::FunctionCallbackInfo<v8::Value>& args);
9797
static void SetECDHCurve(const v8::FunctionCallbackInfo<v8::Value>& args);
98+
static void SetDHParam(const v8::FunctionCallbackInfo<v8::Value>& args);
9899
static void SetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
99100
static void SetSessionIdContext(
100101
const v8::FunctionCallbackInfo<v8::Value>& args);

test/fixtures/dherror.pem

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN DH PARAMETERS-----
2+
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3+
-----END DH PARAMETERS-----

test/fixtures/keys/Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
all: agent1-cert.pem agent2-cert.pem agent3-cert.pem agent4-cert.pem ca2-crl.pem ec-cert.pem
1+
all: agent1-cert.pem agent2-cert.pem agent3-cert.pem agent4-cert.pem ca2-crl.pem ec-cert.pem dh512.pem dh1024.pem dh2048.pem
22

33

44
#
@@ -145,6 +145,15 @@ ec-cert.pem: ec-csr.pem ec-key.pem
145145
-signkey ec-key.pem \
146146
-out ec-cert.pem
147147

148+
dh512.pem:
149+
openssl dhparam -out dh512.pem 512
150+
151+
dh1024.pem:
152+
openssl dhparam -out dh1024.pem 1024
153+
154+
dh2048.pem:
155+
openssl dhparam -out dh2048.pem 2048
156+
148157
clean:
149158
rm -f *.pem *.srl ca2-database.txt ca2-serial
150159

test/fixtures/keys/dh1024.pem

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN DH PARAMETERS-----
2+
MIGHAoGBAO2Ij3VgqTKsxLZiK0uW0xWVszBzNbdjgLkriicMcZuUj1fQOSM6CPwv
3+
5kdrRV8kHCK9q8dU1Dpf2dgh3l4fFFLpjIuUmUx3b7Q+GfHZ1UepNxr1NHSJaCl+
4+
wA0gwSDYhy8xRAsZ3bFVsLfDCuxuzPNC0yjtS5CVqci//vq0NTM7AgEC
5+
-----END DH PARAMETERS-----

test/fixtures/keys/dh2048.pem

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-----BEGIN DH PARAMETERS-----
2+
MIIBCAKCAQEAg3ytm4B8bqS4KmLTw7eeqrzp8Y4Ew65weXKL9eY2FmudR0VUkoti
3+
fs7/fKDsxMVgLyL+9UpbTs18CNslwWgMCSkrXe/NtXWlQQBLgXhEHOaeK/6j9zyt
4+
S6rlSvGK68NF0/e7o6jZXOSktIeflJQXoUyeds65+la/l5O2iuX0tgnGfNjB2Pdt
5+
RnoAPNJW+SBvyfcmiWjfd5Lh67SBgckqFMiH+CPiw54U2CeDPB343DUEPpTcnLJB
6+
aJs4uuxDnskz/0ZVidNBpUBs1wPQ8ruVNZx3hG2+PIqNPvOfYUPXIgn1ABj3mGAR
7+
sgtN63KUBX322zkTVPJnt30mrWp/F62GSwIBAg==
8+
-----END DH PARAMETERS-----

test/fixtures/keys/dh512.pem

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-----BEGIN DH PARAMETERS-----
2+
MEYCQQDpl3okBAjG92NSOaQEsIyqzvJRN06yHuGXunxYVIqxg7TnU8DBZW0ZYyiJ
3+
rJLRA/9b9dCk5DXpq1pFGoAkYLoDAgEC
4+
-----END DH PARAMETERS-----

0 commit comments

Comments
 (0)