2011-03-10 08:54:52 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2010-12-01 21:00:04 +00:00
|
|
|
var crypto = require('crypto');
|
2010-12-06 02:19:18 +00:00
|
|
|
var util = require('util');
|
2010-12-01 21:00:04 +00:00
|
|
|
var net = require('net');
|
2012-07-11 19:54:20 +00:00
|
|
|
var url = require('url');
|
2010-12-01 21:00:04 +00:00
|
|
|
var events = require('events');
|
2013-01-31 17:20:49 +00:00
|
|
|
var stream = require('stream');
|
2011-01-28 00:59:28 +00:00
|
|
|
var assert = require('assert').ok;
|
2012-02-10 05:58:58 +00:00
|
|
|
var constants = require('constants');
|
2010-12-04 01:07:09 +00:00
|
|
|
|
2012-08-21 20:27:13 +00:00
|
|
|
var DEFAULT_CIPHERS = 'ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:' + // TLS 1.2
|
2012-08-22 16:46:07 +00:00
|
|
|
'RC4:HIGH:!MD5:!aNULL:!EDH'; // TLS 1.0
|
2012-08-21 20:27:13 +00:00
|
|
|
|
2012-02-15 18:26:43 +00:00
|
|
|
// Allow {CLIENT_RENEG_LIMIT} client-initiated session renegotiations
|
|
|
|
// every {CLIENT_RENEG_WINDOW} seconds. An error event is emitted if more
|
|
|
|
// renegotations are seen. The settings are applied to all remote client
|
|
|
|
// connections.
|
2012-02-18 23:01:35 +00:00
|
|
|
exports.CLIENT_RENEG_LIMIT = 3;
|
2012-02-15 18:26:43 +00:00
|
|
|
exports.CLIENT_RENEG_WINDOW = 600;
|
|
|
|
|
2013-01-22 13:27:05 +00:00
|
|
|
exports.SLAB_BUFFER_SIZE = 10 * 1024 * 1024;
|
|
|
|
|
2013-03-18 23:16:55 +00:00
|
|
|
exports.getCiphers = function() {
|
|
|
|
var names = process.binding('crypto').getSSLCiphers();
|
|
|
|
// Drop all-caps names in favor of their lowercase aliases,
|
|
|
|
var ctx = {};
|
|
|
|
names.forEach(function(name) {
|
|
|
|
if (/^[0-9A-Z\-]+$/.test(name)) name = name.toLowerCase();
|
|
|
|
ctx[name] = true;
|
|
|
|
});
|
|
|
|
return Object.getOwnPropertyNames(ctx).sort();
|
|
|
|
};
|
|
|
|
|
2012-02-15 18:26:43 +00:00
|
|
|
|
2010-12-06 02:19:18 +00:00
|
|
|
var debug;
|
2011-01-03 23:27:57 +00:00
|
|
|
if (process.env.NODE_DEBUG && /tls/.test(process.env.NODE_DEBUG)) {
|
2011-02-09 18:23:26 +00:00
|
|
|
debug = function(a) { console.error('TLS:', a); };
|
2010-12-06 02:19:18 +00:00
|
|
|
} else {
|
|
|
|
debug = function() { };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-11 10:13:24 +00:00
|
|
|
var Connection = null;
|
|
|
|
try {
|
2011-01-07 00:06:27 +00:00
|
|
|
Connection = process.binding('crypto').Connection;
|
|
|
|
} catch (e) {
|
2010-12-11 10:13:24 +00:00
|
|
|
throw new Error('node.js not compiled with openssl crypto support.');
|
|
|
|
}
|
|
|
|
|
2011-04-14 03:53:39 +00:00
|
|
|
// Convert protocols array into valid OpenSSL protocols list
|
|
|
|
// ("\x06spdy/2\x08http/1.1\x08http/1.0")
|
|
|
|
function convertNPNProtocols(NPNProtocols, out) {
|
|
|
|
// If NPNProtocols is Array - translate it into buffer
|
|
|
|
if (Array.isArray(NPNProtocols)) {
|
|
|
|
var buff = new Buffer(NPNProtocols.reduce(function(p, c) {
|
|
|
|
return p + 1 + Buffer.byteLength(c);
|
|
|
|
}, 0));
|
|
|
|
|
|
|
|
NPNProtocols.reduce(function(offset, c) {
|
|
|
|
var clen = Buffer.byteLength(c);
|
|
|
|
buff[offset] = clen;
|
|
|
|
buff.write(c, offset + 1);
|
|
|
|
|
|
|
|
return offset + 1 + clen;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
NPNProtocols = buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's already a Buffer - store it
|
|
|
|
if (Buffer.isBuffer(NPNProtocols)) {
|
|
|
|
out.NPNProtocols = NPNProtocols;
|
|
|
|
}
|
2011-07-29 17:03:05 +00:00
|
|
|
}
|
2010-12-09 08:10:16 +00:00
|
|
|
|
2012-07-05 19:50:21 +00:00
|
|
|
|
2012-07-11 19:54:20 +00:00
|
|
|
function checkServerIdentity(host, cert) {
|
|
|
|
// Create regexp to much hostnames
|
|
|
|
function regexpify(host, wildcards) {
|
|
|
|
// Add trailing dot (make hostnames uniform)
|
|
|
|
if (!/\.$/.test(host)) host += '.';
|
|
|
|
|
|
|
|
// The same applies to hostname with more than one wildcard,
|
|
|
|
// if hostname has wildcard when wildcards are not allowed,
|
|
|
|
// or if there are less than two dots after wildcard (i.e. *.com or *d.com)
|
2013-01-14 23:29:46 +00:00
|
|
|
//
|
|
|
|
// also
|
|
|
|
//
|
|
|
|
// "The client SHOULD NOT attempt to match a presented identifier in
|
|
|
|
// which the wildcard character comprises a label other than the
|
|
|
|
// left-most label (e.g., do not match bar.*.example.net)."
|
|
|
|
// RFC6125
|
|
|
|
if (!wildcards && /\*/.test(host) || /[\.\*].*\*/.test(host) ||
|
2012-07-11 19:54:20 +00:00
|
|
|
/\*/.test(host) && !/\*.*\..+\..+/.test(host)) {
|
|
|
|
return /$./;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace wildcard chars with regexp's wildcard and
|
|
|
|
// escape all characters that have special meaning in regexps
|
|
|
|
// (i.e. '.', '[', '{', '*', and others)
|
|
|
|
var re = host.replace(
|
2012-07-20 18:07:16 +00:00
|
|
|
/\*([a-z0-9\\-_\.])|[\.,\-\\\^\$+?*\[\]\(\):!\|{}]/g,
|
|
|
|
function(all, sub) {
|
|
|
|
if (sub) return '[a-z0-9\\-_]*' + (sub === '-' ? '\\-' : sub);
|
|
|
|
return '\\' + all;
|
|
|
|
});
|
2012-07-11 19:54:20 +00:00
|
|
|
|
|
|
|
return new RegExp('^' + re + '$', 'i');
|
|
|
|
}
|
|
|
|
|
|
|
|
var dnsNames = [],
|
|
|
|
uriNames = [],
|
|
|
|
ips = [],
|
2013-01-14 23:29:46 +00:00
|
|
|
matchCN = true,
|
2012-07-11 19:54:20 +00:00
|
|
|
valid = false;
|
|
|
|
|
|
|
|
// There're several names to perform check against:
|
|
|
|
// CN and altnames in certificate extension
|
|
|
|
// (DNS names, IP addresses, and URIs)
|
|
|
|
//
|
|
|
|
// Walk through altnames and generate lists of those names
|
|
|
|
if (cert.subjectaltname) {
|
2013-01-14 23:29:46 +00:00
|
|
|
matchCN = false;
|
2012-07-11 19:54:20 +00:00
|
|
|
cert.subjectaltname.split(/, /g).forEach(function(altname) {
|
|
|
|
if (/^DNS:/.test(altname)) {
|
|
|
|
dnsNames.push(altname.slice(4));
|
|
|
|
} else if (/^IP Address:/.test(altname)) {
|
|
|
|
ips.push(altname.slice(11));
|
|
|
|
} else if (/^URI:/.test(altname)) {
|
|
|
|
var uri = url.parse(altname.slice(4));
|
|
|
|
if (uri) uriNames.push(uri.hostname);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// If hostname is an IP address, it should be present in the list of IP
|
|
|
|
// addresses.
|
|
|
|
if (net.isIP(host)) {
|
|
|
|
valid = ips.some(function(ip) {
|
|
|
|
return ip === host;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Transform hostname to canonical form
|
|
|
|
if (!/\.$/.test(host)) host += '.';
|
|
|
|
|
|
|
|
// Otherwise check all DNS/URI records from certificate
|
|
|
|
// (with allowed wildcards)
|
|
|
|
dnsNames = dnsNames.map(function(name) {
|
|
|
|
return regexpify(name, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Wildcards ain't allowed in URI names
|
|
|
|
uriNames = uriNames.map(function(name) {
|
|
|
|
return regexpify(name, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
dnsNames = dnsNames.concat(uriNames);
|
|
|
|
|
2013-01-14 23:29:46 +00:00
|
|
|
if (dnsNames.length > 0) matchCN = false;
|
|
|
|
|
|
|
|
// Match against Common Name (CN) only if altnames are not present.
|
|
|
|
//
|
|
|
|
// "As noted, a client MUST NOT seek a match for a reference identifier
|
|
|
|
// of CN-ID if the presented identifiers include a DNS-ID, SRV-ID,
|
|
|
|
// URI-ID, or any application-specific identifier types supported by the
|
|
|
|
// client."
|
|
|
|
// RFC6125
|
|
|
|
if (matchCN) {
|
|
|
|
var commonNames = cert.subject.CN;
|
|
|
|
if (Array.isArray(commonNames)) {
|
|
|
|
for (var i = 0, k = commonNames.length; i < k; ++i) {
|
|
|
|
dnsNames.push(regexpify(commonNames[i], true));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dnsNames.push(regexpify(commonNames, true));
|
2012-08-12 19:46:56 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-11 19:54:20 +00:00
|
|
|
|
|
|
|
valid = dnsNames.some(function(re) {
|
|
|
|
return re.test(host);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return valid;
|
2012-07-20 18:07:16 +00:00
|
|
|
}
|
2012-07-11 19:54:20 +00:00
|
|
|
exports.checkServerIdentity = checkServerIdentity;
|
|
|
|
|
|
|
|
|
2012-07-05 19:50:21 +00:00
|
|
|
function SlabBuffer() {
|
|
|
|
this.create();
|
2012-07-12 00:46:28 +00:00
|
|
|
}
|
2012-07-05 19:50:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
SlabBuffer.prototype.create = function create() {
|
|
|
|
this.isFull = false;
|
2013-01-22 13:27:05 +00:00
|
|
|
this.pool = new Buffer(exports.SLAB_BUFFER_SIZE);
|
2012-07-05 19:50:21 +00:00
|
|
|
this.offset = 0;
|
|
|
|
this.remaining = this.pool.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
SlabBuffer.prototype.use = function use(context, fn, size) {
|
2012-07-05 19:50:21 +00:00
|
|
|
if (this.remaining === 0) {
|
|
|
|
this.isFull = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
var actualSize = this.remaining;
|
2012-07-05 19:50:21 +00:00
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
if (size !== null) actualSize = Math.min(size, actualSize);
|
|
|
|
|
|
|
|
var bytes = fn.call(context, this.pool, this.offset, actualSize);
|
2012-07-05 19:50:21 +00:00
|
|
|
if (bytes > 0) {
|
|
|
|
this.offset += bytes;
|
|
|
|
this.remaining -= bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(this.remaining >= 0);
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-22 13:27:05 +00:00
|
|
|
var slabBuffer = null;
|
2012-07-05 19:50:21 +00:00
|
|
|
|
|
|
|
|
2010-12-09 08:10:16 +00:00
|
|
|
// Base class of both CleartextStream and EncryptedStream
|
2013-01-31 17:20:49 +00:00
|
|
|
function CryptoStream(pair, options) {
|
|
|
|
stream.Duplex.call(this, options);
|
2010-12-09 08:10:16 +00:00
|
|
|
|
|
|
|
this.pair = pair;
|
2013-01-31 17:20:49 +00:00
|
|
|
this._pending = null;
|
2013-03-04 03:14:06 +00:00
|
|
|
this._pendingEncoding = '';
|
2013-01-31 17:20:49 +00:00
|
|
|
this._pendingCallback = null;
|
|
|
|
this._doneFlag = false;
|
|
|
|
this._resumingSession = false;
|
2013-02-21 09:28:51 +00:00
|
|
|
this._reading = true;
|
2013-01-31 17:20:49 +00:00
|
|
|
this._destroyed = false;
|
|
|
|
this._ended = false;
|
|
|
|
this._finished = false;
|
|
|
|
this._opposite = null;
|
2010-12-09 08:10:16 +00:00
|
|
|
|
2013-01-22 13:27:05 +00:00
|
|
|
if (slabBuffer === null) slabBuffer = new SlabBuffer();
|
2012-07-05 19:50:21 +00:00
|
|
|
this._buffer = slabBuffer;
|
2013-01-31 17:20:49 +00:00
|
|
|
|
|
|
|
this.once('finish', onCryptoStreamFinish);
|
|
|
|
|
|
|
|
// net.Socket calls .onend too
|
|
|
|
this.once('end', onCryptoStreamEnd);
|
2010-12-09 08:10:16 +00:00
|
|
|
}
|
2013-01-31 17:20:49 +00:00
|
|
|
util.inherits(CryptoStream, stream.Duplex);
|
|
|
|
|
2010-12-09 08:10:16 +00:00
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
function onCryptoStreamFinish() {
|
|
|
|
this._finished = true;
|
2010-12-09 08:10:16 +00:00
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.onfinish');
|
|
|
|
if (this.pair.ssl) {
|
|
|
|
// Generate close notify
|
|
|
|
// NOTE: first call checks if client has sent us shutdown,
|
|
|
|
// second call enqueues shutdown into the BIO.
|
|
|
|
if (this.pair.ssl.shutdown() !== 1) {
|
|
|
|
this.pair.ssl.shutdown();
|
|
|
|
}
|
|
|
|
}
|
2011-02-09 18:23:26 +00:00
|
|
|
} else {
|
2013-01-31 17:20:49 +00:00
|
|
|
debug('encrypted.onfinish');
|
2011-02-09 18:23:26 +00:00
|
|
|
}
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
// Try to read just to get sure that we won't miss EOF
|
|
|
|
if (this._opposite.readable) this._opposite.read(0);
|
2011-02-02 23:26:28 +00:00
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
if (this._opposite._ended) {
|
|
|
|
this._done();
|
2010-12-20 19:08:51 +00:00
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
// No half-close, sorry
|
|
|
|
if (this === this.pair.cleartext) this._opposite._done();
|
2010-12-20 19:08:51 +00:00
|
|
|
}
|
2013-01-31 17:20:49 +00:00
|
|
|
}
|
2010-12-20 19:08:51 +00:00
|
|
|
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
function onCryptoStreamEnd() {
|
|
|
|
this._ended = true;
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.onend');
|
|
|
|
} else {
|
|
|
|
debug('encrypted.onend');
|
2010-12-20 19:08:51 +00:00
|
|
|
}
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
if (this.onend) this.onend();
|
|
|
|
}
|
2010-12-20 19:08:51 +00:00
|
|
|
|
2011-04-13 20:54:47 +00:00
|
|
|
|
2013-03-04 03:14:06 +00:00
|
|
|
CryptoStream.prototype._write = function write(data, encoding, cb) {
|
2013-01-31 17:20:49 +00:00
|
|
|
assert(this._pending === null);
|
2011-02-17 02:09:43 +00:00
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
// Black-hole data
|
|
|
|
if (!this.pair.ssl) return cb(null);
|
|
|
|
|
|
|
|
// When resuming session don't accept any new data.
|
|
|
|
// And do not put too much data into openssl, before writing it from encrypted
|
|
|
|
// side.
|
2011-09-27 09:25:10 +00:00
|
|
|
//
|
2013-01-31 17:20:49 +00:00
|
|
|
// TODO(indutny): Remove magic number, use watermark based limits
|
|
|
|
if (!this._resumingSession &&
|
2013-02-21 09:28:51 +00:00
|
|
|
this._opposite._internallyPendingBytes() < 128 * 1024) {
|
2013-01-31 17:20:49 +00:00
|
|
|
// Write current buffer now
|
|
|
|
var written;
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.write called with ' + data.length + ' bytes');
|
|
|
|
written = this.pair.ssl.clearIn(data, 0, data.length);
|
2011-09-27 09:25:10 +00:00
|
|
|
} else {
|
2013-01-31 17:20:49 +00:00
|
|
|
debug('encrypted.write called with ' + data.length + ' bytes');
|
|
|
|
written = this.pair.ssl.encIn(data, 0, data.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Force SSL_read call to cycle some states/data inside OpenSSL
|
|
|
|
this.pair.cleartext.read(0);
|
|
|
|
|
|
|
|
// Cycle encrypted data
|
|
|
|
if (this.pair.encrypted._internallyPendingBytes()) {
|
|
|
|
this.pair.encrypted.read(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle and report errors
|
|
|
|
if (this.pair.ssl && this.pair.ssl.error) {
|
|
|
|
return cb(this.pair.error());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get NPN and Server name when ready
|
|
|
|
this.pair.maybeInitFinished();
|
|
|
|
|
|
|
|
// Whole buffer was written
|
|
|
|
if (written === data.length) {
|
2011-09-27 09:25:10 +00:00
|
|
|
if (this === this.pair.cleartext) {
|
2013-01-31 17:20:49 +00:00
|
|
|
debug('cleartext.write succeed with ' + data.length + ' bytes');
|
2011-09-27 09:25:10 +00:00
|
|
|
} else {
|
2013-01-31 17:20:49 +00:00
|
|
|
debug('encrypted.write succeed with ' + data.length + ' bytes');
|
2011-09-27 09:25:10 +00:00
|
|
|
}
|
2013-01-31 17:20:49 +00:00
|
|
|
|
|
|
|
return cb(null);
|
2011-09-27 09:25:10 +00:00
|
|
|
}
|
2013-01-31 17:20:49 +00:00
|
|
|
assert(written === 0 || written === -1);
|
|
|
|
} else {
|
|
|
|
debug('cleartext.write queue is full');
|
|
|
|
|
|
|
|
// Force SSL_read call to cycle some states/data inside OpenSSL
|
|
|
|
this.pair.cleartext.read(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No write has happened
|
|
|
|
this._pending = data;
|
2013-03-04 03:14:06 +00:00
|
|
|
this._pendingEncoding = encoding;
|
2013-01-31 17:20:49 +00:00
|
|
|
this._pendingCallback = cb;
|
|
|
|
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.write queued with ' + data.length + ' bytes');
|
|
|
|
} else {
|
|
|
|
debug('encrypted.write queued with ' + data.length + ' bytes');
|
2011-09-27 09:25:10 +00:00
|
|
|
}
|
2010-12-09 08:10:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
CryptoStream.prototype._writePending = function writePending() {
|
|
|
|
var data = this._pending,
|
2013-03-04 03:14:06 +00:00
|
|
|
encoding = this._pendingEncoding,
|
2013-01-31 17:20:49 +00:00
|
|
|
cb = this._pendingCallback;
|
|
|
|
|
|
|
|
this._pending = null;
|
2013-03-04 03:14:06 +00:00
|
|
|
this._pendingEncoding = '';
|
2013-01-31 17:20:49 +00:00
|
|
|
this._pendingCallback = null;
|
2013-03-04 03:14:06 +00:00
|
|
|
this._write(data, encoding, cb);
|
2010-12-09 08:10:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-02-28 23:32:32 +00:00
|
|
|
CryptoStream.prototype._read = function read(size) {
|
2013-01-31 17:20:49 +00:00
|
|
|
// XXX: EOF?!
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-02-28 23:32:32 +00:00
|
|
|
if (!this.pair.ssl) return this.push(null);
|
2013-01-31 17:20:49 +00:00
|
|
|
|
|
|
|
// Wait for session to be resumed
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-02-28 23:32:32 +00:00
|
|
|
// Mark that we're done reading, but don't provide data or EOF
|
|
|
|
if (this._resumingSession || !this._reading) return this.push('');
|
2013-01-31 17:20:49 +00:00
|
|
|
|
|
|
|
var out;
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.read called with ' + size + ' bytes');
|
|
|
|
out = this.pair.ssl.clearOut;
|
|
|
|
} else {
|
|
|
|
debug('encrypted.read called with ' + size + ' bytes');
|
|
|
|
out = this.pair.ssl.encOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
var bytesRead = 0,
|
|
|
|
start = this._buffer.offset;
|
|
|
|
do {
|
|
|
|
var read = this._buffer.use(this.pair.ssl, out, size);
|
|
|
|
if (read > 0) {
|
|
|
|
bytesRead += read;
|
|
|
|
size -= read;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle and report errors
|
|
|
|
if (this.pair.ssl && this.pair.ssl.error) {
|
|
|
|
this.pair.error();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get NPN and Server name when ready
|
|
|
|
this.pair.maybeInitFinished();
|
|
|
|
} while (read > 0 && !this._buffer.isFull && bytesRead < size);
|
|
|
|
|
|
|
|
// Create new buffer if previous was filled up
|
|
|
|
var pool = this._buffer.pool;
|
|
|
|
if (this._buffer.isFull) this._buffer.create();
|
|
|
|
|
|
|
|
assert(bytesRead >= 0);
|
|
|
|
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.read succeed with ' + bytesRead + ' bytes');
|
|
|
|
} else {
|
|
|
|
debug('encrypted.read succeed with ' + bytesRead + ' bytes');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try writing pending data
|
|
|
|
if (this._pending !== null) this._writePending();
|
2013-03-15 18:49:31 +00:00
|
|
|
if (this._opposite._pending !== null) this._opposite._writePending();
|
2013-01-31 17:20:49 +00:00
|
|
|
|
|
|
|
if (bytesRead === 0) {
|
|
|
|
// EOF when cleartext has finished and we have nothing to read
|
|
|
|
if (this._opposite._finished && this._internallyPendingBytes() === 0) {
|
|
|
|
// Perform graceful shutdown
|
|
|
|
this._done();
|
|
|
|
|
|
|
|
// No half-open, sorry!
|
|
|
|
if (this === this.pair.cleartext)
|
|
|
|
this._opposite._done();
|
|
|
|
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-02-28 23:32:32 +00:00
|
|
|
// EOF
|
|
|
|
return this.push(null);
|
2013-01-31 17:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bail out
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-02-28 23:32:32 +00:00
|
|
|
return this.push('');
|
2013-01-31 17:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Give them requested data
|
|
|
|
if (this.ondata) {
|
|
|
|
var self = this;
|
|
|
|
this.ondata(pool, start, start + bytesRead);
|
|
|
|
|
|
|
|
// Consume data automatically
|
|
|
|
// simple/test-https-drain fails without it
|
|
|
|
process.nextTick(function() {
|
|
|
|
self.read(bytesRead);
|
|
|
|
});
|
|
|
|
}
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-02-28 23:32:32 +00:00
|
|
|
return this.push(pool.slice(start, start + bytesRead));
|
2010-12-09 08:10:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-29 01:14:40 +00:00
|
|
|
CryptoStream.prototype.setTimeout = function(timeout, callback) {
|
|
|
|
if (this.socket) this.socket.setTimeout(timeout, callback);
|
2011-01-02 09:13:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-29 01:14:40 +00:00
|
|
|
CryptoStream.prototype.setNoDelay = function(noDelay) {
|
|
|
|
if (this.socket) this.socket.setNoDelay(noDelay);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CryptoStream.prototype.setKeepAlive = function(enable, initialDelay) {
|
2012-03-22 02:50:58 +00:00
|
|
|
if (this.socket) this.socket.setKeepAlive(enable, initialDelay);
|
2011-01-19 01:56:52 +00:00
|
|
|
};
|
|
|
|
|
2013-01-23 23:17:04 +00:00
|
|
|
CryptoStream.prototype.__defineGetter__('bytesWritten', function() {
|
|
|
|
return this.socket ? this.socket.bytesWritten : 0;
|
|
|
|
});
|
2011-01-19 01:56:52 +00:00
|
|
|
|
2011-01-19 02:03:55 +00:00
|
|
|
|
2011-08-26 11:00:40 +00:00
|
|
|
// Example:
|
|
|
|
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\nemailAddress=ry@clouds.org
|
2011-01-07 00:06:27 +00:00
|
|
|
function parseCertString(s) {
|
2010-12-18 19:18:33 +00:00
|
|
|
var out = {};
|
2011-08-26 11:00:40 +00:00
|
|
|
var parts = s.split('\n');
|
|
|
|
for (var i = 0, len = parts.length; i < len; i++) {
|
2010-12-18 19:18:33 +00:00
|
|
|
var sepIndex = parts[i].indexOf('=');
|
|
|
|
if (sepIndex > 0) {
|
|
|
|
var key = parts[i].slice(0, sepIndex);
|
|
|
|
var value = parts[i].slice(sepIndex + 1);
|
2012-03-03 09:46:45 +00:00
|
|
|
if (key in out) {
|
|
|
|
if (!Array.isArray(out[key])) {
|
|
|
|
out[key] = [out[key]];
|
|
|
|
}
|
|
|
|
out[key].push(value);
|
|
|
|
} else {
|
|
|
|
out[key] = value;
|
|
|
|
}
|
2010-12-18 19:18:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-09 10:31:22 +00:00
|
|
|
CryptoStream.prototype.getPeerCertificate = function() {
|
2011-05-06 23:48:44 +00:00
|
|
|
if (this.pair.ssl) {
|
|
|
|
var c = this.pair.ssl.getPeerCertificate();
|
2010-12-18 19:18:33 +00:00
|
|
|
|
|
|
|
if (c) {
|
|
|
|
if (c.issuer) c.issuer = parseCertString(c.issuer);
|
|
|
|
if (c.subject) c.subject = parseCertString(c.subject);
|
|
|
|
return c;
|
|
|
|
}
|
2010-12-09 10:31:22 +00:00
|
|
|
}
|
2010-12-18 19:18:33 +00:00
|
|
|
|
|
|
|
return null;
|
2010-12-09 10:31:22 +00:00
|
|
|
};
|
|
|
|
|
2011-08-29 18:09:01 +00:00
|
|
|
CryptoStream.prototype.getSession = function() {
|
|
|
|
if (this.pair.ssl) {
|
|
|
|
return this.pair.ssl.getSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
CryptoStream.prototype.isSessionReused = function() {
|
|
|
|
if (this.pair.ssl) {
|
|
|
|
return this.pair.ssl.isSessionReused();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
2010-12-09 10:31:22 +00:00
|
|
|
|
|
|
|
CryptoStream.prototype.getCipher = function(err) {
|
2011-05-06 23:48:44 +00:00
|
|
|
if (this.pair.ssl) {
|
|
|
|
return this.pair.ssl.getCurrentCipher();
|
2010-12-09 10:31:22 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
CryptoStream.prototype.end = function(chunk, encoding) {
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.end');
|
|
|
|
} else {
|
|
|
|
debug('encrypted.end');
|
2011-05-06 23:48:44 +00:00
|
|
|
}
|
2010-12-11 09:18:32 +00:00
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
// Write pending data first
|
|
|
|
if (this._pending !== null) this._writePending();
|
2011-02-03 20:17:26 +00:00
|
|
|
|
2011-05-06 23:48:44 +00:00
|
|
|
this.writable = false;
|
2011-02-02 23:26:28 +00:00
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
stream.Duplex.prototype.end.call(this, chunk, encoding);
|
2010-12-11 09:18:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-04 19:22:17 +00:00
|
|
|
CryptoStream.prototype.destroySoon = function(err) {
|
2013-01-31 17:20:49 +00:00
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.destroySoon');
|
2011-03-09 18:53:06 +00:00
|
|
|
} else {
|
2013-01-31 17:20:49 +00:00
|
|
|
debug('encrypted.destroySoon');
|
2011-03-09 18:53:06 +00:00
|
|
|
}
|
2013-01-31 17:20:49 +00:00
|
|
|
|
|
|
|
if (this.writable)
|
|
|
|
this.end();
|
|
|
|
|
2013-03-04 00:12:02 +00:00
|
|
|
if (this._writableState.finished)
|
2013-01-31 17:20:49 +00:00
|
|
|
this.destroy();
|
|
|
|
else
|
|
|
|
this.once('finish', this.destroy);
|
2011-01-04 19:22:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-11 09:21:25 +00:00
|
|
|
CryptoStream.prototype.destroy = function(err) {
|
2013-01-31 17:20:49 +00:00
|
|
|
if (this._destroyed) return;
|
|
|
|
this._destroyed = true;
|
|
|
|
this.readable = this.writable = false;
|
|
|
|
|
|
|
|
// Destroy both ends
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.destroy');
|
|
|
|
} else {
|
|
|
|
debug('encrypted.destroy');
|
|
|
|
}
|
|
|
|
this._opposite.destroy();
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
process.nextTick(function() {
|
|
|
|
// Force EOF
|
|
|
|
self.push(null);
|
|
|
|
|
|
|
|
// Emit 'close' event
|
|
|
|
self.emit('close', err ? true : false);
|
|
|
|
});
|
2010-12-11 09:21:25 +00:00
|
|
|
};
|
|
|
|
|
2011-02-02 23:26:28 +00:00
|
|
|
|
2011-02-03 20:17:26 +00:00
|
|
|
CryptoStream.prototype._done = function() {
|
|
|
|
this._doneFlag = true;
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
if (this === this.pair.encrypted && !this.pair._secureEstablished)
|
|
|
|
return this.pair.error();
|
|
|
|
|
2011-02-03 20:17:26 +00:00
|
|
|
if (this.pair.cleartext._doneFlag &&
|
|
|
|
this.pair.encrypted._doneFlag &&
|
2011-05-06 23:48:44 +00:00
|
|
|
!this.pair._doneFlag) {
|
2011-02-03 20:17:26 +00:00
|
|
|
// If both streams are done:
|
2013-01-31 17:20:49 +00:00
|
|
|
this.pair.destroy();
|
2011-02-03 20:17:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-05-20 17:08:08 +00:00
|
|
|
// readyState is deprecated. Don't use it.
|
|
|
|
Object.defineProperty(CryptoStream.prototype, 'readyState', {
|
|
|
|
get: function() {
|
|
|
|
if (this._connecting) {
|
|
|
|
return 'opening';
|
|
|
|
} else if (this.readable && this.writable) {
|
|
|
|
return 'open';
|
|
|
|
} else if (this.readable && !this.writable) {
|
|
|
|
return 'readOnly';
|
|
|
|
} else if (!this.readable && this.writable) {
|
|
|
|
return 'writeOnly';
|
|
|
|
} else {
|
|
|
|
return 'closed';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-12-11 09:21:25 +00:00
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
function CleartextStream(pair, options) {
|
|
|
|
CryptoStream.call(this, pair, options);
|
2013-02-21 09:28:51 +00:00
|
|
|
|
tls: Prevent hang in readStart
This is not a great fix, and it's a bug that's very tricky to reproduce.
Occasionally, while downloading a file, especially on Linux for some
reason, the pause/resume timing will be just right such that the
CryptoStream is in a 'reading' state, but actually has no data, so it
ought to pull more in. Because there's no reads happening, it just sits
there, and the process will exit
This is, fundamentally, a factor of how the HTTP implementation sits
atop CryptoStreams and TCP Socket objects, which is utterly horrible,
and needs to be rewritten. However, in the meantime, npm downloads are
prematurely exiting, causing hard-to-debug "cb() never called!" errors.
2013-03-20 23:14:36 +00:00
|
|
|
// This is a fake kludge to support how the http impl sits
|
|
|
|
// on top of net Sockets
|
2013-02-21 09:28:51 +00:00
|
|
|
var self = this;
|
|
|
|
this._handle = {
|
|
|
|
readStop: function() {
|
|
|
|
self._reading = false;
|
|
|
|
},
|
|
|
|
readStart: function() {
|
tls: Prevent hang in readStart
This is not a great fix, and it's a bug that's very tricky to reproduce.
Occasionally, while downloading a file, especially on Linux for some
reason, the pause/resume timing will be just right such that the
CryptoStream is in a 'reading' state, but actually has no data, so it
ought to pull more in. Because there's no reads happening, it just sits
there, and the process will exit
This is, fundamentally, a factor of how the HTTP implementation sits
atop CryptoStreams and TCP Socket objects, which is utterly horrible,
and needs to be rewritten. However, in the meantime, npm downloads are
prematurely exiting, causing hard-to-debug "cb() never called!" errors.
2013-03-20 23:14:36 +00:00
|
|
|
if (self._reading && self._readableState.length > 0) return;
|
2013-02-21 09:28:51 +00:00
|
|
|
self._reading = true;
|
|
|
|
self.read(0);
|
tls: Prevent hang in readStart
This is not a great fix, and it's a bug that's very tricky to reproduce.
Occasionally, while downloading a file, especially on Linux for some
reason, the pause/resume timing will be just right such that the
CryptoStream is in a 'reading' state, but actually has no data, so it
ought to pull more in. Because there's no reads happening, it just sits
there, and the process will exit
This is, fundamentally, a factor of how the HTTP implementation sits
atop CryptoStreams and TCP Socket objects, which is utterly horrible,
and needs to be rewritten. However, in the meantime, npm downloads are
prematurely exiting, causing hard-to-debug "cb() never called!" errors.
2013-03-20 23:14:36 +00:00
|
|
|
if (self._opposite.readable) self._opposite.read(0);
|
2013-02-21 09:28:51 +00:00
|
|
|
}
|
|
|
|
};
|
2010-12-11 06:29:46 +00:00
|
|
|
}
|
2010-12-11 09:42:34 +00:00
|
|
|
util.inherits(CleartextStream, CryptoStream);
|
2010-12-11 06:29:46 +00:00
|
|
|
|
|
|
|
|
2011-04-13 20:54:47 +00:00
|
|
|
CleartextStream.prototype._internallyPendingBytes = function() {
|
2011-05-06 23:48:44 +00:00
|
|
|
if (this.pair.ssl) {
|
|
|
|
return this.pair.ssl.clearPending();
|
2011-02-02 23:26:28 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-01 01:29:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-10-26 12:12:23 +00:00
|
|
|
CleartextStream.prototype.address = function() {
|
|
|
|
return this.socket && this.socket.address();
|
|
|
|
};
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
|
2011-10-26 12:12:23 +00:00
|
|
|
CleartextStream.prototype.__defineGetter__('remoteAddress', function() {
|
|
|
|
return this.socket && this.socket.remoteAddress;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
CleartextStream.prototype.__defineGetter__('remotePort', function() {
|
|
|
|
return this.socket && this.socket.remotePort;
|
|
|
|
});
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
function EncryptedStream(pair, options) {
|
|
|
|
CryptoStream.call(this, pair, options);
|
2010-12-11 09:42:34 +00:00
|
|
|
}
|
|
|
|
util.inherits(EncryptedStream, CryptoStream);
|
|
|
|
|
|
|
|
|
2011-04-13 20:54:47 +00:00
|
|
|
EncryptedStream.prototype._internallyPendingBytes = function() {
|
2011-05-06 23:48:44 +00:00
|
|
|
if (this.pair.ssl) {
|
|
|
|
return this.pair.ssl.encPending();
|
2011-02-02 23:26:28 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-01 01:29:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-02-15 18:26:43 +00:00
|
|
|
function onhandshakestart() {
|
|
|
|
debug('onhandshakestart');
|
|
|
|
|
2012-10-08 00:18:30 +00:00
|
|
|
var self = this;
|
|
|
|
var ssl = self.ssl;
|
|
|
|
var now = Date.now();
|
|
|
|
|
|
|
|
assert(now >= ssl.lastHandshakeTime);
|
2012-02-15 18:26:43 +00:00
|
|
|
|
2012-10-08 00:18:30 +00:00
|
|
|
if ((now - ssl.lastHandshakeTime) >= exports.CLIENT_RENEG_WINDOW * 1000) {
|
|
|
|
ssl.handshakes = 0;
|
2012-02-15 18:26:43 +00:00
|
|
|
}
|
2012-10-08 00:18:30 +00:00
|
|
|
|
|
|
|
var first = (ssl.lastHandshakeTime === 0);
|
|
|
|
ssl.lastHandshakeTime = now;
|
|
|
|
if (first) return;
|
|
|
|
|
|
|
|
if (++ssl.handshakes > exports.CLIENT_RENEG_LIMIT) {
|
2012-02-15 18:26:43 +00:00
|
|
|
// 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.
|
2012-08-13 15:31:22 +00:00
|
|
|
setImmediate(function() {
|
2012-02-15 18:26:43 +00:00
|
|
|
var err = new Error('TLS session renegotiation attack detected.');
|
|
|
|
if (self.cleartext) self.cleartext.emit('error', err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onhandshakedone() {
|
|
|
|
// for future use
|
|
|
|
debug('onhandshakedone');
|
|
|
|
}
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
|
2012-07-07 20:20:23 +00:00
|
|
|
function onclienthello(hello) {
|
|
|
|
var self = this,
|
|
|
|
once = false;
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
this._resumingSession = true;
|
2012-07-07 20:20:23 +00:00
|
|
|
function callback(err, session) {
|
|
|
|
if (once) return;
|
|
|
|
once = true;
|
|
|
|
|
|
|
|
if (err) return self.socket.destroy(err);
|
|
|
|
|
|
|
|
self.ssl.loadSession(session);
|
|
|
|
|
2013-01-31 17:20:49 +00:00
|
|
|
// Cycle data
|
|
|
|
self._resumingSession = false;
|
|
|
|
self.cleartext.read(0);
|
|
|
|
self.encrypted.read(0);
|
2012-07-07 20:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hello.sessionId.length <= 0 ||
|
|
|
|
!this.server ||
|
|
|
|
!this.server.emit('resumeSession', hello.sessionId, callback)) {
|
|
|
|
callback(null, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onnewsession(key, session) {
|
|
|
|
if (!this.server) return;
|
|
|
|
this.server.emit('newSession', key, session);
|
|
|
|
}
|
|
|
|
|
2012-02-15 18:26:43 +00:00
|
|
|
|
2010-12-06 02:19:18 +00:00
|
|
|
/**
|
|
|
|
* Provides a pair of streams to do encrypted communication.
|
|
|
|
*/
|
|
|
|
|
2011-04-14 03:53:39 +00:00
|
|
|
function SecurePair(credentials, isServer, requestCert, rejectUnauthorized,
|
2011-07-28 15:52:04 +00:00
|
|
|
options) {
|
2010-12-06 02:19:18 +00:00
|
|
|
if (!(this instanceof SecurePair)) {
|
2010-12-08 19:22:08 +00:00
|
|
|
return new SecurePair(credentials,
|
|
|
|
isServer,
|
|
|
|
requestCert,
|
2011-04-14 03:53:39 +00:00
|
|
|
rejectUnauthorized,
|
2011-07-28 15:52:04 +00:00
|
|
|
options);
|
2010-12-06 02:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2011-07-28 15:52:04 +00:00
|
|
|
options || (options = {});
|
|
|
|
|
2010-12-06 02:19:18 +00:00
|
|
|
events.EventEmitter.call(this);
|
|
|
|
|
2012-07-07 20:20:23 +00:00
|
|
|
this.server = options.server;
|
2010-12-06 02:19:18 +00:00
|
|
|
this._secureEstablished = false;
|
|
|
|
this._isServer = isServer ? true : false;
|
|
|
|
this._encWriteState = true;
|
|
|
|
this._clearWriteState = true;
|
2011-05-06 23:48:44 +00:00
|
|
|
this._doneFlag = false;
|
2013-01-31 17:20:49 +00:00
|
|
|
this._destroying = false;
|
2010-12-06 02:19:18 +00:00
|
|
|
|
|
|
|
if (!credentials) {
|
|
|
|
this.credentials = crypto.createCredentials();
|
|
|
|
} else {
|
|
|
|
this.credentials = credentials;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._isServer) {
|
|
|
|
// For clients, we will always have either a given ca list or be using
|
|
|
|
// default one
|
|
|
|
requestCert = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._rejectUnauthorized = rejectUnauthorized ? true : false;
|
|
|
|
this._requestCert = requestCert ? true : false;
|
|
|
|
|
2011-05-06 23:48:44 +00:00
|
|
|
this.ssl = new Connection(this.credentials.context,
|
2012-07-07 20:20:23 +00:00
|
|
|
this._isServer ? true : false,
|
|
|
|
this._isServer ? this._requestCert :
|
|
|
|
options.servername,
|
|
|
|
this._rejectUnauthorized);
|
2010-12-06 02:19:18 +00:00
|
|
|
|
2012-02-15 18:26:43 +00:00
|
|
|
if (this._isServer) {
|
|
|
|
this.ssl.onhandshakestart = onhandshakestart.bind(this);
|
|
|
|
this.ssl.onhandshakedone = onhandshakedone.bind(this);
|
2012-07-07 20:20:23 +00:00
|
|
|
this.ssl.onclienthello = onclienthello.bind(this);
|
|
|
|
this.ssl.onnewsession = onnewsession.bind(this);
|
2012-10-08 00:18:30 +00:00
|
|
|
this.ssl.lastHandshakeTime = 0;
|
2012-02-15 18:26:43 +00:00
|
|
|
this.ssl.handshakes = 0;
|
|
|
|
}
|
|
|
|
|
2011-07-28 15:52:04 +00:00
|
|
|
if (process.features.tls_sni) {
|
|
|
|
if (this._isServer && options.SNICallback) {
|
|
|
|
this.ssl.setSNICallback(options.SNICallback);
|
|
|
|
}
|
|
|
|
this.servername = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.features.tls_npn && options.NPNProtocols) {
|
|
|
|
this.ssl.setNPNProtocols(options.NPNProtocols);
|
2011-04-14 03:53:39 +00:00
|
|
|
this.npnProtocol = null;
|
|
|
|
}
|
2010-12-06 02:19:18 +00:00
|
|
|
|
|
|
|
/* Acts as a r/w stream to the cleartext side of the stream. */
|
2013-01-31 17:20:49 +00:00
|
|
|
this.cleartext = new CleartextStream(this, options.cleartext);
|
2010-12-06 02:19:18 +00:00
|
|
|
|
|
|
|
/* Acts as a r/w stream to the encrypted side of the stream. */
|
2013-01-31 17:20:49 +00:00
|
|
|
this.encrypted = new EncryptedStream(this, options.encrypted);
|
|
|
|
|
|
|
|
/* Let streams know about each other */
|
|
|
|
this.cleartext._opposite = this.encrypted;
|
|
|
|
this.encrypted._opposite = this.cleartext;
|
2010-12-06 02:19:18 +00:00
|
|
|
|
|
|
|
process.nextTick(function() {
|
2011-07-14 09:52:43 +00:00
|
|
|
/* The Connection may be destroyed by an abort call */
|
|
|
|
if (self.ssl) {
|
|
|
|
self.ssl.start();
|
|
|
|
}
|
2010-12-06 02:19:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(SecurePair, events.EventEmitter);
|
|
|
|
|
|
|
|
|
|
|
|
exports.createSecurePair = function(credentials,
|
|
|
|
isServer,
|
|
|
|
requestCert,
|
|
|
|
rejectUnauthorized) {
|
|
|
|
var pair = new SecurePair(credentials,
|
|
|
|
isServer,
|
|
|
|
requestCert,
|
|
|
|
rejectUnauthorized);
|
|
|
|
return pair;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-05-06 23:48:44 +00:00
|
|
|
SecurePair.prototype.maybeInitFinished = function() {
|
|
|
|
if (this.ssl && !this._secureEstablished && this.ssl.isInitFinished()) {
|
2011-07-28 15:52:04 +00:00
|
|
|
if (process.features.tls_npn) {
|
2011-05-08 03:38:32 +00:00
|
|
|
this.npnProtocol = this.ssl.getNegotiatedProtocol();
|
2011-04-14 03:53:39 +00:00
|
|
|
}
|
2011-07-28 15:52:04 +00:00
|
|
|
|
|
|
|
if (process.features.tls_sni) {
|
|
|
|
this.servername = this.ssl.getServername();
|
|
|
|
}
|
|
|
|
|
2010-12-06 02:19:18 +00:00
|
|
|
this._secureEstablished = true;
|
|
|
|
debug('secure established');
|
|
|
|
this.emit('secure');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-05-06 23:48:44 +00:00
|
|
|
SecurePair.prototype.destroy = function() {
|
2013-01-31 17:20:49 +00:00
|
|
|
if (this._destroying) return;
|
2011-02-16 00:45:54 +00:00
|
|
|
|
2011-05-06 23:48:44 +00:00
|
|
|
if (!this._doneFlag) {
|
2013-01-31 17:20:49 +00:00
|
|
|
debug('SecurePair.destroy');
|
|
|
|
this._destroying = true;
|
|
|
|
|
|
|
|
// SecurePair should be destroyed only after it's streams
|
|
|
|
this.cleartext.destroy();
|
|
|
|
this.encrypted.destroy();
|
|
|
|
|
2011-05-06 23:48:44 +00:00
|
|
|
this._doneFlag = true;
|
|
|
|
this.ssl.error = null;
|
|
|
|
this.ssl.close();
|
|
|
|
this.ssl = null;
|
2010-12-06 02:19:18 +00:00
|
|
|
}
|
|
|
|
};
|
2011-12-27 08:33:23 +00:00
|
|
|
|
2010-12-06 02:19:18 +00:00
|
|
|
|
2011-05-06 23:48:44 +00:00
|
|
|
SecurePair.prototype.error = function() {
|
2013-03-20 13:35:38 +00:00
|
|
|
var err = this.ssl.error;
|
|
|
|
this.ssl.error = null;
|
|
|
|
|
2011-12-27 08:33:23 +00:00
|
|
|
if (!this._secureEstablished) {
|
2013-03-20 13:35:38 +00:00
|
|
|
if (!err) {
|
|
|
|
err = new Error('socket hang up');
|
|
|
|
err.code = 'ECONNRESET';
|
2011-12-17 17:09:16 +00:00
|
|
|
}
|
2011-05-06 23:48:44 +00:00
|
|
|
this.destroy();
|
2013-03-20 13:35:38 +00:00
|
|
|
this.emit('error', err);
|
|
|
|
} else if (this._isServer &&
|
|
|
|
this._rejectUnauthorized &&
|
|
|
|
/peer did not return a certificate/.test(err.message)) {
|
|
|
|
// Not really an error.
|
|
|
|
this.destroy();
|
2010-12-06 02:19:18 +00:00
|
|
|
} else {
|
2013-03-20 13:35:38 +00:00
|
|
|
this.cleartext.emit('error', err);
|
2010-12-06 02:19:18 +00:00
|
|
|
}
|
2013-03-20 13:35:38 +00:00
|
|
|
return err;
|
2010-12-06 02:19:18 +00:00
|
|
|
};
|
|
|
|
|
2010-12-01 21:00:04 +00:00
|
|
|
// TODO: support anonymous (nocert) and PSK
|
|
|
|
|
|
|
|
|
2010-12-04 01:07:09 +00:00
|
|
|
// 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
|
2012-11-01 08:29:33 +00:00
|
|
|
// true.
|
2010-12-04 01:07:09 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
2010-12-01 21:00:04 +00:00
|
|
|
// Options:
|
2010-12-04 01:07:09 +00:00
|
|
|
// - requestCert. Send verify request. Default to false.
|
2012-11-01 08:29:33 +00:00
|
|
|
// - rejectUnauthorized. Boolean, default to true.
|
2010-12-01 21:00:04 +00:00
|
|
|
// - key. string.
|
|
|
|
// - cert: string.
|
|
|
|
// - ca: string or array of strings.
|
|
|
|
//
|
2010-12-08 20:05:19 +00:00
|
|
|
// emit 'secureConnection'
|
|
|
|
// function (cleartextStream, encryptedStream) { }
|
|
|
|
//
|
|
|
|
// 'cleartextStream' has the boolean property 'authorized' to determine if
|
|
|
|
// it was verified by the CA. If 'authorized' is false, a property
|
|
|
|
// 'authorizationError' is set on cleartextStream and has the possible
|
|
|
|
// values:
|
2010-12-01 21:00:04 +00:00
|
|
|
//
|
|
|
|
// "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"
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// TODO:
|
|
|
|
// cleartext.credentials (by mirroring from pair object)
|
|
|
|
// cleartext.getCertificate() (by mirroring from pair.credentials.context)
|
2010-12-02 04:59:06 +00:00
|
|
|
function Server(/* [options], listener */) {
|
2010-12-01 21:00:04 +00:00
|
|
|
var options, listener;
|
2010-12-02 04:59:06 +00:00
|
|
|
if (typeof arguments[0] == 'object') {
|
2010-12-01 21:00:04 +00:00
|
|
|
options = arguments[0];
|
|
|
|
listener = arguments[1];
|
2010-12-02 04:59:06 +00:00
|
|
|
} else if (typeof arguments[0] == 'function') {
|
2010-12-01 21:00:04 +00:00
|
|
|
options = {};
|
|
|
|
listener = arguments[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(this instanceof Server)) return new Server(options, listener);
|
|
|
|
|
2012-02-18 23:01:35 +00:00
|
|
|
this._contexts = [];
|
|
|
|
|
2010-12-01 21:00:04 +00:00
|
|
|
var self = this;
|
|
|
|
|
2011-05-19 19:42:13 +00:00
|
|
|
// Handle option defaults:
|
|
|
|
this.setOptions(options);
|
|
|
|
|
2012-08-29 20:53:00 +00:00
|
|
|
if (!self.pfx && (!self.cert || !self.key)) {
|
|
|
|
throw new Error('Missing PFX or certificate + private key.');
|
|
|
|
}
|
|
|
|
|
2011-05-19 19:42:13 +00:00
|
|
|
var sharedCreds = crypto.createCredentials({
|
2012-05-15 20:03:43 +00:00
|
|
|
pfx: self.pfx,
|
2011-05-19 19:42:13 +00:00
|
|
|
key: self.key,
|
2011-10-26 17:34:56 +00:00
|
|
|
passphrase: self.passphrase,
|
2011-05-19 19:42:13 +00:00
|
|
|
cert: self.cert,
|
|
|
|
ca: self.ca,
|
2012-08-21 20:27:13 +00:00
|
|
|
ciphers: self.ciphers || DEFAULT_CIPHERS,
|
2011-05-19 19:42:13 +00:00
|
|
|
secureProtocol: self.secureProtocol,
|
2011-05-20 17:27:39 +00:00
|
|
|
secureOptions: self.secureOptions,
|
2011-09-21 04:19:22 +00:00
|
|
|
crl: self.crl,
|
|
|
|
sessionIdContext: self.sessionIdContext
|
2011-05-19 19:42:13 +00:00
|
|
|
});
|
|
|
|
|
2012-12-03 17:29:01 +00:00
|
|
|
var timeout = options.handshakeTimeout || (120 * 1000);
|
|
|
|
|
|
|
|
if (typeof timeout !== 'number') {
|
|
|
|
throw new TypeError('handshakeTimeout must be a number');
|
|
|
|
}
|
|
|
|
|
2010-12-01 21:00:04 +00:00
|
|
|
// constructor call
|
2010-12-02 04:59:06 +00:00
|
|
|
net.Server.call(this, function(socket) {
|
2011-05-19 19:42:13 +00:00
|
|
|
var creds = crypto.createCredentials(null, sharedCreds.context);
|
2010-12-01 21:00:04 +00:00
|
|
|
|
2010-12-06 02:19:18 +00:00
|
|
|
var pair = new SecurePair(creds,
|
|
|
|
true,
|
|
|
|
self.requestCert,
|
2011-04-14 03:53:39 +00:00
|
|
|
self.rejectUnauthorized,
|
2011-07-28 15:52:04 +00:00
|
|
|
{
|
2012-07-07 20:20:23 +00:00
|
|
|
server: self,
|
2011-07-28 15:52:04 +00:00
|
|
|
NPNProtocols: self.NPNProtocols,
|
2013-01-31 17:20:49 +00:00
|
|
|
SNICallback: self.SNICallback,
|
|
|
|
|
|
|
|
// Stream options
|
|
|
|
cleartext: self._cleartext,
|
|
|
|
encrypted: self._encrypted
|
2011-07-28 15:52:04 +00:00
|
|
|
});
|
2010-12-01 21:00:04 +00:00
|
|
|
|
2011-01-07 18:57:39 +00:00
|
|
|
var cleartext = pipe(pair, socket);
|
|
|
|
cleartext._controlReleased = false;
|
2010-12-19 00:38:32 +00:00
|
|
|
|
2012-12-03 17:29:01 +00:00
|
|
|
function listener() {
|
|
|
|
pair.emit('error', new Error('TLS handshake timeout'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout > 0) {
|
|
|
|
socket.setTimeout(timeout, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
pair.once('secure', function() {
|
|
|
|
socket.setTimeout(0, listener);
|
|
|
|
|
2010-12-08 20:05:19 +00:00
|
|
|
pair.cleartext.authorized = false;
|
2011-04-14 03:53:39 +00:00
|
|
|
pair.cleartext.npnProtocol = pair.npnProtocol;
|
2011-07-28 15:52:04 +00:00
|
|
|
pair.cleartext.servername = pair.servername;
|
|
|
|
|
2010-12-04 01:07:09 +00:00
|
|
|
if (!self.requestCert) {
|
2011-01-07 18:57:39 +00:00
|
|
|
cleartext._controlReleased = true;
|
2010-12-08 20:05:19 +00:00
|
|
|
self.emit('secureConnection', pair.cleartext, pair.encrypted);
|
2010-12-04 01:07:09 +00:00
|
|
|
} else {
|
2011-05-06 23:48:44 +00:00
|
|
|
var verifyError = pair.ssl.verifyError();
|
2010-12-04 01:07:09 +00:00
|
|
|
if (verifyError) {
|
2012-07-20 17:10:23 +00:00
|
|
|
pair.cleartext.authorizationError = verifyError.message;
|
2010-12-08 20:05:19 +00:00
|
|
|
|
2010-12-08 19:22:08 +00:00
|
|
|
if (self.rejectUnauthorized) {
|
|
|
|
socket.destroy();
|
2011-05-06 23:48:44 +00:00
|
|
|
pair.destroy();
|
2010-12-08 19:22:08 +00:00
|
|
|
} else {
|
2011-01-07 18:57:39 +00:00
|
|
|
cleartext._controlReleased = true;
|
2010-12-08 20:05:19 +00:00
|
|
|
self.emit('secureConnection', pair.cleartext, pair.encrypted);
|
2010-12-08 19:22:08 +00:00
|
|
|
}
|
2010-12-01 21:00:04 +00:00
|
|
|
} else {
|
2010-12-08 20:05:19 +00:00
|
|
|
pair.cleartext.authorized = true;
|
2011-01-07 18:57:39 +00:00
|
|
|
cleartext._controlReleased = true;
|
2010-12-08 20:05:19 +00:00
|
|
|
self.emit('secureConnection', pair.cleartext, pair.encrypted);
|
2010-12-01 21:00:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-12-17 17:09:16 +00:00
|
|
|
pair.on('error', function(err) {
|
2012-10-07 23:22:44 +00:00
|
|
|
self.emit('clientError', err, this);
|
2011-12-17 17:09:16 +00:00
|
|
|
});
|
2010-12-01 21:00:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (listener) {
|
2010-12-08 20:05:19 +00:00
|
|
|
this.on('secureConnection', listener);
|
2010-12-01 21:00:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-06 02:19:18 +00:00
|
|
|
util.inherits(Server, net.Server);
|
2010-12-01 21:00:04 +00:00
|
|
|
exports.Server = Server;
|
2010-12-02 04:59:06 +00:00
|
|
|
exports.createServer = function(options, listener) {
|
2010-12-01 21:00:04 +00:00
|
|
|
return new Server(options, listener);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-02 04:59:06 +00:00
|
|
|
Server.prototype.setOptions = function(options) {
|
2010-12-04 01:07:09 +00:00
|
|
|
if (typeof options.requestCert == 'boolean') {
|
|
|
|
this.requestCert = options.requestCert;
|
|
|
|
} else {
|
|
|
|
this.requestCert = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof options.rejectUnauthorized == 'boolean') {
|
|
|
|
this.rejectUnauthorized = options.rejectUnauthorized;
|
2010-12-01 21:00:04 +00:00
|
|
|
} else {
|
2010-12-04 01:07:09 +00:00
|
|
|
this.rejectUnauthorized = false;
|
2010-12-01 21:00:04 +00:00
|
|
|
}
|
|
|
|
|
2012-05-13 19:38:23 +00:00
|
|
|
if (options.pfx) this.pfx = options.pfx;
|
2010-12-01 21:00:04 +00:00
|
|
|
if (options.key) this.key = options.key;
|
2011-10-26 17:34:56 +00:00
|
|
|
if (options.passphrase) this.passphrase = options.passphrase;
|
2010-12-01 21:00:04 +00:00
|
|
|
if (options.cert) this.cert = options.cert;
|
|
|
|
if (options.ca) this.ca = options.ca;
|
2011-04-02 04:53:07 +00:00
|
|
|
if (options.secureProtocol) this.secureProtocol = options.secureProtocol;
|
2010-11-23 18:00:42 +00:00
|
|
|
if (options.crl) this.crl = options.crl;
|
2011-04-05 03:27:20 +00:00
|
|
|
if (options.ciphers) this.ciphers = options.ciphers;
|
2012-02-10 05:58:58 +00:00
|
|
|
var secureOptions = options.secureOptions || 0;
|
2012-03-04 07:48:57 +00:00
|
|
|
if (options.honorCipherOrder) {
|
|
|
|
secureOptions |= constants.SSL_OP_CIPHER_SERVER_PREFERENCE;
|
|
|
|
}
|
2012-02-10 05:58:58 +00:00
|
|
|
if (secureOptions) this.secureOptions = secureOptions;
|
2011-04-14 03:53:39 +00:00
|
|
|
if (options.NPNProtocols) convertNPNProtocols(options.NPNProtocols, this);
|
2011-07-28 15:52:04 +00:00
|
|
|
if (options.SNICallback) {
|
|
|
|
this.SNICallback = options.SNICallback;
|
|
|
|
} else {
|
|
|
|
this.SNICallback = this.SNICallback.bind(this);
|
|
|
|
}
|
2011-09-21 04:19:22 +00:00
|
|
|
if (options.sessionIdContext) {
|
|
|
|
this.sessionIdContext = options.sessionIdContext;
|
|
|
|
} else if (this.requestCert) {
|
|
|
|
this.sessionIdContext = crypto.createHash('md5')
|
|
|
|
.update(process.argv.join(' '))
|
|
|
|
.digest('hex');
|
|
|
|
}
|
2013-01-31 17:20:49 +00:00
|
|
|
if (options.cleartext) this.cleartext = options.cleartext;
|
|
|
|
if (options.encrypted) this.encrypted = options.encrypted;
|
2011-07-28 15:52:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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')
|
|
|
|
.replace(/\*/g, '.*') +
|
|
|
|
'$');
|
|
|
|
this._contexts.push([re, crypto.createCredentials(credentials).context]);
|
|
|
|
};
|
|
|
|
|
|
|
|
Server.prototype.SNICallback = function(servername) {
|
|
|
|
var ctx;
|
|
|
|
|
|
|
|
this._contexts.some(function(elem) {
|
|
|
|
if (servername.match(elem[0]) !== null) {
|
|
|
|
ctx = elem[1];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ctx;
|
2010-12-01 21:00:04 +00:00
|
|
|
};
|
|
|
|
|
2010-12-09 08:10:16 +00:00
|
|
|
|
2010-12-09 08:35:16 +00:00
|
|
|
// Target API:
|
|
|
|
//
|
2011-11-01 15:23:30 +00:00
|
|
|
// var s = tls.connect({port: 8000, host: "google.com"}, function() {
|
2010-12-09 08:35:16 +00:00
|
|
|
// if (!s.authorized) {
|
|
|
|
// s.destroy();
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // s.socket;
|
|
|
|
//
|
|
|
|
// s.end("hello world\n");
|
|
|
|
// });
|
|
|
|
//
|
2011-02-23 12:43:13 +00:00
|
|
|
//
|
2012-08-30 15:11:05 +00:00
|
|
|
function normalizeConnectArgs(listArgs) {
|
|
|
|
var args = net._normalizeConnectArgs(listArgs);
|
|
|
|
var options = args[0];
|
|
|
|
var cb = args[1];
|
|
|
|
|
|
|
|
if (typeof listArgs[1] === 'object') {
|
|
|
|
options = util._extend(options, listArgs[1]);
|
|
|
|
} else if (typeof listArgs[2] === 'object') {
|
|
|
|
options = util._extend(options, listArgs[2]);
|
2011-11-01 15:23:30 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 15:11:05 +00:00
|
|
|
return (cb) ? [options, cb] : [options];
|
|
|
|
}
|
2012-02-20 19:32:10 +00:00
|
|
|
|
2012-08-30 15:11:05 +00:00
|
|
|
exports.connect = function(/* [port, host], options, cb */) {
|
|
|
|
var args = normalizeConnectArgs(arguments);
|
|
|
|
var options = args[0];
|
|
|
|
var cb = args[1];
|
2010-12-09 08:10:16 +00:00
|
|
|
|
2012-08-30 13:14:37 +00:00
|
|
|
var defaults = {
|
|
|
|
rejectUnauthorized: '0' !== process.env.NODE_TLS_REJECT_UNAUTHORIZED
|
|
|
|
};
|
|
|
|
options = util._extend(defaults, options || {});
|
|
|
|
|
2012-01-09 01:28:49 +00:00
|
|
|
var socket = options.socket ? options.socket : new net.Stream();
|
2010-12-09 08:35:16 +00:00
|
|
|
|
|
|
|
var sslcontext = crypto.createCredentials(options);
|
|
|
|
|
2011-04-14 03:53:39 +00:00
|
|
|
convertNPNProtocols(options.NPNProtocols, this);
|
2012-07-20 17:43:12 +00:00
|
|
|
var hostname = options.servername || options.host || 'localhost',
|
2012-07-11 19:54:20 +00:00
|
|
|
pair = new SecurePair(sslcontext, false, true,
|
2011-12-07 13:47:06 +00:00
|
|
|
options.rejectUnauthorized === true ? true : false,
|
2011-07-28 15:52:04 +00:00
|
|
|
{
|
|
|
|
NPNProtocols: this.NPNProtocols,
|
2013-01-31 17:20:49 +00:00
|
|
|
servername: hostname,
|
|
|
|
cleartext: options.cleartext,
|
|
|
|
encrypted: options.encrypted
|
2011-07-28 15:52:04 +00:00
|
|
|
});
|
2010-12-09 08:35:16 +00:00
|
|
|
|
2011-08-29 18:09:01 +00:00
|
|
|
if (options.session) {
|
2012-10-13 01:41:30 +00:00
|
|
|
var session = options.session;
|
|
|
|
if (typeof session === 'string')
|
|
|
|
session = new Buffer(session, 'binary');
|
|
|
|
pair.ssl.setSession(session);
|
2011-08-29 18:09:01 +00:00
|
|
|
}
|
|
|
|
|
2011-01-07 18:57:39 +00:00
|
|
|
var cleartext = pipe(pair, socket);
|
2011-10-15 10:27:21 +00:00
|
|
|
if (cb) {
|
2012-11-26 00:50:04 +00:00
|
|
|
cleartext.once('secureConnect', cb);
|
2011-10-15 10:27:21 +00:00
|
|
|
}
|
2010-12-09 08:35:16 +00:00
|
|
|
|
2012-01-09 01:28:49 +00:00
|
|
|
if (!options.socket) {
|
2012-08-30 15:11:05 +00:00
|
|
|
var connect_opt = (options.path && !options.port) ? {path: options.path} : {
|
2012-02-23 15:37:49 +00:00
|
|
|
port: options.port,
|
|
|
|
host: options.host,
|
|
|
|
localAddress: options.localAddress
|
2012-08-30 15:11:05 +00:00
|
|
|
};
|
|
|
|
socket.connect(connect_opt);
|
2012-01-09 01:28:49 +00:00
|
|
|
}
|
2010-12-09 08:35:16 +00:00
|
|
|
|
|
|
|
pair.on('secure', function() {
|
2011-05-06 23:48:44 +00:00
|
|
|
var verifyError = pair.ssl.verifyError();
|
2010-12-09 08:35:16 +00:00
|
|
|
|
2011-04-14 03:53:39 +00:00
|
|
|
cleartext.npnProtocol = pair.npnProtocol;
|
|
|
|
|
2012-07-11 19:54:20 +00:00
|
|
|
// Verify that server's identity matches it's certificate's names
|
|
|
|
if (!verifyError) {
|
|
|
|
var validCert = checkServerIdentity(hostname,
|
|
|
|
pair.cleartext.getPeerCertificate());
|
|
|
|
if (!validCert) {
|
|
|
|
verifyError = new Error('Hostname/IP doesn\'t match certificate\'s ' +
|
|
|
|
'altnames');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-09 10:35:16 +00:00
|
|
|
if (verifyError) {
|
|
|
|
cleartext.authorized = false;
|
2012-07-11 19:54:20 +00:00
|
|
|
cleartext.authorizationError = verifyError.message;
|
2011-12-07 13:47:06 +00:00
|
|
|
|
|
|
|
if (pair._rejectUnauthorized) {
|
|
|
|
cleartext.emit('error', verifyError);
|
|
|
|
pair.destroy();
|
|
|
|
} else {
|
|
|
|
cleartext.emit('secureConnect');
|
|
|
|
}
|
2010-12-09 10:35:16 +00:00
|
|
|
} else {
|
|
|
|
cleartext.authorized = true;
|
2011-12-07 13:47:06 +00:00
|
|
|
cleartext.emit('secureConnect');
|
2010-12-09 08:35:16 +00:00
|
|
|
}
|
|
|
|
});
|
2011-12-17 17:09:16 +00:00
|
|
|
pair.on('error', function(err) {
|
|
|
|
cleartext.emit('error', err);
|
|
|
|
});
|
2010-12-09 08:35:16 +00:00
|
|
|
|
2011-01-07 18:57:39 +00:00
|
|
|
cleartext._controlReleased = true;
|
2010-12-09 08:35:16 +00:00
|
|
|
return cleartext;
|
|
|
|
};
|
2011-01-07 18:57:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
function pipe(pair, socket) {
|
|
|
|
pair.encrypted.pipe(socket);
|
|
|
|
socket.pipe(pair.encrypted);
|
|
|
|
|
2011-02-09 01:29:33 +00:00
|
|
|
pair.fd = socket.fd;
|
2011-01-07 18:57:39 +00:00
|
|
|
var cleartext = pair.cleartext;
|
|
|
|
cleartext.socket = socket;
|
|
|
|
cleartext.encrypted = pair.encrypted;
|
|
|
|
cleartext.authorized = false;
|
|
|
|
|
2013-02-11 23:46:19 +00:00
|
|
|
// cycle the data whenever the socket drains, so that
|
|
|
|
// we can pull some more into it. normally this would
|
|
|
|
// be handled by the fact that pipe() triggers read() calls
|
|
|
|
// on writable.drain, but CryptoStreams are a bit more
|
|
|
|
// complicated. Since the encrypted side actually gets
|
|
|
|
// its data from the cleartext side, we have to give it a
|
|
|
|
// light kick to get in motion again.
|
|
|
|
socket.on('drain', function() {
|
2013-02-19 19:37:48 +00:00
|
|
|
if (pair.encrypted._pending)
|
|
|
|
pair.encrypted._writePending();
|
|
|
|
if (pair.cleartext._pending)
|
|
|
|
pair.cleartext._writePending();
|
2013-02-11 23:46:19 +00:00
|
|
|
pair.encrypted.read(0);
|
|
|
|
pair.cleartext.read(0);
|
|
|
|
});
|
|
|
|
|
2011-01-07 18:57:39 +00:00
|
|
|
function onerror(e) {
|
|
|
|
if (cleartext._controlReleased) {
|
|
|
|
cleartext.emit('error', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onclose() {
|
|
|
|
socket.removeListener('error', onerror);
|
2011-05-14 19:17:13 +00:00
|
|
|
socket.removeListener('timeout', ontimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ontimeout() {
|
|
|
|
cleartext.emit('timeout');
|
2011-01-07 18:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('error', onerror);
|
|
|
|
socket.on('close', onclose);
|
2011-05-14 19:17:13 +00:00
|
|
|
socket.on('timeout', ontimeout);
|
2011-01-07 18:57:39 +00:00
|
|
|
|
|
|
|
return cleartext;
|
|
|
|
}
|