Commit c399c247 authored by Jonas Schievink's avatar Jonas Schievink
Browse files

Add RSA::private_key_from_pem_cb

parent 8119f06c
Loading
Loading
Loading
Loading
+22 −1
Original line number Diff line number Diff line
@@ -3,12 +3,13 @@ use std::fmt;
use ssl::error::{SslError, StreamError};
use std::ptr;
use std::io::{self, Read, Write};
use libc::c_int;
use libc::{c_int, c_void};

use bn::BigNum;
use bio::MemBio;
use crypto::HashTypeInternals;
use crypto::hash;
use crypto::util::{CallbackState, invoke_passwd_cb};

pub struct RSA(*mut ffi::RSA);

@@ -76,6 +77,26 @@ impl RSA {
        }
    }

    /// Reads an RSA private key from PEM formatted data and supplies a password callback.
    pub fn private_key_from_pem_cb<R, F>(reader: &mut R, pass_cb: F) -> Result<RSA, SslError>
        where R: Read, F: FnMut(&mut [i8]) -> usize
    {
        let mut cb = CallbackState::new(pass_cb);

        let mut mem_bio = try!(MemBio::new());
        try!(io::copy(reader, &mut mem_bio).map_err(StreamError));

        unsafe {
            let cb_ptr = &mut cb as *mut _ as *mut c_void;
            let rsa = try_ssl_null!(ffi::PEM_read_bio_RSAPrivateKey(mem_bio.get_handle(),
                                                                    ptr::null_mut(),
                                                                    Some(invoke_passwd_cb::<F>),
                                                                    cb_ptr));

            Ok(RSA(rsa))
        }
    }

    /// Writes an RSA private key as unencrypted PEM formatted data
    pub fn private_key_to_pem<W>(&self, writer: &mut W) -> Result<(), SslError>
        where W: Write