src/sync_impl.c
author Edouard Tisserant <edouard@pep-project.org>
Thu, 27 Oct 2016 10:15:31 +0200
changeset 1339 a8b118fae4ae
parent 1338 515f4edb52d0
child 1340 276625d4f083
permissions -rw-r--r--
sync : exclude handshake message from device already using trusted keys, i.e. presumably in the 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@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
edouard@1339
   260
    bool consume = false;
edouard@1339
   261
    bool discard = false;
edouard@1248
   262
    bool force_keep_msg = false;
vb@1338
   263
vb@1059
   264
    for (bloblist_t *bl = src->attachments; bl && bl->value; bl = bl->next) {
vb@1132
   265
        if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0
vb@1059
   266
                && bl->size) {
vb@1077
   267
            DeviceGroup_Protocol_t *msg = NULL;
vb@1091
   268
            uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol, (void **)
vb@1091
   269
                    &msg, bl->value, bl->size);
edouard@1248
   270
vb@1059
   271
            if (msg) {
edouard@1257
   272
                PEP_STATUS status = PEP_STATUS_OK;
vb@1088
   273
vb@1091
   274
                char *user_id = strndup((char *) msg->header.me.user_id->buf,
vb@1091
   275
                        msg->header.me.user_id->size);
vb@1091
   276
                assert(user_id);
vb@1091
   277
                if (!user_id) {
vb@1091
   278
                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
vb@1091
   279
                    return PEP_OUT_OF_MEMORY;
vb@1091
   280
                }
vb@1091
   281
edouard@1248
   282
                // check message expiry 
edouard@1257
   283
                if(src->recv) {
edouard@1316
   284
                    time_t expiry = timegm(src->recv) + SYNC_MSG_EXPIRE_TIME;
edouard@1257
   285
                    time_t now = time(NULL);
edouard@1257
   286
                    if(expiry != 0 && now != 0 && expiry < now){
edouard@1339
   287
                        consume = true;
edouard@1257
   288
                        goto free_all;
edouard@1257
   289
                    }
edouard@1165
   290
                }
edouard@1165
   291
edouard@1248
   292
                int32_t value = (int32_t) msg->header.sequence;
edouard@1257
   293
                status = sequence_value(session, (char *) user_id,
vb@1091
   294
                        &value);
vb@1088
   295
vb@1088
   296
                if (status == PEP_STATUS_OK) {
edouard@1248
   297
                    switch (msg->payload.present) {
edouard@1248
   298
                        // HandshakeRequest needs encryption
edouard@1248
   299
                        case DeviceGroup_Protocol__payload_PR_handshakeRequest:
edouard@1248
   300
                            if (rating < PEP_rating_reliable ||
edouard@1248
   301
                                strncmp(session->sync_uuid,
edouard@1248
   302
                                        (const char *)msg->payload.choice.handshakeRequest.partner.user_id->buf,
edouard@1248
   303
                                        msg->payload.choice.handshakeRequest.partner.user_id->size) != 0){
edouard@1339
   304
                                discard = true;
edouard@1256
   305
                                goto free_all;
edouard@1248
   306
                            }
edouard@1339
   307
                            
edouard@1339
   308
                            // Ignore and consume handshakes with devices
edouard@1339
   309
                            // already using trusted own key to encrypt
edouard@1339
   310
                            if (rating >= PEP_rating_trusted){
edouard@1339
   311
                                consume = true;
edouard@1339
   312
                                goto free_all;
edouard@1339
   313
                            }
edouard@1339
   314
edouard@1248
   315
                            break;
edouard@1297
   316
                        // accepting GroupKeys needs encryption and trust of peer device
edouard@1248
   317
                        case DeviceGroup_Protocol__payload_PR_groupKeys:
edouard@1297
   318
                        {
edouard@1248
   319
                            if (!keylist || rating < PEP_rating_reliable ||
edouard@1297
   320
                                // message is only consumed by instance it is addressed to
edouard@1297
   321
                                (strncmp(session->sync_uuid,
edouard@1248
   322
                                        (const char *)msg->payload.choice.groupKeys.partner.user_id->buf,
edouard@1248
   323
                                        msg->payload.choice.groupKeys.partner.user_id->size) != 0)){
edouard@1339
   324
                                discard = true;
edouard@1256
   325
                                goto free_all;
edouard@1248
   326
                            }
edouard@1248
   327
edouard@1297
   328
                            // check trust of identity using user_id given in payload
edouard@1297
   329
                            // to exacly match identity of device, the one trusted in
edouard@1297
   330
                            // case of accepted handshake
edouard@1297
   331
                            pEp_identity *_from = new_identity(NULL, 
edouard@1248
   332
                                                               keylist->value,
edouard@1248
   333
                                                               user_id,
edouard@1297
   334
                                                               NULL);
edouard@1248
   335
                            if (_from == NULL){
edouard@1248
   336
                                status = PEP_OUT_OF_MEMORY;
edouard@1256
   337
                                goto free_all;
edouard@1248
   338
                            }
edouard@1297
   339
                            status = get_trust(session, _from);
edouard@1297
   340
                            if (_from->comm_type < PEP_ct_strong_encryption) {
edouard@1297
   341
                                free_identity(_from);
edouard@1339
   342
                                discard = true;
edouard@1256
   343
                                goto free_all;
edouard@1248
   344
                            }
edouard@1297
   345
                            free_identity(_from);
edouard@1248
   346
                            break;
edouard@1297
   347
                        }
edouard@1297
   348
                        case DeviceGroup_Protocol__payload_PR_groupUpdate:
edouard@1316
   349
                        case DeviceGroup_Protocol__payload_PR_updateRequest:
edouard@1316
   350
                        {
edouard@1297
   351
                            // inject message but don't consume it, so 
edouard@1297
   352
                            // that other group members can also be updated
edouard@1297
   353
                            force_keep_msg = true;
edouard@1297
   354
                            
edouard@1297
   355
                            if (!keylist || rating < PEP_rating_reliable){
edouard@1339
   356
                                discard = true;
edouard@1297
   357
                                goto free_all;
edouard@1297
   358
                            }
edouard@1297
   359
                            // GroupUpdate and UpdateRequests come from group.
edouard@1297
   360
                            // check trust relation in between signer key and 
edouard@1297
   361
                            // own id to be sure.
edouard@1297
   362
                            pEp_identity *_from = new_identity(NULL, 
edouard@1297
   363
                                                               keylist->value,
edouard@1297
   364
                                                               PEP_OWN_USERID,
edouard@1297
   365
                                                               NULL);
edouard@1297
   366
                            if (_from == NULL){
edouard@1297
   367
                                status = PEP_OUT_OF_MEMORY;
edouard@1297
   368
                                goto free_all;
edouard@1297
   369
                            }
edouard@1297
   370
                            status = get_trust(session, _from);
edouard@1297
   371
                            if (_from->comm_type < PEP_ct_pEp) {
edouard@1297
   372
                                free_identity(_from);
edouard@1339
   373
                                discard = true;
edouard@1297
   374
                                goto free_all;
edouard@1297
   375
                            }
edouard@1297
   376
                            free_identity(_from);
edouard@1297
   377
                        }
edouard@1248
   378
                        default:
edouard@1248
   379
                            break;
edouard@1248
   380
                    }
edouard@1248
   381
edouard@1248
   382
edouard@1339
   383
                    consume = true;
edouard@1172
   384
                    sync_msg_t *sync_msg = malloc(sizeof(sync_msg_t));
edouard@1172
   385
                    if(sync_msg == NULL){
edouard@1248
   386
                        status = PEP_OUT_OF_MEMORY;
edouard@1256
   387
                        goto free_all;
edouard@1172
   388
                    }
edouard@1172
   389
                    sync_msg->is_a_message = true;
edouard@1172
   390
                    sync_msg->u.message = msg;
edouard@1172
   391
                    status = call_inject_sync_msg(session, sync_msg);
edouard@1165
   392
                    if (status != PEP_STATUS_OK){
edouard@1172
   393
                        if (status == PEP_SYNC_NO_INJECT_CALLBACK){
edouard@1172
   394
                            free(sync_msg);
edouard@1172
   395
                        }
edouard@1256
   396
                        goto free_all;
edouard@1165
   397
                    }
edouard@1339
   398
                    // don't free message now that it is in the queue
edouard@1256
   399
                    goto free_userid;
edouard@1165
   400
                }
edouard@1165
   401
                else if (status == PEP_OWN_SEQUENCE) {
edouard@1248
   402
                    status = PEP_STATUS_OK;
edouard@1339
   403
                    discard = true;
edouard@1256
   404
                    goto free_all;
vb@1088
   405
                }
edouard@1248
   406
edouard@1256
   407
            free_all:
edouard@1248
   408
                ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
edouard@1256
   409
            free_userid:
edouard@1248
   410
                free(user_id);
edouard@1248
   411
edouard@1248
   412
                if (status != PEP_STATUS_OK)
edouard@1248
   413
                    return status;
vb@1059
   414
            }
