src/pgp_gpg.c
author Edouard Tisserant
Tue, 24 May 2016 11:26:24 +0200
changeset 677 0fe0f82eb621
parent 674 a8afa1168b12
child 701 331a6b3aaeab
permissions -rw-r--r--
Fixing double unreferencing of key in pgp_gpg.c
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@677
   479
Edouard@674
   480
                                gpg.gpgme_key_unref(key);
Edouard@677
   481
Edouard@677
   482
                                if (k == NULL) {
Edouard@677
   483
                                    free_stringlist(_keylist);
Edouard@677
   484
                                    gpg.gpgme_data_release(plain);
Edouard@677
   485
                                    gpg.gpgme_data_release(cipher);
Edouard@677
   486
                                    free(_buffer);
Edouard@677
   487
                                    return PEP_OUT_OF_MEMORY;
Edouard@677
   488
                                }
Edouard@674
   489
                            }
Edouard@674
   490
                            else 
Edouard@674
   491
                            {
Edouard@505
   492
                                result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
Edouard@505
   493
                                break;
Edouard@505
   494
                            }
vb@344
   495
                            break;
Edouard@505
   496
                        }
vb@344
   497
                        case GPG_ERR_CERT_REVOKED:
vb@344
   498
                        case GPG_ERR_BAD_SIGNATURE:
vb@344
   499
                            result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
vb@344
   500
                            break;
vb@344
   501
                        case GPG_ERR_SIG_EXPIRED:
vb@344
   502
                        case GPG_ERR_KEY_EXPIRED:
vb@344
   503
                        case GPG_ERR_NO_PUBKEY:
vb@344
   504
                            k = stringlist_add(k, gpgme_signature->fpr);
Edouard@674
   505
                            if (k == NULL) {
Edouard@674
   506
                                free_stringlist(_keylist);
Edouard@674
   507
                                gpg.gpgme_data_release(plain);
Edouard@674
   508
                                gpg.gpgme_data_release(cipher);
Edouard@674
   509
                                free(_buffer);
Edouard@674
   510
                                return PEP_OUT_OF_MEMORY;
Edouard@674
   511
                            }
vb@344
   512
                            if (result == PEP_DECRYPTED_AND_VERIFIED)
vb@344
   513
                                result = PEP_DECRYPTED;
vb@344
   514
                            break;
vb@344
   515
                        case GPG_ERR_GENERAL:
vb@344
   516
                            break;
vb@344
   517
                        default:
vb@344
   518
                            if (result == PEP_DECRYPTED_AND_VERIFIED)
vb@344
   519
                                result = PEP_DECRYPTED;
vb@344
   520
                            break;
vb@344
   521
                        }
vb@344
   522
                    } while ((gpgme_signature = gpgme_signature->next));
vb@344
   523
                }
vb@344
   524
                else {
vb@344
   525
                    result = PEP_DECRYPTED;
vb@344
   526
                }
vb@344
   527
vb@344
   528
                if (result == PEP_DECRYPTED_AND_VERIFIED
vb@344
   529
                    || result == PEP_DECRYPTED) {
vb@344
   530
                    *ptext = _buffer;
vb@344
   531
                    *psize = reading;
vb@344
   532
                    (*ptext)[*psize] = 0; // safeguard for naive users
vb@344
   533
                    *keylist = _keylist;
vb@344
   534
                }
vb@344
   535
                else {
vb@344
   536
                    free_stringlist(_keylist);
vb@344
   537
                    free(_buffer);
vb@344
   538
                }
vb@344
   539
                break;
vb@344
   540
            }
vb@344
   541
            case GPG_ERR_BAD_PASSPHRASE:
vb@344
   542
                NOT_IMPLEMENTED;
vb@344
   543
            case GPG_ERR_DECRYPT_FAILED:
vb@344
   544
            default:
vb@344
   545
            {
vb@344
   546
                gpgme_decrypt_result_t gpgme_decrypt_result = gpg.gpgme_op_decrypt_result(session->ctx);
vb@344
   547
                result = PEP_DECRYPT_NO_KEY;
vb@344
   548
vb@344
   549
                if (gpgme_decrypt_result != NULL) {
vb@344
   550
                    if (gpgme_decrypt_result->unsupported_algorithm)
vb@344
   551
                        *keylist = new_stringlist(gpgme_decrypt_result->unsupported_algorithm);
vb@344
   552
                    else
vb@344
   553
                        *keylist = new_stringlist("");
vb@344
   554
                    assert(*keylist);
vb@344
   555
                    if (*keylist == NULL) {
vb@24
   556
                        result = PEP_OUT_OF_MEMORY;
vb@24
   557
                        break;
vb@24
   558
                    }
vb@344
   559
                    stringlist_t *_keylist = *keylist;
vb@344
   560
                    for (gpgme_recipient_t r = gpgme_decrypt_result->recipients; r != NULL; r = r->next) {
vb@344
   561
                        _keylist = stringlist_add(_keylist, r->keyid);
vb@344
   562
                        assert(_keylist);
vb@344
   563
                        if (_keylist == NULL) {
vb@344
   564
                            free_stringlist(*keylist);
vb@344
   565
                            *keylist = NULL;
vb@344
   566
                            result = PEP_OUT_OF_MEMORY;
vb@344
   567
                            break;
vb@344
   568
                        }
vb@344
   569
                    }
vb@344
   570
                    if (result == PEP_OUT_OF_MEMORY)
vb@344
   571
                        break;
vb@24
   572
                }
vb@24
   573
            }
vb@24
   574
        }
vb@24
   575
        break;
vb@24
   576
vb@24
   577
    default:
vb@24
   578
        result = PEP_DECRYPT_WRONG_FORMAT;
vb@24
   579
    }
vb@24
   580
vb@74
   581
    gpg.gpgme_data_release(plain);
vb@74
   582
    gpg.gpgme_data_release(cipher);
vb@24
   583
    return result;
vb@24
   584
}
vb@24
   585
vb@24
   586
PEP_STATUS pgp_verify_text(
vb@24
   587
    PEP_SESSION session, const char *text, size_t size,
vb@24
   588
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
   589
    )
vb@24
   590
{
vb@24
   591
    PEP_STATUS result;
vb@24
   592
    gpgme_error_t gpgme_error;
vb@24
   593
    gpgme_data_t d_text, d_sig;
vb@24
   594
    stringlist_t *_keylist;
vb@24
   595
vb@24
   596
    assert(session);
vb@24
   597
    assert(text);
vb@24
   598
    assert(size);
vb@24
   599
    assert(signature);
vb@24
   600
    assert(sig_size);
vb@24
   601
    assert(keylist);
vb@24
   602
vb@24
   603
    *keylist = NULL;
vb@24
   604
vb@74
   605
    gpgme_error = gpg.gpgme_data_new_from_mem(&d_text, text, size, 0);
vb@45
   606
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   607
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   608
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@24
   609
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   610
            return PEP_OUT_OF_MEMORY;
vb@24
   611
        else
vb@24
   612
            return PEP_UNKNOWN_ERROR;
vb@24
   613
    }
vb@24
   614
vb@74
   615
    gpgme_error = gpg.gpgme_data_new_from_mem(&d_sig, signature, sig_size, 0);
vb@45
   616
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   617
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   618
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@74
   619
        gpg.gpgme_data_release(d_text);
vb@24
   620
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   621
            return PEP_OUT_OF_MEMORY;
vb@24
   622
        else
vb@24
   623
            return PEP_UNKNOWN_ERROR;
vb@24
   624
    }
vb@24
   625
vb@74
   626
    gpgme_error = gpg.gpgme_op_verify(session->ctx, d_sig, d_text, NULL);
vb@45
   627
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   628
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
   629
