sync/cond_act_sync.yml2
author Volker Birk <vb@pep-project.org>
Fri, 26 Apr 2019 05:47:58 +0200
branchsync
changeset 3550 9cc0d4868311
parent 3527 a4c50a8eeccf
child 3583 3c796707c727
permissions -rw-r--r--
rename to negotiation
vb@2831
     1
// This file is under GNU General Public License 3.0
vb@2831
     2
// see LICENSE.txt
vb@2831
     3
vb@2831
     4
// generate conditions and actions
vb@2831
     5
vb@3512
     6
// Copyleft (c) 2017-2019, p≡p foundation
vb@2831
     7
vb@2831
     8
// Written by Volker Birk
vb@2831
     9
vb@2831
    10
vb@2844
    11
include ./sql_func.yml2
vb@2831
    12
vb@2831
    13
// condition: PEP_STATUS «@name»(PEP_SESSION session, bool *result)
vb@2831
    14
vb@2831
    15
condition deviceGrouped {
vb@2831
    16
    call "exec_sql_int" with "sql"
vb@3525
    17
        > "select count(*) from identity where is_own = 1 and (flags & 0x100) = 0x100;"
vb@2831
    18
    |> *result = _result > 0;
vb@2831
    19
}
vb@2831
    20
vb@2902
    21
condition weAreFirst
vb@2902
    22
||
vb@2902
    23
    TID_t *t1 = &session->sync_state.keysync.challenge;
vb@2902
    24
    TID_t *t2 = &session->own_sync_state.challenge;
vb@2902
    25
vb@2905
    26
    *result = _TID_greater(t1, t2);
vb@2902
    27
||
vb@2902
    28
vb@2831
    29
condition partnerIsGrouped
vb@2831
    30
|> *result = session->sync_state.keysync.is_group;
vb@2831
    31
vb@2831
    32
condition challengeAccepted
vb@2831
    33
||
vb@2831
    34
    TID_t *t1 = &session->sync_state.keysync.challenge;
vb@2831
    35
    TID_t *t2 = &session->own_sync_state.challenge;
vb@2831
    36
vb@2831
    37
    *result = t1->size == t2->size && memcmp(t1->buf, t2->buf, t1->size) == 0;
vb@2831
    38
||
vb@2831
    39
vb@3514
    40
condition sameChallenge
vb@3514
    41
||
vb@3514
    42
    TID_t *t1 = &session->sync_state.keysync.challenge;
vb@3514
    43
    TID_t *t2 = &session->own_sync_state.challenge;
vb@3514
    44
vb@3514
    45
    *result = t1->size == t2->size && memcmp(t1->buf, t2->buf, t1->size) == 0;
vb@3514
    46
||
vb@3514
    47
vb@3510
    48
condition sameTransaction
vb@3510
    49
||
vb@3550
    50
    TID_t *t1 = &session->sync_state.keysync.negotiation;
vb@3550
    51
    TID_t *t2 = &session->own_sync_state.negotiation;
vb@3510
    52
vb@3510
    53
    *result = t1->size == t2->size && memcmp(t1->buf, t2->buf, t1->size) == 0;
vb@3510
    54
||
vb@3510
    55
vb@2831
    56
condition keyElectionWon
vb@2831
    57
||
vb@2838
    58
    pEp_identity *from = session->sync_state.common.from;
vb@3389
    59
    char *signature_fpr = session->sync_state.common.signature_fpr;
vb@2831
    60
vb@3389
    61
    assert(from && from->address && from->address[0] && from->user_id &&
vb@3389
    62
            from->user_id[0]);
vb@3389
    63
    if (!(from && from->address && from->address[0] && from->user_id &&
vb@3389
    64
            from->user_id[0]))
vb@2831
    65
        return PEP_ILLEGAL_VALUE;
vb@2831
    66
vb@2831
    67
    pEp_identity *me = NULL;
vb@2901
    68
    PEP_STATUS status = get_identity(session, from->address, from->user_id, &me);
vb@2831
    69
    assert(status == PEP_STATUS_OK);
