src/sync_actions.c
author Volker Birk <vb@pep.foundation>
Mon, 01 Aug 2016 12:32:04 +0200
branchkeysync
changeset 959 c1c463ecf30c
parent 954 a72e5c669833
child 964 ef02296c41ca
permissions -rw-r--r--
error handling in state machine
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@690
     5
#include "message.h"
vb@623
     6
#include "sync_fsm.h"
vb@951
     7
#include "map_asn1.h"
vb@623
     8
vb@951
     9
// conditions
vb@951
    10
vb@951
    11
static const char *sql_stored_group_keys =
vb@951
    12
        "select count(device_group) from person where id = "PEP_OWN_USERID";"; 
vb@951
    13
vb@951
    14
static int _stored_group_keys(void *_gc, int count, char **text, char **name)
vb@951
    15
{
vb@951
    16
    assert(_gc);
vb@951
    17
    assert(count == 1);
vb@951
    18
    assert(text && text[0]);
vb@951
    19
    if (!(_gc && count == 1 && text && text[0]))
vb@951
    20
        return -1;
vb@951
    21
vb@951
    22
    bool *gc = (bool *) _gc;
vb@951
    23
    *gc = atoi(text[0]) != 0;
vb@951
    24
    return 0;
vb@951
    25
}
vb@951
    26
vb@959
    27
int storedGroupKeys(PEP_SESSION session)
vb@951
    28
{
vb@951
    29
    assert(session);
vb@951
    30
    if (!session)
vb@959
    31
        return -1; // error
vb@951
    32
vb@951
    33
    bool gc = false;
vb@951
    34
    int int_result = sqlite3_exec(
vb@951
    35
        session->db,
vb@951
    36
        sql_stored_group_keys,
vb@951
    37
        _stored_group_keys,
vb@951
    38
        &gc,
vb@951
    39
        NULL
vb@951
    40
    );
vb@951
    41
    assert(int_result == SQLITE_OK);
vb@959
    42
    if (int_result != SQLITE_OK)
vb@959
    43
        return -1; // error
vb@959
    44
vb@959
    45
    if (gc)
vb@959
    46
        return 1;
vb@959
    47
    else
vb@959
    48
        return 0;
vb@951
    49
}
vb@951
    50
vb@959
    51
int keyElectionWon(PEP_SESSION session, Identity partner)
vb@951
    52
{
vb@951
    53
    assert(session);
vb@951
    54
    assert(partner);
vb@951
    55
    if (!(session && partner))
vb@959
    56
        return -1; // error
vb@951
    57
vb@951
    58
    // an already existing group always wins
vb@951
    59
vb@951
    60
    if (storedGroupKeys(session)) {
vb@951
    61
        assert(!(partner->flags & PEP_idf_devicegroup));
vb@959
    62
        return 1;
vb@951
    63
    }
vb@951
    64
vb@951
    65
    if (partner->flags & PEP_idf_devicegroup)
vb@959
    66
        return 0;
vb@951
    67
vb@951
    68
    Identity me = NULL;
vb@951
    69
    PEP_STATUS status = get_identity(session, partner->address, PEP_OWN_USERID,
vb@951
    70
            &me);
vb@951
    71
    if (status != PEP_STATUS_OK)
vb@959
    72
        return -1; // error
vb@951
    73
vb@959
    74
    int result = -1; // error state has to be overwritten
vb@951
    75
vb@959
    76
    time_t own_created;
vb@959
    77
    time_t partners_created;
vb@959
    78
vb@959
    79
    status = key_created(session, me->fpr, &own_created);
vb@959
    80
    if (status != PEP_STATUS_OK)
vb@959
    81
        goto the_end;
vb@959
    82
vb@959
    83
    status = key_created(session, partner->fpr, &partners_created);
vb@959
    84
    if (status != PEP_STATUS_OK)
vb@959
    85
        goto the_end;
vb@959
    86
vb@959
    87
    if (own_created > partners_created)
vb@959
    88
        result = 0;
vb@959
    89
    else
vb@959
    90
        result = 1;
vb@951
    91
vb@951
    92
the_end:
vb@951
    93
    free_identity(me);
vb@951
    94
    return result;
vb@951
    95
}
vb@650
    96
vb@884
    97
// showHandshake() - trigger the handshake dialog of the application
vb@623
    98
//
vb@623
    99
//  params:
vb@627
   100
//      session (in)        session handle
vb@690
   101
//      state (in)          state the state machine is in
vb@807
   102
//      partner (in)        partner to communicate with
vb@623
   103
//
vb@623
   104
//  returns:
vb@623
   105
//      PEP_STATUS_OK or any other value on error
vb@623
   106
vb@690
   107
PEP_STATUS showHandshake(
vb@690
   108
        PEP_SESSION session,
vb@690
   109
        DeviceState_state state,
vb@939
   110
        Identity partner,
vb@939
   111
        void *extra
vb@690
   112
    )