vb@1059
   415
        }
vb@1059
   416
    }
vb@1059
   417
edouard@1248
   418
    if (force_keep_msg) {
edouard@1248
   419
        return PEP_MESSAGE_DISCARDED;
edouard@1248
   420
    }
edouard@1248
   421
edouard@1339
   422
    if (consume && !session->keep_sync_msg) {
vb@1059
   423
        for (stringpair_list_t *spl = src->opt_fields ; spl && spl->value ;
vb@1059
   424
                spl = spl->next) {
vb@1059
   425
            if (spl->value->key &&
vb@1059
   426
                    strcasecmp(spl->value->key, "pEp-auto-consume") == 0) {
vb@1059
   427
                if (spl->value->value &&
vb@1059
   428
                        strcasecmp(spl->value->value, "yes") == 0)
vb@1059
   429
                    return PEP_MESSAGE_CONSUMED;
vb@1059
   430
            }
vb@1059
   431
        }
edouard@1248
   432
        return PEP_MESSAGE_DISCARDED;
vb@1059
   433
    }
vb@1059
   434
edouard@1339
   435
    if(discard)
edouard@1248
   436
        return PEP_MESSAGE_DISCARDED;
edouard@1248
   437
vb@1338
   438
    if (!session->keep_sync_msg) {
vb@1338
   439
        bloblist_t *last = NULL;
vb@1338
   440
        for (bloblist_t *bl = src->attachments; bl && bl->value; ) {
vb@1338
   441
            if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0) {
vb@1338
   442
                bloblist_t *b = bl;
vb@1338
   443
                bl = bl->next;
vb@1338
   444
                if (!last)
vb@1338
   445
                    src->attachments = bl;
vb@1338
   446
                else
vb@1338
   447
                    last->next = bl;
vb@1338
   448
                free(b->mime_type);
vb@1338
   449
                free(b->filename);
vb@1338
   450
                free(b->value);
vb@1338
   451
                free(b);
vb@1338
   452
            }
vb@1338
   453
            else {
vb@1338
   454
                last = bl;
vb@1338
   455
                bl = bl->next;
vb@1338
   456
            }
vb@1338
   457
        }