vb@2831
    70
    if (status)
vb@2831
    71
        return status;
vb@2831
    72
vb@2831
    73
    assert(me->fpr && me->fpr[0]);
vb@2831
    74
    if (!(me->fpr && me->fpr[0])) {
vb@2831
    75
        free_identity(me);
vb@2831
    76
        return PEP_ILLEGAL_VALUE;
vb@2831
    77
    }
vb@2831
    78
vb@3389
    79
    size_t len = MIN(strlen(signature_fpr), strlen(me->fpr));
vb@3389
    80
    *result = strncasecmp(signature_fpr, me->fpr, len) > 0;
vb@2831
    81
    free_identity(me);
vb@2831
    82
||
vb@2831
    83
vb@2831
    84
// action: PEP_STATUS «@name»(PEP_SESSION session)
vb@2831
    85
vb@2831
    86
function "new_UUID" {
vb@2831
    87
    param "dst";
vb@2831
    88
    ||
vb@2831
    89
        pEpUUID c;
vb@2831
    90
        uuid_generate_random(c);
vb@2831
    91
vb@2831
    92
        OCTET_STRING_fromBuf(«$dst», (char *) c, 16);
vb@2831
    93
    ||
vb@2831
    94
}
vb@2831
    95
vb@2831
    96
function "copy_UUID" {
vb@2831
    97
    param "src", param "dst";
vb@2831
    98
    ||
vb@2831
    99
        TID_t *src = «$src»;
vb@2831
   100
        TID_t *dst = «$dst»;
vb@2831
   101
vb@2831
   102
        assert(src->size == 16);
vb@2831
   103
        if (!(src->size == 16))
vb@2831
   104
            return PEP_UNKNOWN_ERROR;
vb@2831
   105
vb@2831
   106
        OCTET_STRING_fromBuf(dst, (char *) src->buf, src->size);
vb@2831
   107
    ||
vb@2831
   108
}
vb@2831
   109
vb@3510
   110
action newChallenge
vb@2831
   111
    call "new_UUID" with "dst" > &session->own_sync_state.challenge
vb@2831
   112
vb@2831
   113
action storeChallenge call "copy_UUID" {
vb@2831
   114
    with "src" > &session->sync_state.keysync.challenge
vb@2831
   115
    with "dst" > &session->own_sync_state.challenge
vb@2831
   116
}
vb@2831
   117
vb@3510
   118
action openTransaction {
vb@3510
   119
||
vb@3550
   120
    for (int i=0; i<session->sync_state.keysync.negotiation.size; ++i) {
vb@3550
   121
        if (session->sync_state.keysync.negotiation.buf[i])
vb@3510
   122
            return PEP_STATUS_OK;
vb@3510
   123
    }
vb@3510
   124
||
vb@3550
   125
    call "new_UUID" with "dst" > &session->sync_state.keysync.negotiation
vb@3510
   126
}
vb@3510
   127
vb@3510
   128
action closeTransaction
vb@3510
   129
||
vb@3550
   130
    memset(session->sync_state.keysync.negotiation.buf, 0,
vb@3550
   131
            session->sync_state.keysync.negotiation.size);
vb@3510
   132
||
vb@2831
   133
vb@2831
   134
action storeTransaction call "copy_UUID" {
vb@3550
   135
    with "src" > &session->sync_state.keysync.negotiation
vb@3550
   136
    with "dst" > &session->own_sync_state.negotiation
vb@2831
   137
}
vb@2831
   138
vb@2831
   139
