src/pgp_gpg.c
author Edouard Tisserant <edouard@pep-project.org>
Tue, 23 May 2017 11:55:30 +0200
changeset 1803 656f58b5b244
parent 1802 88931f5dcf75
child 1914 e40bb1f761fe
permissions -rw-r--r--
Fixed C compiler warning about unused variable when not building in debug, continued.
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
vb@24
  1063
PEP_STATUS pgp_generate_keypair(
vb@24
  1064
    PEP_SESSION session, pEp_identity *identity
vb@24
  1065
    )
vb@24
  1066
{
vb@24
  1067
    gpgme_error_t gpgme_error;
vb@24
  1068
    char *parms;
vb@24
  1069
    const char *template =
vb@24
  1070
        "<GnupgKeyParms format=\"internal\">\n"
vb@24
  1071
        "Key-Type: RSA\n"
vb@24
  1072
        "Key-Length: 4096\n"
vb@429
  1073
        "Subkey-Type: RSA\n"
vb@429
  1074
        "Subkey-Length: 4096\n"
vb@24
  1075
        "Name-Real: %s\n"
vb@24
  1076
        "Name-Email: %s\n"
vb@24
  1077
        /* "Passphrase: %s\n" */
vb@24
  1078
        "Expire-Date: 1y\n"
vb@24
  1079
        "</GnupgKeyParms>\n";
vb@24
  1080
    int result;
vb@24
  1081
    gpgme_genkey_result_t gpgme_genkey_result;
vb@24
  1082
vb@24
  1083
    assert(session);
vb@24
  1084
    assert(identity);
vb@24
  1085
    assert(identity->address);
vb@298
  1086
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@24
  1087
    assert(identity->username);
vb@24
  1088
vb@24
  1089
    parms = calloc(1, PARMS_MAX);
vb@24
  1090
    assert(parms);
vb@24
  1091
    if (parms == NULL)
vb@24
  1092
        return PEP_OUT_OF_MEMORY;
vb@24
  1093
vb@24
  1094
    result = snprintf(parms, PARMS_MAX, template, identity->username,
vb@46
  1095
        identity->address); // , session->passphrase);
vb@24
  1096
    assert(result < PARMS_MAX);
vb@24
  1097
    if (result >= PARMS_MAX) {
vb@24
  1098
        free(parms);
vb@24
  1099
        return PEP_BUFFER_TOO_SMALL;
vb@24
  1100
    }
vb@24
  1101
vb@74
  1102
    gpgme_error = gpg.gpgme_op_genkey(session->ctx, parms, NULL, NULL);
vb@45
  1103
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1104
    free(parms);
vb@24
  1105
vb@24
  1106
    switch (gpgme_error) {
vb@24
  1107
    case GPG_ERR_NO_ERROR:
vb@24
  1108
        break;
vb@24
  1109
    case GPG_ERR_INV_VALUE:
vb@24
  1110
        return PEP_ILLEGAL_VALUE;
vb@24
  1111
    case GPG_ERR_GENERAL:
vb@24
  1112
        return PEP_CANNOT_CREATE_KEY;
vb@24
  1113
    default:
vb@24
  1114
        assert(0);
vb@24
  1115
        return PEP_UNKNOWN_ERROR;
vb@24
  1116
    }
vb@24
  1117
vb@74
  1118
    gpgme_genkey_result = gpg.gpgme_op_genkey_result(session->ctx);
vb@24
  1119
    assert(gpgme_genkey_result);
vb@24
  1120
    assert(gpgme_genkey_result->fpr);
vb@24
  1121
vb@298
  1122
    free(identity->fpr);
vb@24
  1123
    identity->fpr = strdup(gpgme_genkey_result->fpr);
vb@298
  1124
    if (identity->fpr == NULL)
vb@298
  1125
        return PEP_OUT_OF_MEMORY;
vb@24
  1126
vb@24
  1127
    return PEP_STATUS_OK;
vb@24
  1128
}
vb@24
  1129
vb@24
  1130
PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
  1131
{
vb@24
  1132
    gpgme_error_t gpgme_error;
vb@24
  1133
    gpgme_key_t key;
vb@24
  1134
vb@24
  1135
    assert(session);
vb@24
  1136
    assert(fpr);
vb@24
  1137
vb@74
  1138
    gpgme_error = gpg.gpgme_get_key(session->ctx, fpr, &key, 0);
vb@45
  1139
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1140
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1141
    switch (gpgme_error) {
vb@24
  1142
    case GPG_ERR_NO_ERROR:
vb@24
  1143
        break;
vb@24
  1144
    case GPG_ERR_EOF:
vb@24
  1145
        return PEP_KEY_NOT_FOUND;
vb@24
  1146
    case GPG_ERR_INV_VALUE:
vb@24
  1147
        return PEP_ILLEGAL_VALUE;
vb@24
  1148
    case GPG_ERR_AMBIGUOUS_NAME:
vb@24
  1149
        return PEP_KEY_HAS_AMBIG_NAME;
vb@24
  1150
    case GPG_ERR_ENOMEM:
vb@24
  1151
        return PEP_OUT_OF_MEMORY;
vb@24
  1152
    default:
vb@24
  1153
        assert(0);
vb@24
  1154
        return PEP_UNKNOWN_ERROR;
vb@24
  1155
    }
vb@24
  1156
vb@74
  1157
    gpgme_error = gpg.gpgme_op_delete(session->ctx, key, 1);
vb@45
  1158
    gpgme_error = _GPGERR(gpgme_error);
vb@74
  1159
    gpg.gpgme_key_unref(key);
vb@24
  1160
    switch (gpgme_error) {
vb@24
  1161
    case GPG_ERR_NO_ERROR:
vb@24
  1162
        break;
vb@24
  1163
    case GPG_ERR_INV_VALUE:
vb@24
  1164
        assert(0);
vb@24
  1165
        return PEP_UNKNOWN_ERROR;
vb@24
  1166
    case GPG_ERR_NO_PUBKEY:
vb@24
  1167
        assert(0);
vb@24
  1168
        return PEP_KEY_NOT_FOUND;
vb@24
  1169
    case GPG_ERR_AMBIGUOUS_NAME:
vb@24
  1170
        assert(0);
vb@24
  1171
        return PEP_KEY_HAS_AMBIG_NAME;
vb@24
  1172
    default:
vb@24
  1173
        assert(0);
vb@24
  1174
        return PEP_UNKNOWN_ERROR;
vb@24
  1175
    }
vb@24
  1176
vb@24
  1177
    return PEP_STATUS_OK;
vb@24
  1178
}
vb@24
  1179
Edouard@728
  1180
PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data,
Edouard@728
  1181
                              size_t size, identity_list **private_idents)
