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