src/keymanagement.c
author vb
Wed, 20 Aug 2014 16:11:46 +0200
changeset 21 89f925c7d5be
parent 14 c99db0b8fc0f
child 22 e235409d570a
permissions -rw-r--r--
bugfix: when identity is not stored yet, update_identity failes
vb@0
     1
#ifndef WIN32 // UNIX
vb@0
     2
#define _POSIX_C_SOURCE 200809L
vb@0
     3
#else
vb@0
     4
#include "platform_windows.h"
vb@0
     5
#endif
vb@0
     6
vb@0
     7
#include <string.h>
vb@0
     8
#include <stdio.h>
vb@0
     9
#include <stdlib.h>
vb@0
    10
#include <assert.h>
vb@0
    11
vb@0
    12
#define _EXPORT_PEP_ENGINE_DLL
vb@0
    13
#include "pEpEngine.h"
vb@0
    14
#include "keymanagement.h"
vb@0
    15
vb@0
    16
#ifndef MIN
vb@0
    17
#define MIN(A, B) ((B) > (A) ? (A) : (B))
vb@0
    18
#endif
vb@0
    19
vb@8
    20
#ifndef EMPTY
vb@8
    21
#define EMPTY(STR) ((STR == NULL) || (STR)[0] == 0)
vb@8
    22
#endif
vb@8
    23
vb@0
    24
DYNAMIC_API PEP_STATUS update_identity(
vb@0
    25
        PEP_SESSION session, pEp_identity * identity
vb@0
    26
    )
vb@0
    27
{
vb@0
    28
    pEp_identity *stored_identity;
vb@0
    29
    PEP_STATUS status;
vb@0
    30
vb@0
    31
    assert(session);
vb@0
    32
    assert(identity);
vb@21
    33
    assert(!EMPTY(identity->address));
vb@0
    34
vb@0
    35
    status = get_identity(session, identity->address, &stored_identity);
vb@0
    36
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
    37
    if (status == PEP_OUT_OF_MEMORY)
vb@0
    38
        return PEP_OUT_OF_MEMORY;
vb@0
    39
vb@14
    40
    if (stored_identity) {
vb@14
    41
        PEP_comm_type _comm_type_key;
vb@14
    42
        status = get_key_rating(session, stored_identity->fpr, &_comm_type_key);
vb@14
    43
        assert(status != PEP_OUT_OF_MEMORY);
vb@14
    44
        if (status == PEP_OUT_OF_MEMORY)
vb@14
    45
            return PEP_OUT_OF_MEMORY;
vb@10
    46
vb@8
    47
        if (EMPTY(identity->fpr)) {
vb@8
    48
            identity->fpr = strdup(stored_identity->fpr);
vb@8
    49
            assert(identity->fpr);
vb@8
    50
            if (identity->fpr == NULL)
vb@8
    51
                return PEP_OUT_OF_MEMORY;
vb@8
    52
            identity->fpr_size = stored_identity->address_size;
vb@14
    53
            if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
vb@14
    54
                identity->comm_type = _comm_type_key;
vb@14
    55
            }
vb@14
    56
            else {
vb@14
    57
                identity->comm_type = stored_identity->comm_type;
vb@14
    58
            }
vb@8
    59
        }
vb@8
    60
        else /* !EMPTY(identity->fpr) */ {
vb@14
    61
            if (_comm_type_key != PEP_ct_unknown) {
vb@14
    62
                if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
vb@14
    63
                    identity->comm_type = _comm_type_key;
vb@14
    64
                }
vb@14
    65
                else if (identity->comm_type == PEP_ct_unknown) {
vb@8
    66
                    if (strcmp(identity->fpr, stored_identity->fpr) == 0) {
vb@8
    67
                        identity->comm_type = stored_identity->comm_type;
vb@8
    68
                    }
vb@8
    69
                    else {
vb@8
    70
                        status = get_trust(session, identity);
vb@8
    71
                        assert(status != PEP_OUT_OF_MEMORY);
vb@8
    72
                        if (status == PEP_OUT_OF_MEMORY)
vb@8
    73
                            return PEP_OUT_OF_MEMORY;
vb@8
    74
                    }
vb@8
    75
                }
vb@8
    76
            }
vb@8
    77
            else
vb@8
    78
                identity->comm_type = PEP_ct_unknown;
vb@8
    79
        }
vb@8
    80
