sync/sync.fsm
author Neal H. Walfield <neal@pep.foundation>
Wed, 08 May 2019 12:20:37 +0200
branchemail_comparison
changeset 3649 1dd837adc30b
parent 3605 27595b58666d
child 3647 1295702dbeaf
permissions -rw-r--r--
Fix public key.
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
vb@3569
    13
    fsm KeySync 1, threshold=30 {
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@3605
    26
                do closeTransaction;
vb@3605
    27
                do newChallenge;
vb@3525
    28
                do showBeingSole;
vb@2865
    29
                send Beacon;
vb@2831
    30
            }
vb@2831
    31
vb@3601
    32
            on KeyGen {
vb@2865
    33
                send Beacon;
vb@3601
    34
            }
vb@2831
    35
vb@3601
    36
            on CannotDecrypt { // cry baby
vb@2865
    37
                send Beacon;
vb@3601
    38
            }
vb@2831
    39
vb@2831
    40
            on Beacon {
vb@3514
    41
                if sameChallenge {
vb@3591
    42
                    // this is our own Beacon; ignore
vb@2907
    43
                }
vb@3514
    44
                else {
vb@3514
    45
                    if weAreFirst {
vb@3514
    46
                        send Beacon;
vb@3514
    47
                    }
vb@3514
    48
                    else /* we are second */ {
vb@3591
    49
                        do newTransaction;
vb@3518
    50
                        do tellWeAreNotGrouped;
vb@3556
    51
                        // second is sending NegotiationRequest
vb@3605
    52
                        do replyChallenge; // partner's challenge
vb@3556
    53
                        send NegotiationRequest;
vb@3605
    54
                        do useOwnChallenge;
vb@3514
    55
                    }
vb@2902
    56
                }
vb@2831
    57
            }
vb@2831
    58
vb@3601
    59
            on NegotiationRequest {
vb@3601
    60
                if challengeAccepted {
vb@3601
    61
                    if sameTransaction {
vb@3601
    62
                        // this is our own NegotiationRequest; ignore
vb@3601
    63
                    }
vb@3601
    64
                    else {
vb@3601
    65
                        // first is receiving NegotiationRequest
vb@3601
    66
                        do storeTransaction;
vb@3601
    67
                        // first is sending NegotiationOpen
vb@3601
    68
                        send NegotiationOpen;
vb@3601
    69
                        if partnerIsGrouped
vb@3601
    70
                            go HandshakingWithGroup;
vb@3601
    71
                        go HandshakingNewFirst;
vb@3601
    72
                    }
vb@2831
    73
                }
vb@2831
    74
            }
vb@3341
    75
vb@3591
    76
            on NegotiationOpen if sameTransactionAndPartner {
vb@3556
    77
                // second is receiving NegotiationOpen
vb@3510
    78
                go HandshakingNewSecond;
vb@3509
    79
            }
vb@2831
    80
        }
vb@2831
    81
vb@2831
    82
        // handshaking without existing Device group
vb@3509
    83
        state HandshakingNewFirst {
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@3591
    93
            on Rollback if sameTransactionAndPartner
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@3591
   103
            on CommitReject if sameTransactionAndPartner {
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@3550
   111
                send CommitAcceptFirst;
vb@3509
   112
                go HandshakingNewPhase1First;
vb@3509
   113
            }
vb@3509
   114
vb@3509
   115
            // got a CommitAccept from second
vb@3591
   116
            on CommitAcceptSecond if sameTransactionAndPartner
vb@3517
   117
                go HandshakingNewPhase2First;
vb@3509
   118
        }
vb@3509
   119
vb@3509
   120
        // handshaking without existing Device group
vb@3509
   121
        state HandshakingNewSecond {
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@3591
   131
            on Rollback if sameTransactionAndPartner
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@3591
   141
            on CommitReject if sameTransactionAndPartner {
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@3550
   149
                send CommitAcceptSecond;
vb@3509
   150
                go HandshakingNewPhase1Second;
vb@3509
   151
            }
vb@3509
   152
vb@3509
   153
            // got a CommitAccept from first
vb@3591
   154
            on CommitAcceptFirst if sameTransactionAndPartner
vb@3517
   155
                go HandshakingNewPhase2Second;
vb@3509
   156
        }
