sync/sync.fsm
author Volker Birk <vb@pep-project.org>
Sat, 08 Jun 2019 19:45:18 +0200
branchsync
changeset 3825 b8d55cc4755e
parent 3821 46cc98d0f16b
child 3826 09813a43e17d
permissions -rw-r--r--
art & beauty
vb@2831
     1
// This file is under BSD License 2.0
vb@2831
     2
vb@2831
     3
// Sync protocol for p≡p
vb@3512
     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
Thomas@3647
    13
    fsm KeySync 1, threshold=60 {
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@3781
    26
                do newChallengeAndNegotiationBase;
vb@3525
    27
                do showBeingSole;
vb@2865
    28
                send Beacon;
vb@2831
    29
            }
vb@2831
    30
vb@3601
    31
            on KeyGen {
vb@2865
    32
                send Beacon;
vb@3601
    33
            }
vb@2831
    34
vb@3601
    35
            on CannotDecrypt { // cry baby
vb@2865
    36
                send Beacon;
vb@3601
    37
            }
vb@2831
    38
vb@2831
    39
            on Beacon {
vb@3514
    40
                if sameChallenge {
vb@3591
    41
                    // this is our own Beacon; ignore
vb@2907
    42
                }
vb@3514
    43
                else {
vb@3825
    44
                    if weAreOfferer {
vb@3779
    45
                        do useOwnChallenge;
vb@3514
    46
                        send Beacon;
vb@3514
    47
                    }
vb@3825
    48
                    else /* we are requester */ {
vb@3781
    49
                        do openNegotiation;
vb@3518
    50
                        do tellWeAreNotGrouped;
vb@3825
    51
                        // requester is sending NegotiationRequest
vb@3556
    52
                        send NegotiationRequest;
vb@3605
    53
                        do useOwnChallenge;
vb@3514
    54
                    }
vb@2902
    55
                }
vb@2831
    56
            }
vb@2831
    57
vb@3601
    58
            on NegotiationRequest {
vb@3779
    59
                if sameChallenge { // challenge accepted
vb@3779
    60
                    if sameNegotiation {
vb@3601
    61
                        // this is our own NegotiationRequest; ignore
vb@3601
    62
                    }
vb@3601
    63
                    else {
vb@3779
    64
                        do storeNegotiation;
vb@3825
    65
                        // offerer is accepting by confirming NegotiationOpen
vb@3601
    66
                        send NegotiationOpen;
vb@3601
    67
                        if partnerIsGrouped
vb@3825
    68
                            go HandshakingToJoin;
vb@3779
    69
                        else
vb@3825
    70
                            go HandshakingOfferer;
vb@3601
    71
                    }
vb@2831
    72
                }
vb@2831
    73
            }
vb@3341
    74
vb@3779
    75
            on NegotiationOpen if sameNegotiationAndPartner {
vb@3825
    76
                // requester is receiving NegotiationOpen
vb@3791
    77
                do storeNegotiation;
vb@3825
    78
                go HandshakingRequester;
vb@3509
    79
            }
vb@2831
    80
        }
vb@2831
    81
vb@2831
    82
        // handshaking without existing Device group
vb@3825
    83
        state HandshakingOfferer timeout=600 {
vb@3470
    84
            on Init
vb@2831
    85
                do showSoleHandshake;
vb@2831
    86
vb@2831
    87
            // Cancel is Rollback
vb@2831
    88
            on Cancel {
vb@2865
    89
                send Rollback;
vb@2831
    90
                go Sole;
vb@2831
    91
            }
vb@2831
    92
vb@3779
    93
            on Rollback if sameNegotiationAndPartner
vb@2831
    94
                go Sole;
vb@2831
    95
vb@2831
    96
            // Reject is CommitReject
vb@2831
    97
            on Reject {
vb@2865
    98
                send CommitReject;
vb@2831
    99
                do disable;
vb@2831
   100
                go End;
vb@2831
   101
            }
vb@2831
   102
vb@3779
   103
            on CommitReject if sameNegotiationAndPartner {
vb@2831
   104
                do disable;
vb@2831
   105
                go End;
vb@2831
   106
            }
vb@2831
   107
vb@3509
   108
            // Accept means init Phase1Commit
vb@2831
   109
            on Accept {
vb@3516
   110
                do trustThisKey;
vb@3825
   111
                send CommitAcceptOfferer;
vb@3825
   112
                go HandshakingPhase1Offerer;
vb@3509
   113
            }
vb@3509
   114
vb@3825
   115
            // got a CommitAccept from requester
vb@3825
   116
            on CommitAcceptRequester if sameNegotiationAndPartner
vb@3825
   117
                go HandshakingPhase2Offerer;
vb@3509
   118
        }
vb@3509
   119
vb@3509
   120
        // handshaking without existing Device group
vb@3825
   121
        state HandshakingRequester timeout=600 {
vb@3509
   122
            on Init
vb@3509
   123
                do showSoleHandshake;
vb@3509
   124
vb@3509
   125
            // Cancel is Rollback
vb@3509
   126
            on Cancel {
vb@3509
   127
                send Rollback;
vb@3509
   128
                go Sole;
vb@3509
   129
            }
vb@3509
   130
vb@3779
   131
            on Rollback if sameNegotiationAndPartner
vb@3509
   132
                go Sole;
vb@3509
   133
vb@3509
   134
            // Reject is CommitReject
vb@3509
   135
            on Reject {
vb@3509
   136
                send CommitReject;
vb@3509
   137
                do disable;
vb@3509
   138
                go End;
vb@3509
   139
            }
vb@3509
   140
vb@3779
   141
            on CommitReject if sameNegotiationAndPartner {
vb@3509
   142
                do disable;
vb@3509
   143
                go End;
vb@3509
   144
            }
vb@3509
   145
vb@3509
   146
            // Accept means init Phase1Commit
vb@3509
   147
            on Accept {
vb@3516
   148
                do trustThisKey;
vb@3825
   149
                send CommitAcceptRequester;
vb@3825
   150
                go HandshakingPhase1Requester;
vb@3509
   151
            }
vb@3509
   152
vb@3825
   153
            // got a CommitAccept from offerer
vb@3825
   154
            on CommitAcceptOfferer if sameNegotiationAndPartner
vb@3825
   155
                go HandshakingPhase2Requester;
vb@3509
   156
        }
vb@3509
   157
vb@3825
   158
        state HandshakingPhase1Offerer {
vb@3779
   159
            on Rollback if sameNegotiationAndPartner {
vb@3524
   160
                do untrustThisKey;
vb@3509
   161
                go Sole;
vb@3524
   162
            }
vb@3509
   163
            
vb@3779
   164
            on CommitReject if sameNegotiationAndPartner {
vb@3524
   165
                do untrustThisKey;
vb@3509
   166
                do disable;
vb@3509
   167
                go End;
vb@2831
   168
            }
vb@2831
   169
vb@3825
   170
            on CommitAcceptRequester if sameNegotiationAndPartner {
vb@3825
   171
                go FormingGroupOfferer;
vb@3550
   172
            }
vb@2831
   173
        }
vb@2831
   174
vb@3825
   175
        state HandshakingPhase1Requester {
vb@3779
   176
            on Rollback if sameNegotiationAndPartner {
vb@3524
   177
                do untrustThisKey;
vb@2831
   178
                go Sole;
vb@3524
   179
            }
vb@2831
   180
            
vb@3779
   181
            on CommitReject if sameNegotiationAndPartner {
vb@3524
   182
                do untrustThisKey;
vb@2831
   183
                do disable;
vb@2831
   184
                go End;
vb@2831
   185
            }
vb@2831
   186
vb@3825
   187
            on CommitAcceptOfferer if sameNegotiationAndPartner {
vb@3825
   188
                go FormingGroupRequester;
vb@3550
   189
            }
vb@2831
   190
        }
vb@2831
   191
vb@3825
   192
        state HandshakingPhase2Offerer {
vb@2831
   193
            on Cancel {
vb@2865
   194
                send Rollback;
vb@2831
   195
                go Sole;
vb@2831
   196
            }
vb@2831
   197
vb@2831
   198
            on Reject {
vb@2865
   199
                send CommitReject;
vb@2831
   200
                do disable;
vb@2831
   201
                go End;
vb@2831
   202
            }
vb@2831
   203
vb@3516
   204
            on Accept {
vb@3825
   205
                send CommitAcceptOfferer;
vb@3516
   206
                do trustThisKey;
vb@3825
   207
                go FormingGroupOfferer;
vb@3516
   208
            }
vb@2831
   209
        }
vb@2831
   210
vb@3825
   211
        state HandshakingPhase2Requester {
vb@3517
   212
            on Cancel {
vb@3517
   213
                send Rollback;
vb@3517
   214
                go Sole;
vb@3517
   215
            }
vb@3517
   216
vb@3517
   217
            on Reject {
vb@3517
   218
                send CommitReject;
vb@3517
   219
                do disable;
vb@3517
   220
                go End;
vb@3517
   221
            }
vb@3517
   222
vb@3517
   223
            on Accept {
vb@3825
   224
                send CommitAcceptRequester;
vb@3517
   225
                do trustThisKey;
vb@3825
   226
                go FormingGroupRequester;
vb@3517
   227
            }
vb@3517
   228
        }
vb@3517
   229
vb@3825
   230
        state FormingGroupOfferer {
vb@3517
   231
            on Init {
vb@3517
   232
                do prepareOwnKeys;
vb@3825
   233
                send OwnKeysOfferer; // we're not grouped yet, this is our own keys
vb@3517
   234
            }
vb@3517
   235
vb@3825
   236
            on OwnKeysRequester {
vb@3520
   237
                do saveGroupKeys;
vb@3815
   238
                do receivedKeysAreDefaultKeys;
vb@3524
   239
                do showGroupCreated;
vb@3517
   240
                go Grouped;
vb@3517
   241
            }
vb@3517
   242
        }
vb@3517
   243
vb@3825
   244
        state FormingGroupRequester {
vb@3390
   245
            on Init {
vb@3390
   246
                do prepareOwnKeys;
vb@3825
   247
                send OwnKeysRequester; // we're not grouped yet, this is our own keys
vb@3390
   248
            }
vb@2831
   249
vb@3825
   250
            on OwnKeysOfferer {
vb@3520
   251
                do saveGroupKeys;
vb@3815
   252
                do ownKeysAreDefaultKeys;
vb@3524
   253
                do showGroupCreated;
vb@2831
   254
                go Grouped;
vb@2831
   255
            }
vb@2831
   256
        }
vb@2831
   257
vb@3518
   258
        state Grouped timeout=off {
vb@3525
   259
            on Init {
vb@3781
   260
                do newChallengeAndNegotiationBase;
vb@3525
   261
                do showBeingInGroup;
vb@3525
   262
            }
vb@3518
   263
vb@3518
   264
            on GroupKeys
vb@3518
   265
                do saveGroupKeys;
vb@3518
   266
vb@3543
   267
            on KeyGen {
vb@3543
   268
                do prepareOwnKeys;
vb@3518
   269
                send GroupKeys;
vb@3543
   270
            }
vb@3518
   271
vb@3518
   272
            on Beacon {
vb@3781
   273
                do openNegotiation;
vb@3518
   274
                do tellWeAreGrouped;
vb@3556
   275
                send NegotiationRequest;
vb@3605
   276
                do useOwnChallenge;
vb@3518
   277
            }
vb@3518
   278
vb@3791
   279
            on NegotiationOpen if sameNegotiationAndPartner {
vb@3791
   280
                do storeNegotiation;
vb@3518
   281
                go HandshakingGrouped;
vb@3791
   282
            }
vb@3524
   283
vb@3524
   284
            on GroupTrustThisKey {
vb@3524
   285
                do trustThisKey;
vb@3524
   286
            }
vb@3518
   287
        }
vb@3518
   288
vb@3519
   289
        // sole device handshaking with group
vb@3825
   290
        state HandshakingToJoin {
vb@2831
   291
            on Init
vb@2831
   292
                do showJoinGroupHandshake;
vb@2831
   293
vb@2831
   294
            // Cancel is Rollback
vb@2831
   295
            on Cancel {
vb@2865
   296
                send Rollback;
vb@2831
   297
                go Sole;
vb@2831
   298
            }
vb@2831
   299
vb@3779
   300
            on Rollback if sameNegotiationAndPartner
vb@2831
   301
                go Sole;
vb@2831
   302
vb@2831
   303
            // Reject is CommitReject
vb@2831
   304
            on Reject {
vb@2865
   305
                send CommitReject;
vb@2831
   306
                do disable;
vb@2831
   307
                go End;
vb@2831
   308
            }
vb@2831
   309
vb@3821
   310
            on CommitAcceptForGroup if sameNegotiationAndPartner
vb@3825
   311
                go HandshakingToJoinPhase2;
vb@3814
   312
vb@3779
   313
            on CommitReject if sameNegotiationAndPartner {
vb@2831
   314
                do disable;
vb@2831
   315
                go End;
vb@2831
   316
            }
vb@2831
   317
vb@2831
   318
            // Accept is Phase1Commit
vb@2831
   319
            on Accept {
vb@3516
   320
                do trustThisKey;
vb@2865
   321
                send CommitAccept;
vb@3825
   322
                go HandshakingToJoinPhase1;
vb@2831
   323
            }
vb@2831
   324
        }
