Update PKey
This commit is contained in:
parent
d6579ab058
commit
f640613863
|
|
@ -10,6 +10,7 @@ use {cvt, cvt_p};
|
||||||
use pkey::PKey;
|
use pkey::PKey;
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
use x509::X509;
|
use x509::X509;
|
||||||
|
use types::OpenSslType;
|
||||||
|
|
||||||
/// A PKCS #12 archive.
|
/// A PKCS #12 archive.
|
||||||
pub struct Pkcs12(*mut ffi::PKCS12);
|
pub struct Pkcs12(*mut ffi::PKCS12);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
use libc::{c_void, c_char, c_int};
|
use libc::{c_void, c_char, c_int};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::ops::Deref;
|
|
||||||
use ffi;
|
use ffi;
|
||||||
|
|
||||||
use {cvt, cvt_p};
|
use {cvt, cvt_p};
|
||||||
|
|
@ -10,20 +9,11 @@ use dsa::Dsa;
|
||||||
use rsa::{Rsa, RsaRef};
|
use rsa::{Rsa, RsaRef};
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
use util::{CallbackState, invoke_passwd_cb};
|
use util::{CallbackState, invoke_passwd_cb};
|
||||||
use opaque::Opaque;
|
use types::{OpenSslType, Ref};
|
||||||
|
|
||||||
/// A borrowed `PKey`.
|
type_!(PKey, ffi::EVP_PKEY, ffi::EVP_PKEY_free);
|
||||||
pub struct PKeyRef(Opaque);
|
|
||||||
|
|
||||||
impl PKeyRef {
|
|
||||||
pub unsafe fn from_ptr<'a>(ptr: *mut ffi::EVP_PKEY) -> &'a PKeyRef {
|
|
||||||
&*(ptr as *mut _)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_ptr(&self) -> *mut ffi::EVP_PKEY {
|
|
||||||
self as *const _ as *mut _
|
|
||||||
}
|
|
||||||
|
|
||||||
|
impl Ref<PKey> {
|
||||||
/// Get a reference to the interal RSA key for direct access to the key components
|
/// Get a reference to the interal RSA key for direct access to the key components
|
||||||
pub fn rsa(&self) -> Result<Rsa, ErrorStack> {
|
pub fn rsa(&self) -> Result<Rsa, ErrorStack> {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -59,14 +49,11 @@ impl PKeyRef {
|
||||||
Ok(mem_bio.get_buf().to_owned())
|
Ok(mem_bio.get_buf().to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn public_eq(&self, other: &PKeyRef) -> bool {
|
pub fn public_eq(&self, other: &Ref<PKey>) -> bool {
|
||||||
unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 }
|
unsafe { ffi::EVP_PKEY_cmp(self.as_ptr(), other.as_ptr()) == 1 }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents a public key, optionally with a private key attached.
|
|
||||||
pub struct PKey(*mut ffi::EVP_PKEY);
|
|
||||||
|
|
||||||
unsafe impl Send for PKey {}
|
unsafe impl Send for PKey {}
|
||||||
unsafe impl Sync for PKey {}
|
unsafe impl Sync for PKey {}
|
||||||
|
|
||||||
|
|
@ -105,10 +92,6 @@ impl PKey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn from_ptr(handle: *mut ffi::EVP_PKEY) -> PKey {
|
|
||||||
PKey(handle)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Reads private key from PEM, takes ownership of handle
|
/// Reads private key from PEM, takes ownership of handle
|
||||||
pub fn private_key_from_pem(buf: &[u8]) -> Result<PKey, ErrorStack> {
|
pub fn private_key_from_pem(buf: &[u8]) -> Result<PKey, ErrorStack> {
|
||||||
ffi::init();
|
ffi::init();
|
||||||
|
|
@ -166,22 +149,6 @@ impl PKey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for PKey {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
unsafe {
|
|
||||||
ffi::EVP_PKEY_free(self.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for PKey {
|
|
||||||
type Target = PKeyRef;
|
|
||||||
|
|
||||||
fn deref(&self) -> &PKeyRef {
|
|
||||||
unsafe { PKeyRef::from_ptr(self.0) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,9 @@ use dh::Dh;
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
use ssl::{self, SslMethod, SslContextBuilder, SslContext, Ssl, SSL_VERIFY_PEER, SslStream,
|
use ssl::{self, SslMethod, SslContextBuilder, SslContext, Ssl, SSL_VERIFY_PEER, SslStream,
|
||||||
HandshakeError};
|
HandshakeError};
|
||||||
use pkey::PKeyRef;
|
use pkey::PKey;
|
||||||
use x509::X509Ref;
|
use x509::X509Ref;
|
||||||
|
use types::Ref;
|
||||||
|
|
||||||
// apps/dh2048.pem
|
// apps/dh2048.pem
|
||||||
const DHPARAM_PEM: &'static str = r#"
|
const DHPARAM_PEM: &'static str = r#"
|
||||||
|
|
@ -116,7 +117,7 @@ impl SslAcceptorBuilder {
|
||||||
///
|
///
|
||||||
/// [docs]: https://wiki.mozilla.org/Security/Server_Side_TLS
|
/// [docs]: https://wiki.mozilla.org/Security/Server_Side_TLS
|
||||||
pub fn mozilla_intermediate<I>(method: SslMethod,
|
pub fn mozilla_intermediate<I>(method: SslMethod,
|
||||||
private_key: &PKeyRef,
|
private_key: &Ref<PKey>,
|
||||||
certificate: &X509Ref,
|
certificate: &X509Ref,
|
||||||
chain: I)
|
chain: I)
|
||||||
-> Result<SslAcceptorBuilder, ErrorStack>
|
-> Result<SslAcceptorBuilder, ErrorStack>
|
||||||
|
|
@ -151,7 +152,7 @@ impl SslAcceptorBuilder {
|
||||||
///
|
///
|
||||||
/// [docs]: https://wiki.mozilla.org/Security/Server_Side_TLS
|
/// [docs]: https://wiki.mozilla.org/Security/Server_Side_TLS
|
||||||
pub fn mozilla_modern<I>(method: SslMethod,
|
pub fn mozilla_modern<I>(method: SslMethod,
|
||||||
private_key: &PKeyRef,
|
private_key: &Ref<PKey>,
|
||||||
certificate: &X509Ref,
|
certificate: &X509Ref,
|
||||||
chain: I)
|
chain: I)
|
||||||
-> Result<SslAcceptorBuilder, ErrorStack>
|
-> Result<SslAcceptorBuilder, ErrorStack>
|
||||||
|
|
@ -169,7 +170,7 @@ impl SslAcceptorBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finish_setup<I>(mut ctx: SslContextBuilder,
|
fn finish_setup<I>(mut ctx: SslContextBuilder,
|
||||||
private_key: &PKeyRef,
|
private_key: &Ref<PKey>,
|
||||||
certificate: &X509Ref,
|
certificate: &X509Ref,
|
||||||
chain: I)
|
chain: I)
|
||||||
-> Result<SslAcceptorBuilder, ErrorStack>
|
-> Result<SslAcceptorBuilder, ErrorStack>
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ use ec_key::EcKey;
|
||||||
use x509::{X509StoreContextRef, X509FileType, X509, X509Ref, X509VerifyError};
|
use x509::{X509StoreContextRef, X509FileType, X509, X509Ref, X509VerifyError};
|
||||||
#[cfg(any(ossl102, ossl110))]
|
#[cfg(any(ossl102, ossl110))]
|
||||||
use verify::X509VerifyParamRef;
|
use verify::X509VerifyParamRef;
|
||||||
use pkey::PKeyRef;
|
use pkey::PKey;
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
use opaque::Opaque;
|
use opaque::Opaque;
|
||||||
use types::Ref;
|
use types::Ref;
|
||||||
|
|
@ -615,7 +615,7 @@ impl SslContextBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Specifies the private key
|
/// Specifies the private key
|
||||||
pub fn set_private_key(&mut self, key: &PKeyRef) -> Result<(), ErrorStack> {
|
pub fn set_private_key(&mut self, key: &Ref<PKey>) -> Result<(), ErrorStack> {
|
||||||
unsafe { cvt(ffi::SSL_CTX_use_PrivateKey(self.as_ptr(), key.as_ptr())).map(|_| ()) }
|
unsafe { cvt(ffi::SSL_CTX_use_PrivateKey(self.as_ptr(), key.as_ptr())).map(|_| ()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,13 +17,13 @@ use asn1::Asn1Time;
|
||||||
use bio::{MemBio, MemBioSlice};
|
use bio::{MemBio, MemBioSlice};
|
||||||
use crypto::CryptoString;
|
use crypto::CryptoString;
|
||||||
use hash::MessageDigest;
|
use hash::MessageDigest;
|
||||||
use pkey::{PKey, PKeyRef};
|
use pkey::PKey;
|
||||||
use rand::rand_bytes;
|
use rand::rand_bytes;
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
use ffi;
|
use ffi;
|
||||||
use nid::Nid;
|
use nid::Nid;
|
||||||
use opaque::Opaque;
|
use opaque::Opaque;
|
||||||
use types::Ref;
|
use types::{OpenSslType, Ref};
|
||||||
|
|
||||||
#[cfg(ossl10x)]
|
#[cfg(ossl10x)]
|
||||||
use ffi::{X509_set_notBefore, X509_set_notAfter, ASN1_STRING_data};
|
use ffi::{X509_set_notBefore, X509_set_notAfter, ASN1_STRING_data};
|
||||||
|
|
@ -269,7 +269,7 @@ impl X509Generator {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the certificate public-key, then self-sign and return it
|
/// Sets the certificate public-key, then self-sign and return it
|
||||||
pub fn sign(&self, p_key: &PKeyRef) -> Result<X509, ErrorStack> {
|
pub fn sign(&self, p_key: &Ref<PKey>) -> Result<X509, ErrorStack> {
|
||||||
ffi::init();
|
ffi::init();
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -321,7 +321,7 @@ impl X509Generator {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Obtain a certificate signing request (CSR)
|
/// Obtain a certificate signing request (CSR)
|
||||||
pub fn request(&self, p_key: &PKeyRef) -> Result<X509Req, ErrorStack> {
|
pub fn request(&self, p_key: &Ref<PKey>) -> Result<X509Req, ErrorStack> {
|
||||||
let cert = match self.sign(p_key) {
|
let cert = match self.sign(p_key) {
|
||||||
Ok(c) => c,
|
Ok(c) => c,
|
||||||
Err(x) => return Err(x),
|
Err(x) => return Err(x),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue