Encapsulates the SSL/TLS algorithms connection information.
Example #1
0
        /// <summary>
        /// Connects to a FTP server using the provided parameters. 
        /// The default representation tipe is set to Binary.
        /// The text encoding is set to UTF8, if supported by the server via the FEAT command.
        /// </summary>
        /// <param name="hostname"></param>
        /// <param name="port"></param>
        /// <param name="credential"></param>
        /// <param name="sslSupportMode"></param>
        /// <param name="userValidateServerCertificate"></param>
        /// <param name="x509ClientCert"></param>
        /// <param name="sslMinKeyExchangeAlgStrength"></param>
        /// <param name="sslMinCipherAlgStrength"></param>
        /// <param name="sslMinHashAlgStrength"></param>
        /// <param name="timeout">Connection timeout in ms. <c>null</c> can be specifiad to keep the default value of 120s.</param>
        /// <param name="useCtrlEndPointAddressForData"><c>true</c> to use the control channel remote address for data connections instead of the address returned by PASV</param>
        /// <param name="dataConnectionMode">Active or Passive data connection mode</param>
        /// <returns>The text of the \"welcome message\" sent by the server.</returns>
        public string Connect(string hostname, int port, NetworkCredential credential, EsslSupportMode sslSupportMode,
                            RemoteCertificateValidationCallback userValidateServerCertificate, X509Certificate x509ClientCert,
                            int sslMinKeyExchangeAlgStrength, int sslMinCipherAlgStrength, int sslMinHashAlgStrength,
                            int? timeout, bool useCtrlEndPointAddressForData, EDataConnectionMode dataConnectionMode)
        {
            Close();

            // Anonymous authentication
            if (credential == null)
                credential = new NetworkCredential(AnonUsername, AnonPassword);

            if (timeout != null)
                _timeout = timeout.Value;

            _sslClientCert = x509ClientCert;

            _userValidateServerCertificate = userValidateServerCertificate;

            _sslMinKeyExchangeAlgStrength = sslMinKeyExchangeAlgStrength;
            _sslMinCipherAlgStrength = sslMinCipherAlgStrength;
            _sslMinHashAlgStrength = sslMinHashAlgStrength;

            _sslSupportRequestedMode = sslSupportMode;
            _sslSupportCurrentMode = sslSupportMode;

            _useCtrlEndPointAddressForData = useCtrlEndPointAddressForData;

            _dataConnectionMode = dataConnectionMode;

            _sslInfo = null;

            _features = null;

            _transferMode = ETransferMode.Ascii;
            _textEncoding = ETextEncoding.Ascii;

            _bannerMessage = null;
            _welcomeMessage = null;

            _currDirStack.Clear();

            // Ok, member initialization is done. Start with setting up a control connection
            SetupCtrlConnection(hostname, port, Encoding.ASCII);

            // Used later for SSL/TLS auth
            _hostname = hostname;

            // Implicit SSL/TLS
            bool isImplicitSsl = (sslSupportMode & EsslSupportMode.Implicit) == EsslSupportMode.Implicit;
            if (isImplicitSsl)
                SwitchCtrlToSslMode();

            // Wait fot server message
            _bannerMessage = GetReply().Message;

            // Explicit SSL/TLS
            if (!isImplicitSsl)
                SslControlChannelCheckExplicitEncryptionRequest(sslSupportMode);

            // Login. Note that a password might not be required
            // TODO: check if the welcomeMessage is returned by the USER command in case the PASS command is not required.
            if (UserCmd(credential.UserName))
                _welcomeMessage = PassCmd(credential.Password);

            GetFeaturesFromServer();

            if (IsControlChannelEncrypted)
                if (!isImplicitSsl) {
                    SslDataChannelCheckExplicitEncryptionRequest();

                    if ((sslSupportMode & EsslSupportMode.ControlChannelRequested) != EsslSupportMode.ControlChannelRequested)
                        SSlCtrlChannelCheckRevertToClearText();
                } else
                    SslDataChannelImplicitEncryptionRequest();

            try {
                // This is required by some FTP servers and must precede any OPTS command
                if (CheckFeature("CLNT"))
                    ClntCmd(ClntName);

                // Set UTF8 as character encoding, but only if listed among the FEAT features
                if (CheckFeature("UTF8"))
                    SetTextEncoding(ETextEncoding.Utf8);
            } catch (Exception) {
                //TODO: add warning info
            }

            // Default binary transfers
            SetTransferMode(ETransferMode.Binary);

            return _welcomeMessage;
        }
Example #2
0
 /// <summary>
 /// Copies the protocol information form the given stream.
 /// </summary>
 /// <param name="sslStream"></param>
 private void SetSslInfo(SslStream sslStream)
 {
     _sslInfo = new SslInfo {
         SslProtocol = sslStream.SslProtocol,
         CipherAlgorithm = sslStream.CipherAlgorithm,
         CipherStrength = sslStream.CipherStrength,
         HashAlgorithm = sslStream.HashAlgorithm,
         HashStrength = sslStream.HashStrength,
         KeyExchangeAlgorithm = sslStream.KeyExchangeAlgorithm,
         KeyExchangeStrength = sslStream.KeyExchangeStrength
     };
 }