vb@3509
   157
vb@3509
   158
        state HandshakingNewPhase1First {
vb@3591
   159
            on Rollback if sameTransactionAndPartner {
vb@3524
   160
                do untrustThisKey;
vb@3509
   161
                go Sole;
vb@3524
   162
            }
vb@3509
   163
            
vb@3591
   164
            on CommitReject if sameTransactionAndPartner {
vb@3524
   165
                do untrustThisKey;
vb@3509
   166
                do disable;
vb@3509
   167
                go End;
vb@2831
   168
            }
vb@2831
   169
vb@3591
   170
            on CommitAcceptSecond if sameTransactionAndPartner {
vb@3517
   171
                go NewGroupFirst;
vb@3550
   172
            }
vb@2831
   173
        }
vb@2831
   174
vb@3509
   175
        state HandshakingNewPhase1Second {
vb@3591
   176
            on Rollback if sameTransactionAndPartner {
vb@3524
   177
                do untrustThisKey;
vb@2831
   178
                go Sole;
vb@3524
   179
            }
vb@2831
   180
            
vb@3591
   181
            on CommitReject if sameTransactionAndPartner {
vb@3524
   182
                do untrustThisKey;
vb@2831
   183
                do disable;
vb@2831
   184
                go End;
vb@2831
   185
            }
vb@2831
   186
vb@3591
   187
            on CommitAcceptFirst if sameTransactionAndPartner {
vb@3517
   188
                go NewGroupSecond;
vb@3550
   189
            }
vb@2831
   190
        }
vb@2831
   191
vb@3517
   192
        state HandshakingNewPhase2First {
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@3550
   205
                send CommitAcceptFirst;
vb@3516
   206
                do trustThisKey;
vb@3517
   207
                go NewGroupFirst;
vb@3516
   208
            }
vb@2831
   209
        }
vb@2831
   210
vb@3517
   211
        state HandshakingNewPhase2Second {
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@3550
   224
                send CommitAcceptSecond;
vb@3517
   225
                do trustThisKey;
vb@3517
   226
                go NewGroupSecond;
vb@3517
   227
            }
vb@3517
   228
        }
vb@3517
   229
vb@3517
   230
        state NewGroupFirst {
vb@3517
   231
            on Init {
vb@3517
   232
                do prepareOwnKeys;
vb@3550
   233
                send OwnKeysFirst; // we're not grouped yet, this is our own keys
vb@3517
   234
            }
vb@3517
   235
vb@3550
   236
            on OwnKeysSecond {
vb@3520
   237
                do saveGroupKeys;
vb@3520
   238
vb@3517
   239
                if keyElectionWon
vb@3517
   240
                    do ownKeysAreGroupKeys;
vb@3517
   241
                else
vb@3517
   242
                    do receivedKeysAreGroupKeys;
vb@3524
   243
                do showGroupCreated;
vb@3517
   244
                go Grouped;
vb@3517
   245
            }
vb@3517
   246
        }
vb@3517
   247
vb@3517
   248
        state NewGroupSecond {
vb@3390
   249
            on Init {
vb@3390
   250
                do prepareOwnKeys;
vb@3550
   251
                send OwnKeysSecond; // we're not grouped yet, this is our own keys
vb@3390
   252
            }
vb@2831
   253
vb@3550
   254
            on OwnKeysFirst {
vb@3520
   255
                do saveGroupKeys;
vb@3520
   256
vb@3406
   257
                if keyElectionWon
vb@2831
   258
                    do ownKeysAreGroupKeys;
vb@3406
   259
                else
vb@3406
   260
                    do receivedKeysAreGroupKeys;
vb@3524
   261
                do showGroupCreated;
vb@2831
   262
                go Grouped;
vb@2831
   263
            }
vb@2831
   264
        }
vb@2831
   265
