ENGINE-693: integrated with key reset at least. Needs explicit testing, but breaks nothing right now. sync
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Wed, 05 Feb 2020 19:55:56 +0100
branchsync
changeset 4410a1121e886baa
parent 4409 727cc39f2ded
child 4411 67187599f606
ENGINE-693: integrated with key reset at least. Needs explicit testing, but breaks nothing right now.
src/key_reset.c
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
     1.1 --- a/src/key_reset.c	Wed Feb 05 15:15:47 2020 +0100
     1.2 +++ b/src/key_reset.c	Wed Feb 05 19:55:56 2020 +0100
     1.3 @@ -572,6 +572,11 @@
     1.4              // Make new key the default    
     1.5              curr_ident->fpr = new_fpr;
     1.6      
     1.7 +            // Whether new_key is NULL or not, if this key is equal to the current user default, we 
     1.8 +            // replace it.
     1.9 +            status = replace_main_user_fpr_if_equal(session, curr_ident->user_id, 
    1.10 +                                                    new_fpr, old_fpr);                    
    1.11 +
    1.12              // This only sets as the default, does NOT TRUST IN ANY WAY
    1.13              PEP_comm_type new_key_rating = PEP_ct_unknown;
    1.14              
    1.15 @@ -601,6 +606,7 @@
    1.16              // then so be it - but we need to double-check to 
    1.17              // ensure that in this case, we end up with a private one,
    1.18              // so talk to vb about this.
    1.19 +            
    1.20              // Make new key the default    
    1.21              
    1.22              // This is REQUIRED for set_own_key (see doc)
    1.23 @@ -610,15 +616,27 @@
    1.24              
    1.25              if (status != PEP_STATUS_OK)
    1.26                  return status;
    1.27 -            
    1.28 +
    1.29 +            // Whether new_key is NULL or not, if this key is equal to the current user default, we 
    1.30 +            // replace it.
    1.31 +            status = replace_main_user_fpr_if_equal(session, curr_ident->user_id, 
    1.32 +                                                    new_fpr, old_fpr);                    
    1.33 +
    1.34 +            if (status != PEP_STATUS_OK)
    1.35 +                return status;            
    1.36 +                
    1.37              status = myself(session, curr_ident);
    1.38  
    1.39 +            if (status != PEP_STATUS_OK)
    1.40 +                return status;            
    1.41 +
    1.42              char* old_copy = NULL;
    1.43              char* new_copy = NULL;
    1.44              old_copy = strdup(old_fpr);
    1.45              new_copy = strdup(new_fpr);
    1.46              if (!old_copy || !new_copy)
    1.47                  return PEP_OUT_OF_MEMORY;
    1.48 +                                
    1.49  
    1.50              stringpair_t* revp = new_stringpair(old_copy, new_copy);                
    1.51              if (!rev_pairs) {
    1.52 @@ -636,11 +654,12 @@
    1.53          new_fpr = NULL;    
    1.54      }
    1.55  
    1.56 -    // actually revoke
    1.57 +    // actually revoke - list only exists with own keys
    1.58      stringpair_list_t* curr_rev_pair = rev_pairs;
    1.59      while (curr_rev_pair && curr_rev_pair->value) {
    1.60          char* rev_key = curr_rev_pair->value->key;
    1.61          char* new_key = curr_rev_pair->value->value;
    1.62 +            
    1.63          if (EMPTYSTR(rev_key) || EMPTYSTR(new_key))
    1.64              return PEP_UNKNOWN_ERROR;
    1.65          bool revoked = false;
    1.66 @@ -659,7 +678,8 @@
    1.67              status = set_revoked(session, rev_key, new_key, time(NULL));            
    1.68  
    1.69          if (status != PEP_STATUS_OK)
    1.70 -            goto pEp_free;        
    1.71 +            goto pEp_free;      
    1.72 +                  
    1.73          curr_rev_pair = curr_rev_pair->next;    
    1.74      }
    1.75  
    1.76 @@ -981,6 +1001,7 @@
    1.77                  return status;
    1.78              }
    1.79              free(ident->fpr);
    1.80 +
    1.81              // release ownership to the struct again
    1.82              ident->fpr = new_key;
    1.83                  
    1.84 @@ -991,6 +1012,16 @@
    1.85  
    1.86              if (status != PEP_STATUS_OK)
    1.87                  goto pEp_free;
    1.88 +
    1.89 +            // Whether new_key is NULL or not, if this key is equal to the current user default, we 
    1.90 +            // replace it.
    1.91 +            status = replace_main_user_fpr_if_equal(session, 
    1.92 +                                                    ident->user_id, 
    1.93 +                                                    new_key, 
    1.94 +                                                    old_key);                    
    1.95 +            
    1.96 +            if (status != PEP_STATUS_OK)
    1.97 +                goto pEp_free;
    1.98                  
    1.99              pEp_identity* tmp_ident = identity_dup(ident);
   1.100              if (!tmp_ident) {
   1.101 @@ -1202,6 +1233,10 @@
   1.102              status = get_identities_by_main_key_id(session, fpr_copy, &key_idents);
   1.103              
   1.104              if (status != PEP_CANNOT_FIND_IDENTITY) {
   1.105 +                
   1.106 +                // N.B. Possible user default key replacement will happen inside
   1.107 +                //      _key_reset_device_group_for_shared_key in the first case.
   1.108 +                //      We handle the reassignment for the second case in the block here.
   1.109                  if (is_grouped) 
   1.110                      status = _key_reset_device_group_for_shared_key(session, key_idents, fpr_copy, false);
   1.111                  else if (status == PEP_STATUS_OK) {
   1.112 @@ -1267,7 +1302,11 @@
   1.113                              if (status == PEP_STATUS_OK)
   1.114                                  status = send_key_reset_to_recents(session, this_ident, fpr_copy, new_key);        
   1.115                              tmp_ident->fpr = NULL;    
   1.116 -                        }                    
   1.117 +                        }
   1.118 +                        
   1.119 +                        // Whether new_key is NULL or not, if this key is equal to the current user default, we 
   1.120 +                        // replace it.
   1.121 +                        status = replace_main_user_fpr_if_equal(session, this_ident->user_id, new_key, fpr_copy);                    
   1.122                      }  // Ident list gets freed below, do not free here!
   1.123                  }
   1.124                  // Ok, we've either now reset for each own identity with this key, or 
     2.1 --- a/src/pEpEngine.c	Wed Feb 05 15:15:47 2020 +0100
     2.2 +++ b/src/pEpEngine.c	Wed Feb 05 19:55:56 2020 +0100
     2.3 @@ -210,6 +210,11 @@
     2.4      "select main_key_id from person"
     2.5      "   where id = ?1 ;";
     2.6  
     2.7 +static const char *sql_replace_main_user_fpr_if_equal =  
     2.8 +    "update person "
     2.9 +    "   set main_key_id = ?1 "
    2.10 +    "   where id = ?2 and main_key_id = ?3;";
    2.11 +
    2.12  static const char *sql_refresh_userid_default_key =
    2.13      "update person "
    2.14      "   set main_key_id = "
    2.15 @@ -886,7 +891,7 @@
    2.16          }
    2.17      }  
    2.18      sqlite3_finalize(update_revoked_w_addr_stmt);
    2.19 -                
    2.20 +                    
    2.21      int_result = sqlite3_exec(
    2.22          session->db,
    2.23          "delete from revocation_contact_list where own_address is NULL;\n"        
    2.24 @@ -1772,6 +1777,10 @@
    2.25              (int)strlen(sql_replace_main_user_fpr), &_session->replace_main_user_fpr, NULL);
    2.26      assert(int_result == SQLITE_OK);
    2.27  
    2.28 +    int_result = sqlite3_prepare_v2(_session->db, sql_replace_main_user_fpr_if_equal,
    2.29 +            (int)strlen(sql_replace_main_user_fpr_if_equal), &_session->replace_main_user_fpr_if_equal, NULL);
    2.30 +    assert(int_result == SQLITE_OK);
    2.31 +
    2.32      int_result = sqlite3_prepare_v2(_session->db, sql_get_main_user_fpr,
    2.33              (int)strlen(sql_get_main_user_fpr), &_session->get_main_user_fpr, NULL);
    2.34      assert(int_result == SQLITE_OK);
    2.35 @@ -2207,6 +2216,8 @@
    2.36                  sqlite3_finalize(session->delete_key);                
    2.37              if (session->replace_main_user_fpr)
    2.38                  sqlite3_finalize(session->replace_main_user_fpr);                
    2.39 +            if (session->replace_main_user_fpr_if_equal)
    2.40 +                sqlite3_finalize(session->replace_main_user_fpr_if_equal);                                
    2.41              if (session->get_main_user_fpr)
    2.42                  sqlite3_finalize(session->get_main_user_fpr);
    2.43              if (session->refresh_userid_default_key)
    2.44 @@ -4343,6 +4354,35 @@
    2.45      return PEP_STATUS_OK;
    2.46  }
    2.47  
    2.48 +PEP_STATUS replace_main_user_fpr_if_equal(PEP_SESSION session, const char* user_id,
    2.49 +                                          const char* new_fpr, const char* compare_fpr) {
    2.50 +    assert(session);
    2.51 +    assert(user_id);
    2.52 +    assert(new_fpr);
    2.53 +    
    2.54 +    if (!session || !user_id || !compare_fpr)
    2.55 +        return PEP_ILLEGAL_VALUE;
    2.56 +
    2.57 +    // N.B. new_fpr can be NULL - if there's no key to replace it, this is fine.
    2.58 +    // See sqlite3 documentation on sqlite3_bind_text() and sqlite3_bind_null()
    2.59 +
    2.60 +    int result;
    2.61 +
    2.62 +    sqlite3_reset(session->replace_main_user_fpr_if_equal);
    2.63 +    sqlite3_bind_text(session->replace_main_user_fpr, 1, new_fpr, -1,
    2.64 +            SQLITE_STATIC);
    2.65 +    sqlite3_bind_text(session->replace_main_user_fpr_if_equal, 2, user_id, -1,
    2.66 +            SQLITE_STATIC);
    2.67 +    sqlite3_bind_text(session->replace_main_user_fpr_if_equal, 3, compare_fpr, -1,
    2.68 +            SQLITE_STATIC);            
    2.69 +    result = sqlite3_step(session->replace_main_user_fpr_if_equal);
    2.70 +    sqlite3_reset(session->replace_main_user_fpr_if_equal);
    2.71 +    if (result != SQLITE_DONE)
    2.72 +        return PEP_CANNOT_SET_PERSON;
    2.73 +
    2.74 +    return PEP_STATUS_OK;
    2.75 +}
    2.76 +
    2.77  PEP_STATUS get_main_user_fpr(PEP_SESSION session, 
    2.78                               const char* user_id,
    2.79                               char** main_fpr)
     3.1 --- a/src/pEpEngine.h	Wed Feb 05 15:15:47 2020 +0100
     3.2 +++ b/src/pEpEngine.h	Wed Feb 05 19:55:56 2020 +0100
     3.3 @@ -1366,6 +1366,9 @@
     3.4  
     3.5  PEP_STATUS replace_main_user_fpr(PEP_SESSION session, const char* user_id,
     3.6                                const char* new_fpr);
     3.7 +
     3.8 +PEP_STATUS replace_main_user_fpr_if_equal(PEP_SESSION session, const char* user_id,
     3.9 +                                          const char* new_fpr, const char* compare_fpr);
    3.10      
    3.11  DYNAMIC_API PEP_STATUS get_replacement_fpr(
    3.12          PEP_SESSION session,
     4.1 --- a/src/pEp_internal.h	Wed Feb 05 15:15:47 2020 +0100
     4.2 +++ b/src/pEp_internal.h	Wed Feb 05 19:55:56 2020 +0100
     4.3 @@ -175,6 +175,7 @@
     4.4      sqlite3_stmt *get_identities_by_main_key_id;
     4.5      sqlite3_stmt *replace_identities_fpr;
     4.6      sqlite3_stmt *replace_main_user_fpr;
     4.7 +    sqlite3_stmt *replace_main_user_fpr_if_equal;
     4.8      sqlite3_stmt *get_main_user_fpr;
     4.9      sqlite3_stmt *refresh_userid_default_key;
    4.10      sqlite3_stmt *delete_key;