function "show_handshake" {
vb@2831
   140
    param "type";
vb@2831
   141
    ||
vb@2831
   142
        assert(session->notifyHandshake);
vb@2831
   143
        if (!session->notifyHandshake)
vb@2831
   144
            return PEP_SYNC_NO_NOTIFY_CALLBACK;
vb@2831
   145
     
vb@3525
   146
    ||
vb@3525
   147
    choose {
vb@3525
   148
    when "$type = 'SYNC_NOTIFY_TIMEOUT' or $type = 'SYNC_NOTIFY_SOLE' or $type = 'SYNC_NOTIFY_IN_GROUP'"
vb@3525
   149
    ||
vb@3525
   150
        pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
vb@3525
   151
        pEp_identity *partner = new_identity(NULL, NULL, NULL, NULL);
vb@3525
   152
        assert(me && partner);
vb@3525
   153
        if (!(me && partner)) {
vb@3525
   154
            free_identity(me);
vb@3525
   155
            free_identity(partner);
vb@3525
   156
            return PEP_OUT_OF_MEMORY;
vb@3525
   157
        }
vb@3525
   158
vb@3525
   159
        PEP_STATUS status = session->notifyHandshake(me, partner, «$type»);
vb@3525
   160
        if (status)
vb@3525
   161
            return status;
vb@3525
   162
    ||
vb@3525
   163
    otherwise
vb@3525
   164
    ||
vb@2838
   165
        assert(session->sync_state.common.from);
vb@2838
   166
        if (!session->sync_state.common.from)
vb@2831
   167
            return PEP_ILLEGAL_VALUE;
vb@2831
   168
vb@2838
   169
        pEp_identity *from = session->sync_state.common.from;
vb@2831
   170
        pEp_identity *me = NULL;
vb@2901
   171
        PEP_STATUS status = get_identity(session, from->address, from->user_id, &me);
vb@2831
   172
        assert(status == PEP_STATUS_OK);
vb@2831
   173
        if (status)
vb@2831
   174
            return status;
vb@2831
   175
vb@2831
   176
        assert(me->fpr && me->fpr[0]);
vb@2831
   177
        if (!(me->fpr && me->fpr[0])) {
vb@2831
   178
            free_identity(me);
vb@2831
   179
            return PEP_ILLEGAL_VALUE;
vb@2831
   180
        }
vb@2831
   181
vb@2831
   182
        pEp_identity *partner = identity_dup(from);
vb@2831
   183
        if (!partner) {
vb@2831
   184
            free_identity(me);
vb@2831
   185
            return PEP_OUT_OF_MEMORY;
vb@2831
   186
        }
vb@3365
   187
vb@3379
   188
        assert(session->sync_state.common.signature_fpr);
vb@3379
   189
        if (session->sync_state.common.signature_fpr) {
vb@3379
   190
            free(partner->fpr);
vb@3379
   191
            partner->fpr = strdup(session->sync_state.common.signature_fpr);
vb@3379
   192
            if (!partner->fpr) {
vb@3379
   193
                free_identity(me);
vb@3379
   194
                free_identity(partner);
vb@3379
   195
                return PEP_OUT_OF_MEMORY;
vb@3379
   196
            }
vb@3364
   197
        }
vb@2831
   198
vb@3047
   199
        status = session->notifyHandshake(me, partner, «$type»);
vb@2831
   200
        if (status)
vb@2831
   201
            return status;
vb@2831
   202
    ||
vb@3525
   203
    }
vb@2831
   204
}
vb@2831
   205
vb@2831
   206
action showSoleHandshake
vb@2831
   207
    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_FORM_GROUP
vb@2831
   208
vb@2831
   209
action showJoinGroupHandshake
vb@2831
   210
    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_ADD_OUR_DEVICE
vb@2831
   211
vb@2831
   212
action showGroupedHandshake
vb@2831
   213
    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE
vb@2831
   214
vb@3522
   215
action hideHandshakeDialog
vb@3522
   216
    call "show_handshake" with "type" > SYNC_NOTIFY_OVERTAKEN
vb@3522
   217
vb@3524
   218
action showDeviceAdded
vb@3524
   219
    call "show_handshake" with "type" > SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED
vb@3524
   220
vb@3524
   221
action showGroupCreated
vb@3524
   222
    call "show_handshake" with "type" > SYNC_NOTIFY_ACCEPTED_GROUP_CREATED
vb@3524
   223
vb@3525
   224
action showBeingSole
vb@3525
   225
    call "show_handshake" with "type" > SYNC_NOTIFY_SOLE
vb@3525
   226
