src/keymanagement.c
author Volker Birk <vb@pep-project.org>
Tue, 21 Apr 2015 15:40:18 +0200
changeset 215 fac4aa582acf
parent 214 79333debeee0
child 216 e949c98e45e5
permissions -rw-r--r--
DEBUG_LOG() for keymanagement
vb@130
     1
#include "platform.h"
vb@0
     2
vb@0
     3
#include <string.h>
vb@0
     4
#include <stdio.h>
vb@0
     5
#include <stdlib.h>
vb@0
     6
#include <assert.h>
vb@0
     7
vb@0
     8
#define _EXPORT_PEP_ENGINE_DLL
vb@0
     9
#include "pEpEngine.h"
vb@0
    10
#include "keymanagement.h"
vb@0
    11
vb@0
    12
#ifndef MIN
vb@0
    13
#define MIN(A, B) ((B) > (A) ? (A) : (B))
vb@0
    14
#endif
vb@0
    15
vb@8
    16
#ifndef EMPTY
vb@8
    17
#define EMPTY(STR) ((STR == NULL) || (STR)[0] == 0)
vb@8
    18
#endif
vb@8
    19
vb@214
    20
#define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
vb@214
    21
vb@215
    22
#ifdef NDEBUG
vb@215
    23
#define DEBUG_LOG(TITLE, ENTITY, DESC)
vb@215
    24
#else
vb@215
    25
#define DEBUG_LOG(TITLE, ENTITY, DESC) \
vb@215
    26
    log_event(session, (TITLE), (ENTITY), (DESC), "debug");
vb@215
    27
#endif
vb@215
    28
vb@0
    29
DYNAMIC_API PEP_STATUS update_identity(
vb@0
    30
        PEP_SESSION session, pEp_identity * identity
vb@0
    31
    )
vb@0
    32
{
vb@0
    33
    pEp_identity *stored_identity;
vb@0
    34
    PEP_STATUS status;
vb@0
    35
vb@0
    36
    assert(session);
vb@0
    37
    assert(identity);
vb@21
    38
    assert(!EMPTY(identity->address));
vb@0
    39
vb@191
    40
    if (!(session && identity && !EMPTY(identity->address)))
vb@191
    41
        return PEP_ILLEGAL_VALUE;
vb@191
    42
vb@0
    43
    status = get_identity(session, identity->address, &stored_identity);
vb@0
    44
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
    45
    if (status == PEP_OUT_OF_MEMORY)
vb@0
    46
        return PEP_OUT_OF_MEMORY;
vb@0
    47
vb@14
    48
    if (stored_identity) {
vb@14
    49
        PEP_comm_type _comm_type_key;
vb@14
    50
        status = get_key_rating(session, stored_identity->fpr, &_comm_type_key);
vb@14
    51
        assert(status != PEP_OUT_OF_MEMORY);
vb@14
    52
        if (status == PEP_OUT_OF_MEMORY)
vb@14
    53
            return PEP_OUT_OF_MEMORY;
vb@10
    54
vb@22
    55
        if (EMPTY(identity->user_id)) {
vb@22
    56
            free(identity->user_id);
vb@22
    57
            identity->user_id = strdup(stored_identity->user_id);
vb@22
    58
            if (identity->user_id == NULL)
vb@22
    59
                return PEP_OUT_OF_MEMORY;
vb@22
    60
            identity->user_id_size = stored_identity->user_id_size;
vb@22
    61
        }
vb@22
    62
vb@22
    63
        if (EMPTY(identity->username)) {
vb@22
    64
            free(identity->username);
vb@22
    65
            identity->username = strdup(stored_identity->username);
vb@22
    66
            if (identity->username == NULL)
vb@22
    67
                return PEP_OUT_OF_MEMORY;
vb@22
    68
            identity->username_size = stored_identity->username_size;
vb@22
    69
        }
vb@22
    70
vb@8
    71
        if (EMPTY(identity->fpr)) {
vb@8
    72
            identity->fpr = strdup(stored_identity->fpr);
vb@8
    73
            assert(identity->fpr);
vb@8
    74
            if (identity->fpr == NULL)
vb@8
    75
                return PEP_OUT_OF_MEMORY;
vb@8
    76
            identity->fpr_size = stored_identity->address_size;
vb@14
    77
            if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
vb@14
    78
                identity->comm_type = _comm_type_key;
vb@14
    79
            }
vb@14
    80
            else {
vb@14
    81
                identity->comm_type = stored_identity->comm_type;
vb@14
    82
            }
vb@8
    83
        }
vb@8
    84
        else /* !EMPTY(identity->fpr) */ {
vb@14
    85
            if (_comm_type_key != PEP_ct_unknown) {
vb@14
    86
                if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
vb@14
    87
                    identity->comm_type = _comm_type_key;
vb@14
    88
                }
vb@14
    89
                else if (identity->comm_type == PEP_ct_unknown) {
vb@8
    90
                    if (strcmp(identity->fpr, stored_identity->fpr) == 0) {
vb@8
    91
                        identity->comm_type = stored_identity->comm_type;
vb@8
    92
                    }
vb@8
    93
                    else {
vb@8
    94
                        status = get_trust(session, identity);
vb@8
    95
                        assert(status != PEP_OUT_OF_MEMORY);
vb@8
    96
                        if (status == PEP_OUT_OF_MEMORY)
vb@8
    97
                            return PEP_OUT_OF_MEMORY;
vb@8
    98
                    }
vb@8
    99
                }
vb@8
   100
            }
vb@8
   101
            else
vb@8
   102
                identity->comm_type = PEP_ct_unknown;
vb@8
   103
        }
