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