src/keymanagement.c
author Krista Bennett <krista@pep-project.org>
Sun, 11 Feb 2018 17:02:19 +0100
changeset 2494 56c577c84996
parent 2493 a4fd5af7fde1
child 2496 389abc0e9b54
permissions -rw-r--r--
Fixed ENGINE-364 and some test bugs. Users now much be CERTAIN to call update_identity if they need the comm_type afte trust_personal_key (this is in accord with the API)
     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         }
   400         if (EMPTYSTR(return_id->username)) {
   401             free(return_id->username);
   402             return_id->username = strdup(stored_ident->username);
   403         }
   404     }
   405     else {
   406         if (EMPTYSTR(return_id->username))
   407             return_id->username = strdup(return_id->address);
   408     }
   409     
   410     return_id->me = stored_ident->me;
   411     
   412     // FIXME: Do we ALWAYS do this? We probably should...
   413     if (EMPTYSTR(return_id->user_id)) {
   414         free(return_id->user_id);
   415         return_id->user_id = strdup(stored_ident->user_id);
   416     }    
   417     // Call set_identity() to store
   418     if ((is_identity_default || is_user_default) &&
   419          is_address_default) {                 
   420          // if we got an fpr which is default for either user
   421          // or identity AND is valid for this address, set in DB
   422          // as default
   423          status = set_identity(session, return_id);
   424     }
   425     else {
   426         // Store without default fpr/ct, but return the fpr and ct 
   427         // for current use
   428         char* save_fpr = return_id->fpr;
   429         PEP_comm_type save_ct = return_id->comm_type;
   430         return_id->fpr = NULL;
   431         return_id->comm_type = PEP_ct_unknown;
   432         PEP_STATUS save_status = status;
   433         status = set_identity(session, return_id);
   434         if (save_status != PEP_STATUS_OK)
   435             status = save_status;
   436         return_id->fpr = save_fpr;
   437         return_id->comm_type = save_ct;
   438     }
   439     
   440     transfer_ident_lang_and_flags(return_id, stored_ident);
   441     
   442     if (return_id->comm_type == PEP_ct_unknown)
   443         return_id->comm_type = PEP_ct_key_not_found;
   444     
   445     return status;
   446 }
   447 
   448 
   449 DYNAMIC_API PEP_STATUS update_identity(
   450         PEP_SESSION session, pEp_identity * identity
   451     )
   452 {
   453     PEP_STATUS status;
   454 
   455     assert(session);
   456     assert(identity);
   457     assert(!EMPTYSTR(identity->address));
   458 
   459     if (!(session && identity && !EMPTYSTR(identity->address)))
   460         return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
   461 
   462     char* default_own_id = NULL;
   463     status = get_default_own_userid(session, &default_own_id);    
   464 
   465     // Is this me, temporary or not? If so, BAIL.
   466     if (identity->me || 
   467        (default_own_id && identity->user_id && (strcmp(default_own_id, identity->user_id) == 0))) 
   468     {
   469         return PEP_ILLEGAL_VALUE;
   470     }
   471 
   472     // We have, at least, an address.
   473     // Retrieve stored identity information!    
   474     pEp_identity* stored_ident = NULL;
   475 
   476     if (!EMPTYSTR(identity->user_id)) {            
   477         // (we're gonna update the trust/fpr anyway, so we use the no-fpr-from-trust-db variant)
   478         //      * do get_identity() to retrieve stored identity information
   479         status = get_identity_without_trust_check(session, identity->address, identity->user_id, &stored_ident);
   480 
   481         // Before we start - if there was no stored identity, we should check to make sure we don't
   482         // have a stored identity with a temporary user_id that differs from the input user_id. This
   483         // happens in multithreaded environments sometimes.
   484         if (!stored_ident) {
   485             identity_list* id_list = NULL;
   486             status = get_identities_by_address(session, identity->address, &id_list);
   487 
   488             if (id_list) {
   489                 identity_list* id_curr = id_list;
   490                 while (id_curr) {
   491                     pEp_identity* this_id = id_curr->ident;
   492                     if (this_id) {
   493                         char* this_uid = this_id->user_id;
   494                         if (this_uid && (strstr(this_uid, "TOFU_") == this_uid)) {
   495                             // FIXME: should we also be fixing pEp_own_userId in this
   496                             // function here?
   497                             
   498                             // if usernames match, we replace the userid. Or if the temp username
   499                             // is anonymous.
   500                             // FIXME: do we need to create an address match function which
   501                             // matches the whole dot-and-case rigamarole from 
   502                             if (EMPTYSTR(this_id->username) ||
   503                                 strcasecmp(this_id->username, this_id->address) == 0 ||
   504                                 (identity->username && 
   505                                  strcasecmp(identity->username, 
   506                                             this_id->username) == 0)) {
   507                                 
   508                                 // Ok, we have a temp ID. We have to replace this
   509                                 // with the real ID.
   510                                 status = replace_userid(session, 
   511                                                         this_uid, 
   512                                                         identity->user_id);
   513                                 if (status != PEP_STATUS_OK) {
   514                                     free_identity_list(id_list);
   515                                     return status;
   516                                 }
   517                                     
   518                                 free(this_uid);
   519                                 this_uid = NULL;
   520                                 
   521                                 // Reflect the change we just made to the DB
   522                                 this_id->user_id = strdup(identity->user_id);
   523                                 stored_ident = this_id;
   524                                 // FIXME: free list.
   525                                 break;                                
   526                             }                            
   527                         } 
   528                     }
   529                     id_curr = id_curr->next;
   530                 }
   531             }
   532         } 
   533                 
   534         if (status == PEP_STATUS_OK && stored_ident) { 
   535             //  * if identity available
   536             //      * patch it with username
   537             //          (note: this will happen when 
   538             //           setting automatically below...)
   539             //      * elect valid key for identity
   540             //    * if valid key exists
   541             //        * set return value's fpr
   542             status = prepare_updated_identity(session,
   543                                               identity,
   544                                               stored_ident, true);
   545         }
   546         //  * else (identity unavailable)
   547         else {
   548             status = PEP_STATUS_OK;
   549 
   550             // FIXME: We may need to roll this back.
   551             // FIXME: change docs if we don't
   552             //  if we only have user_id and address and identity not available
   553             //      * return error status (identity not found)
   554             if (EMPTYSTR(identity->username)) {
   555                 free(identity->username);
   556                 identity->username = strdup(identity->address);
   557             }
   558             
   559             // Otherwise, if we had user_id, address, and username:
   560             //    * create identity with user_id, address, username
   561             //      (this is the input id without the fpr + comm type!)
   562 
   563             if (status == PEP_STATUS_OK) {
   564                 elect_pubkey(session, identity);
   565             }
   566                         
   567             //    * We've already checked and retrieved
   568             //      any applicable temporary identities above. If we're 
   569             //      here, none of them fit.
   570             //    * call set_identity() to store
   571             if (status == PEP_STATUS_OK)
   572                 // FIXME: Do we set if we had to copy in the address?
   573                 status = set_identity(session, identity);
   574             //  * Return: created identity
   575         }        
   576     }
   577     else if (!EMPTYSTR(identity->username)) {
   578         /*
   579          * Temporary identity information with username supplied
   580             * Input: address, username (no others)
   581          */
   582          
   583         //  * See if there is an own identity that uses this address. If so, we'll
   584         //    prefer that
   585         stored_ident = NULL;
   586         
   587         if (default_own_id) {
   588             status = get_identity(session, 
   589                                   default_own_id, 
   590                                   identity->address, 
   591                                   &stored_ident);
   592         }
   593         // If there isn't an own identity, search for a non-temp stored ident
   594         // with this address.                      
   595         if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) { 
   596  
   597             identity_list* id_list = NULL;
   598             status = get_identities_by_address(session, identity->address, &id_list);
   599 
   600             if (id_list) {
   601                 identity_list* id_curr = id_list;
   602                 while (id_curr) {
   603                     pEp_identity* this_id = id_curr->ident;
   604                     if (this_id) {
   605                         char* this_uid = this_id->user_id;
   606                         if (this_uid && (strstr(this_uid, "TOFU_") != this_uid)) {
   607                             // if usernames match, we replace the userid.
   608                             if (identity->username && 
   609                                 strcasecmp(identity->username, 
   610                                            this_id->username) == 0) {
   611                                 
   612                                 // Ok, we have a real ID. Copy it!
   613                                 identity->user_id = strdup(this_uid);
   614                                 
   615                                 if (!identity->user_id)
   616                                     status = PEP_OUT_OF_MEMORY;
   617                                 stored_ident = this_id;
   618                                 
   619                                 break;                                
   620                             }                            
   621                         } 
   622                     }
   623                     id_curr = id_curr->next;
   624                 }
   625             }
   626         }
   627         
   628         if (stored_ident) {
   629             status = prepare_updated_identity(session,
   630                                               identity,
   631                                               stored_ident, true);
   632         }
   633         else {
   634             // create temporary identity, store it, and Return this
   635             // This means TOFU_ user_id
   636             identity->user_id = calloc(1, strlen(identity->address) + 6);
   637             if (!identity->user_id)
   638                 return PEP_OUT_OF_MEMORY;
   639 
   640             snprintf(identity->user_id, strlen(identity->address) + 6,
   641                      "TOFU_%s", identity->address);        
   642             
   643             free(identity->fpr);
   644             identity->fpr = NULL;
   645             identity->comm_type = PEP_ct_unknown;
   646              
   647             //    * We've already checked and retrieved
   648             //      any applicable temporary identities above. If we're 
   649             //      here, none of them fit.
   650             //    * call set_identity() to store
   651             status = set_identity(session, identity);
   652             if (status == PEP_STATUS_OK) {
   653                 elect_pubkey(session, identity);
   654             }
   655         }
   656     }
   657     else {
   658         /*
   659         * Input: address (no others)
   660          * Temporary identity information without username suplied
   661          */
   662          
   663         //  * Again, see if there is an own identity that uses this address. If so, we'll
   664         //    prefer that
   665         stored_ident = NULL;
   666          
   667         if (default_own_id) {
   668             status = get_identity(session, 
   669                                   default_own_id, 
   670                                   identity->address, 
   671                                   &stored_ident);
   672         }
   673         // If there isn't an own identity, search for a non-temp stored ident
   674         // with this address.                      
   675         if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) { 
   676  
   677             identity_list* id_list = NULL;
   678             status = get_identities_by_address(session, identity->address, &id_list);
   679 
   680             //    * Search for identity with this address
   681             if (id_list && !(id_list->next)) { // exactly one            
   682                 //    * If exactly one found
   683                 //      * elect valid key for identity (see below)
   684                 //      * Return this identity
   685                 stored_ident = id_list->ident;
   686             }
   687         }
   688         if (stored_ident)
   689             status = prepare_updated_identity(session, identity,
   690                                               stored_ident, false);
   691         else // too little info
   692             status = PEP_CANNOT_FIND_IDENTITY; 
   693     }
   694     
   695     // FIXME: This is legacy. I presume it's a notification for the caller...
   696     // Revisit once I can talk to Volker
   697     if (identity->comm_type != PEP_ct_compromized &&
   698         identity->comm_type < PEP_ct_strong_but_unconfirmed)
   699         if (session->examine_identity)
   700             session->examine_identity(identity, session->examine_management);
   701 
   702     return ADD_TO_LOG(status);
   703 }
   704 
   705 PEP_STATUS elect_ownkey(
   706         PEP_SESSION session, pEp_identity * identity
   707     )
   708 {
   709     PEP_STATUS status;
   710     stringlist_t *keylist = NULL;
   711 
   712     free(identity->fpr);
   713     identity->fpr = NULL;
   714 
   715     status = find_private_keys(session, identity->address, &keylist);
   716     assert(status != PEP_OUT_OF_MEMORY);
   717     if (status == PEP_OUT_OF_MEMORY)
   718         return PEP_OUT_OF_MEMORY;
   719     
   720     if (keylist != NULL && keylist->value != NULL)
   721     {
   722         char *_fpr = NULL;
   723         identity->comm_type = PEP_ct_unknown;
   724 
   725         stringlist_t *_keylist;
   726         for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
   727             bool is_own = false;
   728             
   729             status = own_key_is_listed(session, _keylist->value, &is_own);
   730             assert(status == PEP_STATUS_OK);
   731             if (status != PEP_STATUS_OK) {
   732                 free_stringlist(keylist);
   733                 return status;
   734             }
   735             
   736             if (is_own)
   737             {
   738                 PEP_comm_type _comm_type_key;
   739                 
   740                 status = get_key_rating(session, _keylist->value, &_comm_type_key);
   741                 assert(status != PEP_OUT_OF_MEMORY);
   742                 if (status == PEP_OUT_OF_MEMORY) {
   743                     free_stringlist(keylist);
   744                     return PEP_OUT_OF_MEMORY;
   745                 }
   746                 
   747                 if (_comm_type_key != PEP_ct_compromized &&
   748                     _comm_type_key != PEP_ct_unknown)
   749                 {
   750                     if (identity->comm_type == PEP_ct_unknown ||
   751                         _comm_type_key > identity->comm_type)
   752                     {
   753                         identity->comm_type = _comm_type_key;
   754                         _fpr = _keylist->value;
   755                     }
   756                 }
   757             }
   758         }
   759         
   760         if (_fpr)
   761         {
   762             identity->fpr = strdup(_fpr);
   763             assert(identity->fpr);
   764             if (identity->fpr == NULL)
   765             {
   766                 free_stringlist(keylist);
   767                 return PEP_OUT_OF_MEMORY;
   768             }
   769         }
   770         free_stringlist(keylist);
   771     }
   772     return PEP_STATUS_OK;
   773 }
   774 
   775 PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
   776                                 bool* is_usable) {
   777     
   778     bool dont_use_fpr = true;
   779     
   780     PEP_STATUS status = blacklist_is_listed(session, fpr, &dont_use_fpr);
   781     if (status == PEP_STATUS_OK && !dont_use_fpr) {
   782         // Make sure there is a *private* key associated with this fpr
   783         bool has_private = false;
   784         status = contains_priv_key(session, fpr, &has_private);
   785 
   786         if (status == PEP_STATUS_OK)
   787             dont_use_fpr = !has_private;
   788     }
   789     
   790     *is_usable = !dont_use_fpr;
   791     
   792     return ADD_TO_LOG(status);
   793 }
   794 
   795 PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
   796 {
   797 
   798     PEP_STATUS status;
   799 
   800     assert(session);
   801     assert(identity);
   802     assert(!EMPTYSTR(identity->address));
   803     assert(!EMPTYSTR(identity->user_id));
   804 
   805     if (!session || !identity || EMPTYSTR(identity->address) ||
   806         EMPTYSTR(identity->user_id))
   807         return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
   808 
   809     pEp_identity *stored_identity = NULL;
   810     char* revoked_fpr = NULL; 
   811         
   812     char* default_own_id = NULL;
   813     status = get_default_own_userid(session, &default_own_id);
   814 
   815     // Deal with non-default user_ids.
   816     if (default_own_id && strcmp(default_own_id, identity->user_id) != 0) {
   817         
   818         status = set_userid_alias(session, default_own_id, identity->user_id);
   819         // Do we want this to be fatal? For now, we'll do it...
   820         if (status != PEP_STATUS_OK)
   821             goto pep_free;
   822             
   823         free(identity->user_id);
   824         identity->user_id = strdup(default_own_id);
   825         if (identity->user_id == NULL) {
   826             status = PEP_OUT_OF_MEMORY;
   827             goto pep_free;
   828         }
   829     }
   830 
   831     // NOTE: IF WE DON'T YET HAVE AN OWN_ID, WE IGNORE REFERENCES TO THIS ADDRESS IN THE
   832     // DB (WHICH MAY HAVE BEEN SET BEFORE MYSELF WAS CALLED BY RECEIVING AN EMAIL FROM
   833     // THIS ADDRESS), AS IT IS NOT AN OWN_IDENTITY AND HAS NO INFORMATION WE NEED OR WHAT TO
   834     // SET FOR MYSELF
   835     
   836     // Ok, so now, set up the own_identity:
   837     identity->comm_type = PEP_ct_pEp;
   838     identity->me = true;
   839     if(ignore_flags)
   840         identity->flags = 0;
   841     
   842     // Let's see if we have an identity record in the DB for 
   843     // this user_id + address
   844     DEBUG_LOG("myself", "debug", identity->address);
   845  
   846     status = get_identity(session,
   847                           identity->address,
   848                           identity->user_id,
   849                           &stored_identity);
   850 
   851     assert(status != PEP_OUT_OF_MEMORY);
   852     if (status == PEP_OUT_OF_MEMORY)
   853         return PEP_OUT_OF_MEMORY;
   854 
   855     // Set usernames - priority is input username > stored name > address
   856     // If there's an input username, we always patch the username with that
   857     // input.
   858     if (EMPTYSTR(identity->username)) {
   859         bool stored_uname = (stored_identity && !EMPTYSTR(stored_identity->username));
   860         char* uname = (stored_uname ? stored_identity->username : identity->address);
   861         free(identity->username);
   862         identity->username = strdup(uname);
   863         if (identity->username == NULL)
   864             return PEP_OUT_OF_MEMORY;
   865     }
   866 
   867     bool valid_key_found = false;
   868     
   869     // Now deal with keys.
   870     // Different from update_identity(), the input fpr here
   871     // MATTERS. 
   872     // If the input fpr is invalid, we return, giving the reason why.
   873     if (!EMPTYSTR(identity->fpr)) {
   874         status = validate_fpr(session, identity);
   875     
   876         if (status != PEP_STATUS_OK || 
   877             identity->comm_type < PEP_ct_strong_but_unconfirmed) {
   878             if (identity->comm_type != PEP_ct_key_expired)
   879                 goto pep_free;
   880             // Otherwise, it was expired and key renewal failed
   881             // and we take the stored one or do keygen. 
   882         } 
   883         else
   884             valid_key_found = true;
   885     }    
   886     
   887     // Ok, if there wasn't a valid input fpr, check stored identity
   888     if (!valid_key_found && stored_identity && 
   889         (EMPTYSTR(identity->fpr) || strcmp(stored_identity->fpr, identity->fpr) != 0)) {
   890         
   891         // Fall back / retrieve
   892         status = validate_fpr(session, stored_identity);
   893         if (status == PEP_STATUS_OK && 
   894             stored_identity->comm_type >= PEP_ct_strong_but_unconfirmed) {
   895           
   896             free(identity->fpr);
   897             identity->fpr = strdup(stored_identity->fpr);
   898             valid_key_found = true;            
   899         }
   900         else {
   901             bool revoked = false;
   902             if (!EMPTYSTR(stored_identity->fpr)) {
   903                 status = key_revoked(session, stored_identity->fpr, &revoked);
   904                 if (revoked)
   905                     revoked_fpr = strdup(stored_identity->fpr);
   906             }        
   907         }
   908     }
   909     
   910     // Nothing left to do but generate a key
   911     if (!valid_key_found) {
   912         if (!do_keygen)
   913             status = PEP_GET_KEY_FAILED;
   914         else {
   915             DEBUG_LOG("Generating key pair", "debug", identity->address);
   916 
   917             free(identity->fpr);
   918             identity->fpr = NULL;
   919             status = generate_keypair(session, identity);
   920             assert(status != PEP_OUT_OF_MEMORY);
   921 
   922             if (status != PEP_STATUS_OK) {
   923                 char buf[11];
   924                 snprintf(buf, 11, "%d", status); // uh, this is kludgey. FIXME
   925                 DEBUG_LOG("Generating key pair failed", "debug", buf);
   926             }        
   927             else {
   928                 valid_key_found = true;
   929                 if (revoked_fpr) {
   930                     status = set_revoked(session, revoked_fpr,
   931                                          stored_identity->fpr, time(NULL));
   932                 }
   933             }
   934         }
   935     }
   936 
   937     if (valid_key_found) {
   938         identity->comm_type = PEP_ct_pEp;
   939         status = PEP_STATUS_OK;
   940     }
   941     else {
   942         free(identity->fpr);
   943         identity->fpr = NULL;
   944         identity->comm_type = PEP_ct_unknown;
   945     }
   946     
   947     status = set_identity(session, identity);
   948 
   949 pep_free:    
   950     free(default_own_id);
   951     free(revoked_fpr);                     
   952     free_identity(stored_identity);
   953     return ADD_TO_LOG(status);
   954 }
   955 
   956 DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
   957                                                  identity_list* my_idents) {
   958     PEP_STATUS status = PEP_STATUS_OK;
   959     if (!session)
   960         return PEP_ILLEGAL_VALUE;
   961         
   962     if (!my_idents)
   963         return PEP_STATUS_OK;
   964             
   965     identity_list* ident_curr = my_idents;
   966     while (ident_curr) {
   967         pEp_identity* ident = ident_curr->ident;
   968         if (!ident || !ident->address) {
   969             status = PEP_ILLEGAL_VALUE;
   970             goto pep_error;
   971         }
   972 
   973         status = _myself(session, ident, false, false);
   974         
   975         ident_curr = ident_curr->next;
   976     }
   977     
   978 pep_error:
   979     return status;
   980 }
   981 
   982 DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
   983 {
   984     return ADD_TO_LOG(_myself(session, identity, true, false));
   985 }
   986 
   987 DYNAMIC_API PEP_STATUS register_examine_function(
   988         PEP_SESSION session, 
   989         examine_identity_t examine_identity,
   990         void *management
   991     )
   992 {
   993     assert(session);
   994     if (!session)
   995         return PEP_ILLEGAL_VALUE;
   996 
   997     session->examine_management = management;
   998     session->examine_identity = examine_identity;
   999 
  1000     return PEP_STATUS_OK;
  1001 }
  1002 
  1003 DYNAMIC_API PEP_STATUS do_keymanagement(
  1004         retrieve_next_identity_t retrieve_next_identity,
  1005         void *management
  1006     )
  1007 {
  1008     PEP_SESSION session;
  1009     pEp_identity *identity;
  1010     PEP_STATUS status;
  1011 
  1012     assert(retrieve_next_identity);
  1013     assert(management);
  1014 
  1015     if (!retrieve_next_identity || !management)
  1016         return PEP_ILLEGAL_VALUE;
  1017 
  1018     status = init(&session);
  1019     assert(status == PEP_STATUS_OK);
  1020     if (status != PEP_STATUS_OK)
  1021         return status;
  1022 
  1023     log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
  1024 
  1025     while ((identity = retrieve_next_identity(management))) 
  1026     {
  1027         assert(identity->address);
  1028         if(identity->address)
  1029         {
  1030             DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
  1031 
  1032             if (identity->me) {
  1033                 status = myself(session, identity);
  1034             } else {
  1035                 status = recv_key(session, identity->address);
  1036             }
  1037 
  1038             assert(status != PEP_OUT_OF_MEMORY);
  1039             if(status == PEP_OUT_OF_MEMORY)
  1040                 return PEP_OUT_OF_MEMORY;
  1041         }
  1042         free_identity(identity);
  1043     }
  1044 
  1045     log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
  1046 
  1047     release(session);
  1048     return PEP_STATUS_OK;
  1049 }
  1050 
  1051 DYNAMIC_API PEP_STATUS key_mistrusted(
  1052         PEP_SESSION session,
  1053         pEp_identity *ident
  1054     )
  1055 {
  1056     PEP_STATUS status = PEP_STATUS_OK;
  1057 
  1058     assert(session);
  1059     assert(ident);
  1060     assert(!EMPTYSTR(ident->fpr));
  1061 
  1062     if (!(session && ident && ident->fpr))
  1063         return PEP_ILLEGAL_VALUE;
  1064 
  1065     if (ident->me)
  1066     {
  1067         revoke_key(session, ident->fpr, NULL);
  1068         myself(session, ident);
  1069     }
  1070     else
  1071     {
  1072         // for undo
  1073         if (session->cached_mistrusted)
  1074             free(session->cached_mistrusted);
  1075         session->cached_mistrusted = identity_dup(ident);
  1076         
  1077         // set mistrust for this user_id/keypair (even if there's not an
  1078         // identity set yet, this is important, as we need to record the mistrust
  1079         // action)
  1080         
  1081         // double-check to be sure key is even in the DB
  1082         if (ident->fpr)
  1083             status = set_pgp_keypair(session, ident->fpr);
  1084 
  1085         // We set this temporarily but will grab it back from the cache afterwards
  1086         ident->comm_type = PEP_ct_mistrusted;
  1087         status = set_trust(session, ident);
  1088         ident->comm_type = session->cached_mistrusted->comm_type;
  1089         
  1090         if (status == PEP_STATUS_OK)
  1091             // cascade that mistrust for anyone using this key
  1092             status = mark_as_compromized(session, ident->fpr);
  1093         if (status == PEP_STATUS_OK)
  1094             status = remove_fpr_as_default(session, ident->fpr);
  1095         if (status == PEP_STATUS_OK)
  1096             status = add_mistrusted_key(session, ident->fpr);
  1097     }
  1098 
  1099     return status;
  1100 }
  1101 
  1102 DYNAMIC_API PEP_STATUS undo_last_mistrust(PEP_SESSION session) {
  1103     assert(session);
  1104     
  1105     if (!session)
  1106         return PEP_ILLEGAL_VALUE;
  1107     
  1108     PEP_STATUS status = PEP_STATUS_OK;
  1109         
  1110     pEp_identity* cached_ident = session->cached_mistrusted;
  1111     
  1112     if (!cached_ident)
  1113         status = PEP_CANNOT_FIND_IDENTITY;
  1114     else {
  1115         status = delete_mistrusted_key(session, cached_ident->fpr);
  1116         if (status == PEP_STATUS_OK) {
  1117             status = set_identity(session, cached_ident);            
  1118             free_identity(session->cached_mistrusted);
  1119         }
  1120     }
  1121     
  1122     session->cached_mistrusted = NULL;
  1123     
  1124     return status;
  1125 }
  1126 
  1127 DYNAMIC_API PEP_STATUS key_reset_trust(
  1128         PEP_SESSION session,
  1129         pEp_identity *ident
  1130     )
  1131 {
  1132     PEP_STATUS status = PEP_STATUS_OK;
  1133 
  1134     assert(session);
  1135     assert(ident);
  1136     assert(!EMPTYSTR(ident->fpr));
  1137     assert(!EMPTYSTR(ident->address));
  1138     assert(!EMPTYSTR(ident->user_id));
  1139 
  1140     if (!(session && ident && ident->fpr && ident->fpr[0] != '\0' && ident->address &&
  1141             ident->user_id))
  1142         return PEP_ILLEGAL_VALUE;
  1143 
  1144     // we do not change the input struct at ALL.
  1145     pEp_identity* input_copy = identity_dup(ident);
  1146     
  1147     pEp_identity* tmp_ident = NULL;
  1148     
  1149     status = get_trust(session, input_copy);
  1150     
  1151     if (status != PEP_STATUS_OK)
  1152         goto pep_free;
  1153         
  1154     PEP_comm_type new_trust = PEP_ct_unknown;
  1155 
  1156     if (input_copy->comm_type != PEP_ct_mistrusted)
  1157         new_trust = input_copy->comm_type & ~PEP_ct_confirmed;
  1158 
  1159     // We'll return the status from the input_copy cache afterward
  1160     input_copy->comm_type = new_trust;
  1161     status = set_trust(session, input_copy);
  1162     
  1163     if (status != PEP_STATUS_OK)
  1164         goto pep_free;
  1165 
  1166     bool mistrusted_key = false;
  1167         
  1168     status = is_mistrusted_key(session, ident->fpr, &mistrusted_key);
  1169 
  1170     if (status != PEP_STATUS_OK)
  1171         goto pep_free;
  1172     
  1173     if (mistrusted_key)
  1174         status = delete_mistrusted_key(session, ident->fpr);
  1175 
  1176     if (status != PEP_STATUS_OK)
  1177         goto pep_free;
  1178         
  1179     tmp_ident = new_identity(ident->address, NULL, ident->user_id, NULL);
  1180 
  1181     if (!tmp_ident)
  1182         return PEP_OUT_OF_MEMORY;
  1183     
  1184     status = update_identity(session, tmp_ident);
  1185     
  1186     if (status != PEP_STATUS_OK)
  1187         goto pep_free;
  1188     
  1189     // remove as default if necessary
  1190     if (strcmp(tmp_ident->fpr, ident->fpr) == 0) {
  1191         free(tmp_ident->fpr);
  1192         tmp_ident->fpr = NULL;
  1193         tmp_ident->comm_type = PEP_ct_unknown;
  1194         status = set_identity(session, tmp_ident);
  1195         if (status != PEP_STATUS_OK)
  1196             goto pep_free;
  1197     }
  1198     
  1199     char* user_default = NULL;
  1200     status = get_main_user_fpr(session, tmp_ident->user_id, &user_default);
  1201     
  1202     if (!EMPTYSTR(user_default)) {
  1203         if (strcmp(user_default, ident->fpr) == 0)
  1204             status = refresh_userid_default_key(session, ident->user_id);
  1205         if (status != PEP_STATUS_OK)
  1206             goto pep_free;    
  1207     }
  1208             
  1209 pep_free:
  1210     free_identity(tmp_ident);
  1211     free_identity(input_copy);
  1212     return status;
  1213 }
  1214 
  1215 DYNAMIC_API PEP_STATUS trust_personal_key(
  1216         PEP_SESSION session,
  1217         pEp_identity *ident
  1218     )
  1219 {
  1220     PEP_STATUS status = PEP_STATUS_OK;
  1221 
  1222     assert(session);
  1223     assert(ident);
  1224     assert(!EMPTYSTR(ident->address));
  1225     assert(!EMPTYSTR(ident->user_id));
  1226     assert(!EMPTYSTR(ident->fpr));
  1227 
  1228     if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
  1229             EMPTYSTR(ident->fpr))
  1230         return PEP_ILLEGAL_VALUE;
  1231 
  1232     //bool ident_has_trusted_default = false;
  1233     char* ident_default_fpr = NULL;
  1234 
  1235     // Before we do anything, be sure the input fpr is even eligible to be trusted
  1236     PEP_comm_type input_default_ct = PEP_ct_unknown;
  1237     status = get_key_rating(session, ident->fpr, &input_default_ct);
  1238     if (input_default_ct < PEP_ct_strong_but_unconfirmed)
  1239         return PEP_KEY_UNSUITABLE;
  1240 
  1241     status = set_pgp_keypair(session, ident->fpr);
  1242     if (status != PEP_STATUS_OK)
  1243         return status;
  1244 
  1245     bool me = is_me(session, ident);
  1246 
  1247     pEp_identity* ident_copy = identity_dup(ident);
  1248     char* cached_fpr = NULL;
  1249 
  1250     // for setting up a temp trusted identity for the input fpr
  1251     pEp_identity* tmp_id = NULL;
  1252 
  1253     if (me) {
  1254         status = myself(session, ident_copy); 
  1255         goto pep_free;
  1256     }
  1257     
  1258     // For later, in case we need to check the user default key
  1259     pEp_identity* tmp_user_ident = NULL;
  1260     
  1261     // Save the input fpr
  1262     cached_fpr = strdup(ident->fpr);
  1263 
  1264     // First, set up a temp trusted identity for the input fpr without a comm type;
  1265     tmp_id = new_identity(ident->address, ident->fpr, ident->user_id, NULL);
  1266     status = validate_fpr(session, tmp_id);
  1267         
  1268     if (status == PEP_STATUS_OK) {
  1269         // Validate fpr gets trust DB or, when that fails, key comm type. we checked
  1270         // above that the key was ok. (not revoked or expired), but we want the max.
  1271         tmp_id->comm_type = _MAX(tmp_id->comm_type, input_default_ct) | PEP_ct_confirmed;
  1272                                        
  1273         // Get the default identity without setting the fpr
  1274         status = update_identity(session, ident_copy);
  1275         ident_default_fpr = strdup(ident_copy->fpr);
  1276 
  1277         if (status == PEP_STATUS_OK) {
  1278             bool trusted_default = false;
  1279 
  1280             // If there's no default, or the default is different from the input...
  1281             if (EMPTYSTR(ident_default_fpr) || strcmp(cached_fpr, ident_default_fpr) != 0) {
  1282                 
  1283                 // If the default fpr (if there is one) is trusted and key is strong enough,
  1284                 // don't replace, we just set the trusted bit on this key for this user_id...
  1285                 // (If there's no default fpr, this won't be true anyway.)
  1286                 if (ident->comm_type >= PEP_ct_strong_but_unconfirmed && 
  1287                     (ident->comm_type & PEP_ct_confirmed)) {                        
  1288 
  1289                     trusted_default = true;
  1290                                     
  1291                     status = set_trust(session, tmp_id);
  1292                     input_default_ct = tmp_id->comm_type;                    
  1293                 }
  1294                 else {
  1295                     free(ident_copy->fpr);
  1296                     ident_copy->fpr = strdup(cached_fpr);
  1297                     ident_copy->comm_type = tmp_id->comm_type;
  1298                     status = set_identity(session, ident_copy); // replace identity default            
  1299                 }
  1300             }
  1301             else { // we're setting this on the default fpr
  1302                 ident->comm_type = tmp_id->comm_type;
  1303                 status = set_identity(session, ident);
  1304                 trusted_default = true;
  1305             }
  1306             if (status == PEP_STATUS_OK && !trusted_default) {
  1307                 // Ok, there wasn't a trusted default, so we replaced. Thus, we also
  1308                 // make sure there's a trusted default on the user_id. If there
  1309                 // is not, we make this the default.
  1310                 char* user_default = NULL;
  1311                 status = get_main_user_fpr(session, ident->user_id, &user_default);
  1312             
  1313                 if (status == PEP_STATUS_OK && user_default) {
  1314                     tmp_user_ident = new_identity(ident->address, 
  1315                                                   user_default, 
  1316                                                   ident->user_id, 
  1317                                                   NULL);
  1318                     if (!tmp_user_ident)
  1319                         status = PEP_OUT_OF_MEMORY;
  1320                     else {
  1321                         status = validate_fpr(session, tmp_user_ident);
  1322                         
  1323                         if (status != PEP_STATUS_OK ||
  1324                             tmp_user_ident->comm_type < PEP_ct_strong_but_unconfirmed ||
  1325                             !(tmp_user_ident->comm_type & PEP_ct_confirmed)) 
  1326                         {
  1327                             char* trusted_fpr = (trusted_default ? ident_default_fpr : cached_fpr);
  1328                             status = replace_main_user_fpr(session, ident->user_id, trusted_fpr);
  1329                         } 
  1330                     }
  1331                 }
  1332             }
  1333         }
  1334     }    
  1335 
  1336 pep_free:
  1337     free(ident_default_fpr);
  1338     free(cached_fpr);
  1339     free_identity(tmp_id);
  1340     free_identity(ident_copy);
  1341     free_identity(tmp_user_ident);
  1342     return status;
  1343 }
  1344 
  1345 DYNAMIC_API PEP_STATUS own_key_is_listed(
  1346         PEP_SESSION session,
  1347         const char *fpr,
  1348         bool *listed
  1349     )
  1350 {
  1351     PEP_STATUS status = PEP_STATUS_OK;
  1352     int count;
  1353     
  1354     assert(session && fpr && fpr[0] && listed);
  1355     
  1356     if (!(session && fpr && fpr[0] && listed))
  1357         return PEP_ILLEGAL_VALUE;
  1358     
  1359     *listed = false;
  1360     
  1361     sqlite3_reset(session->own_key_is_listed);
  1362     sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
  1363     
  1364     int result;
  1365     
  1366     result = sqlite3_step(session->own_key_is_listed);
  1367     switch (result) {
  1368         case SQLITE_ROW:
  1369             count = sqlite3_column_int(session->own_key_is_listed, 0);
  1370             *listed = count > 0;
  1371             status = PEP_STATUS_OK;
  1372             break;
  1373             
  1374         default:
  1375             status = PEP_UNKNOWN_ERROR;
  1376     }
  1377     
  1378     sqlite3_reset(session->own_key_is_listed);
  1379     return status;
  1380 }
  1381 
  1382 PEP_STATUS _own_identities_retrieve(
  1383         PEP_SESSION session,
  1384         identity_list **own_identities,
  1385         identity_flags_t excluded_flags
  1386       )
  1387 {
  1388     PEP_STATUS status = PEP_STATUS_OK;
  1389     
  1390     assert(session && own_identities);
  1391     if (!(session && own_identities))
  1392         return PEP_ILLEGAL_VALUE;
  1393     
  1394     *own_identities = NULL;
  1395     identity_list *_own_identities = new_identity_list(NULL);
  1396     if (_own_identities == NULL)
  1397         goto enomem;
  1398     
  1399     sqlite3_reset(session->own_identities_retrieve);
  1400     
  1401     int result;
  1402     // address, fpr, username, user_id, comm_type, lang, flags
  1403     const char *address = NULL;
  1404     const char *fpr = NULL;
  1405     const char *username = NULL;
  1406     const char *user_id = NULL;
  1407     PEP_comm_type comm_type = PEP_ct_unknown;
  1408     const char *lang = NULL;
  1409     unsigned int flags = 0;
  1410     
  1411     identity_list *_bl = _own_identities;
  1412     do {
  1413         sqlite3_bind_int(session->own_identities_retrieve, 1, excluded_flags);
  1414         result = sqlite3_step(session->own_identities_retrieve);
  1415         switch (result) {
  1416             case SQLITE_ROW:
  1417                 address = (const char *)
  1418                     sqlite3_column_text(session->own_identities_retrieve, 0);
  1419                 fpr = (const char *)
  1420                     sqlite3_column_text(session->own_identities_retrieve, 1);
  1421                 user_id = (const char *)
  1422                     sqlite3_column_text(session->own_identities_retrieve, 2);
  1423                 username = (const char *)
  1424                     sqlite3_column_text(session->own_identities_retrieve, 3);
  1425                 comm_type = PEP_ct_pEp;
  1426                 lang = (const char *)
  1427                     sqlite3_column_text(session->own_identities_retrieve, 4);
  1428                 flags = (unsigned int)
  1429                     sqlite3_column_int(session->own_identities_retrieve, 5);
  1430 
  1431                 pEp_identity *ident = new_identity(address, fpr, user_id, username);
  1432                 if (!ident)
  1433                     goto enomem;
  1434                 ident->comm_type = comm_type;
  1435                 if (lang && lang[0]) {
  1436                     ident->lang[0] = lang[0];
  1437                     ident->lang[1] = lang[1];
  1438                     ident->lang[2] = 0;
  1439                 }
  1440                 ident->me = true;
  1441                 ident->flags = flags;
  1442 
  1443                 _bl = identity_list_add(_bl, ident);
  1444                 if (_bl == NULL) {
  1445                     free_identity(ident);
  1446                     goto enomem;
  1447                 }
  1448                 
  1449                 break;
  1450                 
  1451             case SQLITE_DONE:
  1452                 break;
  1453                 
  1454             default:
  1455                 status = PEP_UNKNOWN_ERROR;
  1456                 result = SQLITE_DONE;
  1457         }
  1458     } while (result != SQLITE_DONE);
  1459     
  1460     sqlite3_reset(session->own_identities_retrieve);
  1461     if (status == PEP_STATUS_OK)
  1462         *own_identities = _own_identities;
  1463     else
  1464         free_identity_list(_own_identities);
  1465     
  1466     goto the_end;
  1467     
  1468 enomem:
  1469     free_identity_list(_own_identities);
  1470     status = PEP_OUT_OF_MEMORY;
  1471     
  1472 the_end:
  1473     return status;
  1474 }
  1475 
  1476 DYNAMIC_API PEP_STATUS own_identities_retrieve(
  1477         PEP_SESSION session,
  1478         identity_list **own_identities
  1479       )
  1480 {
  1481     return _own_identities_retrieve(session, own_identities, 0);
  1482 }
  1483 
  1484 PEP_STATUS _own_keys_retrieve(
  1485         PEP_SESSION session,
  1486         stringlist_t **keylist,
  1487         identity_flags_t excluded_flags
  1488       )
  1489 {
  1490     PEP_STATUS status = PEP_STATUS_OK;
  1491     
  1492     assert(session && keylist);
  1493     if (!(session && keylist))
  1494         return PEP_ILLEGAL_VALUE;
  1495     
  1496     *keylist = NULL;
  1497     stringlist_t *_keylist = NULL;
  1498     
  1499     sqlite3_reset(session->own_keys_retrieve);
  1500     
  1501     int result;
  1502     char *fpr = NULL;
  1503     
  1504     stringlist_t *_bl = _keylist;
  1505     do {
  1506         sqlite3_bind_int(session->own_keys_retrieve, 1, excluded_flags);
  1507         result = sqlite3_step(session->own_keys_retrieve);
  1508         switch (result) {
  1509             case SQLITE_ROW:
  1510                 fpr = strdup((const char *) sqlite3_column_text(session->own_keys_retrieve, 0));
  1511                 if(fpr == NULL)
  1512                     goto enomem;
  1513 
  1514                 _bl = stringlist_add(_bl, fpr);
  1515                 if (_bl == NULL) {
  1516                     free(fpr);
  1517                     goto enomem;
  1518                 }
  1519                 if (_keylist == NULL)
  1520                     _keylist = _bl;
  1521                 
  1522                 break;
  1523                 
  1524             case SQLITE_DONE:
  1525                 break;
  1526                 
  1527             default:
  1528                 status = PEP_UNKNOWN_ERROR;
  1529                 result = SQLITE_DONE;
  1530         }
  1531     } while (result != SQLITE_DONE);
  1532     
  1533     sqlite3_reset(session->own_keys_retrieve);
  1534     if (status == PEP_STATUS_OK)
  1535         *keylist = _keylist;
  1536     else
  1537         free_stringlist(_keylist);
  1538     
  1539     goto the_end;
  1540     
  1541 enomem:
  1542     free_stringlist(_keylist);
  1543     status = PEP_OUT_OF_MEMORY;
  1544     
  1545 the_end:
  1546     return status;
  1547 }
  1548 
  1549 DYNAMIC_API PEP_STATUS own_keys_retrieve(PEP_SESSION session, stringlist_t **keylist)
  1550 {
  1551     return _own_keys_retrieve(session, keylist, 0);
  1552 }
  1553 
  1554 // FIXME: should it be be used when sync receive old keys ? (ENGINE-145)
  1555 DYNAMIC_API PEP_STATUS set_own_key(
  1556        PEP_SESSION session,
  1557        const char *address,
  1558        const char *fpr
  1559     )
  1560 {
  1561     PEP_STATUS status = PEP_STATUS_OK;
  1562     
  1563     assert(session &&
  1564            address &&
  1565            fpr && fpr[0]
  1566           );
  1567     
  1568     if (!(session &&
  1569           address &&
  1570           fpr && fpr[0]
  1571          ))
  1572         return PEP_ILLEGAL_VALUE;
  1573                         
  1574     // First see if we have it in own identities already, AND we retrieve
  1575     // our own user_id
  1576     char* my_user_id = NULL;
  1577     status = get_default_own_userid(session, &my_user_id);
  1578     if (status != PEP_STATUS_OK)
  1579         return status;
  1580         
  1581     if (!my_user_id) {
  1582         // We have no own user_id. So we cannot set it for an identity.
  1583         return PEP_CANNOT_FIND_IDENTITY;
  1584     }
  1585     
  1586     pEp_identity* my_id = NULL;
  1587     
  1588     status = get_identity(session, my_user_id, address, &my_id);
  1589 
  1590     if (status == PEP_STATUS_OK && my_id) {
  1591         if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0) {
  1592             // We're done. It was already here.
  1593             goto pep_free;
  1594         }           
  1595     }
  1596                 
  1597     // If there's an id w/ user_id + address
  1598     if (my_id) {
  1599         free(my_id->fpr);
  1600         my_id->fpr = my_user_id;
  1601         my_id->comm_type = PEP_ct_pEp;
  1602         my_id->me = true;
  1603     }
  1604     else { // Else, we need a new identity
  1605         my_id = new_identity(address, fpr, my_user_id, NULL); 
  1606         if (status != PEP_STATUS_OK)
  1607             goto pep_free; 
  1608         my_id->me = true;
  1609         my_id->comm_type = PEP_ct_pEp;
  1610     }
  1611         
  1612     status = set_identity(session, my_id);
  1613     
  1614 pep_free:
  1615     free(my_id);
  1616     free(my_user_id);
  1617     return status;
  1618 }
  1619 
  1620 PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
  1621                              bool *has_private) {
  1622 
  1623     assert(session);
  1624     assert(fpr);
  1625     assert(has_private);
  1626     
  1627     if (!(session && fpr && has_private))
  1628         return PEP_ILLEGAL_VALUE;
  1629 
  1630     return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
  1631 }
  1632 
  1633 PEP_STATUS add_mistrusted_key(PEP_SESSION session, const char* fpr)
  1634 {
  1635     int result;
  1636 
  1637     assert(!EMPTYSTR(fpr));
  1638     
  1639     if (!(session) || EMPTYSTR(fpr))
  1640         return PEP_ILLEGAL_VALUE;
  1641 
  1642     sqlite3_reset(session->add_mistrusted_key);
  1643     sqlite3_bind_text(session->add_mistrusted_key, 1, fpr, -1,
  1644             SQLITE_STATIC);
  1645 
  1646     result = sqlite3_step(session->add_mistrusted_key);
  1647     sqlite3_reset(session->add_mistrusted_key);
  1648 
  1649     if (result != SQLITE_DONE)
  1650         return PEP_CANNOT_SET_PGP_KEYPAIR; // FIXME: Better status?
  1651 
  1652     return PEP_STATUS_OK;
  1653 }
  1654 
  1655 PEP_STATUS delete_mistrusted_key(PEP_SESSION session, const char* fpr)
  1656 {
  1657     int result;
  1658 
  1659     assert(!EMPTYSTR(fpr));
  1660     
  1661     if (!(session) || EMPTYSTR(fpr))
  1662         return PEP_ILLEGAL_VALUE;
  1663 
  1664     sqlite3_reset(session->delete_mistrusted_key);
  1665     sqlite3_bind_text(session->delete_mistrusted_key, 1, fpr, -1,
  1666             SQLITE_STATIC);
  1667 
  1668     result = sqlite3_step(session->delete_mistrusted_key);
  1669     sqlite3_reset(session->delete_mistrusted_key);
  1670 
  1671     if (result != SQLITE_DONE)
  1672         return PEP_UNKNOWN_ERROR; // FIXME: Better status?
  1673 
  1674     return PEP_STATUS_OK;
  1675 }
  1676 
  1677 PEP_STATUS is_mistrusted_key(PEP_SESSION session, const char* fpr,
  1678                              bool* mistrusted)
  1679 {
  1680     PEP_STATUS status = PEP_STATUS_OK;
  1681 
  1682     assert(session);
  1683     assert(!EMPTYSTR(fpr));
  1684 
  1685     if (!(session && fpr))
  1686         return PEP_ILLEGAL_VALUE;
  1687 
  1688     *mistrusted = false;
  1689 
  1690     sqlite3_reset(session->is_mistrusted_key);
  1691     sqlite3_bind_text(session->is_mistrusted_key, 1, fpr, -1, SQLITE_STATIC);
  1692 
  1693     int result;
  1694 
  1695     result = sqlite3_step(session->is_mistrusted_key);
  1696     switch (result) {
  1697     case SQLITE_ROW:
  1698         *mistrusted = sqlite3_column_int(session->is_mistrusted_key, 0);
  1699         status = PEP_STATUS_OK;
  1700         break;
  1701 
  1702     default:
  1703         status = PEP_UNKNOWN_ERROR;
  1704     }
  1705 
  1706     sqlite3_reset(session->is_mistrusted_key);
  1707     return status;
  1708 }