vb@3525
   227
action showBeingInGroup
vb@3525
   228
    call "show_handshake" with "type" > SYNC_NOTIFY_IN_GROUP
vb@3525
   229
vb@2914
   230
timeout KeySync
vb@2913
   231
    call "show_handshake" with "type" > SYNC_NOTIFY_TIMEOUT
vb@2913
   232
vb@3390
   233
action prepareOwnKeys
vb@3390
   234
||
vb@3390
   235
    stringlist_t *own_keys;
vb@3390
   236
    PEP_STATUS status = _own_keys_retrieve(session, &own_keys, PEP_idf_not_for_sync);
vb@3390
   237
    if (status)
vb@3390
   238
        return status;
vb@3390
   239
vb@3390
   240
    if (session->sync_state.common.own_keys)
vb@3390
   241
        free_stringlist(session->sync_state.common.own_keys);
vb@3390
   242
    session->sync_state.common.own_keys = own_keys;
vb@3394
   243
vb@3394
   244
    identity_list *il;
vb@3394
   245
    status = _own_identities_retrieve(session, &il, PEP_idf_not_for_sync);
vb@3394
   246
    if (status)
vb@3394
   247
        return status;
vb@3394
   248
vb@3394
   249
    IdentityList_from_identity_list(il, &session->sync_state.keysync.ownIdentities);
vb@3390
   250
||
vb@3390
   251
vb@2831
   252
action saveGroupKeys
vb@2831
   253
||
vb@3390
   254
    identity_list *il = IdentityList_to_identity_list(&session->sync_state.keysync.ownIdentities, NULL);
vb@2831
   255
    if (!il)
vb@2831
   256
        return PEP_OUT_OF_MEMORY;
vb@2831
   257
    
vb@2831
   258
    // BUG: this should be a transaction and been rolled back completely on error
vb@2831
   259
    for (identity_list *_il = il; _il && _il->ident; _il = _il->next) {
vb@2831
   260
        PEP_STATUS status = set_identity(session, _il->ident);
vb@2831
   261
        if (status) {
vb@2831
   262
            free_identity_list(il);
vb@2831
   263
            return status;
vb@2831
   264
        }
vb@2831
   265
    }
vb@2831
   266
vb@2831
   267
    free_identity_list(il);
vb@2831
   268
||
vb@2831
   269
vb@3434
   270
action ownKeysAreGroupKeys
vb@3434
   271
||
vb@3434
   272
    PEP_STATUS status = PEP_STATUS_OK;
vb@3434
   273
vb@3434
   274
    // set flag for current keys
vb@3434
   275
    for (identity_list *il = session->sync_state.common.own_identities; il && il->ident ; il = il->next) {
vb@3434
   276
        if (!(il->ident->flags && PEP_idf_not_for_sync)) {
vb@3434
   277
            status = set_identity_flags(session, il->ident, PEP_idf_devicegroup);
vb@3434
   278
            if (status)
vb@3434
   279
                return status;
vb@3434
   280
        }
vb@2831
   281
    }
vb@3434
   282
||
vb@2831
   283
vb@3525
   284
action receivedKeysAreGroupKeys
vb@3525
   285
||
vb@3525
   286
    PEP_STATUS status = PEP_STATUS_OK;
vb@3527
   287
vb@3527
   288
    // set flag for current keys
vb@3527
   289
    for (identity_list *il = session->sync_state.common.own_identities; il && il->ident ; il = il->next) {
vb@3527
   290
        if (!(il->ident->flags && PEP_idf_not_for_sync)) {
vb@3527
   291
            status = set_identity_flags(session, il->ident, PEP_idf_devicegroup);
vb@3527
   292
            if (status)
vb@3527
   293
                return status;
vb@3527
   294
        }
vb@3527
   295
    }
vb@3527
   296
vb@3525
   297
    identity_list *il = IdentityList_to_identity_list(&session->sync_state.keysync.ownIdentities, NULL);
vb@3525
   298
    if (!il)
vb@3525
   299
        return PEP_OUT_OF_MEMORY;
