src/sync_send_actions.c
author Volker Birk <vb@pep.foundation>
Tue, 23 Aug 2016 18:54:46 +0200
branchkeysync
changeset 1075 6ac3fd936e93
parent 1066 e6db6bb9d542
child 1091 0571ce4b97a2
permissions -rw-r--r--
BUGFIX: double free
     1 // Send Actions for DeviceState state machine
     2 
     3 #include <assert.h>
     4 #include "pEp_internal.h"
     5 #include "keymanagement.h"
     6 #include "message.h"
     7 #include "sync_fsm.h"
     8 #include "baseprotocol.h"
     9 #include "map_asn1.h"
    10 #include "../asn.1/DeviceGroup-Protocol.h"
    11 #include "sync_impl.h"
    12 #include "../asn.1/Beacon.h"
    13 #include "../asn.1/HandshakeRequest.h"
    14 #include "../asn.1/GroupKeys.h"
    15 
    16 
    17 // sendBeacon() - send Beacon message
    18 //
    19 //  params:
    20 //      session (in)        session handle
    21 //      state (in)          state the state machine is in
    22 //      partner (in)        (must be NULL)
    23 //
    24 //  returns:
    25 //      PEP_STATUS_OK or any other value on error
    26 
    27 PEP_STATUS sendBeacon(
    28         PEP_SESSION session,
    29         DeviceState_state state,
    30         Identity partner,
    31         void *extra
    32     )
    33 {
    34     assert(session && state);
    35     if (!(session && state))
    36         return PEP_ILLEGAL_VALUE;
    37 
    38     PEP_STATUS status = PEP_STATUS_OK;
    39 
    40     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_beacon);
    41     if (!msg)
    42         goto enomem;
    43 
    44     status = multicast_self_msg(session, state, msg);
    45     if (status != PEP_STATUS_OK)
    46         goto error;
    47 
    48     free_DeviceGroup_Protocol_msg(msg);
    49     return PEP_STATUS_OK;
    50 
    51 enomem:
    52     status = PEP_OUT_OF_MEMORY;
    53 error:
    54     free(partner);
    55     free_DeviceGroup_Protocol_msg(msg);
    56     return status;
    57 }
    58 
    59 
    60 // sendHandshakeRequest() - send HandshakeRequest message
    61 //
    62 //  params:
    63 //      session (in)        session handle
    64 //      state (in)          state the state machine is in
    65 //      partner (in)        partner to communicate with
    66 //
    67 //  returns:
    68 //      PEP_STATUS_OK or any other value on error
    69 
    70 PEP_STATUS sendHandshakeRequest(
    71         PEP_SESSION session,
    72         DeviceState_state state,
    73         Identity partner,
    74         void *extra
    75     )
    76 {
    77     assert(session && state);
    78     if (!(session && state))
    79         return PEP_ILLEGAL_VALUE;
    80 
    81     PEP_STATUS status = PEP_STATUS_OK;
    82 
    83     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_handshakeRequest);
    84     if (!msg)
    85         goto enomem;
    86 
    87     status = unicast_msg(session, partner, state, msg);
    88     if (status != PEP_STATUS_OK)
    89         goto error;
    90 
    91     free_DeviceGroup_Protocol_msg(msg);
    92     return PEP_STATUS_OK;
    93 
    94 enomem:
    95     status = PEP_OUT_OF_MEMORY;
    96 error:
    97     free(partner);
    98     free_DeviceGroup_Protocol_msg(msg);
    99     return status;
   100 }
   101 
   102 
   103 // sendGroupKeys() - send GroupKeys message
   104 //
   105 //  params:
   106 //      session (in)        session handle
   107 //      state (in)          state the state machine is in
   108 //      partner (in)        (must be NULL)
   109 //
   110 //  returns:
   111 //      PEP_STATUS_OK or any other value on error
   112 
   113 PEP_STATUS sendGroupKeys(
   114         PEP_SESSION session,
   115         DeviceState_state state,
   116         Identity partner,
   117         void *extra
   118     )
   119 {
   120     assert(session && state);
   121     if (!(session && state))
   122         return PEP_ILLEGAL_VALUE;
   123 
   124     PEP_STATUS status = PEP_STATUS_OK;
   125     identity_list *kl = new_identity_list(NULL);
   126 
   127     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_groupKeys);
   128     if (!msg)
   129         goto enomem;
   130 
   131     status = own_identities_retrieve(session, &kl);
   132     if (status != PEP_STATUS_OK)
   133         goto error;
   134     if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupKeys.ownIdentities) == NULL)
   135         goto enomem;
   136 
   137     status = unicast_msg(session, partner, state, msg);
   138     if (status != PEP_STATUS_OK)
   139         goto error;
   140 
   141     free_identity_list(kl);
   142     free_DeviceGroup_Protocol_msg(msg);
   143     return PEP_STATUS_OK;
   144 
   145 enomem:
   146     status = PEP_OUT_OF_MEMORY;
   147 error:
   148     free(partner);
   149     free_DeviceGroup_Protocol_msg(msg);
   150     free_identity_list(kl);
   151     return status;
   152 }
   153