src/keymanagement.c
author Roker <roker@pep-project.org>
Mon, 08 May 2017 08:18:47 +0200
branchENGINE-199
changeset 1753 01faac16cec6
parent 1734 6d5ea8793614
parent 1738 a7c4c6241b93
child 1764 e1c31ee55aad
permissions -rw-r--r--
merge "default" into "ENGINE-199"
     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             if (session->use_only_own_private_keys)
   343             {
   344                 status = own_key_is_listed(session, _keylist->value, &is_own);
   345                 assert(status == PEP_STATUS_OK);
   346                 if (status != PEP_STATUS_OK) {
   347                     free_stringlist(keylist);
   348                     return status;
   349                 }
   350             }
   351 
   352             // TODO : also accept synchronized device group keys ?
   353             
   354             if (!session->use_only_own_private_keys || is_own)
   355             {
   356                 PEP_comm_type _comm_type_key;
   357                 
   358                 status = get_key_rating(session, _keylist->value, &_comm_type_key);
   359                 assert(status != PEP_OUT_OF_MEMORY);
   360                 if (status == PEP_OUT_OF_MEMORY) {
   361                     free_stringlist(keylist);
   362                     return PEP_OUT_OF_MEMORY;
   363                 }
   364                 
   365                 if (_comm_type_key != PEP_ct_compromized &&
   366                     _comm_type_key != PEP_ct_unknown)
   367                 {
   368                     if (identity->comm_type == PEP_ct_unknown ||
   369                         _comm_type_key > identity->comm_type)
   370                     {
   371                         identity->comm_type = _comm_type_key;
   372                         _fpr = _keylist->value;
   373                     }
   374                 }
   375             }
   376         }
   377         
   378         if (_fpr)
   379         {
   380             identity->fpr = strdup(_fpr);
   381             assert(identity->fpr);
   382             if (identity->fpr == NULL)
   383             {
   384                 free_stringlist(keylist);
   385                 return PEP_OUT_OF_MEMORY;
   386             }
   387         }
   388         free_stringlist(keylist);
   389     }
   390     return PEP_STATUS_OK;
   391 }
   392 
   393 PEP_STATUS _has_usable_priv_key(PEP_SESSION session, char* fpr,
   394                                 bool* is_usable) {
   395     
   396     bool dont_use_fpr = true;
   397     
   398     PEP_STATUS status = blacklist_is_listed(session, fpr, &dont_use_fpr);
   399     if (status == PEP_STATUS_OK && !dont_use_fpr) {
   400         // Make sure there is a *private* key associated with this fpr
   401         bool has_private = false;
   402         status = contains_priv_key(session, fpr, &has_private);
   403 
   404         if (status == PEP_STATUS_OK)
   405             dont_use_fpr = !has_private;
   406     }
   407     
   408     *is_usable = !dont_use_fpr;
   409     
   410     return ERROR(status);
   411 }
   412 
   413 PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
   414 {
   415     pEp_identity *stored_identity = NULL;
   416     PEP_STATUS status;
   417 
   418     assert(session);
   419     assert(identity);
   420     assert(!EMPTYSTR(identity->address));
   421 
   422     assert(EMPTYSTR(identity->user_id) ||
   423            strcmp(identity->user_id, PEP_OWN_USERID) == 0);
   424 
   425     if (!(session && identity && !EMPTYSTR(identity->address) &&
   426             (EMPTYSTR(identity->user_id) ||
   427             strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
   428         return ERROR(PEP_ILLEGAL_VALUE);
   429 
   430     identity->comm_type = PEP_ct_pEp;
   431     identity->me = true;
   432     if(ignore_flags)
   433         identity->flags = 0;
   434     
   435     if (EMPTYSTR(identity->user_id))
   436     {
   437         free(identity->user_id);
   438         identity->user_id = strdup(PEP_OWN_USERID);
   439         assert(identity->user_id);
   440         if (identity->user_id == NULL)
   441             return PEP_OUT_OF_MEMORY;
   442     }
   443 
   444     if (EMPTYSTR(identity->username))
   445     {
   446         free(identity->username);
   447         identity->username = strdup("anonymous");
   448         assert(identity->username);
   449         if (identity->username == NULL)
   450             return PEP_OUT_OF_MEMORY;
   451     }
   452 
   453     DEBUG_LOG("myself", "debug", identity->address);
   454  
   455     status = get_identity(session,
   456                           identity->address,
   457                           identity->user_id,
   458                           &stored_identity);
   459     
   460     assert(status != PEP_OUT_OF_MEMORY);
   461     if (status == PEP_OUT_OF_MEMORY)
   462         return PEP_OUT_OF_MEMORY;
   463 
   464     bool dont_use_stored_fpr = true;
   465     bool dont_use_input_fpr = true;
   466         
   467     if (stored_identity)
   468     {
   469         if (EMPTYSTR(identity->fpr)) {
   470             
   471             bool has_private = false;
   472             
   473             status = _has_usable_priv_key(session, stored_identity->fpr, &has_private); 
   474             
   475             // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
   476             if (has_private) {
   477                 identity->fpr = strdup(stored_identity->fpr);
   478                 assert(identity->fpr);
   479                 if (identity->fpr == NULL)
   480                 {
   481                     return PEP_OUT_OF_MEMORY;
   482                 }
   483                 dont_use_stored_fpr = false;
   484             }
   485         }
   486         
   487         identity->flags = (identity->flags & 255) | stored_identity->flags;
   488         free_identity(stored_identity);
   489     }
   490     
   491     if (dont_use_stored_fpr && !EMPTYSTR(identity->fpr))
   492     {
   493         // App must have a good reason to give fpr, such as explicit
   494         // import of private key, or similar.
   495 
   496         // Take given fpr as-is.
   497 
   498         // BUT:
   499         // First check to see if it's blacklisted or private part is missing?
   500         bool has_private = false;
   501         
   502         status = _has_usable_priv_key(session, identity->fpr, &has_private); 
   503         
   504         // N.B. has_private is never true if the returned status is not PEP_STATUS_OK
   505         if (has_private) {
   506             dont_use_input_fpr = false;
   507         }
   508     }
   509     
   510     // Ok, we failed to get keys either way, so let's elect one.
   511     if (dont_use_input_fpr && dont_use_stored_fpr)
   512     {
   513         status = elect_ownkey(session, identity);
   514         assert(status == PEP_STATUS_OK);
   515         if (status != PEP_STATUS_OK) {
   516             return ERROR(status);
   517         }
   518 
   519         bool has_private = false;
   520         if (identity->fpr) {
   521             // ok, we elected something.
   522             // elect_ownkey only returns private keys, so we don't check again.
   523             // Check to see if it's blacklisted
   524             bool listed;
   525             status = blacklist_is_listed(session, identity->fpr, &listed); 
   526 
   527             if (status == PEP_STATUS_OK)
   528                 has_private = !listed;
   529         }
   530         
   531         if (has_private) {
   532             dont_use_input_fpr = false;
   533         }
   534         else { // OK, we've tried everything. Time to generate new keys.
   535             free(identity->fpr); // It can stay in this state (unallocated) because we'll generate a new key 
   536             identity->fpr = NULL;
   537         }
   538     }
   539 
   540     bool revoked = false;
   541     char *r_fpr = NULL;
   542     if (!EMPTYSTR(identity->fpr))
   543     {
   544         status = key_revoked(session, identity->fpr, &revoked);
   545 
   546         // Forces re-election if key is missing and own-key-only not forced
   547         if (!session->use_only_own_private_keys && status == PEP_KEY_NOT_FOUND) 
   548         {
   549             status = elect_ownkey(session, identity);
   550             assert(status == PEP_STATUS_OK);
   551             if (status != PEP_STATUS_OK) {
   552                 return ERROR(status);
   553             }
   554         } 
   555         else 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 // TODO: Unused for now, but should 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 && address[0] &&
  1024            fpr && fpr[0]
  1025           );
  1026     
  1027     if (!(session &&
  1028           address && address[0] &&
  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 }