vb@8
   104
vb@21
   105
        if (identity->lang[0] == 0) {
vb@21
   106
            identity->lang[0] = stored_identity->lang[0];
vb@21
   107
            identity->lang[1] = stored_identity->lang[1];
vb@21
   108
            identity->lang[2] = 0;
vb@21
   109
        }
vb@21
   110
    }
vb@21
   111
    else /* stored_identity == NULL */ {
vb@21
   112
        if (!EMPTY(identity->fpr)) {
vb@21
   113
            PEP_comm_type _comm_type_key;
vb@21
   114
vb@21
   115
            status = get_key_rating(session, identity->fpr, &_comm_type_key);
vb@21
   116
            assert(status != PEP_OUT_OF_MEMORY);
vb@21
   117
            if (status == PEP_OUT_OF_MEMORY)
vb@21
   118
                return PEP_OUT_OF_MEMORY;
vb@21
   119
vb@21
   120
            identity->comm_type = _comm_type_key;
vb@21
   121
        }
vb@21
   122
        else /* EMPTY(identity->fpr) */ {
vb@21
   123
            PEP_STATUS status;
vb@21
   124
            stringlist_t *keylist;
vb@21
   125
            char *_fpr = NULL;
vb@21
   126
            identity->comm_type = PEP_ct_unknown;
vb@21
   127
vb@21
   128
            status = find_keys(session, identity->address, &keylist);
vb@21
   129
            assert(status != PEP_OUT_OF_MEMORY);
vb@21
   130
            if (status == PEP_OUT_OF_MEMORY)
vb@21
   131
                return PEP_OUT_OF_MEMORY;
vb@21
   132
vb@21
   133
            stringlist_t *_keylist;
vb@21
   134
            for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
vb@21
   135
                PEP_comm_type _comm_type_key;
vb@21
   136
vb@21
   137
                status = get_key_rating(session, _keylist->value, &_comm_type_key);
vb@21
   138
                assert(status != PEP_OUT_OF_MEMORY);
vb@21
   139
                if (status == PEP_OUT_OF_MEMORY) {
vb@21
   140
                    free_stringlist(keylist);
vb@21
   141
                    return PEP_OUT_OF_MEMORY;
vb@21
   142
                }
vb@21
   143
vb@21
   144
                if (identity->comm_type == PEP_ct_unknown) {
vb@21
   145
                    if (_comm_type_key != PEP_ct_compromized && _comm_type_key != PEP_ct_unknown) {
vb@21
   146
                        identity->comm_type = _comm_type_key;
vb@21
   147
                        _fpr = _keylist->value;
vb@21
   148
                    }
vb@21
   149
                }
vb@21
   150
                else {
vb@21
   151
                    if (_comm_type_key != PEP_ct_compromized && _comm_type_key != PEP_ct_unknown) {
vb@21
   152
                        if (_comm_type_key > identity->comm_type) {
vb@21
   153
                            identity->comm_type = _comm_type_key;
vb@21
   154
                            _fpr = _keylist->value;
vb@21
   155
                        }
vb@21
   156
                    }
vb@21
   157
                }
vb@21
   158
            }
vb@21
   159
vb@21
   160
            if (_fpr) {
vb@21
   161
                free(identity->fpr);
vb@21
   162
vb@21
   163
                identity->fpr = strdup(_fpr);
vb@21
   164
                if (identity->fpr == NULL) {
vb@21
   165
                    free_stringlist(keylist);
vb@21
   166
                    return PEP_OUT_OF_MEMORY;
vb@21
   167
                }
vb@21
   168
                identity->fpr_size = strlen(identity->fpr);
vb@21
   169
            }
vb@21
   170
            free_stringlist(keylist);
vb@21
   171
        }