vb@24
   630
    switch (gpgme_error) {
vb@24
   631
    case GPG_ERR_NO_ERROR:
vb@24
   632
    {
vb@24
   633
        gpgme_verify_result_t gpgme_verify_result;
vb@24
   634
        gpgme_signature_t gpgme_signature;
vb@24
   635
vb@24
   636
        gpgme_verify_result =
vb@74
   637
            gpg.gpgme_op_verify_result(session->ctx);
vb@24
   638
        assert(gpgme_verify_result);
vb@24
   639
        gpgme_signature = gpgme_verify_result->signatures;
vb@24
   640
vb@24
   641
        if (gpgme_signature) {
vb@24
   642
            stringlist_t *k;
vb@24
   643
            _keylist = new_stringlist(NULL);
vb@24
   644
            assert(_keylist);
vb@24
   645
            if (_keylist == NULL) {
vb@74
   646
                gpg.gpgme_data_release(d_text);
vb@74
   647
                gpg.gpgme_data_release(d_sig);
vb@24
   648
                return PEP_OUT_OF_MEMORY;
vb@24
   649
            }
vb@24
   650
            k = _keylist;
vb@24
   651
vb@24
   652
            result = PEP_VERIFIED;
vb@24
   653
            do {
Edouard@502
   654
                gpgme_key_t key;
Edouard@502
   655
                memset(&key,0,sizeof(key));
Edouard@502
   656
Edouard@502
   657
                // GPGME may give subkey's fpr instead of primary key's fpr. 
Edouard@502
   658
                // Therefore we ask for the primary fingerprint instead
Edouard@502
   659
                // we assume that gpgme_get_key can find key by subkey's fpr
Edouard@502
   660
                gpgme_error = gpg.gpgme_get_key(session->ctx,
Edouard@502
   661
                    gpgme_signature->fpr, &key, 0);
Edouard@502
   662
                gpgme_error = _GPGERR(gpgme_error);
Edouard@502
   663
                assert(gpgme_error != GPG_ERR_ENOMEM);
Edouard@502
   664
                if (gpgme_error == GPG_ERR_ENOMEM) {
vb@24
   665
                    free_stringlist(_keylist);
Edouard@505
   666
                    gpg.gpgme_data_release(d_text);
Edouard@505
   667
                    gpg.gpgme_data_release(d_sig);
vb@24
   668
                    return PEP_OUT_OF_MEMORY;
vb@24
   669
                }
Edouard@502
   670
                // Primary key is given as the first subkey
Edouard@674
   671
                if (gpgme_error == GPG_ERR_NO_ERROR &&  
Edouard@674
   672
                    key && key->subkeys && key->subkeys->fpr 
Edouard@674
   673
                    && key->subkeys->fpr[0])
Edouard@674
   674
                {
Edouard@504
   675
                    k = stringlist_add(k, key->subkeys->fpr);
Edouard@677
   676
Edouard@674
   677
                    gpg.gpgme_key_unref(key);
Edouard@677
   678
Edouard@677
   679
                    if (k == NULL) {
Edouard@677
   680
                        free_stringlist(_keylist);
Edouard@677
   681
                        gpg.gpgme_data_release(d_text);
Edouard@677
   682
                        gpg.gpgme_data_release(d_sig);
Edouard@677
   683
                        return PEP_OUT_OF_MEMORY;
Edouard@677
   684
                    }
Edouard@502
   685
                }
Edouard@502
   686
                else {
Edouard@502
   687
                    result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
Edouard@502
   688
                    break;
Edouard@502
   689
                }
Edouard@502
   690
vb@24
   691
                if (gpgme_signature->summary & GPGME_SIGSUM_RED) {
vb@24
   692
                    if (gpgme_signature->summary & GPGME_SIGSUM_KEY_EXPIRED
vb@24
   693
                        || gpgme_signature->summary & GPGME_SIGSUM_SIG_EXPIRED) {
vb@24
   694
                        if (result == PEP_VERIFIED
vb@24
   695
                            || result == PEP_VERIFIED_AND_TRUSTED)
vb@24
   696
                            result = PEP_UNENCRYPTED;
vb@24
   697
                    }
vb@24
   698
                    else {
vb@24
   699
                        result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
vb@24
   700
                        break;
vb@24
   701
                    }
vb@24
   702
                }
vb@24
   703
                else {
vb@24
   704
                    if (gpgme_signature->summary & GPGME_SIGSUM_VALID) {
vb@24
   705
                        if (result == PEP_VERIFIED)
vb@24
   706
                            result = PEP_VERIFIED_AND_TRUSTED;
vb@24
   707
                    }
vb@24
   708
                    if (gpgme_signature->summary & GPGME_SIGSUM_GREEN) {
vb@24
   709
                        // good
vb@24
   710
                    }
vb@24
   711
                    else if (gpgme_signature->summary & GPGME_SIGSUM_KEY_MISSING) {
vb@24
   712
                        result = PEP_VERIFY_NO_KEY;
vb@24
   713
                    }
vb@24
   714
                    else if (gpgme_signature->summary & GPGME_SIGSUM_SYS_ERROR) {
vb@24
   715
                        if (result == PEP_VERIFIED
vb@24
   716
                            || result == PEP_VERIFIED_AND_TRUSTED)
vb@24
   717
                            result = PEP_UNENCRYPTED;
vb@24
   718
                    }
vb@24
   719
                    else {
vb@24
   720
                        // do nothing
vb@24
   721
                    }
vb@24
   722
                }
vb@24
   723
            } while ((gpgme_signature = gpgme_signature->next));
vb@24
   724
            *keylist = _keylist;
vb@24
   725
        }
vb@24
   726
        else {
vb@24
   727
            result = PEP_UNENCRYPTED;
vb@24
   728
        }
vb@24
   729
        break;
vb@24
   730
    }
vb@24
   731
        break;
vb@24
   732
    case GPG_ERR_NO_DATA:
vb@24
   733
        result = PEP_DECRYPT_WRONG_FORMAT;
vb@24
   734
        break;
vb@24
   735
    case GPG_ERR_INV_VALUE:
vb@24
   736
    default:
vb@24
   737
        result = PEP_UNKNOWN_ERROR;
vb@24
   738
        break;
vb@24
   739
    }
vb@24
   740
vb@74
   741
    gpg.gpgme_data_release(d_text);
vb@74
   742
    gpg.gpgme_data_release(d_sig);
vb@24
   743
vb@24
   744
    return result;
vb@24
   745
}
vb@24
   746
vb@24
   747
PEP_STATUS pgp_encrypt_and_sign(
vb@24
   748
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
   749
    size_t psize, char **ctext, size_t *csize
vb@24
   750
    )
vb@24
   751
{
vb@24
   752
    PEP_STATUS result;
vb@24
   753
    gpgme_error_t gpgme_error;
vb@24
   754
    gpgme_data_t plain, cipher;
vb@24
   755
    gpgme_key_t *rcpt;
vb@24
   756
    gpgme_encrypt_flags_t flags;
vb@24
   757
    const stringlist_t *_keylist;
vb@24
   758
    int i, j;
vb@24
   759
vb@46
   760
    assert(session);
vb@24
   761
    assert(keylist);
vb@24
   762
    assert(ptext);
vb@24
   763
    assert(psize);
vb@24
   764
    assert(ctext);
vb@24
   765
    assert(csize);
vb@24
   766
vb@24
   767
    *ctext = NULL;
vb@24
   768
    *csize = 0;
vb@24
   769
vb@74
   770
    gpgme_error = gpg.gpgme_data_new_from_mem(&plain, ptext, psize, 0);
vb@45
   771
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   772
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   773
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@24
   774
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   775
            return PEP_OUT_OF_MEMORY;
vb@24
   776
        else
vb@24
   777
            return PEP_UNKNOWN_ERROR;
vb@24
   778
    }
vb@24
   779
vb@74
   780
    gpgme_error = gpg.gpgme_data_new(&cipher);
vb@45
   781
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   782
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   783
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@74
   784
        gpg.gpgme_data_release(plain);
vb@24
   785
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   786
            return PEP_OUT_OF_MEMORY;
vb@24
   787
        else
vb@24
   788
            return PEP_UNKNOWN_ERROR;
vb@24
   789
    }
vb@24
   790
vb@109
   791
    rcpt = calloc(stringlist_length(keylist) + 1, sizeof(gpgme_key_t));
vb@24
   792
    assert(rcpt);
vb@24
   793
    if (rcpt == NULL) {
vb@74
   794
        gpg.gpgme_data_release(plain);
vb@74
   795
        gpg.gpgme_data_release(cipher);
vb@24
   796
        return PEP_OUT_OF_MEMORY;
vb@24
   797
    }
vb@24
   798
vb@74
   799
    gpg.gpgme_signers_clear(session->ctx);
vb@24
   800
