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