sync/cond_act_sync.yml2
author Volker Birk <vb@pep-project.org>
Mon, 08 Apr 2019 19:30:36 +0200
branchsync
changeset 3470 3f5f6d631953
parent 3439 3333c94c7827
child 3510 98f79218de04
permissions -rw-r--r--
this is more clever
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@2845
     6
// Copyleft (c) 2017, 2018, 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@2831
    40
condition keyElectionWon
vb@2831
    41
||
vb@2838
    42
    pEp_identity *from = session->sync_state.common.from;
vb@3389
    43
    char *signature_fpr = session->sync_state.common.signature_fpr;
vb@2831
    44
vb@3389
    45
    assert(from && from->address && from->address[0] && from->user_id &&
vb@3389
    46
            from->user_id[0]);
vb@3389
    47
    if (!(from && from->address && from->address[0] && from->user_id &&
vb@3389
    48
            from->user_id[0]))
vb@2831
    49
        return PEP_ILLEGAL_VALUE;
vb@2831
    50
vb@2831
    51
    pEp_identity *me = NULL;
vb@2901
    52
    PEP_STATUS status = get_identity(session, from->address, from->user_id, &me);
vb@2831
    53
    assert(status == PEP_STATUS_OK);
vb@2831
    54
    if (status)
vb@2831
    55
        return status;
vb@2831
    56
vb@2831
    57
    assert(me->fpr && me->fpr[0]);
vb@2831
    58
    if (!(me->fpr && me->fpr[0])) {
vb@2831
    59
        free_identity(me);
vb@2831
    60
        return PEP_ILLEGAL_VALUE;
vb@2831
    61
    }
vb@2831
    62
vb@3389
    63
    size_t len = MIN(strlen(signature_fpr), strlen(me->fpr));
vb@3389
    64
    *result = strncasecmp(signature_fpr, me->fpr, len) > 0;
vb@2831
    65
    free_identity(me);
vb@2831
    66
||
vb@2831
    67
vb@2831
    68
// action: PEP_STATUS «@name»(PEP_SESSION session)
vb@2831
    69
vb@2831
    70
function "new_UUID" {
vb@2831
    71
    param "dst";
vb@2831
    72
    ||
vb@2831
    73
        pEpUUID c;
vb@2831
    74
        uuid_generate_random(c);
vb@2831
    75
vb@2831
    76
        OCTET_STRING_fromBuf(«$dst», (char *) c, 16);
vb@2831
    77
    ||
vb@2831
    78
}
vb@2831
    79
vb@2831
    80
function "copy_UUID" {
vb@2831
    81
    param "src", param "dst";
vb@2831
    82
    ||
vb@2831
    83
        TID_t *src = «$src»;
vb@2831
    84
        TID_t *dst = «$dst»;
vb@2831
    85
vb@2831
    86
        assert(src->size == 16);
vb@2831
    87
        if (!(src->size == 16))
vb@2831
    88
            return PEP_UNKNOWN_ERROR;
vb@2831
    89
vb@2831
    90
        OCTET_STRING_fromBuf(dst, (char *) src->buf, src->size);
vb@2831
    91
    ||
vb@2831
    92
}
vb@2831
    93
vb@2831
    94
action openChallenge
vb@2831
    95
    call "new_UUID" with "dst" > &session->own_sync_state.challenge
vb@2831
    96
vb@2831
    97
action storeChallenge call "copy_UUID" {
vb@2831
    98
    with "src" > &session->sync_state.keysync.challenge
vb@2831
    99
    with "dst" > &session->own_sync_state.challenge
vb@2831
   100
}
vb@2831
   101
vb@2831
   102
action openTransaction
Thomas@3125
   103
    call "new_UUID" with "dst" > &session->sync_state.keysync.transaction
vb@2831
   104
vb@2831
   105
action storeTransaction call "copy_UUID" {
vb@2831
   106
    with "src" > &session->sync_state.keysync.transaction
Thomas@3125
   107
    with "dst" > &session->own_sync_state.transaction
vb@2831
   108
}
vb@2831
   109
vb@2831
   110
