src/pgp_gpg.c
author Edouard Tisserant
Sun, 22 May 2016 15:14:47 +0200
changeset 674 a8afa1168b12
parent 664 1bec487d0d47
child 677 0fe0f82eb621
permissions -rw-r--r--
Fixes for running pEpEngine tests in debian
vb@219
     1
#include "platform.h"
vb@130
     2
#include "pEp_internal.h"
vb@24
     3
#include "pgp_gpg.h"
vb@24
     4
vb@78
     5
#include <limits.h>
vb@78
     6
vb@62
     7
#include "wrappers.h"
vb@62
     8
vb@45
     9
#define _GPGERR(X) ((X) & 0xffffL)
vb@45
    10
vb@76
    11
static void *gpgme;
vb@76
    12
static struct gpg_s gpg;
vb@74
    13
vb@78
    14
static bool ensure_config_values(stringlist_t *keys, stringlist_t *values)
vb@24
    15
{
vb@24
    16
    static char buf[MAX_LINELENGTH];
vb@78
    17
    int r;
vb@62
    18
    FILE *f;
vb@78
    19
    stringlist_t *_k;
vb@78
    20
    stringlist_t *_v;
vb@79
    21
    unsigned int i;
vb@78
    22
    unsigned int found = 0;
vb@62
    23
vb@62
    24
    f = Fopen(gpg_conf(), "r");
vb@63
    25
    if (f == NULL && errno == ENOMEM)
vb@62
    26
        return false;
vb@24
    27
vb@24
    28
    if (f != NULL) {
vb@78
    29
        int length = stringlist_length(keys);
vb@79
    30
        unsigned int n = (1 << length) - 1;
vb@78
    31
vb@78
    32
        assert(length <= sizeof(unsigned int) * CHAR_BIT);
vb@78
    33
        assert(length == stringlist_length(values));
vb@78
    34
vb@62
    35
        do {
vb@62
    36
            char * s;
vb@62
    37
vb@63
    38
            s = Fgets(buf, MAX_LINELENGTH, f);
vb@78
    39
            if (!feof(f)) {
vb@78
    40
                assert(s);
vb@78
    41
                if (s == NULL)
vb@78
    42
                    return false;
vb@62
    43
vb@78
    44
                if (s && !feof(f)) {
vb@80
    45
                    char * rest;
vb@80
    46
                    char * t = strtok_r(s, " ", &rest);
vb@78
    47
                    for (i = 1, _k = keys, _v = values; _k != NULL;
vb@78
    48
                            _k = _k->next, _v = _v->next, i <<= 1) {
vb@80
    49
                        if (t && strncmp(t, _k->value, strlen(_k->value)) == 0)
vb@78
    50
                            found |= i;
vb@78
    51
vb@78
    52
                        if (i == n) {
vb@78
    53
                            r = Fclose(f);
vb@78
    54
                            return true;
vb@78
    55
                        }
vb@78
    56
                    }
vb@24
    57
                }
vb@24
    58
            }
vb@62
    59
        } while (!feof(f));
vb@63
    60
        f = Freopen(gpg_conf(), "a", f);
vb@24
    61
    }
vb@24
    62
    else {
vb@63
    63
        f = Fopen(gpg_conf(), "w");
vb@24
    64
    }
vb@24
    65
vb@24
    66
    assert(f);
vb@24
    67
    if (f == NULL)
vb@24
    68
        return false;
vb@24
    69
vb@78
    70
    for (i = 1, _k = keys, _v = values; _k != NULL; _k = _k->next,
vb@78
    71
            _v = _v->next, i <<= 1) {
vb@78
    72
        if ((found & i) == 0) {
vb@79
    73
            r = Fprintf(f, "%s %s\n", _k->value, _v->value);
vb@79
    74
            assert(r >= 0);
vb@78
    75
        }
vb@78
    76
    }
vb@62
    77
vb@63
    78
    r = Fclose(f);
vb@62
    79
    assert(r == 0);
vb@24
    80
vb@24
    81
    return true;
vb@24
    82
}
vb@24
    83
vb@62
    84
PEP_STATUS pgp_init(PEP_SESSION session, bool in_first)
vb@24
    85
{
vb@66
    86
    PEP_STATUS status = PEP_STATUS_OK;
vb@24
    87
    gpgme_error_t gpgme_error;
vb@62
    88
    bool bResult;
Edouard@348
    89
    char *cLocal;
vb@62
    90
    
vb@62
    91
    if (in_first) {
vb@78
    92
        stringlist_t *conf_keys   = new_stringlist("keyserver");
vb@78
    93
        stringlist_t *conf_values = new_stringlist("hkp://keys.gnupg.net");
vb@79
    94
vb@78
    95
        stringlist_add(conf_keys, "cert-digest-algo");
vb@78
    96
        stringlist_add(conf_values, "SHA256");
vb@78
    97
vb@80
    98
        stringlist_add(conf_keys, "no-emit-version");
vb@80
    99
        stringlist_add(conf_values, "");
vb@80
   100
vb@80
   101
        stringlist_add(conf_keys, "no-comments");
vb@80
   102
        stringlist_add(conf_values, "");
vb@80
   103
vb@80
   104
        stringlist_add(conf_keys, "personal-cipher-preferences");
vb@80
   105
        stringlist_add(conf_values, "AES AES256 AES192 CAST5");
vb@80
   106
vb@80
   107
        stringlist_add(conf_keys, "personal-digest-preferences");
vb@312
   108
        stringlist_add(conf_values, "SHA256 SHA512 SHA384 SHA224");
vb@80
   109
        
vb@78
   110
        bResult = ensure_config_values(conf_keys, conf_values);
vb@24
   111
vb@78
   112
        free_stringlist(conf_keys);
vb@78
   113
        free_stringlist(conf_values);
vb@78
   114
Edouard@175
   115
        assert(bResult);
Edouard@175
   116
        if(!bResult){
Edouard@175
   117
            status = PEP_INIT_NO_GPG_HOME;
Edouard@175
   118
            goto pep_error;
Edouard@175
   119
        }
Edouard@175
   120
Edouard@175
   121
vb@62
   122
        gpgme = dlopen(LIBGPGME, RTLD_LAZY);
vb@62
   123
        if (gpgme == NULL) {
vb@66
   124
            status = PEP_INIT_CANNOT_LOAD_GPGME;
vb@66
   125
            goto pep_error;
vb@62
   126
        }
vb@62
   127
vb@62
   128
        memset(&gpg, 0, sizeof(struct gpg_s));
vb@62
   129
vb@62
   130
        gpg.gpgme_set_locale
vb@62
   131
            = (gpgme_set_locale_t) (intptr_t) dlsym(gpgme,
vb@62
   132
            "gpgme_set_locale");
vb@62
   133
        assert(gpg.gpgme_set_locale);
vb@62
   134
vb@62
   135
        gpg.gpgme_check
vb@62
   136
            = (gpgme_check_version_t) (intptr_t) dlsym(gpgme,
vb@62
   137
            "gpgme_check_version");
vb@62
   138
        assert(gpg.gpgme_check);
vb@62
   139
vb@62
   140
        gpg.gpgme_new
vb@62
   141
            = (gpgme_new_t) (intptr_t) dlsym(gpgme, "gpgme_new");
vb@62
   142
        assert(gpg.gpgme_new);
vb@62
   143
vb@62
   144
        gpg.gpgme_release
vb@62
   145
            = (gpgme_release_t) (intptr_t) dlsym(gpgme, "gpgme_release");
vb@62
   146
        assert(gpg.gpgme_release);
vb@62
   147
vb@507
   148
        gpg.gpgme_get_engine_info
vb@507
   149
            = (gpgme_get_engine_info_t) (intptr_t) dlsym(gpgme,
vb@507
   150
            "gpgme_get_engine_info");
vb@507
   151
        assert(gpg.gpgme_get_engine_info);
vb@507
   152
vb@62
   153
        gpg.gpgme_set_protocol
vb@62
   154
            = (gpgme_set_protocol_t) (intptr_t) dlsym(gpgme,
vb@62
   155
            "gpgme_set_protocol");
vb@62
   156
        assert(gpg.gpgme_set_protocol);
vb@62
   157
vb@62
   158
        gpg.gpgme_set_armor
vb@62
   159
            = (gpgme_set_armor_t) (intptr_t) dlsym(gpgme,
vb@62
   160
            "gpgme_set_armor");
vb@62
   161
        assert(gpg.gpgme_set_armor);
vb@62
   162
vb@62
   163
        gpg.gpgme_data_new
vb@62
   164
            = (gpgme_data_new_t) (intptr_t) dlsym(gpgme,
vb@62
   165
            "gpgme_data_new");
vb@62
   166
        assert(gpg.gpgme_data_new);
vb@62
   167
vb@62
   168
        gpg.gpgme_data_new_from_mem
vb@62
   169
            = (gpgme_data_new_from_mem_t) (intptr_t) dlsym(gpgme,
vb@62
   170
            "gpgme_data_new_from_mem");
vb@62
   171
        assert(gpg.gpgme_data_new_from_mem);
vb@62
   172
vb@221
   173
        gpg.gpgme_data_new_from_cbs
vb@221
   174
            = (gpgme_data_new_from_cbs_t) (intptr_t) dlsym(gpgme,
vb@221
   175
            "gpgme_data_new_from_cbs");
vb@221
   176
        assert(gpg.gpgme_data_new_from_cbs);
vb@221
   177
vb@62
   178
        gpg.gpgme_data_release
vb@62
   179
            = (gpgme_data_release_t) (intptr_t) dlsym(gpgme,
vb@62
   180
            "gpgme_data_release");
vb@62
   181
        assert(gpg.gpgme_data_release);
vb@62
   182
vb@62
   183
        gpg.gpgme_data_identify
vb@62
   184
            = (gpgme_data_identify_t) (intptr_t) dlsym(gpgme,
vb@62
   185
            "gpgme_data_identify");
vb@62
   186
        assert(gpg.gpgme_data_identify);
vb@62
   187
vb@62
   188
        gpg.gpgme_data_seek
vb@62
   189
            = (gpgme_data_seek_t) (intptr_t) dlsym(gpgme,
vb@62
   190
            "gpgme_data_seek");
vb@62
   191
        assert(gpg.gpgme_data_seek);
vb@62
   192
vb@62
   193
        gpg.gpgme_data_read
vb@62
   194
            = (gpgme_data_read_t) (intptr_t) dlsym(gpgme,
vb@62
   195
            "gpgme_data_read");
vb@62
   196
        assert(gpg.gpgme_data_read);
vb@62
   197
vb@62
   198
        gpg.gpgme_op_decrypt
vb@62
   199
            = (gpgme_op_decrypt_t) (intptr_t) dlsym(gpgme,
vb@62
   200
            "gpgme_op_decrypt");
vb@62
   201
        assert(gpg.gpgme_op_decrypt);
vb@62
   202
vb@62
   203
        gpg.gpgme_op_verify
vb@62
   204
            = (gpgme_op_verify_t) (intptr_t) dlsym(gpgme,
vb@62
   205
            "gpgme_op_verify");
vb@62
   206
        assert(gpg.gpgme_op_verify);
vb@62
   207
vb@62
   208
        gpg.gpgme_op_decrypt_verify
vb@62
   209
            = (gpgme_op_decrypt_verify_t) (intptr_t) dlsym(gpgme,
vb@62
   210
            "gpgme_op_decrypt_verify");
vb@62
   211
        assert(gpg.gpgme_op_decrypt_verify);
vb@62
   212
vb@62
   213
        gpg.gpgme_op_decrypt_result
vb@62
   214
            = (gpgme_op_decrypt_result_t) (intptr_t) dlsym(gpgme,
vb@62
   215
            "gpgme_op_decrypt_result");
vb@62
   216
        assert(gpg.gpgme_op_decrypt_result);
vb@62
   217
vb@62
   218
        gpg.gpgme_op_encrypt_sign
vb@62
   219
            = (gpgme_op_encrypt_sign_t) (intptr_t) dlsym(gpgme,
vb@62
   220
            "gpgme_op_encrypt_sign");
vb@62
   221
        assert(gpg.gpgme_op_encrypt_sign);
vb@62
   222
vb@62
   223
        gpg.gpgme_op_verify_result
vb@62
   224
            = (gpgme_op_verify_result_t) (intptr_t) dlsym(gpgme,
vb@62
   225
            "gpgme_op_verify_result");
vb@62
   226
        assert(gpg.gpgme_op_verify_result);
vb@62
   227
vb@62
   228
        gpg.gpgme_signers_clear
vb@62
   229
            = (gpgme_signers_clear_t) (intptr_t) dlsym(gpgme,
vb@62
   230
            "gpgme_signers_clear");
vb@62
   231
        assert(gpg.gpgme_signers_clear);
vb@62
   232
vb@62
   233
        gpg.gpgme_signers_add
vb@62
   234
            = (gpgme_signers_add_t) (intptr_t) dlsym(gpgme,
vb@62
   235
            "gpgme_signers_add");
vb@62
   236
        assert(gpg.gpgme_signers_add);
vb@62
   237
vb@62
   238
        gpg.gpgme_get_key
vb@62
   239
            = (gpgme_get_key_t) (intptr_t) dlsym(gpgme, "gpgme_get_key");
vb@62
   240
        assert(gpg.gpgme_get_key);
vb@62
   241
vb@62
   242
        gpg.gpgme_op_genkey
vb@62
   243
            = (gpgme_op_genkey_t) (intptr_t) dlsym(gpgme,
vb@62
   244
            "gpgme_op_genkey");
vb@62
   245
        assert(gpg.gpgme_op_genkey);
vb@62
   246
vb@62
   247
        gpg.gpgme_op_genkey_result
vb@62
   248
            = (gpgme_op_genkey_result_t) (intptr_t) dlsym(gpgme,
vb@62
   249
            "gpgme_op_genkey_result");
vb@62
   250
        assert(gpg.gpgme_op_genkey_result);
vb@62
   251
vb@62
   252
        gpg.gpgme_op_delete = (gpgme_op_delete_t) (intptr_t)
vb@62
   253
            dlsym(gpgme, "gpgme_op_delete");
vb@62
   254
        assert(gpg.gpgme_op_delete);
vb@62
   255
vb@62
   256
        gpg.gpgme_op_import = (gpgme_op_import_t) (intptr_t)
vb@62
   257
            dlsym(gpgme, "gpgme_op_import");
vb@62
   258
        assert(gpg.gpgme_op_import);
vb@62
   259
vb@62
   260
        gpg.gpgme_op_export = (gpgme_op_export_t) (intptr_t)
vb@62
   261
            dlsym(gpgme, "gpgme_op_export");
vb@62
   262
        assert(gpg.gpgme_op_export);
vb@62
   263
vb@62
   264
        gpg.gpgme_set_keylist_mode = (gpgme_set_keylist_mode_t) (intptr_t)
vb@62
   265
            dlsym(gpgme, "gpgme_set_keylist_mode");
vb@62
   266
        assert(gpg.gpgme_set_keylist_mode);
vb@62
   267
vb@62
   268
        gpg.gpgme_get_keylist_mode = (gpgme_get_keylist_mode_t) (intptr_t)
vb@62
   269
            dlsym(gpgme, "gpgme_get_keylist_mode");
vb@62
   270
        assert(gpg.gpgme_get_keylist_mode);
vb@62
   271
vb@62
   272
        gpg.gpgme_op_keylist_start = (gpgme_op_keylist_start_t) (intptr_t)
vb@62
   273
            dlsym(gpgme, "gpgme_op_keylist_start");
vb@62
   274
        assert(gpg.gpgme_op_keylist_start);
vb@62
   275
vb@62
   276
        gpg.gpgme_op_keylist_next = (gpgme_op_keylist_next_t) (intptr_t)
vb@62
   277
            dlsym(gpgme, "gpgme_op_keylist_next");
vb@62
   278
        assert(gpg.gpgme_op_keylist_next);
vb@62
   279
vb@62
   280
        gpg.gpgme_op_keylist_end = (gpgme_op_keylist_end_t) (intptr_t)
vb@62
   281
            dlsym(gpgme, "gpgme_op_keylist_end");
vb@62
   282
        assert(gpg.gpgme_op_keylist_end);
vb@62
   283
vb@62
   284
        gpg.gpgme_op_import_keys = (gpgme_op_import_keys_t) (intptr_t)
vb@62
   285
            dlsym(gpgme, "gpgme_op_import_keys");
vb@62
   286
        assert(gpg.gpgme_op_import_keys);
vb@62
   287
vb@62
   288
        gpg.gpgme_key_ref = (gpgme_key_ref_t) (intptr_t)
vb@62
   289
            dlsym(gpgme, "gpgme_key_ref");
vb@62
   290
        assert(gpg.gpgme_key_ref);
vb@62
   291
vb@62
   292
        gpg.gpgme_key_unref = (gpgme_key_unref_t) (intptr_t)
vb@62
   293
            dlsym(gpgme, "gpgme_key_unref");
vb@62
   294
        assert(gpg.gpgme_key_unref);
vb@62
   295
vb@221
   296
        gpg.gpgme_op_edit = (gpgme_op_edit_t) (intptr_t)
vb@221
   297
            dlsym(gpgme, "gpgme_op_edit");
vb@221
   298
        assert(gpg.gpgme_op_edit);
vb@221
   299
vb@223
   300
        gpg.gpgme_io_write = (gpgme_io_write_t) (intptr_t)
vb@223
   301
            dlsym(gpgme, "gpgme_io_write");
vb@223
   302
        assert(gpg.gpgme_io_write);
vb@223
   303
vb@62
   304
        gpg.version = gpg.gpgme_check(NULL);
vb@62
   305
        
Edouard@348
   306
        cLocal = setlocale(LC_ALL, NULL);
Edouard@348
   307
        if (!cLocal || (strcmp(cLocal, "C") == 0))
vb@62
   308
            setlocale(LC_ALL, "");
vb@62
   309
vb@62
   310
        gpg.gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
vb@62
   311
#ifdef LC_MESSAGES // Windoze
vb@62
   312
        gpg.gpgme_set_locale (NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
vb@62
   313
#endif
vb@24
   314
    }
vb@24
   315
vb@62
   316
    gpgme_error = gpg.gpgme_new(&session->ctx);
vb@45
   317
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   318
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@66
   319
        status = PEP_INIT_GPGME_INIT_FAILED;
vb@66
   320
        goto pep_error;
vb@24
   321
    }
vb@46
   322
    assert(session->ctx);
vb@24
   323
vb@62
   324
    gpgme_error = gpg.gpgme_set_protocol(session->ctx, GPGME_PROTOCOL_OpenPGP);
vb@45
   325
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   326
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   327
vb@62
   328
    gpg.gpgme_set_armor(session->ctx, 1);
vb@24
   329
vb@24
   330
    return PEP_STATUS_OK;
vb@66
   331
vb@66
   332
pep_error:
vb@66
   333
    pgp_release(session, in_first);
vb@66
   334
    return status;
vb@24
   335
}
vb@24
   336
