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