vb@3518
   266
        state Grouped timeout=off {
vb@3525
   267
            on Init {
vb@3605
   268
                do closeTransaction;
vb@3605
   269
                do newChallenge;
vb@3525
   270
                do showBeingInGroup;
vb@3525
   271
            }
vb@3518
   272
vb@3518
   273
            on GroupKeys
vb@3518
   274
                do saveGroupKeys;
vb@3518
   275
vb@3543
   276
            on KeyGen {
vb@3543
   277
                do prepareOwnKeys;
vb@3518
   278
                send GroupKeys;
vb@3543
   279
            }
vb@3518
   280
vb@3518
   281
            on Beacon {
vb@3591
   282
                do newTransaction;
vb@3518
   283
                do tellWeAreGrouped;
vb@3605
   284
                do replyChallenge; // partner's challenge
vb@3556
   285
                send NegotiationRequest;
vb@3605
   286
                do useOwnChallenge;
vb@3518
   287
            }
vb@3518
   288
vb@3591
   289
            on NegotiationOpen if sameTransactionAndPartner
vb@3518
   290
                go HandshakingGrouped;
vb@3524
   291
vb@3524
   292
            on GroupTrustThisKey {
vb@3524
   293
                do trustThisKey;
vb@3524
   294
            }
vb@3518
   295
        }
vb@3518
   296
vb@3519
   297
        // sole device handshaking with group
vb@2831
   298
        state HandshakingWithGroup {
vb@2831
   299
            on Init
vb@2831
   300
                do showJoinGroupHandshake;
vb@2831
   301
vb@2831
   302
            // Cancel is Rollback
vb@2831
   303
            on Cancel {
vb@2865
   304
                send Rollback;
vb@2831
   305
                go Sole;
vb@2831
   306
            }
vb@2831
   307
vb@3591
   308
            on Rollback if sameTransactionAndPartner
vb@2831
   309
                go Sole;
vb@2831
   310
vb@2831
   311
            // Reject is CommitReject
vb@2831
   312
            on Reject {
vb@2865
   313
                send CommitReject;
vb@2831
   314
                do disable;
vb@2831
   315
                go End;
vb@2831
   316
            }
vb@2831
   317
vb@3591
   318
            on CommitReject if sameTransactionAndPartner {
vb@2831
   319
                do disable;
vb@2831
   320
                go End;
vb@2831
   321
            }
vb@2831
   322
vb@2831
   323
            // Accept is Phase1Commit
vb@2831
   324
            on Accept {
vb@3516
   325
                do trustThisKey;
vb@2865
   326
                send CommitAccept;
vb@2831
   327
                go HandshakingJoinPhase1;
vb@2831
   328
            }
vb@2831
   329
vb@3591
   330
            on CommitAcceptForGroup if sameTransactionAndPartner
vb@3519
   331
                go HandshakingJoinPhase2;
vb@2831
   332
        }
vb@2831
   333
vb@2831
   334
        state HandshakingJoinPhase1 {
vb@3591
   335
            on Rollback if sameTransactionAndPartner
vb@2831
   336
                go Sole;
vb@2831
   337
            
vb@3591
   338
            on CommitReject if sameTransactionAndPartner {
vb@2831
   339
                do disable;
vb@2831
   340
                go End;
vb@2831
   341
            }
vb@2831
   342
vb@3591
   343
            on CommitAcceptForGroup if sameTransactionAndPartner {
vb@2831
   344
                go JoinGroup;
vb@3550
   345
            }
vb@2831
   346
        }
vb@2831
   347
vb@3519
   348
        state HandshakingJoinPhase2 {
vb@2831
   349
            on Cancel {
vb@2865
   350
                send Rollback;
vb@2831
   351
                go Sole;
vb@2831
   352
            }
vb@2831
   353
vb@2831
   354
            on Reject {
vb@2865
   355
                send CommitReject;
vb@2831
   356
                do disable;
vb@2831
   357
                go End;
vb@2831
   358
            }
vb@2831
   359
vb@3516
   360
            on Accept {
vb@3516
   361
                do trustThisKey;
vb@3520
   362
                go JoinGroup;
vb@3516
   363
            }
vb@2831
   364
        }
vb@2831
   365
