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