Add methods to access private and public part of DSA keys

This commit is contained in:
Moritz Wanzenböck 2018-06-18 11:39:15 +02:00
parent 6440ee04ef
commit 52c942f4b3
1 changed files with 43 additions and 1 deletions

View File

@ -13,7 +13,7 @@ use std::ptr;
use bn::BigNumRef; use bn::BigNumRef;
use error::ErrorStack; use error::ErrorStack;
use pkey::{HasParams, HasPublic, Private, Public}; use pkey::{HasParams, HasPrivate, HasPublic, Private, Public};
use {cvt, cvt_p}; use {cvt, cvt_p};
generic_foreign_type_and_impl_send_sync! { generic_foreign_type_and_impl_send_sync! {
@ -83,6 +83,28 @@ where
public_key_to_der, public_key_to_der,
ffi::i2d_DSA_PUBKEY ffi::i2d_DSA_PUBKEY
} }
/// Returns a reference to the public exponent.
pub fn pub_key(&self) -> &BigNumRef {
unsafe {
let mut pub_key = ptr::null();
DSA_get0_key(self.as_ptr(), &mut pub_key, ptr::null_mut());
BigNumRef::from_ptr(pub_key as *mut _)
}
}
}
impl<T> DsaRef<T>
where
T: HasPrivate,
{
pub fn priv_key(&self) -> &BigNumRef {
unsafe {
let mut priv_key = ptr::null();
DSA_get0_key(self.as_ptr(), ptr::null_mut(), &mut priv_key);
BigNumRef::from_ptr(priv_key as *mut _)
}
}
} }
impl<T> DsaRef<T> impl<T> DsaRef<T>
@ -211,6 +233,26 @@ cfg_if! {
} }
} }
cfg_if! {
if #[cfg(any(ossl110, libressl273))] {
use ffi::DSA_get0_key;
} else {
#[allow(bad_style)]
unsafe fn DSA_get0_pqg(
d: *mut ffi::DSA,
pub_key: *mut *const ffi::BIGNUM,
priv_key: *mut *const ffi::BIGNUM)
{
if !pub_key.is_null() {
*pub_key = (*d).pub_key;
}
if !priv_key.is_null() {
*priv_key = (*d).priv_key;
}
}
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;