src/sync_send_actions.c
author Volker Birk <vb@pep-project.org>
Fri, 29 Jul 2016 10:45:13 +0200
branchkeysync
changeset 939 ed7dfdae3031
parent 883 096ff09932ad
child 951 dec407deb10e
permissions -rw-r--r--
changing message_api: adding flags to encrypt_message()
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@883
    44
    if (!session->messageToSend)
vb@883
    45
        return PEP_SEND_FUNCTION_NOT_REGISTERED;
vb@883
    46
vb@883
    47
    msg = (Beacon_t *) calloc(1, sizeof(Beacon_t));
vb@883
    48
    assert(msg);
vb@883
    49
    if (!msg)
vb@883
    50
        goto enomem;
vb@883
    51
vb@883
    52
    int32_t seq;
vb@883
    53
    status = sequence_value(session, "DeviceGroup", &seq);
vb@883
    54
    if (status != PEP_STATUS_OK)
vb@883
    55
        goto error;
vb@883
    56
    msg->header.sequence = (long) seq;
vb@883
    57
vb@883
    58
    msg->state = (long) state;
vb@883
    59
vb@883
    60
    me = new_identity(NULL, NULL, NULL, NULL);
vb@883
    61
    if (!me)
vb@883
    62
        goto enomem;
vb@883
    63
    status = myself(session, me);
vb@883
    64
    if (status != PEP_STATUS_OK)
vb@883
    65
        goto error;
vb@883
    66
    if (Identity_from_Struct(me, &msg->me) == NULL)
vb@883
    67
        goto enomem;
vb@883
    68
vb@883
    69
    if (asn_check_constraints(&asn_DEF_Beacon, msg, NULL, NULL)) {
vb@883
    70
        status = PEP_CONTRAINTS_VIOLATED;
vb@883
    71
        goto error;
vb@883
    72
    }
vb@883
    73
vb@883
    74
    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_Beacon,
vb@883
    75
            NULL, msg, (void **) &payload);
vb@883
    76
    if (size == -1) {
vb@883
    77
        status = PEP_CANNOT_ENCODE;
vb@883
    78
        goto error;
vb@883
    79
    }
vb@883
    80
vb@883
    81
    status = prepare_message(me, partner, payload, size, &_message);
vb@883
    82
    if (status != PEP_STATUS_OK)
vb@883
    83
        goto error;
vb@883
    84
    payload = NULL;
vb@883
    85
vb@883
    86
    free_identity(me);
vb@883
    87
    me = NULL;
vb@883
    88
vb@883
    89
    status = session->messageToSend(session->sync_obj, _message);
vb@883
    90
vb@883
    91
    free_message(_message);
vb@883
    92
    ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
vb@883
    93
vb@883
    94
    return status;
vb@883
    95
vb@883
    96
enomem:
vb@883
    97
    status = PEP_OUT_OF_MEMORY;
vb@883
    98
error:
vb@883
    99
    ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
vb@883
   100
    free(payload);
vb@883
   101
    free_message(_message);
vb@883
   102
    free_identity(me);
vb@883
   103
    return status;
vb@883
   104
}
vb@883
   105
vb@883
   106
vb@883
   107
// sendHandshakeRequest() - send HandshakeRequest message
vb@883
   108
//
vb@883
   109
//  params:
vb@883
   110
//      session (in)        session handle
vb@883
   111
//      state (in)          state the state machine is in
vb@883
   112
//      partner (in)        partner to communicate with
vb@883
   113
//
vb@883
   114
//  returns:
vb@883
   115
//      PEP_STATUS_OK or any other value on error
vb@883
   116
vb@883
   117
PEP_STATUS sendHandshakeRequest(
vb@883
   118
        PEP_SESSION session,
vb@883
   119
        DeviceState_state state,
vb@939
   120
        Identity partner,
vb@939
   121
        void *extra
vb@883
   122
    )