vb@2831
   325
vb@3825
   326
        state HandshakingToJoinPhase1 {
vb@3779
   327
            on Rollback if sameNegotiationAndPartner
vb@2831
   328
                go Sole;
vb@2831
   329
            
vb@3779
   330
            on CommitReject if sameNegotiationAndPartner {
vb@2831
   331
                do disable;
vb@2831
   332
                go End;
vb@2831
   333
            }
vb@2831
   334
vb@3821
   335
            on CommitAcceptForGroup if sameNegotiationAndPartner
vb@3825
   336
                go JoiningGroup;
vb@2831
   337
        }
vb@2831
   338
vb@3825
   339
        state HandshakingToJoinPhase2 {
vb@2831
   340
            on Cancel {
vb@2865
   341
                send Rollback;
vb@2831
   342
                go Sole;
vb@2831
   343
            }
vb@2831
   344
vb@2831
   345
            on Reject {
vb@2865
   346
                send CommitReject;
vb@2831
   347
                do disable;
vb@2831
   348
                go End;
vb@2831
   349
            }
vb@2831
   350
vb@3516
   351
            on Accept {
vb@3516
   352
                do trustThisKey;
vb@3825
   353
                go JoiningGroup;
vb@3516
   354
            }
vb@2831
   355
        }
vb@2831
   356
vb@3825
   357
        state JoiningGroup {
vb@3543
   358
            on Init {
vb@3543
   359
                do prepareOwnKeys;
vb@3572
   360
                send OwnKeys;
vb@3543
   361
            }
vb@3520
   362
vb@3572
   363
            on GroupKeys {
vb@3521
   364
                do saveGroupKeys;
vb@3815
   365
                do receivedKeysAreDefaultKeys;
vb@3524
   366
                do showDeviceAdded;
vb@3509
   367
                go Grouped;
vb@3509
   368
            }
vb@3509
   369
        }
vb@3509
   370
vb@2831
   371
        state HandshakingGrouped {
vb@2831
   372
            on Init
vb@2831
   373
                do showGroupedHandshake;
vb@2831
   374
    
vb@2831
   375
            // Cancel is Rollback
vb@2831
   376
            on Cancel {
vb@2865
   377
                send Rollback;
vb@2831
   378
                go Grouped;
vb@2831
   379
            }
vb@2831
   380
vb@3779
   381
            on Rollback if sameNegotiationAndPartner
vb@2831
   382
                go Grouped;
vb@2831
   383
vb@2831
   384
            // Reject is CommitReject
vb@2831
   385
            on Reject {
vb@2865
   386
                send CommitReject;
vb@2831
   387
                go Grouped;
vb@2831
   388
            }
vb@2831
   389
vb@3779
   390
            on CommitReject if sameNegotiationAndPartner
vb@2831
   391
                go Grouped;
vb@2831
   392
vb@2831
   393
            // Accept is Phase1Commit
vb@2831
   394
            on Accept {
vb@3516
   395
                do trustThisKey;
vb@3523
   396
                send GroupTrustThisKey;
vb@3439
   397
                send CommitAcceptForGroup;
vb@2831
   398
                go HandshakingGroupedPhase1;
vb@2831
   399
            }
vb@2831
   400
vb@3779
   401
            on CommitAccept if sameNegotiationAndPartner
vb@3521
   402
                go HandshakingGroupedPhase2;
vb@2831
   403
vb@3523
   404
            on GroupTrustThisKey {
vb@3523
   405
                do hideHandshakeDialog;
vb@3524
   406
                do trustThisKey;
vb@3524
   407
            }
vb@3524
   408
vb@2831
   409
            on GroupKeys
vb@2831
   410
                do saveGroupKeys;
vb@2831
   411
        }
vb@2831
   412
vb@2831
   413
        state HandshakingGroupedPhase1 {
vb@3779
   414
            on Rollback if sameNegotiationAndPartner
vb@2831
   415
                go Grouped;
vb@2831
   416
vb@3779
   417
            on CommitReject if sameNegotiationAndPartner
vb@2831
   418
                go Grouped;
vb@2831
   419
vb@3779
   420
            on CommitAccept if sameNegotiationAndPartner {
vb@3788
   421
                do prepareOwnKeys;
vb@3550
   422
                send GroupKeys;
vb@2831
   423
                go Grouped;
vb@2831
   424
            }
vb@2831
   425
vb@3524
   426
            on GroupTrustThisKey {
vb@3524
   427
                do trustThisKey;
vb@3524
   428
            }
vb@3524
   429
vb@2831
   430
            on GroupKeys
vb@2831
   431
                do saveGroupKeys;
vb@2831
   432
        }
