src/sync_actions.c
author Volker Birk <vb@pep-project.org>
Sun, 29 May 2016 14:13:53 +0200
changeset 691 67c247f1da64
parent 690 3ae86b68fdfe
child 692 6f7f1b7627bc
permissions -rw-r--r--
checking constraints
     1 // 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/OwnKeys.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         const Identity partner
    29     )
    30 {
    31     PEP_STATUS status = PEP_STATUS_OK;
    32     Beacon_t *msg = NULL;
    33     char *payload = NULL;
    34     message *_message = NULL;
    35 
    36     assert(session);
    37     assert(!partner);
    38     if (!(session && !partner))
    39         return PEP_ILLEGAL_VALUE;
    40 
    41     assert(session->messageToSend);
    42     if (!session->messageToSend)
    43         return PEP_SEND_FUNCTION_NOT_REGISTERED;
    44 
    45     msg = (Beacon_t *) calloc(1, sizeof(Beacon_t));
    46     assert(msg);
    47     if (!msg)
    48         goto enomem;
    49 
    50     int32_t seq;
    51     status = sequence_value(session, "DeviceGroup", &seq);
    52     if (status != PEP_STATUS_OK)
    53         goto error;
    54     msg->header.sequence = (long) seq;
    55 
    56     msg->state = (long) state;
    57 
    58     pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
    59     if (!me)
    60         goto enomem;
    61     status = myself(session, me);
    62     if (status != PEP_STATUS_OK)
    63         goto error;
    64     if (Identity_from_Struct(me, &msg->me) == NULL)
    65         goto enomem;
    66 
    67     if (asn_check_constraints(&asn_DEF_HandshakeRequest, msg, NULL, NULL)) {
    68         status = PEP_CONTRAINTS_VIOLATED;
    69         goto error;
    70     }
    71 
    72     ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
    73             NULL, msg, (void **) &payload);
    74     if (size == -1) {
    75         status = PEP_CANNOT_ENCODE;
    76         goto error;
    77     }
    78 
    79     status = prepare_message(me, partner, payload, size, &_message);
    80     if (status != PEP_STATUS_OK)
    81         goto error;
    82     payload = NULL;
    83 
    84     status = session->messageToSend(session->sync_obj, _message);
    85 
    86     free_message(_message);
    87     ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    88 
    89     return status;
    90 
    91 enomem:
    92     status = PEP_OUT_OF_MEMORY;
    93 error:
    94     ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    95     free(payload);
    96     free_message(_message);
    97     return status;
    98 }
    99 
   100 
   101 // sendHandshakeRequest() - send HandshakeRequest message
   102 //
   103 //  params:
   104 //      session (in)        session handle
   105 //      state (in)          state the state machine is in
   106 //      partner (in)        partner in sync
   107 //
   108 //  returns:
   109 //      PEP_STATUS_OK or any other value on error
   110 
   111 PEP_STATUS sendHandshakeRequest(
   112         PEP_SESSION session,
   113         DeviceState_state state,
   114         const Identity partner
   115     )
   116 {
   117     PEP_STATUS status = PEP_STATUS_OK;
   118     HandshakeRequest_t *msg = NULL;
   119     char *payload = NULL;
   120     message *_message = NULL;
   121 
   122     assert(session);
   123     assert(partner);
   124     if (!(session && partner))
   125         return PEP_ILLEGAL_VALUE;
   126 
   127     assert(session->messageToSend);
   128     if (!session->messageToSend)
   129         return PEP_SEND_FUNCTION_NOT_REGISTERED;
   130 
   131     msg = (HandshakeRequest_t *) calloc(1, sizeof(HandshakeRequest_t));
   132     assert(msg);
   133     if (!msg)
   134         goto enomem;
   135 
   136     int32_t seq;
   137     status = sequence_value(session, "DeviceGroup", &seq);
   138     if (status != PEP_STATUS_OK)
   139         goto error;
   140     msg->header.sequence = (long) seq;
   141 
   142     msg->state = (long) state;
   143 
   144     pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
   145     if (!me)
   146         goto enomem;
   147     status = myself(session, me);
   148     if (status != PEP_STATUS_OK)
   149         goto error;
   150     if (Identity_from_Struct(me, &msg->me) == NULL)
   151         goto enomem;
   152 
   153     if (Identity_from_Struct(partner, &msg->partner) == NULL)
   154         goto enomem;
   155 
   156     ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
   157             NULL, msg, (void **) &payload);
   158     if (size == -1) {
   159         status = PEP_CANNOT_ENCODE;
   160         goto error;
   161     }
   162 
   163     status = prepare_message(me, partner, payload, size, &_message);
   164     if (status != PEP_STATUS_OK)
   165         goto error;
   166     payload = NULL;
   167 
   168     status = session->messageToSend(session->sync_obj, _message);
   169 
   170     free_message(_message);
   171     ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   172 
   173     return status;
   174 
   175 enomem:
   176     status = PEP_OUT_OF_MEMORY;
   177 error:
   178     ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   179     free(payload);
   180     free_message(_message);
   181     return status;
   182 }
   183 
   184 
   185 // showHandshake() - 
   186 //
   187 //  params:
   188 //      session (in)        session handle
   189 //      state (in)          state the state machine is in
   190 //      partner (in)        partner in sync
   191 //
   192 //  returns:
   193 //      PEP_STATUS_OK or any other value on error
   194 
   195 PEP_STATUS showHandshake(
   196         PEP_SESSION session,
   197         DeviceState_state state,
   198         const Identity partner
   199     )
   200 {
   201     PEP_STATUS status = PEP_STATUS_OK;
   202 
   203     assert(session);
   204     assert(partner);
   205     if (!(session && partner))
   206         return PEP_ILLEGAL_VALUE;
   207 
   208     // working code
   209 
   210 
   211     return status;
   212 
   213 enomem:
   214     status = PEP_OUT_OF_MEMORY;
   215 error:
   216     // free...
   217     return status;
   218 }
   219 
   220 
   221 // reject() - 
   222 //
   223 //  params:
   224 //      session (in)        session handle
   225 //      state (in)          state the state machine is in
   226 //      partner (in)        partner in sync
   227 //
   228 //  returns:
   229 //      PEP_STATUS_OK or any other value on error
   230 
   231 PEP_STATUS reject(
   232         PEP_SESSION session,
   233         DeviceState_state state,
   234         const Identity partner
   235     )
   236 {
   237     PEP_STATUS status = PEP_STATUS_OK;
   238 
   239     assert(session);
   240     assert(partner);
   241     if (!(session && partner))
   242         return PEP_ILLEGAL_VALUE;
   243 
   244     // working code
   245 
   246 
   247     return status;
   248 
   249 enomem:
   250     status = PEP_OUT_OF_MEMORY;
   251 error:
   252     // free...
   253     return status;
   254 }
   255 
   256 
   257 // storeGroupKeys() - 
   258 //
   259 //  params:
   260 //      session (in)        session handle
   261 //      state (in)          state the state machine is in
   262 //      partner (in)        partner in sync
   263 //
   264 //  returns:
   265 //      PEP_STATUS_OK or any other value on error
   266 
   267 PEP_STATUS storeGroupKeys(
   268         PEP_SESSION session,
   269         DeviceState_state state,
   270         const Identity partner
   271     )
   272 {
   273     PEP_STATUS status = PEP_STATUS_OK;
   274 
   275     assert(session);
   276     assert(partner);
   277     if (!(session && partner))
   278         return PEP_ILLEGAL_VALUE;
   279 
   280     // working code
   281 
   282 
   283     return status;
   284 
   285 enomem:
   286     status = PEP_OUT_OF_MEMORY;
   287 error:
   288     // free...
   289     return status;
   290 }
   291 
   292 
   293 // sendOwnKeys() - send OwnKeys message
   294 //
   295 //  params:
   296 //      session (in)        session handle
   297 //      state (in)          state the state machine is in
   298 //      partner (in)        (must be NULL)
   299 //
   300 //  returns:
   301 //      PEP_STATUS_OK or any other value on error
   302 
   303 PEP_STATUS sendOwnKeys(
   304         PEP_SESSION session,
   305         DeviceState_state state,
   306         const Identity partner
   307     )
   308 {
   309     PEP_STATUS status = PEP_STATUS_OK;
   310     OwnKeys_t *msg = NULL;
   311     char *payload = NULL;
   312     message *_message = NULL;
   313 
   314     assert(session);
   315     assert(!partner);
   316     if (!(session && !partner))
   317         return PEP_ILLEGAL_VALUE;
   318 
   319     assert(session->messageToSend);
   320     if (!session->messageToSend)
   321         return PEP_SEND_FUNCTION_NOT_REGISTERED;
   322 
   323     msg = (OwnKeys_t *) calloc(1, sizeof(OwnKeys_t));
   324     assert(msg);
   325     if (!msg)
   326         goto enomem;
   327 
   328     int32_t seq;
   329     status = sequence_value(session, "DeviceGroup", &seq);
   330     if (status != PEP_STATUS_OK)
   331         goto error;
   332     msg->header.sequence = (long) seq;
   333 
   334     msg->state = (long) state;
   335 
   336     pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
   337     if (!me)
   338         goto enomem;
   339     status = myself(session, me);
   340     if (status != PEP_STATUS_OK)
   341         goto error;
   342     if (Identity_from_Struct(me, &msg->me) == NULL)
   343         goto enomem;
   344 
   345     stringlist_t *sl;
   346     status = own_key_retrieve(session, &sl);
   347     if (status != PEP_STATUS_OK)
   348         goto error;
   349     if (KeyList_from_stringlist(sl, &msg->keylist) == NULL)
   350         goto enomem;
   351 
   352     ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
   353             NULL, msg, (void **) &payload);
   354     if (size == -1) {
   355         status = PEP_CANNOT_ENCODE;
   356         goto error;
   357     }
   358 
   359     status = prepare_message(me, partner, payload, size, &_message);
   360     if (status != PEP_STATUS_OK)
   361         goto error;
   362     payload = NULL;
   363 
   364     status = session->messageToSend(session->sync_obj, _message);
   365 
   366     free_message(_message);
   367     ASN_STRUCT_FREE(asn_DEF_OwnKeys, msg);
   368 
   369     return status;
   370 
   371 enomem:
   372     status = PEP_OUT_OF_MEMORY;
   373 error:
   374     ASN_STRUCT_FREE(asn_DEF_OwnKeys, msg);
   375     free(payload);
   376     free_message(_message);
   377     return status;
   378 }
   379 
   380 
   381 // transmitGroupKeys() - 
   382 //
   383 //  params:
   384 //      session (in)        session handle
   385 //      state (in)          state the state machine is in
   386 //      partner (in)        partner in sync
   387 //
   388 //  returns:
   389 //      PEP_STATUS_OK or any other value on error
   390 
   391 PEP_STATUS transmitGroupKeys(
   392         PEP_SESSION session,
   393         DeviceState_state state,
   394         const Identity partner
   395     )
   396 {
   397     PEP_STATUS status = PEP_STATUS_OK;
   398 
   399     assert(session);
   400     assert(partner);
   401     if (!(session && partner))
   402         return PEP_ILLEGAL_VALUE;
   403 
   404     // working code
   405 
   406 
   407     return status;
   408 
   409 enomem:
   410     status = PEP_OUT_OF_MEMORY;
   411 error:
   412     // free...
   413     return status;
   414 }
   415