ConnectionInfo.cs 25 KB

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