vb@24
   801
    for (_keylist = keylist, i = 0; _keylist != NULL; _keylist = _keylist->next, i++) {
vb@24
   802
        assert(_keylist->value);
vb@74
   803
        gpgme_error = gpg.gpgme_get_key(session->ctx, _keylist->value,
vb@24
   804
            &rcpt[i], 0);
vb@45
   805
        gpgme_error = _GPGERR(gpgme_error);
vb@24
   806
        assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
   807
vb@24
   808
        switch (gpgme_error) {
vb@24
   809
        case GPG_ERR_ENOMEM:
vb@24
   810
            for (j = 0; j<i; j++)
vb@74
   811
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
   812
            free(rcpt);
vb@74
   813
            gpg.gpgme_data_release(plain);
vb@74
   814
            gpg.gpgme_data_release(cipher);
vb@24
   815
            return PEP_OUT_OF_MEMORY;
vb@24
   816
        case GPG_ERR_NO_ERROR:
vb@24
   817
            if (i == 0) {
vb@74
   818
                gpgme_error_t _gpgme_error = gpg.gpgme_signers_add(session->ctx, rcpt[0]);
vb@45
   819
                _gpgme_error = _GPGERR(_gpgme_error);
vb@24
   820
                assert(_gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   821
            }
vb@24
   822
            break;
vb@24
   823
        case GPG_ERR_EOF:
vb@24
   824
            for (j = 0; j<i; j++)
vb@74
   825
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
   826
            free(rcpt);
vb@74
   827
            gpg.gpgme_data_release(plain);
vb@74
   828
            gpg.gpgme_data_release(cipher);
vb@24
   829
            return PEP_KEY_NOT_FOUND;
vb@24
   830
        case GPG_ERR_AMBIGUOUS_NAME:
vb@24
   831
            for (j = 0; j<i; j++)
vb@74
   832
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
   833
            free(rcpt);
vb@74
   834
            gpg.gpgme_data_release(plain);
vb@74
   835
            gpg.gpgme_data_release(cipher);
vb@24
   836
            return PEP_KEY_HAS_AMBIG_NAME;
vb@24
   837
        default: // GPG_ERR_INV_VALUE if CTX or R_KEY is not a valid pointer or
vb@24
   838
            // FPR is not a fingerprint or key ID
vb@24
   839
            for (j = 0; j<i; j++)
vb@74
   840
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
   841
            free(rcpt);
vb@74
   842
            gpg.gpgme_data_release(plain);
vb@74
   843
            gpg.gpgme_data_release(cipher);
vb@24
   844
            return PEP_GET_KEY_FAILED;
vb@24
   845
        }
vb@24
   846
    }
vb@24
   847
vb@24
   848
    // TODO: remove that and replace with proper key management
vb@24
   849
    flags = GPGME_ENCRYPT_ALWAYS_TRUST;
vb@24
   850
vb@74
   851
    gpgme_error = gpg.gpgme_op_encrypt_sign(session->ctx, rcpt, flags,
vb@24
   852
        plain, cipher);
vb@45
   853
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   854
    switch (gpgme_error) {
vb@24
   855
    case GPG_ERR_NO_ERROR:
vb@24
   856
    {
vb@24
   857
        char *_buffer = NULL;
vb@24
   858
        size_t reading;
vb@74
   859
        size_t length = gpg.gpgme_data_seek(cipher, 0, SEEK_END);
vb@24
   860
        assert(length != -1);
vb@74
   861
        gpg.gpgme_data_seek(cipher, 0, SEEK_SET);
vb@24
   862
vb@24
   863
        // TODO: make things less memory consuming
vb@24
   864
        // the following algorithm allocates a buffer for the complete text
vb@24
   865
vb@112
   866
        _buffer = malloc(length + 1);
vb@24
   867
        assert(_buffer);
vb@24
   868
        if (_buffer == NULL) {
vb@24
   869
            for (j = 0; j<stringlist_length(keylist); j++)
vb@74
   870
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
   871
            free(rcpt);
vb@74
   872
            gpg.gpgme_data_release(plain);
vb@74
   873
            gpg.gpgme_data_release(cipher);
vb@24
   874
            return PEP_OUT_OF_MEMORY;
vb@24
   875
        }
vb@24
   876
vb@74
   877
        reading = gpg.gpgme_data_read(cipher, _buffer, length);
vb@24
   878
        assert(length == reading);
vb@24
   879
vb@24
   880
        *ctext = _buffer;
vb@24
   881
        *csize = reading;
vb@24
   882
        (*ctext)[*csize] = 0; // safeguard for naive users
vb@24
   883
        result = PEP_STATUS_OK;
vb@24
   884
        break;
vb@24
   885
    }
vb@24
   886
    default:
vb@24
   887
        result = PEP_UNKNOWN_ERROR;
vb@24
   888
    }
vb@24
   889
vb@24
   890
    for (j = 0; j<stringlist_length(keylist); j++)
vb@74
   891
        gpg.gpgme_key_unref(rcpt[j]);
vb@24
   892
    free(rcpt);
vb@74
   893
    gpg.gpgme_data_release(plain);
vb@74
   894
    gpg.gpgme_data_release(cipher);
vb@24
   895
    return result;
vb@24
   896
}
vb@24
   897
vb@24
   898
PEP_STATUS pgp_generate_keypair(
vb@24
   899
    PEP_SESSION session, pEp_identity *identity
vb@24
   900
    )
vb@24
   901
{
vb@24
   902
    gpgme_error_t gpgme_error;
vb@24
   903
    char *parms;
vb@24
   904
    const char *template =
vb@24
   905
        "<GnupgKeyParms format=\"internal\">\n"
vb@24
   906
        "Key-Type: RSA\n"
vb@24
   907
        "Key-Length: 4096\n"
vb@429
   908
        "Subkey-Type: RSA\n"
vb@429
   909
        "Subkey-Length: 4096\n"
vb@24
   910
        "Name-Real: %s\n"
vb@24
   911
        "Name-Email: %s\n"
vb@24
   912
        /* "Passphrase: %s\n" */
vb@24
   913
        "Expire-Date: 1y\n"
vb@24
   914
        "</GnupgKeyParms>\n";
vb@24
   915
    int result;
vb@24
   916
    gpgme_genkey_result_t gpgme_genkey_result;
vb@24
   917
vb@24
   918
    assert(session);
vb@24
   919
    assert(identity);
vb@24
   920
    assert(identity->address);
vb@298
   921
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@24
   922
    assert(identity->username);
vb@24
   923
vb@24
   924
    parms = calloc(1, PARMS_MAX);
vb@24
   925
    assert(parms);
vb@24
   926
    if (parms == NULL)
vb@24
   927
        return PEP_OUT_OF_MEMORY;
vb@24
   928
vb@24
   929
    result = snprintf(parms, PARMS_MAX, template, identity->username,
vb@46
   930
        identity->address); // , session->passphrase);
vb@24
   931
    assert(result < PARMS_MAX);
vb@24
   932
    if (result >= PARMS_MAX) {
vb@24
   933
        free(parms);
vb@24
   934
        return PEP_BUFFER_TOO_SMALL;
vb@24
   935
    }
vb@24
   936
vb@74
   937
    gpgme_error = gpg.gpgme_op_genkey(session->ctx, parms, NULL, NULL);
vb@45
   938
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   939
    free(parms);
vb@24
   940
vb@24
   941
    switch (gpgme_error) {
vb@24
   942
    case GPG_ERR_NO_ERROR:
vb@24
   943
        break;
vb@24
   944
    case GPG_ERR_INV_VALUE:
vb@24
   945
        return PEP_ILLEGAL_VALUE;
vb@24
   946
    case GPG_ERR_GENERAL:
vb@24
   947
        return PEP_CANNOT_CREATE_KEY;
vb@24
   948
    default:
vb@24
   949
        assert(0);
vb@24
   950
        return PEP_UNKNOWN_ERROR;
vb@24
   951
    }
vb@24
   952
vb@74
   953
    gpgme_genkey_result = gpg.gpgme_op_genkey_result(session->ctx);
vb@24
   954
    assert(gpgme_genkey_result);
vb@24
   955
    assert(gpgme_genkey_result->fpr);
vb@24
   956
vb@298
   957
    free(identity->fpr);
vb@24
   958
    identity->fpr = strdup(gpgme_genkey_result->fpr);
vb@298
   959
    if (identity->fpr == NULL)
vb@298
   960
        return PEP_OUT_OF_MEMORY;
vb@24
   961
vb@24
   962
    return PEP_STATUS_OK;
vb@24
   963
}
vb@24
   964
