src/sync_impl.c
author Edouard Tisserant <edouard@pep-project.org>
Fri, 06 Jan 2017 10:23:51 +0100
changeset 1527 75d56a822caa
parent 1523 bc4a9401af51
child 1567 7e51ef02ab31
permissions -rw-r--r--
Memory leak and unhandled out of memory case.
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
edouard@1261
     4
#include "platform.h"
edouard@1260
     5
edouard@1260
     6
// it seems pEp_internal.h needs to be the first pEp include due to the 
markus@1259
     7
// #define for the dllimport / dllexport DYNAMIC_API stuff.
markus@1259
     8
#include "pEp_internal.h"
markus@1259
     9
vb@1059
    10
#include "../asn.1/DeviceGroup-Protocol.h"
vb@1059
    11
#include "sync_impl.h"
vb@1059
    12
#include "keymanagement.h"
vb@1076
    13
#include "message_api.h"
vb@1059
    14
#include "map_asn1.h"
vb@1059
    15
#include "baseprotocol.h"
vb@1059
    16
vb@1077
    17
#define SYNC_VERSION_MAJOR 1
vb@1077
    18
#define SYNC_VERSION_MINOR 0
vb@1077
    19
edouard@1316
    20
#define SYNC_INHIBIT_TIME (60*10)
edouard@1316
    21
#define SYNC_MSG_EXPIRE_TIME (60 * 10)
edouard@1316
    22
edouard@1172
    23
struct _sync_msg_t {
edouard@1172
    24
    bool is_a_message;
edouard@1172
    25
    union {
edouard@1172
    26
        DeviceGroup_Protocol_t *message;
edouard@1172
    27
        struct {
edouard@1172
    28
            DeviceState_event event;
edouard@1172
    29
            Identity partner;
edouard@1172
    30
            void *extra;
edouard@1172
    31
        } event;
edouard@1172
    32
    } u;
edouard@1172
    33
};
edouard@1172
    34
vb@1059
    35
PEP_STATUS receive_sync_msg(
vb@1059
    36
        PEP_SESSION session,
edouard@1445
    37
        sync_msg_t *sync_msg,
edouard@1445
    38
        time_t *timeout
vb@1059
    39
    )
