sync/sync.fsm
author Volker Birk <vb@pep-project.org>
Fri, 22 Mar 2019 16:05:04 +0100
branchsync
changeset 3379 2f957d83c0f7
parent 3377 922fd505e0c0
child 3381 d9347f38d69c
permissions -rw-r--r--
more careful
     1 // This file is under BSD License 2.0
     2 
     3 // Sync protocol for p≡p
     4 // Copyright (c) 2016 - 2018, p≡p foundation
     5 
     6 // Written by Volker Birk
     7 
     8 include ./fsm.yml2
     9 
    10 protocol Sync 1 {
    11     // all messages have a timestamp, time out and are removed after timeout
    12 
    13     fsm KeySync 1 {
    14         version 1, 2;
    15 
    16         state InitState {
    17             on Init {
    18                 if deviceGrouped
    19                     go Grouped;
    20                 go Sole;
    21             }
    22         }
    23 
    24         state Sole timeout=off {
    25             on Init {
    26                 do openChallenge; // own challenge
    27                 send Beacon;
    28             }
    29 
    30             on KeyGen
    31                 send Beacon;
    32 
    33             on CannotDecrypt // cry baby
    34                 send Beacon;
    35 
    36             on Beacon {
    37                 if weAreFirst {
    38                     send Beacon;
    39                 }
    40                 else {
    41                     do storeChallenge; // partner's challenge
    42                     do openTransaction;
    43                     do storeTransaction;
    44                     send HandshakeRequest;
    45                     go HandshakeRequested;
    46                 }
    47             }
    48 
    49             on HandshakeRequest {
    50                 if challengeAccepted {
    51                     do storeTransaction;
    52                     send HandshakeAnswer;
    53                     if partnerIsGrouped
    54                         go HandshakingWithGroup;
    55                     go HandshakingNew;
    56                 }
    57             }
    58         }
    59 
    60         state HandshakeRequested {
    61             on HandshakeAnswer
    62                 go HandshakingNew;
    63         }
    64 
    65         // handshaking without existing Device group
    66         state HandshakingNew {
    67             on Init
    68                 do showSoleHandshake;
    69 
    70             // Cancel is Rollback
    71             on Cancel {
    72                 send Rollback;
    73                 go Sole;
    74             }
    75 
    76             on Rollback
    77                 go Sole;
    78 
    79             // Reject is CommitReject
    80             on Reject {
    81                 send CommitReject;
    82                 do disable;
    83                 go End;
    84             }
    85 
    86             on CommitReject {
    87                 do disable;
    88                 go End;
    89             }
    90 
    91             // Accept is Phase1Commit
    92             on Accept {
    93                 send CommitAccept;
    94                 go HandshakingNewPhase1;
    95             }
    96 
    97             on CommitAccept
    98                 go HandshakingNewPhase1Own;
    99         }
   100 
   101         state HandshakingNewPhase1 {
   102             on Rollback
   103                 go Sole;
   104             
   105             on CommitReject {
   106                 do disable;
   107                 go End;
   108             }
   109 
   110             on CommitAccept
   111                 go NewGroup;
   112         }
   113 
   114         state HandshakingNewPhase1Own {
   115             on Cancel {
   116                 send Rollback;
   117                 go Sole;
   118             }
   119 
   120             on Reject {
   121                 send CommitReject;
   122                 do disable;
   123                 go End;
   124             }
   125 
   126             on Accept
   127                 go NewGroup;
   128         }
   129 
   130         state NewGroup {
   131             on Init
   132                 send GroupKeysAndClose; // we're not grouped yet, this is our own keys
   133 
   134             on GroupKeysAndClose {
   135                 do saveGroupKeys;
   136                 if keyElectionWon {
   137                     do ownKeysAreGroupKeys;
   138                     go Grouped;
   139                 }
   140                 // do receivedKeysAreGroupKeys; -- implicit
   141                 go Grouped;
   142             }
   143         }
   144 
   145         state HandshakingWithGroup {
   146             on Init
   147                 do showJoinGroupHandshake;
   148 
   149             // Cancel is Rollback
   150             on Cancel {
   151                 send Rollback;
   152                 go Sole;
   153             }
   154 
   155             on Rollback
   156                 go Sole;
   157 
   158             // Reject is CommitReject
   159             on Reject {
   160                 send CommitReject;
   161                 do disable;
   162                 go End;
   163             }
   164 
   165             on CommitReject {
   166                 do disable;
   167                 go End;
   168             }
   169 
   170             // Accept is Phase1Commit
   171             on Accept {
   172                 send CommitAccept;
   173                 go HandshakingJoinPhase1;
   174             }
   175 
   176             on CommitAccept
   177                 go HandshakingJoinPhase1Own;
   178         }
   179 
   180         state HandshakingJoinPhase1 {
   181             on Rollback
   182                 go Sole;
   183             
   184             on CommitReject {
   185                 do disable;
   186                 go End;
   187             }
   188 
   189             on CommitAccept
   190                 go JoinGroup;
   191         }
   192 
   193         state HandshakingJoinPhase1Own {
   194             on Cancel {
   195                 send Rollback;
   196                 go Sole;
   197             }
   198 
   199             on Reject {
   200                 send CommitReject;
   201                 do disable;
   202                 go End;
   203             }
   204 
   205             on Accept
   206                 go JoinGroup;
   207         }
   208 
   209         state JoinGroup {
   210             on GroupKeysAndClose {
   211                 send GroupKeys; // first send own keys
   212                 do saveGroupKeys; // then store new group keys
   213                 go Grouped;
   214             }
   215         }
   216 
   217         state Grouped timeout=off {
   218             on GroupKeys
   219                 do saveGroupKeys;
   220 
   221             on KeyGen
   222                 send GroupKeys;
   223 
   224             on Beacon
   225                 send HandshakeRequest;
   226 
   227             on HandshakeAnswer
   228                 go HandshakingGrouped;
   229         }
   230 
   231         state HandshakingGrouped {
   232             on Init
   233                 do showGroupedHandshake;
   234     
   235             // Cancel is Rollback
   236             on Cancel {
   237                 send Rollback;
   238                 go Grouped;
   239             }
   240 
   241             on Rollback
   242                 go Grouped;
   243 
   244             // Reject is CommitReject
   245             on Reject {
   246                 send CommitReject;
   247                 go Grouped;
   248             }
   249 
   250             on CommitReject
   251                 go Grouped;
   252 
   253             // Accept is Phase1Commit
   254             on Accept {
   255                 send CommitAccept;
   256                 go HandshakingGroupedPhase1;
   257             }
   258 
   259             on CommitAccept
   260                 go HandshakingGroupedPhase1Own;
   261 
   262             on GroupKeys
   263                 do saveGroupKeys;
   264         }
   265 
   266         state HandshakingGroupedPhase1 {
   267             on Rollback
   268                 go Grouped;
   269 
   270             on CommitReject
   271                 go Grouped;
   272 
   273             on CommitAccept {
   274                 send GroupKeysAndClose;
   275                 go Grouped;
   276             }
   277 
   278             on GroupKeys
   279                 do saveGroupKeys;
   280         }
   281 
   282         state HandshakingGroupedPhase1Own {
   283             on Cancel {
   284                 send Rollback;
   285                 go Grouped;
   286             }
   287 
   288             on Reject {
   289                 send CommitReject;
   290                 go Grouped;
   291             }
   292 
   293             on Accept {
   294                 send GroupKeysAndClose;
   295                 go Grouped;
   296             }
   297 
   298             on GroupKeys
   299                 do saveGroupKeys;
   300         }
   301  
   302         external Accept 30;
   303         external Reject 31;
   304         external Cancel 32;
   305 
   306         // beacons are always broadcasted
   307 
   308         message Beacon 2, type=broadcast, security=unencrypted {
   309             field TID challenge;
   310             auto Version version;
   311         }
   312 
   313         message HandshakeRequest 3, security=untrusted {
   314             field TID challenge;
   315             auto Version version;
   316             field TID transaction;
   317             field bool is_group;
   318         }
   319 
   320         message HandshakeAnswer 4 {
   321             auto Version version;
   322             field TID transaction;
   323         }
   324 
   325         message Rollback 5, security=untrusted {
   326             field TID transaction;
   327         }
   328 
   329         message CommitReject 6, security=untrusted {
   330             field TID transaction;
   331         }
   332 
   333         message CommitAccept 7 {
   334             field TID transaction;
   335         }
   336 
   337         message GroupKeysAndClose 8 {
   338             field TID transaction;
   339             field IdentityList identities;
   340         }
   341 
   342         message GroupKeys 9 {
   343             field IdentityList identities;
   344         }
   345     }
   346 }
   347