src/keymanagement.c
author Krista Bennett <krista@pep-project.org>
Tue, 05 Dec 2017 10:00:48 +0100
branchENGINE-293
changeset 2304 3bd45e2b8f92
parent 2303 e7b3fa443f3e
child 2305 f617e0acae94
permissions -rw-r--r--
ENGINE-293: compiling again. Need to fix test cases and see what I have wrought with the PEP_OWN_USERID removals. After that, next step is to really fix myself()
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "platform.h"
     5 
     6 #include <string.h>
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 #include <assert.h>
    10 #include <ctype.h>
    11 
    12 #include "pEp_internal.h"
    13 #include "keymanagement.h"
    14 
    15 #include "sync_fsm.h"
    16 #include "blacklist.h"
    17 
    18 #ifndef EMPTYSTR
    19 #define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
    20 #endif
    21 
    22 #define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
    23 
    24 PEP_STATUS elect_pubkey(
    25         PEP_SESSION session, pEp_identity * identity
    26     )
    27 {
    28     PEP_STATUS status;
    29     stringlist_t *keylist = NULL;
    30     char *_fpr = "";
    31     identity->comm_type = PEP_ct_unknown;
    32 
    33     status = find_keys(session, identity->address, &keylist);
    34     assert(status != PEP_OUT_OF_MEMORY);
    35     if (status == PEP_OUT_OF_MEMORY)
    36         return PEP_OUT_OF_MEMORY;
    37 
    38     stringlist_t *_keylist;
    39     for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
    40         PEP_comm_type _comm_type_key;
    41 
    42         status = get_key_rating(session, _keylist->value, &_comm_type_key);
    43         assert(status != PEP_OUT_OF_MEMORY);
    44         if (status == PEP_OUT_OF_MEMORY) {
    45             free_stringlist(keylist);
    46             return PEP_OUT_OF_MEMORY;
    47         }
    48 
    49         if (_comm_type_key != PEP_ct_compromized &&
    50             _comm_type_key != PEP_ct_unknown)
    51         {
    52             if (identity->comm_type == PEP_ct_unknown ||
    53                 _comm_type_key > identity->comm_type)
    54             {
    55                 bool blacklisted;
    56                 status = blacklist_is_listed(session, _keylist->value, &blacklisted);
    57                 if (status == PEP_STATUS_OK && !blacklisted) {
    58                     identity->comm_type = _comm_type_key;
    59                     _fpr = _keylist->value;
    60                 }
    61             }
    62         }
    63     }
    64 
    65     
    66 //    if (_fpr) {
    67     free(identity->fpr);
    68 
    69     identity->fpr = strdup(_fpr);
    70     if (identity->fpr == NULL) {
    71         free_stringlist(keylist);
    72         return PEP_OUT_OF_MEMORY;
    73     }
    74 //    }
    75     free_stringlist(keylist);
    76     return PEP_STATUS_OK;
    77 }
    78 
    79 PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags);
    80 
    81 DYNAMIC_API PEP_STATUS update_identity(
    82         PEP_SESSION session, pEp_identity * identity
    83     )
    84 {
    85     pEp_identity *stored_identity = NULL;
    86     pEp_identity *temp_id = NULL;
    87     PEP_STATUS status;
    88 
    89     assert(session);
    90     assert(identity);
    91     assert(!EMPTYSTR(identity->address));
    92 
    93     if (!(session && identity && !EMPTYSTR(identity->address)))
    94         return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
    95 
    96     // dunno if we should keep this function shortcut.
    97     if (_identity_me(identity)) {
    98         identity->me = true;
    99         return _myself(session, identity, false, true);
   100     }
   101 
   102     int _no_user_id = EMPTYSTR(identity->user_id);
   103     int _did_elect_new_key = 0;
   104 
   105     if (_no_user_id)
   106     {
   107         char* own_id = NULL;
   108         status = get_own_userid(session, &own_id);
   109         if (status == PEP_STATUS_OK && own_id) {
   110             status = get_identity(session, identity->address, own_id,
   111                     &stored_identity);
   112             if (status == PEP_STATUS_OK) {
   113                 free_identity(stored_identity);
   114                 identity->user_id = own_id;
   115                 return _myself(session, identity, false, true);
   116             }
   117         }
   118         
   119         free(identity->user_id);
   120 
   121         identity->user_id = calloc(1, strlen(identity->address) + 6);
   122         if (!identity->user_id)
   123         {
   124             return PEP_OUT_OF_MEMORY;
   125         }
   126         snprintf(identity->user_id, strlen(identity->address) + 6,
   127                  "TOFU_%s", identity->address);
   128     }
   129  
   130     status = get_identity(session,
   131                           identity->address,
   132                           identity->user_id,
   133                           &stored_identity);
   134     
   135     assert(status != PEP_OUT_OF_MEMORY);
   136     if (status == PEP_OUT_OF_MEMORY)
   137         goto exit_free;
   138 
   139     temp_id = identity_dup(identity);
   140     
   141     /* We don't take given fpr. 
   142        In case there's no acceptable stored fpr, it will be elected. */
   143     free(temp_id->fpr);
   144     temp_id->fpr = NULL;
   145     temp_id->comm_type = PEP_ct_unknown;
   146             
   147     if (stored_identity) {
   148         
   149         bool dont_use_stored_fpr = true;
   150 
   151         /* if we have a stored_identity fpr */
   152         if (!EMPTYSTR(stored_identity->fpr)) {
   153             bool revoked = false;
   154             status = key_revoked(session, stored_identity->fpr, &revoked);
   155             
   156             if (status != PEP_STATUS_OK || revoked)
   157                 dont_use_stored_fpr = true;
   158                 
   159             if (revoked) {
   160                 // Do stuff
   161                 status = update_trust_for_fpr(session, stored_identity->fpr, PEP_ct_key_revoked);
   162                 // What to do on failure? FIXME
   163                 status = replace_identities_fpr(session, stored_identity->fpr, "");
   164             }
   165             else {    
   166                 status = blacklist_is_listed(session, stored_identity->fpr, &dont_use_stored_fpr);
   167                 if (status != PEP_STATUS_OK)
   168                     dont_use_stored_fpr = true; 
   169             }
   170         }
   171             
   172 
   173         if (!dont_use_stored_fpr) {
   174             /* Check stored comm_type */
   175             PEP_comm_type _comm_type_key;
   176             status = get_key_rating(session, stored_identity->fpr, &_comm_type_key);
   177             assert(status != PEP_OUT_OF_MEMORY);
   178             if (status == PEP_OUT_OF_MEMORY) {
   179                 goto exit_free;
   180             }
   181             if (status == PEP_KEY_NOT_FOUND){
   182                 /* stored key was deleted from keyring. any other candidate ?*/
   183                 status = elect_pubkey(session, temp_id);
   184                 if (status != PEP_STATUS_OK) {
   185                     goto exit_free;
   186                 } else {
   187                     _did_elect_new_key = 1;
   188                 }
   189             } else {
   190                 temp_id->fpr = strdup(stored_identity->fpr);
   191                 assert(temp_id->fpr);
   192                 if (temp_id->fpr == NULL) {
   193                     status = PEP_OUT_OF_MEMORY;
   194                     goto exit_free;
   195                 }
   196 
   197                 if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
   198                     /* if key not good anymore, 
   199                        downgrade eventually trusted comm_type */
   200                     temp_id->comm_type = _comm_type_key;
   201                 } else {
   202                     /* otherwise take stored comm_type as-is except if 
   203                        is unknown or is expired (but key not expired anymore) */
   204                     temp_id->comm_type = stored_identity->comm_type;
   205                     if (temp_id->comm_type == PEP_ct_unknown ||
   206                         temp_id->comm_type == PEP_ct_key_expired) {
   207                         temp_id->comm_type = _comm_type_key;
   208                     }
   209                 }
   210             }
   211         }
   212         else {
   213             status = elect_pubkey(session, temp_id);
   214             if (status != PEP_STATUS_OK){
   215                 goto exit_free;
   216             } else {
   217                 _did_elect_new_key = 1;
   218             }
   219         }
   220         
   221         /* ok, from here on out, use temp_id */
   222         
   223         
   224         /* At this point, we either have a non-blacklisted fpr we can work */
   225         /* with, or we've got nada.                                        */
   226 
   227         if (EMPTYSTR(temp_id->fpr)) {
   228             /* nada : set comm_type accordingly */
   229             temp_id->comm_type = PEP_ct_key_not_found;
   230         }
   231         
   232         if (EMPTYSTR(temp_id->username)) {
   233             free(temp_id->username);
   234             temp_id->username = strdup(stored_identity->username);
   235             assert(temp_id->username);
   236             if (temp_id->username == NULL){
   237                 status = PEP_OUT_OF_MEMORY;
   238                 goto exit_free;
   239             }
   240         }
   241 
   242         if (temp_id->lang[0] == 0) {
   243             temp_id->lang[0] = stored_identity->lang[0];
   244             temp_id->lang[1] = stored_identity->lang[1];
   245             temp_id->lang[2] = 0;
   246         }
   247 
   248         temp_id->flags = stored_identity->flags;
   249     }
   250     else /* stored_identity == NULL */ {
   251         temp_id->flags = 0;
   252 
   253         /* We elect a pubkey */
   254         status = elect_pubkey(session, temp_id);
   255         if (status != PEP_STATUS_OK)
   256             goto exit_free;
   257         
   258         /* Work with the elected key */
   259         if (!EMPTYSTR(temp_id->fpr)) {
   260             
   261             PEP_comm_type _comm_type_key = temp_id->comm_type;
   262             
   263             _did_elect_new_key = 1;
   264 
   265             // We don't want to lose a previous trust entry!!!
   266             status = get_trust(session, temp_id);
   267 
   268             bool has_trust_status = (status == PEP_STATUS_OK);
   269 
   270             if (!has_trust_status)
   271                 temp_id->comm_type = _comm_type_key;
   272         }
   273     }
   274 
   275     if (temp_id->fpr == NULL) {
   276         temp_id->fpr = strdup("");
   277         if (temp_id->fpr == NULL) {
   278             status = PEP_OUT_OF_MEMORY;
   279             goto exit_free;
   280         }
   281     }
   282     
   283     
   284     status = PEP_STATUS_OK;
   285 
   286     if (temp_id->comm_type != PEP_ct_unknown && !EMPTYSTR(temp_id->user_id)) {
   287 
   288         if (EMPTYSTR(temp_id->username)) { // mitigate
   289             free(temp_id->username);
   290             temp_id->username = strdup("Anonymous");
   291             assert(temp_id->username);
   292             if (temp_id->username == NULL){
   293                 status = PEP_OUT_OF_MEMORY;
   294                 goto exit_free;
   295             }
   296         }
   297 
   298         // Identity doesn't get stored if call was just about checking existing
   299         // user by address (i.e. no user id given but already stored)
   300         if (!(_no_user_id && stored_identity) || _did_elect_new_key)
   301         {
   302             status = set_identity(session, temp_id);
   303             assert(status == PEP_STATUS_OK);
   304             if (status != PEP_STATUS_OK) {
   305                 goto exit_free;
   306             }
   307         }
   308     }
   309 
   310     if (temp_id->comm_type != PEP_ct_compromized &&
   311             temp_id->comm_type < PEP_ct_strong_but_unconfirmed)
   312         if (session->examine_identity)
   313             session->examine_identity(temp_id, session->examine_management);
   314     
   315     /* ok, we got to the end. So we can assign the output identity */
   316     free(identity->address);
   317     identity->address = strdup(temp_id->address);
   318     free(identity->fpr);
   319     identity->fpr = strdup(temp_id->fpr);
   320     free(identity->user_id);
   321     identity->user_id = strdup(temp_id->user_id);
   322     free(identity->username);
   323     identity->username = strdup(temp_id->username ? temp_id->username : "Anonymous");
   324     identity->comm_type = temp_id->comm_type;
   325     identity->lang[0] = temp_id->lang[0];
   326     identity->lang[1] = temp_id->lang[1];
   327     identity->lang[2] = 0;
   328     identity->me = temp_id->me;
   329     identity->flags = temp_id->flags;
   330 
   331 exit_free :
   332     free_identity(stored_identity);
   333     free_identity(temp_id);
   334     
   335     return ADD_TO_LOG(status);
   336 }
   337 
   338 PEP_STATUS elect_ownkey(
   339         PEP_SESSION session, pEp_identity * identity
   340     )
   341 {
   342     PEP_STATUS status;
   343     stringlist_t *keylist = NULL;
   344 
   345     free(identity->fpr);
   346     identity->fpr = NULL;
   347 
   348     status = find_private_keys(session, identity->address, &keylist);
   349     assert(status != PEP_OUT_OF_MEMORY);
   350     if (status == PEP_OUT_OF_MEMORY)
   351         return PEP_OUT_OF_MEMORY;
   352     
   353     if (keylist != NULL && keylist->value != NULL)
   354     {
   355         char *_fpr = NULL;
   356         identity->comm_type = PEP_ct_unknown;
   357 
   358         stringlist_t *_keylist;
   359         for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
   360             bool is_own = false;
   361             
   362             status = own_key_is_listed(session, _keylist->value, &is_own);
   363             assert(status == PEP_STATUS_OK);
   364             if (status != PEP_STATUS_OK) {
   365                 free_stringlist(keylist);
   366                 return status;
   367             }
   368             
   369             if (is_own)
   370             {
   371                 PEP_comm_type _comm_type_key;
   372                 
   373                 status = get_key_rating(session, _keylist->value, &_comm_type_key);
   374                 assert(status != PEP_OUT_OF_MEMORY);
   375                 if (status == PEP_OUT_OF_MEMORY) {
   376                     free_stringlist(keylist);
   377                     return PEP_OUT_OF_MEMORY;
   378                 }
   379                 
   380                 if (_comm_type_key != PEP_ct_compromized &&
   381                     _comm_type_key != PEP_ct_unknown)
   382                 {
   383                     if (identity->comm_type == PEP_ct_unknown ||
   384                         _comm_type_key > identity->comm_type)
   385                     {
   386                         identity->comm_type = _comm_type_key;
   387                         _fpr = _keylist->value;
   388                     }
   389                 }
   390             }
   391         }
   392         
   393         if (_fpr)
   394         {
   395             identity->fpr = strdup(_fpr);
   396             assert(identity->fpr);
   397             if (identity->fpr == NULL)
   398             {
   399                 free_stringlist(keylist);
   400                 return PEP_OUT_OF_MEMORY;
   401             }
   402         }
   403         free_stringlist(keylist);
   404     }
   405     return PEP_STATUS_OK;
   406 }
   407 
   408 PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
   409                                 bool* is_usable) {
   410     
   411     bool dont_use_fpr = true;
   412     
   413     PEP_STATUS status = blacklist_is_listed(session, fpr, &dont_use_fpr);
   414     if (status == PEP_STATUS_OK && !dont_use_fpr) {
   415         // Make sure there is a *private* key associated with this fpr
   416         bool has_private = false;
   417         status = contains_priv_key(session, fpr, &has_private);
   418 
   419         if (status == PEP_STATUS_OK)
   420             dont_use_fpr = !has_private;
   421     }
   422     
   423     *is_usable = !dont_use_fpr;
   424     
   425     return ADD_TO_LOG(status);
   426 }
   427 
   428 PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
   429 {
   430     pEp_identity *stored_identity = NULL;
   431     PEP_STATUS status;
   432 
   433     assert(session);
   434     assert(identity);
   435     assert(!EMPTYSTR(identity->address));
   436 
   437     char* own_id = NULL;
   438     status = get_own_userid(session, &own_id);
   439 
   440     assert(EMPTYSTR(identity->user_id) ||
   441            (own_id && strcmp(identity->user_id, own_id) == 0));
   442 
   443     if (!(session && identity && !EMPTYSTR(identity->address) &&
   444             (EMPTYSTR(identity->user_id) ||
   445             (own_id && strcmp(identity->user_id, own_id) == 0))))
   446         return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
   447 
   448     identity->comm_type = PEP_ct_pEp;
   449     identity->me = true;
   450     if(ignore_flags)
   451         identity->flags = 0;
   452     
   453     if (EMPTYSTR(identity->user_id))
   454     {
   455         free(identity->user_id);
   456         identity->user_id = (own_id ? own_id : strdup(PEP_OWN_USERID));
   457         assert(identity->user_id);
   458         if (identity->user_id == NULL)
   459             return PEP_OUT_OF_MEMORY;
   460     }
   461 
   462     if (EMPTYSTR(identity->username))
   463     {
   464         free(identity->username);
   465         identity->username = strdup("Anonymous");
   466         assert(identity->username);
   467         if (identity->username == NULL)
   468             return PEP_OUT_OF_MEMORY;
   469     }
   470 
   471     DEBUG_LOG("myself", "debug", identity->address);
   472  
   473     status = get_identity(session,
   474                           identity->address,
   475                           identity->user_id,
   476                           &stored_identity);
   477     
   478     assert(status != PEP_OUT_OF_MEMORY);
   479     if (status == PEP_OUT_OF_MEMORY)
   480         return PEP_OUT_OF_MEMORY;
   481 
   482     bool dont_use_stored_fpr = true;
   483     bool dont_use_input_fpr = true;
   484         
   485     if (stored_identity)
   486     {
   487         if (EMPTYSTR(identity->fpr)) {
   488             
   489             bool has_private = false;
   490             
   491             status = _has_usable_priv_key(session, stored_identity->fpr, &has_private); 
   492             
   493             // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
   494             if (has_private) {
   495                 identity->fpr = strdup(stored_identity->fpr);
   496                 assert(identity->fpr);
   497                 if (identity->fpr == NULL)
   498                 {
   499                     return PEP_OUT_OF_MEMORY;
   500                 }
   501                 dont_use_stored_fpr = false;
   502             }
   503         }
   504         
   505         identity->flags = (identity->flags & 255) | stored_identity->flags;
   506         free_identity(stored_identity);
   507     }
   508     
   509     if (dont_use_stored_fpr && !EMPTYSTR(identity->fpr))
   510     {
   511         // App must have a good reason to give fpr, such as explicit
   512         // import of private key, or similar.
   513 
   514         // Take given fpr as-is.
   515 
   516         // BUT:
   517         // First check to see if it's blacklisted or private part is missing?
   518         bool has_private = false;
   519         
   520         status = _has_usable_priv_key(session, identity->fpr, &has_private); 
   521         
   522         // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
   523         if (has_private) {
   524             dont_use_input_fpr = false;
   525         }
   526     }
   527     
   528     // Ok, we failed to get keys either way, so let's elect one.
   529     if (dont_use_input_fpr && dont_use_stored_fpr)
   530     {
   531         status = elect_ownkey(session, identity);
   532         assert(status == PEP_STATUS_OK);
   533         if (status != PEP_STATUS_OK) {
   534             return ADD_TO_LOG(status);
   535         }
   536 
   537         bool has_private = false;
   538         if (identity->fpr) {
   539             // ok, we elected something.
   540             // elect_ownkey only returns private keys, so we don't check again.
   541             // Check to see if it's blacklisted
   542             bool listed;
   543             status = blacklist_is_listed(session, identity->fpr, &listed); 
   544 
   545             if (status == PEP_STATUS_OK)
   546                 has_private = !listed;
   547         }
   548         
   549         if (has_private) {
   550             dont_use_input_fpr = false;
   551         }
   552         else { // OK, we've tried everything. Time to generate new keys.
   553             free(identity->fpr); // It can stay in this state (unallocated) because we'll generate a new key 
   554             identity->fpr = NULL;
   555         }
   556     }
   557 
   558     bool revoked = false;
   559     char *r_fpr = NULL;
   560     if (!EMPTYSTR(identity->fpr))
   561     {
   562         status = key_revoked(session, identity->fpr, &revoked);
   563 
   564         if (status != PEP_STATUS_OK) 
   565         {
   566             return ADD_TO_LOG(status);
   567         }
   568     }
   569    
   570     if (EMPTYSTR(identity->fpr) || revoked)
   571     {
   572         if(!do_keygen){
   573             return ADD_TO_LOG(PEP_GET_KEY_FAILED);
   574         }
   575 
   576         if(revoked)
   577         {
   578             r_fpr = identity->fpr;
   579             identity->fpr = NULL;
   580         }
   581         
   582         DEBUG_LOG("generating key pair", "debug", identity->address);
   583         status = generate_keypair(session, identity);
   584         assert(status != PEP_OUT_OF_MEMORY);
   585         if (status != PEP_STATUS_OK) {
   586             char buf[11];
   587             snprintf(buf, 11, "%d", status);
   588             DEBUG_LOG("generating key pair failed", "debug", buf);
   589             if(revoked && r_fpr)
   590                 free(r_fpr);
   591             return ADD_TO_LOG(status);
   592         }
   593 
   594         
   595         if(revoked)
   596         {
   597             status = set_revoked(session, r_fpr,
   598                                  identity->fpr, time(NULL));
   599             free(r_fpr);
   600             if (status != PEP_STATUS_OK) {
   601                 return ADD_TO_LOG(status);
   602             }
   603         }
   604     }
   605     else
   606     {
   607         bool expired;
   608         status = key_expired(session, identity->fpr, 
   609                              time(NULL) + (7*24*3600), // In a week
   610                              &expired);
   611 
   612         assert(status == PEP_STATUS_OK);
   613         if (status != PEP_STATUS_OK) {
   614             return ADD_TO_LOG(status);
   615         }
   616 
   617         if (status == PEP_STATUS_OK && expired) {
   618             timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
   619             renew_key(session, identity->fpr, ts);
   620             free_timestamp(ts);
   621         }
   622     }
   623 
   624     if (!identity->username)
   625         identity->username = strdup("");
   626     
   627     status = set_identity(session, identity);
   628     assert(status == PEP_STATUS_OK);
   629     if (status != PEP_STATUS_OK) {
   630         return status;
   631     }
   632 
   633     return ADD_TO_LOG(PEP_STATUS_OK);
   634 }
   635 
   636 DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
   637 {
   638     return ADD_TO_LOG(_myself(session, identity, true, false));
   639 }
   640 
   641 DYNAMIC_API PEP_STATUS register_examine_function(
   642         PEP_SESSION session, 
   643         examine_identity_t examine_identity,
   644         void *management
   645     )
   646 {
   647     assert(session);
   648     if (!session)
   649         return PEP_ILLEGAL_VALUE;
   650 
   651     session->examine_management = management;
   652     session->examine_identity = examine_identity;
   653 
   654     return PEP_STATUS_OK;
   655 }
   656 
   657 DYNAMIC_API PEP_STATUS do_keymanagement(
   658         retrieve_next_identity_t retrieve_next_identity,
   659         void *management
   660     )
   661 {
   662     PEP_SESSION session;
   663     pEp_identity *identity;
   664     PEP_STATUS status;
   665 
   666     assert(retrieve_next_identity);
   667     assert(management);
   668 
   669     if (!retrieve_next_identity || !management)
   670         return PEP_ILLEGAL_VALUE;
   671 
   672     status = init(&session);
   673     assert(status == PEP_STATUS_OK);
   674     if (status != PEP_STATUS_OK)
   675         return status;
   676 
   677     log_event(session, "keymanagement thread started", "pEp engine", NULL, NULL);
   678 
   679     while ((identity = retrieve_next_identity(management))) 
   680     {
   681         assert(identity->address);
   682         if(identity->address)
   683         {
   684             DEBUG_LOG("do_keymanagement", "retrieve_next_identity", identity->address);
   685 
   686             if (_identity_me(identity)) {
   687                 status = myself(session, identity);
   688             } else {
   689                 status = recv_key(session, identity->address);
   690             }
   691 
   692             assert(status != PEP_OUT_OF_MEMORY);
   693             if(status == PEP_OUT_OF_MEMORY)
   694                 return PEP_OUT_OF_MEMORY;
   695         }
   696         free_identity(identity);
   697     }
   698 
   699     log_event(session, "keymanagement thread shutdown", "pEp engine", NULL, NULL);
   700 
   701     release(session);
   702     return PEP_STATUS_OK;
   703 }
   704 
   705 DYNAMIC_API PEP_STATUS key_mistrusted(
   706         PEP_SESSION session,
   707         pEp_identity *ident
   708     )
   709 {
   710     PEP_STATUS status = PEP_STATUS_OK;
   711 
   712     assert(session);
   713     assert(ident);
   714     assert(!EMPTYSTR(ident->fpr));
   715 
   716     if (!(session && ident && ident->fpr))
   717         return PEP_ILLEGAL_VALUE;
   718 
   719     if (_identity_me(ident))
   720     {
   721         revoke_key(session, ident->fpr, NULL);
   722         myself(session, ident);
   723     }
   724     else
   725     {
   726         // for undo
   727         if (session->cached_mistrusted)
   728             free(session->cached_mistrusted);
   729         session->cached_mistrusted = identity_dup(ident);
   730         status = mark_as_compromized(session, ident->fpr);
   731     }
   732 
   733     return status;
   734 }
   735 
   736 DYNAMIC_API PEP_STATUS undo_last_mistrust(PEP_SESSION session) {
   737     assert(session);
   738     
   739     if (!session)
   740         return PEP_ILLEGAL_VALUE;
   741     
   742     PEP_STATUS status = PEP_STATUS_OK;
   743         
   744     pEp_identity* cached_ident = session->cached_mistrusted;
   745     
   746     if (!cached_ident)
   747         status = PEP_CANNOT_FIND_IDENTITY;
   748     else {
   749         status = set_identity(session, cached_ident);            
   750         free_identity(session->cached_mistrusted);
   751     }
   752     
   753     session->cached_mistrusted = NULL;
   754 
   755     return status;
   756 }
   757 
   758 DYNAMIC_API PEP_STATUS key_reset_trust(
   759         PEP_SESSION session,
   760         pEp_identity *ident
   761     )
   762 {
   763     PEP_STATUS status = PEP_STATUS_OK;
   764 
   765     assert(session);
   766     assert(ident);
   767     assert(!_identity_me(ident));
   768     assert(!EMPTYSTR(ident->fpr));
   769     assert(!EMPTYSTR(ident->address));
   770     assert(!EMPTYSTR(ident->user_id));
   771 
   772     if (!(session && ident && !_identity_me(ident) && ident->fpr && ident->address &&
   773             ident->user_id))
   774         return PEP_ILLEGAL_VALUE;
   775 
   776     status = update_identity(session, ident);
   777     if (status != PEP_STATUS_OK)
   778         return status;
   779 
   780     if (ident->comm_type == PEP_ct_mistrusted)
   781         ident->comm_type = PEP_ct_unknown;
   782     else
   783         ident->comm_type &= ~PEP_ct_confirmed;
   784 
   785     status = set_identity(session, ident);
   786     if (status != PEP_STATUS_OK)
   787         return status;
   788 
   789     if (ident->comm_type == PEP_ct_unknown)
   790         status = update_identity(session, ident);
   791     return status;
   792 }
   793 
   794 DYNAMIC_API PEP_STATUS trust_personal_key(
   795         PEP_SESSION session,
   796         pEp_identity *ident
   797     )
   798 {
   799     PEP_STATUS status = PEP_STATUS_OK;
   800 
   801     assert(session);
   802     assert(ident);
   803     assert(!EMPTYSTR(ident->address));
   804     assert(!EMPTYSTR(ident->user_id));
   805     assert(!EMPTYSTR(ident->fpr));
   806 
   807     if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
   808             EMPTYSTR(ident->fpr))
   809         return PEP_ILLEGAL_VALUE;
   810 
   811     status = update_identity(session, ident);
   812     if (status != PEP_STATUS_OK)
   813         return status;
   814 
   815     if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
   816         ident->comm_type |= PEP_ct_confirmed;
   817         status = set_identity(session, ident);
   818     }
   819     else {
   820         // MISSING: S/MIME has to be handled depending on trusted CAs
   821         status = PEP_CANNOT_SET_TRUST;
   822     }
   823 
   824     return status;
   825 }
   826 
   827 DYNAMIC_API PEP_STATUS own_key_is_listed(
   828         PEP_SESSION session,
   829         const char *fpr,
   830         bool *listed
   831     )
   832 {
   833     PEP_STATUS status = PEP_STATUS_OK;
   834     int count;
   835     
   836     assert(session && fpr && fpr[0] && listed);
   837     
   838     if (!(session && fpr && fpr[0] && listed))
   839         return PEP_ILLEGAL_VALUE;
   840     
   841     *listed = false;
   842     
   843     sqlite3_reset(session->own_key_is_listed);
   844     sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
   845     
   846     int result;
   847     
   848     result = sqlite3_step(session->own_key_is_listed);
   849     switch (result) {
   850         case SQLITE_ROW:
   851             count = sqlite3_column_int(session->own_key_is_listed, 0);
   852             *listed = count > 0;
   853             status = PEP_STATUS_OK;
   854             break;
   855             
   856         default:
   857             status = PEP_UNKNOWN_ERROR;
   858     }
   859     
   860     sqlite3_reset(session->own_key_is_listed);
   861     return status;
   862 }
   863 
   864 PEP_STATUS _own_identities_retrieve(
   865         PEP_SESSION session,
   866         identity_list **own_identities,
   867         identity_flags_t excluded_flags
   868       )
   869 {
   870     PEP_STATUS status = PEP_STATUS_OK;
   871     
   872     assert(session && own_identities);
   873     if (!(session && own_identities))
   874         return PEP_ILLEGAL_VALUE;
   875     
   876     *own_identities = NULL;
   877     identity_list *_own_identities = new_identity_list(NULL);
   878     if (_own_identities == NULL)
   879         goto enomem;
   880     
   881     sqlite3_reset(session->own_identities_retrieve);
   882     
   883     int result;
   884     // address, fpr, username, user_id, comm_type, lang, flags
   885     const char *address = NULL;
   886     const char *fpr = NULL;
   887     const char *username = NULL;
   888     const char *user_id = NULL;
   889     PEP_comm_type comm_type = PEP_ct_unknown;
   890     const char *lang = NULL;
   891     unsigned int flags = 0;
   892     
   893     identity_list *_bl = _own_identities;
   894     do {
   895         sqlite3_bind_int(session->own_identities_retrieve, 1, excluded_flags);
   896         result = sqlite3_step(session->own_identities_retrieve);
   897         switch (result) {
   898             case SQLITE_ROW:
   899                 address = (const char *)
   900                     sqlite3_column_text(session->own_identities_retrieve, 0);
   901                 fpr = (const char *)
   902                     sqlite3_column_text(session->own_identities_retrieve, 1);
   903                 user_id = (const char *)
   904                     sqlite3_column_text(session->own_identities_retrieve, 2);
   905                 username = (const char *)
   906                     sqlite3_column_text(session->own_identities_retrieve, 3);
   907                 comm_type = PEP_ct_pEp;
   908                 lang = (const char *)
   909                     sqlite3_column_text(session->own_identities_retrieve, 4);
   910                 flags = (unsigned int)
   911                     sqlite3_column_int(session->own_identities_retrieve, 5);
   912 
   913                 pEp_identity *ident = new_identity(address, fpr, user_id, username);
   914                 if (!ident)
   915                     goto enomem;
   916                 ident->comm_type = comm_type;
   917                 if (lang && lang[0]) {
   918                     ident->lang[0] = lang[0];
   919                     ident->lang[1] = lang[1];
   920                     ident->lang[2] = 0;
   921                 }
   922                 ident->me = true;
   923                 ident->flags = flags;
   924 
   925                 _bl = identity_list_add(_bl, ident);
   926                 if (_bl == NULL) {
   927                     free_identity(ident);
   928                     goto enomem;
   929                 }
   930                 
   931                 break;
   932                 
   933             case SQLITE_DONE:
   934                 break;
   935                 
   936             default:
   937                 status = PEP_UNKNOWN_ERROR;
   938                 result = SQLITE_DONE;
   939         }
   940     } while (result != SQLITE_DONE);
   941     
   942     sqlite3_reset(session->own_identities_retrieve);
   943     if (status == PEP_STATUS_OK)
   944         *own_identities = _own_identities;
   945     else
   946         free_identity_list(_own_identities);
   947     
   948     goto the_end;
   949     
   950 enomem:
   951     free_identity_list(_own_identities);
   952     status = PEP_OUT_OF_MEMORY;
   953     
   954 the_end:
   955     return status;
   956 }
   957 
   958 DYNAMIC_API PEP_STATUS own_identities_retrieve(
   959         PEP_SESSION session,
   960         identity_list **own_identities
   961       )
   962 {
   963     return _own_identities_retrieve(session, own_identities, 0);
   964 }
   965 
   966 PEP_STATUS _own_keys_retrieve(
   967         PEP_SESSION session,
   968         stringlist_t **keylist,
   969         identity_flags_t excluded_flags
   970       )
   971 {
   972     PEP_STATUS status = PEP_STATUS_OK;
   973     
   974     assert(session && keylist);
   975     if (!(session && keylist))
   976         return PEP_ILLEGAL_VALUE;
   977     
   978     *keylist = NULL;
   979     stringlist_t *_keylist = NULL;
   980     
   981     sqlite3_reset(session->own_keys_retrieve);
   982     
   983     int result;
   984     char *fpr = NULL;
   985     
   986     stringlist_t *_bl = _keylist;
   987     do {
   988         sqlite3_bind_int(session->own_keys_retrieve, 1, excluded_flags);
   989         result = sqlite3_step(session->own_keys_retrieve);
   990         switch (result) {
   991             case SQLITE_ROW:
   992                 fpr = strdup((const char *) sqlite3_column_text(session->own_keys_retrieve, 0));
   993                 if(fpr == NULL)
   994                     goto enomem;
   995 
   996                 _bl = stringlist_add(_bl, fpr);
   997                 if (_bl == NULL) {
   998                     free(fpr);
   999                     goto enomem;
  1000                 }
  1001                 if (_keylist == NULL)
  1002                     _keylist = _bl;
  1003                 
  1004                 break;
  1005                 
  1006             case SQLITE_DONE:
  1007                 break;
  1008                 
  1009             default:
  1010                 status = PEP_UNKNOWN_ERROR;
  1011                 result = SQLITE_DONE;
  1012         }
  1013     } while (result != SQLITE_DONE);
  1014     
  1015     sqlite3_reset(session->own_keys_retrieve);
  1016     if (status == PEP_STATUS_OK)
  1017         *keylist = _keylist;
  1018     else
  1019         free_stringlist(_keylist);
  1020     
  1021     goto the_end;
  1022     
  1023 enomem:
  1024     free_stringlist(_keylist);
  1025     status = PEP_OUT_OF_MEMORY;
  1026     
  1027 the_end:
  1028     return status;
  1029 }
  1030 
  1031 DYNAMIC_API PEP_STATUS own_keys_retrieve(PEP_SESSION session, stringlist_t **keylist)
  1032 {
  1033     return _own_keys_retrieve(session, keylist, 0);
  1034 }
  1035 
  1036 // FIXME: should it be be used when sync receive old keys ? (ENGINE-145)
  1037 DYNAMIC_API PEP_STATUS set_own_key(
  1038        PEP_SESSION session,
  1039        const char *address,
  1040        const char *fpr
  1041     )
  1042 {
  1043     PEP_STATUS status = PEP_STATUS_OK;
  1044     
  1045     assert(session &&
  1046            address &&
  1047            fpr && fpr[0]
  1048           );
  1049     
  1050     if (!(session &&
  1051           address &&
  1052           fpr && fpr[0]
  1053          ))
  1054         return PEP_ILLEGAL_VALUE;
  1055             
  1056             
  1057     // First see if we have it in own identities already, AND we retrieve
  1058     // our own user_id
  1059     pEp_identity* my_id = NULL;
  1060     identity_list* my_identities = NULL;
  1061     char* my_user_id = NULL;
  1062     status = own_identities_retrieve(session, &my_identities);
  1063     
  1064     if (status == PEP_STATUS_OK) {
  1065         if (my_identities) {
  1066             if (!(my_identities->ident && my_identities->ident->user_id))
  1067                 return PEP_ILLEGAL_VALUE;
  1068 
  1069             my_user_id = strdup(my_identities->ident->user_id);
  1070 
  1071             if (!my_user_id) 
  1072                 return PEP_OUT_OF_MEMORY;
  1073             
  1074             // Probably cheaper than all the strcmps if there are many,
  1075             // plus this avoids the capitalisation and . problems:
  1076             
  1077             status = get_identity(session, my_user_id, address, &my_id);
  1078             
  1079             if (status == PEP_STATUS_OK && my_id) {
  1080                 if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0) {
  1081                     // We're done. It was already here.
  1082                     // FIXME: Do we check trust/revocation/?
  1083                     goto pep_free;
  1084                 }            
  1085             }
  1086             
  1087             // Otherwise, we see if there's a binding for this user_id/key
  1088             // in the trust DB
  1089             
  1090             // If there's an id w/ user_id + address
  1091             if (my_id) {
  1092                 free(my_id->fpr);
  1093                 my_id->fpr = my_user_id;
  1094                 my_id->comm_type = PEP_ct_pEp;
  1095                 my_id->me = true; // just in case? 
  1096             }
  1097             else { // Else, we need a new identity
  1098                 my_id = new_identity(address, fpr, my_user_id, NULL); 
  1099                 if (status != PEP_STATUS_OK)
  1100                     goto pep_free; 
  1101                 my_id->me = true;
  1102                 my_id->comm_type = PEP_ct_pEp;
  1103             }
  1104         }
  1105         else {
  1106             // I think the prerequisite should be that at least one own identity
  1107             // already in the DB, so REALLY look at this.
  1108             return PEP_CANNOT_FIND_IDENTITY;
  1109         }
  1110         
  1111         status = set_identity(session, my_id);
  1112     }  
  1113     
  1114 pep_free:
  1115     free(my_id);
  1116     free(my_user_id);
  1117     return status;
  1118 }
  1119 
  1120 PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
  1121                              bool *has_private) {
  1122 
  1123     assert(session);
  1124     assert(fpr);
  1125     assert(has_private);
  1126     
  1127     if (!(session && fpr && has_private))
  1128         return PEP_ILLEGAL_VALUE;
  1129 
  1130     return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
  1131 }