Skip to content

Commit 4ba994d

Browse files
committed
Add X509::get_ext_by_obj
This function allows retrieving extensions from X509 certificates using ASN1 Object IDs.
1 parent 372f856 commit 4ba994d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

openssl/src/x509/mod.rs

+32
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,38 @@ impl X509Ref {
449449
}
450450
}
451451

452+
/// Returns this certificate's extensions for the given [Asn1Object].
453+
///
454+
/// # Examples
455+
///
456+
/// ```
457+
/// use openssl::asn1::Asn1Object;
458+
/// use openssl::x509::X509;
459+
///
460+
/// let cert = X509::from_pem(include_bytes!("../../test/extensions.pem")).unwrap();
461+
/// let obj = Asn1Object::from_str(&"1.3.6.1.4.1.41482.5.3").unwrap();
462+
/// let extension = cert.get_ext_by_obj(&obj).unwrap().unwrap();
463+
/// let value = extension.data().as_slice();
464+
/// assert_eq!(value, [4, 3, 5, 2, 7]);
465+
/// ```
466+
#[corresponds(X509_get_ext_by_OBJ)]
467+
pub fn get_ext_by_obj(
468+
&self,
469+
obj: &Asn1ObjectRef,
470+
) -> Result<Option<&X509ExtensionRef>, ErrorStack> {
471+
unsafe {
472+
let loc = ffi::X509_get_ext_by_OBJ(self.as_ptr(), obj.as_ptr(), -1);
473+
Ok(if loc >= 0 {
474+
Some(X509ExtensionRef::from_ptr(cvt_p(ffi::X509_get_ext(
475+
self.as_ptr(),
476+
loc,
477+
))?))
478+
} else {
479+
None
480+
})
481+
}
482+
}
483+
452484
/// Returns this certificate's subject alternative name entries, if they exist.
453485
#[corresponds(X509_get_ext_d2i)]
454486
pub fn subject_alt_names(&self) -> Option<Stack<GeneralName>> {

0 commit comments

Comments
 (0)