diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs index be3ae53e1e7558dcca9b46d79f96532306348712..ab29f55c76378eb5c97d1ac0d405e40a4c3f9e12 100644 --- a/openssl-sys/src/lib.rs +++ b/openssl-sys/src/lib.rs @@ -7,7 +7,7 @@ extern crate libc; #[cfg(target_os = "nacl")] extern crate libressl_pnacl_sys; -use libc::{c_void, c_int, c_char, c_ulong, c_long, c_uint, c_uchar, size_t}; +use libc::{c_void, c_int, c_char, c_ulong, c_long, c_uint, c_uchar, size_t, FILE}; use std::mem; use std::ptr; use std::sync::{Mutex, MutexGuard}; @@ -625,13 +625,16 @@ extern "C" { pub fn ASN1_INTEGER_set(dest: *mut ASN1_INTEGER, value: c_long) -> c_int; pub fn ASN1_STRING_type_new(ty: c_int) -> *mut ASN1_STRING; pub fn ASN1_TIME_free(tm: *mut ASN1_TIME); + pub fn ASN1_TIME_print(b: *mut BIO, tm: *const ASN1_TIME) -> c_int; pub fn BIO_ctrl(b: *mut BIO, cmd: c_int, larg: c_long, parg: *mut c_void) -> c_long; pub fn BIO_free_all(b: *mut BIO); pub fn BIO_new(type_: *const BIO_METHOD) -> *mut BIO; + pub fn BIO_new_fp(stream: *mut FILE, close_flag: c_int) -> *mut BIO; pub fn BIO_new_socket(sock: c_int, close_flag: c_int) -> *mut BIO; pub fn BIO_read(b: *mut BIO, buf: *mut c_void, len: c_int) -> c_int; pub fn BIO_write(b: *mut BIO, buf: *const c_void, len: c_int) -> c_int; + pub fn BIO_s_file() -> *const BIO_METHOD; pub fn BIO_s_mem() -> *const BIO_METHOD; pub fn BIO_new_mem_buf(buf: *const c_void, len: c_int) -> *mut BIO; pub fn BIO_set_flags(b: *mut BIO, flags: c_int); diff --git a/openssl/Cargo.toml b/openssl/Cargo.toml index d1d709a388c33b0a86e5728c9163930ba670e6ea..9c09aed62f844f7645c20c54f79d125998e11ec5 100644 --- a/openssl/Cargo.toml +++ b/openssl/Cargo.toml @@ -30,6 +30,7 @@ hmac_clone = ["openssl-sys/hmac_clone"] c_helpers = ["gcc"] x509_clone = ["c_helpers"] x509_generator_request = ["c_helpers"] +x509_expiry = ["c_helpers"] ssl_context_clone = ["c_helpers"] hmac = ["c_helpers"] dh_from_params = ["c_helpers"] diff --git a/openssl/src/asn1/mod.rs b/openssl/src/asn1/mod.rs index 7d2097757020d5f33c16813d8b25abd203d25650..1eab9f041d99f96e9d6332a316d4cc382efd9e1a 100644 --- a/openssl/src/asn1/mod.rs +++ b/openssl/src/asn1/mod.rs @@ -1,15 +1,19 @@ use libc::c_long; -use std::ptr; +use std::{ptr, fmt}; +use std::marker::PhantomData; +use std::ops::Deref; +use bio::MemBio; use ffi; use error::ErrorStack; -pub struct Asn1Time(*mut ffi::ASN1_TIME); +/// Corresponds to the ASN.1 structure Time defined in RFC5280 +pub struct Asn1Time(Asn1TimeRef<'static>); impl Asn1Time { /// Wraps existing ASN1_TIME and takes ownership 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 { @@ -25,6 +29,24 @@ impl Asn1Time { pub fn days_from_now(days: u32) -> Result { 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 pub fn as_ptr(&self) -> *mut ffi::ASN1_TIME { @@ -32,8 +54,19 @@ impl Asn1Time { } } +impl<'a> fmt::Display for Asn1TimeRef<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mem_bio = try!(MemBio::new()); + let as_str = unsafe { + try_ssl!(ffi::ASN1_TIME_print(mem_bio.as_ptr(), self.0)); + String::from_utf8_unchecked(mem_bio.get_buf().to_owned()) + }; + write!(f, "{}", as_str) + } +} + impl Drop for Asn1Time { fn drop(&mut self) { - unsafe { ffi::ASN1_TIME_free(self.0) }; + unsafe { ffi::ASN1_TIME_free(self.as_ptr()) }; } } diff --git a/openssl/src/c_helpers.c b/openssl/src/c_helpers.c index 5d149553333fc2b650667865bd9c373ab906db84..6e6a5021f29824fa1821b2c05bff3987db8759da 100644 --- a/openssl/src/c_helpers.c +++ b/openssl/src/c_helpers.c @@ -15,6 +15,14 @@ STACK_OF(X509_EXTENSION) *rust_0_8_X509_get_extensions(X509 *x) { return x->cert_info ? x->cert_info->extensions : NULL; } +ASN1_TIME* rust_0_8_X509_get_notAfter(X509 *x) { + return X509_get_notAfter(x); +} + +ASN1_TIME* rust_0_8_X509_get_notBefore(X509 *x) { + return X509_get_notBefore(x); +} + DH *rust_0_8_DH_new_from_params(BIGNUM *p, BIGNUM *g, BIGNUM *q) { DH *dh; diff --git a/openssl/src/c_helpers.rs b/openssl/src/c_helpers.rs index 74ddb9ac799be2f22b3281e54e7539d776707878..d16c3125fbf4bd9ee554d330a46c4c55c04d58c9 100644 --- a/openssl/src/c_helpers.rs +++ b/openssl/src/c_helpers.rs @@ -6,7 +6,8 @@ extern "C" { pub fn rust_0_8_SSL_CTX_clone(cxt: *mut ffi::SSL_CTX); pub fn rust_0_8_X509_clone(x509: *mut ffi::X509); pub fn rust_0_8_X509_get_extensions(x: *mut ffi::X509) -> *mut ffi::stack_st_X509_EXTENSION; - + pub fn rust_0_8_X509_get_notAfter(x: *mut ffi::X509) -> *mut ffi::ASN1_TIME; + pub fn rust_0_8_X509_get_notBefore(x: *mut ffi::X509) -> *mut ffi::ASN1_TIME; pub fn rust_0_8_HMAC_Init_ex(ctx: *mut ffi::HMAC_CTX, key: *const c_void, keylen: c_int, md: *const ffi::EVP_MD, impl_: *mut ffi::ENGINE) -> c_int; pub fn rust_0_8_HMAC_Final(ctx: *mut ffi::HMAC_CTX, output: *mut c_uchar, len: *mut c_uint) -> c_int; pub fn rust_0_8_HMAC_Update(ctx: *mut ffi::HMAC_CTX, input: *const c_uchar, len: c_uint) -> c_int; diff --git a/openssl/src/error.rs b/openssl/src/error.rs index 5fa542c2cf53ce2baeedbee77e6bc34931a810fb..cc89b5db708eba426126f21a71bb1985f770a20e 100644 --- a/openssl/src/error.rs +++ b/openssl/src/error.rs @@ -54,6 +54,12 @@ impl From for io::Error { } } +impl From for fmt::Error { + fn from(_: ErrorStack) -> fmt::Error { + fmt::Error + } +} + /// An error reported from OpenSSL. pub struct Error(c_ulong); diff --git a/openssl/src/x509/mod.rs b/openssl/src/x509/mod.rs index 0cc0eca7c39372e7b4338fa219f9f3b7737eef3f..1319b75c7c567b6e05d4b086b5612c7d60119fe7 100644 --- a/openssl/src/x509/mod.rs +++ b/openssl/src/x509/mod.rs @@ -11,6 +11,9 @@ use std::marker::PhantomData; use HashTypeInternals; use asn1::Asn1Time; +#[cfg(feature = "x509_expiry")] +use asn1::Asn1TimeRef; + use bio::{MemBio, MemBioSlice}; use crypto::hash; use crypto::hash::Type as HashType; @@ -433,6 +436,28 @@ impl<'a> X509Ref<'a> { } } + /// Returns certificate Not After validity period. + /// Requires the `x509_expiry` feature. + #[cfg(feature = "x509_expiry")] + pub fn not_after<'b>(&'b self) -> Asn1TimeRef<'b> { + unsafe { + let date = ::c_helpers::rust_0_8_X509_get_notAfter(self.0); + assert!(!date.is_null()); + Asn1TimeRef::from_ptr(date) + } + } + + /// Returns certificate Not Before validity period. + /// Requires the `x509_expiry` feature. + #[cfg(feature = "x509_expiry")] + pub fn not_before<'b>(&'b self) -> Asn1TimeRef<'b> { + unsafe { + let date = ::c_helpers::rust_0_8_X509_get_notBefore(self.0); + assert!(!date.is_null()); + Asn1TimeRef::from_ptr(date) + } + } + /// Writes certificate as PEM pub fn to_pem(&self) -> Result, ErrorStack> { let mem_bio = try!(MemBio::new()); diff --git a/openssl/src/x509/tests.rs b/openssl/src/x509/tests.rs index 43add8968fc15612beec59f19e4eca259bca21f0..eac08941f6c909a478730ab26b5cb438c72001a5 100644 --- a/openssl/src/x509/tests.rs +++ b/openssl/src/x509/tests.rs @@ -92,6 +92,18 @@ fn test_cert_loading() { assert_eq!(fingerprint, hash_vec); } +#[test] +#[cfg(feature = "x509_expiry")] +fn test_cert_issue_validity() { + let cert = include_bytes!("../../test/cert.pem"); + let cert = X509::from_pem(cert).ok().expect("Failed to load PEM"); + let not_before = cert.not_before().to_string(); + let not_after = cert.not_after().to_string(); + + assert_eq!(not_before, "Aug 14 17:00:03 2016 GMT"); + assert_eq!(not_after, "Aug 12 17:00:03 2026 GMT"); +} + #[test] fn test_save_der() { let cert = include_bytes!("../../test/cert.pem"); diff --git a/openssl/test/run.sh b/openssl/test/run.sh index b94818378365b35e7638f7eb0f5574637463f08d..2c2473b1aaeab885442a531eaa80f5c8daf5a2d7 100755 --- a/openssl/test/run.sh +++ b/openssl/test/run.sh @@ -4,7 +4,7 @@ set -e MAIN_TARGETS=https://static.rust-lang.org/dist if [ "$TEST_FEATURES" == "true" ]; then - FEATURES="tlsv1_2 tlsv1_1 dtlsv1 dtlsv1_2 sslv3 aes_xts aes_ctr npn alpn rfc5114 ecdh_auto pkcs5_pbkdf2_hmac x509_clone ssl_context_clone x509_generator_request hmac hmac_clone dh_from_params" + FEATURES="tlsv1_2 tlsv1_1 dtlsv1 dtlsv1_2 sslv3 aes_xts aes_ctr npn alpn rfc5114 ecdh_auto pkcs5_pbkdf2_hmac x509_clone ssl_context_clone x509_generator_request hmac hmac_clone dh_from_params x509_expiry" fi if [ "$TRAVIS_OS_NAME" != "osx" ]; then