src/key_reset.c
author Krista Bennett <krista@pep-project.org>
Fri, 14 Sep 2018 20:18:53 +0200
branchENGINE-398
changeset 2947 4b525ec0f95c
child 2948 3f66f366dc5f
permissions -rw-r--r--
ENGINE-398: Moved key_reset into its own files
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "pEp_internal.h"
     5 #include "dynamic_api.h"
     6 #include "message_api.h"
     7 
     8 #include <string.h>
     9 #include <stdlib.h>
    10 
    11 PEP_STATUS has_key_reset_been_sent(
    12         PEP_SESSION session, 
    13         const char* user_id, 
    14         const char* revoked_fpr,
    15         bool* contacted)
    16 {
    17     assert(session);
    18     assert(contacted);
    19     assert(user_id);
    20     assert(revoked_fpr);
    21     assert(!EMPTYSTR(user_id));
    22 
    23     if (!session || !contacted || EMPTYSTR(revoked_fpr) || EMPTYSTR(user_id))
    24         return PEP_ILLEGAL_VALUE;
    25     
    26     *contacted = false;
    27                     
    28     char* alias_default = NULL;
    29     
    30     PEP_STATUS status = get_userid_alias_default(session, user_id, &alias_default);
    31     
    32     if (status == PEP_CANNOT_FIND_ALIAS || EMPTYSTR(alias_default)) {
    33         free(alias_default);
    34         alias_default = strdup(user_id);
    35     }
    36     
    37     sqlite3_reset(session->was_id_for_revoke_contacted);
    38     sqlite3_bind_text(session->was_id_for_revoke_contacted, 1, revoked_fpr, -1,
    39             SQLITE_STATIC);
    40     sqlite3_bind_text(session->was_id_for_revoke_contacted, 2, user_id, -1,
    41             SQLITE_STATIC);        
    42     int result = sqlite3_step(session->was_id_for_revoke_contacted);
    43     switch (result) {
    44         case SQLITE_ROW: {
    45             *contacted = (sqlite3_column_int(session->was_id_for_revoke_contacted, 0) != 0);
    46             break;
    47         }
    48         default:
    49             sqlite3_reset(session->was_id_for_revoke_contacted);
    50             free(alias_default);
    51             return PEP_UNKNOWN_DB_ERROR;
    52     }
    53 
    54     sqlite3_reset(session->was_id_for_revoke_contacted);
    55     return PEP_STATUS_OK;
    56 }
    57 
    58 //static const char *sql_set_revoke_contact_as_notified =
    59 //    "insert or replace into revocation_contact_list(fpr, contact_id) values (?1, ?2) ;";
    60 
    61 PEP_STATUS set_reset_contact_notified(
    62         PEP_SESSION session,
    63         const char* revoke_fpr,
    64         const char* contact_id
    65     )
    66 {
    67     PEP_STATUS status = PEP_STATUS_OK;
    68     
    69     assert(session && !EMPTYSTR(revoke_fpr) && !EMPTYSTR(contact_id));
    70     
    71     if (!session || EMPTYSTR(revoke_fpr) || EMPTYSTR(contact_id))
    72         return PEP_ILLEGAL_VALUE;
    73     
    74     sqlite3_reset(session->set_revoke_contact_as_notified);
    75     sqlite3_bind_text(session->set_revoke_contact_as_notified, 1, revoke_fpr, -1, 
    76             SQLITE_STATIC);
    77     sqlite3_bind_text(session->set_revoke_contact_as_notified, 2, contact_id, -1,
    78             SQLITE_STATIC);
    79 
    80     int result;
    81     
    82     result = sqlite3_step(session->set_revoke_contact_as_notified);
    83     switch (result) {
    84         case SQLITE_DONE:
    85             status = PEP_STATUS_OK;
    86             break;
    87             
    88         default:
    89             status = PEP_UNKNOWN_DB_ERROR;
    90     }
    91     
    92     sqlite3_reset(session->set_revoke_contact_as_notified);
    93     return status;    
    94 }
    95 
    96 
    97 PEP_STATUS receive_key_reset(PEP_SESSION session,
    98                              message* reset_msg) {
    99 
   100     if (!session || !reset_msg)
   101         return PEP_ILLEGAL_VALUE;
   102 
   103     pEp_identity* sender_id = reset_msg->from;
   104                 
   105     if (!sender_id)
   106         return PEP_MALFORMED_KEY_RESET_MSG;
   107         
   108     PEP_STATUS status = update_identity(session, sender_id);
   109     if (!sender_id->user_id)
   110         return PEP_UNKNOWN_ERROR;
   111         
   112     if (is_me(session, sender_id))
   113         return PEP_ILLEGAL_VALUE;    
   114         
   115     if (!reset_msg->longmsg || strncmp(reset_msg->longmsg, "OLD: ", 5) != 0) 
   116         return PEP_MALFORMED_KEY_RESET_MSG;
   117 
   118     status = PEP_STATUS_OK;
   119     char* old_fpr = NULL;
   120     char* new_fpr = NULL;
   121     
   122     stringlist_t* keylist = NULL;
   123     pEp_identity* temp_ident = identity_dup(sender_id);
   124     if (!temp_ident) {
   125         status = PEP_OUT_OF_MEMORY;
   126         goto pep_free;
   127     }        
   128             
   129     char* rest = NULL;
   130     char* p = strtok_r(reset_msg->longmsg, "\r\n", &rest);
   131     if (!EMPTYSTR(p + 5))
   132         old_fpr = strdup(p + 5);
   133     else {
   134         status = PEP_MALFORMED_KEY_RESET_MSG;
   135         goto pep_free;
   136     }
   137     
   138     bool own_key = false;
   139     status = is_own_key(session, old_fpr, &own_key);
   140     
   141     if (own_key) {
   142         // Nope, no one can make us our own default. If we want to do that,
   143         // that's keysync, NOT key reset.
   144         status = PEP_ILLEGAL_VALUE;
   145         goto pep_free;
   146     }
   147             
   148     p = strtok_r(NULL, "\r\n", &rest); 
   149     if (strncmp(p, "NEW: ", 5) != 0  || EMPTYSTR(p + 5)) {
   150         status = PEP_MALFORMED_KEY_RESET_MSG;
   151         goto pep_free;
   152     }
   153 
   154     new_fpr = strdup(p + 5);
   155         
   156     // Reset the original key
   157     status = key_reset(session, old_fpr, temp_ident);
   158     if (status != PEP_STATUS_OK)
   159         goto pep_free;
   160         
   161     status = find_keys(session, new_fpr, &keylist);
   162     if (status != PEP_STATUS_OK)
   163         goto pep_free;
   164         
   165     if (!keylist) {
   166         status = PEP_KEY_NOT_FOUND;
   167         goto pep_free;
   168     }
   169 
   170     // alright, we've checked as best we can. Let's set that baby.
   171     sender_id->fpr = new_fpr;
   172     
   173     // This only sets as the default, does NOT TRUST IN ANY WAY
   174     sender_id->comm_type = sender_id->comm_type & (~PEP_ct_confirmed);
   175     status = set_identity(session, sender_id);
   176     
   177     sender_id->fpr = NULL; // ownership for free
   178 pep_free:    
   179     free_stringlist(keylist);    
   180     free(old_fpr);
   181     free(new_fpr);
   182     free_identity(temp_ident);
   183     return status;
   184 }
   185 
   186 PEP_STATUS create_standalone_key_reset_message(PEP_SESSION session,
   187                                                message** dst, 
   188                                                pEp_identity* recip,
   189                                                const char* old_fpr,
   190                                                const char* new_fpr) {
   191                                                    
   192     if (!dst || !recip->user_id || !recip->address)
   193         return PEP_ILLEGAL_VALUE;
   194 
   195     if (!old_fpr || !new_fpr)
   196         return PEP_ILLEGAL_VALUE;
   197         
   198     *dst = NULL;
   199     // Get own identity user has corresponded with
   200     pEp_identity* own_identity = NULL;
   201     
   202     PEP_STATUS status = get_own_ident_for_contact_id(session,
   203                                                      recip,
   204                                                      &own_identity);                                                       
   205     if (status != PEP_STATUS_OK)
   206         return status;
   207         
   208     message* reset_message = new_message(PEP_dir_outgoing);
   209     reset_message->from = own_identity;
   210     reset_message->to = new_identity_list(identity_dup(recip)); // ?
   211     
   212     const char* oldtag = "OLD: ";
   213     const char* newtag = "\nNEW: ";
   214     const size_t taglens = 11;
   215     size_t full_len = taglens + strlen(old_fpr) + strlen(new_fpr) + 2; // \n and \0
   216     char* longmsg = calloc(full_len, 1);
   217     strlcpy(longmsg, oldtag, full_len);
   218     strlcat(longmsg, old_fpr, full_len);
   219     strlcat(longmsg, newtag, full_len);
   220     strlcat(longmsg, new_fpr, full_len);
   221     strlcat(longmsg, "\n", full_len);
   222     reset_message->longmsg = longmsg; 
   223     reset_message->shortmsg = strdup("Key reset");    
   224     
   225     message* output_msg = NULL;
   226     
   227     status = encrypt_message(session, reset_message, NULL,
   228                              &output_msg, PEP_enc_PGP_MIME,
   229                              PEP_encrypt_flag_key_reset_only);
   230 
   231     if (status == PEP_STATUS_OK)
   232         *dst = output_msg;
   233         
   234     free_message(reset_message);
   235     return status;
   236 }
   237 
   238 PEP_STATUS send_key_reset_to_recents(PEP_SESSION session,
   239                                      const char* old_fpr, 
   240                                      const char* new_fpr) {
   241     assert(old_fpr);
   242     assert(new_fpr);
   243     assert(session);
   244     assert(session->messageToSend || session->sync_session->messageToSend);
   245     
   246     if (!session || !old_fpr || !new_fpr)
   247         return PEP_ILLEGAL_VALUE;
   248 
   249     messageToSend_t send_cb = send_cb = session->messageToSend;
   250     void* sync_obj = session->sync_obj;
   251     if (!send_cb) {
   252         send_cb = session->sync_session->messageToSend;
   253         sync_obj = session->sync_session->sync_obj;
   254     }
   255     if (!send_cb)
   256         return PEP_SYNC_NO_MESSAGE_SEND_CALLBACK;
   257         
   258     identity_list* recent_contacts = NULL;
   259     message* reset_msg = NULL;
   260 
   261     PEP_STATUS status = get_last_contacted(session, &recent_contacts);
   262     
   263     if (status != PEP_STATUS_OK)
   264         goto pep_free;
   265                     
   266     identity_list* curr_id_ptr = recent_contacts;
   267 
   268     for (curr_id_ptr = recent_contacts; curr_id_ptr; curr_id_ptr = curr_id_ptr->next) {
   269         pEp_identity* curr_id = curr_id_ptr->ident;
   270         
   271         if (!curr_id)
   272             break;
   273     
   274         const char* user_id = curr_id->user_id;
   275         
   276         // Should be impossible, but?
   277         if (!user_id)
   278             continue;
   279         
   280         // Check if it's us - if so, pointless...
   281         if (is_me(session, curr_id))
   282             continue;
   283             
   284         // Check if they've already been told - this shouldn't be the case, but...
   285         bool contacted = false;
   286         status = has_key_reset_been_sent(session, user_id, old_fpr, &contacted);
   287         if (status != PEP_STATUS_OK)
   288             goto pep_free;
   289     
   290         if (contacted)
   291             continue;
   292             
   293         // if not, make em a message    
   294         reset_msg = NULL;
   295         
   296         status = create_standalone_key_reset_message(session,
   297                                                      &reset_msg,
   298                                                      curr_id,
   299                                                      old_fpr,
   300                                                      new_fpr);
   301 
   302         if (status == PEP_CANNOT_FIND_IDENTITY) { // this is ok, just means we never mailed them 
   303             status = PEP_STATUS_OK;
   304             continue; 
   305         }
   306             
   307         if (status != PEP_STATUS_OK) {
   308             free(reset_msg);
   309             goto pep_free;
   310         }
   311         
   312         // insert into queue
   313         status = send_cb(sync_obj, reset_msg);
   314 
   315         if (status != PEP_STATUS_OK) {
   316             free(reset_msg);
   317             goto pep_free;            
   318         }
   319             
   320         // Put into notified DB
   321         status = set_reset_contact_notified(session, old_fpr, user_id);
   322         if (status != PEP_STATUS_OK)
   323             goto pep_free;            
   324     }
   325     
   326 pep_free:
   327     free_identity_list(recent_contacts);
   328     return status;
   329 }
   330 
   331 DYNAMIC_API PEP_STATUS key_reset(
   332         PEP_SESSION session,
   333         const char* key_id,
   334         pEp_identity* ident
   335     )
   336 {
   337     if (!session)
   338         return PEP_ILLEGAL_VALUE;
   339         
   340     PEP_STATUS status = PEP_STATUS_OK;
   341         
   342     char* fpr_copy = NULL;
   343     char* own_id = NULL;
   344     char* new_key = NULL;
   345     identity_list* key_idents = NULL;
   346     stringlist_t* keys = NULL;
   347     
   348     if (!EMPTYSTR(key_id)) {
   349         fpr_copy = strdup(key_id);
   350         if (!fpr_copy)
   351             return PEP_OUT_OF_MEMORY;
   352     }
   353         
   354     if (!ident) {
   355         // Get list of own identities
   356         status = get_default_own_userid(session, &own_id);
   357         if (status != PEP_STATUS_OK)
   358             goto pep_free;
   359             
   360         if (EMPTYSTR(fpr_copy)) {
   361             status = get_all_keys_for_user(session, own_id, &keys);
   362             if (status == PEP_STATUS_OK) {
   363                 stringlist_t* curr_key;
   364                 for (curr_key = keys; curr_key && curr_key->value; curr_key = curr_key->next) {
   365                     status = key_reset(session, curr_key->value, NULL);
   366                     if (status != PEP_STATUS_OK)
   367                         break;
   368                 }
   369             }
   370             goto pep_free;
   371         } // otherwise, we have a specific fpr to process
   372 
   373         // fpr_copy exists, so... let's go.
   374         // Process own identities with this fpr
   375         status = get_identities_by_main_key_id(session, fpr_copy, &key_idents);
   376         
   377         if (status == PEP_STATUS_OK) {
   378             // have ident list, or should
   379             identity_list* curr_ident;
   380             for (curr_ident = key_idents; curr_ident && curr_ident->ident; 
   381                  curr_ident = curr_ident->next) {
   382                 pEp_identity* this_identity = curr_ident->ident;
   383                 status = key_reset(session, fpr_copy, this_identity);
   384                 if (status != PEP_STATUS_OK)
   385                     break;                    
   386             }
   387         }
   388         goto pep_free;
   389     }
   390     else { // an identity was specified.       
   391         if (is_me(session, ident)) {            
   392             // FIXME: make sure this IS our fpr?
   393             
   394             // If it got sent in with an empty fpr...
   395             if (EMPTYSTR(fpr_copy)) {
   396                 //
   397                 // if (!EMPTYSTR(ident->fpr))
   398                 //     fpr_copy = strdup(ident->fpr);
   399                 status = _myself(session, ident, false, true);
   400                 if (status == PEP_STATUS_OK && ident->fpr)
   401                     fpr_copy = strdup(ident->fpr);
   402                 else {
   403                     // last resort?
   404                     // Get list of own identities
   405                     char* own_id = NULL;
   406                     status = get_default_own_userid(session, &own_id);
   407                     if (status == PEP_STATUS_OK)
   408                         status = get_user_default_key(session, own_id, &fpr_copy);
   409                     if (status != PEP_STATUS_OK || EMPTYSTR(fpr_copy))  {
   410                         free(own_id);
   411                         return (status == PEP_STATUS_OK ? PEP_KEY_NOT_FOUND : status);
   412                     }
   413                 }
   414             }
   415                         
   416             free(ident->fpr);
   417             ident->fpr = fpr_copy;            
   418             // Create revocation
   419             status = revoke_key(session, fpr_copy, NULL);
   420             // generate new key
   421             if (status == PEP_STATUS_OK) {
   422                 ident->fpr = NULL;
   423                 status = generate_keypair(session, ident);
   424             }
   425             if (status == PEP_STATUS_OK) {
   426                 new_key = strdup(ident->fpr);
   427                 status = set_own_key(session, ident, new_key);
   428             }
   429             // mistrust fpr from trust
   430             ident->fpr = fpr_copy;
   431             
   432             ident->comm_type = PEP_ct_mistrusted;
   433             status = set_trust(session, ident);
   434             ident->fpr = NULL;
   435             
   436             // Done with old use of ident.
   437             if (status == PEP_STATUS_OK) {
   438                 // Update fpr for outgoing
   439                 status = myself(session, ident);
   440             }
   441             
   442             if (status == PEP_STATUS_OK)
   443                 // cascade that mistrust for anyone using this key
   444                 status = mark_as_compromised(session, fpr_copy);
   445             if (status == PEP_STATUS_OK)
   446                 status = remove_fpr_as_default(session, fpr_copy);
   447             if (status == PEP_STATUS_OK)
   448                 status = add_mistrusted_key(session, fpr_copy);
   449             // add to revocation list 
   450             if (status == PEP_STATUS_OK) 
   451                 status = set_revoked(session, fpr_copy, new_key, time(NULL));            
   452             // for all active communication partners:
   453             //      active_send revocation
   454             if (status == PEP_STATUS_OK)
   455                 status = send_key_reset_to_recents(session, fpr_copy, new_key);
   456                 
   457         }
   458         else { // not is_me
   459             // remove fpr from all identities
   460             // remove fpr from all users
   461             if (status == PEP_STATUS_OK)
   462                 status = remove_fpr_as_default(session, fpr_copy);
   463             // delete key from DB
   464             if (status == PEP_STATUS_OK) {};
   465 //                status = delete_keypair(session, fpr_copy);
   466             // N.B. If this key is being replaced by something else, it
   467             // is done outside of this function.    
   468         }
   469     }
   470     
   471 pep_free:
   472     free(fpr_copy);
   473     free(own_id);
   474     free_identity_list(key_idents);
   475     free_stringlist(keys);
   476     free(new_key);    
   477     return status;
   478 }