vb@62
   337
void pgp_release(PEP_SESSION session, bool out_last)
vb@24
   338
{
vb@62
   339
    if (session->ctx) {
vb@74
   340
        gpg.gpgme_release(session->ctx);
vb@62
   341
        session->ctx = NULL;
vb@62
   342
    }
vb@62
   343
vb@74
   344
    if (out_last)
vb@74
   345
        if (gpgme)
vb@74
   346
            dlclose(gpgme);
vb@24
   347
}
vb@24
   348
vb@24
   349
PEP_STATUS pgp_decrypt_and_verify(
vb@24
   350
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
   351
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
   352
    )
vb@24
   353
{
vb@24
   354
    PEP_STATUS result;
vb@24
   355
    gpgme_error_t gpgme_error;
vb@24
   356
    gpgme_data_t cipher, plain;
vb@24
   357
    gpgme_data_type_t dt;
vb@24
   358
vb@24
   359
    stringlist_t *_keylist = NULL;
vb@24
   360
    int i_key = 0;
vb@24
   361
vb@46
   362
    assert(session);
vb@24
   363
    assert(ctext);
vb@24
   364
    assert(csize);
vb@24
   365
    assert(ptext);
vb@24
   366
    assert(psize);
vb@24
   367
    assert(keylist);
vb@24
   368
vb@24
   369
    *ptext = NULL;
vb@24
   370
    *psize = 0;
vb@24
   371
    *keylist = NULL;
vb@24
   372
vb@74
   373
    gpgme_error = gpg.gpgme_data_new_from_mem(&cipher, ctext, csize, 0);
vb@45
   374
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   375
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   376
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@24
   377
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   378
            return PEP_OUT_OF_MEMORY;
vb@24
   379
        else
vb@24
   380
            return PEP_UNKNOWN_ERROR;
vb@24
   381
    }
vb@24
   382
vb@74
   383
    gpgme_error = gpg.gpgme_data_new(&plain);
vb@45
   384
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   385
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   386
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@74
   387
        gpg.gpgme_data_release(cipher);
vb@24
   388
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   389
            return PEP_OUT_OF_MEMORY;
vb@24
   390
        else
vb@24
   391
            return PEP_UNKNOWN_ERROR;
vb@24
   392
    }
vb@24
   393
vb@74
   394
    dt = gpg.gpgme_data_identify(cipher);
