sync/sync.fsm
author Volker Birk <vb@pep-project.org>
Sat, 13 Apr 2019 22:18:57 +0200
branchsync
changeset 3509 a9c0c6f31c56
parent 3470 3f5f6d631953
child 3510 98f79218de04
permissions -rw-r--r--
make it simple
vb@2831
     1
// This file is under BSD License 2.0
vb@2831
     2
vb@2831
     3
// Sync protocol for p≡p
vb@3389
     4
// Copyright (c) 2016 - 2019, 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
    fsm KeySync 1 {
vb@2831
    14
        version 1, 2;
vb@2913
    15
vb@2831
    16
        state InitState {
vb@2831
    17
            on Init {
vb@2831
    18
                if deviceGrouped
vb@2831
    19
                    go Grouped;
vb@2831
    20
                go Sole;
vb@2831
    21
            }
vb@2831
    22
        }
vb@2831
    23
vb@2908
    24
        state Sole timeout=off {
vb@2831
    25
            on Init {
vb@2831
    26
                do openChallenge; // own challenge
vb@2865
    27
                send Beacon;
vb@2831
    28
            }
vb@2831
    29
vb@2831
    30
            on KeyGen
vb@2865
    31
                send Beacon;
vb@2831
    32
vb@2831
    33
            on CannotDecrypt // cry baby
vb@2865
    34
                send Beacon;
vb@2831
    35
vb@2831
    36
            on Beacon {
vb@2907
    37
                if weAreFirst {
vb@2907
    38
                    send Beacon;
vb@2907
    39
                }
vb@3509
    40
                else /* we are second */ {
vb@2902
    41
                    do storeChallenge; // partner's challenge
vb@2902
    42
                    do openTransaction;
vb@2902
    43
                    do storeTransaction;
vb@3509
    44
                    // second is sending HandshakeRequest
vb@2902
    45
                    send HandshakeRequest;
vb@2902
    46
                }
vb@2831
    47
            }
vb@2831
    48
vb@2831
    49
            on HandshakeRequest {
vb@3509
    50
                if weAreFirst {
vb@3509
    51
                    if challengeAccepted {
vb@3509
    52
                        // first is receiving HandshakeRequest
vb@3509
    53
                        do storeTransaction;
vb@3509
    54
                        // first is sending HandshakeAnswer
vb@3509
    55
                        send HandshakeAnswer;
vb@3509
    56
                        if partnerIsGrouped
vb@3509
    57
                            go HandshakingWithGroup;
vb@3509
    58
                        go HandshakingNewFirst;
vb@3509
    59
                    }
vb@2831
    60
                }
vb@2831
    61
            }
vb@3341
    62
vb@3509
    63
            on HandshakeAnswer {
vb@3509
    64
                if weAreFirst {
vb@3509
    65
                    // ignore
vb@3509
    66
                }
vb@3509
    67
                else {
vb@3509
    68
                    // second is receiving HandshakeAnswer
vb@3509
    69
                    go HandshakingNewSecond;
vb@3509
    70
                }
vb@3509
    71
            }
vb@2831
    72
        }
vb@2831
    73
vb@2831
    74
        // handshaking without existing Device group
vb@3509
    75
        state HandshakingNewFirst {
vb@3470
    76
            on Init
vb@2831
    77
                do showSoleHandshake;
vb@2831
    78
vb@2831
    79
            // Cancel is Rollback
vb@2831
    80
            on Cancel {
vb@2865
    81
                send Rollback;
vb@2831
    82
                go Sole;
vb@2831
    83
            }
vb@2831
    84
vb@2831
    85
            on Rollback
vb@2831
    86
                go Sole;
vb@2831
    87
vb@2831
    88
            // Reject is CommitReject
vb@2831
    89
            on Reject {
vb@2865
    90
                send CommitReject;
vb@2831
    91
                do disable;
vb@2831
    92
                go End;
vb@2831
    93
            }
vb@2831
    94
vb@2831
    95
            on CommitReject {
vb@2831
    96
                do disable;
vb@2831
    97
                go End;
vb@2831
    98
            }
vb@2831
    99
vb@3509
   100
            // Accept means init Phase1Commit
vb@2831
   101
            on Accept {
vb@3470
   102
                send CommitAcceptForGroup;
vb@3509
   103
                go HandshakingNewPhase1First;
vb@3509
   104
            }
vb@3509
   105
vb@3509
   106
            // got a CommitAccept from second
vb@3509
   107
            on CommitAccept
vb@3509
   108
                go HandshakingNewPhase2;
vb@3509
   109
        }
vb@3509
   110
vb@3509
   111
        // handshaking without existing Device group
vb@3509
   112
        state HandshakingNewSecond {
vb@3509
   113
            on Init
vb@3509
   114
                do showSoleHandshake;
vb@3509
   115
vb@3509
   116
            // Cancel is Rollback
vb@3509
   117
            on Cancel {
vb@3509
   118
                send Rollback;
vb@3509
   119
                go Sole;
vb@3509
   120
            }
vb@3509
   121
vb@3509
   122
            on Rollback
vb@3509
   123
                go Sole;
vb@3509
   124
vb@3509
   125
            // Reject is CommitReject
vb@3509
   126
            on Reject {
vb@3509
   127
                send CommitReject;
vb@3509
   128
                do disable;
vb@3509
   129
                go End;
vb@3509
   130
            }
vb@3509
   131
vb@3509
   132
            on CommitReject {
vb@3509
   133
                do disable;
vb@3509
   134
                go End;
vb@3509
   135
            }
vb@3509
   136
vb@3509
   137
            // Accept means init Phase1Commit
vb@3509
   138
            on Accept {
vb@3509
   139
                send CommitAccept;
vb@3509
   140
                go HandshakingNewPhase1Second;
vb@3509
   141
            }
vb@3509
   142
vb@3509
   143
            // got a CommitAccept from first
vb@3509
   144
            on CommitAccept
vb@3509
   145
                go HandshakingNewPhase2;
vb@3509
   146
        }
vb@3509
   147
vb@3509
   148
        state HandshakingNewPhase1First {
vb@3509
   149
            on Rollback
vb@3509
   150
                go Sole;
vb@3509
   151
            
vb@3509
   152
            on CommitReject {
vb@3509
   153
                do disable;
vb@3509
   154
                go End;
vb@2831
   155
            }
vb@2831
   156
vb@3470
   157
            on CommitAccept
vb@3509
   158
                go NewGroup;
vb@2831
   159
        }
vb@2831
   160
vb@3509
   161
        state HandshakingNewPhase1Second {
vb@2831
   162
            on Rollback
vb@2831
   163
                go Sole;
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@3470
   170
            on CommitAcceptForGroup
vb@3470
   171
                go NewGroup;
vb@2831
   172
        }
vb@2831
   173
vb@3509
   174
        state HandshakingNewPhase2 {
vb@2831
   175
            on Cancel {
vb@2865
   176
                send Rollback;
vb@2831
   177
                go Sole;
vb@2831
   178
            }
vb@2831
   179
vb@2831
   180
            on Reject {
vb@2865
   181
                send CommitReject;
vb@2831
   182
                do disable;
vb@2831
   183
                go End;
vb@2831
   184
            }
vb@2831
   185
vb@2831
   186
            on Accept
vb@3470
   187
                go NewGroup;
vb@2831
   188
        }
vb@2831
   189
vb@2831
   190
        state NewGroup {
vb@3390
   191
            on Init {
vb@3390
   192
                do prepareOwnKeys;
vb@2865
   193
                send GroupKeysAndClose; // we're not grouped yet, this is our own keys
vb@3390
   194
            }
vb@2831
   195
vb@2831
   196
            on GroupKeysAndClose {
vb@3406
   197
                if keyElectionWon
vb@2831
   198
                    do ownKeysAreGroupKeys;
vb@3406
   199
                else
vb@3406
   200
                    do receivedKeysAreGroupKeys;
vb@2831
   201
                go Grouped;
vb@2831
   202
            }
vb@2831
   203
        }
vb@2831
   204
vb@2831
   205
        state HandshakingWithGroup {
vb@2831
   206
            on Init
vb@2831
   207
                do showJoinGroupHandshake;
vb@2831
   208
vb@2831
   209
            // Cancel is Rollback
vb@2831
   210
            on Cancel {
vb@2865
   211
                send Rollback;
vb@2831
   212
                go Sole;
vb@2831
   213
            }
vb@2831
   214
vb@2831
   215
            on Rollback
vb@2831
   216
                go Sole;
vb@2831
   217
vb@2831
   218
            // Reject is CommitReject
vb@2831
   219
            on Reject {
vb@2865
   220
                send CommitReject;
vb@2831
   221
                do disable;
vb@2831
   222
                go End;
vb@2831
   223
            }
vb@2831
   224
vb@2831
   225
            on CommitReject {
vb@2831
   226
                do disable;
vb@2831
   227
                go End;
vb@2831
   228
            }
vb@2831
   229
vb@2831
   230
            // Accept is Phase1Commit
vb@2831
   231
            on Accept {
vb@2865
   232
                send CommitAccept;
vb@2831
   233
                go HandshakingJoinPhase1;
vb@2831
   234
            }
vb@2831
   235
vb@3439
   236
            on CommitAcceptForGroup
vb@2831
   237
                go HandshakingJoinPhase1Own;
vb@2831
   238
        }
vb@2831
   239
vb@2831
   240
        state HandshakingJoinPhase1 {
vb@2831
   241
            on Rollback
vb@2831
   242
                go Sole;
vb@2831
   243
            
vb@2831
   244
            on CommitReject {
vb@2831
   245
                do disable;
vb@2831
   246
                go End;
vb@2831
   247
            }
vb@2831
   248
vb@3439
   249
            on CommitAcceptForGroup
vb@2831
   250
                go JoinGroup;
vb@2831
   251
        }
vb@2831
   252
vb@2831
   253
        state HandshakingJoinPhase1Own {
vb@2831
   254
            on Cancel {
vb@2865
   255
                send Rollback;
vb@2831
   256
                go Sole;
vb@2831
   257
            }
vb@2831
   258
vb@2831
   259
            on Reject {
vb@2865
   260
                send CommitReject;
vb@2831
   261
                do disable;
vb@2831
   262
                go End;
vb@2831
   263
            }
vb@2831
   264
vb@2831
   265
            on Accept
vb@3509
   266
                go OwnGroup;
vb@2831
   267
        }
vb@2831
   268
vb@2831
   269
        state JoinGroup {
vb@2831
   270
            on GroupKeysAndClose {
vb@2865
   271
                send GroupKeys; // first send own keys
vb@2831
   272
                do saveGroupKeys; // then store new group keys
vb@2831
   273
                go Grouped;
vb@2831
   274
            }
vb@2831
   275
        }
vb@2831
   276
vb@3509
   277
        state OwnGroup {
vb@3509
   278
            on GroupKeys {
vb@3509
   279
                send GroupKeys; // first send own keys
vb@3509
   280
                do saveGroupKeys; // then store new group keys
vb@3509
   281
                go Grouped;
vb@3509
   282
            }
vb@3509
   283
        }
vb@3509
   284
vb@2908
   285
        state Grouped timeout=off {
vb@2831
   286
            on GroupKeys
vb@2831
   287
                do saveGroupKeys;
vb@2831
   288
vb@2831
   289
            on KeyGen
vb@2865
   290
                send GroupKeys;
vb@2831
   291
vb@2831
   292
            on Beacon
vb@2865
   293
                send HandshakeRequest;
vb@2831
   294
vb@2831
   295
            on HandshakeAnswer
vb@2831
   296
                go HandshakingGrouped;
vb@2831
   297
        }
vb@2831
   298
vb@2831
   299
        state HandshakingGrouped {
vb@2831
   300
            on Init
vb@2831
   301
                do showGroupedHandshake;
vb@2831
   302
    
vb@2831
   303
            // Cancel is Rollback
vb@2831
   304
            on Cancel {
vb@2865
   305
                send Rollback;
vb@2831
   306
                go Grouped;
vb@2831
   307
            }
vb@2831
   308
vb@2831
   309
            on Rollback
vb@2831
   310
                go Grouped;
vb@2831
   311
vb@2831
   312
            // Reject is CommitReject
vb@2831
   313
            on Reject {
vb@2865
   314
                send CommitReject;
vb@2831
   315
                go Grouped;
vb@2831
   316
            }
vb@2831
   317
vb@2831
   318
            on CommitReject
vb@2831
   319
                go Grouped;
vb@2831
   320
vb@2831
   321
            // Accept is Phase1Commit
vb@2831
   322
            on Accept {
vb@3439
   323
                send CommitAcceptForGroup;
vb@2831
   324
                go HandshakingGroupedPhase1;
vb@2831
   325
            }
vb@2831
   326
vb@2831
   327
            on CommitAccept
vb@2831
   328
                go HandshakingGroupedPhase1Own;
vb@2831
   329
vb@2831
   330
            on GroupKeys
vb@2831
   331
                do saveGroupKeys;
vb@2831
   332
        }
vb@2831
   333
vb@2831
   334
        state HandshakingGroupedPhase1 {
vb@2831
   335
            on Rollback
vb@2831
   336
                go Grouped;
vb@2831
   337
vb@2831
   338
            on CommitReject
vb@2831
   339
                go Grouped;
vb@2831
   340
vb@2831
   341
            on CommitAccept {
vb@2865
   342
                send GroupKeysAndClose;
vb@2831
   343
                go Grouped;
vb@2831
   344
            }
vb@2831
   345
vb@2831
   346
            on GroupKeys
vb@2831
   347
                do saveGroupKeys;
vb@2831
   348
        }
vb@2831
   349
vb@2831
   350
        state HandshakingGroupedPhase1Own {
vb@2831
   351
            on Cancel {
vb@2865
   352
                send Rollback;
vb@2831
   353
                go Grouped;
vb@2831
   354
            }
vb@2831
   355
vb@2831
   356
            on Reject {
vb@2865
   357
                send CommitReject;
vb@2831
   358
                go Grouped;
vb@2831
   359
            }
vb@2831
   360
vb@2831
   361
            on Accept {
vb@2865
   362
                send GroupKeysAndClose;
vb@2831
   363
                go Grouped;
vb@2831
   364
            }
vb@2831
   365
vb@2831
   366
            on GroupKeys
vb@2831
   367
                do saveGroupKeys;
vb@2831
   368
        }
vb@2831
   369
 
vb@3384
   370
        external Accept 129;
vb@3384
   371
        external Reject 130;
vb@3384
   372
        external Cancel 131;
vb@2831
   373
vb@2867
   374
        // beacons are always broadcasted
vb@2867
   375
vb@2876
   376
        message Beacon 2, type=broadcast, security=unencrypted {
vb@2831
   377
            field TID challenge;
vb@2831
   378
            auto Version version;
vb@2831
   379
        }
vb@2831
   380
vb@2876
   381
        message HandshakeRequest 3, security=untrusted {
vb@2831
   382
            field TID challenge;
vb@2831
   383
            auto Version version;
vb@2831
   384
            field TID transaction;
vb@2831
   385
            field bool is_group;
vb@2831
   386
        }
vb@2831
   387
vb@3381
   388
        message HandshakeAnswer 4, security=untrusted {
vb@3379
   389
            auto Version version;
vb@2831
   390
            field TID transaction;
vb@2831
   391
        }
vb@2831
   392
vb@2876
   393
        message Rollback 5, security=untrusted {
vb@2831
   394
            field TID transaction;
vb@2831
   395
        }
vb@2831
   396
vb@2876
   397
        message CommitReject 6, security=untrusted {
vb@2831
   398
            field TID transaction;
vb@2831
   399
        }
vb@2831
   400
vb@2831
   401
        message CommitAccept 7 {
vb@2831
   402
            field TID transaction;
vb@2831
   403
        }
vb@2831
   404
vb@3439
   405
        message CommitAcceptForGroup 8 {
vb@3439
   406
            field TID transaction;
vb@3439
   407
        }
vb@3439
   408
vb@3439
   409
        message GroupKeysAndClose 9, security=attach_own_keys {
vb@2831
   410
            field TID transaction;
vb@3390
   411
            field IdentityList ownIdentities;
vb@2831
   412
        }
vb@2831
   413
vb@3439
   414
        message GroupKeys 10, security=attach_own_keys {
vb@3390
   415
            field IdentityList ownIdentities;
vb@2831
   416
        }
vb@2831
   417
    }
vb@2831
   418
}
vb@2831
   419