vb@24
   965
PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
   966
{
vb@24
   967
    gpgme_error_t gpgme_error;
vb@24
   968
    gpgme_key_t key;
vb@24
   969
vb@24
   970
    assert(session);
vb@24
   971
    assert(fpr);
vb@24
   972
vb@74
   973
    gpgme_error = gpg.gpgme_get_key(session->ctx, fpr, &key, 0);
vb@45
   974
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   975
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
   976
    switch (gpgme_error) {
vb@24
   977
    case GPG_ERR_NO_ERROR:
vb@24
   978
        break;
vb@24
   979
    case GPG_ERR_EOF:
vb@24
   980
        return PEP_KEY_NOT_FOUND;
vb@24
   981
    case GPG_ERR_INV_VALUE:
vb@24
   982
        return PEP_ILLEGAL_VALUE;
vb@24
   983
    case GPG_ERR_AMBIGUOUS_NAME:
vb@24
   984
        return PEP_KEY_HAS_AMBIG_NAME;
vb@24
   985
    case GPG_ERR_ENOMEM:
vb@24
   986
        return PEP_OUT_OF_MEMORY;
vb@24
   987
    default:
vb@24
   988
        assert(0);
vb@24
   989
        return PEP_UNKNOWN_ERROR;
vb@24
   990
    }
vb@24
   991
vb@74
   992
    gpgme_error = gpg.gpgme_op_delete(session->ctx, key, 1);
vb@45
   993
    gpgme_error = _GPGERR(gpgme_error);
vb@74
   994
    gpg.gpgme_key_unref(key);
vb@24
   995
    switch (gpgme_error) {
vb@24
   996
    case GPG_ERR_NO_ERROR:
vb@24
   997
        break;
vb@24
   998
    case GPG_ERR_INV_VALUE:
vb@24
   999
        assert(0);
vb@24
  1000
        return PEP_UNKNOWN_ERROR;
vb@24
  1001
    case GPG_ERR_NO_PUBKEY:
vb@24
  1002
        assert(0);
vb@24
  1003
        return PEP_KEY_NOT_FOUND;
vb@24
  1004
    case GPG_ERR_AMBIGUOUS_NAME:
vb@24
  1005
        assert(0);
vb@24
  1006
        return PEP_KEY_HAS_AMBIG_NAME;
vb@24
  1007
    default:
vb@24
  1008
        assert(0);
vb@24
  1009
        return PEP_UNKNOWN_ERROR;
vb@24
  1010
    }
vb@24
  1011
vb@24
  1012
    return PEP_STATUS_OK;
vb@24
  1013
}
vb@24
  1014
Edouard@170
  1015
PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data, size_t size)
vb@24
  1016
{
vb@24
  1017
    gpgme_error_t gpgme_error;
vb@24
  1018
    gpgme_data_t dh;
vb@24
  1019
vb@24
  1020
    assert(session);
vb@24
  1021
    assert(key_data);
vb@24
  1022
vb@74
  1023
    gpgme_error = gpg.gpgme_data_new_from_mem(&dh, key_data, size, 0);
vb@45
  1024
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1025
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1026
    switch (gpgme_error) {
vb@24
  1027
    case GPG_ERR_NO_ERROR:
vb@24
  1028
        break;
vb@24
  1029
    case GPG_ERR_ENOMEM:
vb@24
  1030
        return PEP_OUT_OF_MEMORY;
vb@24
  1031
    case GPG_ERR_INV_VALUE:
vb@24
  1032
        assert(0);
vb@24
  1033
        return PEP_UNKNOWN_ERROR;
vb@24
  1034
    default:
vb@24
  1035
        assert(0);
vb@24
  1036
        return PEP_UNKNOWN_ERROR;
vb@24
  1037
    }
vb@24
  1038
vb@74
  1039
    gpgme_error = gpg.gpgme_op_import(session->ctx, dh);
vb@45
  1040
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1041
    switch (gpgme_error) {
vb@24
  1042
    case GPG_ERR_NO_ERROR:
vb@24
  1043
        break;
vb@24
  1044
    case GPG_ERR_INV_VALUE:
vb@24
  1045
        assert(0);
vb@74
  1046
        gpg.gpgme_data_release(dh);
vb@24
  1047
        return PEP_UNKNOWN_ERROR;
vb@24
  1048
    case GPG_ERR_NO_DATA:
vb@74
  1049
        gpg.gpgme_data_release(dh);
vb@24
  1050
        return PEP_ILLEGAL_VALUE;
vb@24
  1051
    default:
vb@24
  1052
        assert(0);
vb@74
  1053
        gpg.gpgme_data_release(dh);
vb@24
  1054
        return PEP_UNKNOWN_ERROR;
vb@24
  1055
    }
vb@24
  1056
vb@74
  1057
    gpg.gpgme_data_release(dh);
vb@24
  1058
    return PEP_STATUS_OK;
vb@24
  1059
}
vb@24
  1060
Edouard@170
  1061
PEP_STATUS pgp_export_keydata(
vb@24
  1062
    PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
  1063
    )
vb@24
  1064
{
vb@24
  1065
    gpgme_error_t gpgme_error;
vb@24
  1066
    gpgme_data_t dh;
vb@24
  1067
    size_t _size;
vb@24
  1068
    char *buffer;
vb@24
  1069
    int reading;
vb@24
  1070
vb@24
  1071
    assert(session);
vb@24
  1072
    assert(fpr);
vb@24
  1073
    assert(key_data);
vb@24
  1074
    assert(size);
vb@24
  1075
vb@74
  1076
    gpgme_error = gpg.gpgme_data_new(&dh);
vb@45
  1077
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1078
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1079
    switch (gpgme_error) {
vb@24
  1080
    case GPG_ERR_NO_ERROR:
vb@24
  1081
        break;
vb@24
  1082
    case GPG_ERR_ENOMEM:
vb@24
  1083
        return PEP_OUT_OF_MEMORY;
vb@24
  1084
    case GPG_ERR_INV_VALUE:
vb@24
  1085
        assert(0);
vb@24
  1086
        return PEP_UNKNOWN_ERROR;
vb@24
  1087
    default:
vb@24
  1088
        assert(0);
vb@24
  1089
        return PEP_UNKNOWN_ERROR;
vb@24
  1090
    }
vb@24
  1091
vb@74
  1092
    gpgme_error = gpg.gpgme_op_export(session->ctx, fpr,
vb@24
  1093
        GPGME_EXPORT_MODE_MINIMAL, dh);
vb@45
  1094
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1095
    switch (gpgme_error) {
vb@24
  1096
    case GPG_ERR_NO_ERROR:
vb@24
  1097
        break;
vb@24
  1098
    case GPG_ERR_EOF:
vb@74
  1099
        gpg.gpgme_data_release(dh);
vb@24
  1100
        return PEP_KEY_NOT_FOUND;
vb@24
  1101
    case GPG_ERR_INV_VALUE:
vb@24
  1102
        assert(0);
vb@74
  1103
        gpg.gpgme_data_release(dh);
vb@24
  1104
        return PEP_UNKNOWN_ERROR;
vb@24
  1105
    default:
vb@24
  1106
        assert(0);
vb@74
  1107
        gpg.gpgme_data_release(dh);
vb@24
  1108
        return PEP_UNKNOWN_ERROR;
vb@24
  1109
    };
vb@24
  1110
vb@74
  1111
    _size = gpg.gpgme_data_seek(dh, 0, SEEK_END);
vb@24
  1112
    assert(_size != -1);
vb@74
  1113
    gpg.gpgme_data_seek(dh, 0, SEEK_SET);
vb@24
  1114
vb@24
  1115
    buffer = malloc(_size + 1);
vb@24
  1116
    assert(buffer);
vb@24
  1117
    if (buffer == NULL) {
vb@74
  1118
        gpg.gpgme_data_release(dh);
vb@24
  1119
        return PEP_OUT_OF_MEMORY;
vb@24
  1120
    }
vb@24
  1121
vb@74
  1122
    reading = gpg.gpgme_data_read(dh, buffer, _size);
vb@24
  1123
    assert(_size == reading);
vb@24
  1124
vb@24
  1125
    // safeguard for the naive user
vb@24
  1126
    buffer[_size] = 0;
vb@24
  1127
vb@24
  1128
    *key_data = buffer;
vb@24
  1129
    *size = _size;
vb@24
  1130
vb@74
  1131
    gpg.gpgme_data_release(dh);
vb@24
  1132
    return PEP_STATUS_OK;
vb@24
  1133
}
vb@24
  1134
