sync/sync.fsm
author Volker Birk <vb@pep.foundation>
Tue, 21 Aug 2018 20:28:07 +0200
branchsync
changeset 2865 96aaa55b2a54
parent 2856 451ac4260963
child 2867 3e1e674fee7d
permissions -rw-r--r--
reworking send message
vb@2831
     1
// This file is under BSD License 2.0
vb@2831
     2
vb@2831
     3
// Sync protocol for p≡p
vb@2840
     4
// Copyright (c) 2016 - 2018, 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
    unencrypted sendBeacon;
vb@2831
    14
    acceptUnencrypted Beacon;
vb@2831
    15
vb@2831
    16
    acceptUntrusted HandshakeRequest;
vb@2831
    17
    acceptUntrusted Rollback;
vb@2831
    18
    acceptUntrusted CommitReject;
vb@2831
    19
vb@2831
    20
    fsm KeySync 1 {
vb@2831
    21
        version 1, 2;
vb@2831
    22
        state InitState {
vb@2831
    23
            on Init {
vb@2831
    24
                if deviceGrouped
vb@2831
    25
                    go Grouped;
vb@2831
    26
                go Sole;
vb@2831
    27
            }
vb@2831
    28
        }
vb@2831
    29
vb@2831
    30
        state Sole {
vb@2831
    31
            on Init {
vb@2831
    32
                do closeHandshakeDialog;
vb@2856
    33
                do clearState;
vb@2831
    34
                do openChallenge; // own challenge
vb@2865
    35
                send Beacon;
vb@2831
    36
            }
vb@2831
    37
vb@2831
    38
            on KeyGen
vb@2865
    39
                send Beacon;
vb@2831
    40
vb@2831
    41
            on CannotDecrypt // cry baby
vb@2865
    42
                send Beacon;
vb@2831
    43
vb@2831
    44
            on Beacon {
vb@2831
    45
                do storeChallenge; // partner's challenge
vb@2831
    46
                do openTransaction;
vb@2831
    47
                do storeTransaction;
vb@2865
    48
                send HandshakeRequest;
vb@2831
    49
            }
vb@2831
    50
vb@2831
    51
            on HandshakeRequest {
vb@2831
    52
                if challengeAccepted {
vb@2831
    53
                    do storeTransaction;
vb@2865
    54
                    send HandshakeAnswer;
vb@2831
    55
                    if partnerIsGrouped
vb@2831
    56
                        go HandshakingWithGroup;
vb@2831
    57
vb@2831
    58
                    go HandshakingNew();
vb@2831
    59
                }
vb@2831
    60
            }
vb@2831
    61
vb@2831
    62
            on HandshakeAnswer
vb@2831
    63
                go HandshakingNew();
vb@2831
    64
        }
vb@2831
    65
vb@2831
    66
        // handshaking without existing Device group
vb@2831
    67
        state HandshakingNew {
vb@2831
    68
            on Init
vb@2831
    69
                do showSoleHandshake;
vb@2831
    70
vb@2831
    71
            // Cancel is Rollback
vb@2831
    72
            on Cancel {
vb@2865
    73
                send Rollback;
vb@2831
    74
                go Sole;
vb@2831
    75
            }
vb@2831
    76
vb@2831
    77
            on Rollback
vb@2831
    78
                go Sole;
vb@2831
    79
vb@2831
    80
            // Reject is CommitReject
vb@2831
    81
            on Reject {
vb@2865
    82
                send CommitReject;
vb@2831
    83
                do disable;
vb@2831
    84
                go End;
vb@2831
    85
            }
vb@2831
    86
vb@2831
    87
            on CommitReject {
vb@2831
    88
                do disable;
vb@2831
    89
                go End;
vb@2831
    90
            }
vb@2831
    91
vb@2831
    92
            // Accept is Phase1Commit
vb@2831
    93
            on Accept {
vb@2865
    94
                send CommitAccept;
vb@2831
    95
                go HandshakingNewPhase1;
vb@2831
    96
            }
vb@2831
    97
vb@2831
    98
            on CommitAccept
vb@2831
    99
                go HandshakingNewPhase1Own;
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@2831
   112
                go NewGroup;
vb@2831
   113
        }
vb@2831
   114
vb@2831
   115
        state HandshakingNewPhase1Own {
vb@2831
   116
            on Cancel {
vb@2865
   117
                send Rollback;
vb@2831
   118
                go Sole;
vb@2831
   119
            }
vb@2831
   120
vb@2831
   121
            on Reject {
vb@2865
   122
                send CommitReject;
vb@2831
   123
                do disable;
vb@2831
   124
                go End;
vb@2831
   125
            }
vb@2831
   126
vb@2831
   127
            on Accept
vb@2831
   128
                go NewGroup;
vb@2831
   129
        }
vb@2831
   130
vb@2831
   131
        state NewGroup {
vb@2831
   132
            on Init
vb@2865
   133
                send GroupKeysAndClose; // we're not grouped yet, this is our own keys
vb@2831
   134
vb@2831
   135
            on GroupKeysAndClose {
vb@2831
   136
                do saveGroupKeys;
vb@2831
   137
                if keyElectionWon {
vb@2831
   138
                    do ownKeysAreGroupKeys;
vb@2831
   139
                    go Grouped;
vb@2831
   140
                }
vb@2831
   141
                // do receivedKeysAreGroupKeys; -- implicit
vb@2831
   142
                go Grouped;
vb@2831
   143
            }
vb@2831
   144
        }
vb@2831
   145
vb@2831
   146
        state HandshakingWithGroup {
vb@2831
   147
            on Init
vb@2831
   148
                do showJoinGroupHandshake;
vb@2831
   149
vb@2831
   150
            // Cancel is Rollback
vb@2831
   151
            on Cancel {
vb@2865
   152
                send Rollback;
vb@2831
   153
                go Sole;
vb@2831
   154
            }
vb@2831
   155
vb@2831
   156
            on Rollback
vb@2831
   157
                go Sole;
vb@2831
   158
vb@2831
   159
            // Reject is CommitReject
vb@2831
   160
            on Reject {
vb@2865
   161
                send CommitReject;
vb@2831
   162
                do disable;
vb@2831
   163
                go End;
vb@2831
   164
            }
vb@2831
   165
vb@2831
   166
            on CommitReject {
vb@2831
   167
                do disable;
vb@2831
   168
                go End;
vb@2831
   169
            }
vb@2831
   170
vb@2831
   171
            // Accept is Phase1Commit
vb@2831
   172
            on Accept {
vb@2865
   173
                send CommitAccept;
vb@2831
   174
                go HandshakingJoinPhase1;
vb@2831
   175
            }
vb@2831
   176
vb@2831
   177
            on CommitAccept
vb@2831
   178
                go HandshakingJoinPhase1Own;
vb@2831
   179
        }
vb@2831
   180
vb@2831
   181
        state HandshakingJoinPhase1 {
vb@2831
   182
            on Rollback
vb@2831
   183
                go Sole;
vb@2831
   184
            
vb@2831
   185
            on CommitReject {
vb@2831
   186
                do disable;
vb@2831
   187
                go End;
vb@2831
   188
            }
vb@2831
   189
vb@2831
   190
            on CommitAccept
vb@2831
   191
                go JoinGroup;
vb@2831
   192
        }
vb@2831
   193
vb@2831
   194
        state HandshakingJoinPhase1Own {
vb@2831
   195
            on Cancel {
vb@2865
   196
                send Rollback;
vb@2831
   197
                go Sole;
vb@2831
   198
            }
vb@2831
   199
vb@2831
   200
            on Reject {
vb@2865
   201
                send CommitReject;
vb@2831
   202
                do disable;
vb@2831
   203
                go End;
vb@2831
   204
            }
vb@2831
   205
vb@2831
   206
            on Accept
vb@2831
   207
                go JoinGroup;
vb@2831
   208
        }
vb@2831
   209
vb@2831
   210
        state JoinGroup {
vb@2831
   211
            on GroupKeysAndClose {
vb@2865
   212
                send GroupKeys; // first send own keys
vb@2831
   213
                do saveGroupKeys; // then store new group keys
vb@2831
   214
                go Grouped;
vb@2831
   215
            }
vb@2831
   216
        }
vb@2831
   217
vb@2831
   218
        state Grouped {
vb@2856
   219
            on Init {
vb@2831
   220
                do closeHandshakeDialog;
vb@2856
   221
                do clearState;
vb@2856
   222
            }
vb@2831
   223
vb@2831
   224
            on GroupKeys
vb@2831
   225
                do saveGroupKeys;
vb@2831
   226
vb@2831
   227
            on KeyGen
vb@2865
   228
                send GroupKeys;
vb@2831
   229
vb@2831
   230
            on Beacon
vb@2865
   231
                send HandshakeRequest;
vb@2831
   232
vb@2831
   233
            on HandshakeAnswer
vb@2831
   234
                go HandshakingGrouped;
vb@2831
   235
        }
vb@2831
   236
vb@2831
   237
        state HandshakingGrouped {
vb@2831
   238
            on Init
vb@2831
   239
                do showGroupedHandshake;
vb@2831
   240
    
vb@2831
   241
            // Cancel is Rollback
vb@2831
   242
            on Cancel {
vb@2865
   243
                send Rollback;
vb@2831
   244
                go Grouped;
vb@2831
   245
            }
vb@2831
   246
vb@2831
   247
            on Rollback
vb@2831
   248
                go Grouped;
vb@2831
   249
vb@2831
   250
            // Reject is CommitReject
vb@2831
   251
            on Reject {
vb@2865
   252
                send CommitReject;
vb@2831
   253
                go Grouped;
vb@2831
   254
            }
vb@2831
   255
vb@2831
   256
            on CommitReject
vb@2831
   257
                go Grouped;
vb@2831
   258
vb@2831
   259
            // Accept is Phase1Commit
vb@2831
   260
            on Accept {
vb@2865
   261
                send CommitAccept;
vb@2831
   262
                go HandshakingGroupedPhase1;
vb@2831
   263
            }
vb@2831
   264
vb@2831
   265
            on CommitAccept
vb@2831
   266
                go HandshakingGroupedPhase1Own;
vb@2831
   267
vb@2831
   268
            on GroupKeys
vb@2831
   269
                do saveGroupKeys;
vb@2831
   270
        }
vb@2831
   271
vb@2831
   272
        state HandshakingGroupedPhase1 {
vb@2831
   273
            on Rollback
vb@2831
   274
                go Grouped;
vb@2831
   275
vb@2831
   276
            on CommitReject
vb@2831
   277
                go Grouped;
vb@2831
   278
vb@2831
   279
            on CommitAccept {
vb@2865
   280
                send GroupKeysAndClose;
vb@2831
   281
                go Grouped;
vb@2831
   282
            }
vb@2831
   283
vb@2831
   284
            on GroupKeys
vb@2831
   285
                do saveGroupKeys;
vb@2831
   286
        }
vb@2831
   287
vb@2831
   288
        state HandshakingGroupedPhase1Own {
vb@2831
   289
            on Cancel {
vb@2865
   290
                send Rollback;
vb@2831
   291
                go Grouped;
vb@2831
   292
            }
vb@2831
   293
vb@2831
   294
            on Reject {
vb@2865
   295
                send CommitReject;
vb@2831
   296
                go Grouped;
vb@2831
   297
            }
vb@2831
   298
vb@2831
   299
            on Accept {
vb@2865
   300
                send GroupKeysAndClose;
vb@2831
   301
                go Grouped;
vb@2831
   302
            }
vb@2831
   303
vb@2831
   304
            on GroupKeys
vb@2831
   305
                do saveGroupKeys;
vb@2831
   306
        }
vb@2831
   307
 
vb@2831
   308
        external Accept 30;
vb@2831
   309
        external Reject 31;
vb@2831
   310
        external Cancel 32;
vb@2831
   311
vb@2831
   312
        message Beacon 2 {
vb@2831
   313
            field TID challenge;
vb@2831
   314
            auto Version version;
vb@2831
   315
        }
vb@2831
   316
vb@2831
   317
        message HandshakeRequest 3 {
vb@2831
   318
            field TID challenge;
vb@2831
   319
            auto Version version;
vb@2831
   320
            field TID transaction;
vb@2831
   321
            field Hash fpr;
vb@2831
   322
            field bool is_group;
vb@2831
   323
        }
vb@2831
   324
vb@2831
   325
        message HandshakeAnswer 4 {
vb@2831
   326
            field TID transaction;
vb@2831
   327
            field Hash fpr;
vb@2831
   328
        }
vb@2831
   329
vb@2831
   330
        message Rollback 5 {
vb@2831
   331
            field TID transaction;
vb@2831
   332
        }
vb@2831
   333
vb@2831
   334
        message CommitReject 6 {
vb@2831
   335
            field TID transaction;
vb@2831
   336
        }
vb@2831
   337
vb@2831
   338
        message CommitAccept 7 {
vb@2831
   339
            field TID transaction;
vb@2831
   340
        }
vb@2831
   341
vb@2831
   342
        message GroupKeysAndClose 8 {
vb@2831
   343
            field TID transaction;
vb@2831
   344
            field IdentityList identities;
vb@2831
   345
        }
vb@2831
   346
vb@2831
   347
        message GroupKeys 9 {
vb@2831
   348
            field IdentityList identities;
vb@2831
   349
        }
vb@2831
   350
    }
vb@2831
   351
}
vb@2831
   352