vb@24
   395
    switch (dt) {
vb@24
   396
    case GPGME_DATA_TYPE_PGP_SIGNED:
vb@24
   397
    case GPGME_DATA_TYPE_PGP_OTHER:
vb@74
   398
        gpgme_error = gpg.gpgme_op_decrypt_verify(session->ctx, cipher,
vb@24
   399
            plain);
vb@45
   400
        gpgme_error = _GPGERR(gpgme_error);
vb@24
   401
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
   402
        assert(gpgme_error != GPG_ERR_NO_DATA);
vb@24
   403
vb@24
   404
        switch (gpgme_error) {
vb@344
   405
            case GPG_ERR_NO_ERROR:
vb@344
   406
            {
vb@344
   407
                gpgme_verify_result_t gpgme_verify_result;
vb@344
   408
                char *_buffer = NULL;
vb@344
   409
                size_t reading;
vb@344
   410
                size_t length = gpg.gpgme_data_seek(plain, 0, SEEK_END);
vb@344
   411
                gpgme_signature_t gpgme_signature;
vb@24
   412
vb@344
   413
                assert(length != -1);
vb@344
   414
                gpg.gpgme_data_seek(plain, 0, SEEK_SET);
vb@24
   415
vb@344
   416
                // TODO: make things less memory consuming
vb@344
   417
                // the following algorithm allocates memory for the complete
vb@344
   418
                // text
vb@24
   419
vb@344
   420
                _buffer = malloc(length + 1);
vb@344
   421
                assert(_buffer);
vb@344
   422
                if (_buffer == NULL) {
vb@74
   423
                    gpg.gpgme_data_release(plain);
vb@74
   424
                    gpg.gpgme_data_release(cipher);
vb@24
   425
                    return PEP_OUT_OF_MEMORY;
vb@24
   426
                }
vb@24
   427
vb@344
   428
                reading = gpg.gpgme_data_read(plain, _buffer, length);
vb@344
   429
                assert(length == reading);
vb@24
   430
vb@344
   431
                gpgme_verify_result =
vb@344
   432
                    gpg.gpgme_op_verify_result(session->ctx);
vb@344
   433
                assert(gpgme_verify_result);
vb@344
   434
                gpgme_signature = gpgme_verify_result->signatures;
vb@24
   435
vb@344
   436
                if (gpgme_signature) {
vb@344
   437
                    stringlist_t *k;
vb@344
   438
                    _keylist = new_stringlist(NULL);
vb@24
   439
                    assert(_keylist);
vb@24
   440
                    if (_keylist == NULL) {
vb@344
   441
                        gpg.gpgme_data_release(plain);
vb@344
   442
                        gpg.gpgme_data_release(cipher);
vb@344
   443
                        free(_buffer);
vb@344
   444
                        return PEP_OUT_OF_MEMORY;
vb@344
   445
                    }
vb@344
   446
                    k = _keylist;
vb@344
   447
vb@344
   448
                    result = PEP_DECRYPTED_AND_VERIFIED;
vb@344
   449
                    do {
vb@344
   450
                        switch (_GPGERR(gpgme_signature->status)) {
vb@344
   451
                        case GPG_ERR_NO_ERROR:
Edouard@505
   452
                        {
Edouard@674
   453
                            // Some versions of gpg returns signer's 
Edouard@674
   454
                            // signing subkey fingerprint instead of
Edouard@674
   455
                            // signer's primary key fingerprint.
Edouard@674
   456
                            // This is meant to get signer's primary 
Edouard@674
   457
                            // key fingerprint, using subkey's.
Edouard@674
   458
Edouard@505
   459
                            gpgme_key_t key;
Edouard@505
   460
                            memset(&key,0,sizeof(key));
Edouard@505
   461
Edouard@505
   462
                            gpgme_error = gpg.gpgme_get_key(session->ctx,
Edouard@505
   463
                                gpgme_signature->fpr, &key, 0);
Edouard@505
   464
                            gpgme_error = _GPGERR(gpgme_error);
Edouard@505
   465
                            assert(gpgme_error != GPG_ERR_ENOMEM);
Edouard@505
   466
                            if (gpgme_error == GPG_ERR_ENOMEM) {
Edouard@505
   467
                                free_stringlist(_keylist);
Edouard@505
   468
                                gpg.gpgme_data_release(plain);
Edouard@505
   469
                                gpg.gpgme_data_release(cipher);
Edouard@505
   470
                                free(_buffer);
Edouard@505
   471
                                return PEP_OUT_OF_MEMORY;
Edouard@505
   472
                            }
Edouard@505
   473
                            // Primary key is given as the first subkey
Edouard@674
   474
                            if (gpgme_error == GPG_ERR_NO_ERROR &&  
Edouard@674
   475
                                key && key->subkeys && key->subkeys->fpr 
Edouard@674
   476
                                && key->subkeys->fpr[0])
Edouard@674
   477
                            {
Edouard@505
   478
                                k = stringlist_add(k, key->subkeys->fpr);
Edouard@674
   479
                                gpg.gpgme_key_unref(key);
Edouard@505
   480
                            }
Edouard@674
   481
                            else if(gpgme_error == GPG_ERR_NOT_OPERATIONAL)
Edouard@674
   482
                            {
Edouard@674
   483
                                // With some gpgme version, gpgme_get_key fail
Edouard@674
   484
                                // with GPG_ERR_NOT_OPERATIONAL, but in that
Edouard@674
   485
                                // case fpr is the primary one...
Edouard@674
   486
                                k = stringlist_add(k, gpgme_signature->fpr);
Edouard@674
   487
                            }
Edouard@674
   488
                            else 
Edouard@674
   489
                            {
Edouard@505
   490
                                result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
Edouard@505
   491
                                break;
Edouard@505
   492
                            }
Edouard@674
   493
                            if (k == NULL) {
Edouard@674
   494
                                free_stringlist(_keylist);
Edouard@674
   495
                                gpg.gpgme_data_release(plain);
Edouard@674
   496
                                gpg.gpgme_data_release(cipher);
Edouard@674
   497
                                free(_buffer);
Edouard@674
   498
                                return PEP_OUT_OF_MEMORY;
Edouard@674
   499
                            }
Edouard@505
   500
vb@344
   501
                            break;
Edouard@505
   502
                        }
vb@344
   503
                        case GPG_ERR_CERT_REVOKED:
vb@344
   504
                        case GPG_ERR_BAD_SIGNATURE:
vb@344
   505
                            result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
vb@344
   506
                            break;
vb@344
   507
                        case GPG_ERR_SIG_EXPIRED:
vb@344
   508
                        case GPG_ERR_KEY_EXPIRED:
vb@344
   509
                        case GPG_ERR_NO_PUBKEY:
vb@344
   510
                            k = stringlist_add(k, gpgme_signature->fpr);
Edouard@674
   511
                            if (k == NULL) {
Edouard@674
   512
                                free_stringlist(_keylist);
Edouard@674
   513
                                gpg.gpgme_data_release(plain);
Edouard@674
   514
                                gpg.gpgme_data_release(cipher);
Edouard@674
   515
                                free(_buffer);
Edouard@674
   516
                                return PEP_OUT_OF_MEMORY;
Edouard@674
   517
                            }
vb@344
   518
                            if (result == PEP_DECRYPTED_AND_VERIFIED)
vb@344
   519
                                result = PEP_DECRYPTED;
vb@344
   520
                            break;
vb@344
   521
                        case GPG_ERR_GENERAL:
vb@344
   522
                            break;
vb@344
   523
                        default:
vb@344
   524
                            if (result == PEP_DECRYPTED_AND_VERIFIED)
vb@344
   525
                                result = PEP_DECRYPTED;
vb@344
   526
                            break;
vb@344
   527
                        }
vb@344
   528
                    } while ((gpgme_signature = gpgme_signature->next));
vb@344
   529
                }
vb@344
   530
                else {
vb@344
   531
                    result = PEP_DECRYPTED;
vb@344
   532
                }
vb@344
   533
vb@344
   534
                if (result == PEP_DECRYPTED_AND_VERIFIED
vb@344
   535
                    || result == PEP_DECRYPTED) {
vb@344
   536
                    *ptext = _buffer;
vb@344
   537
                    *psize = reading;
vb@344
   538
                    (*ptext)[*psize] = 0; // safeguard for naive users
vb@344
   539
                    *keylist = _keylist;
vb@344
   540
                }
vb@344
   541
                else {
vb@344
   542
                    free_stringlist(_keylist);
vb@344
   543
                    free(_buffer);
vb@344
   544
                }
vb@344
   545
                break;
vb@344
   546
            }
vb@344
   547
            case GPG_ERR_BAD_PASSPHRASE:
vb@344
   548
                NOT_IMPLEMENTED;
vb@344
   549
            case GPG_ERR_DECRYPT_FAILED:
vb@344
   550
            default:
vb@344
   551
            {
vb@344
   552
                gpgme_decrypt_result_t gpgme_decrypt_result = gpg.gpgme_op_decrypt_result(session->ctx);
vb@344
   553
                result = PEP_DECRYPT_NO_KEY;
vb@344
   554
vb@344
   555
                if (gpgme_decrypt_result != NULL) {
vb@344
   556
                    if (gpgme_decrypt_result->unsupported_algorithm)
vb@344
   557
                        *keylist = new_stringlist(gpgme_decrypt_result->unsupported_algorithm);
vb@344
   558
                    else
vb@344
   559
                        *keylist = new_stringlist("");
vb@344
   560
                    assert(*keylist);
vb@344
   561
                    if (*keylist == NULL) {
vb@24
   562
                        result = PEP_OUT_OF_MEMORY;
vb@24
   563
                        break;
vb@24
   564
                    }
vb@344
   565
                    stringlist_t *_keylist = *keylist;
vb@344
   566
                    for (gpgme_recipient_t r = gpgme_decrypt_result->recipients; r != NULL; r = r->next) {
vb@344
   567
                        _keylist = stringlist_add(_keylist, r->keyid);
vb@344
   568
                        assert(_keylist);
vb@344
   569
                        if (_keylist == NULL) {
vb@344
   570
                            free_stringlist(*keylist);
vb@344
   571
                            *keylist = NULL;
vb@344
   572
                            result = PEP_OUT_OF_MEMORY;
vb@344
   573
                            break;
vb@344
   574
                        }
vb@344
   575
                    }
vb@344
   576
                    if (result == PEP_OUT_OF_MEMORY)
vb@344
   577
                        break;
vb@24
   578
                }
vb@24
   579
            }
vb@24
   580
        }
vb@24
   581
        break;
vb@24
   582
vb@24
   583
    default:
vb@24
   584
        result = PEP_DECRYPT_WRONG_FORMAT;
vb@24
   585
    }
vb@24
   586
vb@74
   587
    gpg.gpgme_data_release(plain);
vb@74
   588
    gpg.gpgme_data_release(cipher);
vb@24
   589
    return result;
vb@24
   590
}
vb@24
   591
vb@24
   592
PEP_STATUS pgp_verify_text(
vb@24
   593
    PEP_SESSION session, const char *text, size_t size,
vb@24
   594
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
   595
    )
vb@24
   596
{
vb@24
   597
    PEP_STATUS result;
vb@24
   598
    gpgme_error_t gpgme_error;
vb@24
   599
    gpgme_data_t d_text, d_sig;
vb@24
   600
    stringlist_t *_keylist;
vb@24
   601
vb@24
   602
    assert(session);
vb@24
   603
    assert(text);
vb@24
   604
    assert(size);
vb@24
   605
    assert(signature);
vb@24
   606
    assert(sig_size);
vb@24
   607
    assert(keylist);
vb@24
   608
vb@24
   609
    *keylist = NULL;
vb@24
   610
vb@74
   611
    gpgme_error = gpg.gpgme_data_new_from_mem(&d_text, text, size, 0);
vb@45
   612
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   613
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   614
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@24
   615
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   616
            return PEP_OUT_OF_MEMORY;
vb@24
   617
        else
vb@24
   618
            return PEP_UNKNOWN_ERROR;
vb@24
   619
    }
vb@24
   620
vb@74
   621
    gpgme_error = gpg.gpgme_data_new_from_mem(&d_sig, signature, sig_size, 0);
vb@45
   622
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   623
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   624
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@74
   625
        gpg.gpgme_data_release(d_text);
vb@24
   626
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   627
            return PEP_OUT_OF_MEMORY;
vb@24
   628
        else
vb@24
   629
            return PEP_UNKNOWN_ERROR;
vb@24
   630
    }
vb@24
   631
vb@74
   632
    gpgme_error = gpg.gpgme_op_verify(session->ctx, d_sig, d_text, NULL);
vb@45
   633
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   634
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
   635