vb@46
  1135
static void _switch_mode(pEpSession *session, gpgme_keylist_mode_t remove_mode,
vb@24
  1136
    gpgme_keylist_mode_t add_mode)
vb@24
  1137
{
vb@24
  1138
    gpgme_error_t gpgme_error;
vb@24
  1139
    gpgme_keylist_mode_t mode;
vb@24
  1140
vb@74
  1141
    mode = gpg.gpgme_get_keylist_mode(session->ctx);
vb@24
  1142
vb@24
  1143
    mode &= ~remove_mode;
vb@24
  1144
    mode |= add_mode;
vb@24
  1145
vb@74
  1146
    gpgme_error = gpg.gpgme_set_keylist_mode(session->ctx, mode);
vb@45
  1147
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1148
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1149
}
vb@24
  1150
vb@24
  1151
PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1152
{
vb@24
  1153
    gpgme_error_t gpgme_error;
vb@24
  1154
    gpgme_key_t key;
vb@24
  1155
vb@24
  1156
    assert(session);
vb@24
  1157
    assert(pattern);
vb@24
  1158
vb@46
  1159
    _switch_mode(session, GPGME_KEYLIST_MODE_LOCAL, GPGME_KEYLIST_MODE_EXTERN);
vb@24
  1160
vb@74
  1161
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
vb@45
  1162
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1163
    switch (gpgme_error) {
vb@24
  1164
    case GPG_ERR_NO_ERROR:
vb@24
  1165
        break;
vb@24
  1166
    case GPG_ERR_INV_VALUE:
vb@24
  1167
        assert(0);
vb@47
  1168
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1169
        return PEP_UNKNOWN_ERROR;
vb@24
  1170
    default:
vb@47
  1171
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1172
        return PEP_GET_KEY_FAILED;
vb@24
  1173
    };
vb@24
  1174
vb@47
  1175
    gpgme_ctx_t import_ctx;
vb@74
  1176
    gpgme_error = gpg.gpgme_new(&import_ctx);
vb@47
  1177
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@47
  1178
vb@24
  1179
    do {
vb@74
  1180
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1181
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1182
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1183
        switch (gpgme_error) {
vb@24
  1184
        case GPG_ERR_EOF:
vb@24
  1185
            break;
vb@24
  1186
        case GPG_ERR_NO_ERROR:
vb@24
  1187
        {
vb@24
  1188
            gpgme_error_t gpgme_error;
vb@24
  1189
            gpgme_key_t keys[2];
vb@24
  1190
vb@24
  1191
            keys[0] = key;
vb@24
  1192
            keys[1] = NULL;
vb@24
  1193
vb@74
  1194
            gpgme_error = gpg.gpgme_op_import_keys(import_ctx, keys);
vb@45
  1195
            gpgme_error = _GPGERR(gpgme_error);
vb@74
  1196
            gpg.gpgme_key_unref(key);
vb@24
  1197
            assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1198
            assert(gpgme_error != GPG_ERR_CONFLICT);
vb@24
  1199
        }
vb@24
  1200
            break;
vb@24
  1201
        case GPG_ERR_ENOMEM:
vb@74
  1202
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1203
            gpg.gpgme_release(import_ctx);
vb@47
  1204
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1205
            return PEP_OUT_OF_MEMORY;
vb@24
  1206
        default:
vb@74
  1207
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1208
            gpg.gpgme_release(import_ctx);
vb@47
  1209
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@46
  1210
            return PEP_UNKNOWN_ERROR;
vb@24
  1211
        };
vb@24
  1212
    } while (gpgme_error != GPG_ERR_EOF);
vb@24
  1213
vb@74
  1214
    gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1215
    gpg.gpgme_release(import_ctx);
vb@47
  1216
    _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1217
    return PEP_STATUS_OK;
vb@24
  1218
}
vb@24
  1219
vb@24
  1220
PEP_STATUS pgp_find_keys(
vb@24
  1221
    PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
  1222
    )
vb@24
  1223
{
vb@24
  1224
    gpgme_error_t gpgme_error;
vb@24
  1225
    gpgme_key_t key;
vb@24
  1226
    stringlist_t *_keylist;
vb@24
  1227
    char *fpr;
vb@24
  1228
vb@24
  1229
    assert(session);
vb@24
  1230
    assert(pattern);
vb@24
  1231
    assert(keylist);
vb@24
  1232
vb@24
  1233
    *keylist = NULL;
vb@24
  1234
vb@74
  1235
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
vb@45
  1236
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1237
    switch (gpgme_error) {
vb@24
  1238
    case GPG_ERR_NO_ERROR:
vb@24
  1239
        break;
vb@24
  1240
    case GPG_ERR_INV_VALUE:
vb@24
  1241
        assert(0);
vb@24
  1242
        return PEP_UNKNOWN_ERROR;
vb@24
  1243
    default:
vb@74
  1244
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1245
        return PEP_GET_KEY_FAILED;
vb@24
  1246
    };
vb@24
  1247
vb@24
  1248
    _keylist = new_stringlist(NULL);
vb@24
  1249
    stringlist_t *_k = _keylist;
vb@24
  1250
vb@24
  1251
    do {
vb@74
  1252
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1253
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1254
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1255
        switch (gpgme_error) {
vb@24
  1256
        case GPG_ERR_EOF:
vb@24
  1257
            break;
vb@24
  1258
        case GPG_ERR_NO_ERROR:
vb@24
  1259
            assert(key);
vb@24
  1260
            assert(key->subkeys);
vb@24
  1261
            fpr = key->subkeys->fpr;
vb@24
  1262
            assert(fpr);
vb@24
  1263
            _k = stringlist_add(_k, fpr);
vb@24
  1264
            assert(_k);
vb@24
  1265
            if (_k != NULL)
vb@24
  1266
                break;
vb@24
  1267
        case GPG_ERR_ENOMEM:
vb@24
  1268
            free_stringlist(_keylist);
vb@74
  1269
            gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1270
            return PEP_OUT_OF_MEMORY;
vb@24
  1271
        default:
vb@74
  1272
            gpg.gpgme_op_keylist_end(session->ctx);
vb@46
  1273
            return PEP_UNKNOWN_ERROR;
vb@24
  1274
        };
vb@24
  1275
    } while (gpgme_error != GPG_ERR_EOF);
vb@24
  1276
vb@74
  1277
    gpg.gpgme_op_keylist_end(session->ctx);
vb@523
  1278
    if (_keylist->value == NULL) {
vb@523
  1279
        free_stringlist(_keylist);
vb@523
  1280
        _keylist = NULL;
vb@523
  1281
    }
vb@24
  1282
    *keylist = _keylist;
vb@24
  1283
    return PEP_STATUS_OK;
vb@24
  1284
}
vb@24
  1285
vb@24
  1286
PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)
vb@24
  1287
{
vb@24
  1288
    gpgme_error_t gpgme_error;
vb@24
  1289
vb@24
  1290
    assert(session);
vb@24
  1291
    assert(pattern);
vb@24
  1292
vb@74
  1293
    gpgme_error = gpg.gpgme_op_export(session->ctx, pattern,
vb@24
  1294
        GPGME_EXPORT_MODE_EXTERN, NULL);
vb@45
  1295
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1296
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1297
    if (gpgme_error == GPG_ERR_NO_ERROR)
vb@24
  1298
        return PEP_STATUS_OK;
vb@24
  1299
    else
vb@24
  1300
        return PEP_CANNOT_SEND_KEY;
vb@24
  1301
}
vb@24
  1302
vb@24
  1303
PEP_STATUS pgp_get_key_rating(
vb@24
  1304
    PEP_SESSION session,
vb@24
  1305
    const char *fpr,
vb@24
  1306
    PEP_comm_type *comm_type
vb@24
  1307
    )
