sync/devicegroup.fsm
author Edouard Tisserant <edouard@pep-project.org>
Thu, 16 Feb 2017 12:43:35 +0100
branchGroupMerge
changeset 1586 599deda50386
parent 1574 12b7a45140d3
child 1588 1a43a7eddf90
permissions -rw-r--r--
KeySync: added device group merge. Builds but still untested.
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
vb@563
     4
// DeviceGroup protocol for p≡p
vb@563
     5
vb@563
     6
// Copyleft (c) 2016, p≡p foundation
vb@563
     7
vb@563
     8
// Written by Volker Birk
vb@563
     9
vb@563
    10
include ./fsm.yml2
vb@563
    11
vb@563
    12
protocol DeviceGroup {
vb@563
    13
    // all messages have a timestamp, time out and are removed after timeout
vb@563
    14
vb@1047
    15
    broadcast sendBeacon;
edouard@1281
    16
    broadcast sendGroupUpdate;
edouard@1297
    17
    broadcast sendUpdateRequest;
vb@1097
    18
    unencrypted sendBeacon;
vb@1047
    19
vb@807
    20
    fsm DeviceState filename=sync {
edouard@1574
    21
        condition deviceGrouped();
vb@951
    22
        condition keyElectionWon(Identity partner);
edouard@1523
    23
        condition sameIdentities(Identity a, Identity b);
vb@951
    24
vb@624
    25
        state InitState {
vb@624
    26
            on Init {
edouard@1574
    27
                if deviceGrouped()
vb@624
    28
                    go Grouped;
vb@624
    29
                go Sole;
vb@624
    30
            }
vb@563
    31
        }
vb@563
    32
vb@1409
    33
        state Sole end=1 {
edouard@1523
    34
            on KeyGen
vb@1097
    35
                do sendBeacon;
vb@563
    36
            on CannotDecrypt
edouard@1523
    37
                do sendBeacon;
edouard@1523
    38
            on Beacon(Identity partner){
vb@582
    39
                do sendHandshakeRequest(partner);
edouard@1523
    40
                go SoleBeaconed(partner);
edouard@1523
    41
            }
Edouard@594
    42
            on HandshakeRequest(Identity partner) {
vb@582
    43
                do sendHandshakeRequest(partner);
vb@563
    44
                go HandshakingSole(partner);
vb@563
    45
            }
vb@563
    46
        }
vb@563
    47
edouard@1523
    48
        state SoleBeaconed timeout=600 (Identity expected) {
edouard@1523
    49
            on KeyGen{
edouard@1523
    50
                do sendBeacon;
edouard@1523
    51
                go Sole;
edouard@1523
    52
            }
edouard@1523
    53
            on CannotDecrypt{
edouard@1523
    54
                do sendBeacon;
edouard@1523
    55
                go Sole;
edouard@1523
    56
            }
edouard@1523
    57
            on Beacon(Identity partner) {
edouard@1523
    58
                do sendHandshakeRequest(partner);
edouard@1523
    59
                go SoleBeaconed(partner);
edouard@1523
    60
            }
edouard@1523
    61
            on HandshakeRequest(Identity partner) {
edouard@1523
    62
                if sameIdentities(partner, expected) {
edouard@1523
    63
                    // do nothing, to avoid sending handshake request twice 
edouard@1523
    64
                } else {
edouard@1523
    65
                    do sendHandshakeRequest(partner);
edouard@1523
    66
                }
edouard@1523
    67
                go HandshakingSole(partner);
edouard@1523
    68
            }
edouard@1523
    69
            on Timeout go Sole;
edouard@1523
    70
        }
edouard@1523
    71
edouard@1460
    72
        state HandshakingSole timeout=600 (Identity expected) {
edouard@1477
    73
            on Init{
edouard@1523
    74
                if keyElectionWon(partner) {
edouard@1477
    75
                    do notifyInitFormGroup(partner);
edouard@1477
    76
                } else {
edouard@1477
    77
                    do notifyInitAddOurDevice(partner);
edouard@1477
    78
                }
edouard@1477
    79
            }
Edouard@594
    80
            on HandshakeRejected(Identity partner) {
edouard@1523
    81
                do rejectHandshake(partner);
vb@563
    82
                go Sole;
vb@563
    83
            }
Edouard@594
    84
            on HandshakeAccepted(Identity partner) {
edouard@1161
    85
                do acceptHandshake(partner); 
edouard@1523
    86
                if keyElectionWon(partner) {
edouard@1586
    87
                    do makeGroup;
edouard@1161
    88
                    do sendGroupKeys(partner);
edouard@1477
    89
                    do notifyAcceptedGroupCreated(partner);
vb@563
    90
                    go Grouped;
vb@563
    91
                }
edouard@1460
    92
                go WaitForGroupKeysSole(partner);
vb@563
    93
            }
edouard@1445
    94
            on Cancel go Sole;
edouard@1445
    95
            on Timeout {
edouard@1477
    96
                do notifyTimeout(expected);
edouard@1555
    97
                do sendBeacon;
edouard@1445
    98
                go Sole;
edouard@1445
    99
            }
vb@563
   100
        }
vb@563
   101
    
edouard@1460
   102
        state WaitForGroupKeysSole timeout=600 (Identity expected) {
vb@711
   103
            on GroupKeys(Identity partner, Stringlist keys) {
edouard@1523
   104
                if sameIdentities(partner, expected) {
edouard@1523
   105
                    do storeGroupKeys(partner, keys);
edouard@1566
   106
                    do sendGroupUpdate;
edouard@1523
   107
                    do notifyAcceptedDeviceAdded(partner);
edouard@1523
   108
                    go Grouped;
edouard@1523
   109
                }
vb@563
   110
            }
edouard@1445
   111
            on Timeout {
edouard@1477
   112
                do notifyTimeout(expected);
vb@569
   113
                go Sole;
vb@569
   114
            }
vb@563
   115
        }
vb@563
   116
vb@1409
   117
        state Grouped end=1 {
vb@563
   118
            on KeyGen
edouard@1281
   119
                do sendGroupUpdate;
edouard@1586
   120
            on CannotDecrypt {
edouard@1523
   121
                do sendUpdateRequest;
edouard@1586
   122
                do sendBeacon;
edouard@1586
   123
            }
edouard@1297
   124
            on UpdateRequest
edouard@1297
   125
                do sendGroupUpdate;
edouard@1523
   126
            on Beacon(Identity partner){
edouard@1216
   127
                do sendHandshakeRequest(partner);
edouard@1523
   128
                go GroupedBeaconed(partner);
edouard@1523
   129
            }
Edouard@594
   130
            on HandshakeRequest(Identity partner) {
vb@582
   131
                do sendHandshakeRequest(partner);
edouard@1216
   132
                go HandshakingGrouped(partner);
edouard@1216
   133
            }
edouard@1281
   134
            on GroupUpdate(Identity partner, Stringlist keys)
edouard@1586
   135
                do storeGroupUpdate(partner, keys);
edouard@1216
   136
        }
edouard@1216
   137
edouard@1523
   138
        state GroupedBeaconed timeout=600 (Identity expected){
edouard@1523
   139
            on KeyGen
edouard@1523
   140
                do sendGroupUpdate;
edouard@1586
   141
            on CannotDecrypt {
edouard@1523
   142
                do sendUpdateRequest;
edouard@1586
   143
                do sendBeacon;
edouard@1586
   144
            }
edouard@1523
   145
            on UpdateRequest
edouard@1523
   146
                do sendGroupUpdate;
edouard@1523
   147
            on Beacon(Identity partner){
edouard@1523
   148
                do sendHandshakeRequest(partner);
edouard@1523
   149
                go GroupedBeaconed(partner);
edouard@1523
   150
            }
edouard@1523
   151
            on HandshakeRequest(Identity partner) {
edouard@1523
   152
                if sameIdentities(partner, expected) {
edouard@1523
   153
                    // do nothing, to avoid sending handshake request twice 
edouard@1523
   154
                } else {
edouard@1523
   155
                    do sendHandshakeRequest(partner);
edouard@1523
   156
                }
edouard@1523
   157
                go HandshakingGrouped(partner);
edouard@1523
   158
            }
edouard@1523
   159
            on GroupUpdate(Identity partner, Stringlist keys)
edouard@1523
   160
                do storeGroupKeys(partner, keys);
edouard@1523
   161
            on Timeout go Grouped;
edouard@1523
   162
        }
edouard@1523
   163
edouard@1460
   164
        state HandshakingGrouped timeout=600 (Identity expected) {
edouard@1586
   165
            // HandshakeRequest from same group are filtered in receive_sync_msg
edouard@1586
   166
            on Init{
edouard@1586
   167
                if keyElectionWon(partner) {
edouard@1586
   168
                    do notifyInitAddOtherDevice(partner);
edouard@1586
   169
                } else {
edouard@1586
   170
                    do notifyInitMoveOurDevice(partner);
edouard@1586
   171
                }
edouard@1586
   172
            }
edouard@1216
   173
            on HandshakeRejected(Identity partner) {
edouard@1216
   174
                do rejectHandshake(partner);             // stores rejection of partner
edouard@1216
   175
                go Grouped;
vb@563
   176
            }
edouard@1216
   177
            on HandshakeAccepted(Identity partner) {
edouard@1161
   178
                do acceptHandshake(partner); 
edouard@1586
   179
                if keyElectionWon(partner) {
edouard@1586
   180
                    do sendGroupKeys(partner);
edouard@1586
   181
                    do notifyAcceptedDeviceAdded(partner);
edouard@1586
   182
                    go Grouped;
edouard@1586
   183
                }
edouard@1586
   184
                go WaitForGroupKeysGrouped(partner);
edouard@1586
   185
            }
edouard@1586
   186
            on Cancel go Grouped;
edouard@1586
   187
            on Timeout {
edouard@1586
   188
                do notifyTimeout(expected);
edouard@1445
   189
                go Grouped;
edouard@1445
   190
            }
edouard@1586
   191
        }
edouard@1586
   192
edouard@1586
   193
        state WaitForGroupKeysGrouped timeout=600 (Identity expected) {
edouard@1586
   194
            on GroupKeys(Identity partner, Stringlist keys) {
edouard@1586
   195
                if sameIdentities(partner, expected) {
edouard@1586
   196
                    do storeGroupKeys(partner, keys);
edouard@1586
   197
                    do sendGroupUpdate;
edouard@1586
   198
                    do notifyAcceptedDeviceMoved(partner);
edouard@1586
   199
                    go Grouped;
edouard@1586
   200
                }
edouard@1586
   201
            }
edouard@1445
   202
            on Timeout {
edouard@1477
   203
                do notifyTimeout(expected);
edouard@1216
   204
                go Grouped;
edouard@1161
   205
            }
vb@563
   206
        }
Edouard@613
   207
vb@951
   208
        tag Init 1;
vb@951
   209
        tag Beacon 2;
vb@951
   210
        tag HandshakeRequest 3;
vb@951
   211
        tag GroupKeys 4;
Edouard@613
   212
    }
vb@563
   213
}
vb@563
   214