vb@883
   123
{
vb@883
   124
    PEP_STATUS status = PEP_STATUS_OK;
vb@883
   125
    HandshakeRequest_t *msg = NULL;
vb@883
   126
    char *payload = NULL;
vb@883
   127
    message *_message = NULL;
vb@883
   128
    pEp_identity *me = NULL;
vb@883
   129
vb@883
   130
    assert(session);
vb@883
   131
    assert(partner);
vb@883
   132
    if (!(session && partner))
vb@883
   133
        return PEP_ILLEGAL_VALUE;
vb@883
   134
vb@883
   135
    assert(session->messageToSend);
vb@883
   136
    if (!session->messageToSend)
vb@883
   137
        return PEP_SEND_FUNCTION_NOT_REGISTERED;
vb@883
   138
vb@883
   139
    msg = (HandshakeRequest_t *) calloc(1, sizeof(HandshakeRequest_t));
vb@883
   140
    assert(msg);
vb@883
   141
    if (!msg)
vb@883
   142
        goto enomem;
vb@883
   143
vb@883
   144
    int32_t seq;
vb@883
   145
    status = sequence_value(session, "DeviceGroup", &seq);
vb@883
   146
    if (status != PEP_STATUS_OK)
vb@883
   147
        goto error;
vb@883
   148
    msg->header.sequence = (long) seq;
vb@883
   149
vb@883
   150
    msg->state = (long) state;
vb@883
   151
vb@883
   152
    me = new_identity(NULL, NULL, NULL, NULL);
vb@883
   153
    if (!me)
vb@883
   154
        goto enomem;
vb@883
   155
    status = myself(session, me);
vb@883
   156
    if (status != PEP_STATUS_OK)
vb@883
   157
        goto error;
vb@883
   158
    if (Identity_from_Struct(me, &msg->me) == NULL)
vb@883
   159
        goto enomem;
vb@883
   160
vb@883
   161
    if (Identity_from_Struct(partner, &msg->partner) == NULL)
vb@883
   162
        goto enomem;
vb@883
   163
vb@883
   164
    if (asn_check_constraints(&asn_DEF_HandshakeRequest, msg, NULL, NULL)) {
vb@883
   165
        status = PEP_CONTRAINTS_VIOLATED;
vb@883
   166
        goto error;
vb@883
   167
    }
vb@883
   168
vb@883
   169
    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
vb@883
   170
            NULL, msg, (void **) &payload);
vb@883
   171
    if (size == -1) {
vb@883
   172
        status = PEP_CANNOT_ENCODE;
vb@883
   173
        goto error;
vb@883
   174
    }
vb@883
   175
vb@883
   176
    status = prepare_message(me, partner, payload, size, &_message);
vb@883
   177
    if (status != PEP_STATUS_OK)
vb@883
   178
        goto error;
vb@883
   179
    payload = NULL;
vb@883
   180
vb@883
   181
    free_identity(me);
vb@883
   182
    me = NULL;
vb@883
   183
vb@883
   184
    status = session->messageToSend(session->sync_obj, _message);
vb@883
   185
vb@883
   186
    free_message(_message);
vb@883
   187
    ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
vb@883
   188
vb@883
   189
    return status;
vb@883
   190
vb@883
   191
enomem:
vb@883
   192
    status = PEP_OUT_OF_MEMORY;
vb@883
   193
error:
vb@883
   194
    ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
vb@883
   195
    free(payload);
vb@883
   196
    free_message(_message);
vb@883
   197
    free_identity(me);
vb@883
   198
    return status;
vb@883
   199
}
vb@883
   200
vb@883
   201
vb@883
   202
// sendGroupKeys() - send GroupKeys message
vb@883
   203
//
vb@883
   204
//  params:
vb@883
   205
//      session (in)        session handle
vb@883
   206
//      state (in)          state the state machine is in
vb@883
   207
//      partner (in)        (must be NULL)
vb@883
   208
//
vb@883
   209
//  returns:
vb@883
   210
