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