src/keymanagement.c
author Edouard Tisserant <edouard@pep-project.org>
Wed, 09 Aug 2017 14:23:23 +0200
branchENGINE-226
changeset 1945 9503c962f746
parent 1853 a86d6290d493
child 2089 174f5577c0aa
child 2129 fd3108a623c5
permissions -rw-r--r--
ENGINE-226 identity->me => _identity_me()
     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         status = mark_as_compromized(session, ident->fpr);
   727     }
   728 
   729     return status;
   730 }
   731 
   732 DYNAMIC_API PEP_STATUS key_reset_trust(
   733         PEP_SESSION session,
   734         pEp_identity *ident
   735     )
   736 {
   737     PEP_STATUS status = PEP_STATUS_OK;
   738 
   739     assert(session);
   740     assert(ident);
   741     assert(!_identity_me(ident));
   742     assert(!EMPTYSTR(ident->fpr));
   743     assert(!EMPTYSTR(ident->address));
   744     assert(!EMPTYSTR(ident->user_id));
   745 
   746     if (!(session && ident && !_identity_me(ident) && ident->fpr && ident->address &&
   747             ident->user_id))
   748         return PEP_ILLEGAL_VALUE;
   749 
   750     status = update_identity(session, ident);
   751     if (status != PEP_STATUS_OK)
   752         return status;
   753 
   754     if (ident->comm_type == PEP_ct_mistrusted)
   755         ident->comm_type = PEP_ct_unknown;
   756     else
   757         ident->comm_type &= ~PEP_ct_confirmed;
   758 
   759     status = set_identity(session, ident);
   760     if (status != PEP_STATUS_OK)
   761         return status;
   762 
   763     if (ident->comm_type == PEP_ct_unknown)
   764         status = update_identity(session, ident);
   765     return status;
   766 }
   767 
   768 DYNAMIC_API PEP_STATUS trust_personal_key(
   769         PEP_SESSION session,
   770         pEp_identity *ident
   771     )
   772 {
   773     PEP_STATUS status = PEP_STATUS_OK;
   774 
   775     assert(session);
   776     assert(ident);
   777     assert(!EMPTYSTR(ident->address));
   778     assert(!EMPTYSTR(ident->user_id));
   779     assert(!EMPTYSTR(ident->fpr));
   780 
   781     if (!ident || EMPTYSTR(ident->address) || EMPTYSTR(ident->user_id) ||
   782             EMPTYSTR(ident->fpr))
   783         return PEP_ILLEGAL_VALUE;
   784 
   785     status = update_identity(session, ident);
   786     if (status != PEP_STATUS_OK)
   787         return status;
   788 
   789     if (ident->comm_type > PEP_ct_strong_but_unconfirmed) {
   790         ident->comm_type |= PEP_ct_confirmed;
   791         status = set_identity(session, ident);
   792     }
   793     else {
   794         // MISSING: S/MIME has to be handled depending on trusted CAs
   795         status = PEP_CANNOT_SET_TRUST;
   796     }
   797 
   798     return status;
   799 }
   800 
   801 DYNAMIC_API PEP_STATUS own_key_is_listed(
   802         PEP_SESSION session,
   803         const char *fpr,
   804         bool *listed
   805     )
   806 {
   807     PEP_STATUS status = PEP_STATUS_OK;
   808     int count;
   809     
   810     assert(session && fpr && fpr[0] && listed);
   811     
   812     if (!(session && fpr && fpr[0] && listed))
   813         return PEP_ILLEGAL_VALUE;
   814     
   815     *listed = false;
   816     
   817     sqlite3_reset(session->own_key_is_listed);
   818     sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
   819     
   820     int result;
   821     
   822     result = sqlite3_step(session->own_key_is_listed);
   823     switch (result) {
   824         case SQLITE_ROW:
   825             count = sqlite3_column_int(session->own_key_is_listed, 0);
   826             *listed = count > 0;
   827             status = PEP_STATUS_OK;
   828             break;
   829             
   830         default:
   831             status = PEP_UNKNOWN_ERROR;
   832     }
   833     
   834     sqlite3_reset(session->own_key_is_listed);
   835     return status;
   836 }
   837 
   838 PEP_STATUS _own_identities_retrieve(
   839         PEP_SESSION session,
   840         identity_list **own_identities,
   841         identity_flags_t excluded_flags
   842       )
   843 {
   844     PEP_STATUS status = PEP_STATUS_OK;
   845     
   846     assert(session && own_identities);
   847     if (!(session && own_identities))
   848         return PEP_ILLEGAL_VALUE;
   849     
   850     *own_identities = NULL;
   851     identity_list *_own_identities = new_identity_list(NULL);
   852     if (_own_identities == NULL)
   853         goto enomem;
   854     
   855     sqlite3_reset(session->own_identities_retrieve);
   856     
   857     int result;
   858     // address, fpr, username, user_id, comm_type, lang, flags
   859     const char *address = NULL;
   860     const char *fpr = NULL;
   861     const char *username = NULL;
   862     const char *user_id = NULL;
   863     PEP_comm_type comm_type = PEP_ct_unknown;
   864     const char *lang = NULL;
   865     unsigned int flags = 0;
   866     
   867     identity_list *_bl = _own_identities;
   868     do {
   869         sqlite3_bind_int(session->own_identities_retrieve, 1, excluded_flags);
   870         result = sqlite3_step(session->own_identities_retrieve);
   871         switch (result) {
   872             case SQLITE_ROW:
   873                 address = (const char *)
   874                     sqlite3_column_text(session->own_identities_retrieve, 0);
   875                 fpr = (const char *)
   876                     sqlite3_column_text(session->own_identities_retrieve, 1);
   877                 user_id = PEP_OWN_USERID;
   878                 username = (const char *)
   879                     sqlite3_column_text(session->own_identities_retrieve, 2);
   880                 comm_type = PEP_ct_pEp;
   881                 lang = (const char *)
   882                     sqlite3_column_text(session->own_identities_retrieve, 3);
   883                 flags = (unsigned int)
   884                     sqlite3_column_int(session->own_identities_retrieve, 4);
   885 
   886                 pEp_identity *ident = new_identity(address, fpr, user_id, username);
   887                 if (!ident)
   888                     goto enomem;
   889                 ident->comm_type = comm_type;
   890                 if (lang && lang[0]) {
   891                     ident->lang[0] = lang[0];
   892                     ident->lang[1] = lang[1];
   893                     ident->lang[2] = 0;
   894                 }
   895                 ident->flags = flags;
   896 
   897                 _bl = identity_list_add(_bl, ident);
   898                 if (_bl == NULL) {
   899                     free_identity(ident);
   900                     goto enomem;
   901                 }
   902                 
   903                 break;
   904                 
   905             case SQLITE_DONE:
   906                 break;
   907                 
   908             default:
   909                 status = PEP_UNKNOWN_ERROR;
   910                 result = SQLITE_DONE;
   911         }
   912     } while (result != SQLITE_DONE);
   913     
   914     sqlite3_reset(session->own_identities_retrieve);
   915     if (status == PEP_STATUS_OK)
   916         *own_identities = _own_identities;
   917     else
   918         free_identity_list(_own_identities);
   919     
   920     goto the_end;
   921     
   922 enomem:
   923     free_identity_list(_own_identities);
   924     status = PEP_OUT_OF_MEMORY;
   925     
   926 the_end:
   927     return status;
   928 }
   929 
   930 DYNAMIC_API PEP_STATUS own_identities_retrieve(
   931         PEP_SESSION session,
   932         identity_list **own_identities
   933       )
   934 {
   935     return _own_identities_retrieve(session, own_identities, 0);
   936 }
   937 
   938 PEP_STATUS _own_keys_retrieve(
   939         PEP_SESSION session,
   940         stringlist_t **keylist,
   941         identity_flags_t excluded_flags
   942       )
   943 {
   944     PEP_STATUS status = PEP_STATUS_OK;
   945     
   946     assert(session && keylist);
   947     if (!(session && keylist))
   948         return PEP_ILLEGAL_VALUE;
   949     
   950     *keylist = NULL;
   951     stringlist_t *_keylist = NULL;
   952     
   953     sqlite3_reset(session->own_keys_retrieve);
   954     
   955     int result;
   956     char *fpr = NULL;
   957     
   958     stringlist_t *_bl = _keylist;
   959     do {
   960         sqlite3_bind_int(session->own_keys_retrieve, 1, excluded_flags);
   961         result = sqlite3_step(session->own_keys_retrieve);
   962         switch (result) {
   963             case SQLITE_ROW:
   964                 fpr = strdup((const char *) sqlite3_column_text(session->own_keys_retrieve, 0));
   965                 if(fpr == NULL)
   966                     goto enomem;
   967 
   968                 _bl = stringlist_add(_bl, fpr);
   969                 if (_bl == NULL) {
   970                     free(fpr);
   971                     goto enomem;
   972                 }
   973                 if (_keylist == NULL)
   974                     _keylist = _bl;
   975                 
   976                 break;
   977                 
   978             case SQLITE_DONE:
   979                 break;
   980                 
   981             default:
   982                 status = PEP_UNKNOWN_ERROR;
   983                 result = SQLITE_DONE;
   984         }
   985     } while (result != SQLITE_DONE);
   986     
   987     sqlite3_reset(session->own_keys_retrieve);
   988     if (status == PEP_STATUS_OK)
   989         *keylist = _keylist;
   990     else
   991         free_stringlist(_keylist);
   992     
   993     goto the_end;
   994     
   995 enomem:
   996     free_stringlist(_keylist);
   997     status = PEP_OUT_OF_MEMORY;
   998     
   999 the_end:
  1000     return status;
  1001 }
  1002 
  1003 DYNAMIC_API PEP_STATUS own_keys_retrieve(PEP_SESSION session, stringlist_t **keylist)
  1004 {
  1005     return _own_keys_retrieve(session, keylist, 0);
  1006 }
  1007 
  1008 // FIXME: should it be be used when sync receive old keys ? (ENGINE-145)
  1009 DYNAMIC_API PEP_STATUS set_own_key(
  1010        PEP_SESSION session,
  1011        const char *address,
  1012        const char *fpr
  1013     )
  1014 {
  1015     PEP_STATUS status = PEP_STATUS_OK;
  1016     
  1017     assert(session &&
  1018            address &&
  1019            fpr && fpr[0]
  1020           );
  1021     
  1022     if (!(session &&
  1023           address &&
  1024           fpr && fpr[0]
  1025          ))
  1026         return PEP_ILLEGAL_VALUE;
  1027     
  1028     sqlite3_reset(session->set_own_key);
  1029     sqlite3_bind_text(session->set_own_key, 1, address, -1, SQLITE_STATIC);
  1030     sqlite3_bind_text(session->set_own_key, 2, fpr, -1, SQLITE_STATIC);
  1031 
  1032     int result;
  1033     
  1034     result = sqlite3_step(session->set_own_key);
  1035     switch (result) {
  1036         case SQLITE_DONE:
  1037             status = PEP_STATUS_OK;
  1038             break;
  1039             
  1040         default:
  1041             status = PEP_UNKNOWN_ERROR;
  1042     }
  1043     
  1044     sqlite3_reset(session->set_own_key);
  1045     return status;
  1046 }
  1047 
  1048 PEP_STATUS contains_priv_key(PEP_SESSION session, const char *fpr,
  1049                              bool *has_private) {
  1050 
  1051     assert(session);
  1052     assert(fpr);
  1053     assert(has_private);
  1054     
  1055     if (!(session && fpr && has_private))
  1056         return PEP_ILLEGAL_VALUE;
  1057 
  1058     return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
  1059 }