| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | using Renci.SshNet.Common; |
| | | 4 | | using Renci.SshNet.Messages.Transport; |
| | | 5 | | |
| | | 6 | | namespace Renci.SshNet.Security |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents base class for Diffie Hellman key exchange algorithm. |
| | | 10 | | /// </summary> |
| | | 11 | | internal abstract class KeyExchangeDiffieHellman : KeyExchange |
| | | 12 | | { |
| | | 13 | | #pragma warning disable SA1401 // Fields should be private |
| | | 14 | | /// <summary> |
| | | 15 | | /// Specifies key exchange group number. |
| | | 16 | | /// </summary> |
| | | 17 | | protected BigInteger _group; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Specifies key exchange prime number. |
| | | 21 | | /// </summary> |
| | | 22 | | protected BigInteger _prime; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Specifies client payload. |
| | | 26 | | /// </summary> |
| | | 27 | | protected byte[] _clientPayload; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Specifies server payload. |
| | | 31 | | /// </summary> |
| | | 32 | | protected byte[] _serverPayload; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Specifies client exchange number. |
| | | 36 | | /// </summary> |
| | | 37 | | protected byte[] _clientExchangeValue; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Specifies server exchange number. |
| | | 41 | | /// </summary> |
| | | 42 | | protected byte[] _serverExchangeValue; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Specifies random generated number. |
| | | 46 | | /// </summary> |
| | | 47 | | protected BigInteger _privateExponent; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Specifies host key data. |
| | | 51 | | /// </summary> |
| | | 52 | | protected byte[] _hostKey; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Specifies signature data. |
| | | 56 | | /// </summary> |
| | | 57 | | protected byte[] _signature; |
| | | 58 | | #pragma warning restore SA1401 // Fields should be private |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the size, in bits, of the computed hash code. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <value> |
| | | 64 | | /// The size, in bits, of the computed hash code. |
| | | 65 | | /// </value> |
| | | 66 | | protected abstract int HashSize { get; } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Validates the exchange hash. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <returns> |
| | | 72 | | /// true if exchange hash is valid; otherwise false. |
| | | 73 | | /// </returns> |
| | | 74 | | protected override bool ValidateExchangeHash() |
| | 18 | 75 | | { |
| | 18 | 76 | | return ValidateExchangeHash(_hostKey, _signature); |
| | 18 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Starts key exchange algorithm. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="session">The session.</param> |
| | | 83 | | /// <param name="message">Key exchange init message.</param> |
| | | 84 | | public override void Start(Session session, KeyExchangeInitMessage message) |
| | 18 | 85 | | { |
| | 18 | 86 | | base.Start(session, message); |
| | | 87 | | |
| | 18 | 88 | | _serverPayload = message.GetBytes(); |
| | 18 | 89 | | _clientPayload = Session.ClientInitMessage.GetBytes(); |
| | 18 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Populates the client exchange value. |
| | | 94 | | /// </summary> |
| | | 95 | | protected void PopulateClientExchangeValue() |
| | 18 | 96 | | { |
| | 18 | 97 | | if (_group.IsZero) |
| | 0 | 98 | | { |
| | 0 | 99 | | throw new ArgumentNullException("_group"); |
| | | 100 | | } |
| | | 101 | | |
| | 18 | 102 | | if (_prime.IsZero) |
| | 0 | 103 | | { |
| | 0 | 104 | | throw new ArgumentNullException("_prime"); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | // generate private exponent that is twice the hash size (RFC 4419) with a minimum |
| | | 108 | | // of 1024 bits (whatever is less) |
| | 18 | 109 | | var privateExponentSize = Math.Max(HashSize * 2, 1024); |
| | | 110 | | |
| | | 111 | | BigInteger clientExchangeValue; |
| | | 112 | | |
| | | 113 | | do |
| | 18 | 114 | | { |
| | | 115 | | // Create private component |
| | 18 | 116 | | _privateExponent = BigInteger.Random(privateExponentSize); |
| | | 117 | | |
| | | 118 | | // Generate public component |
| | 18 | 119 | | clientExchangeValue = BigInteger.ModPow(_group, _privateExponent, _prime); |
| | 18 | 120 | | } |
| | 18 | 121 | | while (clientExchangeValue < 1 || clientExchangeValue > (_prime - 1)); |
| | | 122 | | |
| | 18 | 123 | | _clientExchangeValue = clientExchangeValue.ToByteArray().Reverse(); |
| | 18 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Handles the server DH reply message. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="hostKey">The host key.</param> |
| | | 130 | | /// <param name="serverExchangeValue">The server exchange value.</param> |
| | | 131 | | /// <param name="signature">The signature.</param> |
| | | 132 | | protected virtual void HandleServerDhReply(byte[] hostKey, byte[] serverExchangeValue, byte[] signature) |
| | 18 | 133 | | { |
| | 18 | 134 | | _serverExchangeValue = serverExchangeValue; |
| | 18 | 135 | | _hostKey = hostKey; |
| | 18 | 136 | | SharedKey = BigInteger.ModPow(serverExchangeValue.ToBigInteger(), _privateExponent, _prime).ToByteArray().Re |
| | 18 | 137 | | _signature = signature; |
| | 18 | 138 | | } |
| | | 139 | | } |
| | | 140 | | } |