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