vb@2831
   366
        state JoinGroup {
vb@3543
   367
            on Init {
vb@3543
   368
                do prepareOwnKeys;
vb@3572
   369
                send OwnKeys;
vb@3543
   370
            }
vb@3520
   371
vb@3572
   372
            on GroupKeys {
vb@3521
   373
                do saveGroupKeys;
vb@3520
   374
                do receivedKeysAreGroupKeys;
vb@3524
   375
                do showDeviceAdded;
vb@3509
   376
                go Grouped;
vb@3509
   377
            }
vb@3509
   378
        }
vb@3509
   379
vb@2831
   380
        state HandshakingGrouped {
vb@2831
   381
            on Init
vb@2831
   382
                do showGroupedHandshake;
vb@2831
   383
    
vb@2831
   384
            // Cancel is Rollback
vb@2831
   385
            on Cancel {
vb@2865
   386
                send Rollback;
vb@2831
   387
                go Grouped;
vb@2831
   388
            }
vb@2831
   389
vb@3591
   390
            on Rollback if sameTransactionAndPartner
vb@2831
   391
                go Grouped;
vb@2831
   392
vb@2831
   393
            // Reject is CommitReject
vb@2831
   394
            on Reject {
vb@2865
   395
                send CommitReject;
vb@2831
   396
                go Grouped;
vb@2831
   397
            }
vb@2831
   398
vb@3591
   399
            on CommitReject if sameTransactionAndPartner
vb@2831
   400
                go Grouped;
vb@2831
   401
vb@2831
   402
            // Accept is Phase1Commit
vb@2831
   403
            on Accept {
vb@3516
   404
                do trustThisKey;
vb@3523
   405
                send GroupTrustThisKey;
vb@3439
   406
                send CommitAcceptForGroup;
vb@2831
   407
                go HandshakingGroupedPhase1;
vb@2831
   408
            }
vb@2831
   409
vb@3591
   410
            on CommitAccept if sameTransactionAndPartner
vb@3521
   411
                go HandshakingGroupedPhase2;
vb@2831
   412
vb@3523
   413
            on GroupTrustThisKey {
vb@3523
   414
                do hideHandshakeDialog;
vb@3524
   415
                do trustThisKey;
vb@3524
   416
            }
vb@3524
   417
vb@3524
   418
            on CommitAcceptForGroup {
vb@3524
   419
                do showDeviceAdded;
vb@3591
   420
                if sameTransactionAndPartner {
vb@3524
   421
                    do hideHandshakeDialog;
vb@3524
   422
                    go Grouped;
vb@3524
   423
                }
vb@3523
   424
            }
vb@3523
   425
vb@2831
   426
            on GroupKeys
vb@2831
   427
                do saveGroupKeys;
vb@2831
   428
        }
vb@2831
   429
vb@2831
   430
        state HandshakingGroupedPhase1 {
vb@3591
   431
            on Rollback if sameTransactionAndPartner
vb@2831
   432
                go Grouped;
vb@2831
   433
vb@3591
   434
            on CommitReject if sameTransactionAndPartner
vb@2831
   435
                go Grouped;
vb@2831
   436
vb@3591
   437
            on CommitAccept if sameTransactionAndPartner {
vb@3550
   438
                send GroupKeys;
vb@2831
   439
                go Grouped;
vb@2831
   440
            }
vb@2831
   441
vb@3524
   442
            on GroupTrustThisKey {
vb@3524
   443
                do trustThisKey;
vb@3524
   444
            }
vb@3524
   445
vb@3524
   446
            on CommitAcceptForGroup {
vb@3524
   447
                do showDeviceAdded;
vb@3591
   448
                if sameTransactionAndPartner
vb@3524
   449
                    go Grouped;
vb@3524
   450
            }
vb@3523
   451
vb@2831
   452
            on GroupKeys
vb@2831
   453
                do saveGroupKeys;
vb@2831
   454
        }
vb@2831
   455
