first version compiles for key_reset_and_deliver_revocations key_reset_get_revocations
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 10 Dec 2019 12:15:21 +0100
branchkey_reset_get_revocations
changeset 424540ef37a1e806
parent 4242 813d4d700713
child 4246 587727826dc4
first version compiles for key_reset_and_deliver_revocations
src/key_reset.c
src/key_reset.h
src/pEp_internal.h
test/src/KeyResetMessageTest.cc
     1.1 --- a/src/key_reset.c	Thu Dec 05 10:12:16 2019 +0100
     1.2 +++ b/src/key_reset.c	Tue Dec 10 12:15:21 2019 +0100
     1.3 @@ -156,7 +156,7 @@
     1.4      new_fpr = strdup(p + 5);
     1.5          
     1.6      // Reset the original key
     1.7 -    status = key_reset(session, old_fpr, temp_ident);
     1.8 +    status = key_reset(session, old_fpr, temp_ident, NULL, NULL);
     1.9      if (status != PEP_STATUS_OK)
    1.10          goto pEp_free;
    1.11          
    1.12 @@ -334,7 +334,7 @@
    1.13      if (!session || !ident || (ident && (EMPTYSTR(ident->user_id) || EMPTYSTR(ident->address))))
    1.14          return PEP_ILLEGAL_VALUE;
    1.15      
    1.16 -    return key_reset(session, fpr, ident);    
    1.17 +    return key_reset(session, fpr, ident, NULL, NULL);    
    1.18  }
    1.19  
    1.20  DYNAMIC_API PEP_STATUS key_reset_user(
    1.21 @@ -353,13 +353,13 @@
    1.22      if (is_me(session, input_ident) && EMPTYSTR(fpr))
    1.23          return PEP_ILLEGAL_VALUE;
    1.24          
    1.25 -    PEP_STATUS status = key_reset(session, fpr, input_ident);
    1.26 +    PEP_STATUS status = key_reset(session, fpr, input_ident, NULL, NULL);
    1.27      free_identity(input_ident);
    1.28      return status;
    1.29  }
    1.30  
    1.31  DYNAMIC_API PEP_STATUS key_reset_all_own_keys(PEP_SESSION session) {
    1.32 -    return key_reset(session, NULL, NULL);
    1.33 +    return key_reset(session, NULL, NULL, NULL, NULL);
    1.34  }
    1.35  
    1.36  // Notes to integrate into header:
    1.37 @@ -367,7 +367,9 @@
    1.38  PEP_STATUS key_reset(
    1.39          PEP_SESSION session,
    1.40          const char* key_id,
    1.41 -        pEp_identity* ident
    1.42 +        pEp_identity* ident,
    1.43 +        identity_list** own_identities,
    1.44 +        stringlist_t** own_revoked_fprs
    1.45      )
    1.46  {
    1.47      if (!session || (ident && EMPTYSTR(ident->user_id)))
    1.48 @@ -417,7 +419,7 @@
    1.49              
    1.50              for (curr_key = keys; curr_key && curr_key->value; curr_key = curr_key->next) {
    1.51                  // FIXME: Is the ident really necessary?
    1.52 -                status = key_reset(session, curr_key->value, tmp_ident);
    1.53 +                status = key_reset(session, curr_key->value, tmp_ident, own_identities, own_revoked_fprs);
    1.54                  if (status != PEP_STATUS_OK)
    1.55                      break;
    1.56              }
    1.57 @@ -497,7 +499,7 @@
    1.58                              
    1.59                              pEp_identity* this_identity = curr_ident->ident;
    1.60                              // Do the full reset on this identity        
    1.61 -                            status = key_reset(session, fpr_copy, this_identity);
    1.62 +                            status = key_reset(session, fpr_copy, this_identity, own_identities, own_revoked_fprs);
    1.63                              
    1.64                              // Ident list gets freed below, do not free here!
    1.65  
    1.66 @@ -512,7 +514,8 @@
    1.67                  }    
    1.68              }
    1.69              
    1.70 -            // Create revocation
    1.71 +            // Base case for is_own_private starts here
    1.72 +            
    1.73              status = revoke_key(session, fpr_copy, NULL);
    1.74              
    1.75              // If we have a full identity, we have some cleanup and generation tasks here
    1.76 @@ -526,9 +529,23 @@
    1.77                      new_key = strdup(tmp_ident->fpr);
    1.78  //                    status = set_own_key(session, tmp_ident, new_key);
    1.79                  }
    1.80 +
    1.81 +                if (own_revoked_fprs) {
    1.82 +                    // We can dedup this later
    1.83 +                    if (!(*own_revoked_fprs))
    1.84 +                        *own_revoked_fprs = new_stringlist(NULL);
    1.85 +                    
    1.86 +                    char* revkey = strdup(fpr_copy);
    1.87 +                    if (!revkey) {
    1.88 +                        status = PEP_OUT_OF_MEMORY;
    1.89 +                        goto pEp_free;
    1.90 +                    }
    1.91 +                    stringlist_add(*own_revoked_fprs, revkey);                
    1.92 +                }
    1.93 +                
    1.94                  // mistrust fpr from trust
    1.95                  tmp_ident->fpr = fpr_copy;
    1.96 -                
    1.97 +                                                
    1.98                  tmp_ident->comm_type = PEP_ct_mistrusted;
    1.99                  status = set_trust(session, tmp_ident);
   1.100                  tmp_ident->fpr = NULL;
   1.101 @@ -538,6 +555,18 @@
   1.102                      // Update fpr for outgoing
   1.103                      status = myself(session, tmp_ident);
   1.104                  }
   1.105 +                
   1.106 +                if (own_identities) {
   1.107 +                    if (!(*own_identities))
   1.108 +                        *own_identities = new_identity_list(NULL);
   1.109 +                    
   1.110 +                    pEp_identity* new_ident = identity_dup(tmp_ident);
   1.111 +                    if (!new_ident) {
   1.112 +                        status = PEP_OUT_OF_MEMORY;
   1.113 +                        goto pEp_free;
   1.114 +                    }
   1.115 +                    identity_list_add(*own_identities, new_ident);            
   1.116 +                }    
   1.117              }    
   1.118              
   1.119              if (status == PEP_STATUS_OK)
   1.120 @@ -577,6 +606,7 @@
   1.121              // we want it gone anyway)
   1.122              //
   1.123              // Delete this key from the keyring.
   1.124 +            // FIXME: when key election disappears, so should this!
   1.125              status = delete_keypair(session, fpr_copy);
   1.126          }
   1.127  
   1.128 @@ -604,3 +634,74 @@
   1.129      free(new_key);    
   1.130      return status;
   1.131  }
   1.132 +
   1.133 +static stringlist_t* collect_key_material(PEP_SESSION session, stringlist_t* fprs) {
   1.134 +    stringlist_t* keydata = NULL;    
   1.135 +    stringlist_t* curr_fpr = fprs;    
   1.136 +    while (curr_fpr) {
   1.137 +        if (curr_fpr->value) {
   1.138 +            char* key_material = NULL;
   1.139 +            size_t datasize = 0;
   1.140 +            PEP_STATUS status = export_key(session, curr_fpr->value, &key_material, &datasize);
   1.141 +            if (status) {
   1.142 +                free_stringlist(keydata);
   1.143 +                return NULL;
   1.144 +            }
   1.145 +            if (datasize > 0 && key_material) {
   1.146 +                if (!(keydata))
   1.147 +                    keydata = new_stringlist(NULL);
   1.148 +                    
   1.149 +                stringlist_add(keydata, key_material);
   1.150 +            }
   1.151 +        }
   1.152 +        curr_fpr = curr_fpr->next;        
   1.153 +    }   
   1.154 +    return keydata; 
   1.155 +}
   1.156 +
   1.157 +PEP_STATUS key_reset_own_and_deliver_revocations(PEP_SESSION session, 
   1.158 +                                                 identity_list** own_identities, 
   1.159 +                                                 stringlist_t** revocations, 
   1.160 +                                                 stringlist_t** keys) {
   1.161 +
   1.162 +    if (!(session && own_identities && revocations && keys))
   1.163 +        return PEP_ILLEGAL_VALUE;
   1.164 +        
   1.165 +    stringlist_t* revoked_fprs = NULL;
   1.166 +    identity_list* affected_idents = NULL;
   1.167 +        
   1.168 +    PEP_STATUS status = key_reset(session, NULL, NULL, &affected_idents, &revoked_fprs);                                                 
   1.169 +
   1.170 +    // FIXME: free things
   1.171 +    if (status != PEP_STATUS_OK)
   1.172 +        return status;
   1.173 +    
   1.174 +    dedup_stringlist(revoked_fprs);
   1.175 +
   1.176 +    *revocations = collect_key_material(session, revoked_fprs);
   1.177 +    stringlist_t* keydata = NULL;
   1.178 +    
   1.179 +    if (affected_idents) {
   1.180 +        keydata = new_stringlist(NULL);
   1.181 +        identity_list* curr_ident = affected_idents;
   1.182 +        while (curr_ident) {
   1.183 +            if (curr_ident->ident && curr_ident->ident->fpr) {
   1.184 +                char* key_material = NULL;
   1.185 +                size_t datasize = 0;
   1.186 +                status = export_key(session, curr_ident->ident->fpr, &key_material, &datasize);
   1.187 +                if (status) {
   1.188 +                    free_stringlist(keydata);
   1.189 +                    return status;
   1.190 +                }
   1.191 +                if (datasize > 0 && key_material)
   1.192 +                    stringlist_add(keydata, key_material);
   1.193 +            }
   1.194 +        }
   1.195 +    }
   1.196 +    
   1.197 +    *own_identities = affected_idents;
   1.198 +    *keys = keydata;
   1.199 +    
   1.200 +    free(revoked_fprs);
   1.201 +    return PEP_STATUS_OK;
   1.202 +}
     2.1 --- a/src/key_reset.h	Thu Dec 05 10:12:16 2019 +0100
     2.2 +++ b/src/key_reset.h	Tue Dec 10 12:15:21 2019 +0100
     2.3 @@ -122,10 +122,17 @@
     2.4  PEP_STATUS key_reset(
     2.5          PEP_SESSION session,
     2.6          const char* fpr,
     2.7 -        pEp_identity* ident
     2.8 +        pEp_identity* ident,
     2.9 +        identity_list** own_identities,
    2.10 +        stringlist_t** own_revoked_fprs
    2.11      );
    2.12  
    2.13  
    2.14 +PEP_STATUS key_reset_own_and_deliver_revocations(PEP_SESSION session, 
    2.15 +                                                 identity_list** own_identities, 
    2.16 +                                                 stringlist_t** revocations, 
    2.17 +                                                 stringlist_t** keys);
    2.18 +
    2.19  
    2.20  PEP_STATUS has_key_reset_been_sent(
    2.21          PEP_SESSION session, 
     3.1 --- a/src/pEp_internal.h	Thu Dec 05 10:12:16 2019 +0100
     3.2 +++ b/src/pEp_internal.h	Tue Dec 10 12:15:21 2019 +0100
     3.3 @@ -228,6 +228,7 @@
     3.4      sqlite3_stmt *is_own_address;
     3.5      sqlite3_stmt *own_identities_retrieve;
     3.6      sqlite3_stmt *own_keys_retrieve;
     3.7 +    sqlite3_stmt *key_identities_retrieve;
     3.8      sqlite3_stmt *get_user_default_key;
     3.9      sqlite3_stmt *get_all_keys_for_user;
    3.10          
    3.11 @@ -570,4 +571,3 @@
    3.12      } while (rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
    3.13      return rc;
    3.14  }
    3.15 -
     4.1 --- a/test/src/KeyResetMessageTest.cc	Thu Dec 05 10:12:16 2019 +0100
     4.2 +++ b/test/src/KeyResetMessageTest.cc	Tue Dec 10 12:15:21 2019 +0100
     4.3 @@ -307,7 +307,7 @@
     4.4      );
     4.5      ASSERT_EQ(int_result , SQLITE_OK);
     4.6  
     4.7 -    status = key_reset(session, alice_fpr, from_ident);
     4.8 +    status = key_reset(session, alice_fpr, from_ident, NULL, NULL);
     4.9      ASSERT_EQ(status , PEP_STATUS_OK);
    4.10      ASSERT_GT(m_queue.size(), 0);
    4.11      status = myself(session, from_ident);
    4.12 @@ -460,7 +460,7 @@
    4.13      ASSERT_STRCASEEQ(from_ident->fpr, alice_fpr);
    4.14      ASSERT_TRUE(from_ident->me);
    4.15  
    4.16 -    status = key_reset(session, alice_fpr, from_ident);
    4.17 +    status = key_reset(session, alice_fpr, from_ident, NULL, NULL);
    4.18      ASSERT_EQ(status , PEP_STATUS_OK);
    4.19      m_queue.clear();
    4.20  
    4.21 @@ -524,7 +524,7 @@
    4.22      // FIXME: longer term we need to fix the test, but the key attached to the message below has expired, so for now, we give her a new key
    4.23      slurp_and_import_key(session, "test_keys/pub/pep-test-gabrielle-0xE203586C_pub.asc");
    4.24  
    4.25 -    status = key_reset(session, alice_fpr, from_ident);
    4.26 +    status = key_reset(session, alice_fpr, from_ident, NULL, NULL);
    4.27      ASSERT_EQ(status , PEP_STATUS_OK);
    4.28      ASSERT_EQ(m_queue.size() , 0);
    4.29      m_queue.clear();
    4.30 @@ -570,10 +570,10 @@
    4.31      ASSERT_STRCASEEQ(from_ident->fpr, alice_fpr);
    4.32      ASSERT_TRUE(from_ident->me);
    4.33  
    4.34 -    status = key_reset(session, NULL, NULL);
    4.35 +    status = key_reset(session, NULL, NULL, NULL, NULL);
    4.36      ASSERT_EQ(status , PEP_STATUS_OK);
    4.37  
    4.38 -    status = key_reset(session, NULL, NULL);
    4.39 +    status = key_reset(session, NULL, NULL, NULL, NULL);
    4.40      ASSERT_EQ(status , PEP_STATUS_OK);
    4.41  
    4.42      status = myself(session, from_ident);