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