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