diff options
author | Alex Crichton <alex@alexcrichton.com> | 2018-02-16 21:29:45 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-16 21:29:45 -0600 |
commit | 00067c96dd60a1e7180895886f0bac3761a3f40c (patch) | |
tree | 762eed73a323228dade0d2311b38f66975c444ae /src | |
parent | 3c521f49847f4145c682b9e1675266ed59c69881 (diff) | |
parent | 8528c093bd703e91ca2821676de9a59dcba2581b (diff) | |
download | ssh2-rs-00067c96dd60a1e7180895886f0bac3761a3f40c.zip |
Merge pull request #80 from eldad/master
Support in-memory keypair.
Diffstat (limited to 'src')
-rw-r--r-- | src/session.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/session.rs b/src/session.rs index e3566d1..c9dd266 100644 --- a/src/session.rs +++ b/src/session.rs @@ -5,6 +5,8 @@ use std::path::Path; use std::slice; use std::str; use libc::{self, c_uint, c_int, c_void, c_long}; +#[cfg(unix)] +use libc::size_t; use {raw, Error, DisconnectCode, ByApplication, HostKeyType}; use {MethodType, Agent, Channel, Listener, HashType, KnownHosts, Sftp}; @@ -219,6 +221,40 @@ impl Session { }) } + /// Attempt public key authentication using a PEM encoded private key from + /// memory. Public key is computed from private key if none passed. + /// This is available only for `unix` targets, as it relies on openssl. + /// It is therefore recommended to use `#[cfg(unix)]` or otherwise test for + /// the `unix` compliation target when using this function. + #[cfg(unix)] + pub fn userauth_pubkey_memory(&self, + username: &str, + pubkeydata: Option<&str>, + privatekeydata: &str, + passphrase: Option<&str>) -> Result<(), Error> { + let (pubkeydata, pubkeydata_len) = match pubkeydata { + Some(s) => (Some(try!(CString::new(s))), s.len()), + None => (None, 0), + }; + let privatekeydata_len = privatekeydata.len(); + let privatekeydata = try!(CString::new(privatekeydata)); + let passphrase = match passphrase { + Some(s) => Some(try!(CString::new(s))), + None => None, + }; + self.rc(unsafe { + raw::libssh2_userauth_publickey_frommemory(self.raw, + username.as_ptr() as *const _, + username.len() as size_t, + pubkeydata.as_ref().map(|s| s.as_ptr()).unwrap_or(0 as *const _), + pubkeydata_len as size_t, + privatekeydata.as_ptr(), + privatekeydata_len as size_t, + passphrase.as_ref().map(|s| s.as_ptr()) + .unwrap_or(0 as *const _)) + }) + } + // Umm... I wish this were documented in libssh2? #[allow(missing_docs)] pub fn userauth_hostbased_file(&self, |