function "show_handshake" {
vb@2831
   111
    param "type";
vb@2831
   112
    ||
vb@2831
   113
        assert(session->notifyHandshake);
vb@2831
   114
        if (!session->notifyHandshake)
vb@2831
   115
            return PEP_SYNC_NO_NOTIFY_CALLBACK;
vb@2831
   116
     
vb@2838
   117
        assert(session->sync_state.common.from);
vb@2838
   118
        if (!session->sync_state.common.from)
vb@2831
   119
            return PEP_ILLEGAL_VALUE;
vb@2831
   120
vb@2838
   121
        pEp_identity *from = session->sync_state.common.from;
vb@2831
   122
        pEp_identity *me = NULL;
vb@2901
   123
        PEP_STATUS status = get_identity(session, from->address, from->user_id, &me);
vb@2831
   124
        assert(status == PEP_STATUS_OK);
vb@2831
   125
        if (status)
vb@2831
   126
            return status;
vb@2831
   127
vb@2831
   128
        assert(me->fpr && me->fpr[0]);
vb@2831
   129
        if (!(me->fpr && me->fpr[0])) {
vb@2831
   130
            free_identity(me);
vb@2831
   131
            return PEP_ILLEGAL_VALUE;
vb@2831
   132
        }
vb@2831
   133
vb@2831
   134
        pEp_identity *partner = identity_dup(from);
vb@2831
   135
        if (!partner) {
vb@2831
   136
            free_identity(me);
vb@2831
   137
            return PEP_OUT_OF_MEMORY;
vb@2831
   138
        }
vb@3365
   139
vb@3379
   140
        assert(session->sync_state.common.signature_fpr);
vb@3379
   141
        if (session->sync_state.common.signature_fpr) {
vb@3379
   142
            free(partner->fpr);
vb@3379
   143
            partner->fpr = strdup(session->sync_state.common.signature_fpr);
vb@3379
   144
            if (!partner->fpr) {
vb@3379
   145
                free_identity(me);
vb@3379
   146
                free_identity(partner);
vb@3379
   147
                return PEP_OUT_OF_MEMORY;
vb@3379
   148
            }
vb@3364
   149
        }
vb@2831
   150
vb@3047
   151
        status = session->notifyHandshake(me, partner, «$type»);
vb@2831
   152
        if (status)
vb@2831
   153
            return status;
vb@2831
   154
    ||
vb@2831
   155
}
vb@2831
   156
vb@2831
   157
action showSoleHandshake
vb@2831
   158
    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_FORM_GROUP
vb@2831
   159
vb@2831
   160
action showJoinGroupHandshake
vb@2831
   161
    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_ADD_OUR_DEVICE
vb@2831
   162
vb@2831
   163
action showGroupedHandshake
vb@2831
   164
    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE
vb@2831
   165
vb@2914
   166
timeout KeySync
vb@2913
   167
    call "show_handshake" with "type" > SYNC_NOTIFY_TIMEOUT
vb@2913
   168
vb@3390
   169
action prepareOwnKeys
vb@3390
   170
||
vb@3390
   171
    stringlist_t *own_keys;
vb@3390
   172
    PEP_STATUS status = _own_keys_retrieve(session, &own_keys, PEP_idf_not_for_sync);
vb@3390
   173
    if (status)
vb@3390
   174
        return status;
vb@3390
   175
vb@3390
   176
    if (session->sync_state.common.own_keys)
vb@3390
   177
        free_stringlist(session->sync_state.common.own_keys);
vb@3390
   178
    session->sync_state.common.own_keys = own_keys;
vb@3394
   179
vb@3394
   180
    identity_list *il;
vb@3394
   181
    status = _own_identities_retrieve(session, &il, PEP_idf_not_for_sync);
vb@3394
   182
    if (status)
vb@3394
   183
        return status;
vb@3394
   184
vb@3394
   185
    IdentityList_from_identity_list(il, &session->sync_state.keysync.ownIdentities);
vb@3390
   186
||
vb@3390
   187
vb@2831
   188
action saveGroupKeys
vb@2831
   189
||
vb@3390
   190
    identity_list *il = IdentityList_to_identity_list(&session->sync_state.keysync.ownIdentities, NULL);
vb@2831
   191
    if (!il)
vb@2831
   192
        return PEP_OUT_OF_MEMORY;
vb@2831
   193
    
vb@2831
   194
    // BUG: this should be a transaction and been rolled back completely on error
vb@2831
   195
    for (identity_list *_il = il; _il && _il->ident; _il = _il->next) {
vb@2831
   196
        PEP_STATUS status = set_identity(session, _il->ident);
vb@2831
   197
        if (status) {
vb@2831
   198
            free_identity_list(il);
vb@2831
   199
            return status;
vb@2831
   200
        }
vb@2831
   201
    }
vb@2831
   202
vb@2831
   203
    free_identity_list(il);
vb@2831
   204
||
vb@2831
   205
vb@3434
   206
action ownKeysAreGroupKeys
vb@3434
   207
||
vb@3434
   208
    PEP_STATUS status = PEP_STATUS_OK;
vb@3434
   209
vb@3434
   210
    // set flag for current keys
vb@3434
   211
    for (identity_list *il = session->sync_state.common.own_identities; il && il->ident ; il = il->next) {
vb@3434
   212
        if (!(il->ident->flags && PEP_idf_not_for_sync)) {
vb@3434
   213
            status = set_identity_flags(session, il->ident, PEP_idf_devicegroup);
vb@3434
   214
            if (status)
vb@3434
   215
                return status;
vb@3434
   216
        }
vb@2831
   217
    }
vb@3434
   218
||
vb@2831
   219
vb@3406
   220
action receivedKeysAreGroupKeys;
vb@3406
   221
vb@2831
   222
action disable;
vb@2831
   223