vb@623
   113
{
vb@623
   114
    PEP_STATUS status = PEP_STATUS_OK;
vb@939
   115
vb@650
   116
    assert(session);
vb@665
   117
    assert(partner);
vb@939
   118
    assert(extra == NULL);
vb@939
   119
vb@665
   120
    if (!(session && partner))
vb@665
   121
        return PEP_ILLEGAL_VALUE;
vb@939
   122
vb@907
   123
    assert(session->showHandshake);
vb@884
   124
    if (!session->showHandshake)
vb@884
   125
        return PEP_SYNC_NO_TRUSTWORDS_CALLBACK;
vb@623
   126
vb@884
   127
    pEp_identity *me = NULL;
vb@884
   128
    status = get_identity(session, partner->address, PEP_OWN_USERID, &me);
vb@884
   129
    if (status != PEP_STATUS_OK)
vb@884
   130
        goto error;
vb@884
   131
    
vb@884
   132
    status = session->showHandshake(session, me, partner);
vb@884
   133
    if (status != PEP_STATUS_OK)
vb@884
   134
        goto error;
vb@623
   135
vb@884
   136
    free_identity(me);
vb@951
   137
    free_identity(partner);
vb@623
   138
    return status;
vb@650
   139
vb@650
   140
enomem:
vb@650
   141
    status = PEP_OUT_OF_MEMORY;
vb@650
   142
error:
vb@884
   143
    free_identity(me);
vb@951
   144
    free_identity(partner);
vb@650
   145
    return status;
vb@623
   146
}
vb@623
   147
vb@650
   148
vb@907
   149
// reject() - stores rejection of partner
vb@623
   150
//
vb@623
   151
//  params:
vb@627
   152
//      session (in)        session handle
vb@690
   153
//      state (in)          state the state machine is in
vb@807
   154
//      partner (in)        partner to communicate with
vb@623
   155
//
vb@623
   156
//  returns:
vb@623
   157
//      PEP_STATUS_OK or any other value on error
vb@623
   158
vb@690
   159
PEP_STATUS reject(
vb@690
   160
        PEP_SESSION session,
vb@690
   161
        DeviceState_state state,
vb@939
   162
        Identity partner,
vb@939
   163
        void *extra
vb@690
   164
    )
vb@623
   165
{
vb@623
   166
    PEP_STATUS status = PEP_STATUS_OK;
vb@623
   167
vb@650
   168
    assert(session);
vb@665
   169
    assert(partner);
vb@939
   170
    assert(extra == NULL);
vb@665
   171
    if (!(session && partner))
vb@665
   172
        return PEP_ILLEGAL_VALUE;
vb@623
   173
vb@939
   174
    status = set_identity_flags(session, partner,
vb@939
   175
            partner->flags | PEP_idf_not_for_sync);
vb@623
   176
vb@951
   177
    free_identity(partner);
vb@623
   178
    return status;
vb@650
   179
vb@650
   180
enomem:
vb@650
   181
    status = PEP_OUT_OF_MEMORY;
vb@650
   182
error:
vb@951
   183
    free_identity(partner);
vb@650
   184
    // free...
vb@650
   185
    return status;
vb@623
   186
}
vb@623
   187
vb@650
   188
vb@657
   189
// storeGroupKeys() - 
vb@623
   190
//
vb@623
   191
//  params:
vb@627
   192
//      session (in)        session handle
vb@690
   193
//      state (in)          state the state machine is in
vb@807
   194
//      partner (in)        partner to communicate with
vb@939
   195
//      _group_keys (in)    group keys received from partner
vb@623
   196
//
vb@623
   197
//  returns:
vb@623
   198
//      PEP_STATUS_OK or any other value on error
vb@623
   199
vb@690
   200
PEP_STATUS storeGroupKeys(
vb@690
   201
        PEP_SESSION session,
vb@690
   202
        DeviceState_state state,
vb@939
   203
        Identity partner,
vb@939
   204
        void *_group_keys
vb@690
   205
    )
vb@623
   206
{
vb@623
   207
    PEP_STATUS status = PEP_STATUS_OK;
vb@623
   208
vb@650
   209
    assert(session);
vb@665
   210
    assert(partner);
vb@939
   211
    assert(_group_keys);
vb@939
   212
    if (!(session && partner && _group_keys))
vb@665
   213
        return PEP_ILLEGAL_VALUE;
vb@623
   214
vb@951
   215
    identity_list *group_keys = (identity_list *) _group_keys;
vb@951
   216
    if (!group_keys)
vb@951
   217
        goto enomem;
vb@623
   218
vb@951
   219
    free_identity(partner);
vb@951
   220
    free_identity_list(group_keys);
vb@623
   221
    return status;
vb@650
   222
vb@650
   223
enomem:
vb@650
   224
    status = PEP_OUT_OF_MEMORY;
vb@650
   225
error:
vb@650
   226
    // free...
vb@951
   227
    free_identity(partner);
vb@951
   228
    free_identity_list(group_keys);
vb@650
   229
    return status;
vb@623
   230
}
vb@623
   231
