Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConnectionInfo" /> class.
        /// </summary>
        /// <param name="host">Connection host.</param>
        /// <param name="port">Connection port.</param>
        /// <param name="username">Connection username.</param>
        /// <param name="proxyType">Type of the proxy.</param>
        /// <param name="proxyHost">The proxy host.</param>
        /// <param name="proxyPort">The proxy port.</param>
        /// <param name="proxyUsername">The proxy username.</param>
        /// <param name="proxyPassword">The proxy password.</param>
        /// <param name="authenticationMethods">The authentication methods.</param>
        /// <exception cref="ArgumentNullException"><paramref name="host"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="username" /> is null, a zero-length string or contains only whitespace characters.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None"/> and <paramref name="proxyHost" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="proxyType"/> is not <see cref="ProxyTypes.None"/> and <paramref name="proxyPort" /> is not within <see cref="F:System.Net.IPEndPoint.MinPort" /> and <see cref="F:System.Net.IPEndPoint.MaxPort" />.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="authenticationMethods"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
        public ConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params AuthenticationMethod[] authenticationMethods)
        {
            if (host == null)
            {
                throw new ArgumentNullException("host");
            }
            port.ValidatePort("port");

            if (username == null)
            {
                throw new ArgumentNullException("username");
            }
            if (username.All(char.IsWhiteSpace))
            {
                throw new ArgumentException("Cannot be empty or contain only whitespace.", "username");
            }

            if (proxyType != ProxyTypes.None)
            {
                if (proxyHost == null)
                {
                    throw new ArgumentNullException("proxyHost");
                }
                proxyPort.ValidatePort("proxyPort");
            }

            if (authenticationMethods == null)
            {
                throw new ArgumentNullException("authenticationMethods");
            }
            if (authenticationMethods.Length == 0)
            {
                throw new ArgumentException("At least one authentication method should be specified.", "authenticationMethods");
            }

            //  Set default connection values
            Timeout       = TimeSpan.FromSeconds(30);
            RetryAttempts = 10;
            MaxSessions   = 10;
            Encoding      = Encoding.UTF8;

            KeyExchangeAlgorithms = new Dictionary <string, Type>
            {
                { "diffie-hellman-group-exchange-sha256", typeof(KeyExchangeDiffieHellmanGroupExchangeSha256) },
                { "diffie-hellman-group-exchange-sha1", typeof(KeyExchangeDiffieHellmanGroupExchangeSha1) },
                { "diffie-hellman-group14-sha1", typeof(KeyExchangeDiffieHellmanGroup14Sha1) },
                { "diffie-hellman-group1-sha1", typeof(KeyExchangeDiffieHellmanGroup1Sha1) },
                //{"ecdh-sha2-nistp256", typeof(KeyExchangeEllipticCurveDiffieHellman)},
                //{"ecdh-sha2-nistp256", typeof(...)},
                //{"ecdh-sha2-nistp384", typeof(...)},
                //{"ecdh-sha2-nistp521", typeof(...)},
                //"gss-group1-sha1-toWM5Slw5Ew8Mqkay+al2g==" - WinSSHD
                //"gss-gex-sha1-toWM5Slw5Ew8Mqkay+al2g==" - WinSSHD
            };

            Encryptions = new Dictionary <string, CipherInfo>
            {
                { "aes256-ctr", new CipherInfo(256, (key, iv) => new AesCipher(key, new CtrCipherMode(iv), null)) },
                { "3des-cbc", new CipherInfo(192, (key, iv) => new TripleDesCipher(key, new CbcCipherMode(iv), null)) },
                { "aes128-cbc", new CipherInfo(128, (key, iv) => new AesCipher(key, new CbcCipherMode(iv), null)) },
                { "aes192-cbc", new CipherInfo(192, (key, iv) => new AesCipher(key, new CbcCipherMode(iv), null)) },
                { "aes256-cbc", new CipherInfo(256, (key, iv) => new AesCipher(key, new CbcCipherMode(iv), null)) },
                { "blowfish-cbc", new CipherInfo(128, (key, iv) => new BlowfishCipher(key, new CbcCipherMode(iv), null)) },
                { "twofish-cbc", new CipherInfo(256, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), null)) },
                { "twofish192-cbc", new CipherInfo(192, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), null)) },
                { "twofish128-cbc", new CipherInfo(128, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), null)) },
                { "twofish256-cbc", new CipherInfo(256, (key, iv) => new TwofishCipher(key, new CbcCipherMode(iv), null)) },
                ////{"serpent256-cbc", typeof(CipherSerpent256CBC)},
                ////{"serpent192-cbc", typeof(...)},
                ////{"serpent128-cbc", typeof(...)},
                { "arcfour", new CipherInfo(128, (key, iv) => new Arc4Cipher(key, false)) },
                { "arcfour128", new CipherInfo(128, (key, iv) => new Arc4Cipher(key, true)) },
                { "arcfour256", new CipherInfo(256, (key, iv) => new Arc4Cipher(key, true)) },
                ////{"idea-cbc", typeof(...)},
                { "cast128-cbc", new CipherInfo(128, (key, iv) => new CastCipher(key, new CbcCipherMode(iv), null)) },
                ////{"*****@*****.**", typeof(...)},
                { "aes128-ctr", new CipherInfo(128, (key, iv) => new AesCipher(key, new CtrCipherMode(iv), null)) },
                { "aes192-ctr", new CipherInfo(192, (key, iv) => new AesCipher(key, new CtrCipherMode(iv), null)) },
            };

            HmacAlgorithms = new Dictionary <string, HashInfo>
            {
                { "hmac-md5", new HashInfo(16 * 8, CryptoAbstraction.CreateHmacmd5) },
                { "hmac-md5-96", new HashInfo(16 * 8, key => CryptoAbstraction.CreateHmacmd5(key, 96)) },
                { "hmac-sha1", new HashInfo(20 * 8, CryptoAbstraction.CreateHmacsha1) },
                { "hmac-sha1-96", new HashInfo(20 * 8, key => CryptoAbstraction.CreateHmacsha1(key, 96)) },
                { "hmac-sha2-256", new HashInfo(32 * 8, CryptoAbstraction.CreateHmacsha256) },
                { "hmac-sha2-256-96", new HashInfo(32 * 8, key => CryptoAbstraction.CreateHmacsha256(key, 96)) },
                { "hmac-sha2-512", new HashInfo(64 * 8, CryptoAbstraction.CreateHmacsha512) },
                { "hmac-sha2-512-96", new HashInfo(64 * 8, key => CryptoAbstraction.CreateHmacsha512(key, 96)) },
                //{"*****@*****.**", typeof(HMacSha1)},
                { "hmac-ripemd160", new HashInfo(160, CryptoAbstraction.CreateHmacripemd160) },
                { "*****@*****.**", new HashInfo(160, CryptoAbstraction.CreateHmacripemd160) },
                //{"none", typeof(...)},
            };

            HostKeyAlgorithms = new Dictionary <string, Func <byte[], KeyHostAlgorithm> >
            {
                { "ssh-rsa", data => new KeyHostAlgorithm("ssh-rsa", new RsaKey(), data) },
                { "ssh-dss", data => new KeyHostAlgorithm("ssh-dss", new DsaKey(), data) },
                //{"ecdsa-sha2-nistp256 "}
                //{"x509v3-sign-rsa", () => { ... },
                //{"x509v3-sign-dss", () => { ... },
                //{"spki-sign-rsa", () => { ... },
                //{"spki-sign-dss", () => { ... },
                //{"pgp-sign-rsa", () => { ... },
                //{"pgp-sign-dss", () => { ... },
            };

            CompressionAlgorithms = new Dictionary <string, Type>
            {
                //{"*****@*****.**", typeof(ZlibOpenSsh)},
                //{"zlib", typeof(Zlib)},
                { "none", null },
            };

            ChannelRequests = new Dictionary <string, RequestInfo>
            {
                { EnvironmentVariableRequestInfo.Name, new EnvironmentVariableRequestInfo() },
                { ExecRequestInfo.Name, new ExecRequestInfo() },
                { ExitSignalRequestInfo.Name, new ExitSignalRequestInfo() },
                { ExitStatusRequestInfo.Name, new ExitStatusRequestInfo() },
                { PseudoTerminalRequestInfo.Name, new PseudoTerminalRequestInfo() },
                { ShellRequestInfo.Name, new ShellRequestInfo() },
                { SignalRequestInfo.Name, new SignalRequestInfo() },
                { SubsystemRequestInfo.Name, new SubsystemRequestInfo() },
                { WindowChangeRequestInfo.Name, new WindowChangeRequestInfo() },
                { X11ForwardingRequestInfo.Name, new X11ForwardingRequestInfo() },
                { XonXoffRequestInfo.Name, new XonXoffRequestInfo() },
                { EndOfWriteRequestInfo.Name, new EndOfWriteRequestInfo() },
                { KeepAliveRequestInfo.Name, new KeepAliveRequestInfo() },
            };

            Host     = host;
            Port     = port;
            Username = username;

            ProxyType     = proxyType;
            ProxyHost     = proxyHost;
            ProxyPort     = proxyPort;
            ProxyUsername = proxyUsername;
            ProxyPassword = proxyPassword;

            AuthenticationMethods = authenticationMethods;
        }