@@ -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 ) ;
0 commit comments