use std::ffi::c_void;
use std::os::raw::c_char;
use std::sync::LazyLock;
const LIBPACT_PATH: &str = "libpact.so";
const LOAD_LIBRARY_ERROR: &str = "Failed to load libpact.so";
pub static LIBPACT: LazyLock<Libpact> = LazyLock::new(Libpact::load);
pub struct Libpact {
shared_library: Result<libloading::Library, libloading::Error>,
}
impl Libpact {
pub fn load() -> Self {
let shared_library = unsafe { libloading::Library::new(LIBPACT_PATH) };
Self { shared_library }
}
pub unsafe fn attestation_init(&self) -> i32 {
let attestation_init: libloading::Symbol<unsafe extern "C" fn() -> i32> = self
.shared_library
.as_ref()
.expect(LOAD_LIBRARY_ERROR)
.get(b"attestation_init")
.unwrap();
attestation_init()
}
pub unsafe fn attestation_get_session_parameters(&self, output: *mut *mut c_char) -> i32 {
let attestation_get_session_parameters: libloading::Symbol<
unsafe extern "C" fn(*mut *mut c_char) -> i32,
> = self
.shared_library
.as_ref()
.expect(LOAD_LIBRARY_ERROR)
.get(b"attestation_get_session_parameters")
.unwrap();
attestation_get_session_parameters(output)
}
pub unsafe fn attestation_decrypt_session_id(
&self,
cred_buffer: *const c_void,
cred_length: i32,
output: *mut *mut c_char,
) -> i32 {
let attestation_decrypt_session_id: libloading::Symbol<
unsafe extern "C" fn(*const c_void, i32, *mut *mut c_char) -> i32,
> = self
.shared_library
.as_ref()
.expect(LOAD_LIBRARY_ERROR)
.get(b"attestation_decrypt_session_id")
.unwrap();
attestation_decrypt_session_id(cred_buffer, cred_length, output)
}
pub unsafe fn attestation_get_quote(
&self,
nonce_buffer: *const c_void,
nonce_length: i32,
output: *mut *mut c_char,
) -> i32 {
let attestation_get_quote: libloading::Symbol<
unsafe extern "C" fn(*const c_void, i32, *mut *mut c_char) -> i32,
> = self
.shared_library
.as_ref()
.expect(LOAD_LIBRARY_ERROR)
.get(b"attestation_get_quote")
.unwrap();
attestation_get_quote(nonce_buffer, nonce_length, output)
}
pub unsafe fn buffer_free(&self, buffer: *mut c_void) {
let buffer_free: libloading::Symbol<unsafe extern "C" fn(*mut c_void)> = self
.shared_library
.as_ref()
.expect(LOAD_LIBRARY_ERROR)
.get(b"buffer_free")
.unwrap();
buffer_free(buffer)
}
}