sync/devicegroup.fsm
author Edouard Tisserant <edouard@pep-project.org>
Thu, 05 Jan 2017 13:05:00 +0100
branchENGINE-135
changeset 1523 bc4a9401af51
parent 1513 e7f7e42385b5
child 1555 7ffa6cf9d054
permissions -rw-r--r--
ENGINE-135 added *Beaconed states to avoid sending handshake requests twice
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 {
vb@951
    21
        condition storedGroupKeys();
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 {
vb@951
    27
                if storedGroupKeys()
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@1477
    80
Edouard@594
    81
            on HandshakeRejected(Identity partner) {
edouard@1523
    82
                do rejectHandshake(partner);
vb@563
    83
                go Sole;
vb@563
    84
            }
Edouard@594
    85
            on HandshakeAccepted(Identity partner) {
edouard@1161
    86
                do acceptHandshake(partner); 
edouard@1523
    87
                if keyElectionWon(partner) {
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@1445
    97
                go Sole;
edouard@1445
    98
            }
vb@563
    99
        }
vb@563
   100
    
edouard@1460
   101
        state WaitForGroupKeysSole timeout=600 (Identity expected) {
vb@711
   102
            on GroupKeys(Identity partner, Stringlist keys) {
edouard@1523
   103
                if sameIdentities(partner, expected) {
edouard@1523
   104
                    do storeGroupKeys(partner, keys);
edouard@1523
   105
                    do notifyAcceptedDeviceAdded(partner);
edouard@1523
   106
                    go Grouped;
edouard@1523
   107
                }
vb@563
   108
            }
edouard@1445
   109
            on Timeout {
edouard@1477
   110
                do notifyTimeout(expected);
vb@569
   111
                go Sole;
vb@569
   112
            }
vb@563
   113
        }
vb@563
   114
vb@1409
   115
        state Grouped end=1 {
edouard@1297
   116
            on Init 
edouard@1297
   117
                do enterGroup;
vb@563
   118
            on KeyGen
edouard@1281
   119
                do sendGroupUpdate;
edouard@1297
   120
            on CannotDecrypt
edouard@1523
   121
                do sendUpdateRequest;
edouard@1297
   122
            on UpdateRequest
edouard@1297
   123
                do sendGroupUpdate;
edouard@1523
   124
            on Beacon(Identity partner){
edouard@1216
   125
                do sendHandshakeRequest(partner);
edouard@1523
   126
                go GroupedBeaconed(partner);
edouard@1523
   127
            }
Edouard@594
   128
            on HandshakeRequest(Identity partner) {
vb@582
   129
                do sendHandshakeRequest(partner);
edouard@1216
   130
                go HandshakingGrouped(partner);
edouard@1216
   131
            }
edouard@1281
   132
            on GroupUpdate(Identity partner, Stringlist keys)
edouard@1236
   133
                do storeGroupKeys(partner, keys);
edouard@1216
   134
        }
edouard@1216
   135
edouard@1523
   136
        state GroupedBeaconed timeout=600 (Identity expected){
edouard@1523
   137
            on KeyGen
edouard@1523
   138
                do sendGroupUpdate;
edouard@1523
   139
            on CannotDecrypt
edouard@1523
   140
                do sendUpdateRequest;
edouard@1523
   141
            on UpdateRequest
edouard@1523
   142
                do sendGroupUpdate;
edouard@1523
   143
            on Beacon(Identity partner){
edouard@1523
   144
                do sendHandshakeRequest(partner);
edouard@1523
   145
                go GroupedBeaconed(partner);
edouard@1523
   146
            }
edouard@1523
   147
            on HandshakeRequest(Identity partner) {
edouard@1523
   148
                if sameIdentities(partner, expected) {
edouard@1523
   149
                    // do nothing, to avoid sending handshake request twice 
edouard@1523
   150
                } else {
edouard@1523
   151
                    do sendHandshakeRequest(partner);
edouard@1523
   152
                }
edouard@1523
   153
                go HandshakingGrouped(partner);
edouard@1523
   154
            }
edouard@1523
   155
            on GroupUpdate(Identity partner, Stringlist keys)
edouard@1523
   156
                do storeGroupKeys(partner, keys);
edouard@1523
   157
            on Timeout go Grouped;
edouard@1523
   158
        }
edouard@1523
   159
edouard@1460
   160
        state HandshakingGrouped timeout=600 (Identity expected) {
edouard@1216
   161
            on Init
edouard@1477
   162
                do notifyInitAddOurDevice(partner);
edouard@1216
   163
            on HandshakeRejected(Identity partner) {
edouard@1216
   164
                do rejectHandshake(partner);             // stores rejection of partner
edouard@1216
   165
                go Grouped;
vb@563
   166
            }
edouard@1216
   167
            on HandshakeAccepted(Identity partner) {
edouard@1161
   168
                do acceptHandshake(partner); 
edouard@1216
   169
edouard@1216
   170
                // an already existing group always wins
vb@711
   171
                do sendGroupKeys(partner);
edouard@1445
   172
edouard@1445
   173
                go Grouped;
edouard@1445
   174
            }
edouard@1445
   175
            on Timeout {
edouard@1477
   176
                do notifyTimeout(expected);
edouard@1216
   177
                go Grouped;
edouard@1161
   178
            }
vb@563
   179
        }
Edouard@613
   180
vb@951
   181
        tag Init 1;
vb@951
   182
        tag Beacon 2;
vb@951
   183
        tag HandshakeRequest 3;
vb@951
   184
        tag GroupKeys 4;
Edouard@613
   185
    }
vb@563
   186
}
vb@563
   187