sync/sync.fsm
author Dirk Zimmermann <dz@pep.security>
Tue, 09 Apr 2019 16:02:46 +0200
branchIOS-1482
changeset 3480 689c15d6bef7
parent 3470 3f5f6d631953
child 3509 a9c0c6f31c56
permissions -rw-r--r--
IOS-1482 Xcode: Change organization name.
     1 // This file is under BSD License 2.0
     2 
     3 // Sync protocol for p≡p
     4 // Copyright (c) 2016 - 2019, 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                 }
    46             }
    47 
    48             on HandshakeRequest {
    49                 if challengeAccepted {
    50                     do storeTransaction;
    51                     send HandshakeAnswer;
    52                     if partnerIsGrouped
    53                         go HandshakingWithGroup;
    54                     go HandshakingNew;
    55                 }
    56             }
    57 
    58             on HandshakeAnswer
    59                 go HandshakingNew;
    60         }
    61 
    62         // handshaking without existing Device group
    63         state HandshakingNew {
    64             on Init
    65                 do showSoleHandshake;
    66 
    67             // Cancel is Rollback
    68             on Cancel {
    69                 send Rollback;
    70                 go Sole;
    71             }
    72 
    73             on Rollback
    74                 go Sole;
    75 
    76             // Reject is CommitReject
    77             on Reject {
    78                 send CommitReject;
    79                 do disable;
    80                 go End;
    81             }
    82 
    83             on CommitReject {
    84                 do disable;
    85                 go End;
    86             }
    87 
    88             // Accept is Phase1Commit
    89             on Accept {
    90                 send CommitAcceptForGroup;
    91                 go HandshakingNewPhase1;
    92             }
    93 
    94             on CommitAccept
    95                 go HandshakingNewPhase1Own;
    96         }
    97 
    98         state HandshakingNewPhase1 {
    99             on Rollback
   100                 go Sole;
   101             
   102             on CommitReject {
   103                 do disable;
   104                 go End;
   105             }
   106 
   107             on CommitAcceptForGroup
   108                 go NewGroup;
   109         }
   110 
   111         state HandshakingNewPhase1Own {
   112             on Cancel {
   113                 send Rollback;
   114                 go Sole;
   115             }
   116 
   117             on Reject {
   118                 send CommitReject;
   119                 do disable;
   120                 go End;
   121             }
   122 
   123             on Accept
   124                 go NewGroup;
   125         }
   126 
   127         state NewGroup {
   128             on Init {
   129                 do prepareOwnKeys;
   130                 send GroupKeysAndClose; // we're not grouped yet, this is our own keys
   131             }
   132 
   133             on GroupKeysAndClose {
   134                 if keyElectionWon
   135                     do ownKeysAreGroupKeys;
   136                 else
   137                     do receivedKeysAreGroupKeys;
   138                 go Grouped;
   139             }
   140         }
   141 
   142         state HandshakingWithGroup {
   143             on Init
   144                 do showJoinGroupHandshake;
   145 
   146             // Cancel is Rollback
   147             on Cancel {
   148                 send Rollback;
   149                 go Sole;
   150             }
   151 
   152             on Rollback
   153                 go Sole;
   154 
   155             // Reject is CommitReject
   156             on Reject {
   157                 send CommitReject;
   158                 do disable;
   159                 go End;
   160             }
   161 
   162             on CommitReject {
   163                 do disable;
   164                 go End;
   165             }
   166 
   167             // Accept is Phase1Commit
   168             on Accept {
   169                 send CommitAccept;
   170                 go HandshakingJoinPhase1;
   171             }
   172 
   173             on CommitAcceptForGroup
   174                 go HandshakingJoinPhase1Own;
   175         }
   176 
   177         state HandshakingJoinPhase1 {
   178             on Rollback
   179                 go Sole;
   180             
   181             on CommitReject {
   182                 do disable;
   183                 go End;
   184             }
   185 
   186             on CommitAcceptForGroup
   187                 go JoinGroup;
   188         }
   189 
   190         state HandshakingJoinPhase1Own {
   191             on Cancel {
   192                 send Rollback;
   193                 go Sole;
   194             }
   195 
   196             on Reject {
   197                 send CommitReject;
   198                 do disable;
   199                 go End;
   200             }
   201 
   202             on Accept
   203                 go JoinGroup;
   204         }
   205 
   206         state JoinGroup {
   207             on GroupKeysAndClose {
   208                 send GroupKeys; // first send own keys
   209                 do saveGroupKeys; // then store new group keys
   210                 go Grouped;
   211             }
   212         }
   213 
   214         state Grouped timeout=off {
   215             on GroupKeys
   216                 do saveGroupKeys;
   217 
   218             on KeyGen
   219                 send GroupKeys;
   220 
   221             on Beacon
   222                 send HandshakeRequest;
   223 
   224             on HandshakeAnswer
   225                 go HandshakingGrouped;
   226         }
   227 
   228         state HandshakingGrouped {
   229             on Init
   230                 do showGroupedHandshake;
   231     
   232             // Cancel is Rollback
   233             on Cancel {
   234                 send Rollback;
   235                 go Grouped;
   236             }
   237 
   238             on Rollback
   239                 go Grouped;
   240 
   241             // Reject is CommitReject
   242             on Reject {
   243                 send CommitReject;
   244                 go Grouped;
   245             }
   246 
   247             on CommitReject
   248                 go Grouped;
   249 
   250             // Accept is Phase1Commit
   251             on Accept {
   252                 send CommitAcceptForGroup;
   253                 go HandshakingGroupedPhase1;
   254             }
   255 
   256             on CommitAccept
   257                 go HandshakingGroupedPhase1Own;
   258 
   259             on GroupKeys
   260                 do saveGroupKeys;
   261         }
   262 
   263         state HandshakingGroupedPhase1 {
   264             on Rollback
   265                 go Grouped;
   266 
   267             on CommitReject
   268                 go Grouped;
   269 
   270             on CommitAccept {
   271                 send GroupKeysAndClose;
   272                 go Grouped;
   273             }
   274 
   275             on GroupKeys
   276                 do saveGroupKeys;
   277         }
   278 
   279         state HandshakingGroupedPhase1Own {
   280             on Cancel {
   281                 send Rollback;
   282                 go Grouped;
   283             }
   284 
   285             on Reject {
   286                 send CommitReject;
   287                 go Grouped;
   288             }
   289 
   290             on Accept {
   291                 send GroupKeysAndClose;
   292                 go Grouped;
   293             }
   294 
   295             on GroupKeys
   296                 do saveGroupKeys;
   297         }
   298  
   299         external Accept 129;
   300         external Reject 130;
   301         external Cancel 131;
   302 
   303         // beacons are always broadcasted
   304 
   305         message Beacon 2, type=broadcast, security=unencrypted {
   306             field TID challenge;
   307             auto Version version;
   308         }
   309 
   310         message HandshakeRequest 3, security=untrusted {
   311             field TID challenge;
   312             auto Version version;
   313             field TID transaction;
   314             field bool is_group;
   315         }
   316 
   317         message HandshakeAnswer 4, security=untrusted {
   318             auto Version version;
   319             field TID transaction;
   320         }
   321 
   322         message Rollback 5, security=untrusted {
   323             field TID transaction;
   324         }
   325 
   326         message CommitReject 6, security=untrusted {
   327             field TID transaction;
   328         }
   329 
   330         message CommitAccept 7 {
   331             field TID transaction;
   332         }
   333 
   334         message CommitAcceptForGroup 8 {
   335             field TID transaction;
   336         }
   337 
   338         message GroupKeysAndClose 9, security=attach_own_keys {
   339             field TID transaction;
   340             field IdentityList ownIdentities;
   341         }
   342 
   343         message GroupKeys 10, security=attach_own_keys {
   344             field IdentityList ownIdentities;
   345         }
   346     }
   347 }
   348