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