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