vb@954
   232
static PEP_STATUS receive_sync_msg(
vb@954
   233
        PEP_SESSION session,
vb@954
   234
        DeviceGroup_Protocol_t *msg
vb@954
   235
    )
vb@951
   236
{
vb@951
   237
    assert(session && msg && msg->present != DeviceGroup_Protocol_PR_NOTHING);
vb@951
   238
    if (!(session && msg && msg->present != DeviceGroup_Protocol_PR_NOTHING))
vb@951
   239
        return PEP_ILLEGAL_VALUE;
vb@951
   240
vb@951
   241
    void *extra = NULL;
vb@951
   242
    Identity partner = NULL;
vb@951
   243
    DeviceState_event event = DeviceState_event_NONE;
vb@951
   244
vb@951
   245
    switch (msg->present) {
vb@951
   246
        case DeviceGroup_Protocol_PR_beacon:
vb@951
   247
            partner = Identity_to_Struct(&msg->choice.beacon.header.me, NULL);
vb@951
   248
            if (!partner)
vb@951
   249
                return PEP_OUT_OF_MEMORY;
vb@951
   250
            event = Beacon;
vb@951
   251
            break;
vb@951
   252
vb@951
   253
        case DeviceGroup_Protocol_PR_handshakeRequest:
vb@954
   254
            partner = Identity_to_Struct(
vb@954
   255
                    &msg->choice.handshakeRequest.header.me, NULL);
vb@951
   256
            if (!partner)
vb@951
   257
                return PEP_OUT_OF_MEMORY;
vb@951
   258
            event = HandshakeRequest;
vb@951
   259
            break;
vb@951
   260
vb@951
   261
        case DeviceGroup_Protocol_PR_groupKeys:
vb@954
   262
            partner = Identity_to_Struct(&msg->choice.groupKeys.header.me,
vb@954
   263
                    NULL);
vb@951
   264
            if (!partner)
vb@951
   265
                return PEP_OUT_OF_MEMORY;
vb@951
   266
            identity_list *group_keys = IdentityList_to_identity_list(
vb@951
   267
                    &msg->choice.groupKeys.ownIdentities, NULL);
vb@951
   268
            if (!group_keys) {
vb@951
   269
                free_identity(partner);
vb@951
   270
                return PEP_OUT_OF_MEMORY;
vb@951
   271
            }
vb@951
   272
            extra = (void *) group_keys;
vb@951
   273
            event = GroupKeys;
vb@951
   274
            break;
vb@951
   275
vb@951
   276
        default:
vb@951
   277
            return PEP_SYNC_ILLEGAL_MESSAGE;
vb@951
   278
    }
vb@951
   279
vb@951
   280
    return fsm_DeviceState_inject(session, event, partner, extra);
vb@951
   281
}
vb@951
   282
vb@952
   283
PEP_STATUS receive_DeviceState_msg(PEP_SESSION session, message *src)
vb@952
   284
{
vb@952
   285
    assert(session && src);
vb@952
   286
    if (!(session && src))
vb@952
   287
        return PEP_ILLEGAL_VALUE;
vb@952
   288
vb@953
   289
    bool found = false;
vb@953
   290
vb@953
   291
    for (bloblist_t *bl = src->attachments; bl && bl->value; bl = bl->next) {
vb@953
   292
        if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp") == 0
vb@953
   293
                && bl->size) {
vb@953
   294
            DeviceGroup_Protocol_t *msg;
vb@954
   295
            uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol,
vb@953
   296
                    (void **) &msg, bl->value, bl->size);
vb@953
   297
            if (msg) {
vb@953
   298
                found = true;
vb@953
   299
                PEP_STATUS status = receive_sync_msg(session, msg);
vb@953
   300
                ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
vb@953
   301
                if (status != PEP_STATUS_OK)
vb@953
   302
                    return status;
vb@953
   303
            }
vb@953
   304
        }
vb@953
   305
    }
vb@953
   306
vb@953
   307
    if (found) {
vb@953
   308
        for (stringpair_list_t *spl = src->opt_fields ; spl && spl->value ;
vb@953
   309
                spl = spl->next) {
vb@953
   310
            if (spl->value->key &&
vb@953
   311
                    strcasecmp(spl->value->key, "pEp-auto-consume") == 0) {
vb@953
   312
                if (spl->value->value &&
vb@953
   313
                        strcasecmp(spl->value->value, "yes") == 0)
vb@953
   314
                    return PEP_MESSAGE_CONSUMED;
vb@953
   315
            }
vb@953
   316
        }
vb@953
   317
    }
vb@953
   318
vb@952
   319
    return PEP_STATUS_OK;
vb@952
   320
}
vb@952
   321