src/baseprotocol.c
author Volker Birk <vb@pep.foundation>
Tue, 20 Nov 2018 17:18:46 +0100
branchsync
changeset 3143 81c80e27cbf7
parent 3130 7416d52013ff
child 3144 334a6c3faacd
permissions -rw-r--r--
signing of beacons
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
vb@585
     4
#include "pEp_internal.h"
vb@952
     5
#include "message_api.h"
vb@585
     6
vb@2846
     7
PEP_STATUS base_decorate_message(
vb@3143
     8
        PEP_SESSION session,
vb@587
     9
        message *msg,
vb@587
    10
        char *payload,
vb@3129
    11
        size_t size,
vb@3129
    12
        char *fpr
vb@587
    13
    )
vb@587
    14
{
vb@3143
    15
    PEP_STATUS status = PEP_STATUS_OK;
vb@3143
    16
vb@587
    17
    assert(msg);
vb@587
    18
    assert(payload);
vb@587
    19
    assert(size);
vb@587
    20
vb@587
    21
    if (!(msg && payload && size))
vb@587
    22
        return PEP_ILLEGAL_VALUE;
vb@587
    23
vb@587
    24
    bloblist_t *bl = bloblist_add(msg->attachments, payload, size,
krista@1871
    25
            "application/pEp.sync", "ignore_this_attachment.pEp");
vb@587
    26
    if (bl == NULL)
vb@587
    27
        goto enomem;
vb@587
    28
vb@3129
    29
    if (fpr) {
vb@3143
    30
        char *sign;
vb@3143
    31
        size_t sign_size;
vb@3143
    32
        status = sign_only(session,  payload, size, fpr, &sign, &sign_size);
vb@3143
    33
        if (status)
vb@3143
    34
            goto error;
vb@3143
    35
vb@3143
    36
        assert(sign && sign_size);
vb@3143
    37
vb@3143
    38
        bl = bloblist_add(bl, sign, sign_size,
vb@3143
    39
                "application/pEp.sign", "ignore_this_attachment.pEp");
vb@3143
    40
        if (!bl)
vb@3143
    41
            goto enomem;
vb@3129
    42
    }
vb@3129
    43
vb@587
    44
    return PEP_STATUS_OK;
vb@587
    45
vb@587
    46
enomem:
vb@3143
    47
    status = PEP_OUT_OF_MEMORY;
vb@3143
    48
vb@3143
    49
error:
vb@3143
    50
    return status;
vb@587
    51
}
vb@587
    52
vb@2846
    53
PEP_STATUS base_prepare_message(
vb@3143
    54
        PEP_SESSION session,
vb@585
    55
        const pEp_identity *me,
vb@585
    56
        const pEp_identity *partner,
vb@585
    57
        char *payload,
vb@585
    58
        size_t size,
vb@3129
    59
        char *fpr,
vb@585
    60
        message **result
vb@585
    61
    )
vb@585
    62
{
vb@587
    63
    PEP_STATUS status = PEP_STATUS_OK;
vb@587
    64
vb@585
    65
    assert(me);
vb@585
    66
    assert(partner);
vb@585
    67
    assert(payload);
vb@587
    68
    assert(size);
vb@587
    69
    assert(result);
vb@587
    70
vb@587
    71
    if (!(me && partner && payload && size && result))
vb@587
    72
        return PEP_ILLEGAL_VALUE;
vb@585
    73
vb@585
    74
    *result = NULL;
vb@585
    75
vb@585
    76
    message *msg = new_message(PEP_dir_outgoing);
vb@585
    77
    if (!msg)
vb@585
    78
        goto enomem;
vb@585
    79
vb@1132
    80
    add_opt_field(msg, "pEp-auto-consume", "yes");
vb@952
    81
vb@585
    82
    msg->from = identity_dup(me);
vb@585
    83
    if (!msg->from)
vb@585
    84
        goto enomem;
vb@585
    85
vb@585
    86
    msg->to = new_identity_list(identity_dup(partner));
vb@585
    87
    if (!msg->to)
vb@585
    88
        goto enomem;
vb@585
    89
vb@952
    90
    msg->shortmsg = strdup("p≡p synchronization message - please ignore");
vb@585
    91
    assert(msg->shortmsg);
vb@585
    92
    if (!msg->shortmsg)
vb@585
    93
        goto enomem;
vb@585
    94
vb@585
    95
    msg->longmsg = strdup("This message is part of p≡p's concept to synchronize.\n\n"
vb@585
    96
                        "You can safely ignore it. It will be deleted automatically.\n");
vb@585
    97
    assert(msg->longmsg);
vb@585
    98
    if (!msg->longmsg)
vb@585
    99
        goto enomem;
vb@585
   100
vb@3143
   101
    status = base_decorate_message(session, msg, payload, size, fpr);
vb@587
   102
    if (status == PEP_STATUS_OK)
vb@587
   103
        *result = msg;
vb@587
   104
    return status;
vb@585
   105
vb@585
   106
enomem:
vb@585
   107
    free_message(msg);
vb@585
   108
    return PEP_OUT_OF_MEMORY;
vb@585
   109
}
krista@1871
   110
vb@3143
   111
PEP_STATUS base_extract_message(
vb@3143
   112
        PEP_SESSION session,
vb@3143
   113
        message *msg,
vb@3143
   114
        size_t *size,
vb@3143
   115
        const char **payload,
vb@3143
   116
        char **fpr
vb@3143
   117
    )
vb@3130
   118
{
vb@3130
   119
    PEP_STATUS status = PEP_STATUS_OK;
vb@3130
   120
vb@3143
   121
    assert(session && msg && size && payload && fpr);
vb@3143
   122
    if (!(session && msg && size && payload && fpr))
vb@3130
   123
        return PEP_ILLEGAL_VALUE;
vb@3130
   124
vb@3130
   125
    *size = 0;
vb@3130
   126
    *payload = NULL;
vb@3130
   127
vb@3143
   128
    const char *_payload = NULL;
vb@3143
   129
    size_t _payload_size = 0;
vb@3143
   130
    const char *_sign = NULL;
vb@3143
   131
    size_t _sign_size = 0;
vb@3143
   132
    stringlist_t *keylist = NULL;
vb@3143
   133
vb@3130
   134
    for (bloblist_t *bl = msg->attachments; bl ; bl = bl->next) {
vb@3130
   135
        if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0) {
vb@3143
   136
            if (!_payload) {
vb@3143
   137
                _payload = bl->value;
vb@3143
   138
                _payload_size = bl->size;
vb@3143
   139
            }
vb@3143
   140
            else {
vb@3143
   141
                status = PEP_DECRYPT_WRONG_FORMAT;
vb@3143
   142
                goto the_end;
vb@3143
   143
            }
vb@3143
   144
        }
vb@3143
   145
        else if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sign") == 0) {
vb@3143
   146
            if (!_sign) {
vb@3143
   147
                _sign = bl->value;
vb@3143
   148
                _sign_size = bl->size;
vb@3143
   149
            }
vb@3143
   150
            else {
vb@3143
   151
                status = PEP_DECRYPT_WRONG_FORMAT;
vb@3143
   152
                goto the_end;
vb@3143
   153
            }
vb@3130
   154
        }
vb@3130
   155
    }
vb@3143
   156
    
vb@3143
   157
    if (!(_payload && _payload_size))
vb@3143
   158
        goto the_end;
vb@3130
   159
vb@3143
   160
    if (_sign) {
vb@3143
   161
        status = verify_text(session, _payload, _payload_size, _sign, _sign_size, &keylist);
vb@3143
   162
        if (status != PEP_VERIFIED || !keylist || !keylist->value) {
vb@3143
   163
            // signature invalid or does not match; ignore sync message
vb@3143
   164
            status = PEP_STATUS_OK;
vb@3143
   165
            goto the_end;
vb@3143
   166
        }
vb@3143
   167
vb@3143
   168
        char *_fpr = strdup(keylist->value);
vb@3143
   169
        assert(_fpr);
vb@3143
   170
        if (!_fpr) {
vb@3143
   171
            status = PEP_OUT_OF_MEMORY;
vb@3143
   172
            goto the_end;
vb@3143
   173
        }
vb@3143
   174
vb@3143
   175
        *fpr = _fpr;
vb@3143
   176
    }
vb@3143
   177
vb@3143
   178
    *size = _payload_size;
vb@3143
   179
    *payload = _payload;
vb@3143
   180
    status = PEP_STATUS_OK;
vb@3143
   181
vb@3143
   182
the_end:
vb@3143
   183
    free_stringlist(keylist);
vb@3130
   184
    return status;
vb@3130
   185
}
vb@3130
   186