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