src/keymanagement.c
author vb
Wed, 25 Jun 2014 18:44:58 +0200
changeset 0 16f27efbef98
child 8 26cc9f0228f4
permissions -rw-r--r--
initial commit
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@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
    bool bDirty;
vb@0
    27
vb@0
    28
    assert(session);
vb@0
    29
    assert(identity);
vb@0
    30
    assert(identity->address);
vb@0
    31
vb@0
    32
    status = get_identity(session, identity->address, &stored_identity);
vb@0
    33
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
    34
    if (status == PEP_OUT_OF_MEMORY)
vb@0
    35
        return PEP_OUT_OF_MEMORY;
vb@0
    36
vb@0
    37
    if (stored_identity) {
vb@0
    38
        if (identity->username == NULL || identity->username[0] == 0) {
vb@0
    39
            free(identity->username);
vb@0
    40
            identity->username = strdup(stored_identity->username);
vb@0
    41
        }
vb@0
    42
        if (identity->user_id == NULL || identity->user_id[0] == 0) {
vb@0
    43
            free(identity->user_id);
vb@0
    44
            identity->user_id = strdup(stored_identity->user_id);
vb@0
    45
        }
vb@0
    46
        if (identity->fpr != NULL && identity->fpr[0] != 0) {
vb@0
    47
            if (strcmp(identity->fpr, stored_identity->fpr) != 0)
vb@0
    48
                identity->comm_type = PEP_ct_unknown;
vb@0
    49
        }
vb@0
    50
    }
vb@0
    51
    else
vb@0
    52
        identity->comm_type = PEP_ct_unknown;
vb@0
    53
vb@0
    54
    status = set_identity(session, identity);
vb@0
    55
vb@0
    56
    return PEP_STATUS_OK;
vb@0
    57
}
vb@0
    58
vb@0
    59
DYNAMIC_API PEP_STATUS outgoing_comm_type(
vb@0
    60
        PEP_SESSION session,
vb@0
    61
        const stringlist_t *addresses,
vb@0
    62
        PEP_comm_type *comm_type
vb@0
    63
    )
vb@0
    64
{
vb@0
    65
    int i;
vb@0
    66
    const stringlist_t *l;
vb@0
    67
vb@0
    68
    assert(session);
vb@0
    69
    assert(addresses);
vb@0
    70
    assert(addresses->value);
vb@0
    71
    assert(comm_type);
vb@0
    72
vb@0
    73
    *comm_type = PEP_ct_unknown;
vb@0
    74
vb@0
    75
    for (l=addresses; l && l->value; l = l->next) {
vb@0
    76
        PEP_STATUS _status;
vb@0
    77
        pEp_identity *identity;
vb@0
    78
vb@0
    79
        _status = get_identity(session, l->value, &identity);
vb@0
    80
        assert(_status != PEP_OUT_OF_MEMORY);
vb@0
    81
vb@0
    82
        if (identity == NULL) {
vb@0
    83
            *comm_type = PEP_ct_no_encryption;
vb@0
    84
            return PEP_STATUS_OK;
vb@0
    85
        }
vb@0
    86
        else if (identity->comm_type == PEP_ct_unknown) {
vb@0
    87
            *comm_type = PEP_ct_no_encryption;
vb@0
    88
            free_identity(identity);
vb@0
    89
            return PEP_STATUS_OK;
vb@0
    90
        }
vb@0
    91
        else if (*comm_type == PEP_ct_unknown) {
vb@0
    92
            *comm_type = identity->comm_type;
vb@0
    93
        }
vb@0
    94
        else if (*comm_type != identity->comm_type) {
vb@0
    95
            PEP_comm_type min = MIN(*comm_type, identity->comm_type);
vb@0
    96
            if (min < PEP_ct_unconfirmed_encryption) {
vb@0
    97
                *comm_type = PEP_ct_no_encryption;
vb@0
    98
                free_identity(identity);
vb@0
    99
                return PEP_STATUS_OK;
vb@0
   100
            }
vb@0
   101
            else if (min < PEP_ct_unconfirmed_enc_anon)
vb@0
   102
                *comm_type = PEP_ct_unconfirmed_encryption;
vb@0
   103
            else if (min < PEP_ct_confirmed_encryption)
vb@0
   104
                *comm_type = PEP_ct_unconfirmed_enc_anon;
vb@0
   105
            else if (min < PEP_ct_confirmed_enc_anon)
vb@0
   106
                *comm_type = PEP_ct_confirmed_encryption;
vb@0
   107
            else
vb@0
   108
                *comm_type = PEP_ct_confirmed_enc_anon;
vb@0
   109
        }
vb@0
   110
vb@0
   111
        free_identity(identity);
vb@0
   112
    }
vb@0
   113
vb@0
   114
    return PEP_STATUS_OK;
vb@0
   115
}
vb@0
   116
