src/keymanagement.c
author Krista Bennett <krista@pep-project.org>
Mon, 12 Feb 2018 23:02:13 +0100
changeset 2500 b9e025865715
parent 2497 b3d58a60b753
child 2501 2e8bb7f39f04
permissions -rw-r--r--
ENGINE-360 and ENGINE-373 - double free fun action.
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "platform.h"
     5 
     6 #include <string.h>
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 #include <assert.h>
    10 #include <ctype.h>
    11 
    12 #include "pEp_internal.h"
    13 #include "keymanagement.h"
    14 
    15 #include "sync_fsm.h"
    16 #include "blacklist.h"
    17 
    18 #define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
    19 
    20 static bool key_matches_address(PEP_SESSION session, const char* address,
    21                                 const char* fpr) {
    22     if (!session || !address || !fpr)
    23         return false;
    24     
    25     bool retval = false;
    26     stringlist_t *keylist = NULL;
    27     PEP_STATUS status = find_keys(session, address, &keylist);
    28     if (status == PEP_STATUS_OK && keylist) {
    29         stringlist_t* curr = keylist;
    30         while (curr) {
    31             if (curr->value) {
    32                 if (strcasecmp(curr->value, fpr)) {
    33                     retval = true;
    34                     break;
    35                 }
    36             }
    37             curr = curr->next;
    38         }
    39     }
    40     
    41     free_stringlist(keylist);
    42     return retval;                             
    43 }
    44 
    45 PEP_STATUS elect_pubkey(
    46         PEP_SESSION session, pEp_identity * identity
    47     )
    48 {
    49     PEP_STATUS status;
    50     stringlist_t *keylist = NULL;
    51     char *_fpr = "";
    52     identity->comm_type = PEP_ct_unknown;
    53 
    54     status = find_keys(session, identity->address, &keylist);
    55     assert(status != PEP_OUT_OF_MEMORY);
    56     if (status == PEP_OUT_OF_MEMORY)
    57         return PEP_OUT_OF_MEMORY;
    58     
    59     if (!keylist || !keylist->value)
    60         identity->comm_type = PEP_ct_key_not_found;    
    61     else {
    62         stringlist_t *_keylist;
    63         for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
    64             PEP_comm_type _comm_type_key;
    65 
    66             status = get_key_rating(session, _keylist->value, &_comm_type_key);
    67             assert(status != PEP_OUT_OF_MEMORY);
    68             if (status == PEP_OUT_OF_MEMORY) {
    69                 free_stringlist(keylist);
    70                 return PEP_OUT_OF_MEMORY;
    71             }
    72 
    73             if (_comm_type_key != PEP_ct_compromized &&
    74                 _comm_type_key != PEP_ct_unknown)
    75             {
    76                 if (identity->comm_type == PEP_ct_unknown ||
    77                     _comm_type_key > identity->comm_type)
    78                 {
    79                     bool blacklisted;
    80                     bool mistrusted;
    81                     status = is_mistrusted_key(session, _keylist->value, &mistrusted);
    82                     if (status == PEP_STATUS_OK)
    83                         status = blacklist_is_listed(session, _keylist->value, &blacklisted);
    84                     if (status == PEP_STATUS_OK && !mistrusted && !blacklisted) {
    85                         identity->comm_type = _comm_type_key;
    86                         _fpr = _keylist->value;
    87                     }
    88                 }
    89             }
    90         }
    91     }
    92     free(identity->fpr);
    93 
    94     if (!_fpr || _fpr[0] == '\0')
    95         identity->fpr = NULL;
    96     else {    
    97         identity->fpr = strdup(_fpr);
    98         if (identity->fpr == NULL) {
    99             free_stringlist(keylist);
   100             return PEP_OUT_OF_MEMORY;
   101         }
   102     }
   103     
   104     free_stringlist(keylist);
   105     return PEP_STATUS_OK;
   106 }
   107 
   108 static PEP_STATUS validate_fpr(PEP_SESSION session, 
   109                                pEp_identity* ident) {
   110     
   111     PEP_STATUS status = PEP_STATUS_OK;
   112     
   113     if (!session || !ident || !ident->fpr || !ident->fpr[0])
   114         return PEP_ILLEGAL_VALUE;    
   115         
   116     char* fpr = ident->fpr;
   117     
   118     bool has_private = false;
   119     
   120     if (ident->me) {
   121         status = contains_priv_key(session, fpr, &has_private);
   122         if (status != PEP_STATUS_OK || !has_private)
   123             return PEP_KEY_UNSUITABLE;
   124     }
   125     
   126     status = get_trust(session, ident);
   127     if (status != PEP_STATUS_OK)
   128         ident->comm_type = PEP_ct_unknown;
   129             
   130     PEP_comm_type ct = ident->comm_type;
   131 
   132     if (ct == PEP_ct_unknown) {
   133         // If status is bad, it's ok, we get the rating
   134         // we should use then (PEP_ct_unknown)
   135         get_key_rating(session, fpr, &ct);
   136         ident->comm_type = ct;
   137     }
   138     
   139     bool revoked, expired;
   140     bool blacklisted = false;
   141     
   142     status = key_revoked(session, fpr, &revoked);    
   143         
   144     if (status != PEP_STATUS_OK) {
   145         return ADD_TO_LOG(status);
   146     }
   147     
   148     if (!revoked) {
   149         time_t exp_time = (ident->me ? 
   150                            time(NULL) + (7*24*3600) : time(NULL));
   151                            
   152         status = key_expired(session, fpr, 
   153                              exp_time,
   154                              &expired);
   155                              
   156         assert(status == PEP_STATUS_OK);
   157         if (status != PEP_STATUS_OK)
   158             return ADD_TO_LOG(status);
   159 
   160         if ((ct | PEP_ct_confirmed) == PEP_ct_OpenPGP &&
   161             !ident->me) {
   162             status = blacklist_is_listed(session, 
   163                                          fpr, 
   164                                          &blacklisted);
   165                                          
   166             if (status != PEP_STATUS_OK)
   167                 return ADD_TO_LOG(status);
   168         }
   169     }
   170             
   171     if (ident->me && (ct >= PEP_ct_strong_but_unconfirmed) && !revoked && expired) {
   172         // extend key
   173         timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
   174         status = renew_key(session, fpr, ts);
   175         free_timestamp(ts);
   176 
   177         if (status == PEP_STATUS_OK) {
   178             // if key is valid (second check because pEp key might be extended above)
   179             //      Return fpr        
   180             status = key_expired(session, fpr, time(NULL), &expired);            
   181             if (status != PEP_STATUS_OK) {
   182                  ident->comm_type = PEP_ct_key_expired;
   183                  return ADD_TO_LOG(status);
   184              }
   185             // communicate key(?)
   186         }        
   187     }
   188      
   189     if (revoked) 
   190         ct = PEP_ct_key_revoked;
   191     else if (expired)
   192         ct = PEP_ct_key_expired;        
   193     else if (blacklisted) { // never true for .me
   194         ident->comm_type = ct = PEP_ct_key_not_found;
   195         free(ident->fpr);
   196             ident->fpr = strdup("");
   197         status = PEP_KEY_BLACKLISTED;
   198     }
   199     
   200     switch (ct) {
   201         case PEP_ct_key_expired:
   202         case PEP_ct_key_revoked:
   203         case PEP_ct_key_b0rken:
   204             // delete key from being default key for all users/identities
   205             status = remove_fpr_as_default(session, fpr);
   206             status = update_trust_for_fpr(session, 
   207                                           fpr, 
   208                                           ct);
   209         case PEP_ct_mistrusted:                                  
   210             free(ident->fpr);
   211             ident->fpr = NULL;
   212             ident->comm_type = ct;            
   213             status = PEP_KEY_UNSUITABLE;
   214         default:
   215             break;
   216     }            
   217 
   218     return status;
   219 }
   220 
   221 PEP_STATUS get_user_default_key(PEP_SESSION session, const char* user_id,
   222                                 char** default_key) {
   223     assert(session);
   224     assert(user_id);
   225     
   226     if (!session || !user_id)
   227         return PEP_ILLEGAL_VALUE;
   228 
   229     PEP_STATUS status = PEP_STATUS_OK;
   230             
   231     // try to get default key for user_data
   232     sqlite3_reset(session->get_user_default_key);
   233     sqlite3_bind_text(session->get_user_default_key, 1, user_id, 
   234                       -1, SQLITE_STATIC);
   235     
   236     const int result = sqlite3_step(session->get_user_default_key);
   237     char* user_fpr = NULL;
   238     if (result == SQLITE_ROW) {
   239         const char* u_fpr =
   240             (char *) sqlite3_column_text(session->get_user_default_key, 0);
   241         if (u_fpr)
   242             user_fpr = strdup(u_fpr);
   243     }
   244     else
   245         status = PEP_GET_KEY_FAILED;
   246         
   247     sqlite3_reset(session->get_user_default_key);
   248     
   249     *default_key = user_fpr;
   250     return status;     
   251 }
   252 
   253 // Only call on retrieval of previously stored identity!
   254 // Also, we presume that if the stored_identity was sent in
   255 // without an fpr, there wasn't one in the trust DB for this
   256 // identity.
   257 PEP_STATUS get_valid_pubkey(PEP_SESSION session,
   258                          pEp_identity* stored_identity,
   259                          bool* is_identity_default,
   260                          bool* is_user_default,
   261                          bool* is_address_default) {
   262     
   263     PEP_STATUS status = PEP_STATUS_OK;
   264 
   265     if (!stored_identity || EMPTYSTR(stored_identity->user_id)
   266         || !is_identity_default || !is_user_default || !is_address_default)
   267         return PEP_ILLEGAL_VALUE;
   268         
   269     *is_identity_default = *is_user_default = *is_address_default = false;
   270 
   271     PEP_comm_type first_reject_comm_type = PEP_ct_key_not_found;
   272     PEP_STATUS first_reject_status = PEP_KEY_NOT_FOUND;
   273     
   274     char* stored_fpr = stored_identity->fpr;
   275     // Input: stored identity retrieved from database
   276     // if stored identity contains a default key
   277     if (!EMPTYSTR(stored_fpr)) {
   278         status = validate_fpr(session, stored_identity);    
   279         if (status == PEP_STATUS_OK && !EMPTYSTR(stored_identity->fpr)) {
   280             *is_identity_default = *is_address_default = true;
   281             return status;
   282         }
   283         else if (status != PEP_KEY_NOT_FOUND) {
   284             first_reject_status = status;
   285             first_reject_comm_type = stored_identity->comm_type;
   286         }
   287     }
   288     // if no valid default stored identity key found
   289     free(stored_identity->fpr);
   290     stored_identity->fpr = NULL;
   291     
   292     char* user_fpr = NULL;
   293     status = get_user_default_key(session, stored_identity->user_id, &user_fpr);
   294     
   295     if (!EMPTYSTR(user_fpr)) {             
   296         // There exists a default key for user, so validate
   297         stored_identity->fpr = user_fpr;
   298         status = validate_fpr(session, stored_identity);
   299         if (status == PEP_STATUS_OK && stored_identity->fpr) {
   300             *is_user_default = true;
   301             *is_address_default = key_matches_address(session, 
   302                                                       stored_identity->address,
   303                                                       stored_identity->fpr);
   304             return status;
   305         }        
   306         else if (status != PEP_KEY_NOT_FOUND && first_reject_status != PEP_KEY_NOT_FOUND) {
   307             first_reject_status = status;
   308             first_reject_comm_type = stored_identity->comm_type;
   309         }
   310     }
   311     
   312     status = elect_pubkey(session, stored_identity);
   313     if (status == PEP_STATUS_OK) {
   314         if (!EMPTYSTR(stored_identity->fpr))
   315             validate_fpr(session, stored_identity);
   316     }    
   317     else if (status != PEP_KEY_NOT_FOUND && first_reject_status != PEP_KEY_NOT_FOUND) {
   318         first_reject_status = status;
   319         first_reject_comm_type = stored_identity->comm_type;
   320     }
   321     
   322     switch (stored_identity->comm_type) {
   323         case PEP_ct_key_revoked:
   324         case PEP_ct_key_b0rken:
   325         case PEP_ct_key_expired:
   326         case PEP_ct_compromized:
   327         case PEP_ct_mistrusted:
   328             // this only happens when it's all there is
   329             status = first_reject_status;
   330             free(stored_identity->fpr);
   331             stored_identity->fpr = NULL;
   332             stored_identity->comm_type = first_reject_comm_type;
   333             break;    
   334         default:
   335             break;
   336     }
   337     return status;
   338 }
   339 
   340 static void transfer_ident_lang_and_flags(pEp_identity* new_ident,
   341                                           pEp_identity* stored_ident) {
   342     if (new_ident->lang[0] == 0) {
   343       new_ident->lang[0] = stored_ident->lang[0];
   344       new_ident->lang[1] = stored_ident->lang[1];
   345       new_ident->lang[2] = 0;
   346     }
   347 
   348     new_ident->flags = stored_ident->flags;
   349     new_ident->me = new_ident->me || stored_ident->me;
   350 }
   351 
   352 static PEP_STATUS prepare_updated_identity(PEP_SESSION session,
   353                                                  pEp_identity* return_id,
   354                                                  pEp_identity* stored_ident,
   355                                                  bool store) {
   356     
   357     if (!session || !return_id || !stored_ident)
   358         return PEP_ILLEGAL_VALUE;
   359     
   360     PEP_STATUS status;
   361     
   362     bool is_identity_default, is_user_default, is_address_default;
   363     status = get_valid_pubkey(session, stored_ident,
   364                                 &is_identity_default,
   365                                 &is_user_default,
   366                                 &is_address_default);
   367                                 
   368     if (status == PEP_STATUS_OK && stored_ident->fpr && *(stored_ident->fpr) != '\0') {
   369     // set identity comm_type from trust db (user_id, FPR)
   370         status = get_trust(session, stored_ident);
   371         if (status == PEP_CANNOT_FIND_IDENTITY || stored_ident->comm_type == PEP_ct_unknown) {
   372             // This is OK - there is no trust DB entry, but we
   373             // found a key. We won't store this, but we'll
   374             // use it.
   375             PEP_comm_type ct = PEP_ct_unknown;
   376             status = get_key_rating(session, stored_ident->fpr, &ct);
   377             stored_ident->comm_type = ct;
   378         }
   379     }
   380     else {
   381         if (stored_ident->comm_type == PEP_ct_unknown)
   382             stored_ident->comm_type = PEP_ct_key_not_found;
   383     }
   384     free(return_id->fpr);
   385     return_id->fpr = NULL;
   386     if (status == PEP_STATUS_OK && !EMPTYSTR(stored_ident->fpr))
   387         return_id->fpr = strdup(stored_ident->fpr);
   388         
   389     return_id->comm_type = stored_ident->comm_type;
   390                 
   391     // We patch the DB with the input username, but if we didn't have
   392     // one, we pull it out of storage if available.
   393     // (also, if the input username is "anonymous" and there exists
   394     //  a DB username, we replace)
   395     if (!EMPTYSTR(stored_ident->username)) {
   396         if (!EMPTYSTR(return_id->username) && 
   397             (strcasecmp(return_id->username, return_id->address) == 0)) {
   398             free(return_id->username);
   399             return_id->username = NULL;
   400         }
   401         if (EMPTYSTR(return_id->username)) {
   402             free(return_id->username);
   403             return_id->username = strdup(stored_ident->username);
   404         }
   405     }
   406     else {
   407         if (EMPTYSTR(return_id->username))
   408             return_id->username = strdup(return_id->address);
   409     }
   410     
   411     return_id->me = stored_ident->me;
   412     
   413     // FIXME: Do we ALWAYS do this? We probably should...
   414     if (EMPTYSTR(return_id->user_id)) {
   415         free(return_id->user_id);
   416         return_id->user_id = strdup(stored_ident->user_id);
   417     }    
   418     // Call set_identity() to store
   419     if ((is_identity_default || is_user_default) &&
   420          is_address_default) {                 
   421          // if we got an fpr which is default for either user
   422          // or identity AND is valid for this address, set in DB
   423          // as default
   424          status = set_identity(session, return_id);
   425     }
   426     else {
   427         // Store without default fpr/ct, but return the fpr and ct 
   428         // for current use
   429         char* save_fpr = return_id->fpr;
   430         PEP_comm_type save_ct = return_id->comm_type;
   431         return_id->fpr = NULL;
   432         return_id->comm_type = PEP_ct_unknown;
   433         PEP_STATUS save_status = status;
   434         status = set_identity(session, return_id);
   435         if (save_status != PEP_STATUS_OK)
   436             status = save_status;
   437         return_id->fpr = save_fpr;
   438         return_id->comm_type = save_ct;
   439     }
   440     
   441     transfer_ident_lang_and_flags(return_id, stored_ident);
   442     
   443     if (return_id->comm_type == PEP_ct_unknown)
   444         return_id->comm_type = PEP_ct_key_not_found;
   445     
   446     return status;
   447 }
   448 
   449 
   450 DYNAMIC_API PEP_STATUS update_identity(
   451         PEP_SESSION session, pEp_identity * identity
   452     )
   453 {
   454     PEP_STATUS status;
   455 
   456     assert(session);
   457     assert(identity);
   458     assert(!EMPTYSTR(identity->address));
   459 
   460     if (!(session && identity && !EMPTYSTR(identity->address)))
   461         return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
   462 
   463     char* default_own_id = NULL;
   464     status = get_default_own_userid(session, &default_own_id);    
   465 
   466     // Is this me, temporary or not? If so, BAIL.
   467     if (identity->me || 
   468        (default_own_id && identity->user_id && (strcmp(default_own_id, identity->user_id) == 0))) 
   469     {
   470         return PEP_ILLEGAL_VALUE;
   471     }
   472 
   473     // We have, at least, an address.
   474     // Retrieve stored identity information!    
   475     pEp_identity* stored_ident = NULL;
   476 
   477     if (!EMPTYSTR(identity->user_id)) {            
   478         // (we're gonna update the trust/fpr anyway, so we use the no-fpr-from-trust-db variant)
   479         //      * do get_identity() to retrieve stored identity information
   480         status = get_identity_without_trust_check(session, identity->address, identity->user_id, &stored_ident);
   481 
   482         // Before we start - if there was no stored identity, we should check to make sure we don't
   483         // have a stored identity with a temporary user_id that differs from the input user_id. This
   484         // happens in multithreaded environments sometimes.
   485         if (!stored_ident) {
   486             identity_list* id_list = NULL;
   487             status = get_identities_by_address(session, identity->address, &id_list);
   488 
   489             if (id_list) {
   490                 identity_list* id_curr = id_list;
   491                 while (id_curr) {
   492                     pEp_identity* this_id = id_curr->ident;
   493                     if (this_id) {
   494                         char* this_uid = this_id->user_id;
   495                         if (this_uid && (strstr(this_uid, "TOFU_") == this_uid)) {
   496                             // FIXME: should we also be fixing pEp_own_userId in this
   497                             // function here?
   498                             
   499                             // if usernames match, we replace the userid. Or if the temp username
   500                             // is anonymous.
   501                             // FIXME: do we need to create an address match function which
   502                             // matches the whole dot-and-case rigamarole from 
   503                             if (EMPTYSTR(this_id->username) ||
   504                                 strcasecmp(this_id->username, this_id->address) == 0 ||
   505                                 (identity->username && 
   506                                  strcasecmp(identity->username, 
   507                                             this_id->username) == 0)) {
   508                                 
   509                                 // Ok, we have a temp ID. We have to replace this
   510                                 // with the real ID.
   511                                 status = replace_userid(session, 
   512                                                         this_uid, 
   513                                                         identity->user_id);
   514                                 if (status != PEP_STATUS_OK) {
   515                                     free_identity_list(id_list);
   516                                     return status;
   517                                 }
   518                                     
   519                                 free(this_uid);
   520                                 this_uid = NULL;
   521                                 
   522                                 // Reflect the change we just made to the DB
   523                                 this_id->user_id = strdup(identity->user_id);
   524                                 stored_ident = this_id;
   525                                 // FIXME: free list.
   526                                 break;                                
   527                             }                            
   528                         } 
   529                     }
   530                     id_curr = id_curr->next;
   531                 }
   532             }
   533         } 
   534                 
   535         if (status == PEP_STATUS_OK && stored_ident) { 
   536             //  * if identity available
   537             //      * patch it with username
   538             //          (note: this will happen when 
   539             //           setting automatically below...)
   540             //      * elect valid key for identity
   541             //    * if valid key exists
   542             //        * set return value's fpr
   543             status = prepare_updated_identity(session,
   544                                               identity,
   545                                               stored_ident, true);
   546         }
   547         //  * else (identity unavailable)
   548         else {
   549             status = PEP_STATUS_OK;
   550 
   551             // FIXME: We may need to roll this back.
   552             // FIXME: change docs if we don't
   553             //  if we only have user_id and address and identity not available
   554             //      * return error status (identity not found)
   555             if (EMPTYSTR(identity->username)) {
   556                 free(identity->username);
   557                 identity->username = strdup(identity->address);
   558             }
   559             
   560             // Otherwise, if we had user_id, address, and username:
   561             //    * create identity with user_id, address, username
   562             //      (this is the input id without the fpr + comm type!)
   563 
   564             if (status == PEP_STATUS_OK) {
   565                 elect_pubkey(session, identity);
   566             }
   567                         
   568             //    * We've already checked and retrieved
   569             //      any applicable temporary identities above. If we're 
   570             //      here, none of them fit.
   571             //    * call set_identity() to store
   572             if (status == PEP_STATUS_OK)
   573                 // FIXME: Do we set if we had to copy in the address?
   574                 status = set_identity(session, identity);
   575             //  * Return: created identity
   576         }        
   577     }
   578     else if (!EMPTYSTR(identity->username)) {
   579         /*
   580          * Temporary identity information with username supplied
   581             * Input: address, username (no others)
   582          */
   583          
   584         //  * See if there is an own identity that uses this address. If so, we'll
   585         //    prefer that
   586         stored_ident = NULL;
   587         
   588         if (default_own_id) {
   589             status = get_identity(session, 
   590                                   default_own_id, 
   591                                   identity->address, 
   592                                   &stored_ident);
   593         }
   594         // If there isn't an own identity, search for a non-temp stored ident
   595         // with this address.                      
   596         if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) { 
   597  
   598             identity_list* id_list = NULL;
   599             status = get_identities_by_address(session, identity->address, &id_list);
   600 
   601             if (id_list) {
   602                 identity_list* id_curr = id_list;
   603                 while (id_curr) {
   604                     pEp_identity* this_id = id_curr->ident;
   605                     if (this_id) {
   606                         char* this_uid = this_id->user_id;
   607                         if (this_uid && (strstr(this_uid, "TOFU_") != this_uid)) {
   608                             // if usernames match, we replace the userid.
   609                             if (identity->username && 
   610                                 strcasecmp(identity->username, 
   611                                            this_id->username) == 0) {
   612                                 
   613                                 // Ok, we have a real ID. Copy it!
   614                                 identity->user_id = strdup(this_uid);
   615                                 
   616                                 if (!identity->user_id)
   617                                     status = PEP_OUT_OF_MEMORY;
   618                                 stored_ident = this_id;
   619                                 
   620                                 break;                                
   621                             }                            
   622                         } 
   623                     }
   624                     id_curr = id_curr->next;
   625                 }
   626             }
   627         }
   628         
   629         if (stored_ident) {
   630             status = prepare_updated_identity(session,
   631                                               identity,
   632                                               stored_ident, true);
   633         }
   634         else {
   635             // create temporary identity, store it, and Return this
   636             // This means TOFU_ user_id
   637             identity->user_id = calloc(1, strlen(identity->address) + 6);
   638             if (!identity->user_id)
   639                 return PEP_OUT_OF_MEMORY;
   640 
   641             snprintf(identity->user_id, strlen(identity->address) + 6,
   642                      "TOFU_%s", identity->address);        
   643 
   644             //    * We've already checked and retrieved
   645             //      any applicable temporary identities above. If we're 
   646             //      here, none of them fit.
   647             
   648             status = elect_pubkey(session, identity);
   649                          
   650             //    * call set_identity() to store
   651             if (identity->fpr)
   652                 status = get_key_rating(session, identity->fpr, &identity->comm_type);
   653         
   654             //    * call set_identity() to store            
   655             status = set_identity(session, identity);
   656         }
   657     }
   658     else {
   659         /*
   660         * Input: address (no others)
   661          * Temporary identity information without username suplied
   662          */
   663          
   664         //  * Again, see if there is an own identity that uses this address. If so, we'll
   665         //    prefer that
   666         stored_ident = NULL;
   667          
   668         if (default_own_id) {
   669             status = get_identity(session, 
   670                                   default_own_id, 
   671                                   identity->address, 
   672                                   &stored_ident);
   673         }
   674         // If there isn't an own identity, search for a non-temp stored ident
   675         // with this address.                      
   676         if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) { 
   677  
   678             identity_list* id_list = NULL;
   679             status = get_identities_by_address(session, identity->address, &id_list);
   680 
   681             //    * Search for identity with this address
   682             if (id_list && !(id_list->next)) { // exactly one            
   683                 //    * If exactly one found
   684                 //      * elect valid key for identity (see below)
   685                 //      * Return this identity
   686                 stored_ident = id_list->ident;
   687             }
   688         }
   689         if (stored_ident)
   690             status = prepare_updated_identity(session, identity,
   691                                               stored_ident, false);
   692         else  {            
   693             // too little info. BUT. We see if we can find a key; if so, we create a
   694             // temp identity, look for a key, and store.
   695                          
   696             // create temporary identity, store it, and Return this
   697             // This means TOFU_ user_id
   698             identity->user_id = calloc(1, strlen(identity->address) + 6);
   699             if (!identity->user_id)
   700                 return PEP_OUT_OF_MEMORY;
   701 
   702             snprintf(identity->user_id, strlen(identity->address) + 6,
   703                      "TOFU_%s", identity->address);        
   704         
   705             identity->username = strdup(identity->address);
   706             if (!identity->address)
   707                 return PEP_OUT_OF_MEMORY;            
   708             
   709             free(identity->fpr);
   710             identity->fpr = NULL;
   711             identity->comm_type = PEP_ct_unknown;
   712 
   713             status = elect_pubkey(session, identity);
   714                          
   715             if (identity->fpr)
   716                 status = get_key_rating(session, identity->fpr, &identity->comm_type);
   717         
   718             //    * call set_identity() to store            
   719             status = set_identity(session, identity);
   720 
   721         }
   722             
   723     }
   724     
   725     // FIXME: This is legacy. I presume it's a notification for the caller...
   726     // Revisit once I can talk to Volker
   727     if (identity->comm_type != PEP_ct_compromized &&
   728         identity->comm_type < PEP_ct_strong_but_unconfirmed)
   729         if (session->examine_identity)
   730             session->examine_identity(identity, session->examine_management);
   731 
   732     return ADD_TO_LOG(status);
   733 }
   734 
   735 PEP_STATUS elect_ownkey(
   736         PEP_SESSION session, pEp_identity * identity
   737     )
   738 {
   739     PEP_STATUS status;
   740     stringlist_t *keylist = NULL;
   741 
   742     free(identity->fpr);
   743     identity->fpr = NULL;
   744 
   745     status = find_private_keys(session, identity->address, &keylist);
   746     assert(status != PEP_OUT_OF_MEMORY);
   747     if (status == PEP_OUT_OF_MEMORY)
   748         return PEP_OUT_OF_MEMORY;
   749     
   750     if (keylist != NULL && keylist->value != NULL)
   751     {
   752         char *_fpr = NULL;
   753         identity->comm_type = PEP_ct_unknown;
   754 
   755         stringlist_t *_keylist;
   756         for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
   757             bool is_own = false;
   758             
   759             status = own_key_is_listed(session, _keylist->value, &is_own);
   760             assert(status == PEP_STATUS_OK);
   761             if (status != PEP_STATUS_OK) {
   762                 free_stringlist(keylist);
   763                 return status;
   764             }
   765             
   766             if (is_own)
   767             {
   768                 PEP_comm_type _comm_type_key;
   769                 
   770                 status = get_key_rating(session, _keylist->value, &_comm_type_key);
   771                 assert(status != PEP_OUT_OF_MEMORY);
   772                 if (status == PEP_OUT_OF_MEMORY) {
   773                     free_stringlist(keylist);
   774                     return PEP_OUT_OF_MEMORY;
   775                 }
   776                 
   777                 if (_comm_type_key != PEP_ct_compromized &&
   778                     _comm_type_key != PEP_ct_unknown)
   779                 {
   780                     if (identity->comm_type == PEP_ct_unknown ||
   781                         _comm_type_key > identity->comm_type)
   782                     {
   783                         identity->comm_type = _comm_type_key;
   784                         _fpr = _keylist->value;
   785                     }
   786                 }
   787             }
   788         }
   789         
   790         if (_fpr)
   791         {
   792             identity->fpr = strdup(_fpr);
   793             assert(identity->fpr);
   794             if (identity->fpr == NULL)
   795             {
   796                 free_stringlist(keylist);
   797                 return PEP_OUT_OF_MEMORY;
   798             }
   799         }
   800         free_stringlist(keylist);
   801     }
   802     return PEP_STATUS_OK;
   803 }
   804 
   805 PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
   806                                 bool* is_usable) {
   807     
   808     bool dont_use_fpr = true;
   809     
   810     PEP_STATUS status = blacklist_is_listed(session, fpr, &dont_use_fpr);
   811     if (status == PEP_STATUS_OK && !dont_use_fpr) {
   812         // Make sure there is a *private* key associated with this fpr
   813         bool has_private = false;
   814         status = contains_priv_key(session, fpr, &has_private);
   815 
   816         if (status == PEP_STATUS_OK)
   817             dont_use_fpr = !has_private;
   818     }
   819     
   820     *is_usable = !dont_use_fpr;
   821     
   822     return ADD_TO_LOG(status);
   823 }
   824 
   825 PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
   826 {
   827 
   828     PEP_STATUS status;
   829 
   830     assert(session);
   831     assert(identity);
   832     assert(!EMPTYSTR(identity->address));
   833     assert(!EMPTYSTR(identity->user_id));
   834 
   835     if (!session || !identity || EMPTYSTR(identity->address) ||
   836         EMPTYSTR(identity->user_id))
   837         return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
   838 
   839     pEp_identity *stored_identity = NULL;
   840     char* revoked_fpr = NULL; 
   841         
   842     char* default_own_id = NULL;
   843     status = get_default_own_userid(session, &default_own_id);
   844 
   845     // Deal with non-default user_ids.
   846     if (default_own_id && strcmp(default_own_id, identity->user_id) != 0) {
   847         
   848         status = set_userid_alias(session, default_own_id, identity->user_id);
   849         // Do we want this to be fatal? For now, we'll do it...
   850         if (status != PEP_STATUS_OK)
   851             goto pep_free;
   852             
   853         free(identity->user_id);
   854         identity->user_id = strdup(default_own_id);
   855         if (identity->user_id == NULL) {
   856             status = PEP_OUT_OF_MEMORY;
   857             goto pep_free;
   858         }
   859     }
   860 
   861     // NOTE: IF WE DON'T YET HAVE AN OWN_ID, WE IGNORE REFERENCES TO THIS ADDRESS IN THE
   862     // DB (WHICH MAY HAVE BEEN SET BEFORE MYSELF WAS CALLED BY RECEIVING AN EMAIL FROM
   863     // THIS ADDRESS), AS IT IS NOT AN OWN_IDENTITY AND HAS NO INFORMATION WE NEED OR WHAT TO
   864     // SET FOR MYSELF
   865     
   866     // Ok, so now, set up the own_identity:
   867     identity->comm_type = PEP_ct_pEp;
   868     identity->me = true;
   869     if(ignore_flags)
   870         identity->flags = 0;
   871     
   872     // Let's see if we have an identity record in the DB for 
   873     // this user_id + address
   874     DEBUG_LOG("myself", "debug", identity->address);
   875  
   876     status = get_identity(session,
   877                           identity->address,
   878                           identity->user_id,
   879                           &stored_identity);
   880 
   881     assert(status != PEP_OUT_OF_MEMORY);
   882     if (status == PEP_OUT_OF_MEMORY)
   883         return PEP_OUT_OF_MEMORY;
   884 
   885     // Set usernames - priority is input username > stored name > address
   886     // If there's an input username, we always patch the username with that
   887     // input.
   888     if (EMPTYSTR(identity->username)) {
   889         bool stored_uname = (stored_identity && !EMPTYSTR(stored_identity->username));
   890         char* uname = (stored_uname ? stored_identity->username : identity->address);
   891         free(identity->username);
   892         identity->username = strdup(uname);
   893         if (identity->username == NULL)
   894             return PEP_OUT_OF_MEMORY;
   895     }
   896 
   897     bool valid_key_found = false;
   898     
   899     // Now deal with keys.
   900     // Different from update_identity(), the input fpr here
   901     // MATTERS. 
   902     // If the input fpr is invalid, we return, giving the reason why.
   903     if (!EMPTYSTR(identity->fpr)) {
   904         status = validate_fpr(session, identity);
   905     
   906         if (status != PEP_STATUS_OK || 
   907             identity->comm_type < PEP_ct_strong_but_unconfirmed) {
   908             if (identity->comm_type != PEP_ct_key_expired)
   909                 goto pep_free;
   910             // Otherwise, it was expired and key renewal failed
   911             // and we take the stored one or do keygen. 
   912         } 
   913         else
   914             valid_key_found = true;
   915     }    
   916     
   917     // Ok, if there wasn't a valid input fpr, check stored identity
   918     if (!valid_key_found && stored_identity && 
   919         (EMPTYSTR(identity->fpr) || strcmp(stored_identity->fpr, identity->fpr) != 0)) {
   920         
   921         // Fall back / retrieve
   922         status = validate_fpr(session, stored_identity);
   923         if (status == PEP_STATUS_OK && 
   924             stored_identity->comm_type >= PEP_ct_strong_but_unconfirmed) {
   925           
   926             free(identity->fpr);
   927             identity->fpr = strdup(stored_identity->fpr);
   928             valid_key_found = true;            
   929         }
   930         else {
   931             bool revoked = false;
   932             if (!EMPTYSTR(stored_identity->fpr)) {
   933                 status = key_revoked(session, stored_identity->fpr, &revoked);
   934                 if (revoked)
   935                     revoked_fpr = strdup(stored_identity->fpr);
   936             }        
   937         }
   938     }
   939     
   940     // Nothing left to do but generate a key
   941     if (!valid_key_found) {
   942         if (!do_keygen)
   943             status = PEP_GET_KEY_FAILED;
   944         else {
   945             DEBUG_LOG("Generating key pair", "debug", identity->address);
   946 
   947             free(identity->fpr);
   948             identity->fpr = NULL;
   949             status = generate_keypair(session, identity);
   950             assert(status != PEP_OUT_OF_MEMORY);
   951 
   952             if (status != PEP_STATUS_OK) {
   953                 char buf[11];
   954                 snprintf(buf, 11, "%d", status); // uh, this is kludgey. FIXME
   955                 DEBUG_LOG("Generating key pair failed", "debug", buf);
   956             }        
   957             else {
   958                 valid_key_found = true;
   959                 if (revoked_fpr) {
   960                     status = set_revoked(session, revoked_fpr,
   961                                          stored_identity->fpr, time(NULL));
   962                 }
   963             }
   964         }
   965     }
   966 
   967     if (valid_key_found) {
   968         identity->comm_type = PEP_ct_pEp;
   969         status = PEP_STATUS_OK;
   970     }
   971     else {
   972         free(identity->fpr);
   973         identity->fpr = NULL;
   974         identity->comm_type = PEP_ct_unknown;
   975     }
   976     
   977     status = set_identity(session, identity);
   978 
   979 pep_free:    
   980     free(default_own_id);
   981     free(revoked_fpr);                     
   982     free_identity(stored_identity);
   983     return ADD_TO_LOG(status);
   984 }
   985 
   986 DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
   987                                                  identity_list* my_idents) {
   988     PEP_STATUS status = PEP_STATUS_OK;
   989     if (!session)
   990         return PEP_ILLEGAL_VALUE;
   991         
   992     if (!my_idents)
   993         return PEP_STATUS_OK;
   994             
   995     identity_list* ident_curr = my_idents;
   996     while (ident_curr) {
   997         pEp_identity* ident = ident_curr->ident;
   998         if (!ident || !ident->address) {
   999             status = PEP_ILLEGAL_VALUE;
  1000             goto pep_error;
  1001         }
  1002 
  1003         status = _myself(session, ident, false, false);
  1004         
  1005         ident_curr = ident_curr->next;
  1006     }
  1007     
  1008 pep_error:
  1009     return status;
  1010 }
  1011 
  1012 DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
  1013 {
  1014     return ADD_TO_LOG(_myself(session, identity, true, false));
  1015 }
  1016 
  1017 DYNAMIC_API PEP_STATUS register_examine_function(
  1018         PEP_SESSION session, 
  1019         examine_identity_t examine_identity,
  1020         void *management
  1021     )
  1022 {
  1023     assert(session);
  1024     if (!session)
  1025         return PEP_ILLEGAL_VALUE;
  1026 
  1027     session->examine_management = management;
  1028     session->examine_identity = examine_identity;
  1029 
  1030     return PEP_STATUS_OK;
  1031 }
  1032 
  1033 DYNAMIC_API PEP_STATUS do_keymanagement(
  1034         retrieve_next_identity_t retrieve_next_identity,
  1035         void *management
  1036     )
  1037 {
  1038     PEP_SESSION session;
  1039     pEp_identity *identity;
  1040     PEP_STATUS status;
  1041 
  1042     assert(retrieve_next_identity);
  1043     assert(management);
  1044 
  1045     if (!retrieve_next_identity || !management)
  1046         return PEP_ILLEGAL_VALUE;
  1047 
  1048     status = init(&session);
  1049     assert(status == PEP_STATUS_OK);
  1050     if (status != PEP_STATUS_OK)
  1051         return status;
  1052 
  1053     log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
  1054 
  1055     while ((identity = retrieve_next_identity(management))) 
  1056     {
  1057         assert(identity->address);
  1058         if(identity->address)
  1059         {
  1060             DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
  1061 
  1062             if (identity->me) {
  1063                 status = myself(session, identity);
  1064             } else {
  1065                 status = recv_key(session, identity->address);
  1066             }
  1067 
  1068             assert(status != PEP_OUT_OF_MEMORY);
  1069             if(status == PEP_OUT_OF_MEMORY)
  1070                 return PEP_OUT_OF_MEMORY;
  1071         }
  1072         free_identity(identity);
  1073     }
  1074 
  1075     log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
  1076 
  1077     release(session);
  1078     return PEP_STATUS_OK;
  1079 }
  1080 
  1081 DYNAMIC_API PEP_STATUS key_mistrusted(
  1082         PEP_SESSION session,
  1083         pEp_identity *ident
  1084     )
  1085 {
  1086     PEP_STATUS status = PEP_STATUS_OK;
  1087 
  1088     assert(session);
  1089     assert(ident);
  1090     assert(!EMPTYSTR(ident->fpr));
  1091 
  1092     if (!(session && ident && ident->fpr))
  1093         return PEP_ILLEGAL_VALUE;
  1094 
  1095     if (ident->me)
  1096     {
  1097         revoke_key(session, ident->fpr, NULL);
  1098         myself(session, ident);
  1099     }
  1100     else
  1101     {
  1102         // for undo
  1103         if (session->cached_mistrusted)
  1104             free(session->cached_mistrusted);
  1105         session->cached_mistrusted = identity_dup(ident);
  1106         
  1107         // set mistrust for this user_id/keypair (even if there's not an
  1108         // identity set yet, this is important, as we need to record the mistrust
  1109         // action)
  1110         
  1111         // double-check to be sure key is even in the DB
  1112         if (ident->fpr)
  1113             status = set_pgp_keypair(session, ident->fpr);
  1114 
  1115         // We set this temporarily but will grab it back from the cache afterwards
  1116         ident->comm_type = PEP_ct_mistrusted;
  1117         status = set_trust(session, ident);
  1118         ident->comm_type = session->cached_mistrusted->comm_type;
  1119         
  1120         if (status == PEP_STATUS_OK)
  1121             // cascade that mistrust for anyone using this key
  1122             status = mark_as_compromized(session, ident->fpr);
  1123         if (status == PEP_STATUS_OK)
  1124             status = remove_fpr_as_default(session, ident->fpr);
  1125         if (status == PEP_STATUS_OK)
  1126             status = add_mistrusted_key(session, ident->fpr);
  1127     }
  1128 
  1129     return status;
  1130 }
  1131 
  1132 DYNAMIC_API PEP_STATUS undo_last_mistrust(PEP_SESSION session) {
  1133     assert(session);
  1134     
  1135     if (!session)
  1136         return PEP_ILLEGAL_VALUE;
  1137     
  1138     PEP_STATUS status = PEP_STATUS_OK;
  1139         
  1140     pEp_identity* cached_ident = session->cached_mistrusted;
  1141     
  1142     if (!cached_ident)
  1143         status = PEP_CANNOT_FIND_IDENTITY;
  1144     else {
  1145         status = delete_mistrusted_key(session, cached_ident->fpr);
  1146         if (status == PEP_STATUS_OK) {
  1147             status = set_identity(session, cached_ident);            
  1148             free_identity(session->cached_mistrusted);
  1149         }
  1150     }
  1151     
  1152     session->cached_mistrusted = NULL;
  1153     
  1154     return status;
  1155 }
  1156 
  1157 DYNAMIC_API PEP_STATUS key_reset_trust(
  1158         PEP_SESSION session,
  1159         pEp_identity *ident
  1160     )
  1161 {
  1162     PEP_STATUS status = PEP_STATUS_OK;
  1163 
  1164     assert(session);
  1165     assert(ident);
  1166     assert(!EMPTYSTR(ident->fpr));
  1167     assert(!EMPTYSTR(ident->address));
  1168     assert(!EMPTYSTR(ident->user_id));
  1169 
  1170     if (!(session && ident && ident->fpr && ident->fpr[0] != '\0' && ident->address &&
  1171             ident->user_id))
  1172         return PEP_ILLEGAL_VALUE;
  1173 
  1174     // we do not change the input struct at ALL.
  1175     pEp_identity* input_copy = identity_dup(ident);
  1176     
  1177     pEp_identity* tmp_ident = NULL;
  1178     
  1179     status = get_trust(session, input_copy);
  1180     
  1181     if (status != PEP_STATUS_OK)
  1182         goto pep_free;
  1183         
  1184     PEP_comm_type new_trust = PEP_ct_unknown;
  1185 
  1186     if (input_copy->comm_type != PEP_ct_mistrusted)
  1187         new_trust = input_copy->comm_type & ~PEP_ct_confirmed;
  1188 
  1189     // We'll return the status from the input_copy cache afterward
  1190     input_copy->comm_type = new_trust;
  1191     status = set_trust(session, input_copy);
  1192     
  1193     if (status != PEP_STATUS_OK)
  1194         goto pep_free;
  1195 
  1196     bool mistrusted_key = false;
  1197         
  1198     status = is_mistrusted_key(session, ident->fpr, &mistrusted_key);
  1199 
  1200     if (status != PEP_STATUS_OK)
  1201         goto pep_free;
  1202     
  1203     if (mistrusted_key)
  1204         status = delete_mistrusted_key(session, ident->fpr);
  1205 
  1206     if (status != PEP_STATUS_OK)
  1207         goto pep_free;
  1208         
  1209     tmp_ident = new_identity(ident->address, NULL, ident->user_id, NULL);
  1210 
  1211     if (!tmp_ident)
  1212         return PEP_OUT_OF_MEMORY;
  1213     
  1214     status = update_identity(session, tmp_ident);
  1215     
  1216     if (status != PEP_STATUS_OK)
  1217         goto pep_free;
  1218     
  1219     // remove as default if necessary
  1220     if (strcmp(tmp_ident->fpr, ident->fpr) == 0) {
  1221         free(tmp_ident->fpr);
  1222         tmp_ident->fpr = NULL;
  1223         tmp_ident->comm_type = PEP_ct_unknown;
  1224         status = set_identity(session, tmp_ident);
  1225         if (status != PEP_STATUS_OK)
  1226             goto pep_free;
  1227     }
  1228     
  1229     char* user_default = NULL;
  1230     status = get_main_user_fpr(session, tmp_ident->user_id, &user_default);
  1231     
  1232     if (!EMPTYSTR(user_default)) {
  1233         if (strcmp(user_default, ident->fpr) == 0)
  1234             status = refresh_userid_default_key(session, ident->user_id);
  1235         if (status != PEP_STATUS_OK)
  1236             goto pep_free;    
  1237     }
  1238             
  1239 pep_free:
  1240     free_identity(tmp_ident);
  1241     free_identity(input_copy);
  1242     return status;
  1243 }
  1244 
  1245 DYNAMIC_API PEP_STATUS trust_personal_key(
  1246         PEP_SESSION session,
  1247         pEp_identity *ident
  1248     )
  1249 {
  1250     PEP_STATUS status = PEP_STATUS_OK;
  1251 
  1252     assert(session);
  1253     assert(ident);
  1254     assert(!EMPTYSTR(ident->address));
  1255     assert(!EMPTYSTR(ident->user_id));
  1256     assert(!EMPTYSTR(ident->fpr));
  1257 
  1258     if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
  1259             EMPTYSTR(ident->fpr))
  1260         return PEP_ILLEGAL_VALUE;
  1261 
  1262     //bool ident_has_trusted_default = false;
  1263     char* ident_default_fpr = NULL;
  1264 
  1265     // Before we do anything, be sure the input fpr is even eligible to be trusted
  1266     PEP_comm_type input_default_ct = PEP_ct_unknown;
  1267     status = get_key_rating(session, ident->fpr, &input_default_ct);
  1268     if (input_default_ct < PEP_ct_strong_but_unconfirmed)
  1269         return PEP_KEY_UNSUITABLE;
  1270 
  1271     status = set_pgp_keypair(session, ident->fpr);
  1272     if (status != PEP_STATUS_OK)
  1273         return status;
  1274 
  1275     bool me = is_me(session, ident);
  1276 
  1277     pEp_identity* ident_copy = identity_dup(ident);
  1278     char* cached_fpr = NULL;
  1279 
  1280     // for setting up a temp trusted identity for the input fpr
  1281     pEp_identity* tmp_id = NULL;
  1282 
  1283     // For later, in case we need to check the user default key
  1284     pEp_identity* tmp_user_ident = NULL;
  1285 
  1286     if (me) {
  1287         status = myself(session, ident_copy); 
  1288         goto pep_free;
  1289     }
  1290     
  1291     
  1292     // Save the input fpr
  1293     cached_fpr = strdup(ident->fpr);
  1294 
  1295     // First, set up a temp trusted identity for the input fpr without a comm type;
  1296     tmp_id = new_identity(ident->address, ident->fpr, ident->user_id, NULL);
  1297     status = validate_fpr(session, tmp_id);
  1298         
  1299     if (status == PEP_STATUS_OK) {
  1300         // Validate fpr gets trust DB or, when that fails, key comm type. we checked
  1301         // above that the key was ok. (not revoked or expired), but we want the max.
  1302         tmp_id->comm_type = _MAX(tmp_id->comm_type, input_default_ct) | PEP_ct_confirmed;
  1303                                        
  1304         // Get the default identity without setting the fpr
  1305         status = update_identity(session, ident_copy);
  1306         ident_default_fpr = strdup(ident_copy->fpr);
  1307 
  1308         if (status == PEP_STATUS_OK) {
  1309             bool trusted_default = false;
  1310 
  1311             // If there's no default, or the default is different from the input...
  1312             if (EMPTYSTR(ident_default_fpr) || strcmp(cached_fpr, ident_default_fpr) != 0) {
  1313                 
  1314                 // If the default fpr (if there is one) is trusted and key is strong enough,
  1315                 // don't replace, we just set the trusted bit on this key for this user_id...
  1316                 // (If there's no default fpr, this won't be true anyway.)
  1317                 if (ident->comm_type >= PEP_ct_strong_but_unconfirmed && 
  1318                     (ident->comm_type & PEP_ct_confirmed)) {                        
  1319 
  1320                     trusted_default = true;
  1321                                     
  1322                     status = set_trust(session, tmp_id);
  1323                     input_default_ct = tmp_id->comm_type;                    
  1324                 }
  1325                 else {
  1326                     free(ident_copy->fpr);
  1327                     ident_copy->fpr = strdup(cached_fpr);
  1328                     ident_copy->comm_type = tmp_id->comm_type;
  1329                     status = set_identity(session, ident_copy); // replace identity default            
  1330                 }
  1331             }
  1332             else { // we're setting this on the default fpr
  1333                 ident->comm_type = tmp_id->comm_type;
  1334                 status = set_identity(session, ident);
  1335                 trusted_default = true;
  1336             }
  1337             if (status == PEP_STATUS_OK && !trusted_default) {
  1338                 // Ok, there wasn't a trusted default, so we replaced. Thus, we also
  1339                 // make sure there's a trusted default on the user_id. If there
  1340                 // is not, we make this the default.
  1341                 char* user_default = NULL;
  1342                 status = get_main_user_fpr(session, ident->user_id, &user_default);
  1343             
  1344                 if (status == PEP_STATUS_OK && user_default) {
  1345                     tmp_user_ident = new_identity(ident->address, 
  1346                                                   user_default, 
  1347                                                   ident->user_id, 
  1348                                                   NULL);
  1349                     if (!tmp_user_ident)
  1350                         status = PEP_OUT_OF_MEMORY;
  1351                     else {
  1352                         status = validate_fpr(session, tmp_user_ident);
  1353                         
  1354                         if (status != PEP_STATUS_OK ||
  1355                             tmp_user_ident->comm_type < PEP_ct_strong_but_unconfirmed ||
  1356                             !(tmp_user_ident->comm_type & PEP_ct_confirmed)) 
  1357                         {
  1358                             char* trusted_fpr = (trusted_default ? ident_default_fpr : cached_fpr);
  1359                             status = replace_main_user_fpr(session, ident->user_id, trusted_fpr);
  1360                         } 
  1361                     }
  1362                 }
  1363             }
  1364         }
  1365     }    
  1366 
  1367 pep_free:
  1368     free(ident_default_fpr);
  1369     free(cached_fpr);
  1370     free_identity(tmp_id);
  1371     free_identity(ident_copy);
  1372     free_identity(tmp_user_ident);
  1373     return status;
  1374 }
  1375 
  1376 DYNAMIC_API PEP_STATUS own_key_is_listed(
  1377         PEP_SESSION session,
  1378         const char *fpr,
  1379         bool *listed
  1380     )
  1381 {
  1382     PEP_STATUS status = PEP_STATUS_OK;
  1383     int count;
  1384     
  1385     assert(session && fpr && fpr[0] && listed);
  1386     
  1387     if (!(session && fpr && fpr[0] && listed))
  1388         return PEP_ILLEGAL_VALUE;
  1389     
  1390     *listed = false;
  1391     
  1392     sqlite3_reset(session->own_key_is_listed);
  1393     sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
  1394     
  1395     int result;
  1396     
  1397     result = sqlite3_step(session->own_key_is_listed);
  1398     switch (result) {
  1399         case SQLITE_ROW:
  1400             count = sqlite3_column_int(session->own_key_is_listed, 0);
  1401             *listed = count > 0;
  1402             status = PEP_STATUS_OK;
  1403             break;
  1404             
  1405         default:
  1406             status = PEP_UNKNOWN_ERROR;
  1407     }
  1408     
  1409     sqlite3_reset(session->own_key_is_listed);
  1410     return status;
  1411 }
  1412 
  1413 PEP_STATUS _own_identities_retrieve(
  1414         PEP_SESSION session,
  1415         identity_list **own_identities,
  1416         identity_flags_t excluded_flags
  1417       )
  1418 {
  1419     PEP_STATUS status = PEP_STATUS_OK;
  1420     
  1421     assert(session && own_identities);
  1422     if (!(session && own_identities))
  1423         return PEP_ILLEGAL_VALUE;
  1424     
  1425     *own_identities = NULL;
  1426     identity_list *_own_identities = new_identity_list(NULL);
  1427     if (_own_identities == NULL)
  1428         goto enomem;
  1429     
  1430     sqlite3_reset(session->own_identities_retrieve);
  1431     
  1432     int result;
  1433     // address, fpr, username, user_id, comm_type, lang, flags
  1434     const char *address = NULL;
  1435     const char *fpr = NULL;
  1436     const char *username = NULL;
  1437     const char *user_id = NULL;
  1438     PEP_comm_type comm_type = PEP_ct_unknown;
  1439     const char *lang = NULL;
  1440     unsigned int flags = 0;
  1441     
  1442     identity_list *_bl = _own_identities;
  1443     do {
  1444         sqlite3_bind_int(session->own_identities_retrieve, 1, excluded_flags);
  1445         result = sqlite3_step(session->own_identities_retrieve);
  1446         switch (result) {
  1447             case SQLITE_ROW:
  1448                 address = (const char *)
  1449                     sqlite3_column_text(session->own_identities_retrieve, 0);
  1450                 fpr = (const char *)
  1451                     sqlite3_column_text(session->own_identities_retrieve, 1);
  1452                 user_id = (const char *)
  1453                     sqlite3_column_text(session->own_identities_retrieve, 2);
  1454                 username = (const char *)
  1455                     sqlite3_column_text(session->own_identities_retrieve, 3);
  1456                 comm_type = PEP_ct_pEp;
  1457                 lang = (const char *)
  1458                     sqlite3_column_text(session->own_identities_retrieve, 4);
  1459                 flags = (unsigned int)
  1460                     sqlite3_column_int(session->own_identities_retrieve, 5);
  1461 
  1462                 pEp_identity *ident = new_identity(address, fpr, user_id, username);
  1463                 if (!ident)
  1464                     goto enomem;
  1465                 ident->comm_type = comm_type;
  1466                 if (lang && lang[0]) {
  1467                     ident->lang[0] = lang[0];
  1468                     ident->lang[1] = lang[1];
  1469                     ident->lang[2] = 0;
  1470                 }
  1471                 ident->me = true;
  1472                 ident->flags = flags;
  1473 
  1474                 _bl = identity_list_add(_bl, ident);
  1475                 if (_bl == NULL) {
  1476                     free_identity(ident);
  1477                     goto enomem;
  1478                 }
  1479                 
  1480                 break;
  1481                 
  1482             case SQLITE_DONE:
  1483                 break;
  1484                 
  1485             default:
  1486                 status = PEP_UNKNOWN_ERROR;
  1487                 result = SQLITE_DONE;
  1488         }
  1489     } while (result != SQLITE_DONE);
  1490     
  1491     sqlite3_reset(session->own_identities_retrieve);
  1492     if (status == PEP_STATUS_OK)
  1493         *own_identities = _own_identities;
  1494     else
  1495         free_identity_list(_own_identities);
  1496     
  1497     goto the_end;
  1498     
  1499 enomem:
  1500     free_identity_list(_own_identities);
  1501     status = PEP_OUT_OF_MEMORY;
  1502     
  1503 the_end:
  1504     return status;
  1505 }
  1506 
  1507 DYNAMIC_API PEP_STATUS own_identities_retrieve(
  1508         PEP_SESSION session,
  1509         identity_list **own_identities
  1510       )
  1511 {
  1512     return _own_identities_retrieve(session, own_identities, 0);
  1513 }
  1514 
  1515 PEP_STATUS _own_keys_retrieve(
  1516         PEP_SESSION session,
  1517         stringlist_t **keylist,
  1518         identity_flags_t excluded_flags
  1519       )
  1520 {
  1521     PEP_STATUS status = PEP_STATUS_OK;
  1522     
  1523     assert(session && keylist);
  1524     if (!(session && keylist))
  1525         return PEP_ILLEGAL_VALUE;
  1526     
  1527     *keylist = NULL;
  1528     stringlist_t *_keylist = NULL;
  1529     
  1530     sqlite3_reset(session->own_keys_retrieve);
  1531     
  1532     int result;
  1533     char *fpr = NULL;
  1534     
  1535     stringlist_t *_bl = _keylist;
  1536     do {
  1537         sqlite3_bind_int(session->own_keys_retrieve, 1, excluded_flags);
  1538         result = sqlite3_step(session->own_keys_retrieve);
  1539         switch (result) {
  1540             case SQLITE_ROW:
  1541                 fpr = strdup((const char *) sqlite3_column_text(session->own_keys_retrieve, 0));
  1542                 if(fpr == NULL)
  1543                     goto enomem;
  1544 
  1545                 _bl = stringlist_add(_bl, fpr);
  1546                 if (_bl == NULL) {
  1547                     free(fpr);
  1548                     goto enomem;
  1549                 }
  1550                 if (_keylist == NULL)
  1551                     _keylist = _bl;
  1552                 
  1553                 break;
  1554                 
  1555             case SQLITE_DONE:
  1556                 break;
  1557                 
  1558             default:
  1559                 status = PEP_UNKNOWN_ERROR;
  1560                 result = SQLITE_DONE;
  1561         }
  1562     } while (result != SQLITE_DONE);
  1563     
  1564     sqlite3_reset(session->own_keys_retrieve);
  1565     if (status == PEP_STATUS_OK)
  1566         *keylist = _keylist;
  1567     else
  1568         free_stringlist(_keylist);
  1569     
  1570     goto the_end;
  1571     
  1572 enomem:
  1573     free_stringlist(_keylist);
  1574     status = PEP_OUT_OF_MEMORY;
  1575     
  1576 the_end:
  1577     return status;
  1578 }
  1579 
  1580 DYNAMIC_API PEP_STATUS own_keys_retrieve(PEP_SESSION session, stringlist_t **keylist)
  1581 {
  1582     return _own_keys_retrieve(session, keylist, 0);
  1583 }
  1584 
  1585 // FIXME: should it be be used when sync receive old keys ? (ENGINE-145)
  1586 DYNAMIC_API PEP_STATUS set_own_key(
  1587        PEP_SESSION session,
  1588        const char *address,
  1589        const char *fpr
  1590     )
  1591 {
  1592     PEP_STATUS status = PEP_STATUS_OK;
  1593     
  1594     assert(session &&
  1595            address &&
  1596            fpr && fpr[0]
  1597           );
  1598     
  1599     if (!(session &&
  1600           address &&
  1601           fpr && fpr[0]
  1602          ))
  1603         return PEP_ILLEGAL_VALUE;
  1604                         
  1605     // First see if we have it in own identities already, AND we retrieve
  1606     // our own user_id
  1607     char* my_user_id = NULL;
  1608     status = get_default_own_userid(session, &my_user_id);
  1609     if (status != PEP_STATUS_OK)
  1610         return status;
  1611         
  1612     if (!my_user_id) {
  1613         // We have no own user_id. So we cannot set it for an identity.
  1614         return PEP_CANNOT_FIND_IDENTITY;
  1615     }
  1616     
  1617     pEp_identity* my_id = NULL;
  1618     
  1619     status = get_identity(session, my_user_id, address, &my_id);
  1620 
  1621     if (status == PEP_STATUS_OK && my_id) {
  1622         if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0) {
  1623             // We're done. It was already here.
  1624             goto pep_free;
  1625         }           
  1626     }
  1627                 
  1628     // If there's an id w/ user_id + address
  1629     if (my_id) {
  1630         free(my_id->fpr);
  1631         my_id->fpr = my_user_id;
  1632         my_id->comm_type = PEP_ct_pEp;
  1633         my_id->me = true;
  1634     }
  1635     else { // Else, we need a new identity
  1636         my_id = new_identity(address, fpr, my_user_id, NULL); 
  1637         if (status != PEP_STATUS_OK)
  1638             goto pep_free; 
  1639         my_id->me = true;
  1640         my_id->comm_type = PEP_ct_pEp;
  1641     }
  1642         
  1643     status = set_identity(session, my_id);
  1644     
  1645 pep_free:
  1646     free(my_id);
  1647     free(my_user_id);
  1648     return status;
  1649 }
  1650 
  1651 PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
  1652                              bool *has_private) {
  1653 
  1654     assert(session);
  1655     assert(fpr);
  1656     assert(has_private);
  1657     
  1658     if (!(session && fpr && has_private))
  1659         return PEP_ILLEGAL_VALUE;
  1660 
  1661     return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
  1662 }
  1663 
  1664 PEP_STATUS add_mistrusted_key(PEP_SESSION session, const char* fpr)
  1665 {
  1666     int result;
  1667 
  1668     assert(!EMPTYSTR(fpr));
  1669     
  1670     if (!(session) || EMPTYSTR(fpr))
  1671         return PEP_ILLEGAL_VALUE;
  1672 
  1673     sqlite3_reset(session->add_mistrusted_key);
  1674     sqlite3_bind_text(session->add_mistrusted_key, 1, fpr, -1,
  1675             SQLITE_STATIC);
  1676 
  1677     result = sqlite3_step(session->add_mistrusted_key);
  1678     sqlite3_reset(session->add_mistrusted_key);
  1679 
  1680     if (result != SQLITE_DONE)
  1681         return PEP_CANNOT_SET_PGP_KEYPAIR; // FIXME: Better status?
  1682 
  1683     return PEP_STATUS_OK;
  1684 }
  1685 
  1686 PEP_STATUS delete_mistrusted_key(PEP_SESSION session, const char* fpr)
  1687 {
  1688     int result;
  1689 
  1690     assert(!EMPTYSTR(fpr));
  1691     
  1692     if (!(session) || EMPTYSTR(fpr))
  1693         return PEP_ILLEGAL_VALUE;
  1694 
  1695     sqlite3_reset(session->delete_mistrusted_key);
  1696     sqlite3_bind_text(session->delete_mistrusted_key, 1, fpr, -1,
  1697             SQLITE_STATIC);
  1698 
  1699     result = sqlite3_step(session->delete_mistrusted_key);
  1700     sqlite3_reset(session->delete_mistrusted_key);
  1701 
  1702     if (result != SQLITE_DONE)
  1703         return PEP_UNKNOWN_ERROR; // FIXME: Better status?
  1704 
  1705     return PEP_STATUS_OK;
  1706 }
  1707 
  1708 PEP_STATUS is_mistrusted_key(PEP_SESSION session, const char* fpr,
  1709                              bool* mistrusted)
  1710 {
  1711     PEP_STATUS status = PEP_STATUS_OK;
  1712 
  1713     assert(session);
  1714     assert(!EMPTYSTR(fpr));
  1715 
  1716     if (!(session && fpr))
  1717         return PEP_ILLEGAL_VALUE;
  1718 
  1719     *mistrusted = false;
  1720 
  1721     sqlite3_reset(session->is_mistrusted_key);
  1722     sqlite3_bind_text(session->is_mistrusted_key, 1, fpr, -1, SQLITE_STATIC);
  1723 
  1724     int result;
  1725 
  1726     result = sqlite3_step(session->is_mistrusted_key);
  1727     switch (result) {
  1728     case SQLITE_ROW:
  1729         *mistrusted = sqlite3_column_int(session->is_mistrusted_key, 0);
  1730         status = PEP_STATUS_OK;
  1731         break;
  1732 
  1733     default:
  1734         status = PEP_UNKNOWN_ERROR;
  1735     }
  1736 
  1737     sqlite3_reset(session->is_mistrusted_key);
  1738     return status;
  1739 }