Skip to content

Commit

Permalink
Add X509::get_ext
Browse files Browse the repository at this point in the history
This function allows retrieving extensions from X509 certificates.
  • Loading branch information
wiktor-k committed Aug 30, 2023
1 parent 2e6bc97 commit 67bd97f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
29 changes: 29 additions & 0 deletions openssl/src/x509/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,35 @@ impl X509Ref {
}
}

/// Returns this certificate's extensions for the given [Nid].
///
/// # Examples
///
/// ```
/// use openssl::nid::Nid;
/// use openssl::x509::X509;
///
/// let cert = X509::from_pem(include_bytes!("../../test/extensions.pem")).unwrap();
/// let nid = Nid::create(&"1.3.6.1.4.1.41482.5.3", &"fw", &"firmware").unwrap();
/// let extension = cert.get_ext(nid).unwrap().unwrap();
/// let value = extension.data().as_slice();
/// assert_eq!(value, [4, 3, 5, 2, 7]);
/// ```
#[corresponds(X509_get_ext_by_NID)]
pub fn get_ext(&self, nid: Nid) -> Result<Option<&X509ExtensionRef>, ErrorStack> {
unsafe {
let loc = ffi::X509_get_ext_by_NID(self.as_ptr(), nid.as_raw(), -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<Stack<GeneralName>> {
Expand Down
20 changes: 20 additions & 0 deletions openssl/test/extensions.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-----BEGIN CERTIFICATE-----
MIIDOTCCAiGgAwIBAgIJAN1RLZj8pwQyMA0GCSqGSIb3DQEBCwUAMCgxJjAkBgNV
BAMMHVl1YmljbyBPcGVuUEdQIEF0dGVzdGF0aW9uIENBMB4XDTE5MDgwMTAwMDAw
MFoXDTQ2MTIxNzAwMDAwMFowIzEhMB8GA1UEAwwYWXViaWtleSBPUEdQIEF0dGVz
dGF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