sync/sync.fsm
author Thomas
Thu, 27 Jun 2019 09:29:20 +0200
branchsequoia_windows
changeset 3878 37426cf0d1fe
parent 3827 3030ae87f2f9
child 3934 88d7900fa705
permissions -rw-r--r--
Merge with ENGINE-581
     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                 go Sole;
    21             }
    22         }
    23 
    24         state Sole timeout=off {
    25             on Init {
    26                 do newChallengeAndNegotiationBase;
    27                 do showBeingSole;
    28                 send Beacon;
    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             }
   235 
   236             on OwnKeysRequester {
   237                 do saveGroupKeys;
   238                 do receivedKeysAreDefaultKeys;
   239                 do showGroupCreated;
   240                 go Grouped;
   241             }
   242         }
   243 
   244         state FormingGroupRequester {
   245             on Init {
   246                 do prepareOwnKeys;
   247                 send OwnKeysRequester; // we're not grouped yet, this is our own keys
   248             }
   249 
   250             on OwnKeysOfferer {
   251                 do saveGroupKeys;
   252                 do ownKeysAreDefaultKeys;
   253                 do showGroupCreated;
   254                 go Grouped;
   255             }
   256         }
   257 
   258         state Grouped timeout=off {
   259             on Init {
   260                 do newChallengeAndNegotiationBase;
   261                 do showBeingInGroup;
   262             }
   263 
   264             on GroupKeys
   265                 do saveGroupKeys;
   266 
   267             on KeyGen {
   268                 do prepareOwnKeys;
   269                 send GroupKeys;
   270             }
   271 
   272             on Beacon {
   273                 do openNegotiation;
   274                 do tellWeAreGrouped;
   275                 send NegotiationRequest;
   276                 do useOwnChallenge;
   277             }
   278 
   279             on NegotiationOpen if sameNegotiationAndPartner {
   280                 do storeNegotiation;
   281                 go HandshakingGrouped;
   282             }
   283 
   284             on GroupTrustThisKey
   285                 do trustThisKey;
   286         }
   287 
   288         // sole device handshaking with group
   289         state HandshakingToJoin {
   290             on Init
   291                 do showJoinGroupHandshake;
   292 
   293             // Cancel is Rollback
   294             on Cancel {
   295                 send Rollback;
   296                 go Sole;
   297             }
   298 
   299             on Rollback if sameNegotiationAndPartner
   300                 go Sole;
   301 
   302             // Reject is CommitReject
   303             on Reject {
   304                 send CommitReject;
   305                 do disable;
   306                 go End;
   307             }
   308 
   309             on CommitAcceptForGroup if sameNegotiationAndPartner
   310                 go HandshakingToJoinPhase2;
   311 
   312             on CommitReject if sameNegotiationAndPartner {
   313                 do disable;
   314                 go End;
   315             }
   316 
   317             // Accept is Phase1Commit
   318             on Accept {
   319                 do trustThisKey;
   320                 send CommitAccept;
   321                 go HandshakingToJoinPhase1;
   322             }
   323         }
   324 
   325         state HandshakingToJoinPhase1 {
   326             on Rollback if sameNegotiationAndPartner
   327                 go Sole;
   328             
   329             on CommitReject if sameNegotiationAndPartner {
   330                 do disable;
   331                 go End;
   332             }
   333 
   334             on CommitAcceptForGroup if sameNegotiationAndPartner
   335                 go JoiningGroup;
   336         }
   337 
   338         state HandshakingToJoinPhase2 {
   339             on Cancel {
   340                 send Rollback;
   341                 go Sole;
   342             }
   343 
   344             on Reject {
   345                 send CommitReject;
   346                 do disable;
   347                 go End;
   348             }
   349 
   350             on Accept {
   351                 do trustThisKey;
   352                 go JoiningGroup;
   353             }
   354         }
   355 
   356         state JoiningGroup {
   357             on GroupKeys {
   358                 do saveGroupKeys;
   359                 do receivedKeysAreDefaultKeys;
   360                 do prepareOwnKeys;
   361                 send GroupKeys;
   362                 do showDeviceAdded;
   363                 go Grouped;
   364             }
   365         }
   366 
   367         state HandshakingGrouped {
   368             on Init
   369                 do showGroupedHandshake;
   370     
   371             // Cancel is Rollback
   372             on Cancel {
   373                 send Rollback;
   374                 go Grouped;
   375             }
   376 
   377             on Rollback if sameNegotiationAndPartner
   378                 go Grouped;
   379 
   380             // Reject is CommitReject
   381             on Reject {
   382                 send CommitReject;
   383                 go Grouped;
   384             }
   385 
   386             on CommitReject if sameNegotiationAndPartner
   387                 go Grouped;
   388 
   389             // Accept is Phase1Commit
   390             on Accept {
   391                 do trustThisKey;
   392                 send GroupTrustThisKey;
   393                 send CommitAcceptForGroup;
   394                 go HandshakingGroupedPhase1;
   395             }
   396 
   397             on CommitAccept if sameNegotiationAndPartner
   398                 go HandshakingGroupedPhase2;
   399 
   400             on GroupTrustThisKey {
   401                 do hideHandshakeDialog;
   402                 do trustThisKey;
   403             }
   404 
   405             on GroupKeys
   406                 do saveGroupKeys;
   407         }
   408 
   409         state HandshakingGroupedPhase1 {
   410             on Rollback if sameNegotiationAndPartner
   411                 go Grouped;
   412 
   413             on CommitReject if sameNegotiationAndPartner
   414                 go Grouped;
   415 
   416             on CommitAccept if sameNegotiationAndPartner {
   417                 do prepareOwnKeys;
   418                 send GroupKeys;
   419                 go Grouped;
   420             }
   421 
   422             on GroupTrustThisKey {
   423                 do trustThisKey;
   424             }
   425 
   426             on GroupKeys
   427                 do saveGroupKeys;
   428         }
   429 
   430         state HandshakingGroupedPhase2 {
   431             on Cancel {
   432                 send Rollback;
   433                 go Grouped;
   434             }
   435 
   436             on Reject {
   437                 send CommitReject;
   438                 go Grouped;
   439             }
   440 
   441             on Accept {
   442                 do trustThisKey;
   443                 send GroupTrustThisKey;
   444                 do prepareOwnKeys;
   445                 send GroupKeys;
   446                 go Grouped;
   447             }
   448 
   449             on GroupTrustThisKey {
   450                 do trustThisKey;
   451             }
   452 
   453             on GroupKeys
   454                 do saveGroupKeys;
   455         }
   456  
   457         external Accept 129;
   458         external Reject 130;
   459         external Cancel 131;
   460 
   461         // beacons are always broadcasted
   462 
   463         message Beacon 2, type=broadcast, security=unencrypted {
   464             field TID challenge;
   465             auto Version version;
   466         }
   467 
   468         message NegotiationRequest 3, security=untrusted {
   469             field TID challenge;
   470             auto Version version;
   471             field TID negotiation;
   472             field bool is_group;
   473         }
   474 
   475         message NegotiationOpen 4, security=untrusted {
   476             auto Version version;
   477             field TID negotiation;
   478         }
   479 
   480         message Rollback 5, security=untrusted {
   481             field TID negotiation;
   482         }
   483 
   484         message CommitReject 6, security=untrusted {
   485             field TID negotiation;
   486         }
   487 
   488         message CommitAcceptOfferer 7, security=untrusted {
   489             field TID negotiation;
   490         }
   491 
   492         message CommitAcceptRequester 8, security=untrusted {
   493             field TID negotiation;
   494         }
   495 
   496         message CommitAccept 9, security=untrusted {
   497             field TID negotiation;
   498         }
   499 
   500         message CommitAcceptForGroup 10, security=untrusted {
   501             field TID negotiation;
   502         }
   503 
   504         // default: security=trusted only
   505         message GroupTrustThisKey 11 {
   506             field Hash key;
   507         }
   508 
   509         // trust in future
   510         message GroupKeys 12, security=attach_own_keys {
   511             field IdentityList ownIdentities;
   512         }
   513 
   514         message OwnKeysOfferer 13, security=attach_own_keys {
   515             field IdentityList ownIdentities;
   516         }
   517 
   518         message OwnKeysRequester 14, security=attach_own_keys {
   519             field IdentityList ownIdentities;
   520         }
   521     }
   522 }
   523