Skip to content

Commit 21724ec

Browse files
indutnyry
authored andcommitted
Share SSL context between server connections
Fixes nodejs#1073.
1 parent 6461af1 commit 21724ec

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

lib/crypto.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ try {
3636
}
3737

3838

39-
function Credentials(secureProtocol) {
39+
function Credentials(secureProtocol, context) {
4040
if (!(this instanceof Credentials)) {
4141
return new Credentials(secureProtocol);
4242
}
@@ -45,22 +45,28 @@ function Credentials(secureProtocol) {
4545
throw new Error('node.js not compiled with openssl crypto support.');
4646
}
4747

48-
this.context = new SecureContext();
49-
50-
if (secureProtocol) {
51-
this.context.init(secureProtocol);
48+
if (context) {
49+
this.context = context;
50+
this.reuseContext = true;
5251
} else {
53-
this.context.init();
54-
}
52+
this.context = new SecureContext();
5553

54+
if (secureProtocol) {
55+
this.context.init(secureProtocol);
56+
} else {
57+
this.context.init();
58+
}
59+
}
5660
}
5761

5862
exports.Credentials = Credentials;
5963

6064

61-
exports.createCredentials = function(options) {
65+
exports.createCredentials = function(options, context) {
6266
if (!options) options = {};
63-
var c = new Credentials(options.secureProtocol);
67+
var c = new Credentials(options.secureProtocol, context);
68+
69+
if (context) return c;
6470

6571
if (options.key) c.context.setKey(options.key);
6672

lib/tls.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -713,16 +713,23 @@ function Server(/* [options], listener */) {
713713

714714
var self = this;
715715

716+
// Handle option defaults:
717+
this.setOptions(options);
718+
719+
var sharedCreds = crypto.createCredentials({
720+
key: self.key,
721+
cert: self.cert,
722+
ca: self.ca,
723+
ciphers: self.ciphers,
724+
secureProtocol: self.secureProtocol,
725+
crl: self.crl
726+
});
727+
728+
sharedCreds.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
729+
716730
// constructor call
717731
net.Server.call(this, function(socket) {
718-
var creds = crypto.createCredentials({
719-
key: self.key,
720-
cert: self.cert,
721-
ca: self.ca,
722-
secureProtocol: self.secureProtocol,
723-
crl: self.crl
724-
});
725-
creds.context.setCiphers('RC4-SHA:AES128-SHA:AES256-SHA');
732+
var creds = crypto.createCredentials(null, sharedCreds.context);
726733

727734
var pair = new SecurePair(creds,
728735
true,

0 commit comments

Comments
 (0)