vb@21
    81
        if (identity->lang[0] == 0) {
vb@21
    82
            identity->lang[0] = stored_identity->lang[0];
vb@21
    83
            identity->lang[1] = stored_identity->lang[1];
vb@21
    84
            identity->lang[2] = 0;
vb@21
    85
        }
vb@21
    86
    }
vb@21
    87
    else /* stored_identity == NULL */ {
vb@21
    88
        if (!EMPTY(identity->fpr)) {
vb@21
    89
            PEP_comm_type _comm_type_key;
vb@21
    90
vb@21
    91
            status = get_key_rating(session, identity->fpr, &_comm_type_key);
vb@21
    92
            assert(status != PEP_OUT_OF_MEMORY);
vb@21
    93
            if (status == PEP_OUT_OF_MEMORY)
vb@21
    94
                return PEP_OUT_OF_MEMORY;
vb@21
    95
vb@21
    96
            identity->comm_type = _comm_type_key;
vb@21
    97
        }
vb@21
    98
        else /* EMPTY(identity->fpr) */ {
vb@21
    99
            PEP_STATUS status;
vb@21
   100
            stringlist_t *keylist;
vb@21
   101
            char *_fpr = NULL;
vb@21
   102
            identity->comm_type = PEP_ct_unknown;
vb@21
   103
vb@21
   104
            status = find_keys(session, identity->address, &keylist);
vb@21
   105
            assert(status != PEP_OUT_OF_MEMORY);
vb@21
   106
            if (status == PEP_OUT_OF_MEMORY)
vb@21
   107
                return PEP_OUT_OF_MEMORY;
vb@21
   108
vb@21
   109
            stringlist_t *_keylist;
vb@21
   110
            for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
vb@21
   111
                PEP_comm_type _comm_type_key;
vb@21
   112
vb@21
   113
                status = get_key_rating(session, _keylist->value, &_comm_type_key);
vb@21
   114
                assert(status != PEP_OUT_OF_MEMORY);
vb@21
   115
                if (status == PEP_OUT_OF_MEMORY) {
vb@21
   116
                    free_stringlist(keylist);
vb@21
   117
                    return PEP_OUT_OF_MEMORY;
vb@21
   118
                }
vb@21
   119
vb@21
   120
                if (identity->comm_type == PEP_ct_unknown) {
vb@21
   121
                    if (_comm_type_key != PEP_ct_compromized && _comm_type_key != PEP_ct_unknown) {
vb@21
   122
                        identity->comm_type = _comm_type_key;
vb@21
   123
                        _fpr = _keylist->value;
vb@21
   124
                    }
vb@21
   125
                }
vb@21
   126
                else {
vb@21
   127
                    if (_comm_type_key != PEP_ct_compromized && _comm_type_key != PEP_ct_unknown) {
vb@21
   128
                        if (_comm_type_key > identity->comm_type) {
vb@21
   129
                            identity->comm_type = _comm_type_key;
vb@21
   130
                            _fpr = _keylist->value;
vb@21
   131
                        }
vb@21
   132
                    }
vb@21
   133
                }
vb@21
   134
            }
vb@21
   135
vb@21
   136
            if (_fpr) {
vb@21
   137
                free(identity->fpr);
vb@21
   138
vb@21
   139
                identity->fpr = strdup(_fpr);
vb@21
   140
                if (identity->fpr == NULL) {
vb@21
   141
                    free_stringlist(keylist);
vb@21
   142
                    return PEP_OUT_OF_MEMORY;
vb@21
   143
                }
vb@21
   144
                identity->fpr_size = strlen(identity->fpr);
vb@21
   145
            }
vb@21
   146
            free_stringlist(keylist);
vb@21
   147
        }
vb@21
   148
    }
vb@21
   149
vb@21
   150
    status = PEP_STATUS_OK;
vb@21
   151
vb@21
   152
    if (identity->comm_type != PEP_ct_unknown && !EMPTY(identity->user_id)) {
vb@8
   153
        if (EMPTY(identity->username)) {
vb@0
   154
            free(identity->username);
vb@0
   155
            identity->username = strdup(stored_identity->username);
vb@8
   156
            assert(identity->username);
vb@8
   157
            if (identity->username == NULL)
vb@8
   158
                return PEP_OUT_OF_MEMORY;
vb@8
   159
            identity->username_size = stored_identity->username_size;
vb@0
   160
        }
vb@8
   161
vb@8
   162
        status = set_identity(session, identity);
vb@8
   163
        assert(status == PEP_STATUS_OK);
vb@8
   164
    }
