@@ -70,6 +70,17 @@ const MACS: &[MessageAuthenticationCode] = &[MessageAuthenticationCode::HkdfHmac
70
70
const STRINGS : & [ ShortAuthenticationString ] =
71
71
& [ ShortAuthenticationString :: Decimal , ShortAuthenticationString :: Emoji ] ;
72
72
73
+ fn the_protocol_definitions ( ) -> SasV1Content {
74
+ SasV1ContentInit {
75
+ short_authentication_string : STRINGS . to_vec ( ) ,
76
+ key_agreement_protocols : KEY_AGREEMENT_PROTOCOLS . to_vec ( ) ,
77
+ message_authentication_codes : MACS . to_vec ( ) ,
78
+ hashes : HASHES . to_vec ( ) ,
79
+ }
80
+ . try_into ( )
81
+ . expect ( "Invalid protocol definition." )
82
+ }
83
+
73
84
// The max time a SAS flow can take from start to done.
74
85
const MAX_AGE : Duration = Duration :: from_secs ( 60 * 5 ) ;
75
86
@@ -420,16 +431,7 @@ impl SasState<Created> {
420
431
last_event_time : Arc :: new ( Instant :: now ( ) ) ,
421
432
started_from_request,
422
433
423
- state : Arc :: new ( Created {
424
- protocol_definitions : SasV1ContentInit {
425
- short_authentication_string : STRINGS . to_vec ( ) ,
426
- key_agreement_protocols : KEY_AGREEMENT_PROTOCOLS . to_vec ( ) ,
427
- message_authentication_codes : MACS . to_vec ( ) ,
428
- hashes : HASHES . to_vec ( ) ,
429
- }
430
- . try_into ( )
431
- . expect ( "Invalid protocol definition." ) ,
432
- } ) ,
434
+ state : Arc :: new ( Created { protocol_definitions : the_protocol_definitions ( ) } ) ,
433
435
}
434
436
}
435
437
@@ -571,7 +573,7 @@ impl SasState<Started> {
571
573
}
572
574
}
573
575
574
- pub fn into_accepted ( self , methods : Vec < ShortAuthenticationString > ) -> SasState < WeAccepted > {
576
+ pub fn into_we_accepted ( self , methods : Vec < ShortAuthenticationString > ) -> SasState < WeAccepted > {
575
577
let mut accepted_protocols = self . state . accepted_protocols . as_ref ( ) . to_owned ( ) ;
576
578
accepted_protocols. short_auth_string = methods;
577
579
@@ -594,6 +596,69 @@ impl SasState<Started> {
594
596
} ) ,
595
597
}
596
598
}
599
+
600
+ fn as_content ( & self ) -> OwnedStartContent {
601
+ match self . verification_flow_id . as_ref ( ) {
602
+ FlowId :: ToDevice ( s) => {
603
+ OwnedStartContent :: ToDevice ( ToDeviceKeyVerificationStartEventContent :: new (
604
+ self . device_id ( ) . into ( ) ,
605
+ s. to_string ( ) ,
606
+ StartMethod :: SasV1 ( the_protocol_definitions ( ) ) ,
607
+ ) )
608
+ }
609
+ FlowId :: InRoom ( r, e) => OwnedStartContent :: Room (
610
+ r. clone ( ) ,
611
+ KeyVerificationStartEventContent :: new (
612
+ self . device_id ( ) . into ( ) ,
613
+ StartMethod :: SasV1 ( the_protocol_definitions ( ) ) ,
614
+ Relation :: new ( e. clone ( ) ) ,
615
+ ) ,
616
+ ) ,
617
+ }
618
+ }
619
+
620
+ /// Receive a m.key.verification.accept event, changing the state into
621
+ /// an Accepted one.
622
+ ///
623
+ /// Note: Even though the other side has started the (or rather "a") sas
624
+ /// verification, it can still accept one, if we have sent one
625
+ /// simultaneously. In this case we just go on with the verification
626
+ /// that *we* started.
627
+ ///
628
+ /// # Arguments
629
+ ///
630
+ /// * `event` - The m.key.verification.accept event that was sent to us by
631
+ /// the other side.
632
+ pub fn into_accepted (
633
+ self ,
634
+ sender : & UserId ,
635
+ content : & AcceptContent ,
636
+ ) -> Result < SasState < Accepted > , SasState < Cancelled > > {
637
+ self . check_event ( sender, content. flow_id ( ) ) . map_err ( |c| self . clone ( ) . cancel ( true , c) ) ?;
638
+
639
+ if let AcceptMethod :: SasV1 ( content) = content. method ( ) {
640
+ let accepted_protocols = AcceptedProtocols :: try_from ( content. clone ( ) )
641
+ . map_err ( |c| self . clone ( ) . cancel ( true , c) ) ?;
642
+
643
+ let start_content = self . as_content ( ) . into ( ) ;
644
+
645
+ Ok ( SasState {
646
+ inner : self . inner ,
647
+ ids : self . ids ,
648
+ verification_flow_id : self . verification_flow_id ,
649
+ creation_time : self . creation_time ,
650
+ last_event_time : Instant :: now ( ) . into ( ) ,
651
+ started_from_request : self . started_from_request ,
652
+ state : Arc :: new ( Accepted {
653
+ start_content,
654
+ commitment : content. commitment . clone ( ) ,
655
+ accepted_protocols : accepted_protocols. into ( ) ,
656
+ } ) ,
657
+ } )
658
+ } else {
659
+ Err ( self . cancel ( true , CancelCode :: UnknownMethod ) )
660
+ }
661
+ }
597
662
}
598
663
599
664
impl SasState < WeAccepted > {
@@ -1191,7 +1256,7 @@ mod test {
1191
1256
& start_content. as_start_content ( ) ,
1192
1257
false ,
1193
1258
) ;
1194
- let bob_sas = bob_sas. unwrap ( ) . into_accepted ( vec ! [ ShortAuthenticationString :: Emoji ] ) ;
1259
+ let bob_sas = bob_sas. unwrap ( ) . into_we_accepted ( vec ! [ ShortAuthenticationString :: Emoji ] ) ;
1195
1260
1196
1261
( alice_sas, bob_sas)
1197
1262
}
0 commit comments