Skip to content

Commit 212e9cd

Browse files
committed
tls: session API returns
1 parent 0a4260c commit 212e9cd

File tree

7 files changed

+480
-11
lines changed

7 files changed

+480
-11
lines changed

doc/api/tls.markdown

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,31 @@ established - it will be forwarded here.
403403
`tlsSocket` is the [tls.TLSSocket][] that the error originated from.
404404

405405

406+
### Event: 'newSession'
407+
408+
`function (sessionId, sessionData) { }`
409+
410+
Emitted on creation of TLS session. May be used to store sessions in external
411+
storage.
412+
413+
NOTE: adding this event listener will have an effect only on connections
414+
established after addition of event listener.
415+
416+
417+
### Event: 'resumeSession'
418+
419+
`function (sessionId, callback) { }`
420+
421+
Emitted when client wants to resume previous TLS session. Event listener may
422+
perform lookup in external storage using given `sessionId`, and invoke
423+
`callback(null, sessionData)` once finished. If session can't be resumed
424+
(i.e. doesn't exist in storage) one may call `callback(null, null)`. Calling
425+
`callback(err)` will terminate incoming connection and destroy socket.
426+
427+
NOTE: adding this event listener will have an effect only on connections
428+
established after addition of event listener.
429+
430+
406431
### server.listen(port, [host], [callback])
407432

408433
Begin accepting connections on the specified `port` and `host`. If the

lib/_tls_wrap.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ function onhandshakedone() {
4747
}
4848

4949

50+
function onclienthello(hello) {
51+
var self = this,
52+
once = false;
53+
54+
function callback(err, session) {
55+
if (once)
56+
return self.destroy(new Error('TLS session callback was called twice'));
57+
once = true;
58+
59+
if (err)
60+
return self.destroy(err);
61+
62+
self.ssl.loadSession(session);
63+
}
64+
65+
if (hello.sessionId.length <= 0 ||
66+
!this.server.emit('resumeSession', hello.sessionId, callback)) {
67+
callback(null, null);
68+
}
69+
}
70+
71+
72+
function onnewsession(key, session) {
73+
this.server.emit('newSession', key, session);
74+
}
75+
76+
5077
/**
5178
* Provides a wrap of socket stream to do encrypted communication.
5279
*/
@@ -92,6 +119,7 @@ TLSSocket.prototype._init = function() {
92119
// Wrap socket's handle
93120
var credentials = options.credentials || crypto.createCredentials();
94121
this.ssl = tls_wrap.wrap(this._handle, credentials.context, options.isServer);
122+
this.server = options.server || null;
95123

96124
// For clients, we will always have either a given ca list or be using
97125
// default one
@@ -104,8 +132,15 @@ TLSSocket.prototype._init = function() {
104132
if (options.isServer) {
105133
this.ssl.onhandshakestart = onhandshakestart.bind(this);
106134
this.ssl.onhandshakedone = onhandshakedone.bind(this);
135+
this.ssl.onclienthello = onclienthello.bind(this);
136+
this.ssl.onnewsession = onnewsession.bind(this);
107137
this.ssl.lastHandshakeTime = 0;
108138
this.ssl.handshakes = 0;
139+
140+
if (this.server.listeners('resumeSession').length > 0 ||
141+
this.server.listeners('newSession').length > 0) {
142+
this.ssl.enableSessionCallbacks();
143+
}
109144
} else {
110145
this.ssl.onhandshakestart = function() {};
111146
this.ssl.onhandshakedone = this._finishInit.bind(this);

src/node_crypto.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ class SecureContext : ObjectWrap {
6060
// TODO: ca_store_ should probably be removed, it's not used anywhere.
6161
X509_STORE *ca_store_;
6262

63-
protected:
6463
static const int kMaxSessionSize = 10 * 1024;
6564

65+
protected:
66+
6667
static v8::Handle<v8::Value> New(const v8::Arguments& args);
6768
static v8::Handle<v8::Value> Init(const v8::Arguments& args);
6869
static v8::Handle<v8::Value> SetKey(const v8::Arguments& args);

src/node_crypto_bio.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ class NodeBIO {
8787
}
8888

8989
protected:
90-
static const size_t kBufferLength = 16 * 1024;
90+
// NOTE: Size is maximum TLS frame length, this is required if we want
91+
// to fit whole ClientHello into one Buffer of NodeBIO.
92+
static const size_t kBufferLength = 16 * 1024 + 5;
9193

9294
class Buffer {
9395
public:

0 commit comments

Comments
 (0)