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