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