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