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