sync/sync.fsm
author Volker Birk <vb@pep-project.org>
Fri, 05 Apr 2019 16:56:44 +0200
branchsync
changeset 3439 3333c94c7827
parent 3413 206593c06991
child 3470 3f5f6d631953
permissions -rw-r--r--
moving libetpan dependent code to etpan_mime.c
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@2907
    40
                else {
vb@2902
    41
                    do storeChallenge; // partner's challenge
vb@2902
    42
                    do openTransaction;
vb@2902
    43
                    do storeTransaction;
vb@2902
    44
                    send HandshakeRequest;
vb@2902
    45
                }
vb@2831
    46
            }
vb@2831
    47
vb@2831
    48
            on HandshakeRequest {
vb@2831
    49
                if challengeAccepted {
vb@2831
    50
                    do storeTransaction;
vb@2865
    51
                    send HandshakeAnswer;
vb@2831
    52
                    if partnerIsGrouped
vb@2831
    53
                        go HandshakingWithGroup;
vb@3345
    54
                    go HandshakingNew;
vb@2831
    55
                }
vb@2831
    56
            }
vb@3341
    57
vb@2831
    58
            on HandshakeAnswer
vb@3345
    59
                go HandshakingNew;
vb@2831
    60
        }
vb@2831
    61
vb@2831
    62
        // handshaking without existing Device group
vb@2831
    63
        state HandshakingNew {
vb@3439
    64
            on Init {
vb@3439
    65
                do identifySole;
vb@2831
    66
                do showSoleHandshake;
vb@3439
    67
            }
vb@2831
    68
vb@2831
    69
            // Cancel is Rollback
vb@2831
    70
            on Cancel {
vb@2865
    71
                send Rollback;
vb@2831
    72
                go Sole;
vb@2831
    73
            }
vb@2831
    74
vb@2831
    75
            on Rollback
vb@2831
    76
                go Sole;
vb@2831
    77
vb@2831
    78
            // Reject is CommitReject
vb@2831
    79
            on Reject {
vb@2865
    80
                send CommitReject;
vb@2831
    81
                do disable;
vb@2831
    82
                go End;
vb@2831
    83
            }
vb@2831
    84
vb@2831
    85
            on CommitReject {
vb@2831
    86
                do disable;
vb@2831
    87
                go End;
vb@2831
    88
            }
vb@2831
    89
vb@2831
    90
            // Accept is Phase1Commit
vb@2831
    91
            on Accept {
vb@2865
    92
                send CommitAccept;
vb@2831
    93
                go HandshakingNewPhase1;
vb@2831
    94
            }
vb@2831
    95
vb@3439
    96
            on CommitAccept {
vb@3439
    97
                if notMe
vb@3439
    98
                    go HandshakingNewPhase1Own;
vb@3439
    99
            }
vb@2831
   100
        }
vb@2831
   101
vb@2831
   102
        state HandshakingNewPhase1 {
vb@2831
   103
            on Rollback
vb@2831
   104
                go Sole;
vb@2831
   105
            
vb@2831
   106
            on CommitReject {
vb@2831
   107
                do disable;
vb@2831
   108
                go End;
vb@2831
   109
            }
vb@2831
   110
vb@2831
   111
            on CommitAccept
vb@3439
   112
                if notMe
vb@3439
   113
                    go NewGroup;
vb@2831
   114
        }
vb@2831
   115
vb@2831
   116
        state HandshakingNewPhase1Own {
vb@2831
   117
            on Cancel {
vb@2865
   118
                send Rollback;
vb@2831
   119
                go Sole;
vb@2831
   120
            }
vb@2831
   121
vb@2831
   122
            on Reject {
vb@2865
   123
                send CommitReject;
vb@2831
   124
                do disable;
vb@2831
   125
                go End;
vb@2831
   126
            }
vb@2831
   127
vb@2831
   128
            on Accept
vb@3439
   129
                if notMe
vb@3439
   130
                    go NewGroup;
vb@2831
   131
        }
vb@2831
   132
vb@2831
   133
        state NewGroup {
vb@3390
   134
            on Init {
vb@3390
   135
                do prepareOwnKeys;
vb@2865
   136
                send GroupKeysAndClose; // we're not grouped yet, this is our own keys
vb@3390
   137
            }
vb@2831
   138
vb@2831
   139
            on GroupKeysAndClose {
vb@3406
   140
                if keyElectionWon
vb@2831
   141
                    do ownKeysAreGroupKeys;
vb@3406
   142
                else
vb@3406
   143
                    do receivedKeysAreGroupKeys;
vb@2831
   144
                go Grouped;
vb@2831
   145
            }
vb@2831
   146
        }
vb@2831
   147
