src/pgp_gpg.c
author Krista Grothoff <krista@pep-project.org>
Wed, 15 Mar 2017 15:00:19 +0100
branchfdik_sync
changeset 1673 559931685411
parent 1639 be84d676f3ef
child 1675 369041ac3724
permissions -rw-r--r--
pgp functions + high-level sign-text in, not compiled yet.
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "platform.h"
     5 #include "pEp_internal.h"
     6 #include "pgp_gpg.h"
     7 
     8 #include <limits.h>
     9 
    10 #include "wrappers.h"
    11 
    12 #define _GPGERR(X) ((X) & 0xffffL)
    13 
    14 static void *gpgme;
    15 static struct gpg_s gpg;
    16 
    17 static bool ensure_config_values(stringlist_t *keys, stringlist_t *values, const char* config_file_path)
    18 {
    19     static char buf[MAX_LINELENGTH];
    20     int r;
    21     stringlist_t *_k;
    22     stringlist_t *_v;
    23     unsigned int i;
    24     unsigned int found = 0;
    25 
    26     FILE *f = Fopen(config_file_path, "r");
    27     if (f == NULL && errno == ENOMEM)
    28         return false;
    29 
    30     if (f != NULL) {
    31         int length = stringlist_length(keys);
    32         unsigned int n = (1 << length) - 1;
    33 
    34         // make sure we 1) have the same number of keys and values
    35         // and 2) we don't have more key/value pairs than
    36         // the size of the bitfield used to hold the indices
    37         // of key/value pairs matching keys in the config file.
    38         assert(length <= sizeof(unsigned int) * CHAR_BIT);
    39         assert(length == stringlist_length(values));
    40         if (!(length == stringlist_length(values) &&
    41               length <= sizeof(unsigned int) * CHAR_BIT)) {
    42             r = Fclose(f);
    43             assert(r == 0);
    44 
    45             return false;
    46         }
    47 
    48         do {
    49             char * s = Fgets(buf, MAX_LINELENGTH, f);
    50             if (!feof(f)) {
    51                 assert(s);
    52                 if (s == NULL)
    53                     return false;
    54 
    55                 if (s && !feof(f)) {
    56                     char * rest;
    57                     char * t = strtok_r(s, " ", &rest);
    58                     for (i = 1, _k = keys, _v = values; _k != NULL;
    59                             _k = _k->next, _v = _v->next, i <<= 1) {
    60                         if (t && strncmp(t, _k->value, strlen(_k->value)) == 0)
    61                             found |= i;
    62 
    63                         if (i == n) {
    64                             r = Fclose(f);
    65                             return true;
    66                         }
    67                     }
    68                 }
    69             }
    70         } while (!feof(f));
    71         f = Freopen(config_file_path, "a", f);
    72     }
    73     else {
    74         f = Fopen(config_file_path, "w");
    75     }
    76 
    77     assert(f);
    78     if (f == NULL)
    79         return false;
    80 
    81     for (i = 1, _k = keys, _v = values; _k != NULL; _k = _k->next,
    82             _v = _v->next, i <<= 1) {
    83         if ((found & i) == 0) {
    84             r = Fprintf(f, "%s %s\n", _k->value, _v->value);
    85             assert(r >= 0);
    86         }
    87     }
    88 
    89     r = Fclose(f);
    90     assert(r == 0);
    91 
    92     return true;
    93 }
    94 
    95 
    96 PEP_STATUS pgp_init(PEP_SESSION session, bool in_first)
    97 {
    98     PEP_STATUS status = PEP_STATUS_OK;
    99     gpgme_error_t gpgme_error;
   100     bool bResult;
   101 
   102     if (in_first) {
   103         stringlist_t *conf_keys   = new_stringlist("keyserver");
   104         stringlist_t *conf_values = new_stringlist("hkp://keys.gnupg.net");
   105 
   106         stringlist_add(conf_keys, "cert-digest-algo");
   107         stringlist_add(conf_values, "SHA256");
   108 
   109         stringlist_add(conf_keys, "no-emit-version");
   110         stringlist_add(conf_values, "");
   111 
   112         stringlist_add(conf_keys, "no-comments");
   113         stringlist_add(conf_values, "");
   114 
   115         stringlist_add(conf_keys, "personal-cipher-preferences");
   116         stringlist_add(conf_values, "AES AES256 AES192 CAST5");
   117 
   118         stringlist_add(conf_keys, "personal-digest-preferences");
   119         stringlist_add(conf_values, "SHA256 SHA512 SHA384 SHA224");
   120 
   121         stringlist_add(conf_keys, "ignore-time-conflict");
   122         stringlist_add(conf_values, "");
   123 
   124         bResult = ensure_config_values(conf_keys, conf_values, gpg_conf());
   125 
   126         free_stringlist(conf_keys);
   127         free_stringlist(conf_values);
   128 
   129         assert(bResult);
   130         if(!bResult){
   131             status = PEP_INIT_NO_GPG_HOME;
   132             goto pep_error;
   133         }
   134 
   135         conf_keys = new_stringlist("default-cache-ttl");
   136         conf_values = new_stringlist("300");
   137 
   138         stringlist_add(conf_keys, "max-cache-ttl");
   139         stringlist_add(conf_values, "1200");
   140 
   141         bResult = ensure_config_values(conf_keys, conf_values, gpg_agent_conf());
   142 
   143         free_stringlist(conf_keys);
   144         free_stringlist(conf_values);
   145 
   146         assert(bResult);
   147         if(!bResult){
   148             status = PEP_INIT_NO_GPG_HOME; /* FIXME: Wrong error here? */
   149             goto pep_error;
   150         }
   151 
   152         gpgme = dlopen(LIBGPGME, RTLD_LAZY);
   153         if (gpgme == NULL) {
   154             status = PEP_INIT_CANNOT_LOAD_GPGME;
   155             goto pep_error;
   156         }
   157 
   158         memset(&gpg, 0, sizeof(struct gpg_s));
   159 
   160         gpg.gpgme_set_locale
   161             = (gpgme_set_locale_t) (intptr_t) dlsym(gpgme,
   162             "gpgme_set_locale");
   163         assert(gpg.gpgme_set_locale);
   164 
   165         gpg.gpgme_check
   166             = (gpgme_check_version_t) (intptr_t) dlsym(gpgme,
   167             "gpgme_check_version");
   168         assert(gpg.gpgme_check);
   169 
   170         gpg.gpgme_new
   171             = (gpgme_new_t) (intptr_t) dlsym(gpgme, "gpgme_new");
   172         assert(gpg.gpgme_new);
   173 
   174         gpg.gpgme_release
   175             = (gpgme_release_t) (intptr_t) dlsym(gpgme, "gpgme_release");
   176         assert(gpg.gpgme_release);
   177 
   178         gpg.gpgme_get_engine_info
   179             = (gpgme_get_engine_info_t) (intptr_t) dlsym(gpgme,
   180             "gpgme_get_engine_info");
   181         assert(gpg.gpgme_get_engine_info);
   182 
   183         gpg.gpgme_set_protocol
   184             = (gpgme_set_protocol_t) (intptr_t) dlsym(gpgme,
   185             "gpgme_set_protocol");
   186         assert(gpg.gpgme_set_protocol);
   187 
   188         gpg.gpgme_set_armor
   189             = (gpgme_set_armor_t) (intptr_t) dlsym(gpgme,
   190             "gpgme_set_armor");
   191         assert(gpg.gpgme_set_armor);
   192 
   193         gpg.gpgme_data_new
   194             = (gpgme_data_new_t) (intptr_t) dlsym(gpgme,
   195             "gpgme_data_new");
   196         assert(gpg.gpgme_data_new);
   197 
   198         gpg.gpgme_data_new_from_mem
   199             = (gpgme_data_new_from_mem_t) (intptr_t) dlsym(gpgme,
   200             "gpgme_data_new_from_mem");
   201         assert(gpg.gpgme_data_new_from_mem);
   202 
   203         gpg.gpgme_data_new_from_cbs
   204             = (gpgme_data_new_from_cbs_t) (intptr_t) dlsym(gpgme,
   205             "gpgme_data_new_from_cbs");
   206         assert(gpg.gpgme_data_new_from_cbs);
   207 
   208         gpg.gpgme_data_release
   209             = (gpgme_data_release_t) (intptr_t) dlsym(gpgme,
   210             "gpgme_data_release");
   211         assert(gpg.gpgme_data_release);
   212 
   213         gpg.gpgme_data_identify
   214             = (gpgme_data_identify_t) (intptr_t) dlsym(gpgme,
   215             "gpgme_data_identify");
   216         assert(gpg.gpgme_data_identify);
   217 
   218         gpg.gpgme_data_seek
   219             = (gpgme_data_seek_t) (intptr_t) dlsym(gpgme,
   220             "gpgme_data_seek");
   221         assert(gpg.gpgme_data_seek);
   222 
   223         gpg.gpgme_data_read
   224             = (gpgme_data_read_t) (intptr_t) dlsym(gpgme,
   225             "gpgme_data_read");
   226         assert(gpg.gpgme_data_read);
   227 
   228         gpg.gpgme_op_decrypt
   229             = (gpgme_op_decrypt_t) (intptr_t) dlsym(gpgme,
   230             "gpgme_op_decrypt");
   231         assert(gpg.gpgme_op_decrypt);
   232 
   233         gpg.gpgme_op_verify
   234             = (gpgme_op_verify_t) (intptr_t) dlsym(gpgme,
   235             "gpgme_op_verify");
   236         assert(gpg.gpgme_op_verify);
   237 
   238         gpg.gpgme_op_decrypt_verify
   239             = (gpgme_op_decrypt_verify_t) (intptr_t) dlsym(gpgme,
   240             "gpgme_op_decrypt_verify");
   241         assert(gpg.gpgme_op_decrypt_verify);
   242 
   243         gpg.gpgme_op_decrypt_result
   244             = (gpgme_op_decrypt_result_t) (intptr_t) dlsym(gpgme,
   245             "gpgme_op_decrypt_result");
   246         assert(gpg.gpgme_op_decrypt_result);
   247 
   248         gpg.gpgme_op_encrypt_sign
   249             = (gpgme_op_encrypt_sign_t) (intptr_t) dlsym(gpgme,
   250             "gpgme_op_encrypt_sign");
   251         assert(gpg.gpgme_op_encrypt_sign);
   252 
   253         gpg.gpgme_op_encrypt
   254             = (gpgme_op_encrypt_t) (intptr_t) dlsym(gpgme,
   255             "gpgme_op_encrypt");
   256         assert(gpg.gpgme_op_encrypt);
   257 
   258         gpg.gpgme_op_verify_result
   259             = (gpgme_op_verify_result_t) (intptr_t) dlsym(gpgme,
   260             "gpgme_op_verify_result");
   261         assert(gpg.gpgme_op_verify_result);
   262 
   263         gpg.gpgme_signers_clear
   264             = (gpgme_signers_clear_t) (intptr_t) dlsym(gpgme,
   265             "gpgme_signers_clear");
   266         assert(gpg.gpgme_signers_clear);
   267 
   268         gpg.gpgme_signers_add
   269             = (gpgme_signers_add_t) (intptr_t) dlsym(gpgme,
   270             "gpgme_signers_add");
   271         assert(gpg.gpgme_signers_add);
   272 
   273         gpg.gpgme_get_key
   274             = (gpgme_get_key_t) (intptr_t) dlsym(gpgme, "gpgme_get_key");
   275         assert(gpg.gpgme_get_key);
   276 
   277         gpg.gpgme_op_genkey
   278             = (gpgme_op_genkey_t) (intptr_t) dlsym(gpgme,
   279             "gpgme_op_genkey");
   280         assert(gpg.gpgme_op_genkey);
   281 
   282         gpg.gpgme_op_genkey_result
   283             = (gpgme_op_genkey_result_t) (intptr_t) dlsym(gpgme,
   284             "gpgme_op_genkey_result");
   285         assert(gpg.gpgme_op_genkey_result);
   286 
   287         gpg.gpgme_op_delete = (gpgme_op_delete_t) (intptr_t)
   288             dlsym(gpgme, "gpgme_op_delete");
   289         assert(gpg.gpgme_op_delete);
   290 
   291         gpg.gpgme_op_import = (gpgme_op_import_t) (intptr_t)
   292             dlsym(gpgme, "gpgme_op_import");
   293         assert(gpg.gpgme_op_import);
   294 
   295         gpg.gpgme_op_import_result
   296             = (gpgme_op_import_result_t) (intptr_t) dlsym(gpgme,
   297             "gpgme_op_import_result");
   298         assert(gpg.gpgme_op_import_result);
   299 
   300         gpg.gpgme_op_export = (gpgme_op_export_t) (intptr_t)
   301             dlsym(gpgme, "gpgme_op_export");
   302         assert(gpg.gpgme_op_export);
   303 
   304         gpg.gpgme_set_keylist_mode = (gpgme_set_keylist_mode_t) (intptr_t)
   305             dlsym(gpgme, "gpgme_set_keylist_mode");
   306         assert(gpg.gpgme_set_keylist_mode);
   307 
   308         gpg.gpgme_get_keylist_mode = (gpgme_get_keylist_mode_t) (intptr_t)
   309             dlsym(gpgme, "gpgme_get_keylist_mode");
   310         assert(gpg.gpgme_get_keylist_mode);
   311 
   312         gpg.gpgme_op_keylist_start = (gpgme_op_keylist_start_t) (intptr_t)
   313             dlsym(gpgme, "gpgme_op_keylist_start");
   314         assert(gpg.gpgme_op_keylist_start);
   315 
   316         gpg.gpgme_op_keylist_next = (gpgme_op_keylist_next_t) (intptr_t)
   317             dlsym(gpgme, "gpgme_op_keylist_next");
   318         assert(gpg.gpgme_op_keylist_next);
   319 
   320         gpg.gpgme_op_keylist_end = (gpgme_op_keylist_end_t) (intptr_t)
   321             dlsym(gpgme, "gpgme_op_keylist_end");
   322         assert(gpg.gpgme_op_keylist_end);
   323 
   324         gpg.gpgme_op_import_keys = (gpgme_op_import_keys_t) (intptr_t)
   325             dlsym(gpgme, "gpgme_op_import_keys");
   326         assert(gpg.gpgme_op_import_keys);
   327 
   328         gpg.gpgme_key_ref = (gpgme_key_ref_t) (intptr_t)
   329             dlsym(gpgme, "gpgme_key_ref");
   330         assert(gpg.gpgme_key_ref);
   331 
   332         gpg.gpgme_key_unref = (gpgme_key_unref_t) (intptr_t)
   333             dlsym(gpgme, "gpgme_key_unref");
   334         assert(gpg.gpgme_key_unref);
   335 
   336 		gpg.gpgme_key_release = (gpgme_key_release_t)(intptr_t)
   337 			dlsym(gpgme, "gpgme_key_release");
   338 		assert(gpg.gpgme_key_release);
   339 
   340         gpg.gpgme_op_edit = (gpgme_op_edit_t) (intptr_t)
   341             dlsym(gpgme, "gpgme_op_edit");
   342         assert(gpg.gpgme_op_edit);
   343 
   344         gpg.gpgme_io_write = (gpgme_io_write_t) (intptr_t)
   345             dlsym(gpgme, "gpgme_io_write");
   346         assert(gpg.gpgme_io_write);
   347 
   348         gpg.version = gpg.gpgme_check(NULL);
   349 
   350         const char * const cLocal = setlocale(LC_ALL, NULL);
   351         if (!cLocal || (strcmp(cLocal, "C") == 0))
   352             setlocale(LC_ALL, "");
   353 
   354         gpg.gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
   355 #ifdef LC_MESSAGES // Windoze
   356         gpg.gpgme_set_locale (NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
   357 #endif
   358     }
   359 
   360     gpg.gpgme_check(NULL);
   361     gpgme_error = gpg.gpgme_new(&session->ctx);
   362     gpgme_error = _GPGERR(gpgme_error);
   363     if (gpgme_error != GPG_ERR_NO_ERROR) {
   364         status = PEP_INIT_GPGME_INIT_FAILED;
   365         goto pep_error;
   366     }
   367     assert(session->ctx);
   368 
   369     gpgme_error = gpg.gpgme_set_protocol(session->ctx, GPGME_PROTOCOL_OpenPGP);
   370     gpgme_error = _GPGERR(gpgme_error);
   371     assert(gpgme_error == GPG_ERR_NO_ERROR);
   372 
   373     gpg.gpgme_set_armor(session->ctx, 1);
   374 
   375     return PEP_STATUS_OK;
   376 
   377 pep_error:
   378     pgp_release(session, in_first);
   379     return status;
   380 }
   381 
   382 void pgp_release(PEP_SESSION session, bool out_last)
   383 {
   384     if (session->ctx) {
   385         gpg.gpgme_release(session->ctx);
   386         session->ctx = NULL;
   387     }
   388 
   389     if (out_last)
   390         if (gpgme)
   391             dlclose(gpgme);
   392 }
   393 
   394 PEP_STATUS pgp_decrypt_and_verify(
   395     PEP_SESSION session, const char *ctext, size_t csize,
   396     const char *dsigtext, size_t dsigsize,
   397     char **ptext, size_t *psize, stringlist_t **keylist
   398     )
   399 {
   400     PEP_STATUS result;
   401     gpgme_error_t gpgme_error;
   402     gpgme_data_t cipher, plain;
   403     gpgme_data_type_t dt;
   404     gpgme_decrypt_result_t gpgme_decrypt_result = NULL;
   405 
   406     stringlist_t *_keylist = NULL;
   407     //int i_key = 0;
   408 
   409     assert(session);
   410     assert(ctext);
   411     assert(csize);
   412     assert(ptext);
   413     assert(psize);
   414     assert(keylist);
   415 
   416     *ptext = NULL;
   417     *psize = 0;
   418     *keylist = NULL;
   419 
   420     gpgme_error = gpg.gpgme_data_new_from_mem(&cipher, ctext, csize, 0);
   421     gpgme_error = _GPGERR(gpgme_error);
   422     assert(gpgme_error == GPG_ERR_NO_ERROR);
   423     if (gpgme_error != GPG_ERR_NO_ERROR) {
   424         if (gpgme_error == GPG_ERR_ENOMEM)
   425             return PEP_OUT_OF_MEMORY;
   426         else
   427             return PEP_UNKNOWN_ERROR;
   428     }
   429 
   430     gpgme_error = gpg.gpgme_data_new(&plain);
   431     gpgme_error = _GPGERR(gpgme_error);
   432     assert(gpgme_error == GPG_ERR_NO_ERROR);
   433     if (gpgme_error != GPG_ERR_NO_ERROR) {
   434         gpg.gpgme_data_release(cipher);
   435         if (gpgme_error == GPG_ERR_ENOMEM)
   436             return PEP_OUT_OF_MEMORY;
   437         else
   438             return PEP_UNKNOWN_ERROR;
   439     }
   440 
   441 
   442     dt = gpg.gpgme_data_identify(cipher);
   443     switch (dt) {
   444 #if GPGME_VERSION_NUMBER > 0x010600
   445     case GPGME_DATA_TYPE_PGP_ENCRYPTED:
   446 #endif
   447     case GPGME_DATA_TYPE_PGP_SIGNED:
   448     case GPGME_DATA_TYPE_PGP_OTHER:
   449         if (dsigtext) {
   450             gpgme_error = gpg.gpgme_op_decrypt(session->ctx, cipher, plain);
   451         }
   452         else {
   453             gpgme_error = gpg.gpgme_op_decrypt_verify(session->ctx, cipher,
   454                 plain);
   455         }
   456         gpgme_error = _GPGERR(gpgme_error);
   457         assert(gpgme_error != GPG_ERR_INV_VALUE);
   458         assert(gpgme_error != GPG_ERR_NO_DATA);
   459 
   460         switch (gpgme_error) {
   461             case GPG_ERR_NO_ERROR:
   462             {
   463                 gpgme_decrypt_result = gpg.gpgme_op_decrypt_result(session->ctx);
   464                 /* NOW is when we have to process the decrypt_result, period.
   465                    it is only valid until the next call on the context. */
   466                    
   467                 gpgme_key_t key;
   468                 memset(&key,0,sizeof(key));
   469                 stringlist_t* recipient_keylist = new_stringlist(NULL);
   470                 if (!recipient_keylist) {
   471                     gpg.gpgme_data_release(plain);
   472                     gpg.gpgme_data_release(cipher);
   473                     if (recipient_keylist)
   474                         free_stringlist(recipient_keylist);
   475                     return PEP_OUT_OF_MEMORY;
   476                 }
   477                
   478                 if (gpgme_decrypt_result != NULL) {
   479                     stringlist_t* _keylist = recipient_keylist;
   480                     for (gpgme_recipient_t r = gpgme_decrypt_result->recipients; r != NULL; r = r->next) {
   481                         // GPGME may give subkey's fpr instead of primary key's fpr.
   482                         // Therefore we ask for the primary fingerprint instead
   483                         // we assume that gpgme_get_key can find key by subkey's fpr
   484                         gpgme_error = gpg.gpgme_get_key(session->ctx,
   485                             r->keyid, &key, 0);
   486                         gpgme_error = _GPGERR(gpgme_error);
   487                         assert(gpgme_error != GPG_ERR_ENOMEM);
   488                         if (gpgme_error == GPG_ERR_ENOMEM) {
   489                             free_stringlist(_keylist);
   490                             result = PEP_OUT_OF_MEMORY;
   491                         }
   492                         // Primary key is given as the first subkey
   493                         if (gpgme_error == GPG_ERR_NO_ERROR &&
   494                             key && key->subkeys && key->subkeys->fpr
   495                             && key->subkeys->fpr[0]) {
   496                             _keylist = stringlist_add(_keylist, key->subkeys->fpr);
   497  
   498                             gpg.gpgme_key_unref(key);
   499  
   500                         }
   501                     }
   502                     assert(_keylist);
   503                     if (_keylist == NULL) {
   504                         free_stringlist(recipient_keylist);
   505                         if (*keylist)
   506                             free_stringlist(*keylist);
   507                         *keylist = NULL;
   508                         result = PEP_OUT_OF_MEMORY;
   509                     }
   510                 } /* Ok, so now we have any recipients it was encrypted for
   511                      in recipient_keylist */
   512             
   513                    
   514                 gpgme_verify_result_t gpgme_verify_result;
   515                 char *_buffer = NULL;
   516                 size_t reading;
   517                 size_t length = gpg.gpgme_data_seek(plain, 0, SEEK_END);
   518                 gpgme_signature_t gpgme_signature;
   519 
   520                 assert(length != -1);
   521                 gpg.gpgme_data_seek(plain, 0, SEEK_SET);
   522 
   523                 // TODO: make things less memory consuming
   524                 // the following algorithm allocates memory for the complete
   525                 // text
   526 
   527                 _buffer = malloc(length + 1);
   528                 assert(_buffer);
   529                 if (_buffer == NULL) {
   530                     gpg.gpgme_data_release(plain);
   531                     gpg.gpgme_data_release(cipher);
   532                     if (recipient_keylist)
   533                         free_stringlist(recipient_keylist);
   534                     return PEP_OUT_OF_MEMORY;
   535                 }
   536 
   537                 reading = gpg.gpgme_data_read(plain, _buffer, length);
   538                 assert(length == reading);
   539 
   540                 if (dsigtext) {  // Is this safe to do?
   541                     gpgme_data_t sigdata;
   542                     gpg.gpgme_data_new_from_mem(&sigdata, dsigtext,
   543                                                 dsigsize, 0);
   544                     gpg.gpgme_op_verify(session->ctx, sigdata, plain, NULL);
   545                     gpg.gpgme_data_release(sigdata);
   546                 }
   547 
   548                 gpgme_verify_result =
   549                     gpg.gpgme_op_verify_result(session->ctx);
   550                 assert(gpgme_verify_result);
   551                 gpgme_signature = gpgme_verify_result->signatures;
   552 
   553                 if (!gpgme_signature) {
   554                     // try cleartext sig verification
   555                     gpg.gpgme_op_verify(session->ctx, plain, NULL, plain);
   556                     gpgme_verify_result =
   557                         gpg.gpgme_op_verify_result(session->ctx);
   558                             assert(gpgme_verify_result);
   559                     gpgme_signature = gpgme_verify_result->signatures;                    
   560                 }
   561 
   562                 if (gpgme_signature) {
   563                     stringlist_t *k;
   564                     _keylist = new_stringlist(NULL);
   565                     assert(_keylist);
   566                     if (_keylist == NULL) {
   567                         gpg.gpgme_data_release(plain);
   568                         gpg.gpgme_data_release(cipher);
   569                         free(_buffer);
   570                         return PEP_OUT_OF_MEMORY;
   571                     }
   572                     k = _keylist;
   573 
   574                     result = PEP_DECRYPTED_AND_VERIFIED;
   575                     gpg.gpgme_check(NULL);
   576                     do { /* get all signers and put them at the front off
   577                             the keylist (likely only one) */
   578                         switch (_GPGERR(gpgme_signature->status)) {
   579                         case GPG_ERR_NO_ERROR:
   580                         {
   581                             // Some versions of gpg returns signer's
   582                             // signing subkey fingerprint instead of
   583                             // signer's primary key fingerprint.
   584                             // This is meant to get signer's primary
   585                             // key fingerprint, using subkey's.
   586 
   587                             gpgme_key_t key = NULL;
   588 
   589                             gpgme_error = gpg.gpgme_get_key(session->ctx,
   590                                 gpgme_signature->fpr, &key, 0);
   591                             gpgme_error = _GPGERR(gpgme_error);
   592                             assert(gpgme_error != GPG_ERR_ENOMEM);
   593                             if (gpgme_error == GPG_ERR_ENOMEM) {
   594                                 free_stringlist(_keylist);
   595                                 gpg.gpgme_data_release(plain);
   596                                 gpg.gpgme_data_release(cipher);
   597                                 free(_buffer);
   598                                 return PEP_OUT_OF_MEMORY;
   599                             }
   600                             // Primary key is given as the first subkey
   601                             if (gpgme_error == GPG_ERR_NO_ERROR &&
   602                                 key && key->subkeys && key->subkeys->fpr
   603                                 && key->subkeys->fpr[0])
   604                             {
   605                                 k = stringlist_add(k, key->subkeys->fpr);
   606 
   607                                 gpg.gpgme_key_unref(key);
   608 
   609                                 if (k == NULL) {
   610                                     free_stringlist(_keylist);
   611                                     if (recipient_keylist)
   612                                         free (recipient_keylist);
   613                                     gpg.gpgme_data_release(plain);
   614                                     gpg.gpgme_data_release(cipher);
   615                                     free(_buffer);
   616                                     return PEP_OUT_OF_MEMORY;
   617                                 }
   618                             }
   619                             else
   620                             {
   621                                 result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
   622                                 break;
   623                             }
   624                             break;
   625                         }
   626                         case GPG_ERR_CERT_REVOKED:
   627                         case GPG_ERR_BAD_SIGNATURE:
   628                             result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
   629                             break;
   630                         case GPG_ERR_SIG_EXPIRED:
   631                         case GPG_ERR_KEY_EXPIRED:
   632                         case GPG_ERR_NO_PUBKEY:
   633                             k = stringlist_add(k, gpgme_signature->fpr);
   634                             if (k == NULL) {
   635                                 free_stringlist(_keylist);
   636                                 if (recipient_keylist)
   637                                     free_stringlist(recipient_keylist);
   638                                 gpg.gpgme_data_release(plain);
   639                                 gpg.gpgme_data_release(cipher);
   640                                 free(_buffer);
   641                                 return PEP_OUT_OF_MEMORY;
   642                             }
   643                             if (result == PEP_DECRYPTED_AND_VERIFIED)
   644                                 result = PEP_DECRYPTED;
   645                             break;
   646                         case GPG_ERR_GENERAL:
   647                             break;
   648                         default:
   649                             if (result == PEP_DECRYPTED_AND_VERIFIED)
   650                                 result = PEP_DECRYPTED;
   651                             break;
   652                         }
   653                     } while ((gpgme_signature = gpgme_signature->next));
   654                 }
   655                 else {
   656                     result = PEP_DECRYPTED;
   657                 }
   658 
   659                 if (result == PEP_DECRYPTED_AND_VERIFIED
   660                     || result == PEP_DECRYPTED) {
   661                     *ptext = _buffer;
   662                     *psize = reading;
   663                     (*ptext)[*psize] = 0; // safeguard for naive users
   664                     *keylist = _keylist;
   665                     if (recipient_keylist)
   666                         if (!_keylist)
   667                             *keylist = new_stringlist(""); // no sig 
   668                         if (!(*keylist)) {
   669                             free_stringlist(_keylist);
   670                             if (recipient_keylist)
   671                                 free_stringlist(recipient_keylist);
   672                             gpg.gpgme_data_release(plain);
   673                             gpg.gpgme_data_release(cipher);
   674                             free(_buffer);
   675                             return PEP_OUT_OF_MEMORY;
   676                         }    
   677                         stringlist_append(*keylist, recipient_keylist);
   678                 }
   679                 else {
   680                     free_stringlist(_keylist);
   681                     if (recipient_keylist)
   682                         free_stringlist(recipient_keylist);
   683                     free(_buffer);
   684                 }
   685                 break;
   686             }
   687             case GPG_ERR_BAD_PASSPHRASE:
   688                 result = PEP_DECRYPT_NO_KEY;
   689                 break;
   690             case GPG_ERR_DECRYPT_FAILED:
   691             default:
   692             {
   693                 gpgme_decrypt_result = gpg.gpgme_op_decrypt_result(session->ctx);
   694                 result = PEP_DECRYPT_NO_KEY;
   695 
   696                 if (gpgme_decrypt_result != NULL) {
   697                     if (gpgme_decrypt_result->unsupported_algorithm)
   698                         *keylist = new_stringlist(gpgme_decrypt_result->unsupported_algorithm);
   699                     else
   700                         *keylist = new_stringlist("");
   701                     assert(*keylist);
   702                     if (*keylist == NULL) {
   703                         result = PEP_OUT_OF_MEMORY;
   704                         break;
   705                     }
   706                 }
   707             }
   708         }
   709         break;
   710 
   711     default:
   712         result = PEP_DECRYPT_WRONG_FORMAT;
   713     }
   714 
   715     gpg.gpgme_data_release(plain);
   716     gpg.gpgme_data_release(cipher);
   717     return result;
   718 }
   719 
   720 PEP_STATUS pgp_verify_text(
   721     PEP_SESSION session, const char *text, size_t size,
   722     const char *signature, size_t sig_size, stringlist_t **keylist
   723     )
   724 {
   725     PEP_STATUS result;
   726     gpgme_error_t gpgme_error;
   727     gpgme_data_t d_text, d_sig;
   728     stringlist_t *_keylist;
   729 
   730     assert(session);
   731     assert(text);
   732     assert(size);
   733     assert(signature);
   734     assert(sig_size);
   735     assert(keylist);
   736 
   737     *keylist = NULL;
   738 
   739     gpgme_error = gpg.gpgme_data_new_from_mem(&d_text, text, size, 0);
   740     gpgme_error = _GPGERR(gpgme_error);
   741     assert(gpgme_error == GPG_ERR_NO_ERROR);
   742     if (gpgme_error != GPG_ERR_NO_ERROR) {
   743         if (gpgme_error == GPG_ERR_ENOMEM)
   744             return PEP_OUT_OF_MEMORY;
   745         else
   746             return PEP_UNKNOWN_ERROR;
   747     }
   748 
   749     gpgme_error = gpg.gpgme_data_new_from_mem(&d_sig, signature, sig_size, 0);
   750     gpgme_error = _GPGERR(gpgme_error);
   751     assert(gpgme_error == GPG_ERR_NO_ERROR);
   752     if (gpgme_error != GPG_ERR_NO_ERROR) {
   753         gpg.gpgme_data_release(d_text);
   754         if (gpgme_error == GPG_ERR_ENOMEM)
   755             return PEP_OUT_OF_MEMORY;
   756         else
   757             return PEP_UNKNOWN_ERROR;
   758     }
   759 
   760     gpgme_error = gpg.gpgme_op_verify(session->ctx, d_sig, d_text, NULL);
   761     gpgme_error = _GPGERR(gpgme_error);
   762     assert(gpgme_error != GPG_ERR_INV_VALUE);
   763 
   764     switch (gpgme_error) {
   765     case GPG_ERR_NO_ERROR:
   766     {
   767         gpgme_verify_result_t gpgme_verify_result;
   768         gpgme_signature_t gpgme_signature;
   769 
   770         gpgme_verify_result =
   771             gpg.gpgme_op_verify_result(session->ctx);
   772         assert(gpgme_verify_result);
   773         gpgme_signature = gpgme_verify_result->signatures;
   774 
   775         if (gpgme_signature) {
   776             stringlist_t *k;
   777             _keylist = new_stringlist(NULL);
   778             assert(_keylist);
   779             if (_keylist == NULL) {
   780                 gpg.gpgme_data_release(d_text);
   781                 gpg.gpgme_data_release(d_sig);
   782                 return PEP_OUT_OF_MEMORY;
   783             }
   784             k = _keylist;
   785 
   786             result = PEP_VERIFIED;
   787             do {
   788                 gpgme_key_t key;
   789                 memset(&key,0,sizeof(key));
   790 
   791                 // GPGME may give subkey's fpr instead of primary key's fpr.
   792                 // Therefore we ask for the primary fingerprint instead
   793                 // we assume that gpgme_get_key can find key by subkey's fpr
   794                 gpgme_error = gpg.gpgme_get_key(session->ctx,
   795                     gpgme_signature->fpr, &key, 0);
   796                 gpgme_error = _GPGERR(gpgme_error);
   797                 assert(gpgme_error != GPG_ERR_ENOMEM);
   798                 if (gpgme_error == GPG_ERR_ENOMEM) {
   799                     free_stringlist(_keylist);
   800                     gpg.gpgme_data_release(d_text);
   801                     gpg.gpgme_data_release(d_sig);
   802                     return PEP_OUT_OF_MEMORY;
   803                 }
   804                 // Primary key is given as the first subkey
   805                 if (gpgme_error == GPG_ERR_NO_ERROR &&
   806                     key && key->subkeys && key->subkeys->fpr
   807                     && key->subkeys->fpr[0])
   808                 {
   809                     k = stringlist_add(k, key->subkeys->fpr);
   810 
   811                     gpg.gpgme_key_unref(key);
   812 
   813                     if (k == NULL) {
   814                         free_stringlist(_keylist);
   815                         gpg.gpgme_data_release(d_text);
   816                         gpg.gpgme_data_release(d_sig);
   817                         return PEP_OUT_OF_MEMORY;
   818                     }
   819                 }
   820                 else {
   821                     result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
   822                     break;
   823                 }
   824 
   825                 if (gpgme_signature->summary & GPGME_SIGSUM_RED) {
   826                     if (gpgme_signature->summary & GPGME_SIGSUM_KEY_EXPIRED
   827                         || gpgme_signature->summary & GPGME_SIGSUM_SIG_EXPIRED) {
   828                         if (result == PEP_VERIFIED
   829                             || result == PEP_VERIFIED_AND_TRUSTED)
   830                             result = PEP_UNENCRYPTED;
   831                     }
   832                     else {
   833                         result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
   834                         break;
   835                     }
   836                 }
   837                 else {
   838                     if (gpgme_signature->summary & GPGME_SIGSUM_VALID) {
   839                         if (result == PEP_VERIFIED)
   840                             result = PEP_VERIFIED_AND_TRUSTED;
   841                     }
   842                     if (gpgme_signature->summary & GPGME_SIGSUM_GREEN) {
   843                         // good
   844                     }
   845                     else if (gpgme_signature->summary & GPGME_SIGSUM_KEY_MISSING) {
   846                         result = PEP_VERIFY_NO_KEY;
   847                     }
   848                     else if (gpgme_signature->summary & GPGME_SIGSUM_SYS_ERROR) {
   849                         if (result == PEP_VERIFIED
   850                             || result == PEP_VERIFIED_AND_TRUSTED)
   851                             result = PEP_UNENCRYPTED;
   852                     }
   853                     else {
   854                         // do nothing
   855                     }
   856                 }
   857             } while ((gpgme_signature = gpgme_signature->next));
   858             *keylist = _keylist;
   859         }
   860         else {
   861             result = PEP_UNENCRYPTED;
   862         }
   863         break;
   864     }
   865         break;
   866     case GPG_ERR_NO_DATA:
   867         result = PEP_DECRYPT_WRONG_FORMAT;
   868         break;
   869     case GPG_ERR_INV_VALUE:
   870     default:
   871         result = PEP_UNKNOWN_ERROR;
   872         break;
   873     }
   874 
   875     gpg.gpgme_data_release(d_text);
   876     gpg.gpgme_data_release(d_sig);
   877 
   878     return result;
   879 }
   880 
   881 
   882 static PEP_STATUS pgp_encrypt_sign_optional(    
   883     PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
   884     size_t psize, char **ctext, size_t *csize, bool sign
   885 )
   886 {    
   887     PEP_STATUS result;
   888     gpgme_error_t gpgme_error;
   889     gpgme_data_t plain, cipher;
   890     gpgme_key_t *rcpt;
   891     gpgme_encrypt_flags_t flags;
   892     const stringlist_t *_keylist;
   893     int i, j;
   894 
   895     assert(session);
   896     assert(keylist);
   897     assert(ptext);
   898     assert(psize);
   899     assert(ctext);
   900     assert(csize);
   901 
   902     *ctext = NULL;
   903     *csize = 0;
   904 
   905     gpgme_error = gpg.gpgme_data_new_from_mem(&plain, ptext, psize, 0);
   906     gpgme_error = _GPGERR(gpgme_error);
   907     assert(gpgme_error == GPG_ERR_NO_ERROR);
   908     if (gpgme_error != GPG_ERR_NO_ERROR) {
   909         if (gpgme_error == GPG_ERR_ENOMEM)
   910             return PEP_OUT_OF_MEMORY;
   911         else
   912             return PEP_UNKNOWN_ERROR;
   913     }
   914 
   915     gpgme_error = gpg.gpgme_data_new(&cipher);
   916     gpgme_error = _GPGERR(gpgme_error);
   917     assert(gpgme_error == GPG_ERR_NO_ERROR);
   918     if (gpgme_error != GPG_ERR_NO_ERROR) {
   919         gpg.gpgme_data_release(plain);
   920         if (gpgme_error == GPG_ERR_ENOMEM)
   921             return PEP_OUT_OF_MEMORY;
   922         else
   923             return PEP_UNKNOWN_ERROR;
   924     }
   925 
   926     rcpt = calloc(stringlist_length(keylist) + 1, sizeof(gpgme_key_t));
   927     assert(rcpt);
   928     if (rcpt == NULL) {
   929         gpg.gpgme_data_release(plain);
   930         gpg.gpgme_data_release(cipher);
   931         return PEP_OUT_OF_MEMORY;
   932     }
   933 
   934     gpg.gpgme_signers_clear(session->ctx);
   935 
   936     for (_keylist = keylist, i = 0; _keylist != NULL; _keylist = _keylist->next, i++) {
   937         assert(_keylist->value);
   938         gpgme_error = gpg.gpgme_get_key(session->ctx, _keylist->value,
   939             &rcpt[i], 0);
   940         gpgme_error = _GPGERR(gpgme_error);
   941         assert(gpgme_error != GPG_ERR_ENOMEM);
   942 
   943         switch (gpgme_error) {
   944         case GPG_ERR_ENOMEM:
   945             for (j = 0; j<i; j++)
   946                 gpg.gpgme_key_unref(rcpt[j]);
   947             free(rcpt);
   948             gpg.gpgme_data_release(plain);
   949             gpg.gpgme_data_release(cipher);
   950             return PEP_OUT_OF_MEMORY;
   951         case GPG_ERR_NO_ERROR:
   952             if (i == 0 && sign) {
   953                 gpgme_error_t _gpgme_error = gpg.gpgme_signers_add(session->ctx, rcpt[0]);
   954                 _gpgme_error = _GPGERR(_gpgme_error);
   955                 assert(_gpgme_error == GPG_ERR_NO_ERROR);
   956             }
   957             break;
   958         case GPG_ERR_EOF:
   959             for (j = 0; j<i; j++)
   960                 gpg.gpgme_key_unref(rcpt[j]);
   961             free(rcpt);
   962             gpg.gpgme_data_release(plain);
   963             gpg.gpgme_data_release(cipher);
   964             return PEP_KEY_NOT_FOUND;
   965         case GPG_ERR_AMBIGUOUS_NAME:
   966             for (j = 0; j<i; j++)
   967                 gpg.gpgme_key_unref(rcpt[j]);
   968             free(rcpt);
   969             gpg.gpgme_data_release(plain);
   970             gpg.gpgme_data_release(cipher);
   971             return PEP_KEY_HAS_AMBIG_NAME;
   972         default: // GPG_ERR_INV_VALUE if CTX or R_KEY is not a valid pointer or
   973             // FPR is not a fingerprint or key ID
   974             for (j = 0; j<i; j++)
   975                 gpg.gpgme_key_unref(rcpt[j]);
   976             free(rcpt);
   977             gpg.gpgme_data_release(plain);
   978             gpg.gpgme_data_release(cipher);
   979             return PEP_GET_KEY_FAILED;
   980         }
   981     }
   982 
   983     // TODO: remove that and replace with proper key management
   984     flags = GPGME_ENCRYPT_ALWAYS_TRUST;
   985     
   986     if (sign) {
   987         gpgme_error = gpg.gpgme_op_encrypt_sign(session->ctx, rcpt, flags,
   988             plain, cipher);
   989     }
   990     else {
   991         gpgme_error = gpg.gpgme_op_encrypt(session->ctx, rcpt, flags,
   992             plain, cipher);
   993     }
   994     
   995     gpgme_error = _GPGERR(gpgme_error);
   996     switch (gpgme_error) {
   997     case GPG_ERR_NO_ERROR:
   998     {
   999         char *_buffer = NULL;
  1000         size_t reading;
  1001         size_t length = gpg.gpgme_data_seek(cipher, 0, SEEK_END);
  1002         assert(length != -1);
  1003         gpg.gpgme_data_seek(cipher, 0, SEEK_SET);
  1004 
  1005         // TODO: make things less memory consuming
  1006         // the following algorithm allocates a buffer for the complete text
  1007 
  1008         _buffer = malloc(length + 1);
  1009         assert(_buffer);
  1010         if (_buffer == NULL) {
  1011             for (j = 0; j<stringlist_length(keylist); j++)
  1012                 gpg.gpgme_key_unref(rcpt[j]);
  1013             free(rcpt);
  1014             gpg.gpgme_data_release(plain);
  1015             gpg.gpgme_data_release(cipher);
  1016             return PEP_OUT_OF_MEMORY;
  1017         }
  1018 
  1019         reading = gpg.gpgme_data_read(cipher, _buffer, length);
  1020         assert(length == reading);
  1021 
  1022         *ctext = _buffer;
  1023         *csize = reading;
  1024         (*ctext)[*csize] = 0; // safeguard for naive users
  1025         result = PEP_STATUS_OK;
  1026         break;
  1027     }
  1028     default:
  1029         result = PEP_UNKNOWN_ERROR;
  1030     }
  1031 
  1032     for (j = 0; j<stringlist_length(keylist); j++)
  1033         gpg.gpgme_key_unref(rcpt[j]);
  1034     free(rcpt);
  1035     gpg.gpgme_data_release(plain);
  1036     gpg.gpgme_data_release(cipher);
  1037     return result;
  1038 }
  1039 
  1040 PEP_STATUS pgp_encrypt_only(
  1041     PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  1042     size_t psize, char **ctext, size_t *csize
  1043     )
  1044 {
  1045     return pgp_encrypt_sign_optional(session, keylist, ptext,
  1046         psize, ctext, csize, false);
  1047 }
  1048 
  1049 PEP_STATUS pgp_encrypt_and_sign(
  1050     PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  1051     size_t psize, char **ctext, size_t *csize
  1052     )
  1053 {
  1054     return pgp_encrypt_sign_optional(session, keylist, ptext,
  1055         psize, ctext, csize, true);
  1056 }
  1057 
  1058 PEP_STATUS pgp_sign_text(
  1059     PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  1060     size_t psize, char **stext, size_t *ssize
  1061     )
  1062 {
  1063     PEP_STATUS result;
  1064     gpgme_error_t gpgme_error;
  1065     gpgme_data_t plain, signeddata;
  1066     gpgme_key_t *rcpt;
  1067     gpgme_sig_mode_t sig_mode;
  1068     const stringlist_t *_keylist;
  1069     int i, j;
  1070 
  1071     assert(session);
  1072     assert(keylist);
  1073     assert(ptext);
  1074     assert(psize);
  1075     assert(stext);
  1076     assert(ssize);
  1077 
  1078     *stext = NULL;
  1079     *ssize = 0;
  1080 
  1081     gpgme_error = gpg.gpgme_data_new_from_mem(&plain, ptext, psize, 0);
  1082     gpgme_error = _GPGERR(gpgme_error);
  1083     assert(gpgme_error == GPG_ERR_NO_ERROR);
  1084     if (gpgme_error != GPG_ERR_NO_ERROR) {
  1085         if (gpgme_error == GPG_ERR_ENOMEM)
  1086             return PEP_OUT_OF_MEMORY;
  1087         else
  1088             return PEP_UNKNOWN_ERROR;
  1089     }
  1090 
  1091     gpgme_error = gpg.gpgme_data_new(&signeddata);
  1092     gpgme_error = _GPGERR(gpgme_error);
  1093     assert(gpgme_error == GPG_ERR_NO_ERROR);
  1094     if (gpgme_error != GPG_ERR_NO_ERROR) {
  1095         gpg.gpgme_data_release(plain);
  1096         if (gpgme_error == GPG_ERR_ENOMEM)
  1097             return PEP_OUT_OF_MEMORY;
  1098         else
  1099             return PEP_UNKNOWN_ERROR;
  1100     }
  1101 
  1102     /* FIXME: Why +1? */
  1103     rcpt = calloc(2, sizeof(gpgme_key_t));
  1104     assert(rcpt);
  1105     if (rcpt == NULL) {
  1106         gpg.gpgme_data_release(plain);
  1107         gpg.gpgme_data_release(signeddata);
  1108         return PEP_OUT_OF_MEMORY;
  1109     }
  1110 
  1111     gpg.gpgme_signers_clear(session->ctx);
  1112 
  1113     assert(keylist->value);
  1114     if (!keylist->value)
  1115         return PEP_KEY_NOT_FOUND;
  1116         
  1117     gpgme_error = gpg.gpgme_get_key(session->ctx, _keylist->value,
  1118         &rcpt[0], 0);
  1119         
  1120     gpgme_error = _GPGERR(gpgme_error);
  1121     assert(gpgme_error != GPG_ERR_ENOMEM);
  1122 
  1123     switch (gpgme_error) {
  1124         case GPG_ERR_ENOMEM:
  1125             gpg.gpgme_key_unref(rcpt[0]);
  1126             free(rcpt);
  1127             gpg.gpgme_data_release(plain);
  1128             gpg.gpgme_data_release(signeddata);
  1129             return PEP_OUT_OF_MEMORY;
  1130         case GPG_ERR_NO_ERROR:
  1131             gpgme_error_t _gpgme_error = gpg.gpgme_signers_add(session->ctx, rcpt[0]);
  1132             _gpgme_error = _GPGERR(_gpgme_error);
  1133             assert(_gpgme_error == GPG_ERR_NO_ERROR);
  1134             break;
  1135         case GPG_ERR_EOF:
  1136             gpg.gpgme_key_unref(rcpt[0]);
  1137             free(rcpt);
  1138             gpg.gpgme_data_release(plain);
  1139             gpg.gpgme_data_release(signeddata);
  1140             return PEP_KEY_NOT_FOUND;
  1141         case GPG_ERR_AMBIGUOUS_NAME:
  1142             gpg.gpgme_key_unref(rcpt[0]);
  1143             free(rcpt);
  1144             gpg.gpgme_data_release(plain);
  1145             gpg.gpgme_data_release(signeddata);
  1146             return PEP_KEY_HAS_AMBIG_NAME;
  1147         default: // GPG_ERR_INV_VALUE if CTX or R_KEY is not a valid pointer or
  1148             // FPR is not a fingerprint or key ID
  1149             gpg.gpgme_key_unref(rcpt[0]);
  1150             free(rcpt);
  1151             gpg.gpgme_data_release(plain);
  1152             gpg.gpgme_data_release(signeddata);
  1153             return PEP_GET_KEY_FAILED;
  1154         }
  1155     }
  1156 
  1157     sig_mode = GPGME_SIG_MODE_NORMAL;
  1158     
  1159     gpgme_error = gpg.gpgme_op_sign(session->ctx, plain, signeddata, sig_mode);
  1160     
  1161     gpgme_error = _GPGERR(gpgme_error);
  1162     switch (gpgme_error) {
  1163     case GPG_ERR_NO_ERROR:
  1164     {
  1165         char *_buffer = NULL;
  1166         size_t reading;
  1167         size_t length = gpg.gpgme_data_seek(signeddata, 0, SEEK_END);
  1168         assert(length != -1);
  1169         gpg.gpgme_data_seek(signeddata, 0, SEEK_SET);
  1170 
  1171         // TODO: make things less memory consuming
  1172         // the following algorithm allocates a buffer for the complete text
  1173 
  1174         _buffer = malloc(length + 1);
  1175         assert(_buffer);
  1176         if (_buffer == NULL) {
  1177             for (j = 0; j<stringlist_length(keylist); j++)
  1178                 gpg.gpgme_key_unref(rcpt[j]);
  1179             free(rcpt);
  1180             gpg.gpgme_data_release(plain);
  1181             gpg.gpgme_data_release(signeddata);
  1182             return PEP_OUT_OF_MEMORY;
  1183         }
  1184 
  1185         reading = gpg.gpgme_data_read(signeddata, _buffer, length);
  1186         assert(length == reading);
  1187 
  1188         *stext = _buffer;
  1189         *ssize = reading;
  1190         (*stext)[*ssize] = 0; // safeguard for naive users
  1191         result = PEP_STATUS_OK;
  1192         break;
  1193     }
  1194     default:
  1195         result = PEP_UNKNOWN_ERROR;
  1196     }
  1197 
  1198     gpg.gpgme_key_unref(rcpt[0]);
  1199     free(rcpt);
  1200     gpg.gpgme_data_release(plain);
  1201     gpg.gpgme_data_release(signeddata);
  1202     return result;
  1203 }
  1204 
  1205 PEP_STATUS pgp_generate_keypair(
  1206     PEP_SESSION session, pEp_identity *identity
  1207     )
  1208 {
  1209     gpgme_error_t gpgme_error;
  1210     char *parms;
  1211     const char *template =
  1212         "<GnupgKeyParms format=\"internal\">\n"
  1213         "Key-Type: RSA\n"
  1214         "Key-Length: 4096\n"
  1215         "Subkey-Type: RSA\n"
  1216         "Subkey-Length: 4096\n"
  1217         "Name-Real: %s\n"
  1218         "Name-Email: %s\n"
  1219         /* "Passphrase: %s\n" */
  1220         "Expire-Date: 1y\n"
  1221         "</GnupgKeyParms>\n";
  1222     int result;
  1223     gpgme_genkey_result_t gpgme_genkey_result;
  1224 
  1225     assert(session);
  1226     assert(identity);
  1227     assert(identity->address);
  1228     assert(identity->fpr == NULL || identity->fpr[0] == 0);
  1229     assert(identity->username);
  1230 
  1231     parms = calloc(1, PARMS_MAX);
  1232     assert(parms);
  1233     if (parms == NULL)
  1234         return PEP_OUT_OF_MEMORY;
  1235 
  1236     result = snprintf(parms, PARMS_MAX, template, identity->username,
  1237         identity->address); // , session->passphrase);
  1238     assert(result < PARMS_MAX);
  1239     if (result >= PARMS_MAX) {
  1240         free(parms);
  1241         return PEP_BUFFER_TOO_SMALL;
  1242     }
  1243 
  1244     gpgme_error = gpg.gpgme_op_genkey(session->ctx, parms, NULL, NULL);
  1245     gpgme_error = _GPGERR(gpgme_error);
  1246     free(parms);
  1247 
  1248     switch (gpgme_error) {
  1249     case GPG_ERR_NO_ERROR:
  1250         break;
  1251     case GPG_ERR_INV_VALUE:
  1252         return PEP_ILLEGAL_VALUE;
  1253     case GPG_ERR_GENERAL:
  1254         return PEP_CANNOT_CREATE_KEY;
  1255     default:
  1256         assert(0);
  1257         return PEP_UNKNOWN_ERROR;
  1258     }
  1259 
  1260     gpgme_genkey_result = gpg.gpgme_op_genkey_result(session->ctx);
  1261     assert(gpgme_genkey_result);
  1262     assert(gpgme_genkey_result->fpr);
  1263 
  1264     free(identity->fpr);
  1265     identity->fpr = strdup(gpgme_genkey_result->fpr);
  1266     if (identity->fpr == NULL)
  1267         return PEP_OUT_OF_MEMORY;
  1268 
  1269     return PEP_STATUS_OK;
  1270 }
  1271 
  1272 PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr)
  1273 {
  1274     gpgme_error_t gpgme_error;
  1275     gpgme_key_t key;
  1276 
  1277     assert(session);
  1278     assert(fpr);
  1279 
  1280     gpgme_error = gpg.gpgme_get_key(session->ctx, fpr, &key, 0);
  1281     gpgme_error = _GPGERR(gpgme_error);
  1282     assert(gpgme_error != GPG_ERR_ENOMEM);
  1283     switch (gpgme_error) {
  1284     case GPG_ERR_NO_ERROR:
  1285         break;
  1286     case GPG_ERR_EOF:
  1287         return PEP_KEY_NOT_FOUND;
  1288     case GPG_ERR_INV_VALUE:
  1289         return PEP_ILLEGAL_VALUE;
  1290     case GPG_ERR_AMBIGUOUS_NAME:
  1291         return PEP_KEY_HAS_AMBIG_NAME;
  1292     case GPG_ERR_ENOMEM:
  1293         return PEP_OUT_OF_MEMORY;
  1294     default:
  1295         assert(0);
  1296         return PEP_UNKNOWN_ERROR;
  1297     }
  1298 
  1299     gpgme_error = gpg.gpgme_op_delete(session->ctx, key, 1);
  1300     gpgme_error = _GPGERR(gpgme_error);
  1301     gpg.gpgme_key_unref(key);
  1302     switch (gpgme_error) {
  1303     case GPG_ERR_NO_ERROR:
  1304         break;
  1305     case GPG_ERR_INV_VALUE:
  1306         assert(0);
  1307         return PEP_UNKNOWN_ERROR;
  1308     case GPG_ERR_NO_PUBKEY:
  1309         assert(0);
  1310         return PEP_KEY_NOT_FOUND;
  1311     case GPG_ERR_AMBIGUOUS_NAME:
  1312         assert(0);
  1313         return PEP_KEY_HAS_AMBIG_NAME;
  1314     default:
  1315         assert(0);
  1316         return PEP_UNKNOWN_ERROR;
  1317     }
  1318 
  1319     return PEP_STATUS_OK;
  1320 }
  1321 
  1322 PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data,
  1323                               size_t size, identity_list **private_idents)
  1324 {
  1325     gpgme_error_t gpgme_error;
  1326     gpgme_data_t dh;
  1327 
  1328     assert(session);
  1329     assert(key_data);
  1330 
  1331     if(private_idents)
  1332         *private_idents = NULL;
  1333 
  1334     gpgme_error = gpg.gpgme_data_new_from_mem(&dh, key_data, size, 0);
  1335     gpgme_error = _GPGERR(gpgme_error);
  1336     assert(gpgme_error != GPG_ERR_ENOMEM);
  1337     switch (gpgme_error) {
  1338     case GPG_ERR_NO_ERROR:
  1339         break;
  1340     case GPG_ERR_ENOMEM:
  1341         return PEP_OUT_OF_MEMORY;
  1342     case GPG_ERR_INV_VALUE:
  1343         assert(0);
  1344         return PEP_UNKNOWN_ERROR;
  1345     default:
  1346         assert(0);
  1347         return PEP_UNKNOWN_ERROR;
  1348     }
  1349 
  1350     gpgme_import_result_t gpgme_import_result;
  1351 
  1352     gpgme_error = gpg.gpgme_op_import(session->ctx, dh);
  1353     gpgme_error = _GPGERR(gpgme_error);
  1354     switch (gpgme_error) {
  1355     case GPG_ERR_NO_ERROR:
  1356         if(private_idents)
  1357         {
  1358             gpgme_import_result =
  1359                 gpg.gpgme_op_import_result(session->ctx);
  1360             assert(gpgme_import_result);
  1361             if (!gpgme_import_result) {
  1362                 gpg.gpgme_data_release(dh);
  1363                 return PEP_UNKNOWN_ERROR;
  1364             }
  1365 
  1366             gpgme_import_status_t import;
  1367             for (import = gpgme_import_result->imports;
  1368                  import;
  1369                  import = import->next)
  1370              {
  1371                 if (import &&
  1372                     import->result == GPG_ERR_NO_ERROR &&
  1373                     import->status & GPGME_IMPORT_SECRET )
  1374                 {
  1375                     gpgme_key_t key = NULL;
  1376 
  1377                     gpgme_error = gpg.gpgme_get_key(session->ctx,
  1378                         import->fpr, &key, 0);
  1379                     gpgme_error = _GPGERR(gpgme_error);
  1380                     assert(gpgme_error != GPG_ERR_ENOMEM);
  1381                     if (gpgme_error == GPG_ERR_ENOMEM) {
  1382                         gpg.gpgme_data_release(dh);
  1383                         return PEP_OUT_OF_MEMORY;
  1384                     }
  1385 
  1386                     if (gpgme_error == GPG_ERR_NO_ERROR &&
  1387                         key && key->uids &&
  1388                         key->uids->email && key->uids->name)
  1389                     {
  1390                         pEp_identity *ident = new_identity(
  1391                              key->uids->email, import->fpr, NULL, key->uids->name);
  1392 
  1393                         gpg.gpgme_key_unref(key);
  1394 
  1395                         if (ident == NULL) {
  1396                             gpg.gpgme_data_release(dh);
  1397                             return PEP_OUT_OF_MEMORY;
  1398                         }
  1399 
  1400                         *private_idents = identity_list_add(*private_idents, ident);
  1401 
  1402                         if (*private_idents == NULL) {
  1403                             gpg.gpgme_data_release(dh);
  1404                             return PEP_OUT_OF_MEMORY;
  1405                         }
  1406                     }
  1407                     else
  1408                     {
  1409                         gpg.gpgme_key_unref(key);
  1410                         gpg.gpgme_data_release(dh);
  1411                         return PEP_UNKNOWN_ERROR;
  1412                     }
  1413                 }
  1414             }
  1415         }
  1416         break;
  1417     case GPG_ERR_INV_VALUE:
  1418         assert(0);
  1419         gpg.gpgme_data_release(dh);
  1420         return PEP_UNKNOWN_ERROR;
  1421     case GPG_ERR_NO_DATA:
  1422         gpg.gpgme_data_release(dh);
  1423         return PEP_ILLEGAL_VALUE;
  1424     default:
  1425         assert(0);
  1426         gpg.gpgme_data_release(dh);
  1427         return PEP_UNKNOWN_ERROR;
  1428     }
  1429 
  1430     gpg.gpgme_data_release(dh);
  1431     return PEP_STATUS_OK;
  1432 }
  1433 
  1434 PEP_STATUS pgp_export_keydata(
  1435         PEP_SESSION session, const char *fpr, char **key_data, size_t *size,
  1436         bool secret
  1437     )
  1438 {
  1439     gpgme_error_t gpgme_error;
  1440     gpgme_data_t dh;
  1441     size_t _size;
  1442     char *buffer;
  1443     int reading;
  1444 
  1445     assert(session);
  1446     assert(fpr);
  1447     assert(key_data);
  1448     assert(size);
  1449 
  1450     gpgme_error = gpg.gpgme_data_new(&dh);
  1451     gpgme_error = _GPGERR(gpgme_error);
  1452     assert(gpgme_error != GPG_ERR_ENOMEM);
  1453     switch (gpgme_error) {
  1454     case GPG_ERR_NO_ERROR:
  1455         break;
  1456     case GPG_ERR_ENOMEM:
  1457         return PEP_OUT_OF_MEMORY;
  1458     case GPG_ERR_INV_VALUE:
  1459         assert(0);
  1460         return PEP_UNKNOWN_ERROR;
  1461     default:
  1462         assert(0);
  1463         return PEP_UNKNOWN_ERROR;
  1464     }
  1465 
  1466     if (secret)
  1467         gpgme_error = gpg.gpgme_op_export(session->ctx, fpr,
  1468             GPGME_EXPORT_MODE_SECRET, dh);
  1469     else
  1470         gpgme_error = gpg.gpgme_op_export(session->ctx, fpr,
  1471             GPGME_EXPORT_MODE_MINIMAL, dh);
  1472     gpgme_error = _GPGERR(gpgme_error);
  1473     switch (gpgme_error) {
  1474     case GPG_ERR_NO_ERROR:
  1475         break;
  1476     case GPG_ERR_EOF:
  1477         gpg.gpgme_data_release(dh);
  1478         return PEP_KEY_NOT_FOUND;
  1479     case GPG_ERR_INV_VALUE:
  1480         assert(0);
  1481         gpg.gpgme_data_release(dh);
  1482         return PEP_UNKNOWN_ERROR;
  1483     default:
  1484         assert(0);
  1485         gpg.gpgme_data_release(dh);
  1486         return PEP_UNKNOWN_ERROR;
  1487     };
  1488 
  1489     _size = gpg.gpgme_data_seek(dh, 0, SEEK_END);
  1490     assert(_size != -1);
  1491     gpg.gpgme_data_seek(dh, 0, SEEK_SET);
  1492 
  1493     buffer = malloc(_size + 1);
  1494     assert(buffer);
  1495     if (buffer == NULL) {
  1496         gpg.gpgme_data_release(dh);
  1497         return PEP_OUT_OF_MEMORY;
  1498     }
  1499 
  1500     reading = gpg.gpgme_data_read(dh, buffer, _size);
  1501     assert(_size == reading);
  1502 
  1503     // safeguard for the naive user
  1504     buffer[_size] = 0;
  1505 
  1506     *key_data = buffer;
  1507     *size = _size;
  1508 
  1509     gpg.gpgme_data_release(dh);
  1510     return PEP_STATUS_OK;
  1511 }
  1512 
  1513 PEP_STATUS pgp_list_keyinfo(PEP_SESSION session, const char* pattern,
  1514                             stringpair_list_t** keyinfo_list)
  1515 {
  1516     gpgme_error_t gpgme_error;
  1517     assert(session);
  1518     assert(keyinfo_list);
  1519 
  1520     if (!session || !keyinfo_list)
  1521         return PEP_ILLEGAL_VALUE;
  1522 
  1523     *keyinfo_list = NULL;
  1524 
  1525     gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
  1526     gpgme_error = _GPGERR(gpgme_error);
  1527 
  1528     switch(gpgme_error) {
  1529         case GPG_ERR_NO_ERROR:
  1530             break;
  1531         case GPG_ERR_INV_VALUE:
  1532             assert(0);
  1533             return PEP_UNKNOWN_ERROR;
  1534         default:
  1535             gpg.gpgme_op_keylist_end(session->ctx);
  1536             return PEP_GET_KEY_FAILED;
  1537     };
  1538 
  1539     gpgme_key_t key;
  1540     stringpair_list_t* _keyinfo_list = new_stringpair_list(NULL);
  1541     stringpair_list_t* list_curr = _keyinfo_list;
  1542     stringpair_t* pair = NULL;
  1543 
  1544     do {
  1545         gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
  1546         gpgme_error = _GPGERR(gpgme_error);
  1547 
  1548         switch(gpgme_error) {
  1549             case GPG_ERR_EOF:
  1550                 break;
  1551             case GPG_ERR_NO_ERROR:
  1552                 assert(key);
  1553                 assert(key->subkeys);
  1554                 if (!key || !key->subkeys)
  1555                     return PEP_GET_KEY_FAILED;
  1556 
  1557                 // first subkey is primary key
  1558                 char* fpr = key->subkeys->fpr;
  1559                 char* uid = key->uids->uid;
  1560 
  1561                 assert(fpr);
  1562                 assert(uid);
  1563                 if (!fpr)
  1564                     return PEP_GET_KEY_FAILED;
  1565 
  1566                 if (key->subkeys->revoked)
  1567                     continue;
  1568 
  1569                 pair = new_stringpair(fpr, uid);
  1570 
  1571                 assert(pair);
  1572 
  1573                 if (pair) {
  1574                     list_curr = stringpair_list_add(list_curr, pair);
  1575                     pair = NULL;
  1576 
  1577                     assert(list_curr);
  1578                     if (list_curr != NULL)
  1579                         break;
  1580                     else
  1581                         free_stringpair(pair);
  1582                 }
  1583                 // else fallthrough (list_curr or pair wasn't allocateable)
  1584             case GPG_ERR_ENOMEM:
  1585                 free_stringpair_list(_keyinfo_list);
  1586                 gpg.gpgme_op_keylist_end(session->ctx);
  1587                 return PEP_OUT_OF_MEMORY;
  1588             default:
  1589                 gpg.gpgme_op_keylist_end(session->ctx);
  1590                 return PEP_UNKNOWN_ERROR;
  1591         }
  1592     } while (gpgme_error != GPG_ERR_EOF);
  1593 
  1594     if (_keyinfo_list->value == NULL) {
  1595         free_stringpair_list(_keyinfo_list);
  1596         _keyinfo_list = NULL;
  1597     }
  1598 
  1599     *keyinfo_list = _keyinfo_list;
  1600 
  1601     return PEP_STATUS_OK;
  1602 }
  1603 
  1604 static void _switch_mode(pEpSession *session, gpgme_keylist_mode_t remove_mode,
  1605     gpgme_keylist_mode_t add_mode)
  1606 {
  1607     gpgme_error_t gpgme_error;
  1608     gpgme_keylist_mode_t mode;
  1609 
  1610     mode = gpg.gpgme_get_keylist_mode(session->ctx);
  1611 
  1612     mode &= ~remove_mode;
  1613     mode |= add_mode;
  1614 
  1615     gpgme_error = gpg.gpgme_set_keylist_mode(session->ctx, mode);
  1616     gpgme_error = _GPGERR(gpgme_error);
  1617     assert(gpgme_error == GPG_ERR_NO_ERROR);
  1618 }
  1619 
  1620 PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern)
  1621 {
  1622     gpgme_error_t gpgme_error;
  1623     gpgme_key_t key;
  1624 
  1625     assert(session);
  1626     assert(pattern);
  1627 
  1628     _switch_mode(session, GPGME_KEYLIST_MODE_LOCAL, GPGME_KEYLIST_MODE_EXTERN);
  1629 
  1630     gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
  1631     gpgme_error = _GPGERR(gpgme_error);
  1632     switch (gpgme_error) {
  1633     case GPG_ERR_NO_ERROR:
  1634         break;
  1635     case GPG_ERR_INV_VALUE:
  1636         assert(0);
  1637         _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
  1638         return PEP_UNKNOWN_ERROR;
  1639     default:
  1640         _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
  1641         return PEP_GET_KEY_FAILED;
  1642     };
  1643 
  1644     gpgme_ctx_t import_ctx;
  1645     gpgme_error = gpg.gpgme_new(&import_ctx);
  1646     assert(gpgme_error == GPG_ERR_NO_ERROR);
  1647 
  1648     do {
  1649         gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
  1650         gpgme_error = _GPGERR(gpgme_error);
  1651         assert(gpgme_error != GPG_ERR_INV_VALUE);
  1652         switch (gpgme_error) {
  1653         case GPG_ERR_EOF:
  1654             break;
  1655         case GPG_ERR_NO_ERROR:
  1656         {
  1657             gpgme_error_t gpgme_error;
  1658             gpgme_key_t keys[2];
  1659 
  1660             keys[0] = key;
  1661             keys[1] = NULL;
  1662 
  1663             gpgme_error = gpg.gpgme_op_import_keys(import_ctx, keys);
  1664             gpgme_error = _GPGERR(gpgme_error);
  1665             gpg.gpgme_key_unref(key);
  1666             assert(gpgme_error != GPG_ERR_INV_VALUE);
  1667             assert(gpgme_error != GPG_ERR_CONFLICT);
  1668         }
  1669             break;
  1670         case GPG_ERR_ENOMEM:
  1671             gpg.gpgme_op_keylist_end(session->ctx);
  1672             gpg.gpgme_release(import_ctx);
  1673             _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
  1674             return PEP_OUT_OF_MEMORY;
  1675         default:
  1676             gpg.gpgme_op_keylist_end(session->ctx);
  1677             gpg.gpgme_release(import_ctx);
  1678             _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
  1679             return PEP_UNKNOWN_ERROR;
  1680         };
  1681     } while (gpgme_error != GPG_ERR_EOF);
  1682 
  1683     gpg.gpgme_op_keylist_end(session->ctx);
  1684     gpg.gpgme_release(import_ctx);
  1685     _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
  1686     return PEP_STATUS_OK;
  1687 }
  1688 
  1689 
  1690 static PEP_STATUS _pgp_search_keys(PEP_SESSION session, const char* pattern,
  1691                             stringlist_t** keylist,
  1692                             int private_only) {
  1693     gpgme_error_t gpgme_error;
  1694     gpgme_key_t key;
  1695 
  1696     assert(session);
  1697     assert(pattern);
  1698     assert(keylist);
  1699 
  1700     *keylist = NULL;
  1701 
  1702     gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, private_only);
  1703     gpgme_error = _GPGERR(gpgme_error);
  1704     switch (gpgme_error) {
  1705         case GPG_ERR_NO_ERROR:
  1706             break;
  1707         case GPG_ERR_INV_VALUE:
  1708             assert(0);
  1709             return PEP_UNKNOWN_ERROR;
  1710         default:
  1711             gpg.gpgme_op_keylist_end(session->ctx);
  1712             return PEP_GET_KEY_FAILED;
  1713     };
  1714 
  1715     stringlist_t *_keylist = new_stringlist(NULL);
  1716     stringlist_t *_k = _keylist;
  1717 
  1718     do {
  1719         gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
  1720         gpgme_error = _GPGERR(gpgme_error);
  1721         assert(gpgme_error != GPG_ERR_INV_VALUE);
  1722         switch (gpgme_error) {
  1723             case GPG_ERR_EOF:
  1724                 break;
  1725             case GPG_ERR_NO_ERROR:
  1726                 assert(key);
  1727                 assert(key->subkeys);
  1728                 if(!key->subkeys)
  1729                     break;
  1730                 assert(key->uids);
  1731                 gpgme_user_id_t kuid = key->uids;
  1732                 // check that at least one uid's email matches pattern exactly
  1733                 while(kuid) {
  1734                     if(kuid->email && strcmp(kuid->email, pattern) == 0){
  1735                         char *fpr = key->subkeys->fpr;
  1736                         assert(fpr);
  1737                         _k = stringlist_add(_k, fpr);
  1738                         assert(_k);
  1739                         if (_k == NULL){
  1740                             free_stringlist(_keylist);
  1741                             gpg.gpgme_op_keylist_end(session->ctx);
  1742                             return PEP_OUT_OF_MEMORY;
  1743                         }
  1744                         break;
  1745                     }
  1746                     kuid = kuid->next;
  1747                 }
  1748                 break;
  1749             case GPG_ERR_ENOMEM:
  1750                 free_stringlist(_keylist);
  1751                 gpg.gpgme_op_keylist_end(session->ctx);
  1752                 return PEP_OUT_OF_MEMORY;
  1753             default:
  1754                 gpg.gpgme_op_keylist_end(session->ctx);
  1755                 return PEP_UNKNOWN_ERROR;
  1756         };
  1757     } while (gpgme_error != GPG_ERR_EOF);
  1758 
  1759     gpg.gpgme_op_keylist_end(session->ctx);
  1760     if (_keylist->value == NULL) {
  1761         free_stringlist(_keylist);
  1762         _keylist = NULL;
  1763     }
  1764     *keylist = _keylist;
  1765     return PEP_STATUS_OK;
  1766 }
  1767 
  1768 PEP_STATUS pgp_find_keys(
  1769     PEP_SESSION session, const char *pattern, stringlist_t **keylist
  1770     )
  1771 {
  1772     return _pgp_search_keys(session, pattern, keylist, 0);
  1773 }
  1774 
  1775 PEP_STATUS pgp_find_private_keys(
  1776     PEP_SESSION session, const char *pattern, stringlist_t **keylist
  1777 )
  1778 {
  1779     return _pgp_search_keys(session, pattern, keylist, 1);
  1780 }
  1781 
  1782 PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)
  1783 {
  1784     gpgme_error_t gpgme_error;
  1785 
  1786     assert(session);
  1787     assert(pattern);
  1788 
  1789     gpgme_error = gpg.gpgme_op_export(session->ctx, pattern,
  1790         GPGME_EXPORT_MODE_EXTERN, NULL);
  1791     gpgme_error = _GPGERR(gpgme_error);
  1792     assert(gpgme_error != GPG_ERR_INV_VALUE);
  1793     if (gpgme_error == GPG_ERR_NO_ERROR)
  1794         return PEP_STATUS_OK;
  1795     else
  1796         return PEP_CANNOT_SEND_KEY;
  1797 }
  1798 
  1799 PEP_STATUS pgp_get_key_rating(
  1800     PEP_SESSION session,
  1801     const char *fpr,
  1802     PEP_comm_type *comm_type
  1803     )
  1804 {
  1805     PEP_STATUS status = PEP_STATUS_OK;
  1806     gpgme_error_t gpgme_error;
  1807     gpgme_key_t key;
  1808 
  1809     assert(session);
  1810     assert(fpr);
  1811     assert(comm_type);
  1812 
  1813     *comm_type = PEP_ct_unknown;
  1814 
  1815     gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
  1816     gpgme_error = _GPGERR(gpgme_error);
  1817     switch (gpgme_error) {
  1818     case GPG_ERR_NO_ERROR:
  1819         break;
  1820     case GPG_ERR_INV_VALUE:
  1821         assert(0);
  1822         return PEP_UNKNOWN_ERROR;
  1823     default:
  1824         return PEP_GET_KEY_FAILED;
  1825     };
  1826 
  1827     gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
  1828     gpgme_error = _GPGERR(gpgme_error);
  1829     assert(gpgme_error != GPG_ERR_INV_VALUE);
  1830 
  1831     if (key == NULL) {
  1832         gpg.gpgme_op_keylist_end(session->ctx);
  1833         return PEP_KEY_NOT_FOUND;
  1834     }
  1835 
  1836     switch (key->protocol) {
  1837     case GPGME_PROTOCOL_OpenPGP:
  1838     case GPGME_PROTOCOL_DEFAULT:
  1839         *comm_type = PEP_ct_OpenPGP_unconfirmed;
  1840         break;
  1841     case GPGME_PROTOCOL_CMS:
  1842         *comm_type = PEP_ct_CMS_unconfirmed;
  1843         break;
  1844     default:
  1845         *comm_type = PEP_ct_unknown;
  1846         gpg.gpgme_op_keylist_end(session->ctx);
  1847         return PEP_STATUS_OK;
  1848     }
  1849 
  1850     switch (gpgme_error) {
  1851     case GPG_ERR_EOF:
  1852         break;
  1853     case GPG_ERR_NO_ERROR:
  1854         assert(key);
  1855         assert(key->subkeys);
  1856         for (gpgme_subkey_t sk = key->subkeys; sk != NULL; sk = sk->next) {
  1857             if (sk->length < 1024)
  1858                 *comm_type = PEP_ct_key_too_short;
  1859             else if (
  1860                 (
  1861                 (sk->pubkey_algo == GPGME_PK_RSA)
  1862                 || (sk->pubkey_algo == GPGME_PK_RSA_E)
  1863                 || (sk->pubkey_algo == GPGME_PK_RSA_S)
  1864                 )
  1865                 && sk->length == 1024
  1866                 )
  1867                 *comm_type = PEP_ct_OpenPGP_weak_unconfirmed;
  1868 
  1869             if (sk->invalid) {
  1870                 *comm_type = PEP_ct_key_b0rken;
  1871                 break;
  1872             }
  1873             if (sk->expired) {
  1874                 *comm_type = PEP_ct_key_expired;
  1875                 break;
  1876             }
  1877             if (sk->revoked) {
  1878                 *comm_type = PEP_ct_key_revoked;
  1879                 break;
  1880             }
  1881         }
  1882         break;
  1883     case GPG_ERR_ENOMEM:
  1884         gpg.gpgme_op_keylist_end(session->ctx);
  1885         *comm_type = PEP_ct_unknown;
  1886         return PEP_OUT_OF_MEMORY;
  1887     default:
  1888         gpg.gpgme_op_keylist_end(session->ctx);
  1889         return PEP_UNKNOWN_ERROR;
  1890     };
  1891 
  1892     gpg.gpgme_op_keylist_end(session->ctx);
  1893 
  1894     return status;
  1895 }
  1896 
  1897 static PEP_STATUS find_single_key(
  1898         PEP_SESSION session,
  1899         const char *fpr,
  1900         gpgme_key_t *key
  1901     )
  1902 {
  1903     gpgme_error_t gpgme_error;
  1904 
  1905     *key = NULL;
  1906 
  1907     gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
  1908     gpgme_error = _GPGERR(gpgme_error);
  1909     switch (gpgme_error) {
  1910     case GPG_ERR_NO_ERROR:
  1911         break;
  1912     case GPG_ERR_INV_VALUE:
  1913         assert(0);
  1914         return PEP_UNKNOWN_ERROR;
  1915     default:
  1916         return PEP_GET_KEY_FAILED;
  1917     };
  1918 
  1919     gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, key);
  1920     gpgme_error = _GPGERR(gpgme_error);
  1921     assert(gpgme_error != GPG_ERR_INV_VALUE);
  1922 
  1923     gpg.gpgme_op_keylist_end(session->ctx);
  1924 
  1925     return PEP_STATUS_OK;
  1926 }
  1927 
  1928 typedef struct _renew_state {
  1929     enum {
  1930         renew_command = 0,
  1931         renew_date,
  1932         renew_secret_key,
  1933         renew_command2,
  1934         renew_date2,
  1935         renew_quit,
  1936         renew_save,
  1937         renew_exit,
  1938         renew_error = -1
  1939     } state;
  1940     const char *date_ref;
  1941 } renew_state;
  1942 
  1943 static gpgme_error_t renew_fsm(
  1944         void *_handle,
  1945         gpgme_status_code_t statuscode,
  1946         const char *args,
  1947         int fd
  1948     )
  1949 {
  1950     renew_state *handle = _handle;
  1951 
  1952     switch (handle->state) {
  1953         case renew_command:
  1954             if (statuscode == GPGME_STATUS_GET_LINE) {
  1955                 assert(strcmp(args, "keyedit.prompt") == 0);
  1956                 if (strcmp(args, "keyedit.prompt")) {
  1957                     handle->state = renew_error;
  1958                     return GPG_ERR_GENERAL;
  1959                 }
  1960                 gpg.gpgme_io_write(fd, "expire\n", 7);
  1961                 handle->state = renew_date;
  1962             }
  1963             break;
  1964 
  1965         case renew_date:
  1966             if (statuscode == GPGME_STATUS_GET_LINE) {
  1967                 assert(strcmp(args, "keygen.valid") == 0);
  1968                 if (strcmp(args, "keygen.valid")) {
  1969                     handle->state = renew_error;
  1970                     return GPG_ERR_GENERAL;
  1971                 }
  1972                 gpg.gpgme_io_write(fd, handle->date_ref, 11);
  1973                 handle->state = renew_secret_key;
  1974             }
  1975             break;
  1976 
  1977         case renew_secret_key:
  1978             if (statuscode == GPGME_STATUS_GET_LINE) {
  1979                 assert(strcmp(args, "keyedit.prompt") == 0);
  1980                 if (strcmp(args, "keyedit.prompt")) {
  1981                     handle->state = renew_error;
  1982                     return GPG_ERR_GENERAL;
  1983                 }
  1984                 gpg.gpgme_io_write(fd, "key 1\n", 6);
  1985                 handle->state = renew_command2;
  1986             }
  1987             break;
  1988 
  1989         case renew_command2:
  1990             if (statuscode == GPGME_STATUS_GET_LINE) {
  1991                 assert(strcmp(args, "keyedit.prompt") == 0);
  1992                 if (strcmp(args, "keyedit.prompt")) {
  1993                     handle->state = renew_error;
  1994                     return GPG_ERR_GENERAL;
  1995                 }
  1996                 gpg.gpgme_io_write(fd, "expire\n", 7);
  1997                 handle->state = renew_date2;
  1998             }
  1999             break;
  2000 
  2001         case renew_date2:
  2002             if (statuscode == GPGME_STATUS_GET_LINE) {
  2003                 assert(strcmp(args, "keygen.valid") == 0);
  2004                 if (strcmp(args, "keygen.valid")) {
  2005                     handle->state = renew_error;
  2006                     return GPG_ERR_GENERAL;
  2007                 }
  2008                 gpg.gpgme_io_write(fd, handle->date_ref, 11);
  2009                 handle->state = renew_quit;
  2010             }
  2011             break;
  2012 
  2013         case renew_quit:
  2014             if (statuscode == GPGME_STATUS_GET_LINE) {
  2015                 assert(strcmp(args, "keyedit.prompt") == 0);
  2016                 if (strcmp(args, "keyedit.prompt")) {
  2017                     handle->state = renew_error;
  2018                     return GPG_ERR_GENERAL;
  2019                 }
  2020                 gpg.gpgme_io_write(fd, "quit\n", 5);
  2021                 handle->state = renew_save;
  2022             }
  2023             break;
  2024 
  2025         case renew_save:
  2026             if (statuscode == GPGME_STATUS_GET_BOOL) {
  2027                 assert(strcmp(args, "keyedit.save.okay") == 0);
  2028                 if (strcmp(args, "keyedit.save.okay")) {
  2029                     handle->state = renew_error;
  2030                     return GPG_ERR_GENERAL;
  2031                 }
  2032                 gpg.gpgme_io_write(fd, "Y\n", 2);
  2033                 handle->state = renew_exit;
  2034             }
  2035             break;
  2036 
  2037         case renew_exit:
  2038             break;
  2039 
  2040         case renew_error:
  2041             return GPG_ERR_GENERAL;
  2042     }
  2043 
  2044     return GPG_ERR_NO_ERROR;
  2045 }
  2046 
  2047 static ssize_t _nullwriter(
  2048         void *_handle,
  2049         const void *buffer,
  2050         size_t size
  2051     )
  2052 {
  2053     return size;
  2054 }
  2055 
  2056 PEP_STATUS pgp_renew_key(
  2057         PEP_SESSION session,
  2058         const char *fpr,
  2059         const timestamp *ts
  2060     )
  2061 {
  2062     PEP_STATUS status = PEP_STATUS_OK;
  2063     gpgme_error_t gpgme_error;
  2064     gpgme_key_t key;
  2065     gpgme_data_t output;
  2066     renew_state handle;
  2067     char date_text[12];
  2068 
  2069     assert(session);
  2070     assert(fpr);
  2071 
  2072     memset(&handle, 0, sizeof(renew_state));
  2073     snprintf(date_text, 12, "%.4d-%.2d-%.2d\n", ts->tm_year + 1900,
  2074             ts->tm_mon + 1, ts->tm_mday);
  2075     handle.date_ref = date_text;
  2076 
  2077     status = find_single_key(session, fpr, &key);
  2078     if (status != PEP_STATUS_OK)
  2079         return status;
  2080 
  2081     struct gpgme_data_cbs data_cbs;
  2082     memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
  2083     data_cbs.write = _nullwriter;
  2084     gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
  2085 
  2086     gpgme_error = gpg.gpgme_op_edit(session->ctx, key, renew_fsm, &handle,
  2087             output);
  2088     assert(gpgme_error == GPG_ERR_NO_ERROR);
  2089 
  2090     gpg.gpgme_data_release(output);
  2091     gpg.gpgme_key_unref(key);
  2092 
  2093     return PEP_STATUS_OK;
  2094 }
  2095 
  2096 typedef struct _revoke_state {
  2097     enum {
  2098         revoke_command = 0,
  2099         revoke_approve,
  2100         revoke_reason_code,
  2101         revoke_reason_text,
  2102         revoke_reason_ok,
  2103         revoke_quit,
  2104         revoke_save,
  2105         revoke_exit,
  2106         revoke_error = -1
  2107     } state;
  2108     const char *reason_ref;
  2109 } revoke_state;
  2110 
  2111 
  2112 /*** unused?
  2113 static bool isemptystring(const char *str)
  2114 {
  2115     if (str == NULL)
  2116         return true;
  2117 
  2118     for (; str; str++) {
  2119         if (*str != ' ' && *str != '\t' && *str != '\n')
  2120             return false;
  2121     }
  2122 
  2123     return true;
  2124 }
  2125 ***/
  2126 
  2127 
  2128 static gpgme_error_t revoke_fsm(
  2129         void *_handle,
  2130         gpgme_status_code_t statuscode,
  2131         const char *args,
  2132         int fd
  2133     )
  2134 {
  2135     revoke_state *handle = _handle;
  2136 
  2137     switch (handle->state) {
  2138         case revoke_command:
  2139             if (statuscode == GPGME_STATUS_GET_LINE) {
  2140                 assert(strcmp(args, "keyedit.prompt") == 0);
  2141                 if (strcmp(args, "keyedit.prompt")) {
  2142                     handle->state = revoke_error;
  2143                     return GPG_ERR_GENERAL;
  2144                 }
  2145                 gpg.gpgme_io_write(fd, "revkey\n", 7);
  2146                 handle->state = revoke_approve;
  2147             }
  2148             break;
  2149 
  2150         case revoke_approve:
  2151             if (statuscode == GPGME_STATUS_GET_BOOL) {
  2152                 assert(strcmp(args, "keyedit.revoke.subkey.okay") == 0);
  2153                 if (strcmp(args, "keyedit.revoke.subkey.okay")) {
  2154                     handle->state = revoke_error;
  2155                     return GPG_ERR_GENERAL;
  2156                 }
  2157                 gpg.gpgme_io_write(fd, "Y\n", 2);
  2158                 handle->state = revoke_reason_code;
  2159             }
  2160             break;
  2161 
  2162         case revoke_reason_code:
  2163             if (statuscode == GPGME_STATUS_GET_LINE) {
  2164                 assert(strcmp(args, "ask_revocation_reason.code") == 0);
  2165                 if (strcmp(args, "ask_revocation_reason.code")) {
  2166                     handle->state = revoke_error;
  2167                     return GPG_ERR_GENERAL;
  2168                 }
  2169                 gpg.gpgme_io_write(fd, "1\n", 2);
  2170                 handle->state = revoke_reason_text;
  2171             }
  2172             break;
  2173 
  2174         case revoke_reason_text:
  2175             if (statuscode == GPGME_STATUS_GET_LINE) {
  2176                 assert(strcmp(args, "ask_revocation_reason.text") == 0);
  2177                 if (strcmp(args, "ask_revocation_reason.text")) {
  2178                     handle->state = revoke_error;
  2179                     return GPG_ERR_GENERAL;
  2180                 }
  2181                 // BUG: issues when reason given
  2182                 // Assertion failed: (gpg->cmd.code), function command_handler,
  2183                 // file engine-gpg.c, line 662.
  2184                 //
  2185                 // if (isemptystring(handle->reason_ref)) {
  2186                     gpg.gpgme_io_write(fd, "\n", 1);
  2187                 // }
  2188                 // else {
  2189                 //     size_t len = strlen(handle->reason_ref);
  2190                 //     gpg.gpgme_io_write(fd, handle->reason_ref, len);
  2191                 //     if (handle->reason_ref[len - 1] == '\n')
  2192                 //         gpg.gpgme_io_write(fd, "\n", 1);
  2193                 //     else
  2194                 //         gpg.gpgme_io_write(fd, "\n\n", 2);
  2195                 // }
  2196                 handle->state = revoke_reason_ok;
  2197             }
  2198             break;
  2199 
  2200         case revoke_reason_ok:
  2201             if (statuscode == GPGME_STATUS_GET_BOOL) {
  2202                 assert(strcmp(args, "ask_revocation_reason.okay") == 0);
  2203                 if (strcmp(args, "ask_revocation_reason.okay")) {
  2204                     handle->state = revoke_error;
  2205                     return GPG_ERR_GENERAL;
  2206                 }
  2207                 gpg.gpgme_io_write(fd, "Y\n", 2);
  2208                 handle->state = revoke_quit;
  2209             }
  2210             break;
  2211 
  2212         case revoke_quit:
  2213             if (statuscode == GPGME_STATUS_GET_LINE) {
  2214                 assert(strcmp(args, "keyedit.prompt") == 0);
  2215                 if (strcmp(args, "keyedit.prompt")) {
  2216                     handle->state = revoke_error;
  2217                     return GPG_ERR_GENERAL;
  2218                 }
  2219                 gpg.gpgme_io_write(fd, "quit\n", 5);
  2220                 handle->state = revoke_save;
  2221             }
  2222             break;
  2223 
  2224         case revoke_save:
  2225             if (statuscode == GPGME_STATUS_GET_BOOL) {
  2226                 assert(strcmp(args, "keyedit.save.okay") == 0);
  2227                 if (strcmp(args, "keyedit.save.okay")) {
  2228                     handle->state = revoke_error;
  2229                     return GPG_ERR_GENERAL;
  2230                 }
  2231                 gpg.gpgme_io_write(fd, "Y\n", 2);
  2232                 handle->state = revoke_exit;
  2233             }
  2234             break;
  2235 
  2236         case revoke_exit:
  2237             break;
  2238 
  2239         case revoke_error:
  2240             return GPG_ERR_GENERAL;
  2241     }
  2242 
  2243     return GPG_ERR_NO_ERROR;
  2244 }
  2245 
  2246 PEP_STATUS pgp_revoke_key(
  2247         PEP_SESSION session,
  2248         const char *fpr,
  2249         const char *reason
  2250     )
  2251 {
  2252     PEP_STATUS status = PEP_STATUS_OK;
  2253     gpgme_error_t gpgme_error;
  2254     gpgme_key_t key;
  2255     gpgme_data_t output;
  2256     revoke_state handle;
  2257 
  2258     assert(session);
  2259     assert(fpr);
  2260 
  2261     memset(&handle, 0, sizeof(revoke_state));
  2262     handle.reason_ref = reason;
  2263 
  2264     status = find_single_key(session, fpr, &key);
  2265     if (status != PEP_STATUS_OK)
  2266         return status;
  2267 
  2268     struct gpgme_data_cbs data_cbs;
  2269     memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
  2270     data_cbs.write = _nullwriter;
  2271     gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
  2272 
  2273     gpgme_error = gpg.gpgme_op_edit(session->ctx, key, revoke_fsm, &handle,
  2274             output);
  2275     assert(gpgme_error == GPG_ERR_NO_ERROR);
  2276 
  2277     gpg.gpgme_data_release(output);
  2278     gpg.gpgme_key_unref(key);
  2279 
  2280     return PEP_STATUS_OK;
  2281 }
  2282 
  2283 PEP_STATUS pgp_key_expired(
  2284         PEP_SESSION session,
  2285         const char *fpr,
  2286         const time_t when,
  2287         bool *expired
  2288     )
  2289 {
  2290     PEP_STATUS status = PEP_STATUS_OK;
  2291     gpgme_key_t key;
  2292 
  2293     assert(session);
  2294     assert(fpr);
  2295     assert(expired);
  2296 
  2297     *expired = false;
  2298 
  2299     status = find_single_key(session, fpr, &key);
  2300     if (status != PEP_STATUS_OK)
  2301         return status;
  2302 
  2303     if ((key && key->expired) ||
  2304         (key && key->subkeys && key->subkeys->expired))
  2305     {
  2306         // Already marked expired
  2307         *expired = 1;
  2308     }
  2309     else if (key)
  2310     {
  2311         // Detect if will be expired
  2312         // i.e. Check that keys capabilities will
  2313         // not be expired at given time.
  2314         gpgme_subkey_t _sk;
  2315         bool crt_available = false;
  2316         bool sgn_available = false;
  2317         bool enc_available = false;
  2318         for (_sk = key->subkeys; _sk; _sk = _sk->next) {
  2319             if (_sk->expires > when || _sk->expires == 0) // not expired at that date ?
  2320                                                           // Also, zero means "does not expire"
  2321             {
  2322                 if (_sk->can_certify) crt_available = true;
  2323                 if (_sk->can_sign) sgn_available = true;
  2324                 if (_sk->can_encrypt) enc_available = true;
  2325                 // Authenticate is not used here.
  2326             }
  2327         }
  2328         if(!(crt_available && sgn_available && enc_available))
  2329         {
  2330             *expired = 1;
  2331         }
  2332     }
  2333     else
  2334     {
  2335         status = PEP_KEY_NOT_FOUND;
  2336     }
  2337 
  2338     gpg.gpgme_key_unref(key);
  2339     return status;
  2340 }
  2341 
  2342 PEP_STATUS pgp_key_revoked(
  2343         PEP_SESSION session,
  2344         const char *fpr,
  2345         bool *revoked
  2346     )
  2347 {
  2348     PEP_STATUS status = PEP_STATUS_OK;
  2349     gpgme_key_t key;
  2350 
  2351     assert(session);
  2352     assert(fpr);
  2353     assert(revoked);
  2354 
  2355     *revoked = false;
  2356 
  2357     status = find_single_key(session, fpr, &key);
  2358     if (status != PEP_STATUS_OK)
  2359         return status;
  2360 
  2361     if (key && key->subkeys)
  2362     {
  2363         *revoked = key->subkeys->revoked;
  2364     }
  2365     else
  2366     {
  2367         status = PEP_KEY_NOT_FOUND;
  2368     }
  2369 
  2370     gpg.gpgme_key_unref(key);
  2371     return status;
  2372 }
  2373 
  2374 PEP_STATUS pgp_key_created(
  2375         PEP_SESSION session,
  2376         const char *fpr,
  2377         time_t *created
  2378     )
  2379 {
  2380     PEP_STATUS status = PEP_STATUS_OK;
  2381     gpgme_key_t key;
  2382 
  2383     assert(session);
  2384     assert(fpr);
  2385     assert(created);
  2386 
  2387     *created = 0;
  2388 
  2389     status = find_single_key(session, fpr, &key);
  2390     if (status != PEP_STATUS_OK)
  2391         return status;
  2392 
  2393     if (key && key->subkeys)
  2394     {
  2395         *created = (time_t) key->subkeys->timestamp;
  2396     }
  2397     else
  2398     {
  2399         status = PEP_KEY_NOT_FOUND;
  2400     }
  2401 
  2402     gpg.gpgme_key_unref(key);
  2403     return status;
  2404 }
  2405 
  2406 PEP_STATUS pgp_binary(const char **path)
  2407 {
  2408     assert(path);
  2409     if (path == NULL)
  2410         return PEP_ILLEGAL_VALUE;
  2411 
  2412     *path = NULL;
  2413 
  2414     gpgme_engine_info_t info;
  2415     int err = gpg.gpgme_get_engine_info(&info);
  2416     assert(err == GPG_ERR_NO_ERROR);
  2417     if (err != GPG_ERR_NO_ERROR)
  2418         return PEP_OUT_OF_MEMORY;
  2419 
  2420     *path = info->file_name;
  2421 
  2422     return PEP_STATUS_OK;
  2423 }
  2424 
  2425 PEP_STATUS pgp_contains_priv_key(PEP_SESSION session, const char *fpr,
  2426         bool *has_private) {
  2427     PEP_STATUS status = PEP_STATUS_OK;
  2428     gpgme_key_t output_key;
  2429     gpgme_error_t gpgerr = gpg.gpgme_get_key(session->ctx, fpr, &output_key, true);
  2430     *has_private = false;
  2431     switch (gpgerr) {
  2432         case GPG_ERR_EOF:
  2433         case GPG_ERR_INV_VALUE:
  2434             status = PEP_KEY_NOT_FOUND;
  2435             break;
  2436         case GPG_ERR_AMBIGUOUS_NAME:
  2437             status = PEP_KEY_HAS_AMBIG_NAME;
  2438             break;
  2439         case GPG_ERR_NO_ERROR:
  2440             *has_private = true;
  2441             gpg.gpgme_key_release(output_key);
  2442             break;
  2443         case GPG_ERR_ENOMEM:
  2444             status = PEP_OUT_OF_MEMORY;
  2445             break;
  2446         default:
  2447             status = PEP_UNKNOWN_ERROR;
  2448             break;
  2449     }
  2450     return status;
  2451 }