Make password callback return a Result

This commit is contained in:
Steven Fackler 2016-11-13 16:18:52 +00:00
parent 387e78257b
commit ed9f600e28
4 changed files with 11 additions and 5 deletions

View File

@ -237,7 +237,7 @@ mod test {
Dsa::private_key_from_pem_callback(key, |password| { Dsa::private_key_from_pem_callback(key, |password| {
password_queried = true; password_queried = true;
password[..6].copy_from_slice(b"mypass"); password[..6].copy_from_slice(b"mypass");
6 Ok(6)
}) })
.unwrap(); .unwrap();

View File

@ -81,7 +81,7 @@ macro_rules! private_key_from_pem {
pub fn private_key_from_pem_callback<F>(pem: &[u8], pub fn private_key_from_pem_callback<F>(pem: &[u8],
callback: F) callback: F)
-> Result<$t, ::error::ErrorStack> -> Result<$t, ::error::ErrorStack>
where F: FnOnce(&mut [u8]) -> usize where F: FnOnce(&mut [u8]) -> Result<usize, ::error::ErrorStack>
{ {
unsafe { unsafe {
ffi::init(); ffi::init();

View File

@ -421,7 +421,7 @@ mod test {
Rsa::private_key_from_pem_callback(key, |password| { Rsa::private_key_from_pem_callback(key, |password| {
password_queried = true; password_queried = true;
password[..6].copy_from_slice(b"mypass"); password[..6].copy_from_slice(b"mypass");
6 Ok(6)
}) })
.unwrap(); .unwrap();

View File

@ -4,6 +4,8 @@ use std::cell::UnsafeCell;
use std::panic::{self, AssertUnwindSafe}; use std::panic::{self, AssertUnwindSafe};
use std::slice; use std::slice;
use error::ErrorStack;
/// Wraps a user-supplied callback and a slot for panics thrown inside the callback (while FFI /// Wraps a user-supplied callback and a slot for panics thrown inside the callback (while FFI
/// frames are on the stack). /// frames are on the stack).
/// ///
@ -64,7 +66,7 @@ pub unsafe extern fn invoke_passwd_cb<F>(buf: *mut c_char,
_rwflag: c_int, _rwflag: c_int,
cb_state: *mut c_void) cb_state: *mut c_void)
-> c_int -> c_int
where F: FnOnce(&mut [u8]) -> usize where F: FnOnce(&mut [u8]) -> Result<usize, ErrorStack>
{ {
let callback = &mut *(cb_state as *mut CallbackState<F>); let callback = &mut *(cb_state as *mut CallbackState<F>);
@ -74,7 +76,11 @@ pub unsafe extern fn invoke_passwd_cb<F>(buf: *mut c_char,
})); }));
match result { match result {
Ok(len) => len as c_int, Ok(Ok(len)) => len as c_int,
Ok(Err(_)) => {
// FIXME restore error stack
0
}
Err(err) => { Err(err) => {
callback.panic = Some(err); callback.panic = Some(err);
0 0