alexcrichton / ssh2-rs

Rust bindings for libssh2
https://docs.rs/ssh2
Apache License 2.0
491 stars 148 forks source link

Not support ECDSA key files to login a Ubuntu Server #321

Open nightstream opened 4 months ago

nightstream commented 4 months ago

System:

Windows 11 -> Ubuntu 24.04 LTS ii openssh-server 1:9.6p1-3ubuntu13.4 amd64 secure shell (SSH) server, for secure access from remote machines

version:

ssh2 = "0.9.4"

code

    pub fn login(&mut self) {
        /* 登录 */
        let tcp = TcpStream::connect(format!("{}:{}", self.host, self.port)).expect(format!("TCP创建失败: {}:{}", self.host, self.port).as_str());
        self.session.set_tcp_stream(tcp);
        self.session.handshake().expect(format!("TCP握手失败: {}:{}", self.host, self.port).as_str());

        if self.prikey.chars().count() > 0 {
            // 使用证书认证
            let prikeyf = Path::new(self.prikey.as_str());
            self.session.userauth_pubkey_file(&self.username, None, prikeyf, Some(&self.password)).expect("证书认证失败");
        } else {
            // 使用密码认证
            self.session.userauth_password(&self.username, &self.password).expect("密码认证失败");
        }
        assert!(self.session.authenticated());
    }

If I use an RSA key file, it works well, but when I use an ECDSA key file, it runs into an error like this:

证书认证失败: Error { code: Session(-1), msg: "unknown error" }
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library\std\src\panicking.rs:645
   1: core::panicking::panic_fmt
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library\core\src\panicking.rs:72
   2: core::result::unwrap_failed
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library\core\src\result.rs:1654
   3: enum2$<core::result::Result<tuple$<>,ssh2::error::Error> >::expect<tuple$<>,ssh2::error::Error>
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6\library\core\src\result.rs:1034
   4: sync_ssh_dir::sshdirsyncer::SshdirSyncer::login
             at .\src\sshdirsyncer.rs:93
   5: sync_ssh_dir::sshdirsyncer::SshdirSyncer::run_sync
             at .\src\sshdirsyncer.rs:260
   6: sync_ssh_dir::main
             at .\src\main.rs:14
   7: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6\library\core\src\ops\function.rs:250
   8: core::hint::black_box
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6\library\core\src\hint.rs:337
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: process didn't exit successfully: `target\debug\sync_ssh_dir.exe` (exit code: 101)
yodaldevoid commented 2 weeks ago

This may have been fixed by #334.