From 6482f419b859c146467f06b63fb937dfab5f5caa Mon Sep 17 00:00:00 2001 From: Jacob Hoffman-Andrews Date: Fri, 29 May 2020 21:34:20 -0700 Subject: Add Debug trait for X509 and other types. This currently leaves out at least two useful things: - The detailed SubjectPublicKeyInfo, e.g. the modulus of RSA keys. - Extensions. --- openssl/src/asn1.rs | 53 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 11 deletions(-) (limited to 'openssl/src/asn1.rs') diff --git a/openssl/src/asn1.rs b/openssl/src/asn1.rs index 8c7f7b1a..43c92f1f 100644 --- a/openssl/src/asn1.rs +++ b/openssl/src/asn1.rs @@ -67,12 +67,16 @@ foreign_type_and_impl_send_sync! { impl fmt::Display for Asn1GeneralizedTimeRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { unsafe { - let mem_bio = MemBio::new()?; - cvt(ffi::ASN1_GENERALIZEDTIME_print( - mem_bio.as_ptr(), - self.as_ptr(), - ))?; - write!(f, "{}", str::from_utf8_unchecked(mem_bio.get_buf())) + match MemBio::new() { + Err(_) => f.write_str(""), + Ok(mem_bio) => match cvt(ffi::ASN1_GENERALIZEDTIME_print( + mem_bio.as_ptr(), + self.as_ptr(), + )) { + Err(_) => f.write_str(""), + Ok(_) => f.write_str(str::from_utf8_unchecked(mem_bio.get_buf())), + }, + } } } } @@ -207,13 +211,23 @@ impl<'a> PartialOrd for &'a Asn1TimeRef { impl fmt::Display for Asn1TimeRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { unsafe { - let mem_bio = MemBio::new()?; - cvt(ffi::ASN1_TIME_print(mem_bio.as_ptr(), self.as_ptr()))?; - write!(f, "{}", str::from_utf8_unchecked(mem_bio.get_buf())) + match MemBio::new() { + Err(_) => f.write_str("error"), + Ok(mem_bio) => match cvt(ffi::ASN1_TIME_print(mem_bio.as_ptr(), self.as_ptr())) { + Err(_) => f.write_str("error"), + Ok(_) => f.write_str(str::from_utf8_unchecked(mem_bio.get_buf())), + }, + } } } } +impl fmt::Debug for Asn1TimeRef { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str(&self.to_string()) + } +} + impl Asn1Time { fn new() -> Result { ffi::init(); @@ -389,6 +403,15 @@ impl Asn1StringRef { } } +impl fmt::Debug for Asn1StringRef { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match self.as_utf8() { + Ok(openssl_string) => openssl_string.fmt(fmt), + Err(_) => fmt.write_str("error"), + } + } +} + foreign_type_and_impl_send_sync! { type CType = ffi::ASN1_INTEGER; fn drop = ffi::ASN1_INTEGER_free; @@ -527,12 +550,20 @@ impl fmt::Display for Asn1ObjectRef { self.as_ptr(), 0, ); - let s = str::from_utf8(&buf[..len as usize]).map_err(|_| fmt::Error)?; - fmt.write_str(s) + match str::from_utf8(&buf[..len as usize]) { + Err(_) => fmt.write_str("error"), + Ok(s) => fmt.write_str(s), + } } } } +impl fmt::Debug for Asn1ObjectRef { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.write_str(self.to_string().as_str()) + } +} + cfg_if! { if #[cfg(any(ossl110, libressl273))] { use ffi::ASN1_STRING_get0_data; -- cgit v1.2.3