Clean up asn1time

This commit is contained in:
Steven Fackler 2016-08-06 22:23:03 -07:00
parent bc97d088b0
commit 5af01a5dbd
2 changed files with 50 additions and 54 deletions

View File

@ -4,44 +4,36 @@ use std::ptr;
use ffi; use ffi;
use error::ErrorStack; use error::ErrorStack;
pub struct Asn1Time { pub struct Asn1Time(*mut ffi::ASN1_TIME);
handle: *mut ffi::ASN1_TIME,
owned: bool,
}
impl Asn1Time { impl Asn1Time {
/// Wraps existing ASN1_TIME and takes ownership /// Wraps existing ASN1_TIME and takes ownership
pub fn new(handle: *mut ffi::ASN1_TIME) -> Asn1Time { pub unsafe fn from_raw(handle: *mut ffi::ASN1_TIME) -> Asn1Time {
Asn1Time { Asn1Time(handle)
handle: handle,
owned: true,
}
} }
fn new_with_period(period: u64) -> Result<Asn1Time, ErrorStack> { fn from_period(period: u64) -> Result<Asn1Time, ErrorStack> {
ffi::init(); ffi::init();
let handle = unsafe { unsafe {
try_ssl_null!(ffi::X509_gmtime_adj(ptr::null_mut(), period as c_long)) let handle = try_ssl_null!(ffi::X509_gmtime_adj(ptr::null_mut(), period as c_long));
}; Ok(Asn1Time::from_raw(handle))
Ok(Asn1Time::new(handle)) }
} }
/// Creates a new time on specified interval in days from now /// Creates a new time on specified interval in days from now
pub fn days_from_now(days: u32) -> Result<Asn1Time, ErrorStack> { pub fn days_from_now(days: u32) -> Result<Asn1Time, ErrorStack> {
Asn1Time::new_with_period(days as u64 * 60 * 60 * 24) Asn1Time::from_period(days as u64 * 60 * 60 * 24)
} }
/// Returns raw handle /// Returns the raw handle
pub unsafe fn handle(&self) -> *mut ffi::ASN1_TIME { pub fn handle(&self) -> *mut ffi::ASN1_TIME {
return self.handle; self.0
} }
} }
impl Drop for Asn1Time { impl Drop for Asn1Time {
fn drop(&mut self) { fn drop(&mut self) {
if self.owned { unsafe { ffi::ASN1_TIME_free(self.0) };
unsafe { ffi::ASN1_TIME_free(self.handle) };
}
} }
} }

View File

@ -108,42 +108,46 @@ pub enum SslMethod {
} }
impl SslMethod { impl SslMethod {
unsafe fn to_raw(&self) -> *const ffi::SSL_METHOD { fn to_raw(&self) -> *const ffi::SSL_METHOD {
match *self { unsafe {
#[cfg(feature = "sslv2")] match *self {
SslMethod::Sslv2 => ffi::SSLv2_method(), #[cfg(feature = "sslv2")]
#[cfg(feature = "sslv3")] SslMethod::Sslv2 => ffi::SSLv2_method(),
SslMethod::Sslv3 => ffi::SSLv3_method(), #[cfg(feature = "sslv3")]
SslMethod::Tlsv1 => ffi::TLSv1_method(), SslMethod::Sslv3 => ffi::SSLv3_method(),
SslMethod::Sslv23 => ffi::SSLv23_method(), SslMethod::Tlsv1 => ffi::TLSv1_method(),
#[cfg(feature = "tlsv1_1")] SslMethod::Sslv23 => ffi::SSLv23_method(),
SslMethod::Tlsv1_1 => ffi::TLSv1_1_method(), #[cfg(feature = "tlsv1_1")]
#[cfg(feature = "tlsv1_2")] SslMethod::Tlsv1_1 => ffi::TLSv1_1_method(),
SslMethod::Tlsv1_2 => ffi::TLSv1_2_method(), #[cfg(feature = "tlsv1_2")]
#[cfg(feature = "dtlsv1")] SslMethod::Tlsv1_2 => ffi::TLSv1_2_method(),
SslMethod::Dtlsv1 => ffi::DTLSv1_method(), #[cfg(feature = "dtlsv1")]
#[cfg(feature = "dtlsv1_2")] SslMethod::Dtlsv1 => ffi::DTLSv1_method(),
SslMethod::Dtlsv1_2 => ffi::DTLSv1_2_method(), #[cfg(feature = "dtlsv1_2")]
SslMethod::Dtlsv1_2 => ffi::DTLSv1_2_method(),
}
} }
} }
unsafe fn from_raw(method: *const ffi::SSL_METHOD) -> Option<SslMethod> { fn from_raw(method: *const ffi::SSL_METHOD) -> Option<SslMethod> {
match method { unsafe {
#[cfg(feature = "sslv2")] match method {
x if x == ffi::SSLv2_method() => Some(SslMethod::Sslv2), #[cfg(feature = "sslv2")]
#[cfg(feature = "sslv3")] x if x == ffi::SSLv2_method() => Some(SslMethod::Sslv2),
x if x == ffi::SSLv3_method() => Some(SslMethod::Sslv3), #[cfg(feature = "sslv3")]
x if x == ffi::TLSv1_method() => Some(SslMethod::Tlsv1), x if x == ffi::SSLv3_method() => Some(SslMethod::Sslv3),
x if x == ffi::SSLv23_method() => Some(SslMethod::Sslv23), x if x == ffi::TLSv1_method() => Some(SslMethod::Tlsv1),
#[cfg(feature = "tlsv1_1")] x if x == ffi::SSLv23_method() => Some(SslMethod::Sslv23),
x if x == ffi::TLSv1_1_method() => Some(SslMethod::Tlsv1_1), #[cfg(feature = "tlsv1_1")]
#[cfg(feature = "tlsv1_2")] x if x == ffi::TLSv1_1_method() => Some(SslMethod::Tlsv1_1),
x if x == ffi::TLSv1_2_method() => Some(SslMethod::Tlsv1_2), #[cfg(feature = "tlsv1_2")]
#[cfg(feature = "dtlsv1")] x if x == ffi::TLSv1_2_method() => Some(SslMethod::Tlsv1_2),
x if x == ffi::DTLSv1_method() => Some(SslMethod::Dtlsv1), #[cfg(feature = "dtlsv1")]
#[cfg(feature = "dtlsv1_2")] x if x == ffi::DTLSv1_method() => Some(SslMethod::Dtlsv1),
x if x == ffi::DTLSv1_2_method() => Some(SslMethod::Dtlsv1_2), #[cfg(feature = "dtlsv1_2")]
_ => None, x if x == ffi::DTLSv1_2_method() => Some(SslMethod::Dtlsv1_2),
_ => None,
}
} }
} }
} }