Skip to main content

mls_rs_core/
psk.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// Copyright by contributors to this project.
3// SPDX-License-Identifier: (Apache-2.0 OR MIT)
4
5use crate::error::IntoAnyError;
6#[cfg(mls_build_async)]
7use alloc::boxed::Box;
8use alloc::vec::Vec;
9use core::{
10    fmt::{self, Debug},
11    ops::Deref,
12};
13use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize};
14use zeroize::Zeroizing;
15
16#[derive(Clone, PartialEq, Eq, MlsSize, MlsEncode, MlsDecode)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18/// Wrapper type that holds a pre-shared key value and zeroizes on drop.
19pub struct PreSharedKey(
20    #[mls_codec(with = "mls_rs_codec::byte_vec")]
21    #[cfg_attr(feature = "serde", serde(with = "crate::zeroizing_serde"))]
22    Zeroizing<Vec<u8>>,
23);
24
25impl Debug for PreSharedKey {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        f.debug_struct("PreSharedKey").finish()
28    }
29}
30
31impl PreSharedKey {
32    /// Create a new PreSharedKey.
33    pub fn new(data: Vec<u8>) -> Self {
34        PreSharedKey(Zeroizing::new(data))
35    }
36
37    /// Raw byte value.
38    pub fn raw_value(&self) -> &[u8] {
39        &self.0
40    }
41}
42
43impl From<Vec<u8>> for PreSharedKey {
44    fn from(bytes: Vec<u8>) -> Self {
45        Self::new(bytes)
46    }
47}
48
49impl From<Zeroizing<Vec<u8>>> for PreSharedKey {
50    fn from(bytes: Zeroizing<Vec<u8>>) -> Self {
51        Self(bytes)
52    }
53}
54
55impl AsRef<[u8]> for PreSharedKey {
56    fn as_ref(&self) -> &[u8] {
57        self.raw_value()
58    }
59}
60
61impl Deref for PreSharedKey {
62    type Target = [u8];
63
64    fn deref(&self) -> &Self::Target {
65        self.raw_value()
66    }
67}
68
69#[derive(Clone, Eq, Hash, Ord, PartialOrd, PartialEq, MlsSize, MlsEncode, MlsDecode)]
70#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
71#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
72/// An external pre-shared key identifier.
73pub struct ExternalPskId(
74    #[mls_codec(with = "mls_rs_codec::byte_vec")]
75    #[cfg_attr(feature = "serde", serde(with = "crate::vec_serde"))]
76    Vec<u8>,
77);
78
79impl Debug for ExternalPskId {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        crate::debug::pretty_bytes(&self.0)
82            .named("ExternalPskId")
83            .fmt(f)
84    }
85}
86
87impl ExternalPskId {
88    pub fn new(id_data: Vec<u8>) -> Self {
89        Self(id_data)
90    }
91}
92
93impl AsRef<[u8]> for ExternalPskId {
94    fn as_ref(&self) -> &[u8] {
95        &self.0
96    }
97}
98
99impl Deref for ExternalPskId {
100    type Target = [u8];
101
102    fn deref(&self) -> &Self::Target {
103        &self.0
104    }
105}
106
107impl From<Vec<u8>> for ExternalPskId {
108    fn from(value: Vec<u8>) -> Self {
109        ExternalPskId(value)
110    }
111}
112
113/// Storage trait to maintain a set of pre-shared key values.
114#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)]
115#[cfg_attr(mls_build_async, maybe_async::must_be_async)]
116pub trait PreSharedKeyStorage: Send + Sync {
117    /// Error type that the underlying storage mechanism returns on internal
118    /// failure.
119    type Error: IntoAnyError;
120
121    /// Get a pre-shared key by [`ExternalPskId`](ExternalPskId).
122    ///
123    /// `None` should be returned if a pre-shared key can not be found for `id`.
124    async fn get(&self, id: &ExternalPskId) -> Result<Option<PreSharedKey>, Self::Error>;
125
126    /// Determines if a PSK is located within the store
127    async fn contains(&self, id: &ExternalPskId) -> Result<bool, Self::Error> {
128        self.get(id).await.map(|key| key.is_some())
129    }
130}