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