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