sync/devicegroup.fsm
author Edouard Tisserant <edouard@pep-project.org>
Mon, 03 Oct 2016 19:08:15 +0200
branchkeysync
changeset 1236 991afc1aa2a1
parent 1216 76e7a6748913
child 1281 efff88dcdc59
permissions -rw-r--r--
sync : attach_sync_session
     1 // DeviceGroup protocol for p≡p
     2 
     3 // Copyleft (c) 2016, p≡p foundation
     4 
     5 // Written by Volker Birk
     6 
     7 include ./fsm.yml2
     8 
     9 protocol DeviceGroup {
    10     // all messages have a timestamp, time out and are removed after timeout
    11 
    12     broadcast sendBeacon;
    13     unencrypted sendBeacon;
    14 
    15     fsm DeviceState filename=sync {
    16         condition storedGroupKeys();
    17         condition keyElectionWon(Identity partner);
    18 
    19         state InitState {
    20             on Init {
    21                 if storedGroupKeys()
    22                     go Grouped;
    23                 go Sole;
    24             }
    25         }
    26 
    27         state Sole {
    28             on KeyGen // injected by generate_keypair()
    29                 do sendBeacon;
    30             on CannotDecrypt
    31                 do sendBeacon;  // cry, baby
    32             on Beacon(Identity partner) // this event will not happen for already
    33                                         // rejected partners
    34                 do sendHandshakeRequest(partner);
    35             on HandshakeRequest(Identity partner) {
    36                 do sendHandshakeRequest(partner);
    37                 go HandshakingSole(partner);
    38             }
    39         }
    40 
    41         state HandshakingSole(Identity partner) {
    42             on Init
    43                 do showHandshake(partner);
    44             on HandshakeRejected(Identity partner) {
    45                 do rejectHandshake(partner);             // stores rejection of partner
    46                 go Sole;
    47             }
    48             on HandshakeAccepted(Identity partner) {
    49                 do acceptHandshake(partner); 
    50                 if keyElectionWon(partner) {    // an already existing group
    51                                                 // always wins
    52                     do sendGroupKeys(partner);
    53                     go Grouped;
    54                 }
    55                 go WaitForGroupKeysSole(Identity partner);
    56             }
    57         }
    58     
    59         state WaitForGroupKeysSole(Identity partner) {
    60             on GroupKeys(Identity partner, Stringlist keys) {
    61                 do storeGroupKeys(partner, keys);
    62                 // TODO : add a callback to signal finished waiting for group keys
    63                 go Grouped;
    64             }
    65             on Cancel go Sole;
    66             on Reject(Identity partner) {
    67                 do rejectHandshake(partner);
    68                 go Sole;
    69             }
    70         }
    71 
    72         state Grouped {
    73             on KeyGen
    74                 do sendGroupKeys; // always send all keys
    75             on Beacon(Identity partner)
    76                 do sendHandshakeRequest(partner);
    77             on HandshakeRequest(Identity partner) {
    78                 do sendHandshakeRequest(partner);
    79                 go HandshakingGrouped(partner);
    80             }
    81             on GroupKeys(Identity partner, Stringlist keys) {
    82                 do storeGroupKeys(partner, keys);
    83                 // TODO : add a callback to signal finished waiting for group keys
    84                 go Grouped;
    85             }
    86         }
    87 
    88         state HandshakingGrouped(Identity partner) {
    89             on Init
    90                 do showHandshake(partner);
    91             on HandshakeRejected(Identity partner) {
    92                 do rejectHandshake(partner);             // stores rejection of partner
    93                 go Grouped;
    94             }
    95             on HandshakeAccepted(Identity partner) {
    96                 do acceptHandshake(partner); 
    97 
    98                 // an already existing group always wins
    99                 do sendGroupKeys(partner);
   100                 go Grouped;
   101             }
   102         }
   103     
   104 
   105         tag Init 1;
   106         tag Beacon 2;
   107         tag HandshakeRequest 3;
   108         tag GroupKeys 4;
   109     }
   110 }
   111