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