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