vb@24
   636
    switch (gpgme_error) {
vb@24
   637
    case GPG_ERR_NO_ERROR:
vb@24
   638
    {
vb@24
   639
        gpgme_verify_result_t gpgme_verify_result;
vb@24
   640
        gpgme_signature_t gpgme_signature;
vb@24
   641
vb@24
   642
        gpgme_verify_result =
vb@74
   643
            gpg.gpgme_op_verify_result(session->ctx);
vb@24
   644
        assert(gpgme_verify_result);
vb@24
   645
        gpgme_signature = gpgme_verify_result->signatures;
vb@24
   646
vb@24
   647
        if (gpgme_signature) {
vb@24
   648
            stringlist_t *k;
vb@24
   649
            _keylist = new_stringlist(NULL);
vb@24
   650
            assert(_keylist);
vb@24
   651
            if (_keylist == NULL) {
vb@74
   652
                gpg.gpgme_data_release(d_text);
vb@74
   653
                gpg.gpgme_data_release(d_sig);
vb@24
   654
                return PEP_OUT_OF_MEMORY;
vb@24
   655
            }
vb@24
   656
            k = _keylist;
vb@24
   657
vb@24
   658
            result = PEP_VERIFIED;
vb@24
   659
            do {
Edouard@502
   660
                gpgme_key_t key;
Edouard@502
   661
                memset(&key,0,sizeof(key));
Edouard@502
   662
Edouard@502
   663
                // GPGME may give subkey's fpr instead of primary key's fpr. 
Edouard@502
   664
                // Therefore we ask for the primary fingerprint instead
Edouard@502
   665
                // we assume that gpgme_get_key can find key by subkey's fpr
Edouard@502
   666
                gpgme_error = gpg.gpgme_get_key(session->ctx,
Edouard@502
   667
                    gpgme_signature->fpr, &key, 0);
Edouard@502
   668
                gpgme_error = _GPGERR(gpgme_error);
Edouard@502
   669
                assert(gpgme_error != GPG_ERR_ENOMEM);
Edouard@502
   670
                if (gpgme_error == GPG_ERR_ENOMEM) {
vb@24
   671
                    free_stringlist(_keylist);
Edouard@505
   672
                    gpg.gpgme_data_release(d_text);
Edouard@505
   673
                    gpg.gpgme_data_release(d_sig);
vb@24
   674
                    return PEP_OUT_OF_MEMORY;
vb@24
   675
                }
Edouard@502
   676
                // Primary key is given as the first subkey
Edouard@674
   677
                if (gpgme_error == GPG_ERR_NO_ERROR &&  
Edouard@674
   678
                    key && key->subkeys && key->subkeys->fpr 
Edouard@674
   679
                    && key->subkeys->fpr[0])
Edouard@674
   680
                {
Edouard@504
   681
                    k = stringlist_add(k, key->subkeys->fpr);
Edouard@674
   682
                    gpg.gpgme_key_unref(key);
Edouard@674
   683
                }
Edouard@674
   684
                else if(gpgme_error == GPG_ERR_NOT_OPERATIONAL)
Edouard@674
   685
                {
Edouard@674
   686
                    // With some gpgme version, gpgme_get_key fail
Edouard@674
   687
                    // with GPG_ERR_NOT_OPERATIONAL, but in that
Edouard@674
   688
                    // case fpr is the primary one...
Edouard@674
   689
                    k = stringlist_add(k, gpgme_signature->fpr);
Edouard@502
   690
                }
Edouard@502
   691
                else {
Edouard@502
   692
                    result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
Edouard@502
   693
                    break;
Edouard@502
   694
                }
Edouard@674
   695
                if (k == NULL) {
Edouard@674
   696
                    free_stringlist(_keylist);
Edouard@674
   697
                    gpg.gpgme_data_release(d_text);
Edouard@674
   698
                    gpg.gpgme_data_release(d_sig);
Edouard@674
   699
                    return PEP_OUT_OF_MEMORY;
Edouard@674
   700
                }
Edouard@502
   701
Edouard@502
   702
                gpg.gpgme_key_unref(key);
Edouard@502
   703
vb@24
   704
                if (gpgme_signature->summary & GPGME_SIGSUM_RED) {
vb@24
   705
                    if (gpgme_signature->summary & GPGME_SIGSUM_KEY_EXPIRED
vb@24
   706
                        || gpgme_signature->summary & GPGME_SIGSUM_SIG_EXPIRED) {
vb@24
   707
                        if (result == PEP_VERIFIED
vb@24
   708
                            || result == PEP_VERIFIED_AND_TRUSTED)
vb@24
   709
                            result = PEP_UNENCRYPTED;
vb@24
   710
                    }
vb@24
   711
                    else {
vb@24
   712
                        result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
vb@24
   713
                        break;
vb@24
   714
                    }
vb@24
   715
                }
vb@24
   716
                else {
vb@24
   717
                    if (gpgme_signature->summary & GPGME_SIGSUM_VALID) {
vb@24
   718
                        if (result == PEP_VERIFIED)
vb@24
   719
                            result = PEP_VERIFIED_AND_TRUSTED;
vb@24
   720
                    }
vb@24
   721
                    if (gpgme_signature->summary & GPGME_SIGSUM_GREEN) {
vb@24
   722
                        // good
vb@24
   723
                    }
vb@24
   724
                    else if (gpgme_signature->summary & GPGME_SIGSUM_KEY_MISSING) {
vb@24
   725
                        result = PEP_VERIFY_NO_KEY;
vb@24
   726
                    }
vb@24
   727
                    else if (gpgme_signature->summary & GPGME_SIGSUM_SYS_ERROR) {
vb@24
   728
                        if (result == PEP_VERIFIED
vb@24
   729
                            || result == PEP_VERIFIED_AND_TRUSTED)
vb@24
   730
                            result = PEP_UNENCRYPTED;
vb@24
   731
                    }
vb@24
   732
                    else {
vb@24
   733
                        // do nothing
vb@24
   734
                    }
vb@24
   735
                }
vb@24
   736
            } while ((gpgme_signature = gpgme_signature->next));
vb@24
   737
            *keylist = _keylist;
vb@24
   738
        }
vb@24
   739
        else {
vb@24
   740
            result = PEP_UNENCRYPTED;
vb@24
   741
        }
vb@24
   742
        break;
vb@24
   743
    }
vb@24
   744
        break;
vb@24
   745
    case GPG_ERR_NO_DATA:
vb@24
   746
        result = PEP_DECRYPT_WRONG_FORMAT;
vb@24
   747
        break;
vb@24
   748
    case GPG_ERR_INV_VALUE:
vb@24
   749
    default:
vb@24
   750
        result = PEP_UNKNOWN_ERROR;
vb@24
   751
        break;
vb@24
   752
    }
vb@24
   753
vb@74
   754
    gpg.gpgme_data_release(d_text);
vb@74
   755
    gpg.gpgme_data_release(d_sig);
vb@24
   756
vb@24
   757
    return result;
vb@24
   758
}
vb@24
   759
vb@24
   760
PEP_STATUS pgp_encrypt_and_sign(
vb@24
   761
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
   762
    size_t psize, char **ctext, size_t *csize
vb@24
   763
    )
vb@24
   764
{
vb@24
   765
    PEP_STATUS result;
vb@24
   766
    gpgme_error_t gpgme_error;
vb@24
   767
    gpgme_data_t plain, cipher;
vb@24
   768
    gpgme_key_t *rcpt;
vb@24
   769
    gpgme_encrypt_flags_t flags;
vb@24
   770
    const stringlist_t *_keylist;
vb@24
   771
    int i, j;
vb@24
   772
vb@46
   773
    assert(session);
vb@24
   774
    assert(keylist);
vb@24
   775
    assert(ptext);
vb@24
   776
    assert(psize);
vb@24
   777
    assert(ctext);
vb@24
   778
    assert(csize);
vb@24
   779
vb@24
   780
    *ctext = NULL;
vb@24
   781
    *csize = 0;
vb@24
   782
vb@74
   783
    gpgme_error = gpg.gpgme_data_new_from_mem(&plain, ptext, psize, 0);
vb@45
   784
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   785
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   786
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@24
   787
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   788
            return PEP_OUT_OF_MEMORY;
vb@24
   789
        else
vb@24
   790
            return PEP_UNKNOWN_ERROR;
vb@24
   791
    }
vb@24
   792
vb@74
   793
    gpgme_error = gpg.gpgme_data_new(&cipher);
vb@45
   794
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   795
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   796
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@74
   797
        gpg.gpgme_data_release(plain);
vb@24
   798
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   799
            return PEP_OUT_OF_MEMORY;
vb@24
   800
        else
vb@24
   801
            return PEP_UNKNOWN_ERROR;
vb@24
   802
    }
vb@24
   803
vb@109
   804
    rcpt = calloc(stringlist_length(keylist) + 1, sizeof(gpgme_key_t));
vb@24
   805
    assert(rcpt);
vb@24
   806
    if (rcpt == NULL) {
vb@74
   807
        gpg.gpgme_data_release(plain);
vb@74
   808
        gpg.gpgme_data_release(cipher);
vb@24
   809
        return PEP_OUT_OF_MEMORY;
vb@24
   810
    }
vb@24
   811
vb@74
   812
    gpg.gpgme_signers_clear(session->ctx);
vb@24
   813
vb@24
   814
    for (_keylist = keylist, i = 0; _keylist != NULL; _keylist = _keylist->next, i++) {
vb@24
   815
        assert(_keylist->value);
vb@74
   816
        gpgme_error = gpg.gpgme_get_key(session->ctx, _keylist->value,
vb@24
   817
            &rcpt[i], 0);
vb@45
   818
        gpgme_error = _GPGERR(gpgme_error);
vb@24
   819
        assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
   820
vb@24
   821
        switch (gpgme_error) {
vb@24
   822
        case GPG_ERR_ENOMEM:
vb@24
   823
            for (j = 0; j<i; j++)
vb@74
   824
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
   825
            free(rcpt);
vb@74
   826
            gpg.gpgme_data_release(plain);
vb@74
   827
            gpg.gpgme_data_release(cipher);
vb@24
   828
            return PEP_OUT_OF_MEMORY;
vb@24
   829
        case GPG_ERR_NO_ERROR:
vb@24
   830
            if (i == 0) {
vb@74
   831
                gpgme_error_t _gpgme_error = gpg.gpgme_signers_add(session->ctx, rcpt[0]);
vb@45
   832
                _gpgme_error = _GPGERR(_gpgme_error);
vb@24
   833
                assert(_gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   834
            }
vb@24
   835
            break;
vb@24
   836
        case GPG_ERR_EOF:
vb@24
   837
            for (j = 0; j<i; j++)
vb@74
   838
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
   839
            free(rcpt);
vb@74
   840
            gpg.gpgme_data_release(plain);
vb@74
   841
            gpg.gpgme_data_release(cipher);
vb@24
   842
            return PEP_KEY_NOT_FOUND;
vb@24
   843
        case GPG_ERR_AMBIGUOUS_NAME:
vb@24
   844
            for (j = 0; j<i; j++)
vb@74
   845
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
   846
            free(rcpt);
vb@74
   847
            gpg.gpgme_data_release(plain);
vb@74
   848
            gpg.gpgme_data_release(cipher);
vb@24
   849
            return PEP_KEY_HAS_AMBIG_NAME;
vb@24
   850
        default: // GPG_ERR_INV_VALUE if CTX or R_KEY is not a valid pointer or
vb@24
   851
            // FPR is not a fingerprint or key ID
vb@24
   852
            for (j = 0; j<i; j++)
vb@74
   853
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
   854
            free(rcpt);
vb@74
   855
            gpg.gpgme_data_release(plain);
vb@74
   856
            gpg.gpgme_data_release(cipher);
vb@24
   857
            return PEP_GET_KEY_FAILED;
vb@24
   858
        }
vb@24
   859
    }
vb@24
   860
vb@24
   861
    // TODO: remove that and replace with proper key management
vb@24
   862
    flags = GPGME_ENCRYPT_ALWAYS_TRUST;
vb@24
   863
vb@74
   864
    gpgme_error = gpg.gpgme_op_encrypt_sign(session->ctx, rcpt, flags,
vb@24
   865
        plain, cipher);
vb@45
   866
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   867
    switch (gpgme_error) {
vb@24
   868
    case GPG_ERR_NO_ERROR:
vb@24
   869
    {
vb@24
   870
        char *_buffer = NULL;
vb@24
   871
        size_t reading;
vb@74
   872
        size_t length = gpg.gpgme_data_seek(cipher, 0, SEEK_END);
vb@24
   873
        assert(length != -1);
vb@74
   874
        gpg.gpgme_data_seek(cipher, 0, SEEK_SET);
vb@24
   875
vb@24
   876
        // TODO: make things less memory consuming
vb@24
   877
        // the following algorithm allocates a buffer for the complete text
vb@24
   878
vb@112
   879
        _buffer = malloc(length + 1);
vb@24
   880
        assert(_buffer);
vb@24
   881
        if (_buffer == NULL) {
vb@24
   882
            for (j = 0; j<stringlist_length(keylist); j++)
vb@74
   883
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
   884
            free(rcpt);
vb@74
   885
            gpg.gpgme_data_release(plain);
vb@74
   886
            gpg.gpgme_data_release(cipher);
vb@24
   887
            return PEP_OUT_OF_MEMORY;
vb@24
   888
        }
vb@24
   889
vb@74
   890
        reading = gpg.gpgme_data_read(cipher, _buffer, length);
vb@24
   891
        assert(length == reading);
vb@24
   892
vb@24
   893
        *ctext = _buffer;
vb@24
   894
        *csize = reading;
vb@24
   895
        (*ctext)[*csize] = 0; // safeguard for naive users
vb@24
   896
        result = PEP_STATUS_OK;
vb@24
   897
        break;
vb@24
   898
    }
vb@24
   899
    default:
vb@24
   900
        result = PEP_UNKNOWN_ERROR;
vb@24
   901
    }
vb@24
   902
vb@24
   903
    for (j = 0; j<stringlist_length(keylist); j++)
vb@74
   904
        gpg.gpgme_key_unref(rcpt[j]);
vb@24
   905
    free(rcpt);
vb@74
   906
    gpg.gpgme_data_release(plain);
vb@74
   907
    gpg.gpgme_data_release(cipher);
vb@24
   908
    return result;
vb@24
   909
}
vb@24
   910