vb@8
   165
vb@8
   166
    return status;
vb@0
   167
}
vb@0
   168
vb@0
   169
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
vb@0
   170
{
vb@0
   171
    PEP_STATUS status;
vb@0
   172
    stringlist_t *keylist;
vb@0
   173
vb@0
   174
    assert(session);
vb@0
   175
    assert(identity);
vb@0
   176
    assert(identity->address);
vb@0
   177
    assert(identity->username);
vb@0
   178
    assert(identity->user_id);
vb@0
   179
vb@0
   180
    identity->comm_type = PEP_ct_pEp;
vb@0
   181
    identity->me = true;
vb@0
   182
vb@0
   183
    pEp_identity *_identity;
vb@0
   184
vb@0
   185
    log_event(session, "myself", "debug", identity->address, NULL);
vb@0
   186
    status = get_identity(session, identity->address, &_identity);
vb@0
   187
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   188
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   189
        return PEP_OUT_OF_MEMORY;
vb@0
   190
vb@0
   191
    status = find_keys(session, identity->address, &keylist);
vb@0
   192
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   193
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   194
        return PEP_OUT_OF_MEMORY;
vb@0
   195
vb@0
   196
    if (keylist == NULL || keylist->value == NULL) {
vb@0
   197
        log_event(session, "generating key pair", "debug", identity->address, NULL);
vb@0
   198
        status = generate_keypair(session, identity);
vb@0
   199
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   200
        if (status != PEP_STATUS_OK) {
vb@0
   201
            char buf[11];
vb@0
   202
            snprintf(buf, 11, "%d", status);
vb@0
   203
            log_event(session, "generating key pair failed", "debug", buf, NULL);
vb@0
   204
            return status;
vb@0
   205
        }
vb@0
   206
vb@0
   207
        status = find_keys(session, identity->address, &keylist);
vb@0
   208
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   209
        if (status == PEP_OUT_OF_MEMORY)
vb@0
   210
            return PEP_OUT_OF_MEMORY;
vb@0
   211
vb@0
   212
        assert(keylist);
vb@0
   213
    }
vb@0
   214
vb@0
   215
    if (identity->fpr)
vb@0
   216
        free(identity->fpr);
vb@0
   217
    identity->fpr = strdup(keylist->value);
vb@0
   218
    assert(identity->fpr);
vb@0
   219
    free_stringlist(keylist);
vb@0
   220
    if (identity->fpr == NULL)
vb@0
   221
        return PEP_OUT_OF_MEMORY;
vb@8
   222
    identity->fpr_size = strlen(identity->fpr);
vb@0
   223
vb@0
   224
    status = set_identity(session, identity);
vb@0
   225
    assert(status == PEP_STATUS_OK);
vb@0
   226
vb@0
   227
    return PEP_STATUS_OK;
vb@0
   228
}
vb@0
   229
vb@0
   230
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   231
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   232
        void *management
vb@0
   233
    )
vb@0
   234
{
vb@0
   235
    PEP_SESSION session;
vb@0
   236
    pEp_identity *identity;
vb@0
   237
    PEP_STATUS status = init(&session);
vb@0
   238
vb@0
   239
    assert(status == PEP_STATUS_OK);
vb@0
   240
    if (status != PEP_STATUS_OK)
vb@0
   241
        return status;
vb@0
   242
vb@0
   243
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   244
vb@0
   245
    while (identity = retrieve_next_identity(management)) {
vb@0
   246
        assert(identity->address);
vb@0
   247
        log_event(session, "do_keymanagement", "debug", identity->address, NULL);
vb@0
   248
        if (identity->me) {
vb@0
   249
            status = myself(session, identity);
vb@0
   250
            assert(status != PEP_OUT_OF_MEMORY);
vb@0
   251
        } else {
vb@0
   252
            status = recv_key(session, identity->address);
vb@0
   253
            assert(status != PEP_OUT_OF_MEMORY);
vb@0
   254
        }
vb@0
   255
        free_identity(identity);
vb@0
   256
    }
vb@0
   257
vb@0
   258
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   259
vb@0
   260
    release(session);
vb@0
   261
    return PEP_STATUS_OK;
vb@0
   262
}
vb@0
   263