vb@1338
   458
    }
vb@1338
   459
vb@1059
   460
    return PEP_STATUS_OK;
vb@1059
   461
}
vb@1059
   462
vb@1059
   463
DeviceGroup_Protocol_t *new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR type)
vb@1059
   464
{
vb@1059
   465
    DeviceGroup_Protocol_t *msg = (DeviceGroup_Protocol_t *)
vb@1073
   466
            calloc(1, sizeof(DeviceGroup_Protocol_t));
vb@1059
   467
    assert(msg);
vb@1059
   468
    if (!msg)
vb@1059
   469
        return NULL;
vb@1059
   470
    msg->payload.present = type;
vb@1059
   471
    return msg;
vb@1059
   472
}
vb@1059
   473
vb@1059
   474
void free_DeviceGroup_Protocol_msg(DeviceGroup_Protocol_t *msg)
vb@1059
   475
{
vb@1059
   476
    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
vb@1059
   477
}
vb@1059
   478
vb@1059
   479
PEP_STATUS unicast_msg(
vb@1059
   480
        PEP_SESSION session,
vb@1091
   481
        const Identity partner,
vb@1059
   482
        DeviceState_state state,
vb@1097
   483
        DeviceGroup_Protocol_t *msg,
vb@1097
   484
        bool encrypted
vb@1059
   485
    )
