sync/devicegroup.fsm
author Edouard Tisserant <edouard@pep-project.org>
Wed, 28 Sep 2016 14:34:36 +0200
branchkeysync
changeset 1216 76e7a6748913
parent 1161 9a1f9ec6561a
child 1236 991afc1aa2a1
permissions -rw-r--r--
Sync : more than two.
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;
vb@1097
    13
    unencrypted sendBeacon;
vb@1047
    14
vb@807
    15
    fsm DeviceState filename=sync {
vb@951
    16
        condition storedGroupKeys();
vb@951
    17
        condition keyElectionWon(Identity partner);
vb@951
    18
vb@624
    19
        state InitState {
vb@624
    20
            on Init {
vb@951
    21
                if storedGroupKeys()
vb@624
    22
                    go Grouped;
vb@624
    23
                go Sole;
vb@624
    24
            }
vb@563
    25
        }
vb@563
    26
vb@563
    27
        state Sole {
vb@944
    28
            on KeyGen // injected by generate_keypair()
vb@1097
    29
                do sendBeacon;
vb@563
    30
            on CannotDecrypt
vb@566
    31
                do sendBeacon;  // cry, baby
Edouard@594
    32
            on Beacon(Identity partner) // this event will not happen for already
vb@571
    33
                                        // rejected partners
vb@582
    34
                do sendHandshakeRequest(partner);
Edouard@594
    35
            on HandshakeRequest(Identity partner) {
vb@582
    36
                do sendHandshakeRequest(partner);
vb@563
    37
                go HandshakingSole(partner);
vb@563
    38
            }
vb@563
    39
        }
vb@563
    40
Edouard@594
    41
        state HandshakingSole(Identity partner) {
vb@582
    42
            on Init
vb@563
    43
                do showHandshake(partner);
Edouard@594
    44
            on HandshakeRejected(Identity partner) {
edouard@1161
    45
                do rejectHandshake(partner);             // stores rejection of partner
vb@563
    46
                go Sole;
vb@563
    47
            }
Edouard@594
    48
            on HandshakeAccepted(Identity partner) {
edouard@1161
    49
                do acceptHandshake(partner); 
vb@567
    50
                if keyElectionWon(partner) {    // an already existing group
vb@567
    51
                                                // always wins
edouard@1161
    52
                    do sendGroupKeys(partner);
vb@563
    53
                    go Grouped;
vb@563
    54
                }
edouard@1216
    55
                go WaitForGroupKeysSole(Identity partner);
vb@563
    56
            }
vb@563
    57
        }
vb@563
    58
    
edouard@1216
    59
        state WaitForGroupKeysSole(Identity partner) {
vb@711
    60
            on GroupKeys(Identity partner, Stringlist keys) {
Edouard@605
    61
                do storeGroupKeys(partner, keys);
edouard@1216
    62
                // TODO : add a callback to signal finished waiting for group keys
vb@563
    63
                go Grouped;
vb@563
    64
            }
vb@568
    65
            on Cancel go Sole;
Edouard@594
    66
            on Reject(Identity partner) {
edouard@1161
    67
                do rejectHandshake(partner);
vb@569
    68
                go Sole;
vb@569
    69
            }
vb@563
    70
        }
vb@563
    71
vb@563
    72
        state Grouped {
vb@563
    73
            on KeyGen
vb@711
    74
                do sendGroupKeys; // always send all keys
edouard@1216
    75
            on Beacon(Identity partner)
edouard@1216
    76
                do sendHandshakeRequest(partner);
Edouard@594
    77
            on HandshakeRequest(Identity partner) {
vb@582
    78
                do sendHandshakeRequest(partner);
edouard@1216
    79
                go HandshakingGrouped(partner);
edouard@1216
    80
            }
edouard@1216
    81
        }
edouard@1216
    82
edouard@1216
    83
        state HandshakingGrouped(Identity partner) {
edouard@1216
    84
            on Init
vb@563
    85
                do showHandshake(partner);
edouard@1216
    86
            on HandshakeRejected(Identity partner) {
edouard@1216
    87
                do rejectHandshake(partner);             // stores rejection of partner
edouard@1216
    88
                go Grouped;
vb@563
    89
            }
edouard@1216
    90
            on HandshakeAccepted(Identity partner) {
edouard@1161
    91
                do acceptHandshake(partner); 
edouard@1216
    92
edouard@1216
    93
                // an already existing group always wins
vb@711
    94
                do sendGroupKeys(partner);
edouard@1216
    95
                go Grouped;
edouard@1161
    96
            }
vb@563
    97
        }
edouard@1216
    98
    
Edouard@613
    99
vb@951
   100
        tag Init 1;
vb@951
   101
        tag Beacon 2;
vb@951
   102
        tag HandshakeRequest 3;
vb@951
   103
        tag GroupKeys 4;
Edouard@613
   104
    }
vb@563
   105
}
vb@563
   106