src/sync_actions.c
author Volker Birk <vb@pep.foundation>
Mon, 08 Aug 2016 17:32:51 +0200
branchkeysync
changeset 985 087d5d60c082
parent 965 2c833e0a883b
child 991 b230b6418b52
permissions -rw-r--r--
moving #include and installing headers
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@884
   139
    free_identity(me);
vb@951
   140
    free_identity(partner);
vb@623
   141
    return status;
vb@650
   142
vb@650
   143
error:
vb@884
   144
    free_identity(me);
vb@951
   145
    free_identity(partner);
vb@650
   146
    return status;
vb@623
   147
}
vb@623
   148
vb@650
   149
vb@907
   150
// reject() - stores rejection of partner
vb@623
   151
//
vb@623
   152
//  params:
vb@627
   153
//      session (in)        session handle
vb@690
   154
//      state (in)          state the state machine is in
vb@807
   155
//      partner (in)        partner to communicate with
vb@623
   156
//
vb@623
   157
//  returns:
vb@623
   158
//      PEP_STATUS_OK or any other value on error
vb@623
   159
vb@690
   160
PEP_STATUS reject(
vb@690
   161
        PEP_SESSION session,
vb@690
   162
        DeviceState_state state,
vb@939
   163
        Identity partner,
vb@939
   164
        void *extra
vb@690
   165
    )
vb@623
   166
{
vb@623
   167
    PEP_STATUS status = PEP_STATUS_OK;
vb@623
   168
vb@650
   169
    assert(session);
vb@665
   170
    assert(partner);
vb@939
   171
    assert(extra == NULL);
vb@665
   172
    if (!(session && partner))
vb@665
   173
        return PEP_ILLEGAL_VALUE;
vb@623
   174
vb@939
   175
    status = set_identity_flags(session, partner,
vb@939
   176
            partner->flags | PEP_idf_not_for_sync);
vb@623
   177
vb@951
   178
    free_identity(partner);
vb@623
   179
    return status;
vb@623
   180
}
vb@623
   181
vb@650
   182
vb@657
   183
// storeGroupKeys() - 
vb@623
   184
//
vb@623
   185
//  params:
vb@627
   186
//      session (in)        session handle
vb@690
   187
//      state (in)          state the state machine is in
vb@807
   188
//      partner (in)        partner to communicate with
vb@939
   189
//      _group_keys (in)    group keys received from partner
vb@623
   190
//
vb@623
   191
//  returns:
vb@623
   192
//      PEP_STATUS_OK or any other value on error
vb@623
   193
vb@690
   194
PEP_STATUS storeGroupKeys(
vb@690
   195
        PEP_SESSION session,
vb@690
   196
        DeviceState_state state,
vb@939
   197
        Identity partner,
vb@939
   198
        void *_group_keys
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@939
   205
    assert(_group_keys);
vb@939
   206
    if (!(session && partner && _group_keys))
vb@665
   207
        return PEP_ILLEGAL_VALUE;
vb@623
   208
vb@951
   209
    identity_list *group_keys = (identity_list *) _group_keys;
vb@623
   210
vb@964
   211
    for (identity_list *il = group_keys; il && il->ident; il = il->next) {
vb@964
   212
        free(il->ident->user_id);
vb@964
   213
        il->ident->user_id = strdup(PEP_OWN_USERID);
vb@964
   214
        assert(il->ident->user_id);
vb@964
   215
        if (!il->ident->user_id)
vb@964
   216
            goto enomem;
vb@964
   217
        status = set_identity(session, il->ident);
vb@964
   218
        if (status != PEP_STATUS_OK)
vb@964
   219
            break;
vb@964
   220
    }
vb@964
   221
vb@951
   222
    free_identity(partner);
vb@951
   223
    free_identity_list(group_keys);
vb@623
   224
    return status;
vb@650
   225
vb@650
   226
enomem:
vb@650
   227
    status = PEP_OUT_OF_MEMORY;
vb@951
   228
    free_identity(partner);
vb@951
   229
    free_identity_list(group_keys);
vb@650
   230
    return status;
vb@623
   231
}
vb@623
   232
vb@954
   233
static PEP_STATUS receive_sync_msg(
vb@954
   234
        PEP_SESSION session,
vb@954
   235
        DeviceGroup_Protocol_t *msg
vb@954
   236
    )
