src/keymanagement.c
changeset 0 16f27efbef98
child 8 26cc9f0228f4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/keymanagement.c	Wed Jun 25 18:44:58 2014 +0200
     1.3 @@ -0,0 +1,210 @@
     1.4 +#ifndef WIN32 // UNIX
     1.5 +#define _POSIX_C_SOURCE 200809L
     1.6 +#else
     1.7 +#include "platform_windows.h"
     1.8 +#endif
     1.9 +
    1.10 +#include <string.h>
    1.11 +#include <stdio.h>
    1.12 +#include <stdlib.h>
    1.13 +#include <assert.h>
    1.14 +
    1.15 +#define _EXPORT_PEP_ENGINE_DLL
    1.16 +#include "pEpEngine.h"
    1.17 +#include "keymanagement.h"
    1.18 +
    1.19 +#ifndef MIN
    1.20 +#define MIN(A, B) ((B) > (A) ? (A) : (B))
    1.21 +#endif
    1.22 +
    1.23 +DYNAMIC_API PEP_STATUS update_identity(
    1.24 +        PEP_SESSION session, pEp_identity * identity
    1.25 +    )
    1.26 +{
    1.27 +    pEp_identity *stored_identity;
    1.28 +    PEP_STATUS status;
    1.29 +    bool bDirty;
    1.30 +
    1.31 +    assert(session);
    1.32 +    assert(identity);
    1.33 +    assert(identity->address);
    1.34 +
    1.35 +    status = get_identity(session, identity->address, &stored_identity);
    1.36 +    assert(status != PEP_OUT_OF_MEMORY);
    1.37 +    if (status == PEP_OUT_OF_MEMORY)
    1.38 +        return PEP_OUT_OF_MEMORY;
    1.39 +
    1.40 +    if (stored_identity) {
    1.41 +        if (identity->username == NULL || identity->username[0] == 0) {
    1.42 +            free(identity->username);
    1.43 +            identity->username = strdup(stored_identity->username);
    1.44 +        }
    1.45 +        if (identity->user_id == NULL || identity->user_id[0] == 0) {
    1.46 +            free(identity->user_id);
    1.47 +            identity->user_id = strdup(stored_identity->user_id);
    1.48 +        }
    1.49 +        if (identity->fpr != NULL && identity->fpr[0] != 0) {
    1.50 +            if (strcmp(identity->fpr, stored_identity->fpr) != 0)
    1.51 +                identity->comm_type = PEP_ct_unknown;
    1.52 +        }
    1.53 +    }
    1.54 +    else
    1.55 +        identity->comm_type = PEP_ct_unknown;
    1.56 +
    1.57 +    status = set_identity(session, identity);
    1.58 +
    1.59 +    return PEP_STATUS_OK;
    1.60 +}
    1.61 +
    1.62 +DYNAMIC_API PEP_STATUS outgoing_comm_type(
    1.63 +        PEP_SESSION session,
    1.64 +        const stringlist_t *addresses,
    1.65 +        PEP_comm_type *comm_type
    1.66 +    )
    1.67 +{
    1.68 +    int i;
    1.69 +    const stringlist_t *l;
    1.70 +
    1.71 +    assert(session);
    1.72 +    assert(addresses);
    1.73 +    assert(addresses->value);
    1.74 +    assert(comm_type);
    1.75 +
    1.76 +    *comm_type = PEP_ct_unknown;
    1.77 +
    1.78 +    for (l=addresses; l && l->value; l = l->next) {
    1.79 +        PEP_STATUS _status;
    1.80 +        pEp_identity *identity;
    1.81 +
    1.82 +        _status = get_identity(session, l->value, &identity);
    1.83 +        assert(_status != PEP_OUT_OF_MEMORY);
    1.84 +
    1.85 +        if (identity == NULL) {
    1.86 +            *comm_type = PEP_ct_no_encryption;
    1.87 +            return PEP_STATUS_OK;
    1.88 +        }
    1.89 +        else if (identity->comm_type == PEP_ct_unknown) {
    1.90 +            *comm_type = PEP_ct_no_encryption;
    1.91 +            free_identity(identity);
    1.92 +            return PEP_STATUS_OK;
    1.93 +        }
    1.94 +        else if (*comm_type == PEP_ct_unknown) {
    1.95 +            *comm_type = identity->comm_type;
    1.96 +        }
    1.97 +        else if (*comm_type != identity->comm_type) {
    1.98 +            PEP_comm_type min = MIN(*comm_type, identity->comm_type);
    1.99 +            if (min < PEP_ct_unconfirmed_encryption) {
   1.100 +                *comm_type = PEP_ct_no_encryption;
   1.101 +                free_identity(identity);
   1.102 +                return PEP_STATUS_OK;
   1.103 +            }
   1.104 +            else if (min < PEP_ct_unconfirmed_enc_anon)
   1.105 +                *comm_type = PEP_ct_unconfirmed_encryption;
   1.106 +            else if (min < PEP_ct_confirmed_encryption)
   1.107 +                *comm_type = PEP_ct_unconfirmed_enc_anon;
   1.108 +            else if (min < PEP_ct_confirmed_enc_anon)
   1.109 +                *comm_type = PEP_ct_confirmed_encryption;
   1.110 +            else
   1.111 +                *comm_type = PEP_ct_confirmed_enc_anon;
   1.112 +        }
   1.113 +
   1.114 +        free_identity(identity);
   1.115 +    }
   1.116 +
   1.117 +    return PEP_STATUS_OK;
   1.118 +}
   1.119 +
   1.120 +DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
   1.121 +{
   1.122 +    PEP_STATUS status;
   1.123 +    stringlist_t *keylist;
   1.124 +
   1.125 +    assert(session);
   1.126 +    assert(identity);
   1.127 +    assert(identity->address);
   1.128 +    assert(identity->username);
   1.129 +    assert(identity->user_id);
   1.130 +
   1.131 +    identity->comm_type = PEP_ct_pEp;
   1.132 +    identity->me = true;
   1.133 +
   1.134 +    pEp_identity *_identity;
   1.135 +
   1.136 +    log_event(session, "myself", "debug", identity->address, NULL);
   1.137 +    status = get_identity(session, identity->address, &_identity);
   1.138 +    assert(status != PEP_OUT_OF_MEMORY);
   1.139 +    if (status == PEP_OUT_OF_MEMORY)
   1.140 +        return PEP_OUT_OF_MEMORY;
   1.141 +
   1.142 +    status = find_keys(session, identity->address, &keylist);
   1.143 +    assert(status != PEP_OUT_OF_MEMORY);
   1.144 +    if (status == PEP_OUT_OF_MEMORY)
   1.145 +        return PEP_OUT_OF_MEMORY;
   1.146 +
   1.147 +    if (keylist == NULL || keylist->value == NULL) {
   1.148 +        log_event(session, "generating key pair", "debug", identity->address, NULL);
   1.149 +        status = generate_keypair(session, identity);
   1.150 +        assert(status != PEP_OUT_OF_MEMORY);
   1.151 +        if (status != PEP_STATUS_OK) {
   1.152 +            char buf[11];
   1.153 +            snprintf(buf, 11, "%d", status);
   1.154 +            log_event(session, "generating key pair failed", "debug", buf, NULL);
   1.155 +            return status;
   1.156 +        }
   1.157 +
   1.158 +        status = find_keys(session, identity->address, &keylist);
   1.159 +        assert(status != PEP_OUT_OF_MEMORY);
   1.160 +        if (status == PEP_OUT_OF_MEMORY)
   1.161 +            return PEP_OUT_OF_MEMORY;
   1.162 +
   1.163 +        assert(keylist);
   1.164 +    }
   1.165 +
   1.166 +    if (identity->fpr)
   1.167 +        free(identity->fpr);
   1.168 +    identity->fpr = strdup(keylist->value);
   1.169 +    assert(identity->fpr);
   1.170 +    free_stringlist(keylist);
   1.171 +    if (identity->fpr == NULL)
   1.172 +        return PEP_OUT_OF_MEMORY;
   1.173 +
   1.174 +    status = set_identity(session, identity);
   1.175 +    assert(status == PEP_STATUS_OK);
   1.176 +
   1.177 +    return PEP_STATUS_OK;
   1.178 +}
   1.179 +
   1.180 +DYNAMIC_API PEP_STATUS do_keymanagement(
   1.181 +        retrieve_next_identity_t retrieve_next_identity,
   1.182 +        void *management
   1.183 +    )
   1.184 +{
   1.185 +    PEP_SESSION session;
   1.186 +    pEp_identity *identity;
   1.187 +    PEP_STATUS status = init(&session);
   1.188 +
   1.189 +    assert(status == PEP_STATUS_OK);
   1.190 +    if (status != PEP_STATUS_OK)
   1.191 +        return status;
   1.192 +
   1.193 +    log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
   1.194 +
   1.195 +    while (identity = retrieve_next_identity(management)) {
   1.196 +        assert(identity->address);
   1.197 +        log_event(session, "do_keymanagement", "debug", identity->address, NULL);
   1.198 +        if (identity->me) {
   1.199 +            status = myself(session, identity);
   1.200 +            assert(status != PEP_OUT_OF_MEMORY);
   1.201 +        } else {
   1.202 +            status = recv_key(session, identity->address);
   1.203 +            assert(status != PEP_OUT_OF_MEMORY);
   1.204 +        }
   1.205 +        free_identity(identity);
   1.206 +    }
   1.207 +
   1.208 +    log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
   1.209 +
   1.210 +    release(session);
   1.211 +    return PEP_STATUS_OK;
   1.212 +}
   1.213 +