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