sync/devicegroup.fsm
author Krista Grothoff <krista@pep-project.org>
Mon, 13 Feb 2017 22:13:48 +0100
branchenigmail-color-tests
changeset 1582 7dd855780eef
parent 1574 12b7a45140d3
child 1586 599deda50386
permissions -rw-r--r--
closing branch
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 // DeviceGroup protocol for p≡p
     5 
     6 // Copyleft (c) 2016, p≡p foundation
     7 
     8 // Written by Volker Birk
     9 
    10 include ./fsm.yml2
    11 
    12 protocol DeviceGroup {
    13     // all messages have a timestamp, time out and are removed after timeout
    14 
    15     broadcast sendBeacon;
    16     broadcast sendGroupUpdate;
    17     broadcast sendUpdateRequest;
    18     unencrypted sendBeacon;
    19 
    20     fsm DeviceState filename=sync {
    21         condition deviceGrouped();
    22         condition keyElectionWon(Identity partner);
    23         condition sameIdentities(Identity a, Identity b);
    24 
    25         state InitState {
    26             on Init {
    27                 if deviceGrouped()
    28                     go Grouped;
    29                 go Sole;
    30             }
    31         }
    32 
    33         state Sole end=1 {
    34             on KeyGen
    35                 do sendBeacon;
    36             on CannotDecrypt
    37                 do sendBeacon;
    38             on Beacon(Identity partner){
    39                 do sendHandshakeRequest(partner);
    40                 go SoleBeaconed(partner);
    41             }
    42             on HandshakeRequest(Identity partner) {
    43                 do sendHandshakeRequest(partner);
    44                 go HandshakingSole(partner);
    45             }
    46         }
    47 
    48         state SoleBeaconed timeout=600 (Identity expected) {
    49             on KeyGen{
    50                 do sendBeacon;
    51                 go Sole;
    52             }
    53             on CannotDecrypt{
    54                 do sendBeacon;
    55                 go Sole;
    56             }
    57             on Beacon(Identity partner) {
    58                 do sendHandshakeRequest(partner);
    59                 go SoleBeaconed(partner);
    60             }
    61             on HandshakeRequest(Identity partner) {
    62                 if sameIdentities(partner, expected) {
    63                     // do nothing, to avoid sending handshake request twice 
    64                 } else {
    65                     do sendHandshakeRequest(partner);
    66                 }
    67                 go HandshakingSole(partner);
    68             }
    69             on Timeout go Sole;
    70         }
    71 
    72         state HandshakingSole timeout=600 (Identity expected) {
    73             on Init{
    74                 if keyElectionWon(partner) {
    75                     do notifyInitFormGroup(partner);
    76                 } else {
    77                     do notifyInitAddOurDevice(partner);
    78                 }
    79             }
    80             on HandshakeRejected(Identity partner) {
    81                 do rejectHandshake(partner);
    82                 go Sole;
    83             }
    84             on HandshakeAccepted(Identity partner) {
    85                 do acceptHandshake(partner); 
    86                 if keyElectionWon(partner) {
    87                     do enterGroup;
    88                     do sendGroupKeys(partner);
    89                     do notifyAcceptedGroupCreated(partner);
    90                     go Grouped;
    91                 }
    92                 go WaitForGroupKeysSole(partner);
    93             }
    94             on Cancel go Sole;
    95             on Timeout {
    96                 do notifyTimeout(expected);
    97                 do sendBeacon;
    98                 go Sole;
    99             }
   100         }
   101     
   102         state WaitForGroupKeysSole timeout=600 (Identity expected) {
   103             on GroupKeys(Identity partner, Stringlist keys) {
   104                 if sameIdentities(partner, expected) {
   105                     do storeGroupKeys(partner, keys);
   106                     do enterGroup;
   107                     do sendGroupUpdate;
   108                     do notifyAcceptedDeviceAdded(partner);
   109                     go Grouped;
   110                 }
   111             }
   112             on Timeout {
   113                 do notifyTimeout(expected);
   114                 go Sole;
   115             }
   116         }
   117 
   118         state Grouped end=1 {
   119             on KeyGen
   120                 do sendGroupUpdate;
   121             on CannotDecrypt
   122                 do sendUpdateRequest;
   123             on UpdateRequest
   124                 do sendGroupUpdate;
   125             on Beacon(Identity partner){
   126                 do sendHandshakeRequest(partner);
   127                 go GroupedBeaconed(partner);
   128             }
   129             on HandshakeRequest(Identity partner) {
   130                 do sendHandshakeRequest(partner);
   131                 go HandshakingGrouped(partner);
   132             }
   133             on GroupUpdate(Identity partner, Stringlist keys)
   134                 do storeGroupKeys(partner, keys);
   135         }
   136 
   137         state GroupedBeaconed timeout=600 (Identity expected){
   138             on KeyGen
   139                 do sendGroupUpdate;
   140             on CannotDecrypt
   141                 do sendUpdateRequest;
   142             on UpdateRequest
   143                 do sendGroupUpdate;
   144             on Beacon(Identity partner){
   145                 do sendHandshakeRequest(partner);
   146                 go GroupedBeaconed(partner);
   147             }
   148             on HandshakeRequest(Identity partner) {
   149                 if sameIdentities(partner, expected) {
   150                     // do nothing, to avoid sending handshake request twice 
   151                 } else {
   152                     do sendHandshakeRequest(partner);
   153                 }
   154                 go HandshakingGrouped(partner);
   155             }
   156             on GroupUpdate(Identity partner, Stringlist keys)
   157                 do storeGroupKeys(partner, keys);
   158             on Timeout go Grouped;
   159         }
   160 
   161         state HandshakingGrouped timeout=600 (Identity expected) {
   162             on Init
   163                 do notifyInitAddOurDevice(partner);
   164             on HandshakeRejected(Identity partner) {
   165                 do rejectHandshake(partner);             // stores rejection of partner
   166                 go Grouped;
   167             }
   168             on HandshakeAccepted(Identity partner) {
   169                 do acceptHandshake(partner); 
   170 
   171                 // an already existing group always wins
   172                 do sendGroupKeys(partner);
   173 
   174                 go Grouped;
   175             }
   176             on Cancel go Grouped;
   177             on Timeout {
   178                 do notifyTimeout(expected);
   179                 go Grouped;
   180             }
   181         }
   182 
   183         tag Init 1;
   184         tag Beacon 2;
   185         tag HandshakeRequest 3;
   186         tag GroupKeys 4;
   187     }
   188 }
   189