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