Skip to content

Commit 11a02ed

Browse files
feat: Expose parsed X509 certificates and add extra impls to generated bindings
1 parent 87e7ca8 commit 11a02ed

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

esp-mbedtls-sys/src/extra_impls.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//! Provide extra implementations to the generated bindings
2+
3+
4+
impl core::fmt::Debug for crate::bindings::mbedtls_x509_time {
5+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
6+
write!(
7+
f,
8+
// ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ
9+
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
10+
self.year, self.mon, self.day, self.hour, self.min, self.sec
11+
)
12+
}
13+
}

esp-mbedtls-sys/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use esp_wifi as _;
1111

1212
#[cfg(not(target_os = "espidf"))]
1313
mod c_types;
14+
mod extra_impls;
1415

1516
#[allow(
1617
non_camel_case_types,

esp-mbedtls/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,30 @@ impl Certificates<'_> {
618618
}
619619
}
620620

621+
/// Get a reference to the underlying parsed X509 peer certificate, if configured
622+
pub fn certificate(&self) -> Option<&mbedtls_x509_crt> {
623+
self.certificate.as_ref().and_then(|certificate| {
624+
let ptr = certificate.crt;
625+
if ptr.is_null() {
626+
None
627+
} else {
628+
Some(unsafe { &*ptr })
629+
}
630+
})
631+
}
632+
633+
/// Get a reference to the underlying parsed CA Chain X509 certificate, if configured
634+
pub fn ca_chain(&self) -> Option<&mbedtls_x509_crt> {
635+
self.ca_chain.as_ref().and_then(|ca_chain| {
636+
let ptr = ca_chain.crt;
637+
if ptr.is_null() {
638+
None
639+
} else {
640+
Some(unsafe { &*ptr })
641+
}
642+
})
643+
}
644+
621645
/// Initialize the own certificate chain and private key used for requests
622646
/// It should contain in order from the bottom up your certificate chain.
623647
/// The top certificate (self-signed) can be omitted.

0 commit comments

Comments
 (0)