Skip to content

Commit

Permalink
added test, addressed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Goirad committed Aug 29, 2019
1 parent ff14f11 commit 3b4b46e
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/crypt_prov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl<'a> ImportOptions<'a> {
}
}

/// Imports a DER-encoded PKCS8 pricate key.
/// Imports a DER-encoded PKCS8 private key.
pub fn import_pkcs8(&mut self, der: &[u8]) -> io::Result<CryptKey> {
unsafe {
assert!(der.len() <= winapi::DWORD::max_value() as usize);
Expand Down Expand Up @@ -238,13 +238,13 @@ impl<'a> ImportOptions<'a> {
ptr::null_mut(),
&mut buf2 as *mut _ as winapi::LPVOID,
&mut len2);
winbase::LocalFree(buf as *mut _);
if res == winapi::FALSE {
return Err(io::Error::last_os_error());
}

let mut key = 0;
let res = wincrypt::CryptImportKey(self.prov.0, buf2, len2, 0, self.flags, &mut key);
winbase::LocalFree(buf as *mut _);
winbase::LocalFree(buf2 as *mut _);
if res == winapi::TRUE {
Ok(CryptKey::from_inner(key))
Expand All @@ -258,6 +258,7 @@ impl<'a> ImportOptions<'a> {
#[cfg(test)]
mod test {
use super::*;
use winapi::shared::ntdef;

#[test]
fn rsa_key() {
Expand All @@ -271,4 +272,35 @@ mod test {
.import(key)
.unwrap();
}

#[test]
fn pkcs8_key() {
let key = include_str!("../test/key.pem");
let der = unsafe {
let mut len = 0;
assert!(wincrypt::CryptStringToBinaryA(key.as_ptr() as ntdef::LPCSTR,
key.len() as winapi::DWORD,
wincrypt::CRYPT_STRING_BASE64HEADER,
ptr::null_mut(),
&mut len,
ptr::null_mut(),
ptr::null_mut()) == winapi::TRUE);
let mut buf = vec![0; len as usize];
assert!(wincrypt::CryptStringToBinaryA(key.as_ptr() as ntdef::LPCSTR,
key.len() as winapi::DWORD,
wincrypt::CRYPT_STRING_BASE64HEADER,
buf.as_mut_ptr(),
&mut len,
ptr::null_mut(),
ptr::null_mut()) == winapi::TRUE);
buf
};
let mut context = AcquireOptions::new()
.verify_context(true)
.acquire(ProviderType::rsa_full())
.unwrap();
context.import()
.import_pkcs8(&der)
.unwrap();
}
}

0 comments on commit 3b4b46e

Please sign in to comment.