vb@3525
   300
vb@3525
   301
    for (il = session->sync_state.common.own_identities; il && il->ident ; il = il->next) {
vb@3525
   302
        // replace partner's user_id with own user_id
vb@3525
   303
        free(il->ident->user_id);
vb@3525
   304
        il->ident->user_id = strdup(session->sync_state.common.from->user_id);
vb@3525
   305
        if (!il->ident->user_id) {
vb@3525
   306
            free_identity_list(il);
vb@3525
   307
            return PEP_OUT_OF_MEMORY;
vb@3525
   308
        }
vb@3525
   309
vb@3525
   310
        status = myself(session, il->ident);
vb@3525
   311
        if (status) {
vb@3525
   312
            free_identity_list(il);
vb@3525
   313
            return status;
vb@3525
   314
        }
vb@3525
   315
vb@3525
   316
        status = set_identity_flags(session, il->ident, PEP_idf_devicegroup);
vb@3525
   317
        if (status) {
vb@3525
   318
            free_identity_list(il);
vb@3525
   319
            return status;
vb@3525
   320
        }
vb@3525
   321
    }
vb@3525
   322
vb@3525
   323
    free_identity_list(il);
vb@3525
   324
||
vb@3406
   325
vb@3516
   326
action trustThisKey
vb@3516
   327
||
vb@3516
   328
    assert(session->sync_state.common.from && session->sync_state.common.signature_fpr);
vb@3516
   329
    if (!(session->sync_state.common.from && session->sync_state.common.signature_fpr))
vb@3516
   330
        return PEP_ILLEGAL_VALUE;
vb@3516
   331
vb@3516
   332
    pEp_identity *ident = session->sync_state.common.from;
vb@3516
   333
    free(ident->fpr);
vb@3516
   334
    ident->fpr = strdup(session->sync_state.common.signature_fpr);
vb@3516
   335
    assert(ident->fpr);
vb@3516
   336
    if (!ident->fpr)
vb@3516
   337
        return PEP_OUT_OF_MEMORY;
vb@3516
   338
vb@3516
   339
    PEP_STATUS status = trust_own_key(session, ident);
vb@3516
   340
    if (status)
vb@3516
   341
        return status;
vb@3523
   342
vb@3523
   343
    OCTET_STRING_fromBuf(&session->sync_state.keysync.key, ident->fpr, strlen(ident->fpr));
vb@3516
   344
||
vb@3516
   345
vb@3524
   346
action untrustThisKey
vb@3524
   347
||
vb@3524
   348
    assert(session->sync_state.common.from && session->sync_state.common.signature_fpr);
vb@3524
   349
    if (!(session->sync_state.common.from && session->sync_state.common.signature_fpr))
vb@3524
   350
        return PEP_ILLEGAL_VALUE;
vb@3524
   351
vb@3524
   352
    pEp_identity *ident = session->sync_state.common.from;
vb@3524
   353
    free(ident->fpr);
vb@3524
   354
    ident->fpr = strdup(session->sync_state.common.signature_fpr);
vb@3524
   355
    assert(ident->fpr);
vb@3524
   356
    if (!ident->fpr)
vb@3524
   357
        return PEP_OUT_OF_MEMORY;
vb@3524
   358
vb@3524
   359
    PEP_STATUS status = key_reset_trust(session, ident);
vb@3524
   360
    if (status)
vb@3524
   361
        return status;
vb@3524
   362
vb@3524
   363
    OCTET_STRING_fromBuf(&session->sync_state.keysync.key, "", 0);
vb@3524
   364
||
vb@3524
   365
vb@3518
   366
action tellWeAreGrouped
vb@3518
   367
||
vb@3518
   368
    session->sync_state.keysync.is_group = true;
vb@3518
   369
||
vb@3518
   370
vb@3518
   371
action tellWeAreNotGrouped
vb@3518
   372
||
vb@3518
   373
    session->sync_state.keysync.is_group = false;
vb@3518
   374
||
vb@3518
   375
vb@2831
   376
action disable;
vb@2831
   377