vb@24
  1308
{
vb@24
  1309
    PEP_STATUS status = PEP_STATUS_OK;
vb@24
  1310
    gpgme_error_t gpgme_error;
vb@24
  1311
    gpgme_key_t key;
vb@24
  1312
vb@24
  1313
    assert(session);
vb@24
  1314
    assert(fpr);
vb@24
  1315
    assert(comm_type);
vb@24
  1316
vb@24
  1317
    *comm_type = PEP_ct_unknown;
vb@24
  1318
vb@74
  1319
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
vb@45
  1320
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1321
    switch (gpgme_error) {
vb@24
  1322
    case GPG_ERR_NO_ERROR:
vb@24
  1323
        break;
vb@24
  1324
    case GPG_ERR_INV_VALUE:
vb@24
  1325
        assert(0);
vb@24
  1326
        return PEP_UNKNOWN_ERROR;
vb@24
  1327
    default:
vb@24
  1328
        return PEP_GET_KEY_FAILED;
vb@24
  1329
    };
vb@24
  1330
vb@74
  1331
    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1332
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1333
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1334
vb@24
  1335
    if (key == NULL) {
vb@74
  1336
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1337
        return PEP_KEY_NOT_FOUND;
vb@24
  1338
    }
vb@24
  1339
vb@24
  1340
    switch (key->protocol) {
vb@24
  1341
    case GPGME_PROTOCOL_OpenPGP:
vb@24
  1342
    case GPGME_PROTOCOL_DEFAULT:
vb@24
  1343
        *comm_type = PEP_ct_OpenPGP_unconfirmed;
vb@24
  1344
        break;
vb@24
  1345
    case GPGME_PROTOCOL_CMS:
vb@24
  1346
        *comm_type = PEP_ct_CMS_unconfirmed;
vb@24
  1347
        break;
vb@24
  1348
    default:
vb@24
  1349
        *comm_type = PEP_ct_unknown;
vb@74
  1350
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1351
        return PEP_STATUS_OK;
vb@24
  1352
    }
vb@24
  1353
vb@24
  1354
    switch (gpgme_error) {
vb@24
  1355
    case GPG_ERR_EOF:
vb@24
  1356
        break;
vb@24
  1357
    case GPG_ERR_NO_ERROR:
vb@24
  1358
        assert(key);
vb@24
  1359
        assert(key->subkeys);
vb@24
  1360
        for (gpgme_subkey_t sk = key->subkeys; sk != NULL; sk = sk->next) {
vb@24
  1361
            if (sk->length < 1024)
vb@24
  1362
                *comm_type = PEP_ct_key_too_short;
vb@24
  1363
            else if (
vb@24
  1364
                (
vb@24
  1365
                (sk->pubkey_algo == GPGME_PK_RSA)
vb@24
  1366
                || (sk->pubkey_algo == GPGME_PK_RSA_E)
vb@24
  1367
                || (sk->pubkey_algo == GPGME_PK_RSA_S)
vb@24
  1368
                )
vb@24
  1369
                && sk->length == 1024
vb@24
  1370
                )
vb@122
  1371
                *comm_type = PEP_ct_OpenPGP_weak_unconfirmed;
vb@24
  1372
vb@24
  1373
            if (sk->invalid) {
vb@24
  1374
                *comm_type = PEP_ct_key_b0rken;
vb@24
  1375
                break;
vb@24
  1376
            }
vb@24
  1377
            if (sk->expired) {
vb@24
  1378
                *comm_type = PEP_ct_key_expired;
vb@24
  1379
                break;
vb@24
  1380
            }
vb@24
  1381
            if (sk->revoked) {
vb@24
  1382
                *comm_type = PEP_ct_key_revoked;
vb@24
  1383
                break;
vb@24
  1384
            }
vb@24
  1385
        }
vb@24
  1386
        break;
vb@24
  1387
    case GPG_ERR_ENOMEM:
vb@74
  1388
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1389
        *comm_type = PEP_ct_unknown;
vb@24
  1390
        return PEP_OUT_OF_MEMORY;
vb@24
  1391
    default:
vb@74
  1392
        gpg.gpgme_op_keylist_end(session->ctx);
vb@46
  1393
        return PEP_UNKNOWN_ERROR;
vb@24
  1394
    };
vb@24
  1395
vb@74
  1396
    gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1397
vb@24
  1398
    return status;
vb@24
  1399
}
vb@200
  1400
vb@200
  1401
static PEP_STATUS find_single_key(
vb@200
  1402
        PEP_SESSION session,
vb@200
  1403
        const char *fpr,
vb@200
  1404
        gpgme_key_t *key
vb@200
  1405
    )
vb@200
  1406
{
vb@200
  1407
    gpgme_error_t gpgme_error;
vb@200
  1408
vb@200
  1409
    *key = NULL;
vb@200
  1410
vb@200
  1411
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
vb@200
  1412
    gpgme_error = _GPGERR(gpgme_error);
vb@200
  1413
    switch (gpgme_error) {
vb@200
  1414
    case GPG_ERR_NO_ERROR:
vb@200
  1415
        break;
vb@200
  1416
    case GPG_ERR_INV_VALUE:
vb@200
  1417
        assert(0);
vb@200
  1418
        return PEP_UNKNOWN_ERROR;
vb@200
  1419
    default:
vb@200
  1420
        return PEP_GET_KEY_FAILED;
vb@200
  1421
    };
vb@200
  1422
vb@200
  1423
    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, key);
vb@200
  1424
    gpgme_error = _GPGERR(gpgme_error);
vb@200
  1425
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@200
  1426
vb@200
  1427
    gpg.gpgme_op_keylist_end(session->ctx);
vb@200
  1428
vb@200
  1429
    return PEP_STATUS_OK;
vb@200
  1430
}
vb@200
  1431
vb@201
  1432
typedef struct _renew_state {
vb@211
  1433
    enum {
vb@200
  1434
        renew_command = 0,
vb@200
  1435
        renew_date,
vb@200
  1436
        renew_secret_key,
vb@200
  1437
        renew_command2,
vb@200
  1438
        renew_date2,
vb@200
  1439
        renew_quit,
vb@200
  1440
        renew_save,
vb@200
  1441
        renew_exit,
vb@200
  1442
        renew_error = -1
vb@200
  1443
    } state;
vb@201
  1444
    const char *date_ref;
vb@201
  1445
} renew_state;
vb@200
  1446
vb@201
  1447
static gpgme_error_t renew_fsm(
vb@200
  1448
        void *_handle,
vb@200
  1449
        gpgme_status_code_t statuscode,
vb@200
  1450
        const char *args,
vb@200
  1451
        int fd
vb@200
  1452
    )
vb@200
  1453
{
vb@201
  1454
    renew_state *handle = _handle;
vb@200
  1455
vb@200
  1456
    switch (handle->state) {
vb@200
  1457
        case renew_command:
vb@200
  1458
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1459
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1460
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1461
                    handle->state = renew_error;
vb@201
  1462
                    return GPG_ERR_GENERAL;
vb@201
  1463
                }
vb@223
  1464
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  1465
                handle->state = renew_date;
vb@200
  1466
            }
vb@200
  1467
            break;
vb@200
  1468
vb@200
  1469
        case renew_date:
vb@200
  1470
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1471
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  1472
                if (strcmp(args, "keygen.valid")) {
vb@201
  1473
                    handle->state = renew_error;
vb@201
  1474
                    return GPG_ERR_GENERAL;
vb@201
  1475
                }
vb@223
  1476
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  1477
                handle->state = renew_secret_key;
vb@200
  1478
            }
vb@200
  1479
            break;
vb@200
  1480
vb@200
  1481
        case renew_secret_key:
vb@200
  1482
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1483
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1484
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1485
                    handle->state = renew_error;
vb@201
  1486
                    return GPG_ERR_GENERAL;
vb@201
  1487
                }
vb@223
  1488
                gpg.gpgme_io_write(fd, "key 1\n", 6);
vb@200
  1489
                handle->state = renew_command2;
vb@200
  1490
            }
vb@200
  1491
            break;
vb@200
  1492
vb@200
  1493
        case renew_command2:
vb@200
  1494
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1495
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1496
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1497
                    handle->state = renew_error;
vb@201
  1498
                    return GPG_ERR_GENERAL;
vb@201
  1499
                }
vb@223
  1500
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  1501
                handle->state = renew_date2;
