sync/devicegroup.fsm
author Edouard Tisserant <edouard@pep-project.org>
Sun, 04 Dec 2016 02:51:59 +0100
branchENGINE-133
changeset 1460 66ceb5a7f718
parent 1459 ab329d7db8d7
child 1477 c0d761ae1fd9
permissions -rw-r--r--
ENGINE-133 added state 'payload' pointer to data attached to the state, allocated/dealocated on transitions. This state payload allows spontaneous transitions having no context (i.e. timeout) to get some context about the state. It can be used in other cases like checking that received group keys are from the partner we expect (TODO)
vb@563
     1
// DeviceGroup protocol for p≡p
vb@563
     2
vb@563
     3
// Copyleft (c) 2016, p≡p foundation
vb@563
     4
vb@563
     5
// Written by Volker Birk
vb@563
     6
vb@563
     7
include ./fsm.yml2
vb@563
     8
vb@563
     9
protocol DeviceGroup {
vb@563
    10
    // all messages have a timestamp, time out and are removed after timeout
vb@563
    11
vb@1047
    12
    broadcast sendBeacon;
edouard@1281
    13
    broadcast sendGroupUpdate;
edouard@1297
    14
    broadcast sendUpdateRequest;
vb@1097
    15
    unencrypted sendBeacon;
vb@1047
    16
vb@807
    17
    fsm DeviceState filename=sync {
vb@951
    18
        condition storedGroupKeys();
vb@951
    19
        condition keyElectionWon(Identity partner);
vb@951
    20
vb@624
    21
        state InitState {
vb@624
    22
            on Init {
vb@951
    23
                if storedGroupKeys()
vb@624
    24
                    go Grouped;
vb@624
    25
                go Sole;
vb@624
    26
            }
vb@563
    27
        }
vb@563
    28
vb@1409
    29
        state Sole end=1 {
vb@944
    30
            on KeyGen // injected by generate_keypair()
vb@1097
    31
                do sendBeacon;
vb@563
    32
            on CannotDecrypt
vb@566
    33
                do sendBeacon;  // cry, baby
Edouard@594
    34
            on Beacon(Identity partner) // this event will not happen for already
vb@571
    35
                                        // rejected partners
vb@582
    36
                do sendHandshakeRequest(partner);
Edouard@594
    37
            on HandshakeRequest(Identity partner) {
vb@582
    38
                do sendHandshakeRequest(partner);
vb@563
    39
                go HandshakingSole(partner);
vb@563
    40
            }
vb@563
    41
        }
vb@563
    42
edouard@1460
    43
        state HandshakingSole timeout=600 (Identity expected) {
vb@582
    44
            on Init
vb@563
    45
                do showHandshake(partner);
Edouard@594
    46
            on HandshakeRejected(Identity partner) {
edouard@1161
    47
                do rejectHandshake(partner);             // stores rejection of partner
vb@563
    48
                go Sole;
vb@563
    49
            }
Edouard@594
    50
            on HandshakeAccepted(Identity partner) {
edouard@1161
    51
                do acceptHandshake(partner); 
vb@567
    52
                if keyElectionWon(partner) {    // an already existing group
vb@567
    53
                                                // always wins
edouard@1161
    54
                    do sendGroupKeys(partner);
edouard@1459
    55
                    do handshakeGroupCreated(partner);
vb@563
    56
                    go Grouped;
vb@563
    57
                }
edouard@1460
    58
                go WaitForGroupKeysSole(partner);
vb@563
    59
            }
edouard@1445
    60
            on Cancel go Sole;
edouard@1445
    61
            on Timeout {
edouard@1460
    62
                do dismissHandshake(expected);
edouard@1445
    63
                go Sole;
edouard@1445
    64
            }
vb@563
    65
        }
vb@563
    66
    
edouard@1460
    67
        state WaitForGroupKeysSole timeout=600 (Identity expected) {
vb@711
    68
            on GroupKeys(Identity partner, Stringlist keys) {
edouard@1460
    69
                // TODO ensure partner == expected
Edouard@605
    70
                do storeGroupKeys(partner, keys);
edouard@1459
    71
                do handshakeSuccess(partner);
vb@563
    72
                go Grouped;
vb@563
    73
            }
edouard@1445
    74
            on Timeout {
edouard@1460
    75
                do handshakeFailure(expected);
vb@569
    76
                go Sole;
vb@569
    77
            }
vb@563
    78
        }
vb@563
    79
vb@1409
    80
        state Grouped end=1 {
edouard@1297
    81
            on Init 
edouard@1297
    82
                do enterGroup;
vb@563
    83
            on KeyGen
edouard@1281
    84
                do sendGroupUpdate;
edouard@1297
    85
            on CannotDecrypt
edouard@1297
    86
                do sendUpdateRequest; // TODO: narrow request to missing key
edouard@1297
    87
            on UpdateRequest
edouard@1297
    88
                do sendGroupUpdate;
edouard@1216
    89
            on Beacon(Identity partner)
edouard@1216
    90
                do sendHandshakeRequest(partner);
Edouard@594
    91
            on HandshakeRequest(Identity partner) {
vb@582
    92
                do sendHandshakeRequest(partner);
edouard@1216
    93
                go HandshakingGrouped(partner);
edouard@1216
    94
            }
edouard@1281
    95
            on GroupUpdate(Identity partner, Stringlist keys)
edouard@1236
    96
                do storeGroupKeys(partner, keys);
edouard@1216
    97
        }
edouard@1216
    98
edouard@1460
    99
        state HandshakingGrouped timeout=600 (Identity expected) {
edouard@1216
   100
            on Init
vb@563
   101
                do showHandshake(partner);
edouard@1216
   102
            on HandshakeRejected(Identity partner) {
edouard@1216
   103
                do rejectHandshake(partner);             // stores rejection of partner
edouard@1216
   104
                go Grouped;
vb@563
   105
            }
edouard@1216
   106
            on HandshakeAccepted(Identity partner) {
edouard@1161
   107
                do acceptHandshake(partner); 
edouard@1216
   108
edouard@1216
   109
                // an already existing group always wins
vb@711
   110
                do sendGroupKeys(partner);
edouard@1445
   111
edouard@1459
   112
                do handshakeDeviceAdded(partner);
edouard@1445
   113
edouard@1445
   114
                go Grouped;
edouard@1445
   115
            }
edouard@1445
   116
            on Timeout {
edouard@1460
   117
                do handshakeFailure(expected);
edouard@1216
   118
                go Grouped;
edouard@1161
   119
            }
vb@563
   120
        }
Edouard@613
   121
vb@951
   122
        tag Init 1;
vb@951
   123
        tag Beacon 2;
vb@951
   124
        tag HandshakeRequest 3;
vb@951
   125
        tag GroupKeys 4;
Edouard@613
   126
    }
vb@563
   127
}
vb@563
   128