vb@21
   172
    }
vb@21
   173
vb@21
   174
    status = PEP_STATUS_OK;
vb@21
   175
vb@21
   176
    if (identity->comm_type != PEP_ct_unknown && !EMPTY(identity->user_id)) {
vb@22
   177
        assert(!EMPTY(identity->username)); // this should not happen
vb@22
   178
vb@22
   179
        if (EMPTY(identity->username)) { // mitigate
vb@0
   180
            free(identity->username);
vb@22
   181
            identity->username = strdup("anonymous");
vb@8
   182
            if (identity->username == NULL)
vb@8
   183
                return PEP_OUT_OF_MEMORY;
vb@22
   184
            identity->username_size = 9;
vb@0
   185
        }
vb@8
   186
vb@8
   187
        status = set_identity(session, identity);
vb@8
   188
        assert(status == PEP_STATUS_OK);
vb@8
   189
    }
vb@8
   190
vb@8
   191
    return status;
vb@0
   192
}
vb@0
   193
vb@0
   194
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
vb@0
   195
{
vb@0
   196
    PEP_STATUS status;
vb@46
   197
    stringlist_t *keylist = NULL;
vb@0
   198
vb@0
   199
    assert(session);
vb@0
   200
    assert(identity);
vb@0
   201
    assert(identity->address);
vb@0
   202
    assert(identity->username);
vb@0
   203
    assert(identity->user_id);
vb@0
   204
vb@191
   205
    if (!(session && identity && identity->address && identity->username &&
vb@191
   206
                identity->user_id))
vb@191
   207
        return PEP_ILLEGAL_VALUE;
vb@191
   208
vb@0
   209
    identity->comm_type = PEP_ct_pEp;
vb@0
   210
    identity->me = true;
vb@0
   211
vb@0
   212
    pEp_identity *_identity;
vb@0
   213
vb@215
   214
    DEBUG_LOG("myself", "debug", identity->address);
vb@0
   215
    status = get_identity(session, identity->address, &_identity);
vb@0
   216
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   217
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   218
        return PEP_OUT_OF_MEMORY;
vb@0
   219
vb@0
   220
    status = find_keys(session, identity->address, &keylist);
vb@0
   221
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   222
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   223
        return PEP_OUT_OF_MEMORY;
vb@0
   224
vb@0
   225
    if (keylist == NULL || keylist->value == NULL) {
vb@215
   226
        DEBUG_LOG("generating key pair", "debug", identity->address);
vb@0
   227
        status = generate_keypair(session, identity);
vb@0
   228
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   229
        if (status != PEP_STATUS_OK) {
vb@0
   230
            char buf[11];
vb@0
   231
            snprintf(buf, 11, "%d", status);
vb@215
   232
            DEBUG_LOG("generating key pair failed", "debug", buf);
vb@0
   233
            return status;
vb@0
   234
        }
vb@0
   235
vb@0
   236
        status = find_keys(session, identity->address, &keylist);
vb@0
   237
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   238
        if (status == PEP_OUT_OF_MEMORY)
vb@0
   239
            return PEP_OUT_OF_MEMORY;
vb@0
   240
vb@0
   241
        assert(keylist);
vb@0
   242
    }
vb@214
   243
    else {
vb@214
   244
        bool expired;
vb@214
   245
        status = key_expired(session, keylist->value, &expired);
vb@214
   246
        assert(status == PEP_STATUS_OK);
vb@214
   247
vb@214
   248
        if (status == PEP_STATUS_OK && expired) {
vb@214
   249
            timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
vb@214
   250
            renew_key(session, keylist->value, ts);
vb@214
   251
            free_timestamp(ts);
vb@214
   252
        }
vb@214
   253
    }
vb@0
   254
vb@0
   255
    if (identity->fpr)
vb@0
   256
        free(identity->fpr);
vb@0
   257
    identity->fpr = strdup(keylist->value);
vb@0
   258
    assert(identity->fpr);
vb@0
   259
    free_stringlist(keylist);
vb@0
   260
    if (identity->fpr == NULL)
vb@0
   261
        return PEP_OUT_OF_MEMORY;
vb@8
   262
    identity->fpr_size = strlen(identity->fpr);
vb@0
   263
vb@0
   264
    status = set_identity(session, identity);
vb@0
   265
    assert(status == PEP_STATUS_OK);
vb@0
   266
vb@0
   267
    return PEP_STATUS_OK;
vb@0
   268
}
vb@0
   269
