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