src/message_api.c
author vb
Sun, 28 Dec 2014 23:07:55 +0100
changeset 37 7928e0e6eee9
child 38 271bea5552dc
permissions -rw-r--r--
message api
vb@37
     1
#include "message_api.h"
vb@37
     2
#include "keymanagement.h"
vb@37
     3
vb@37
     4
#include <libetpan/libetpan.h>
vb@37
     5
#include <assert.h>
vb@37
     6
#include <string.h>
vb@37
     7
vb@37
     8
PEP_STATUS encrypt_message(
vb@37
     9
        PEP_SESSION session,
vb@37
    10
        const message *src,
vb@37
    11
        stringlist_t * extra,
vb@37
    12
        message **dst
vb@37
    13
    )
vb@37
    14
{
vb@37
    15
    PEP_STATUS status = PEP_STATUS_OK;
vb@37
    16
vb@37
    17
    assert(session);
vb@37
    18
    assert(src);
vb@37
    19
    assert(dst);
vb@37
    20
    *dst = NULL;
vb@37
    21
vb@37
    22
    message *msg = new_message(src->dir, src->from, src->to, NULL);
vb@37
    23
    if (msg == NULL)
vb@37
    24
        return PEP_OUT_OF_MEMORY;
vb@37
    25
vb@37
    26
    src->from->me = true;
vb@37
    27
vb@37
    28
    status = myself(session, src->from);
vb@37
    29
    if (status != PEP_STATUS_OK) {
vb@37
    30
        free_message(msg);
vb@37
    31
        return status;
vb@37
    32
    }
vb@37
    33
vb@37
    34
    stringlist_t * keys = new_stringlist(src->from->fpr);
vb@37
    35
    if (keys == NULL) {
vb@37
    36
        free_message(msg);
vb@37
    37
        return PEP_OUT_OF_MEMORY;
vb@37
    38
    }
vb@37
    39
vb@37
    40
    stringlist_t *_x;
vb@37
    41
    for (_x = extra; _x && _x->value; _x = _x->next) {
vb@37
    42
        if (stringlist_add(keys, _x->value) == NULL) {
vb@37
    43
            free_message(msg);
vb@37
    44
            free_stringlist(keys);
vb@37
    45
            return PEP_OUT_OF_MEMORY;
vb@37
    46
        }
vb@37
    47
    }
vb@37
    48
    
vb@37
    49
    identity_list * _il;
vb@37
    50
    for (_il = src->to; _il && _il->ident; _il = _il->next) {
vb@37
    51
        status = update_identity(session, _il->ident);
vb@37
    52
        if (status != PEP_STATUS_OK) {
vb@37
    53
            free_message(msg);
vb@37
    54
            free_stringlist(keys);
vb@37
    55
            return status;
vb@37
    56
        }
vb@37
    57
        if (_il->ident->fpr) {
vb@37
    58
            if (stringlist_add(keys, _il->ident->fpr) == NULL) {
vb@37
    59
                free_message(msg);
vb@37
    60
                free_stringlist(keys);
vb@37
    61
                return PEP_OUT_OF_MEMORY;
vb@37
    62
            }
vb@37
    63
        }
vb@37
    64
        else
vb@37
    65
            status = PEP_KEY_NOT_FOUND;
vb@37
    66
    }
vb@37
    67
vb@37
    68
    int _own_keys = 1;
vb@37
    69
    if (extra)
vb@37
    70
        _own_keys += stringlist_length(extra);
vb@37
    71
    
vb@37
    72
    if (stringlist_length(keys) > _own_keys) {
vb@37
    73
        char *ptext = NULL;
vb@37
    74
        char *ctext = NULL;
vb@37
    75
        size_t csize = 0;
vb@37
    76
vb@37
    77
        // TODO: set ptext to MIME text
vb@37
    78
vb@37
    79
        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext, &csize);
vb@37
    80
        if (ctext) {
vb@37
    81
            msg->longmsg = ctext;
vb@37
    82
            msg->longmsg_size = csize;
vb@37
    83
            *dst = msg;
vb@37
    84
        }
vb@37
    85
        else
vb@37
    86
            free_message(msg);
vb@37
    87
        free(ptext);
vb@37
    88
    }
vb@37
    89
    else
vb@37
    90
        free_message(msg);
vb@37
    91
vb@37
    92
    free_stringlist(keys);
vb@37
    93
    return status;
vb@37
    94
}
vb@37
    95
vb@37
    96
PEP_STATUS decrypt_message(
vb@37
    97
        PEP_SESSION session,
vb@37
    98
        const message *src,
vb@37
    99
        message **dst
vb@37
   100
    )
vb@37
   101
{
vb@37
   102
    PEP_STATUS status = PEP_STATUS_OK;
vb@37
   103
vb@37
   104
    return status;
vb@37
   105
}
vb@37
   106