vb@951
   237
{
vb@951
   238
    assert(session && msg && msg->present != DeviceGroup_Protocol_PR_NOTHING);
vb@951
   239
    if (!(session && msg && msg->present != DeviceGroup_Protocol_PR_NOTHING))
vb@951
   240
        return PEP_ILLEGAL_VALUE;
vb@951
   241
vb@951
   242
    void *extra = NULL;
vb@951
   243
    Identity partner = NULL;
vb@951
   244
    DeviceState_event event = DeviceState_event_NONE;
vb@951
   245
vb@951
   246
    switch (msg->present) {
vb@951
   247
        case DeviceGroup_Protocol_PR_beacon:
vb@951
   248
            partner = Identity_to_Struct(&msg->choice.beacon.header.me, NULL);
vb@951
   249
            if (!partner)
vb@951
   250
                return PEP_OUT_OF_MEMORY;
vb@951
   251
            event = Beacon;
vb@951
   252
            break;
vb@951
   253
vb@951
   254
        case DeviceGroup_Protocol_PR_handshakeRequest:
vb@954
   255
            partner = Identity_to_Struct(
vb@954
   256
                    &msg->choice.handshakeRequest.header.me, NULL);
vb@951
   257
            if (!partner)
vb@951
   258
                return PEP_OUT_OF_MEMORY;
vb@951
   259
            event = HandshakeRequest;
vb@951
   260
            break;
vb@951
   261
vb@951
   262
        case DeviceGroup_Protocol_PR_groupKeys:
vb@954
   263
            partner = Identity_to_Struct(&msg->choice.groupKeys.header.me,
vb@954
   264
                    NULL);
vb@951
   265
            if (!partner)
vb@951
   266
                return PEP_OUT_OF_MEMORY;
vb@951
   267
            identity_list *group_keys = IdentityList_to_identity_list(
vb@951
   268
                    &msg->choice.groupKeys.ownIdentities, NULL);
vb@951
   269
            if (!group_keys) {
vb@951
   270
                free_identity(partner);
vb@951
   271
                return PEP_OUT_OF_MEMORY;
vb@951
   272
            }
vb@951
   273
            extra = (void *) group_keys;
vb@951
   274
            event = GroupKeys;
vb@951
   275
            break;
vb@951
   276
vb@951
   277
        default:
vb@951
   278
            return PEP_SYNC_ILLEGAL_MESSAGE;
vb@951
   279
    }
vb@951
   280
vb@951
   281
    return fsm_DeviceState_inject(session, event, partner, extra);
vb@951
   282
}
vb@951
   283
vb@952
   284
PEP_STATUS receive_DeviceState_msg(PEP_SESSION session, message *src)
vb@952
   285
{
vb@952
   286
    assert(session && src);
vb@952
   287
    if (!(session && src))
vb@952
   288
        return PEP_ILLEGAL_VALUE;
vb@952
   289
vb@953
   290
    bool found = false;
vb@953
   291
vb@953
   292
    for (bloblist_t *bl = src->attachments; bl && bl->value; bl = bl->next) {
vb@953
   293
        if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp") == 0
vb@953
   294
                && bl->size) {
vb@953
   295
            DeviceGroup_Protocol_t *msg;
vb@954
   296
            uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol,
vb@953
   297
                    (void **) &msg, bl->value, bl->size);
vb@953
   298
            if (msg) {
vb@953
   299
                found = true;
vb@953
   300
                PEP_STATUS status = receive_sync_msg(session, msg);
vb@953
   301
                ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
vb@953
   302
                if (status != PEP_STATUS_OK)
vb@953
   303
                    return status;
vb@953
   304
            }
vb@953
   305
        }
vb@953
   306
    }
vb@953
   307
vb@953
   308
    if (found) {
vb@953
   309
        for (stringpair_list_t *spl = src->opt_fields ; spl && spl->value ;
vb@953
   310
                spl = spl->next) {
vb@953
   311
            if (spl->value->key &&
vb@953
   312
                    strcasecmp(spl->value->key, "pEp-auto-consume") == 0) {
vb@953
   313
                if (spl->value->value &&
vb@953
   314
                        strcasecmp(spl->value->value, "yes") == 0)
vb@953
   315
                    return PEP_MESSAGE_CONSUMED;
vb@953
   316
            }
vb@953
   317
        }
vb@953
   318
    }
vb@953
   319
vb@952
   320
    return PEP_STATUS_OK;
vb@952
   321
}
vb@952
   322