Skip to content

Commit 29c0984

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 29c0984

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
@@ -454,6 +454,38 @@ 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(
473+
&self,
474+
obj: &Asn1ObjectRef,
475+
) -> Result<Option<&X509ExtensionRef>, ErrorStack> {
476+
unsafe {
477+
let loc = ffi::X509_get_ext_by_OBJ(self.as_ptr(), obj.as_ptr(), -1);
478+
Ok(if loc >= 0 {
479+
Some(X509ExtensionRef::from_ptr(cvt_p(ffi::X509_get_ext(
480+
self.as_ptr(),
481+
loc,
482+
))?))
483+
} else {
484+
None
485+
})
486+
}
487+
}
488+
457489
/// Returns this certificate's subject alternative name entries, if they exist.
458490
#[corresponds(X509_get_ext_d2i)]
459491
pub fn subject_alt_names(&self) -> Option<Stack<GeneralName>> {

0 commit comments

Comments
 (0)