core_crypto_ffi/
configuration.rs

1#[cfg(target_family = "wasm")]
2use js_sys::Uint8Array;
3#[cfg(target_family = "wasm")]
4use wasm_bindgen::prelude::*;
5
6use core_crypto::prelude::MlsCustomConfiguration;
7
8use crate::Ciphersuite;
9
10/// See [core_crypto::prelude::MlsWirePolicy]
11#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
12#[cfg_attr(target_family = "wasm", wasm_bindgen, derive(serde::Serialize, serde::Deserialize))]
13#[cfg_attr(not(target_family = "wasm"), derive(uniffi::Enum))]
14#[repr(u8)]
15pub enum WirePolicy {
16    /// Handshake messages are never encrypted
17    #[default]
18    Plaintext = 1,
19    /// Handshake messages are always encrypted
20    Ciphertext = 2,
21}
22
23impl From<core_crypto::prelude::MlsWirePolicy> for WirePolicy {
24    fn from(value: core_crypto::prelude::MlsWirePolicy) -> Self {
25        match value {
26            core_crypto::prelude::MlsWirePolicy::Plaintext => Self::Plaintext,
27            core_crypto::prelude::MlsWirePolicy::Ciphertext => Self::Ciphertext,
28        }
29    }
30}
31
32impl From<WirePolicy> for core_crypto::prelude::MlsWirePolicy {
33    fn from(value: WirePolicy) -> core_crypto::prelude::MlsWirePolicy {
34        match value {
35            WirePolicy::Plaintext => core_crypto::prelude::MlsWirePolicy::Plaintext,
36            WirePolicy::Ciphertext => core_crypto::prelude::MlsWirePolicy::Ciphertext,
37        }
38    }
39}
40
41/// see [core_crypto::prelude::MlsCustomConfiguration]
42#[derive(Debug, Default, Clone, Copy)]
43#[cfg_attr(target_family = "wasm", wasm_bindgen, derive(serde::Serialize, serde::Deserialize))]
44#[cfg_attr(not(target_family = "wasm"), derive(uniffi::Record))]
45pub struct CustomConfiguration {
46    ///  Duration in seconds after which we will automatically force a self-update commit
47    ///  Note: This isn't currently implemented
48    #[cfg_attr(target_family = "wasm", wasm_bindgen(js_name = "keyRotationSpan"))]
49    pub key_rotation_span: Option<u32>,
50    /// Defines if handshake messages are encrypted or not
51    /// Note: encrypted handshake messages are not supported by wire-server
52    #[cfg_attr(target_family = "wasm", wasm_bindgen(js_name = "wirePolicy"))]
53    pub wire_policy: Option<WirePolicy>,
54}
55
56impl From<CustomConfiguration> for MlsCustomConfiguration {
57    fn from(cfg: CustomConfiguration) -> Self {
58        let key_rotation_span = cfg
59            .key_rotation_span
60            .map(|span| std::time::Duration::from_secs(span as u64));
61        let wire_policy = cfg.wire_policy.map(WirePolicy::into).unwrap_or_default();
62        Self {
63            key_rotation_span,
64            wire_policy,
65            ..Default::default()
66        }
67    }
68}
69
70#[cfg(target_family = "wasm")]
71#[wasm_bindgen]
72impl CustomConfiguration {
73    #[wasm_bindgen(constructor)]
74    pub fn new(key_rotation_span: Option<u32>, wire_policy: Option<WirePolicy>) -> Self {
75        Self {
76            key_rotation_span,
77            wire_policy,
78        }
79    }
80}
81
82/// See [core_crypto::prelude::MlsConversationConfiguration]
83#[derive(Debug, Clone)]
84#[cfg_attr(
85    target_family = "wasm",
86    wasm_bindgen(getter_with_clone),
87    derive(serde::Serialize, serde::Deserialize)
88)]
89#[cfg_attr(not(target_family = "wasm"), derive(uniffi::Record))]
90pub struct ConversationConfiguration {
91    #[cfg_attr(target_family = "wasm", wasm_bindgen(readonly))]
92    pub ciphersuite: Option<Ciphersuite>,
93    #[cfg_attr(target_family = "wasm", wasm_bindgen(skip))]
94    pub external_senders: Vec<Vec<u8>>,
95    #[cfg_attr(target_family = "wasm", wasm_bindgen(readonly))]
96    pub custom: CustomConfiguration,
97}
98
99#[cfg(target_family = "wasm")]
100#[wasm_bindgen]
101impl ConversationConfiguration {
102    #[wasm_bindgen(constructor)]
103    pub fn new(
104        ciphersuite: Option<Ciphersuite>,
105        external_senders: Option<Vec<Uint8Array>>,
106        key_rotation_span: Option<u32>,
107        wire_policy: Option<WirePolicy>,
108    ) -> crate::CoreCryptoResult<ConversationConfiguration> {
109        let external_senders = external_senders
110            .unwrap_or_default()
111            .iter()
112            .map(|arr| arr.to_vec())
113            .collect();
114        Ok(Self {
115            ciphersuite,
116            external_senders,
117            custom: CustomConfiguration {
118                key_rotation_span,
119                wire_policy,
120            },
121        })
122    }
123
124    /// List of client IDs that are allowed to be external senders
125    #[wasm_bindgen(getter, js_name = externalSenders)]
126    pub fn external_senders(&self) -> Vec<Uint8Array> {
127        self.external_senders
128            .iter()
129            .map(|sender| sender.as_slice().into())
130            .collect()
131    }
132}