merge sync
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Thu, 02 May 2019 08:18:26 +0200
branchsync
changeset 3597572de760c0bf
parent 3596 37f68fb62406
parent 3595 d491c5d5ef2a
child 3598 a37e451d4fce
merge
     1.1 --- a/src/pEpEngine.c	Thu May 02 08:17:28 2019 +0200
     1.2 +++ b/src/pEpEngine.c	Thu May 02 08:18:26 2019 +0200
     1.3 @@ -1594,14 +1594,6 @@
     1.4              &_session->get_own_address_binding_from_contact, NULL);
     1.5      assert(int_result == SQLITE_OK);
     1.6  
     1.7 -    // int_result = sqlite3_prepare_v2(_session->db, sql_set_device_group,
     1.8 -    //         (int)strlen(sql_set_device_group), &_session->set_device_group, NULL);
     1.9 -    // assert(int_result == SQLITE_OK);
    1.10 -    // 
    1.11 -    // int_result = sqlite3_prepare_v2(_session->db, sql_get_device_group,
    1.12 -    //         (int)strlen(sql_get_device_group), &_session->get_device_group, NULL);
    1.13 -    // assert(int_result == SQLITE_OK);
    1.14 -
    1.15      int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
    1.16              (int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair,
    1.17              NULL);
    1.18 @@ -1885,10 +1877,6 @@
    1.19                  sqlite3_finalize(session->was_id_for_revoke_contacted);   
    1.20              if (session->get_last_contacted)
    1.21                  sqlite3_finalize(session->get_last_contacted);                                       
    1.22 -            // if (session->set_device_group)
    1.23 -            //     sqlite3_finalize(session->set_device_group);
    1.24 -            // if (session->get_device_group)
    1.25 -            //     sqlite3_finalize(session->get_device_group);
    1.26              if (session->set_pgp_keypair)
    1.27                  sqlite3_finalize(session->set_pgp_keypair);
    1.28              if (session->exists_identity_entry)
    1.29 @@ -3421,94 +3409,6 @@
    1.30      return PEP_STATUS_OK;
    1.31  }
    1.32  
    1.33 -// DYNAMIC_API PEP_STATUS set_device_group(
    1.34 -//         PEP_SESSION session,
    1.35 -//         const char *group_name
    1.36 -//     )
    1.37 -// {
    1.38 -//     int result;
    1.39 -// 
    1.40 -//     assert(session);
    1.41 -// 
    1.42 -//     if (!(session && group_name))
    1.43 -//         return PEP_ILLEGAL_VALUE;
    1.44 -// 
    1.45 -//     // 1. Get own user_id
    1.46 -//     char* user_id = NULL;
    1.47 -//     PEP_STATUS status = get_default_own_userid(session, &user_id);
    1.48 -// 
    1.49 -//     // No user_id is returned in this case, no need to free;
    1.50 -//     if (status != PEP_STATUS_OK)
    1.51 -//         return status;
    1.52 -// 
    1.53 -//     // 2. Set device group
    1.54 -//     sqlite3_reset(session->set_device_group);
    1.55 -//     if(group_name){
    1.56 -//         sqlite3_bind_text(session->set_device_group, 1, group_name, -1,
    1.57 -//                 SQLITE_STATIC);
    1.58 -//     } else {
    1.59 -//         sqlite3_bind_null(session->set_device_group, 1);
    1.60 -//     }
    1.61 -// 
    1.62 -//     sqlite3_bind_text(session->set_device_group, 2, user_id, -1,
    1.63 -//             SQLITE_STATIC);
    1.64 -// 
    1.65 -//     result = sqlite3_step(session->set_device_group);
    1.66 -//     sqlite3_reset(session->set_device_group);
    1.67 -// 
    1.68 -//     free(user_id);
    1.69 -// 
    1.70 -//     if (result != SQLITE_DONE)
    1.71 -//         return PEP_CANNOT_SET_PERSON;
    1.72 -// 
    1.73 -//     return PEP_STATUS_OK;
    1.74 -// }
    1.75 -// 
    1.76 -// DYNAMIC_API PEP_STATUS get_device_group(PEP_SESSION session, char **group_name)
    1.77 -// {
    1.78 -//     PEP_STATUS status = PEP_STATUS_OK;
    1.79 -//     int result;
    1.80 -// 
    1.81 -//     assert(session);
    1.82 -//     assert(group_name);
    1.83 -// 
    1.84 -//     if (!(session && group_name))
    1.85 -//         return PEP_ILLEGAL_VALUE;
    1.86 -// 
    1.87 -//     // 1. Get own user_id
    1.88 -//     char* user_id = NULL;
    1.89 -//     status = get_default_own_userid(session, &user_id);
    1.90 -// 
    1.91 -//     // No user_id is returned in this case, no need to free;
    1.92 -//     if (status != PEP_STATUS_OK)
    1.93 -//         return status;
    1.94 -// 
    1.95 -//     // 2. get device group
    1.96 -//     sqlite3_reset(session->get_device_group);
    1.97 -//     sqlite3_bind_text(session->get_device_group, 1, user_id, -1,
    1.98 -//             SQLITE_STATIC);
    1.99 -// 
   1.100 -//     result = sqlite3_step(session->get_device_group);
   1.101 -//     switch (result) {
   1.102 -//     case SQLITE_ROW: {
   1.103 -//         const char *_group_name = (const char *)sqlite3_column_text(session->get_device_group, 0);
   1.104 -//         if(_group_name){
   1.105 -//             *group_name = strdup(_group_name);
   1.106 -//                 if(*group_name == NULL)
   1.107 -//                     status = PEP_OUT_OF_MEMORY;
   1.108 -//         }
   1.109 -//         break;
   1.110 -//     }
   1.111 -// 
   1.112 -//     default:
   1.113 -//         status = PEP_RECORD_NOT_FOUND;
   1.114 -//     }
   1.115 -// 
   1.116 -//     free(user_id);
   1.117 -//     sqlite3_reset(session->get_device_group);
   1.118 -//     return status;
   1.119 -// }
   1.120 -
   1.121  DYNAMIC_API PEP_STATUS set_identity_flags(
   1.122          PEP_SESSION session,
   1.123          pEp_identity *identity,
     2.1 --- a/src/pEpEngine.h	Thu May 02 08:17:28 2019 +0200
     2.2 +++ b/src/pEpEngine.h	Thu May 02 08:18:26 2019 +0200
     2.3 @@ -865,40 +865,6 @@
     2.4          const char* default_id,
     2.5          const char* alias_id);
     2.6  
     2.7 -
     2.8 -// // set_device_group() - update own person's device group
     2.9 -// //
    2.10 -// //    parameters:
    2.11 -// //        session (in)        session handle
    2.12 -// //        group_name (in)     new group name
    2.13 -// //
    2.14 -// //    return value:
    2.15 -// //        PEP_STATUS_OK = 0             device group was updated
    2.16 -// //        PEP_CANNOT_SET_PERSON         update failed
    2.17 -// 
    2.18 -// DYNAMIC_API PEP_STATUS set_device_group(
    2.19 -//         PEP_SESSION session,
    2.20 -//         const char *group_name
    2.21 -//     );
    2.22 -// 
    2.23 -// // get_device_group() - get own person's device group
    2.24 -// //
    2.25 -// //    parameters:
    2.26 -// //        session (in)        session handle
    2.27 -// //        group_name (in)     new group name
    2.28 -// //
    2.29 -// //    return value:
    2.30 -// //        PEP_STATUS_OK = 0             couldn't get device group
    2.31 -// //        PEP_RECORD_NOT_FOUND          update failed
    2.32 -// //
    2.33 -// //    caveat:
    2.34 -// //        the ownerships of group_name is going to the caller
    2.35 -// 
    2.36 -// DYNAMIC_API PEP_STATUS get_device_group(
    2.37 -//         PEP_SESSION session, 
    2.38 -//         char **group_name
    2.39 -//     );
    2.40 -
    2.41  // set_identity_flags() - update identity flags on existing identity
    2.42  //
    2.43  //    parameters:
    2.44 @@ -1519,8 +1485,6 @@
    2.45                       char **sign, 
    2.46                       size_t *sign_size);
    2.47  
    2.48 -const char *get_device_name(PEP_SESSION session);
    2.49 -
    2.50  #ifdef __cplusplus
    2.51  }
    2.52  #endif
     3.1 --- a/sync/cond_act_sync.yml2	Thu May 02 08:17:28 2019 +0200
     3.2 +++ b/sync/cond_act_sync.yml2	Thu May 02 08:18:26 2019 +0200
     3.3 @@ -50,9 +50,24 @@
     3.4      TID_t *t1 = &session->sync_state.keysync.negotiation;
     3.5      TID_t *t2 = &session->own_sync_state.negotiation;
     3.6  
     3.7 +    // test if TID is identical
     3.8      *result = t1->size == t2->size && memcmp(t1->buf, t2->buf, t1->size) == 0;
     3.9  ||
    3.10  
    3.11 +condition sameTransactionAndPartner
    3.12 +||
    3.13 +    TID_t *t1 = &session->sync_state.keysync.negotiation;
    3.14 +    TID_t *t2 = &session->own_sync_state.negotiation;
    3.15 +
    3.16 +    const char *s1 = session->sync_state.common.signature_fpr;
    3.17 +    const char *s2 = session->own_sync_state.signature_fpr;
    3.18 +
    3.19 +    // test if TID is identical
    3.20 +    *result = t1->size == t2->size && memcmp(t1->buf, t2->buf, t1->size) == 0
    3.21 +    // and test if we're talking to the same sender
    3.22 +            && s1 && s2 && strcmp(s1, s2) == 0;
    3.23 +||
    3.24 +
    3.25  condition keyElectionWon
    3.26  ||
    3.27      pEp_identity *from = session->sync_state.common.from;
    3.28 @@ -115,25 +130,49 @@
    3.29      with "dst" > &session->own_sync_state.challenge
    3.30  }
    3.31  
    3.32 -action openTransaction {
    3.33 +action newTransaction {
    3.34  ||
    3.35 -    for (int i=0; i<session->sync_state.keysync.negotiation.size; ++i) {
    3.36 -        if (session->sync_state.keysync.negotiation.buf[i])
    3.37 -            return PEP_STATUS_OK;
    3.38 -    }
    3.39 +    // sender key must be stable while transaction
    3.40 +    assert(session->sync_state.common.signature_fpr);
    3.41 +    free(session->own_sync_state.signature_fpr);
    3.42 +    session->own_sync_state.signature_fpr
    3.43 +            = strdup(session->sync_state.common.signature_fpr);
    3.44 +    assert(session->own_sync_state.signature_fpr);
    3.45 +    if (!session->own_sync_state.signature_fpr)
    3.46 +        return PEP_OUT_OF_MEMORY;
    3.47 +
    3.48  ||
    3.49      call "new_UUID" with "dst" > &session->sync_state.keysync.negotiation
    3.50 +    call "copy_UUID" {
    3.51 +        with "src" > &session->sync_state.keysync.negotiation
    3.52 +        with "dst" > &session->own_sync_state.negotiation
    3.53 +    }
    3.54  }
    3.55  
    3.56  action closeTransaction
    3.57  ||
    3.58      memset(session->sync_state.keysync.negotiation.buf, 0,
    3.59              session->sync_state.keysync.negotiation.size);
    3.60 +    memset(session->own_sync_state.negotiation.buf, 0,
    3.61 +            session->own_sync_state.negotiation.size);
    3.62  ||
    3.63  
    3.64 -action storeTransaction call "copy_UUID" {
    3.65 -    with "src" > &session->sync_state.keysync.negotiation
    3.66 -    with "dst" > &session->own_sync_state.negotiation
    3.67 +action storeTransaction {
    3.68 +||
    3.69 +    // sender key must be stable while transaction
    3.70 +    assert(session->sync_state.common.signature_fpr);
    3.71 +    free(session->own_sync_state.signature_fpr);
    3.72 +    session->own_sync_state.signature_fpr
    3.73 +            = strdup(session->sync_state.common.signature_fpr);
    3.74 +    assert(session->own_sync_state.signature_fpr);
    3.75 +    if (!session->own_sync_state.signature_fpr)
    3.76 +        return PEP_OUT_OF_MEMORY;
    3.77 +
    3.78 +||
    3.79 +    call "copy_UUID" {
    3.80 +        with "src" > &session->sync_state.keysync.negotiation
    3.81 +        with "dst" > &session->own_sync_state.negotiation
    3.82 +    }
    3.83  }
    3.84  
    3.85  function "show_handshake" {
    3.86 @@ -237,9 +276,9 @@
    3.87      if (status)
    3.88          return status;
    3.89  
    3.90 -    if (session->sync_state.common.own_keys)
    3.91 -        free_stringlist(session->sync_state.common.own_keys);
    3.92 -    session->sync_state.common.own_keys = own_keys;
    3.93 +    if (session->own_sync_state.own_keys)
    3.94 +        free_stringlist(session->own_sync_state.own_keys);
    3.95 +    session->own_sync_state.own_keys = own_keys;
    3.96  
    3.97      identity_list *il;
    3.98      status = _own_identities_retrieve(session, &il, PEP_idf_not_for_sync);
    3.99 @@ -272,7 +311,7 @@
   3.100      PEP_STATUS status = PEP_STATUS_OK;
   3.101  
   3.102      // set flag for current keys
   3.103 -    for (identity_list *il = session->sync_state.common.own_identities; il && il->ident ; il = il->next) {
   3.104 +    for (identity_list *il = session->own_sync_state.own_identities; il && il->ident ; il = il->next) {
   3.105          if (!(il->ident->flags && PEP_idf_not_for_sync)) {
   3.106              status = set_identity_flags(session, il->ident, PEP_idf_devicegroup);
   3.107              if (status)
   3.108 @@ -286,7 +325,7 @@
   3.109      PEP_STATUS status = PEP_STATUS_OK;
   3.110  
   3.111      // set flag for current keys
   3.112 -    for (identity_list *il = session->sync_state.common.own_identities; il && il->ident ; il = il->next) {
   3.113 +    for (identity_list *il = session->own_sync_state.own_identities; il && il->ident ; il = il->next) {
   3.114          if (!(il->ident->flags && PEP_idf_not_for_sync)) {
   3.115              status = set_identity_flags(session, il->ident, PEP_idf_devicegroup);
   3.116              if (status)
   3.117 @@ -298,7 +337,7 @@
   3.118      if (!il)
   3.119          return PEP_OUT_OF_MEMORY;
   3.120  
   3.121 -    for (il = session->sync_state.common.own_identities; il && il->ident ; il = il->next) {
   3.122 +    for (il = session->own_sync_state.own_identities; il && il->ident ; il = il->next) {
   3.123          // replace partner's user_id with own user_id
   3.124          free(il->ident->user_id);
   3.125          il->ident->user_id = strdup(session->sync_state.common.from->user_id);
     4.1 --- a/sync/gen_message_func.ysl2	Thu May 02 08:17:28 2019 +0200
     4.2 +++ b/sync/gen_message_func.ysl2	Thu May 02 08:18:26 2019 +0200
     4.3 @@ -39,18 +39,27 @@
     4.4  // state
     4.5  
     4.6  struct «@name»_state_s {
     4.7 +    // common buffer for all types of «@name» messages
     4.8 +
     4.9      struct common_state_s {
    4.10 +        // transport data
    4.11          pEp_identity *from;
    4.12          char *signature_fpr;
    4.13 -        stringlist_t *own_keys;
    4.14 -        identity_list *own_identities;
    4.15      } common;
    4.16 -
    4.17      `` apply "fsm", mode=state
    4.18  };
    4.19  
    4.20 +// own state
    4.21 +
    4.22  struct own_«@name»_state_s {
    4.23 +    stringlist_t *own_keys;
    4.24 +    identity_list *own_identities;
    4.25 +
    4.26 +    `` if "func:distinctName(fsm/message/field[@type='TID'])" |> // active TIDs
    4.27      `` for "func:distinctName(fsm/message/field[@type='TID'])" |> «func:ctype()» «@name»;
    4.28 +
    4.29 +    // transport data
    4.30 +    char *signature_fpr;
    4.31  };
    4.32  
    4.33  void free_«@name»_state(PEP_SESSION session);
    4.34 @@ -73,6 +82,9 @@
    4.35  
    4.36  template "fsm", mode=state
    4.37  ||
    4.38 +
    4.39 +// buffer for «@name» messages
    4.40 +
    4.41  struct _«@name»_state_s {
    4.42      int state;
    4.43  
    4.44 @@ -100,8 +112,8 @@
    4.45  
    4.46      free_identity(session->«yml:lcase(@name)»_state.common.from);
    4.47      free(session->«yml:lcase(@name)»_state.common.signature_fpr);
    4.48 -    free_stringlist(session->«yml:lcase(@name)»_state.common.own_keys);
    4.49 -    free_identity_list(session->«yml:lcase(@name)»_state.common.own_identities);
    4.50 +    free_stringlist(session->own_«yml:lcase(@name)»_state.own_keys);
    4.51 +    free_identity_list(session->own_«yml:lcase(@name)»_state.own_identities);
    4.52  
    4.53  ||
    4.54  for "fsm"
     5.1 --- a/sync/gen_statemachine.ysl2	Thu May 02 08:17:28 2019 +0200
     5.2 +++ b/sync/gen_statemachine.ysl2	Thu May 02 08:18:26 2019 +0200
     5.3 @@ -40,7 +40,6 @@
     5.4              pEp_identity *from;
     5.5              char *signature_fpr;
     5.6  
     5.7 -            // identities to sync
     5.8              identity_list *own_identities;
     5.9          } «@name»_event_t;
    5.10  
    5.11 @@ -349,6 +348,7 @@
    5.12  
    5.13                  «@name»_PR fsm = msg->present;
    5.14                  int event = 0;
    5.15 +                bool is_own_key = false;
    5.16  
    5.17                  switch (fsm) {
    5.18                      `` apply "fsm", 2, mode=signal_message
    5.19 @@ -417,6 +417,8 @@
    5.20                  identity_list *channels = NULL;
    5.21                  char *key_data = NULL;
    5.22                  size_t key_data_size = 0;
    5.23 +                stringlist_t *extra = NULL;
    5.24 +                bool transaction;
    5.25  
    5.26                  status = update_«@name»_message(session, msg);
    5.27                  if (status)
    5.28 @@ -497,8 +499,6 @@
    5.29                      }
    5.30                      memcpy(_data, data, size);
    5.31  
    5.32 -                    stringlist_t *extra = NULL;
    5.33 -
    5.34                      switch (message_type) {
    5.35                      `` for "fsm/message[@security='unencrypted']" |>>> case «../@name»_PR_«yml:mixedCase(@name)»:
    5.36                              status = base_prepare_message(
    5.37 @@ -518,22 +518,76 @@
    5.38                              m = _m;
    5.39                              break;
    5.40  
    5.41 -                    `` for "fsm/message[@security='attach_own_keys']" |>>> case «../@name»_PR_«yml:mixedCase(@name)»:
    5.42 -                            assert(session->«yml:lcase(@name)»_state.common.signature_fpr &&
    5.43 -                                session->«yml:lcase(@name)»_state.common.from &&
    5.44 -                                session->«yml:lcase(@name)»_state.common.from->user_id);
    5.45 -                            if (!(session->«yml:lcase(@name)»_state.common.signature_fpr &&
    5.46 -                                    session->«yml:lcase(@name)»_state.common.from &&
    5.47 -                                    session->«yml:lcase(@name)»_state.common.from->user_id))
    5.48 -                            {
    5.49 +                    `` for "fsm/message[@security='untrusted']" |>>> case «../@name»_PR_«yml:mixedCase(@name)»:
    5.50 +                            // add fpr of key of comm partner
    5.51 +
    5.52 +                            assert(session->«yml:lcase(@name)»_state.common.signature_fpr);
    5.53 +                            if (!session->«yml:lcase(@name)»_state.common.signature_fpr) {
    5.54                                  status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
    5.55                                  goto the_end;
    5.56                              }
    5.57 +
    5.58 +                            extra = new_stringlist(session->«yml:lcase(@name)»_state.common.signature_fpr);
    5.59 +                            if (!extra) {
    5.60 +                                status = PEP_OUT_OF_MEMORY;
    5.61 +                                goto the_end;
    5.62 +                            }
    5.63 +
    5.64 +                            status = base_prepare_message(
    5.65 +                                    session,
    5.66 +                                    li->ident,
    5.67 +                                    li->ident,
    5.68 +                                    _data,
    5.69 +                                    size,
    5.70 +                                    NULL,
    5.71 +                                    &_m
    5.72 +                                );
    5.73 +                            if (status) {
    5.74 +                                free(_data);
    5.75 +                                goto the_end;
    5.76 +                            }
    5.77 +
    5.78 +                            status = encrypt_message(session, _m, extra, &m, PEP_enc_PEP, 0);
    5.79 +                            if (status) {
    5.80 +                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
    5.81 +                                goto the_end;
    5.82 +                            }
    5.83 +                            free_message(_m);
    5.84 +                            break;
    5.85 +
    5.86 +                    `` for "fsm/message[@security='attach_own_keys']" |>>> case «../@name»_PR_«yml:mixedCase(@name)»:
    5.87 +                            // check if this is the key of a former negotiation
    5.88 +
    5.89 +                            transaction = false;
    5.90 +                            for (int i=0; i < session->own_«yml:lcase(@name)»_state.negotiation.size; i++) {
    5.91 +                                if (session->own_«yml:lcase(@name)»_state.negotiation.buf[i]) {
    5.92 +                                    transaction = true;
    5.93 +                                    break;
    5.94 +                                }
    5.95 +                            }
    5.96 +    
    5.97 +                            // if it is a former negotiation check if the key
    5.98 +                            // is fully trusted and the sender key of this
    5.99 +                            // transaction; if so add the sender key to extra
   5.100 +                            // keys allowing this new partner to read the
   5.101 +                            // secret keys
   5.102 +
   5.103 +                            if (transaction) {
   5.104 +                                assert(session->own_«yml:lcase(@name)»_state.signature_fpr &&
   5.105 +                                    session->«yml:lcase(@name)»_state.common.from &&
   5.106 +                                    session->«yml:lcase(@name)»_state.common.from->user_id);
   5.107 +                                if (!(session->own_«yml:lcase(@name)»_state.signature_fpr &&
   5.108 +                                        session->«yml:lcase(@name)»_state.common.from &&
   5.109 +                                        session->«yml:lcase(@name)»_state.common.from->user_id))
   5.110 +                                {
   5.111 +                                    status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
   5.112 +                                    goto the_end;
   5.113 +                                }
   5.114                              
   5.115 -                            // double check if we fully trust this comm partner
   5.116 -                            {
   5.117 +                                // test if this is a green channel
   5.118 +
   5.119                                  pEp_identity *ident = new_identity(NULL,
   5.120 -                                        session->«yml:lcase(@name)»_state.common.signature_fpr,
   5.121 +                                        session->own_«yml:lcase(@name)»_state.signature_fpr,
   5.122                                          session->«yml:lcase(@name)»_state.common.from->user_id,
   5.123                                          NULL
   5.124                                      );
   5.125 @@ -553,8 +607,31 @@
   5.126                                      goto the_end;
   5.127                                  }
   5.128                                  free_identity(ident);
   5.129 +
   5.130 +                                // test if we accepted this as own key already
   5.131 +
   5.132 +                                bool is_own_key = false;
   5.133 +                                status = own_key_is_listed(session,
   5.134 +                                        session->own_«yml:lcase(@name)»_state.signature_fpr,
   5.135 +                                        &is_own_key);
   5.136 +                                assert(!status);
   5.137 +                                if (status)
   5.138 +                                    goto the_end;
   5.139 +                                assert(is_own_key);
   5.140 +                                if (!is_own_key) {
   5.141 +                                    status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
   5.142 +                                    goto the_end;
   5.143 +                                }
   5.144 +
   5.145 +                                // if so add key of comm partner to extra keys
   5.146 +
   5.147 +                                extra = new_stringlist(session->own_«yml:lcase(@name)»_state.signature_fpr);
   5.148 +                                if (!extra) {
   5.149 +                                    status = PEP_OUT_OF_MEMORY;
   5.150 +                                    goto the_end;
   5.151 +                                }
   5.152                              }
   5.153 -
   5.154 +                            
   5.155                              status = base_prepare_message(
   5.156                                      session,
   5.157                                      li->ident,
   5.158 @@ -581,7 +658,7 @@
   5.159                              }
   5.160                              key_data_size = 1;
   5.161  
   5.162 -                            for (stringlist_t *sl = session->«yml:lcase(@name)»_state.common.own_keys;
   5.163 +                            for (stringlist_t *sl = session->own_«yml:lcase(@name)»_state.own_keys;
   5.164                                      sl && sl->value ; sl = sl->next)
   5.165                              {
   5.166                                  char *_key_data = NULL;
   5.167 @@ -644,16 +721,7 @@
   5.168                              }
   5.169                              key_data = NULL;
   5.170  
   5.171 -                            // add fpr of key of comm partner
   5.172 -
   5.173 -                            extra = new_stringlist(session->«yml:lcase(@name)»_state.common.signature_fpr);
   5.174 -                            if (extra) {
   5.175 -                                status = encrypt_message(session, _m, extra, &m, PEP_enc_PEP, 0);
   5.176 -                                free_stringlist(extra);
   5.177 -                            }
   5.178 -                            else {
   5.179 -                                status = PEP_OUT_OF_MEMORY;
   5.180 -                            }
   5.181 +                            status = encrypt_message(session, _m, extra, &m, PEP_enc_PEP, 0);
   5.182                              if (status) {
   5.183                                  status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
   5.184                                  goto the_end;
   5.185 @@ -661,7 +729,7 @@
   5.186                              free_message(_m);
   5.187                              break;
   5.188  
   5.189 -                        default:
   5.190 +                        default: // security=trusted only
   5.191                              status = base_prepare_message(
   5.192                                      session,
   5.193                                      li->ident,
   5.194 @@ -676,20 +744,7 @@
   5.195                                  goto the_end;
   5.196                              }
   5.197  
   5.198 -                            assert(session->«yml:lcase(@name)»_state.common.signature_fpr);
   5.199 -                            if (!session->«yml:lcase(@name)»_state.common.signature_fpr) {
   5.200 -                                status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
   5.201 -                                goto the_end;
   5.202 -                            }
   5.203 -
   5.204 -                            stringlist_t *extra = new_stringlist(session->«yml:lcase(@name)»_state.common.signature_fpr);
   5.205 -                            if (extra) {
   5.206 -                                status = encrypt_message(session, _m, extra, &m, PEP_enc_PEP, 0);
   5.207 -                                free_stringlist(extra);
   5.208 -                            }
   5.209 -                            else {
   5.210 -                                status = PEP_OUT_OF_MEMORY;
   5.211 -                            }
   5.212 +                            status = encrypt_message(session, _m, NULL, &m, PEP_enc_PEP, 0);
   5.213                              if (status) {
   5.214                                  status = PEP_«yml:ucase(@name)»_CANNOT_ENCRYPT;
   5.215                                  goto the_end;
   5.216 @@ -702,6 +757,7 @@
   5.217                  }
   5.218  
   5.219              the_end:
   5.220 +                free_stringlist(extra);
   5.221                  free_identity_list(channels);
   5.222                  free_message(m);
   5.223                  free(data);
   5.224 @@ -761,8 +817,8 @@
   5.225                  // update own identities
   5.226  
   5.227                  if (ev->own_identities && ev->own_identities->ident) {
   5.228 -                    free_identity_list(session->«yml:lcase(@name)»_state.common.own_identities);
   5.229 -                    session->«yml:lcase(@name)»_state.common.own_identities = ev->own_identities;
   5.230 +                    free_identity_list(session->own_«yml:lcase(@name)»_state.own_identities);
   5.231 +                    session->own_«yml:lcase(@name)»_state.own_identities = ev->own_identities;
   5.232                      ev->own_identities = NULL;
   5.233                  }
   5.234  
   5.235 @@ -870,6 +926,13 @@
   5.236                          status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
   5.237                          goto the_end;
   5.238                      }
   5.239 +                    status = own_key_is_listed(session, signature_fpr, &is_own_key);
   5.240 +                    if (status)
   5.241 +                        goto the_end;
   5.242 +                    if (!is_own_key) {
   5.243 +                        status = PEP_«yml:ucase(ancestor::protocol/@name)»_ILLEGAL_MESSAGE;
   5.244 +                        goto the_end;
   5.245 +                    }
   5.246                      event = «@name»;
   5.247                      break;
   5.248  
   5.249 @@ -1126,7 +1189,7 @@
   5.250                  `` apply "event", 2, mode=fsm
   5.251                  default:
   5.252                      // ignore events not handled here
   5.253 -                    «../@name»_SERVICE_LOG("ignoring event", KeySync_event_name(event));
   5.254 +                    «../@name»_SERVICE_LOG("ignoring event", «../@name»_event_name(event));
   5.255                      return invalid_event;
   5.256              }
   5.257              break;
     6.1 --- a/sync/sync.fsm	Thu May 02 08:17:28 2019 +0200
     6.2 +++ b/sync/sync.fsm	Thu May 02 08:18:26 2019 +0200
     6.3 @@ -37,7 +37,7 @@
     6.4  
     6.5              on Beacon {
     6.6                  if sameChallenge {
     6.7 -                    // this is our own beacon; ignore
     6.8 +                    // this is our own Beacon; ignore
     6.9                  }
    6.10                  else {
    6.11                      if weAreFirst {
    6.12 @@ -45,8 +45,7 @@
    6.13                      }
    6.14                      else /* we are second */ {
    6.15                          do storeChallenge; // partner's challenge
    6.16 -                        do openTransaction; // NOP if negotiation already open
    6.17 -                        do storeTransaction;
    6.18 +                        do newTransaction;
    6.19                          do tellWeAreNotGrouped;
    6.20                          // second is sending NegotiationRequest
    6.21                          send NegotiationRequest;
    6.22 @@ -56,7 +55,7 @@
    6.23  
    6.24              on NegotiationRequest if challengeAccepted {
    6.25                  if sameTransaction {
    6.26 -                    // this is our own handshake request; ignore
    6.27 +                    // this is our own NegotiationRequest; ignore
    6.28                  }
    6.29                  else {
    6.30                      // first is receiving NegotiationRequest
    6.31 @@ -69,7 +68,7 @@
    6.32                  }
    6.33              }
    6.34  
    6.35 -            on NegotiationOpen if sameTransaction {
    6.36 +            on NegotiationOpen if sameTransactionAndPartner {
    6.37                  // second is receiving NegotiationOpen
    6.38                  go HandshakingNewSecond;
    6.39              }
    6.40 @@ -86,7 +85,7 @@
    6.41                  go Sole;
    6.42              }
    6.43  
    6.44 -            on Rollback if sameTransaction
    6.45 +            on Rollback if sameTransactionAndPartner
    6.46                  go Sole;
    6.47  
    6.48              // Reject is CommitReject
    6.49 @@ -96,7 +95,7 @@
    6.50                  go End;
    6.51              }
    6.52  
    6.53 -            on CommitReject if sameTransaction {
    6.54 +            on CommitReject if sameTransactionAndPartner {
    6.55                  do disable;
    6.56                  go End;
    6.57              }
    6.58 @@ -109,7 +108,7 @@
    6.59              }
    6.60  
    6.61              // got a CommitAccept from second
    6.62 -            on CommitAcceptSecond if sameTransaction
    6.63 +            on CommitAcceptSecond if sameTransactionAndPartner
    6.64                  go HandshakingNewPhase2First;
    6.65          }
    6.66  
    6.67 @@ -124,7 +123,7 @@
    6.68                  go Sole;
    6.69              }
    6.70  
    6.71 -            on Rollback if sameTransaction
    6.72 +            on Rollback if sameTransactionAndPartner
    6.73                  go Sole;
    6.74  
    6.75              // Reject is CommitReject
    6.76 @@ -134,7 +133,7 @@
    6.77                  go End;
    6.78              }
    6.79  
    6.80 -            on CommitReject if sameTransaction {
    6.81 +            on CommitReject if sameTransactionAndPartner {
    6.82                  do disable;
    6.83                  go End;
    6.84              }
    6.85 @@ -147,40 +146,40 @@
    6.86              }
    6.87  
    6.88              // got a CommitAccept from first
    6.89 -            on CommitAcceptFirst if sameTransaction
    6.90 +            on CommitAcceptFirst if sameTransactionAndPartner
    6.91                  go HandshakingNewPhase2Second;
    6.92          }
    6.93  
    6.94          state HandshakingNewPhase1First {
    6.95 -            on Rollback if sameTransaction {
    6.96 +            on Rollback if sameTransactionAndPartner {
    6.97                  do untrustThisKey;
    6.98                  go Sole;
    6.99              }
   6.100              
   6.101 -            on CommitReject if sameTransaction {
   6.102 +            on CommitReject if sameTransactionAndPartner {
   6.103                  do untrustThisKey;
   6.104                  do disable;
   6.105                  go End;
   6.106              }
   6.107  
   6.108 -            on CommitAcceptSecond if sameTransaction {
   6.109 +            on CommitAcceptSecond if sameTransactionAndPartner {
   6.110                  go NewGroupFirst;
   6.111              }
   6.112          }
   6.113  
   6.114          state HandshakingNewPhase1Second {
   6.115 -            on Rollback if sameTransaction {
   6.116 +            on Rollback if sameTransactionAndPartner {
   6.117                  do untrustThisKey;
   6.118                  go Sole;
   6.119              }
   6.120              
   6.121 -            on CommitReject if sameTransaction {
   6.122 +            on CommitReject if sameTransactionAndPartner {
   6.123                  do untrustThisKey;
   6.124                  do disable;
   6.125                  go End;
   6.126              }
   6.127  
   6.128 -            on CommitAcceptFirst if sameTransaction {
   6.129 +            on CommitAcceptFirst if sameTransactionAndPartner {
   6.130                  go NewGroupSecond;
   6.131              }
   6.132          }
   6.133 @@ -225,7 +224,6 @@
   6.134  
   6.135          state NewGroupFirst {
   6.136              on Init {
   6.137 -                do closeTransaction;
   6.138                  do prepareOwnKeys;
   6.139                  send OwnKeysFirst; // we're not grouped yet, this is our own keys
   6.140              }
   6.141 @@ -244,7 +242,6 @@
   6.142  
   6.143          state NewGroupSecond {
   6.144              on Init {
   6.145 -                do closeTransaction;
   6.146                  do prepareOwnKeys;
   6.147                  send OwnKeysSecond; // we're not grouped yet, this is our own keys
   6.148              }
   6.149 @@ -277,13 +274,12 @@
   6.150  
   6.151              on Beacon {
   6.152                  do storeChallenge;
   6.153 -                do openTransaction;
   6.154 -                do storeTransaction;
   6.155 +                do newTransaction;
   6.156                  do tellWeAreGrouped;
   6.157                  send NegotiationRequest;
   6.158              }
   6.159  
   6.160 -            on NegotiationOpen if sameTransaction
   6.161 +            on NegotiationOpen if sameTransactionAndPartner
   6.162                  go HandshakingGrouped;
   6.163  
   6.164              on GroupTrustThisKey {
   6.165 @@ -302,7 +298,7 @@
   6.166                  go Sole;
   6.167              }
   6.168  
   6.169 -            on Rollback if sameTransaction
   6.170 +            on Rollback if sameTransactionAndPartner
   6.171                  go Sole;
   6.172  
   6.173              // Reject is CommitReject
   6.174 @@ -312,7 +308,7 @@
   6.175                  go End;
   6.176              }
   6.177  
   6.178 -            on CommitReject if sameTransaction {
   6.179 +            on CommitReject if sameTransactionAndPartner {
   6.180                  do disable;
   6.181                  go End;
   6.182              }
   6.183 @@ -324,20 +320,20 @@
   6.184                  go HandshakingJoinPhase1;
   6.185              }
   6.186  
   6.187 -            on CommitAcceptForGroup if sameTransaction
   6.188 +            on CommitAcceptForGroup if sameTransactionAndPartner
   6.189                  go HandshakingJoinPhase2;
   6.190          }
   6.191  
   6.192          state HandshakingJoinPhase1 {
   6.193 -            on Rollback if sameTransaction
   6.194 +            on Rollback if sameTransactionAndPartner
   6.195                  go Sole;
   6.196              
   6.197 -            on CommitReject if sameTransaction {
   6.198 +            on CommitReject if sameTransactionAndPartner {
   6.199                  do disable;
   6.200                  go End;
   6.201              }
   6.202  
   6.203 -            on CommitAcceptForGroup if sameTransaction {
   6.204 +            on CommitAcceptForGroup if sameTransactionAndPartner {
   6.205                  go JoinGroup;
   6.206              }
   6.207          }
   6.208 @@ -362,7 +358,6 @@
   6.209  
   6.210          state JoinGroup {
   6.211              on Init {
   6.212 -                do closeTransaction;
   6.213                  do prepareOwnKeys;
   6.214                  send OwnKeys;
   6.215              }
   6.216 @@ -385,7 +380,7 @@
   6.217                  go Grouped;
   6.218              }
   6.219  
   6.220 -            on Rollback if sameTransaction
   6.221 +            on Rollback if sameTransactionAndPartner
   6.222                  go Grouped;
   6.223  
   6.224              // Reject is CommitReject
   6.225 @@ -394,7 +389,7 @@
   6.226                  go Grouped;
   6.227              }
   6.228  
   6.229 -            on CommitReject if sameTransaction
   6.230 +            on CommitReject if sameTransactionAndPartner
   6.231                  go Grouped;
   6.232  
   6.233              // Accept is Phase1Commit
   6.234 @@ -405,7 +400,7 @@
   6.235                  go HandshakingGroupedPhase1;
   6.236              }
   6.237  
   6.238 -            on CommitAccept if sameTransaction
   6.239 +            on CommitAccept if sameTransactionAndPartner
   6.240                  go HandshakingGroupedPhase2;
   6.241  
   6.242              on GroupTrustThisKey {
   6.243 @@ -415,7 +410,7 @@
   6.244  
   6.245              on CommitAcceptForGroup {
   6.246                  do showDeviceAdded;
   6.247 -                if sameTransaction {
   6.248 +                if sameTransactionAndPartner {
   6.249                      do hideHandshakeDialog;
   6.250                      go Grouped;
   6.251                  }
   6.252 @@ -426,13 +421,13 @@
   6.253          }
   6.254  
   6.255          state HandshakingGroupedPhase1 {
   6.256 -            on Rollback if sameTransaction
   6.257 +            on Rollback if sameTransactionAndPartner
   6.258                  go Grouped;
   6.259  
   6.260 -            on CommitReject if sameTransaction
   6.261 +            on CommitReject if sameTransactionAndPartner
   6.262                  go Grouped;
   6.263  
   6.264 -            on CommitAccept if sameTransaction {
   6.265 +            on CommitAccept if sameTransactionAndPartner {
   6.266                  send GroupKeys;
   6.267                  go Grouped;
   6.268              }
   6.269 @@ -443,7 +438,7 @@
   6.270  
   6.271              on CommitAcceptForGroup {
   6.272                  do showDeviceAdded;
   6.273 -                if sameTransaction
   6.274 +                if sameTransactionAndPartner
   6.275                      go Grouped;
   6.276              }
   6.277  
   6.278 @@ -475,7 +470,7 @@
   6.279  
   6.280              on CommitAcceptForGroup {
   6.281                  do showDeviceAdded;
   6.282 -                if sameTransaction {
   6.283 +                if sameTransactionAndPartner {
   6.284                      do hideHandshakeDialog;
   6.285                      go Grouped;
   6.286                  }
   6.287 @@ -537,7 +532,7 @@
   6.288              field Hash key;
   6.289          }
   6.290  
   6.291 -        // security=attach_own_keys implies security=trusted
   6.292 +        // trust in future
   6.293          message GroupKeys 12, security=attach_own_keys {
   6.294              field IdentityList ownIdentities;
   6.295          }