src/sync_send_actions.c
author Volker Birk <vb@pep.foundation>
Thu, 01 Sep 2016 17:55:03 +0200
branchkeysync
changeset 1111 3c46dc58096d
parent 1098 737031f6f0b3
child 1161 9a1f9ec6561a
permissions -rw-r--r--
merging
     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     bool encrypted = false;
    45     status = multicast_self_msg(session, state, msg, encrypted);
    46     if (status != PEP_STATUS_OK)
    47         goto error;
    48 
    49     free_DeviceGroup_Protocol_msg(msg);
    50     free_identity(partner);
    51     return PEP_STATUS_OK;
    52 
    53 enomem:
    54     status = PEP_OUT_OF_MEMORY;
    55 error:
    56     free_identity(partner);
    57     free_DeviceGroup_Protocol_msg(msg);
    58     return status;
    59 }
    60 
    61 
    62 // sendHandshakeRequest() - send HandshakeRequest message
    63 //
    64 //  params:
    65 //      session (in)        session handle
    66 //      state (in)          state the state machine is in
    67 //      partner (in)        partner to communicate with
    68 //
    69 //  returns:
    70 //      PEP_STATUS_OK or any other value on error
    71 
    72 PEP_STATUS sendHandshakeRequest(
    73         PEP_SESSION session,
    74         DeviceState_state state,
    75         Identity partner,
    76         void *extra
    77     )
    78 {
    79     assert(session && state);
    80     if (!(session && state))
    81         return PEP_ILLEGAL_VALUE;
    82 
    83     PEP_STATUS status = PEP_STATUS_OK;
    84 
    85     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_handshakeRequest);
    86     if (!msg)
    87         goto enomem;
    88 
    89     bool encrypted = true;
    90     status = unicast_msg(session, partner, state, msg, encrypted);
    91     if (status != PEP_STATUS_OK)
    92         goto error;
    93 
    94     free_DeviceGroup_Protocol_msg(msg);
    95     free_identity(partner);
    96     return PEP_STATUS_OK;
    97 
    98 enomem:
    99     status = PEP_OUT_OF_MEMORY;
   100 error:
   101     free_identity(partner);
   102     free_DeviceGroup_Protocol_msg(msg);
   103     return status;
   104 }
   105 
   106 
   107 // sendGroupKeys() - send GroupKeys message
   108 //
   109 //  params:
   110 //      session (in)        session handle
   111 //      state (in)          state the state machine is in
   112 //      partner (in)        (must be NULL)
   113 //
   114 //  returns:
   115 //      PEP_STATUS_OK or any other value on error
   116 
   117 PEP_STATUS sendGroupKeys(
   118         PEP_SESSION session,
   119         DeviceState_state state,
   120         Identity partner,
   121         void *extra
   122     )
   123 {
   124     assert(session && state);
   125     if (!(session && state))
   126         return PEP_ILLEGAL_VALUE;
   127 
   128     PEP_STATUS status = PEP_STATUS_OK;
   129     identity_list *kl = new_identity_list(NULL);
   130 
   131     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_groupKeys);
   132     if (!msg)
   133         goto enomem;
   134 
   135     status = own_identities_retrieve(session, &kl);
   136     if (status != PEP_STATUS_OK)
   137         goto error;
   138     if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupKeys.ownIdentities) == NULL)
   139         goto enomem;
   140 
   141     bool encrypted = true;
   142     status = unicast_msg(session, partner, state, msg, encrypted);
   143     if (status != PEP_STATUS_OK)
   144         goto error;
   145 
   146     free_identity_list(kl);
   147     free_DeviceGroup_Protocol_msg(msg);
   148     free_identity(partner);
   149     return PEP_STATUS_OK;
   150 
   151 enomem:
   152     status = PEP_OUT_OF_MEMORY;
   153 error:
   154     free_identity(partner);
   155     free_DeviceGroup_Protocol_msg(msg);
   156     free_identity_list(kl);
   157     return status;
   158 }
   159