vb@0
   117
DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
vb@0
   118
{
vb@0
   119
    PEP_STATUS status;
vb@0
   120
    stringlist_t *keylist;
vb@0
   121
vb@0
   122
    assert(session);
vb@0
   123
    assert(identity);
vb@0
   124
    assert(identity->address);
vb@0
   125
    assert(identity->username);
vb@0
   126
    assert(identity->user_id);
vb@0
   127
vb@0
   128
    identity->comm_type = PEP_ct_pEp;
vb@0
   129
    identity->me = true;
vb@0
   130
vb@0
   131
    pEp_identity *_identity;
vb@0
   132
vb@0
   133
    log_event(session, "myself", "debug", identity->address, NULL);
vb@0
   134
    status = get_identity(session, identity->address, &_identity);
vb@0
   135
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   136
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   137
        return PEP_OUT_OF_MEMORY;
vb@0
   138
vb@0
   139
    status = find_keys(session, identity->address, &keylist);
vb@0
   140
    assert(status != PEP_OUT_OF_MEMORY);
vb@0
   141
    if (status == PEP_OUT_OF_MEMORY)
vb@0
   142
        return PEP_OUT_OF_MEMORY;
vb@0
   143
vb@0
   144
    if (keylist == NULL || keylist->value == NULL) {
vb@0
   145
        log_event(session, "generating key pair", "debug", identity->address, NULL);
vb@0
   146
        status = generate_keypair(session, identity);
vb@0
   147
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   148
        if (status != PEP_STATUS_OK) {
vb@0
   149
            char buf[11];
vb@0
   150
            snprintf(buf, 11, "%d", status);
vb@0
   151
            log_event(session, "generating key pair failed", "debug", buf, NULL);
vb@0
   152
            return status;
vb@0
   153
        }
vb@0
   154
vb@0
   155
        status = find_keys(session, identity->address, &keylist);
vb@0
   156
        assert(status != PEP_OUT_OF_MEMORY);
vb@0
   157
        if (status == PEP_OUT_OF_MEMORY)
vb@0
   158
            return PEP_OUT_OF_MEMORY;
vb@0
   159
vb@0
   160
        assert(keylist);
vb@0
   161
    }
vb@0
   162
vb@0
   163
    if (identity->fpr)
vb@0
   164
        free(identity->fpr);
vb@0
   165
    identity->fpr = strdup(keylist->value);
vb@0
   166
    assert(identity->fpr);
vb@0
   167
    free_stringlist(keylist);
vb@0
   168
    if (identity->fpr == NULL)
vb@0
   169
        return PEP_OUT_OF_MEMORY;
vb@0
   170
vb@0
   171
    status = set_identity(session, identity);
vb@0
   172
    assert(status == PEP_STATUS_OK);
vb@0
   173
vb@0
   174
    return PEP_STATUS_OK;
vb@0
   175
}
vb@0
   176
vb@0
   177
DYNAMIC_API PEP_STATUS do_keymanagement(
vb@0
   178
        retrieve_next_identity_t retrieve_next_identity,
vb@0
   179
        void *management
vb@0
   180
    )
vb@0
   181
{
vb@0
   182
    PEP_SESSION session;
vb@0
   183
    pEp_identity *identity;
vb@0
   184
    PEP_STATUS status = init(&session);
vb@0
   185
vb@0
   186
    assert(status == PEP_STATUS_OK);
vb@0
   187
    if (status != PEP_STATUS_OK)
vb@0
   188
        return status;
vb@0
   189
vb@0
   190
    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
vb@0
   191
vb@0
   192
    while (identity = retrieve_next_identity(management)) {
vb@0
   193
        assert(identity->address);
vb@0
   194
        log_event(session, "do_keymanagement", "debug", identity->address, NULL);
vb@0
   195
        if (identity->me) {
vb@0
   196
            status = myself(session, identity);
vb@0
   197
            assert(status != PEP_OUT_OF_MEMORY);
vb@0
   198
        } else {
vb@0
   199
            status = recv_key(session, identity->address);
vb@0
   200
            assert(status != PEP_OUT_OF_MEMORY);
vb@0
   201
        }
vb@0
   202
        free_identity(identity);
vb@0
   203
    }
vb@0
   204
vb@0
   205
    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
vb@0
   206
vb@0
   207
    release(session);
vb@0
   208
    return PEP_STATUS_OK;
vb@0
   209
}
vb@0
   210