Skip to content

Commit 69ec6e4

Browse files
committed
add kex traces
1 parent 13b5f9f commit 69ec6e4

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

src/Renci.SshNet/Common/Extensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,5 +351,12 @@ internal static bool IsConnected(this Socket socket)
351351

352352
return socket.Connected;
353353
}
354+
355+
internal static string Join(this IEnumerable<string> values, string separator)
356+
{
357+
// Used to avoid analyzers asking to "use an overload with a char parameter"
358+
// which is not available on all targets.
359+
return string.Join(separator, values);
360+
}
354361
}
355362
}

src/Renci.SshNet/Security/KeyExchange.cs

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,23 @@ public virtual void Start(Session session, KeyExchangeInitMessage message, bool
7878
SendMessage(session.ClientInitMessage);
7979
}
8080

81-
// Determine encryption algorithm
81+
// Determine client encryption algorithm
8282
var clientEncryptionAlgorithmName = (from b in session.ConnectionInfo.Encryptions.Keys
8383
from a in message.EncryptionAlgorithmsClientToServer
8484
where a == b
8585
select a).FirstOrDefault();
8686

87+
if (_logger.IsEnabled(LogLevel.Trace))
88+
{
89+
_logger.LogTrace("[{SessionId}] Encryption client to server: we offer {WeOffer}",
90+
Session.SessionIdHex,
91+
session.ConnectionInfo.Encryptions.Keys.Join(","));
92+
93+
_logger.LogTrace("[{SessionId}] Encryption client to server: they offer {TheyOffer}",
94+
Session.SessionIdHex,
95+
message.EncryptionAlgorithmsClientToServer.Join(","));
96+
}
97+
8798
if (string.IsNullOrEmpty(clientEncryptionAlgorithmName))
8899
{
89100
throw new SshConnectionException("Client encryption algorithm not found", DisconnectReason.KeyExchangeFailed);
@@ -92,11 +103,23 @@ from a in message.EncryptionAlgorithmsClientToServer
92103
session.ConnectionInfo.CurrentClientEncryption = clientEncryptionAlgorithmName;
93104
_clientCipherInfo = session.ConnectionInfo.Encryptions[clientEncryptionAlgorithmName];
94105

95-
// Determine encryption algorithm
106+
// Determine server encryption algorithm
96107
var serverDecryptionAlgorithmName = (from b in session.ConnectionInfo.Encryptions.Keys
97108
from a in message.EncryptionAlgorithmsServerToClient
98109
where a == b
99110
select a).FirstOrDefault();
111+
112+
if (_logger.IsEnabled(LogLevel.Trace))
113+
{
114+
_logger.LogTrace("[{SessionId}] Encryption server to client: we offer {WeOffer}",
115+
Session.SessionIdHex,
116+
session.ConnectionInfo.Encryptions.Keys.Join(","));
117+
118+
_logger.LogTrace("[{SessionId}] Encryption server to client: they offer {TheyOffer}",
119+
Session.SessionIdHex,
120+
message.EncryptionAlgorithmsServerToClient.Join(","));
121+
}
122+
100123
if (string.IsNullOrEmpty(serverDecryptionAlgorithmName))
101124
{
102125
throw new SshConnectionException("Server decryption algorithm not found", DisconnectReason.KeyExchangeFailed);
@@ -112,6 +135,18 @@ from a in message.EncryptionAlgorithmsServerToClient
112135
from a in message.MacAlgorithmsClientToServer
113136
where a == b
114137
select a).FirstOrDefault();
138+
139+
if (_logger.IsEnabled(LogLevel.Trace))
140+
{
141+
_logger.LogTrace("[{SessionId}] MAC client to server: we offer {WeOffer}",
142+
Session.SessionIdHex,
143+
session.ConnectionInfo.HmacAlgorithms.Keys.Join(","));
144+
145+
_logger.LogTrace("[{SessionId}] MAC client to server: they offer {TheyOffer}",
146+
Session.SessionIdHex,
147+
message.MacAlgorithmsClientToServer.Join(","));
148+
}
149+
115150
if (string.IsNullOrEmpty(clientHmacAlgorithmName))
116151
{
117152
throw new SshConnectionException("Client HMAC algorithm not found", DisconnectReason.KeyExchangeFailed);
@@ -128,6 +163,18 @@ from a in message.MacAlgorithmsClientToServer
128163
from a in message.MacAlgorithmsServerToClient
129164
where a == b
130165
select a).FirstOrDefault();
166+
167+
if (_logger.IsEnabled(LogLevel.Trace))
168+
{
169+
_logger.LogTrace("[{SessionId}] MAC server to client: we offer {WeOffer}",
170+
Session.SessionIdHex,
171+
session.ConnectionInfo.HmacAlgorithms.Keys.Join(","));
172+
173+
_logger.LogTrace("[{SessionId}] MAC server to client: they offer {TheyOffer}",
174+
Session.SessionIdHex,
175+
message.MacAlgorithmsServerToClient.Join(","));
176+
}
177+
131178
if (string.IsNullOrEmpty(serverHmacAlgorithmName))
132179
{
133180
throw new SshConnectionException("Server HMAC algorithm not found", DisconnectReason.KeyExchangeFailed);
@@ -142,6 +189,18 @@ from a in message.MacAlgorithmsServerToClient
142189
from a in message.CompressionAlgorithmsClientToServer
143190
where a == b
144191
select a).FirstOrDefault();
192+
193+
if (_logger.IsEnabled(LogLevel.Trace))
194+
{
195+
_logger.LogTrace("[{SessionId}] Compression client to server: we offer {WeOffer}",
196+
Session.SessionIdHex,
197+
session.ConnectionInfo.CompressionAlgorithms.Keys.Join(","));
198+
199+
_logger.LogTrace("[{SessionId}] Compression client to server: they offer {TheyOffer}",
200+
Session.SessionIdHex,
201+
message.CompressionAlgorithmsClientToServer.Join(","));
202+
}
203+
145204
if (string.IsNullOrEmpty(compressionAlgorithmName))
146205
{
147206
throw new SshConnectionException("Compression algorithm not found", DisconnectReason.KeyExchangeFailed);
@@ -155,6 +214,18 @@ from a in message.CompressionAlgorithmsClientToServer
155214
from a in message.CompressionAlgorithmsServerToClient
156215
where a == b
157216
select a).FirstOrDefault();
217+
218+
if (_logger.IsEnabled(LogLevel.Trace))
219+
{
220+
_logger.LogTrace("[{SessionId}] Compression server to client: we offer {WeOffer}",
221+
Session.SessionIdHex,
222+
session.ConnectionInfo.CompressionAlgorithms.Keys.Join(","));
223+
224+
_logger.LogTrace("[{SessionId}] Compression server to client: they offer {TheyOffer}",
225+
Session.SessionIdHex,
226+
message.CompressionAlgorithmsServerToClient.Join(","));
227+
}
228+
158229
if (string.IsNullOrEmpty(decompressionAlgorithmName))
159230
{
160231
throw new SshConnectionException("Decompression algorithm not found", DisconnectReason.KeyExchangeFailed);

0 commit comments

Comments
 (0)