use crate::error::IntoAnyError;
#[cfg(mls_build_async)]
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::ops::Deref;
use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize};
use zeroize::Zeroizing;
#[derive(Clone, Debug, PartialEq, Eq, MlsSize, MlsEncode, MlsDecode)]
pub struct PreSharedKey(#[mls_codec(with = "mls_rs_codec::byte_vec")] Zeroizing<Vec<u8>>);
impl PreSharedKey {
pub fn new(data: Vec<u8>) -> Self {
PreSharedKey(Zeroizing::new(data))
}
pub fn raw_value(&self) -> &[u8] {
&self.0
}
}
impl From<Vec<u8>> for PreSharedKey {
fn from(bytes: Vec<u8>) -> Self {
Self::new(bytes)
}
}
impl From<Zeroizing<Vec<u8>>> for PreSharedKey {
fn from(bytes: Zeroizing<Vec<u8>>) -> Self {
Self(bytes)
}
}
impl AsRef<[u8]> for PreSharedKey {
fn as_ref(&self) -> &[u8] {
self.raw_value()
}
}
impl Deref for PreSharedKey {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.raw_value()
}
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq, MlsSize, MlsEncode, MlsDecode)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(
all(feature = "ffi", not(test)),
safer_ffi_gen::ffi_type(clone, opaque)
)]
pub struct ExternalPskId(#[mls_codec(with = "mls_rs_codec::byte_vec")] Vec<u8>);
#[cfg_attr(all(feature = "ffi", not(test)), safer_ffi_gen::safer_ffi_gen)]
impl ExternalPskId {
pub fn new(id_data: Vec<u8>) -> Self {
Self(id_data)
}
}
impl AsRef<[u8]> for ExternalPskId {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl Deref for ExternalPskId {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<Vec<u8>> for ExternalPskId {
fn from(value: Vec<u8>) -> Self {
ExternalPskId(value)
}
}
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
#[cfg_attr(mls_build_async, maybe_async::must_be_async)]
pub trait PreSharedKeyStorage: Send + Sync {
type Error: IntoAnyError;
async fn get(&self, id: &ExternalPskId) -> Result<Option<PreSharedKey>, Self::Error>;
async fn contains(&self, id: &ExternalPskId) -> Result<bool, Self::Error> {
self.get(id).await.map(|key| key.is_some())
}
}