@@ -372,16 +372,9 @@ function onPskClientCallback(hint, maxPskLen, maxIdentityLen) {
372
372
return { psk : ret . psk , identity : ret . identity } ;
373
373
}
374
374
375
- function onkeylogclient ( line ) {
376
- debug ( 'client onkeylog' ) ;
377
- this [ owner_symbol ] . emit ( 'keylog' , line ) ;
378
- }
379
-
380
375
function onkeylog ( line ) {
381
- debug ( 'server onkeylog' ) ;
382
- const owner = this [ owner_symbol ] ;
383
- if ( owner . server )
384
- owner . server . emit ( 'keylog' , line , owner ) ;
376
+ debug ( 'onkeylog' ) ;
377
+ this [ owner_symbol ] . emit ( 'keylog' , line ) ;
385
378
}
386
379
387
380
function onocspresponse ( resp ) {
@@ -663,13 +656,26 @@ TLSSocket.prototype._init = function(socket, wrap) {
663
656
if ( requestCert || rejectUnauthorized )
664
657
ssl . setVerifyMode ( requestCert , rejectUnauthorized ) ;
665
658
659
+ // Only call .onkeylog if there is a keylog listener.
660
+ ssl . onkeylog = onkeylog ;
661
+ this . on ( 'newListener' , keylogNewListener ) ;
662
+
663
+ function keylogNewListener ( event ) {
664
+ if ( event !== 'keylog' )
665
+ return ;
666
+
667
+ ssl . enableKeylogCallback ( ) ;
668
+
669
+ // Remove this listener since it's no longer needed.
670
+ this . removeListener ( 'newListener' , keylogNewListener ) ;
671
+ }
672
+
666
673
if ( options . isServer ) {
667
674
ssl . onhandshakestart = onhandshakestart ;
668
675
ssl . onhandshakedone = onhandshakedone ;
669
676
ssl . onclienthello = loadSession ;
670
677
ssl . oncertcb = loadSNI ;
671
678
ssl . onnewsession = onnewsession ;
672
- ssl . onkeylog = onkeylog ;
673
679
ssl . lastHandshakeTime = 0 ;
674
680
ssl . handshakes = 0 ;
675
681
@@ -679,8 +685,6 @@ TLSSocket.prototype._init = function(socket, wrap) {
679
685
// Also starts the client hello parser as a side effect.
680
686
ssl . enableSessionCallbacks ( ) ;
681
687
}
682
- if ( this . server . listenerCount ( 'keylog' ) > 0 )
683
- ssl . enableKeylogCallback ( ) ;
684
688
if ( this . server . listenerCount ( 'OCSPRequest' ) > 0 )
685
689
ssl . enableCertCb ( ) ;
686
690
}
@@ -709,39 +713,23 @@ TLSSocket.prototype._init = function(socket, wrap) {
709
713
// Remove this listener since it's no longer needed.
710
714
this . removeListener ( 'newListener' , newListener ) ;
711
715
}
712
-
713
- ssl . onkeylog = onkeylogclient ;
714
-
715
- // Only call .onkeylog if there is a keylog listener.
716
- this . on ( 'newListener' , keylogNewListener ) ;
717
-
718
- function keylogNewListener ( event ) {
719
- if ( event !== 'keylog' )
720
- return ;
721
-
722
- ssl . enableKeylogCallback ( ) ;
723
-
724
- // Remove this listener since it's no longer needed.
725
- this . removeListener ( 'newListener' , keylogNewListener ) ;
726
- }
727
716
}
728
717
729
718
if ( tlsKeylog ) {
730
719
if ( warnOnTlsKeylog ) {
731
720
warnOnTlsKeylog = false ;
732
721
process . emitWarning ( 'Using --tls-keylog makes TLS connections insecure ' +
733
722
'by writing secret key material to file ' + tlsKeylog ) ;
734
- ssl . enableKeylogCallback ( ) ;
735
- this . on ( 'keylog' , ( line ) => {
736
- appendFile ( tlsKeylog , line , { mode : 0o600 } , ( err ) => {
737
- if ( err && warnOnTlsKeylogError ) {
738
- warnOnTlsKeylogError = false ;
739
- process . emitWarning ( 'Failed to write TLS keylog (this warning ' +
740
- 'will not be repeated): ' + err ) ;
741
- }
742
- } ) ;
743
- } ) ;
744
723
}
724
+ this . on ( 'keylog' , ( line ) => {
725
+ appendFile ( tlsKeylog , line , { mode : 0o600 } , ( err ) => {
726
+ if ( err && warnOnTlsKeylogError ) {
727
+ warnOnTlsKeylogError = false ;
728
+ process . emitWarning ( 'Failed to write TLS keylog (this warning ' +
729
+ 'will not be repeated): ' + err ) ;
730
+ }
731
+ } ) ;
732
+ } ) ;
745
733
}
746
734
747
735
ssl . onerror = onerror ;
@@ -1044,6 +1032,10 @@ function onSocketTLSError(err) {
1044
1032
}
1045
1033
}
1046
1034
1035
+ function onSocketKeylog ( line ) {
1036
+ this . _tlsOptions . server . emit ( 'keylog' , line , this ) ;
1037
+ }
1038
+
1047
1039
function onSocketClose ( err ) {
1048
1040
// Closed because of error - no need to emit it twice
1049
1041
if ( err )
@@ -1076,6 +1068,9 @@ function tlsConnectionListener(rawSocket) {
1076
1068
1077
1069
socket . on ( 'secure' , onServerSocketSecure ) ;
1078
1070
1071
+ if ( this . listenerCount ( 'keylog' ) > 0 )
1072
+ socket . on ( 'keylog' , onSocketKeylog ) ;
1073
+
1079
1074
socket [ kErrorEmitted ] = false ;
1080
1075
socket . on ( 'close' , onSocketClose ) ;
1081
1076
socket . on ( '_tlsError' , onSocketTLSError ) ;
0 commit comments