vb@1059
    40
{
edouard@1172
    41
    PEP_STATUS status;
vb@1059
    42
    void *extra = NULL;
vb@1059
    43
    Identity partner = NULL;
vb@1059
    44
    DeviceState_event event = DeviceState_event_NONE;
edouard@1172
    45
    assert(session && sync_msg);
edouard@1172
    46
    if (!(session && sync_msg))
edouard@1172
    47
        return PEP_ILLEGAL_VALUE;
vb@1059
    48
edouard@1340
    49
    bool msgIsFromGroup = false;
edouard@1172
    50
    if(sync_msg->is_a_message){
edouard@1172
    51
        DeviceGroup_Protocol_t *msg = sync_msg->u.message;
edouard@1172
    52
        assert(msg && msg->payload.present != DeviceGroup_Protocol__payload_PR_NOTHING);
edouard@1172
    53
        if (!(msg && msg->payload.present != DeviceGroup_Protocol__payload_PR_NOTHING)){
edouard@1172
    54
            status = PEP_OUT_OF_MEMORY;
edouard@1172
    55
            goto error;
edouard@1172
    56
        }
vb@1059
    57
edouard@1340
    58
        msgIsFromGroup = msg->header.devicegroup;
edouard@1340
    59
edouard@1172
    60
        switch (msg->payload.present) {
edouard@1172
    61
            case DeviceGroup_Protocol__payload_PR_beacon:
edouard@1172
    62
                event = Beacon;
edouard@1172
    63
                break;
vb@1059
    64
edouard@1172
    65
            case DeviceGroup_Protocol__payload_PR_handshakeRequest:
edouard@1316
    66
                event = HandshakeRequest;
edouard@1316
    67
                break;
edouard@1256
    68
edouard@1316
    69
            case DeviceGroup_Protocol__payload_PR_updateRequest:
edouard@1316
    70
                event = UpdateRequest;
edouard@1172
    71
                break;
vb@1059
    72
edouard@1172
    73
            case DeviceGroup_Protocol__payload_PR_groupKeys:
edouard@1281
    74
            case DeviceGroup_Protocol__payload_PR_groupUpdate:
edouard@1316
    75
            {
edouard@1172
    76
                identity_list *group_keys = IdentityList_to_identity_list(
edouard@1281
    77
                        msg->payload.present == 
edouard@1281
    78
                          DeviceGroup_Protocol__payload_PR_groupKeys ?
edouard@1297
    79
                            &msg->payload.choice.groupKeys.ownIdentities :
edouard@1297
    80
                            &msg->payload.choice.groupUpdate.ownIdentities,
edouard@1281
    81
                        NULL);
edouard@1172
    82
                if (!group_keys) {
edouard@1172
    83
                    status = PEP_OUT_OF_MEMORY;
edouard@1172
    84
                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
edouard@1172
    85
                    goto error;
edouard@1172
    86
                }
edouard@1172
    87
                extra = (void *) group_keys;
edouard@1281
    88
                event = msg->payload.present == 
edouard@1281
    89
                          DeviceGroup_Protocol__payload_PR_groupKeys ?
edouard@1297
    90
                            GroupKeys : GroupUpdate;
edouard@1172
    91
                break;
edouard@1316
    92
            }
edouard@1172
    93
edouard@1172
    94
            default:
edouard@1172
    95
                status = PEP_SYNC_ILLEGAL_MESSAGE;
edouard@1172
    96
                ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
edouard@1172
    97
                goto error;
edouard@1172
    98
        }
edouard@1316
    99
edouard@1316
   100
        partner = Identity_to_Struct(&msg->header.me, NULL);
edouard@1316
   101
        if (!partner){
edouard@1316
   102
            status = PEP_OUT_OF_MEMORY;
edouard@1316
   103
            ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
edouard@1316
   104
            goto error;
edouard@1316
   105
        }
edouard@1172
   106
    }
edouard@1172
   107
    else{
edouard@1172
   108
        partner = sync_msg->u.event.partner;
edouard@1172
   109
        extra = sync_msg->u.event.extra;
edouard@1172
   110
        event = sync_msg->u.event.event;
vb@1059
   111
    }
vb@1059
   112
edouard@1316
   113
    // Event inhibition, to limit mailbox and prevent cycles
edouard@1316
   114
    time_t *last = NULL;
edouard@1316
   115
    switch(event){
edouard@1316
   116
        case CannotDecrypt:
edouard@1316
   117
            last = &session->LastCannotDecrypt;
edouard@1316
   118
            break;
edouard@1316
   119
edouard@1316
   120
        case UpdateRequest:
edouard@1316
   121
            last = &session->LastUpdateRequest;
edouard@1316
   122
            break;
edouard@1316
   123
edouard@1316
   124
        default:
edouard@1316
   125
            break;
edouard@1316
   126
    }
edouard@1316
   127
    time_t now = time(NULL);
edouard@1316
   128
    if(last != NULL){
edouard@1316
   129
        if(*last != 0 && (*last + SYNC_INHIBIT_TIME) > now ){
edouard@1316
   130
            free_identity(partner);
edouard@1316
   131
            status = PEP_STATEMACHINE_INHIBITED_EVENT;
edouard@1316
   132
            goto error;
edouard@1316
   133
        }
edouard@1316
   134
        *last = now;
edouard@1316
   135
    }
edouard@1316
   136
edouard@1256
   137
    // partner identity must be explicitely added DB to later
edouard@1256
   138
    // be able to communicate securely with it.
edouard@1256
   139
    if(partner){
edouard@1260
   140
        // protect virtual user IDs 
edouard@1256
   141
        if((strncmp("TOFU_", partner->user_id, 6) == 0 &&
edouard@1256
   142
           strlen(partner->user_id) == strlen(partner->address) + 6 &&
edouard@1256
   143
           strcmp(partner->user_id + 6, partner->address)) ||
edouard@1260
   144
        // protect own ID 
edouard@1256
   145
           (strcmp(PEP_OWN_USERID, partner->user_id) == 0)){
edouard@1256
   146
            status = PEP_SYNC_ILLEGAL_MESSAGE;
edouard@1256
   147
            goto error;
edouard@1256
   148
        }
edouard@1256
   149
edouard@1260
   150
        // partner IDs are UUIDs bound to session lifespan
edouard@1256
   151
        // and therefore partner identities are not supposed
edouard@1260
   152
        // to mutate over time, but just not be used anymore.
edouard@1260
   153
        // It should then be safe to accept given identity if not 
edouard@1256
   154
        // already pre-existing
edouard@1256
   155
        pEp_identity *stored_identity = NULL;
edouard@1256
   156
        status = get_identity(session,
edouard@1256
   157
                              partner->address,
edouard@1256
   158
                              partner->user_id,
edouard@1256
   159
                              &stored_identity);
edouard@1256
   160
edouard@1256
   161
        if (!stored_identity) {
edouard@1260
   162
            // make a safe copy of partner, with no flags or comm_type
edouard@1260
   163
            pEp_identity *tmpident = new_identity(partner->address,
edouard@1260
   164
                                                  partner->fpr,
edouard@1260
   165
                                                  partner->user_id,
edouard@1260
   166
                                                  partner->username);
edouard@1260
   167
            if (tmpident == NULL){
edouard@1260
   168
                status = PEP_OUT_OF_MEMORY;
edouard@1260
   169
                goto error;
edouard@1260
   170
            }
edouard@1260
   171
edouard@1260
   172
            // finaly add partner to DB
edouard@1260
   173
            status = set_identity(session, tmpident);
edouard@1340
   174
            assert(status == PEP_STATUS_OK);
edouard@1340
   175
            if(status == PEP_STATUS_OK && msgIsFromGroup)
edouard@1340
   176
                status = set_identity_flags(session, tmpident, PEP_idf_devicegroup);
edouard@1260
   177
            free_identity(tmpident);
edouard@1256
   178
            assert(status == PEP_STATUS_OK);
edouard@1256
   179
            if (status != PEP_STATUS_OK) {
edouard@1256
   180
                goto error;
edouard@1256
   181
            }
edouard@1256
   182
        }
edouard@1256
   183
        else if (status == PEP_STATUS_OK) {
edouard@1256
   184
            free_identity(stored_identity);
edouard@1256
   185
        } 
edouard@1256
   186
        else
edouard@1256
   187
            goto error;
edouard@1256
   188
    }
edouard@1256
   189
edouard@1445
   190
    status = fsm_DeviceState_inject(session, event, partner, extra, timeout);
edouard@1157
   191
edouard@1157
   192
    free_identity(partner);
edouard@1157
   193
edouard@1172
   194
error:
edouard@1172
   195
    free(sync_msg);
edouard@1172
   196
edouard@1159
   197
    return status;
vb@1059
   198
}
vb@1059
   199
markus@1226
   200
// TODO: DYNAMIC_API was here, but broke the windows build. 
markus@1226
   201
// We need to check whether it belongs here or it's a bug.
markus@1226
   202
/* DYNAMIC_API */ void free_sync_msg(sync_msg_t *sync_msg)
Edouard@1203
   203
{
roker@1482
   204
    if (!sync_msg)
Edouard@1203
   205
        return;
Edouard@1203
   206
Edouard@1203
   207
    if(sync_msg->is_a_message){
Edouard@1203
   208
        DeviceGroup_Protocol_t *msg = sync_msg->u.message;
Edouard@1203
   209
        assert(msg);
Edouard@1203
   210
        if (!(msg))
Edouard@1203
   211
            return;
Edouard@1203
   212
Edouard@1203
   213
        ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
Edouard@1203
   214
    }
Edouard@1203
   215
    else{
Edouard@1203
   216
        Identity partner = NULL;
Edouard@1203
   217
        partner = sync_msg->u.event.partner;
Edouard@1203
   218
        if(partner != NULL)
Edouard@1203
   219
            free_identity(partner);
Edouard@1203
   220
    }
Edouard@1203
   221
Edouard@1203
   222
    free(sync_msg);
Edouard@1203
   223
Edouard@1203
   224
    return;
Edouard@1203
   225
}
Edouard@1203
   226
edouard@1167
   227
// from sync.c
edouard@1167
   228
int call_inject_sync_msg(PEP_SESSION session, void *msg);
edouard@1167
   229
edouard@1172
   230
PEP_STATUS inject_DeviceState_event(
edouard@1172
   231
    PEP_SESSION session, 
edouard@1172
   232
    DeviceState_event event,
edouard@1172
   233
    Identity partner,
edouard@1172
   234
    void *extra)
edouard@1172
   235
{
edouard@1172
   236
    PEP_STATUS status;
edouard@1172
   237
edouard@1172
   238
    assert(session);
edouard@1172
   239
    if (!(session))
edouard@1172
   240
        return PEP_ILLEGAL_VALUE;
edouard@1172
   241
edouard@1172
   242
    sync_msg_t *sync_msg = malloc(sizeof(sync_msg_t));
edouard@1172
   243
    if(sync_msg == NULL)
edouard@1172
   244
        return PEP_OUT_OF_MEMORY;
edouard@1172
   245
edouard@1172
   246
    sync_msg->is_a_message = false;
edouard@1172
   247
    sync_msg->u.event.partner = partner;
edouard@1172
   248
    sync_msg->u.event.extra = extra;
edouard@1172
   249
    sync_msg->u.event.event = event;
edouard@1172
   250
edouard@1172
   251
    status = call_inject_sync_msg(session, sync_msg);
edouard@1172
   252
    if (status == PEP_SYNC_NO_INJECT_CALLBACK){
edouard@1172
   253
        free(sync_msg);
edouard@1172
   254
    }
edouard@1172
   255
edouard@1172
   256
    return status;
edouard@1172
   257
}
edouard@1172
   258
edouard@1165
   259
PEP_STATUS receive_DeviceState_msg(
edouard@1165
   260
    PEP_SESSION session, 
edouard@1165
   261
    message *src, 
edouard@1165
   262
    PEP_rating rating, 
edouard@1165
   263
    stringlist_t *keylist)
vb@1059
   264
{
vb@1059
   265
    assert(session && src);
vb@1059
   266
    if (!(session && src))
vb@1059
   267
        return PEP_ILLEGAL_VALUE;
vb@1059
   268
edouard@1339
   269
    bool consume = false;
edouard@1339
   270
    bool discard = false;
edouard@1248
   271
    bool force_keep_msg = false;
vb@1338
   272
vb@1059
   273
    for (bloblist_t *bl = src->attachments; bl && bl->value; bl = bl->next) {
vb@1132
   274
        if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0
vb@1059
   275
                && bl->size) {
vb@1077
   276
            DeviceGroup_Protocol_t *msg = NULL;
vb@1091
   277
            uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol, (void **)
vb@1091
   278
                    &msg, bl->value, bl->size);
edouard@1248
   279
vb@1059
   280
            if (msg) {
edouard@1257
   281
                PEP_STATUS status = PEP_STATUS_OK;
vb@1088
   282
vb@1091
   283
                char *user_id = strndup((char *) msg->header.me.user_id->buf,
vb@1091
   284
                        msg->header.me.user_id->size);
vb@1091
   285
                assert(user_id);
vb@1091
   286
                if (!user_id) {
vb@1091
   287
                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
vb@1091
   288
                    return PEP_OUT_OF_MEMORY;
vb@1091
   289
                }
vb@1091
   290
edouard@1521
   291
                // detect and mitigate address spoofing
edouard@1521
   292
                Identity check_me = NULL;
edouard@1527
   293
                char* null_terminated_address = 
edouard@1521
   294
                    strndup((char *) msg->header.me.address->buf,
edouard@1521
   295
                            msg->header.me.address->size);
edouard@1521
   296
edouard@1527
   297
                if(null_terminated_address){
edouard@1527
   298
                    status = get_identity(session, 
edouard@1527
   299
                                          null_terminated_address, 
edouard@1527
   300
                                          PEP_OWN_USERID, 
edouard@1527
   301
                                          &check_me);
edouard@1527
   302
                    free(null_terminated_address);
edouard@1527
   303
                } 
edouard@1527
   304
                else
edouard@1527
   305
                    status = PEP_OUT_OF_MEMORY;
edouard@1521
   306
edouard@1521
   307
                if (status == PEP_OUT_OF_MEMORY)
edouard@1521
   308
                    goto free_all;
edouard@1521
   309
edouard@1521
   310
                free_identity(check_me);
edouard@1521
   311
edouard@1521
   312
                bool not_own_address = status != PEP_STATUS_OK;
edouard@1521
   313
                status = PEP_STATUS_OK;
edouard@1521
   314
edouard@1521
   315
                if (not_own_address || 
edouard@1521
   316
                    strncmp(src->from->address,
edouard@1521
   317
                            (char *) msg->header.me.address->buf,
edouard@1521
   318
                            msg->header.me.address->size) != 0 ||
edouard@1521
   319
                    strncmp(src->to->ident->address,
edouard@1521
   320
                            (char *) msg->header.me.address->buf,
edouard@1521
   321
                            msg->header.me.address->size) != 0) {
edouard@1521
   322
                    consume = true;
edouard@1521
   323
                    goto free_all;
edouard@1521
   324
                }
edouard@1521
   325
edouard@1521
   326
                // if encrypted, ensure that header.me.fpr match signer's fpr
edouard@1521
   327
                if (rating >= PEP_rating_reliable && (
edouard@1521
   328
                        !keylist ||
edouard@1521
   329
                        !_same_fpr((char *) msg->header.me.fpr.buf,
edouard@1521
   330
                                   msg->header.me.fpr.size,
edouard@1521
   331
                                   keylist->value,
edouard@1521
   332
                                   strlen(keylist->value)))) {
edouard@1521
   333
                    consume = true;
edouard@1521
   334
                    goto free_all;
edouard@1521
   335
                }
edouard@1521
   336
edouard@1248
   337
                // check message expiry 
edouard@1257
   338
                if(src->recv) {
edouard@1316
   339
                    time_t expiry = timegm(src->recv) + SYNC_MSG_EXPIRE_TIME;
edouard@1257
   340
                    time_t now = time(NULL);
edouard@1257
   341
                    if(expiry != 0 && now != 0 && expiry < now){
edouard@1339
   342
                        consume = true;
edouard@1257
   343
                        goto free_all;
edouard@1257
   344
                    }
edouard@1165
   345
                }
edouard@1165
   346
edouard@1248
   347
                int32_t value = (int32_t) msg->header.sequence;
edouard@1257
   348
                status = sequence_value(session, (char *) user_id,
vb@1091
   349
                        &value);
vb@1088
   350
vb@1088
   351
                if (status == PEP_STATUS_OK) {
edouard@1248
   352
                    switch (msg->payload.present) {
edouard@1248
   353
                        // HandshakeRequest needs encryption
edouard@1248
   354
                        case DeviceGroup_Protocol__payload_PR_handshakeRequest:
edouard@1248
   355
                            if (rating < PEP_rating_reliable ||
edouard@1248
   356
                                strncmp(session->sync_uuid,
edouard@1248
   357
                                        (const char *)msg->payload.choice.handshakeRequest.partner.user_id->buf,
edouard@1248
   358
                                        msg->payload.choice.handshakeRequest.partner.user_id->size) != 0){
edouard@1339
   359
                                discard = true;
edouard@1256
   360
                                goto free_all;
edouard@1248
   361
                            }
edouard@1339
   362
                            
edouard@1248
   363
                            break;
edouard@1297
   364
                        // accepting GroupKeys needs encryption and trust of peer device
edouard@1248
   365
                        case DeviceGroup_Protocol__payload_PR_groupKeys:
edouard@1297
   366
                        {
edouard@1248
   367
                            if (!keylist || rating < PEP_rating_reliable ||
edouard@1297
   368
                                // message is only consumed by instance it is addressed to
edouard@1297
   369
                                (strncmp(session->sync_uuid,
edouard@1248
   370
                                        (const char *)msg->payload.choice.groupKeys.partner.user_id->buf,
edouard@1248
   371
                                        msg->payload.choice.groupKeys.partner.user_id->size) != 0)){
edouard@1339
   372
                                discard = true;
edouard@1256
   373
                                goto free_all;
edouard@1248
   374
                            }
edouard@1248
   375
edouard@1523
   376
                            // check trust of identity using user_id given in msg.header.me
edouard@1297
   377
                            // to exacly match identity of device, the one trusted in
edouard@1297
   378
                            // case of accepted handshake
edouard@1297
   379
                            pEp_identity *_from = new_identity(NULL, 
edouard@1248
   380
                                                               keylist->value,
edouard@1248
   381
                                                               user_id,
edouard@1297
   382
                                                               NULL);
edouard@1248
   383
                            if (_from == NULL){
edouard@1248
   384
                                status = PEP_OUT_OF_MEMORY;
edouard@1256
   385
                                goto free_all;
edouard@1248
   386
                            }
edouard@1297
   387
                            status = get_trust(session, _from);
edouard@1368
   388
                            if (status != PEP_STATUS_OK || _from->comm_type < PEP_ct_strong_encryption) {
edouard@1368
   389
                                status = PEP_STATUS_OK;
edouard@1297
   390
                                free_identity(_from);
edouard@1339
   391
                                discard = true;
edouard@1256
   392
                                goto free_all;
edouard@1248
   393
                            }
edouard@1297
   394
                            free_identity(_from);
edouard@1248
   395
                            break;
edouard@1297
   396
                        }
edouard@1297
   397
                        case DeviceGroup_Protocol__payload_PR_groupUpdate:
edouard@1316
   398
                        case DeviceGroup_Protocol__payload_PR_updateRequest:
edouard@1316
   399
                        {
edouard@1297
   400
                            // inject message but don't consume it, so 
edouard@1297
   401
                            // that other group members can also be updated
edouard@1297
   402
                            force_keep_msg = true;
edouard@1297
   403
                            
edouard@1297
   404
                            if (!keylist || rating < PEP_rating_reliable){
edouard@1339
   405
                                discard = true;
edouard@1297
   406
                                goto free_all;
edouard@1297
   407
                            }
edouard@1297
   408
                            // GroupUpdate and UpdateRequests come from group.
edouard@1297
   409
                            // check trust relation in between signer key and 
edouard@1297
   410
                            // own id to be sure.
edouard@1297
   411
                            pEp_identity *_from = new_identity(NULL, 
edouard@1297
   412
                                                               keylist->value,
edouard@1297
   413
                                                               PEP_OWN_USERID,
edouard@1297
   414
                                                               NULL);
edouard@1297
   415
                            if (_from == NULL){
edouard@1297
   416
                                status = PEP_OUT_OF_MEMORY;
edouard@1297
   417
                                goto free_all;
edouard@1297
   418
                            }
edouard@1297
   419
                            status = get_trust(session, _from);
edouard@1368
   420
                            if (status != PEP_STATUS_OK || _from->comm_type < PEP_ct_pEp) {
edouard@1368
   421
                                status = PEP_STATUS_OK;
edouard@1297
   422
                                free_identity(_from);
edouard@1339
   423
                                discard = true;
edouard@1297
   424
                                goto free_all;
edouard@1297
   425
                            }
edouard@1297
   426
                            free_identity(_from);
edouard@1297
   427
                        }
edouard@1248
   428
                        default:
edouard@1248
   429
                            break;
edouard@1248
   430
                    }
edouard@1248
   431
edouard@1248
   432
edouard@1339
   433
                    consume = true;
edouard@1172
   434
                    sync_msg_t *sync_msg = malloc(sizeof(sync_msg_t));
edouard@1172
   435
                    if(sync_msg == NULL){
edouard@1248
   436
                        status = PEP_OUT_OF_MEMORY;
edouard@1256
   437
                        goto free_all;
edouard@1172
   438
                    }
edouard@1172
   439
                    sync_msg->is_a_message = true;
edouard@1172
   440
                    sync_msg->u.message = msg;
edouard@1172
   441
                    status = call_inject_sync_msg(session, sync_msg);
edouard@1165
   442
                    if (status != PEP_STATUS_OK){
edouard@1172
   443
                        if (status == PEP_SYNC_NO_INJECT_CALLBACK){
edouard@1172
   444
                            free(sync_msg);
edouard@1172
   445
                        }
edouard@1256
   446
                        goto free_all;
edouard@1165
   447
                    }
edouard@1339
   448
                    // don't free message now that it is in the queue
edouard@1256
   449
                    goto free_userid;
edouard@1165
   450
                }
edouard@1165
   451
                else if (status == PEP_OWN_SEQUENCE) {
edouard@1248
   452
                    status = PEP_STATUS_OK;
edouard@1339
   453
                    discard = true;
edouard@1256
   454
                    goto free_all;
vb@1088
   455
                }
edouard@1248
   456
edouard@1256
   457
            free_all:
edouard@1248
   458
                ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
edouard@1256
   459
            free_userid:
edouard@1248
   460
                free(user_id);
edouard@1248
   461
edouard@1248
   462
                if (status != PEP_STATUS_OK)
edouard@1248
   463
                    return status;
vb@1059
   464
            }
vb@1059
   465
        }
vb@1059
   466
    }
vb@1059
   467
edouard@1248
   468
    if (force_keep_msg) {
edouard@1369
   469
        return PEP_MESSAGE_IGNORE;
edouard@1248
   470
    }
edouard@1248
   471
edouard@1339
   472
    if (consume && !session->keep_sync_msg) {
vb@1059
   473
        for (stringpair_list_t *spl = src->opt_fields ; spl && spl->value ;
vb@1059
   474
                spl = spl->next) {
vb@1059
   475
            if (spl->value->key &&
vb@1059
   476
                    strcasecmp(spl->value->key, "pEp-auto-consume") == 0) {
vb@1059
   477
                if (spl->value->value &&
vb@1059
   478
                        strcasecmp(spl->value->value, "yes") == 0)
edouard@1369
   479
                    return PEP_MESSAGE_CONSUME;
vb@1059
   480
            }
vb@1059
   481
        }
edouard@1369
   482
        return PEP_MESSAGE_IGNORE;
vb@1059
   483
    }
vb@1059
   484
edouard@1339
   485
    if(discard)
edouard@1369
   486
        return PEP_MESSAGE_IGNORE;
edouard@1248
   487
vb@1338
   488
    if (!session->keep_sync_msg) {
vb@1338
   489
        bloblist_t *last = NULL;
vb@1338
   490
        for (bloblist_t *bl = src->attachments; bl && bl->value; ) {
vb@1338
   491
            if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0) {
vb@1338
   492
                bloblist_t *b = bl;
vb@1338
   493
                bl = bl->next;
vb@1338
   494
                if (!last)
vb@1338
   495
                    src->attachments = bl;
vb@1338
   496
                else
vb@1338
   497
                    last->next = bl;
vb@1338
   498
                free(b->mime_type);
vb@1338
   499
                free(b->filename);
vb@1338
   500
                free(b->value);
vb@1338
   501
                free(b);
vb@1338
   502
            }
vb@1338
   503
            else {
vb@1338
   504
                last = bl;
vb@1338
   505
                bl = bl->next;
vb@1338
   506
            }
vb@1338
   507
        }
vb@1338
   508
    }
vb@1338
   509
vb@1059
   510
    return PEP_STATUS_OK;
vb@1059
   511
}
vb@1059
   512
vb@1059
   513
DeviceGroup_Protocol_t *new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR type)
vb@1059
   514
{
vb@1059
   515
    DeviceGroup_Protocol_t *msg = (DeviceGroup_Protocol_t *)
vb@1073
   516
            calloc(1, sizeof(DeviceGroup_Protocol_t));
vb@1059
   517
    assert(msg);
vb@1059
   518
    if (!msg)
vb@1059
   519
        return NULL;
vb@1059
   520
    msg->payload.present = type;
vb@1059
   521
    return msg;
vb@1059
   522
}
vb@1059
   523
vb@1059
   524
void free_DeviceGroup_Protocol_msg(DeviceGroup_Protocol_t *msg)
vb@1059
   525
{
vb@1059
   526
    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
vb@1059
   527
}
vb@1059
   528
Edouard@1495
   529
Edouard@1495
   530
#ifndef NDEBUG
Edouard@1495
   531
static int _append(const void *buffer, size_t size, void *appkey)
edouard@1494
   532
{
edouard@1494
   533
    char **dest_ptr = (char **)appkey;
edouard@1494
   534
    size_t osize = strlen(*dest_ptr);
edouard@1494
   535
    size_t nsize = size + osize;
edouard@1494
   536
    *dest_ptr = realloc(*dest_ptr, nsize + 1);
edouard@1494
   537
    if(*dest_ptr == NULL) return -1;
edouard@1494
   538
    memcpy(*dest_ptr + osize, buffer, size);
edouard@1494
   539
    (*dest_ptr)[nsize] = '\0';
edouard@1494
   540
    return 0;
edouard@1494
   541
}
Edouard@1495
   542
#endif
edouard@1494
   543
vb@1059
   544
PEP_STATUS unicast_msg(
vb@1059
   545
        PEP_SESSION session,
vb@1091
   546
        const Identity partner,
vb@1059
   547
        DeviceState_state state,
vb@1097
   548
        DeviceGroup_Protocol_t *msg,
vb@1097
   549
        bool encrypted
vb@1059
   550
    )
vb@1059
   551
{
vb@1059
   552
    PEP_STATUS status = PEP_STATUS_OK;
vb@1059
   553
    char *payload = NULL;
vb@1059
   554
    message *_message = NULL;
vb@1059
   555
    pEp_identity *me = NULL;
vb@1088
   556
    pEp_identity *_me = NULL;
vb@1059
   557
vb@1059
   558
    assert(session && partner && state && msg);
vb@1059
   559
    if (!(session && partner && state && msg))
vb@1059
   560
        return PEP_ILLEGAL_VALUE;
vb@1059
   561
vb@1059
   562
    assert(session->messageToSend);
vb@1059
   563
    if (!session->messageToSend) {
vb@1059
   564
        status = PEP_SEND_FUNCTION_NOT_REGISTERED;
vb@1059
   565
        goto error;
vb@1059
   566
    }
vb@1059
   567
vb@1077
   568
    msg->header.version.major = SYNC_VERSION_MAJOR;
vb@1077
   569
    msg->header.version.minor = SYNC_VERSION_MINOR;
vb@1077
   570
vb@1071
   571
    status = get_identity(session, partner->address, PEP_OWN_USERID, &me);
vb@1059
   572
    if (status != PEP_STATUS_OK)
vb@1059
   573
        goto error;
vb@1088
   574
    
vb@1088
   575
    int32_t seq = 0;
vb@1088
   576
edouard@1236
   577
    status = sequence_value(session, session->sync_uuid, &seq);
vb@1088
   578
    if (status != PEP_OWN_SEQUENCE && status != PEP_STATUS_OK)
vb@1088
   579
        goto error;
vb@1088
   580
vb@1088
   581
    msg->header.sequence = (long) seq;
vb@1088
   582
vb@1088
   583
    _me = identity_dup(me);
vb@1088
   584
    if (!_me)
vb@1059
   585
        goto enomem;
vb@1059
   586
vb@1088
   587
    free(_me->user_id);
edouard@1236
   588
    _me->user_id = strndup(session->sync_uuid, 36);
vb@1088
   589
    assert(_me->user_id);
vb@1088
   590
    if (!_me->user_id)
vb@1088
   591
        goto enomem;
vb@1088
   592
vb@1088
   593
    if (Identity_from_Struct(_me, &msg->header.me) == NULL)
vb@1088
   594
        goto enomem;
vb@1088
   595
vb@1088
   596
    free_identity(_me);
vb@1088
   597
    _me = NULL;
vb@1088
   598
vb@1077
   599
    msg->header.state = (long) state;
vb@1077
   600
vb@1077
   601
    bool devicegroup = storedGroupKeys(session);
vb@1077
   602
    if (devicegroup)
vb@1077
   603
        msg->header.devicegroup = 1;
vb@1077
   604
    else
vb@1077
   605
        msg->header.devicegroup = 0;
vb@1077
   606
vb@1059
   607
    if (asn_check_constraints(&asn_DEF_DeviceGroup_Protocol, msg, NULL, NULL)) {
vb@1059
   608
        status = PEP_CONTRAINTS_VIOLATED;
vb@1059
   609
        goto error;
vb@1059
   610
    }
vb@1059
   611
vb@1059
   612
    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_DeviceGroup_Protocol,
vb@1059
   613
            NULL, msg, (void **) &payload);
vb@1059
   614
    if (size == -1) {
vb@1059
   615
        status = PEP_CANNOT_ENCODE;
vb@1059
   616
        goto error;
vb@1059
   617
    }
vb@1059
   618
vb@1059
   619
    status = prepare_message(me, partner, payload, size, &_message);
vb@1059
   620
    if (status != PEP_STATUS_OK)
vb@1059
   621
        goto error;
vb@1059
   622
    payload = NULL;
vb@1059
   623
    free_identity(me);
vb@1059
   624
    me = NULL;
vb@1059
   625
edouard@1494
   626
#ifndef NDEBUG
edouard@1494
   627
    asn_enc_rval_t er;
edouard@1494
   628
    er = xer_encode(&asn_DEF_DeviceGroup_Protocol, msg, 
edouard@1494
   629
                    XER_F_BASIC, _append, &_message->longmsg);
edouard@1494
   630
    if(er.encoded == -1)
edouard@1494
   631
        goto error;
edouard@1494
   632
#endif
edouard@1494
   633
vb@1097
   634
    if (encrypted) {
edouard@1386
   635
        if (msg->payload.present == DeviceGroup_Protocol__payload_PR_groupKeys || 
edouard@1386
   636
            msg->payload.present == DeviceGroup_Protocol__payload_PR_groupUpdate) {
vb@1101
   637
            PEP_rating rating = PEP_rating_undefined;
vb@1101
   638
            status = outgoing_message_rating(session, _message, &rating);
vb@1101
   639
            if (status != PEP_STATUS_OK)
vb@1101
   640
                goto error;
vb@1101
   641
            if (rating < PEP_rating_trusted) {
vb@1101
   642
                status = PEP_SYNC_NO_TRUST;
vb@1101
   643
                goto error;
vb@1101
   644
            }
edouard@1386
   645
            
edouard@1364
   646
            stringlist_t *keylist = NULL;
edouard@1412
   647
            status = _own_keys_retrieve(session, &keylist, PEP_idf_not_for_sync);
edouard@1364
   648
            if (status != PEP_STATUS_OK)
edouard@1364
   649
                goto error;
edouard@1281
   650
edouard@1364
   651
            for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
edouard@1364
   652
                char *fpr = _keylist->value;
vb@1133
   653
                static char filename[MAX_LINELENGTH];
vb@1133
   654
                int result = snprintf(filename, MAX_LINELENGTH, "%s-sec.asc", fpr);
vb@1133
   655
                if (result < 0)
vb@1133
   656
                    goto enomem;
vb@1133
   657
                char *key = NULL;
vb@1133
   658
                size_t size = 0;
vb@1133
   659
                status = export_secrect_key(session, fpr, &key, &size);
vb@1133
   660
                if (status != PEP_STATUS_OK)
vb@1133
   661
                    goto error;
vb@1104
   662
                bloblist_t *bl = bloblist_add(_message->attachments,
vb@1133
   663
                        (char *) key, size, "application/pgp-keys", filename);
vb@1104
   664
                if (!bl)
vb@1104
   665
                    goto enomem;
vb@1104
   666
                if (!_message->attachments)
vb@1104
   667
                    _message->attachments = bl;
vb@1104
   668
            }
vb@1101
   669
        }
vb@1101
   670
vb@1097
   671
        message *_encrypted = NULL;
vb@1097
   672
        status = encrypt_message(session, _message, NULL, &_encrypted, PEP_enc_PEP, 0);
vb@1097
   673
        if (status != PEP_STATUS_OK)
vb@1097
   674
            goto error;
vb@1097
   675
        free_message(_message);
vb@1097
   676
        _message = _encrypted;
vb@1097
   677
    }
vb@1131
   678
    else {
vb@1131
   679
        attach_own_key(session, _message);
vb@1131
   680
    }
vb@1059
   681
vb@1097
   682
    status = session->messageToSend(session->sync_obj, _message);
vb@1059
   683
    return status;
vb@1059
   684
vb@1059
   685
enomem:
vb@1059
   686
    status = PEP_OUT_OF_MEMORY;
vb@1059
   687
error:
vb@1088
   688
    free_identity(_me);
vb@1059
   689
    free(payload);
vb@1059
   690
    free_message(_message);
vb@1059
   691
    free_identity(me);
vb@1059
   692
    return status;
vb@1059
   693
}
vb@1059
   694
vb@1059
   695
PEP_STATUS multicast_self_msg(
vb@1059
   696
        PEP_SESSION session,
vb@1059
   697
        DeviceState_state state,
vb@1097
   698
        DeviceGroup_Protocol_t *msg,
vb@1097
   699
        bool encrypted
vb@1059
   700
    )
vb@1059
   701
{
vb@1059
   702
    PEP_STATUS status = PEP_STATUS_OK;
vb@1059
   703
vb@1059
   704
    assert(session && state && msg);
vb@1059
   705
    if (!(session && state && msg))
vb@1059
   706
        return PEP_ILLEGAL_VALUE;
vb@1059
   707
vb@1059
   708
    identity_list *own_identities = NULL;
edouard@1412
   709
    status = _own_identities_retrieve(session, &own_identities, PEP_idf_not_for_sync);
vb@1059
   710
    if (status != PEP_STATUS_OK)
vb@1059
   711
        return status;
vb@1059
   712
vb@1059
   713
    for (identity_list *_i = own_identities; _i && _i->ident; _i = _i->next) {
vb@1091
   714
        pEp_identity *me = _i->ident;
vb@1059
   715
vb@1065
   716
        // FIXME: no deep copy for multicast supported yet
edouard@1281
   717
        // DeviceGroup_Protocol_t *_msg = malloc(sizeof(DeviceGroup_Protocol_t));
edouard@1281
   718
        // assert(_msg);
edouard@1281
   719
        // if (_msg == NULL){
edouard@1281
   720
        //     status = PEP_OUT_OF_MEMORY;
edouard@1281
   721
        //     goto error;
edouard@1281
   722
        // }
edouard@1281
   723
        // memcpy(_msg, msg, sizeof(DeviceGroup_Protocol_t));
edouard@1281
   724
        status = unicast_msg(session, me, state, msg, encrypted);
edouard@1281
   725
        //status = unicast_msg(session, me, state, _msg, encrypted);
edouard@1281
   726
        //free_DeviceGroup_Protocol_msg(_msg);
vb@1059
   727
    }
vb@1059
   728
vb@1059
   729
    free_identity_list(own_identities);
vb@1059
   730
    return PEP_STATUS_OK;
vb@1065
   731
edouard@1281
   732
// error:
edouard@1281
   733
//     free_identity_list(own_identities);
edouard@1281
   734
//     return status;
vb@1059
   735
}
vb@1059
   736