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