2017-01-03 22:16:48 +01: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.
|
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-07-15 02:30:28 +02:00
|
|
|
const internalUtil = require('internal/util');
|
|
|
|
internalUtil.assertCrypto();
|
2016-03-09 00:31:31 +01:00
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const assert = require('assert');
|
2017-10-07 16:50:42 +02:00
|
|
|
const { Buffer } = require('buffer');
|
2017-02-16 23:30:29 +01:00
|
|
|
const common = require('_tls_common');
|
2017-10-07 16:50:42 +02:00
|
|
|
const { Connection } = process.binding('crypto');
|
2015-09-17 00:45:29 +02:00
|
|
|
const EventEmitter = require('events');
|
2015-01-21 17:36:59 +01:00
|
|
|
const stream = require('stream');
|
2017-10-07 16:50:42 +02:00
|
|
|
const { Timer } = process.binding('timer_wrap');
|
2015-01-21 17:36:59 +01:00
|
|
|
const tls = require('tls');
|
|
|
|
const util = require('util');
|
2017-02-16 23:30:29 +01:00
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const debug = util.debuglog('tls-legacy');
|
2013-06-13 15:36:00 +02:00
|
|
|
|
|
|
|
function SlabBuffer() {
|
|
|
|
this.create();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SlabBuffer.prototype.create = function create() {
|
|
|
|
this.isFull = false;
|
2016-01-26 00:00:06 +01:00
|
|
|
this.pool = Buffer.allocUnsafe(tls.SLAB_BUFFER_SIZE);
|
2013-06-13 15:36:00 +02:00
|
|
|
this.offset = 0;
|
|
|
|
this.remaining = this.pool.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
SlabBuffer.prototype.use = function use(context, fn, size) {
|
|
|
|
if (this.remaining === 0) {
|
|
|
|
this.isFull = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var actualSize = this.remaining;
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (size !== null) actualSize = Math.min(size, actualSize);
|
2013-06-13 15:36:00 +02:00
|
|
|
|
|
|
|
var bytes = fn.call(context, this.pool, this.offset, actualSize);
|
|
|
|
if (bytes > 0) {
|
|
|
|
this.offset += bytes;
|
|
|
|
this.remaining -= bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(this.remaining >= 0);
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var slabBuffer = null;
|
|
|
|
|
|
|
|
|
|
|
|
// Base class of both CleartextStream and EncryptedStream
|
|
|
|
function CryptoStream(pair, options) {
|
|
|
|
stream.Duplex.call(this, options);
|
|
|
|
|
|
|
|
this.pair = pair;
|
|
|
|
this._pending = null;
|
|
|
|
this._pendingEncoding = '';
|
|
|
|
this._pendingCallback = null;
|
|
|
|
this._doneFlag = false;
|
|
|
|
this._retryAfterPartial = false;
|
|
|
|
this._halfRead = false;
|
|
|
|
this._sslOutCb = null;
|
|
|
|
this._resumingSession = false;
|
|
|
|
this._reading = true;
|
|
|
|
this._destroyed = false;
|
|
|
|
this._ended = false;
|
|
|
|
this._finished = false;
|
|
|
|
this._opposite = null;
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (slabBuffer === null) slabBuffer = new SlabBuffer();
|
2013-06-13 15:36:00 +02:00
|
|
|
this._buffer = slabBuffer;
|
|
|
|
|
|
|
|
this.once('finish', onCryptoStreamFinish);
|
|
|
|
|
|
|
|
// net.Socket calls .onend too
|
|
|
|
this.once('end', onCryptoStreamEnd);
|
|
|
|
}
|
|
|
|
util.inherits(CryptoStream, stream.Duplex);
|
|
|
|
|
|
|
|
|
|
|
|
function onCryptoStreamFinish() {
|
|
|
|
this._finished = true;
|
|
|
|
|
|
|
|
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.
|
stream_base: introduce StreamBase
StreamBase is an improved way to write C++ streams. The class itself is
for separting `StreamWrap` (with the methods like `.writeAsciiString`,
`.writeBuffer`, `.writev`, etc) from the `HandleWrap` class, making
possible to write abstract C++ streams that are not bound to any uv
socket.
The following methods are important part of the abstraction (which
mimics libuv's stream API):
* Events:
* `OnAlloc(size_t size, uv_buf_t*)`
* `OnRead(ssize_t nread, const uv_buf_t*, uv_handle_type pending)`
* `OnAfterWrite(WriteWrap*)`
* Wrappers:
* `DoShutdown(ShutdownWrap*)`
* `DoTryWrite(uv_buf_t** bufs, size_t* count)`
* `DoWrite(WriteWrap*, uv_buf_t*, size_t count, uv_stream_t* handle)`
* `Error()`
* `ClearError()`
The implementation should provide all of these methods, thus providing
the access to the underlying resource (be it uv handle, TLS socket, or
anything else).
A C++ stream may consume the input of another stream by replacing the
event callbacks and proxying the writes. This kind of API is actually
used now for the TLSWrap implementation, making it possible to wrap TLS
stream into another TLS stream. Thus legacy API calls are no longer
required in `_tls_wrap.js`.
PR-URL: https://github.com/iojs/io.js/pull/840
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
2015-02-22 19:59:07 +01:00
|
|
|
if (this.pair.ssl.shutdownSSL() !== 1) {
|
2013-06-13 15:36:00 +02:00
|
|
|
if (this.pair.ssl && this.pair.ssl.error)
|
|
|
|
return this.pair.error();
|
|
|
|
|
stream_base: introduce StreamBase
StreamBase is an improved way to write C++ streams. The class itself is
for separting `StreamWrap` (with the methods like `.writeAsciiString`,
`.writeBuffer`, `.writev`, etc) from the `HandleWrap` class, making
possible to write abstract C++ streams that are not bound to any uv
socket.
The following methods are important part of the abstraction (which
mimics libuv's stream API):
* Events:
* `OnAlloc(size_t size, uv_buf_t*)`
* `OnRead(ssize_t nread, const uv_buf_t*, uv_handle_type pending)`
* `OnAfterWrite(WriteWrap*)`
* Wrappers:
* `DoShutdown(ShutdownWrap*)`
* `DoTryWrite(uv_buf_t** bufs, size_t* count)`
* `DoWrite(WriteWrap*, uv_buf_t*, size_t count, uv_stream_t* handle)`
* `Error()`
* `ClearError()`
The implementation should provide all of these methods, thus providing
the access to the underlying resource (be it uv handle, TLS socket, or
anything else).
A C++ stream may consume the input of another stream by replacing the
event callbacks and proxying the writes. This kind of API is actually
used now for the TLSWrap implementation, making it possible to wrap TLS
stream into another TLS stream. Thus legacy API calls are no longer
required in `_tls_wrap.js`.
PR-URL: https://github.com/iojs/io.js/pull/840
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
2015-02-22 19:59:07 +01:00
|
|
|
this.pair.ssl.shutdownSSL();
|
2013-06-13 15:36:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.pair.ssl && this.pair.ssl.error)
|
|
|
|
return this.pair.error();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
debug('encrypted.onfinish');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to read just to get sure that we won't miss EOF
|
|
|
|
if (this._opposite.readable) this._opposite.read(0);
|
|
|
|
|
|
|
|
if (this._opposite._ended) {
|
|
|
|
this._done();
|
|
|
|
|
|
|
|
// No half-close, sorry
|
|
|
|
if (this === this.pair.cleartext) this._opposite._done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onCryptoStreamEnd() {
|
|
|
|
this._ended = true;
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.onend');
|
|
|
|
} else {
|
|
|
|
debug('encrypted.onend');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE: Called once `this._opposite` is set.
|
|
|
|
CryptoStream.prototype.init = function init() {
|
|
|
|
var self = this;
|
|
|
|
this._opposite.on('sslOutEnd', function() {
|
|
|
|
if (self._sslOutCb) {
|
|
|
|
var cb = self._sslOutCb;
|
|
|
|
self._sslOutCb = null;
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-16 00:02:43 +02:00
|
|
|
CryptoStream.prototype._write = function _write(data, encoding, cb) {
|
2015-01-29 02:05:53 +01:00
|
|
|
assert(this._pending === null);
|
2013-06-13 15:36:00 +02: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.
|
|
|
|
//
|
|
|
|
// TODO(indutny): Remove magic number, use watermark based limits
|
|
|
|
if (!this._resumingSession &&
|
|
|
|
this._opposite._internallyPendingBytes() < 128 * 1024) {
|
|
|
|
// Write current buffer now
|
|
|
|
var written;
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.write called with %d bytes', data.length);
|
|
|
|
written = this.pair.ssl.clearIn(data, 0, data.length);
|
|
|
|
} else {
|
|
|
|
debug('encrypted.write called with %d bytes', data.length);
|
|
|
|
written = this.pair.ssl.encIn(data, 0, data.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle and report errors
|
|
|
|
if (this.pair.ssl && this.pair.ssl.error) {
|
|
|
|
return cb(this.pair.error(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2015-04-23 08:25:15 +02:00
|
|
|
// Get ALPN, NPN and Server name when ready
|
2013-06-13 15:36:00 +02:00
|
|
|
this.pair.maybeInitFinished();
|
|
|
|
|
|
|
|
// Whole buffer was written
|
|
|
|
if (written === data.length) {
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.write succeed with ' + written + ' bytes');
|
|
|
|
} else {
|
|
|
|
debug('encrypted.write succeed with ' + written + ' bytes');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke callback only when all data read from opposite stream
|
|
|
|
if (this._opposite._halfRead) {
|
2015-01-29 02:05:53 +01:00
|
|
|
assert(this._sslOutCb === null);
|
2013-06-13 15:36:00 +02:00
|
|
|
this._sslOutCb = cb;
|
|
|
|
} else {
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (written !== 0 && written !== -1) {
|
|
|
|
assert(!this._retryAfterPartial);
|
|
|
|
this._retryAfterPartial = true;
|
|
|
|
this._write(data.slice(written), encoding, cb);
|
|
|
|
this._retryAfterPartial = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
this._pendingEncoding = encoding;
|
|
|
|
this._pendingCallback = cb;
|
|
|
|
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.write queued with %d bytes', data.length);
|
|
|
|
} else {
|
|
|
|
debug('encrypted.write queued with %d bytes', data.length);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-16 00:02:43 +02:00
|
|
|
CryptoStream.prototype._writePending = function _writePending() {
|
2016-01-12 22:04:50 +01:00
|
|
|
const data = this._pending;
|
|
|
|
const encoding = this._pendingEncoding;
|
|
|
|
const cb = this._pendingCallback;
|
2013-06-13 15:36:00 +02:00
|
|
|
|
|
|
|
this._pending = null;
|
|
|
|
this._pendingEncoding = '';
|
|
|
|
this._pendingCallback = null;
|
|
|
|
this._write(data, encoding, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-16 00:02:43 +02:00
|
|
|
CryptoStream.prototype._read = function _read(size) {
|
2013-06-13 15:36:00 +02:00
|
|
|
// XXX: EOF?!
|
|
|
|
if (!this.pair.ssl) return this.push(null);
|
|
|
|
|
|
|
|
// Wait for session to be resumed
|
|
|
|
// Mark that we're done reading, but don't provide data or EOF
|
|
|
|
if (this._resumingSession || !this._reading) return this.push('');
|
|
|
|
|
|
|
|
var out;
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.read called with %d bytes', size);
|
|
|
|
out = this.pair.ssl.clearOut;
|
|
|
|
} else {
|
|
|
|
debug('encrypted.read called with %d bytes', size);
|
|
|
|
out = this.pair.ssl.encOut;
|
|
|
|
}
|
|
|
|
|
2016-01-12 22:04:50 +01:00
|
|
|
var bytesRead = 0;
|
|
|
|
const start = this._buffer.offset;
|
|
|
|
var last = start;
|
2013-06-13 15:36:00 +02:00
|
|
|
do {
|
2013-12-07 06:27:18 +01:00
|
|
|
assert(last === this._buffer.offset);
|
2013-10-25 20:26:05 +02:00
|
|
|
var read = this._buffer.use(this.pair.ssl, out, size - bytesRead);
|
2013-06-13 15:36:00 +02:00
|
|
|
if (read > 0) {
|
|
|
|
bytesRead += read;
|
|
|
|
}
|
2013-12-07 06:27:18 +01:00
|
|
|
last = this._buffer.offset;
|
2013-06-13 15:36:00 +02:00
|
|
|
|
|
|
|
// Handle and report errors
|
|
|
|
if (this.pair.ssl && this.pair.ssl.error) {
|
|
|
|
this.pair.error();
|
|
|
|
break;
|
|
|
|
}
|
2013-08-21 18:40:10 +02:00
|
|
|
} while (read > 0 &&
|
|
|
|
!this._buffer.isFull &&
|
|
|
|
bytesRead < size &&
|
|
|
|
this.pair.ssl !== null);
|
2013-06-13 15:36:00 +02:00
|
|
|
|
2015-04-23 08:25:15 +02:00
|
|
|
// Get ALPN, NPN and Server name when ready
|
2013-12-07 06:27:18 +01:00
|
|
|
this.pair.maybeInitFinished();
|
|
|
|
|
2013-06-13 15:36:00 +02:00
|
|
|
// 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 %d bytes', bytesRead);
|
|
|
|
} else {
|
|
|
|
debug('encrypted.read succeed with %d bytes', bytesRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try writing pending data
|
2015-01-29 02:05:53 +01:00
|
|
|
if (this._pending !== null) this._writePending();
|
|
|
|
if (this._opposite._pending !== null) this._opposite._writePending();
|
2013-06-13 15:36:00 +02:00
|
|
|
|
|
|
|
if (bytesRead === 0) {
|
|
|
|
// EOF when cleartext has finished and we have nothing to read
|
2013-12-10 20:06:56 +01:00
|
|
|
if (this._opposite._finished && this._internallyPendingBytes() === 0 ||
|
|
|
|
this.pair.ssl && this.pair.ssl.receivedShutdown) {
|
2013-06-13 15:36:00 +02:00
|
|
|
// Perform graceful shutdown
|
|
|
|
this._done();
|
|
|
|
|
|
|
|
// No half-open, sorry!
|
2013-12-10 20:06:56 +01:00
|
|
|
if (this === this.pair.cleartext) {
|
2013-06-13 15:36:00 +02:00
|
|
|
this._opposite._done();
|
|
|
|
|
2013-12-10 20:06:56 +01:00
|
|
|
// EOF
|
|
|
|
this.push(null);
|
|
|
|
} else if (!this.pair.ssl || !this.pair.ssl.receivedShutdown) {
|
|
|
|
// EOF
|
|
|
|
this.push(null);
|
|
|
|
}
|
2013-06-13 15:36:00 +02:00
|
|
|
} else {
|
|
|
|
// Bail out
|
|
|
|
this.push('');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Give them requested data
|
|
|
|
this.push(pool.slice(start, start + bytesRead));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let users know that we've some internal data to read
|
|
|
|
var halfRead = this._internallyPendingBytes() !== 0;
|
|
|
|
|
|
|
|
// Smart check to avoid invoking 'sslOutEnd' in the most of the cases
|
|
|
|
if (this._halfRead !== halfRead) {
|
|
|
|
this._halfRead = halfRead;
|
|
|
|
|
|
|
|
// Notify listeners about internal data end
|
|
|
|
if (!halfRead) {
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.sslOutEnd');
|
|
|
|
} else {
|
|
|
|
debug('encrypted.sslOutEnd');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit('sslOutEnd');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CryptoStream.prototype.setTimeout = function(timeout, callback) {
|
|
|
|
if (this.socket) this.socket.setTimeout(timeout, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CryptoStream.prototype.setNoDelay = function(noDelay) {
|
|
|
|
if (this.socket) this.socket.setNoDelay(noDelay);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CryptoStream.prototype.setKeepAlive = function(enable, initialDelay) {
|
|
|
|
if (this.socket) this.socket.setKeepAlive(enable, initialDelay);
|
|
|
|
};
|
|
|
|
|
2016-05-15 07:29:19 +02:00
|
|
|
Object.defineProperty(CryptoStream.prototype, 'bytesWritten', {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
return this.socket ? this.socket.bytesWritten : 0;
|
|
|
|
}
|
2013-06-13 15:36:00 +02:00
|
|
|
});
|
|
|
|
|
2014-04-17 13:57:36 +02:00
|
|
|
CryptoStream.prototype.getPeerCertificate = function(detailed) {
|
|
|
|
if (this.pair.ssl) {
|
|
|
|
return common.translatePeerCertificate(
|
2017-07-05 18:21:40 +02:00
|
|
|
this.pair.ssl.getPeerCertificate(detailed));
|
2014-04-17 13:57:36 +02:00
|
|
|
}
|
2013-06-13 15:36:00 +02:00
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
CryptoStream.prototype.getCipher = function(err) {
|
|
|
|
if (this.pair.ssl) {
|
|
|
|
return this.pair.ssl.getCurrentCipher();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CryptoStream.prototype.end = function(chunk, encoding) {
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.end');
|
|
|
|
} else {
|
|
|
|
debug('encrypted.end');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write pending data first
|
2015-01-29 02:05:53 +01:00
|
|
|
if (this._pending !== null) this._writePending();
|
2013-06-13 15:36:00 +02:00
|
|
|
|
|
|
|
this.writable = false;
|
|
|
|
|
|
|
|
stream.Duplex.prototype.end.call(this, chunk, encoding);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CryptoStream.prototype.destroySoon = function(err) {
|
|
|
|
if (this === this.pair.cleartext) {
|
|
|
|
debug('cleartext.destroySoon');
|
|
|
|
} else {
|
|
|
|
debug('encrypted.destroySoon');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.writable)
|
|
|
|
this.end();
|
|
|
|
|
|
|
|
if (this._writableState.finished && this._opposite._ended) {
|
|
|
|
this.destroy();
|
|
|
|
} else {
|
|
|
|
// Wait for both `finish` and `end` events to ensure that all data that
|
|
|
|
// was written on this side was read from the other side.
|
|
|
|
var self = this;
|
2013-07-12 22:26:56 +02:00
|
|
|
var waiting = 1;
|
2017-04-28 02:27:05 +02:00
|
|
|
function finish() {
|
2013-06-13 15:36:00 +02:00
|
|
|
if (--waiting === 0) self.destroy();
|
2017-04-28 02:27:05 +02:00
|
|
|
}
|
2013-06-13 15:36:00 +02:00
|
|
|
this._opposite.once('end', finish);
|
2013-07-12 22:26:56 +02:00
|
|
|
if (!this._finished) {
|
|
|
|
this.once('finish', finish);
|
|
|
|
++waiting;
|
|
|
|
}
|
2013-06-13 15:36:00 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CryptoStream.prototype.destroy = function(err) {
|
|
|
|
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();
|
|
|
|
|
2015-05-04 20:40:25 +02:00
|
|
|
process.nextTick(destroyNT, this, err ? true : false);
|
2013-06-13 15:36:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-05-04 20:40:25 +02:00
|
|
|
function destroyNT(self, hadErr) {
|
2015-03-05 22:07:27 +01:00
|
|
|
// Force EOF
|
|
|
|
self.push(null);
|
|
|
|
|
|
|
|
// Emit 'close' event
|
2015-05-04 20:40:25 +02:00
|
|
|
self.emit('close', hadErr);
|
2015-03-05 22:07:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-13 15:36:00 +02:00
|
|
|
CryptoStream.prototype._done = function() {
|
|
|
|
this._doneFlag = true;
|
|
|
|
|
|
|
|
if (this === this.pair.encrypted && !this.pair._secureEstablished)
|
|
|
|
return this.pair.error();
|
|
|
|
|
|
|
|
if (this.pair.cleartext._doneFlag &&
|
|
|
|
this.pair.encrypted._doneFlag &&
|
|
|
|
!this.pair._doneFlag) {
|
|
|
|
// If both streams are done:
|
|
|
|
this.pair.destroy();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// readyState is deprecated. Don't use it.
|
2016-12-04 21:47:01 +01:00
|
|
|
// Deprecation Code: DEP0004
|
2013-06-13 15:36:00 +02:00
|
|
|
Object.defineProperty(CryptoStream.prototype, 'readyState', {
|
|
|
|
get: function() {
|
2016-04-26 22:27:08 +02:00
|
|
|
if (this.connecting) {
|
2013-06-13 15:36:00 +02:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function CleartextStream(pair, options) {
|
|
|
|
CryptoStream.call(this, pair, options);
|
|
|
|
|
|
|
|
// This is a fake kludge to support how the http impl sits
|
|
|
|
// on top of net Sockets
|
|
|
|
var self = this;
|
|
|
|
this._handle = {
|
|
|
|
readStop: function() {
|
|
|
|
self._reading = false;
|
|
|
|
},
|
|
|
|
readStart: function() {
|
|
|
|
if (self._reading && self._readableState.length > 0) return;
|
|
|
|
self._reading = true;
|
|
|
|
self.read(0);
|
|
|
|
if (self._opposite.readable) self._opposite.read(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
util.inherits(CleartextStream, CryptoStream);
|
|
|
|
|
|
|
|
|
|
|
|
CleartextStream.prototype._internallyPendingBytes = function() {
|
|
|
|
if (this.pair.ssl) {
|
|
|
|
return this.pair.ssl.clearPending();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CleartextStream.prototype.address = function() {
|
|
|
|
return this.socket && this.socket.address();
|
|
|
|
};
|
|
|
|
|
2016-05-15 07:29:19 +02:00
|
|
|
Object.defineProperty(CleartextStream.prototype, 'remoteAddress', {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
return this.socket && this.socket.remoteAddress;
|
|
|
|
}
|
2013-06-13 15:36:00 +02:00
|
|
|
});
|
|
|
|
|
2016-05-15 07:29:19 +02:00
|
|
|
Object.defineProperty(CleartextStream.prototype, 'remoteFamily', {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
return this.socket && this.socket.remoteFamily;
|
|
|
|
}
|
2014-07-16 16:04:34 +02:00
|
|
|
});
|
2013-06-13 15:36:00 +02:00
|
|
|
|
2016-05-15 07:29:19 +02:00
|
|
|
Object.defineProperty(CleartextStream.prototype, 'remotePort', {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
return this.socket && this.socket.remotePort;
|
|
|
|
}
|
2013-06-13 15:36:00 +02:00
|
|
|
});
|
|
|
|
|
2016-05-15 07:29:19 +02:00
|
|
|
Object.defineProperty(CleartextStream.prototype, 'localAddress', {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
return this.socket && this.socket.localAddress;
|
|
|
|
}
|
2013-06-13 15:36:00 +02:00
|
|
|
});
|
|
|
|
|
2016-05-15 07:29:19 +02:00
|
|
|
Object.defineProperty(CleartextStream.prototype, 'localPort', {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: function() {
|
|
|
|
return this.socket && this.socket.localPort;
|
|
|
|
}
|
2013-06-13 15:36:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function EncryptedStream(pair, options) {
|
|
|
|
CryptoStream.call(this, pair, options);
|
|
|
|
}
|
|
|
|
util.inherits(EncryptedStream, CryptoStream);
|
|
|
|
|
|
|
|
|
|
|
|
EncryptedStream.prototype._internallyPendingBytes = function() {
|
|
|
|
if (this.pair.ssl) {
|
|
|
|
return this.pair.ssl.encPending();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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() {
|
2015-10-14 23:10:25 +02:00
|
|
|
var err = new Error('TLS session renegotiation attack detected');
|
2013-06-13 15:36:00 +02:00
|
|
|
if (self.cleartext) self.cleartext.emit('error', err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onhandshakedone() {
|
|
|
|
// for future use
|
|
|
|
debug('onhandshakedone');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onclienthello(hello) {
|
2016-01-12 22:04:50 +01:00
|
|
|
const self = this;
|
|
|
|
var once = false;
|
2013-06-13 15:36:00 +02:00
|
|
|
|
|
|
|
this._resumingSession = true;
|
|
|
|
function callback(err, session) {
|
|
|
|
if (once) return;
|
|
|
|
once = true;
|
|
|
|
|
|
|
|
if (err) return self.socket.destroy(err);
|
|
|
|
|
2016-01-06 04:42:32 +01:00
|
|
|
setImmediate(function() {
|
|
|
|
self.ssl.loadSession(session);
|
|
|
|
self.ssl.endParser();
|
2013-06-13 15:36:00 +02:00
|
|
|
|
2016-01-06 04:42:32 +01:00
|
|
|
// Cycle data
|
|
|
|
self._resumingSession = false;
|
|
|
|
self.cleartext.read(0);
|
|
|
|
self.encrypted.read(0);
|
|
|
|
});
|
2013-06-13 15:36:00 +02: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;
|
2014-02-14 14:01:34 +01:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var once = false;
|
|
|
|
|
2014-11-04 17:14:55 +01:00
|
|
|
if (!self.server.emit('newSession', key, session, done))
|
|
|
|
done();
|
|
|
|
|
|
|
|
function done() {
|
2014-02-14 14:01:34 +01:00
|
|
|
if (once)
|
|
|
|
return;
|
|
|
|
once = true;
|
|
|
|
|
|
|
|
if (self.ssl)
|
|
|
|
self.ssl.newSessionDone();
|
2015-04-28 19:46:14 +02:00
|
|
|
}
|
2014-02-14 14:01:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-14 19:15:57 +02:00
|
|
|
function onocspresponse(resp) {
|
|
|
|
this.emit('OCSPResponse', resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-13 15:36:00 +02:00
|
|
|
/**
|
|
|
|
* Provides a pair of streams to do encrypted communication.
|
|
|
|
*/
|
|
|
|
|
2014-03-07 00:27:01 +01:00
|
|
|
function SecurePair(context, isServer, requestCert, rejectUnauthorized,
|
2013-06-13 15:36:00 +02:00
|
|
|
options) {
|
|
|
|
if (!(this instanceof SecurePair)) {
|
2014-03-07 00:27:01 +01:00
|
|
|
return new SecurePair(context,
|
2013-06-13 15:36:00 +02:00
|
|
|
isServer,
|
|
|
|
requestCert,
|
|
|
|
rejectUnauthorized,
|
|
|
|
options);
|
|
|
|
}
|
|
|
|
|
|
|
|
options || (options = {});
|
|
|
|
|
2015-09-17 00:45:29 +02:00
|
|
|
EventEmitter.call(this);
|
2013-06-13 15:36:00 +02:00
|
|
|
|
|
|
|
this.server = options.server;
|
|
|
|
this._secureEstablished = false;
|
|
|
|
this._isServer = isServer ? true : false;
|
|
|
|
this._encWriteState = true;
|
|
|
|
this._clearWriteState = true;
|
|
|
|
this._doneFlag = false;
|
|
|
|
this._destroying = false;
|
|
|
|
|
2014-03-07 00:27:01 +01:00
|
|
|
if (!context) {
|
|
|
|
this.credentials = tls.createSecureContext();
|
2013-06-13 15:36:00 +02:00
|
|
|
} else {
|
2014-03-07 00:27:01 +01:00
|
|
|
this.credentials = context;
|
2013-06-13 15:36:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-07-07 16:31:05 +02:00
|
|
|
this.ssl = new Connection(
|
|
|
|
this.credentials.context,
|
|
|
|
this._isServer ? true : false,
|
|
|
|
this._isServer ? this._requestCert : options.servername,
|
|
|
|
this._rejectUnauthorized
|
|
|
|
);
|
2013-06-13 15:36:00 +02:00
|
|
|
|
|
|
|
if (this._isServer) {
|
2015-08-21 23:30:08 +02:00
|
|
|
this.ssl.onhandshakestart = () => onhandshakestart.call(this);
|
|
|
|
this.ssl.onhandshakedone = () => onhandshakedone.call(this);
|
|
|
|
this.ssl.onclienthello = (hello) => onclienthello.call(this, hello);
|
|
|
|
this.ssl.onnewsession =
|
|
|
|
(key, session) => onnewsession.call(this, key, session);
|
2013-06-13 15:36:00 +02:00
|
|
|
this.ssl.lastHandshakeTime = 0;
|
|
|
|
this.ssl.handshakes = 0;
|
2014-04-14 19:15:57 +02:00
|
|
|
} else {
|
2015-08-21 23:30:08 +02:00
|
|
|
this.ssl.onocspresponse = (resp) => onocspresponse.call(this, resp);
|
2013-06-13 15:36:00 +02: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);
|
|
|
|
this.npnProtocol = null;
|
|
|
|
}
|
|
|
|
|
2015-04-23 08:25:15 +02:00
|
|
|
if (process.features.tls_alpn && options.ALPNProtocols) {
|
|
|
|
// keep reference in secureContext not to be GC-ed
|
|
|
|
this.ssl._secureContext.alpnBuffer = options.ALPNProtocols;
|
|
|
|
this.ssl.setALPNrotocols(this.ssl._secureContext.alpnBuffer);
|
|
|
|
this.alpnProtocol = null;
|
|
|
|
}
|
|
|
|
|
2013-06-13 15:36:00 +02:00
|
|
|
/* Acts as a r/w stream to the cleartext side of the stream. */
|
|
|
|
this.cleartext = new CleartextStream(this, options.cleartext);
|
|
|
|
|
|
|
|
/* Acts as a r/w stream to the encrypted side of the stream. */
|
|
|
|
this.encrypted = new EncryptedStream(this, options.encrypted);
|
|
|
|
|
|
|
|
/* Let streams know about each other */
|
|
|
|
this.cleartext._opposite = this.encrypted;
|
|
|
|
this.encrypted._opposite = this.cleartext;
|
|
|
|
this.cleartext.init();
|
|
|
|
this.encrypted.init();
|
|
|
|
|
2015-03-05 22:07:27 +01:00
|
|
|
process.nextTick(securePairNT, this, options);
|
2013-06-13 15:36:00 +02:00
|
|
|
}
|
|
|
|
|
2015-09-17 00:45:29 +02:00
|
|
|
util.inherits(SecurePair, EventEmitter);
|
2013-06-13 15:36:00 +02:00
|
|
|
|
2015-03-05 22:07:27 +01:00
|
|
|
function securePairNT(self, options) {
|
|
|
|
/* The Connection may be destroyed by an abort call */
|
|
|
|
if (self.ssl) {
|
|
|
|
self.ssl.start();
|
|
|
|
|
|
|
|
if (options.requestOCSP)
|
|
|
|
self.ssl.requestOCSP();
|
|
|
|
|
|
|
|
/* In case of cipher suite failures - SSL_accept/SSL_connect may fail */
|
|
|
|
if (self.ssl && self.ssl.error)
|
|
|
|
self.error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 15:36:00 +02:00
|
|
|
|
2017-02-16 23:30:29 +01:00
|
|
|
function createSecurePair(context, isServer, requestCert,
|
|
|
|
rejectUnauthorized, options) {
|
|
|
|
return new SecurePair(context, isServer, requestCert,
|
|
|
|
rejectUnauthorized, options);
|
|
|
|
}
|
2013-06-13 15:36:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
SecurePair.prototype.maybeInitFinished = function() {
|
|
|
|
if (this.ssl && !this._secureEstablished && this.ssl.isInitFinished()) {
|
|
|
|
if (process.features.tls_npn) {
|
|
|
|
this.npnProtocol = this.ssl.getNegotiatedProtocol();
|
|
|
|
}
|
|
|
|
|
2015-04-23 08:25:15 +02:00
|
|
|
if (process.features.tls_alpn) {
|
|
|
|
this.alpnProtocol = this.ssl.getALPNNegotiatedProtocol();
|
|
|
|
}
|
|
|
|
|
2013-06-13 15:36:00 +02:00
|
|
|
if (process.features.tls_sni) {
|
|
|
|
this.servername = this.ssl.getServername();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._secureEstablished = true;
|
|
|
|
debug('secure established');
|
|
|
|
this.emit('secure');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
SecurePair.prototype.destroy = function() {
|
|
|
|
if (this._destroying) return;
|
|
|
|
|
|
|
|
if (!this._doneFlag) {
|
|
|
|
debug('SecurePair.destroy');
|
|
|
|
this._destroying = true;
|
|
|
|
|
|
|
|
// SecurePair should be destroyed only after it's streams
|
|
|
|
this.cleartext.destroy();
|
|
|
|
this.encrypted.destroy();
|
|
|
|
|
|
|
|
this._doneFlag = true;
|
|
|
|
this.ssl.error = null;
|
|
|
|
this.ssl.close();
|
|
|
|
this.ssl = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
SecurePair.prototype.error = function(returnOnly) {
|
|
|
|
var err = this.ssl.error;
|
|
|
|
this.ssl.error = null;
|
|
|
|
|
|
|
|
if (!this._secureEstablished) {
|
|
|
|
// Emit ECONNRESET instead of zero return
|
|
|
|
if (!err || err.message === 'ZERO_RETURN') {
|
|
|
|
var connReset = new Error('socket hang up');
|
|
|
|
connReset.code = 'ECONNRESET';
|
|
|
|
connReset.sslError = err && err.message;
|
|
|
|
|
|
|
|
err = connReset;
|
|
|
|
}
|
|
|
|
this.destroy();
|
|
|
|
if (!returnOnly) 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();
|
2017-10-17 00:37:14 +02:00
|
|
|
} else if (!returnOnly) {
|
|
|
|
this.cleartext.emit('error', err);
|
2013-06-13 15:36:00 +02:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
};
|
2013-09-24 14:53:49 +02:00
|
|
|
|
|
|
|
|
2017-02-16 23:30:29 +01:00
|
|
|
function pipe(pair, socket) {
|
2013-09-24 14:53:49 +02:00
|
|
|
pair.encrypted.pipe(socket);
|
|
|
|
socket.pipe(pair.encrypted);
|
|
|
|
|
|
|
|
pair.encrypted.on('close', function() {
|
2015-03-05 22:07:27 +01:00
|
|
|
process.nextTick(pipeCloseNT, pair, socket);
|
2013-09-24 14:53:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
pair.fd = socket.fd;
|
|
|
|
var cleartext = pair.cleartext;
|
|
|
|
cleartext.socket = socket;
|
|
|
|
cleartext.encrypted = pair.encrypted;
|
|
|
|
cleartext.authorized = false;
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
if (pair.encrypted._pending)
|
|
|
|
pair.encrypted._writePending();
|
|
|
|
if (pair.cleartext._pending)
|
|
|
|
pair.cleartext._writePending();
|
|
|
|
pair.encrypted.read(0);
|
|
|
|
pair.cleartext.read(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
function onerror(e) {
|
|
|
|
if (cleartext._controlReleased) {
|
|
|
|
cleartext.emit('error', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onclose() {
|
|
|
|
socket.removeListener('error', onerror);
|
|
|
|
socket.removeListener('timeout', ontimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ontimeout() {
|
|
|
|
cleartext.emit('timeout');
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('error', onerror);
|
|
|
|
socket.on('close', onclose);
|
|
|
|
socket.on('timeout', ontimeout);
|
|
|
|
|
|
|
|
return cleartext;
|
2017-02-16 23:30:29 +01:00
|
|
|
}
|
2015-03-05 22:07:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
function pipeCloseNT(pair, socket) {
|
|
|
|
// Encrypted should be unpiped from socket to prevent possible
|
|
|
|
// write after destroy.
|
|
|
|
pair.encrypted.unpipe(socket);
|
|
|
|
socket.destroySoon();
|
|
|
|
}
|
2017-02-16 23:30:29 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createSecurePair:
|
|
|
|
internalUtil.deprecate(createSecurePair,
|
|
|
|
'tls.createSecurePair() is deprecated. Please use ' +
|
|
|
|
'tls.Socket instead.', 'DEP0064'),
|
|
|
|
pipe
|
|
|
|
};
|