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