@@ -377,16 +377,9 @@ function onPskClientCallback(hint, maxPskLen, maxIdentityLen) {
377377  return  {  psk : ret . psk ,  identity : ret . identity  } ; 
378378} 
379379
380- function  onkeylogclient ( line )  { 
381-   debug ( 'client onkeylog' ) ; 
382-   this [ owner_symbol ] . emit ( 'keylog' ,  line ) ; 
383- } 
384- 
385380function  onkeylog ( line )  { 
386-   debug ( 'server onkeylog' ) ; 
387-   const  owner  =  this [ owner_symbol ] ; 
388-   if  ( owner . server ) 
389-     owner . server . emit ( 'keylog' ,  line ,  owner ) ; 
381+   debug ( 'onkeylog' ) ; 
382+   this [ owner_symbol ] . emit ( 'keylog' ,  line ) ; 
390383} 
391384
392385function  onocspresponse ( resp )  { 
@@ -678,13 +671,26 @@ TLSSocket.prototype._init = function(socket, wrap) {
678671  if  ( requestCert  ||  rejectUnauthorized ) 
679672    ssl . setVerifyMode ( requestCert ,  rejectUnauthorized ) ; 
680673
674+   // Only call .onkeylog if there is a keylog listener. 
675+   ssl . onkeylog  =  onkeylog ; 
676+   this . on ( 'newListener' ,  keylogNewListener ) ; 
677+ 
678+   function  keylogNewListener ( event )  { 
679+     if  ( event  !==  'keylog' ) 
680+       return ; 
681+ 
682+     ssl . enableKeylogCallback ( ) ; 
683+ 
684+     // Remove this listener since it's no longer needed. 
685+     this . removeListener ( 'newListener' ,  keylogNewListener ) ; 
686+   } 
687+ 
681688  if  ( options . isServer )  { 
682689    ssl . onhandshakestart  =  onhandshakestart ; 
683690    ssl . onhandshakedone  =  onhandshakedone ; 
684691    ssl . onclienthello  =  loadSession ; 
685692    ssl . oncertcb  =  loadSNI ; 
686693    ssl . onnewsession  =  onnewsession ; 
687-     ssl . onkeylog  =  onkeylog ; 
688694    ssl . lastHandshakeTime  =  0 ; 
689695    ssl . handshakes  =  0 ; 
690696
@@ -694,8 +700,6 @@ TLSSocket.prototype._init = function(socket, wrap) {
694700        // Also starts the client hello parser as a side effect. 
695701        ssl . enableSessionCallbacks ( ) ; 
696702      } 
697-       if  ( this . server . listenerCount ( 'keylog' )  >  0 ) 
698-         ssl . enableKeylogCallback ( ) ; 
699703      if  ( this . server . listenerCount ( 'OCSPRequest' )  >  0 ) 
700704        ssl . enableCertCb ( ) ; 
701705    } 
@@ -724,39 +728,23 @@ TLSSocket.prototype._init = function(socket, wrap) {
724728      // Remove this listener since it's no longer needed. 
725729      this . removeListener ( 'newListener' ,  newListener ) ; 
726730    } 
727- 
728-     ssl . onkeylog  =  onkeylogclient ; 
729- 
730-     // Only call .onkeylog if there is a keylog listener. 
731-     this . on ( 'newListener' ,  keylogNewListener ) ; 
732- 
733-     function  keylogNewListener ( event )  { 
734-       if  ( event  !==  'keylog' ) 
735-         return ; 
736- 
737-       ssl . enableKeylogCallback ( ) ; 
738- 
739-       // Remove this listener since it's no longer needed. 
740-       this . removeListener ( 'newListener' ,  keylogNewListener ) ; 
741-     } 
742731  } 
743732
744733  if  ( tlsKeylog )  { 
745734    if  ( warnOnTlsKeylog )  { 
746735      warnOnTlsKeylog  =  false ; 
747736      process . emitWarning ( 'Using --tls-keylog makes TLS connections insecure '  + 
748737        'by writing secret key material to file '  +  tlsKeylog ) ; 
749-       ssl . enableKeylogCallback ( ) ; 
750-       this . on ( 'keylog' ,  ( line )  =>  { 
751-         appendFile ( tlsKeylog ,  line ,  {  mode : 0o600  } ,  ( err )  =>  { 
752-           if  ( err  &&  warnOnTlsKeylogError )  { 
753-             warnOnTlsKeylogError  =  false ; 
754-             process . emitWarning ( 'Failed to write TLS keylog (this warning '  + 
755-               'will not be repeated): '  +  err ) ; 
756-           } 
757-         } ) ; 
758-       } ) ; 
759738    } 
739+     this . on ( 'keylog' ,  ( line )  =>  { 
740+       appendFile ( tlsKeylog ,  line ,  {  mode : 0o600  } ,  ( err )  =>  { 
741+         if  ( err  &&  warnOnTlsKeylogError )  { 
742+           warnOnTlsKeylogError  =  false ; 
743+           process . emitWarning ( 'Failed to write TLS keylog (this warning '  + 
744+             'will not be repeated): '  +  err ) ; 
745+         } 
746+       } ) ; 
747+     } ) ; 
760748  } 
761749
762750  ssl . onerror  =  onerror ; 
@@ -1059,6 +1047,10 @@ function onSocketTLSError(err) {
10591047  } 
10601048} 
10611049
1050+ function  onSocketKeylog ( line )  { 
1051+   this . _tlsOptions . server . emit ( 'keylog' ,  line ,  this ) ; 
1052+ } 
1053+ 
10621054function  onSocketClose ( err )  { 
10631055  // Closed because of error - no need to emit it twice 
10641056  if  ( err ) 
@@ -1091,6 +1083,9 @@ function tlsConnectionListener(rawSocket) {
10911083
10921084  socket . on ( 'secure' ,  onServerSocketSecure ) ; 
10931085
1086+   if  ( this . listenerCount ( 'keylog' )  >  0 ) 
1087+     socket . on ( 'keylog' ,  onSocketKeylog ) ; 
1088+ 
10941089  socket [ kErrorEmitted ]  =  false ; 
10951090  socket . on ( 'close' ,  onSocketClose ) ; 
10961091  socket . on ( '_tlsError' ,  onSocketTLSError ) ; 
0 commit comments