| 1 | #include <mls/key_schedule.h> |
| 2 | #include <mls/messages.h> |
| 3 | #include <mls/state.h> |
| 4 | #include <mls/treekem.h> |
| 5 | |
| 6 | #include "grease.h" |
| 7 | |
| 8 | namespace mlspp { |
| 9 | |
| 10 | // Extensions |
| 11 | |
| 12 | const Extension::Type ExternalPubExtension::type = ExtensionType::external_pub; |
| 13 | const Extension::Type RatchetTreeExtension::type = ExtensionType::ratchet_tree; |
| 14 | const Extension::Type ExternalSendersExtension::type = |
| 15 | ExtensionType::external_senders; |
| 16 | const Extension::Type SFrameParameters::type = ExtensionType::sframe_parameters; |
| 17 | const Extension::Type SFrameCapabilities::type = |
| 18 | ExtensionType::sframe_parameters; |
| 19 | |
| 20 | bool |
| 21 | SFrameCapabilities::compatible(const SFrameParameters& params) const |
| 22 | { |
| 23 | return stdx::contains(cipher_suites, params.cipher_suite); |
| 24 | } |
| 25 | |
| 26 | // GroupContext |
| 27 | |
| 28 | GroupContext::GroupContext(CipherSuite cipher_suite_in, |
| 29 | bytes group_id_in, |
| 30 | epoch_t epoch_in, |
| 31 | bytes tree_hash_in, |
| 32 | bytes confirmed_transcript_hash_in, |
| 33 | ExtensionList extensions_in) |
| 34 | : cipher_suite(cipher_suite_in) |
| 35 | , group_id(std::move(group_id_in)) |
| 36 | , epoch(epoch_in) |
| 37 | , tree_hash(std::move(tree_hash_in)) |
| 38 | , confirmed_transcript_hash(std::move(confirmed_transcript_hash_in)) |
| 39 | , extensions(std::move(extensions_in)) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | // GroupInfo |
| 44 | |
| 45 | GroupInfo::GroupInfo(GroupContext group_context_in, |
| 46 | ExtensionList extensions_in, |
| 47 | bytes confirmation_tag_in) |
| 48 | : group_context(std::move(group_context_in)) |
| 49 | , extensions(std::move(extensions_in)) |
| 50 | , confirmation_tag(std::move(confirmation_tag_in)) |
| 51 | , signer(0) |
| 52 | { |
| 53 | grease(extensions); |
| 54 | } |
| 55 | |
| 56 | struct GroupInfoTBS |
| 57 | { |
| 58 | GroupContext group_context; |
| 59 | ExtensionList extensions; |
| 60 | bytes confirmation_tag; |
| 61 | LeafIndex signer; |
| 62 | |
| 63 | TLS_SERIALIZABLE(group_context, extensions, confirmation_tag, signer) |
| 64 | }; |
| 65 | |
| 66 | bytes |
| 67 | GroupInfo::to_be_signed() const |
| 68 | { |
| 69 | return tls::marshal( |
| 70 | GroupInfoTBS{ group_context, extensions, confirmation_tag, signer }); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | GroupInfo::sign(const TreeKEMPublicKey& tree, |
| 75 | LeafIndex signer_index, |
| 76 | const SignaturePrivateKey& priv) |
| 77 | { |
| 78 | auto maybe_leaf = tree.leaf_node(signer_index); |
| 79 | if (!maybe_leaf) { |
| 80 | throw InvalidParameterError("Cannot sign from a blank leaf"); |
| 81 | } |
| 82 | |
| 83 | if (priv.public_key != opt::get(maybe_leaf).signature_key) { |
| 84 | throw InvalidParameterError("Bad key for index"); |
| 85 | } |
| 86 | |
| 87 | signer = signer_index; |
| 88 | signature = priv.sign(tree.suite, sign_label::group_info, to_be_signed()); |
| 89 | } |
| 90 | |
| 91 | bool |
| 92 | GroupInfo::verify(const TreeKEMPublicKey& tree) const |
| 93 | { |
| 94 | auto maybe_leaf = tree.leaf_node(signer); |
| 95 | if (!maybe_leaf) { |
| 96 | throw InvalidParameterError("Signer not found"); |
| 97 | } |
| 98 | |
| 99 | const auto& leaf = opt::get(maybe_leaf); |
| 100 | return verify(leaf.signature_key); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | GroupInfo::sign(LeafIndex signer_index, const SignaturePrivateKey& priv) |
| 105 | { |
| 106 | signer = signer_index; |
| 107 | signature = priv.sign( |
| 108 | group_context.cipher_suite, sign_label::group_info, to_be_signed()); |
| 109 | } |
| 110 | |
| 111 | bool |
| 112 | GroupInfo::verify(const SignaturePublicKey& pub) const |
| 113 | { |
| 114 | return pub.verify(group_context.cipher_suite, |
| 115 | sign_label::group_info, |
| 116 | to_be_signed(), |
| 117 | signature); |
| 118 | } |
| 119 | |
| 120 | // Welcome |
| 121 | |
| 122 | Welcome::Welcome() |
| 123 | : cipher_suite(CipherSuite::ID::unknown) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | Welcome::Welcome(CipherSuite suite, |
| 128 | const bytes& joiner_secret, |
| 129 | const std::vector<PSKWithSecret>& psks, |
| 130 | const GroupInfo& group_info) |
| 131 | : cipher_suite(suite) |
| 132 | , _joiner_secret(joiner_secret) |
| 133 | { |
| 134 | // Cache the list of PSK IDs |
| 135 | for (const auto& psk : psks) { |
| 136 | _psks.psks.push_back(psk.id); |
| 137 | } |
| 138 | |
| 139 | // Pre-encrypt the GroupInfo |
| 140 | auto [key, nonce] = group_info_key_nonce(suite, joiner_secret, psks); |
| 141 | auto group_info_data = tls::marshal(group_info); |
| 142 | encrypted_group_info = |
| 143 | cipher_suite.hpke().aead.seal(key, nonce, {}, group_info_data); |
| 144 | } |
| 145 | |
| 146 | std::optional<int> |
| 147 | Welcome::find(const KeyPackage& kp) const |
| 148 | { |
| 149 | auto ref = kp.ref(); |
| 150 | for (size_t i = 0; i < secrets.size(); i++) { |
| 151 | if (ref == secrets[i].new_member) { |
| 152 | return static_cast<int>(i); |
| 153 | } |
| 154 | } |
| 155 | return std::nullopt; |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | Welcome::encrypt(const KeyPackage& kp, const std::optional<bytes>& path_secret) |
| 160 | { |
| 161 | auto gs = GroupSecrets{ _joiner_secret, std::nullopt, _psks }; |
| 162 | if (path_secret) { |
| 163 | gs.path_secret = GroupSecrets::PathSecret{ opt::get(path_secret) }; |
| 164 | } |
| 165 | |
| 166 | auto gs_data = tls::marshal(gs); |
| 167 | auto enc_gs = kp.init_key.encrypt( |
| 168 | kp.cipher_suite, encrypt_label::welcome, encrypted_group_info, gs_data); |
| 169 | secrets.push_back({ kp.ref(), enc_gs }); |
| 170 | } |
| 171 | |
| 172 | GroupSecrets |
| 173 | Welcome::decrypt_secrets(int kp_index, const HPKEPrivateKey& init_priv) const |
| 174 | { |
| 175 | auto secrets_data = |
| 176 | init_priv.decrypt(cipher_suite, |
| 177 | encrypt_label::welcome, |
| 178 | encrypted_group_info, |
| 179 | secrets.at(kp_index).encrypted_group_secrets); |
| 180 | return tls::get<GroupSecrets>(secrets_data); |
| 181 | } |
| 182 | |
| 183 | GroupInfo |
| 184 | Welcome::decrypt(const bytes& joiner_secret, |
| 185 | const std::vector<PSKWithSecret>& psks) const |
| 186 | { |
| 187 | auto [key, nonce] = group_info_key_nonce(cipher_suite, joiner_secret, psks); |
| 188 | auto group_info_data = |
| 189 | cipher_suite.hpke().aead.open(key, nonce, {}, encrypted_group_info); |
| 190 | if (!group_info_data) { |
| 191 | throw ProtocolError("Welcome decryption failed"); |
| 192 | } |
| 193 | |
| 194 | return tls::get<GroupInfo>(opt::get(group_info_data)); |
| 195 | } |
| 196 | |
| 197 | KeyAndNonce |
| 198 | Welcome::group_info_key_nonce(CipherSuite suite, |
| 199 | const bytes& joiner_secret, |
| 200 | const std::vector<PSKWithSecret>& psks) |
| 201 | { |
| 202 | auto welcome_secret = |
| 203 | KeyScheduleEpoch::welcome_secret(suite, joiner_secret, psks); |
| 204 | |
| 205 | // XXX(RLB): These used to be done with ExpandWithLabel. Should we do that |
| 206 | // instead, for better domain separation? (In particular, including "mls10") |
| 207 | // That is what we do for the sender data key/nonce. |
| 208 | auto key = |
| 209 | suite.expand_with_label(welcome_secret, "key", {}, suite.key_size()); |
| 210 | auto nonce = |
| 211 | suite.expand_with_label(welcome_secret, "nonce", {}, suite.nonce_size()); |
| 212 | return { std::move(key), std::move(nonce) }; |
| 213 | } |
| 214 | |
| 215 | // Commit |
| 216 | std::optional<bytes> |
| 217 | Commit::valid_external() const |
| 218 | { |
| 219 | // External Commits MUST contain a path field (and is therefore a "full" |
| 220 | // Commit). The joiner is added at the leftmost free leaf node (just as if |
| 221 | // they were added with an Add proposal), and the path is calculated relative |
| 222 | // to that leaf node. |
| 223 | // |
| 224 | // The Commit MUST NOT include any proposals by reference, since an external |
| 225 | // joiner cannot determine the validity of proposals sent within the group |
| 226 | const auto all_by_value = stdx::all_of(proposals, [](const auto& p) { |
| 227 | return var::holds_alternative<Proposal>(p.content); |
| 228 | }); |
| 229 | if (!path || !all_by_value) { |
| 230 | return std::nullopt; |
| 231 | } |
| 232 | |
| 233 | const auto ext_init_ptr = stdx::find_if(proposals, [](const auto& p) { |
| 234 | const auto proposal = var::get<Proposal>(p.content); |
| 235 | return proposal.proposal_type() == ProposalType::external_init; |
| 236 | }); |
| 237 | if (ext_init_ptr == proposals.end()) { |
| 238 | return std::nullopt; |
| 239 | } |
| 240 | |
| 241 | const auto& ext_init_proposal = var::get<Proposal>(ext_init_ptr->content); |
| 242 | const auto& ext_init = var::get<ExternalInit>(ext_init_proposal.content); |
| 243 | return ext_init.kem_output; |
| 244 | } |
| 245 | |
| 246 | // PublicMessage |
| 247 | Proposal::Type |
| 248 | Proposal::proposal_type() const |
| 249 | { |
| 250 | return tls::variant<ProposalType>::type(content).val; |
| 251 | } |
| 252 | |
| 253 | SenderType |
| 254 | Sender::sender_type() const |
| 255 | { |
| 256 | return tls::variant<SenderType>::type(sender); |
| 257 | } |
| 258 | |
| 259 | tls::ostream& |
| 260 | operator<<(tls::ostream& str, const GroupContentAuthData& obj) |
| 261 | { |
| 262 | switch (obj.content_type) { |
| 263 | case ContentType::proposal: |
| 264 | case ContentType::application: |
| 265 | return str << obj.signature; |
| 266 | |
| 267 | case ContentType::commit: |
| 268 | return str << obj.signature << opt::get(obj.confirmation_tag); |
| 269 | |
| 270 | default: |
| 271 | throw InvalidParameterError("Invalid content type"); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | tls::istream& |
| 276 | operator>>(tls::istream& str, GroupContentAuthData& obj) |
| 277 | { |
| 278 | switch (obj.content_type) { |
| 279 | case ContentType::proposal: |
| 280 | case ContentType::application: |
| 281 | return str >> obj.signature; |
| 282 | |
| 283 | case ContentType::commit: |
| 284 | obj.confirmation_tag.emplace(); |
| 285 | return str >> obj.signature >> opt::get(obj.confirmation_tag); |
| 286 | |
| 287 | default: |
| 288 | throw InvalidParameterError("Invalid content type"); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | bool |
| 293 | operator==(const GroupContentAuthData& lhs, const GroupContentAuthData& rhs) |
| 294 | { |
| 295 | return lhs.content_type == rhs.content_type && |
| 296 | lhs.signature == rhs.signature && |
| 297 | lhs.confirmation_tag == rhs.confirmation_tag; |
| 298 | } |
| 299 | |
| 300 | GroupContent::GroupContent(bytes group_id_in, |
| 301 | epoch_t epoch_in, |
| 302 | Sender sender_in, |
| 303 | bytes authenticated_data_in, |
| 304 | RawContent content_in) |
| 305 | : group_id(std::move(group_id_in)) |
| 306 | , epoch(epoch_in) |
| 307 | , sender(sender_in) |
| 308 | , authenticated_data(std::move(authenticated_data_in)) |
| 309 | , content(std::move(content_in)) |
| 310 | { |
| 311 | } |
| 312 | |
| 313 | GroupContent::GroupContent(bytes group_id_in, |
| 314 | epoch_t epoch_in, |
| 315 | Sender sender_in, |
| 316 | bytes authenticated_data_in, |
| 317 | ContentType content_type) |
| 318 | : group_id(std::move(group_id_in)) |
| 319 | , epoch(epoch_in) |
| 320 | , sender(sender_in) |
| 321 | , authenticated_data(std::move(authenticated_data_in)) |
| 322 | { |
| 323 | switch (content_type) { |
| 324 | case ContentType::commit: |
| 325 | content.emplace<Commit>(); |
| 326 | break; |
| 327 | |
| 328 | case ContentType::proposal: |
| 329 | content.emplace<Proposal>(); |
| 330 | break; |
| 331 | |
| 332 | case ContentType::application: |
| 333 | content.emplace<ApplicationData>(); |
| 334 | break; |
| 335 | |
| 336 | default: |
| 337 | throw InvalidParameterError("Invalid content type"); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | ContentType |
| 342 | GroupContent::content_type() const |
| 343 | { |
| 344 | return tls::variant<ContentType>::type(content); |
| 345 | } |
| 346 | |
| 347 | AuthenticatedContent |
| 348 | AuthenticatedContent::sign(WireFormat wire_format, |
| 349 | GroupContent content, |
| 350 | CipherSuite suite, |
| 351 | const SignaturePrivateKey& sig_priv, |
| 352 | const std::optional<GroupContext>& context) |
| 353 | { |
| 354 | if (wire_format == WireFormat::mls_public_message && |
| 355 | content.content_type() == ContentType::application) { |
| 356 | throw InvalidParameterError( |
| 357 | "Application data cannot be sent as PublicMessage"); |
| 358 | } |
| 359 | |
| 360 | auto content_auth = AuthenticatedContent{ wire_format, std::move(content) }; |
| 361 | auto tbs = content_auth.to_be_signed(context); |
| 362 | content_auth.auth.signature = |
| 363 | sig_priv.sign(suite, sign_label::mls_content, tbs); |
| 364 | return content_auth; |
| 365 | } |
| 366 | |
| 367 | bool |
| 368 | AuthenticatedContent::verify(CipherSuite suite, |
| 369 | const SignaturePublicKey& sig_pub, |
| 370 | const std::optional<GroupContext>& context) const |
| 371 | { |
| 372 | if (wire_format == WireFormat::mls_public_message && |
| 373 | content.content_type() == ContentType::application) { |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | auto tbs = to_be_signed(context); |
| 378 | return sig_pub.verify(suite, sign_label::mls_content, tbs, auth.signature); |
| 379 | } |
| 380 | |
| 381 | struct ConfirmedTranscriptHashInput |
| 382 | { |
| 383 | WireFormat wire_format; |
| 384 | const GroupContent& content; |
| 385 | const bytes& signature; |
| 386 | |
| 387 | TLS_SERIALIZABLE(wire_format, content, signature); |
| 388 | }; |
| 389 | |
| 390 | struct InterimTranscriptHashInput |
| 391 | { |
| 392 | const bytes& confirmation_tag; |
| 393 | |
| 394 | TLS_SERIALIZABLE(confirmation_tag); |
| 395 | }; |
| 396 | |
| 397 | bytes |
| 398 | AuthenticatedContent::confirmed_transcript_hash_input() const |
| 399 | { |
| 400 | return tls::marshal(ConfirmedTranscriptHashInput{ |
| 401 | wire_format, |
| 402 | content, |
| 403 | auth.signature, |
| 404 | }); |
| 405 | } |
| 406 | |
| 407 | bytes |
| 408 | AuthenticatedContent::interim_transcript_hash_input() const |
| 409 | { |
| 410 | return tls::marshal( |
| 411 | InterimTranscriptHashInput{ opt::get(auth.confirmation_tag) }); |
| 412 | } |
| 413 | |
| 414 | void |
| 415 | AuthenticatedContent::set_confirmation_tag(const bytes& confirmation_tag) |
| 416 | { |
| 417 | auth.confirmation_tag = confirmation_tag; |
| 418 | } |
| 419 | |
| 420 | bool |
| 421 | AuthenticatedContent::check_confirmation_tag( |
| 422 | const bytes& confirmation_tag) const |
| 423 | { |
| 424 | return confirmation_tag == opt::get(auth.confirmation_tag); |
| 425 | } |
| 426 | |
| 427 | tls::ostream& |
| 428 | operator<<(tls::ostream& str, const AuthenticatedContent& obj) |
| 429 | { |
| 430 | return str << obj.wire_format << obj.content << obj.auth; |
| 431 | } |
| 432 | |
| 433 | tls::istream& |
| 434 | operator>>(tls::istream& str, AuthenticatedContent& obj) |
| 435 | { |
| 436 | str >> obj.wire_format >> obj.content; |
| 437 | |
| 438 | obj.auth.content_type = obj.content.content_type(); |
| 439 | return str >> obj.auth; |
| 440 | } |
| 441 | |
| 442 | bool |
| 443 | operator==(const AuthenticatedContent& lhs, const AuthenticatedContent& rhs) |
| 444 | { |
| 445 | return lhs.wire_format == rhs.wire_format && lhs.content == rhs.content && |
| 446 | lhs.auth == rhs.auth; |
| 447 | } |
| 448 | |
| 449 | AuthenticatedContent::AuthenticatedContent(WireFormat wire_format_in, |
| 450 | GroupContent content_in) |
| 451 | : wire_format(wire_format_in) |
| 452 | , content(std::move(content_in)) |
| 453 | { |
| 454 | auth.content_type = content.content_type(); |
| 455 | } |
| 456 | |
| 457 | AuthenticatedContent::AuthenticatedContent(WireFormat wire_format_in, |
| 458 | GroupContent content_in, |
| 459 | GroupContentAuthData auth_in) |
| 460 | : wire_format(wire_format_in) |
| 461 | , content(std::move(content_in)) |
| 462 | , auth(std::move(auth_in)) |
| 463 | { |
| 464 | } |
| 465 | |
| 466 | const AuthenticatedContent& |
| 467 | ValidatedContent::authenticated_content() const |
| 468 | { |
| 469 | return content_auth; |
| 470 | } |
| 471 | |
| 472 | ValidatedContent::ValidatedContent(AuthenticatedContent content_auth_in) |
| 473 | : content_auth(std::move(content_auth_in)) |
| 474 | { |
| 475 | } |
| 476 | |
| 477 | bool |
| 478 | operator==(const ValidatedContent& lhs, const ValidatedContent& rhs) |
| 479 | { |
| 480 | return lhs.content_auth == rhs.content_auth; |
| 481 | } |
| 482 | |
| 483 | struct GroupContentTBS |
| 484 | { |
| 485 | WireFormat wire_format = WireFormat::reserved; |
| 486 | const GroupContent& content; |
| 487 | const std::optional<GroupContext>& context; |
| 488 | }; |
| 489 | |
| 490 | static tls::ostream& |
| 491 | operator<<(tls::ostream& str, const GroupContentTBS& obj) |
| 492 | { |
| 493 | str << ProtocolVersion::mls10 << obj.wire_format << obj.content; |
| 494 | |
| 495 | switch (obj.content.sender.sender_type()) { |
| 496 | case SenderType::member: |
| 497 | case SenderType::new_member_commit: |
| 498 | str << opt::get(obj.context); |
| 499 | break; |
| 500 | |
| 501 | case SenderType::external: |
| 502 | case SenderType::new_member_proposal: |
| 503 | break; |
| 504 | |
| 505 | default: |
| 506 | throw InvalidParameterError("Invalid sender type"); |
| 507 | } |
| 508 | |
| 509 | return str; |
| 510 | } |
| 511 | |
| 512 | bytes |
| 513 | AuthenticatedContent::to_be_signed( |
| 514 | const std::optional<GroupContext>& context) const |
| 515 | { |
| 516 | return tls::marshal(GroupContentTBS{ |
| 517 | wire_format, |
| 518 | content, |
| 519 | context, |
| 520 | }); |
| 521 | } |
| 522 | |
| 523 | PublicMessage |
| 524 | PublicMessage::protect(AuthenticatedContent content_auth, |
| 525 | CipherSuite suite, |
| 526 | const std::optional<bytes>& membership_key, |
| 527 | const std::optional<GroupContext>& context) |
| 528 | { |
| 529 | auto pt = PublicMessage(std::move(content_auth)); |
| 530 | |
| 531 | // Add the membership_mac if required |
| 532 | switch (pt.content.sender.sender_type()) { |
| 533 | case SenderType::member: |
| 534 | pt.membership_tag = |
| 535 | pt.membership_mac(suite, opt::get(membership_key), context); |
| 536 | break; |
| 537 | |
| 538 | default: |
| 539 | break; |
| 540 | } |
| 541 | |
| 542 | return pt; |
| 543 | } |
| 544 | |
| 545 | std::optional<ValidatedContent> |
| 546 | PublicMessage::unprotect(CipherSuite suite, |
| 547 | const std::optional<bytes>& membership_key, |
| 548 | const std::optional<GroupContext>& context) const |
| 549 | { |
| 550 | // Verify the membership_tag if the message was sent within the group |
| 551 | switch (content.sender.sender_type()) { |
| 552 | case SenderType::member: { |
| 553 | auto candidate = membership_mac(suite, opt::get(membership_key), context); |
| 554 | if (candidate != opt::get(membership_tag)) { |
| 555 | return std::nullopt; |
| 556 | } |
| 557 | break; |
| 558 | } |
| 559 | |
| 560 | default: |
| 561 | break; |
| 562 | } |
| 563 | |
| 564 | return { { AuthenticatedContent{ |
| 565 | WireFormat::mls_public_message, |
| 566 | content, |
| 567 | auth, |
| 568 | } } }; |
| 569 | } |
| 570 | |
| 571 | bool |
| 572 | PublicMessage::contains(const AuthenticatedContent& content_auth) const |
| 573 | { |
| 574 | return content == content_auth.content && auth == content_auth.auth; |
| 575 | } |
| 576 | |
| 577 | AuthenticatedContent |
| 578 | PublicMessage::authenticated_content() const |
| 579 | { |
| 580 | auto auth_content = AuthenticatedContent{}; |
| 581 | auth_content.wire_format = WireFormat::mls_public_message; |
| 582 | auth_content.content = content; |
| 583 | auth_content.auth = auth; |
| 584 | return auth_content; |
| 585 | } |
| 586 | |
| 587 | PublicMessage::PublicMessage(AuthenticatedContent content_auth) |
| 588 | : content(std::move(content_auth.content)) |
| 589 | , auth(std::move(content_auth.auth)) |
| 590 | { |
| 591 | if (content_auth.wire_format != WireFormat::mls_public_message) { |
| 592 | throw InvalidParameterError("Wire format mismatch (not mls_plaintext)"); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | struct GroupContentTBM |
| 597 | { |
| 598 | GroupContentTBS content_tbs; |
| 599 | GroupContentAuthData auth; |
| 600 | |
| 601 | TLS_SERIALIZABLE(content_tbs, auth); |
| 602 | }; |
| 603 | |
| 604 | bytes |
| 605 | PublicMessage::membership_mac(CipherSuite suite, |
| 606 | const bytes& membership_key, |
| 607 | const std::optional<GroupContext>& context) const |
| 608 | { |
| 609 | auto tbm = tls::marshal(GroupContentTBM{ |
| 610 | { WireFormat::mls_public_message, content, context }, |
| 611 | auth, |
| 612 | }); |
| 613 | |
| 614 | return suite.digest().hmac(membership_key, tbm); |
| 615 | } |
| 616 | |
| 617 | tls::ostream& |
| 618 | operator<<(tls::ostream& str, const PublicMessage& obj) |
| 619 | { |
| 620 | switch (obj.content.sender.sender_type()) { |
| 621 | case SenderType::member: |
| 622 | return str << obj.content << obj.auth << opt::get(obj.membership_tag); |
| 623 | |
| 624 | case SenderType::external: |
| 625 | case SenderType::new_member_proposal: |
| 626 | case SenderType::new_member_commit: |
| 627 | return str << obj.content << obj.auth; |
| 628 | |
| 629 | default: |
| 630 | throw InvalidParameterError("Invalid sender type"); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | tls::istream& |
| 635 | operator>>(tls::istream& str, PublicMessage& obj) |
| 636 | { |
| 637 | str >> obj.content; |
| 638 | |
| 639 | obj.auth.content_type = obj.content.content_type(); |
| 640 | str >> obj.auth; |
| 641 | |
| 642 | if (obj.content.sender.sender_type() == SenderType::member) { |
| 643 | obj.membership_tag.emplace(); |
| 644 | str >> opt::get(obj.membership_tag); |
| 645 | } |
| 646 | |
| 647 | return str; |
| 648 | } |
| 649 | |
| 650 | bool |
| 651 | operator==(const PublicMessage& lhs, const PublicMessage& rhs) |
| 652 | { |
| 653 | return lhs.content == rhs.content && lhs.auth == rhs.auth && |
| 654 | lhs.membership_tag == rhs.membership_tag; |
| 655 | } |
| 656 | |
| 657 | bool |
| 658 | operator!=(const PublicMessage& lhs, const PublicMessage& rhs) |
| 659 | { |
| 660 | return !(lhs == rhs); |
| 661 | } |
| 662 | |
| 663 | static bytes |
| 664 | marshal_ciphertext_content(const GroupContent& content, |
| 665 | const GroupContentAuthData& auth, |
| 666 | size_t padding_size) |
| 667 | { |
| 668 | auto w = tls::ostream{}; |
| 669 | var::visit([&w](const auto& val) { w << val; }, content.content); |
| 670 | w << auth; |
| 671 | w.write_raw(bytes(padding_size, 0)); |
| 672 | return w.bytes(); |
| 673 | } |
| 674 | |
| 675 | static void |
| 676 | unmarshal_ciphertext_content(const bytes& content_pt, |
| 677 | GroupContent& content, |
| 678 | GroupContentAuthData& auth) |
| 679 | { |
| 680 | auto r = tls::istream(content_pt); |
| 681 | |
| 682 | var::visit([&r](auto& val) { r >> val; }, content.content); |
| 683 | r >> auth; |
| 684 | |
| 685 | const auto padding = r.bytes(); |
| 686 | const auto nonzero = [](const auto& x) { return x != 0; }; |
| 687 | if (stdx::any_of(padding, nonzero)) { |
| 688 | throw ProtocolError("Malformed AuthenticatedContentTBE padding"); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | struct ContentAAD |
| 693 | { |
| 694 | const bytes& group_id; |
| 695 | const epoch_t epoch; |
| 696 | const ContentType content_type; |
| 697 | const bytes& authenticated_data; |
| 698 | |
| 699 | TLS_SERIALIZABLE(group_id, epoch, content_type, authenticated_data) |
| 700 | }; |
| 701 | |
| 702 | struct SenderData |
| 703 | { |
| 704 | LeafIndex sender{ 0 }; |
| 705 | uint32_t generation{ 0 }; |
| 706 | ReuseGuard reuse_guard{ 0, 0, 0, 0 }; |
| 707 | |
| 708 | TLS_SERIALIZABLE(sender, generation, reuse_guard) |
| 709 | }; |
| 710 | |
| 711 | struct SenderDataAAD |
| 712 | { |
| 713 | const bytes& group_id; |
| 714 | const epoch_t epoch; |
| 715 | const ContentType content_type; |
| 716 | |
| 717 | TLS_SERIALIZABLE(group_id, epoch, content_type) |
| 718 | }; |
| 719 | |
| 720 | PrivateMessage |
| 721 | PrivateMessage::protect(AuthenticatedContent content_auth, |
| 722 | CipherSuite suite, |
| 723 | GroupKeySource& keys, |
| 724 | const bytes& sender_data_secret, |
| 725 | size_t padding_size) |
| 726 | { |
| 727 | // Pull keys from the secret tree |
| 728 | auto index = |
| 729 | var::get<MemberSender>(content_auth.content.sender.sender).sender; |
| 730 | auto content_type = content_auth.content.content_type(); |
| 731 | auto [generation, reuse_guard, content_keys] = keys.next(content_type, index); |
| 732 | |
| 733 | // Encrypt the content |
| 734 | auto content_pt = marshal_ciphertext_content( |
| 735 | content_auth.content, content_auth.auth, padding_size); |
| 736 | auto content_aad = tls::marshal(ContentAAD{ |
| 737 | content_auth.content.group_id, |
| 738 | content_auth.content.epoch, |
| 739 | content_auth.content.content_type(), |
| 740 | content_auth.content.authenticated_data, |
| 741 | }); |
| 742 | |
| 743 | auto content_ct = suite.hpke().aead.seal( |
| 744 | content_keys.key, content_keys.nonce, content_aad, content_pt); |
| 745 | |
| 746 | // Encrypt the sender data |
| 747 | auto sender_index = |
| 748 | var::get<MemberSender>(content_auth.content.sender.sender).sender; |
| 749 | auto sender_data_pt = tls::marshal(SenderData{ |
| 750 | sender_index, |
| 751 | generation, |
| 752 | reuse_guard, |
| 753 | }); |
| 754 | auto sender_data_aad = tls::marshal(SenderDataAAD{ |
| 755 | content_auth.content.group_id, |
| 756 | content_auth.content.epoch, |
| 757 | content_auth.content.content_type(), |
| 758 | }); |
| 759 | |
| 760 | auto sender_data_keys = |
| 761 | KeyScheduleEpoch::sender_data_keys(suite, sender_data_secret, content_ct); |
| 762 | |
| 763 | auto sender_data_ct = suite.hpke().aead.seal(sender_data_keys.key, |
| 764 | sender_data_keys.nonce, |
| 765 | sender_data_aad, |
| 766 | sender_data_pt); |
| 767 | |
| 768 | return PrivateMessage{ |
| 769 | std::move(content_auth.content), |
| 770 | std::move(sender_data_ct), |
| 771 | std::move(content_ct), |
| 772 | }; |
| 773 | } |
| 774 | |
| 775 | std::optional<ValidatedContent> |
| 776 | PrivateMessage::unprotect(CipherSuite suite, |
| 777 | GroupKeySource& keys, |
| 778 | const bytes& sender_data_secret) const |
| 779 | { |
| 780 | // Decrypt and parse the sender data |
| 781 | auto sender_data_keys = |
| 782 | KeyScheduleEpoch::sender_data_keys(suite, sender_data_secret, ciphertext); |
| 783 | auto sender_data_aad = tls::marshal(SenderDataAAD{ |
| 784 | group_id, |
| 785 | epoch, |
| 786 | content_type, |
| 787 | }); |
| 788 | |
| 789 | auto sender_data_pt = suite.hpke().aead.open(sender_data_keys.key, |
| 790 | sender_data_keys.nonce, |
| 791 | sender_data_aad, |
| 792 | encrypted_sender_data); |
| 793 | if (!sender_data_pt) { |
| 794 | return std::nullopt; |
| 795 | } |
| 796 | |
| 797 | auto sender_data = tls::get<SenderData>(opt::get(sender_data_pt)); |
| 798 | if (!keys.has_leaf(sender_data.sender)) { |
| 799 | return std::nullopt; |
| 800 | } |
| 801 | |
| 802 | // Decrypt the content |
| 803 | auto content_keys = keys.get(content_type, |
| 804 | sender_data.sender, |
| 805 | sender_data.generation, |
| 806 | sender_data.reuse_guard); |
| 807 | keys.erase(content_type, sender_data.sender, sender_data.generation); |
| 808 | |
| 809 | auto content_aad = tls::marshal(ContentAAD{ |
| 810 | group_id, |
| 811 | epoch, |
| 812 | content_type, |
| 813 | authenticated_data, |
| 814 | }); |
| 815 | |
| 816 | auto content_pt = suite.hpke().aead.open( |
| 817 | content_keys.key, content_keys.nonce, content_aad, ciphertext); |
| 818 | if (!content_pt) { |
| 819 | return std::nullopt; |
| 820 | } |
| 821 | |
| 822 | // Parse the content |
| 823 | auto content = GroupContent{ group_id, |
| 824 | epoch, |
| 825 | { MemberSender{ sender_data.sender } }, |
| 826 | authenticated_data, |
| 827 | content_type }; |
| 828 | auto auth = GroupContentAuthData{ content_type, {}, {} }; |
| 829 | |
| 830 | unmarshal_ciphertext_content(opt::get(content_pt), content, auth); |
| 831 | |
| 832 | return { { AuthenticatedContent{ |
| 833 | WireFormat::mls_private_message, |
| 834 | std::move(content), |
| 835 | std::move(auth), |
| 836 | } } }; |
| 837 | } |
| 838 | |
| 839 | PrivateMessage::PrivateMessage(GroupContent content, |
| 840 | bytes encrypted_sender_data_in, |
| 841 | bytes ciphertext_in) |
| 842 | : group_id(std::move(content.group_id)) |
| 843 | , epoch(content.epoch) |
| 844 | , content_type(content.content_type()) |
| 845 | , authenticated_data(std::move(content.authenticated_data)) |
| 846 | , encrypted_sender_data(std::move(encrypted_sender_data_in)) |
| 847 | , ciphertext(std::move(ciphertext_in)) |
| 848 | { |
| 849 | } |
| 850 | |
| 851 | bytes |
| 852 | MLSMessage::group_id() const |
| 853 | { |
| 854 | return var::visit( |
| 855 | overloaded{ |
| 856 | [](const PublicMessage& pt) -> bytes { return pt.get_group_id(); }, |
| 857 | [](const PrivateMessage& ct) -> bytes { return ct.get_group_id(); }, |
| 858 | [](const GroupInfo& gi) -> bytes { return gi.group_context.group_id; }, |
| 859 | [](const auto& /* unused */) -> bytes { |
| 860 | throw InvalidParameterError("MLSMessage has no group_id"); |
| 861 | }, |
| 862 | }, |
| 863 | message); |
| 864 | } |
| 865 | |
| 866 | epoch_t |
| 867 | MLSMessage::epoch() const |
| 868 | { |
| 869 | return var::visit( |
| 870 | overloaded{ |
| 871 | [](const PublicMessage& pt) -> epoch_t { return pt.get_epoch(); }, |
| 872 | [](const PrivateMessage& pt) -> epoch_t { return pt.get_epoch(); }, |
| 873 | [](const auto& /* unused */) -> epoch_t { |
| 874 | throw InvalidParameterError("MLSMessage has no epoch"); |
| 875 | }, |
| 876 | }, |
| 877 | message); |
| 878 | } |
| 879 | |
| 880 | WireFormat |
| 881 | MLSMessage::wire_format() const |
| 882 | { |
| 883 | return tls::variant<WireFormat>::type(message); |
| 884 | } |
| 885 | |
| 886 | MLSMessage::MLSMessage(PublicMessage public_message) |
| 887 | : message(std::move(public_message)) |
| 888 | { |
| 889 | } |
| 890 | |
| 891 | MLSMessage::MLSMessage(PrivateMessage private_message) |
| 892 | : message(std::move(private_message)) |
| 893 | { |
| 894 | } |
| 895 | |
| 896 | MLSMessage::MLSMessage(Welcome welcome) |
| 897 | : message(std::move(welcome)) |
| 898 | { |
| 899 | } |
| 900 | |
| 901 | MLSMessage::MLSMessage(GroupInfo group_info) |
| 902 | : message(std::move(group_info)) |
| 903 | { |
| 904 | } |
| 905 | |
| 906 | MLSMessage::MLSMessage(KeyPackage key_package) |
| 907 | : message(std::move(key_package)) |
| 908 | { |
| 909 | } |
| 910 | |
| 911 | MLSMessage |
| 912 | external_proposal(CipherSuite suite, |
| 913 | const bytes& group_id, |
| 914 | epoch_t epoch, |
| 915 | const Proposal& proposal, |
| 916 | uint32_t signer_index, |
| 917 | const SignaturePrivateKey& sig_priv) |
| 918 | { |
| 919 | switch (proposal.proposal_type()) { |
| 920 | // These proposal types are OK |
| 921 | case ProposalType::add: |
| 922 | case ProposalType::remove: |
| 923 | case ProposalType::psk: |
| 924 | case ProposalType::reinit: |
| 925 | case ProposalType::group_context_extensions: |
| 926 | break; |
| 927 | |
| 928 | // These proposal types are forbidden |
| 929 | case ProposalType::invalid: |
| 930 | case ProposalType::update: |
| 931 | case ProposalType::external_init: |
| 932 | default: |
| 933 | throw ProtocolError("External proposal has invalid type"); |
| 934 | } |
| 935 | |
| 936 | auto content = GroupContent{ group_id, |
| 937 | epoch, |
| 938 | { ExternalSenderIndex{ signer_index } }, |
| 939 | { /* no authenticated data */ }, |
| 940 | { proposal } }; |
| 941 | auto content_auth = AuthenticatedContent::sign( |
| 942 | WireFormat::mls_public_message, std::move(content), suite, sig_priv, {}); |
| 943 | |
| 944 | return PublicMessage::protect(std::move(content_auth), suite, {}, {}); |
| 945 | } |
| 946 | |
| 947 | } // namespace mlspp |