vb@24
  1182
{
vb@24
  1183
    gpgme_error_t gpgme_error;
vb@24
  1184
    gpgme_data_t dh;
vb@24
  1185
vb@24
  1186
    assert(session);
vb@24
  1187
    assert(key_data);
krista@1426
  1188
krista@1426
  1189
    if(private_idents)
Edouard@728
  1190
        *private_idents = NULL;
vb@24
  1191
vb@74
  1192
    gpgme_error = gpg.gpgme_data_new_from_mem(&dh, key_data, size, 0);
vb@45
  1193
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1194
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1195
    switch (gpgme_error) {
vb@24
  1196
    case GPG_ERR_NO_ERROR:
vb@24
  1197
        break;
vb@24
  1198
    case GPG_ERR_ENOMEM:
vb@24
  1199
        return PEP_OUT_OF_MEMORY;
vb@24
  1200
    case GPG_ERR_INV_VALUE:
vb@24
  1201
        assert(0);
vb@24
  1202
        return PEP_UNKNOWN_ERROR;
vb@24
  1203
    default:
vb@24
  1204
        assert(0);
vb@24
  1205
        return PEP_UNKNOWN_ERROR;
vb@24
  1206
    }
vb@24
  1207
Edouard@728
  1208
    gpgme_import_result_t gpgme_import_result;
Edouard@728
  1209
vb@74
  1210
    gpgme_error = gpg.gpgme_op_import(session->ctx, dh);
vb@45
  1211
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1212
    switch (gpgme_error) {
vb@24
  1213
    case GPG_ERR_NO_ERROR:
krista@1426
  1214
        if(private_idents)
Edouard@728
  1215
        {
Edouard@728
  1216
            gpgme_import_result =
Edouard@728
  1217
                gpg.gpgme_op_import_result(session->ctx);
Edouard@728
  1218
            assert(gpgme_import_result);
vb@1302
  1219
            if (!gpgme_import_result) {
vb@1302
  1220
                gpg.gpgme_data_release(dh);
vb@1302
  1221
                return PEP_UNKNOWN_ERROR;
vb@1302
  1222
            }
vb@1302
  1223
Edouard@728
  1224
            gpgme_import_status_t import;
krista@1426
  1225
            for (import = gpgme_import_result->imports;
krista@1426
  1226
                 import;
Edouard@728
  1227
                 import = import->next)
Edouard@728
  1228
             {
Edouard@728
  1229
                if (import &&
Edouard@728
  1230
                    import->result == GPG_ERR_NO_ERROR &&
Edouard@728
  1231
                    import->status & GPGME_IMPORT_SECRET )
Edouard@728
  1232
                {
Edouard@728
  1233
                    gpgme_key_t key = NULL;
Edouard@728
  1234
Edouard@728
  1235
                    gpgme_error = gpg.gpgme_get_key(session->ctx,
Edouard@728
  1236
                        import->fpr, &key, 0);
Edouard@728
  1237
                    gpgme_error = _GPGERR(gpgme_error);
Edouard@728
  1238
                    assert(gpgme_error != GPG_ERR_ENOMEM);
Edouard@728
  1239
                    if (gpgme_error == GPG_ERR_ENOMEM) {
Edouard@728
  1240
                        gpg.gpgme_data_release(dh);
Edouard@728
  1241
                        return PEP_OUT_OF_MEMORY;
Edouard@728
  1242
                    }
krista@1426
  1243
krista@1426
  1244
                    if (gpgme_error == GPG_ERR_NO_ERROR &&
krista@1426
  1245
                        key && key->uids &&
Edouard@728
  1246
                        key->uids->email && key->uids->name)
Edouard@728
  1247
                    {
Edouard@728
  1248
                        pEp_identity *ident = new_identity(
Edouard@728
  1249
                             key->uids->email, import->fpr, NULL, key->uids->name);
Edouard@728
  1250
Edouard@728
  1251
                        gpg.gpgme_key_unref(key);
Edouard@728
  1252
Edouard@728
  1253
                        if (ident == NULL) {
Edouard@728
  1254
                            gpg.gpgme_data_release(dh);
Edouard@728
  1255
                            return PEP_OUT_OF_MEMORY;
Edouard@728
  1256
                        }
Edouard@728
  1257
Edouard@728
  1258
                        *private_idents = identity_list_add(*private_idents, ident);
Edouard@728
  1259
Edouard@728
  1260
                        if (*private_idents == NULL) {
Edouard@728
  1261
                            gpg.gpgme_data_release(dh);
Edouard@728
  1262
                            return PEP_OUT_OF_MEMORY;
Edouard@728
  1263
                        }
Edouard@728
  1264
                    }
krista@1426
  1265
                    else
Edouard@728
  1266
                    {
Edouard@728
  1267
                        gpg.gpgme_key_unref(key);
Edouard@728
  1268
                        gpg.gpgme_data_release(dh);
Edouard@728
  1269
                        return PEP_UNKNOWN_ERROR;
Edouard@728
  1270
                    }
Edouard@728
  1271
                }
Edouard@728
  1272
            }
Edouard@728
  1273
        }
vb@24
  1274
        break;
vb@24
  1275
    case GPG_ERR_INV_VALUE:
vb@24
  1276
        assert(0);
vb@74
  1277
        gpg.gpgme_data_release(dh);
vb@24
  1278
        return PEP_UNKNOWN_ERROR;
vb@24
  1279
    case GPG_ERR_NO_DATA:
vb@74
  1280
        gpg.gpgme_data_release(dh);
vb@24
  1281
        return PEP_ILLEGAL_VALUE;
vb@24
  1282
    default:
vb@24
  1283
        assert(0);
vb@74
  1284
        gpg.gpgme_data_release(dh);
vb@24
  1285
        return PEP_UNKNOWN_ERROR;
vb@24
  1286
    }
vb@24
  1287
vb@74
  1288
    gpg.gpgme_data_release(dh);
vb@24
  1289
    return PEP_STATUS_OK;
vb@24
  1290
}
vb@24
  1291
Edouard@170
  1292
PEP_STATUS pgp_export_keydata(
vb@1103
  1293
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size,
vb@1103
  1294
        bool secret
vb@24
  1295
    )
vb@24
  1296
{
vb@24
  1297
    gpgme_error_t gpgme_error;
vb@24
  1298
    gpgme_data_t dh;
vb@24
  1299
    size_t _size;
vb@24
  1300
    char *buffer;
vb@24
  1301
    int reading;
vb@24
  1302
vb@24
  1303
    assert(session);
vb@24
  1304
    assert(fpr);
vb@24
  1305
    assert(key_data);
vb@24
  1306
    assert(size);
vb@24
  1307
vb@74
  1308
    gpgme_error = gpg.gpgme_data_new(&dh);
vb@45
  1309
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1310
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1311
    switch (gpgme_error) {
vb@24
  1312
    case GPG_ERR_NO_ERROR:
vb@24
  1313
        break;
vb@24
  1314
    case GPG_ERR_ENOMEM:
vb@24
  1315
        return PEP_OUT_OF_MEMORY;
vb@24
  1316
    case GPG_ERR_INV_VALUE:
vb@24
  1317
        assert(0);
vb@24
  1318
        return PEP_UNKNOWN_ERROR;
vb@24
  1319
    default:
vb@24
  1320
        assert(0);
vb@24
  1321
        return PEP_UNKNOWN_ERROR;
vb@24
  1322
    }
vb@24
  1323
vb@1103
  1324
    if (secret)
vb@1103
  1325
        gpgme_error = gpg.gpgme_op_export(session->ctx, fpr,
edouard@1163
  1326
            GPGME_EXPORT_MODE_SECRET, dh);
vb@1103
  1327
    else
vb@1103
  1328
        gpgme_error = gpg.gpgme_op_export(session->ctx, fpr,
vb@1103
  1329
            GPGME_EXPORT_MODE_MINIMAL, dh);
vb@45
  1330
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1331
    switch (gpgme_error) {
vb@24
  1332
    case GPG_ERR_NO_ERROR:
vb@24
  1333
        break;
vb@24
  1334
    case GPG_ERR_EOF:
vb@74
  1335
        gpg.gpgme_data_release(dh);
vb@24
  1336
        return PEP_KEY_NOT_FOUND;
vb@24
  1337
    case GPG_ERR_INV_VALUE:
vb@24
  1338
        assert(0);
vb@74
  1339
        gpg.gpgme_data_release(dh);
vb@24
  1340
        return PEP_UNKNOWN_ERROR;
vb@24
  1341
    default:
vb@24
  1342
        assert(0);
vb@74
  1343
        gpg.gpgme_data_release(dh);
vb@24
  1344
        return PEP_UNKNOWN_ERROR;
vb@24
  1345
    };
vb@24
  1346
vb@74
  1347
    _size = gpg.gpgme_data_seek(dh, 0, SEEK_END);
vb@24
  1348
    assert(_size != -1);
vb@74
  1349
    gpg.gpgme_data_seek(dh, 0, SEEK_SET);
vb@24
  1350
vb@24
  1351
    buffer = malloc(_size + 1);
vb@24
  1352
    assert(buffer);
vb@24
  1353
    if (buffer == NULL) {
vb@74
  1354
        gpg.gpgme_data_release(dh);
vb@24
  1355
        return PEP_OUT_OF_MEMORY;
vb@24
  1356
    }
vb@24
  1357
vb@74
  1358
    reading = gpg.gpgme_data_read(dh, buffer, _size);
vb@24
  1359
    assert(_size == reading);
edouard@1803
  1360
    if(_size != reading)
edouard@1803
  1361
        return PEP_CANNOT_EXPORT_KEY;
vb@24
  1362
vb@24
  1363
    // safeguard for the naive user
vb@24
  1364
    buffer[_size] = 0;
vb@24
  1365
vb@24
  1366
    *key_data = buffer;
vb@24
  1367
    *size = _size;
vb@24
  1368
vb@74
  1369
    gpg.gpgme_data_release(dh);
vb@24
  1370
    return PEP_STATUS_OK;
vb@24
  1371
}
vb@24
  1372