//      PEP_STATUS_OK or any other value on error
vb@883
   211
vb@883
   212
PEP_STATUS sendGroupKeys(
vb@883
   213
        PEP_SESSION session,
vb@883
   214
        DeviceState_state state,
vb@939
   215
        Identity partner,
vb@939
   216
        void *extra
vb@883
   217
    )
vb@883
   218
{
vb@883
   219
    PEP_STATUS status = PEP_STATUS_OK;
vb@883
   220
    GroupKeys_t *msg = NULL;
vb@883
   221
    char *payload = NULL;
vb@883
   222
    message *_message = NULL;
vb@883
   223
    pEp_identity *me = NULL;
vb@883
   224
vb@883
   225
    assert(session);
vb@883
   226
    assert(!partner);
vb@883
   227
    if (!(session && !partner))
vb@883
   228
        return PEP_ILLEGAL_VALUE;
vb@883
   229
vb@883
   230
    assert(session->messageToSend);
vb@883
   231
    if (!session->messageToSend)
vb@883
   232
        return PEP_SEND_FUNCTION_NOT_REGISTERED;
vb@883
   233
vb@883
   234
    msg = (GroupKeys_t *) calloc(1, sizeof(GroupKeys_t));
vb@883
   235
    assert(msg);
vb@883
   236
    if (!msg)
vb@883
   237
        goto enomem;
vb@883
   238
vb@883
   239
    int32_t seq;
vb@883
   240
    status = sequence_value(session, "DeviceGroup", &seq);
vb@883
   241
    if (status != PEP_STATUS_OK)
vb@883
   242
        goto error;
vb@883
   243
    msg->header.sequence = (long) seq;
vb@883
   244
vb@883
   245
    msg->state = (long) state;
vb@883
   246
vb@883
   247
    me = new_identity(NULL, NULL, NULL, NULL);
vb@883
   248
    if (!me)
vb@883
   249
        goto enomem;
vb@883
   250
    status = myself(session, me);
vb@883
   251
    if (status != PEP_STATUS_OK)
vb@883
   252
        goto error;
vb@883
   253
    if (Identity_from_Struct(me, &msg->me) == NULL)
vb@883
   254
        goto enomem;
vb@883
   255
vb@883
   256
    if (asn_check_constraints(&asn_DEF_GroupKeys, msg, NULL, NULL)) {
vb@883
   257
        status = PEP_CONTRAINTS_VIOLATED;
vb@883
   258
        goto error;
vb@883
   259
    }
vb@883
   260
vb@883
   261
    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_GroupKeys,
vb@883
   262
            NULL, msg, (void **) &payload);
vb@883
   263
    if (size == -1) {
vb@883
   264
        status = PEP_CANNOT_ENCODE;
vb@883
   265
        goto error;
vb@883
   266
    }
vb@883
   267
vb@883
   268
    status = prepare_message(me, partner, payload, size, &_message);
vb@883
   269
    if (status != PEP_STATUS_OK)
vb@883
   270
        goto error;
vb@883
   271
    payload = NULL;
vb@883
   272
vb@883
   273
    free_identity(me);
vb@883
   274
    me = NULL;
vb@883
   275
vb@883
   276
    status = session->messageToSend(session->sync_obj, _message);
vb@883
   277
vb@883
   278
    free_message(_message);
vb@883
   279
    ASN_STRUCT_FREE(asn_DEF_GroupKeys, msg);
vb@883
   280
vb@883
   281
    return status;
vb@883
   282
vb@883
   283
enomem:
vb@883
   284
    status = PEP_OUT_OF_MEMORY;
vb@883
   285
error:
vb@883
   286
    ASN_STRUCT_FREE(asn_DEF_GroupKeys, msg);
vb@883
   287
    free(payload);
vb@883
   288
    free_message(_message);
vb@883
   289
    free_identity(me);
vb@883
   290
    return status;
vb@883
   291
}
vb@883
   292