vb@2831
   148
        state HandshakingWithGroup {
vb@2831
   149
            on Init
vb@2831
   150
                do showJoinGroupHandshake;
vb@2831
   151
vb@2831
   152
            // Cancel is Rollback
vb@2831
   153
            on Cancel {
vb@2865
   154
                send Rollback;
vb@2831
   155
                go Sole;
vb@2831
   156
            }
vb@2831
   157
vb@2831
   158
            on Rollback
vb@2831
   159
                go Sole;
vb@2831
   160
vb@2831
   161
            // Reject is CommitReject
vb@2831
   162
            on Reject {
vb@2865
   163
                send CommitReject;
vb@2831
   164
                do disable;
vb@2831
   165
                go End;
vb@2831
   166
            }
vb@2831
   167
vb@2831
   168
            on CommitReject {
vb@2831
   169
                do disable;
vb@2831
   170
                go End;
vb@2831
   171
            }
vb@2831
   172
vb@2831
   173
            // Accept is Phase1Commit
vb@2831
   174
            on Accept {
vb@2865
   175
                send CommitAccept;
vb@2831
   176
                go HandshakingJoinPhase1;
vb@2831
   177
            }
vb@2831
   178
vb@3439
   179
            on CommitAcceptForGroup
vb@2831
   180
                go HandshakingJoinPhase1Own;
vb@2831
   181
        }
vb@2831
   182
vb@2831
   183
        state HandshakingJoinPhase1 {
vb@2831
   184
            on Rollback
vb@2831
   185
                go Sole;
vb@2831
   186
            
vb@2831
   187
            on CommitReject {
vb@2831
   188
                do disable;
vb@2831
   189
                go End;
vb@2831
   190
            }
vb@2831
   191
vb@3439
   192
            on CommitAcceptForGroup
vb@2831
   193
                go JoinGroup;
vb@2831
   194
        }
vb@2831
   195
vb@2831
   196
        state HandshakingJoinPhase1Own {
vb@2831
   197
            on Cancel {
vb@2865
   198
                send Rollback;
vb@2831
   199
                go Sole;
vb@2831
   200
            }
vb@2831
   201
vb@2831
   202
            on Reject {
vb@2865
   203
                send CommitReject;
vb@2831
   204
                do disable;
vb@2831
   205
                go End;
vb@2831
   206
            }
vb@2831
   207
vb@2831
   208
            on Accept
vb@2831
   209
                go JoinGroup;
vb@2831
   210
        }
vb@2831
   211
vb@2831
   212
        state JoinGroup {
vb@2831
   213
            on GroupKeysAndClose {
vb@2865
   214
                send GroupKeys; // first send own keys
vb@2831
   215
                do saveGroupKeys; // then store new group keys
vb@2831
   216
                go Grouped;
vb@2831
   217
            }
vb@2831
   218
        }
vb@2831
   219
vb@2908
   220
        state Grouped timeout=off {
vb@2831
   221
            on GroupKeys
vb@2831
   222
                do saveGroupKeys;
vb@2831
   223
vb@2831
   224
            on KeyGen
vb@2865
   225
                send GroupKeys;
vb@2831
   226
vb@2831
   227
            on Beacon
vb@2865
   228
                send HandshakeRequest;
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@2865
   240
                send Rollback;
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@2865
   249
                send CommitReject;
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@3439
   258
                send CommitAcceptForGroup;
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@2865
   277
                send GroupKeysAndClose;
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@2865
   287
                send Rollback;
vb@2831
   288
                go Grouped;
vb@2831
   289
            }
vb@2831
   290
vb@2831
   291
            on Reject {
vb@2865
   292
                send CommitReject;
vb@2831
   293
                go Grouped;
vb@2831
   294
            }
vb@2831
   295
vb@2831
   296
            on Accept {
vb@2865
   297
                send GroupKeysAndClose;
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@3384
   305
        external Accept 129;
vb@3384
   306
        external Reject 130;
vb@3384
   307
        external Cancel 131;
vb@2831
   308
vb@2867
   309
        // beacons are always broadcasted
vb@2867
   310
vb@2876
   311
        message Beacon 2, type=broadcast, security=unencrypted {
vb@2831
   312
            field TID challenge;
vb@2831
   313
            auto Version version;
vb@2831
   314
        }
vb@2831
   315
vb@2876
   316
        message HandshakeRequest 3, security=untrusted {
vb@2831
   317
            field TID challenge;
vb@2831
   318
            auto Version version;
vb@2831
   319
            field TID transaction;
vb@2831
   320
            field bool is_group;
vb@2831
   321
        }
vb@2831
   322
vb@3381
   323
        message HandshakeAnswer 4, security=untrusted {
vb@3379
   324
            auto Version version;
vb@2831
   325
            field TID transaction;
vb@2831
   326
        }
vb@2831
   327
vb@2876
   328
        message Rollback 5, security=untrusted {
vb@2831
   329
            field TID transaction;
vb@2831
   330
        }
vb@2831
   331
vb@2876
   332
        message CommitReject 6, security=untrusted {
vb@2831
   333
            field TID transaction;
vb@2831
   334
        }
vb@2831
   335
vb@2831
   336
        message CommitAccept 7 {
vb@2831
   337
            field TID transaction;
vb@3439
   338
            field TID me;
vb@2831
   339
        }
vb@2831
   340
vb@3439
   341
        message CommitAcceptForGroup 8 {
vb@3439
   342
            field TID transaction;
vb@3439
   343
        }
vb@3439
   344
vb@3439
   345
        message GroupKeysAndClose 9, security=attach_own_keys {
vb@2831
   346
            field TID transaction;
vb@3390
   347
            field IdentityList ownIdentities;
vb@2831
   348
        }
vb@2831
   349
vb@3439
   350
        message GroupKeys 10, security=attach_own_keys {
vb@3390
   351
            field IdentityList ownIdentities;
vb@2831
   352
        }
vb@2831
   353
    }
vb@2831
   354
}
vb@2831
   355