ConnectionInfo.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 CipherMode = System.Security.Cryptography.CipherMode;
  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 IOrderedDictionary<string, Func<IKeyExchange>> KeyExchangeAlgorithms { get; }
  47. /// <summary>
  48. /// Gets supported encryptions for this connection.
  49. /// </summary>
  50. public IOrderedDictionary<string, CipherInfo> Encryptions { get; }
  51. /// <summary>
  52. /// Gets supported hash algorithms for this connection.
  53. /// </summary>
  54. public IOrderedDictionary<string, HashInfo> HmacAlgorithms { get; }
  55. /// <summary>
  56. /// Gets supported host key algorithms for this connection.
  57. /// </summary>
  58. public IOrderedDictionary<string, Func<byte[], KeyHostAlgorithm>> HostKeyAlgorithms { get; }
  59. /// <summary>
  60. /// Gets supported authentication methods for this connection.
  61. /// </summary>
  62. public IList<AuthenticationMethod> AuthenticationMethods { get; }
  63. /// <summary>
  64. /// Gets supported compression algorithms for this connection.
  65. /// </summary>
  66. public IOrderedDictionary<string, Func<Compressor>> CompressionAlgorithms { get; }
  67. /// <summary>
  68. /// Gets the supported channel requests for this connection.
  69. /// </summary>
  70. /// <value>
  71. /// The supported channel requests for this connection.
  72. /// </value>
  73. public IDictionary<string, RequestInfo> ChannelRequests { get; }
  74. /// <summary>
  75. /// Gets a value indicating whether connection is authenticated.
  76. /// </summary>
  77. /// <value>
  78. /// <see langword="true"/> if connection is authenticated; otherwise, <see langword="false"/>.
  79. /// </value>
  80. public bool IsAuthenticated { get; private set; }
  81. /// <summary>
  82. /// Gets connection host.
  83. /// </summary>
  84. /// <value>
  85. /// The connection host.
  86. /// </value>
  87. public string Host { get; }
  88. /// <summary>
  89. /// Gets connection port.
  90. /// </summary>
  91. /// <value>
  92. /// The connection port. The default value is 22.
  93. /// </value>
  94. public int Port { get; }
  95. /// <summary>
  96. /// Gets connection username.
  97. /// </summary>
  98. public string Username { get; }
  99. /// <summary>
  100. /// Gets proxy type.
  101. /// </summary>
  102. /// <value>
  103. /// The type of the proxy.
  104. /// </value>
  105. public ProxyTypes ProxyType { get; }
  106. /// <summary>
  107. /// Gets proxy connection host.
  108. /// </summary>
  109. public string ProxyHost { get; }
  110. /// <summary>
  111. /// Gets proxy connection port.
  112. /// </summary>
  113. public int ProxyPort { get; }
  114. /// <summary>
  115. /// Gets proxy connection username.
  116. /// </summary>
  117. public string ProxyUsername { get; }
  118. /// <summary>
  119. /// Gets proxy connection password.
  120. /// </summary>
  121. public string ProxyPassword { get; }
  122. /// <summary>
  123. /// Gets or sets connection timeout.
  124. /// </summary>
  125. /// <value>
  126. /// The connection timeout. The default value is 30 seconds.
  127. /// </value>
  128. public TimeSpan Timeout
  129. {
  130. get
  131. {
  132. return _timeout;
  133. }
  134. set
  135. {
  136. value.EnsureValidTimeout(nameof(Timeout));
  137. _timeout = value;
  138. }
  139. }
  140. /// <summary>
  141. /// Gets or sets the timeout to use when waiting for a server to acknowledge closing a channel.
  142. /// </summary>
  143. /// <value>
  144. /// The channel close timeout. The default value is 1 second.
  145. /// </value>
  146. /// <remarks>
  147. /// If a server does not send a <c>SSH_MSG_CHANNEL_CLOSE</c> message before the specified timeout
  148. /// elapses, the channel will be closed immediately.
  149. /// </remarks>
  150. public TimeSpan ChannelCloseTimeout
  151. {
  152. get
  153. {
  154. return _channelCloseTimeout;
  155. }
  156. set
  157. {
  158. value.EnsureValidTimeout(nameof(ChannelCloseTimeout));
  159. _channelCloseTimeout = value;
  160. }
  161. }
  162. /// <summary>
  163. /// Gets or sets the character encoding.
  164. /// </summary>
  165. /// <value>
  166. /// The character encoding. The default is <see cref="Encoding.UTF8"/>.
  167. /// </value>
  168. public Encoding Encoding { get; set; }
  169. /// <summary>
  170. /// Gets or sets number of retry attempts when session channel creation failed.
  171. /// </summary>
  172. /// <value>
  173. /// The number of retry attempts when session channel creation failed. The default
  174. /// value is 10.
  175. /// </value>
  176. public int RetryAttempts { get; set; }
  177. /// <summary>
  178. /// Gets or sets maximum number of session channels to be open simultaneously.
  179. /// </summary>
  180. /// <value>
  181. /// The maximum number of session channels to be open simultaneously. The default
  182. /// value is 10.
  183. /// </value>
  184. public int MaxSessions { get; set; }
  185. /// <summary>
  186. /// Occurs when authentication banner is sent by the server.
  187. /// </summary>
  188. public event EventHandler<AuthenticationBannerEventArgs> AuthenticationBanner;
  189. /// <summary>
  190. /// Gets the current key exchange algorithm.
  191. /// </summary>
  192. public string CurrentKeyExchangeAlgorithm { get; internal set; }
  193. /// <summary>
  194. /// Gets the current server encryption.
  195. /// </summary>
  196. public string CurrentServerEncryption { get; internal set; }
  197. /// <summary>
  198. /// Gets the current client encryption.
  199. /// </summary>
  200. public string CurrentClientEncryption { get; internal set; }
  201. /// <summary>
  202. /// Gets the current server hash algorithm.
  203. /// </summary>
  204. public string CurrentServerHmacAlgorithm { get; internal set; }
  205. /// <summary>
  206. /// Gets the current client hash algorithm.
  207. /// </summary>
  208. public string CurrentClientHmacAlgorithm { get; internal set; }
  209. /// <summary>
  210. /// Gets the current host key algorithm.
  211. /// </summary>
  212. public string CurrentHostKeyAlgorithm { get; internal set; }
  213. /// <summary>
  214. /// Gets the current server compression algorithm.
  215. /// </summary>
  216. public string CurrentServerCompressionAlgorithm { get; internal set; }
  217. /// <summary>
  218. /// Gets the server version.
  219. /// </summary>
  220. public string ServerVersion { get; internal set; }
  221. /// <summary>
  222. /// Gets the client version.
  223. /// </summary>
  224. public string ClientVersion { get; internal set; }
  225. /// <summary>
  226. /// Gets the current client compression algorithm.
  227. /// </summary>
  228. public string CurrentClientCompressionAlgorithm { get; internal set; }
  229. /// <summary>
  230. /// Initializes a new instance of the <see cref="ConnectionInfo"/> class.
  231. /// </summary>
  232. /// <param name="host">The host.</param>
  233. /// <param name="username">The username.</param>
  234. /// <param name="authenticationMethods">The authentication methods.</param>
  235. /// <exception cref="ArgumentNullException"><paramref name="host"/> is <see langword="null"/>.</exception>
  236. /// <exception cref="ArgumentException"><paramref name="host"/> is a zero-length string.</exception>
  237. /// <exception cref="ArgumentException"><paramref name="username" /> is <see langword="null"/>, a zero-length string or contains only whitespace characters.</exception>
  238. /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <see langword="null"/>.</exception>
  239. /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
  240. public ConnectionInfo(string host, string username, params AuthenticationMethod[] authenticationMethods)
  241. : this(host, DefaultPort, username, ProxyTypes.None, proxyHost: null, 0, proxyUsername: null, proxyPassword: null, authenticationMethods)
  242. {
  243. }
  244. /// <summary>
  245. /// Initializes a new instance of the <see cref="ConnectionInfo"/> class.
  246. /// </summary>
  247. /// <param name="host">The host.</param>
  248. /// <param name="port">The port.</param>
  249. /// <param name="username">The username.</param>
  250. /// <param name="authenticationMethods">The authentication methods.</param>
  251. /// <exception cref="ArgumentNullException"><paramref name="host"/> is <see langword="null"/>.</exception>
  252. /// <exception cref="ArgumentException"><paramref name="username" /> is <see langword="null"/>, a zero-length string or contains only whitespace characters.</exception>
  253. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> 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, params AuthenticationMethod[] authenticationMethods)
  257. : this(host, port, username, ProxyTypes.None, proxyHost: null, 0, proxyUsername: null, proxyPassword: null, authenticationMethods)
  258. {
  259. }
  260. /// <summary>
  261. /// Initializes a new instance of the <see cref="ConnectionInfo" /> class.
  262. /// </summary>
  263. /// <param name="host">Connection host.</param>
  264. /// <param name="port">Connection port.</param>
  265. /// <param name="username">Connection username.</param>
  266. /// <param name="proxyType">Type of the proxy.</param>
  267. /// <param name="proxyHost">The proxy host.</param>
  268. /// <param name="proxyPort">The proxy port.</param>
  269. /// <param name="proxyUsername">The proxy username.</param>
  270. /// <param name="proxyPassword">The proxy password.</param>
  271. /// <param name="authenticationMethods">The authentication methods.</param>
  272. /// <exception cref="ArgumentNullException"><paramref name="host"/> is <see langword="null"/>.</exception>
  273. /// <exception cref="ArgumentException"><paramref name="username" /> is <see langword="null"/>, a zero-length string or contains only whitespace characters.</exception>
  274. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="IPEndPoint.MinPort" /> and <see cref="IPEndPoint.MaxPort" />.</exception>
  275. /// <exception cref="ArgumentNullException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None"/> and <paramref name="proxyHost" /> is <see langword="null"/>.</exception>
  276. /// <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>
  277. /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <see langword="null"/>.</exception>
  278. /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
  279. public ConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params AuthenticationMethod[] authenticationMethods)
  280. {
  281. ThrowHelper.ThrowIfNull(host);
  282. port.ValidatePort();
  283. ThrowHelper.ThrowIfNullOrWhiteSpace(username);
  284. if (proxyType != ProxyTypes.None)
  285. {
  286. ThrowHelper.ThrowIfNull(proxyHost);
  287. proxyPort.ValidatePort();
  288. }
  289. ThrowHelper.ThrowIfNull(authenticationMethods);
  290. if (authenticationMethods.Length == 0)
  291. {
  292. throw new ArgumentException("At least one authentication method should be specified.", nameof(authenticationMethods));
  293. }
  294. // Set default connection values
  295. Timeout = DefaultTimeout;
  296. ChannelCloseTimeout = DefaultChannelCloseTimeout;
  297. RetryAttempts = 10;
  298. MaxSessions = 10;
  299. Encoding = Encoding.UTF8;
  300. KeyExchangeAlgorithms = new OrderedDictionary<string, Func<IKeyExchange>>
  301. {
  302. { "mlkem768x25519-sha256", () => new KeyExchangeMLKem768X25519Sha256() },
  303. { "sntrup761x25519-sha512", () => new KeyExchangeSNtruP761X25519Sha512() },
  304. { "sntrup761x25519-sha512@openssh.com", () => new KeyExchangeSNtruP761X25519Sha512() },
  305. { "curve25519-sha256", () => new KeyExchangeECCurve25519() },
  306. { "curve25519-sha256@libssh.org", () => new KeyExchangeECCurve25519() },
  307. { "ecdh-sha2-nistp256", () => new KeyExchangeECDH256() },
  308. { "ecdh-sha2-nistp384", () => new KeyExchangeECDH384() },
  309. { "ecdh-sha2-nistp521", () => new KeyExchangeECDH521() },
  310. { "diffie-hellman-group-exchange-sha256", () => new KeyExchangeDiffieHellmanGroupExchangeSha256() },
  311. { "diffie-hellman-group-exchange-sha1", () => new KeyExchangeDiffieHellmanGroupExchangeSha1() },
  312. { "diffie-hellman-group16-sha512", () => new KeyExchangeDiffieHellmanGroup16Sha512() },
  313. { "diffie-hellman-group14-sha256", () => new KeyExchangeDiffieHellmanGroup14Sha256() },
  314. { "diffie-hellman-group14-sha1", () => new KeyExchangeDiffieHellmanGroup14Sha1() },
  315. { "diffie-hellman-group1-sha1", () => new KeyExchangeDiffieHellmanGroup1Sha1() },
  316. };
  317. Encryptions = new OrderedDictionary<string, CipherInfo>
  318. {
  319. { "aes128-ctr", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)) },
  320. { "aes192-ctr", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)) },
  321. { "aes256-ctr", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CTR, pkcs7Padding: false)) },
  322. { "aes128-gcm@openssh.com", new CipherInfo(128, (key, iv) => new AesGcmCipher(key, iv, aadLength: 4), isAead: true) },
  323. { "aes256-gcm@openssh.com", new CipherInfo(256, (key, iv) => new AesGcmCipher(key, iv, aadLength: 4), isAead: true) },
  324. { "chacha20-poly1305@openssh.com", new CipherInfo(512, (key, iv) => new ChaCha20Poly1305Cipher(key, aadLength: 4), isAead: true) },
  325. { "aes128-cbc", new CipherInfo(128, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)) },
  326. { "aes192-cbc", new CipherInfo(192, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)) },
  327. { "aes256-cbc", new CipherInfo(256, (key, iv) => new AesCipher(key, iv, AesCipherMode.CBC, pkcs7Padding: false)) },
  328. { "3des-cbc", new CipherInfo(192, (key, iv) => new TripleDesCipher(key, iv, CipherMode.CBC, pkcs7Padding: false)) },
  329. };
  330. HmacAlgorithms = new OrderedDictionary<string, HashInfo>
  331. {
  332. /* Encrypt-and-MAC (encrypt-and-authenticate) variants */
  333. { "hmac-sha2-256", new HashInfo(32*8, key => new HMACSHA256(key)) },
  334. { "hmac-sha2-512", new HashInfo(64*8, key => new HMACSHA512(key)) },
  335. { "hmac-sha1", new HashInfo(20*8, key => new HMACSHA1(key)) },
  336. /* Encrypt-then-MAC variants */
  337. { "hmac-sha2-256-etm@openssh.com", new HashInfo(32*8, key => new HMACSHA256(key), isEncryptThenMAC: true) },
  338. { "hmac-sha2-512-etm@openssh.com", new HashInfo(64*8, key => new HMACSHA512(key), isEncryptThenMAC: true) },
  339. { "hmac-sha1-etm@openssh.com", new HashInfo(20*8, key => new HMACSHA1(key), isEncryptThenMAC: true) },
  340. };
  341. #pragma warning disable SA1107 // Code should not contain multiple statements on one line
  342. var hostAlgs = new OrderedDictionary<string, Func<byte[], KeyHostAlgorithm>>();
  343. 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); });
  344. 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); });
  345. 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); });
  346. 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); });
  347. 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); });
  348. 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); });
  349. 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); });
  350. 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); });
  351. hostAlgs.Add("ssh-ed25519", data => new KeyHostAlgorithm("ssh-ed25519", new ED25519Key(new SshKeyData(data))));
  352. hostAlgs.Add("ecdsa-sha2-nistp256", data => new KeyHostAlgorithm("ecdsa-sha2-nistp256", new EcdsaKey(new SshKeyData(data))));
  353. hostAlgs.Add("ecdsa-sha2-nistp384", data => new KeyHostAlgorithm("ecdsa-sha2-nistp384", new EcdsaKey(new SshKeyData(data))));
  354. hostAlgs.Add("ecdsa-sha2-nistp521", data => new KeyHostAlgorithm("ecdsa-sha2-nistp521", new EcdsaKey(new SshKeyData(data))));
  355. 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)); });
  356. 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)); });
  357. hostAlgs.Add("ssh-rsa", data => new KeyHostAlgorithm("ssh-rsa", new RsaKey(new SshKeyData(data))));
  358. #pragma warning restore SA1107 // Code should not contain multiple statements on one line
  359. HostKeyAlgorithms = hostAlgs;
  360. CompressionAlgorithms = new OrderedDictionary<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. }