Sync : implementation of group update on missing key. Needs fixing, doesn't pass dedicated test
authorEdouard Tisserant <edouard@pep-project.org>
Tue, 18 Oct 2016 00:57:56 +0200
changeset 129778f5f9894fbd
parent 1291 e17f94b55b0c
child 1298 56a653a59ea1
Sync : implementation of group update on missing key. Needs fixing, doesn't pass dedicated test
asn.1/devicegroup.asn1
src/sync_actions.c
src/sync_fsm.h
src/sync_impl.c
src/sync_send_actions.c
sync/devicegroup.fsm
     1.1 --- a/asn.1/devicegroup.asn1	Mon Oct 17 23:42:47 2016 +0200
     1.2 +++ b/asn.1/devicegroup.asn1	Tue Oct 18 00:57:56 2016 +0200
     1.3 @@ -23,6 +23,9 @@
     1.4      ownIdentities IdentityList
     1.5  }
     1.6  
     1.7 +/* TODO: narrow request to single key */
     1.8 +UpdateRequest ::= NULL 
     1.9 +
    1.10  /* for the tags see end of sync.fsm */
    1.11  
    1.12  DeviceGroup-Protocol ::= SEQUENCE {
    1.13 @@ -39,7 +42,8 @@
    1.14          beacon [APPLICATION 2] Beacon,
    1.15          handshakeRequest [APPLICATION 3] HandshakeRequest,
    1.16          groupKeys [APPLICATION 4] GroupKeys,
    1.17 -        groupUpdate [APPLICATION 5] GroupUpdate
    1.18 +        groupUpdate [APPLICATION 5] GroupUpdate,
    1.19 +        updateRequest [APPLICATION 6] UpdateRequest
    1.20      }
    1.21  }
    1.22  
     2.1 --- a/src/sync_actions.c	Mon Oct 17 23:42:47 2016 +0200
     2.2 +++ b/src/sync_actions.c	Tue Oct 18 00:57:56 2016 +0200
     2.3 @@ -255,6 +255,7 @@
     2.4      }
     2.5  
     2.6      free_identity_list(group_keys);
     2.7 +    
     2.8      return status;
     2.9  
    2.10  enomem:
    2.11 @@ -263,3 +264,30 @@
    2.12      return status;
    2.13  }
    2.14  
    2.15 +// enterGroup() - 
    2.16 +//
    2.17 +//  params:
    2.18 +//      session (in)        session handle
    2.19 +//      state (in)          state the state machine is in
    2.20 +//      partner (in)        ignored
    2.21 +//      extra (in)          ignored
    2.22 +//
    2.23 +//  returns:
    2.24 +//      PEP_STATUS_OK or any other value on error
    2.25 +
    2.26 +PEP_STATUS enterGroup(
    2.27 +        PEP_SESSION session,
    2.28 +        DeviceState_state state,
    2.29 +        Identity partner,
    2.30 +        void *extra
    2.31 +    )
    2.32 +{
    2.33 +    PEP_STATUS status = PEP_STATUS_OK;
    2.34 +
    2.35 +    assert(session);
    2.36 +
    2.37 +    // groups have no uuid for now
    2.38 +    status = set_device_group(session, "1");
    2.39 +    
    2.40 +    return status;
    2.41 +}
     3.1 --- a/src/sync_fsm.h	Mon Oct 17 23:42:47 2016 +0200
     3.2 +++ b/src/sync_fsm.h	Tue Oct 18 00:57:56 2016 +0200
     3.3 @@ -66,6 +66,7 @@
     3.4      HandshakeAccepted, 
     3.5      Cancel, 
     3.6      Reject, 
     3.7 +    UpdateRequest, 
     3.8      GroupUpdate
     3.9  } DeviceState_event;
    3.10  
    3.11 @@ -78,7 +79,9 @@
    3.12  PEP_STATUS acceptHandshake(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
    3.13  PEP_STATUS sendGroupKeys(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
    3.14  PEP_STATUS storeGroupKeys(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
    3.15 +PEP_STATUS enterGroup(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
    3.16  PEP_STATUS sendGroupUpdate(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
    3.17 +PEP_STATUS sendUpdateRequest(PEP_SESSION session, DeviceState_state state, Identity partner, void *extra);
    3.18  
    3.19  // event injector
    3.20  
     4.1 --- a/src/sync_impl.c	Mon Oct 17 23:42:47 2016 +0200
     4.2 +++ b/src/sync_impl.c	Tue Oct 18 00:57:56 2016 +0200
     4.3 @@ -80,8 +80,8 @@
     4.4                  identity_list *group_keys = IdentityList_to_identity_list(
     4.5                          msg->payload.present == 
     4.6                            DeviceGroup_Protocol__payload_PR_groupKeys ?
     4.7 -                        &msg->payload.choice.groupKeys.ownIdentities :
     4.8 -                        &msg->payload.choice.groupUpdate.ownIdentities,
     4.9 +                            &msg->payload.choice.groupKeys.ownIdentities :
    4.10 +                            &msg->payload.choice.groupUpdate.ownIdentities,
    4.11                          NULL);
    4.12                  if (!group_keys) {
    4.13                      free_identity(partner);
    4.14 @@ -92,7 +92,7 @@
    4.15                  extra = (void *) group_keys;
    4.16                  event = msg->payload.present == 
    4.17                            DeviceGroup_Protocol__payload_PR_groupKeys ?
    4.18 -                          GroupKeys : GroupUpdate;
    4.19 +                            GroupKeys : GroupUpdate;
    4.20                  break;
    4.21  
    4.22              default:
    4.23 @@ -290,49 +290,70 @@
    4.24                                  goto free_all;
    4.25                              }
    4.26                              break;
    4.27 -                        // accepting GroupKeys and GroupUpdate needs encryption and trust
    4.28 +                        // accepting GroupKeys needs encryption and trust of peer device
    4.29                          case DeviceGroup_Protocol__payload_PR_groupKeys:
    4.30 -                        case DeviceGroup_Protocol__payload_PR_groupUpdate:
    4.31 +                        {
    4.32                              if (!keylist || rating < PEP_rating_reliable ||
    4.33 -                                // if group is just forming in between 2 devices
    4.34 -                                // message must be addressed to that instance
    4.35 -                                // to be consumed
    4.36 -                                (msg->payload.present == 
    4.37 -                                  DeviceGroup_Protocol__payload_PR_groupKeys && 
    4.38 -                                 strncmp(session->sync_uuid,
    4.39 +                                // message is only consumed by instance it is addressed to
    4.40 +                                (strncmp(session->sync_uuid,
    4.41                                          (const char *)msg->payload.choice.groupKeys.partner.user_id->buf,
    4.42                                          msg->payload.choice.groupKeys.partner.user_id->size) != 0)){
    4.43                                  discarded = true;
    4.44                                  goto free_all;
    4.45                              }
    4.46  
    4.47 -                            // otherwise, when group keys are sent from a 
    4.48 -                            // pre-existing group, inject message but flag is 
    4.49 -                            // as discarded to prevent app to delete it, so 
    4.50 -                            // that other group members can also be updated
    4.51 -                            if (msg->payload.present == 
    4.52 -                                  DeviceGroup_Protocol__payload_PR_groupUpdate){
    4.53 -                                force_keep_msg = true;
    4.54 -                            }
    4.55 -
    4.56 -                            // check trust of identity with the right user_id
    4.57 -                            pEp_identity *_from = new_identity(src->from->address, 
    4.58 +                            // check trust of identity using user_id given in payload
    4.59 +                            // to exacly match identity of device, the one trusted in
    4.60 +                            // case of accepted handshake
    4.61 +                            pEp_identity *_from = new_identity(NULL, 
    4.62                                                                 keylist->value,
    4.63                                                                 user_id,
    4.64 -                                                               src->from->username);
    4.65 +                                                               NULL);
    4.66                              if (_from == NULL){
    4.67                                  status = PEP_OUT_OF_MEMORY;
    4.68                                  goto free_all;
    4.69                              }
    4.70 -                            PEP_rating this_user_id_rating = PEP_rating_undefined;
    4.71 -                            identity_rating(session, _from, &this_user_id_rating);
    4.72 -                            free_identity(_from);
    4.73 -
    4.74 -                            if (this_user_id_rating < PEP_rating_trusted ) {
    4.75 +                            status = get_trust(session, _from);
    4.76 +                            if (_from->comm_type < PEP_ct_strong_encryption) {
    4.77 +                                free_identity(_from);
    4.78                                  discarded = true;
    4.79                                  goto free_all;
    4.80                              }
    4.81 +                            free_identity(_from);
    4.82                              break;
    4.83 +                        }
    4.84 +                        case DeviceGroup_Protocol__payload_PR_groupUpdate:
    4.85 +                            // inject message but don't consume it, so 
    4.86 +                            // that other group members can also be updated
    4.87 +                            force_keep_msg = true;
    4.88 +                            
    4.89 +                            // no break
    4.90 +
    4.91 +                        case DeviceGroup_Protocol__payload_PR_updateRequest:
    4.92 +                        {
    4.93 +                            if (!keylist || rating < PEP_rating_reliable){
    4.94 +                                discarded = true;
    4.95 +                                goto free_all;
    4.96 +                            }
    4.97 +                            // GroupUpdate and UpdateRequests come from group.
    4.98 +                            // check trust relation in between signer key and 
    4.99 +                            // own id to be sure.
   4.100 +                            pEp_identity *_from = new_identity(NULL, 
   4.101 +                                                               keylist->value,
   4.102 +                                                               PEP_OWN_USERID,
   4.103 +                                                               NULL);
   4.104 +                            if (_from == NULL){
   4.105 +                                status = PEP_OUT_OF_MEMORY;
   4.106 +                                goto free_all;
   4.107 +                            }
   4.108 +                            status = get_trust(session, _from);
   4.109 +                            if (_from->comm_type < PEP_ct_pEp) {
   4.110 +                                free_identity(_from);
   4.111 +                                discarded = true;
   4.112 +                                goto free_all;
   4.113 +                            }
   4.114 +                            free_identity(_from);
   4.115 +                        }
   4.116                          default:
   4.117                              break;
   4.118                      }
   4.119 @@ -597,13 +618,10 @@
   4.120      if (status != PEP_STATUS_OK)
   4.121          return status;
   4.122  
   4.123 -    printf("BROADCAST \n");
   4.124 -
   4.125      // FIXME: exclude previously rejected identities
   4.126      for (identity_list *_i = own_identities; _i && _i->ident; _i = _i->next) {
   4.127          pEp_identity *me = _i->ident;
   4.128  
   4.129 -        printf("BROADCAST looop \n");
   4.130          // FIXME: no deep copy for multicast supported yet
   4.131          // DeviceGroup_Protocol_t *_msg = malloc(sizeof(DeviceGroup_Protocol_t));
   4.132          // assert(_msg);
     5.1 --- a/src/sync_send_actions.c	Mon Oct 17 23:42:47 2016 +0200
     5.2 +++ b/src/sync_send_actions.c	Tue Oct 18 00:57:56 2016 +0200
     5.3 @@ -13,6 +13,7 @@
     5.4  #include "../asn.1/HandshakeRequest.h"
     5.5  #include "../asn.1/GroupKeys.h"
     5.6  #include "../asn.1/GroupUpdate.h"
     5.7 +#include "../asn.1/UpdateRequest.h"
     5.8  
     5.9  
    5.10  // sendBeacon() - send Beacon message
    5.11 @@ -211,3 +212,46 @@
    5.12      return status;
    5.13  }
    5.14  
    5.15 +
    5.16 +// sendUpdateRequest() - send UpdateRequest message
    5.17 +//
    5.18 +//  params:
    5.19 +//      session (in)        session handle
    5.20 +//      state (in)          state the state machine is in
    5.21 +//      partner (in)        (must be NULL)
    5.22 +//
    5.23 +//  returns:
    5.24 +//      PEP_STATUS_OK or any other value on error
    5.25 +
    5.26 +PEP_STATUS sendUpdateRequest(
    5.27 +        PEP_SESSION session,
    5.28 +        DeviceState_state state,
    5.29 +        Identity partner,
    5.30 +        void *extra
    5.31 +    )
    5.32 +{
    5.33 +    assert(session && state);
    5.34 +    if (!(session && state))
    5.35 +        return PEP_ILLEGAL_VALUE;
    5.36 +
    5.37 +    PEP_STATUS status = PEP_STATUS_OK;
    5.38 +
    5.39 +    DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_updateRequest);
    5.40 +    if (!msg)
    5.41 +        goto enomem;
    5.42 +
    5.43 +    bool encrypted = true;
    5.44 +    status = multicast_self_msg(session, state, msg, encrypted);
    5.45 +    if (status != PEP_STATUS_OK)
    5.46 +        goto error;
    5.47 +
    5.48 +    free_DeviceGroup_Protocol_msg(msg);
    5.49 +    return PEP_STATUS_OK;
    5.50 +
    5.51 +enomem:
    5.52 +    status = PEP_OUT_OF_MEMORY;
    5.53 +error:
    5.54 +    free_DeviceGroup_Protocol_msg(msg);
    5.55 +    return status;
    5.56 +}
    5.57 +
     6.1 --- a/sync/devicegroup.fsm	Mon Oct 17 23:42:47 2016 +0200
     6.2 +++ b/sync/devicegroup.fsm	Tue Oct 18 00:57:56 2016 +0200
     6.3 @@ -11,6 +11,7 @@
     6.4  
     6.5      broadcast sendBeacon;
     6.6      broadcast sendGroupUpdate;
     6.7 +    broadcast sendUpdateRequest;
     6.8      unencrypted sendBeacon;
     6.9  
    6.10      fsm DeviceState filename=sync {
    6.11 @@ -70,8 +71,14 @@
    6.12          }
    6.13  
    6.14          state Grouped {
    6.15 +            on Init 
    6.16 +                do enterGroup;
    6.17              on KeyGen
    6.18                  do sendGroupUpdate;
    6.19 +            on CannotDecrypt
    6.20 +                do sendUpdateRequest; // TODO: narrow request to missing key
    6.21 +            on UpdateRequest
    6.22 +                do sendGroupUpdate;
    6.23              on Beacon(Identity partner)
    6.24                  do sendHandshakeRequest(partner);
    6.25              on HandshakeRequest(Identity partner) {