vb@24
   911
PEP_STATUS pgp_generate_keypair(
vb@24
   912
    PEP_SESSION session, pEp_identity *identity
vb@24
   913
    )
vb@24
   914
{
vb@24
   915
    gpgme_error_t gpgme_error;
vb@24
   916
    char *parms;
vb@24
   917
    const char *template =
vb@24
   918
        "<GnupgKeyParms format=\"internal\">\n"
vb@24
   919
        "Key-Type: RSA\n"
vb@24
   920
        "Key-Length: 4096\n"
vb@429
   921
        "Subkey-Type: RSA\n"
vb@429
   922
        "Subkey-Length: 4096\n"
vb@24
   923
        "Name-Real: %s\n"
vb@24
   924
        "Name-Email: %s\n"
vb@24
   925
        /* "Passphrase: %s\n" */
vb@24
   926
        "Expire-Date: 1y\n"
vb@24
   927
        "</GnupgKeyParms>\n";
vb@24
   928
    int result;
vb@24
   929
    gpgme_genkey_result_t gpgme_genkey_result;
vb@24
   930
vb@24
   931
    assert(session);
vb@24
   932
    assert(identity);
vb@24
   933
    assert(identity->address);
vb@298
   934
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@24
   935
    assert(identity->username);
vb@24
   936
vb@24
   937
    parms = calloc(1, PARMS_MAX);
vb@24
   938
    assert(parms);
vb@24
   939
    if (parms == NULL)
vb@24
   940
        return PEP_OUT_OF_MEMORY;
vb@24
   941
vb@24
   942
    result = snprintf(parms, PARMS_MAX, template, identity->username,
vb@46
   943
        identity->address); // , session->passphrase);
vb@24
   944
    assert(result < PARMS_MAX);
vb@24
   945
    if (result >= PARMS_MAX) {
vb@24
   946
        free(parms);
vb@24
   947
        return PEP_BUFFER_TOO_SMALL;
vb@24
   948
    }
vb@24
   949
vb@74
   950
    gpgme_error = gpg.gpgme_op_genkey(session->ctx, parms, NULL, NULL);
vb@45
   951
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   952
    free(parms);
vb@24
   953
vb@24
   954
    switch (gpgme_error) {
vb@24
   955
    case GPG_ERR_NO_ERROR:
vb@24
   956
        break;
vb@24
   957
    case GPG_ERR_INV_VALUE:
vb@24
   958
        return PEP_ILLEGAL_VALUE;
vb@24
   959
    case GPG_ERR_GENERAL:
vb@24
   960
        return PEP_CANNOT_CREATE_KEY;
vb@24
   961
    default:
vb@24
   962
        assert(0);
vb@24
   963
        return PEP_UNKNOWN_ERROR;
vb@24
   964
    }
vb@24
   965
vb@74
   966
    gpgme_genkey_result = gpg.gpgme_op_genkey_result(session->ctx);
vb@24
   967
    assert(gpgme_genkey_result);
vb@24
   968
    assert(gpgme_genkey_result->fpr);
vb@24
   969
vb@298
   970
    free(identity->fpr);
vb@24
   971
    identity->fpr = strdup(gpgme_genkey_result->fpr);
vb@298
   972
    if (identity->fpr == NULL)
vb@298
   973
        return PEP_OUT_OF_MEMORY;
vb@24
   974
vb@24
   975
    return PEP_STATUS_OK;
vb@24
   976
}
vb@24
   977
vb@24
   978
PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
   979
{
vb@24
   980
    gpgme_error_t gpgme_error;
vb@24
   981
    gpgme_key_t key;
vb@24
   982
vb@24
   983
    assert(session);
vb@24
   984
    assert(fpr);
vb@24
   985
vb@74
   986
    gpgme_error = gpg.gpgme_get_key(session->ctx, fpr, &key, 0);
vb@45
   987
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   988
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
   989
    switch (gpgme_error) {
vb@24
   990
    case GPG_ERR_NO_ERROR:
vb@24
   991
        break;
vb@24
   992
    case GPG_ERR_EOF:
vb@24
   993
        return PEP_KEY_NOT_FOUND;
vb@24
   994
    case GPG_ERR_INV_VALUE:
vb@24
   995
        return PEP_ILLEGAL_VALUE;
vb@24
   996
    case GPG_ERR_AMBIGUOUS_NAME:
vb@24
   997
        return PEP_KEY_HAS_AMBIG_NAME;
vb@24
   998
    case GPG_ERR_ENOMEM:
vb@24
   999
        return PEP_OUT_OF_MEMORY;
vb@24
  1000
    default:
vb@24
  1001
        assert(0);
vb@24
  1002
        return PEP_UNKNOWN_ERROR;
vb@24
  1003
    }
vb@24
  1004
vb@74
  1005
    gpgme_error = gpg.gpgme_op_delete(session->ctx, key, 1);
vb@45
  1006
    gpgme_error = _GPGERR(gpgme_error);
vb@74
  1007
    gpg.gpgme_key_unref(key);
vb@24
  1008
    switch (gpgme_error) {
vb@24
  1009
    case GPG_ERR_NO_ERROR:
vb@24
  1010
        break;
vb@24
  1011
    case GPG_ERR_INV_VALUE:
vb@24
  1012
        assert(0);
vb@24
  1013
        return PEP_UNKNOWN_ERROR;
vb@24
  1014
    case GPG_ERR_NO_PUBKEY:
vb@24
  1015
        assert(0);
vb@24
  1016
        return PEP_KEY_NOT_FOUND;
vb@24
  1017
    case GPG_ERR_AMBIGUOUS_NAME:
vb@24
  1018
        assert(0);
vb@24
  1019
        return PEP_KEY_HAS_AMBIG_NAME;
vb@24
  1020
    default:
vb@24
  1021
        assert(0);
vb@24
  1022
        return PEP_UNKNOWN_ERROR;
vb@24
  1023
    }
vb@24
  1024
vb@24
  1025
    return PEP_STATUS_OK;
vb@24
  1026
}
vb@24
  1027
Edouard@170
  1028
PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data, size_t size)
vb@24
  1029
{
vb@24
  1030
    gpgme_error_t gpgme_error;
vb@24
  1031
    gpgme_data_t dh;
vb@24
  1032
vb@24
  1033
    assert(session);
vb@24
  1034
    assert(key_data);
vb@24
  1035
vb@74
  1036
    gpgme_error = gpg.gpgme_data_new_from_mem(&dh, key_data, size, 0);
vb@45
  1037
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1038
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1039
    switch (gpgme_error) {
vb@24
  1040
    case GPG_ERR_NO_ERROR:
vb@24
  1041
        break;
vb@24
  1042
    case GPG_ERR_ENOMEM:
vb@24
  1043
        return PEP_OUT_OF_MEMORY;
vb@24
  1044
    case GPG_ERR_INV_VALUE:
vb@24
  1045
        assert(0);
vb@24
  1046
        return PEP_UNKNOWN_ERROR;
vb@24
  1047
    default:
vb@24
  1048
        assert(0);
vb@24
  1049
        return PEP_UNKNOWN_ERROR;
vb@24
  1050
    }
vb@24
  1051
vb@74
  1052
    gpgme_error = gpg.gpgme_op_import(session->ctx, dh);
vb@45
  1053
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1054
    switch (gpgme_error) {
vb@24
  1055
    case GPG_ERR_NO_ERROR:
vb@24
  1056
        break;
vb@24
  1057
    case GPG_ERR_INV_VALUE:
vb@24
  1058
        assert(0);
vb@74
  1059
        gpg.gpgme_data_release(dh);
vb@24
  1060
        return PEP_UNKNOWN_ERROR;
vb@24
  1061
    case GPG_ERR_NO_DATA:
vb@74
  1062
        gpg.gpgme_data_release(dh);
vb@24
  1063
        return PEP_ILLEGAL_VALUE;
vb@24
  1064
    default:
vb@24
  1065
        assert(0);
vb@74
  1066
        gpg.gpgme_data_release(dh);
vb@24
  1067
        return PEP_UNKNOWN_ERROR;
vb@24
  1068
    }
vb@24
  1069
vb@74
  1070
    gpg.gpgme_data_release(dh);
vb@24
  1071
    return PEP_STATUS_OK;
vb@24
  1072
}
vb@24
  1073
Edouard@170
  1074
PEP_STATUS pgp_export_keydata(
vb@24
  1075
    PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
  1076
    )