krista@1426
  1373
PEP_STATUS pgp_list_keyinfo(PEP_SESSION session, const char* pattern,
krista@1031
  1374
                            stringpair_list_t** keyinfo_list)
krista@1426
  1375
{
krista@1005
  1376
    gpgme_error_t gpgme_error;
krista@1005
  1377
    assert(session);
krista@1031
  1378
    assert(keyinfo_list);
krista@1426
  1379
krista@1031
  1380
    if (!session || !keyinfo_list)
krista@1005
  1381
        return PEP_ILLEGAL_VALUE;
krista@1426
  1382
krista@1031
  1383
    *keyinfo_list = NULL;
krista@1426
  1384
krista@1031
  1385
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
krista@1005
  1386
    gpgme_error = _GPGERR(gpgme_error);
krista@1426
  1387
krista@1005
  1388
    switch(gpgme_error) {
krista@1005
  1389
        case GPG_ERR_NO_ERROR:
krista@1005
  1390
            break;
krista@1005
  1391
        case GPG_ERR_INV_VALUE:
krista@1005
  1392
            assert(0);
krista@1005
  1393
            return PEP_UNKNOWN_ERROR;
krista@1005
  1394
        default:
krista@1005
  1395
            gpg.gpgme_op_keylist_end(session->ctx);
krista@1426
  1396
            return PEP_GET_KEY_FAILED;
krista@1005
  1397
    };
krista@1426
  1398
krista@1005
  1399
    gpgme_key_t key;
krista@1031
  1400
    stringpair_list_t* _keyinfo_list = new_stringpair_list(NULL);
krista@1031
  1401
    stringpair_list_t* list_curr = _keyinfo_list;
krista@1031
  1402
    stringpair_t* pair = NULL;
krista@1426
  1403
krista@1426
  1404
    do {
krista@1005
  1405
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
krista@1005
  1406
        gpgme_error = _GPGERR(gpgme_error);
krista@1426
  1407
krista@1005
  1408
        switch(gpgme_error) {
krista@1005
  1409
            case GPG_ERR_EOF:
krista@1005
  1410
                break;
krista@1005
  1411
            case GPG_ERR_NO_ERROR:
krista@1005
  1412
                assert(key);
krista@1005
  1413
                assert(key->subkeys);
krista@1005
  1414
                if (!key || !key->subkeys)
krista@1005
  1415
                    return PEP_GET_KEY_FAILED;
krista@1040
  1416
krista@1005
  1417
                // first subkey is primary key
krista@1005
  1418
                char* fpr = key->subkeys->fpr;
roker@1199
  1419
                char* uid = key->uids->uid;
krista@1187
  1420
krista@1005
  1421
                assert(fpr);
roker@1199
  1422
                assert(uid);
krista@1031
  1423
                if (!fpr)
krista@1005
  1424
                    return PEP_GET_KEY_FAILED;
krista@1426
  1425
krista@1050
  1426
                if (key->subkeys->revoked)
krista@1040
  1427
                    continue;
krista@1426
  1428
krista@1031
  1429
                pair = new_stringpair(fpr, uid);
krista@1019
  1430
krista@1031
  1431
                assert(pair);
krista@1426
  1432
krista@1031
  1433
                if (pair) {
krista@1031
  1434
                    list_curr = stringpair_list_add(list_curr, pair);
krista@1031
  1435
                    pair = NULL;
krista@1426
  1436
krista@1005
  1437
                    assert(list_curr);
krista@1005
  1438
                    if (list_curr != NULL)
krista@1005
  1439
                        break;
krista@1005
  1440
                    else
krista@1031
  1441
                        free_stringpair(pair);
krista@1005
  1442
                }
krista@1031
  1443
                // else fallthrough (list_curr or pair wasn't allocateable)
krista@1005
  1444
            case GPG_ERR_ENOMEM:
krista@1031
  1445
                free_stringpair_list(_keyinfo_list);
krista@1005
  1446
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1005
  1447
                return PEP_OUT_OF_MEMORY;
krista@1005
  1448
            default:
krista@1005
  1449
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1005
  1450
                return PEP_UNKNOWN_ERROR;
krista@1005
  1451
        }
krista@1005
  1452
    } while (gpgme_error != GPG_ERR_EOF);
krista@1426
  1453
krista@1032
  1454
    if (_keyinfo_list->value == NULL) {
krista@1031
  1455
        free_stringpair_list(_keyinfo_list);
krista@1031
  1456
        _keyinfo_list = NULL;
krista@1005
  1457
    }
krista@1426
  1458
krista@1031
  1459
    *keyinfo_list = _keyinfo_list;
krista@1426
  1460
krista@1005
  1461
    return PEP_STATUS_OK;
krista@1005
  1462
}
krista@1005
  1463
vb@46
  1464
static void _switch_mode(pEpSession *session, gpgme_keylist_mode_t remove_mode,
vb@24
  1465
    gpgme_keylist_mode_t add_mode)
vb@24
  1466
{
vb@24
  1467
    gpgme_error_t gpgme_error;
vb@24
  1468
    gpgme_keylist_mode_t mode;
vb@24
  1469
vb@74
  1470
    mode = gpg.gpgme_get_keylist_mode(session->ctx);
vb@24
  1471
vb@24
  1472
    mode &= ~remove_mode;
vb@24
  1473
    mode |= add_mode;
vb@24
  1474
vb@74
  1475
    gpgme_error = gpg.gpgme_set_keylist_mode(session->ctx, mode);
vb@45
  1476
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1477
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1478
}
vb@24
  1479
vb@24
  1480
PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1481
{
vb@24
  1482
    gpgme_error_t gpgme_error;
vb@24
  1483
    gpgme_key_t key;
vb@24
  1484
vb@24
  1485
    assert(session);
vb@24
  1486
    assert(pattern);
vb@24
  1487
vb@46
  1488
    _switch_mode(session, GPGME_KEYLIST_MODE_LOCAL, GPGME_KEYLIST_MODE_EXTERN);
vb@24
  1489
vb@74
  1490
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
vb@45
  1491
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1492
    switch (gpgme_error) {
vb@24
  1493
    case GPG_ERR_NO_ERROR:
vb@24
  1494
        break;
vb@24
  1495
    case GPG_ERR_INV_VALUE:
vb@24
  1496
        assert(0);
vb@47
  1497
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1498
        return PEP_UNKNOWN_ERROR;
vb@24
  1499
    default:
vb@47
  1500
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1501
        return PEP_GET_KEY_FAILED;
vb@24
  1502
    };
vb@24
  1503
vb@47
  1504
    gpgme_ctx_t import_ctx;
vb@74
  1505
    gpgme_error = gpg.gpgme_new(&import_ctx);
vb@47
  1506
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@47
  1507
vb@24
  1508
    do {
vb@74
  1509
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1510
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1511
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1512
        switch (gpgme_error) {
vb@24
  1513
        case GPG_ERR_EOF:
vb@24
  1514
            break;
vb@24
  1515
        case GPG_ERR_NO_ERROR:
vb@24
  1516
        {
vb@24
  1517
            gpgme_error_t gpgme_error;
vb@24
  1518
            gpgme_key_t keys[2];
vb@24
  1519
vb@24
  1520
            keys[0] = key;
vb@24
  1521
            keys[1] = NULL;
vb@24
  1522
vb@74
  1523
            gpgme_error = gpg.gpgme_op_import_keys(import_ctx, keys);
vb@45
  1524
            gpgme_error = _GPGERR(gpgme_error);
vb@74
  1525
            gpg.gpgme_key_unref(key);
vb@24
  1526
            assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1527
            assert(gpgme_error != GPG_ERR_CONFLICT);
vb@24
  1528
        }
vb@24
  1529
            break;
vb@24
  1530
        case GPG_ERR_ENOMEM:
vb@74
  1531
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1532
            gpg.gpgme_release(import_ctx);
vb@47
  1533
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1534
            return PEP_OUT_OF_MEMORY;
vb@24
  1535
        default:
vb@74
  1536
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1537
            gpg.gpgme_release(import_ctx);
vb@47
  1538
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@46
  1539
            return PEP_UNKNOWN_ERROR;
vb@24
  1540
        };
vb@24
  1541
    } while (gpgme_error != GPG_ERR_EOF);
vb@24
  1542
vb@74
  1543
    gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1544
    gpg.gpgme_release(import_ctx);
vb@47
  1545
    _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1546
    return PEP_STATUS_OK;
vb@24
  1547
}
vb@24
  1548
krista@1357
  1549
krista@1357
  1550
static PEP_STATUS _pgp_search_keys(PEP_SESSION session, const char* pattern,
krista@1357
  1551
                            stringlist_t** keylist,
krista@1357
  1552
                            int private_only) {
vb@24
  1553
    gpgme_error_t gpgme_error;
vb@24
  1554
    gpgme_key_t key;
krista@1426
  1555
vb@24
  1556
    assert(session);
vb@24
  1557
    assert(keylist);
krista@1426
  1558
vb@24
  1559
    *keylist = NULL;
krista@1426
  1560
krista@1357
  1561
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, private_only);
vb@45
  1562
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1563
    switch (gpgme_error) {
krista@1357
  1564
        case GPG_ERR_NO_ERROR:
krista@1357
  1565
            break;
krista@1357
  1566
        case GPG_ERR_INV_VALUE:
krista@1357
  1567
            assert(0);
krista@1357
  1568
            return PEP_UNKNOWN_ERROR;
krista@1357
  1569
        default:
krista@1357
  1570
            gpg.gpgme_op_keylist_end(session->ctx);
krista@1357
  1571
            return PEP_GET_KEY_FAILED;
vb@24
  1572
    };
krista@1426
  1573
roker@877
  1574
    stringlist_t *_keylist = new_stringlist(NULL);
vb@24
  1575
    stringlist_t *_k = _keylist;
krista@1426
  1576
vb@24
  1577
    do {
vb@74
  1578
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1579
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1580
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1581
        switch (gpgme_error) {
krista@1357
  1582
            case GPG_ERR_EOF:
vb@24
  1583
                break;
krista@1357
  1584
            case GPG_ERR_NO_ERROR:
krista@1357
  1585
                assert(key);
krista@1357
  1586
                assert(key->subkeys);
edouard@1525
  1587
                if(!key->subkeys)
krista@1357
  1588
                    break;
edouard@1525
  1589
                assert(key->uids);
edouard@1525
  1590
                gpgme_user_id_t kuid = key->uids;
edouard@1525
  1591
                // check that at least one uid's email matches pattern exactly
edouard@1525
  1592
                while(kuid) {
edouard@1760
  1593
                    if((pattern && kuid->email && strcmp(kuid->email, pattern) == 0) ||
edouard@1760
  1594
                       pattern == NULL /* match all */ )
edouard@1760
  1595
                    { 
edouard@1525
  1596
                        char *fpr = key->subkeys->fpr;
edouard@1525
  1597
                        assert(fpr);
edouard@1525
  1598
                        _k = stringlist_add(_k, fpr);
edouard@1525
  1599
                        assert(_k);
edouard@1525
  1600
                        if (_k == NULL){
edouard@1525
  1601
                            free_stringlist(_keylist);
edouard@1525
  1602
                            gpg.gpgme_op_keylist_end(session->ctx);
edouard@1525
  1603
                            return PEP_OUT_OF_MEMORY;
edouard@1525
  1604
                        }
edouard@1525
  1605
                        break;
edouard@1525
  1606
                    }
edouard@1525
  1607
                    kuid = kuid->next;
edouard@1525
  1608
                }
edouard@1525
  1609
                break;
krista@1357
  1610
            case GPG_ERR_ENOMEM:
krista@1357
  1611
                free_stringlist(_keylist);
krista@1357
  1612
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1357
  1613
                return PEP_OUT_OF_MEMORY;
krista@1357
  1614
            default:
krista@1357
  1615
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1357
  1616
                return PEP_UNKNOWN_ERROR;
vb@24
  1617
        };
vb@24
  1618
    } while (gpgme_error != GPG_ERR_EOF);
krista@1426
  1619
vb@74
  1620
    gpg.gpgme_op_keylist_end(session->ctx);
vb@523
  1621
    if (_keylist->value == NULL) {
vb@523
  1622
        free_stringlist(_keylist);
vb@523
  1623
        _keylist = NULL;
vb@523
  1624
    }
vb@24
  1625
    *keylist = _keylist;
vb@24
  1626
    return PEP_STATUS_OK;
vb@24
  1627
}
vb@24
  1628
krista@1357
  1629
PEP_STATUS pgp_find_keys(
krista@1357
  1630
    PEP_SESSION session, const char *pattern, stringlist_t **keylist
krista@1357
  1631
    )
krista@1357
  1632
{
krista@1357
  1633
    return _pgp_search_keys(session, pattern, keylist, 0);
krista@1426
  1634
}
krista@1357
  1635
krista@1357
  1636
PEP_STATUS pgp_find_private_keys(
krista@1357
  1637
    PEP_SESSION session, const char *pattern, stringlist_t **keylist
krista@1357
  1638
)
krista@1357
  1639
{
krista@1357
  1640
    return _pgp_search_keys(session, pattern, keylist, 1);
krista@1357
  1641
}
krista@1357
  1642
vb@24
  1643
PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)
vb@24
  1644
{
vb@24
  1645
    gpgme_error_t gpgme_error;
vb@24
  1646
vb@24
  1647
    assert(session);
vb@24
  1648
    assert(pattern);
vb@24
  1649
vb@74
  1650
    gpgme_error = gpg.gpgme_op_export(session->ctx, pattern,
vb@24
  1651
        GPGME_EXPORT_MODE_EXTERN, NULL);
vb@45
  1652
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1653
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1654
    if (gpgme_error == GPG_ERR_NO_ERROR)
vb@24
  1655
        return PEP_STATUS_OK;
vb@24
  1656
    else
vb@24
  1657
        return PEP_CANNOT_SEND_KEY;
vb@24
  1658
}
vb@24
  1659
