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