vb@24
  1077
{
vb@24
  1078
    gpgme_error_t gpgme_error;
vb@24
  1079
    gpgme_data_t dh;
vb@24
  1080
    size_t _size;
vb@24
  1081
    char *buffer;
vb@24
  1082
    int reading;
vb@24
  1083
vb@24
  1084
    assert(session);
vb@24
  1085
    assert(fpr);
vb@24
  1086
    assert(key_data);
vb@24
  1087
    assert(size);
vb@24
  1088
vb@74
  1089
    gpgme_error = gpg.gpgme_data_new(&dh);
vb@45
  1090
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1091
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1092
    switch (gpgme_error) {
vb@24
  1093
    case GPG_ERR_NO_ERROR:
vb@24
  1094
        break;
vb@24
  1095
    case GPG_ERR_ENOMEM:
vb@24
  1096
        return PEP_OUT_OF_MEMORY;
vb@24
  1097
    case GPG_ERR_INV_VALUE:
vb@24
  1098
        assert(0);
vb@24
  1099
        return PEP_UNKNOWN_ERROR;
vb@24
  1100
    default:
vb@24
  1101
        assert(0);
vb@24
  1102
        return PEP_UNKNOWN_ERROR;
vb@24
  1103
    }
vb@24
  1104
vb@74
  1105
    gpgme_error = gpg.gpgme_op_export(session->ctx, fpr,
vb@24
  1106
        GPGME_EXPORT_MODE_MINIMAL, dh);
vb@45
  1107
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1108
    switch (gpgme_error) {
vb@24
  1109
    case GPG_ERR_NO_ERROR:
vb@24
  1110
        break;
vb@24
  1111
    case GPG_ERR_EOF:
vb@74
  1112
        gpg.gpgme_data_release(dh);
vb@24
  1113
        return PEP_KEY_NOT_FOUND;
vb@24
  1114
    case GPG_ERR_INV_VALUE:
vb@24
  1115
        assert(0);
vb@74
  1116
        gpg.gpgme_data_release(dh);
vb@24
  1117
        return PEP_UNKNOWN_ERROR;
vb@24
  1118
    default:
vb@24
  1119
        assert(0);
vb@74
  1120
        gpg.gpgme_data_release(dh);
vb@24
  1121
        return PEP_UNKNOWN_ERROR;
vb@24
  1122
    };
vb@24
  1123
vb@74
  1124
    _size = gpg.gpgme_data_seek(dh, 0, SEEK_END);
vb@24
  1125
    assert(_size != -1);
vb@74
  1126
    gpg.gpgme_data_seek(dh, 0, SEEK_SET);
vb@24
  1127
vb@24
  1128
    buffer = malloc(_size + 1);
vb@24
  1129
    assert(buffer);
vb@24
  1130
    if (buffer == NULL) {
vb@74
  1131
        gpg.gpgme_data_release(dh);
vb@24
  1132
        return PEP_OUT_OF_MEMORY;
vb@24
  1133
    }
vb@24
  1134
vb@74
  1135
    reading = gpg.gpgme_data_read(dh, buffer, _size);
vb@24
  1136
    assert(_size == reading);
vb@24
  1137
vb@24
  1138
    // safeguard for the naive user
vb@24
  1139
    buffer[_size] = 0;
vb@24
  1140
vb@24
  1141
    *key_data = buffer;
vb@24
  1142
    *size = _size;
vb@24
  1143
vb@74
  1144
    gpg.gpgme_data_release(dh);
vb@24
  1145
    return PEP_STATUS_OK;
vb@24
  1146
}
vb@24
  1147
vb@46
  1148
static void _switch_mode(pEpSession *session, gpgme_keylist_mode_t remove_mode,
vb@24
  1149
    gpgme_keylist_mode_t add_mode)
vb@24
  1150
{
vb@24
  1151
    gpgme_error_t gpgme_error;
vb@24
  1152
    gpgme_keylist_mode_t mode;
vb@24
  1153
vb@74
  1154
    mode = gpg.gpgme_get_keylist_mode(session->ctx);
vb@24
  1155
vb@24
  1156
    mode &= ~remove_mode;
vb@24
  1157
    mode |= add_mode;
vb@24
  1158
vb@74
  1159
    gpgme_error = gpg.gpgme_set_keylist_mode(session->ctx, mode);
vb@45
  1160
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1161
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1162
}
vb@24
  1163
vb@24
  1164
PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1165
{
vb@24
  1166
    gpgme_error_t gpgme_error;
vb@24
  1167
    gpgme_key_t key;
vb@24
  1168
vb@24
  1169
    assert(session);
vb@24
  1170
    assert(pattern);
vb@24
  1171
vb@46
  1172
    _switch_mode(session, GPGME_KEYLIST_MODE_LOCAL, GPGME_KEYLIST_MODE_EXTERN);
vb@24
  1173
vb@74
  1174
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
vb@45
  1175
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1176
    switch (gpgme_error) {
vb@24
  1177
    case GPG_ERR_NO_ERROR:
vb@24
  1178
        break;
vb@24
  1179
    case GPG_ERR_INV_VALUE:
vb@24
  1180
        assert(0);
vb@47
  1181
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1182
        return PEP_UNKNOWN_ERROR;
vb@24
  1183
    default:
vb@47
  1184
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1185
        return PEP_GET_KEY_FAILED;
vb@24
  1186
    };
vb@24
  1187
vb@47
  1188
    gpgme_ctx_t import_ctx;
vb@74
  1189
    gpgme_error = gpg.gpgme_new(&import_ctx);
vb@47
  1190
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@47
  1191
vb@24
  1192
    do {
vb@74
  1193
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1194
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1195
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1196
        switch (gpgme_error) {
vb@24
  1197
        case GPG_ERR_EOF:
vb@24
  1198
            break;
vb@24
  1199
        case GPG_ERR_NO_ERROR:
vb@24
  1200
        {
vb@24
  1201
            gpgme_error_t gpgme_error;
vb@24
  1202
            gpgme_key_t keys[2];
vb@24
  1203
vb@24
  1204
            keys[0] = key;
vb@24
  1205
            keys[1] = NULL;
vb@24
  1206
vb@74
  1207
            gpgme_error = gpg.gpgme_op_import_keys(import_ctx, keys);
vb@45
  1208
            gpgme_error = _GPGERR(gpgme_error);
vb@74
  1209
            gpg.gpgme_key_unref(key);
vb@24
  1210
            assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1211
            assert(gpgme_error != GPG_ERR_CONFLICT);
vb@24
  1212
        }
vb@24
  1213
            break;
vb@24
  1214
        case GPG_ERR_ENOMEM:
vb@74
  1215
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1216
            gpg.gpgme_release(import_ctx);
vb@47
  1217
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1218
            return PEP_OUT_OF_MEMORY;
vb@24
  1219
        default:
vb@74
  1220
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1221
            gpg.gpgme_release(import_ctx);
vb@47
  1222
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@46
  1223
            return PEP_UNKNOWN_ERROR;
vb@24
  1224
        };
vb@24
  1225
    } while (gpgme_error != GPG_ERR_EOF);
vb@24
  1226
vb@74
  1227
    gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1228
    gpg.gpgme_release(import_ctx);
vb@47
  1229
    _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1230
    return PEP_STATUS_OK;
vb@24
  1231
}
vb@24
  1232
vb@24
  1233
PEP_STATUS pgp_find_keys(
vb@24
  1234
    PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
  1235
    )
vb@24
  1236
{
vb@24
  1237
    gpgme_error_t gpgme_error;
vb@24
  1238
    gpgme_key_t key;
vb@24
  1239
    stringlist_t *_keylist;
vb@24
  1240
    char *fpr;
vb@24
  1241
vb@24
  1242
    assert(session);
vb@24
  1243
    assert(pattern);
vb@24
  1244
    assert(keylist);
vb@24
  1245
vb@24
  1246
    *keylist = NULL;
vb@24
  1247
vb@74
  1248
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
vb@45
  1249
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1250
    switch (gpgme_error) {
vb@24
  1251
    case GPG_ERR_NO_ERROR:
vb@24
  1252
        break;
vb@24
  1253
    case GPG_ERR_INV_VALUE:
vb@24
  1254
        assert(0);
vb@24
  1255
        return PEP_UNKNOWN_ERROR;
vb@24
  1256
    default:
vb@74
  1257
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1258
        return PEP_GET_KEY_FAILED;
vb@24
  1259
    };
vb@24
  1260
vb@24
  1261
    _keylist = new_stringlist(NULL);
vb@24
  1262
    stringlist_t *_k = _keylist;
vb@24
  1263
vb@24
  1264
    do {
vb@74
  1265
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1266
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1267
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1268
        switch (gpgme_error) {
vb@24
  1269
        case GPG_ERR_EOF:
vb@24
  1270
            break;
vb@24
  1271
        case GPG_ERR_NO_ERROR:
vb@24
  1272
            assert(key);
vb@24
  1273
            assert(key->subkeys);
vb@24
  1274
            fpr = key->subkeys->fpr;
vb@24
  1275
            assert(fpr);
vb@24
  1276
            _k = stringlist_add(_k, fpr);
vb@24
  1277
            assert(_k);
vb@24
  1278
            if (_k != NULL)
vb@24
  1279
                break;
vb@24
  1280
        case GPG_ERR_ENOMEM:
vb@24
  1281
            free_stringlist(_keylist);
vb@74
  1282
            gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1283
            return PEP_OUT_OF_MEMORY;
vb@24
  1284
        default:
vb@74
  1285
            gpg.gpgme_op_keylist_end(session->ctx);
vb@46
  1286
            return PEP_UNKNOWN_ERROR;
vb@24
  1287
        };
vb@24
  1288
    } while (gpgme_error != GPG_ERR_EOF);
vb@24
  1289
vb@74
  1290
    gpg.gpgme_op_keylist_end(session->ctx);
vb@523
  1291
    if (_keylist->value == NULL) {
vb@523
  1292
        free_stringlist(_keylist);
vb@523
  1293
        _keylist = NULL;
vb@523
  1294
    }
vb@24
  1295
    *keylist = _keylist;
vb@24
  1296
    return PEP_STATUS_OK;
vb@24
  1297
}
vb@24
  1298
vb@24
  1299
PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)
vb@24
  1300
{
vb@24
  1301
    gpgme_error_t gpgme_error;
vb@24
  1302
vb@24
  1303
    assert(session);
vb@24
  1304
    assert(pattern);
vb@24
  1305
vb@74
  1306
    gpgme_error = gpg.gpgme_op_export(session->ctx, pattern,
vb@24
  1307
        GPGME_EXPORT_MODE_EXTERN, NULL);
vb@45
  1308
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1309
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1310
    if (gpgme_error == GPG_ERR_NO_ERROR)
vb@24
  1311
        return PEP_STATUS_OK;
vb@24
  1312
    else
vb@24
  1313
        return PEP_CANNOT_SEND_KEY;
vb@24
  1314
}
vb@24
  1315
vb@24
  1316
PEP_STATUS pgp_get_key_rating(
vb@24
  1317
    PEP_SESSION session,
vb@24
  1318
    const char *fpr,
vb@24
  1319
    PEP_comm_type *comm_type
vb@24
  1320
    )