vb@24
  1660
PEP_STATUS pgp_get_key_rating(
vb@24
  1661
    PEP_SESSION session,
vb@24
  1662
    const char *fpr,
vb@24
  1663
    PEP_comm_type *comm_type
vb@24
  1664
    )
vb@24
  1665
{
vb@24
  1666
    PEP_STATUS status = PEP_STATUS_OK;
vb@24
  1667
    gpgme_error_t gpgme_error;
vb@24
  1668
    gpgme_key_t key;
vb@24
  1669
vb@24
  1670
    assert(session);
vb@24
  1671
    assert(fpr);
vb@24
  1672
    assert(comm_type);
vb@24
  1673
vb@24
  1674
    *comm_type = PEP_ct_unknown;
vb@24
  1675
vb@74
  1676
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
vb@45
  1677
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1678
    switch (gpgme_error) {
vb@24
  1679
    case GPG_ERR_NO_ERROR:
vb@24
  1680
        break;
vb@24
  1681
    case GPG_ERR_INV_VALUE:
vb@24
  1682
        assert(0);
vb@24
  1683
        return PEP_UNKNOWN_ERROR;
vb@24
  1684
    default:
vb@24
  1685
        return PEP_GET_KEY_FAILED;
vb@24
  1686
    };
vb@24
  1687
vb@74
  1688
    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1689
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1690
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1691
vb@24
  1692
    if (key == NULL) {
vb@74
  1693
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1694
        return PEP_KEY_NOT_FOUND;
vb@24
  1695
    }
vb@24
  1696
vb@24
  1697
    switch (key->protocol) {
vb@24
  1698
    case GPGME_PROTOCOL_OpenPGP:
vb@24
  1699
    case GPGME_PROTOCOL_DEFAULT:
vb@24
  1700
        *comm_type = PEP_ct_OpenPGP_unconfirmed;
vb@24
  1701
        break;
vb@24
  1702
    case GPGME_PROTOCOL_CMS:
vb@24
  1703
        *comm_type = PEP_ct_CMS_unconfirmed;
vb@24
  1704
        break;
vb@24
  1705
    default:
vb@24
  1706
        *comm_type = PEP_ct_unknown;
vb@74
  1707
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1708
        return PEP_STATUS_OK;
vb@24
  1709
    }
vb@24
  1710
vb@24
  1711
    switch (gpgme_error) {
vb@24
  1712
    case GPG_ERR_EOF:
vb@24
  1713
        break;
vb@24
  1714
    case GPG_ERR_NO_ERROR:
vb@24
  1715
        assert(key);
vb@24
  1716
        assert(key->subkeys);
vb@24
  1717
        for (gpgme_subkey_t sk = key->subkeys; sk != NULL; sk = sk->next) {
vb@24
  1718
            if (sk->length < 1024)
vb@24
  1719
                *comm_type = PEP_ct_key_too_short;
vb@24
  1720
            else if (
vb@24
  1721
                (
vb@24
  1722
                (sk->pubkey_algo == GPGME_PK_RSA)
vb@24
  1723
                || (sk->pubkey_algo == GPGME_PK_RSA_E)
vb@24
  1724
                || (sk->pubkey_algo == GPGME_PK_RSA_S)
vb@24
  1725
                )
vb@24
  1726
                && sk->length == 1024
vb@24
  1727
                )
vb@122
  1728
                *comm_type = PEP_ct_OpenPGP_weak_unconfirmed;
vb@24
  1729
vb@24
  1730
            if (sk->invalid) {
vb@24
  1731
                *comm_type = PEP_ct_key_b0rken;
vb@24
  1732
                break;
vb@24
  1733
            }
vb@24
  1734
            if (sk->expired) {
vb@24
  1735
                *comm_type = PEP_ct_key_expired;
vb@24
  1736
                break;
vb@24
  1737
            }
vb@24
  1738
            if (sk->revoked) {
vb@24
  1739
                *comm_type = PEP_ct_key_revoked;
vb@24
  1740
                break;
vb@24
  1741
            }
vb@24
  1742
        }
vb@24
  1743
        break;
vb@24
  1744
    case GPG_ERR_ENOMEM:
vb@74
  1745
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1746
        *comm_type = PEP_ct_unknown;
vb@24
  1747
        return PEP_OUT_OF_MEMORY;
vb@24
  1748
    default:
vb@74
  1749
        gpg.gpgme_op_keylist_end(session->ctx);
vb@46
  1750
        return PEP_UNKNOWN_ERROR;
vb@24
  1751
    };
vb@24
  1752
vb@74
  1753
    gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1754
vb@24
  1755
    return status;
vb@24
  1756
}
vb@200
  1757
vb@200
  1758
static PEP_STATUS find_single_key(
vb@200
  1759
        PEP_SESSION session,
vb@200
  1760
        const char *fpr,
vb@200
  1761
        gpgme_key_t *key
vb@200
  1762
    )
vb@200
  1763
{
vb@200
  1764
    gpgme_error_t gpgme_error;
vb@200
  1765
vb@200
  1766
    *key = NULL;
vb@200
  1767
vb@200
  1768
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
vb@200
  1769
    gpgme_error = _GPGERR(gpgme_error);
vb@200
  1770
    switch (gpgme_error) {
vb@200
  1771
    case GPG_ERR_NO_ERROR:
vb@200
  1772
        break;
vb@200
  1773
    case GPG_ERR_INV_VALUE:
vb@200
  1774
        assert(0);
vb@200
  1775
        return PEP_UNKNOWN_ERROR;
vb@200
  1776
    default:
vb@200
  1777
        return PEP_GET_KEY_FAILED;
vb@200
  1778
    };
vb@200
  1779
vb@200
  1780
    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, key);
vb@200
  1781
    gpgme_error = _GPGERR(gpgme_error);
vb@200
  1782
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@200
  1783
vb@200
  1784
    gpg.gpgme_op_keylist_end(session->ctx);
vb@200
  1785
vb@200
  1786
    return PEP_STATUS_OK;
vb@200
  1787
}
vb@200
  1788
vb@201
  1789
typedef struct _renew_state {
vb@211
  1790
    enum {
vb@200
  1791
        renew_command = 0,
vb@200
  1792
        renew_date,
vb@200
  1793
        renew_secret_key,
vb@200
  1794
        renew_command2,
vb@200
  1795
        renew_date2,
vb@200
  1796
        renew_quit,
vb@200
  1797
        renew_save,
vb@200
  1798
        renew_exit,
vb@200
  1799
        renew_error = -1
vb@200
  1800
    } state;
vb@201
  1801
    const char *date_ref;
vb@201
  1802
} renew_state;
vb@200
  1803
vb@201
  1804
static gpgme_error_t renew_fsm(
vb@200
  1805
        void *_handle,
vb@200
  1806
        gpgme_status_code_t statuscode,
vb@200
  1807
        const char *args,
vb@200
  1808
        int fd
vb@200
  1809
    )
vb@200
  1810
{
vb@201
  1811
    renew_state *handle = _handle;
vb@200
  1812
vb@200
  1813
    switch (handle->state) {
vb@200
  1814
        case renew_command:
vb@200
  1815
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1816
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1817
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1818
                    handle->state = renew_error;
vb@201
  1819
                    return GPG_ERR_GENERAL;
vb@201
  1820
                }
vb@223
  1821
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  1822
                handle->state = renew_date;
vb@200
  1823
            }
vb@200
  1824
            break;
vb@200
  1825
vb@200
  1826
        case renew_date:
vb@200
  1827
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1828
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  1829
                if (strcmp(args, "keygen.valid")) {
vb@201
  1830
                    handle->state = renew_error;
vb@201
  1831
                    return GPG_ERR_GENERAL;
vb@201
  1832
                }
vb@223
  1833
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  1834
                handle->state = renew_secret_key;
vb@200
  1835
            }
vb@200
  1836
            break;
vb@200
  1837
vb@200
  1838
        case renew_secret_key:
vb@200
  1839
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1840
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1841
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1842
                    handle->state = renew_error;
vb@201
  1843
                    return GPG_ERR_GENERAL;
vb@201
  1844
                }
vb@223
  1845
                gpg.gpgme_io_write(fd, "key 1\n", 6);
vb@200
  1846
                handle->state = renew_command2;
vb@200
  1847
            }
vb@200
  1848
            break;
vb@200
  1849
vb@200
  1850
        case renew_command2:
vb@200
  1851
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1852
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1853
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1854
                    handle->state = renew_error;
vb@201
  1855
                    return GPG_ERR_GENERAL;
vb@201
  1856
                }
vb@223
  1857
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  1858
                handle->state = renew_date2;
vb@200
  1859
            }
vb@200
  1860
            break;
vb@200
  1861
vb@200
  1862
        case renew_date2:
vb@200
  1863
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1864
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  1865
                if (strcmp(args, "keygen.valid")) {
vb@201
  1866
                    handle->state = renew_error;
vb@201
  1867
                    return GPG_ERR_GENERAL;
vb@201
  1868
                }
vb@223
  1869
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  1870
                handle->state = renew_quit;
vb@200
  1871
            }
vb@200
  1872
            break;
vb@200
  1873
vb@200
  1874
        case renew_quit:
vb@200
  1875
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1876
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1877
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1878
                    handle->state = renew_error;
vb@201
  1879
                    return GPG_ERR_GENERAL;
vb@201
  1880
                }
vb@223
  1881
                gpg.gpgme_io_write(fd, "quit\n", 5);
vb@200
  1882
                handle->state = renew_save;
vb@200
  1883
            }
vb@200
  1884
            break;
vb@200
  1885
vb@200
  1886
        case renew_save:
vb@200
  1887
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@201
  1888
                assert(strcmp(args, "keyedit.save.okay") == 0);
vb@201
  1889
                if (strcmp(args, "keyedit.save.okay")) {
vb@201
  1890
                    handle->state = renew_error;
vb@201
  1891
                    return GPG_ERR_GENERAL;
vb@201
  1892
                }
vb@223
  1893
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@200
  1894
                handle->state = renew_exit;
vb@200
  1895
            }
vb@200
  1896
            break;
vb@200
  1897
vb@200
  1898
        case renew_exit:
vb@200
  1899
            break;
vb@200
  1900
vb@200
  1901
        case renew_error:
vb@200
  1902
            return GPG_ERR_GENERAL;
vb@200
  1903
    }
vb@200
  1904
vb@200
  1905
    return GPG_ERR_NO_ERROR;
vb@200
  1906
}
vb@200
  1907
vb@200
  1908
static ssize_t _nullwriter(
vb@200
  1909
        void *_handle,
vb@200
  1910
        const void *buffer,
vb@200
  1911
        size_t size
vb@200
  1912
    )
vb@200
  1913
{
vb@200
  1914
    return size;
vb@200
  1915
}
vb@200
  1916
vb@201
  1917
PEP_STATUS pgp_renew_key(
vb@201
  1918
        PEP_SESSION session,
vb@201
  1919
        const char *fpr,
vb@201
  1920
        const timestamp *ts
vb@201
  1921
    )
vb@200
  1922
{
vb@200
  1923
    PEP_STATUS status = PEP_STATUS_OK;
vb@200
  1924
    gpgme_error_t gpgme_error;
vb@200
  1925
    gpgme_key_t key;
vb@200
  1926
    gpgme_data_t output;
vb@201
  1927
    renew_state handle;
vb@203
  1928
    char date_text[12];
vb@200
  1929
vb@200
  1930
    assert(session);
vb@200
  1931
    assert(fpr);
vb@200
  1932
vb@201
  1933
    memset(&handle, 0, sizeof(renew_state));
vb@203
  1934
    snprintf(date_text, 12, "%.4d-%.2d-%.2d\n", ts->tm_year + 1900,
vb@201
  1935
            ts->tm_mon + 1, ts->tm_mday);
vb@201
  1936
    handle.date_ref = date_text;
vb@200
  1937
vb@200
  1938
    status = find_single_key(session, fpr, &key);
vb@200
  1939
    if (status != PEP_STATUS_OK)
vb@200
  1940
        return status;
vb@200
  1941
vb@200
  1942
    struct gpgme_data_cbs data_cbs;
vb@200
  1943
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
vb@200
  1944
    data_cbs.write = _nullwriter;
vb@220
  1945
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
vb@200
  1946
vb@220
  1947
    gpgme_error = gpg.gpgme_op_edit(session->ctx, key, renew_fsm, &handle,
vb@200
  1948
            output);
vb@200
  1949
    assert(gpgme_error == GPG_ERR_NO_ERROR);
edouard@1802
  1950
    if(gpgme_error != GPG_ERR_NO_ERROR) {
edouard@1802
  1951
        status = PEP_CANNOT_EDIT_KEY;
edouard@1802
  1952
    }
vb@200
  1953
vb@200
  1954
    gpg.gpgme_data_release(output);
vb@200
  1955
    gpg.gpgme_key_unref(key);
vb@200
  1956
edouard@1802
  1957
    return status;
vb@200
  1958
}
vb@200
  1959
vb@211
  1960
typedef struct _revoke_state {
vb@211
  1961
    enum {
vb@211
  1962
        revoke_command = 0,
vb@211
  1963
        revoke_approve,
vb@211
  1964
        revoke_reason_code,
vb@211
  1965
        revoke_reason_text,
vb@211
  1966
        revoke_reason_ok,
vb@211
  1967
        revoke_quit,
vb@211
  1968
        revoke_save,
vb@211
  1969
        revoke_exit,
vb@211
  1970
        revoke_error = -1
vb@211
  1971
    } state;
vb@211
  1972
    const char *reason_ref;
vb@211
  1973
} revoke_state;
vb@211
  1974
roker@723
  1975
roker@723
  1976
/*** unused?
vb@211
  1977
static bool isemptystring(const char *str)
vb@211
  1978
{
vb@211
  1979
    if (str == NULL)
vb@211
  1980
        return true;
vb@211
  1981
vb@211
  1982
    for (; str; str++) {
vb@211
  1983
        if (*str != ' ' && *str != '\t' && *str != '\n')
vb@211
  1984
            return false;
vb@211
  1985
    }
vb@211
  1986
vb@211
  1987
    return true;
vb@211
  1988
}
roker@723
  1989
***/
roker@723
  1990
vb@211
  1991
vb@211
  1992
static gpgme_error_t revoke_fsm(
vb@211
  1993
        void *_handle,
vb@211
  1994
        gpgme_status_code_t statuscode,
vb@211
  1995
        const char *args,
vb@211
  1996
        int fd
vb@211
  1997
    )
