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