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