PageRenderTime 70ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/_tls_legacy.js

https://gitlab.com/GeekSir/node
JavaScript | 915 lines | 623 code | 193 blank | 99 comment | 158 complexity | a14a2777a59861c6a68d6fcf79b865e0 MD5 | raw file
Possible License(s): 0BSD, Apache-2.0, MPL-2.0-no-copyleft-exception, JSON, WTFPL, CC-BY-SA-3.0, Unlicense, ISC, BSD-3-Clause, MIT, AGPL-3.0
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. 'use strict';
  22. var assert = require('assert');
  23. var events = require('events');
  24. var stream = require('stream');
  25. var tls = require('tls');
  26. var util = require('util');
  27. var common = require('_tls_common');
  28. var Timer = process.binding('timer_wrap').Timer;
  29. var Connection = null;
  30. try {
  31. Connection = process.binding('crypto').Connection;
  32. } catch (e) {
  33. throw new Error('node.js not compiled with openssl crypto support.');
  34. }
  35. var debug = util.debuglog('tls-legacy');
  36. function SlabBuffer() {
  37. this.create();
  38. }
  39. SlabBuffer.prototype.create = function create() {
  40. this.isFull = false;
  41. this.pool = new Buffer(tls.SLAB_BUFFER_SIZE);
  42. this.offset = 0;
  43. this.remaining = this.pool.length;
  44. };
  45. SlabBuffer.prototype.use = function use(context, fn, size) {
  46. if (this.remaining === 0) {
  47. this.isFull = true;
  48. return 0;
  49. }
  50. var actualSize = this.remaining;
  51. if (!util.isNull(size)) actualSize = Math.min(size, actualSize);
  52. var bytes = fn.call(context, this.pool, this.offset, actualSize);
  53. if (bytes > 0) {
  54. this.offset += bytes;
  55. this.remaining -= bytes;
  56. }
  57. assert(this.remaining >= 0);
  58. return bytes;
  59. };
  60. var slabBuffer = null;
  61. // Base class of both CleartextStream and EncryptedStream
  62. function CryptoStream(pair, options) {
  63. stream.Duplex.call(this, options);
  64. this.pair = pair;
  65. this._pending = null;
  66. this._pendingEncoding = '';
  67. this._pendingCallback = null;
  68. this._doneFlag = false;
  69. this._retryAfterPartial = false;
  70. this._halfRead = false;
  71. this._sslOutCb = null;
  72. this._resumingSession = false;
  73. this._reading = true;
  74. this._destroyed = false;
  75. this._ended = false;
  76. this._finished = false;
  77. this._opposite = null;
  78. if (util.isNull(slabBuffer)) slabBuffer = new SlabBuffer();
  79. this._buffer = slabBuffer;
  80. this.once('finish', onCryptoStreamFinish);
  81. // net.Socket calls .onend too
  82. this.once('end', onCryptoStreamEnd);
  83. }
  84. util.inherits(CryptoStream, stream.Duplex);
  85. function onCryptoStreamFinish() {
  86. this._finished = true;
  87. if (this === this.pair.cleartext) {
  88. debug('cleartext.onfinish');
  89. if (this.pair.ssl) {
  90. // Generate close notify
  91. // NOTE: first call checks if client has sent us shutdown,
  92. // second call enqueues shutdown into the BIO.
  93. if (this.pair.ssl.shutdown() !== 1) {
  94. if (this.pair.ssl && this.pair.ssl.error)
  95. return this.pair.error();
  96. this.pair.ssl.shutdown();
  97. }
  98. if (this.pair.ssl && this.pair.ssl.error)
  99. return this.pair.error();
  100. }
  101. } else {
  102. debug('encrypted.onfinish');
  103. }
  104. // Try to read just to get sure that we won't miss EOF
  105. if (this._opposite.readable) this._opposite.read(0);
  106. if (this._opposite._ended) {
  107. this._done();
  108. // No half-close, sorry
  109. if (this === this.pair.cleartext) this._opposite._done();
  110. }
  111. }
  112. function onCryptoStreamEnd() {
  113. this._ended = true;
  114. if (this === this.pair.cleartext) {
  115. debug('cleartext.onend');
  116. } else {
  117. debug('encrypted.onend');
  118. }
  119. }
  120. // NOTE: Called once `this._opposite` is set.
  121. CryptoStream.prototype.init = function init() {
  122. var self = this;
  123. this._opposite.on('sslOutEnd', function() {
  124. if (self._sslOutCb) {
  125. var cb = self._sslOutCb;
  126. self._sslOutCb = null;
  127. cb(null);
  128. }
  129. });
  130. };
  131. CryptoStream.prototype._write = function write(data, encoding, cb) {
  132. assert(util.isNull(this._pending));
  133. // Black-hole data
  134. if (!this.pair.ssl) return cb(null);
  135. // When resuming session don't accept any new data.
  136. // And do not put too much data into openssl, before writing it from encrypted
  137. // side.
  138. //
  139. // TODO(indutny): Remove magic number, use watermark based limits
  140. if (!this._resumingSession &&
  141. this._opposite._internallyPendingBytes() < 128 * 1024) {
  142. // Write current buffer now
  143. var written;
  144. if (this === this.pair.cleartext) {
  145. debug('cleartext.write called with %d bytes', data.length);
  146. written = this.pair.ssl.clearIn(data, 0, data.length);
  147. } else {
  148. debug('encrypted.write called with %d bytes', data.length);
  149. written = this.pair.ssl.encIn(data, 0, data.length);
  150. }
  151. // Handle and report errors
  152. if (this.pair.ssl && this.pair.ssl.error) {
  153. return cb(this.pair.error(true));
  154. }
  155. // Force SSL_read call to cycle some states/data inside OpenSSL
  156. this.pair.cleartext.read(0);
  157. // Cycle encrypted data
  158. if (this.pair.encrypted._internallyPendingBytes())
  159. this.pair.encrypted.read(0);
  160. // Get NPN and Server name when ready
  161. this.pair.maybeInitFinished();
  162. // Whole buffer was written
  163. if (written === data.length) {
  164. if (this === this.pair.cleartext) {
  165. debug('cleartext.write succeed with ' + written + ' bytes');
  166. } else {
  167. debug('encrypted.write succeed with ' + written + ' bytes');
  168. }
  169. // Invoke callback only when all data read from opposite stream
  170. if (this._opposite._halfRead) {
  171. assert(util.isNull(this._sslOutCb));
  172. this._sslOutCb = cb;
  173. } else {
  174. cb(null);
  175. }
  176. return;
  177. } else if (written !== 0 && written !== -1) {
  178. assert(!this._retryAfterPartial);
  179. this._retryAfterPartial = true;
  180. this._write(data.slice(written), encoding, cb);
  181. this._retryAfterPartial = false;
  182. return;
  183. }
  184. } else {
  185. debug('cleartext.write queue is full');
  186. // Force SSL_read call to cycle some states/data inside OpenSSL
  187. this.pair.cleartext.read(0);
  188. }
  189. // No write has happened
  190. this._pending = data;
  191. this._pendingEncoding = encoding;
  192. this._pendingCallback = cb;
  193. if (this === this.pair.cleartext) {
  194. debug('cleartext.write queued with %d bytes', data.length);
  195. } else {
  196. debug('encrypted.write queued with %d bytes', data.length);
  197. }
  198. };
  199. CryptoStream.prototype._writePending = function writePending() {
  200. var data = this._pending,
  201. encoding = this._pendingEncoding,
  202. cb = this._pendingCallback;
  203. this._pending = null;
  204. this._pendingEncoding = '';
  205. this._pendingCallback = null;
  206. this._write(data, encoding, cb);
  207. };
  208. CryptoStream.prototype._read = function read(size) {
  209. // XXX: EOF?!
  210. if (!this.pair.ssl) return this.push(null);
  211. // Wait for session to be resumed
  212. // Mark that we're done reading, but don't provide data or EOF
  213. if (this._resumingSession || !this._reading) return this.push('');
  214. var out;
  215. if (this === this.pair.cleartext) {
  216. debug('cleartext.read called with %d bytes', size);
  217. out = this.pair.ssl.clearOut;
  218. } else {
  219. debug('encrypted.read called with %d bytes', size);
  220. out = this.pair.ssl.encOut;
  221. }
  222. var bytesRead = 0,
  223. start = this._buffer.offset,
  224. last = start;
  225. do {
  226. assert(last === this._buffer.offset);
  227. var read = this._buffer.use(this.pair.ssl, out, size - bytesRead);
  228. if (read > 0) {
  229. bytesRead += read;
  230. }
  231. last = this._buffer.offset;
  232. // Handle and report errors
  233. if (this.pair.ssl && this.pair.ssl.error) {
  234. this.pair.error();
  235. break;
  236. }
  237. } while (read > 0 &&
  238. !this._buffer.isFull &&
  239. bytesRead < size &&
  240. this.pair.ssl !== null);
  241. // Get NPN and Server name when ready
  242. this.pair.maybeInitFinished();
  243. // Create new buffer if previous was filled up
  244. var pool = this._buffer.pool;
  245. if (this._buffer.isFull) this._buffer.create();
  246. assert(bytesRead >= 0);
  247. if (this === this.pair.cleartext) {
  248. debug('cleartext.read succeed with %d bytes', bytesRead);
  249. } else {
  250. debug('encrypted.read succeed with %d bytes', bytesRead);
  251. }
  252. // Try writing pending data
  253. if (!util.isNull(this._pending)) this._writePending();
  254. if (!util.isNull(this._opposite._pending)) this._opposite._writePending();
  255. if (bytesRead === 0) {
  256. // EOF when cleartext has finished and we have nothing to read
  257. if (this._opposite._finished && this._internallyPendingBytes() === 0 ||
  258. this.pair.ssl && this.pair.ssl.receivedShutdown) {
  259. // Perform graceful shutdown
  260. this._done();
  261. // No half-open, sorry!
  262. if (this === this.pair.cleartext) {
  263. this._opposite._done();
  264. // EOF
  265. this.push(null);
  266. } else if (!this.pair.ssl || !this.pair.ssl.receivedShutdown) {
  267. // EOF
  268. this.push(null);
  269. }
  270. } else {
  271. // Bail out
  272. this.push('');
  273. }
  274. } else {
  275. // Give them requested data
  276. this.push(pool.slice(start, start + bytesRead));
  277. }
  278. // Let users know that we've some internal data to read
  279. var halfRead = this._internallyPendingBytes() !== 0;
  280. // Smart check to avoid invoking 'sslOutEnd' in the most of the cases
  281. if (this._halfRead !== halfRead) {
  282. this._halfRead = halfRead;
  283. // Notify listeners about internal data end
  284. if (!halfRead) {
  285. if (this === this.pair.cleartext) {
  286. debug('cleartext.sslOutEnd');
  287. } else {
  288. debug('encrypted.sslOutEnd');
  289. }
  290. this.emit('sslOutEnd');
  291. }
  292. }
  293. };
  294. CryptoStream.prototype.setTimeout = function(timeout, callback) {
  295. if (this.socket) this.socket.setTimeout(timeout, callback);
  296. };
  297. CryptoStream.prototype.setNoDelay = function(noDelay) {
  298. if (this.socket) this.socket.setNoDelay(noDelay);
  299. };
  300. CryptoStream.prototype.setKeepAlive = function(enable, initialDelay) {
  301. if (this.socket) this.socket.setKeepAlive(enable, initialDelay);
  302. };
  303. CryptoStream.prototype.__defineGetter__('bytesWritten', function() {
  304. return this.socket ? this.socket.bytesWritten : 0;
  305. });
  306. CryptoStream.prototype.getPeerCertificate = function(detailed) {
  307. if (this.pair.ssl) {
  308. return common.translatePeerCertificate(
  309. this.pair.ssl.getPeerCertificate(detailed));
  310. }
  311. return null;
  312. };
  313. CryptoStream.prototype.getSession = function() {
  314. if (this.pair.ssl) {
  315. return this.pair.ssl.getSession();
  316. }
  317. return null;
  318. };
  319. CryptoStream.prototype.isSessionReused = function() {
  320. if (this.pair.ssl) {
  321. return this.pair.ssl.isSessionReused();
  322. }
  323. return null;
  324. };
  325. CryptoStream.prototype.getCipher = function(err) {
  326. if (this.pair.ssl) {
  327. return this.pair.ssl.getCurrentCipher();
  328. } else {
  329. return null;
  330. }
  331. };
  332. CryptoStream.prototype.end = function(chunk, encoding) {
  333. if (this === this.pair.cleartext) {
  334. debug('cleartext.end');
  335. } else {
  336. debug('encrypted.end');
  337. }
  338. // Write pending data first
  339. if (!util.isNull(this._pending)) this._writePending();
  340. this.writable = false;
  341. stream.Duplex.prototype.end.call(this, chunk, encoding);
  342. };
  343. /*
  344. * Wait for both `finish` and `end` events to ensure that all data that
  345. * was written on this side was read from the other side.
  346. */
  347. function _destroyWhenReadAndWriteEndsDone(cryptoStream) {
  348. var waiting = 1;
  349. function finish() {
  350. if (--waiting === 0) cryptoStream.destroy();
  351. }
  352. cryptoStream._opposite.once('end', finish);
  353. if (!cryptoStream._finished) {
  354. cryptoStream.once('finish', finish);
  355. ++waiting;
  356. }
  357. }
  358. CryptoStream.prototype.destroySoon = function(err) {
  359. if (this === this.pair.cleartext) {
  360. debug('cleartext.destroySoon');
  361. } else {
  362. debug('encrypted.destroySoon');
  363. }
  364. if (this.writable)
  365. this.end();
  366. if (this._writableState.finished && this._opposite._ended) {
  367. this.destroy();
  368. } else {
  369. _destroyWhenReadAndWriteEndsDone(this);
  370. }
  371. };
  372. CryptoStream.prototype.destroy = function(err) {
  373. if (this._destroyed) return;
  374. this._destroyed = true;
  375. this.readable = this.writable = false;
  376. // Destroy both ends
  377. if (this === this.pair.cleartext) {
  378. debug('cleartext.destroy');
  379. } else {
  380. debug('encrypted.destroy');
  381. }
  382. this._opposite.destroy();
  383. var self = this;
  384. process.nextTick(function() {
  385. // Force EOF
  386. self.push(null);
  387. // Emit 'close' event
  388. self.emit('close', err ? true : false);
  389. });
  390. };
  391. CryptoStream.prototype._done = function() {
  392. this._doneFlag = true;
  393. if (this === this.pair.encrypted && !this.pair._secureEstablished)
  394. return this.pair.error();
  395. if (this.pair.cleartext._doneFlag &&
  396. this.pair.encrypted._doneFlag &&
  397. !this.pair._doneFlag) {
  398. // If both streams are done:
  399. this.pair.destroy();
  400. }
  401. };
  402. // readyState is deprecated. Don't use it.
  403. Object.defineProperty(CryptoStream.prototype, 'readyState', {
  404. get: function() {
  405. if (this._connecting) {
  406. return 'opening';
  407. } else if (this.readable && this.writable) {
  408. return 'open';
  409. } else if (this.readable && !this.writable) {
  410. return 'readOnly';
  411. } else if (!this.readable && this.writable) {
  412. return 'writeOnly';
  413. } else {
  414. return 'closed';
  415. }
  416. }
  417. });
  418. function CleartextStream(pair, options) {
  419. CryptoStream.call(this, pair, options);
  420. // This is a fake kludge to support how the http impl sits
  421. // on top of net Sockets
  422. var self = this;
  423. this._handle = {
  424. readStop: function() {
  425. self._reading = false;
  426. },
  427. readStart: function() {
  428. if (self._reading && self._readableState.length > 0) return;
  429. self._reading = true;
  430. self.read(0);
  431. if (self._opposite.readable) self._opposite.read(0);
  432. }
  433. };
  434. }
  435. util.inherits(CleartextStream, CryptoStream);
  436. CleartextStream.prototype._internallyPendingBytes = function() {
  437. if (this.pair.ssl) {
  438. return this.pair.ssl.clearPending();
  439. } else {
  440. return 0;
  441. }
  442. };
  443. CleartextStream.prototype.address = function() {
  444. return this.socket && this.socket.address();
  445. };
  446. CleartextStream.prototype.__defineGetter__('remoteAddress', function() {
  447. return this.socket && this.socket.remoteAddress;
  448. });
  449. CleartextStream.prototype.__defineGetter__('remoteFamily', function() {
  450. return this.socket && this.socket.remoteFamily;
  451. });
  452. CleartextStream.prototype.__defineGetter__('remotePort', function() {
  453. return this.socket && this.socket.remotePort;
  454. });
  455. CleartextStream.prototype.__defineGetter__('localAddress', function() {
  456. return this.socket && this.socket.localAddress;
  457. });
  458. CleartextStream.prototype.__defineGetter__('localPort', function() {
  459. return this.socket && this.socket.localPort;
  460. });
  461. function EncryptedStream(pair, options) {
  462. CryptoStream.call(this, pair, options);
  463. }
  464. util.inherits(EncryptedStream, CryptoStream);
  465. EncryptedStream.prototype._internallyPendingBytes = function() {
  466. if (this.pair.ssl) {
  467. return this.pair.ssl.encPending();
  468. } else {
  469. return 0;
  470. }
  471. };
  472. function onhandshakestart() {
  473. debug('onhandshakestart');
  474. var self = this;
  475. var ssl = self.ssl;
  476. var now = Timer.now();
  477. assert(now >= ssl.lastHandshakeTime);
  478. if ((now - ssl.lastHandshakeTime) >= tls.CLIENT_RENEG_WINDOW * 1000) {
  479. ssl.handshakes = 0;
  480. }
  481. var first = (ssl.lastHandshakeTime === 0);
  482. ssl.lastHandshakeTime = now;
  483. if (first) return;
  484. if (++ssl.handshakes > tls.CLIENT_RENEG_LIMIT) {
  485. // Defer the error event to the next tick. We're being called from OpenSSL's
  486. // state machine and OpenSSL is not re-entrant. We cannot allow the user's
  487. // callback to destroy the connection right now, it would crash and burn.
  488. setImmediate(function() {
  489. var err = new Error('TLS session renegotiation attack detected.');
  490. if (self.cleartext) self.cleartext.emit('error', err);
  491. });
  492. }
  493. }
  494. function onhandshakedone() {
  495. // for future use
  496. debug('onhandshakedone');
  497. }
  498. function onclienthello(hello) {
  499. var self = this,
  500. once = false;
  501. this._resumingSession = true;
  502. function callback(err, session) {
  503. if (once) return;
  504. once = true;
  505. if (err) return self.socket.destroy(err);
  506. self.ssl.loadSession(session);
  507. self.ssl.endParser();
  508. // Cycle data
  509. self._resumingSession = false;
  510. self.cleartext.read(0);
  511. self.encrypted.read(0);
  512. }
  513. if (hello.sessionId.length <= 0 ||
  514. !this.server ||
  515. !this.server.emit('resumeSession', hello.sessionId, callback)) {
  516. callback(null, null);
  517. }
  518. }
  519. function onnewsession(key, session) {
  520. if (!this.server) return;
  521. var self = this;
  522. var once = false;
  523. self.server.emit('newSession', key, session, function() {
  524. if (once)
  525. return;
  526. once = true;
  527. if (self.ssl)
  528. self.ssl.newSessionDone();
  529. });
  530. }
  531. function onocspresponse(resp) {
  532. this.emit('OCSPResponse', resp);
  533. }
  534. /**
  535. * Provides a pair of streams to do encrypted communication.
  536. */
  537. function SecurePair(context, isServer, requestCert, rejectUnauthorized,
  538. options) {
  539. if (!(this instanceof SecurePair)) {
  540. return new SecurePair(context,
  541. isServer,
  542. requestCert,
  543. rejectUnauthorized,
  544. options);
  545. }
  546. var self = this;
  547. options || (options = {});
  548. events.EventEmitter.call(this);
  549. this.server = options.server;
  550. this._secureEstablished = false;
  551. this._isServer = isServer ? true : false;
  552. this._encWriteState = true;
  553. this._clearWriteState = true;
  554. this._doneFlag = false;
  555. this._destroying = false;
  556. if (!context) {
  557. this.credentials = tls.createSecureContext();
  558. } else {
  559. this.credentials = context;
  560. }
  561. if (!this._isServer) {
  562. // For clients, we will always have either a given ca list or be using
  563. // default one
  564. requestCert = true;
  565. }
  566. this._rejectUnauthorized = rejectUnauthorized ? true : false;
  567. this._requestCert = requestCert ? true : false;
  568. this.ssl = new Connection(this.credentials.context,
  569. this._isServer ? true : false,
  570. this._isServer ? this._requestCert :
  571. options.servername,
  572. this._rejectUnauthorized);
  573. if (this._isServer) {
  574. this.ssl.onhandshakestart = onhandshakestart.bind(this);
  575. this.ssl.onhandshakedone = onhandshakedone.bind(this);
  576. this.ssl.onclienthello = onclienthello.bind(this);
  577. this.ssl.onnewsession = onnewsession.bind(this);
  578. this.ssl.lastHandshakeTime = 0;
  579. this.ssl.handshakes = 0;
  580. } else {
  581. this.ssl.onocspresponse = onocspresponse.bind(this);
  582. }
  583. if (process.features.tls_sni) {
  584. if (this._isServer && options.SNICallback) {
  585. this.ssl.setSNICallback(options.SNICallback);
  586. }
  587. this.servername = null;
  588. }
  589. if (process.features.tls_npn && options.NPNProtocols) {
  590. this.ssl.setNPNProtocols(options.NPNProtocols);
  591. this.npnProtocol = null;
  592. }
  593. /* Acts as a r/w stream to the cleartext side of the stream. */
  594. this.cleartext = new CleartextStream(this, options.cleartext);
  595. /* Acts as a r/w stream to the encrypted side of the stream. */
  596. this.encrypted = new EncryptedStream(this, options.encrypted);
  597. /* Let streams know about each other */
  598. this.cleartext._opposite = this.encrypted;
  599. this.encrypted._opposite = this.cleartext;
  600. this.cleartext.init();
  601. this.encrypted.init();
  602. process.nextTick(function() {
  603. /* The Connection may be destroyed by an abort call */
  604. if (self.ssl) {
  605. self.ssl.start();
  606. if (options.requestOCSP)
  607. self.ssl.requestOCSP();
  608. /* In case of cipher suite failures - SSL_accept/SSL_connect may fail */
  609. if (self.ssl && self.ssl.error)
  610. self.error();
  611. }
  612. });
  613. }
  614. util.inherits(SecurePair, events.EventEmitter);
  615. exports.createSecurePair = function(context,
  616. isServer,
  617. requestCert,
  618. rejectUnauthorized) {
  619. var pair = new SecurePair(context,
  620. isServer,
  621. requestCert,
  622. rejectUnauthorized);
  623. return pair;
  624. };
  625. SecurePair.prototype.maybeInitFinished = function() {
  626. if (this.ssl && !this._secureEstablished && this.ssl.isInitFinished()) {
  627. if (process.features.tls_npn) {
  628. this.npnProtocol = this.ssl.getNegotiatedProtocol();
  629. }
  630. if (process.features.tls_sni) {
  631. this.servername = this.ssl.getServername();
  632. }
  633. this._secureEstablished = true;
  634. debug('secure established');
  635. this.emit('secure');
  636. }
  637. };
  638. SecurePair.prototype.destroy = function() {
  639. if (this._destroying) return;
  640. if (!this._doneFlag) {
  641. debug('SecurePair.destroy');
  642. this._destroying = true;
  643. // SecurePair should be destroyed only after it's streams
  644. this.cleartext.destroy();
  645. this.encrypted.destroy();
  646. this._doneFlag = true;
  647. this.ssl.error = null;
  648. this.ssl.close();
  649. this.ssl = null;
  650. }
  651. };
  652. SecurePair.prototype.error = function(returnOnly) {
  653. var err = this.ssl.error;
  654. this.ssl.error = null;
  655. if (!this._secureEstablished) {
  656. // Emit ECONNRESET instead of zero return
  657. if (!err || err.message === 'ZERO_RETURN') {
  658. var connReset = new Error('socket hang up');
  659. connReset.code = 'ECONNRESET';
  660. connReset.sslError = err && err.message;
  661. err = connReset;
  662. }
  663. this.destroy();
  664. if (!returnOnly) this.emit('error', err);
  665. } else if (this._isServer &&
  666. this._rejectUnauthorized &&
  667. /peer did not return a certificate/.test(err.message)) {
  668. // Not really an error.
  669. this.destroy();
  670. } else {
  671. if (!returnOnly) this.cleartext.emit('error', err);
  672. }
  673. return err;
  674. };
  675. exports.pipe = function pipe(pair, socket) {
  676. pair.encrypted.pipe(socket);
  677. socket.pipe(pair.encrypted);
  678. pair.encrypted.on('close', function() {
  679. process.nextTick(function() {
  680. // Encrypted should be unpiped from socket to prevent possible
  681. // write after destroy.
  682. pair.encrypted.unpipe(socket);
  683. socket.destroySoon();
  684. });
  685. });
  686. pair.fd = socket.fd;
  687. var cleartext = pair.cleartext;
  688. cleartext.socket = socket;
  689. cleartext.encrypted = pair.encrypted;
  690. cleartext.authorized = false;
  691. // cycle the data whenever the socket drains, so that
  692. // we can pull some more into it. normally this would
  693. // be handled by the fact that pipe() triggers read() calls
  694. // on writable.drain, but CryptoStreams are a bit more
  695. // complicated. Since the encrypted side actually gets
  696. // its data from the cleartext side, we have to give it a
  697. // light kick to get in motion again.
  698. socket.on('drain', function() {
  699. if (pair.encrypted._pending)
  700. pair.encrypted._writePending();
  701. if (pair.cleartext._pending)
  702. pair.cleartext._writePending();
  703. pair.encrypted.read(0);
  704. pair.cleartext.read(0);
  705. });
  706. function onerror(e) {
  707. if (cleartext._controlReleased) {
  708. cleartext.emit('error', e);
  709. }
  710. }
  711. function onclose() {
  712. socket.removeListener('error', onerror);
  713. socket.removeListener('timeout', ontimeout);
  714. }
  715. function ontimeout() {
  716. cleartext.emit('timeout');
  717. }
  718. socket.on('error', onerror);
  719. socket.on('close', onclose);
  720. socket.on('timeout', ontimeout);
  721. return cleartext;
  722. };