Skip to content

Commit

Permalink
startTLS implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sidorares committed Feb 5, 2014
1 parent c42c7c7 commit 4b59612
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,17 @@ Connection.prototype.connect = function(cb) {
: Net.createConnection(this.config);

// Node v0.10+ Switch socket into "old mode" (Streams2)
this._socket.on("data",function() {});

this._socket.pipe(this._protocol);
this._protocol.pipe(this._socket);
//this._socket.on("data",function() {});

//this._socket.pipe(this._protocol);
//this._protocol.pipe(this._socket);
var connection = this;
this._protocol.on('data', function(data) {
connection._socket.write(data);
});
this._socket.on('data', function(data) {
connection._protocol.write(data);
});

this._socket.on('error', this._handleNetworkError.bind(this));
this._socket.on('connect', this._handleProtocolConnect.bind(this));
Expand Down Expand Up @@ -200,6 +207,49 @@ Connection.prototype.format = function(sql, values) {
return SqlString.format(sql, values, this.config.stringifyObjects, this.config.timezone);
};


Connection.prototype._startTLS = function(onSecure) {

var crypto = require('crypto');
var tls = require('tls');
var sslProfiles, sslProfileName;
if (typeof this.config.ssl == 'string') {
sslProfileName = this.config.ssl;
sslProfiles = require('../fixtures/ssl-profiles.json');
this.config.ssl = sslProfiles[this.config.ssl];
if (!this.config.ssl)
throw new Error('Unknown SSL profile for ' + sslProfileName);
}

// before TLS:
// _socket <-> _protocol
// after:
// _socket <-> securePair.encrypted <-> securePair.cleartext <-> _protocol

var credentials = crypto.createCredentials({
key: this.config.ssl.key,
cert: this.config.ssl.cert,
passphrase: this.config.ssl.passphrase,
ca: this.config.ssl.ca
});

var securePair = tls.createSecurePair(credentials, false);

securePair.encrypted.pipe(this._socket);
securePair.cleartext.pipe(this._protocol);

This comment has been minimized.

Copy link
@mscdex

mscdex Feb 21, 2014

Contributor

createSecurePair is deprecated in v0.11.x.

You're probably better off just doing something like this for v0.10+ instead:

var wrappedSock = tls.connect({
  key: this.config.ssl.key,
  cert: this.config.ssl.cert,
  passphrase: this.config.ssl.passphrase,
  ca: this.config.ssl.ca,
  socket: this._socket
}, onSecure);

wrappedSock.pipe(this._protocol);

// write to wrappedSock and not this._socket from here on out ...

This comment has been minimized.

Copy link
@sidorares

sidorares Feb 21, 2014

Author Member

Thanks, I'll try this! (also in node-mysql2 as the code is the same)

This comment has been minimized.

Copy link
@sidorares

sidorares Feb 21, 2014

Author Member

created issue for this - #737


// TODO: change to unpipe/pipe (does not work for some reason. Streams1/2 conflict?)
this._socket.removeAllListeners('data');
this._protocol.removeAllListeners('data');
this._socket.on('data', function(data) {
securePair.encrypted.write(data);
});
this._protocol.on('data', function(data) {
securePair.cleartext.write(data);
});
securePair.on('secure', onSecure);
};

Connection.prototype._handleConnectTimeout = function() {
if (this._socket) {
this._socket.setTimeout(0);
Expand Down

0 comments on commit 4b59612

Please sign in to comment.