vb@200
  1502
            }
vb@200
  1503
            break;
vb@200
  1504
vb@200
  1505
        case renew_date2:
vb@200
  1506
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1507
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  1508
                if (strcmp(args, "keygen.valid")) {
vb@201
  1509
                    handle->state = renew_error;
vb@201
  1510
                    return GPG_ERR_GENERAL;
vb@201
  1511
                }
vb@223
  1512
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  1513
                handle->state = renew_quit;
vb@200
  1514
            }
vb@200
  1515
            break;
vb@200
  1516
vb@200
  1517
        case renew_quit:
vb@200
  1518
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1519
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1520
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1521
                    handle->state = renew_error;
vb@201
  1522
                    return GPG_ERR_GENERAL;
vb@201
  1523
                }
vb@223
  1524
                gpg.gpgme_io_write(fd, "quit\n", 5);
vb@200
  1525
                handle->state = renew_save;
vb@200
  1526
            }
vb@200
  1527
            break;
vb@200
  1528
vb@200
  1529
        case renew_save:
vb@200
  1530
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@201
  1531
                assert(strcmp(args, "keyedit.save.okay") == 0);
vb@201
  1532
                if (strcmp(args, "keyedit.save.okay")) {
vb@201
  1533
                    handle->state = renew_error;
vb@201
  1534
                    return GPG_ERR_GENERAL;
vb@201
  1535
                }
vb@223
  1536
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@200
  1537
                handle->state = renew_exit;
vb@200
  1538
            }
vb@200
  1539
            break;
vb@200
  1540
vb@200
  1541
        case renew_exit:
vb@200
  1542
            break;
vb@200
  1543
vb@200
  1544
        case renew_error:
vb@200
  1545
            return GPG_ERR_GENERAL;
vb@200
  1546
    }
vb@200
  1547
vb@200
  1548
    return GPG_ERR_NO_ERROR;
vb@200
  1549
}
vb@200
  1550
vb@200
  1551
static ssize_t _nullwriter(
vb@200
  1552
        void *_handle,
vb@200
  1553
        const void *buffer,
vb@200
  1554
        size_t size
vb@200
  1555
    )
vb@200
  1556
{
vb@200
  1557
    return size;
vb@200
  1558
}
vb@200
  1559
vb@201
  1560
PEP_STATUS pgp_renew_key(
vb@201
  1561
        PEP_SESSION session,
vb@201
  1562
        const char *fpr,
vb@201
  1563
        const timestamp *ts
vb@201
  1564
    )
vb@200
  1565
{
vb@200
  1566
    PEP_STATUS status = PEP_STATUS_OK;
vb@200
  1567
    gpgme_error_t gpgme_error;
vb@200
  1568
    gpgme_key_t key;
vb@200
  1569
    gpgme_data_t output;
vb@201
  1570
    renew_state handle;
vb@203
  1571
    char date_text[12];
vb@200
  1572
vb@200
  1573
    assert(session);
vb@200
  1574
    assert(fpr);
vb@200
  1575
vb@201
  1576
    memset(&handle, 0, sizeof(renew_state));
vb@203
  1577
    snprintf(date_text, 12, "%.4d-%.2d-%.2d\n", ts->tm_year + 1900,
vb@201
  1578
            ts->tm_mon + 1, ts->tm_mday);
vb@201
  1579
    handle.date_ref = date_text;
vb@200
  1580
vb@200
  1581
    status = find_single_key(session, fpr, &key);
vb@200
  1582
    if (status != PEP_STATUS_OK)
vb@200
  1583
        return status;
vb@200
  1584
vb@200
  1585
    struct gpgme_data_cbs data_cbs;
vb@200
  1586
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
vb@200
  1587
    data_cbs.write = _nullwriter;
vb@220
  1588
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
vb@200
  1589
vb@220
  1590
    gpgme_error = gpg.gpgme_op_edit(session->ctx, key, renew_fsm, &handle,
vb@200
  1591
            output);
vb@200
  1592
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@200
  1593
vb@200
  1594
    gpg.gpgme_data_release(output);
vb@200
  1595
    gpg.gpgme_key_unref(key);
vb@200
  1596
vb@200
  1597
    return PEP_STATUS_OK;
vb@200
  1598
}
vb@200
  1599
vb@211
  1600
typedef struct _revoke_state {
vb@211
  1601
    enum {
vb@211
  1602
        revoke_command = 0,
vb@211
  1603
        revoke_approve,
vb@211
  1604
        revoke_reason_code,
vb@211
  1605
        revoke_reason_text,
vb@211
  1606
        revoke_reason_ok,
vb@211
  1607
        revoke_quit,
vb@211
  1608
        revoke_save,
vb@211
  1609
        revoke_exit,
vb@211
  1610
        revoke_error = -1
vb@211
  1611
    } state;
vb@211
  1612
    const char *reason_ref;
vb@211
  1613
} revoke_state;
vb@211
  1614
vb@211
  1615
static bool isemptystring(const char *str)
vb@211
  1616
{
vb@211
  1617
    if (str == NULL)
vb@211
  1618
        return true;
vb@211
  1619
vb@211
  1620
    for (; str; str++) {
vb@211
  1621
        if (*str != ' ' && *str != '\t' && *str != '\n')
vb@211
  1622
            return false;
vb@211
  1623
    }
vb@211
  1624
vb@211
  1625
    return true;
vb@211
  1626
}
vb@211
  1627
vb@211
  1628
static gpgme_error_t revoke_fsm(
vb@211
  1629
        void *_handle,
vb@211
  1630
        gpgme_status_code_t statuscode,
vb@211
  1631
        const char *args,
vb@211
  1632
        int fd
vb@211
  1633
    )
vb@211
  1634
{
vb@211
  1635
    revoke_state *handle = _handle;
vb@211
  1636
vb@211
  1637
    switch (handle->state) {
vb@211
  1638
        case revoke_command:
vb@211
  1639
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1640
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@211
  1641
                if (strcmp(args, "keyedit.prompt")) {
vb@211
  1642
                    handle->state = revoke_error;
vb@211
  1643
                    return GPG_ERR_GENERAL;
vb@211
  1644
                }
vb@223
  1645
                gpg.gpgme_io_write(fd, "revkey\n", 7);
vb@211
  1646
                handle->state = revoke_approve;
vb@211
  1647
            }
vb@211
  1648
            break;
vb@211
  1649
vb@211
  1650
        case revoke_approve:
vb@211
  1651
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  1652
                assert(strcmp(args, "keyedit.revoke.subkey.okay") == 0);
vb@211
  1653
                if (strcmp(args, "keyedit.revoke.subkey.okay")) {
vb@211
  1654
                    handle->state = revoke_error;
vb@211
  1655
                    return GPG_ERR_GENERAL;
vb@211
  1656
                }
vb@223
  1657
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  1658
                handle->state = revoke_reason_code;
vb@211
  1659
            }
vb@211
  1660
            break;
vb@211
  1661
vb@211
  1662
        case revoke_reason_code:
vb@211
  1663
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1664
                assert(strcmp(args, "ask_revocation_reason.code") == 0);
vb@211
  1665
                if (strcmp(args, "ask_revocation_reason.code")) {
vb@211
  1666
                    handle->state = revoke_error;
vb@211
  1667
                    return GPG_ERR_GENERAL;
vb@211
  1668
                }
vb@223
  1669
                gpg.gpgme_io_write(fd, "1\n", 2);
vb@211
  1670
                handle->state = revoke_reason_text;
vb@211
  1671
            }
vb@211
  1672
            break;
vb@211
  1673
vb@211
  1674
        case revoke_reason_text:
vb@211
  1675
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1676
                assert(strcmp(args, "ask_revocation_reason.text") == 0);
vb@211
  1677
                if (strcmp(args, "ask_revocation_reason.text")) {
vb@211
  1678
                    handle->state = revoke_error;
vb@211
  1679
                    return GPG_ERR_GENERAL;
vb@211
  1680
                }
vb@213
  1681
                // BUG: issues when reason given
vb@213
  1682
                // Assertion failed: (gpg->cmd.code), function command_handler,
vb@213
  1683
                // file engine-gpg.c, line 662.
vb@213
  1684
                //
vb@213
  1685
                // if (isemptystring(handle->reason_ref)) {
