aboutsummaryrefslogtreecommitdiff
path: root/openssl/src/x509
diff options
context:
space:
mode:
Diffstat (limited to 'openssl/src/x509')
-rw-r--r--openssl/src/x509/mod.rs36
-rw-r--r--openssl/src/x509/tests.rs12
2 files changed, 48 insertions, 0 deletions
diff --git a/openssl/src/x509/mod.rs b/openssl/src/x509/mod.rs
index 0cc0eca7..f5369447 100644
--- a/openssl/src/x509/mod.rs
+++ b/openssl/src/x509/mod.rs
@@ -1,4 +1,5 @@
use libc::{c_char, c_int, c_long, c_ulong, c_void};
+use std::cmp;
use std::ffi::CString;
use std::mem;
use std::ptr;
@@ -11,6 +12,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 +437,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<Vec<u8>, ErrorStack> {
let mem_bio = try!(MemBio::new());
@@ -467,6 +493,16 @@ impl X509 {
X509::from_ptr(x509)
}
+ /// Reads a certificate from DER.
+ pub fn from_der(buf: &[u8]) -> Result<X509, ErrorStack> {
+ unsafe {
+ let mut ptr = buf.as_ptr() as *mut _;
+ let len = cmp::min(buf.len(), c_long::max_value() as usize) as c_long;
+ let x509 = try_ssl_null!(ffi::d2i_X509(ptr::null_mut(), &mut ptr, len));
+ Ok(X509::from_ptr(x509))
+ }
+ }
+
/// Reads a certificate from PEM.
pub fn from_pem(buf: &[u8]) -> Result<X509, ErrorStack> {
let mem_bio = try!(MemBioSlice::new(buf));
diff --git a/openssl/src/x509/tests.rs b/openssl/src/x509/tests.rs
index 43add896..eac08941 100644
--- a/openssl/src/x509/tests.rs
+++ b/openssl/src/x509/tests.rs
@@ -93,6 +93,18 @@ fn test_cert_loading() {
}
#[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");
let cert = X509::from_pem(cert).ok().expect("Failed to load PEM");