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