Commit ed9f600e authored by Steven Fackler's avatar Steven Fackler
Browse files

Make password callback return a Result

parent 387e7825
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -237,7 +237,7 @@ mod test {
        Dsa::private_key_from_pem_callback(key, |password| {
                password_queried = true;
                password[..6].copy_from_slice(b"mypass");
                6
                Ok(6)
            })
            .unwrap();

+1 −1
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ macro_rules! private_key_from_pem {
        pub fn private_key_from_pem_callback<F>(pem: &[u8],
                                                callback: F)
                                                -> Result<$t, ::error::ErrorStack>
            where F: FnOnce(&mut [u8]) -> usize
            where F: FnOnce(&mut [u8]) -> Result<usize, ::error::ErrorStack>
        {
            unsafe {
                ffi::init();
+1 −1
Original line number Diff line number Diff line
@@ -421,7 +421,7 @@ mod test {
        Rsa::private_key_from_pem_callback(key, |password| {
                password_queried = true;
                password[..6].copy_from_slice(b"mypass");
                6
                Ok(6)
            })
            .unwrap();

+8 −2
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ use std::cell::UnsafeCell;
use std::panic::{self, AssertUnwindSafe};
use std::slice;

use error::ErrorStack;

/// Wraps a user-supplied callback and a slot for panics thrown inside the callback (while FFI
/// frames are on the stack).
///
@@ -64,7 +66,7 @@ pub unsafe extern fn invoke_passwd_cb<F>(buf: *mut c_char,
                                         _rwflag: c_int,
                                         cb_state: *mut c_void)
                                         -> 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>);

@@ -74,7 +76,11 @@ pub unsafe extern fn invoke_passwd_cb<F>(buf: *mut c_char,
    }));

    match result {
        Ok(len) => len as c_int,
        Ok(Ok(len)) => len as c_int,
        Ok(Err(_)) => {
            // FIXME restore error stack
            0
        }
        Err(err) => {
            callback.panic = Some(err);
            0