| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Net; |
| | | 5 | | using System.Security.Cryptography; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | using Renci.SshNet.Abstractions; |
| | | 9 | | using Renci.SshNet.Common; |
| | | 10 | | using Renci.SshNet.Messages.Authentication; |
| | | 11 | | using Renci.SshNet.Messages.Connection; |
| | | 12 | | using Renci.SshNet.Security; |
| | | 13 | | using Renci.SshNet.Security.Cryptography; |
| | | 14 | | using Renci.SshNet.Security.Cryptography.Ciphers; |
| | | 15 | | using Renci.SshNet.Security.Cryptography.Ciphers.Modes; |
| | | 16 | | |
| | | 17 | | namespace Renci.SshNet |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Represents remote connection information class. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <remarks> |
| | | 23 | | /// This class is NOT thread-safe. Do not use the same <see cref="ConnectionInfo"/> with multiple |
| | | 24 | | /// client instances. |
| | | 25 | | /// </remarks> |
| | | 26 | | public class ConnectionInfo : IConnectionInfoInternal |
| | | 27 | | { |
| | | 28 | | internal const int DefaultPort = 22; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// The default connection timeout. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <value> |
| | | 34 | | /// 30 seconds. |
| | | 35 | | /// </value> |
| | 4 | 36 | | private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(30); |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// The default channel close timeout. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <value> |
| | | 42 | | /// 1 second. |
| | | 43 | | /// </value> |
| | 4 | 44 | | private static readonly TimeSpan DefaultChannelCloseTimeout = TimeSpan.FromSeconds(1); |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets supported key exchange algorithms for this connection. |
| | | 48 | | /// </summary> |
| | 7155 | 49 | | public IDictionary<string, Type> KeyExchangeAlgorithms { get; private set; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets supported encryptions for this connection. |
| | | 53 | | /// </summary> |
| | 9998 | 54 | | public IDictionary<string, CipherInfo> Encryptions { get; private set; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets supported hash algorithms for this connection. |
| | | 58 | | /// </summary> |
| | 9998 | 59 | | public IDictionary<string, HashInfo> HmacAlgorithms { get; private set; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets supported host key algorithms for this connection. |
| | | 63 | | /// </summary> |
| | 5202 | 64 | | public IDictionary<string, Func<byte[], KeyHostAlgorithm>> HostKeyAlgorithms { get; private set; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets supported authentication methods for this connection. |
| | | 68 | | /// </summary> |
| | 4611 | 69 | | public IList<AuthenticationMethod> AuthenticationMethods { get; private set; } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets supported compression algorithms for this connection. |
| | | 73 | | /// </summary> |
| | 9998 | 74 | | public IDictionary<string, Type> CompressionAlgorithms { get; private set; } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the supported channel requests for this connection. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <value> |
| | | 80 | | /// The supported channel requests for this connection. |
| | | 81 | | /// </value> |
| | 4502 | 82 | | public IDictionary<string, RequestInfo> ChannelRequests { get; private set; } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Gets a value indicating whether connection is authenticated. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <value> |
| | | 88 | | /// <see langword="true"/> if connection is authenticated; otherwise, <see langword="false"/>. |
| | | 89 | | /// </value> |
| | 3658 | 90 | | public bool IsAuthenticated { get; private set; } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets connection host. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <value> |
| | | 96 | | /// The connection host. |
| | | 97 | | /// </value> |
| | 5117 | 98 | | public string Host { get; private set; } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Gets connection port. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <value> |
| | | 104 | | /// The connection port. The default value is 22. |
| | | 105 | | /// </value> |
| | 5203 | 106 | | public int Port { get; private set; } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Gets connection username. |
| | | 110 | | /// </summary> |
| | 5830 | 111 | | public string Username { get; private set; } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Gets proxy type. |
| | | 115 | | /// </summary> |
| | | 116 | | /// <value> |
| | | 117 | | /// The type of the proxy. |
| | | 118 | | /// </value> |
| | 4015 | 119 | | public ProxyTypes ProxyType { get; private set; } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Gets proxy connection host. |
| | | 123 | | /// </summary> |
| | 3230 | 124 | | public string ProxyHost { get; private set; } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Gets proxy connection port. |
| | | 128 | | /// </summary> |
| | 3575 | 129 | | public int ProxyPort { get; private set; } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Gets proxy connection username. |
| | | 133 | | /// </summary> |
| | 3344 | 134 | | public string ProxyUsername { get; private set; } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Gets proxy connection password. |
| | | 138 | | /// </summary> |
| | 3080 | 139 | | public string ProxyPassword { get; private set; } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Gets or sets connection timeout. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <value> |
| | | 145 | | /// The connection timeout. The default value is 30 seconds. |
| | | 146 | | /// </value> |
| | | 147 | | /// <example> |
| | | 148 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshClientTest.cs" region="Example SshClient Connect Tim |
| | | 149 | | /// </example> |
| | 21306 | 150 | | public TimeSpan Timeout { get; set; } |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Gets or sets the timeout to use when waiting for a server to acknowledge closing a channel. |
| | | 154 | | /// </summary> |
| | | 155 | | /// <value> |
| | | 156 | | /// The channel close timeout. The default value is 1 second. |
| | | 157 | | /// </value> |
| | | 158 | | /// <remarks> |
| | | 159 | | /// If a server does not send a <c>SSH_MSG_CHANNEL_CLOSE</c> message before the specified timeout |
| | | 160 | | /// elapses, the channel will be closed immediately. |
| | | 161 | | /// </remarks> |
| | 4506 | 162 | | public TimeSpan ChannelCloseTimeout { get; set; } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Gets or sets the character encoding. |
| | | 166 | | /// </summary> |
| | | 167 | | /// <value> |
| | | 168 | | /// The character encoding. The default is <see cref="Encoding.UTF8"/>. |
| | | 169 | | /// </value> |
| | 6841 | 170 | | public Encoding Encoding { get; set; } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Gets or sets number of retry attempts when session channel creation failed. |
| | | 174 | | /// </summary> |
| | | 175 | | /// <value> |
| | | 176 | | /// The number of retry attempts when session channel creation failed. The default |
| | | 177 | | /// value is 10. |
| | | 178 | | /// </value> |
| | 4561 | 179 | | public int RetryAttempts { get; set; } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Gets or sets maximum number of session channels to be open simultaneously. |
| | | 183 | | /// </summary> |
| | | 184 | | /// <value> |
| | | 185 | | /// The maximum number of session channels to be open simultaneously. The default |
| | | 186 | | /// value is 10. |
| | | 187 | | /// </value> |
| | 3958 | 188 | | public int MaxSessions { get; set; } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Occurs when authentication banner is sent by the server. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <example> |
| | | 194 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example Passwo |
| | | 195 | | /// </example> |
| | | 196 | | public event EventHandler<AuthenticationBannerEventArgs> AuthenticationBanner; |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Gets the current key exchange algorithm. |
| | | 200 | | /// </summary> |
| | 3629 | 201 | | public string CurrentKeyExchangeAlgorithm { get; internal set; } |
| | | 202 | | |
| | | 203 | | /// <summary> |
| | | 204 | | /// Gets the current server encryption. |
| | | 205 | | /// </summary> |
| | 2397 | 206 | | public string CurrentServerEncryption { get; internal set; } |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Gets the current client encryption. |
| | | 210 | | /// </summary> |
| | 2397 | 211 | | public string CurrentClientEncryption { get; internal set; } |
| | | 212 | | |
| | | 213 | | /// <summary> |
| | | 214 | | /// Gets the current server hash algorithm. |
| | | 215 | | /// </summary> |
| | 2397 | 216 | | public string CurrentServerHmacAlgorithm { get; internal set; } |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Gets the current client hash algorithm. |
| | | 220 | | /// </summary> |
| | 2397 | 221 | | public string CurrentClientHmacAlgorithm { get; internal set; } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Gets the current host key algorithm. |
| | | 225 | | /// </summary> |
| | 1199 | 226 | | public string CurrentHostKeyAlgorithm { get; internal set; } |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// Gets the current server compression algorithm. |
| | | 230 | | /// </summary> |
| | 1199 | 231 | | public string CurrentServerCompressionAlgorithm { get; internal set; } |
| | | 232 | | |
| | | 233 | | /// <summary> |
| | | 234 | | /// Gets the server version. |
| | | 235 | | /// </summary> |
| | 1817 | 236 | | public string ServerVersion { get; internal set; } |
| | | 237 | | |
| | | 238 | | /// <summary> |
| | | 239 | | /// Gets the client version. |
| | | 240 | | /// </summary> |
| | 1817 | 241 | | public string ClientVersion { get; internal set; } |
| | | 242 | | |
| | | 243 | | /// <summary> |
| | | 244 | | /// Gets the current client compression algorithm. |
| | | 245 | | /// </summary> |
| | 1199 | 246 | | public string CurrentClientCompressionAlgorithm { get; internal set; } |
| | | 247 | | |
| | | 248 | | /// <summary> |
| | | 249 | | /// Initializes a new instance of the <see cref="ConnectionInfo"/> class. |
| | | 250 | | /// </summary> |
| | | 251 | | /// <param name="host">The host.</param> |
| | | 252 | | /// <param name="username">The username.</param> |
| | | 253 | | /// <param name="authenticationMethods">The authentication methods.</param> |
| | | 254 | | /// <exception cref="ArgumentNullException"><paramref name="host"/> is <see langword="null"/>.</exception> |
| | | 255 | | /// <exception cref="ArgumentException"><paramref name="host"/> is a zero-length string.</exception> |
| | | 256 | | /// <exception cref="ArgumentException"><paramref name="username" /> is <see langword="null"/>, a zero-length st |
| | | 257 | | /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <see langword="null"/>.< |
| | | 258 | | /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception> |
| | | 259 | | public ConnectionInfo(string host, string username, params AuthenticationMethod[] authenticationMethods) |
| | 429 | 260 | | : this(host, DefaultPort, username, ProxyTypes.None, proxyHost: null, 0, proxyUsername: null, proxyPassword: |
| | 429 | 261 | | { |
| | 429 | 262 | | } |
| | | 263 | | |
| | | 264 | | /// <summary> |
| | | 265 | | /// Initializes a new instance of the <see cref="ConnectionInfo"/> class. |
| | | 266 | | /// </summary> |
| | | 267 | | /// <param name="host">The host.</param> |
| | | 268 | | /// <param name="port">The port.</param> |
| | | 269 | | /// <param name="username">The username.</param> |
| | | 270 | | /// <param name="authenticationMethods">The authentication methods.</param> |
| | | 271 | | /// <exception cref="ArgumentNullException"><paramref name="host"/> is <see langword="null"/>.</exception> |
| | | 272 | | /// <exception cref="ArgumentException"><paramref name="username" /> is <see langword="null"/>, a zero-length st |
| | | 273 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="IPEndPoint.M |
| | | 274 | | /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <see langword="null"/>.< |
| | | 275 | | /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception> |
| | | 276 | | public ConnectionInfo(string host, int port, string username, params AuthenticationMethod[] authenticationMethod |
| | 1675 | 277 | | : this(host, port, username, ProxyTypes.None, proxyHost: null, 0, proxyUsername: null, proxyPassword: null, |
| | 1675 | 278 | | { |
| | 1675 | 279 | | } |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Initializes a new instance of the <see cref="ConnectionInfo" /> class. |
| | | 283 | | /// </summary> |
| | | 284 | | /// <param name="host">Connection host.</param> |
| | | 285 | | /// <param name="port">Connection port.</param> |
| | | 286 | | /// <param name="username">Connection username.</param> |
| | | 287 | | /// <param name="proxyType">Type of the proxy.</param> |
| | | 288 | | /// <param name="proxyHost">The proxy host.</param> |
| | | 289 | | /// <param name="proxyPort">The proxy port.</param> |
| | | 290 | | /// <param name="proxyUsername">The proxy username.</param> |
| | | 291 | | /// <param name="proxyPassword">The proxy password.</param> |
| | | 292 | | /// <param name="authenticationMethods">The authentication methods.</param> |
| | | 293 | | /// <exception cref="ArgumentNullException"><paramref name="host"/> is <see langword="null"/>.</exception> |
| | | 294 | | /// <exception cref="ArgumentException"><paramref name="username" /> is <see langword="null"/>, a zero-length st |
| | | 295 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="IPEndPoint.M |
| | | 296 | | /// <exception cref="ArgumentNullException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None"/> an |
| | | 297 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None |
| | | 298 | | /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <see langword="null"/>.< |
| | | 299 | | /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception> |
| | 2846 | 300 | | public ConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyP |
| | 2846 | 301 | | { |
| | 2846 | 302 | | if (host is null) |
| | 6 | 303 | | { |
| | 6 | 304 | | throw new ArgumentNullException(nameof(host)); |
| | | 305 | | } |
| | | 306 | | |
| | 2840 | 307 | | port.ValidatePort("port"); |
| | | 308 | | |
| | 2828 | 309 | | if (username is null) |
| | 3 | 310 | | { |
| | 3 | 311 | | throw new ArgumentNullException(nameof(username)); |
| | | 312 | | } |
| | | 313 | | |
| | 2825 | 314 | | if (username.All(char.IsWhiteSpace)) |
| | 6 | 315 | | { |
| | 6 | 316 | | throw new ArgumentException("Cannot be empty or contain only whitespace.", nameof(username)); |
| | | 317 | | } |
| | | 318 | | |
| | 2819 | 319 | | if (proxyType != ProxyTypes.None) |
| | 435 | 320 | | { |
| | 435 | 321 | | if (proxyHost is null) |
| | 3 | 322 | | { |
| | 3 | 323 | | throw new ArgumentNullException(nameof(proxyHost)); |
| | | 324 | | } |
| | | 325 | | |
| | 432 | 326 | | proxyPort.ValidatePort("proxyPort"); |
| | 426 | 327 | | } |
| | | 328 | | |
| | 2810 | 329 | | if (authenticationMethods is null) |
| | 3 | 330 | | { |
| | 3 | 331 | | throw new ArgumentNullException(nameof(authenticationMethods)); |
| | | 332 | | } |
| | | 333 | | |
| | 2807 | 334 | | if (authenticationMethods.Length == 0) |
| | 3 | 335 | | { |
| | 3 | 336 | | throw new ArgumentException("At least one authentication method should be specified.", nameof(authentica |
| | | 337 | | } |
| | | 338 | | |
| | | 339 | | // Set default connection values |
| | 2804 | 340 | | Timeout = DefaultTimeout; |
| | 2804 | 341 | | ChannelCloseTimeout = DefaultChannelCloseTimeout; |
| | 2804 | 342 | | RetryAttempts = 10; |
| | 2804 | 343 | | MaxSessions = 10; |
| | 2804 | 344 | | Encoding = Encoding.UTF8; |
| | | 345 | | |
| | 2804 | 346 | | KeyExchangeAlgorithms = new Dictionary<string, Type> |
| | 2804 | 347 | | { |
| | 2804 | 348 | | { "curve25519-sha256", typeof(KeyExchangeECCurve25519) }, |
| | 2804 | 349 | | { "curve25519-sha256@libssh.org", typeof(KeyExchangeECCurve25519) }, |
| | 2804 | 350 | | { "ecdh-sha2-nistp256", typeof(KeyExchangeECDH256) }, |
| | 2804 | 351 | | { "ecdh-sha2-nistp384", typeof(KeyExchangeECDH384) }, |
| | 2804 | 352 | | { "ecdh-sha2-nistp521", typeof(KeyExchangeECDH521) }, |
| | 2804 | 353 | | { "diffie-hellman-group-exchange-sha256", typeof(KeyExchangeDiffieHellmanGroupExchangeSha256) }, |
| | 2804 | 354 | | { "diffie-hellman-group-exchange-sha1", typeof(KeyExchangeDiffieHellmanGroupExchangeSha1) }, |
| | 2804 | 355 | | { "diffie-hellman-group16-sha512", typeof(KeyExchangeDiffieHellmanGroup16Sha512) }, |
| | 2804 | 356 | | { "diffie-hellman-group14-sha256", typeof(KeyExchangeDiffieHellmanGroup14Sha256) }, |
| | 2804 | 357 | | { "diffie-hellman-group14-sha1", typeof(KeyExchangeDiffieHellmanGroup14Sha1) }, |
| | 2804 | 358 | | { "diffie-hellman-group1-sha1", typeof(KeyExchangeDiffieHellmanGroup1Sha1) }, |
| | 2804 | 359 | | }; |
| | | 360 | | |
| | 2804 | 361 | | Encryptions = new Dictionary<string, CipherInfo> |
| | 2804 | 362 | | { |
| | 2360 | 363 | | { "aes128-ctr", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padd |
| | 6 | 364 | | { "aes192-ctr", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padd |
| | 6 | 365 | | { "aes256-ctr", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padd |
| | 6 | 366 | | { "aes128-cbc", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padd |
| | 6 | 367 | | { "aes192-cbc", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padd |
| | 6 | 368 | | { "aes256-cbc", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padd |
| | 6 | 369 | | { "3des-cbc", new CipherInfo(192, (key, iv) => new TripleDesCipher(key, new CbcCipherMode(iv), paddi |
| | 0 | 370 | | { "blowfish-cbc", new CipherInfo(128, (key, iv) => new BlowfishCipher(key, new CbcCipherMode(iv), pa |
| | 0 | 371 | | { "twofish-cbc", new CipherInfo(256, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), padd |
| | 0 | 372 | | { "twofish192-cbc", new CipherInfo(192, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), p |
| | 0 | 373 | | { "twofish128-cbc", new CipherInfo(128, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), p |
| | 0 | 374 | | { "twofish256-cbc", new CipherInfo(256, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), p |
| | 0 | 375 | | { "arcfour", new CipherInfo(128, (key, iv) => new Arc4Cipher(key, dischargeFirstBytes: false)) }, |
| | 0 | 376 | | { "arcfour128", new CipherInfo(128, (key, iv) => new Arc4Cipher(key, dischargeFirstBytes: true)) }, |
| | 0 | 377 | | { "arcfour256", new CipherInfo(256, (key, iv) => new Arc4Cipher(key, dischargeFirstBytes: true)) }, |
| | 0 | 378 | | { "cast128-cbc", new CipherInfo(128, (key, iv) => new CastCipher(key, new CbcCipherMode(iv), padding |
| | 2804 | 379 | | }; |
| | | 380 | | |
| | 2804 | 381 | | HmacAlgorithms = new Dictionary<string, HashInfo> |
| | 2804 | 382 | | { |
| | 2804 | 383 | | { "hmac-md5", new HashInfo(16*8, CryptoAbstraction.CreateHMACMD5) }, |
| | 6 | 384 | | { "hmac-md5-96", new HashInfo(16*8, key => CryptoAbstraction.CreateHMACMD5(key, 96)) }, |
| | 2804 | 385 | | { "hmac-sha1", new HashInfo(20*8, CryptoAbstraction.CreateHMACSHA1) }, |
| | 6 | 386 | | { "hmac-sha1-96", new HashInfo(20*8, key => CryptoAbstraction.CreateHMACSHA1(key, 96)) }, |
| | 2804 | 387 | | { "hmac-sha2-256", new HashInfo(32*8, CryptoAbstraction.CreateHMACSHA256) }, |
| | 0 | 388 | | { "hmac-sha2-256-96", new HashInfo(32*8, key => CryptoAbstraction.CreateHMACSHA256(key, 96)) }, |
| | 2804 | 389 | | { "hmac-sha2-512", new HashInfo(64 * 8, CryptoAbstraction.CreateHMACSHA512) }, |
| | 0 | 390 | | { "hmac-sha2-512-96", new HashInfo(64 * 8, key => CryptoAbstraction.CreateHMACSHA512(key, 96)) }, |
| | 2804 | 391 | | { "hmac-ripemd160", new HashInfo(160, CryptoAbstraction.CreateHMACRIPEMD160) }, |
| | 2804 | 392 | | { "hmac-ripemd160@openssh.com", new HashInfo(160, CryptoAbstraction.CreateHMACRIPEMD160) }, |
| | 2804 | 393 | | }; |
| | | 394 | | |
| | 2804 | 395 | | HostKeyAlgorithms = new Dictionary<string, Func<byte[], KeyHostAlgorithm>> |
| | 2804 | 396 | | { |
| | 3 | 397 | | { "ssh-ed25519", data => new KeyHostAlgorithm("ssh-ed25519", new ED25519Key(), data) }, |
| | 0 | 398 | | { "ecdsa-sha2-nistp256", data => new KeyHostAlgorithm("ecdsa-sha2-nistp256", new EcdsaKey(), data) } |
| | 0 | 399 | | { "ecdsa-sha2-nistp384", data => new KeyHostAlgorithm("ecdsa-sha2-nistp384", new EcdsaKey(), data) } |
| | 0 | 400 | | { "ecdsa-sha2-nistp521", data => new KeyHostAlgorithm("ecdsa-sha2-nistp521", new EcdsaKey(), data) } |
| | 2804 | 401 | | #pragma warning disable SA1107 // Code should not contain multiple statements on one line |
| | 4748 | 402 | | { "rsa-sha2-512", data => { var key = new RsaKey(); return new KeyHostAlgorithm("rsa-sha2-512", key, |
| | 12 | 403 | | { "rsa-sha2-256", data => { var key = new RsaKey(); return new KeyHostAlgorithm("rsa-sha2-256", key, |
| | 2804 | 404 | | #pragma warning restore SA1107 // Code should not contain multiple statements on one line |
| | 3 | 405 | | { "ssh-rsa", data => new KeyHostAlgorithm("ssh-rsa", new RsaKey(), data) }, |
| | 3 | 406 | | { "ssh-dss", data => new KeyHostAlgorithm("ssh-dss", new DsaKey(), data) }, |
| | 2804 | 407 | | }; |
| | | 408 | | |
| | 2804 | 409 | | CompressionAlgorithms = new Dictionary<string, Type> |
| | 2804 | 410 | | { |
| | 2804 | 411 | | { "none", null }, |
| | 2804 | 412 | | }; |
| | | 413 | | |
| | 2804 | 414 | | ChannelRequests = new Dictionary<string, RequestInfo> |
| | 2804 | 415 | | { |
| | 2804 | 416 | | { EnvironmentVariableRequestInfo.Name, new EnvironmentVariableRequestInfo() }, |
| | 2804 | 417 | | { ExecRequestInfo.Name, new ExecRequestInfo() }, |
| | 2804 | 418 | | { ExitSignalRequestInfo.Name, new ExitSignalRequestInfo() }, |
| | 2804 | 419 | | { ExitStatusRequestInfo.Name, new ExitStatusRequestInfo() }, |
| | 2804 | 420 | | { PseudoTerminalRequestInfo.Name, new PseudoTerminalRequestInfo() }, |
| | 2804 | 421 | | { ShellRequestInfo.Name, new ShellRequestInfo() }, |
| | 2804 | 422 | | { SignalRequestInfo.Name, new SignalRequestInfo() }, |
| | 2804 | 423 | | { SubsystemRequestInfo.Name, new SubsystemRequestInfo() }, |
| | 2804 | 424 | | { WindowChangeRequestInfo.Name, new WindowChangeRequestInfo() }, |
| | 2804 | 425 | | { X11ForwardingRequestInfo.Name, new X11ForwardingRequestInfo() }, |
| | 2804 | 426 | | { XonXoffRequestInfo.Name, new XonXoffRequestInfo() }, |
| | 2804 | 427 | | { EndOfWriteRequestInfo.Name, new EndOfWriteRequestInfo() }, |
| | 2804 | 428 | | { KeepAliveRequestInfo.Name, new KeepAliveRequestInfo() }, |
| | 2804 | 429 | | }; |
| | | 430 | | |
| | 2804 | 431 | | Host = host; |
| | 2804 | 432 | | Port = port; |
| | 2804 | 433 | | Username = username; |
| | | 434 | | |
| | 2804 | 435 | | ProxyType = proxyType; |
| | 2804 | 436 | | ProxyHost = proxyHost; |
| | 2804 | 437 | | ProxyPort = proxyPort; |
| | 2804 | 438 | | ProxyUsername = proxyUsername; |
| | 2804 | 439 | | ProxyPassword = proxyPassword; |
| | | 440 | | |
| | 2804 | 441 | | AuthenticationMethods = authenticationMethods; |
| | 2804 | 442 | | } |
| | | 443 | | |
| | | 444 | | /// <summary> |
| | | 445 | | /// Authenticates the specified session. |
| | | 446 | | /// </summary> |
| | | 447 | | /// <param name="session">The session to be authenticated.</param> |
| | | 448 | | /// <param name="serviceFactory">The factory to use for creating new services.</param> |
| | | 449 | | /// <exception cref="ArgumentNullException"><paramref name="session"/> is <see langword="null"/>.</exception> |
| | | 450 | | /// <exception cref="ArgumentNullException"><paramref name="serviceFactory"/> is <see langword="null"/>.</except |
| | | 451 | | /// <exception cref="SshAuthenticationException">No suitable authentication method found to complete authenticat |
| | | 452 | | internal void Authenticate(ISession session, IServiceFactory serviceFactory) |
| | 1837 | 453 | | { |
| | 1837 | 454 | | if (serviceFactory is null) |
| | 3 | 455 | | { |
| | 3 | 456 | | throw new ArgumentNullException(nameof(serviceFactory)); |
| | | 457 | | } |
| | | 458 | | |
| | 1834 | 459 | | IsAuthenticated = false; |
| | 1834 | 460 | | var clientAuthentication = serviceFactory.CreateClientAuthentication(); |
| | 1834 | 461 | | clientAuthentication.Authenticate(this, session); |
| | 1818 | 462 | | IsAuthenticated = true; |
| | 1818 | 463 | | } |
| | | 464 | | |
| | | 465 | | /// <summary> |
| | | 466 | | /// Signals that an authentication banner message was received from the server. |
| | | 467 | | /// </summary> |
| | | 468 | | /// <param name="sender">The session in which the banner message was received.</param> |
| | | 469 | | /// <param name="e">The banner message.</param> |
| | | 470 | | void IConnectionInfoInternal.UserAuthenticationBannerReceived(object sender, MessageEventArgs<BannerMessage> e) |
| | 0 | 471 | | { |
| | 0 | 472 | | AuthenticationBanner?.Invoke(this, new AuthenticationBannerEventArgs(Username, e.Message.Message, e.Message. |
| | 0 | 473 | | } |
| | | 474 | | |
| | | 475 | | /// <summary> |
| | | 476 | | /// Creates a <c>none</c> authentication method. |
| | | 477 | | /// </summary> |
| | | 478 | | /// <returns> |
| | | 479 | | /// A <c>none</c> authentication method. |
| | | 480 | | /// </returns> |
| | | 481 | | IAuthenticationMethod IConnectionInfoInternal.CreateNoneAuthenticationMethod() |
| | 1198 | 482 | | { |
| | 1198 | 483 | | return new NoneAuthenticationMethod(Username); |
| | 1198 | 484 | | } |
| | | 485 | | |
| | | 486 | | /// <summary> |
| | | 487 | | /// Gets the supported authentication methods for this connection. |
| | | 488 | | /// </summary> |
| | | 489 | | /// <value> |
| | | 490 | | /// The supported authentication methods for this connection. |
| | | 491 | | /// </value> |
| | | 492 | | IList<IAuthenticationMethod> IConnectionInfoInternal.AuthenticationMethods |
| | | 493 | | { |
| | | 494 | | #pragma warning disable S2365 // Properties should not make collection or array copies |
| | 3591 | 495 | | get { return AuthenticationMethods.Cast<IAuthenticationMethod>().ToList(); } |
| | | 496 | | #pragma warning restore S2365 // Properties should not make collection or array copies |
| | | 497 | | } |
| | | 498 | | } |
| | | 499 | | } |