Sync : added group update, on keygen for now
authorEdouard Tisserant <edouard@pep-project.org>
Sat, 15 Oct 2016 17:45:16 +0200
changeset 1281efff88dcdc59
parent 1280 0a442a64830f
child 1283 e5b5143795b5
child 1288 5e3f2a87122a
Sync : added group update, on keygen for now
asn.1/devicegroup.asn1
src/sync_fsm.h
src/sync_impl.c
src/sync_send_actions.c
sync/devicegroup.fsm
sync/gen_actions_skeleton.ysl2
     1.1 --- a/asn.1/devicegroup.asn1	Fri Oct 14 16:55:06 2016 +0200
     1.2 +++ b/asn.1/devicegroup.asn1	Sat Oct 15 17:45:16 2016 +0200
     1.3 @@ -19,6 +19,10 @@
     1.4      ownIdentities IdentityList
     1.5  }
     1.6  
     1.7 +GroupUpdate ::= SEQUENCE {
     1.8 +    ownIdentities IdentityList
     1.9 +}
    1.10 +
    1.11  /* for the tags see end of sync.fsm */
    1.12  
    1.13  DeviceGroup-Protocol ::= SEQUENCE {
    1.14 @@ -34,7 +38,8 @@
    1.15      payload CHOICE {
    1.16          beacon [APPLICATION 2] Beacon,
    1.17          handshakeRequest [APPLICATION 3] HandshakeRequest,
    1.18 -        groupKeys [APPLICATION 4] GroupKeys
    1.19 +        groupKeys [APPLICATION 4] GroupKeys,
    1.20 +        groupUpdate [APPLICATION 5] GroupUpdate
    1.21      }
    1.22  }
    1.23  
     2.1 --- a/src/sync_fsm.h	Fri Oct 14 16:55:06 2016 +0200
     2.2 +++ b/src/sync_fsm.h	Sat Oct 15 17:45:16 2016 +0200
     2.3 @@ -65,7 +65,8 @@
     2.4      HandshakeRejected, 
     2.5      HandshakeAccepted, 
     2.6      Cancel, 
     2.7 -    Reject
     2.8 +    Reject, 
     2.9 +    GroupUpdate
    2.10  } DeviceState_event;
    2.11  
    2.12  // actions
    2.13 @@ -77,6 +78,7 @@
    2.14  PEP_STATUS acceptHandshake(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
    2.15  PEP_STATUS sendGroupKeys(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
    2.16  PEP_STATUS storeGroupKeys(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
    2.17 +PEP_STATUS sendGroupUpdate(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
    2.18  
    2.19  // event injector
    2.20  
     3.1 --- a/src/sync_impl.c	Fri Oct 14 16:55:06 2016 +0200
     3.2 +++ b/src/sync_impl.c	Sat Oct 15 17:45:16 2016 +0200
     3.3 @@ -70,6 +70,7 @@
     3.4                  break;
     3.5  
     3.6              case DeviceGroup_Protocol__payload_PR_groupKeys:
     3.7 +            case DeviceGroup_Protocol__payload_PR_groupUpdate:
     3.8                  partner = Identity_to_Struct(&msg->header.me, NULL);
     3.9                  if (!partner){
    3.10                      status = PEP_OUT_OF_MEMORY;
    3.11 @@ -77,7 +78,11 @@
    3.12                      goto error;
    3.13                  }
    3.14                  identity_list *group_keys = IdentityList_to_identity_list(
    3.15 -                        &msg->payload.choice.groupKeys.ownIdentities, NULL);
    3.16 +                        msg->payload.present == 
    3.17 +                          DeviceGroup_Protocol__payload_PR_groupKeys ?
    3.18 +                        &msg->payload.choice.groupKeys.ownIdentities :
    3.19 +                        &msg->payload.choice.groupUpdate.ownIdentities,
    3.20 +                        NULL);
    3.21                  if (!group_keys) {
    3.22                      free_identity(partner);
    3.23                      status = PEP_OUT_OF_MEMORY;
    3.24 @@ -85,7 +90,9 @@
    3.25                      goto error;
    3.26                  }
    3.27                  extra = (void *) group_keys;
    3.28 -                event = GroupKeys;
    3.29 +                event = msg->payload.present == 
    3.30 +                          DeviceGroup_Protocol__payload_PR_groupKeys ?
    3.31 +                          GroupKeys : GroupUpdate;
    3.32                  break;
    3.33  
    3.34              default:
    3.35 @@ -283,14 +290,15 @@
    3.36                                  goto free_all;
    3.37                              }
    3.38                              break;
    3.39 -                        // accepting GroupKeys needs encryption and trust
    3.40 +                        // accepting GroupKeys and GroupUpdate needs encryption and trust
    3.41                          case DeviceGroup_Protocol__payload_PR_groupKeys:
    3.42 +                        case DeviceGroup_Protocol__payload_PR_groupUpdate:
    3.43                              if (!keylist || rating < PEP_rating_reliable ||
    3.44 -                                // if header.state is HandshakingSole, then
    3.45 -                                // group is just forming in between 2 devices
    3.46 +                                // if group is just forming in between 2 devices
    3.47                                  // message must be addressed to that instance
    3.48                                  // to be consumed
    3.49 -                                (msg->header.state == HandshakingSole && 
    3.50 +                                (msg->payload.present == 
    3.51 +                                  DeviceGroup_Protocol__payload_PR_groupKeys && 
    3.52                                   strncmp(session->sync_uuid,
    3.53                                          (const char *)msg->payload.choice.groupKeys.partner.user_id->buf,
    3.54                                          msg->payload.choice.groupKeys.partner.user_id->size) != 0)){
    3.55 @@ -302,7 +310,8 @@
    3.56                              // pre-existing group, inject message but flag is 
    3.57                              // as discarded to prevent app to delete it, so 
    3.58                              // that other group members can also be updated
    3.59 -                            if(msg->header.state != HandshakingSole){
    3.60 +                            if (msg->payload.present == 
    3.61 +                                  DeviceGroup_Protocol__payload_PR_groupUpdate){
    3.62                                  force_keep_msg = true;
    3.63                              }
    3.64  
    3.65 @@ -504,7 +513,8 @@
    3.66      me = NULL;
    3.67  
    3.68      if (encrypted) {
    3.69 -        if (msg->payload.present == DeviceGroup_Protocol__payload_PR_groupKeys) {
    3.70 +        if (msg->payload.present == DeviceGroup_Protocol__payload_PR_groupKeys || 
    3.71 +            msg->payload.present == DeviceGroup_Protocol__payload_PR_groupUpdate) {
    3.72              PEP_rating rating = PEP_rating_undefined;
    3.73              status = outgoing_message_rating(session, _message, &rating);
    3.74              if (status != PEP_STATUS_OK)
    3.75 @@ -514,7 +524,12 @@
    3.76                  goto error;
    3.77              }
    3.78              
    3.79 -            IdentityList_t *list = &msg->payload.choice.groupKeys.ownIdentities;
    3.80 +            IdentityList_t *list = 
    3.81 +                msg->payload.present == 
    3.82 +                  DeviceGroup_Protocol__payload_PR_groupKeys ?
    3.83 +                  &msg->payload.choice.groupKeys.ownIdentities :
    3.84 +                  &msg->payload.choice.groupUpdate.ownIdentities;
    3.85 +
    3.86              for (int i=0; i<list->list.count; i++) {
    3.87                  Identity_t *ident = list->list.array[i];
    3.88                  char *fpr = strndup((const char *)ident->fpr.buf, ident->fpr.size);
    3.89 @@ -582,25 +597,31 @@
    3.90      if (status != PEP_STATUS_OK)
    3.91          return status;
    3.92  
    3.93 +    printf("BROADCAST \n");
    3.94 +
    3.95      // FIXME: exclude previously rejected identities
    3.96      for (identity_list *_i = own_identities; _i && _i->ident; _i = _i->next) {
    3.97          pEp_identity *me = _i->ident;
    3.98  
    3.99 +        printf("BROADCAST looop \n");
   3.100          // FIXME: no deep copy for multicast supported yet
   3.101 -        DeviceGroup_Protocol_t *_msg = malloc(sizeof(DeviceGroup_Protocol_t));
   3.102 -        assert(_msg);
   3.103 -        if (_msg == NULL)
   3.104 -            goto enomem;
   3.105 -        memcpy(_msg, msg, sizeof(DeviceGroup_Protocol_t));
   3.106 -        status = unicast_msg(session, me, state, _msg, encrypted);
   3.107 -        free_DeviceGroup_Protocol_msg(_msg);
   3.108 +        // DeviceGroup_Protocol_t *_msg = malloc(sizeof(DeviceGroup_Protocol_t));
   3.109 +        // assert(_msg);
   3.110 +        // if (_msg == NULL){
   3.111 +        //     status = PEP_OUT_OF_MEMORY;
   3.112 +        //     goto error;
   3.113 +        // }
   3.114 +        // memcpy(_msg, msg, sizeof(DeviceGroup_Protocol_t));
   3.115 +        status = unicast_msg(session, me, state, msg, encrypted);
   3.116 +        //status = unicast_msg(session, me, state, _msg, encrypted);
   3.117 +        //free_DeviceGroup_Protocol_msg(_msg);
   3.118      }
   3.119  
   3.120      free_identity_list(own_identities);
   3.121      return PEP_STATUS_OK;
   3.122  
   3.123 -enomem:
   3.124 -    free_identity_list(own_identities);
   3.125 -    return PEP_OUT_OF_MEMORY;
   3.126 +// error:
   3.127 +//     free_identity_list(own_identities);
   3.128 +//     return status;
   3.129  }
   3.130  
     4.1 --- a/src/sync_send_actions.c	Fri Oct 14 16:55:06 2016 +0200
     4.2 +++ b/src/sync_send_actions.c	Sat Oct 15 17:45:16 2016 +0200
     4.3 @@ -12,6 +12,7 @@
     4.4  #include "../asn.1/Beacon.h"
     4.5  #include "../asn.1/HandshakeRequest.h"
     4.6  #include "../asn.1/GroupKeys.h"
     4.7 +#include "../asn.1/GroupUpdate.h"
     4.8  
     4.9  
    4.10  // sendBeacon() - send Beacon message
    4.11 @@ -159,3 +160,54 @@
    4.12      return status;
    4.13  }
    4.14  
    4.15 +
    4.16 +// sendGroupUpdate() - send GroupUpdate message
    4.17 +//
    4.18 +//  params:
    4.19 +//      session (in)        session handle
    4.20 +//      state (in)          state the state machine is in
    4.21 +//      partner (in)        (must be NULL)
    4.22 +//
    4.23 +//  returns:
    4.24 +//      PEP_STATUS_OK or any other value on error
    4.25 +
    4.26 +PEP_STATUS sendGroupUpdate(
    4.27 +        PEP_SESSION session,
    4.28 +        DeviceState_state state,
    4.29 +        Identity partner,
    4.30 +        void *extra
    4.31 +    )
    4.32 +{
    4.33 +    assert(session && state);
    4.34 +    if (!(session && state))
    4.35 +        return PEP_ILLEGAL_VALUE;
    4.36 +
    4.37 +    PEP_STATUS status = PEP_STATUS_OK;
    4.38 +    identity_list *kl = new_identity_list(NULL);
    4.39 +
    4.40 +    DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_groupUpdate);
    4.41 +    if (!msg)
    4.42 +        goto enomem;
    4.43 +
    4.44 +    status = own_identities_retrieve(session, &kl);
    4.45 +    if (status != PEP_STATUS_OK)
    4.46 +        goto error;
    4.47 +    if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupUpdate.ownIdentities) == NULL)
    4.48 +        goto enomem;
    4.49 +
    4.50 +    bool encrypted = true;
    4.51 +    status = multicast_self_msg(session, state, msg, encrypted);
    4.52 +    if (status != PEP_STATUS_OK)
    4.53 +        goto error;
    4.54 +
    4.55 +    free_identity_list(kl);
    4.56 +    free_DeviceGroup_Protocol_msg(msg);
    4.57 +    return PEP_STATUS_OK;
    4.58 +
    4.59 +enomem:
    4.60 +    status = PEP_OUT_OF_MEMORY;
    4.61 +error:
    4.62 +    free_DeviceGroup_Protocol_msg(msg);
    4.63 +    return status;
    4.64 +}
    4.65 +
     5.1 --- a/sync/devicegroup.fsm	Fri Oct 14 16:55:06 2016 +0200
     5.2 +++ b/sync/devicegroup.fsm	Sat Oct 15 17:45:16 2016 +0200
     5.3 @@ -10,6 +10,7 @@
     5.4      // all messages have a timestamp, time out and are removed after timeout
     5.5  
     5.6      broadcast sendBeacon;
     5.7 +    broadcast sendGroupUpdate;
     5.8      unencrypted sendBeacon;
     5.9  
    5.10      fsm DeviceState filename=sync {
    5.11 @@ -59,7 +60,6 @@
    5.12          state WaitForGroupKeysSole(Identity partner) {
    5.13              on GroupKeys(Identity partner, Stringlist keys) {
    5.14                  do storeGroupKeys(partner, keys);
    5.15 -                // TODO : add a callback to signal finished waiting for group keys
    5.16                  go Grouped;
    5.17              }
    5.18              on Cancel go Sole;
    5.19 @@ -71,18 +71,15 @@
    5.20  
    5.21          state Grouped {
    5.22              on KeyGen
    5.23 -                do sendGroupKeys; // always send all keys
    5.24 +                do sendGroupUpdate;
    5.25              on Beacon(Identity partner)
    5.26                  do sendHandshakeRequest(partner);
    5.27              on HandshakeRequest(Identity partner) {
    5.28                  do sendHandshakeRequest(partner);
    5.29                  go HandshakingGrouped(partner);
    5.30              }
    5.31 -            on GroupKeys(Identity partner, Stringlist keys) {
    5.32 +            on GroupUpdate(Identity partner, Stringlist keys)
    5.33                  do storeGroupKeys(partner, keys);
    5.34 -                // TODO : add a callback to signal finished waiting for group keys
    5.35 -                go Grouped;
    5.36 -            }
    5.37          }
    5.38  
    5.39          state HandshakingGrouped(Identity partner) {
     6.1 --- a/sync/gen_actions_skeleton.ysl2	Fri Oct 14 16:55:06 2016 +0200
     6.2 +++ b/sync/gen_actions_skeleton.ysl2	Sat Oct 15 17:45:16 2016 +0200
     6.3 @@ -160,19 +160,19 @@
     6.4                  return PEP_ILLEGAL_VALUE;
     6.5  
     6.6              PEP_STATUS status = PEP_STATUS_OK;
     6.7 -            `` if "$name='GroupKeys'" |> identity_list *kl = new_identity_list(NULL);
     6.8 +            `` if "$name='GroupKeys' or $name='GroupUpdate'" |> identity_list *kl = new_identity_list(NULL);
     6.9  
    6.10              DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_«$lname»);
    6.11              if (!msg)
    6.12                  goto enomem;
    6.13          ||
    6.14          choose {
    6.15 -            when "$name='GroupKeys'" {
    6.16 +            when "$name='GroupKeys' or $name='GroupUpdate'" {
    6.17                  |
    6.18                  |> status = own_identities_retrieve(session, &kl);
    6.19                  |> if (status != PEP_STATUS_OK)
    6.20                  |>> goto error;
    6.21 -                |> if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupKeys.ownIdentities) == NULL)
    6.22 +                |> if (IdentityList_from_identity_list(kl, &msg->payload.choice.«$lname».ownIdentities) == NULL)
    6.23                  |>> goto enomem;
    6.24              }
    6.25          }
    6.26 @@ -203,7 +203,7 @@
    6.27              if (status != PEP_STATUS_OK)
    6.28                  goto error;
    6.29  
    6.30 -            `` if "$name='GroupKeys'" |> free_identity_list(kl);
    6.31 +            `` if "$name='GroupKeys' or $name='GroupUpdate'" |> free_identity_list(kl);
    6.32              free_DeviceGroup_Protocol_msg(msg);
    6.33              return PEP_STATUS_OK;
    6.34