Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

tls: async session storage #3661

Closed
wants to merge 1 commit into from

Conversation

indutny
Copy link
Member

@indutny indutny commented Jul 7, 2012

WIP

Example:

var cluster = require('cluster'),
    https = require('https'),
    fs = require('fs'),
    Buffer = require('buffer').Buffer;

var redis = require('redis').createClient();

function start() {
  var options = {
    key: fs.readFileSync('./test/fixtures/test_key.pem'),
    cert: fs.readFileSync('./test/fixtures/test_cert.pem')
  };

  var server = https.createServer(options, function (req, res) {
    if (req.url === '/abc') process.exit();
    res.end('hello');
  });

  server.listen(44300, function() {
    console.log('listening!');
  });

  server.on('resumeTlsSession', function(key, callback) {
    redis.get('tls_sessions:' + key.toString('base64'), function(err, sess) {
      if (err || !sess) return callback(err, sess);
      callback(null, new Buffer(sess, 'base64'));
    });
  });

  server.on('newTlsSession', function(key, session) {
    var rkey = 'tls_sessions:' + key.toString('base64');
    redis.set(rkey, session.toString('base64'));
    redis.expire(rkey, 300);
  });
}

if (cluster.isMaster) {
  for (var i = 0; i < 4; i++) {
    cluster.fork();
  }

  cluster.on('exit', function(worker, code, signal) {
    cluster.fork();
  });
} else {
  start();
}

@bnoordhuis
Copy link
Member

Fedor, is this still relevant? If so, can you rebase it?

@indutny
Copy link
Member Author

indutny commented Jul 26, 2012

Rebased.

@indutny
Copy link
Member Author

indutny commented Jul 26, 2012

Btw, are we going to try it out? /cc @isaacs @bnoordhuis

}

if (hello.sessionId.length > 0 &&
this.server.listeners('resumeTlsSession').length > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Premature optimization, probably even a deoptimization when .listeners() returns a copy (which is likely going to happen).

EDIT: Hah, sorry - missed the else. :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is only a deoptimization. It should be something like this:

if (hello.sessionId.length <= 0 ||
    !this.server.emit('resumeTlsSession', hello.sessionId, callback)) {
  callback(null, null);
}

emit returns true if any listeners were called, or false if there are no listeners.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Force pushed with @isaacs variant

@indutny
Copy link
Member Author

indutny commented Aug 6, 2012

Fixed everything mentioned.

@indutny
Copy link
Member Author

indutny commented Aug 22, 2012

Rebased patch on master.


Connection* p = static_cast<Connection*>(SSL_get_app_data(s));

*copy = NULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, 0


if (ss->is_server_ && !ss->hello_parser_.ended()) {
bytes_written = ss->hello_parser_.Write(
reinterpret_cast<uint8_t*>(buffer_data + off),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style: needs to align. Maybe use a temp var.

@bnoordhuis
Copy link
Member

Generally LGTM. Fix the latest round of comments and add a few tests and it's ready to go in.

@bnoordhuis
Copy link
Member

Your patch seems to break simple/test-tls-securepair-server:

$ out/Release/node test/simple/test-tls-securepair-server.js
***server*** connection fd=undefined
***server*** i set it secure

tls.js:732
  this.socket.pause();
              ^
TypeError: Cannot call method 'pause' of null
    at SecurePair.onclienthello (tls.js:732:15)
    at process.startup.processMakeCallback.process._makeCallback (node.js:248:20)
    at EncryptedStream._puller (tls.js:689:24)
    at EncryptedStream.CryptoStream._pull (tls.js:582:19)
    at SecurePair.cycle (tls.js:898:20)
    at EncryptedStream.CryptoStream.write (tls.js:266:13)
    at Socket.ondata (stream.js:38:26)
    at Socket.EventEmitter.emit (events.js:88:17)
    at TCP.onread (net.js:395:14)
    at process.startup.processMakeCallback.process._makeCallback (node.js:248:20)

@indutny
Copy link
Member Author

indutny commented Sep 4, 2012

Fixed nits, tests are passing.

}

// Check if we overflowed (do not reply with any private data)
if (session_id == NULL || session_id + session_size > data_ + offset_) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe session_size should always be >=16 and <= 32. I think its safer to encode these limitations here, and fallback to openssl -- I want to be paranoid about parsing this kind of thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fFixed.

@bnoordhuis
Copy link
Member

One final nit, otherwise LGTM.

@indutny
Copy link
Member Author

indutny commented Sep 4, 2012

Thank you very much for reviewing this for a lot of times! Landed 8e0c830

@indutny indutny closed this Sep 4, 2012
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants