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