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