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