src/sync_actions.c
author Volker Birk <vb@pep.foundation>
Thu, 01 Sep 2016 17:55:03 +0200
branchkeysync
changeset 1111 3c46dc58096d
parent 1047 2a4a2ec1340b
child 1158 91f23957e964
permissions -rw-r--r--
merging
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@1046
     8
#include "baseprotocol.h"
vb@623
     9
vb@951
    10
// conditions
vb@951
    11
vb@951
    12
static const char *sql_stored_group_keys =
vb@1004
    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@959
    76
vb@964
    77
    int result = invalid_condition; // error state has to be overwritten
vb@959
    78
vb@959
    79
    time_t own_created;
vb@959
    80
    time_t partners_created;
vb@951
    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@951
    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