2013-08-27 02:14:42 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
2014-03-02 19:54:19 +00:00
|
|
|
// // Emit `beforeExit` if the loop became alive either after emitting
|
|
|
|
// event, or after running some callbacks.
|
|
|
|
//
|
2013-08-27 02:14:42 +00:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
var constants = require('constants');
|
|
|
|
var crypto = require('crypto');
|
|
|
|
var net = require('net');
|
|
|
|
var tls = require('tls');
|
|
|
|
var util = require('util');
|
|
|
|
|
|
|
|
var Timer = process.binding('timer_wrap').Timer;
|
|
|
|
var tls_wrap = process.binding('tls_wrap');
|
|
|
|
|
2013-09-24 12:53:49 +00:00
|
|
|
// Lazy load
|
|
|
|
var tls_legacy;
|
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
var debug = util.debuglog('tls');
|
|
|
|
|
|
|
|
function onhandshakestart() {
|
|
|
|
debug('onhandshakestart');
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var ssl = self.ssl;
|
|
|
|
var now = Timer.now();
|
|
|
|
|
|
|
|
assert(now >= ssl.lastHandshakeTime);
|
|
|
|
|
|
|
|
if ((now - ssl.lastHandshakeTime) >= tls.CLIENT_RENEG_WINDOW * 1000) {
|
|
|
|
ssl.handshakes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var first = (ssl.lastHandshakeTime === 0);
|
|
|
|
ssl.lastHandshakeTime = now;
|
|
|
|
if (first) return;
|
|
|
|
|
|
|
|
if (++ssl.handshakes > tls.CLIENT_RENEG_LIMIT) {
|
|
|
|
// Defer the error event to the next tick. We're being called from OpenSSL's
|
|
|
|
// state machine and OpenSSL is not re-entrant. We cannot allow the user's
|
|
|
|
// callback to destroy the connection right now, it would crash and burn.
|
|
|
|
setImmediate(function() {
|
|
|
|
var err = new Error('TLS session renegotiation attack detected.');
|
|
|
|
self._tlsError(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onhandshakedone() {
|
|
|
|
// for future use
|
|
|
|
debug('onhandshakedone');
|
|
|
|
this._finishInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-17 10:11:13 +00:00
|
|
|
function onclienthello(hello) {
|
|
|
|
var self = this,
|
2013-08-03 17:29:54 +00:00
|
|
|
onceSession = false,
|
|
|
|
onceSNI = false;
|
2013-06-17 10:11:13 +00:00
|
|
|
|
|
|
|
function callback(err, session) {
|
2013-08-03 17:29:54 +00:00
|
|
|
if (onceSession)
|
|
|
|
return self.destroy(new Error('TLS session callback was called 2 times'));
|
|
|
|
onceSession = true;
|
2013-06-17 10:11:13 +00:00
|
|
|
|
|
|
|
if (err)
|
|
|
|
return self.destroy(err);
|
|
|
|
|
2013-08-03 17:29:54 +00:00
|
|
|
// NOTE: That we have disabled OpenSSL's internal session storage in
|
|
|
|
// `node_crypto.cc` and hence its safe to rely on getting servername only
|
|
|
|
// from clienthello or this place.
|
|
|
|
var ret = self.ssl.loadSession(session);
|
|
|
|
|
|
|
|
// Servername came from SSL session
|
|
|
|
// NOTE: TLS Session ticket doesn't include servername information
|
|
|
|
//
|
|
|
|
// Another note, From RFC3546:
|
|
|
|
//
|
|
|
|
// If, on the other hand, the older
|
|
|
|
// session is resumed, then the server MUST ignore extensions appearing
|
|
|
|
// in the client hello, and send a server hello containing no
|
|
|
|
// extensions; in this case the extension functionality negotiated
|
|
|
|
// during the original session initiation is applied to the resumed
|
|
|
|
// session.
|
|
|
|
//
|
|
|
|
// Therefore we should account session loading when dealing with servername
|
|
|
|
if (ret && ret.servername) {
|
|
|
|
self._SNICallback(ret.servername, onSNIResult);
|
|
|
|
} else if (hello.servername && self._SNICallback) {
|
|
|
|
self._SNICallback(hello.servername, onSNIResult);
|
|
|
|
} else {
|
|
|
|
self.ssl.endParser();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onSNIResult(err, context) {
|
|
|
|
if (onceSNI)
|
|
|
|
return self.destroy(new Error('TLS SNI callback was called 2 times'));
|
|
|
|
onceSNI = true;
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return self.destroy(err);
|
|
|
|
|
|
|
|
if (context)
|
|
|
|
self.ssl.sni_context = context;
|
|
|
|
|
|
|
|
self.ssl.endParser();
|
2013-06-17 10:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hello.sessionId.length <= 0 ||
|
2013-07-21 16:16:26 +00:00
|
|
|
hello.tlsTicket ||
|
2013-07-03 07:46:01 +00:00
|
|
|
this.server &&
|
2013-06-17 10:11:13 +00:00
|
|
|
!this.server.emit('resumeSession', hello.sessionId, callback)) {
|
2013-08-03 17:29:54 +00:00
|
|
|
// Invoke SNI callback, since we've no session to resume
|
|
|
|
if (hello.servername && this._SNICallback)
|
|
|
|
this._SNICallback(hello.servername, onSNIResult);
|
|
|
|
else
|
|
|
|
this.ssl.endParser();
|
2013-06-17 10:11:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onnewsession(key, session) {
|
2014-02-14 13:01:34 +00:00
|
|
|
if (!this.server)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var once = false;
|
|
|
|
|
|
|
|
this._newSessionPending = true;
|
|
|
|
this.server.emit('newSession', key, session, function() {
|
|
|
|
if (once)
|
|
|
|
return;
|
|
|
|
once = true;
|
|
|
|
|
|
|
|
self.ssl.newSessionDone();
|
|
|
|
|
|
|
|
self._newSessionPending = false;
|
|
|
|
if (self._securePending)
|
|
|
|
self._finishInit();
|
|
|
|
self._securePending = false;
|
|
|
|
});
|
2013-06-17 10:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
/**
|
|
|
|
* Provides a wrap of socket stream to do encrypted communication.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function TLSSocket(socket, options) {
|
2013-09-24 12:53:49 +00:00
|
|
|
// Disallow wrapping TLSSocket in TLSSocket
|
|
|
|
assert(!(socket instanceof TLSSocket));
|
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
net.Socket.call(this, socket && {
|
|
|
|
handle: socket._handle,
|
|
|
|
allowHalfOpen: socket.allowHalfOpen,
|
|
|
|
readable: socket.readable,
|
|
|
|
writable: socket.writable
|
|
|
|
});
|
|
|
|
|
2013-11-05 14:14:28 +00:00
|
|
|
// To prevent assertion in afterConnect()
|
|
|
|
if (socket)
|
|
|
|
this._connecting = socket._connecting;
|
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
this._tlsOptions = options;
|
|
|
|
this._secureEstablished = false;
|
2014-02-14 13:01:34 +00:00
|
|
|
this._securePending = false;
|
|
|
|
this._newSessionPending = false;
|
2013-06-13 13:36:00 +00:00
|
|
|
this._controlReleased = false;
|
2013-08-03 17:29:54 +00:00
|
|
|
this._SNICallback = null;
|
2013-06-13 13:36:00 +00:00
|
|
|
this.ssl = null;
|
|
|
|
this.servername = null;
|
|
|
|
this.npnProtocol = null;
|
|
|
|
this.authorized = false;
|
|
|
|
this.authorizationError = null;
|
|
|
|
|
2013-12-19 09:04:34 +00:00
|
|
|
// Just a documented property to make secure sockets
|
|
|
|
// distinguishable from regular ones.
|
|
|
|
this.encrypted = true;
|
|
|
|
|
2013-08-07 12:50:36 +00:00
|
|
|
this.on('error', this._tlsError);
|
|
|
|
|
2014-01-23 12:55:28 +00:00
|
|
|
if (!this._handle) {
|
|
|
|
this.once('connect', function() {
|
|
|
|
this._init(null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._init(socket);
|
|
|
|
}
|
2013-06-13 13:36:00 +00:00
|
|
|
}
|
|
|
|
util.inherits(TLSSocket, net.Socket);
|
2013-07-03 07:46:01 +00:00
|
|
|
exports.TLSSocket = TLSSocket;
|
2013-06-13 13:36:00 +00:00
|
|
|
|
2014-01-23 12:55:28 +00:00
|
|
|
TLSSocket.prototype._init = function(socket) {
|
2013-06-13 13:36:00 +00:00
|
|
|
assert(this._handle);
|
|
|
|
|
|
|
|
// lib/net.js expect this value to be non-zero if write hasn't been flushed
|
|
|
|
// immediately
|
|
|
|
// TODO(indutny): rewise this solution, it might be 1 before handshake and
|
2013-08-12 22:01:05 +00:00
|
|
|
// represent real writeQueueSize during regular writes.
|
2013-06-13 13:36:00 +00:00
|
|
|
this._handle.writeQueueSize = 1;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var options = this._tlsOptions;
|
|
|
|
|
|
|
|
// Wrap socket's handle
|
|
|
|
var credentials = options.credentials || crypto.createCredentials();
|
|
|
|
this.ssl = tls_wrap.wrap(this._handle, credentials.context, options.isServer);
|
2013-06-17 10:11:13 +00:00
|
|
|
this.server = options.server || null;
|
2013-06-13 13:36:00 +00:00
|
|
|
|
|
|
|
// For clients, we will always have either a given ca list or be using
|
|
|
|
// default one
|
|
|
|
var requestCert = !!options.requestCert || !options.isServer,
|
|
|
|
rejectUnauthorized = !!options.rejectUnauthorized;
|
|
|
|
|
2013-08-23 13:53:16 +00:00
|
|
|
this._requestCert = requestCert;
|
|
|
|
this._rejectUnauthorized = rejectUnauthorized;
|
2013-06-13 13:36:00 +00:00
|
|
|
if (requestCert || rejectUnauthorized)
|
|
|
|
this.ssl.setVerifyMode(requestCert, rejectUnauthorized);
|
|
|
|
|
|
|
|
if (options.isServer) {
|
|
|
|
this.ssl.onhandshakestart = onhandshakestart.bind(this);
|
|
|
|
this.ssl.onhandshakedone = onhandshakedone.bind(this);
|
2013-06-17 10:11:13 +00:00
|
|
|
this.ssl.onclienthello = onclienthello.bind(this);
|
|
|
|
this.ssl.onnewsession = onnewsession.bind(this);
|
2013-06-13 13:36:00 +00:00
|
|
|
this.ssl.lastHandshakeTime = 0;
|
|
|
|
this.ssl.handshakes = 0;
|
2013-06-17 10:11:13 +00:00
|
|
|
|
2013-07-03 07:46:01 +00:00
|
|
|
if (this.server &&
|
|
|
|
(this.server.listeners('resumeSession').length > 0 ||
|
|
|
|
this.server.listeners('newSession').length > 0)) {
|
2013-06-17 10:11:13 +00:00
|
|
|
this.ssl.enableSessionCallbacks();
|
|
|
|
}
|
2013-06-13 13:36:00 +00:00
|
|
|
} else {
|
|
|
|
this.ssl.onhandshakestart = function() {};
|
|
|
|
this.ssl.onhandshakedone = this._finishInit.bind(this);
|
2014-02-03 21:32:13 +00:00
|
|
|
|
|
|
|
if (options.session)
|
|
|
|
this.ssl.setSession(options.session);
|
2013-06-13 13:36:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.ssl.onerror = function(err) {
|
2014-02-09 11:09:34 +00:00
|
|
|
if (self._writableState.errorEmitted)
|
2014-01-22 01:07:11 +00:00
|
|
|
return;
|
2014-02-09 11:09:34 +00:00
|
|
|
self._writableState.errorEmitted = true;
|
2014-01-22 01:07:11 +00:00
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
// Destroy socket if error happened before handshake's finish
|
|
|
|
if (!this._secureEstablished) {
|
|
|
|
self._tlsError(err);
|
|
|
|
self.destroy();
|
|
|
|
} else if (options.isServer &&
|
|
|
|
rejectUnauthorized &&
|
|
|
|
/peer did not return a certificate/.test(err.message)) {
|
|
|
|
// Ignore server's authorization errors
|
|
|
|
self.destroy();
|
|
|
|
} else {
|
|
|
|
// Throw error
|
|
|
|
self._tlsError(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-02 16:11:17 +00:00
|
|
|
// If custom SNICallback was given, or if
|
|
|
|
// there're SNI contexts to perform match against -
|
|
|
|
// set `.onsniselect` callback.
|
2013-06-13 13:36:00 +00:00
|
|
|
if (process.features.tls_sni &&
|
|
|
|
options.isServer &&
|
2013-07-03 07:46:01 +00:00
|
|
|
options.server &&
|
2013-08-02 16:11:17 +00:00
|
|
|
(options.SNICallback !== SNICallback ||
|
|
|
|
options.server._contexts.length)) {
|
|
|
|
assert(typeof options.SNICallback === 'function');
|
2013-08-03 17:29:54 +00:00
|
|
|
this._SNICallback = options.SNICallback;
|
|
|
|
this.ssl.enableHelloParser();
|
2013-06-13 13:36:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (process.features.tls_npn && options.NPNProtocols)
|
|
|
|
this.ssl.setNPNProtocols(options.NPNProtocols);
|
2013-08-23 13:53:16 +00:00
|
|
|
|
|
|
|
if (options.handshakeTimeout > 0)
|
|
|
|
this.setTimeout(options.handshakeTimeout, this._handleTimeout);
|
2014-01-23 12:55:28 +00:00
|
|
|
|
|
|
|
// Socket already has some buffered data - emulate receiving it
|
|
|
|
if (socket && socket._readableState.length) {
|
|
|
|
var buf;
|
|
|
|
while ((buf = socket.read()) !== null)
|
|
|
|
this.ssl.receive(buf);
|
|
|
|
}
|
2013-08-23 13:53:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TLSSocket.prototype.renegotiate = function(options, callback) {
|
|
|
|
var requestCert = this._requestCert,
|
|
|
|
rejectUnauthorized = this._rejectUnauthorized;
|
|
|
|
|
|
|
|
if (typeof options.requestCert !== 'undefined')
|
|
|
|
requestCert = !!options.requestCert;
|
|
|
|
if (typeof options.rejectUnauthorized !== 'undefined')
|
|
|
|
rejectUnauthorized = !!options.rejectUnauthorized;
|
|
|
|
|
|
|
|
if (requestCert !== this._requestCert ||
|
|
|
|
rejectUnauthorized !== this._rejectUnauthorized) {
|
|
|
|
this.ssl.setVerifyMode(requestCert, rejectUnauthorized);
|
|
|
|
this._requestCert = requestCert;
|
|
|
|
this._rejectUnauthorized = rejectUnauthorized;
|
|
|
|
}
|
|
|
|
if (!this.ssl.renegotiate()) {
|
|
|
|
if (callback) {
|
|
|
|
process.nextTick(function() {
|
|
|
|
callback(new Error('Failed to renegotiate'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that we'll cycle through internal openssl's state
|
|
|
|
this.write('');
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
this.once('secure', function() {
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2014-01-17 18:46:49 +00:00
|
|
|
TLSSocket.prototype.setMaxSendFragment = function setMaxSendFragment(size) {
|
|
|
|
return this.ssl.setMaxSendFragment(size) == 1;
|
|
|
|
};
|
|
|
|
|
2014-02-03 21:32:13 +00:00
|
|
|
TLSSocket.prototype.getTLSTicket = function getTLSTicket() {
|
|
|
|
return this.ssl.getTLSTicket();
|
|
|
|
};
|
|
|
|
|
2013-08-23 13:53:16 +00:00
|
|
|
TLSSocket.prototype._handleTimeout = function() {
|
|
|
|
this._tlsError(new Error('TLS handshake timeout'));
|
2013-06-13 13:36:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TLSSocket.prototype._tlsError = function(err) {
|
|
|
|
this.emit('_tlsError', err);
|
|
|
|
if (this._controlReleased)
|
|
|
|
this.emit('error', err);
|
|
|
|
};
|
|
|
|
|
2013-08-07 12:50:36 +00:00
|
|
|
TLSSocket.prototype._releaseControl = function() {
|
|
|
|
if (this._controlReleased)
|
2013-08-23 13:53:16 +00:00
|
|
|
return false;
|
2013-08-07 12:50:36 +00:00
|
|
|
this._controlReleased = true;
|
|
|
|
this.removeListener('error', this._tlsError);
|
2013-08-23 13:53:16 +00:00
|
|
|
return true;
|
2013-08-07 12:50:36 +00:00
|
|
|
};
|
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
TLSSocket.prototype._finishInit = function() {
|
2014-02-14 13:01:34 +00:00
|
|
|
// `newSession` callback wasn't called yet
|
|
|
|
if (this._newSessionPending) {
|
|
|
|
this._securePending = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
if (process.features.tls_npn) {
|
|
|
|
this.npnProtocol = this.ssl.getNegotiatedProtocol();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.features.tls_sni && this._tlsOptions.isServer) {
|
|
|
|
this.servername = this.ssl.getServername();
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('secure established');
|
|
|
|
this._secureEstablished = true;
|
2013-08-23 13:53:16 +00:00
|
|
|
if (this._tlsOptions.handshakeTimeout > 0)
|
|
|
|
this.setTimeout(0, this._handleTimeout);
|
2013-06-13 13:36:00 +00:00
|
|
|
this.emit('secure');
|
|
|
|
};
|
|
|
|
|
|
|
|
TLSSocket.prototype._start = function() {
|
|
|
|
this.ssl.start();
|
|
|
|
};
|
|
|
|
|
|
|
|
TLSSocket.prototype.setServername = function(name) {
|
|
|
|
this.ssl.setServername(name);
|
|
|
|
};
|
|
|
|
|
|
|
|
TLSSocket.prototype.setSession = function(session) {
|
2013-07-26 21:38:08 +00:00
|
|
|
if (util.isString(session))
|
2013-06-13 13:36:00 +00:00
|
|
|
session = new Buffer(session, 'binary');
|
|
|
|
this.ssl.setSession(session);
|
|
|
|
};
|
|
|
|
|
|
|
|
TLSSocket.prototype.getPeerCertificate = function() {
|
|
|
|
if (this.ssl) {
|
|
|
|
var c = this.ssl.getPeerCertificate();
|
|
|
|
|
|
|
|
if (c) {
|
|
|
|
if (c.issuer) c.issuer = tls.parseCertString(c.issuer);
|
|
|
|
if (c.subject) c.subject = tls.parseCertString(c.subject);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
TLSSocket.prototype.getSession = function() {
|
|
|
|
if (this.ssl) {
|
|
|
|
return this.ssl.getSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
TLSSocket.prototype.isSessionReused = function() {
|
|
|
|
if (this.ssl) {
|
|
|
|
return this.ssl.isSessionReused();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
TLSSocket.prototype.getCipher = function(err) {
|
|
|
|
if (this.ssl) {
|
|
|
|
return this.ssl.getCurrentCipher();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: support anonymous (nocert) and PSK
|
|
|
|
|
|
|
|
|
|
|
|
// AUTHENTICATION MODES
|
|
|
|
//
|
|
|
|
// There are several levels of authentication that TLS/SSL supports.
|
|
|
|
// Read more about this in "man SSL_set_verify".
|
|
|
|
//
|
|
|
|
// 1. The server sends a certificate to the client but does not request a
|
|
|
|
// cert from the client. This is common for most HTTPS servers. The browser
|
|
|
|
// can verify the identity of the server, but the server does not know who
|
|
|
|
// the client is. Authenticating the client is usually done over HTTP using
|
|
|
|
// login boxes and cookies and stuff.
|
|
|
|
//
|
|
|
|
// 2. The server sends a cert to the client and requests that the client
|
|
|
|
// also send it a cert. The client knows who the server is and the server is
|
|
|
|
// requesting the client also identify themselves. There are several
|
|
|
|
// outcomes:
|
|
|
|
//
|
|
|
|
// A) verifyError returns null meaning the client's certificate is signed
|
|
|
|
// by one of the server's CAs. The server know's the client idenity now
|
|
|
|
// and the client is authorized.
|
|
|
|
//
|
|
|
|
// B) For some reason the client's certificate is not acceptable -
|
|
|
|
// verifyError returns a string indicating the problem. The server can
|
|
|
|
// either (i) reject the client or (ii) allow the client to connect as an
|
|
|
|
// unauthorized connection.
|
|
|
|
//
|
|
|
|
// The mode is controlled by two boolean variables.
|
|
|
|
//
|
|
|
|
// requestCert
|
|
|
|
// If true the server requests a certificate from client connections. For
|
|
|
|
// the common HTTPS case, users will want this to be false, which is what
|
|
|
|
// it defaults to.
|
|
|
|
//
|
|
|
|
// rejectUnauthorized
|
|
|
|
// If true clients whose certificates are invalid for any reason will not
|
|
|
|
// be allowed to make connections. If false, they will simply be marked as
|
|
|
|
// unauthorized but secure communication will continue. By default this is
|
|
|
|
// true.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Options:
|
|
|
|
// - requestCert. Send verify request. Default to false.
|
|
|
|
// - rejectUnauthorized. Boolean, default to true.
|
|
|
|
// - key. string.
|
|
|
|
// - cert: string.
|
|
|
|
// - ca: string or array of strings.
|
|
|
|
// - sessionTimeout: integer.
|
|
|
|
//
|
|
|
|
// emit 'secureConnection'
|
|
|
|
// function (tlsSocket) { }
|
|
|
|
//
|
|
|
|
// "UNABLE_TO_GET_ISSUER_CERT", "UNABLE_TO_GET_CRL",
|
|
|
|
// "UNABLE_TO_DECRYPT_CERT_SIGNATURE", "UNABLE_TO_DECRYPT_CRL_SIGNATURE",
|
|
|
|
// "UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY", "CERT_SIGNATURE_FAILURE",
|
|
|
|
// "CRL_SIGNATURE_FAILURE", "CERT_NOT_YET_VALID" "CERT_HAS_EXPIRED",
|
|
|
|
// "CRL_NOT_YET_VALID", "CRL_HAS_EXPIRED" "ERROR_IN_CERT_NOT_BEFORE_FIELD",
|
|
|
|
// "ERROR_IN_CERT_NOT_AFTER_FIELD", "ERROR_IN_CRL_LAST_UPDATE_FIELD",
|
|
|
|
// "ERROR_IN_CRL_NEXT_UPDATE_FIELD", "OUT_OF_MEM",
|
|
|
|
// "DEPTH_ZERO_SELF_SIGNED_CERT", "SELF_SIGNED_CERT_IN_CHAIN",
|
|
|
|
// "UNABLE_TO_GET_ISSUER_CERT_LOCALLY", "UNABLE_TO_VERIFY_LEAF_SIGNATURE",
|
|
|
|
// "CERT_CHAIN_TOO_LONG", "CERT_REVOKED" "INVALID_CA",
|
|
|
|
// "PATH_LENGTH_EXCEEDED", "INVALID_PURPOSE" "CERT_UNTRUSTED",
|
|
|
|
// "CERT_REJECTED"
|
|
|
|
//
|
|
|
|
function Server(/* [options], listener */) {
|
|
|
|
var options, listener;
|
2013-07-26 21:38:08 +00:00
|
|
|
if (util.isObject(arguments[0])) {
|
2013-06-13 13:36:00 +00:00
|
|
|
options = arguments[0];
|
|
|
|
listener = arguments[1];
|
2013-07-26 21:38:08 +00:00
|
|
|
} else if (util.isFunction(arguments[0])) {
|
2013-06-13 13:36:00 +00:00
|
|
|
options = {};
|
|
|
|
listener = arguments[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(this instanceof Server)) return new Server(options, listener);
|
|
|
|
|
|
|
|
this._contexts = [];
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Handle option defaults:
|
|
|
|
this.setOptions(options);
|
|
|
|
|
|
|
|
var sharedCreds = crypto.createCredentials({
|
|
|
|
pfx: self.pfx,
|
|
|
|
key: self.key,
|
|
|
|
passphrase: self.passphrase,
|
|
|
|
cert: self.cert,
|
|
|
|
ca: self.ca,
|
|
|
|
ciphers: self.ciphers || tls.DEFAULT_CIPHERS,
|
2013-10-14 14:53:59 +00:00
|
|
|
ecdhCurve: util.isUndefined(self.ecdhCurve) ?
|
|
|
|
tls.DEFAULT_ECDH_CURVE : self.ecdhCurve,
|
2013-06-13 13:36:00 +00:00
|
|
|
secureProtocol: self.secureProtocol,
|
|
|
|
secureOptions: self.secureOptions,
|
|
|
|
crl: self.crl,
|
|
|
|
sessionIdContext: self.sessionIdContext
|
|
|
|
});
|
2013-07-21 03:11:02 +00:00
|
|
|
this._sharedCreds = sharedCreds;
|
2013-06-13 13:36:00 +00:00
|
|
|
|
|
|
|
var timeout = options.handshakeTimeout || (120 * 1000);
|
|
|
|
|
2013-07-26 21:38:08 +00:00
|
|
|
if (!util.isNumber(timeout)) {
|
2013-06-13 13:36:00 +00:00
|
|
|
throw new TypeError('handshakeTimeout must be a number');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self.sessionTimeout) {
|
|
|
|
sharedCreds.context.setSessionTimeout(self.sessionTimeout);
|
|
|
|
}
|
|
|
|
|
2013-07-21 03:11:02 +00:00
|
|
|
if (self.ticketKeys) {
|
|
|
|
sharedCreds.context.setTicketKeys(self.ticketKeys);
|
|
|
|
}
|
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
// constructor call
|
|
|
|
net.Server.call(this, function(raw_socket) {
|
|
|
|
var socket = new TLSSocket(raw_socket, {
|
|
|
|
credentials: sharedCreds,
|
|
|
|
isServer: true,
|
|
|
|
server: self,
|
|
|
|
requestCert: self.requestCert,
|
|
|
|
rejectUnauthorized: self.rejectUnauthorized,
|
2013-08-23 13:53:16 +00:00
|
|
|
handshakeTimeout: timeout,
|
2013-06-13 13:36:00 +00:00
|
|
|
NPNProtocols: self.NPNProtocols,
|
2013-08-06 16:30:21 +00:00
|
|
|
SNICallback: options.SNICallback || SNICallback
|
2013-06-13 13:36:00 +00:00
|
|
|
});
|
|
|
|
|
2013-08-23 13:53:16 +00:00
|
|
|
socket.on('secure', function() {
|
|
|
|
if (socket._requestCert) {
|
2013-06-13 13:36:00 +00:00
|
|
|
var verifyError = socket.ssl.verifyError();
|
|
|
|
if (verifyError) {
|
2014-01-17 17:15:36 +00:00
|
|
|
socket.authorizationError = verifyError.code;
|
2013-06-13 13:36:00 +00:00
|
|
|
|
2013-08-23 13:53:16 +00:00
|
|
|
if (socket._rejectUnauthorized)
|
2013-06-13 13:36:00 +00:00
|
|
|
socket.destroy();
|
|
|
|
} else {
|
|
|
|
socket.authorized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 13:53:16 +00:00
|
|
|
if (!socket.destroyed && socket._releaseControl())
|
2013-06-13 13:36:00 +00:00
|
|
|
self.emit('secureConnection', socket);
|
|
|
|
});
|
|
|
|
|
2014-01-22 01:07:11 +00:00
|
|
|
var errorEmitted = false;
|
|
|
|
socket.on('close', function() {
|
|
|
|
// Emit ECONNRESET
|
|
|
|
if (!socket._controlReleased && !errorEmitted) {
|
|
|
|
errorEmitted = true;
|
|
|
|
var connReset = new Error('socket hang up');
|
|
|
|
connReset.code = 'ECONNRESET';
|
|
|
|
self.emit('clientError', connReset, socket);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
socket.on('_tlsError', function(err) {
|
2014-01-22 01:07:11 +00:00
|
|
|
if (!socket._controlReleased && !errorEmitted) {
|
|
|
|
errorEmitted = true;
|
2013-06-13 13:36:00 +00:00
|
|
|
self.emit('clientError', err, socket);
|
2014-01-22 01:07:11 +00:00
|
|
|
}
|
2013-06-13 13:36:00 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
this.on('secureConnection', listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(Server, net.Server);
|
|
|
|
exports.Server = Server;
|
|
|
|
exports.createServer = function(options, listener) {
|
|
|
|
return new Server(options, listener);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-21 03:11:02 +00:00
|
|
|
Server.prototype._getServerData = function() {
|
|
|
|
return {
|
|
|
|
ticketKeys: this._sharedCreds.context.getTicketKeys().toString('hex')
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Server.prototype._setServerData = function(data) {
|
|
|
|
this._sharedCreds.context.setTicketKeys(new Buffer(data.ticketKeys, 'hex'));
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
Server.prototype.setOptions = function(options) {
|
2013-07-26 21:38:08 +00:00
|
|
|
if (util.isBoolean(options.requestCert)) {
|
2013-06-13 13:36:00 +00:00
|
|
|
this.requestCert = options.requestCert;
|
|
|
|
} else {
|
|
|
|
this.requestCert = false;
|
|
|
|
}
|
|
|
|
|
2013-07-26 21:38:08 +00:00
|
|
|
if (util.isBoolean(options.rejectUnauthorized)) {
|
2013-06-13 13:36:00 +00:00
|
|
|
this.rejectUnauthorized = options.rejectUnauthorized;
|
|
|
|
} else {
|
|
|
|
this.rejectUnauthorized = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.pfx) this.pfx = options.pfx;
|
|
|
|
if (options.key) this.key = options.key;
|
|
|
|
if (options.passphrase) this.passphrase = options.passphrase;
|
|
|
|
if (options.cert) this.cert = options.cert;
|
|
|
|
if (options.ca) this.ca = options.ca;
|
|
|
|
if (options.secureProtocol) this.secureProtocol = options.secureProtocol;
|
|
|
|
if (options.crl) this.crl = options.crl;
|
|
|
|
if (options.ciphers) this.ciphers = options.ciphers;
|
2013-10-14 14:53:59 +00:00
|
|
|
if (!util.isUndefined(options.ecdhCurve))
|
|
|
|
this.ecdhCurve = options.ecdhCurve;
|
2013-06-13 13:36:00 +00:00
|
|
|
if (options.sessionTimeout) this.sessionTimeout = options.sessionTimeout;
|
2014-02-03 21:32:13 +00:00
|
|
|
if (options.ticketKeys) this.ticketKeys = options.ticketKeys;
|
2013-06-13 13:36:00 +00:00
|
|
|
var secureOptions = options.secureOptions || 0;
|
|
|
|
if (options.honorCipherOrder) {
|
|
|
|
secureOptions |= constants.SSL_OP_CIPHER_SERVER_PREFERENCE;
|
|
|
|
}
|
|
|
|
if (secureOptions) this.secureOptions = secureOptions;
|
|
|
|
if (options.NPNProtocols) tls.convertNPNProtocols(options.NPNProtocols, this);
|
|
|
|
if (options.sessionIdContext) {
|
|
|
|
this.sessionIdContext = options.sessionIdContext;
|
2013-08-23 13:53:16 +00:00
|
|
|
} else {
|
2013-06-13 13:36:00 +00:00
|
|
|
this.sessionIdContext = crypto.createHash('md5')
|
|
|
|
.update(process.argv.join(' '))
|
|
|
|
.digest('hex');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// SNI Contexts High-Level API
|
|
|
|
Server.prototype.addContext = function(servername, credentials) {
|
|
|
|
if (!servername) {
|
|
|
|
throw 'Servername is required parameter for Server.addContext';
|
|
|
|
}
|
|
|
|
|
|
|
|
var re = new RegExp('^' +
|
|
|
|
servername.replace(/([\.^$+?\-\\[\]{}])/g, '\\$1')
|
2013-12-05 15:16:01 +00:00
|
|
|
.replace(/\*/g, '[^\.]*') +
|
2013-06-13 13:36:00 +00:00
|
|
|
'$');
|
|
|
|
this._contexts.push([re, crypto.createCredentials(credentials).context]);
|
|
|
|
};
|
|
|
|
|
2013-08-03 17:29:54 +00:00
|
|
|
function SNICallback(servername, callback) {
|
2013-06-13 13:36:00 +00:00
|
|
|
var ctx;
|
|
|
|
|
2013-08-06 16:30:21 +00:00
|
|
|
this.server._contexts.some(function(elem) {
|
2013-07-26 21:38:08 +00:00
|
|
|
if (!util.isNull(servername.match(elem[0]))) {
|
2013-06-13 13:36:00 +00:00
|
|
|
ctx = elem[1];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-08-03 17:29:54 +00:00
|
|
|
callback(null, ctx);
|
2013-08-02 16:11:17 +00:00
|
|
|
}
|
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
|
|
|
|
// Target API:
|
|
|
|
//
|
|
|
|
// var s = tls.connect({port: 8000, host: "google.com"}, function() {
|
|
|
|
// if (!s.authorized) {
|
|
|
|
// s.destroy();
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // s.socket;
|
|
|
|
//
|
|
|
|
// s.end("hello world\n");
|
|
|
|
// });
|
|
|
|
//
|
|
|
|
//
|
|
|
|
function normalizeConnectArgs(listArgs) {
|
|
|
|
var args = net._normalizeConnectArgs(listArgs);
|
|
|
|
var options = args[0];
|
|
|
|
var cb = args[1];
|
|
|
|
|
2013-07-26 21:38:08 +00:00
|
|
|
if (util.isObject(listArgs[1])) {
|
2013-06-13 13:36:00 +00:00
|
|
|
options = util._extend(options, listArgs[1]);
|
2013-07-26 21:38:08 +00:00
|
|
|
} else if (util.isObject(listArgs[2])) {
|
2013-06-13 13:36:00 +00:00
|
|
|
options = util._extend(options, listArgs[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (cb) ? [options, cb] : [options];
|
|
|
|
}
|
|
|
|
|
2013-09-24 12:53:49 +00:00
|
|
|
function legacyConnect(hostname, options, NPN, credentials) {
|
|
|
|
assert(options.socket);
|
|
|
|
if (!tls_legacy)
|
|
|
|
tls_legacy = require('_tls_legacy');
|
|
|
|
|
|
|
|
var pair = tls_legacy.createSecurePair(credentials,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
!!options.rejectUnauthorized,
|
|
|
|
{
|
|
|
|
NPNProtocols: NPN.NPNProtocols,
|
|
|
|
servername: hostname
|
|
|
|
});
|
|
|
|
tls_legacy.pipe(pair, options.socket);
|
|
|
|
pair.cleartext._controlReleased = true;
|
|
|
|
pair.on('error', function(err) {
|
|
|
|
pair.cleartext.emit('error', err);
|
|
|
|
});
|
|
|
|
|
|
|
|
return pair;
|
|
|
|
}
|
|
|
|
|
2013-06-13 13:36:00 +00:00
|
|
|
exports.connect = function(/* [port, host], options, cb */) {
|
|
|
|
var args = normalizeConnectArgs(arguments);
|
|
|
|
var options = args[0];
|
|
|
|
var cb = args[1];
|
|
|
|
|
|
|
|
var defaults = {
|
2014-01-24 01:28:08 +00:00
|
|
|
rejectUnauthorized: '0' !== process.env.NODE_TLS_REJECT_UNAUTHORIZED,
|
|
|
|
ciphers: tls.DEFAULT_CIPHERS
|
2013-06-13 13:36:00 +00:00
|
|
|
};
|
|
|
|
options = util._extend(defaults, options || {});
|
|
|
|
|
2013-10-28 10:25:27 +00:00
|
|
|
var hostname = options.servername ||
|
|
|
|
options.host ||
|
2013-10-28 12:10:10 +00:00
|
|
|
options.socket && options.socket._host,
|
2013-09-24 12:53:49 +00:00
|
|
|
NPN = {},
|
|
|
|
credentials = crypto.createCredentials(options);
|
2013-06-13 13:36:00 +00:00
|
|
|
tls.convertNPNProtocols(options.NPNProtocols, NPN);
|
|
|
|
|
2013-09-24 12:53:49 +00:00
|
|
|
// Wrapping TLS socket inside another TLS socket was requested -
|
|
|
|
// create legacy secure pair
|
|
|
|
var socket;
|
|
|
|
var legacy;
|
|
|
|
var result;
|
|
|
|
if (options.socket instanceof TLSSocket) {
|
|
|
|
debug('legacy connect');
|
|
|
|
legacy = true;
|
|
|
|
socket = legacyConnect(hostname, options, NPN, credentials);
|
|
|
|
result = socket.cleartext;
|
|
|
|
} else {
|
|
|
|
legacy = false;
|
|
|
|
socket = new TLSSocket(options.socket, {
|
|
|
|
credentials: credentials,
|
|
|
|
isServer: false,
|
|
|
|
requestCert: true,
|
|
|
|
rejectUnauthorized: options.rejectUnauthorized,
|
2014-02-03 21:32:13 +00:00
|
|
|
session: options.session,
|
2013-09-24 12:53:49 +00:00
|
|
|
NPNProtocols: NPN.NPNProtocols
|
|
|
|
});
|
|
|
|
result = socket;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:14:28 +00:00
|
|
|
if (socket._handle && !socket._connecting) {
|
2013-09-24 12:53:49 +00:00
|
|
|
onHandle();
|
2013-11-05 14:14:28 +00:00
|
|
|
} else {
|
|
|
|
// Not even started connecting yet (or probably resolving dns address),
|
|
|
|
// catch socket errors and assign handle.
|
|
|
|
if (!legacy && options.socket) {
|
|
|
|
options.socket.once('connect', function() {
|
|
|
|
assert(options.socket._handle);
|
|
|
|
socket._handle = options.socket._handle;
|
|
|
|
socket._handle.owner = socket;
|
|
|
|
socket.emit('connect');
|
|
|
|
});
|
|
|
|
}
|
2013-09-24 12:53:49 +00:00
|
|
|
socket.once('connect', onHandle);
|
2013-11-05 14:14:28 +00:00
|
|
|
}
|
2013-09-24 12:53:49 +00:00
|
|
|
|
|
|
|
if (cb)
|
|
|
|
result.once('secureConnect', cb);
|
|
|
|
|
|
|
|
if (!options.socket) {
|
|
|
|
assert(!legacy);
|
|
|
|
var connect_opt;
|
|
|
|
if (options.path && !options.port) {
|
|
|
|
connect_opt = { path: options.path };
|
|
|
|
} else {
|
|
|
|
connect_opt = {
|
|
|
|
port: options.port,
|
|
|
|
host: options.host,
|
|
|
|
localAddress: options.localAddress
|
|
|
|
};
|
2013-09-24 21:35:30 +00:00
|
|
|
}
|
2013-09-24 12:53:49 +00:00
|
|
|
socket.connect(connect_opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2013-06-13 13:36:00 +00:00
|
|
|
|
|
|
|
function onHandle() {
|
2013-09-24 12:53:49 +00:00
|
|
|
if (!legacy)
|
|
|
|
socket._releaseControl();
|
2013-06-13 13:36:00 +00:00
|
|
|
|
|
|
|
if (options.session)
|
|
|
|
socket.setSession(options.session);
|
|
|
|
|
2013-09-24 12:53:49 +00:00
|
|
|
if (!legacy) {
|
|
|
|
if (options.servername)
|
|
|
|
socket.setServername(options.servername);
|
2013-06-13 13:36:00 +00:00
|
|
|
|
2013-09-24 12:53:49 +00:00
|
|
|
socket._start();
|
|
|
|
}
|
2013-06-13 13:36:00 +00:00
|
|
|
socket.on('secure', function() {
|
|
|
|
var verifyError = socket.ssl.verifyError();
|
|
|
|
|
|
|
|
// Verify that server's identity matches it's certificate's names
|
|
|
|
if (!verifyError) {
|
2013-09-24 12:53:49 +00:00
|
|
|
var cert = result.getPeerCertificate();
|
|
|
|
var validCert = tls.checkServerIdentity(hostname, cert);
|
2013-06-13 13:36:00 +00:00
|
|
|
if (!validCert) {
|
|
|
|
verifyError = new Error('Hostname/IP doesn\'t match certificate\'s ' +
|
|
|
|
'altnames');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verifyError) {
|
2013-09-24 12:53:49 +00:00
|
|
|
result.authorized = false;
|
2014-01-17 17:15:36 +00:00
|
|
|
result.authorizationError = verifyError.code || verifyError.message;
|
2013-06-13 13:36:00 +00:00
|
|
|
|
|
|
|
if (options.rejectUnauthorized) {
|
2013-09-24 12:53:49 +00:00
|
|
|
result.emit('error', verifyError);
|
|
|
|
result.destroy();
|
2013-06-13 13:36:00 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2013-09-24 12:53:49 +00:00
|
|
|
result.emit('secureConnect');
|
2013-06-13 13:36:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-09-24 12:53:49 +00:00
|
|
|
result.authorized = true;
|
|
|
|
result.emit('secureConnect');
|
2013-06-13 13:36:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Uncork incoming data
|
2013-09-24 12:53:49 +00:00
|
|
|
result.removeListener('end', onHangUp);
|
2013-06-13 13:36:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function onHangUp() {
|
2013-06-13 19:47:31 +00:00
|
|
|
// NOTE: This logic is shared with _http_client.js
|
|
|
|
if (!socket._hadError) {
|
|
|
|
socket._hadError = true;
|
|
|
|
var error = new Error('socket hang up');
|
|
|
|
error.code = 'ECONNRESET';
|
|
|
|
socket.destroy();
|
|
|
|
socket.emit('error', error);
|
|
|
|
}
|
2013-06-13 13:36:00 +00:00
|
|
|
}
|
2013-09-24 12:53:49 +00:00
|
|
|
result.once('end', onHangUp);
|
2013-06-13 13:36:00 +00:00
|
|
|
}
|
|
|
|
};
|