Introduce `Asn1TimeRef`

This commit is contained in:
David Weinstein 2016-08-08 16:42:02 -04:00
parent f9cd4bff1f
commit 32a4e2ba50
2 changed files with 35 additions and 15 deletions

View File

@ -1,17 +1,19 @@
use libc::c_long; use libc::c_long;
use std::ptr; use std::{ptr, fmt};
use std::fmt; use std::marker::PhantomData;
use std::ops::Deref;
use bio::MemBio;
use ffi; use ffi;
use error::ErrorStack; use error::ErrorStack;
pub struct Asn1Time(*mut ffi::ASN1_TIME); /// Corresponds to the ASN.1 structure Time defined in RFC5280
use bio::MemBio; pub struct Asn1Time(Asn1TimeRef<'static>);
impl Asn1Time { impl Asn1Time {
/// Wraps existing ASN1_TIME and takes ownership /// Wraps existing ASN1_TIME and takes ownership
pub unsafe fn from_ptr(handle: *mut ffi::ASN1_TIME) -> Asn1Time { pub unsafe fn from_ptr(handle: *mut ffi::ASN1_TIME) -> Asn1Time {
Asn1Time(handle) Asn1Time(Asn1TimeRef::from_ptr(handle))
} }
fn from_period(period: c_long) -> Result<Asn1Time, ErrorStack> { fn from_period(period: c_long) -> Result<Asn1Time, ErrorStack> {
@ -27,6 +29,24 @@ impl Asn1Time {
pub fn days_from_now(days: u32) -> Result<Asn1Time, ErrorStack> { pub fn days_from_now(days: u32) -> Result<Asn1Time, ErrorStack> {
Asn1Time::from_period(days as c_long * 60 * 60 * 24) Asn1Time::from_period(days as c_long * 60 * 60 * 24)
} }
}
impl Deref for Asn1Time {
type Target = Asn1TimeRef<'static>;
fn deref(&self) -> &Asn1TimeRef<'static> {
&self.0
}
}
/// A borrowed Asn1Time
pub struct Asn1TimeRef<'a>(*mut ffi::ASN1_TIME, PhantomData<&'a ()>);
impl<'a> Asn1TimeRef<'a> {
/// Creates a new `Asn1TimeRef` wrapping the provided handle.
pub unsafe fn from_ptr(handle: *mut ffi::ASN1_TIME) -> Asn1TimeRef<'a> {
Asn1TimeRef(handle, PhantomData)
}
/// Returns the raw handle /// Returns the raw handle
pub fn as_ptr(&self) -> *mut ffi::ASN1_TIME { pub fn as_ptr(&self) -> *mut ffi::ASN1_TIME {
@ -34,11 +54,11 @@ impl Asn1Time {
} }
} }
impl fmt::Display for Asn1Time { impl<'a> fmt::Display for Asn1TimeRef<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mem_bio = try!(MemBio::new()); let mem_bio = try!(MemBio::new());
let as_str = unsafe { let as_str = unsafe {
ffi::ASN1_TIME_print(mem_bio.handle(), self.handle); ffi::ASN1_TIME_print(mem_bio.as_ptr(), self.0);
String::from_utf8_unchecked(mem_bio.get_buf().to_owned()) String::from_utf8_unchecked(mem_bio.get_buf().to_owned())
}; };
write!(f, "{}", as_str) write!(f, "{}", as_str)
@ -47,6 +67,6 @@ impl fmt::Display for Asn1Time {
impl Drop for Asn1Time { impl Drop for Asn1Time {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { ffi::ASN1_TIME_free(self.0) }; unsafe { ffi::ASN1_TIME_free(self.as_ptr()) };
} }
} }

View File

@ -10,7 +10,7 @@ use std::collections::HashMap;
use std::marker::PhantomData; use std::marker::PhantomData;
use HashTypeInternals; use HashTypeInternals;
use asn1::Asn1Time; use asn1::{Asn1Time, Asn1TimeRef};
use bio::{MemBio, MemBioSlice}; use bio::{MemBio, MemBioSlice};
use crypto::hash; use crypto::hash;
use crypto::hash::Type as HashType; use crypto::hash::Type as HashType;
@ -434,18 +434,18 @@ impl<'a> X509Ref<'a> {
} }
/// Returns Issuer validity notAfter /// Returns Issuer validity notAfter
pub fn not_after(&self) -> Asn1Time { pub fn not_after(&self) -> Asn1TimeRef {
unsafe { unsafe {
let date = ffi_extras::X509_get_notAfter(self.handle()); let date = ::c_helpers::rust_0_8_X509_get_notAfter(self.0);
Asn1Time::from_raw(date) Asn1TimeRef::from_ptr(date)
} }
} }
/// Returns Issuer validity notBefore /// Returns Issuer validity notBefore
pub fn not_before(&self) -> Asn1Time { pub fn not_before(&self) -> Asn1TimeRef {
unsafe { unsafe {
let date = ffi_extras::X509_get_notBefore(self.handle()); let date = ::c_helpers::rust_0_8_X509_get_notBefore(self.0);
Asn1Time::from_raw(date) Asn1TimeRef::from_ptr(date)
} }
} }