src/keymanagement.c
author Hernâni Marques <hernani@pep.foundation>
Wed, 01 Nov 2017 04:58:56 +0100
changeset 2228 823086369b6c
parent 2129 fd3108a623c5
child 2265 73c0738de68c
permissions -rw-r--r--
Unneeded assignment
     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     if (_identity_me(identity)) {
    97         return _myself(session, identity, false, true);
    98     }
    99 
   100     int _no_user_id = EMPTYSTR(identity->user_id);
   101     int _did_elect_new_key = 0;
   102 
   103     if (_no_user_id)
   104     {
   105         status = get_identity(session, identity->address, PEP_OWN_USERID,
   106                 &stored_identity);
   107         if (status == PEP_STATUS_OK) {
   108             free_identity(stored_identity);
   109             return _myself(session, identity, false, true);
   110         }
   111 
   112         free(identity->user_id);
   113 
   114         identity->user_id = calloc(1, strlen(identity->address) + 6);
   115         if (!identity->user_id)
   116         {
   117             return PEP_OUT_OF_MEMORY;
   118         }
   119         snprintf(identity->user_id, strlen(identity->address) + 6,
   120                  "TOFU_%s", identity->address);
   121     }
   122  
   123     status = get_identity(session,
   124                           identity->address,
   125                           identity->user_id,
   126                           &stored_identity);
   127     
   128     assert(status != PEP_OUT_OF_MEMORY);
   129     if (status == PEP_OUT_OF_MEMORY)
   130         goto exit_free;
   131 
   132     temp_id = identity_dup(identity);
   133     
   134     /* We don't take given fpr. 
   135        In case there's no acceptable stored fpr, it will be elected. */
   136     free(temp_id->fpr);
   137     temp_id->fpr = NULL;
   138     temp_id->comm_type = PEP_ct_unknown;
   139             
   140     if (stored_identity) {
   141         
   142         bool dont_use_stored_fpr = true;
   143 
   144         /* if we have a stored_identity fpr */
   145         if (!EMPTYSTR(stored_identity->fpr)) {
   146             bool revoked = false;
   147             status = key_revoked(session, stored_identity->fpr, &revoked);
   148             
   149             if (status != PEP_STATUS_OK || revoked)
   150                 dont_use_stored_fpr = true;
   151                 
   152             if (revoked) {
   153                 // Do stuff
   154                 status = update_trust_for_fpr(session, stored_identity->fpr, PEP_ct_key_revoked);
   155                 // What to do on failure? FIXME
   156                 status = replace_identities_fpr(session, stored_identity->fpr, "");
   157             }
   158             else {    
   159                 status = blacklist_is_listed(session, stored_identity->fpr, &dont_use_stored_fpr);
   160                 if (status != PEP_STATUS_OK)
   161                     dont_use_stored_fpr = true; 
   162             }
   163         }
   164             
   165 
   166         if (!dont_use_stored_fpr) {
   167             /* Check stored comm_type */
   168             PEP_comm_type _comm_type_key;
   169             status = get_key_rating(session, stored_identity->fpr, &_comm_type_key);
   170             assert(status != PEP_OUT_OF_MEMORY);
   171             if (status == PEP_OUT_OF_MEMORY) {
   172                 goto exit_free;
   173             }
   174             if (status == PEP_KEY_NOT_FOUND){
   175                 /* stored key was deleted from keyring. any other candidate ?*/
   176                 status = elect_pubkey(session, temp_id);
   177                 if (status != PEP_STATUS_OK) {
   178                     goto exit_free;
   179                 } else {
   180                     _did_elect_new_key = 1;
   181                 }
   182             } else {
   183                 temp_id->fpr = strdup(stored_identity->fpr);
   184                 assert(temp_id->fpr);
   185                 if (temp_id->fpr == NULL) {
   186                     status = PEP_OUT_OF_MEMORY;
   187                     goto exit_free;
   188                 }
   189 
   190                 if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
   191                     /* if key not good anymore, 
   192                        downgrade eventually trusted comm_type */
   193                     temp_id->comm_type = _comm_type_key;
   194                 } else {
   195                     /* otherwise take stored comm_type as-is except if 
   196                        is unknown or is expired (but key not expired anymore) */
   197                     temp_id->comm_type = stored_identity->comm_type;
   198                     if (temp_id->comm_type == PEP_ct_unknown ||
   199                         temp_id->comm_type == PEP_ct_key_expired) {
   200                         temp_id->comm_type = _comm_type_key;
   201                     }
   202                 }
   203             }
   204         }
   205         else {
   206             status = elect_pubkey(session, temp_id);
   207             if (status != PEP_STATUS_OK){
   208                 goto exit_free;
   209             } else {
   210                 _did_elect_new_key = 1;
   211             }
   212         }
   213         
   214         /* ok, from here on out, use temp_id */
   215         
   216         
   217         /* At this point, we either have a non-blacklisted fpr we can work */
   218         /* with, or we've got nada.                                        */
   219 
   220         if (EMPTYSTR(temp_id->fpr)) {
   221             /* nada : set comm_type accordingly */
   222             temp_id->comm_type = PEP_ct_key_not_found;
   223         }
   224         
   225         if (EMPTYSTR(temp_id->username)) {
   226             free(temp_id->username);
   227             temp_id->username = strdup(stored_identity->username);
   228             assert(temp_id->username);
   229             if (temp_id->username == NULL){
   230                 status = PEP_OUT_OF_MEMORY;
   231                 goto exit_free;
   232             }
   233         }
   234 
   235         if (temp_id->lang[0] == 0) {
   236             temp_id->lang[0] = stored_identity->lang[0];
   237             temp_id->lang[1] = stored_identity->lang[1];
   238             temp_id->lang[2] = 0;
   239         }
   240 
   241         temp_id->flags = stored_identity->flags;
   242     }
   243     else /* stored_identity == NULL */ {
   244         temp_id->flags = 0;
   245 
   246         /* We elect a pubkey */
   247         status = elect_pubkey(session, temp_id);
   248         if (status != PEP_STATUS_OK)
   249             goto exit_free;
   250         
   251         /* Work with the elected key */
   252         if (!EMPTYSTR(temp_id->fpr)) {
   253             
   254             PEP_comm_type _comm_type_key = temp_id->comm_type;
   255             
   256             _did_elect_new_key = 1;
   257 
   258             // We don't want to lose a previous trust entry!!!
   259             status = get_trust(session, temp_id);
   260 
   261             bool has_trust_status = (status == PEP_STATUS_OK);
   262 
   263             if (!has_trust_status)
   264                 temp_id->comm_type = _comm_type_key;
   265         }
   266     }
   267 
   268     if (temp_id->fpr == NULL) {
   269         temp_id->fpr = strdup("");
   270         if (temp_id->fpr == NULL) {
   271             status = PEP_OUT_OF_MEMORY;
   272             goto exit_free;
   273         }
   274     }
   275     
   276     
   277     status = PEP_STATUS_OK;
   278 
   279     if (temp_id->comm_type != PEP_ct_unknown && !EMPTYSTR(temp_id->user_id)) {
   280 
   281         if (EMPTYSTR(temp_id->username)) { // mitigate
   282             free(temp_id->username);
   283             temp_id->username = strdup("anonymous");
   284             assert(temp_id->username);
   285             if (temp_id->username == NULL){
   286                 status = PEP_OUT_OF_MEMORY;
   287                 goto exit_free;
   288             }
   289         }
   290 
   291         // Identity doesn't get stored if call was just about checking existing
   292         // user by address (i.e. no user id given but already stored)
   293         if (!(_no_user_id && stored_identity) || _did_elect_new_key)
   294         {
   295             status = set_identity(session, temp_id);
   296             assert(status == PEP_STATUS_OK);
   297             if (status != PEP_STATUS_OK) {
   298                 goto exit_free;
   299             }
   300         }
   301     }
   302 
   303     if (temp_id->comm_type != PEP_ct_compromized &&
   304             temp_id->comm_type < PEP_ct_strong_but_unconfirmed)
   305         if (session->examine_identity)
   306             session->examine_identity(temp_id, session->examine_management);
   307     
   308     /* ok, we got to the end. So we can assign the output identity */
   309     free(identity->address);
   310     identity->address = strdup(temp_id->address);
   311     free(identity->fpr);
   312     identity->fpr = strdup(temp_id->fpr);
   313     free(identity->user_id);
   314     identity->user_id = strdup(temp_id->user_id);
   315     free(identity->username);
   316     identity->username = strdup(temp_id->username ? temp_id->username : "anonymous");
   317     identity->comm_type = temp_id->comm_type;
   318     identity->lang[0] = temp_id->lang[0];
   319     identity->lang[1] = temp_id->lang[1];
   320     identity->lang[2] = 0;
   321     identity->flags = temp_id->flags;
   322 
   323 exit_free :
   324     free_identity(stored_identity);
   325     free_identity(temp_id);
   326     
   327     return ADD_TO_LOG(status);
   328 }
   329 
   330 PEP_STATUS elect_ownkey(
   331         PEP_SESSION session, pEp_identity * identity
   332     )
   333 {
   334     PEP_STATUS status;
   335     stringlist_t *keylist = NULL;
   336 
   337     free(identity->fpr);
   338     identity->fpr = NULL;
   339 
   340     status = find_private_keys(session, identity->address, &keylist);
   341     assert(status != PEP_OUT_OF_MEMORY);
   342     if (status == PEP_OUT_OF_MEMORY)
   343         return PEP_OUT_OF_MEMORY;
   344     
   345     if (keylist != NULL && keylist->value != NULL)
   346     {
   347         char *_fpr = NULL;
   348         identity->comm_type = PEP_ct_unknown;
   349 
   350         stringlist_t *_keylist;
   351         for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
   352             bool is_own = false;
   353             
   354             status = own_key_is_listed(session, _keylist->value, &is_own);
   355             assert(status == PEP_STATUS_OK);
   356             if (status != PEP_STATUS_OK) {
   357                 free_stringlist(keylist);
   358                 return status;
   359             }
   360             
   361             if (is_own)
   362             {
   363                 PEP_comm_type _comm_type_key;
   364                 
   365                 status = get_key_rating(session, _keylist->value, &_comm_type_key);
   366                 assert(status != PEP_OUT_OF_MEMORY);
   367                 if (status == PEP_OUT_OF_MEMORY) {
   368                     free_stringlist(keylist);
   369                     return PEP_OUT_OF_MEMORY;
   370                 }
   371                 
   372                 if (_comm_type_key != PEP_ct_compromized &&
   373                     _comm_type_key != PEP_ct_unknown)
   374                 {
   375                     if (identity->comm_type == PEP_ct_unknown ||
   376                         _comm_type_key > identity->comm_type)
   377                     {
   378                         identity->comm_type = _comm_type_key;
   379                         _fpr = _keylist->value;
   380                     }
   381                 }
   382             }
   383         }
   384         
   385         if (_fpr)
   386         {
   387             identity->fpr = strdup(_fpr);
   388             assert(identity->fpr);
   389             if (identity->fpr == NULL)
   390             {
   391                 free_stringlist(keylist);
   392                 return PEP_OUT_OF_MEMORY;
   393             }
   394         }
   395         free_stringlist(keylist);
   396     }
   397     return PEP_STATUS_OK;
   398 }
   399 
   400 PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
   401                                 bool* is_usable) {
   402     
   403     bool dont_use_fpr = true;
   404     
   405     PEP_STATUS status = blacklist_is_listed(session, fpr, &dont_use_fpr);
   406     if (status == PEP_STATUS_OK && !dont_use_fpr) {
   407         // Make sure there is a *private* key associated with this fpr
   408         bool has_private = false;
   409         status = contains_priv_key(session, fpr, &has_private);
   410 
   411         if (status == PEP_STATUS_OK)
   412             dont_use_fpr = !has_private;
   413     }
   414     
   415     *is_usable = !dont_use_fpr;
   416     
   417     return ADD_TO_LOG(status);
   418 }
   419 
   420 PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
   421 {
   422     pEp_identity *stored_identity = NULL;
   423     PEP_STATUS status;
   424 
   425     assert(session);
   426     assert(identity);
   427     assert(!EMPTYSTR(identity->address));
   428 
   429     assert(EMPTYSTR(identity->user_id) ||
   430            strcmp(identity->user_id, PEP_OWN_USERID) == 0);
   431 
   432     if (!(session && identity && !EMPTYSTR(identity->address) &&
   433             (EMPTYSTR(identity->user_id) ||
   434             strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
   435         return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
   436 
   437     identity->comm_type = PEP_ct_pEp;
   438     if(ignore_flags)
   439         identity->flags = 0;
   440     
   441     if (EMPTYSTR(identity->user_id))
   442     {
   443         free(identity->user_id);
   444         identity->user_id = strdup(PEP_OWN_USERID);
   445         assert(identity->user_id);
   446         if (identity->user_id == NULL)
   447             return PEP_OUT_OF_MEMORY;
   448     }
   449 
   450     if (EMPTYSTR(identity->username))
   451     {
   452         free(identity->username);
   453         identity->username = strdup("anonymous");
   454         assert(identity->username);
   455         if (identity->username == NULL)
   456             return PEP_OUT_OF_MEMORY;
   457     }
   458 
   459     DEBUG_LOG("myself", "debug", identity->address);
   460  
   461     status = get_identity(session,
   462                           identity->address,
   463                           identity->user_id,
   464                           &stored_identity);
   465     
   466     assert(status != PEP_OUT_OF_MEMORY);
   467     if (status == PEP_OUT_OF_MEMORY)
   468         return PEP_OUT_OF_MEMORY;
   469 
   470     bool dont_use_stored_fpr = true;
   471     bool dont_use_input_fpr = true;
   472         
   473     if (stored_identity)
   474     {
   475         if (EMPTYSTR(identity->fpr)) {
   476             
   477             bool has_private = false;
   478             
   479             status = _has_usable_priv_key(session, stored_identity->fpr, &has_private); 
   480             
   481             // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
   482             if (has_private) {
   483                 identity->fpr = strdup(stored_identity->fpr);
   484                 assert(identity->fpr);
   485                 if (identity->fpr == NULL)
   486                 {
   487                     return PEP_OUT_OF_MEMORY;
   488                 }
   489                 dont_use_stored_fpr = false;
   490             }
   491         }
   492         
   493         identity->flags = (identity->flags & 255) | stored_identity->flags;
   494         free_identity(stored_identity);
   495     }
   496     
   497     if (dont_use_stored_fpr && !EMPTYSTR(identity->fpr))
   498     {
   499         // App must have a good reason to give fpr, such as explicit
   500         // import of private key, or similar.
   501 
   502         // Take given fpr as-is.
   503 
   504         // BUT:
   505         // First check to see if it's blacklisted or private part is missing?
   506         bool has_private = false;
   507         
   508         status = _has_usable_priv_key(session, identity->fpr, &has_private); 
   509         
   510         // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
   511         if (has_private) {
   512             dont_use_input_fpr = false;
   513         }
   514     }
   515     
   516     // Ok, we failed to get keys either way, so let's elect one.
   517     if (dont_use_input_fpr && dont_use_stored_fpr)
   518     {
   519         status = elect_ownkey(session, identity);
   520         assert(status == PEP_STATUS_OK);
   521         if (status != PEP_STATUS_OK) {
   522             return ADD_TO_LOG(status);
   523         }
   524 
   525         bool has_private = false;
   526         if (identity->fpr) {
   527             // ok, we elected something.
   528             // elect_ownkey only returns private keys, so we don't check again.
   529             // Check to see if it's blacklisted
   530             bool listed;
   531             status = blacklist_is_listed(session, identity->fpr, &listed); 
   532 
   533             if (status == PEP_STATUS_OK)
   534                 has_private = !listed;
   535         }
   536         
   537         if (has_private) {
   538             dont_use_input_fpr = false;
   539         }
   540         else { // OK, we've tried everything. Time to generate new keys.
   541             free(identity->fpr); // It can stay in this state (unallocated) because we'll generate a new key 
   542             identity->fpr = NULL;
   543         }
   544     }
   545 
   546     bool revoked = false;
   547     char *r_fpr = NULL;
   548     if (!EMPTYSTR(identity->fpr))
   549     {
   550         status = key_revoked(session, identity->fpr, &revoked);
   551 
   552         if (status != PEP_STATUS_OK) 
   553         {
   554             return ADD_TO_LOG(status);
   555         }
   556     }
   557    
   558     bool new_key_generated = false;
   559 
   560     if (EMPTYSTR(identity->fpr) || revoked)
   561     {
   562         if(!do_keygen){
   563             return ADD_TO_LOG(PEP_GET_KEY_FAILED);
   564         }
   565 
   566         if(revoked)
   567         {
   568             r_fpr = identity->fpr;
   569             identity->fpr = NULL;
   570         }
   571         
   572         DEBUG_LOG("generating key pair", "debug", identity->address);
   573         status = generate_keypair(session, identity);
   574         assert(status != PEP_OUT_OF_MEMORY);
   575         if (status != PEP_STATUS_OK) {
   576             char buf[11];
   577             snprintf(buf, 11, "%d", status);
   578             DEBUG_LOG("generating key pair failed", "debug", buf);
   579             if(revoked && r_fpr)
   580                 free(r_fpr);
   581             return ADD_TO_LOG(status);
   582         }
   583 
   584         new_key_generated = true;
   585         
   586         if(revoked)
   587         {
   588             status = set_revoked(session, r_fpr,
   589                                  identity->fpr, time(NULL));
   590             free(r_fpr);
   591             if (status != PEP_STATUS_OK) {
   592                 return ADD_TO_LOG(status);
   593             }
   594         }
   595     }
   596     else
   597     {
   598         bool expired;
   599         status = key_expired(session, identity->fpr, 
   600                              time(NULL) + (7*24*3600), // In a week
   601                              &expired);
   602 
   603         assert(status == PEP_STATUS_OK);
   604         if (status != PEP_STATUS_OK) {
   605             return ADD_TO_LOG(status);
   606         }
   607 
   608         if (status == PEP_STATUS_OK && expired) {
   609             timestamp *ts = new_timestamp(time(NULL) + KEY_EXPIRE_DELTA);
   610             renew_key(session, identity->fpr, ts);
   611             free_timestamp(ts);
   612         }
   613     }
   614 
   615     if (!identity->username)
   616         identity->username = strdup("");
   617     
   618     status = set_identity(session, identity);
   619     assert(status == PEP_STATUS_OK);
   620     if (status != PEP_STATUS_OK) {
   621         return status;
   622     }
   623 
   624     if(new_key_generated)
   625     {
   626         // if a state machine for keysync is in place, inject notify
   627         status = inject_DeviceState_event(session, KeyGen, NULL, NULL);
   628         if (status == PEP_OUT_OF_MEMORY){
   629             return PEP_OUT_OF_MEMORY;
   630         }
   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             break;
   854             
   855         default:
   856             status = PEP_UNKNOWN_ERROR;
   857     }
   858     
   859     sqlite3_reset(session->own_key_is_listed);
   860     return status;
   861 }
   862 
   863 PEP_STATUS _own_identities_retrieve(
   864         PEP_SESSION session,
   865         identity_list **own_identities,
   866         identity_flags_t excluded_flags
   867       )
   868 {
   869     PEP_STATUS status = PEP_STATUS_OK;
   870     
   871     assert(session && own_identities);
   872     if (!(session && own_identities))
   873         return PEP_ILLEGAL_VALUE;
   874     
   875     *own_identities = NULL;
   876     identity_list *_own_identities = new_identity_list(NULL);
   877     if (_own_identities == NULL)
   878         goto enomem;
   879     
   880     sqlite3_reset(session->own_identities_retrieve);
   881     
   882     int result;
   883     // address, fpr, username, user_id, comm_type, lang, flags
   884     const char *address = NULL;
   885     const char *fpr = NULL;
   886     const char *username = NULL;
   887     const char *user_id = NULL;
   888     PEP_comm_type comm_type = PEP_ct_unknown;
   889     const char *lang = NULL;
   890     unsigned int flags = 0;
   891     
   892     identity_list *_bl = _own_identities;
   893     do {
   894         sqlite3_bind_int(session->own_identities_retrieve, 1, excluded_flags);
   895         result = sqlite3_step(session->own_identities_retrieve);
   896         switch (result) {
   897             case SQLITE_ROW:
   898                 address = (const char *)
   899                     sqlite3_column_text(session->own_identities_retrieve, 0);
   900                 fpr = (const char *)
   901                     sqlite3_column_text(session->own_identities_retrieve, 1);
   902                 user_id = PEP_OWN_USERID;
   903                 username = (const char *)
   904                     sqlite3_column_text(session->own_identities_retrieve, 2);
   905                 comm_type = PEP_ct_pEp;
   906                 lang = (const char *)
   907                     sqlite3_column_text(session->own_identities_retrieve, 3);
   908                 flags = (unsigned int)
   909                     sqlite3_column_int(session->own_identities_retrieve, 4);
   910 
   911                 pEp_identity *ident = new_identity(address, fpr, user_id, username);
   912                 if (!ident)
   913                     goto enomem;
   914                 ident->comm_type = comm_type;
   915                 if (lang && lang[0]) {
   916                     ident->lang[0] = lang[0];
   917                     ident->lang[1] = lang[1];
   918                     ident->lang[2] = 0;
   919                 }
   920                 ident->flags = flags;
   921 
   922                 _bl = identity_list_add(_bl, ident);
   923                 if (_bl == NULL) {
   924                     free_identity(ident);
   925                     goto enomem;
   926                 }
   927                 
   928                 break;
   929                 
   930             case SQLITE_DONE:
   931                 break;
   932                 
   933             default:
   934                 status = PEP_UNKNOWN_ERROR;
   935                 result = SQLITE_DONE;
   936         }
   937     } while (result != SQLITE_DONE);
   938     
   939     sqlite3_reset(session->own_identities_retrieve);
   940     if (status == PEP_STATUS_OK)
   941         *own_identities = _own_identities;
   942     else
   943         free_identity_list(_own_identities);
   944     
   945     goto the_end;
   946     
   947 enomem:
   948     free_identity_list(_own_identities);
   949     status = PEP_OUT_OF_MEMORY;
   950     
   951 the_end:
   952     return status;
   953 }
   954 
   955 DYNAMIC_API PEP_STATUS own_identities_retrieve(
   956         PEP_SESSION session,
   957         identity_list **own_identities
   958       )
   959 {
   960     return _own_identities_retrieve(session, own_identities, 0);
   961 }
   962 
   963 PEP_STATUS _own_keys_retrieve(
   964         PEP_SESSION session,
   965         stringlist_t **keylist,
   966         identity_flags_t excluded_flags
   967       )
   968 {
   969     PEP_STATUS status = PEP_STATUS_OK;
   970     
   971     assert(session && keylist);
   972     if (!(session && keylist))
   973         return PEP_ILLEGAL_VALUE;
   974     
   975     *keylist = NULL;
   976     stringlist_t *_keylist = NULL;
   977     
   978     sqlite3_reset(session->own_keys_retrieve);
   979     
   980     int result;
   981     char *fpr = NULL;
   982     
   983     stringlist_t *_bl = _keylist;
   984     do {
   985         sqlite3_bind_int(session->own_keys_retrieve, 1, excluded_flags);
   986         result = sqlite3_step(session->own_keys_retrieve);
   987         switch (result) {
   988             case SQLITE_ROW:
   989                 fpr = strdup((const char *) sqlite3_column_text(session->own_keys_retrieve, 0));
   990                 if(fpr == NULL)
   991                     goto enomem;
   992 
   993                 _bl = stringlist_add(_bl, fpr);
   994                 if (_bl == NULL) {
   995                     free(fpr);
   996                     goto enomem;
   997                 }
   998                 if (_keylist == NULL)
   999                     _keylist = _bl;
  1000                 
  1001                 break;
  1002                 
  1003             case SQLITE_DONE:
  1004                 break;
  1005                 
  1006             default:
  1007                 status = PEP_UNKNOWN_ERROR;
  1008                 result = SQLITE_DONE;
  1009         }
  1010     } while (result != SQLITE_DONE);
  1011     
  1012     sqlite3_reset(session->own_keys_retrieve);
  1013     if (status == PEP_STATUS_OK)
  1014         *keylist = _keylist;
  1015     else
  1016         free_stringlist(_keylist);
  1017     
  1018     goto the_end;
  1019     
  1020 enomem:
  1021     free_stringlist(_keylist);
  1022     status = PEP_OUT_OF_MEMORY;
  1023     
  1024 the_end:
  1025     return status;
  1026 }
  1027 
  1028 DYNAMIC_API PEP_STATUS own_keys_retrieve(PEP_SESSION session, stringlist_t **keylist)
  1029 {
  1030     return _own_keys_retrieve(session, keylist, 0);
  1031 }
  1032 
  1033 // FIXME: should it be be used when sync receive old keys ? (ENGINE-145)
  1034 DYNAMIC_API PEP_STATUS set_own_key(
  1035        PEP_SESSION session,
  1036        const char *address,
  1037        const char *fpr
  1038     )
  1039 {
  1040     PEP_STATUS status = PEP_STATUS_OK;
  1041     
  1042     assert(session &&
  1043            address &&
  1044            fpr && fpr[0]
  1045           );
  1046     
  1047     if (!(session &&
  1048           address &&
  1049           fpr && fpr[0]
  1050          ))
  1051         return PEP_ILLEGAL_VALUE;
  1052     
  1053     sqlite3_reset(session->set_own_key);
  1054     sqlite3_bind_text(session->set_own_key, 1, address, -1, SQLITE_STATIC);
  1055     sqlite3_bind_text(session->set_own_key, 2, fpr, -1, SQLITE_STATIC);
  1056 
  1057     int result;
  1058     
  1059     result = sqlite3_step(session->set_own_key);
  1060     switch (result) {
  1061         case SQLITE_DONE:
  1062             status = PEP_STATUS_OK;
  1063             break;
  1064             
  1065         default:
  1066             status = PEP_UNKNOWN_ERROR;
  1067     }
  1068     
  1069     sqlite3_reset(session->set_own_key);
  1070     return status;
  1071 }
  1072 
  1073 PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
  1074                              bool *has_private) {
  1075 
  1076     assert(session);
  1077     assert(fpr);
  1078     assert(has_private);
  1079     
  1080     if (!(session && fpr && has_private))
  1081         return PEP_ILLEGAL_VALUE;
  1082 
  1083     return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
  1084 }