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