Skip to main content

core_crypto/mls/conversation/
welcome.rs

1use openmls::prelude::{MlsMessageIn, MlsMessageOut};
2use tls_codec::{Deserialize as _, Serialize as _};
3
4use super::{Error, Result};
5
6/// A Welcome Message as defined in RFC 9420.
7///
8/// This type is fallibly parseable from raw bytes.
9#[derive(Debug, Clone, derive_more::From, derive_more::Into)]
10pub struct WelcomeMessage(pub(crate) MlsMessageIn);
11
12impl TryFrom<&[u8]> for WelcomeMessage {
13    type Error = Error;
14
15    fn try_from(bytes: &[u8]) -> Result<Self> {
16        MlsMessageIn::tls_deserialize_exact(bytes)
17            .map(Self)
18            .map_err(Error::tls_deserialize("deserializing welcome message as MlsMessageIn"))
19    }
20}
21
22impl TryFrom<Vec<u8>> for WelcomeMessage {
23    type Error = Error;
24
25    fn try_from(value: Vec<u8>) -> Result<Self> {
26        value.as_slice().try_into()
27    }
28}
29
30impl From<MlsMessageOut> for WelcomeMessage {
31    fn from(value: MlsMessageOut) -> Self {
32        Self(value.into())
33    }
34}
35
36impl WelcomeMessage {
37    /// Serialize this message per the TLS encoding in the spec
38    pub fn serialize(&self) -> Result<Vec<u8>> {
39        MlsMessageOut::from(self.0.clone())
40            .tls_serialize_detached()
41            .map_err(Error::tls_serialize("serializing welcome message as MlsMessageOut"))
42    }
43}