Debian Codegraph @

mlspp/include/mls/messages.h

raw symbols SCIP
1 #pragma once
2
3 #include "mls/common.h"
4 #include "mls/core_types.h"
5 #include "mls/credential.h"
6 #include "mls/crypto.h"
7 #include "mls/treekem.h"
8 #include <optional>
9 #include <tls/tls_syntax.h>
10
11 namespace mlspp {
12
13 struct ExternalPubExtension
14 {
15 HPKEPublicKey external_pub;
16
17 static const uint16_t type;
18 TLS_SERIALIZABLE(external_pub)
19 };
20
21 struct RatchetTreeExtension
22 {
23 TreeKEMPublicKey tree;
24
25 static const uint16_t type;
26 TLS_SERIALIZABLE(tree)
27 };
28
29 struct ExternalSender
30 {
31 SignaturePublicKey signature_key;
32 Credential credential;
33
34 TLS_SERIALIZABLE(signature_key, credential);
35 };
36
37 struct ExternalSendersExtension
38 {
39 std::vector<ExternalSender> senders;
40
41 static const uint16_t type;
42 TLS_SERIALIZABLE(senders);
43 };
44
45 struct SFrameParameters
46 {
47 uint16_t cipher_suite;
48 uint8_t epoch_bits;
49
50 static const uint16_t type;
51 TLS_SERIALIZABLE(cipher_suite, epoch_bits)
52 };
53
54 struct SFrameCapabilities
55 {
56 std::vector<uint16_t> cipher_suites;
57
58 bool compatible(const SFrameParameters& params) const;
59
60 static const uint16_t type;
61 TLS_SERIALIZABLE(cipher_suites)
62 };
63
64 ///
65 /// PSKs
66 ///
67 enum struct PSKType : uint8_t
68 {
69 reserved = 0,
70 external = 1,
71 resumption = 2,
72 };
73
74 struct ExternalPSK
75 {
76 bytes psk_id;
77 TLS_SERIALIZABLE(psk_id)
78 };
79
80 enum struct ResumptionPSKUsage : uint8_t
81 {
82 reserved = 0,
83 application = 1,
84 reinit = 2,
85 branch = 3,
86 };
87
88 struct ResumptionPSK
89 {
90 ResumptionPSKUsage usage;
91 bytes psk_group_id;
92 epoch_t psk_epoch;
93 TLS_SERIALIZABLE(usage, psk_group_id, psk_epoch)
94 };
95
96 struct PreSharedKeyID
97 {
98 var::variant<ExternalPSK, ResumptionPSK> content;
99 bytes psk_nonce;
100 TLS_SERIALIZABLE(content, psk_nonce)
101 TLS_TRAITS(tls::variant<PSKType>, tls::pass)
102 };
103
104 struct PreSharedKeys
105 {
106 std::vector<PreSharedKeyID> psks;
107 TLS_SERIALIZABLE(psks)
108 };
109
110 struct PSKWithSecret
111 {
112 PreSharedKeyID id;
113 bytes secret;
114 };
115
116 // struct {
117 // ProtocolVersion version = mls10;
118 // CipherSuite cipher_suite;
119 // opaque group_id<V>;
120 // uint64 epoch;
121 // opaque tree_hash<V>;
122 // opaque confirmed_transcript_hash<V>;
123 // Extension extensions<V>;
124 // } GroupContext;
125 struct GroupContext
126 {
127 ProtocolVersion version{ ProtocolVersion::mls10 };
128 CipherSuite cipher_suite;
129 bytes group_id;
130 epoch_t epoch;
131 bytes tree_hash;
132 bytes confirmed_transcript_hash;
133 ExtensionList extensions;
134
135 GroupContext() = default;
136 GroupContext(CipherSuite cipher_suite_in,
137 bytes group_id_in,
138 epoch_t epoch_in,
139 bytes tree_hash_in,
140 bytes confirmed_transcript_hash_in,
141 ExtensionList extensions_in);
142
143 TLS_SERIALIZABLE(version,
144 cipher_suite,
145 group_id,
146 epoch,
147 tree_hash,
148 confirmed_transcript_hash,
149 extensions)
150 };
151
152 // struct {
153 // GroupContext group_context;
154 // Extension extensions<V>;
155 // MAC confirmation_tag;
156 // uint32 signer;
157 // // SignWithLabel(., "GroupInfoTBS", GroupInfoTBS)
158 // opaque signature<V>;
159 // } GroupInfo;
160 struct GroupInfo
161 {
162 GroupContext group_context;
163 ExtensionList extensions;
164 bytes confirmation_tag;
165 LeafIndex signer;
166 bytes signature;
167
168 GroupInfo() = default;
169 GroupInfo(GroupContext group_context_in,
170 ExtensionList extensions_in,
171 bytes confirmation_tag_in);
172
173 bytes to_be_signed() const;
174 void sign(const TreeKEMPublicKey& tree,
175 LeafIndex signer_index,
176 const SignaturePrivateKey& priv);
177 bool verify(const TreeKEMPublicKey& tree) const;
178
179 // These methods exist only to simplify unit testing
180 void sign(LeafIndex signer_index, const SignaturePrivateKey& priv);
181 bool verify(const SignaturePublicKey& pub) const;
182
183 TLS_SERIALIZABLE(group_context,
184 extensions,
185 confirmation_tag,
186 signer,
187 signature)
188 };
189
190 // struct {
191 // opaque joiner_secret<1..255>;
192 // optional<PathSecret> path_secret;
193 // PreSharedKeys psks;
194 // } GroupSecrets;
195 struct GroupSecrets
196 {
197 struct PathSecret
198 {
199 bytes secret;
200
201 TLS_SERIALIZABLE(secret)
202 };
203
204 bytes joiner_secret;
205 std::optional<PathSecret> path_secret;
206 PreSharedKeys psks;
207
208 TLS_SERIALIZABLE(joiner_secret, path_secret, psks)
209 };
210
211 // struct {
212 // opaque key_package_hash<1..255>;
213 // HPKECiphertext encrypted_group_secrets;
214 // } EncryptedGroupSecrets;
215 struct EncryptedGroupSecrets
216 {
217 KeyPackageRef new_member;
218 HPKECiphertext encrypted_group_secrets;
219
220 TLS_SERIALIZABLE(new_member, encrypted_group_secrets)
221 };
222
223 // struct {
224 // ProtocolVersion version = mls10;
225 // CipherSuite cipher_suite;
226 // EncryptedGroupSecrets group_secretss<1..2^32-1>;
227 // opaque encrypted_group_info<1..2^32-1>;
228 // } Welcome;
229 struct Welcome
230 {
231 CipherSuite cipher_suite;
232 std::vector<EncryptedGroupSecrets> secrets;
233 bytes encrypted_group_info;
234
235 Welcome();
236 Welcome(CipherSuite suite,
237 const bytes& joiner_secret,
238 const std::vector<PSKWithSecret>& psks,
239 const GroupInfo& group_info);
240
241 void encrypt(const KeyPackage& kp, const std::optional<bytes>& path_secret);
242 std::optional<int> find(const KeyPackage& kp) const;
243 GroupSecrets decrypt_secrets(int kp_index,
244 const HPKEPrivateKey& init_priv) const;
245 GroupInfo decrypt(const bytes& joiner_secret,
246 const std::vector<PSKWithSecret>& psks) const;
247
248 TLS_SERIALIZABLE(cipher_suite, secrets, encrypted_group_info)
249
250 private:
251 bytes _joiner_secret;
252 PreSharedKeys _psks;
253 static KeyAndNonce group_info_key_nonce(
254 CipherSuite suite,
255 const bytes& joiner_secret,
256 const std::vector<PSKWithSecret>& psks);
257 };
258
259 ///
260 /// Proposals & Commit
261 ///
262
263 // Add
264 struct Add
265 {
266 KeyPackage key_package;
267 TLS_SERIALIZABLE(key_package)
268 };
269
270 // Update
271 struct Update
272 {
273 LeafNode leaf_node;
274 TLS_SERIALIZABLE(leaf_node)
275 };
276
277 // Remove
278 struct Remove
279 {
280 LeafIndex removed;
281 TLS_SERIALIZABLE(removed)
282 };
283
284 // PreSharedKey
285 struct PreSharedKey
286 {
287 PreSharedKeyID psk;
288 TLS_SERIALIZABLE(psk)
289 };
290
291 // ReInit
292 struct ReInit
293 {
294 bytes group_id;
295 ProtocolVersion version;
296 CipherSuite cipher_suite;
297 ExtensionList extensions;
298
299 TLS_SERIALIZABLE(group_id, version, cipher_suite, extensions)
300 };
301
302 // ExternalInit
303 struct ExternalInit
304 {
305 bytes kem_output;
306 TLS_SERIALIZABLE(kem_output)
307 };
308
309 // GroupContextExtensions
310 struct GroupContextExtensions
311 {
312 ExtensionList group_context_extensions;
313 TLS_SERIALIZABLE(group_context_extensions)
314 };
315
316 struct ProposalType;
317
318 struct Proposal
319 {
320 using Type = uint16_t;
321
322 var::variant<Add,
323 Update,
324 Remove,
325 PreSharedKey,
326 ReInit,
327 ExternalInit,
328 GroupContextExtensions>
329 content;
330
331 Type proposal_type() const;
332
333 TLS_SERIALIZABLE(content)
334 TLS_TRAITS(tls::variant<ProposalType>)
335 };
336
337 struct ProposalType
338 {
339 static constexpr Proposal::Type invalid = 0;
340 static constexpr Proposal::Type add = 1;
341 static constexpr Proposal::Type update = 2;
342 static constexpr Proposal::Type remove = 3;
343 static constexpr Proposal::Type psk = 4;
344 static constexpr Proposal::Type reinit = 5;
345 static constexpr Proposal::Type external_init = 6;
346 static constexpr Proposal::Type group_context_extensions = 7;
347
348 constexpr ProposalType()
349 : val(invalid)
350 {
351 }
352
353 constexpr ProposalType(Proposal::Type pt)
354 : val(pt)
355 {
356 }
357
358 Proposal::Type val;
359 TLS_SERIALIZABLE(val)
360 };
361
362 enum struct ProposalOrRefType : uint8_t
363 {
364 reserved = 0,
365 value = 1,
366 reference = 2,
367 };
368
369 struct ProposalOrRef
370 {
371 var::variant<Proposal, ProposalRef> content;
372
373 TLS_SERIALIZABLE(content)
374 TLS_TRAITS(tls::variant<ProposalOrRefType>)
375 };
376
377 // struct {
378 // ProposalOrRef proposals<0..2^32-1>;
379 // optional<UpdatePath> path;
380 // } Commit;
381 struct Commit
382 {
383 std::vector<ProposalOrRef> proposals;
384 std::optional<UpdatePath> path;
385
386 // Validate that the commit is acceptable as an external commit, and if so,
387 // produce the public key from the ExternalInit proposal
388 std::optional<bytes> valid_external() const;
389
390 TLS_SERIALIZABLE(proposals, path)
391 };
392
393 // struct {
394 // opaque group_id<0..255>;
395 // uint32 epoch;
396 // uint32 sender;
397 // ContentType content_type;
398 //
399 // select (PublicMessage.content_type) {
400 // case handshake:
401 // GroupOperation operation;
402 // opaque confirmation<0..255>;
403 //
404 // case application:
405 // opaque application_data<0..2^32-1>;
406 // }
407 //
408 // opaque signature<0..2^16-1>;
409 // } PublicMessage;
410 struct ApplicationData
411 {
412 bytes data;
413 TLS_SERIALIZABLE(data)
414 };
415
416 struct GroupContext;
417
418 enum struct WireFormat : uint16_t
419 {
420 reserved = 0,
421 mls_public_message = 1,
422 mls_private_message = 2,
423 mls_welcome = 3,
424 mls_group_info = 4,
425 mls_key_package = 5,
426 };
427
428 enum struct ContentType : uint8_t
429 {
430 invalid = 0,
431 application = 1,
432 proposal = 2,
433 commit = 3,
434 };
435
436 enum struct SenderType : uint8_t
437 {
438 invalid = 0,
439 member = 1,
440 external = 2,
441 new_member_proposal = 3,
442 new_member_commit = 4,
443 };
444
445 struct MemberSender
446 {
447 LeafIndex sender;
448 TLS_SERIALIZABLE(sender);
449 };
450
451 struct ExternalSenderIndex
452 {
453 uint32_t sender_index;
454 TLS_SERIALIZABLE(sender_index)
455 };
456
457 struct NewMemberProposalSender
458 {
459 TLS_SERIALIZABLE()
460 };
461
462 struct NewMemberCommitSender
463 {
464 TLS_SERIALIZABLE()
465 };
466
467 struct Sender
468 {
469 var::variant<MemberSender,
470 ExternalSenderIndex,
471 NewMemberProposalSender,
472 NewMemberCommitSender>
473 sender;
474
475 SenderType sender_type() const;
476
477 TLS_SERIALIZABLE(sender)
478 TLS_TRAITS(tls::variant<SenderType>)
479 };
480
481 ///
482 /// MLSMessage and friends
483 ///
484 struct GroupKeySource;
485
486 struct GroupContent
487 {
488 using RawContent = var::variant<ApplicationData, Proposal, Commit>;
489
490 bytes group_id;
491 epoch_t epoch;
492 Sender sender;
493 bytes authenticated_data;
494 RawContent content;
495
496 GroupContent() = default;
497 GroupContent(bytes group_id_in,
498 epoch_t epoch_in,
499 Sender sender_in,
500 bytes authenticated_data_in,
501 RawContent content_in);
502 GroupContent(bytes group_id_in,
503 epoch_t epoch_in,
504 Sender sender_in,
505 bytes authenticated_data_in,
506 ContentType content_type);
507
508 ContentType content_type() const;
509
510 TLS_SERIALIZABLE(group_id, epoch, sender, authenticated_data, content)
511 TLS_TRAITS(tls::pass,
512 tls::pass,
513 tls::pass,
514 tls::pass,
515 tls::variant<ContentType>)
516 };
517
518 struct GroupContentAuthData
519 {
520 ContentType content_type = ContentType::invalid;
521 bytes signature;
522 std::optional<bytes> confirmation_tag;
523
524 friend tls::ostream& operator<<(tls::ostream& str,
525 const GroupContentAuthData& obj);
526 friend tls::istream& operator>>(tls::istream& str, GroupContentAuthData& obj);
527 friend bool operator==(const GroupContentAuthData& lhs,
528 const GroupContentAuthData& rhs);
529 };
530
531 struct AuthenticatedContent
532 {
533 WireFormat wire_format;
534 GroupContent content;
535 GroupContentAuthData auth;
536
537 AuthenticatedContent() = default;
538
539 static AuthenticatedContent sign(WireFormat wire_format,
540 GroupContent content,
541 CipherSuite suite,
542 const SignaturePrivateKey& sig_priv,
543 const std::optional<GroupContext>& context);
544 bool verify(CipherSuite suite,
545 const SignaturePublicKey& sig_pub,
546 const std::optional<GroupContext>& context) const;
547
548 bytes confirmed_transcript_hash_input() const;
549 bytes interim_transcript_hash_input() const;
550
551 void set_confirmation_tag(const bytes& confirmation_tag);
552 bool check_confirmation_tag(const bytes& confirmation_tag) const;
553
554 friend tls::ostream& operator<<(tls::ostream& str,
555 const AuthenticatedContent& obj);
556 friend tls::istream& operator>>(tls::istream& str, AuthenticatedContent& obj);
557 friend bool operator==(const AuthenticatedContent& lhs,
558 const AuthenticatedContent& rhs);
559
560 private:
561 AuthenticatedContent(WireFormat wire_format_in, GroupContent content_in);
562 AuthenticatedContent(WireFormat wire_format_in,
563 GroupContent content_in,
564 GroupContentAuthData auth_in);
565
566 bytes to_be_signed(const std::optional<GroupContext>& context) const;
567
568 friend struct PublicMessage;
569 friend struct PrivateMessage;
570 };
571
572 struct ValidatedContent
573 {
574 const AuthenticatedContent& authenticated_content() const;
575
576 friend bool operator==(const ValidatedContent& lhs,
577 const ValidatedContent& rhs);
578
579 private:
580 AuthenticatedContent content_auth;
581
582 ValidatedContent(AuthenticatedContent content_auth_in);
583
584 friend struct PublicMessage;
585 friend struct PrivateMessage;
586 friend class State;
587 };
588
589 struct PublicMessage
590 {
591 PublicMessage() = default;
592
593 bytes get_group_id() const { return content.group_id; }
594 epoch_t get_epoch() const { return content.epoch; }
595
596 static PublicMessage protect(AuthenticatedContent content_auth,
597 CipherSuite suite,
598 const std::optional<bytes>& membership_key,
599 const std::optional<GroupContext>& context);
600 std::optional<ValidatedContent> unprotect(
601 CipherSuite suite,
602 const std::optional<bytes>& membership_key,
603 const std::optional<GroupContext>& context) const;
604
605 bool contains(const AuthenticatedContent& content_auth) const;
606
607 // TODO(RLB) Make this private and expose only to tests
608 AuthenticatedContent authenticated_content() const;
609
610 friend tls::ostream& operator<<(tls::ostream& str, const PublicMessage& obj);
611 friend tls::istream& operator>>(tls::istream& str, PublicMessage& obj);
612 friend bool operator==(const PublicMessage& lhs, const PublicMessage& rhs);
613 friend bool operator!=(const PublicMessage& lhs, const PublicMessage& rhs);
614
615 private:
616 GroupContent content;
617 GroupContentAuthData auth;
618 std::optional<bytes> membership_tag;
619
620 PublicMessage(AuthenticatedContent content_auth);
621
622 bytes membership_mac(CipherSuite suite,
623 const bytes& membership_key,
624 const std::optional<GroupContext>& context) const;
625 };
626
627 struct PrivateMessage
628 {
629 PrivateMessage() = default;
630
631 bytes get_group_id() const { return group_id; }
632 epoch_t get_epoch() const { return epoch; }
633
634 static PrivateMessage protect(AuthenticatedContent content_auth,
635 CipherSuite suite,
636 GroupKeySource& keys,
637 const bytes& sender_data_secret,
638 size_t padding_size);
639 std::optional<ValidatedContent> unprotect(
640 CipherSuite suite,
641 GroupKeySource& keys,
642 const bytes& sender_data_secret) const;
643
644 TLS_SERIALIZABLE(group_id,
645 epoch,
646 content_type,
647 authenticated_data,
648 encrypted_sender_data,
649 ciphertext)
650
651 private:
652 bytes group_id;
653 epoch_t epoch;
654 ContentType content_type;
655 bytes authenticated_data;
656 bytes encrypted_sender_data;
657 bytes ciphertext;
658
659 PrivateMessage(GroupContent content,
660 bytes encrypted_sender_data_in,
661 bytes ciphertext_in);
662 };
663
664 struct MLSMessage
665 {
666 ProtocolVersion version = ProtocolVersion::mls10;
667 var::variant<PublicMessage, PrivateMessage, Welcome, GroupInfo, KeyPackage>
668 message;
669
670 bytes group_id() const;
671 epoch_t epoch() const;
672 WireFormat wire_format() const;
673
674 MLSMessage() = default;
675 MLSMessage(PublicMessage public_message);
676 MLSMessage(PrivateMessage private_message);
677 MLSMessage(Welcome welcome);
678 MLSMessage(GroupInfo group_info);
679 MLSMessage(KeyPackage key_package);
680
681 TLS_SERIALIZABLE(version, message)
682 TLS_TRAITS(tls::pass, tls::variant<WireFormat>)
683 };
684
685 MLSMessage
686 external_proposal(CipherSuite suite,
687 const bytes& group_id,
688 epoch_t epoch,
689 const Proposal& proposal,
690 uint32_t signer_index,
691 const SignaturePrivateKey& sig_priv);
692
693 } // namespace mlspp
694
695 namespace mlspp::tls {
696
697 TLS_VARIANT_MAP(mlspp::PSKType, mlspp::ExternalPSK, external)
698 TLS_VARIANT_MAP(mlspp::PSKType,
699 mlspp::ResumptionPSK,
700 resumption)
701
702 TLS_VARIANT_MAP(mlspp::ProposalOrRefType,
703 mlspp::Proposal,
704 value)
705 TLS_VARIANT_MAP(mlspp::ProposalOrRefType,
706 mlspp::ProposalRef,
707 reference)
708
709 TLS_VARIANT_MAP(mlspp::ProposalType, mlspp::Add, add)
710 TLS_VARIANT_MAP(mlspp::ProposalType, mlspp::Update, update)
711 TLS_VARIANT_MAP(mlspp::ProposalType, mlspp::Remove, remove)
712 TLS_VARIANT_MAP(mlspp::ProposalType, mlspp::PreSharedKey, psk)
713 TLS_VARIANT_MAP(mlspp::ProposalType, mlspp::ReInit, reinit)
714 TLS_VARIANT_MAP(mlspp::ProposalType,
715 mlspp::ExternalInit,
716 external_init)
717 TLS_VARIANT_MAP(mlspp::ProposalType,
718 mlspp::GroupContextExtensions,
719 group_context_extensions)
720
721 TLS_VARIANT_MAP(mlspp::ContentType,
722 mlspp::ApplicationData,
723 application)
724 TLS_VARIANT_MAP(mlspp::ContentType, mlspp::Proposal, proposal)
725 TLS_VARIANT_MAP(mlspp::ContentType, mlspp::Commit, commit)
726
727 TLS_VARIANT_MAP(mlspp::SenderType, mlspp::MemberSender, member)
728 TLS_VARIANT_MAP(mlspp::SenderType,
729 mlspp::ExternalSenderIndex,
730 external)
731 TLS_VARIANT_MAP(mlspp::SenderType,
732 mlspp::NewMemberProposalSender,
733 new_member_proposal)
734 TLS_VARIANT_MAP(mlspp::SenderType,
735 mlspp::NewMemberCommitSender,
736 new_member_commit)
737
738 TLS_VARIANT_MAP(mlspp::WireFormat,
739 mlspp::PublicMessage,
740 mls_public_message)
741 TLS_VARIANT_MAP(mlspp::WireFormat,
742 mlspp::PrivateMessage,
743 mls_private_message)
744 TLS_VARIANT_MAP(mlspp::WireFormat, mlspp::Welcome, mls_welcome)
745 TLS_VARIANT_MAP(mlspp::WireFormat,
746 mlspp::GroupInfo,
747 mls_group_info)
748 TLS_VARIANT_MAP(mlspp::WireFormat,
749 mlspp::KeyPackage,
750 mls_key_package)
751
752 } // namespace mlspp::tls