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