From 8528c093bd703e91ca2821676de9a59dcba2581b Mon Sep 17 00:00:00 2001 From: Eldad Zack Date: Thu, 15 Feb 2018 21:39:46 +0100 Subject: Support in-memory keypair. --- src/session.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src') 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, -- cgit v1.2.3