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