vb@211
  1998
{
vb@211
  1999
    revoke_state *handle = _handle;
vb@211
  2000
vb@211
  2001
    switch (handle->state) {
vb@211
  2002
        case revoke_command:
vb@211
  2003
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  2004
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@211
  2005
                if (strcmp(args, "keyedit.prompt")) {
vb@211
  2006
                    handle->state = revoke_error;
vb@211
  2007
                    return GPG_ERR_GENERAL;
vb@211
  2008
                }
vb@223
  2009
                gpg.gpgme_io_write(fd, "revkey\n", 7);
vb@211
  2010
                handle->state = revoke_approve;
vb@211
  2011
            }
vb@211
  2012
            break;
vb@211
  2013
vb@211
  2014
        case revoke_approve:
vb@211
  2015
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  2016
                assert(strcmp(args, "keyedit.revoke.subkey.okay") == 0);
vb@211
  2017
                if (strcmp(args, "keyedit.revoke.subkey.okay")) {
vb@211
  2018
                    handle->state = revoke_error;
vb@211
  2019
                    return GPG_ERR_GENERAL;
vb@211
  2020
                }
vb@223
  2021
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  2022
                handle->state = revoke_reason_code;
vb@211
  2023
            }
vb@211
  2024
            break;
vb@211
  2025
vb@211
  2026
        case revoke_reason_code:
vb@211
  2027
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  2028
                assert(strcmp(args, "ask_revocation_reason.code") == 0);
vb@211
  2029
                if (strcmp(args, "ask_revocation_reason.code")) {
vb@211
  2030
                    handle->state = revoke_error;
vb@211
  2031
                    return GPG_ERR_GENERAL;
vb@211
  2032
                }
vb@223
  2033
                gpg.gpgme_io_write(fd, "1\n", 2);
vb@211
  2034
                handle->state = revoke_reason_text;
vb@211
  2035
            }
vb@211
  2036
            break;
vb@211
  2037
vb@211
  2038
        case revoke_reason_text:
vb@211
  2039
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  2040
                assert(strcmp(args, "ask_revocation_reason.text") == 0);
vb@211
  2041
                if (strcmp(args, "ask_revocation_reason.text")) {
vb@211
  2042
                    handle->state = revoke_error;
vb@211
  2043
                    return GPG_ERR_GENERAL;
vb@211
  2044
                }
vb@213
  2045
                // BUG: issues when reason given
vb@213
  2046
                // Assertion failed: (gpg->cmd.code), function command_handler,
vb@213
  2047
                // file engine-gpg.c, line 662.
vb@213
  2048
                //
vb@213
  2049
                // if (isemptystring(handle->reason_ref)) {
vb@223
  2050
                    gpg.gpgme_io_write(fd, "\n", 1);
vb@213
  2051
                // }
vb@213
  2052
                // else {
vb@213
  2053
                //     size_t len = strlen(handle->reason_ref);
vb@223
  2054
                //     gpg.gpgme_io_write(fd, handle->reason_ref, len);
vb@213
  2055
                //     if (handle->reason_ref[len - 1] == '\n')
vb@223
  2056
                //         gpg.gpgme_io_write(fd, "\n", 1);
vb@213
  2057
                //     else
vb@223
  2058
                //         gpg.gpgme_io_write(fd, "\n\n", 2);
vb@213
  2059
                // }
vb@211
  2060
                handle->state = revoke_reason_ok;
vb@211
  2061
            }
vb@211
  2062
            break;
vb@211
  2063
vb@211
  2064
        case revoke_reason_ok:
vb@211
  2065
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  2066
                assert(strcmp(args, "ask_revocation_reason.okay") == 0);
vb@211
  2067
                if (strcmp(args, "ask_revocation_reason.okay")) {
vb@211
  2068
                    handle->state = revoke_error;
vb@211
  2069
                    return GPG_ERR_GENERAL;
vb@211
  2070
                }
vb@223
  2071
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  2072
                handle->state = revoke_quit;
vb@211
  2073
            }
vb@211
  2074
            break;
vb@211
  2075
vb@211
  2076
        case revoke_quit:
vb@211
  2077
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  2078
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@211
  2079
                if (strcmp(args, "keyedit.prompt")) {
vb@211
  2080
                    handle->state = revoke_error;
vb@211
  2081
                    return GPG_ERR_GENERAL;
vb@211
  2082
                }
vb@223
  2083
                gpg.gpgme_io_write(fd, "quit\n", 5);
vb@211
  2084
                handle->state = revoke_save;
vb@211
  2085
            }
vb@211
  2086
            break;
vb@211
  2087
vb@211
  2088
        case revoke_save:
vb@211
  2089
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  2090
                assert(strcmp(args, "keyedit.save.okay") == 0);
vb@211
  2091
                if (strcmp(args, "keyedit.save.okay")) {
vb@211
  2092
                    handle->state = revoke_error;
vb@211
  2093
                    return GPG_ERR_GENERAL;
vb@211
  2094
                }
vb@223
  2095
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  2096
                handle->state = revoke_exit;
vb@211
  2097
            }
vb@211
  2098
            break;
vb@211
  2099
vb@211
  2100
        case revoke_exit:
vb@211
  2101
            break;
vb@211
  2102
vb@211
  2103
        case revoke_error:
vb@211
  2104
            return GPG_ERR_GENERAL;
vb@211
  2105
    }
vb@211
  2106
vb@211
  2107
    return GPG_ERR_NO_ERROR;
vb@211
  2108
}
vb@211
  2109
vb@211
  2110
PEP_STATUS pgp_revoke_key(
vb@211
  2111
        PEP_SESSION session,
vb@211
  2112
        const char *fpr,
vb@211
  2113
        const char *reason
vb@211
  2114
    )
vb@200
  2115
{
vb@200
  2116
    PEP_STATUS status = PEP_STATUS_OK;
vb@211
  2117
    gpgme_error_t gpgme_error;
vb@200
  2118
    gpgme_key_t key;
vb@211
  2119
    gpgme_data_t output;
vb@211
  2120
    revoke_state handle;
vb@211
  2121
vb@200
  2122
    assert(session);
vb@200
  2123
    assert(fpr);
vb@200
  2124
vb@211
  2125
    memset(&handle, 0, sizeof(revoke_state));
vb@213
  2126
    handle.reason_ref = reason;
vb@211
  2127
vb@200
  2128
    status = find_single_key(session, fpr, &key);
vb@200
  2129
    if (status != PEP_STATUS_OK)
vb@200
  2130
        return status;
vb@200
  2131
vb@211
  2132
    struct gpgme_data_cbs data_cbs;
vb@211
  2133
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
vb@211
  2134
    data_cbs.write = _nullwriter;
vb@220
  2135
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
vb@211
  2136
vb@220
  2137
    gpgme_error = gpg.gpgme_op_edit(session->ctx, key, revoke_fsm, &handle,
vb@211
  2138
            output);
vb@211
  2139
    assert(gpgme_error == GPG_ERR_NO_ERROR);
edouard@1802
  2140
    if(gpgme_error != GPG_ERR_NO_ERROR) {
edouard@1802
  2141
        status = PEP_CANNOT_EDIT_KEY;
edouard@1802
  2142
    }
vb@211
  2143
vb@211
  2144
    gpg.gpgme_data_release(output);
vb@211
  2145
    gpg.gpgme_key_unref(key);
vb@211
  2146
edouard@1802
  2147
    return status;
vb@200
  2148
}
vb@200
  2149
vb@214
  2150
PEP_STATUS pgp_key_expired(
vb@214
  2151
        PEP_SESSION session,
vb@214
  2152
        const char *fpr,
Edouard@701
  2153
        const time_t when,
vb@214
  2154
        bool *expired
vb@214
  2155
    )