vb@1059
   486
{
vb@1059
   487
    PEP_STATUS status = PEP_STATUS_OK;
vb@1059
   488
    char *payload = NULL;
vb@1059
   489
    message *_message = NULL;
vb@1059
   490
    pEp_identity *me = NULL;
vb@1088
   491
    pEp_identity *_me = NULL;
vb@1059
   492
vb@1059
   493
    assert(session && partner && state && msg);
vb@1059
   494
    if (!(session && partner && state && msg))
vb@1059
   495
        return PEP_ILLEGAL_VALUE;
vb@1059
   496
vb@1059
   497
    assert(session->messageToSend);
vb@1059
   498
    if (!session->messageToSend) {
vb@1059
   499
        status = PEP_SEND_FUNCTION_NOT_REGISTERED;
vb@1059
   500
        goto error;
vb@1059
   501
    }
vb@1059
   502
vb@1077
   503
    msg->header.version.major = SYNC_VERSION_MAJOR;
vb@1077
   504
    msg->header.version.minor = SYNC_VERSION_MINOR;
vb@1077
   505
vb@1071
   506
    status = get_identity(session, partner->address, PEP_OWN_USERID, &me);
vb@1059
   507
    if (status != PEP_STATUS_OK)
vb@1059
   508
        goto error;
vb@1088
   509
    
vb@1088
   510
    int32_t seq = 0;
vb@1088
   511
edouard@1236
   512
    status = sequence_value(session, session->sync_uuid, &seq);
vb@1088
   513
    if (status != PEP_OWN_SEQUENCE && status != PEP_STATUS_OK)
vb@1088
   514
        goto error;
vb@1088
   515
vb@1088
   516
    msg->header.sequence = (long) seq;
vb@1088
   517
vb@1088
   518
    _me = identity_dup(me);
vb@1088
   519
    if (!_me)
vb@1059
   520
        goto enomem;
vb@1059
   521
vb@1088
   522
    free(_me->user_id);
edouard@1236
   523
    _me->user_id = strndup(session->sync_uuid, 36);
vb@1088
   524
    assert(_me->user_id);
vb@1088
   525
    if (!_me->user_id)
vb@1088
   526
        goto enomem;
vb@1088
   527
vb@1088
   528
    if (Identity_from_Struct(_me, &msg->header.me) == NULL)
vb@1088
   529
        goto enomem;
vb@1088
   530
vb@1088
   531
    free_identity(_me);
vb@1088
   532
    _me = NULL;
vb@1088
   533
vb@1077
   534
    msg->header.state = (long) state;
vb@1077
   535
vb@1077
   536
    bool devicegroup = storedGroupKeys(session);
vb@1077
   537
    if (devicegroup)
vb@1077
   538
        msg->header.devicegroup = 1;
vb@1077
   539
    else
vb@1077
   540
        msg->header.devicegroup = 0;
vb@1077
   541
vb@1059
   542
    if (asn_check_constraints(&asn_DEF_DeviceGroup_Protocol, msg, NULL, NULL)) {
vb@1059
   543
        status = PEP_CONTRAINTS_VIOLATED;
vb@1059
   544
        goto error;
vb@1059
   545
    }
vb@1059
   546
vb@1059
   547
    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_DeviceGroup_Protocol,
vb@1059
   548
            NULL, msg, (void **) &payload);
vb@1059
   549
    if (size == -1) {
vb@1059
   550
        status = PEP_CANNOT_ENCODE;
vb@1059
   551
        goto error;
vb@1059
   552
    }
vb@1059
   553
vb@1059
   554
    status = prepare_message(me, partner, payload, size, &_message);
vb@1059
   555
    if (status != PEP_STATUS_OK)
vb@1059
   556
        goto error;
vb@1059
   557
    payload = NULL;
vb@1059
   558
    free_identity(me);
vb@1059
   559
    me = NULL;
vb@1059
   560
vb@1097
   561
    if (encrypted) {
edouard@1281
   562
        if (msg->payload.present == DeviceGroup_Protocol__payload_PR_groupKeys || 
edouard@1281
   563
            msg->payload.present == DeviceGroup_Protocol__payload_PR_groupUpdate) {
vb@1101
   564
            PEP_rating rating = PEP_rating_undefined;
vb@1101
   565
            status = outgoing_message_rating(session, _message, &rating);
vb@1101
   566
            if (status != PEP_STATUS_OK)
vb@1101
   567
                goto error;
vb@1101
   568
            if (rating < PEP_rating_trusted) {
vb@1101
   569
                status = PEP_SYNC_NO_TRUST;
vb@1101
   570
                goto error;
vb@1101
   571
            }
vb@1104
   572
            
edouard@1281
   573
            IdentityList_t *list = 
edouard@1281
   574
                msg->payload.present == 
edouard@1281
   575
                  DeviceGroup_Protocol__payload_PR_groupKeys ?
edouard@1281
   576
                  &msg->payload.choice.groupKeys.ownIdentities :
edouard@1281
   577
                  &msg->payload.choice.groupUpdate.ownIdentities;
edouard@1281
   578
vb@1104
   579
            for (int i=0; i<list->list.count; i++) {
vb@1104
   580
                Identity_t *ident = list->list.array[i];
vb@1133
   581
                char *fpr = strndup((const char *)ident->fpr.buf, ident->fpr.size);
vb@1133
   582
                assert(fpr);
vb@1133
   583
                if (!fpr)
vb@1133
   584
                    goto enomem;
vb@1133
   585
                static char filename[MAX_LINELENGTH];
vb@1133
   586
                int result = snprintf(filename, MAX_LINELENGTH, "%s-sec.asc", fpr);
vb@1133
   587
                if (result < 0)
vb@1133
   588
                    goto enomem;
vb@1133
   589
                char *key = NULL;
vb@1133
   590
                size_t size = 0;
vb@1133
   591
                status = export_secrect_key(session, fpr, &key, &size);
vb@1133
   592
                free(fpr);
vb@1133
   593
                if (status != PEP_STATUS_OK)
vb@1133
   594
                    goto error;
vb@1104
   595
                bloblist_t *bl = bloblist_add(_message->attachments,
vb@1133
   596
                        (char *) key, size, "application/pgp-keys", filename);
vb@1104
   597
                if (!bl)
vb@1104
   598
                    goto enomem;
vb@1104
   599
                if (!_message->attachments)
vb@1104
   600
                    _message->attachments = bl;
vb@1104
   601
            }
vb@1101
   602
        }
vb@1101
   603
vb@1097
   604
        message *_encrypted = NULL;
vb@1097
   605
        status = encrypt_message(session, _message, NULL, &_encrypted, PEP_enc_PEP, 0);
vb@1097
   606
        if (status != PEP_STATUS_OK)
vb@1097
   607
            goto error;
vb@1097
   608
        free_message(_message);
vb@1097
   609
        _message = _encrypted;
vb@1097
   610
    }
