sync/cond_act_sync.yml2
author Volker Birk <vb@pep-project.org>
Sun, 14 Apr 2019 16:00:52 +0200
branchsync
changeset 3523 9555e664423f
parent 3522 c8e3e531d40a
child 3524 c87c6b4f3e08
permissions -rw-r--r--
if one in the group accepted that's valid for all
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@3061
    17
        > "select count(*) from identity where is_own = 1 and (flags & 4) = 4;"
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@3510
    50
    TID_t *t1 = &session->sync_state.keysync.transaction;
vb@3510
    51
    TID_t *t2 = &session->own_sync_state.transaction;
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@3510
   120
    for (int i=0; i<session->sync_state.keysync.transaction.size; ++i) {
vb@3510
   121
        if (session->sync_state.keysync.transaction.buf[i])
vb@3510
   122
            return PEP_STATUS_OK;
vb@3510
   123
    }
vb@3510
   124
||
Thomas@3125
   125
    call "new_UUID" with "dst" > &session->sync_state.keysync.transaction
vb@3510
   126
}
vb@3510
   127
vb@3510
   128
action closeTransaction
vb@3510
   129
||
vb@3510
   130
    memset(session->sync_state.keysync.transaction.buf, 0,
vb@3510
   131
            session->sync_state.keysync.transaction.size);
vb@3510
   132
||
vb@2831
   133
vb@2831
   134
action storeTransaction call "copy_UUID" {
vb@2831
   135
    with "src" > &session->sync_state.keysync.transaction
Thomas@3125
   136
    with "dst" > &session->own_sync_state.transaction
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@2838
   146
        assert(session->sync_state.common.from);
vb@2838
   147
        if (!session->sync_state.common.from)
vb@2831
   148
            return PEP_ILLEGAL_VALUE;
vb@2831
   149
vb@2838
   150
        pEp_identity *from = session->sync_state.common.from;
vb@2831
   151
        pEp_identity *me = NULL;
vb@2901
   152
        PEP_STATUS status = get_identity(session, from->address, from->user_id, &me);
vb@2831
   153
        assert(status == PEP_STATUS_OK);
vb@2831
   154
        if (status)
vb@2831
   155
            return status;
vb@2831
   156
vb@2831
   157
        assert(me->fpr && me->fpr[0]);
vb@2831
   158
        if (!(me->fpr && me->fpr[0])) {
vb@2831
   159
            free_identity(me);
vb@2831
   160
            return PEP_ILLEGAL_VALUE;
vb@2831
   161
        }
vb@2831
   162
vb@2831
   163
        pEp_identity *partner = identity_dup(from);
vb@2831
   164
        if (!partner) {
vb@2831
   165
            free_identity(me);
vb@2831
   166
            return PEP_OUT_OF_MEMORY;
vb@2831
   167
        }
vb@3365
   168
vb@3379
   169
        assert(session->sync_state.common.signature_fpr);
vb@3379
   170
        if (session->sync_state.common.signature_fpr) {
vb@3379
   171
            free(partner->fpr);
vb@3379
   172
            partner->fpr = strdup(session->sync_state.common.signature_fpr);
vb@3379
   173
            if (!partner->fpr) {
vb@3379
   174
                free_identity(me);
vb@3379
   175
                free_identity(partner);
vb@3379
   176
                return PEP_OUT_OF_MEMORY;
vb@3379
   177
            }
vb@3364
   178
        }
vb@2831
   179
vb@3047
   180
        status = session->notifyHandshake(me, partner, «$type»);
vb@2831
   181
        if (status)
vb@2831
   182
            return status;
vb@2831
   183
    ||
vb@2831
   184
}
vb@2831
   185
vb@2831
   186
action showSoleHandshake
vb@2831
   187
    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_FORM_GROUP
vb@2831
   188
vb@2831
   189
action showJoinGroupHandshake
vb@2831
   190
    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_ADD_OUR_DEVICE
vb@2831
   191
vb@2831
   192
action showGroupedHandshake
vb@2831
   193
    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE
vb@2831
   194
vb@3522
   195
action hideHandshakeDialog
vb@3522
   196
    call "show_handshake" with "type" > SYNC_NOTIFY_OVERTAKEN
vb@3522
   197
vb@2914
   198
timeout KeySync
vb@2913
   199
    call "show_handshake" with "type" > SYNC_NOTIFY_TIMEOUT
vb@2913
   200
vb@3390
   201
