Merge remote-tracking branch 'ry/v0.10'
[platform/upstream/nodejs.git] / lib / tls.js
index 4d222f3..f5b175f 100644 (file)
@@ -28,6 +28,8 @@ var stream = require('stream');
 var assert = require('assert').ok;
 var constants = require('constants');
 
+var Timer = process.binding('timer_wrap').Timer;
+
 var DEFAULT_CIPHERS = 'ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:' + // TLS 1.2
                       'RC4:HIGH:!MD5:!aNULL:!EDH';                   // TLS 1.0
 
@@ -52,13 +54,7 @@ exports.getCiphers = function() {
 };
 
 
-var debug;
-if (process.env.NODE_DEBUG && /tls/.test(process.env.NODE_DEBUG)) {
-  debug = function(a) { console.error('TLS:', a); };
-} else {
-  debug = function() { };
-}
-
+var debug = util.debuglog('tls');
 
 var Connection = null;
 try {
@@ -350,10 +346,10 @@ CryptoStream.prototype._write = function write(data, encoding, cb) {
     // Write current buffer now
     var written;
     if (this === this.pair.cleartext) {
-      debug('cleartext.write called with ' + data.length + ' bytes');
+      debug('cleartext.write called with %d bytes', data.length);
       written = this.pair.ssl.clearIn(data, 0, data.length);
     } else {
-      debug('encrypted.write called with ' + data.length + ' bytes');
+      debug('encrypted.write called with %d bytes', data.length);
       written = this.pair.ssl.encIn(data, 0, data.length);
     }
 
@@ -408,9 +404,9 @@ CryptoStream.prototype._write = function write(data, encoding, cb) {
   this._pendingCallback = cb;
 
   if (this === this.pair.cleartext) {
-    debug('cleartext.write queued with ' + data.length + ' bytes');
+    debug('cleartext.write queued with %d bytes', data.length);
   } else {
-    debug('encrypted.write queued with ' + data.length + ' bytes');
+    debug('encrypted.write queued with %d bytes', data.length);
   }
 };
 
@@ -437,10 +433,10 @@ CryptoStream.prototype._read = function read(size) {
 
   var out;
   if (this === this.pair.cleartext) {
-    debug('cleartext.read called with ' + size + ' bytes');
+    debug('cleartext.read called with %d bytes', size);
     out = this.pair.ssl.clearOut;
   } else {
-    debug('encrypted.read called with ' + size + ' bytes');
+    debug('encrypted.read called with %d bytes', size);
     out = this.pair.ssl.encOut;
   }
 
@@ -470,9 +466,9 @@ CryptoStream.prototype._read = function read(size) {
   assert(bytesRead >= 0);
 
   if (this === this.pair.cleartext) {
-    debug('cleartext.read succeed with ' + bytesRead + ' bytes');
+    debug('cleartext.read succeed with %d bytes', bytesRead);
   } else {
-    debug('encrypted.read succeed with ' + bytesRead + ' bytes');
+    debug('encrypted.read succeed with %d bytes', bytesRead);
   }
 
   // Try writing pending data
@@ -756,6 +752,17 @@ CleartextStream.prototype.__defineGetter__('remotePort', function() {
   return this.socket && this.socket.remotePort;
 });
 
+
+CleartextStream.prototype.__defineGetter__('localAddress', function() {
+  return this.socket && this.socket.localAddress;
+});
+
+
+CleartextStream.prototype.__defineGetter__('localPort', function() {
+  return this.socket && this.socket.localPort;
+});
+
+
 function EncryptedStream(pair, options) {
   CryptoStream.call(this, pair, options);
 }
@@ -776,7 +783,7 @@ function onhandshakestart() {
 
   var self = this;
   var ssl = self.ssl;
-  var now = Date.now();
+  var now = Timer.now();
 
   assert(now >= ssl.lastHandshakeTime);
 
@@ -1055,6 +1062,7 @@ SecurePair.prototype.error = function(returnOnly) {
 // - key. string.
 // - cert: string.
 // - ca: string or array of strings.
+// - sessionTimeout: integer.
 //
 // emit 'secureConnection'
 //   function (cleartextStream, encryptedStream) { }
@@ -1123,6 +1131,10 @@ function Server(/* [options], listener */) {
     throw new TypeError('handshakeTimeout must be a number');
   }
 
+  if (self.sessionTimeout) {
+    sharedCreds.context.setSessionTimeout(self.sessionTimeout);
+  }
+
   // constructor call
   net.Server.call(this, function(socket) {
     var creds = crypto.createCredentials(null, sharedCreds.context);
@@ -1219,6 +1231,7 @@ Server.prototype.setOptions = function(options) {
   if (options.secureProtocol) this.secureProtocol = options.secureProtocol;
   if (options.crl) this.crl = options.crl;
   if (options.ciphers) this.ciphers = options.ciphers;
+  if (options.sessionTimeout) this.sessionTimeout = options.sessionTimeout;
   var secureOptions = options.secureOptions || 0;
   if (options.honorCipherOrder) {
     secureOptions |= constants.SSL_OP_CIPHER_SERVER_PREFERENCE;