vb@0
   270
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   271
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   272
        void *management
vb@0
   273
    )
vb@0
   274
{
vb@0
   275
    PEP_SESSION session;
vb@0
   276
    pEp_identity *identity;
vb@0
   277
    PEP_STATUS status = init(&session);
vb@0
   278
vb@0
   279
    assert(status == PEP_STATUS_OK);
vb@0
   280
    if (status != PEP_STATUS_OK)
vb@0
   281
        return status;
vb@0
   282
vb@24
   283
    assert(retrieve_next_identity);
vb@24
   284
    assert(management);
vb@24
   285
vb@0
   286
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   287
vb@36
   288
    while ((identity = retrieve_next_identity(management))) {
vb@0
   289
        assert(identity->address);
vb@215
   290
        DEBUG_LOG("do_keymanagement", "debug", identity->address);
vb@0
   291
        if (identity->me) {
vb@0
   292
            status = myself(session, identity);
vb@0
   293
            assert(status != PEP_OUT_OF_MEMORY);
vb@0
   294
        } else {
vb@0
   295
            status = recv_key(session, identity->address);
vb@0
   296
            assert(status != PEP_OUT_OF_MEMORY);
vb@0
   297
        }
vb@0
   298
        free_identity(identity);
vb@0
   299
    }
vb@0
   300
vb@0
   301
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   302
vb@0
   303
    release(session);
vb@0
   304
    return PEP_STATUS_OK;
vb@0
   305
}
vb@0
   306
vb@215
   307
DYNAMIC_API PEP_STATUS key_compromized(PEP_SESSION session, const char *fpr)
vb@215
   308
{
vb@215
   309
    assert(session);
vb@215
   310
    assert(fpr);
vb@215
   311
vb@215
   312
    if (!(session && fpr))
vb@215
   313
        return PEP_ILLEGAL_VALUE;
vb@215
   314
vb@215
   315
    return PEP_STATUS_OK;
vb@215
   316
}
vb@215
   317