openmls/group/public_group/process.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
//! This module contains the implementation of the processing functions for
//! public groups.
use openmls_traits::crypto::OpenMlsCrypto;
use tls_codec::Serialize;
use crate::{
ciphersuite::OpenMlsSignaturePublicKey,
credentials::CredentialWithKey,
error::LibraryError,
framing::{
mls_content::FramedContentBody, ApplicationMessage, DecryptedMessage, ProcessedMessage,
ProcessedMessageContent, ProtocolMessage, Sender, SenderContext, UnverifiedMessage,
},
group::{
errors::ValidationError, mls_group::errors::ProcessMessageError,
past_secrets::MessageSecretsStore, proposal_store::QueuedProposal,
},
messages::proposals::Proposal,
};
use super::PublicGroup;
impl PublicGroup {
/// This function is used to parse messages from the DS.
/// It checks for syntactic errors and makes some semantic checks as well.
/// If the input is a [PrivateMessage] message, it will be decrypted.
/// Returns an [UnverifiedMessage] that can be inspected and later processed in
/// [Self::process_unverified_message()].
/// Checks the following semantic validation:
/// - ValSem002
/// - ValSem003
/// - ValSem004
/// - ValSem005
/// - ValSem006
/// - ValSem007
/// - ValSem009
/// - ValSem112
/// - ValSem245
pub(crate) fn parse_message<'a>(
&self,
decrypted_message: DecryptedMessage,
message_secrets_store_option: impl Into<Option<&'a MessageSecretsStore>>,
) -> Result<UnverifiedMessage, ValidationError> {
let message_secrets_store_option = message_secrets_store_option.into();
let verifiable_content = decrypted_message.verifiable_content();
// Checks the following semantic validation:
// - ValSem004
// - ValSem005
// - ValSem009
self.validate_verifiable_content(verifiable_content, message_secrets_store_option)?;
let message_epoch = verifiable_content.epoch();
// Depending on the epoch of the message, use the correct set of leaf nodes for getting the
// credential and signature key for the member with given index.
let look_up_credential_with_key = |leaf_node_index| {
if message_epoch == self.group_context().epoch() {
self.treesync()
.leaf(leaf_node_index)
.map(CredentialWithKey::from)
} else if let Some(store) = message_secrets_store_option {
store
.leaves_for_epoch(message_epoch)
.get(leaf_node_index.u32() as usize)
.map(CredentialWithKey::from)
} else {
None
}
};
// Extract the credential if the sender is a member or a new member.
// Checks the following semantic validation:
// - ValSem112
// - ValSem245
// - Prepares ValSem246 by setting the right credential. The remainder
// of ValSem246 is validated as part of ValSem010.
// External senders are not supported yet #106/#151.
let CredentialWithKey {
credential,
signature_key,
} = decrypted_message.credential(
look_up_credential_with_key,
self.group_context().extensions().external_senders(),
)?;
let signature_public_key = OpenMlsSignaturePublicKey::from_signature_key(
signature_key,
self.ciphersuite().signature_algorithm(),
);
// For commit messages, we need to check if the sender is a member or a
// new member and set the tree position accordingly.
let sender_context = match decrypted_message.sender() {
Sender::Member(leaf_index) => Some(SenderContext::Member((
self.group_id().clone(),
*leaf_index,
))),
Sender::NewMemberCommit => Some(SenderContext::ExternalCommit((
self.group_id().clone(),
self.treesync().free_leaf_index(),
))),
Sender::External(_) | Sender::NewMemberProposal => None,
};
Ok(UnverifiedMessage::from_decrypted_message(
decrypted_message,
credential,
signature_public_key,
sender_context,
))
}
/// This function is used to parse messages from the DS. It checks for
/// syntactic errors and does semantic validation as well. It returns a
/// [ProcessedMessage] enum. Checks the following semantic validation:
/// - ValSem002
/// - ValSem003
/// - ValSem004
/// - ValSem005
/// - ValSem006
/// - ValSem007
/// - ValSem008
/// - ValSem009
/// - ValSem010
/// - ValSem101
/// - ValSem102
/// - ValSem104
/// - ValSem106
/// - ValSem107
/// - ValSem108
/// - ValSem110
/// - ValSem111
/// - ValSem112
/// - ValSem200
/// - ValSem201
/// - ValSem202: Path must be the right length
/// - ValSem203: Path secrets must decrypt correctly
/// - ValSem204: Public keys from Path must be verified and match the
/// private keys from the direct path
/// - ValSem205
/// - ValSem240
/// - ValSem241
/// - ValSem242
/// - ValSem244
/// - ValSem245
/// - ValSem246 (as part of ValSem010)
pub fn process_message(
&self,
crypto: &impl OpenMlsCrypto,
message: impl Into<ProtocolMessage>,
) -> Result<ProcessedMessage, ProcessMessageError> {
let protocol_message = message.into();
// Checks the following semantic validation:
// - ValSem002
// - ValSem003
self.validate_framing(&protocol_message)?;
let decrypted_message = match protocol_message {
ProtocolMessage::PrivateMessage(_) => {
return Err(ProcessMessageError::IncompatibleWireFormat)
}
ProtocolMessage::PublicMessage(public_message) => {
DecryptedMessage::from_inbound_public_message(
*public_message,
None,
self.group_context()
.tls_serialize_detached()
.map_err(LibraryError::missing_bound_check)?,
crypto,
self.ciphersuite(),
)?
}
};
let unverified_message = self
.parse_message(decrypted_message, None)
.map_err(ProcessMessageError::from)?;
self.process_unverified_message(crypto, unverified_message)
}
}
impl PublicGroup {
/// This processing function does most of the semantic verifications.
/// It returns a [ProcessedMessage] enum.
/// Checks the following semantic validation:
/// - ValSem008
/// - ValSem010
/// - ValSem101
/// - ValSem102
/// - ValSem104
/// - ValSem106
/// - ValSem107
/// - ValSem108
/// - ValSem110
/// - ValSem111
/// - ValSem112
/// - ValSem200
/// - ValSem201
/// - ValSem202: Path must be the right length
/// - ValSem203: Path secrets must decrypt correctly
/// - ValSem204: Public keys from Path must be verified and match the
/// private keys from the direct path
/// - ValSem205
/// - ValSem240
/// - ValSem241
/// - ValSem242
/// - ValSem244
/// - ValSem246 (as part of ValSem010)
pub(crate) fn process_unverified_message(
&self,
crypto: &impl OpenMlsCrypto,
unverified_message: UnverifiedMessage,
) -> Result<ProcessedMessage, ProcessMessageError> {
// Checks the following semantic validation:
// - ValSem010
// - ValSem246 (as part of ValSem010)
// - https://validation.openmls.tech/#valn1203
let (content, credential) =
unverified_message.verify(self.ciphersuite(), crypto, self.version())?;
match content.sender() {
Sender::Member(_) | Sender::NewMemberCommit | Sender::NewMemberProposal => {
let sender = content.sender().clone();
let authenticated_data = content.authenticated_data().to_owned();
let content = match content.content() {
FramedContentBody::Application(application_message) => {
ProcessedMessageContent::ApplicationMessage(ApplicationMessage::new(
application_message.as_slice().to_owned(),
))
}
FramedContentBody::Proposal(_) => {
let proposal = Box::new(QueuedProposal::from_authenticated_content_by_ref(
self.ciphersuite(),
crypto,
content,
)?);
if matches!(sender, Sender::NewMemberProposal) {
ProcessedMessageContent::ExternalJoinProposalMessage(proposal)
} else {
ProcessedMessageContent::ProposalMessage(proposal)
}
}
FramedContentBody::Commit(_) => {
let staged_commit = self.stage_commit(&content, crypto)?;
ProcessedMessageContent::StagedCommitMessage(Box::new(staged_commit))
}
};
Ok(ProcessedMessage::new(
self.group_id().clone(),
self.group_context().epoch(),
sender,
authenticated_data,
content,
credential,
))
}
Sender::External(_) => {
let sender = content.sender().clone();
let data = content.authenticated_data().to_owned();
match content.content() {
FramedContentBody::Application(_) => {
Err(ProcessMessageError::UnauthorizedExternalApplicationMessage)
}
FramedContentBody::Proposal(Proposal::Remove(_)) => {
let content = ProcessedMessageContent::ProposalMessage(Box::new(
QueuedProposal::from_authenticated_content_by_ref(
self.ciphersuite(),
crypto,
content,
)?,
));
Ok(ProcessedMessage::new(
self.group_id().clone(),
self.group_context().epoch(),
sender,
data,
content,
credential,
))
}
// TODO #151/#106
FramedContentBody::Proposal(_) => {
Err(ProcessMessageError::UnsupportedProposalType)
}
FramedContentBody::Commit(_) => unimplemented!(),
}
}
}
}
}