sync/sync.fsm
author Neal H. Walfield <neal@pep.foundation>
Wed, 08 May 2019 12:20:37 +0200
branchemail_comparison
changeset 3649 1dd837adc30b
parent 3605 27595b58666d
child 3647 1295702dbeaf
permissions -rw-r--r--
Fix public key.
     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, threshold=30 {
    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 closeTransaction;
    27                 do newChallenge;
    28                 do showBeingSole;
    29                 send Beacon;
    30             }
    31 
    32             on KeyGen {
    33                 send Beacon;
    34             }
    35 
    36             on CannotDecrypt { // cry baby
    37                 send Beacon;
    38             }
    39 
    40             on Beacon {
    41                 if sameChallenge {
    42                     // this is our own Beacon; ignore
    43                 }
    44                 else {
    45                     if weAreFirst {
    46                         send Beacon;
    47                     }
    48                     else /* we are second */ {
    49                         do newTransaction;
    50                         do tellWeAreNotGrouped;
    51                         // second is sending NegotiationRequest
    52                         do replyChallenge; // partner's challenge
    53                         send NegotiationRequest;
    54                         do useOwnChallenge;
    55                     }
    56                 }
    57             }
    58 
    59             on NegotiationRequest {
    60                 if challengeAccepted {
    61                     if sameTransaction {
    62                         // this is our own NegotiationRequest; ignore
    63                     }
    64                     else {
    65                         // first is receiving NegotiationRequest
    66                         do storeTransaction;
    67                         // first is sending NegotiationOpen
    68                         send NegotiationOpen;
    69                         if partnerIsGrouped
    70                             go HandshakingWithGroup;
    71                         go HandshakingNewFirst;
    72                     }
    73                 }
    74             }
    75 
    76             on NegotiationOpen if sameTransactionAndPartner {
    77                 // second is receiving NegotiationOpen
    78                 go HandshakingNewSecond;
    79             }
    80         }
    81 
    82         // handshaking without existing Device group
    83         state HandshakingNewFirst {
    84             on Init
    85                 do showSoleHandshake;
    86 
    87             // Cancel is Rollback
    88             on Cancel {
    89                 send Rollback;
    90                 go Sole;
    91             }
    92 
    93             on Rollback if sameTransactionAndPartner
    94                 go Sole;
    95 
    96             // Reject is CommitReject
    97             on Reject {
    98                 send CommitReject;
    99                 do disable;
   100                 go End;
   101             }
   102 
   103             on CommitReject if sameTransactionAndPartner {
   104                 do disable;
   105                 go End;
   106             }
   107 
   108             // Accept means init Phase1Commit
   109             on Accept {
   110                 do trustThisKey;
   111                 send CommitAcceptFirst;
   112                 go HandshakingNewPhase1First;
   113             }
   114 
   115             // got a CommitAccept from second
   116             on CommitAcceptSecond if sameTransactionAndPartner
   117                 go HandshakingNewPhase2First;
   118         }
   119 
   120         // handshaking without existing Device group
   121         state HandshakingNewSecond {
   122             on Init
   123                 do showSoleHandshake;
   124 
   125             // Cancel is Rollback
   126             on Cancel {
   127                 send Rollback;
   128                 go Sole;
   129             }
   130 
   131             on Rollback if sameTransactionAndPartner
   132                 go Sole;
   133 
   134             // Reject is CommitReject
   135             on Reject {
   136                 send CommitReject;
   137                 do disable;
   138                 go End;
   139             }
   140 
   141             on CommitReject if sameTransactionAndPartner {
   142                 do disable;
   143                 go End;
   144             }
   145 
   146             // Accept means init Phase1Commit
   147             on Accept {
   148                 do trustThisKey;
   149                 send CommitAcceptSecond;
   150                 go HandshakingNewPhase1Second;
   151             }
   152 
   153             // got a CommitAccept from first
   154             on CommitAcceptFirst if sameTransactionAndPartner
   155                 go HandshakingNewPhase2Second;
   156         }
   157 
   158         state HandshakingNewPhase1First {
   159             on Rollback if sameTransactionAndPartner {
   160                 do untrustThisKey;
   161                 go Sole;
   162             }
   163             
   164             on CommitReject if sameTransactionAndPartner {
   165                 do untrustThisKey;
   166                 do disable;
   167                 go End;
   168             }
   169 
   170             on CommitAcceptSecond if sameTransactionAndPartner {
   171                 go NewGroupFirst;
   172             }
   173         }
   174 
   175         state HandshakingNewPhase1Second {
   176             on Rollback if sameTransactionAndPartner {
   177                 do untrustThisKey;
   178                 go Sole;
   179             }
   180             
   181             on CommitReject if sameTransactionAndPartner {
   182                 do untrustThisKey;
   183                 do disable;
   184                 go End;
   185             }
   186 
   187             on CommitAcceptFirst if sameTransactionAndPartner {
   188                 go NewGroupSecond;
   189             }
   190         }
   191 
   192         state HandshakingNewPhase2First {
   193             on Cancel {
   194                 send Rollback;
   195                 go Sole;
   196             }
   197 
   198             on Reject {
   199                 send CommitReject;
   200                 do disable;
   201                 go End;
   202             }
   203 
   204             on Accept {
   205                 send CommitAcceptFirst;
   206                 do trustThisKey;
   207                 go NewGroupFirst;
   208             }
   209         }
   210 
   211         state HandshakingNewPhase2Second {
   212             on Cancel {
   213                 send Rollback;
   214                 go Sole;
   215             }
   216 
   217             on Reject {
   218                 send CommitReject;
   219                 do disable;
   220                 go End;
   221             }
   222 
   223             on Accept {
   224                 send CommitAcceptSecond;
   225                 do trustThisKey;
   226                 go NewGroupSecond;
   227             }
   228         }
   229 
   230         state NewGroupFirst {
   231             on Init {
   232                 do prepareOwnKeys;
   233                 send OwnKeysFirst; // we're not grouped yet, this is our own keys
   234             }
   235 
   236             on OwnKeysSecond {
   237                 do saveGroupKeys;
   238 
   239                 if keyElectionWon
   240                     do ownKeysAreGroupKeys;
   241                 else
   242                     do receivedKeysAreGroupKeys;
   243                 do showGroupCreated;
   244                 go Grouped;
   245             }
   246         }
   247 
   248         state NewGroupSecond {
   249             on Init {
   250                 do prepareOwnKeys;
   251                 send OwnKeysSecond; // we're not grouped yet, this is our own keys
   252             }
   253 
   254             on OwnKeysFirst {
   255                 do saveGroupKeys;
   256 
   257                 if keyElectionWon
   258                     do ownKeysAreGroupKeys;
   259                 else
   260                     do receivedKeysAreGroupKeys;
   261                 do showGroupCreated;
   262                 go Grouped;
   263             }
   264         }
   265 
   266         state Grouped timeout=off {
   267             on Init {
   268                 do closeTransaction;
   269                 do newChallenge;
   270                 do showBeingInGroup;
   271             }
   272 
   273             on GroupKeys
   274                 do saveGroupKeys;
   275 
   276             on KeyGen {
   277                 do prepareOwnKeys;
   278                 send GroupKeys;
   279             }
   280 
   281             on Beacon {
   282                 do newTransaction;
   283                 do tellWeAreGrouped;
   284                 do replyChallenge; // partner's challenge
   285                 send NegotiationRequest;
   286                 do useOwnChallenge;
   287             }
   288 
   289             on NegotiationOpen if sameTransactionAndPartner
   290                 go HandshakingGrouped;
   291 
   292             on GroupTrustThisKey {
   293                 do trustThisKey;
   294             }
   295         }
   296 
   297         // sole device handshaking with group
   298         state HandshakingWithGroup {
   299             on Init
   300                 do showJoinGroupHandshake;
   301 
   302             // Cancel is Rollback
   303             on Cancel {
   304                 send Rollback;
   305                 go Sole;
   306             }
   307 
   308             on Rollback if sameTransactionAndPartner
   309                 go Sole;
   310 
   311             // Reject is CommitReject
   312             on Reject {
   313                 send CommitReject;
   314                 do disable;
   315                 go End;
   316             }
   317 
   318             on CommitReject if sameTransactionAndPartner {
   319                 do disable;
   320                 go End;
   321             }
   322 
   323             // Accept is Phase1Commit
   324             on Accept {
   325                 do trustThisKey;
   326                 send CommitAccept;
   327                 go HandshakingJoinPhase1;
   328             }
   329 
   330             on CommitAcceptForGroup if sameTransactionAndPartner
   331                 go HandshakingJoinPhase2;
   332         }
   333 
   334         state HandshakingJoinPhase1 {
   335             on Rollback if sameTransactionAndPartner
   336                 go Sole;
   337             
   338             on CommitReject if sameTransactionAndPartner {
   339                 do disable;
   340                 go End;
   341             }
   342 
   343             on CommitAcceptForGroup if sameTransactionAndPartner {
   344                 go JoinGroup;
   345             }
   346         }
   347 
   348         state HandshakingJoinPhase2 {
   349             on Cancel {
   350                 send Rollback;
   351                 go Sole;
   352             }
   353 
   354             on Reject {
   355                 send CommitReject;
   356                 do disable;
   357                 go End;
   358             }
   359 
   360             on Accept {
   361                 do trustThisKey;
   362                 go JoinGroup;
   363             }
   364         }
   365 
   366         state JoinGroup {
   367             on Init {
   368                 do prepareOwnKeys;
   369                 send OwnKeys;
   370             }
   371 
   372             on GroupKeys {
   373                 do saveGroupKeys;
   374                 do receivedKeysAreGroupKeys;
   375                 do showDeviceAdded;
   376                 go Grouped;
   377             }
   378         }
   379 
   380         state HandshakingGrouped {
   381             on Init
   382                 do showGroupedHandshake;
   383     
   384             // Cancel is Rollback
   385             on Cancel {
   386                 send Rollback;
   387                 go Grouped;
   388             }
   389 
   390             on Rollback if sameTransactionAndPartner
   391                 go Grouped;
   392 
   393             // Reject is CommitReject
   394             on Reject {
   395                 send CommitReject;
   396                 go Grouped;
   397             }
   398 
   399             on CommitReject if sameTransactionAndPartner
   400                 go Grouped;
   401 
   402             // Accept is Phase1Commit
   403             on Accept {
   404                 do trustThisKey;
   405                 send GroupTrustThisKey;
   406                 send CommitAcceptForGroup;
   407                 go HandshakingGroupedPhase1;
   408             }
   409 
   410             on CommitAccept if sameTransactionAndPartner
   411                 go HandshakingGroupedPhase2;
   412 
   413             on GroupTrustThisKey {
   414                 do hideHandshakeDialog;
   415                 do trustThisKey;
   416             }
   417 
   418             on CommitAcceptForGroup {
   419                 do showDeviceAdded;
   420                 if sameTransactionAndPartner {
   421                     do hideHandshakeDialog;
   422                     go Grouped;
   423                 }
   424             }
   425 
   426             on GroupKeys
   427                 do saveGroupKeys;
   428         }
   429 
   430         state HandshakingGroupedPhase1 {
   431             on Rollback if sameTransactionAndPartner
   432                 go Grouped;
   433 
   434             on CommitReject if sameTransactionAndPartner
   435                 go Grouped;
   436 
   437             on CommitAccept if sameTransactionAndPartner {
   438                 send GroupKeys;
   439                 go Grouped;
   440             }
   441 
   442             on GroupTrustThisKey {
   443                 do trustThisKey;
   444             }
   445 
   446             on CommitAcceptForGroup {
   447                 do showDeviceAdded;
   448                 if sameTransactionAndPartner
   449                     go Grouped;
   450             }
   451 
   452             on GroupKeys
   453                 do saveGroupKeys;
   454         }
   455 
   456         state HandshakingGroupedPhase2 {
   457             on Cancel {
   458                 send Rollback;
   459                 go Grouped;
   460             }
   461 
   462             on Reject {
   463                 send CommitReject;
   464                 go Grouped;
   465             }
   466 
   467             on Accept {
   468                 do trustThisKey;
   469                 send GroupTrustThisKey;
   470                 send GroupKeys;
   471                 go Grouped;
   472             }
   473 
   474             on GroupTrustThisKey {
   475                 do trustThisKey;
   476             }
   477 
   478             on CommitAcceptForGroup {
   479                 do showDeviceAdded;
   480                 if sameTransactionAndPartner {
   481                     do hideHandshakeDialog;
   482                     go Grouped;
   483                 }
   484             }
   485 
   486             on GroupKeys
   487                 do saveGroupKeys;
   488         }
   489  
   490         external Accept 129;
   491         external Reject 130;
   492         external Cancel 131;
   493 
   494         // beacons are always broadcasted
   495 
   496         message Beacon 2, type=broadcast, security=unencrypted {
   497             field TID challenge;
   498             auto Version version;
   499         }
   500 
   501         message NegotiationRequest 3, security=untrusted {
   502             field TID challenge;
   503             auto Version version;
   504             field TID negotiation;
   505             field bool is_group;
   506         }
   507 
   508         message NegotiationOpen 4, security=untrusted {
   509             auto Version version;
   510             field TID negotiation;
   511         }
   512 
   513         message Rollback 5, security=untrusted {
   514             field TID negotiation;
   515         }
   516 
   517         message CommitReject 6, security=untrusted {
   518             field TID negotiation;
   519         }
   520 
   521         message CommitAcceptFirst 7, security=untrusted {
   522             field TID negotiation;
   523         }
   524 
   525         message CommitAcceptSecond 8, security=untrusted {
   526             field TID negotiation;
   527         }
   528 
   529         message CommitAccept 9, security=untrusted {
   530             field TID negotiation;
   531         }
   532 
   533         message CommitAcceptForGroup 10, security=untrusted {
   534             field TID negotiation;
   535         }
   536 
   537         // default: security=trusted only
   538         message GroupTrustThisKey 11 {
   539             field Hash key;
   540         }
   541 
   542         // trust in future
   543         message GroupKeys 12, security=attach_own_keys {
   544             field IdentityList ownIdentities;
   545         }
   546 
   547         message OwnKeys 13, security=attach_own_keys {
   548             field IdentityList ownIdentities;
   549         }
   550 
   551         message OwnKeysFirst 14, security=attach_own_keys {
   552             field IdentityList ownIdentities;
   553         }
   554 
   555         message OwnKeysSecond 15, security=attach_own_keys {
   556             field IdentityList ownIdentities;
   557         }
   558     }
   559 }
   560