src/sync_send_actions.c
author Volker Birk <vb@pep.foundation>
Mon, 08 Aug 2016 17:32:51 +0200
branchkeysync
changeset 985 087d5d60c082
parent 955 060272dfaaeb
child 991 b230b6418b52
permissions -rw-r--r--
moving #include and installing headers
     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 "../asn.1/Beacon.h"
    12 #include "../asn.1/HandshakeRequest.h"
    13 #include "../asn.1/GroupKeys.h"
    14 
    15 
    16 // sendBeacon() - send Beacon message
    17 //
    18 //  params:
    19 //      session (in)        session handle
    20 //      state (in)          state the state machine is in
    21 //      partner (in)        (must be NULL)
    22 //
    23 //  returns:
    24 //      PEP_STATUS_OK or any other value on error
    25 
    26 PEP_STATUS sendBeacon(
    27         PEP_SESSION session,
    28         DeviceState_state state,
    29         Identity partner,
    30         void *extra
    31     )
    32 {
    33     PEP_STATUS status = PEP_STATUS_OK;
    34     Beacon_t *msg = NULL;
    35     char *payload = NULL;
    36     message *_message = NULL;
    37     pEp_identity *me = NULL;
    38 
    39     assert(session);
    40     assert(!partner);
    41     if (!(session && !partner))
    42         return PEP_ILLEGAL_VALUE;
    43 
    44     assert(session->messageToSend);
    45     if (!session->messageToSend) {
    46         status = PEP_SEND_FUNCTION_NOT_REGISTERED;
    47         goto error;
    48     }
    49 
    50     msg = (Beacon_t *) calloc(1, sizeof(Beacon_t));
    51     assert(msg);
    52     if (!msg)
    53         goto enomem;
    54 
    55     int32_t seq;
    56     status = sequence_value(session, "DeviceGroup", &seq);
    57     if (status != PEP_STATUS_OK)
    58         goto error;
    59     msg->header.sequence = (long) seq;
    60 
    61     bool devicegroup = storedGroupKeys(session);
    62     if (devicegroup) { // default is FALSE
    63         BOOLEAN_t *dg = malloc(sizeof(BOOLEAN_t));
    64         assert(dg);
    65         if (!dg)
    66             goto enomem;
    67 
    68         *dg = 1;
    69         msg->header.devicegroup = dg;
    70     }
    71 
    72     msg->header.state = (long) state;
    73 
    74     me = new_identity(NULL, NULL, NULL, NULL);
    75     if (!me)
    76         goto enomem;
    77     status = myself(session, me);
    78     if (status != PEP_STATUS_OK)
    79         goto error;
    80     if (Identity_from_Struct(me, &msg->header.me) == NULL)
    81         goto enomem;
    82 
    83     if (asn_check_constraints(&asn_DEF_Beacon, msg, NULL, NULL)) {
    84         status = PEP_CONTRAINTS_VIOLATED;
    85         goto error;
    86     }
    87 
    88     ssize_t size = uper_encode_to_new_buffer(&asn_DEF_Beacon,
    89             NULL, msg, (void **) &payload);
    90     if (size == -1) {
    91         status = PEP_CANNOT_ENCODE;
    92         goto error;
    93     }
    94 
    95     status = prepare_message(me, partner, payload, size, &_message);
    96     if (status != PEP_STATUS_OK)
    97         goto error;
    98     payload = NULL;
    99 
   100     free_identity(me);
   101     me = NULL;
   102 
   103     status = session->messageToSend(session->sync_obj, _message);
   104 
   105     free_message(_message);
   106     ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
   107     free_identity(partner);
   108     return status;
   109 
   110 enomem:
   111     status = PEP_OUT_OF_MEMORY;
   112 error:
   113     ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
   114     free(payload);
   115     free_message(_message);
   116     free_identity(me);
   117     free_identity(partner);
   118     return status;
   119 }
   120 
   121 
   122 // sendHandshakeRequest() - send HandshakeRequest message
   123 //
   124 //  params:
   125 //      session (in)        session handle
   126 //      state (in)          state the state machine is in
   127 //      partner (in)        partner to communicate with
   128 //
   129 //  returns:
   130 //      PEP_STATUS_OK or any other value on error
   131 
   132 PEP_STATUS sendHandshakeRequest(
   133         PEP_SESSION session,
   134         DeviceState_state state,
   135         Identity partner,
   136         void *extra
   137     )
   138 {
   139     PEP_STATUS status = PEP_STATUS_OK;
   140     HandshakeRequest_t *msg = NULL;
   141     char *payload = NULL;
   142     message *_message = NULL;
   143     pEp_identity *me = NULL;
   144 
   145     assert(session);
   146     assert(partner);
   147     if (!(session && partner))
   148         return PEP_ILLEGAL_VALUE;
   149 
   150     assert(session->messageToSend);
   151     if (!session->messageToSend) {
   152         status = PEP_SEND_FUNCTION_NOT_REGISTERED;
   153         goto error;
   154     }
   155 
   156     msg = (HandshakeRequest_t *) calloc(1, sizeof(HandshakeRequest_t));
   157     assert(msg);
   158     if (!msg)
   159         goto enomem;
   160 
   161     int32_t seq;
   162     status = sequence_value(session, "DeviceGroup", &seq);
   163     if (status != PEP_STATUS_OK)
   164         goto error;
   165     msg->header.sequence = (long) seq;
   166 
   167     bool devicegroup = storedGroupKeys(session);
   168     if (devicegroup) { // default is FALSE
   169         BOOLEAN_t *dg = malloc(sizeof(BOOLEAN_t));
   170         assert(dg);
   171         if (!dg)
   172             goto enomem;
   173 
   174         *dg = 1;
   175         msg->header.devicegroup = dg;
   176     }
   177 
   178     msg->header.state = (long) state;
   179 
   180     me = new_identity(NULL, NULL, NULL, NULL);
   181     if (!me)
   182         goto enomem;
   183     status = myself(session, me);
   184     if (status != PEP_STATUS_OK)
   185         goto error;
   186     if (Identity_from_Struct(me, &msg->header.me) == NULL)
   187         goto enomem;
   188 
   189     if (asn_check_constraints(&asn_DEF_HandshakeRequest, msg, NULL, NULL)) {
   190         status = PEP_CONTRAINTS_VIOLATED;
   191         goto error;
   192     }
   193 
   194     ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
   195             NULL, msg, (void **) &payload);
   196     if (size == -1) {
   197         status = PEP_CANNOT_ENCODE;
   198         goto error;
   199     }
   200 
   201     status = prepare_message(me, partner, payload, size, &_message);
   202     if (status != PEP_STATUS_OK)
   203         goto error;
   204     payload = NULL;
   205 
   206     free_identity(me);
   207     me = NULL;
   208 
   209     status = session->messageToSend(session->sync_obj, _message);
   210 
   211     free_message(_message);
   212     ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   213     free_identity(partner);
   214     return status;
   215 
   216 enomem:
   217     status = PEP_OUT_OF_MEMORY;
   218 error:
   219     ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   220     free(payload);
   221     free_message(_message);
   222     free_identity(me);
   223     free_identity(partner);
   224     return status;
   225 }
   226 
   227 
   228 // sendGroupKeys() - send GroupKeys message
   229 //
   230 //  params:
   231 //      session (in)        session handle
   232 //      state (in)          state the state machine is in
   233 //      partner (in)        (must be NULL)
   234 //
   235 //  returns:
   236 //      PEP_STATUS_OK or any other value on error
   237 
   238 PEP_STATUS sendGroupKeys(
   239         PEP_SESSION session,
   240         DeviceState_state state,
   241         Identity partner,
   242         void *extra
   243     )
   244 {
   245     PEP_STATUS status = PEP_STATUS_OK;
   246     GroupKeys_t *msg = NULL;
   247     char *payload = NULL;
   248     message *_message = NULL;
   249     pEp_identity *me = NULL;
   250     identity_list *kl = NULL;
   251 
   252     assert(session);
   253     assert(!partner);
   254     if (!(session && !partner))
   255         return PEP_ILLEGAL_VALUE;
   256 
   257     assert(session->messageToSend);
   258     if (!session->messageToSend) {
   259         status = PEP_SEND_FUNCTION_NOT_REGISTERED;
   260         goto error;
   261     }
   262 
   263     msg = (GroupKeys_t *) calloc(1, sizeof(GroupKeys_t));
   264     assert(msg);
   265     if (!msg)
   266         goto enomem;
   267 
   268     int32_t seq;
   269     status = sequence_value(session, "DeviceGroup", &seq);
   270     if (status != PEP_STATUS_OK)
   271         goto error;
   272     msg->header.sequence = (long) seq;
   273 
   274     bool devicegroup = storedGroupKeys(session);
   275     if (devicegroup) { // default is FALSE
   276         BOOLEAN_t *dg = malloc(sizeof(BOOLEAN_t));
   277         assert(dg);
   278         if (!dg)
   279             goto enomem;
   280 
   281         *dg = 1;
   282         msg->header.devicegroup = dg;
   283     }
   284 
   285     msg->header.state = (long) state;
   286 
   287     me = new_identity(NULL, NULL, NULL, NULL);
   288     if (!me)
   289         goto enomem;
   290     status = myself(session, me);
   291     if (status != PEP_STATUS_OK)
   292         goto error;
   293     if (Identity_from_Struct(me, &msg->header.me) == NULL)
   294         goto enomem;
   295 
   296     status = own_identities_retrieve(session, &kl);
   297     if (status != PEP_STATUS_OK)
   298         goto error;
   299     if (IdentityList_from_identity_list(kl, &msg->ownIdentities) == NULL)
   300         goto enomem;
   301 
   302     if (asn_check_constraints(&asn_DEF_GroupKeys, msg, NULL, NULL)) {
   303         status = PEP_CONTRAINTS_VIOLATED;
   304         goto error;
   305     }
   306 
   307     ssize_t size = uper_encode_to_new_buffer(&asn_DEF_GroupKeys,
   308             NULL, msg, (void **) &payload);
   309     if (size == -1) {
   310         status = PEP_CANNOT_ENCODE;
   311         goto error;
   312     }
   313 
   314     status = prepare_message(me, partner, payload, size, &_message);
   315     if (status != PEP_STATUS_OK)
   316         goto error;
   317     payload = NULL;
   318 
   319     free_identity(me);
   320     me = NULL;
   321 
   322     status = session->messageToSend(session->sync_obj, _message);
   323 
   324     free_message(_message);
   325     ASN_STRUCT_FREE(asn_DEF_GroupKeys, msg);
   326     free_identity(partner);
   327     free_identity_list(kl);
   328     return status;
   329 
   330 enomem:
   331     status = PEP_OUT_OF_MEMORY;
   332 error:
   333     ASN_STRUCT_FREE(asn_DEF_GroupKeys, msg);
   334     free(payload);
   335     free_message(_message);
   336     free_identity(me);
   337     free_identity(partner);
   338     free_identity_list(kl);
   339     return status;
   340 }
   341