vb@214
  2156
{
vb@214
  2157
    PEP_STATUS status = PEP_STATUS_OK;
vb@214
  2158
    gpgme_key_t key;
vb@214
  2159
vb@214
  2160
    assert(session);
vb@214
  2161
    assert(fpr);
vb@214
  2162
    assert(expired);
vb@214
  2163
vb@214
  2164
    *expired = false;
vb@214
  2165
vb@214
  2166
    status = find_single_key(session, fpr, &key);
vb@214
  2167
    if (status != PEP_STATUS_OK)
vb@214
  2168
        return status;
vb@214
  2169
Edouard@701
  2170
    if ((key && key->expired) ||
Edouard@701
  2171
        (key && key->subkeys && key->subkeys->expired))
Edouard@620
  2172
    {
Edouard@701
  2173
        // Already marked expired
Edouard@701
  2174
        *expired = 1;
Edouard@701
  2175
    }
Edouard@701
  2176
    else if (key)
Edouard@701
  2177
    {
Edouard@701
  2178
        // Detect if will be expired
Edouard@701
  2179
        // i.e. Check that keys capabilities will
Edouard@701
  2180
        // not be expired at given time.
Edouard@701
  2181
        gpgme_subkey_t _sk;
Edouard@701
  2182
        bool crt_available = false;
Edouard@701
  2183
        bool sgn_available = false;
Edouard@701
  2184
        bool enc_available = false;
Edouard@701
  2185
        for (_sk = key->subkeys; _sk; _sk = _sk->next) {
krista@1326
  2186
            if (_sk->expires > when || _sk->expires == 0) // not expired at that date ?
krista@1326
  2187
                                                          // Also, zero means "does not expire"
Edouard@701
  2188
            {
Edouard@701
  2189
                if (_sk->can_certify) crt_available = true;
Edouard@701
  2190
                if (_sk->can_sign) sgn_available = true;
Edouard@701
  2191
                if (_sk->can_encrypt) enc_available = true;
Edouard@701
  2192
                // Authenticate is not used here.
Edouard@701
  2193
            }
Edouard@701
  2194
        }
Edouard@701
  2195
        if(!(crt_available && sgn_available && enc_available))
Edouard@701
  2196
        {
Edouard@701
  2197
            *expired = 1;
Edouard@701
  2198
        }
Edouard@620
  2199
    }
Edouard@620
  2200
    else
Edouard@620
  2201
    {
Edouard@620
  2202
        status = PEP_KEY_NOT_FOUND;
Edouard@620
  2203
    }
Edouard@620
  2204
vb@214
  2205
    gpg.gpgme_key_unref(key);
Edouard@620
  2206
    return status;
vb@214
  2207
}
vb@214
  2208
Edouard@662
  2209
PEP_STATUS pgp_key_revoked(
Edouard@662
  2210
        PEP_SESSION session,
Edouard@662
  2211
        const char *fpr,
Edouard@662
  2212
        bool *revoked
Edouard@662
  2213
    )
Edouard@662
  2214
{
Edouard@662
  2215
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@662
  2216
    gpgme_key_t key;
Edouard@662
  2217
Edouard@662
  2218
    assert(session);
Edouard@662
  2219
    assert(fpr);
Edouard@662
  2220
    assert(revoked);
Edouard@662
  2221
Edouard@664
  2222
    *revoked = false;
Edouard@662
  2223
Edouard@662
  2224
    status = find_single_key(session, fpr, &key);
Edouard@662
  2225
    if (status != PEP_STATUS_OK)
Edouard@662
  2226
        return status;
Edouard@662
  2227
Edouard@662
  2228
    if (key && key->subkeys)
Edouard@662
  2229
    {
Edouard@664
  2230
        *revoked = key->subkeys->revoked;
Edouard@662
  2231
    }
Edouard@662
  2232
    else
Edouard@662
  2233
    {
Edouard@662
  2234
        status = PEP_KEY_NOT_FOUND;
Edouard@662
  2235
    }
Edouard@662
  2236
Edouard@662
  2237
    gpg.gpgme_key_unref(key);
Edouard@662
  2238
    return status;
Edouard@662
  2239
}
Edouard@662
  2240
vb@958
  2241
PEP_STATUS pgp_key_created(
vb@958
  2242
        PEP_SESSION session,
vb@958
  2243
        const char *fpr,
vb@958
  2244
        time_t *created
vb@958
  2245
    )
vb@958
  2246
{
vb@958
  2247
    PEP_STATUS status = PEP_STATUS_OK;
vb@958
  2248
    gpgme_key_t key;
vb@958
  2249
vb@958
  2250
    assert(session);
vb@958
  2251
    assert(fpr);
vb@958
  2252
    assert(created);
vb@958
  2253
vb@959
  2254
    *created = 0;
vb@959
  2255
vb@958
  2256
    status = find_single_key(session, fpr, &key);
vb@958
  2257
    if (status != PEP_STATUS_OK)
vb@958
  2258
        return status;
vb@958
  2259
vb@958
  2260
    if (key && key->subkeys)
vb@958
  2261
    {
vb@958
  2262
        *created = (time_t) key->subkeys->timestamp;
vb@958
  2263
    }
vb@958
  2264
    else
vb@958
  2265
    {
vb@958
  2266
        status = PEP_KEY_NOT_FOUND;
vb@958
  2267
    }
vb@958
  2268
vb@958
  2269
    gpg.gpgme_key_unref(key);
vb@958
  2270
    return status;
vb@958
  2271
}
vb@958
  2272
vb@507
  2273
PEP_STATUS pgp_binary(const char **path)
vb@507
  2274
{
vb@507
  2275
    assert(path);
vb@507
  2276
    if (path == NULL)
vb@507
  2277
        return PEP_ILLEGAL_VALUE;
vb@507
  2278
vb@507
  2279
    *path = NULL;
vb@507
  2280
vb@507
  2281
    gpgme_engine_info_t info;
vb@507
  2282
    int err = gpg.gpgme_get_engine_info(&info);
vb@507
  2283
    assert(err == GPG_ERR_NO_ERROR);
vb@507
  2284
    if (err != GPG_ERR_NO_ERROR)
vb@507
  2285
        return PEP_OUT_OF_MEMORY;
vb@507
  2286
vb@507
  2287
    *path = info->file_name;
vb@507
  2288
vb@507
  2289
    return PEP_STATUS_OK;
vb@507
  2290
}
vb@507
  2291
krista@1357
  2292
PEP_STATUS pgp_contains_priv_key(PEP_SESSION session, const char *fpr,
krista@1352
  2293
        bool *has_private) {
krista@1357
  2294
    PEP_STATUS status = PEP_STATUS_OK;
krista@1357
  2295
    gpgme_key_t output_key;
krista@1357
  2296
    gpgme_error_t gpgerr = gpg.gpgme_get_key(session->ctx, fpr, &output_key, true);
krista@1352
  2297
    *has_private = false;
krista@1352
  2298
    switch (gpgerr) {
krista@1352
  2299
        case GPG_ERR_EOF:
krista@1352
  2300
        case GPG_ERR_INV_VALUE:
krista@1352
  2301
            status = PEP_KEY_NOT_FOUND;
krista@1352
  2302
            break;
krista@1352
  2303
        case GPG_ERR_AMBIGUOUS_NAME:
krista@1352
  2304
            status = PEP_KEY_HAS_AMBIG_NAME;
krista@1352
  2305
            break;
krista@1352
  2306
        case GPG_ERR_NO_ERROR:
krista@1352
  2307
            *has_private = true;
vb@1387
  2308
            gpg.gpgme_key_release(output_key);
krista@1352
  2309
            break;
krista@1352
  2310
        case GPG_ERR_ENOMEM:
krista@1352
  2311
            status = PEP_OUT_OF_MEMORY;
krista@1352
  2312
            break;
krista@1352
  2313
        default:
krista@1352
  2314
            status = PEP_UNKNOWN_ERROR;
krista@1352
  2315
            break;
krista@1352
  2316
    }
krista@1352
  2317
    return status;
edouard@1385
  2318
}