Commit 50d8cca6 authored by Steven Fackler's avatar Steven Fackler Committed by GitHub
Browse files

Merge pull request #507 from sfackler/ref

Add a unified Ref type
parents f2295531 ab30ad0c
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -1658,12 +1658,14 @@ extern {

    pub fn X509_EXTENSION_free(ext: *mut X509_EXTENSION);

    pub fn X509_NAME_free(x: *mut X509_NAME);
    pub fn X509_NAME_add_entry_by_txt(x: *mut X509_NAME, field: *const c_char, ty: c_int, bytes: *const c_uchar, len: c_int, loc: c_int, set: c_int) -> c_int;
    pub fn X509_NAME_get_index_by_NID(n: *mut X509_NAME, nid: c_int, last_pos: c_int) ->c_int;

    pub fn ASN1_STRING_length(x: *const ASN1_STRING) -> c_int;

    pub fn X509_STORE_CTX_get_current_cert(ct: *mut X509_STORE_CTX) -> *mut X509;
    pub fn X509_STORE_CTX_free(ctx: *mut X509_STORE_CTX);
    pub fn X509_STORE_CTX_get_current_cert(ctx: *mut X509_STORE_CTX) -> *mut X509;
    pub fn X509_STORE_CTX_get_error(ctx: *mut X509_STORE_CTX) -> c_int;
    pub fn X509_STORE_CTX_get_ex_data(ctx: *mut X509_STORE_CTX, idx: c_int) -> *mut c_void;
    pub fn X509_STORE_CTX_get_error_depth(ctx: *mut X509_STORE_CTX) -> c_int;
@@ -1675,6 +1677,8 @@ extern {
    pub fn X509_REQ_add_extensions(req: *mut X509_REQ, exts: *mut stack_st_X509_EXTENSION) -> c_int;
    pub fn X509_REQ_sign(x: *mut X509_REQ, pkey: *mut EVP_PKEY, md: *const EVP_MD) -> c_int;

    #[cfg(not(ossl101))]
    pub fn X509_VERIFY_PARAM_free(param: *mut X509_VERIFY_PARAM);
    #[cfg(not(ossl101))]
    pub fn X509_VERIFY_PARAM_set_hostflags(param: *mut X509_VERIFY_PARAM, flags: c_uint);
    #[cfg(not(ossl101))]
+3 −41
Original line number Diff line number Diff line
use libc::c_long;
use std::{ptr, fmt};
use std::ops::Deref;

use ffi;
use opaque::Opaque;

use {cvt, cvt_p};
use bio::MemBio;
use error::ErrorStack;
use types::{OpenSslType, Ref};

/// A borrowed Asn1Time
pub struct Asn1TimeRef(Opaque);

impl Asn1TimeRef {
    /// Creates a new `Asn1TimeRef` wrapping the provided handle.
    pub unsafe fn from_ptr<'a>(handle: *mut ffi::ASN1_TIME) -> &'a Asn1TimeRef {
        &*(handle as *mut _)
    }
type_!(Asn1Time, ffi::ASN1_TIME, ffi::ASN1_TIME_free);

    /// Returns the raw handle
    pub fn as_ptr(&self) -> *mut ffi::ASN1_TIME {
        self as *const _ as *mut _
    }
}

impl fmt::Display for Asn1TimeRef {
impl fmt::Display for Ref<Asn1Time> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mem_bio = try!(MemBio::new());
        let as_str = unsafe {
@@ -35,16 +20,7 @@ impl fmt::Display for Asn1TimeRef {
    }
}


/// Corresponds to the ASN.1 structure Time defined in RFC5280
pub struct Asn1Time(*mut ffi::ASN1_TIME);

impl Asn1Time {
    /// Wraps existing ASN1_TIME and takes ownership
    pub unsafe fn from_ptr(handle: *mut ffi::ASN1_TIME) -> Asn1Time {
        Asn1Time(handle)
    }

    fn from_period(period: c_long) -> Result<Asn1Time, ErrorStack> {
        ffi::init();

@@ -59,17 +35,3 @@ impl Asn1Time {
        Asn1Time::from_period(days as c_long * 60 * 60 * 24)
    }
}

impl Deref for Asn1Time {
    type Target = Asn1TimeRef;

    fn deref(&self) -> &Asn1TimeRef {
        unsafe { Asn1TimeRef::from_ptr(self.0) }
    }
}

impl Drop for Asn1Time {
    fn drop(&mut self) {
        unsafe { ffi::ASN1_TIME_free(self.as_ptr()) };
    }
}
+93 −153

File changed.

Preview size limit exceeded, changes collapsed.

+2 −29
Original line number Diff line number Diff line
@@ -3,25 +3,12 @@ use error::ErrorStack;
use bio::MemBioSlice;
use std::ptr;
use std::mem;
use std::ops::Deref;

use {cvt, cvt_p};
use bn::BigNum;
use opaque::Opaque;
use types::OpenSslType;

pub struct DhRef(Opaque);

impl DhRef {
    pub unsafe fn from_ptr<'a>(ptr: *mut ffi::DH) -> &'a DhRef {
        &*(ptr as *mut _)
    }

    pub fn as_ptr(&self) -> *mut ffi::DH {
        self as *const _ as *mut _
    }
}

pub struct Dh(*mut ffi::DH);
type_!(Dh, ffi::DH, ffi::DH_free);

impl Dh {
    pub fn from_params(p: BigNum, g: BigNum, q: BigNum) -> Result<Dh, ErrorStack> {
@@ -63,20 +50,6 @@ impl Dh {
    }
}

impl Drop for Dh {
    fn drop(&mut self) {
        unsafe { ffi::DH_free(self.0) }
    }
}

impl Deref for Dh {
    type Target = DhRef;

    fn deref(&self) -> &DhRef {
        unsafe { DhRef::from_ptr(self.0) }
    }
}

#[cfg(ossl110)]
mod compat {
    pub use ffi::DH_set0_pqg;
+15 −46
Original line number Diff line number Diff line
@@ -2,26 +2,17 @@ use error::ErrorStack;
use ffi;
use libc::{c_int, c_char, c_void};
use std::fmt;
use std::ops::Deref;
use std::ptr;

use {cvt, cvt_p};
use bn::BigNumRef;
use bio::{MemBio, MemBioSlice};
use bn::BigNum;
use {cvt, cvt_p};
use types::Ref;
use util::{CallbackState, invoke_passwd_cb};
use opaque::Opaque;

pub struct DsaRef(Opaque);

impl DsaRef {
    pub unsafe fn from_ptr<'a>(ptr: *mut ffi::DSA) -> &'a DsaRef {
        &*(ptr as *mut _)
    }

    pub fn as_ptr(&self) -> *mut ffi::DSA {
        self as *const _ as *mut _
    }
type_!(Dsa, ffi::DSA, ffi::DSA_free);

impl Ref<Dsa> {
    /// Writes an DSA private key as unencrypted PEM formatted data
    pub fn private_key_to_pem(&self) -> Result<Vec<u8>, ErrorStack> {
        assert!(self.has_private_key());
@@ -53,35 +44,35 @@ impl DsaRef {
        }
    }

    pub fn p(&self) -> Option<&BigNumRef> {
    pub fn p(&self) -> Option<&Ref<BigNum>> {
        unsafe {
            let p = compat::pqg(self.as_ptr())[0];
            if p.is_null() {
                None
            } else {
                Some(BigNumRef::from_ptr(p as *mut _))
                Some(Ref::<BigNum>::from_ptr(p as *mut _))
            }
        }
    }

    pub fn q(&self) -> Option<&BigNumRef> {
    pub fn q(&self) -> Option<&Ref<BigNum>> {
        unsafe {
            let q = compat::pqg(self.as_ptr())[1];
            if q.is_null() {
                None
            } else {
                Some(BigNumRef::from_ptr(q as *mut _))
                Some(Ref::<BigNum>::from_ptr(q as *mut _))
            }
        }
    }

    pub fn g(&self) -> Option<&BigNumRef> {
    pub fn g(&self) -> Option<&Ref<BigNum>> {
        unsafe {
            let g = compat::pqg(self.as_ptr())[2];
            if g.is_null() {
                None
            } else {
                Some(BigNumRef::from_ptr(g as *mut _))
                Some(Ref::<BigNum>::from_ptr(g as *mut _))
            }
        }
    }
@@ -95,21 +86,7 @@ impl DsaRef {
    }
}

pub struct Dsa(*mut ffi::DSA);

impl Drop for Dsa {
    fn drop(&mut self) {
        unsafe {
            ffi::DSA_free(self.0);
        }
    }
}

impl Dsa {
    pub unsafe fn from_ptr(dsa: *mut ffi::DSA) -> Dsa {
        Dsa(dsa)
    }

    /// Generate a DSA key pair.
    pub fn generate(bits: u32) -> Result<Dsa, ErrorStack> {
        unsafe {
@@ -177,11 +154,9 @@ impl Dsa {
    }
}

impl Deref for Dsa {
    type Target = DsaRef;

    fn deref(&self) -> &DsaRef {
        unsafe { DsaRef::from_ptr(self.0) }
impl fmt::Debug for Dsa {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "DSA")
    }
}

@@ -216,12 +191,6 @@ mod compat {
    }
}

impl fmt::Debug for Dsa {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "DSA")
    }
}

#[cfg(test)]
mod test {
    use libc::c_char;
Loading