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