KeyExchange.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using Renci.SshNet.Abstractions;
  6. using Renci.SshNet.Common;
  7. using Renci.SshNet.Compression;
  8. using Renci.SshNet.Messages;
  9. using Renci.SshNet.Messages.Transport;
  10. using Renci.SshNet.Security.Cryptography;
  11. namespace Renci.SshNet.Security
  12. {
  13. /// <summary>
  14. /// Represents base class for different key exchange algorithm implementations
  15. /// </summary>
  16. public abstract class KeyExchange : Algorithm, IKeyExchange
  17. {
  18. private CipherInfo _clientCipherInfo;
  19. private CipherInfo _serverCipherInfo;
  20. private HashInfo _clientHashInfo;
  21. private HashInfo _serverHashInfo;
  22. private Type _compressionType;
  23. private Type _decompressionType;
  24. /// <summary>
  25. /// Gets the session.
  26. /// </summary>
  27. /// <value>
  28. /// The session.
  29. /// </value>
  30. protected Session Session { get; private set; }
  31. /// <summary>
  32. /// Gets or sets key exchange shared key.
  33. /// </summary>
  34. /// <value>
  35. /// The shared key.
  36. /// </value>
  37. public byte[] SharedKey { get; protected set; }
  38. private byte[] _exchangeHash;
  39. /// <summary>
  40. /// Gets the exchange hash.
  41. /// </summary>
  42. /// <value>The exchange hash.</value>
  43. public byte[] ExchangeHash
  44. {
  45. get
  46. {
  47. _exchangeHash ??= CalculateHash();
  48. return _exchangeHash;
  49. }
  50. }
  51. /// <summary>
  52. /// Occurs when host key received.
  53. /// </summary>
  54. public event EventHandler<HostKeyEventArgs> HostKeyReceived;
  55. /// <summary>
  56. /// Starts key exchange algorithm.
  57. /// </summary>
  58. /// <param name="session">The session.</param>
  59. /// <param name="message">Key exchange init message.</param>
  60. public virtual void Start(Session session, KeyExchangeInitMessage message)
  61. {
  62. Session = session;
  63. SendMessage(session.ClientInitMessage);
  64. // Determine encryption algorithm
  65. var clientEncryptionAlgorithmName = (from b in session.ConnectionInfo.Encryptions.Keys
  66. from a in message.EncryptionAlgorithmsClientToServer
  67. where a == b
  68. select a).FirstOrDefault();
  69. if (string.IsNullOrEmpty(clientEncryptionAlgorithmName))
  70. {
  71. throw new SshConnectionException("Client encryption algorithm not found", DisconnectReason.KeyExchangeFailed);
  72. }
  73. session.ConnectionInfo.CurrentClientEncryption = clientEncryptionAlgorithmName;
  74. // Determine encryption algorithm
  75. var serverDecryptionAlgorithmName = (from b in session.ConnectionInfo.Encryptions.Keys
  76. from a in message.EncryptionAlgorithmsServerToClient
  77. where a == b
  78. select a).FirstOrDefault();
  79. if (string.IsNullOrEmpty(serverDecryptionAlgorithmName))
  80. {
  81. throw new SshConnectionException("Server decryption algorithm not found", DisconnectReason.KeyExchangeFailed);
  82. }
  83. session.ConnectionInfo.CurrentServerEncryption = serverDecryptionAlgorithmName;
  84. // Determine client hmac algorithm
  85. var clientHmacAlgorithmName = (from b in session.ConnectionInfo.HmacAlgorithms.Keys
  86. from a in message.MacAlgorithmsClientToServer
  87. where a == b
  88. select a).FirstOrDefault();
  89. if (string.IsNullOrEmpty(clientHmacAlgorithmName))
  90. {
  91. throw new SshConnectionException("Server HMAC algorithm not found", DisconnectReason.KeyExchangeFailed);
  92. }
  93. session.ConnectionInfo.CurrentClientHmacAlgorithm = clientHmacAlgorithmName;
  94. // Determine server hmac algorithm
  95. var serverHmacAlgorithmName = (from b in session.ConnectionInfo.HmacAlgorithms.Keys
  96. from a in message.MacAlgorithmsServerToClient
  97. where a == b
  98. select a).FirstOrDefault();
  99. if (string.IsNullOrEmpty(serverHmacAlgorithmName))
  100. {
  101. throw new SshConnectionException("Server HMAC algorithm not found", DisconnectReason.KeyExchangeFailed);
  102. }
  103. session.ConnectionInfo.CurrentServerHmacAlgorithm = serverHmacAlgorithmName;
  104. // Determine compression algorithm
  105. var compressionAlgorithmName = (from b in session.ConnectionInfo.CompressionAlgorithms.Keys
  106. from a in message.CompressionAlgorithmsClientToServer
  107. where a == b
  108. select a).LastOrDefault();
  109. if (string.IsNullOrEmpty(compressionAlgorithmName))
  110. {
  111. throw new SshConnectionException("Compression algorithm not found", DisconnectReason.KeyExchangeFailed);
  112. }
  113. session.ConnectionInfo.CurrentClientCompressionAlgorithm = compressionAlgorithmName;
  114. // Determine decompression algorithm
  115. var decompressionAlgorithmName = (from b in session.ConnectionInfo.CompressionAlgorithms.Keys
  116. from a in message.CompressionAlgorithmsServerToClient
  117. where a == b
  118. select a).LastOrDefault();
  119. if (string.IsNullOrEmpty(decompressionAlgorithmName))
  120. {
  121. throw new SshConnectionException("Decompression algorithm not found", DisconnectReason.KeyExchangeFailed);
  122. }
  123. session.ConnectionInfo.CurrentServerCompressionAlgorithm = decompressionAlgorithmName;
  124. _clientCipherInfo = session.ConnectionInfo.Encryptions[clientEncryptionAlgorithmName];
  125. _serverCipherInfo = session.ConnectionInfo.Encryptions[serverDecryptionAlgorithmName];
  126. _clientHashInfo = session.ConnectionInfo.HmacAlgorithms[clientHmacAlgorithmName];
  127. _serverHashInfo = session.ConnectionInfo.HmacAlgorithms[serverHmacAlgorithmName];
  128. _compressionType = session.ConnectionInfo.CompressionAlgorithms[compressionAlgorithmName];
  129. _decompressionType = session.ConnectionInfo.CompressionAlgorithms[decompressionAlgorithmName];
  130. }
  131. /// <summary>
  132. /// Finishes key exchange algorithm.
  133. /// </summary>
  134. public virtual void Finish()
  135. {
  136. if (!ValidateExchangeHash())
  137. {
  138. throw new SshConnectionException("Key exchange negotiation failed.", DisconnectReason.KeyExchangeFailed);
  139. }
  140. SendMessage(new NewKeysMessage());
  141. }
  142. /// <summary>
  143. /// Creates the server side cipher to use.
  144. /// </summary>
  145. /// <returns>Server cipher.</returns>
  146. public Cipher CreateServerCipher()
  147. {
  148. // Resolve Session ID
  149. var sessionId = Session.SessionId ?? ExchangeHash;
  150. // Calculate server to client initial IV
  151. var serverVector = Hash(GenerateSessionKey(SharedKey, ExchangeHash, 'B', sessionId));
  152. // Calculate server to client encryption
  153. var serverKey = Hash(GenerateSessionKey(SharedKey, ExchangeHash, 'D', sessionId));
  154. serverKey = GenerateSessionKey(SharedKey, ExchangeHash, serverKey, _serverCipherInfo.KeySize / 8);
  155. DiagnosticAbstraction.Log(string.Format("[{0}] Creating server cipher (Name:{1},Key:{2},IV:{3})",
  156. Session.ToHex(Session.SessionId),
  157. Session.ConnectionInfo.CurrentServerEncryption,
  158. Session.ToHex(serverKey),
  159. Session.ToHex(serverVector)));
  160. // Create server cipher
  161. return _serverCipherInfo.Cipher(serverKey, serverVector);
  162. }
  163. /// <summary>
  164. /// Creates the client side cipher to use.
  165. /// </summary>
  166. /// <returns>Client cipher.</returns>
  167. public Cipher CreateClientCipher()
  168. {
  169. // Resolve Session ID
  170. var sessionId = Session.SessionId ?? ExchangeHash;
  171. // Calculate client to server initial IV
  172. var clientVector = Hash(GenerateSessionKey(SharedKey, ExchangeHash, 'A', sessionId));
  173. // Calculate client to server encryption
  174. var clientKey = Hash(GenerateSessionKey(SharedKey, ExchangeHash, 'C', sessionId));
  175. clientKey = GenerateSessionKey(SharedKey, ExchangeHash, clientKey, _clientCipherInfo.KeySize / 8);
  176. // Create client cipher
  177. return _clientCipherInfo.Cipher(clientKey, clientVector);
  178. }
  179. /// <summary>
  180. /// Creates the server side hash algorithm to use.
  181. /// </summary>
  182. /// <returns>
  183. /// The server-side hash algorithm.
  184. /// </returns>
  185. public HashAlgorithm CreateServerHash()
  186. {
  187. // Resolve Session ID
  188. var sessionId = Session.SessionId ?? ExchangeHash;
  189. var serverKey = GenerateSessionKey(SharedKey,
  190. ExchangeHash,
  191. Hash(GenerateSessionKey(SharedKey, ExchangeHash, 'F', sessionId)),
  192. _serverHashInfo.KeySize / 8);
  193. return _serverHashInfo.HashAlgorithm(serverKey);
  194. }
  195. /// <summary>
  196. /// Creates the client side hash algorithm to use.
  197. /// </summary>
  198. /// <returns>
  199. /// The client-side hash algorithm.
  200. /// </returns>
  201. public HashAlgorithm CreateClientHash()
  202. {
  203. // Resolve Session ID
  204. var sessionId = Session.SessionId ?? ExchangeHash;
  205. var clientKey = GenerateSessionKey(SharedKey,
  206. ExchangeHash,
  207. Hash(GenerateSessionKey(SharedKey, ExchangeHash, 'E', sessionId)),
  208. _clientHashInfo.KeySize / 8);
  209. return _clientHashInfo.HashAlgorithm(clientKey);
  210. }
  211. /// <summary>
  212. /// Creates the compression algorithm to use to deflate data.
  213. /// </summary>
  214. /// <returns>
  215. /// The compression method.
  216. /// </returns>
  217. public Compressor CreateCompressor()
  218. {
  219. if (_compressionType is null)
  220. {
  221. return null;
  222. }
  223. var compressor = _compressionType.CreateInstance<Compressor>();
  224. compressor.Init(Session);
  225. return compressor;
  226. }
  227. /// <summary>
  228. /// Creates the compression algorithm to use to inflate data.
  229. /// </summary>
  230. /// <returns>
  231. /// The decompression method.
  232. /// </returns>
  233. public Compressor CreateDecompressor()
  234. {
  235. if (_decompressionType is null)
  236. {
  237. return null;
  238. }
  239. var decompressor = _decompressionType.CreateInstance<Compressor>();
  240. decompressor.Init(Session);
  241. return decompressor;
  242. }
  243. /// <summary>
  244. /// Determines whether the specified host key can be trusted.
  245. /// </summary>
  246. /// <param name="host">The host algorithm.</param>
  247. /// <returns>
  248. /// <c>true</c> if the specified host can be trusted; otherwise, <c>false</c>.
  249. /// </returns>
  250. protected bool CanTrustHostKey(KeyHostAlgorithm host)
  251. {
  252. var handlers = HostKeyReceived;
  253. if (handlers != null)
  254. {
  255. var args = new HostKeyEventArgs(host);
  256. handlers(this, args);
  257. return args.CanTrust;
  258. }
  259. return true;
  260. }
  261. /// <summary>
  262. /// Validates the exchange hash.
  263. /// </summary>
  264. /// <returns>true if exchange hash is valid; otherwise false.</returns>
  265. protected abstract bool ValidateExchangeHash();
  266. private protected bool ValidateExchangeHash(byte[] encodedKey, byte[] encodedSignature)
  267. {
  268. var exchangeHash = CalculateHash();
  269. var signatureData = new KeyHostAlgorithm.SignatureKeyData();
  270. signatureData.Load(encodedSignature);
  271. var keyAlgorithm = Session.ConnectionInfo.HostKeyAlgorithms[signatureData.AlgorithmName](encodedKey);
  272. Session.ConnectionInfo.CurrentHostKeyAlgorithm = signatureData.AlgorithmName;
  273. if (CanTrustHostKey(keyAlgorithm))
  274. {
  275. // keyAlgorithm.VerifySignature decodes the signature data before verifying.
  276. // But as we have already decoded the data to find the signature algorithm,
  277. // we just verify the decoded data directly through the DigitalSignature.
  278. return keyAlgorithm.DigitalSignature.Verify(exchangeHash, signatureData.Signature);
  279. }
  280. return false;
  281. }
  282. /// <summary>
  283. /// Calculates key exchange hash value.
  284. /// </summary>
  285. /// <returns>Key exchange hash.</returns>
  286. protected abstract byte[] CalculateHash();
  287. /// <summary>
  288. /// Hashes the specified data bytes.
  289. /// </summary>
  290. /// <param name="hashData">The hash data.</param>
  291. /// <returns>
  292. /// The hash of the data.
  293. /// </returns>
  294. protected abstract byte[] Hash(byte[] hashData);
  295. /// <summary>
  296. /// Sends SSH message to the server.
  297. /// </summary>
  298. /// <param name="message">The message.</param>
  299. protected void SendMessage(Message message)
  300. {
  301. Session.SendMessage(message);
  302. }
  303. /// <summary>
  304. /// Generates the session key.
  305. /// </summary>
  306. /// <param name="sharedKey">The shared key.</param>
  307. /// <param name="exchangeHash">The exchange hash.</param>
  308. /// <param name="key">The key.</param>
  309. /// <param name="size">The size.</param>
  310. /// <returns>
  311. /// The session key.
  312. /// </returns>
  313. private byte[] GenerateSessionKey(byte[] sharedKey, byte[] exchangeHash, byte[] key, int size)
  314. {
  315. var result = new List<byte>(key);
  316. while (size > result.Count)
  317. {
  318. var sessionKeyAdjustment = new SessionKeyAdjustment
  319. {
  320. SharedKey = sharedKey,
  321. ExchangeHash = exchangeHash,
  322. Key = key,
  323. };
  324. result.AddRange(Hash(sessionKeyAdjustment.GetBytes()));
  325. }
  326. return result.ToArray();
  327. }
  328. /// <summary>
  329. /// Generates the session key.
  330. /// </summary>
  331. /// <param name="sharedKey">The shared key.</param>
  332. /// <param name="exchangeHash">The exchange hash.</param>
  333. /// <param name="p">The p.</param>
  334. /// <param name="sessionId">The session id.</param>
  335. /// <returns>
  336. /// The session key.
  337. /// </returns>
  338. private static byte[] GenerateSessionKey(byte[] sharedKey, byte[] exchangeHash, char p, byte[] sessionId)
  339. {
  340. var sessionKeyGeneration = new SessionKeyGeneration
  341. {
  342. SharedKey = sharedKey,
  343. ExchangeHash = exchangeHash,
  344. Char = p,
  345. SessionId = sessionId
  346. };
  347. return sessionKeyGeneration.GetBytes();
  348. }
  349. private sealed class SessionKeyGeneration : SshData
  350. {
  351. public byte[] SharedKey { get; set; }
  352. public byte[] ExchangeHash { get; set; }
  353. public char Char { get; set; }
  354. public byte[] SessionId { get; set; }
  355. /// <summary>
  356. /// Gets the size of the message in bytes.
  357. /// </summary>
  358. /// <value>
  359. /// The size of the messages in bytes.
  360. /// </value>
  361. protected override int BufferCapacity
  362. {
  363. get
  364. {
  365. var capacity = base.BufferCapacity;
  366. capacity += 4; // SharedKey length
  367. capacity += SharedKey.Length; // SharedKey
  368. capacity += ExchangeHash.Length; // ExchangeHash
  369. capacity += 1; // Char
  370. capacity += SessionId.Length; // SessionId
  371. return capacity;
  372. }
  373. }
  374. protected override void LoadData()
  375. {
  376. throw new NotImplementedException();
  377. }
  378. protected override void SaveData()
  379. {
  380. WriteBinaryString(SharedKey);
  381. Write(ExchangeHash);
  382. Write((byte) Char);
  383. Write(SessionId);
  384. }
  385. }
  386. private sealed class SessionKeyAdjustment : SshData
  387. {
  388. public byte[] SharedKey { get; set; }
  389. public byte[] ExchangeHash { get; set; }
  390. public byte[] Key { get; set; }
  391. /// <summary>
  392. /// Gets the size of the message in bytes.
  393. /// </summary>
  394. /// <value>
  395. /// The size of the messages in bytes.
  396. /// </value>
  397. protected override int BufferCapacity
  398. {
  399. get
  400. {
  401. var capacity = base.BufferCapacity;
  402. capacity += 4; // SharedKey length
  403. capacity += SharedKey.Length; // SharedKey
  404. capacity += ExchangeHash.Length; // ExchangeHash
  405. capacity += Key.Length; // Key
  406. return capacity;
  407. }
  408. }
  409. protected override void LoadData()
  410. {
  411. throw new NotImplementedException();
  412. }
  413. protected override void SaveData()
  414. {
  415. WriteBinaryString(SharedKey);
  416. Write(ExchangeHash);
  417. Write(Key);
  418. }
  419. }
  420. #region IDisposable Members
  421. /// <summary>
  422. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  423. /// </summary>
  424. public void Dispose()
  425. {
  426. Dispose(disposing: true);
  427. GC.SuppressFinalize(this);
  428. }
  429. /// <summary>
  430. /// Releases unmanaged and - optionally - managed resources
  431. /// </summary>
  432. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  433. protected virtual void Dispose(bool disposing)
  434. {
  435. }
  436. /// <summary>
  437. /// Releases unmanaged resources and performs other cleanup operations before the
  438. /// <see cref="KeyExchange"/> is reclaimed by garbage collection.
  439. /// </summary>
  440. ~KeyExchange()
  441. {
  442. Dispose(disposing: false);
  443. }
  444. #endregion
  445. }
  446. }