sync/sync.fsm
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Wed, 16 Oct 2019 16:35:08 +0200
branchENGINE-654
changeset 4118 b0995cc07d02
parent 4051 61f1e92fa6df
child 4130 a4174e1e7199
permissions -rw-r--r--
merged in sync
     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=300 {
    14         version 1, 2;
    15 
    16         state InitState {
    17             on Init {
    18                 if deviceGrouped
    19                     go Grouped;
    20                 do newChallengeAndNegotiationBase;
    21                 send Beacon;
    22                 go Sole;
    23             }
    24         }
    25 
    26         state Sole timeout=off {
    27             on Init {
    28                 do showBeingSole;
    29             }
    30 
    31             on KeyGen {
    32                 send Beacon;
    33             }
    34 
    35             on CannotDecrypt { // cry baby
    36                 send Beacon;
    37             }
    38 
    39             on Beacon {
    40                 if sameChallenge {
    41                     // this is our own Beacon; ignore
    42                 }
    43                 else {
    44                     if weAreOfferer {
    45                         do useOwnChallenge;
    46                         send Beacon;
    47                     }
    48                     else /* we are requester */ {
    49                         do openNegotiation;
    50                         do tellWeAreNotGrouped;
    51                         // requester is sending NegotiationRequest
    52                         send NegotiationRequest;
    53                         do useOwnChallenge;
    54                     }
    55                 }
    56             }
    57 
    58             on NegotiationRequest {
    59                 if sameChallenge { // challenge accepted
    60                     if sameNegotiation {
    61                         // this is our own NegotiationRequest; ignore
    62                     }
    63                     else {
    64                         do storeNegotiation;
    65                         // offerer is accepting by confirming NegotiationOpen
    66                         send NegotiationOpen;
    67                         if partnerIsGrouped
    68                             go HandshakingToJoin;
    69                         else
    70                             go HandshakingOfferer;
    71                     }
    72                 }
    73             }
    74 
    75             on NegotiationOpen if sameNegotiationAndPartner {
    76                 // requester is receiving NegotiationOpen
    77                 do storeNegotiation;
    78                 go HandshakingRequester;
    79             }
    80         }
    81 
    82         // handshaking without existing Device group
    83         state HandshakingOfferer timeout=600 {
    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 sameNegotiationAndPartner
    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 sameNegotiationAndPartner {
   104                 do disable;
   105                 go End;
   106             }
   107 
   108             // Accept means init Phase1Commit
   109             on Accept {
   110                 do trustThisKey;
   111                 send CommitAcceptOfferer;
   112                 go HandshakingPhase1Offerer;
   113             }
   114 
   115             // got a CommitAccept from requester
   116             on CommitAcceptRequester if sameNegotiationAndPartner
   117                 go HandshakingPhase2Offerer;
   118         }
   119 
   120         // handshaking without existing Device group
   121         state HandshakingRequester timeout=600 {
   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 sameNegotiationAndPartner
   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 sameNegotiationAndPartner {
   142                 do disable;
   143                 go End;
   144             }
   145 
   146             // Accept means init Phase1Commit
   147             on Accept {
   148                 do trustThisKey;
   149                 send CommitAcceptRequester;
   150                 go HandshakingPhase1Requester;
   151             }
   152 
   153             // got a CommitAccept from offerer
   154             on CommitAcceptOfferer if sameNegotiationAndPartner
   155                 go HandshakingPhase2Requester;
   156         }
   157 
   158         state HandshakingPhase1Offerer {
   159             on Rollback if sameNegotiationAndPartner {
   160                 do untrustThisKey;
   161                 go Sole;
   162             }
   163             
   164             on CommitReject if sameNegotiationAndPartner {
   165                 do untrustThisKey;
   166                 do disable;
   167                 go End;
   168             }
   169 
   170             on CommitAcceptRequester if sameNegotiationAndPartner {
   171                 go FormingGroupOfferer;
   172             }
   173         }
   174 
   175         state HandshakingPhase1Requester {
   176             on Rollback if sameNegotiationAndPartner {
   177                 do untrustThisKey;
   178                 go Sole;
   179             }
   180             
   181             on CommitReject if sameNegotiationAndPartner {
   182                 do untrustThisKey;
   183                 do disable;
   184                 go End;
   185             }
   186 
   187             on CommitAcceptOfferer if sameNegotiationAndPartner {
   188                 go FormingGroupRequester;
   189             }
   190         }
   191 
   192         state HandshakingPhase2Offerer {
   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 CommitAcceptOfferer;
   206                 do trustThisKey;
   207                 go FormingGroupOfferer;
   208             }
   209         }
   210 
   211         state HandshakingPhase2Requester {
   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 CommitAcceptRequester;
   225                 do trustThisKey;
   226                 go FormingGroupRequester;
   227             }
   228         }
   229 
   230         state FormingGroupOfferer {
   231             on Init {
   232                 do prepareOwnKeys;
   233                 send OwnKeysOfferer; // we're not grouped yet, this is our own keys
   234                 do showFormingGroup;
   235             }
   236 
   237             on Cancel {
   238                 send Rollback;
   239                 go Sole;
   240             }
   241 
   242             on Rollback
   243                 go Sole;
   244 
   245             on OwnKeysRequester {
   246                 do saveGroupKeys;
   247                 do receivedKeysAreDefaultKeys;
   248                 do showGroupCreated;
   249                 go Grouped;
   250             }
   251         }
   252 
   253         state FormingGroupRequester {
   254             on Init
   255                 do showFormingGroup;
   256 
   257             on Cancel {
   258                 send Rollback;
   259                 go Sole;
   260             }
   261 
   262             on Rollback
   263                 go Sole;
   264 
   265             on OwnKeysOfferer {
   266                 do saveGroupKeys;
   267                 do prepareOwnKeys;
   268                 do ownKeysAreDefaultKeys;
   269                 send OwnKeysRequester;
   270                 do showGroupCreated;
   271                 go Grouped;
   272             }
   273         }
   274 
   275         state Grouped timeout=off {
   276             on Init {
   277                 do newChallengeAndNegotiationBase;
   278                 do showBeingInGroup;
   279             }
   280 
   281             on GroupKeys
   282                 do saveGroupKeys;
   283 
   284             on KeyGen {
   285                 do prepareOwnKeys;
   286                 send GroupKeys;
   287             }
   288 
   289             on Beacon {
   290                 do openNegotiation;
   291                 do tellWeAreGrouped;
   292                 send NegotiationRequest;
   293                 do useOwnChallenge;
   294             }
   295 
   296             on NegotiationOpen if sameNegotiationAndPartner {
   297                 do storeNegotiation;
   298                 go HandshakingGrouped;
   299             }
   300 
   301             on GroupTrustThisKey
   302                 do trustThisKey;
   303         }
   304 
   305         // sole device handshaking with group
   306         state HandshakingToJoin {
   307             on Init
   308                 do showJoinGroupHandshake;
   309 
   310             // Cancel is Rollback
   311             on Cancel {
   312                 send Rollback;
   313                 go Sole;
   314             }
   315 
   316             on Rollback if sameNegotiationAndPartner
   317                 go Sole;
   318 
   319             // Reject is CommitReject
   320             on Reject {
   321                 send CommitReject;
   322                 do disable;
   323                 go End;
   324             }
   325 
   326             on CommitAcceptForGroup if sameNegotiationAndPartner
   327                 go HandshakingToJoinPhase2;
   328 
   329             on CommitReject if sameNegotiationAndPartner {
   330                 do disable;
   331                 go End;
   332             }
   333 
   334             // Accept is Phase1Commit
   335             on Accept {
   336                 do trustThisKey;
   337                 send CommitAccept;
   338                 go HandshakingToJoinPhase1;
   339             }
   340         }
   341 
   342         state HandshakingToJoinPhase1 {
   343             on Rollback if sameNegotiationAndPartner
   344                 go Sole;
   345             
   346             on CommitReject if sameNegotiationAndPartner {
   347                 do disable;
   348                 go End;
   349             }
   350 
   351             on CommitAcceptForGroup if sameNegotiationAndPartner
   352                 go JoiningGroup;
   353         }
   354 
   355         state HandshakingToJoinPhase2 {
   356             on Cancel {
   357                 send Rollback;
   358                 go Sole;
   359             }
   360 
   361             on Reject {
   362                 send CommitReject;
   363                 do disable;
   364                 go End;
   365             }
   366 
   367             on Accept {
   368                 do trustThisKey;
   369                 go JoiningGroup;
   370             }
   371         }
   372 
   373         state JoiningGroup {
   374             on GroupKeys {
   375                 do saveGroupKeys;
   376                 do receivedKeysAreDefaultKeys;
   377                 do prepareOwnKeys;
   378                 send GroupKeys;
   379                 do showDeviceAdded;
   380                 go Grouped;
   381             }
   382         }
   383 
   384         state HandshakingGrouped {
   385             on Init
   386                 do showGroupedHandshake;
   387     
   388             // Cancel is Rollback
   389             on Cancel {
   390                 send Rollback;
   391                 go Grouped;
   392             }
   393 
   394             on Rollback if sameNegotiationAndPartner
   395                 go Grouped;
   396 
   397             // Reject is CommitReject
   398             on Reject {
   399                 send CommitReject;
   400                 go Grouped;
   401             }
   402 
   403             on CommitReject if sameNegotiationAndPartner
   404                 go Grouped;
   405 
   406             // Accept is Phase1Commit
   407             on Accept {
   408                 do trustThisKey;
   409                 send GroupTrustThisKey;
   410                 send CommitAcceptForGroup;
   411                 go HandshakingGroupedPhase1;
   412             }
   413 
   414             on CommitAccept if sameNegotiationAndPartner
   415                 go HandshakingGroupedPhase2;
   416 
   417             on GroupTrustThisKey {
   418                 do hideHandshakeDialog;
   419                 do trustThisKey;
   420             }
   421 
   422             on GroupKeys
   423                 do saveGroupKeys;
   424         }
   425 
   426         state HandshakingGroupedPhase1 {
   427             on Rollback if sameNegotiationAndPartner
   428                 go Grouped;
   429 
   430             on CommitReject if sameNegotiationAndPartner
   431                 go Grouped;
   432 
   433             on CommitAccept if sameNegotiationAndPartner {
   434                 do prepareOwnKeys;
   435                 send GroupKeys;
   436                 go Grouped;
   437             }
   438 
   439             on GroupTrustThisKey {
   440                 do trustThisKey;
   441             }
   442 
   443             on GroupKeys
   444                 do saveGroupKeys;
   445         }
   446 
   447         state HandshakingGroupedPhase2 {
   448             on Cancel {
   449                 send Rollback;
   450                 go Grouped;
   451             }
   452 
   453             on Reject {
   454                 send CommitReject;
   455                 go Grouped;
   456             }
   457 
   458             on Accept {
   459                 do trustThisKey;
   460                 send GroupTrustThisKey;
   461                 do prepareOwnKeys;
   462                 send GroupKeys;
   463                 go Grouped;
   464             }
   465 
   466             on GroupTrustThisKey {
   467                 do trustThisKey;
   468             }
   469 
   470             on GroupKeys
   471                 do saveGroupKeys;
   472         }
   473  
   474         external Accept 129;
   475         external Reject 130;
   476         external Cancel 131;
   477 
   478         // beacons are always broadcasted
   479 
   480         message Beacon 2, type=broadcast, security=unencrypted {
   481             field TID challenge;
   482             auto Version version;
   483         }
   484 
   485         message NegotiationRequest 3, security=untrusted {
   486             field TID challenge;
   487             auto Version version;
   488             field TID negotiation;
   489             field bool is_group;
   490         }
   491 
   492         message NegotiationOpen 4, security=untrusted {
   493             auto Version version;
   494             field TID negotiation;
   495         }
   496 
   497         message Rollback 5, security=untrusted {
   498             field TID negotiation;
   499         }
   500 
   501         message CommitReject 6, security=untrusted {
   502             field TID negotiation;
   503         }
   504 
   505         message CommitAcceptOfferer 7, security=untrusted {
   506             field TID negotiation;
   507         }
   508 
   509         message CommitAcceptRequester 8, security=untrusted {
   510             field TID negotiation;
   511         }
   512 
   513         message CommitAccept 9, security=untrusted {
   514             field TID negotiation;
   515         }
   516 
   517         message CommitAcceptForGroup 10, security=untrusted {
   518             field TID negotiation;
   519         }
   520 
   521         // default: security=trusted only
   522         message GroupTrustThisKey 11 {
   523             field Hash key;
   524         }
   525 
   526         // trust in future
   527         message GroupKeys 12, security=attach_own_keys {
   528             field IdentityList ownIdentities;
   529         }
   530 
   531         message OwnKeysOfferer 13, security=attach_own_keys {
   532             field IdentityList ownIdentities;
   533         }
   534 
   535         message OwnKeysRequester 14, security=attach_own_keys {
   536             field IdentityList ownIdentities;
   537         }
   538     }
   539 }
   540