use crate::crypto::xmlsec::wrapper::bindings;
use super::backend;
use super::error::XmlSecError;
use super::error::XmlSecResult;
use super::xmlsec_internal;
use std::ptr::null;
use std::ptr::null_mut;
#[allow(dead_code)]
#[allow(missing_docs)]
#[repr(u32)]
pub enum XmlSecKeyFormat {
Unknown = bindings::xmlSecKeyDataFormat_xmlSecKeyDataFormatUnknown,
Binary = bindings::xmlSecKeyDataFormat_xmlSecKeyDataFormatBinary,
Pem = bindings::xmlSecKeyDataFormat_xmlSecKeyDataFormatPem,
Der = bindings::xmlSecKeyDataFormat_xmlSecKeyDataFormatDer,
Pkcs8Pem = bindings::xmlSecKeyDataFormat_xmlSecKeyDataFormatPkcs8Pem,
Pkcs8Der = bindings::xmlSecKeyDataFormat_xmlSecKeyDataFormatPkcs8Der,
Pkcs12 = bindings::xmlSecKeyDataFormat_xmlSecKeyDataFormatPkcs12,
CertPem = bindings::xmlSecKeyDataFormat_xmlSecKeyDataFormatCertPem,
CertDer = bindings::xmlSecKeyDataFormat_xmlSecKeyDataFormatCertDer,
}
#[derive(Debug)]
pub struct XmlSecKey(*mut bindings::xmlSecKey);
impl XmlSecKey {
pub fn from_memory(buffer: &[u8], format: XmlSecKeyFormat) -> XmlSecResult<Self> {
xmlsec_internal::guarantee_xmlsec_init()?;
let key = unsafe {
backend::xmlSecCryptoAppKeyLoadMemory(
buffer.as_ptr(),
buffer.len().try_into().expect("Key buffer length overflow"),
format as u32,
null(),
null_mut(),
null_mut(),
)
};
if key.is_null() {
return Err(XmlSecError::KeyLoadError);
}
Ok(Self(key))
}
pub unsafe fn from_ptr(ptr: *mut bindings::xmlSecKey) -> Self {
Self(ptr)
}
pub unsafe fn leak(key: Self) -> *mut bindings::xmlSecKey {
let ptr = key.0;
std::mem::forget(key);
ptr
}
}
impl PartialEq for XmlSecKey {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0 }
}
impl Eq for XmlSecKey {}
impl Clone for XmlSecKey {
fn clone(&self) -> Self {
let new = unsafe { bindings::xmlSecKeyDuplicate(self.0) };
Self(new)
}
}
impl Drop for XmlSecKey {
fn drop(&mut self) {
unsafe { bindings::xmlSecKeyDestroy(self.0) };
}
}