vb@24
  1321
{
vb@24
  1322
    PEP_STATUS status = PEP_STATUS_OK;
vb@24
  1323
    gpgme_error_t gpgme_error;
vb@24
  1324
    gpgme_key_t key;
vb@24
  1325
vb@24
  1326
    assert(session);
vb@24
  1327
    assert(fpr);
vb@24
  1328
    assert(comm_type);
vb@24
  1329
vb@24
  1330
    *comm_type = PEP_ct_unknown;
vb@24
  1331
vb@74
  1332
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
vb@45
  1333
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1334
    switch (gpgme_error) {
vb@24
  1335
    case GPG_ERR_NO_ERROR:
vb@24
  1336
        break;
vb@24
  1337
    case GPG_ERR_INV_VALUE:
vb@24
  1338
        assert(0);
vb@24
  1339
        return PEP_UNKNOWN_ERROR;
vb@24
  1340
    default:
vb@24
  1341
        return PEP_GET_KEY_FAILED;
vb@24
  1342
    };
vb@24
  1343
vb@74
  1344
    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1345
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1346
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1347
vb@24
  1348
    if (key == NULL) {
vb@74
  1349
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1350
        return PEP_KEY_NOT_FOUND;
vb@24
  1351
    }
vb@24
  1352
vb@24
  1353
    switch (key->protocol) {
vb@24
  1354
    case GPGME_PROTOCOL_OpenPGP:
vb@24
  1355
    case GPGME_PROTOCOL_DEFAULT:
vb@24
  1356
        *comm_type = PEP_ct_OpenPGP_unconfirmed;
vb@24
  1357
        break;
vb@24
  1358
    case GPGME_PROTOCOL_CMS:
vb@24
  1359
        *comm_type = PEP_ct_CMS_unconfirmed;
vb@24
  1360
        break;
vb@24
  1361
    default:
vb@24
  1362
        *comm_type = PEP_ct_unknown;
vb@74
  1363
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1364
        return PEP_STATUS_OK;
vb@24
  1365
    }
vb@24
  1366
vb@24
  1367
    switch (gpgme_error) {
vb@24
  1368
    case GPG_ERR_EOF:
vb@24
  1369
        break;
vb@24
  1370
    case GPG_ERR_NO_ERROR:
vb@24
  1371
        assert(key);
vb@24
  1372
        assert(key->subkeys);
vb@24
  1373
        for (gpgme_subkey_t sk = key->subkeys; sk != NULL; sk = sk->next) {
vb@24
  1374
            if (sk->length < 1024)
vb@24
  1375
                *comm_type = PEP_ct_key_too_short;
vb@24
  1376
            else if (
vb@24
  1377
                (
vb@24
  1378
                (sk->pubkey_algo == GPGME_PK_RSA)
vb@24
  1379
                || (sk->pubkey_algo == GPGME_PK_RSA_E)
vb@24
  1380
                || (sk->pubkey_algo == GPGME_PK_RSA_S)
vb@24
  1381
                )
vb@24
  1382
                && sk->length == 1024
vb@24
  1383
                )
vb@122
  1384
                *comm_type = PEP_ct_OpenPGP_weak_unconfirmed;
vb@24
  1385
vb@24
  1386
            if (sk->invalid) {
vb@24
  1387
                *comm_type = PEP_ct_key_b0rken;
vb@24
  1388
                break;
vb@24
  1389
            }
vb@24
  1390
            if (sk->expired) {
vb@24
  1391
                *comm_type = PEP_ct_key_expired;
vb@24
  1392
                break;
vb@24
  1393
            }
vb@24
  1394
            if (sk->revoked) {
vb@24
  1395
                *comm_type = PEP_ct_key_revoked;
vb@24
  1396
                break;
vb@24
  1397
            }
vb@24
  1398
        }
vb@24
  1399
        break;
vb@24
  1400
    case GPG_ERR_ENOMEM:
vb@74
  1401
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1402
        *comm_type = PEP_ct_unknown;
vb@24
  1403
        return PEP_OUT_OF_MEMORY;
vb@24
  1404
    default:
vb@74
  1405
        gpg.gpgme_op_keylist_end(session->ctx);
vb@46
  1406
        return PEP_UNKNOWN_ERROR;
vb@24
  1407
    };
vb@24
  1408
vb@74
  1409
    gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1410
vb@24
  1411
    return status;
vb@24
  1412
}
vb@200
  1413
vb@200
  1414
static PEP_STATUS find_single_key(
vb@200
  1415
        PEP_SESSION session,
vb@200
  1416
        const char *fpr,
vb@200
  1417
        gpgme_key_t *key
vb@200
  1418
    )
vb@200
  1419
{
vb@200
  1420
    gpgme_error_t gpgme_error;
vb@200
  1421
vb@200
  1422
    *key = NULL;
vb@200
  1423
vb@200
  1424
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
vb@200
  1425
    gpgme_error = _GPGERR(gpgme_error);
vb@200
  1426
    switch (gpgme_error) {
vb@200
  1427
    case GPG_ERR_NO_ERROR:
vb@200
  1428
        break;
vb@200
  1429
    case GPG_ERR_INV_VALUE:
vb@200
  1430
        assert(0);
vb@200
  1431
        return PEP_UNKNOWN_ERROR;
vb@200
  1432
    default:
vb@200
  1433
        return PEP_GET_KEY_FAILED;
vb@200
  1434
    };
vb@200
  1435
vb@200
  1436
    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, key);
vb@200
  1437
    gpgme_error = _GPGERR(gpgme_error);
vb@200
  1438
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@200
  1439
vb@200
  1440
    gpg.gpgme_op_keylist_end(session->ctx);
vb@200
  1441
vb@200
  1442
    return PEP_STATUS_OK;
vb@200
  1443
}
vb@200
  1444
vb@201
  1445
typedef struct _renew_state {
vb@211
  1446
    enum {
vb@200
  1447
        renew_command = 0,
vb@200
  1448
        renew_date,
vb@200
  1449
        renew_secret_key,
vb@200
  1450
        renew_command2,
vb@200
  1451
        renew_date2,
vb@200
  1452
        renew_quit,
vb@200
  1453
        renew_save,
vb@200
  1454
        renew_exit,
vb@200
  1455
        renew_error = -1
vb@200
  1456
    } state;
vb@201
  1457
    const char *date_ref;
vb@201
  1458
} renew_state;
vb@200
  1459
vb@201
  1460
static gpgme_error_t renew_fsm(
vb@200
  1461
        void *_handle,
vb@200
  1462
        gpgme_status_code_t statuscode,
vb@200
  1463
        const char *args,
vb@200
  1464
        int fd
vb@200
  1465
    )
vb@200
  1466
{
vb@201
  1467
    renew_state *handle = _handle;
vb@200
  1468
vb@200
  1469
    switch (handle->state) {
vb@200
  1470
        case renew_command:
vb@200
  1471
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1472
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1473
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1474
                    handle->state = renew_error;
vb@201
  1475
                    return GPG_ERR_GENERAL;
vb@201
  1476
                }
vb@223
  1477
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  1478
                handle->state = renew_date;
vb@200
  1479
            }
vb@200
  1480
            break;
vb@200
  1481
vb@200
  1482
        case renew_date:
vb@200
  1483
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1484
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  1485
                if (strcmp(args, "keygen.valid")) {
vb@201
  1486
                    handle->state = renew_error;
vb@201
  1487
                    return GPG_ERR_GENERAL;
vb@201
  1488
                }
vb@223
  1489
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  1490
                handle->state = renew_secret_key;
vb@200
  1491
            }
vb@200
  1492
            break;
vb@200
  1493
vb@200
  1494
        case renew_secret_key:
vb@200
  1495
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1496
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1497
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1498
                    handle->state = renew_error;
vb@201
  1499
                    return GPG_ERR_GENERAL;
vb@201
  1500
                }
vb@223
  1501
                gpg.gpgme_io_write(fd, "key 1\n", 6);
vb@200
  1502
                handle->state = renew_command2;
vb@200
  1503
            }
vb@200
  1504
            break;
vb@200
  1505
vb@200
  1506
        case renew_command2:
vb@200
  1507
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1508
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1509
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1510
                    handle->state = renew_error;
vb@201
  1511
                    return GPG_ERR_GENERAL;
vb@201
  1512
                }
vb@223
  1513
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  1514
                handle->state = renew_date2;
vb@200
  1515
            }
vb@200
  1516
            break;
vb@200
  1517
vb@200
  1518
        case renew_date2:
vb@200
  1519
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1520
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  1521
                if (strcmp(args, "keygen.valid")) {
vb@201
  1522
                    handle->state = renew_error;
vb@201
  1523
                    return GPG_ERR_GENERAL;
vb@201
  1524
                }
vb@223
  1525
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  1526
                handle->state = renew_quit;
vb@200
  1527
            }
vb@200
  1528
            break;
vb@200
  1529
vb@200
  1530
        case renew_quit:
vb@200
  1531
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1532
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1533
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1534
                    handle->state = renew_error;
vb@201
  1535
                    return GPG_ERR_GENERAL;
vb@201
  1536
                }
vb@223
  1537
                gpg.gpgme_io_write(fd, "quit\n", 5);
vb@200
  1538
                handle->state = renew_save;
vb@200
  1539
            }
vb@200
  1540
            break;
vb@200
  1541
vb@200
  1542
        case renew_save:
vb@200
  1543
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@201
  1544
                assert(strcmp(args, "keyedit.save.okay") == 0);
vb@201
  1545
                if (strcmp(args, "keyedit.save.okay")) {
vb@201
  1546
                    handle->state = renew_error;
vb@201
  1547
                    return GPG_ERR_GENERAL;
vb@201
  1548
                }
vb@223
  1549
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@200
  1550
                handle->state = renew_exit;
vb@200
  1551
            }
vb@200
  1552
            break;
vb@200
  1553
vb@200
  1554
        case renew_exit:
vb@200
  1555
            break;
vb@200
  1556
vb@200
  1557
        case renew_error:
vb@200
  1558
            return GPG_ERR_GENERAL;
vb@200
  1559
    }
vb@200
  1560
vb@200
  1561
    return GPG_ERR_NO_ERROR;
vb@200
  1562
}
vb@200
  1563
vb@200
  1564
static ssize_t _nullwriter(
vb@200
  1565
        void *_handle,
vb@200
  1566
        const void *buffer,
vb@200
  1567
        size_t size
vb@200
  1568
    )
vb@200
  1569
{
vb@200
  1570
    return size;
vb@200
  1571
}
vb@200
  1572
vb@201
  1573
PEP_STATUS pgp_renew_key(
vb@201
  1574
        PEP_SESSION session,
vb@201
  1575
        const char *fpr,
vb@201
  1576
        const timestamp *ts
vb@201
  1577
    )
vb@200
  1578
{
vb@200
  1579
    PEP_STATUS status = PEP_STATUS_OK;
vb@200
  1580
    gpgme_error_t gpgme_error;
vb@200
  1581
    gpgme_key_t key;
vb@200
  1582
    gpgme_data_t output;
vb@201
  1583
    renew_state handle;
vb@203
  1584
    char date_text[12];
vb@200
  1585
vb@200
  1586
    assert(session);
vb@200
  1587
    assert(fpr);
vb@200
  1588
vb@201
  1589
    memset(&handle, 0, sizeof(renew_state));
vb@203
  1590
    snprintf(date_text, 12, "%.4d-%.2d-%.2d\n", ts->tm_year + 1900,
vb@201
  1591
            ts->tm_mon + 1, ts->tm_mday);
vb@201
  1592
    handle.date_ref = date_text;
vb@200
  1593
vb@200
  1594
    status = find_single_key(session, fpr, &key);
vb@200
  1595
    if (status != PEP_STATUS_OK)
vb@200
  1596
        return status;
vb@200
  1597
vb@200
  1598
    struct gpgme_data_cbs data_cbs;
vb@200
  1599
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
vb@200
  1600
    data_cbs.write = _nullwriter;
vb@220
  1601
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
vb@200
  1602
vb@220
  1603
    gpgme_error = gpg.gpgme_op_edit(session->ctx, key, renew_fsm, &handle,
vb@200
  1604
            output);
vb@200
  1605
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@200
  1606
vb@200
  1607
    gpg.gpgme_data_release(output);
vb@200
  1608
    gpg.gpgme_key_unref(key);
vb@200
  1609
vb@200
  1610
    return PEP_STATUS_OK;
vb@200
  1611
}
vb@200
  1612
vb@211
  1613
typedef struct _revoke_state {
vb@211
  1614
    enum {
vb@211
  1615
        revoke_command = 0,
vb@211
  1616
        revoke_approve,
vb@211
  1617
        revoke_reason_code,
vb@211
  1618
        revoke_reason_text,
vb@211
  1619
        revoke_reason_ok,
vb@211
  1620
        revoke_quit,
vb@211
  1621
        revoke_save,
vb@211
  1622
        revoke_exit,
vb@211
  1623
        revoke_error = -1
vb@211
  1624
    } state;
vb@211
  1625
    const char *reason_ref;
vb@211
  1626
} revoke_state;
vb@211
  1627
