From 52c942f4b3d96cff239646d86998b7b898c5e0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Wanzenb=C3=B6ck?= Date: Mon, 18 Jun 2018 11:39:15 +0200 Subject: [PATCH] Add methods to access private and public part of DSA keys --- openssl/src/dsa.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/openssl/src/dsa.rs b/openssl/src/dsa.rs index e8d78dcb..684666cf 100644 --- a/openssl/src/dsa.rs +++ b/openssl/src/dsa.rs @@ -13,7 +13,7 @@ use std::ptr; use bn::BigNumRef; use error::ErrorStack; -use pkey::{HasParams, HasPublic, Private, Public}; +use pkey::{HasParams, HasPrivate, HasPublic, Private, Public}; use {cvt, cvt_p}; generic_foreign_type_and_impl_send_sync! { @@ -83,6 +83,28 @@ where public_key_to_der, 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 DsaRef +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 DsaRef @@ -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)] mod test { use super::*;