From bcbb32a23a205031c463216f4419c3b8a77e517c Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Wed, 30 Aug 2023 12:24:13 +0200 Subject: [PATCH] Add `X509::get_ext_by_obj` This function allows retrieving extensions from X509 certificates using ASN1 Object IDs. --- openssl/src/x509/mod.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/openssl/src/x509/mod.rs b/openssl/src/x509/mod.rs index 4d8780876..618a952c3 100644 --- a/openssl/src/x509/mod.rs +++ b/openssl/src/x509/mod.rs @@ -449,6 +449,38 @@ impl X509Ref { } } + /// Returns this certificate's extensions for the given [Asn1Object]. + /// + /// # Examples + /// + /// ``` + /// use openssl::asn1::Asn1Object; + /// use openssl::x509::X509; + /// + /// let cert = X509::from_pem(include_bytes!("../../test/extensions.pem")).unwrap(); + /// let obj = Asn1Object::from_str(&"1.3.6.1.4.1.41482.5.3").unwrap(); + /// let extension = cert.get_ext_by_obj(&obj).unwrap().unwrap(); + /// let value = extension.data().as_slice(); + /// assert_eq!(value, [4, 3, 5, 2, 7]); + /// ``` + #[corresponds(X509_get_ext_by_OBJ)] + pub fn get_ext_by_obj( + &self, + obj: &Asn1ObjectRef, + ) -> Result, ErrorStack> { + unsafe { + let loc = ffi::X509_get_ext_by_OBJ(self.as_ptr(), obj.as_ptr(), -1); + Ok(if loc >= 0 { + Some(X509ExtensionRef::from_ptr(cvt_p(ffi::X509_get_ext( + self.as_ptr(), + loc, + ))?)) + } else { + None + }) + } + } + /// Returns this certificate's subject alternative name entries, if they exist. #[corresponds(X509_get_ext_d2i)] pub fn subject_alt_names(&self) -> Option> {