vb@211
  1628
static bool isemptystring(const char *str)
vb@211
  1629
{
vb@211
  1630
    if (str == NULL)
vb@211
  1631
        return true;
vb@211
  1632
vb@211
  1633
    for (; str; str++) {
vb@211
  1634
        if (*str != ' ' && *str != '\t' && *str != '\n')
vb@211
  1635
            return false;
vb@211
  1636
    }
vb@211
  1637
vb@211
  1638
    return true;
vb@211
  1639
}
vb@211
  1640
vb@211
  1641
static gpgme_error_t revoke_fsm(
vb@211
  1642
        void *_handle,
vb@211
  1643
        gpgme_status_code_t statuscode,
vb@211
  1644
        const char *args,
vb@211
  1645
        int fd
vb@211
  1646
    )
vb@211
  1647
{
vb@211
  1648
    revoke_state *handle = _handle;
vb@211
  1649
vb@211
  1650
    switch (handle->state) {
vb@211
  1651
        case revoke_command:
vb@211
  1652
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1653
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@211
  1654
                if (strcmp(args, "keyedit.prompt")) {
vb@211
  1655
                    handle->state = revoke_error;
vb@211
  1656
                    return GPG_ERR_GENERAL;
vb@211
  1657
                }
vb@223
  1658
                gpg.gpgme_io_write(fd, "revkey\n", 7);
vb@211
  1659
                handle->state = revoke_approve;
vb@211
  1660
            }
vb@211
  1661
            break;
vb@211
  1662
vb@211
  1663
        case revoke_approve:
vb@211
  1664
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  1665
                assert(strcmp(args, "keyedit.revoke.subkey.okay") == 0);
vb@211
  1666
                if (strcmp(args, "keyedit.revoke.subkey.okay")) {
vb@211
  1667
                    handle->state = revoke_error;
vb@211
  1668
                    return GPG_ERR_GENERAL;
vb@211
  1669
                }
vb@223
  1670
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  1671
                handle->state = revoke_reason_code;
vb@211
  1672
            }
vb@211
  1673
            break;
vb@211
  1674
vb@211
  1675
        case revoke_reason_code:
vb@211
  1676
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1677
                assert(strcmp(args, "ask_revocation_reason.code") == 0);
vb@211
  1678
                if (strcmp(args, "ask_revocation_reason.code")) {
vb@211
  1679
                    handle->state = revoke_error;
vb@211
  1680
                    return GPG_ERR_GENERAL;
vb@211
  1681
                }
vb@223
  1682
                gpg.gpgme_io_write(fd, "1\n", 2);
vb@211
  1683
                handle->state = revoke_reason_text;
vb@211
  1684
            }
vb@211
  1685
            break;
vb@211
  1686
vb@211
  1687
        case revoke_reason_text:
vb@211
  1688
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1689
                assert(strcmp(args, "ask_revocation_reason.text") == 0);
vb@211
  1690
                if (strcmp(args, "ask_revocation_reason.text")) {
vb@211
  1691
                    handle->state = revoke_error;
vb@211
  1692
                    return GPG_ERR_GENERAL;
vb@211
  1693
                }
vb@213
  1694
                // BUG: issues when reason given
vb@213
  1695
                // Assertion failed: (gpg->cmd.code), function command_handler,
vb@213
  1696
                // file engine-gpg.c, line 662.
vb@213
  1697
                //
vb@213
  1698
                // if (isemptystring(handle->reason_ref)) {
vb@223
  1699
                    gpg.gpgme_io_write(fd, "\n", 1);
vb@213
  1700
                // }
vb@213
  1701
                // else {
vb@213
  1702
                //     size_t len = strlen(handle->reason_ref);
vb@223
  1703
                //     gpg.gpgme_io_write(fd, handle->reason_ref, len);
vb@213
  1704
                //     if (handle->reason_ref[len - 1] == '\n')
vb@223
  1705
                //         gpg.gpgme_io_write(fd, "\n", 1);
vb@213
  1706
                //     else
vb@223
  1707
                //         gpg.gpgme_io_write(fd, "\n\n", 2);
vb@213
  1708
                // }
vb@211
  1709
                handle->state = revoke_reason_ok;
vb@211
  1710
            }
vb@211
  1711
            break;
vb@211
  1712
vb@211
  1713
        case revoke_reason_ok:
vb@211
  1714
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  1715
                assert(strcmp(args, "ask_revocation_reason.okay") == 0);
vb@211
  1716
                if (strcmp(args, "ask_revocation_reason.okay")) {
vb@211
  1717
                    handle->state = revoke_error;
vb@211
  1718
                    return GPG_ERR_GENERAL;
vb@211
  1719
                }
vb@223
  1720
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  1721
                handle->state = revoke_quit;
vb@211
  1722
            }
vb@211
  1723
            break;
vb@211
  1724
vb@211
  1725
        case revoke_quit:
vb@211
  1726
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1727
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@211
  1728
                if (strcmp(args, "keyedit.prompt")) {
vb@211
  1729
                    handle->state = revoke_error;
vb@211
  1730
                    return GPG_ERR_GENERAL;
vb@211
  1731
                }
vb@223
  1732
                gpg.gpgme_io_write(fd, "quit\n", 5);
vb@211
  1733
                handle->state = revoke_save;
vb@211
  1734
            }
vb@211
  1735
            break;
vb@211
  1736
vb@211
  1737
        case revoke_save:
vb@211
  1738
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  1739
                assert(strcmp(args, "keyedit.save.okay") == 0);
vb@211
  1740
                if (strcmp(args, "keyedit.save.okay")) {
vb@211
  1741
                    handle->state = revoke_error;
vb@211
  1742
                    return GPG_ERR_GENERAL;
vb@211
  1743
                }
vb@223
  1744
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  1745
                handle->state = revoke_exit;
vb@211
  1746
            }
vb@211
  1747
            break;
vb@211
  1748
vb@211
  1749
        case revoke_exit:
vb@211
  1750
            break;
vb@211
  1751
vb@211
  1752
        case revoke_error:
vb@211
  1753
            return GPG_ERR_GENERAL;
vb@211
  1754
    }
vb@211
  1755
vb@211
  1756
    return GPG_ERR_NO_ERROR;
vb@211
  1757
}
vb@211
  1758
vb@211
  1759
PEP_STATUS pgp_revoke_key(
vb@211
  1760
        PEP_SESSION session,
vb@211
  1761
        const char *fpr,
vb@211
  1762
        const char *reason
vb@211
  1763
    )
vb@200
  1764
{
vb@200
  1765
    PEP_STATUS status = PEP_STATUS_OK;
vb@211
  1766
    gpgme_error_t gpgme_error;
vb@200
  1767
    gpgme_key_t key;
vb@211
  1768
    gpgme_data_t output;
vb@211
  1769
    revoke_state handle;
vb@211
  1770
vb@200
  1771
    assert(session);
vb@200
  1772
    assert(fpr);
vb@200
  1773
vb@211
  1774
    memset(&handle, 0, sizeof(revoke_state));
vb@213
  1775
    handle.reason_ref = reason;
vb@211
  1776
vb@200
  1777
    status = find_single_key(session, fpr, &key);
vb@200
  1778
    if (status != PEP_STATUS_OK)
vb@200
  1779
        return status;
vb@200
  1780
vb@211
  1781
    struct gpgme_data_cbs data_cbs;
vb@211
  1782
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
vb@211
  1783
    data_cbs.write = _nullwriter;
vb@220
  1784
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
vb@211
  1785
vb@220
  1786
    gpgme_error = gpg.gpgme_op_edit(session->ctx, key, revoke_fsm, &handle,
vb@211
  1787
            output);
vb@211
  1788
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@211
  1789
vb@211
  1790
    gpg.gpgme_data_release(output);
vb@211
  1791
    gpg.gpgme_key_unref(key);
vb@211
  1792
vb@200
  1793
    return PEP_STATUS_OK;
vb@200
  1794
}
vb@200
  1795
vb@214
  1796
PEP_STATUS pgp_key_expired(
vb@214
  1797
        PEP_SESSION session,
vb@214
  1798
        const char *fpr,
vb@214
  1799
        bool *expired
vb@214
  1800
    )
vb@214
  1801
{
vb@214
  1802
    PEP_STATUS status = PEP_STATUS_OK;
vb@214
  1803
    gpgme_key_t key;
vb@214
  1804
vb@214
  1805
    assert(session);
vb@214
  1806
    assert(fpr);
vb@214
  1807
    assert(expired);
vb@214
  1808
vb@214
  1809
    *expired = false;
vb@214
  1810
vb@214
  1811
    status = find_single_key(session, fpr, &key);
vb@214
  1812
    if (status != PEP_STATUS_OK)
vb@214
  1813
        return status;
vb@214
  1814
Edouard@621
  1815
    if (key && key->subkeys)
Edouard@620
  1816
    {
Edouard@620
  1817
        *expired = key->subkeys->expired;
Edouard@620
  1818
    }
Edouard@620
  1819
    else
Edouard@620
  1820
    {
Edouard@620
  1821
        status = PEP_KEY_NOT_FOUND;
Edouard@620
  1822
    }
Edouard@620
  1823
vb@214
  1824
    gpg.gpgme_key_unref(key);
Edouard@620
  1825
    return status;
vb@214
  1826
}
vb@214
  1827
Edouard@662
  1828
PEP_STATUS pgp_key_revoked(
Edouard@662
  1829
        PEP_SESSION session,
Edouard@662
  1830
        const char *fpr,
Edouard@662
  1831
        bool *revoked
Edouard@662
  1832
    )
Edouard@662
  1833
{
Edouard@662
  1834
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@662
  1835
    gpgme_key_t key;
Edouard@662
  1836
Edouard@662
  1837
    assert(session);
Edouard@662
  1838
    assert(fpr);
Edouard@662
  1839
    assert(revoked);
Edouard@662
  1840
Edouard@664
  1841
    *revoked = false;
Edouard@662
  1842
Edouard@662
  1843
    status = find_single_key(session, fpr, &key);
Edouard@662
  1844
    if (status != PEP_STATUS_OK)
Edouard@662
  1845
        return status;
Edouard@662
  1846
Edouard@662
  1847
    if (key && key->subkeys)
Edouard@662
  1848
    {
Edouard@664
  1849
        *revoked = key->subkeys->revoked;
Edouard@662
  1850
    }
Edouard@662
  1851
    else
Edouard@662
  1852
    {
Edouard@662
  1853
        status = PEP_KEY_NOT_FOUND;
Edouard@662
  1854
    }
Edouard@662
  1855
Edouard@662
  1856
    gpg.gpgme_key_unref(key);
Edouard@662
  1857
    return status;
Edouard@662
  1858
}
Edouard@662
  1859
vb@507
  1860
PEP_STATUS pgp_binary(const char **path)
vb@507
  1861
{
vb@507
  1862
    assert(path);
vb@507
  1863
    if (path == NULL)
vb@507
  1864
        return PEP_ILLEGAL_VALUE;
vb@507
  1865
vb@507
  1866
    *path = NULL;
vb@507
  1867
vb@507
  1868
    gpgme_engine_info_t info;
vb@507
  1869
    int err = gpg.gpgme_get_engine_info(&info);
vb@507
  1870
    assert(err == GPG_ERR_NO_ERROR);
vb@507
  1871
    if (err != GPG_ERR_NO_ERROR)
vb@507
  1872
        return PEP_OUT_OF_MEMORY;
vb@507
  1873
vb@507
  1874
    *path = info->file_name;
vb@507
  1875
vb@507
  1876
    return PEP_STATUS_OK;
vb@507
  1877
}
vb@507
  1878