-
-
Notifications
You must be signed in to change notification settings - Fork 980
Expand file tree
/
Copy pathKeyExchange.cs
More file actions
677 lines (561 loc) · 26.3 KB
/
KeyExchange.cs
File metadata and controls
677 lines (561 loc) · 26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using Microsoft.Extensions.Logging;
using Renci.SshNet.Common;
using Renci.SshNet.Compression;
using Renci.SshNet.Messages;
using Renci.SshNet.Messages.Transport;
using Renci.SshNet.Security.Cryptography;
namespace Renci.SshNet.Security
{
/// <summary>
/// Represents base class for different key exchange algorithm implementations.
/// </summary>
public abstract class KeyExchange : Algorithm, IKeyExchange
{
private ILogger _logger;
private Func<byte[], KeyHostAlgorithm> _hostKeyAlgorithmFactory;
private CipherInfo _clientCipherInfo;
private CipherInfo _serverCipherInfo;
private HashInfo _clientHashInfo;
private HashInfo _serverHashInfo;
private Func<Compressor> _compressorFactory;
private Func<Compressor> _decompressorFactory;
/// <summary>
/// Gets the session.
/// </summary>
/// <value>
/// The session.
/// </value>
protected Session Session { get; private set; }
/// <summary>
/// Gets or sets key exchange shared key.
/// </summary>
/// <value>
/// The shared key.
/// </value>
public byte[] SharedKey { get; protected set; }
private byte[] _exchangeHash;
/// <summary>
/// Gets the exchange hash.
/// </summary>
/// <value>The exchange hash.</value>
public byte[] ExchangeHash
{
get
{
_exchangeHash ??= CalculateHash();
return _exchangeHash;
}
}
/// <summary>
/// Occurs when host key received.
/// </summary>
public event EventHandler<HostKeyEventArgs> HostKeyReceived;
/// <summary>
/// Initializes a new instance of the <see cref="KeyExchange"/> class.
/// </summary>
protected KeyExchange()
{
}
/// <inheritdoc/>
public virtual void Start(Session session, KeyExchangeInitMessage message, bool sendClientInitMessage)
{
Session = session;
_logger = Session.SessionLoggerFactory.CreateLogger(GetType());
if (sendClientInitMessage)
{
SendMessage(session.ClientInitMessage);
}
// Determine host key algorithm
var hostKeyAlgorithmName = (from b in session.ConnectionInfo.HostKeyAlgorithms.Keys
from a in message.ServerHostKeyAlgorithms
where a == b
select a).FirstOrDefault();
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("[{SessionId}] Host key algorithm: we offer {WeOffer}",
Session.SessionIdHex,
session.ConnectionInfo.HostKeyAlgorithms.Keys.Join(","));
_logger.LogTrace("[{SessionId}] Host key algorithm: they offer {TheyOffer}",
Session.SessionIdHex,
message.ServerHostKeyAlgorithms.Join(","));
}
if (hostKeyAlgorithmName is null)
{
throw new SshConnectionException(
$"No matching host key algorithm (server offers {message.ServerHostKeyAlgorithms.Join(",")})",
DisconnectReason.KeyExchangeFailed);
}
session.ConnectionInfo.CurrentHostKeyAlgorithm = hostKeyAlgorithmName;
_hostKeyAlgorithmFactory = session.ConnectionInfo.HostKeyAlgorithms[hostKeyAlgorithmName];
// Determine client encryption algorithm
var clientEncryptionAlgorithmName = (from b in session.ConnectionInfo.Encryptions.Keys
from a in message.EncryptionAlgorithmsClientToServer
where a == b
select a).FirstOrDefault();
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("[{SessionId}] Encryption client to server: we offer {WeOffer}",
Session.SessionIdHex,
session.ConnectionInfo.Encryptions.Keys.Join(","));
_logger.LogTrace("[{SessionId}] Encryption client to server: they offer {TheyOffer}",
Session.SessionIdHex,
message.EncryptionAlgorithmsClientToServer.Join(","));
}
if (clientEncryptionAlgorithmName is null)
{
throw new SshConnectionException(
$"No matching client encryption algorithm (server offers {message.EncryptionAlgorithmsClientToServer.Join(",")})",
DisconnectReason.KeyExchangeFailed);
}
session.ConnectionInfo.CurrentClientEncryption = clientEncryptionAlgorithmName;
_clientCipherInfo = session.ConnectionInfo.Encryptions[clientEncryptionAlgorithmName];
// Determine server encryption algorithm
var serverDecryptionAlgorithmName = (from b in session.ConnectionInfo.Encryptions.Keys
from a in message.EncryptionAlgorithmsServerToClient
where a == b
select a).FirstOrDefault();
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("[{SessionId}] Encryption server to client: we offer {WeOffer}",
Session.SessionIdHex,
session.ConnectionInfo.Encryptions.Keys.Join(","));
_logger.LogTrace("[{SessionId}] Encryption server to client: they offer {TheyOffer}",
Session.SessionIdHex,
message.EncryptionAlgorithmsServerToClient.Join(","));
}
if (serverDecryptionAlgorithmName is null)
{
throw new SshConnectionException(
$"No matching server encryption algorithm (server offers {message.EncryptionAlgorithmsServerToClient.Join(",")})",
DisconnectReason.KeyExchangeFailed);
}
session.ConnectionInfo.CurrentServerEncryption = serverDecryptionAlgorithmName;
_serverCipherInfo = session.ConnectionInfo.Encryptions[serverDecryptionAlgorithmName];
if (!_clientCipherInfo.IsAead)
{
// Determine client hmac algorithm
var clientHmacAlgorithmName = (from b in session.ConnectionInfo.HmacAlgorithms.Keys
from a in message.MacAlgorithmsClientToServer
where a == b
select a).FirstOrDefault();
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("[{SessionId}] MAC client to server: we offer {WeOffer}",
Session.SessionIdHex,
session.ConnectionInfo.HmacAlgorithms.Keys.Join(","));
_logger.LogTrace("[{SessionId}] MAC client to server: they offer {TheyOffer}",
Session.SessionIdHex,
message.MacAlgorithmsClientToServer.Join(","));
}
if (clientHmacAlgorithmName is null)
{
throw new SshConnectionException(
$"No matching client MAC algorithm (server offers {message.MacAlgorithmsClientToServer.Join(",")})",
DisconnectReason.KeyExchangeFailed);
}
session.ConnectionInfo.CurrentClientHmacAlgorithm = clientHmacAlgorithmName;
_clientHashInfo = session.ConnectionInfo.HmacAlgorithms[clientHmacAlgorithmName];
}
if (!_serverCipherInfo.IsAead)
{
// Determine server hmac algorithm
var serverHmacAlgorithmName = (from b in session.ConnectionInfo.HmacAlgorithms.Keys
from a in message.MacAlgorithmsServerToClient
where a == b
select a).FirstOrDefault();
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("[{SessionId}] MAC server to client: we offer {WeOffer}",
Session.SessionIdHex,
session.ConnectionInfo.HmacAlgorithms.Keys.Join(","));
_logger.LogTrace("[{SessionId}] MAC server to client: they offer {TheyOffer}",
Session.SessionIdHex,
message.MacAlgorithmsServerToClient.Join(","));
}
if (serverHmacAlgorithmName is null)
{
throw new SshConnectionException(
$"No matching server MAC algorithm (server offers {message.MacAlgorithmsServerToClient.Join(",")})",
DisconnectReason.KeyExchangeFailed);
}
session.ConnectionInfo.CurrentServerHmacAlgorithm = serverHmacAlgorithmName;
_serverHashInfo = session.ConnectionInfo.HmacAlgorithms[serverHmacAlgorithmName];
}
// Determine compression algorithm
var compressionAlgorithmName = (from b in session.ConnectionInfo.CompressionAlgorithms.Keys
from a in message.CompressionAlgorithmsClientToServer
where a == b
select a).FirstOrDefault();
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("[{SessionId}] Compression client to server: we offer {WeOffer}",
Session.SessionIdHex,
session.ConnectionInfo.CompressionAlgorithms.Keys.Join(","));
_logger.LogTrace("[{SessionId}] Compression client to server: they offer {TheyOffer}",
Session.SessionIdHex,
message.CompressionAlgorithmsClientToServer.Join(","));
}
if (compressionAlgorithmName is null)
{
throw new SshConnectionException(
$"No matching client compression algorithm (server offers {message.CompressionAlgorithmsClientToServer.Join(",")})",
DisconnectReason.KeyExchangeFailed);
}
session.ConnectionInfo.CurrentClientCompressionAlgorithm = compressionAlgorithmName;
_compressorFactory = session.ConnectionInfo.CompressionAlgorithms[compressionAlgorithmName];
// Determine decompression algorithm
var decompressionAlgorithmName = (from b in session.ConnectionInfo.CompressionAlgorithms.Keys
from a in message.CompressionAlgorithmsServerToClient
where a == b
select a).FirstOrDefault();
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("[{SessionId}] Compression server to client: we offer {WeOffer}",
Session.SessionIdHex,
session.ConnectionInfo.CompressionAlgorithms.Keys.Join(","));
_logger.LogTrace("[{SessionId}] Compression server to client: they offer {TheyOffer}",
Session.SessionIdHex,
message.CompressionAlgorithmsServerToClient.Join(","));
}
if (decompressionAlgorithmName is null)
{
throw new SshConnectionException(
$"No matching server compression algorithm (server offers {message.CompressionAlgorithmsServerToClient.Join(",")})",
DisconnectReason.KeyExchangeFailed);
}
session.ConnectionInfo.CurrentServerCompressionAlgorithm = decompressionAlgorithmName;
_decompressorFactory = session.ConnectionInfo.CompressionAlgorithms[decompressionAlgorithmName];
}
/// <summary>
/// Finishes key exchange algorithm.
/// </summary>
public virtual void Finish()
{
if (!ValidateExchangeHash())
{
throw new SshConnectionException("Host key could not be verified.", DisconnectReason.KeyExchangeFailed);
}
SendMessage(new NewKeysMessage());
}
/// <summary>
/// Creates the server side cipher to use.
/// </summary>
/// <param name="isAead"><see langword="true"/> to indicate the cipher is AEAD, <see langword="false"/> to indicate the cipher is not AEAD.</param>
/// <returns>Server cipher.</returns>
public Cipher CreateServerCipher(out bool isAead)
{
isAead = _serverCipherInfo.IsAead;
// Resolve Session ID
var sessionId = Session.SessionId ?? ExchangeHash;
// Calculate server to client initial IV
var serverVector = Hash(GenerateSessionKey(SharedKey, ExchangeHash, 'B', sessionId));
// Calculate server to client encryption
var serverKey = Hash(GenerateSessionKey(SharedKey, ExchangeHash, 'D', sessionId));
serverKey = GenerateSessionKey(SharedKey, ExchangeHash, serverKey, _serverCipherInfo.KeySize / 8);
_logger.LogDebug("[{SessionId}] Creating {ServerEncryption} server cipher.",
Session.SessionIdHex,
Session.ConnectionInfo.CurrentServerEncryption);
// Create server cipher
return _serverCipherInfo.Cipher(serverKey, serverVector);
}
/// <summary>
/// Creates the client side cipher to use.
/// </summary>
/// <param name="isAead"><see langword="true"/> to indicate the cipher is AEAD, <see langword="false"/> to indicate the cipher is not AEAD.</param>
/// <returns>Client cipher.</returns>
public Cipher CreateClientCipher(out bool isAead)
{
isAead = _clientCipherInfo.IsAead;
// Resolve Session ID
var sessionId = Session.SessionId ?? ExchangeHash;
// Calculate client to server initial IV
var clientVector = Hash(GenerateSessionKey(SharedKey, ExchangeHash, 'A', sessionId));
// Calculate client to server encryption
var clientKey = Hash(GenerateSessionKey(SharedKey, ExchangeHash, 'C', sessionId));
clientKey = GenerateSessionKey(SharedKey, ExchangeHash, clientKey, _clientCipherInfo.KeySize / 8);
_logger.LogDebug("[{SessionId}] Creating {ClientEncryption} client cipher.",
Session.SessionIdHex,
Session.ConnectionInfo.CurrentClientEncryption);
// Create client cipher
return _clientCipherInfo.Cipher(clientKey, clientVector);
}
/// <summary>
/// Creates the server side hash algorithm to use.
/// </summary>
/// <param name="isEncryptThenMAC"><see langword="true"/> to enable encrypt-then-MAC, <see langword="false"/> to use encrypt-and-MAC.</param>
/// <returns>
/// The server-side hash algorithm.
/// </returns>
public HashAlgorithm CreateServerHash(out bool isEncryptThenMAC)
{
if (_serverHashInfo == null)
{
isEncryptThenMAC = false;
return null;
}
isEncryptThenMAC = _serverHashInfo.IsEncryptThenMAC;
// Resolve Session ID
var sessionId = Session.SessionId ?? ExchangeHash;
var serverKey = GenerateSessionKey(SharedKey,
ExchangeHash,
Hash(GenerateSessionKey(SharedKey, ExchangeHash, 'F', sessionId)),
_serverHashInfo.KeySize / 8);
_logger.LogDebug("[{SessionId}] Creating {ServerHmacAlgorithm} server hmac algorithm.",
Session.SessionIdHex,
Session.ConnectionInfo.CurrentServerHmacAlgorithm);
return _serverHashInfo.HashAlgorithm(serverKey);
}
/// <summary>
/// Creates the client side hash algorithm to use.
/// </summary>
/// <param name="isEncryptThenMAC"><see langword="true"/> to enable encrypt-then-MAC, <see langword="false"/> to use encrypt-and-MAC.</param>
/// <returns>
/// The client-side hash algorithm.
/// </returns>
public HashAlgorithm CreateClientHash(out bool isEncryptThenMAC)
{
if (_clientHashInfo == null)
{
isEncryptThenMAC = false;
return null;
}
isEncryptThenMAC = _clientHashInfo.IsEncryptThenMAC;
// Resolve Session ID
var sessionId = Session.SessionId ?? ExchangeHash;
var clientKey = GenerateSessionKey(SharedKey,
ExchangeHash,
Hash(GenerateSessionKey(SharedKey, ExchangeHash, 'E', sessionId)),
_clientHashInfo.KeySize / 8);
_logger.LogDebug("[{SessionId}] Creating {ClientHmacAlgorithm} client hmac algorithm.",
Session.SessionIdHex,
Session.ConnectionInfo.CurrentClientHmacAlgorithm);
return _clientHashInfo.HashAlgorithm(clientKey);
}
/// <summary>
/// Creates the compression algorithm to use to deflate data.
/// </summary>
/// <returns>
/// The compression method.
/// </returns>
public Compressor CreateCompressor()
{
if (_compressorFactory is null)
{
return null;
}
_logger.LogDebug("[{SessionId}] Creating {CompressionAlgorithm} client compressor.",
Session.SessionIdHex,
Session.ConnectionInfo.CurrentClientCompressionAlgorithm);
var compressor = _compressorFactory();
compressor.Init(Session);
return compressor;
}
/// <summary>
/// Creates the compression algorithm to use to inflate data.
/// </summary>
/// <returns>
/// The decompression method.
/// </returns>
public Compressor CreateDecompressor()
{
if (_decompressorFactory is null)
{
return null;
}
_logger.LogDebug("[{SessionId}] Creating {ServerCompressionAlgorithm} server decompressor.",
Session.SessionIdHex,
Session.ConnectionInfo.CurrentServerCompressionAlgorithm);
var decompressor = _decompressorFactory();
decompressor.Init(Session);
return decompressor;
}
/// <summary>
/// Determines whether the specified host key can be trusted.
/// </summary>
/// <param name="host">The host algorithm.</param>
/// <returns>
/// <see langword="true"/> if the specified host can be trusted; otherwise, <see langword="false"/>.
/// </returns>
protected bool CanTrustHostKey(KeyHostAlgorithm host)
{
var handlers = HostKeyReceived;
if (handlers != null)
{
var args = new HostKeyEventArgs(host);
handlers(this, args);
return args.CanTrust;
}
return true;
}
/// <summary>
/// Validates the exchange hash.
/// </summary>
/// <returns>true if exchange hash is valid; otherwise false.</returns>
protected abstract bool ValidateExchangeHash();
private protected bool ValidateExchangeHash(byte[] encodedKey, byte[] encodedSignature)
{
var exchangeHash = CalculateHash();
var keyAlgorithm = _hostKeyAlgorithmFactory(encodedKey);
return keyAlgorithm.VerifySignature(exchangeHash, encodedSignature) && CanTrustHostKey(keyAlgorithm);
}
/// <summary>
/// Calculates key exchange hash value.
/// </summary>
/// <returns>Key exchange hash.</returns>
protected abstract byte[] CalculateHash();
/// <summary>
/// Hashes the specified data bytes.
/// </summary>
/// <param name="hashData">The hash data.</param>
/// <returns>
/// The hash of the data.
/// </returns>
protected abstract byte[] Hash(byte[] hashData);
/// <summary>
/// Sends SSH message to the server.
/// </summary>
/// <param name="message">The message.</param>
protected void SendMessage(Message message)
{
Session.SendMessage(message);
}
/// <summary>
/// Generates the session key.
/// </summary>
/// <param name="sharedKey">The shared key.</param>
/// <param name="exchangeHash">The exchange hash.</param>
/// <param name="key">The key.</param>
/// <param name="size">The size.</param>
/// <returns>
/// The session key.
/// </returns>
private byte[] GenerateSessionKey(byte[] sharedKey, byte[] exchangeHash, byte[] key, int size)
{
var result = new List<byte>(key);
while (size > result.Count)
{
var sessionKeyAdjustment = new SessionKeyAdjustment
{
SharedKey = sharedKey,
ExchangeHash = exchangeHash,
Key = key,
};
result.AddRange(Hash(sessionKeyAdjustment.GetBytes()));
}
return result.ToArray();
}
/// <summary>
/// Generates the session key.
/// </summary>
/// <param name="sharedKey">The shared key.</param>
/// <param name="exchangeHash">The exchange hash.</param>
/// <param name="p">The p.</param>
/// <param name="sessionId">The session id.</param>
/// <returns>
/// The session key.
/// </returns>
private static byte[] GenerateSessionKey(byte[] sharedKey, byte[] exchangeHash, char p, byte[] sessionId)
{
var sessionKeyGeneration = new SessionKeyGeneration
{
SharedKey = sharedKey,
ExchangeHash = exchangeHash,
Char = p,
SessionId = sessionId
};
return sessionKeyGeneration.GetBytes();
}
private sealed class SessionKeyGeneration : SshData
{
public byte[] SharedKey { get; set; }
public byte[] ExchangeHash { get; set; }
public char Char { get; set; }
public byte[] SessionId { get; set; }
/// <summary>
/// Gets the size of the message in bytes.
/// </summary>
/// <value>
/// The size of the messages in bytes.
/// </value>
protected override int BufferCapacity
{
get
{
var capacity = base.BufferCapacity;
capacity += 4; // SharedKey length
capacity += SharedKey.Length; // SharedKey
capacity += ExchangeHash.Length; // ExchangeHash
capacity += 1; // Char
capacity += SessionId.Length; // SessionId
return capacity;
}
}
protected override void LoadData()
{
throw new NotImplementedException();
}
protected override void SaveData()
{
WriteBinaryString(SharedKey);
Write(ExchangeHash);
Write((byte)Char);
Write(SessionId);
}
}
private sealed class SessionKeyAdjustment : SshData
{
public byte[] SharedKey { get; set; }
public byte[] ExchangeHash { get; set; }
public byte[] Key { get; set; }
/// <summary>
/// Gets the size of the message in bytes.
/// </summary>
/// <value>
/// The size of the messages in bytes.
/// </value>
protected override int BufferCapacity
{
get
{
var capacity = base.BufferCapacity;
capacity += 4; // SharedKey length
capacity += SharedKey.Length; // SharedKey
capacity += ExchangeHash.Length; // ExchangeHash
capacity += Key.Length; // Key
return capacity;
}
}
protected override void LoadData()
{
throw new NotImplementedException();
}
protected override void SaveData()
{
WriteBinaryString(SharedKey);
Write(ExchangeHash);
Write(Key);
}
}
#region IDisposable Members
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
}
#endregion
}
}