vb@3521
   456
        state HandshakingGroupedPhase2 {
vb@2831
   457
            on Cancel {
vb@2865
   458
                send Rollback;
vb@2831
   459
                go Grouped;
vb@2831
   460
            }
vb@2831
   461
vb@2831
   462
            on Reject {
vb@2865
   463
                send CommitReject;
vb@2831
   464
                go Grouped;
vb@2831
   465
            }
vb@2831
   466
vb@2831
   467
            on Accept {
vb@3516
   468
                do trustThisKey;
vb@3523
   469
                send GroupTrustThisKey;
vb@3550
   470
                send GroupKeys;
vb@2831
   471
                go Grouped;
vb@2831
   472
            }
vb@2831
   473
vb@3523
   474
            on GroupTrustThisKey {
vb@3524
   475
                do trustThisKey;
vb@3524
   476
            }
vb@3524
   477
vb@3524
   478
            on CommitAcceptForGroup {
vb@3524
   479
                do showDeviceAdded;
vb@3591
   480
                if sameTransactionAndPartner {
vb@3524
   481
                    do hideHandshakeDialog;
vb@3524
   482
                    go Grouped;
vb@3524
   483
                }
vb@3523
   484
            }
vb@3523
   485
vb@2831
   486
            on GroupKeys
vb@2831
   487
                do saveGroupKeys;
vb@2831
   488
        }
vb@2831
   489
 
vb@3384
   490
        external Accept 129;
vb@3384
   491
        external Reject 130;
vb@3384
   492
        external Cancel 131;
vb@2831
   493
vb@2867
   494
        // beacons are always broadcasted
vb@2867
   495
vb@2876
   496
        message Beacon 2, type=broadcast, security=unencrypted {
vb@2831
   497
            field TID challenge;
vb@2831
   498
            auto Version version;
vb@2831
   499
        }
vb@2831
   500
vb@3556
   501
        message NegotiationRequest 3, security=untrusted {
vb@2831
   502
            field TID challenge;
vb@2831
   503
            auto Version version;
vb@3550
   504
            field TID negotiation;
vb@2831
   505
            field bool is_group;
vb@2831
   506
        }
vb@2831
   507
vb@3556
   508
        message NegotiationOpen 4, security=untrusted {
vb@3379
   509
            auto Version version;
vb@3550
   510
            field TID negotiation;
vb@2831
   511
        }
vb@2831
   512
vb@2876
   513
        message Rollback 5, security=untrusted {
vb@3550
   514
            field TID negotiation;
vb@2831
   515
        }
vb@2831
   516
vb@2876
   517
        message CommitReject 6, security=untrusted {
vb@3550
   518
            field TID negotiation;
vb@2831
   519
        }
vb@2831
   520
vb@3550
   521
        message CommitAcceptFirst 7, security=untrusted {
vb@3550
   522
            field TID negotiation;
vb@2831
   523
        }
vb@2831
   524
vb@3550
   525
        message CommitAcceptSecond 8, security=untrusted {
vb@3550
   526
            field TID negotiation;
vb@3550
   527
        }
vb@3550
   528
vb@3550
   529
        message CommitAccept 9, security=untrusted {
vb@3550
   530
            field TID negotiation;
vb@3550
   531
        }
vb@3550
   532
vb@3550
   533
        message CommitAcceptForGroup 10, security=untrusted {
vb@3550
   534
            field TID negotiation;
vb@3439
   535
        }
vb@3439
   536
vb@3523
   537
        // default: security=trusted only
vb@3550
   538
        message GroupTrustThisKey 11 {
vb@3523
   539
            field Hash key;
vb@3523
   540
        }
vb@3523
   541
vb@3594
   542
        // trust in future
vb@3550
   543
        message GroupKeys 12, security=attach_own_keys {
vb@3390
   544
            field IdentityList ownIdentities;
vb@2831
   545
        }
vb@2831
   546
vb@3550
   547
        message OwnKeys 13, security=attach_own_keys {
vb@3550
   548
            field IdentityList ownIdentities;
vb@3550
   549
        }
vb@3550
   550
vb@3550
   551
        message OwnKeysFirst 14, security=attach_own_keys {
vb@3550
   552
            field IdentityList ownIdentities;
vb@3550
   553
        }
vb@3550
   554
vb@3550
   555
        message OwnKeysSecond 15, security=attach_own_keys {
vb@3390
   556
            field IdentityList ownIdentities;
vb@2831
   557
        }
vb@2831
   558
    }
vb@2831
   559
}
vb@2831
   560