use super::*;
use crate::tree::sender_ratchet::SenderRatchetConfiguration;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct MlsGroupConfig {
pub(crate) wire_format_policy: WireFormatPolicy,
pub(crate) padding_size: usize,
pub(crate) max_past_epochs: usize,
pub(crate) number_of_resumption_secrets: usize,
pub(crate) use_ratchet_tree_extension: bool,
pub(crate) required_capabilities: RequiredCapabilitiesExtension,
pub(crate) sender_ratchet_configuration: SenderRatchetConfiguration,
}
impl MlsGroupConfig {
pub fn builder() -> MlsGroupConfigBuilder {
MlsGroupConfigBuilder::new()
}
pub fn wire_format_policy(&self) -> WireFormatPolicy {
self.wire_format_policy
}
pub fn padding_size(&self) -> usize {
self.padding_size
}
pub fn max_past_epochs(&self) -> usize {
self.max_past_epochs
}
pub fn number_of_resumption_secrets(&self) -> usize {
self.number_of_resumption_secrets
}
pub fn use_ratchet_tree_extension(&self) -> bool {
self.use_ratchet_tree_extension
}
pub fn sender_ratchet_configuration(&self) -> &SenderRatchetConfiguration {
&self.sender_ratchet_configuration
}
#[cfg(any(feature = "test-utils", test))]
pub fn test_default() -> Self {
Self::builder()
.wire_format_policy(WireFormatPolicy::new(
OutgoingWireFormatPolicy::AlwaysPlaintext,
IncomingWireFormatPolicy::Mixed,
))
.build()
}
}
#[derive(Default)]
pub struct MlsGroupConfigBuilder {
config: MlsGroupConfig,
}
impl MlsGroupConfigBuilder {
pub fn new() -> Self {
MlsGroupConfigBuilder {
config: MlsGroupConfig::default(),
}
}
pub fn wire_format_policy(mut self, wire_format_policy: WireFormatPolicy) -> Self {
self.config.wire_format_policy = wire_format_policy;
self
}
pub fn padding_size(mut self, padding_size: usize) -> Self {
self.config.padding_size = padding_size;
self
}
pub fn max_past_epochs(mut self, max_past_epochs: usize) -> Self {
self.config.max_past_epochs = max_past_epochs;
self
}
pub fn number_of_resumtion_secrets(mut self, number_of_resumption_secrets: usize) -> Self {
self.config.number_of_resumption_secrets = number_of_resumption_secrets;
self
}
pub fn use_ratchet_tree_extension(mut self, use_ratchet_tree_extension: bool) -> Self {
self.config.use_ratchet_tree_extension = use_ratchet_tree_extension;
self
}
pub fn sender_ratchet_configuration(
mut self,
sender_ratchet_configuration: SenderRatchetConfiguration,
) -> Self {
self.config.sender_ratchet_configuration = sender_ratchet_configuration;
self
}
pub fn build(self) -> MlsGroupConfig {
self.config
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum IncomingWireFormatPolicy {
AlwaysCiphertext,
AlwaysPlaintext,
Mixed,
}
impl IncomingWireFormatPolicy {
pub(crate) fn is_compatible_with(&self, wire_format: WireFormat) -> bool {
match self {
IncomingWireFormatPolicy::AlwaysCiphertext => wire_format == WireFormat::MlsCiphertext,
IncomingWireFormatPolicy::AlwaysPlaintext => wire_format == WireFormat::MlsPlaintext,
IncomingWireFormatPolicy::Mixed => {
wire_format == WireFormat::MlsCiphertext || wire_format == WireFormat::MlsPlaintext
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub enum OutgoingWireFormatPolicy {
AlwaysCiphertext,
AlwaysPlaintext,
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct WireFormatPolicy {
outgoing: OutgoingWireFormatPolicy,
incoming: IncomingWireFormatPolicy,
}
impl WireFormatPolicy {
#[cfg(any(feature = "test-utils", test))]
pub(crate) fn new(
outgoing: OutgoingWireFormatPolicy,
incoming: IncomingWireFormatPolicy,
) -> Self {
Self { outgoing, incoming }
}
pub fn outgoing(&self) -> OutgoingWireFormatPolicy {
self.outgoing
}
pub fn incoming(&self) -> IncomingWireFormatPolicy {
self.incoming
}
}
impl Default for WireFormatPolicy {
fn default() -> Self {
PURE_CIPHERTEXT_WIRE_FORMAT_POLICY
}
}
impl From<OutgoingWireFormatPolicy> for WireFormat {
fn from(outgoing: OutgoingWireFormatPolicy) -> Self {
match outgoing {
OutgoingWireFormatPolicy::AlwaysCiphertext => WireFormat::MlsCiphertext,
OutgoingWireFormatPolicy::AlwaysPlaintext => WireFormat::MlsPlaintext,
}
}
}
pub const WIRE_FORMAT_POLICIES: [WireFormatPolicy; 4] = [
PURE_PLAINTEXT_WIRE_FORMAT_POLICY,
PURE_CIPHERTEXT_WIRE_FORMAT_POLICY,
MIXED_PLAINTEXT_WIRE_FORMAT_POLICY,
MIXED_CIPHERTEXT_WIRE_FORMAT_POLICY,
];
pub const PURE_PLAINTEXT_WIRE_FORMAT_POLICY: WireFormatPolicy = WireFormatPolicy {
outgoing: OutgoingWireFormatPolicy::AlwaysPlaintext,
incoming: IncomingWireFormatPolicy::AlwaysPlaintext,
};
pub const PURE_CIPHERTEXT_WIRE_FORMAT_POLICY: WireFormatPolicy = WireFormatPolicy {
outgoing: OutgoingWireFormatPolicy::AlwaysCiphertext,
incoming: IncomingWireFormatPolicy::AlwaysCiphertext,
};
pub const MIXED_PLAINTEXT_WIRE_FORMAT_POLICY: WireFormatPolicy = WireFormatPolicy {
outgoing: OutgoingWireFormatPolicy::AlwaysPlaintext,
incoming: IncomingWireFormatPolicy::Mixed,
};
pub const MIXED_CIPHERTEXT_WIRE_FORMAT_POLICY: WireFormatPolicy = WireFormatPolicy {
outgoing: OutgoingWireFormatPolicy::AlwaysCiphertext,
incoming: IncomingWireFormatPolicy::Mixed,
};