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