2
0

ConnectionInfo.cs 25 KB

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