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