use crate::error_code::ErrorCode;
use anyhow::anyhow;
use eric_bindings::{EricCloseHandleToCertificate, EricGetHandleToCertificate};
use std::{ffi::CStr, ptr};
use tracing::debug;
pub struct Certificate {
pub handle: u32,
}
impl Certificate {
pub fn new(path: &CStr) -> Result<Self, anyhow::Error> {
debug!(path = %path.to_str()?, "Preparing certificate");
let mut handle = 0;
let pin_support = ptr::null_mut::<u32>();
let error_code =
unsafe { EricGetHandleToCertificate(&mut handle, pin_support, path.as_ptr()) };
match error_code {
x if x == ErrorCode::ERIC_OK as i32 => Ok(Certificate { handle }),
error_code => Err(anyhow!("Can't create certificate: {}", error_code)),
}
}
}
impl Drop for Certificate {
fn drop(&mut self) {
debug!("Cleaning up certificate");
let error_code = unsafe { EricCloseHandleToCertificate(self.handle) };
match error_code {
x if x == ErrorCode::ERIC_OK as i32 => (),
error_code => panic!("Can't drop certificate handle: {}", error_code),
}
}
}