vb@1131
   611
    else {
vb@1131
   612
        attach_own_key(session, _message);
vb@1131
   613
    }
vb@1059
   614
vb@1097
   615
    status = session->messageToSend(session->sync_obj, _message);
vb@1059
   616
    return status;
vb@1059
   617
vb@1059
   618
enomem:
vb@1059
   619
    status = PEP_OUT_OF_MEMORY;
vb@1059
   620
error:
vb@1088
   621
    free_identity(_me);
vb@1059
   622
    free(payload);
vb@1059
   623
    free_message(_message);
vb@1059
   624
    free_identity(me);
vb@1059
   625
    return status;
vb@1059
   626
}
vb@1059
   627
vb@1059
   628
PEP_STATUS multicast_self_msg(
vb@1059
   629
        PEP_SESSION session,
vb@1059
   630
        DeviceState_state state,
vb@1097
   631
        DeviceGroup_Protocol_t *msg,
vb@1097
   632
        bool encrypted
vb@1059
   633
    )
vb@1059
   634
{
vb@1059
   635
    PEP_STATUS status = PEP_STATUS_OK;
vb@1059
   636
vb@1059
   637
    assert(session && state && msg);
vb@1059
   638
    if (!(session && state && msg))
vb@1059
   639
        return PEP_ILLEGAL_VALUE;
vb@1059
   640
vb@1059
   641
    identity_list *own_identities = NULL;
vb@1059
   642
    status = own_identities_retrieve(session, &own_identities);
vb@1059
   643
    if (status != PEP_STATUS_OK)
vb@1059
   644
        return status;
vb@1059
   645
edouard@1236
   646
    // FIXME: exclude previously rejected identities
vb@1059
   647
    for (identity_list *_i = own_identities; _i && _i->ident; _i = _i->next) {
vb@1091
   648
        pEp_identity *me = _i->ident;
vb@1059
   649
vb@1065
   650
        // FIXME: no deep copy for multicast supported yet
edouard@1281
   651
        // DeviceGroup_Protocol_t *_msg = malloc(sizeof(DeviceGroup_Protocol_t));
edouard@1281
   652
        // assert(_msg);
edouard@1281
   653
        // if (_msg == NULL){
edouard@1281
   654
        //     status = PEP_OUT_OF_MEMORY;
edouard@1281
   655
        //     goto error;
edouard@1281
   656
        // }
edouard@1281
   657
        // memcpy(_msg, msg, sizeof(DeviceGroup_Protocol_t));
edouard@1281
   658
        status = unicast_msg(session, me, state, msg, encrypted);
edouard@1281
   659
        //status = unicast_msg(session, me, state, _msg, encrypted);
edouard@1281
   660
        //free_DeviceGroup_Protocol_msg(_msg);
vb@1059
   661
    }
vb@1059
   662
vb@1059
   663
    free_identity_list(own_identities);
vb@1059
   664
    return PEP_STATUS_OK;
vb@1065
   665
edouard@1281
   666
// error:
edouard@1281
   667
//     free_identity_list(own_identities);
edouard@1281
   668
//     return status;
vb@1059
   669
}
vb@1059
   670