sync/sync.fsm
author Volker Birk <vb@pep-project.org>
Mon, 10 Jun 2019 12:38:19 +0200
branchsync
changeset 3826 09813a43e17d
parent 3825 b8d55cc4755e
child 3827 3030ae87f2f9
child 3892 e7e371c1ad53
permissions -rw-r--r--
not needed
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@3826
   284
            on GroupTrustThisKey
vb@3524
   285
                do trustThisKey;
vb@3518
   286
        }
vb@3518
   287
vb@3519
   288
        // sole device handshaking with group
vb@3825
   289
        state HandshakingToJoin {
vb@2831
   290
            on Init
vb@2831
   291
                do showJoinGroupHandshake;
vb@2831
   292
vb@2831
   293
            // Cancel is Rollback
vb@2831
   294
            on Cancel {
vb@2865
   295
                send Rollback;
vb@2831
   296
                go Sole;
vb@2831
   297
            }
vb@2831
   298
vb@3779
   299
            on Rollback if sameNegotiationAndPartner
vb@2831
   300
                go Sole;
vb@2831
   301
vb@2831
   302
            // Reject is CommitReject
vb@2831
   303
            on Reject {
vb@2865
   304
                send CommitReject;
vb@2831
   305
                do disable;
vb@2831
   306
                go End;
vb@2831
   307
            }
vb@2831
   308
vb@3821
   309
            on CommitAcceptForGroup if sameNegotiationAndPartner
vb@3825
   310
                go HandshakingToJoinPhase2;
vb@3814
   311
vb@3779
   312
            on CommitReject if sameNegotiationAndPartner {
vb@2831
   313
                do disable;
vb@2831
   314
                go End;
vb@2831
   315
            }
vb@2831
   316
vb@2831
   317
            // Accept is Phase1Commit
vb@2831
   318
            on Accept {
vb@3516
   319
                do trustThisKey;
vb@2865
   320
                send CommitAccept;
vb@3825
   321
                go HandshakingToJoinPhase1;
vb@2831
   322
            }
vb@2831
   323
        }
vb@2831
   324
vb@3825
   325
        state HandshakingToJoinPhase1 {
vb@3779
   326
            on Rollback if sameNegotiationAndPartner
vb@2831
   327
                go Sole;
vb@2831
   328
            
vb@3779
   329
            on CommitReject if sameNegotiationAndPartner {
vb@2831
   330
                do disable;
vb@2831
   331
                go End;
vb@2831
   332
            }
vb@2831
   333
vb@3821
   334
            on CommitAcceptForGroup if sameNegotiationAndPartner
vb@3825
   335
                go JoiningGroup;
vb@2831
   336
        }
vb@2831
   337
vb@3825
   338
        state HandshakingToJoinPhase2 {
vb@2831
   339
            on Cancel {
vb@2865
   340
                send Rollback;
vb@2831
   341
                go Sole;
vb@2831
   342
            }
vb@2831
   343
vb@2831
   344
            on Reject {
vb@2865
   345
                send CommitReject;
vb@2831
   346
                do disable;
vb@2831
   347
                go End;
vb@2831
   348
            }
vb@2831
   349
vb@3516
   350
            on Accept {
vb@3516
   351
                do trustThisKey;
vb@3825
   352
                go JoiningGroup;
vb@3516
   353
            }
vb@2831
   354
        }
vb@2831
   355
vb@3825
   356
        state JoiningGroup {
vb@3572
   357
            on GroupKeys {
vb@3521
   358
                do saveGroupKeys;
vb@3815
   359
                do receivedKeysAreDefaultKeys;
vb@3826
   360
                do prepareOwnKeys;
vb@3826
   361
                send GroupKeys;
vb@3524
   362
                do showDeviceAdded;
vb@3509
   363
                go Grouped;
vb@3509
   364
            }
vb@3509
   365
        }
vb@3509
   366
vb@2831
   367
        state HandshakingGrouped {
vb@2831
   368
            on Init
vb@2831
   369
                do showGroupedHandshake;
vb@2831
   370
    
vb@2831
   371
            // Cancel is Rollback
vb@2831
   372
            on Cancel {
vb@2865
   373
                send Rollback;
vb@2831
   374
                go Grouped;
vb@2831
   375
            }
vb@2831
   376
vb@3779
   377
            on Rollback if sameNegotiationAndPartner
vb@2831
   378
                go Grouped;
vb@2831
   379
vb@2831
   380
            // Reject is CommitReject
vb@2831
   381
            on Reject {
vb@2865
   382
                send CommitReject;
vb@2831
   383
                go Grouped;
vb@2831
   384
            }
vb@2831
   385
vb@3779
   386
            on CommitReject if sameNegotiationAndPartner
vb@2831
   387
                go Grouped;
vb@2831
   388
vb@2831
   389
            // Accept is Phase1Commit
vb@2831
   390
            on Accept {
vb@3516
   391
                do trustThisKey;
vb@3523
   392
                send GroupTrustThisKey;
vb@3439
   393
                send CommitAcceptForGroup;
vb@2831
   394
                go HandshakingGroupedPhase1;
vb@2831
   395
            }
vb@2831
   396
vb@3779
   397
            on CommitAccept if sameNegotiationAndPartner
vb@3521
   398
                go HandshakingGroupedPhase2;
vb@2831
   399
vb@3523
   400
            on GroupTrustThisKey {
vb@3523
   401
                do hideHandshakeDialog;
vb@3524
   402
                do trustThisKey;
vb@3524
   403
            }
vb@3524
   404
vb@2831
   405
            on GroupKeys
vb@2831
   406
                do saveGroupKeys;
vb@2831
   407
        }
