/// <summary>
        /// Converts an SSH key between .pem and opensshv2 formats
        /// </summary>
        /// <returns>conversion succeeded</returns>
        public bool Convert()
        {
            // get the file contents
            string fileContent = string.Empty;
            using (var reader = new StreamReader(_privateKeyFile))
            {
                fileContent = reader.ReadToEnd();
            }
            // check to see whether this has worked
            var key = new SshKey {Password = _password};
            // get the ssh key
            bool converted = key.FromOpenSshPrivateKey(fileContent);
            // if this hasn't worked then just drop out of this method
            if (!converted)
                return false;
            // update the ssh key
            string content = key.ToOpenSshPrivateKey(false);
            // get the filename without the key extension
            string openSsh2Filename = Path.GetFileNameWithoutExtension(_privateKeyFile) + ".pvk";
            // get the path
            string openSsh2Directory = Path.GetDirectoryName(_privateKeyFile);

            // get the full path
            KeyFilePath = Path.Combine(openSsh2Directory, openSsh2Filename);
            // create the file
            using(var writer = new StreamWriter(File.Create(KeyFilePath)))
            {
                writer.Write(content);
            }
            // if that worked return out of this
            return true;
        }
示例#2
0
        /// <summary>
        /// Converts an SSH key between .pem and opensshv2 formats
        /// </summary>
        /// <returns>conversion succeeded</returns>
        public bool Convert()
        {
            // get the file contents
            string fileContent = string.Empty;

            using (var reader = new StreamReader(_privateKeyFile))
            {
                fileContent = reader.ReadToEnd();
            }
            // check to see whether this has worked
            var key = new SshKey {
                Password = _password
            };
            // get the ssh key
            bool converted = key.FromOpenSshPrivateKey(fileContent);

            // if this hasn't worked then just drop out of this method
            if (!converted)
            {
                return(false);
            }
            // update the ssh key
            string content = key.ToOpenSshPrivateKey(false);
            // get the filename without the key extension
            string openSsh2Filename = Path.GetFileNameWithoutExtension(_privateKeyFile) + ".pvk";
            // get the path
            string openSsh2Directory = Path.GetDirectoryName(_privateKeyFile);

            // get the full path
            KeyFilePath = Path.Combine(openSsh2Directory, openSsh2Filename);
            // create the file
            using (var writer = new StreamWriter(File.Create(KeyFilePath)))
            {
                writer.Write(content);
            }
            // if that worked return out of this
            return(_converted = true);
        }
        internal SftpLogFileInfoChilkat(SftpFileSystem sftFileSystem, Uri fileUri, ILogExpertLogger logger)
        {
            _logger         = logger;
            _sftFileSystem  = sftFileSystem;
            Uri             = fileUri;
            _remoteFileName = Uri.PathAndQuery;

            //  Any string automatically begins a fully-functional 30-day trial.
            bool success;

            success = _sftp.UnlockComponent("PUT_SERIAL_HERE");
            if (success != true)
            {
                _logger.LogError(_sftp.LastErrorText);
                MessageBox.Show(_sftp.LastErrorText);
                return;
            }

            int port = Uri.Port != -1 ? Uri.Port : 22;

            success = _sftp.Connect(Uri.Host, port);
            if (success != true)
            {
                _logger.LogError(_sftp.LastErrorText);
                MessageBox.Show(_sftp.LastErrorText);
                return;
            }

            success = false;
            bool cancelled = false;

            if (_sftFileSystem.ConfigData.UseKeyfile)
            {
                lock (_sshKeyMonitor) // prevent multiple password dialogs when opening multiple files at once
                {
                    while (_sftFileSystem.SshKey == null)
                    {
                        // Load key from file, possibly encrypted by password

                        SshKey sshKey  = new SshKey();
                        string keyText = sshKey.LoadText(_sftFileSystem.ConfigData.KeyFile);

                        PrivateKeyPasswordDialog dlg          = new PrivateKeyPasswordDialog();
                        DialogResult             dialogResult = dlg.ShowDialog();
                        if (dialogResult == DialogResult.Cancel)
                        {
                            cancelled = true;
                            break;
                        }

                        sshKey.Password = dlg.Password;
                        if (_sftFileSystem.ConfigData.KeyType == KeyType.Ssh)
                        {
                            logger.Info("Loading SSH key from " + _sftFileSystem.ConfigData.KeyFile);
                            success = sshKey.FromOpenSshPrivateKey(keyText);
                        }
                        else
                        {
                            logger.Info("Loading Putty key from " + _sftFileSystem.ConfigData.KeyFile);
                            success = sshKey.FromPuttyPrivateKey(keyText);
                        }

                        if (!success)
                        {
                            MessageBox.Show("Loading key file failed");
                        }
                        else
                        {
                            _sftFileSystem.SshKey = sshKey;
                        }
                    }
                }

                if (!cancelled)
                {
                    success = false;
                    Credentials credentials = _sftFileSystem.GetCredentials(Uri, true, true);
                    while (!success)
                    {
                        success = _sftp.AuthenticatePk(credentials.UserName, _sftFileSystem.SshKey);
                        if (!success)
                        {
                            FailedKeyDialog dlg = new FailedKeyDialog();
                            DialogResult    res = dlg.ShowDialog();
                            dlg.Dispose();
                            if (res == DialogResult.Cancel)
                            {
                                return;
                            }

                            if (res == DialogResult.OK)
                            {
                                break; // go to user/pw auth
                            }

                            // retries with disabled cache
                            credentials = _sftFileSystem.GetCredentials(Uri, false, true);
                        }
                    }
                }
            }

            if (!success)
            {
                // username/password auth

                Credentials credentials = _sftFileSystem.GetCredentials(Uri, true, false);
                success = _sftp.AuthenticatePw(credentials.UserName, credentials.Password);
                if (success != true)
                {
                    // first fail -> try again with disabled cache
                    credentials = _sftFileSystem.GetCredentials(Uri, false, false);
                    success     = _sftp.AuthenticatePw(credentials.UserName, credentials.Password);
                    if (success != true)
                    {
                        // 2nd fail -> abort
                        MessageBox.Show("Authentication failed!");
                        //MessageBox.Show(sftp.LastErrorText);
                        return;
                    }
                }
            }

            success = _sftp.InitializeSftp();
            if (success != true)
            {
                _logger.LogError(_sftp.LastErrorText);
                MessageBox.Show(_sftp.LastErrorText);
                return;
            }

            OriginalLength = _lastLength = Length;
        }