src/keymanagement.c
author Volker Birk <vb@pep-project.org>
Thu, 09 Apr 2020 18:00:52 +0200
branchsync
changeset 4548 147b5dae32b2
parent 4482 95bda81ed60f
child 4516 57acbb39869d
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         if (identity->major_ver == 0) {
   459             identity->major_ver = 2;
   460             identity->minor_ver = 0;
   461         }    
   462     }
   463 }
   464 
   465 
   466 static PEP_STATUS prepare_updated_identity(PEP_SESSION session,
   467                                                  pEp_identity* return_id,
   468                                                  pEp_identity* stored_ident,
   469                                                  bool store) {
   470     
   471     if (!session || !return_id || !stored_ident)
   472         return PEP_ILLEGAL_VALUE;
   473     
   474     PEP_STATUS status;
   475     
   476     bool is_identity_default, is_user_default, is_address_default;
   477     bool no_stored_default = EMPTYSTR(stored_ident->fpr);
   478     
   479     status = get_valid_pubkey(session, stored_ident,
   480                                 &is_identity_default,
   481                                 &is_user_default,
   482                                 &is_address_default,
   483                               false);
   484                                 
   485     if (status == PEP_STATUS_OK && stored_ident->fpr && *(stored_ident->fpr) != '\0') {
   486     // set identity comm_type from trust db (user_id, FPR)
   487         status = get_trust(session, stored_ident);
   488         if (status == PEP_CANNOT_FIND_IDENTITY || stored_ident->comm_type == PEP_ct_unknown) {
   489             // This is OK - there is no trust DB entry, but we
   490             // found a key. We won't store this, but we'll
   491             // use it.
   492             PEP_comm_type ct = PEP_ct_unknown;
   493             status = get_key_rating(session, stored_ident->fpr, &ct);
   494             stored_ident->comm_type = ct;
   495         }
   496     }
   497     else if (status != PEP_STATUS_OK) {
   498         free(stored_ident->fpr);
   499         stored_ident->fpr = NULL;
   500         stored_ident->comm_type = PEP_ct_key_not_found;        
   501     }
   502     else { // no key returned, but status ok?
   503         if (stored_ident->comm_type == PEP_ct_unknown)
   504             stored_ident->comm_type = PEP_ct_key_not_found;
   505     }
   506     free(return_id->fpr);
   507     return_id->fpr = NULL;
   508     if (status == PEP_STATUS_OK && !EMPTYSTR(stored_ident->fpr))
   509         return_id->fpr = strdup(stored_ident->fpr);
   510         
   511     return_id->comm_type = stored_ident->comm_type;
   512                     
   513     // We patch the DB with the input username, but if we didn't have
   514     // one, we pull it out of storage if available.
   515     if (!EMPTYSTR(stored_ident->username)) {
   516         if (!EMPTYSTR(return_id->username) && 
   517             (strcasecmp(return_id->username, return_id->address) == 0)) {
   518             free(return_id->username);
   519             return_id->username = NULL;
   520         }
   521         if (EMPTYSTR(return_id->username)) {
   522             free(return_id->username);
   523             return_id->username = strdup(stored_ident->username);
   524         }
   525     }
   526     else {
   527         if (EMPTYSTR(return_id->username))
   528             return_id->username = strdup(return_id->address);
   529     }
   530     
   531     return_id->me = stored_ident->me;
   532     
   533     return_id->major_ver = stored_ident->major_ver;
   534     return_id->minor_ver = stored_ident->minor_ver;
   535 
   536     // FIXME: Do we ALWAYS do this? We probably should...
   537     if (EMPTYSTR(return_id->user_id)) {
   538         free(return_id->user_id);
   539         return_id->user_id = strdup(stored_ident->user_id);
   540     } 
   541     
   542     adjust_pEp_trust_status(session, return_id);
   543    
   544     // Call set_identity() to store
   545     if ((is_identity_default || is_user_default) &&
   546          is_address_default) {                 
   547          // if we got an fpr which is default for either user
   548          // or identity AND is valid for this address, set in DB
   549          // as default
   550          status = set_identity(session, return_id);
   551     } 
   552     else if (no_stored_default && !EMPTYSTR(return_id->fpr) 
   553              && return_id->comm_type != PEP_ct_key_revoked
   554              && return_id->comm_type != PEP_ct_key_expired
   555              && return_id->comm_type != PEP_ct_key_expired_but_confirmed
   556              && return_id->comm_type != PEP_ct_mistrusted 
   557              && return_id->comm_type != PEP_ct_key_b0rken) { 
   558         // We would have stored this anyway for a first-time elected key. We just have an ident w/ no default already.
   559         status = set_identity(session, return_id);
   560     }
   561     else { // this is a key other than the default, but there IS a default (FIXME: fdik, do we really want behaviour below?)
   562         // Store without default fpr/ct, but return the fpr and ct 
   563         // for current use
   564         char* save_fpr = return_id->fpr;
   565         PEP_comm_type save_ct = return_id->comm_type;
   566         return_id->fpr = NULL;
   567         return_id->comm_type = PEP_ct_unknown;
   568         PEP_STATUS save_status = status;
   569         status = set_identity(session, return_id);
   570         if (save_status != PEP_STATUS_OK)
   571             status = save_status;
   572         return_id->fpr = save_fpr;
   573         return_id->comm_type = save_ct;
   574     }
   575     
   576     transfer_ident_lang_and_flags(return_id, stored_ident);
   577         
   578     if (return_id->comm_type == PEP_ct_unknown)
   579         return_id->comm_type = PEP_ct_key_not_found;
   580     
   581     return status;
   582 }
   583 
   584 DYNAMIC_API PEP_STATUS update_identity(
   585         PEP_SESSION session, pEp_identity * identity
   586     )
   587 {
   588     PEP_STATUS status;
   589 
   590     assert(session);
   591     assert(identity);
   592     assert(!EMPTYSTR(identity->address));
   593 
   594     if (!(session && identity && !EMPTYSTR(identity->address)))
   595         return PEP_ILLEGAL_VALUE;
   596 
   597     char* default_own_id = NULL;
   598     status = get_default_own_userid(session, &default_own_id);    
   599 
   600     bool is_own_user = identity->me;
   601 
   602     // Is this me, temporary or not? If so, BAIL.    
   603     if (!is_own_user) {
   604         if (default_own_id) {
   605             if (!EMPTYSTR(identity->user_id)) {
   606                 if (strcmp(default_own_id, identity->user_id) == 0) {
   607                     is_own_user = true;
   608                 }
   609                 else {
   610                     char* alias = NULL;
   611                     if (get_userid_alias_default(session, identity->user_id, &alias) == PEP_STATUS_OK) {
   612                         if (alias && strcmp(default_own_id, alias) == 0)
   613                             is_own_user = true;
   614                         free(alias);    
   615                     }
   616                 }
   617             }
   618             else {
   619                 // Check if own address. For now, this is a special case;
   620                 // we try to require apps to send in user_ids, but must prevent
   621                 // writes to an own identity from within THIS function
   622                 // NOTE: These semantics MAY CHANGE.
   623                 bool _own_addr = false;
   624                 is_own_address(session, identity->address, &_own_addr);
   625                 
   626                 // N.B. KB: I would prefer consistent semantics here - that is to say,
   627                 // we also set is_own_user here and force PEP_ILLEGAL_VALUE                
   628                 if (_own_addr) {
   629                     free(identity->user_id);
   630                     identity->user_id = strdup(default_own_id);
   631                     return _myself(session, identity, false, false, true);
   632                 }    
   633             }
   634         }
   635         // Otherwise, we don't even HAVE an own user yet, so we're ok.
   636     }    
   637     if (is_own_user)
   638     {
   639         free(default_own_id);
   640         return PEP_ILLEGAL_VALUE;
   641     }
   642 
   643     // We have, at least, an address.
   644     // Retrieve stored identity information!    
   645     pEp_identity* stored_ident = NULL;
   646 
   647     if (!EMPTYSTR(identity->user_id)) {            
   648         // (we're gonna update the trust/fpr anyway, so we use the no-fpr-from-trust-db variant)
   649         //      * do get_identity() to retrieve stored identity information
   650         status = get_identity_without_trust_check(session, identity->address, identity->user_id, &stored_ident);
   651 
   652         // Before we start - if there was no stored identity, we should check to make sure we don't
   653         // have a stored identity with a temporary user_id that differs from the input user_id. This
   654         // happens in multithreaded environments sometimes.
   655         if (!stored_ident) {
   656             identity_list* id_list = NULL;
   657             status = get_identities_by_address(session, identity->address, &id_list);
   658 
   659             if (id_list) {
   660                 identity_list* id_curr = id_list;
   661                 bool input_is_TOFU = (strstr(identity->user_id, "TOFU_") == identity->user_id);
   662                 while (id_curr) {
   663                     pEp_identity* this_id = id_curr->ident;
   664                     if (this_id) {
   665                         char* this_uid = this_id->user_id;
   666                         bool curr_is_TOFU = false;
   667                         // this_uid should never be NULL, as this is half of the ident
   668                         // DB primary key
   669                         assert(!EMPTYSTR(this_uid));
   670 
   671                         curr_is_TOFU = (strstr(this_uid, "TOFU_") == this_uid);
   672                         if (curr_is_TOFU && !input_is_TOFU) {
   673                             // FIXME: should we also be fixing pEp_own_userId in this
   674                             // function here?
   675                             
   676                             // if usernames match, we replace the userid.
   677                             // FIXME: do we need to create an address match function which
   678                             // matches the whole dot-and-case rigamarole from 
   679                             if (EMPTYSTR(this_id->username) ||
   680                                 strcasecmp(this_id->username, this_id->address) == 0 ||
   681                                 (identity->username && 
   682                                  strcasecmp(identity->username, 
   683                                             this_id->username) == 0)) {
   684                                 
   685                                 // Ok, we have a temp ID. We have to replace this
   686                                 // with the real ID.
   687                                 status = replace_userid(session, 
   688                                                         this_uid, 
   689                                                         identity->user_id);
   690                                 if (status != PEP_STATUS_OK) {
   691                                     free_identity_list(id_list);
   692                                     free(default_own_id);
   693                                     return status;
   694                                 }
   695                                     
   696                                 free(this_uid);
   697                                 this_uid = NULL;
   698                                 
   699                                 // Reflect the change we just made to the DB
   700                                 this_id->user_id = strdup(identity->user_id);
   701                                 stored_ident = this_id;
   702                                 // FIXME: free list.
   703                                 break;                                
   704                             }
   705                         }
   706                         else if (input_is_TOFU && !curr_is_TOFU) {
   707                             // Replace ruthlessly - this is NOT supposed to happen.
   708                             // BAD APP BEHAVIOUR.
   709                             free(identity->user_id);
   710                             identity->user_id = strdup(this_id->user_id);
   711                             stored_ident = this_id;
   712                             // FIXME: free list.
   713                             break;                                
   714                         }                            
   715                     }
   716                     id_curr = id_curr->next;
   717                 }
   718             }
   719         } 
   720                 
   721         if (status == PEP_STATUS_OK && stored_ident) { 
   722             //  * if identity available
   723             //      * patch it with username
   724             //          (note: this will happen when 
   725             //           setting automatically below...)
   726             //      * elect valid key for identity
   727             //    * if valid key exists
   728             //        * set return value's fpr
   729             status = prepare_updated_identity(session,
   730                                               identity,
   731                                               stored_ident, true);
   732         }
   733         //  * else (identity unavailable)
   734         else {
   735             status = PEP_STATUS_OK;
   736 
   737             // FIXME: We may need to roll this back.
   738             // FIXME: change docs if we don't
   739             //  if we only have user_id and address and identity not available
   740             //      * return error status (identity not found)
   741             if (EMPTYSTR(identity->username)) {
   742                 free(identity->username);
   743                 identity->username = strdup(identity->address);
   744             }
   745             
   746             // Otherwise, if we had user_id, address, and username:
   747             //    * create identity with user_id, address, username
   748             //      (this is the input id without the fpr + comm type!)
   749 
   750             elect_pubkey(session, identity, false);
   751                         
   752             //    * We've already checked and retrieved
   753             //      any applicable temporary identities above. If we're 
   754             //      here, none of them fit.
   755             //    * call set_identity() to store
   756             // FIXME: Do we set if we had to copy in the address?
   757             adjust_pEp_trust_status(session, identity);
   758             status = set_identity(session, identity);
   759             //  * Return: created identity
   760         }        
   761     }
   762     else if (!EMPTYSTR(identity->username)) {
   763         /*
   764          * Temporary identity information with username supplied
   765             * Input: address, username (no others)
   766          */
   767          
   768         //  * See if there is an own identity that uses this address. If so, we'll
   769         //    prefer that
   770         stored_ident = NULL;
   771         
   772         if (default_own_id) {
   773             status = get_identity(session, 
   774                                   identity->address, 
   775                                   default_own_id, 
   776                                   &stored_ident);
   777         }
   778         // If there isn't an own identity, search for a non-temp stored ident
   779         // with this address.                      
   780         if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) { 
   781  
   782             identity_list* id_list = NULL;
   783             status = get_identities_by_address(session, identity->address, &id_list);
   784 
   785             if (id_list) {
   786                 identity_list* id_curr = id_list;
   787                 while (id_curr) {
   788                     pEp_identity* this_id = id_curr->ident;
   789                     if (this_id) {
   790                         char* this_uid = this_id->user_id;
   791                         assert(!EMPTYSTR(this_uid));
   792                         // Should never be NULL - DB primary key
   793                         
   794                         if (strstr(this_uid, "TOFU_") != this_uid) {
   795                             // if usernames match, we replace the userid.
   796                             if (identity->username && 
   797                                 strcasecmp(identity->username, 
   798                                            this_id->username) == 0) {
   799                                 
   800                                 // Ok, we have a real ID. Copy it!
   801                                 identity->user_id = strdup(this_uid);
   802                                 assert(identity->user_id);
   803                                 if (!identity->user_id)
   804                                     goto enomem;
   805 
   806                                 stored_ident = this_id;
   807                                 
   808                                 break;                                
   809                             }                            
   810                         } 
   811                     }
   812                     id_curr = id_curr->next;
   813                 }
   814             }
   815         }
   816         
   817         if (stored_ident) {
   818             status = prepare_updated_identity(session,
   819                                               identity,
   820                                               stored_ident, true);
   821         }
   822         else {
   823             identity->user_id = calloc(1, strlen(identity->address) + 6);
   824             if (!identity->user_id)
   825                 goto enomem;
   826 
   827             snprintf(identity->user_id, strlen(identity->address) + 6,
   828                      "TOFU_%s", identity->address);        
   829 
   830             status = get_identity(session, 
   831                                   identity->address, 
   832                                   identity->user_id, 
   833                                   &stored_ident);
   834 
   835             if (status == PEP_STATUS_OK && stored_ident) {
   836                 status = prepare_updated_identity(session,
   837                                                   identity,
   838                                                   stored_ident, true);
   839             }
   840             else {
   841                          
   842                 //    * We've already checked and retrieved
   843                 //      any applicable temporary identities above. If we're 
   844                 //      here, none of them fit.
   845                 
   846                 status = elect_pubkey(session, identity, false);
   847                              
   848                 //    * call set_identity() to store
   849                 if (identity->fpr) {
   850                     // it is still possible we have DB information on this key. Better check.
   851                     status = get_trust(session, identity);
   852                     PEP_comm_type db_ct = identity->comm_type;
   853                     status = get_key_rating(session, identity->fpr, &identity->comm_type);
   854                     PEP_comm_type key_ct = identity->comm_type;
   855                                         
   856                     if (status == PEP_STATUS_OK) {
   857                         switch (key_ct) {
   858                             case PEP_ct_key_expired:
   859                                 if (db_ct == PEP_ct_key_expired_but_confirmed)
   860                                     identity->comm_type = db_ct;
   861                                 break;    
   862                             default:
   863                                 switch(db_ct) {
   864                                     case PEP_ct_key_expired_but_confirmed:
   865                                         if (key_ct >= PEP_ct_strong_but_unconfirmed)
   866                                             identity->comm_type |= PEP_ct_confirmed;
   867                                         break;
   868                                     case PEP_ct_mistrusted:
   869                                     case PEP_ct_compromised:
   870                                     case PEP_ct_key_b0rken:
   871                                         identity->comm_type = db_ct;
   872                                     default:
   873                                         break;
   874                                 }    
   875                                 break;
   876                         }
   877                     }
   878                 }
   879                 //    * call set_identity() to store
   880                 adjust_pEp_trust_status(session, identity);            
   881                 status = set_identity(session, identity);
   882             }
   883         }
   884     }
   885     else {
   886         /*
   887         * Input: address (no others)
   888          * Temporary identity information without username suplied
   889          */
   890          
   891         //  * Again, see if there is an own identity that uses this address. If so, we'll
   892         //    prefer that
   893         stored_ident = NULL;
   894          
   895         if (default_own_id) {
   896             status = get_identity(session, 
   897                                   identity->address, 
   898                                   default_own_id, 
   899                                   &stored_ident);
   900         }
   901         // If there isn't an own identity, search for a non-temp stored ident
   902         // with this address.                      
   903         if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) { 
   904  
   905             identity_list* id_list = NULL;
   906             //    * Search for identity with this address
   907             status = get_identities_by_address(session, identity->address, &id_list);
   908 
   909             // Results are ordered by timestamp descending, so this covers
   910             // both the one-result and multi-result cases
   911             if (id_list) {
   912                 if (stored_ident) // unlikely
   913                     free_identity(stored_ident);
   914                 stored_ident = id_list->ident;
   915             }
   916         }
   917         if (stored_ident)
   918             status = prepare_updated_identity(session, identity,
   919                                               stored_ident, false);
   920         else  {            
   921             // too little info. BUT. We see if we can find a key; if so, we create a
   922             // temp identity, look for a key, and store.
   923                          
   924             // create temporary identity, store it, and Return this
   925             // This means TOFU_ user_id
   926             identity->user_id = calloc(1, strlen(identity->address) + 6);
   927             if (!identity->user_id)
   928                 goto enomem;
   929 
   930             snprintf(identity->user_id, strlen(identity->address) + 6,
   931                      "TOFU_%s", identity->address);        
   932         
   933             identity->username = strdup(identity->address);
   934             if (!identity->address)
   935                 goto enomem;
   936             
   937             free(identity->fpr);
   938             identity->fpr = NULL;
   939             identity->comm_type = PEP_ct_unknown;
   940 
   941             status = elect_pubkey(session, identity, false);
   942                          
   943             if (identity->fpr)
   944                 status = get_key_rating(session, identity->fpr, &identity->comm_type);
   945         
   946             //    * call set_identity() to store
   947             adjust_pEp_trust_status(session, identity);            
   948             status = set_identity(session, identity);
   949 
   950         }
   951     }
   952     
   953     // FIXME: This is legacy. I presume it's a notification for the caller...
   954     // Revisit once I can talk to Volker
   955     if (identity->comm_type != PEP_ct_compromised &&
   956         identity->comm_type < PEP_ct_strong_but_unconfirmed)
   957         if (session->examine_identity)
   958             session->examine_identity(identity, session->examine_management);
   959 
   960     goto pEp_free;
   961 
   962 enomem:
   963     status = PEP_OUT_OF_MEMORY;
   964 
   965 pEp_free:
   966     free(default_own_id);
   967     free_identity(stored_ident);
   968     return status;
   969 }
   970 
   971 PEP_STATUS elect_ownkey(
   972         PEP_SESSION session, pEp_identity * identity
   973     )
   974 {
   975     PEP_STATUS status;
   976     stringlist_t *keylist = NULL;
   977 
   978     free(identity->fpr);
   979     identity->fpr = NULL;
   980 
   981     status = find_private_keys(session, identity->address, &keylist);
   982     assert(status != PEP_OUT_OF_MEMORY);
   983     if (status == PEP_OUT_OF_MEMORY)
   984         return PEP_OUT_OF_MEMORY;
   985     
   986     if (keylist != NULL && keylist->value != NULL)
   987     {
   988         char *_fpr = NULL;
   989         identity->comm_type = PEP_ct_unknown;
   990 
   991         stringlist_t *_keylist;
   992         for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
   993             bool is_own = false;
   994             
   995             status = own_key_is_listed(session, _keylist->value, &is_own);
   996             assert(status == PEP_STATUS_OK);
   997             if (status != PEP_STATUS_OK) {
   998                 free_stringlist(keylist);
   999                 return status;
  1000             }
  1001             
  1002             if (is_own)
  1003             {
  1004                 PEP_comm_type _comm_type_key;
  1005                 
  1006                 status = get_key_rating(session, _keylist->value, &_comm_type_key);
  1007                 assert(status != PEP_OUT_OF_MEMORY);
  1008                 if (status == PEP_OUT_OF_MEMORY) {
  1009                     free_stringlist(keylist);
  1010                     return PEP_OUT_OF_MEMORY;
  1011                 }
  1012                 
  1013                 if (_comm_type_key != PEP_ct_compromised &&
  1014                     _comm_type_key != PEP_ct_unknown)
  1015                 {
  1016                     if (identity->comm_type == PEP_ct_unknown ||
  1017                         _comm_type_key > identity->comm_type)
  1018                     {
  1019                         identity->comm_type = _comm_type_key;
  1020                         _fpr = _keylist->value;
  1021                     }
  1022                 }
  1023             }
  1024         }
  1025         
  1026         if (_fpr)
  1027         {
  1028             identity->fpr = strdup(_fpr);
  1029             assert(identity->fpr);
  1030             if (identity->fpr == NULL)
  1031             {
  1032                 free_stringlist(keylist);
  1033                 return PEP_OUT_OF_MEMORY;
  1034             }
  1035         }
  1036         free_stringlist(keylist);
  1037     }
  1038     return PEP_STATUS_OK;
  1039 }
  1040 
  1041 PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
  1042                                 bool* is_usable) {
  1043     
  1044     bool has_private = false;
  1045     PEP_STATUS status = contains_priv_key(session, fpr, &has_private);
  1046     
  1047     *is_usable = has_private;
  1048     
  1049     return status;
  1050 }
  1051 
  1052 PEP_STATUS _myself(PEP_SESSION session, 
  1053                    pEp_identity * identity, 
  1054                    bool do_keygen, 
  1055                    bool ignore_flags,
  1056                    bool read_only)
  1057 {
  1058 
  1059     PEP_STATUS status;
  1060 
  1061     assert(session);
  1062     assert(identity);
  1063     assert(!EMPTYSTR(identity->address));
  1064     assert(!EMPTYSTR(identity->user_id));
  1065 
  1066     if (!session || !identity || EMPTYSTR(identity->address) ||
  1067         EMPTYSTR(identity->user_id))
  1068         return PEP_ILLEGAL_VALUE;
  1069 
  1070     pEp_identity *stored_identity = NULL;
  1071     char* revoked_fpr = NULL; 
  1072     bool valid_key_found = false;
  1073         
  1074     char* default_own_id = NULL;
  1075     status = get_default_own_userid(session, &default_own_id);
  1076     
  1077     // Deal with non-default user_ids.
  1078     // FIXME: if non-default and read-only, reject totally?
  1079     if (default_own_id && strcmp(default_own_id, identity->user_id) != 0) {
  1080         if (read_only) {
  1081             free(identity->user_id);
  1082             identity->user_id = strdup(default_own_id);
  1083             assert(identity->user_id);
  1084             if (!identity->user_id)
  1085                 return PEP_OUT_OF_MEMORY;
  1086         }
  1087         else {
  1088             status = set_userid_alias(session, default_own_id, identity->user_id);
  1089             // Do we want this to be fatal? For now, we'll do it...
  1090             if (status != PEP_STATUS_OK)
  1091                 goto pEp_free;
  1092                 
  1093             free(identity->user_id);
  1094             identity->user_id = strdup(default_own_id);
  1095             assert(identity->user_id);
  1096             if (identity->user_id == NULL) {
  1097                 status = PEP_OUT_OF_MEMORY;
  1098                 goto pEp_free;
  1099             }
  1100         }
  1101     }
  1102 
  1103     // NOTE: IF WE DON'T YET HAVE AN OWN_ID, WE IGNORE REFERENCES TO THIS ADDRESS IN THE
  1104     // DB (WHICH MAY HAVE BEEN SET BEFORE MYSELF WAS CALLED BY RECEIVING AN EMAIL FROM
  1105     // THIS ADDRESS), AS IT IS NOT AN OWN_IDENTITY AND HAS NO INFORMATION WE NEED OR WHAT TO
  1106     // SET FOR MYSELF
  1107     
  1108     // Ok, so now, set up the own_identity:
  1109     identity->comm_type = PEP_ct_pEp;
  1110     identity->me = true;
  1111     if(ignore_flags)
  1112         identity->flags = 0;
  1113     
  1114     // Let's see if we have an identity record in the DB for 
  1115     // this user_id + address
  1116 //    DEBUG_LOG("myself", "debug", identity->address);
  1117  
  1118     // This will grab the actual flags from the db
  1119     status = get_identity(session,
  1120                           identity->address,
  1121                           identity->user_id,
  1122                           &stored_identity);
  1123 
  1124     assert(status != PEP_OUT_OF_MEMORY);
  1125     if (status == PEP_OUT_OF_MEMORY) {
  1126         status = PEP_OUT_OF_MEMORY;
  1127         goto pEp_free;
  1128     }
  1129 
  1130     // Set usernames - priority is input username > stored name > address
  1131     // If there's an input username, we always patch the username with that
  1132     // input.
  1133     if (EMPTYSTR(identity->username) || read_only) {
  1134         bool stored_uname = (stored_identity && !EMPTYSTR(stored_identity->username));
  1135         char* uname = (stored_uname ? stored_identity->username : identity->address);
  1136         if (uname) {
  1137             free(identity->username);
  1138             identity->username = strdup(uname);
  1139             assert(identity->username);
  1140             if (identity->username == NULL) {
  1141                 status = PEP_OUT_OF_MEMORY;
  1142                 goto pEp_free;
  1143             }
  1144         }
  1145     }
  1146 
  1147     // ignore input fpr
  1148 
  1149     if (identity->fpr) {
  1150         free(identity->fpr);
  1151         identity->fpr = NULL;
  1152     }
  1153 
  1154     // check stored identity
  1155     if (stored_identity) {
  1156         if (!EMPTYSTR(stored_identity->fpr)) {
  1157             // Fall back / retrieve
  1158             status = validate_fpr(session, stored_identity, false, true);
  1159             if (status == PEP_OUT_OF_MEMORY)
  1160                 goto pEp_free;
  1161             if (status == PEP_STATUS_OK) {
  1162                 if (stored_identity->comm_type >= PEP_ct_strong_but_unconfirmed) {
  1163                     identity->fpr = strdup(stored_identity->fpr);
  1164                     assert(identity->fpr);
  1165                     if (!identity->fpr) {
  1166                         status = PEP_OUT_OF_MEMORY;
  1167                         goto pEp_free;
  1168                     }
  1169                     valid_key_found = true;            
  1170                 }
  1171                 else {
  1172                     bool revoked = false;
  1173                     status = key_revoked(session, stored_identity->fpr, &revoked);
  1174                     if (status)
  1175                         goto pEp_free;
  1176                     if (revoked) {
  1177                         revoked_fpr = strdup(stored_identity->fpr);
  1178                         assert(revoked_fpr);
  1179                         if (!revoked_fpr) {
  1180                             status = PEP_OUT_OF_MEMORY;
  1181                             goto pEp_free;
  1182                         }
  1183                     }
  1184                 }
  1185             }
  1186         }
  1187         // reconcile language, flags
  1188         transfer_ident_lang_and_flags(identity, stored_identity);
  1189     }
  1190     
  1191     // Nothing left to do but generate a key
  1192     if (!valid_key_found) {
  1193         if (!do_keygen || read_only)
  1194             status = PEP_GET_KEY_FAILED;
  1195         else {
  1196 // /            DEBUG_LOG("Generating key pair", "debug", identity->address);
  1197 
  1198             free(identity->fpr);
  1199             identity->fpr = NULL;
  1200             status = generate_keypair(session, identity);
  1201             assert(status != PEP_OUT_OF_MEMORY);
  1202 
  1203             if (status != PEP_STATUS_OK) {
  1204                 char buf[11];
  1205                 snprintf(buf, 11, "%d", status); // uh, this is kludgey. FIXME
  1206 //                DEBUG_LOG("Generating key pair failed", "debug", buf);
  1207             }        
  1208             else {
  1209                 valid_key_found = true;
  1210                 if (revoked_fpr) {
  1211                     status = set_revoked(session, revoked_fpr,
  1212                                          stored_identity->fpr, time(NULL));
  1213                     assert(status == PEP_STATUS_OK);                     
  1214                 }
  1215             }
  1216         }
  1217     }
  1218 
  1219     if (valid_key_found) {
  1220         identity->comm_type = PEP_ct_pEp;
  1221         status = PEP_STATUS_OK;
  1222     }
  1223     else {
  1224         free(identity->fpr);
  1225         identity->fpr = NULL;
  1226         identity->comm_type = PEP_ct_unknown;
  1227     }
  1228     
  1229     unsigned int major_ver = 0;
  1230     unsigned int minor_ver = 0;
  1231     pEp_version_major_minor(PEP_VERSION, &major_ver, &minor_ver);
  1232     identity->major_ver = major_ver;
  1233     identity->minor_ver = minor_ver;
  1234     
  1235     // We want to set an identity in the DB even if a key isn't found, but we have to preserve the status if
  1236     // it's NOT ok
  1237     if (!read_only) {
  1238         PEP_STATUS set_id_status = set_identity(session, identity);
  1239         if (set_id_status == PEP_STATUS_OK)
  1240             set_id_status = set_as_pEp_user(session, identity);
  1241 
  1242         status = (status == PEP_STATUS_OK ? set_id_status : status);
  1243     }
  1244     
  1245 pEp_free:    
  1246     free(default_own_id);
  1247     free(revoked_fpr);                     
  1248     free_identity(stored_identity);
  1249     return status;
  1250 }
  1251 
  1252 DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
  1253 {
  1254     return _myself(session, identity, true, false, false);
  1255 }
  1256 
  1257 DYNAMIC_API PEP_STATUS register_examine_function(
  1258         PEP_SESSION session, 
  1259         examine_identity_t examine_identity,
  1260         void *management
  1261     )
  1262 {
  1263     assert(session);
  1264     if (!session)
  1265         return PEP_ILLEGAL_VALUE;
  1266 
  1267     session->examine_management = management;
  1268     session->examine_identity = examine_identity;
  1269 
  1270     return PEP_STATUS_OK;
  1271 }
  1272 
  1273 DYNAMIC_API PEP_STATUS do_keymanagement(
  1274         retrieve_next_identity_t retrieve_next_identity,
  1275         void *management
  1276     )
  1277 {
  1278     PEP_SESSION session;
  1279     pEp_identity *identity;
  1280     PEP_STATUS status = init(&session, NULL, NULL);
  1281     assert(!status);
  1282     if (status)
  1283         return status;
  1284 
  1285     assert(session && retrieve_next_identity);
  1286     if (!(session && retrieve_next_identity))
  1287         return PEP_ILLEGAL_VALUE;
  1288 
  1289     log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
  1290 
  1291     while ((identity = retrieve_next_identity(management))) 
  1292     {
  1293         assert(identity->address);
  1294         if(identity->address)
  1295         {
  1296             DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
  1297 
  1298             if (identity->me) {
  1299                 status = myself(session, identity);
  1300             } else {
  1301                 status = recv_key(session, identity->address);
  1302             }
  1303 
  1304             assert(status != PEP_OUT_OF_MEMORY);
  1305             if(status == PEP_OUT_OF_MEMORY)
  1306                 return PEP_OUT_OF_MEMORY;
  1307         }
  1308         free_identity(identity);
  1309     }
  1310 
  1311     log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
  1312     release(session);
  1313     return PEP_STATUS_OK;
  1314 }
  1315 
  1316 DYNAMIC_API PEP_STATUS key_mistrusted(
  1317         PEP_SESSION session,
  1318         pEp_identity *ident
  1319     )
  1320 {
  1321     PEP_STATUS status = PEP_STATUS_OK;
  1322 
  1323     assert(session);
  1324     assert(ident);
  1325     assert(!EMPTYSTR(ident->fpr));
  1326 
  1327     if (!(session && ident && ident->fpr))
  1328         return PEP_ILLEGAL_VALUE;
  1329 
  1330     bool has_private = false;
  1331     
  1332     status = contains_priv_key(session, ident->fpr, &has_private);        
  1333 
  1334     if (status != PEP_STATUS_OK && status != PEP_KEY_NOT_FOUND)
  1335         return status;
  1336         
  1337     // See if key is revoked already
  1338     if (has_private) {
  1339         bool revoked = false;
  1340         status = key_revoked(session, ident->fpr, &revoked);
  1341 
  1342         if (!revoked)
  1343             revoke_key(session, ident->fpr, NULL);
  1344     }
  1345     else {
  1346         if (ident->fpr) {
  1347             // Make sure there was a default in the DB for this identity;
  1348             // if not, set one, even though we're going to mistrust this. Otherwise,
  1349             // cannot reset.
  1350             pEp_identity* stored_ident = NULL;
  1351             get_identity(session, ident->address, ident->user_id, &stored_ident);
  1352             bool set_in_db = true;
  1353             if (!stored_ident)
  1354                 stored_ident = identity_dup(ident);
  1355             else if (!stored_ident->fpr)
  1356                 stored_ident->fpr = strdup(ident->fpr);
  1357             else
  1358                 set_in_db = false;
  1359                         
  1360             if (set_in_db)
  1361                 status = set_identity(session, stored_ident);    
  1362             
  1363             free_identity(stored_ident);
  1364             if (status != PEP_STATUS_OK)
  1365                 return status;
  1366         }
  1367     }            
  1368             
  1369     // double-check to be sure key is even in the DB
  1370     if (ident->fpr)
  1371         status = set_pgp_keypair(session, ident->fpr);
  1372 
  1373     // We set this temporarily but will grab it back from the cache afterwards
  1374     ident->comm_type = PEP_ct_mistrusted;
  1375     status = set_trust(session, ident);
  1376     
  1377     if (status == PEP_STATUS_OK)
  1378         // cascade that mistrust for anyone using this key
  1379         status = mark_as_compromised(session, ident->fpr);
  1380     if (status == PEP_STATUS_OK)
  1381         status = add_mistrusted_key(session, ident->fpr);
  1382             
  1383     return status;
  1384 }
  1385 
  1386 DYNAMIC_API PEP_STATUS key_reset_trust(
  1387         PEP_SESSION session,
  1388         pEp_identity *ident
  1389     )
  1390 {
  1391     PEP_STATUS status = PEP_STATUS_OK;
  1392 
  1393     assert(session);
  1394     assert(ident);
  1395     assert(!EMPTYSTR(ident->fpr));
  1396     assert(!EMPTYSTR(ident->address));
  1397     assert(!EMPTYSTR(ident->user_id));
  1398 
  1399     if (!(session && ident && ident->fpr && ident->fpr[0] != '\0' && ident->address &&
  1400             ident->user_id))
  1401         return PEP_ILLEGAL_VALUE;
  1402 
  1403     // we do not change the input struct at ALL.
  1404     pEp_identity* input_copy = identity_dup(ident);
  1405     
  1406     pEp_identity* tmp_ident = NULL;
  1407     
  1408     status = get_trust(session, input_copy);
  1409     
  1410     if (status != PEP_STATUS_OK)
  1411         goto pEp_free;
  1412         
  1413     PEP_comm_type new_trust = PEP_ct_unknown;
  1414     status = get_key_rating(session, ident->fpr, &new_trust);
  1415     if (status != PEP_STATUS_OK)
  1416         goto pEp_free;
  1417 
  1418     bool pEp_user = false;
  1419     
  1420     status = is_pEp_user(session, ident, &pEp_user);
  1421     
  1422     if (pEp_user && new_trust >= PEP_ct_unconfirmed_encryption)
  1423         input_copy->comm_type = PEP_ct_pEp_unconfirmed;
  1424     else
  1425         input_copy->comm_type = new_trust;
  1426         
  1427     status = set_trust(session, input_copy);
  1428     
  1429     if (status != PEP_STATUS_OK)
  1430         goto pEp_free;
  1431 
  1432     bool mistrusted_key = false;
  1433         
  1434     status = is_mistrusted_key(session, ident->fpr, &mistrusted_key);
  1435 
  1436     if (status != PEP_STATUS_OK)
  1437         goto pEp_free;
  1438     
  1439     if (mistrusted_key)
  1440         status = delete_mistrusted_key(session, ident->fpr);
  1441 
  1442     if (status != PEP_STATUS_OK)
  1443         goto pEp_free;
  1444         
  1445     tmp_ident = new_identity(ident->address, NULL, ident->user_id, NULL);
  1446 
  1447     if (!tmp_ident)
  1448         return PEP_OUT_OF_MEMORY;
  1449     
  1450     if (is_me(session, tmp_ident))
  1451         status = myself(session, tmp_ident);
  1452     else
  1453         status = update_identity(session, tmp_ident);
  1454     
  1455     if (status != PEP_STATUS_OK)
  1456         goto pEp_free;
  1457     
  1458     // remove as default if necessary
  1459     if (!EMPTYSTR(tmp_ident->fpr) && strcmp(tmp_ident->fpr, ident->fpr) == 0) {
  1460         free(tmp_ident->fpr);
  1461         tmp_ident->fpr = NULL;
  1462         tmp_ident->comm_type = PEP_ct_unknown;
  1463         status = set_identity(session, tmp_ident);
  1464         if (status != PEP_STATUS_OK)
  1465             goto pEp_free;
  1466     }
  1467     
  1468     char* user_default = NULL;
  1469     get_main_user_fpr(session, tmp_ident->user_id, &user_default);
  1470     
  1471     if (!EMPTYSTR(user_default)) {
  1472         if (strcmp(user_default, ident->fpr) == 0)
  1473             status = refresh_userid_default_key(session, ident->user_id);
  1474         if (status != PEP_STATUS_OK)
  1475             goto pEp_free;    
  1476     }
  1477             
  1478 pEp_free:
  1479     free_identity(tmp_ident);
  1480     free_identity(input_copy);
  1481     return status;
  1482 }
  1483 
  1484 DYNAMIC_API PEP_STATUS trust_personal_key(
  1485         PEP_SESSION session,
  1486         pEp_identity *ident
  1487     )
  1488 {
  1489     PEP_STATUS status = PEP_STATUS_OK;
  1490 
  1491     assert(session);
  1492     assert(ident);
  1493     assert(!EMPTYSTR(ident->address));
  1494     assert(!EMPTYSTR(ident->user_id));
  1495     assert(!EMPTYSTR(ident->fpr));
  1496 
  1497     if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
  1498             EMPTYSTR(ident->fpr))
  1499         return PEP_ILLEGAL_VALUE;
  1500 
  1501     if (is_me(session, ident))
  1502         return PEP_ILLEGAL_VALUE;
  1503         
  1504     char* ident_default_fpr = NULL;
  1505 
  1506     // Before we do anything, be sure the input fpr is even eligible to be trusted
  1507     PEP_comm_type input_default_ct = PEP_ct_unknown;
  1508     status = get_key_rating(session, ident->fpr, &input_default_ct);
  1509     if (input_default_ct < PEP_ct_strong_but_unconfirmed)
  1510         return PEP_KEY_UNSUITABLE;
  1511 
  1512     status = set_pgp_keypair(session, ident->fpr);
  1513     if (status != PEP_STATUS_OK)
  1514         return status;
  1515 
  1516     pEp_identity* ident_copy = identity_dup(ident);
  1517     char* cached_fpr = NULL;
  1518 
  1519     // for setting up a temp trusted identity for the input fpr
  1520     pEp_identity* tmp_id = NULL;
  1521 
  1522     // For later, in case we need to check the user default key
  1523     pEp_identity* tmp_user_ident = NULL;
  1524         
  1525     // Save the input fpr, which we already tested as non-NULL
  1526     cached_fpr = strdup(ident->fpr);
  1527 
  1528     // Set up a temp trusted identity for the input fpr without a comm type;
  1529     tmp_id = new_identity(ident->address, ident->fpr, ident->user_id, NULL);
  1530     
  1531     status = validate_fpr(session, tmp_id, false, true);
  1532         
  1533     if (status == PEP_STATUS_OK) {
  1534         // Validate fpr gets trust DB or, when that fails, key comm type. we checked
  1535         // above that the key was ok. (not revoked or expired), but we want the max.
  1536         tmp_id->comm_type = _MAX(tmp_id->comm_type, input_default_ct) | PEP_ct_confirmed;
  1537 
  1538         // Get the default identity without setting the fpr                                       
  1539         status = update_identity(session, ident_copy);
  1540             
  1541         ident_default_fpr = (EMPTYSTR(ident_copy->fpr) ? NULL : strdup(ident_copy->fpr));
  1542 
  1543         if (status == PEP_STATUS_OK) {
  1544             bool trusted_default = false;
  1545 
  1546             // If there's no default, or the default is different from the input...
  1547             if (EMPTYSTR(ident_default_fpr) || strcmp(cached_fpr, ident_default_fpr) != 0) {
  1548                 
  1549                 // If the default fpr (if there is one) is trusted and key is strong enough,
  1550                 // don't replace, we just set the trusted bit on this key for this user_id...
  1551                 // (If there's no default fpr, this won't be true anyway.)
  1552                 if ((ident_copy->comm_type >= PEP_ct_strong_but_unconfirmed && 
  1553                     (ident_copy->comm_type & PEP_ct_confirmed))) {                        
  1554 
  1555                     trusted_default = true;
  1556                                     
  1557                     status = set_trust(session, tmp_id);
  1558                     input_default_ct = tmp_id->comm_type;                    
  1559                 }
  1560                 else {
  1561                     free(ident_copy->fpr);
  1562                     ident_copy->fpr = strdup(cached_fpr);
  1563                     ident_copy->comm_type = tmp_id->comm_type;
  1564                     status = set_identity(session, ident_copy); // replace identity default
  1565                     if (status == PEP_STATUS_OK) {
  1566                         if ((ident_copy->comm_type | PEP_ct_confirmed) == PEP_ct_pEp)
  1567                             status = set_as_pEp_user(session, ident_copy);
  1568                     }            
  1569                 }
  1570             }
  1571             else { // we're setting this on the default fpr
  1572                 ident->comm_type = tmp_id->comm_type;
  1573                 status = set_identity(session, ident);
  1574                 trusted_default = true;
  1575             }
  1576             if (status == PEP_STATUS_OK && !trusted_default) {
  1577                 // Ok, there wasn't a trusted default, so we replaced. Thus, we also
  1578                 // make sure there's a trusted default on the user_id. If there
  1579                 // is not, we make this the default.
  1580                 char* user_default = NULL;
  1581                 status = get_main_user_fpr(session, ident->user_id, &user_default);
  1582             
  1583                 if (status == PEP_STATUS_OK && user_default) {
  1584                     tmp_user_ident = new_identity(ident->address, 
  1585                                                   user_default, 
  1586                                                   ident->user_id, 
  1587                                                   NULL);
  1588                     if (!tmp_user_ident)
  1589                         status = PEP_OUT_OF_MEMORY;
  1590                     else {
  1591                         status = validate_fpr(session, tmp_user_ident, false, true);
  1592                         
  1593                         if (status != PEP_STATUS_OK ||
  1594                             tmp_user_ident->comm_type < PEP_ct_strong_but_unconfirmed ||
  1595                             !(tmp_user_ident->comm_type & PEP_ct_confirmed)) 
  1596                         {
  1597                             char* trusted_fpr = (trusted_default ? ident_default_fpr : cached_fpr);
  1598                             status = replace_main_user_fpr(session, ident->user_id, trusted_fpr);
  1599                         } 
  1600                     }
  1601                 }
  1602             }
  1603         }
  1604     }    
  1605 
  1606     free(ident_default_fpr);
  1607     free(cached_fpr);
  1608     free_identity(tmp_id);
  1609     free_identity(ident_copy);
  1610     free_identity(tmp_user_ident);
  1611     return status;
  1612 }
  1613 
  1614 DYNAMIC_API PEP_STATUS trust_own_key(
  1615         PEP_SESSION session,
  1616         pEp_identity* ident
  1617     ) 
  1618 {
  1619     assert(session);
  1620     assert(ident);
  1621     assert(!EMPTYSTR(ident->address));
  1622     assert(!EMPTYSTR(ident->user_id));
  1623     assert(!EMPTYSTR(ident->fpr));
  1624     
  1625     if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
  1626             EMPTYSTR(ident->fpr))
  1627         return PEP_ILLEGAL_VALUE;
  1628 
  1629     if (!is_me(session, ident))
  1630         return PEP_ILLEGAL_VALUE;
  1631 
  1632     // don't check blacklist or require a private key
  1633     PEP_STATUS status = validate_fpr(session, ident, false, false);
  1634 
  1635     if (status != PEP_STATUS_OK)
  1636         return status;
  1637 
  1638     status = set_pgp_keypair(session, ident->fpr);
  1639     if (status != PEP_STATUS_OK)
  1640         return status;
  1641             
  1642     if (ident->comm_type < PEP_ct_strong_but_unconfirmed)
  1643         return PEP_KEY_UNSUITABLE;
  1644 
  1645     ident->comm_type |= PEP_ct_confirmed;
  1646     
  1647     status = set_trust(session, ident);
  1648 
  1649     return status;
  1650 }
  1651 
  1652 
  1653 DYNAMIC_API PEP_STATUS own_key_is_listed(
  1654         PEP_SESSION session,
  1655         const char *fpr,
  1656         bool *listed
  1657     )
  1658 {
  1659     PEP_STATUS status = PEP_STATUS_OK;
  1660     int count;
  1661     
  1662     assert(session && fpr && fpr[0] && listed);
  1663     
  1664     if (!(session && fpr && fpr[0] && listed))
  1665         return PEP_ILLEGAL_VALUE;
  1666     
  1667     *listed = false;
  1668     
  1669     sqlite3_reset(session->own_key_is_listed);
  1670     sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
  1671     
  1672     int result;
  1673     
  1674     result = sqlite3_step(session->own_key_is_listed);
  1675     switch (result) {
  1676         case SQLITE_ROW:
  1677             count = sqlite3_column_int(session->own_key_is_listed, 0);
  1678             *listed = count > 0;
  1679             status = PEP_STATUS_OK;
  1680             break;
  1681             
  1682         default:
  1683             status = PEP_UNKNOWN_ERROR;
  1684     }
  1685     
  1686     sqlite3_reset(session->own_key_is_listed);
  1687     return status;
  1688 }
  1689 
  1690 PEP_STATUS _own_identities_retrieve(
  1691         PEP_SESSION session,
  1692         identity_list **own_identities,
  1693         identity_flags_t excluded_flags
  1694       )
  1695 {
  1696     PEP_STATUS status = PEP_STATUS_OK;
  1697     
  1698     assert(session && own_identities);
  1699     if (!(session && own_identities))
  1700         return PEP_ILLEGAL_VALUE;
  1701     
  1702     *own_identities = NULL;
  1703     identity_list *_own_identities = new_identity_list(NULL);
  1704     if (_own_identities == NULL)
  1705         goto enomem;
  1706     
  1707     sqlite3_reset(session->own_identities_retrieve);
  1708     
  1709     int result;
  1710     // address, fpr, username, user_id, comm_type, lang, flags
  1711     const char *address = NULL;
  1712     const char *fpr = NULL;
  1713     const char *username = NULL;
  1714     const char *user_id = NULL;
  1715     PEP_comm_type comm_type = PEP_ct_unknown;
  1716     const char *lang = NULL;
  1717     unsigned int flags = 0;
  1718     
  1719     identity_list *_bl = _own_identities;
  1720 
  1721     sqlite3_bind_int(session->own_identities_retrieve, 1, excluded_flags);
  1722 
  1723     do {
  1724         result = sqlite3_step(session->own_identities_retrieve);
  1725         switch (result) {
  1726             case SQLITE_ROW:
  1727                 address = (const char *)
  1728                     sqlite3_column_text(session->own_identities_retrieve, 0);
  1729                 fpr = (const char *)
  1730                     sqlite3_column_text(session->own_identities_retrieve, 1);
  1731                 user_id = (const char *)
  1732                     sqlite3_column_text(session->own_identities_retrieve, 2);
  1733                 username = (const char *)
  1734                     sqlite3_column_text(session->own_identities_retrieve, 3);
  1735                 comm_type = PEP_ct_pEp;
  1736                 lang = (const char *)
  1737                     sqlite3_column_text(session->own_identities_retrieve, 4);
  1738                 flags = (unsigned int)
  1739                     sqlite3_column_int(session->own_identities_retrieve, 5);
  1740 
  1741                 pEp_identity *ident = new_identity(address, fpr, user_id, username);
  1742                 if (!ident)
  1743                     goto enomem;
  1744                 ident->comm_type = comm_type;
  1745                 if (lang && lang[0]) {
  1746                     ident->lang[0] = lang[0];
  1747                     ident->lang[1] = lang[1];
  1748                     ident->lang[2] = 0;
  1749                 }
  1750                 ident->me = true;
  1751                 ident->flags = flags;
  1752 
  1753                 _bl = identity_list_add(_bl, ident);
  1754                 if (_bl == NULL) {
  1755                     free_identity(ident);
  1756                     goto enomem;
  1757                 }
  1758                 
  1759                 break;
  1760                 
  1761             case SQLITE_DONE:
  1762                 break;
  1763                 
  1764             default:
  1765                 status = PEP_UNKNOWN_ERROR;
  1766                 result = SQLITE_DONE;
  1767         }
  1768     } while (result != SQLITE_DONE);
  1769     
  1770     sqlite3_reset(session->own_identities_retrieve);
  1771     if (status == PEP_STATUS_OK)
  1772         *own_identities = _own_identities;
  1773     else
  1774         free_identity_list(_own_identities);
  1775     
  1776     goto the_end;
  1777     
  1778 enomem:
  1779     free_identity_list(_own_identities);
  1780     status = PEP_OUT_OF_MEMORY;
  1781     
  1782 the_end:
  1783     return status;
  1784 }
  1785 
  1786 DYNAMIC_API PEP_STATUS own_identities_retrieve(
  1787         PEP_SESSION session,
  1788         identity_list **own_identities
  1789       )
  1790 {
  1791     return _own_identities_retrieve(session, own_identities, 0);
  1792 }
  1793 
  1794 PEP_STATUS _own_keys_retrieve(
  1795         PEP_SESSION session,
  1796         stringlist_t **keylist,
  1797         identity_flags_t excluded_flags,
  1798         bool private_only
  1799       )
  1800 {
  1801     PEP_STATUS status = PEP_STATUS_OK;
  1802     
  1803     assert(session && keylist);
  1804     if (!(session && keylist))
  1805         return PEP_ILLEGAL_VALUE;
  1806     
  1807     *keylist = NULL;
  1808     stringlist_t *_keylist = NULL;
  1809     
  1810     sqlite3_reset(session->own_keys_retrieve);
  1811     
  1812     int result;
  1813     
  1814     stringlist_t *_bl = _keylist;
  1815     sqlite3_bind_int(session->own_keys_retrieve, 1, excluded_flags);
  1816 
  1817     do {        
  1818         result = sqlite3_step(session->own_keys_retrieve);
  1819         switch (result) {
  1820             case SQLITE_ROW:
  1821                 _bl = stringlist_add(_bl, (const char *)
  1822                         sqlite3_column_text(session->own_keys_retrieve, 0));
  1823                 if (_bl == NULL)
  1824                     goto enomem;
  1825                 if (_keylist == NULL)
  1826                     _keylist = _bl;
  1827                 break;
  1828                 
  1829             case SQLITE_DONE:
  1830                 break;
  1831                 
  1832             default:
  1833                 status = PEP_UNKNOWN_ERROR;
  1834                 result = SQLITE_DONE;
  1835         }
  1836     } while (result != SQLITE_DONE);
  1837     
  1838     sqlite3_reset(session->own_keys_retrieve);
  1839     if (status == PEP_STATUS_OK) {
  1840         dedup_stringlist(_keylist);
  1841         if (private_only) {
  1842             stringlist_t* _kl = _keylist;
  1843             stringlist_t* _kl_prev = NULL;
  1844             while (_kl) {
  1845                 bool has_private = false;
  1846                 contains_priv_key(session, _kl->value, &has_private);
  1847                 if (!has_private) {
  1848                     stringlist_t* _kl_tmp = _kl;
  1849                     if (_kl_prev)
  1850                         _kl_prev->next = _kl->next;
  1851                     else 
  1852                         _keylist = _kl->next;
  1853                         
  1854                     _kl = _kl->next;
  1855                     
  1856                     _kl_tmp->next = NULL;
  1857                     free_stringlist(_kl_tmp);
  1858                     continue;
  1859                 }
  1860                 _kl_prev = _kl;
  1861                 _kl = _kl->next;
  1862             }
  1863         }
  1864         *keylist = _keylist;
  1865     }
  1866     else
  1867         free_stringlist(_keylist);
  1868     
  1869     goto the_end;
  1870     
  1871 enomem:
  1872     free_stringlist(_keylist);
  1873     status = PEP_OUT_OF_MEMORY;
  1874     
  1875 the_end:
  1876     return status;
  1877 }
  1878 
  1879 DYNAMIC_API PEP_STATUS own_keys_retrieve(PEP_SESSION session, stringlist_t **keylist)
  1880 {
  1881     return _own_keys_retrieve(session, keylist, 0, true);
  1882 }
  1883 
  1884 DYNAMIC_API PEP_STATUS set_own_key(
  1885        PEP_SESSION session,
  1886        pEp_identity *me,
  1887        const char *fpr
  1888     )
  1889 {
  1890     PEP_STATUS status = PEP_STATUS_OK;
  1891     
  1892     assert(session && me);
  1893     assert(!EMPTYSTR(fpr));
  1894     assert(!EMPTYSTR(me->address));
  1895     assert(!EMPTYSTR(me->user_id));
  1896     assert(!EMPTYSTR(me->username));
  1897 
  1898     if (!session || !me || EMPTYSTR(fpr) || EMPTYSTR(me->address) ||
  1899             EMPTYSTR(me->user_id) || EMPTYSTR(me->username))
  1900         return PEP_ILLEGAL_VALUE;
  1901 
  1902     if (me->fpr == fpr)
  1903         me->fpr = NULL;
  1904 
  1905     status = _myself(session, me, false, true, false);
  1906     // we do not need a valid key but dislike other errors
  1907     if (status != PEP_STATUS_OK && status != PEP_GET_KEY_FAILED && status != PEP_KEY_UNSUITABLE)
  1908         return status;
  1909     status = PEP_STATUS_OK;
  1910 
  1911     bool private = false;
  1912     status = contains_priv_key(session, fpr, &private);
  1913     
  1914     if (status != PEP_STATUS_OK)
  1915         return status;
  1916         
  1917     if (!private)
  1918         return PEP_KEY_UNSUITABLE;
  1919  
  1920     if (me->fpr)
  1921         free(me->fpr);
  1922     me->fpr = strdup(fpr);
  1923     assert(me->fpr);
  1924     if (!me->fpr)
  1925         return PEP_OUT_OF_MEMORY;
  1926 
  1927     status = validate_fpr(session, me, false, true);
  1928     if (status)
  1929         return status;
  1930 
  1931     me->comm_type = PEP_ct_pEp;
  1932     status = set_identity(session, me);
  1933     return status;
  1934 }
  1935 
  1936 PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
  1937                              bool *has_private) {
  1938 
  1939     assert(session);
  1940     assert(fpr);
  1941     assert(has_private);
  1942     
  1943     if (!(session && fpr && has_private))
  1944         return PEP_ILLEGAL_VALUE;
  1945 
  1946     return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
  1947 }
  1948 
  1949 PEP_STATUS add_mistrusted_key(PEP_SESSION session, const char* fpr)
  1950 {
  1951     int result;
  1952 
  1953     assert(!EMPTYSTR(fpr));
  1954     
  1955     if (!(session) || EMPTYSTR(fpr))
  1956         return PEP_ILLEGAL_VALUE;
  1957 
  1958     sqlite3_reset(session->add_mistrusted_key);
  1959     sqlite3_bind_text(session->add_mistrusted_key, 1, fpr, -1,
  1960             SQLITE_STATIC);
  1961 
  1962     result = sqlite3_step(session->add_mistrusted_key);
  1963     sqlite3_reset(session->add_mistrusted_key);
  1964 
  1965     if (result != SQLITE_DONE)
  1966         return PEP_CANNOT_SET_PGP_KEYPAIR; // FIXME: Better status?
  1967 
  1968     return PEP_STATUS_OK;
  1969 }
  1970 
  1971 PEP_STATUS delete_mistrusted_key(PEP_SESSION session, const char* fpr)
  1972 {
  1973     int result;
  1974 
  1975     assert(!EMPTYSTR(fpr));
  1976     
  1977     if (!(session) || EMPTYSTR(fpr))
  1978         return PEP_ILLEGAL_VALUE;
  1979 
  1980     sqlite3_reset(session->delete_mistrusted_key);
  1981     sqlite3_bind_text(session->delete_mistrusted_key, 1, fpr, -1,
  1982             SQLITE_STATIC);
  1983 
  1984     result = sqlite3_step(session->delete_mistrusted_key);
  1985     sqlite3_reset(session->delete_mistrusted_key);
  1986 
  1987     if (result != SQLITE_DONE)
  1988         return PEP_UNKNOWN_ERROR; // FIXME: Better status?
  1989 
  1990     return PEP_STATUS_OK;
  1991 }
  1992 
  1993 PEP_STATUS is_mistrusted_key(PEP_SESSION session, const char* fpr,
  1994                              bool* mistrusted)
  1995 {
  1996     PEP_STATUS status = PEP_STATUS_OK;
  1997 
  1998     assert(session);
  1999     assert(!EMPTYSTR(fpr));
  2000 
  2001     if (!(session && fpr))
  2002         return PEP_ILLEGAL_VALUE;
  2003 
  2004     *mistrusted = false;
  2005 
  2006     sqlite3_reset(session->is_mistrusted_key);
  2007     sqlite3_bind_text(session->is_mistrusted_key, 1, fpr, -1, SQLITE_STATIC);
  2008 
  2009     int result;
  2010 
  2011     result = sqlite3_step(session->is_mistrusted_key);
  2012     switch (result) {
  2013     case SQLITE_ROW:
  2014         *mistrusted = sqlite3_column_int(session->is_mistrusted_key, 0);
  2015         status = PEP_STATUS_OK;
  2016         break;
  2017 
  2018     default:
  2019         status = PEP_UNKNOWN_ERROR;
  2020     }
  2021 
  2022     sqlite3_reset(session->is_mistrusted_key);
  2023     return status;
  2024 }
  2025 
  2026 #ifdef USE_GPG
  2027 PEP_STATUS pgp_find_trusted_private_keys(
  2028         PEP_SESSION session, stringlist_t **keylist
  2029     );
  2030 
  2031 enum _pgp_thing {
  2032     _pgp_none = 0,
  2033     _pgp_fpr,
  2034     _pgp_email,
  2035     _pgp_name
  2036 };
  2037 
  2038 static enum _pgp_thing _pgp_thing_next(enum _pgp_thing thing)
  2039 {
  2040     switch (thing) {
  2041         case _pgp_fpr:
  2042             return _pgp_email;
  2043         case _pgp_email:
  2044             return _pgp_name;
  2045         case _pgp_name:
  2046             return _pgp_fpr;
  2047         default:
  2048             return _pgp_fpr;
  2049     }
  2050 }
  2051 
  2052 PEP_STATUS pgp_import_ultimately_trusted_keypairs(PEP_SESSION session) {
  2053     assert(session);
  2054     if (!session)
  2055         return PEP_ILLEGAL_VALUE;
  2056 
  2057     stringlist_t* priv_keylist = NULL;
  2058     PEP_STATUS status = PEP_STATUS_OK;
  2059 
  2060     // 1. get keys
  2061     status = pgp_find_trusted_private_keys(session, &priv_keylist);
  2062     if (status)
  2063         return status;
  2064 
  2065     pEp_identity *identity = NULL;
  2066     stringlist_t *_sl;
  2067 	
  2068     char *fpr = NULL;
  2069     enum _pgp_thing thing = _pgp_none;
  2070     for (_sl = priv_keylist; _sl && _sl->value; _sl = _sl->next) {
  2071         thing = _pgp_thing_next(thing);
  2072         switch (thing) {
  2073             case _pgp_fpr:
  2074                 // PEP_OWN_USERID is ok here because this is only run on first use!
  2075                 identity = new_identity(NULL, NULL, PEP_OWN_USERID, NULL);
  2076                 if (!identity) {
  2077                     status = PEP_OUT_OF_MEMORY;
  2078                     break;
  2079                 }
  2080                 identity->me = true;
  2081                 fpr = strdup(_sl->value);
  2082                 assert(fpr);
  2083                 if (!fpr) {
  2084                     status = PEP_OUT_OF_MEMORY;
  2085                     free_identity(identity);
  2086                 }
  2087                 break;
  2088             case _pgp_email:
  2089                 assert(identity);
  2090                 identity->address = strdup(_sl->value);
  2091                 assert(identity->address);
  2092                 if (!identity->address) {
  2093                     status = PEP_OUT_OF_MEMORY;
  2094                     free_identity(identity);
  2095                 }
  2096                 break;
  2097             case _pgp_name:
  2098                 assert(identity);
  2099                 identity->username = strdup(_sl->value);
  2100                 assert(identity->username);
  2101                 if (!identity->username)
  2102                     status = PEP_OUT_OF_MEMORY;
  2103                 else
  2104                     status = set_own_key(session, identity, fpr);
  2105                 free_identity(identity);
  2106                 identity = NULL;
  2107                 break;
  2108             default:
  2109                 assert(0);
  2110                 free_identity(identity);
  2111                 status = PEP_UNKNOWN_ERROR;
  2112         }
  2113         if (status)
  2114             break;
  2115     }
  2116     
  2117     free_stringlist(priv_keylist);
  2118     return status;
  2119 }
  2120 #endif // USE_GPG