vb@2831
   408
vb@2831
   409
        state HandshakingGroupedPhase1 {
vb@3779
   410
            on Rollback if sameNegotiationAndPartner
vb@2831
   411
                go Grouped;
vb@2831
   412
vb@3779
   413
            on CommitReject if sameNegotiationAndPartner
vb@2831
   414
                go Grouped;
vb@2831
   415
vb@3779
   416
            on CommitAccept if sameNegotiationAndPartner {
vb@3788
   417
                do prepareOwnKeys;
vb@3550
   418
                send GroupKeys;
vb@2831
   419
                go Grouped;
vb@2831
   420
            }
vb@2831
   421
vb@3524
   422
            on GroupTrustThisKey {
vb@3524
   423
                do trustThisKey;
vb@3524
   424
            }
vb@3524
   425
vb@2831
   426
            on GroupKeys
vb@2831
   427
                do saveGroupKeys;
vb@2831
   428
        }
vb@2831
   429
vb@3521
   430
        state HandshakingGroupedPhase2 {
vb@2831
   431
            on Cancel {
vb@2865
   432
                send Rollback;
vb@2831
   433
                go Grouped;
vb@2831
   434
            }
vb@2831
   435
vb@2831
   436
            on Reject {
vb@2865
   437
                send CommitReject;
vb@2831
   438
                go Grouped;
vb@2831
   439
            }
vb@2831
   440
vb@2831
   441
            on Accept {
vb@3516
   442
                do trustThisKey;
vb@3523
   443
                send GroupTrustThisKey;
vb@3788
   444
                do prepareOwnKeys;
vb@3550
   445
                send GroupKeys;
vb@2831
   446
                go Grouped;
vb@2831
   447
            }
vb@2831
   448
vb@3523
   449
            on GroupTrustThisKey {
vb@3524
   450
                do trustThisKey;
vb@3524
   451
            }
vb@3524
   452
vb@2831
   453
            on GroupKeys
vb@2831
   454
                do saveGroupKeys;
vb@2831
   455
        }
vb@2831
   456
 
vb@3384
   457
        external Accept 129;
vb@3384
   458
        external Reject 130;
vb@3384
   459
        external Cancel 131;
vb@2831
   460
vb@2867
   461
        // beacons are always broadcasted
vb@2867
   462
vb@2876
   463
        message Beacon 2, type=broadcast, security=unencrypted {
vb@2831
   464
            field TID challenge;
vb@2831
   465
            auto Version version;
vb@2831
   466
        }
vb@2831
   467
vb@3556
   468
        message NegotiationRequest 3, security=untrusted {
vb@2831
   469
            field TID challenge;
vb@2831
   470
            auto Version version;
vb@3550
   471
            field TID negotiation;
vb@2831
   472
            field bool is_group;
vb@2831
   473
        }
vb@2831
   474
vb@3556
   475
        message NegotiationOpen 4, security=untrusted {
vb@3379
   476
            auto Version version;
vb@3550
   477
            field TID negotiation;
vb@2831
   478
        }
vb@2831
   479
vb@2876
   480
        message Rollback 5, security=untrusted {
vb@3550
   481
            field TID negotiation;
vb@2831
   482
        }
vb@2831
   483
vb@2876
   484
        message CommitReject 6, security=untrusted {
vb@3550
   485
            field TID negotiation;
vb@2831
   486
        }
vb@2831
   487
vb@3825
   488
        message CommitAcceptOfferer 7, security=untrusted {
vb@3550
   489
            field TID negotiation;
vb@2831
   490
        }
vb@2831
   491
vb@3825
   492
        message CommitAcceptRequester 8, security=untrusted {
vb@3550
   493
            field TID negotiation;
vb@3550
   494
        }
vb@3550
   495
vb@3550
   496
        message CommitAccept 9, security=untrusted {
vb@3550
   497
            field TID negotiation;
vb@3550
   498
        }
vb@3550
   499
vb@3550
   500
        message CommitAcceptForGroup 10, security=untrusted {
vb@3550
   501
            field TID negotiation;
vb@3439
   502
        }
vb@3439
   503
vb@3523
   504
        // default: security=trusted only
vb@3550
   505
        message GroupTrustThisKey 11 {
vb@3523
   506
            field Hash key;
vb@3523
   507
        }
vb@3523
   508
vb@3594
   509
        // trust in future
vb@3550
   510
        message GroupKeys 12, security=attach_own_keys {
vb@3390
   511
            field IdentityList ownIdentities;
vb@2831
   512
        }
vb@2831
   513
vb@3826
   514
        message OwnKeysOfferer 13, security=attach_own_keys {
vb@3550
   515
            field IdentityList ownIdentities;
vb@3550
   516
        }
vb@3550
   517
vb@3826
   518
        message OwnKeysRequester 14, security=attach_own_keys {
vb@3390
   519
            field IdentityList ownIdentities;
vb@2831
   520
        }
vb@2831
   521
    }
vb@2831
   522
}
vb@2831
   523