vb@223
  1686
                    gpg.gpgme_io_write(fd, "\n", 1);
vb@213
  1687
                // }
vb@213
  1688
                // else {
vb@213
  1689
                //     size_t len = strlen(handle->reason_ref);
vb@223
  1690
                //     gpg.gpgme_io_write(fd, handle->reason_ref, len);
vb@213
  1691
                //     if (handle->reason_ref[len - 1] == '\n')
vb@223
  1692
                //         gpg.gpgme_io_write(fd, "\n", 1);
vb@213
  1693
                //     else
vb@223
  1694
                //         gpg.gpgme_io_write(fd, "\n\n", 2);
vb@213
  1695
                // }
vb@211
  1696
                handle->state = revoke_reason_ok;
vb@211
  1697
            }
vb@211
  1698
            break;
vb@211
  1699
vb@211
  1700
        case revoke_reason_ok:
vb@211
  1701
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  1702
                assert(strcmp(args, "ask_revocation_reason.okay") == 0);
vb@211
  1703
                if (strcmp(args, "ask_revocation_reason.okay")) {
vb@211
  1704
                    handle->state = revoke_error;
vb@211
  1705
                    return GPG_ERR_GENERAL;
vb@211
  1706
                }
vb@223
  1707
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  1708
                handle->state = revoke_quit;
vb@211
  1709
            }
vb@211
  1710
            break;
vb@211
  1711
vb@211
  1712
        case revoke_quit:
vb@211
  1713
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1714
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@211
  1715
                if (strcmp(args, "keyedit.prompt")) {
vb@211
  1716
                    handle->state = revoke_error;
vb@211
  1717
                    return GPG_ERR_GENERAL;
vb@211
  1718
                }
vb@223
  1719
                gpg.gpgme_io_write(fd, "quit\n", 5);
vb@211
  1720
                handle->state = revoke_save;
vb@211
  1721
            }
vb@211
  1722
            break;
vb@211
  1723
vb@211
  1724
        case revoke_save:
vb@211
  1725
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  1726
                assert(strcmp(args, "keyedit.save.okay") == 0);
vb@211
  1727
                if (strcmp(args, "keyedit.save.okay")) {
vb@211
  1728
                    handle->state = revoke_error;
vb@211
  1729
                    return GPG_ERR_GENERAL;
vb@211
  1730
                }
vb@223
  1731
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  1732
                handle->state = revoke_exit;
vb@211
  1733
            }
vb@211
  1734
            break;
vb@211
  1735
vb@211
  1736
        case revoke_exit:
vb@211
  1737
            break;
vb@211
  1738
vb@211
  1739
        case revoke_error:
vb@211
  1740
            return GPG_ERR_GENERAL;
vb@211
  1741
    }
vb@211
  1742
vb@211
  1743
    return GPG_ERR_NO_ERROR;
vb@211
  1744
}
vb@211
  1745
vb@211
  1746
PEP_STATUS pgp_revoke_key(
vb@211
  1747
        PEP_SESSION session,
vb@211
  1748
        const char *fpr,
vb@211
  1749
        const char *reason
vb@211
  1750
    )
vb@200
  1751
{
vb@200
  1752
    PEP_STATUS status = PEP_STATUS_OK;
vb@211
  1753
    gpgme_error_t gpgme_error;
vb@200
  1754
    gpgme_key_t key;
vb@211
  1755
    gpgme_data_t output;
vb@211
  1756
    revoke_state handle;
vb@211
  1757
vb@200
  1758
    assert(session);
vb@200
  1759
    assert(fpr);
vb@200
  1760
vb@211
  1761
    memset(&handle, 0, sizeof(revoke_state));
vb@213
  1762
    handle.reason_ref = reason;
vb@211
  1763
vb@200
  1764
    status = find_single_key(session, fpr, &key);
vb@200
  1765
    if (status != PEP_STATUS_OK)
vb@200
  1766
        return status;
vb@200
  1767
vb@211
  1768
    struct gpgme_data_cbs data_cbs;
vb@211
  1769
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
vb@211
  1770
    data_cbs.write = _nullwriter;
vb@220
  1771
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
vb@211
  1772
vb@220
  1773
    gpgme_error = gpg.gpgme_op_edit(session->ctx, key, revoke_fsm, &handle,
vb@211
  1774
            output);
vb@211
  1775
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@211
  1776
vb@211
  1777
    gpg.gpgme_data_release(output);
vb@211
  1778
    gpg.gpgme_key_unref(key);
vb@211
  1779
vb@200
  1780
    return PEP_STATUS_OK;
vb@200
  1781
}
vb@200
  1782
vb@214
  1783
PEP_STATUS pgp_key_expired(
vb@214
  1784
        PEP_SESSION session,
vb@214
  1785
        const char *fpr,
vb@214
  1786
        bool *expired
vb@214
  1787
    )
vb@214
  1788
{
vb@214
  1789
    PEP_STATUS status = PEP_STATUS_OK;
vb@214
  1790
    gpgme_key_t key;
vb@214
  1791
vb@214
  1792
    assert(session);
vb@214
  1793
    assert(fpr);
vb@214
  1794
    assert(expired);
vb@214
  1795
vb@214
  1796
    *expired = false;
vb@214
  1797
vb@214
  1798
    status = find_single_key(session, fpr, &key);
vb@214
  1799
    if (status != PEP_STATUS_OK)
vb@214
  1800
        return status;
vb@214
  1801
Edouard@621
  1802
    if (key && key->subkeys)
Edouard@620
  1803
    {
Edouard@620
  1804
        *expired = key->subkeys->expired;
Edouard@620
  1805
    }
Edouard@620
  1806
    else
Edouard@620
  1807
    {
Edouard@620
  1808
        status = PEP_KEY_NOT_FOUND;
Edouard@620
  1809
    }
Edouard@620
  1810
vb@214
  1811
    gpg.gpgme_key_unref(key);
Edouard@620
  1812
    return status;
vb@214
  1813
}
vb@214
  1814
Edouard@662
  1815
PEP_STATUS pgp_key_revoked(
Edouard@662
  1816
        PEP_SESSION session,
Edouard@662
  1817
        const char *fpr,
Edouard@662
  1818
        bool *revoked
Edouard@662
  1819
    )
Edouard@662
  1820
{
Edouard@662
  1821
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@662
  1822
    gpgme_key_t key;
Edouard@662
  1823
Edouard@662
  1824
    assert(session);
Edouard@662
  1825
    assert(fpr);
Edouard@662
  1826
    assert(revoked);
Edouard@662
  1827
Edouard@664
  1828
    *revoked = false;
Edouard@662
  1829
Edouard@662
  1830
    status = find_single_key(session, fpr, &key);
Edouard@662
  1831
    if (status != PEP_STATUS_OK)
Edouard@662
  1832
        return status;
Edouard@662
  1833
Edouard@662
  1834
    if (key && key->subkeys)
Edouard@662
  1835
    {
Edouard@664
  1836
        *revoked = key->subkeys->revoked;
Edouard@662
  1837
    }
Edouard@662
  1838
    else
Edouard@662
  1839
    {
Edouard@662
  1840
        status = PEP_KEY_NOT_FOUND;
Edouard@662
  1841
    }
Edouard@662
  1842
Edouard@662
  1843
    gpg.gpgme_key_unref(key);
Edouard@662
  1844
    return status;
Edouard@662
  1845
}
Edouard@662
  1846
vb@507
  1847
PEP_STATUS pgp_binary(const char **path)
vb@507
  1848
{
vb@507
  1849
    assert(path);
vb@507
  1850
    if (path == NULL)
vb@507
  1851
        return PEP_ILLEGAL_VALUE;
vb@507
  1852
vb@507
  1853
    *path = NULL;
vb@507
  1854
vb@507
  1855
    gpgme_engine_info_t info;
vb@507
  1856
    int err = gpg.gpgme_get_engine_info(&info);
vb@507
  1857
    assert(err == GPG_ERR_NO_ERROR);
vb@507
  1858
    if (err != GPG_ERR_NO_ERROR)
vb@507
  1859
        return PEP_OUT_OF_MEMORY;
vb@507
  1860
vb@507
  1861
    *path = info->file_name;
vb@507
  1862
vb@507
  1863
    return PEP_STATUS_OK;
vb@507
  1864
}
vb@507
  1865