File
Blob: firmware/crates/esp32-radio/src/platform/certificate.rs
| 1 | use super::ffi; |
| 2 | use crate::error::{Error, Result, check}; |
| 3 | use radio_webrtc::Certificate; |
| 4 | use std::net::Ipv4Addr; |
| 5 | |
| 6 | pub(crate) fn certificate() -> Result<Certificate> { |
| 7 | let mut cert = vec![0; 2048]; |
| 8 | let mut key = vec![0; 512]; |
| 9 | let mut cert_len = 0; |
| 10 | let mut key_len = 0; |
| 11 | // SAFETY: separate initialized writable buffers and lengths live for this |
| 12 | // synchronous call. C bounds each output and retains no Rust pointers. |
| 13 | let result = unsafe { |
| 14 | ffi::radio_certificate_generate( |
| 15 | cert.as_mut_ptr(), |
| 16 | cert.len(), |
| 17 | &mut cert_len, |
| 18 | key.as_mut_ptr(), |
| 19 | key.len(), |
| 20 | &mut key_len, |
| 21 | ) |
| 22 | }; |
| 23 | if result != 0 { |
| 24 | key.fill(0); |
| 25 | } |
| 26 | check(result, "certificate generation failed")?; |
| 27 | if cert_len > cert.len() || key_len > key.len() { |
| 28 | key.fill(0); |
| 29 | return Err(Error::new("invalid certificate output length")); |
| 30 | } |
| 31 | cert.truncate(cert_len); |
| 32 | key.truncate(key_len); |
| 33 | Ok(Certificate::from_der(cert, key)?) |
| 34 | } |
| 35 | |
| 36 | pub(crate) fn ipv4() -> Result<Ipv4Addr> { |
| 37 | let mut octets = [0; 4]; |
| 38 | check( |
| 39 | // SAFETY: C writes exactly four octets and retains no pointer. |
| 40 | unsafe { ffi::radio_ipv4(octets.as_mut_ptr()) }, |
| 41 | "Wi-Fi address unavailable", |
| 42 | )?; |
| 43 | Ok(Ipv4Addr::from(octets)) |
| 44 | } |