Skip to content

Commit e8d7eca

Browse files
committed
Add X509::get_ext_by_obj
This function allows retrieving extensions from X509 certificates using ASN1 Object IDs.
1 parent a96cdf7 commit e8d7eca

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

openssl/src/x509/mod.rs

+29
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,35 @@ impl X509Ref {
454454
}
455455
}
456456

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

0 commit comments

Comments
 (0)