action prepareOwnKeys
vb@3390
   202
||
vb@3390
   203
    stringlist_t *own_keys;
vb@3390
   204
    PEP_STATUS status = _own_keys_retrieve(session, &own_keys, PEP_idf_not_for_sync);
vb@3390
   205
    if (status)
vb@3390
   206
        return status;
vb@3390
   207
vb@3390
   208
    if (session->sync_state.common.own_keys)
vb@3390
   209
        free_stringlist(session->sync_state.common.own_keys);
vb@3390
   210
    session->sync_state.common.own_keys = own_keys;
vb@3394
   211
vb@3394
   212
    identity_list *il;
vb@3394
   213
    status = _own_identities_retrieve(session, &il, PEP_idf_not_for_sync);
vb@3394
   214
    if (status)
vb@3394
   215
        return status;
vb@3394
   216
vb@3394
   217
    IdentityList_from_identity_list(il, &session->sync_state.keysync.ownIdentities);
vb@3390
   218
||
vb@3390
   219
vb@2831
   220
action saveGroupKeys
vb@2831
   221
||
vb@3390
   222
    identity_list *il = IdentityList_to_identity_list(&session->sync_state.keysync.ownIdentities, NULL);
vb@2831
   223
    if (!il)
vb@2831
   224
        return PEP_OUT_OF_MEMORY;
vb@2831
   225
    
vb@2831
   226
    // BUG: this should be a transaction and been rolled back completely on error
vb@2831
   227
    for (identity_list *_il = il; _il && _il->ident; _il = _il->next) {
vb@2831
   228
        PEP_STATUS status = set_identity(session, _il->ident);
vb@2831
   229
        if (status) {
vb@2831
   230
            free_identity_list(il);
vb@2831
   231
            return status;
vb@2831
   232
        }
vb@2831
   233
    }
vb@2831
   234
vb@2831
   235
    free_identity_list(il);
vb@2831
   236
||
vb@2831
   237
vb@3434
   238
action ownKeysAreGroupKeys
vb@3434
   239
||
vb@3434
   240
    PEP_STATUS status = PEP_STATUS_OK;
vb@3434
   241
vb@3434
   242
    // set flag for current keys
vb@3434
   243
    for (identity_list *il = session->sync_state.common.own_identities; il && il->ident ; il = il->next) {
vb@3434
   244
        if (!(il->ident->flags && PEP_idf_not_for_sync)) {
vb@3434
   245
            status = set_identity_flags(session, il->ident, PEP_idf_devicegroup);
vb@3434
   246
            if (status)
vb@3434
   247
                return status;
vb@3434
   248
        }
vb@2831
   249
    }
vb@3434
   250
||
vb@2831
   251
vb@3406
   252
action receivedKeysAreGroupKeys;
vb@3406
   253
vb@3516
   254
action trustThisKey
vb@3516
   255
||
vb@3516
   256
    assert(session->sync_state.common.from && session->sync_state.common.signature_fpr);
vb@3516
   257
    if (!(session->sync_state.common.from && session->sync_state.common.signature_fpr))
vb@3516
   258
        return PEP_ILLEGAL_VALUE;
vb@3516
   259
vb@3516
   260
    pEp_identity *ident = session->sync_state.common.from;
vb@3516
   261
    free(ident->fpr);
vb@3516
   262
    ident->fpr = strdup(session->sync_state.common.signature_fpr);
vb@3516
   263
    assert(ident->fpr);
vb@3516
   264
    if (!ident->fpr)
vb@3516
   265
        return PEP_OUT_OF_MEMORY;
vb@3516
   266
vb@3516
   267
    PEP_STATUS status = trust_own_key(session, ident);
vb@3516
   268
    if (status)
vb@3516
   269
        return status;
vb@3523
   270
vb@3523
   271
    OCTET_STRING_fromBuf(&session->sync_state.keysync.key, ident->fpr, strlen(ident->fpr));
vb@3516
   272
||
vb@3516
   273
vb@3518
   274
action tellWeAreGrouped
vb@3518
   275
||
vb@3518
   276
    session->sync_state.keysync.is_group = true;
vb@3518
   277
||
vb@3518
   278
vb@3518
   279
action tellWeAreNotGrouped
vb@3518
   280
||
vb@3518
   281
    session->sync_state.keysync.is_group = false;
vb@3518
   282
||
vb@3518
   283
vb@2831
   284
action disable;
vb@2831
   285