vb@2831
   433
vb@3521
   434
        state HandshakingGroupedPhase2 {
vb@2831
   435
            on Cancel {
vb@2865
   436
                send Rollback;
vb@2831
   437
                go Grouped;
vb@2831
   438
            }
vb@2831
   439
vb@2831
   440
            on Reject {
vb@2865
   441
                send CommitReject;
vb@2831
   442
                go Grouped;
vb@2831
   443
            }
vb@2831
   444
vb@2831
   445
            on Accept {
vb@3516
   446
                do trustThisKey;
vb@3523
   447
                send GroupTrustThisKey;
vb@3788
   448
                do prepareOwnKeys;
vb@3550
   449
                send GroupKeys;
vb@2831
   450
                go Grouped;
vb@2831
   451
            }
vb@2831
   452
vb@3523
   453
            on GroupTrustThisKey {
vb@3524
   454
                do trustThisKey;
vb@3524
   455
            }
vb@3524
   456
vb@2831
   457
            on GroupKeys
vb@2831
   458
                do saveGroupKeys;
vb@2831
   459
        }
vb@2831
   460
 
vb@3384
   461
        external Accept 129;
vb@3384
   462
        external Reject 130;
vb@3384
   463
        external Cancel 131;
vb@2831
   464
vb@2867
   465
        // beacons are always broadcasted
vb@2867
   466
vb@2876
   467
        message Beacon 2, type=broadcast, security=unencrypted {
vb@2831
   468
            field TID challenge;
vb@2831
   469
            auto Version version;
vb@2831
   470
        }
vb@2831
   471
vb@3556
   472
        message NegotiationRequest 3, security=untrusted {
vb@2831
   473
            field TID challenge;
vb@2831
   474
            auto Version version;
vb@3550
   475
            field TID negotiation;
vb@2831
   476
            field bool is_group;
vb@2831
   477
        }
vb@2831
   478
vb@3556
   479
        message NegotiationOpen 4, security=untrusted {
vb@3379
   480
            auto Version version;
vb@3550
   481
            field TID negotiation;
vb@2831
   482
        }
vb@2831
   483
vb@2876
   484
        message Rollback 5, security=untrusted {
vb@3550
   485
            field TID negotiation;
vb@2831
   486
        }
vb@2831
   487
vb@2876
   488
        message CommitReject 6, security=untrusted {
vb@3550
   489
            field TID negotiation;
vb@2831
   490
        }
vb@2831
   491
vb@3825
   492
        message CommitAcceptOfferer 7, security=untrusted {
vb@3550
   493
            field TID negotiation;
vb@2831
   494
        }
vb@2831
   495
vb@3825
   496
        message CommitAcceptRequester 8, security=untrusted {
vb@3550
   497
            field TID negotiation;
vb@3550
   498
        }
vb@3550
   499
vb@3550
   500
        message CommitAccept 9, security=untrusted {
vb@3550
   501
            field TID negotiation;
vb@3550
   502
        }
vb@3550
   503
vb@3550
   504
        message CommitAcceptForGroup 10, security=untrusted {
vb@3550
   505
            field TID negotiation;
vb@3439
   506
        }
vb@3439
   507
vb@3523
   508
        // default: security=trusted only
vb@3550
   509
        message GroupTrustThisKey 11 {
vb@3523
   510
            field Hash key;
vb@3523
   511
        }
vb@3523
   512
vb@3594
   513
        // trust in future
vb@3550
   514
        message GroupKeys 12, security=attach_own_keys {
vb@3390
   515
            field IdentityList ownIdentities;
vb@2831
   516
        }
vb@2831
   517
vb@3550
   518
        message OwnKeys 13, security=attach_own_keys {
vb@3550
   519
            field IdentityList ownIdentities;
vb@3550
   520
        }
vb@3550
   521
vb@3825
   522
        message OwnKeysOfferer 14, security=attach_own_keys {
vb@3550
   523
            field IdentityList ownIdentities;
vb@3550
   524
        }
vb@3550
   525
vb@3825
   526
        message OwnKeysRequester 15, security=attach_own_keys {
vb@3390
   527
            field IdentityList ownIdentities;
vb@2831
   528
        }
vb@2831
   529
    }
vb@2831
   530
}
vb@2831
   531