@@ -377,16 +377,9 @@ function onPskClientCallback(hint, maxPskLen, maxIdentityLen) {
377
377
return { psk : ret . psk , identity : ret . identity } ;
378
378
}
379
379
380
- function onkeylogclient ( line ) {
381
- debug ( 'client onkeylog' ) ;
382
- this [ owner_symbol ] . emit ( 'keylog' , line ) ;
383
- }
384
-
385
380
function 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 ) ;
390
383
}
391
384
392
385
function onocspresponse ( resp ) {
@@ -678,13 +671,26 @@ TLSSocket.prototype._init = function(socket, wrap) {
678
671
if ( requestCert || rejectUnauthorized )
679
672
ssl . setVerifyMode ( requestCert , rejectUnauthorized ) ;
680
673
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
+
681
688
if ( options . isServer ) {
682
689
ssl . onhandshakestart = onhandshakestart ;
683
690
ssl . onhandshakedone = onhandshakedone ;
684
691
ssl . onclienthello = loadSession ;
685
692
ssl . oncertcb = loadSNI ;
686
693
ssl . onnewsession = onnewsession ;
687
- ssl . onkeylog = onkeylog ;
688
694
ssl . lastHandshakeTime = 0 ;
689
695
ssl . handshakes = 0 ;
690
696
@@ -694,8 +700,6 @@ TLSSocket.prototype._init = function(socket, wrap) {
694
700
// Also starts the client hello parser as a side effect.
695
701
ssl . enableSessionCallbacks ( ) ;
696
702
}
697
- if ( this . server . listenerCount ( 'keylog' ) > 0 )
698
- ssl . enableKeylogCallback ( ) ;
699
703
if ( this . server . listenerCount ( 'OCSPRequest' ) > 0 )
700
704
ssl . enableCertCb ( ) ;
701
705
}
@@ -724,39 +728,23 @@ TLSSocket.prototype._init = function(socket, wrap) {
724
728
// Remove this listener since it's no longer needed.
725
729
this . removeListener ( 'newListener' , newListener ) ;
726
730
}
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
- }
742
731
}
743
732
744
733
if ( tlsKeylog ) {
745
734
if ( warnOnTlsKeylog ) {
746
735
warnOnTlsKeylog = false ;
747
736
process . emitWarning ( 'Using --tls-keylog makes TLS connections insecure ' +
748
737
'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
- } ) ;
759
738
}
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
+ } ) ;
760
748
}
761
749
762
750
ssl . onerror = onerror ;
@@ -1059,6 +1047,10 @@ function onSocketTLSError(err) {
1059
1047
}
1060
1048
}
1061
1049
1050
+ function onSocketKeylog ( line ) {
1051
+ this . _tlsOptions . server . emit ( 'keylog' , line , this ) ;
1052
+ }
1053
+
1062
1054
function onSocketClose ( err ) {
1063
1055
// Closed because of error - no need to emit it twice
1064
1056
if ( err )
@@ -1091,6 +1083,9 @@ function tlsConnectionListener(rawSocket) {
1091
1083
1092
1084
socket . on ( 'secure' , onServerSocketSecure ) ;
1093
1085
1086
+ if ( this . listenerCount ( 'keylog' ) > 0 )
1087
+ socket . on ( 'keylog' , onSocketKeylog ) ;
1088
+
1094
1089
socket [ kErrorEmitted ] = false ;
1095
1090
socket . on ( 'close' , onSocketClose ) ;
1096
1091
socket . on ( '_tlsError' , onSocketTLSError ) ;
0 commit comments