src/pgp_gpg.c
author Krista Grothoff <krista@pep-project.org>
Thu, 18 Aug 2016 14:20:25 +0200
branchENGINE-73
changeset 1031 ed718acfd6fe
parent 1019 e38a6ec264bd
child 1032 b4004d33c2a2
permissions -rw-r--r--
ENGINE-73 #comment underlying netpgp and openpgp variants modified. netpgp now uses find_keys_do, because there's a convenient callback we can slip our modified functionality into. gpgme still uses a separate function, because no such callback mechanism is in there. We may want to add it to avoid duplication and clean this up at a later date.
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);
Edouard@728
  1086
            gpgme_import_status_t import;
Edouard@728
  1087
            for (import = gpgme_import_result->imports; 
Edouard@728
  1088
                 import; 
Edouard@728
  1089
                 import = import->next)
Edouard@728
  1090
             {
Edouard@728
  1091
                if (import &&
Edouard@728
  1092
                    import->result == GPG_ERR_NO_ERROR &&
Edouard@728
  1093
                    import->status & GPGME_IMPORT_SECRET )
Edouard@728
  1094
                {
Edouard@728
  1095
                    gpgme_key_t key = NULL;
Edouard@728
  1096
Edouard@728
  1097
                    gpgme_error = gpg.gpgme_get_key(session->ctx,
Edouard@728
  1098
                        import->fpr, &key, 0);
Edouard@728
  1099
                    gpgme_error = _GPGERR(gpgme_error);
Edouard@728
  1100
                    assert(gpgme_error != GPG_ERR_ENOMEM);
Edouard@728
  1101
                    if (gpgme_error == GPG_ERR_ENOMEM) {
Edouard@728
  1102
                        gpg.gpgme_data_release(dh);
Edouard@728
  1103
                        return PEP_OUT_OF_MEMORY;
Edouard@728
  1104
                    }
Edouard@728
  1105
                    
Edouard@728
  1106
                    if (gpgme_error == GPG_ERR_NO_ERROR &&  
Edouard@728
  1107
                        key && key->uids && 
Edouard@728
  1108
                        key->uids->email && key->uids->name)
Edouard@728
  1109
                    {
Edouard@728
  1110
                        pEp_identity *ident = new_identity(
Edouard@728
  1111
                             key->uids->email, import->fpr, NULL, key->uids->name);
Edouard@728
  1112
Edouard@728
  1113
                        gpg.gpgme_key_unref(key);
Edouard@728
  1114
Edouard@728
  1115
                        if (ident == NULL) {
Edouard@728
  1116
                            gpg.gpgme_data_release(dh);
Edouard@728
  1117
                            return PEP_OUT_OF_MEMORY;
Edouard@728
  1118
                        }
Edouard@728
  1119
Edouard@728
  1120
                        *private_idents = identity_list_add(*private_idents, ident);
Edouard@728
  1121
Edouard@728
  1122
                        if (*private_idents == NULL) {
Edouard@728
  1123
                            gpg.gpgme_data_release(dh);
Edouard@728
  1124
                            return PEP_OUT_OF_MEMORY;
Edouard@728
  1125
                        }
Edouard@728
  1126
                    }
Edouard@728
  1127
                    else 
Edouard@728
  1128
                    {
Edouard@728
  1129
                        gpg.gpgme_key_unref(key);
Edouard@728
  1130
                        gpg.gpgme_data_release(dh);
Edouard@728
  1131
                        return PEP_UNKNOWN_ERROR;
Edouard@728
  1132
                    }
Edouard@728
  1133
                }
Edouard@728
  1134
            }
Edouard@728
  1135
        }
vb@24
  1136
        break;
vb@24
  1137
    case GPG_ERR_INV_VALUE:
vb@24
  1138
        assert(0);
vb@74
  1139
        gpg.gpgme_data_release(dh);
vb@24
  1140
        return PEP_UNKNOWN_ERROR;
vb@24
  1141
    case GPG_ERR_NO_DATA:
vb@74
  1142
        gpg.gpgme_data_release(dh);
vb@24
  1143
        return PEP_ILLEGAL_VALUE;
vb@24
  1144
    default:
vb@24
  1145
        assert(0);
vb@74
  1146
        gpg.gpgme_data_release(dh);
vb@24
  1147
        return PEP_UNKNOWN_ERROR;
vb@24
  1148
    }
vb@24
  1149
vb@74
  1150
    gpg.gpgme_data_release(dh);
vb@24
  1151
    return PEP_STATUS_OK;
vb@24
  1152
}
vb@24
  1153
Edouard@170
  1154
PEP_STATUS pgp_export_keydata(
vb@24
  1155
    PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
  1156
    )
vb@24
  1157
{
vb@24
  1158
    gpgme_error_t gpgme_error;
vb@24
  1159
    gpgme_data_t dh;
vb@24
  1160
    size_t _size;
vb@24
  1161
    char *buffer;
vb@24
  1162
    int reading;
vb@24
  1163
vb@24
  1164
    assert(session);
vb@24
  1165
    assert(fpr);
vb@24
  1166
    assert(key_data);
vb@24
  1167
    assert(size);
vb@24
  1168
vb@74
  1169
    gpgme_error = gpg.gpgme_data_new(&dh);
vb@45
  1170
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1171
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1172
    switch (gpgme_error) {
vb@24
  1173
    case GPG_ERR_NO_ERROR:
vb@24
  1174
        break;
vb@24
  1175
    case GPG_ERR_ENOMEM:
vb@24
  1176
        return PEP_OUT_OF_MEMORY;
vb@24
  1177
    case GPG_ERR_INV_VALUE:
vb@24
  1178
        assert(0);
vb@24
  1179
        return PEP_UNKNOWN_ERROR;
vb@24
  1180
    default:
vb@24
  1181
        assert(0);
vb@24
  1182
        return PEP_UNKNOWN_ERROR;
vb@24
  1183
    }
vb@24
  1184
vb@74
  1185
    gpgme_error = gpg.gpgme_op_export(session->ctx, fpr,
vb@24
  1186
        GPGME_EXPORT_MODE_MINIMAL, dh);
vb@45
  1187
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1188
    switch (gpgme_error) {
vb@24
  1189
    case GPG_ERR_NO_ERROR:
vb@24
  1190
        break;
vb@24
  1191
    case GPG_ERR_EOF:
vb@74
  1192
        gpg.gpgme_data_release(dh);
vb@24
  1193
        return PEP_KEY_NOT_FOUND;
vb@24
  1194
    case GPG_ERR_INV_VALUE:
vb@24
  1195
        assert(0);
vb@74
  1196
        gpg.gpgme_data_release(dh);
vb@24
  1197
        return PEP_UNKNOWN_ERROR;
vb@24
  1198
    default:
vb@24
  1199
        assert(0);
vb@74
  1200
        gpg.gpgme_data_release(dh);
vb@24
  1201
        return PEP_UNKNOWN_ERROR;
vb@24
  1202
    };
vb@24
  1203
vb@74
  1204
    _size = gpg.gpgme_data_seek(dh, 0, SEEK_END);
vb@24
  1205
    assert(_size != -1);
vb@74
  1206
    gpg.gpgme_data_seek(dh, 0, SEEK_SET);
vb@24
  1207
vb@24
  1208
    buffer = malloc(_size + 1);
vb@24
  1209
    assert(buffer);
vb@24
  1210
    if (buffer == NULL) {
vb@74
  1211
        gpg.gpgme_data_release(dh);
vb@24
  1212
        return PEP_OUT_OF_MEMORY;
vb@24
  1213
    }
vb@24
  1214
vb@74
  1215
    reading = gpg.gpgme_data_read(dh, buffer, _size);
vb@24
  1216
    assert(_size == reading);
vb@24
  1217
vb@24
  1218
    // safeguard for the naive user
vb@24
  1219
    buffer[_size] = 0;
vb@24
  1220
vb@24
  1221
    *key_data = buffer;
vb@24
  1222
    *size = _size;
vb@24
  1223
vb@74
  1224
    gpg.gpgme_data_release(dh);
vb@24
  1225
    return PEP_STATUS_OK;
vb@24
  1226
}
vb@24
  1227
krista@1031
  1228
PEP_STATUS pgp_list_keyinfo(PEP_SESSION session, const char* pattern, 
krista@1031
  1229
                            stringpair_list_t** keyinfo_list)
krista@1031
  1230
{    
krista@1005
  1231
    gpgme_error_t gpgme_error;
krista@1005
  1232
    assert(session);
krista@1031
  1233
    assert(keyinfo_list);
krista@1005
  1234
    
krista@1031
  1235
    if (!session || !keyinfo_list)
krista@1005
  1236
        return PEP_ILLEGAL_VALUE;
krista@1005
  1237
    
krista@1031
  1238
    *keyinfo_list = NULL;
krista@1005
  1239
    
krista@1031
  1240
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
krista@1005
  1241
    gpgme_error = _GPGERR(gpgme_error);
krista@1005
  1242
    
krista@1005
  1243
    switch(gpgme_error) {
krista@1005
  1244
        case GPG_ERR_NO_ERROR:
krista@1005
  1245
            break;
krista@1005
  1246
        case GPG_ERR_INV_VALUE:
krista@1005
  1247
            assert(0);
krista@1005
  1248
            return PEP_UNKNOWN_ERROR;
krista@1005
  1249
        default:
krista@1005
  1250
            gpg.gpgme_op_keylist_end(session->ctx);
krista@1005
  1251
            return PEP_GET_KEY_FAILED;        
krista@1005
  1252
    };
krista@1005
  1253
    
krista@1005
  1254
    gpgme_key_t key;
krista@1031
  1255
    stringpair_list_t* _keyinfo_list = new_stringpair_list(NULL);
krista@1031
  1256
    stringpair_list_t* list_curr = _keyinfo_list;
krista@1031
  1257
    stringpair_t* pair = NULL;
krista@1019
  1258
        
krista@1005
  1259
    do {
krista@1005
  1260
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
krista@1005
  1261
        gpgme_error = _GPGERR(gpgme_error);
krista@1005
  1262
      
krista@1005
  1263
        switch(gpgme_error) {
krista@1005
  1264
            case GPG_ERR_EOF:
krista@1005
  1265
                break;
krista@1005
  1266
            case GPG_ERR_NO_ERROR:
krista@1005
  1267
                assert(key);
krista@1005
  1268
                assert(key->subkeys);
krista@1005
  1269
                if (!key || !key->subkeys)
krista@1005
  1270
                    return PEP_GET_KEY_FAILED;
krista@1005
  1271
               
krista@1005
  1272
                // first subkey is primary key
krista@1005
  1273
                char* fpr = key->subkeys->fpr;
krista@1031
  1274
//                char* primary_email = key->uids->email;
krista@1031
  1275
//                char* uname = key->uids->name;
krista@1019
  1276
                char* uid = key->uids->uid;
krista@1019
  1277
                
krista@1005
  1278
                assert(fpr);
krista@1031
  1279
                assert(uid);
krista@1031
  1280
                if (!fpr)
krista@1005
  1281
                    return PEP_GET_KEY_FAILED;
krista@1005
  1282
                
krista@1031
  1283
                pair = new_stringpair(fpr, uid);
krista@1019
  1284
krista@1031
  1285
                assert(pair);
krista@1005
  1286
                
krista@1031
  1287
                if (pair) {
krista@1031
  1288
                    list_curr = stringpair_list_add(list_curr, pair);
krista@1031
  1289
                    pair = NULL;
krista@1005
  1290
                    
krista@1005
  1291
                    assert(list_curr);
krista@1005
  1292
                    if (list_curr != NULL)
krista@1005
  1293
                        break;
krista@1005
  1294
                    else
krista@1031
  1295
                        free_stringpair(pair);
krista@1005
  1296
                }
krista@1031
  1297
                // else fallthrough (list_curr or pair wasn't allocateable)
krista@1005
  1298
            case GPG_ERR_ENOMEM:
krista@1031
  1299
                free_stringpair_list(_keyinfo_list);
krista@1005
  1300
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1005
  1301
                return PEP_OUT_OF_MEMORY;
krista@1005
  1302
            default:
krista@1005
  1303
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1005
  1304
                return PEP_UNKNOWN_ERROR;
krista@1005
  1305
        }
krista@1005
  1306
    } while (gpgme_error != GPG_ERR_EOF);
krista@1005
  1307
    
krista@1031
  1308
    if (_keyinfo_list->ident == NULL) {
krista@1031
  1309
        free_stringpair_list(_keyinfo_list);
krista@1031
  1310
        _keyinfo_list = NULL;
krista@1005
  1311
    }
krista@1005
  1312
    
krista@1031
  1313
    *keyinfo_list = _keyinfo_list;
krista@1005
  1314
    
krista@1005
  1315
    return PEP_STATUS_OK;
krista@1005
  1316
}
krista@1005
  1317
vb@46
  1318
static void _switch_mode(pEpSession *session, gpgme_keylist_mode_t remove_mode,
vb@24
  1319
    gpgme_keylist_mode_t add_mode)
vb@24
  1320
{
vb@24
  1321
    gpgme_error_t gpgme_error;
vb@24
  1322
    gpgme_keylist_mode_t mode;
vb@24
  1323
vb@74
  1324
    mode = gpg.gpgme_get_keylist_mode(session->ctx);
vb@24
  1325
vb@24
  1326
    mode &= ~remove_mode;
vb@24
  1327
    mode |= add_mode;
vb@24
  1328
vb@74
  1329
    gpgme_error = gpg.gpgme_set_keylist_mode(session->ctx, mode);
vb@45
  1330
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1331
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1332
}
vb@24
  1333
vb@24
  1334
PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1335
{
vb@24
  1336
    gpgme_error_t gpgme_error;
vb@24
  1337
    gpgme_key_t key;
vb@24
  1338
vb@24
  1339
    assert(session);
vb@24
  1340
    assert(pattern);
vb@24
  1341
vb@46
  1342
    _switch_mode(session, GPGME_KEYLIST_MODE_LOCAL, GPGME_KEYLIST_MODE_EXTERN);
vb@24
  1343
vb@74
  1344
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
vb@45
  1345
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1346
    switch (gpgme_error) {
vb@24
  1347
    case GPG_ERR_NO_ERROR:
vb@24
  1348
        break;
vb@24
  1349
    case GPG_ERR_INV_VALUE:
vb@24
  1350
        assert(0);
vb@47
  1351
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1352
        return PEP_UNKNOWN_ERROR;
vb@24
  1353
    default:
vb@47
  1354
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1355
        return PEP_GET_KEY_FAILED;
vb@24
  1356
    };
vb@24
  1357
vb@47
  1358
    gpgme_ctx_t import_ctx;
vb@74
  1359
    gpgme_error = gpg.gpgme_new(&import_ctx);
vb@47
  1360
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@47
  1361
vb@24
  1362
    do {
vb@74
  1363
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1364
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1365
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1366
        switch (gpgme_error) {
vb@24
  1367
        case GPG_ERR_EOF:
vb@24
  1368
            break;
vb@24
  1369
        case GPG_ERR_NO_ERROR:
vb@24
  1370
        {
vb@24
  1371
            gpgme_error_t gpgme_error;
vb@24
  1372
            gpgme_key_t keys[2];
vb@24
  1373
vb@24
  1374
            keys[0] = key;
vb@24
  1375
            keys[1] = NULL;
vb@24
  1376
vb@74
  1377
            gpgme_error = gpg.gpgme_op_import_keys(import_ctx, keys);
vb@45
  1378
            gpgme_error = _GPGERR(gpgme_error);
vb@74
  1379
            gpg.gpgme_key_unref(key);
vb@24
  1380
            assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1381
            assert(gpgme_error != GPG_ERR_CONFLICT);
vb@24
  1382
        }
vb@24
  1383
            break;
vb@24
  1384
        case GPG_ERR_ENOMEM:
vb@74
  1385
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1386
            gpg.gpgme_release(import_ctx);
vb@47
  1387
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1388
            return PEP_OUT_OF_MEMORY;
vb@24
  1389
        default:
vb@74
  1390
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1391
            gpg.gpgme_release(import_ctx);
vb@47
  1392
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@46
  1393
            return PEP_UNKNOWN_ERROR;
vb@24
  1394
        };
vb@24
  1395
    } while (gpgme_error != GPG_ERR_EOF);
vb@24
  1396
vb@74
  1397
    gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1398
    gpg.gpgme_release(import_ctx);
vb@47
  1399
    _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1400
    return PEP_STATUS_OK;
vb@24
  1401
}
vb@24
  1402
vb@24
  1403
PEP_STATUS pgp_find_keys(
vb@24
  1404
    PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
  1405
    )
vb@24
  1406
{
vb@24
  1407
    gpgme_error_t gpgme_error;
vb@24
  1408
    gpgme_key_t key;
vb@24
  1409
vb@24
  1410
    assert(session);
vb@24
  1411
    assert(pattern);
vb@24
  1412
    assert(keylist);
vb@24
  1413
vb@24
  1414
    *keylist = NULL;
vb@24
  1415
vb@74
  1416
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
vb@45
  1417
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1418
    switch (gpgme_error) {
vb@24
  1419
    case GPG_ERR_NO_ERROR:
vb@24
  1420
        break;
vb@24
  1421
    case GPG_ERR_INV_VALUE:
vb@24
  1422
        assert(0);
vb@24
  1423
        return PEP_UNKNOWN_ERROR;
vb@24
  1424
    default:
vb@74
  1425
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1426
        return PEP_GET_KEY_FAILED;
vb@24
  1427
    };
vb@24
  1428
roker@877
  1429
    stringlist_t *_keylist = new_stringlist(NULL);
vb@24
  1430
    stringlist_t *_k = _keylist;
vb@24
  1431
vb@24
  1432
    do {
vb@74
  1433
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1434
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1435
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1436
        switch (gpgme_error) {
vb@24
  1437
        case GPG_ERR_EOF:
vb@24
  1438
            break;
vb@24
  1439
        case GPG_ERR_NO_ERROR:
vb@24
  1440
            assert(key);
vb@24
  1441
            assert(key->subkeys);
roker@877
  1442
            char *fpr = key->subkeys->fpr;
vb@24
  1443
            assert(fpr);
vb@24
  1444
            _k = stringlist_add(_k, fpr);
vb@24
  1445
            assert(_k);
vb@24
  1446
            if (_k != NULL)
vb@24
  1447
                break;
vb@24
  1448
        case GPG_ERR_ENOMEM:
vb@24
  1449
            free_stringlist(_keylist);
vb@74
  1450
            gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1451
            return PEP_OUT_OF_MEMORY;
vb@24
  1452
        default:
vb@74
  1453
            gpg.gpgme_op_keylist_end(session->ctx);
vb@46
  1454
            return PEP_UNKNOWN_ERROR;
vb@24
  1455
        };
vb@24
  1456
    } while (gpgme_error != GPG_ERR_EOF);
vb@24
  1457
vb@74
  1458
    gpg.gpgme_op_keylist_end(session->ctx);
vb@523
  1459
    if (_keylist->value == NULL) {
vb@523
  1460
        free_stringlist(_keylist);
vb@523
  1461
        _keylist = NULL;
vb@523
  1462
    }
vb@24
  1463
    *keylist = _keylist;
vb@24
  1464
    return PEP_STATUS_OK;
vb@24
  1465
}
vb@24
  1466
vb@24
  1467
PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)
vb@24
  1468
{
vb@24
  1469
    gpgme_error_t gpgme_error;
vb@24
  1470
vb@24
  1471
    assert(session);
vb@24
  1472
    assert(pattern);
vb@24
  1473
vb@74
  1474
    gpgme_error = gpg.gpgme_op_export(session->ctx, pattern,
vb@24
  1475
        GPGME_EXPORT_MODE_EXTERN, NULL);
vb@45
  1476
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1477
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1478
    if (gpgme_error == GPG_ERR_NO_ERROR)
vb@24
  1479
        return PEP_STATUS_OK;
vb@24
  1480
    else
vb@24
  1481
        return PEP_CANNOT_SEND_KEY;
vb@24
  1482
}
vb@24
  1483
vb@24
  1484
PEP_STATUS pgp_get_key_rating(
vb@24
  1485
    PEP_SESSION session,
vb@24
  1486
    const char *fpr,
vb@24
  1487
    PEP_comm_type *comm_type
vb@24
  1488
    )
vb@24
  1489
{
vb@24
  1490
    PEP_STATUS status = PEP_STATUS_OK;
vb@24
  1491
    gpgme_error_t gpgme_error;
vb@24
  1492
    gpgme_key_t key;
vb@24
  1493
vb@24
  1494
    assert(session);
vb@24
  1495
    assert(fpr);
vb@24
  1496
    assert(comm_type);
vb@24
  1497
vb@24
  1498
    *comm_type = PEP_ct_unknown;
vb@24
  1499
vb@74
  1500
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
vb@45
  1501
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1502
    switch (gpgme_error) {
vb@24
  1503
    case GPG_ERR_NO_ERROR:
vb@24
  1504
        break;
vb@24
  1505
    case GPG_ERR_INV_VALUE:
vb@24
  1506
        assert(0);
vb@24
  1507
        return PEP_UNKNOWN_ERROR;
vb@24
  1508
    default:
vb@24
  1509
        return PEP_GET_KEY_FAILED;
vb@24
  1510
    };
vb@24
  1511
vb@74
  1512
    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1513
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1514
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1515
vb@24
  1516
    if (key == NULL) {
vb@74
  1517
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1518
        return PEP_KEY_NOT_FOUND;
vb@24
  1519
    }
vb@24
  1520
vb@24
  1521
    switch (key->protocol) {
vb@24
  1522
    case GPGME_PROTOCOL_OpenPGP:
vb@24
  1523
    case GPGME_PROTOCOL_DEFAULT:
vb@24
  1524
        *comm_type = PEP_ct_OpenPGP_unconfirmed;
vb@24
  1525
        break;
vb@24
  1526
    case GPGME_PROTOCOL_CMS:
vb@24
  1527
        *comm_type = PEP_ct_CMS_unconfirmed;
vb@24
  1528
        break;
vb@24
  1529
    default:
vb@24
  1530
        *comm_type = PEP_ct_unknown;
vb@74
  1531
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1532
        return PEP_STATUS_OK;
vb@24
  1533
    }
vb@24
  1534
vb@24
  1535
    switch (gpgme_error) {
vb@24
  1536
    case GPG_ERR_EOF:
vb@24
  1537
        break;
vb@24
  1538
    case GPG_ERR_NO_ERROR:
vb@24
  1539
        assert(key);
vb@24
  1540
        assert(key->subkeys);
vb@24
  1541
        for (gpgme_subkey_t sk = key->subkeys; sk != NULL; sk = sk->next) {
vb@24
  1542
            if (sk->length < 1024)
vb@24
  1543
                *comm_type = PEP_ct_key_too_short;
vb@24
  1544
            else if (
vb@24
  1545
                (
vb@24
  1546
                (sk->pubkey_algo == GPGME_PK_RSA)
vb@24
  1547
                || (sk->pubkey_algo == GPGME_PK_RSA_E)
vb@24
  1548
                || (sk->pubkey_algo == GPGME_PK_RSA_S)
vb@24
  1549
                )
vb@24
  1550
                && sk->length == 1024
vb@24
  1551
                )
vb@122
  1552
                *comm_type = PEP_ct_OpenPGP_weak_unconfirmed;
vb@24
  1553
vb@24
  1554
            if (sk->invalid) {
vb@24
  1555
                *comm_type = PEP_ct_key_b0rken;
vb@24
  1556
                break;
vb@24
  1557
            }
vb@24
  1558
            if (sk->expired) {
vb@24
  1559
                *comm_type = PEP_ct_key_expired;
vb@24
  1560
                break;
vb@24
  1561
            }
vb@24
  1562
            if (sk->revoked) {
vb@24
  1563
                *comm_type = PEP_ct_key_revoked;
vb@24
  1564
                break;
vb@24
  1565
            }
vb@24
  1566
        }
vb@24
  1567
        break;
vb@24
  1568
    case GPG_ERR_ENOMEM:
vb@74
  1569
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1570
        *comm_type = PEP_ct_unknown;
vb@24
  1571
        return PEP_OUT_OF_MEMORY;
vb@24
  1572
    default:
vb@74
  1573
        gpg.gpgme_op_keylist_end(session->ctx);
vb@46
  1574
        return PEP_UNKNOWN_ERROR;
vb@24
  1575
    };
vb@24
  1576
vb@74
  1577
    gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1578
vb@24
  1579
    return status;
vb@24
  1580
}
vb@200
  1581
vb@200
  1582
static PEP_STATUS find_single_key(
vb@200
  1583
        PEP_SESSION session,
vb@200
  1584
        const char *fpr,
vb@200
  1585
        gpgme_key_t *key
vb@200
  1586
    )
vb@200
  1587
{
vb@200
  1588
    gpgme_error_t gpgme_error;
vb@200
  1589
vb@200
  1590
    *key = NULL;
vb@200
  1591
vb@200
  1592
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
vb@200
  1593
    gpgme_error = _GPGERR(gpgme_error);
vb@200
  1594
    switch (gpgme_error) {
vb@200
  1595
    case GPG_ERR_NO_ERROR:
vb@200
  1596
        break;
vb@200
  1597
    case GPG_ERR_INV_VALUE:
vb@200
  1598
        assert(0);
vb@200
  1599
        return PEP_UNKNOWN_ERROR;
vb@200
  1600
    default:
vb@200
  1601
        return PEP_GET_KEY_FAILED;
vb@200
  1602
    };
vb@200
  1603
vb@200
  1604
    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, key);
vb@200
  1605
    gpgme_error = _GPGERR(gpgme_error);
vb@200
  1606
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@200
  1607
vb@200
  1608
    gpg.gpgme_op_keylist_end(session->ctx);
vb@200
  1609
vb@200
  1610
    return PEP_STATUS_OK;
vb@200
  1611
}
vb@200
  1612
vb@201
  1613
typedef struct _renew_state {
vb@211
  1614
    enum {
vb@200
  1615
        renew_command = 0,
vb@200
  1616
        renew_date,
vb@200
  1617
        renew_secret_key,
vb@200
  1618
        renew_command2,
vb@200
  1619
        renew_date2,
vb@200
  1620
        renew_quit,
vb@200
  1621
        renew_save,
vb@200
  1622
        renew_exit,
vb@200
  1623
        renew_error = -1
vb@200
  1624
    } state;
vb@201
  1625
    const char *date_ref;
vb@201
  1626
} renew_state;
vb@200
  1627
vb@201
  1628
static gpgme_error_t renew_fsm(
vb@200
  1629
        void *_handle,
vb@200
  1630
        gpgme_status_code_t statuscode,
vb@200
  1631
        const char *args,
vb@200
  1632
        int fd
vb@200
  1633
    )
vb@200
  1634
{
vb@201
  1635
    renew_state *handle = _handle;
vb@200
  1636
vb@200
  1637
    switch (handle->state) {
vb@200
  1638
        case renew_command:
vb@200
  1639
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1640
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1641
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1642
                    handle->state = renew_error;
vb@201
  1643
                    return GPG_ERR_GENERAL;
vb@201
  1644
                }
vb@223
  1645
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  1646
                handle->state = renew_date;
vb@200
  1647
            }
vb@200
  1648
            break;
vb@200
  1649
vb@200
  1650
        case renew_date:
vb@200
  1651
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1652
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  1653
                if (strcmp(args, "keygen.valid")) {
vb@201
  1654
                    handle->state = renew_error;
vb@201
  1655
                    return GPG_ERR_GENERAL;
vb@201
  1656
                }
vb@223
  1657
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  1658
                handle->state = renew_secret_key;
vb@200
  1659
            }
vb@200
  1660
            break;
vb@200
  1661
vb@200
  1662
        case renew_secret_key:
vb@200
  1663
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1664
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1665
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1666
                    handle->state = renew_error;
vb@201
  1667
                    return GPG_ERR_GENERAL;
vb@201
  1668
                }
vb@223
  1669
                gpg.gpgme_io_write(fd, "key 1\n", 6);
vb@200
  1670
                handle->state = renew_command2;
vb@200
  1671
            }
vb@200
  1672
            break;
vb@200
  1673
vb@200
  1674
        case renew_command2:
vb@200
  1675
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1676
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1677
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1678
                    handle->state = renew_error;
vb@201
  1679
                    return GPG_ERR_GENERAL;
vb@201
  1680
                }
vb@223
  1681
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  1682
                handle->state = renew_date2;
vb@200
  1683
            }
vb@200
  1684
            break;
vb@200
  1685
vb@200
  1686
        case renew_date2:
vb@200
  1687
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1688
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  1689
                if (strcmp(args, "keygen.valid")) {
vb@201
  1690
                    handle->state = renew_error;
vb@201
  1691
                    return GPG_ERR_GENERAL;
vb@201
  1692
                }
vb@223
  1693
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  1694
                handle->state = renew_quit;
vb@200
  1695
            }
vb@200
  1696
            break;
vb@200
  1697
vb@200
  1698
        case renew_quit:
vb@200
  1699
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1700
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1701
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1702
                    handle->state = renew_error;
vb@201
  1703
                    return GPG_ERR_GENERAL;
vb@201
  1704
                }
vb@223
  1705
                gpg.gpgme_io_write(fd, "quit\n", 5);
vb@200
  1706
                handle->state = renew_save;
vb@200
  1707
            }
vb@200
  1708
            break;
vb@200
  1709
vb@200
  1710
        case renew_save:
vb@200
  1711
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@201
  1712
                assert(strcmp(args, "keyedit.save.okay") == 0);
vb@201
  1713
                if (strcmp(args, "keyedit.save.okay")) {
vb@201
  1714
                    handle->state = renew_error;
vb@201
  1715
                    return GPG_ERR_GENERAL;
vb@201
  1716
                }
vb@223
  1717
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@200
  1718
                handle->state = renew_exit;
vb@200
  1719
            }
vb@200
  1720
            break;
vb@200
  1721
vb@200
  1722
        case renew_exit:
vb@200
  1723
            break;
vb@200
  1724
vb@200
  1725
        case renew_error:
vb@200
  1726
            return GPG_ERR_GENERAL;
vb@200
  1727
    }
vb@200
  1728
vb@200
  1729
    return GPG_ERR_NO_ERROR;
vb@200
  1730
}
vb@200
  1731
vb@200
  1732
static ssize_t _nullwriter(
vb@200
  1733
        void *_handle,
vb@200
  1734
        const void *buffer,
vb@200
  1735
        size_t size
vb@200
  1736
    )
vb@200
  1737
{
vb@200
  1738
    return size;
vb@200
  1739
}
vb@200
  1740
vb@201
  1741
PEP_STATUS pgp_renew_key(
vb@201
  1742
        PEP_SESSION session,
vb@201
  1743
        const char *fpr,
vb@201
  1744
        const timestamp *ts
vb@201
  1745
    )
vb@200
  1746
{
vb@200
  1747
    PEP_STATUS status = PEP_STATUS_OK;
vb@200
  1748
    gpgme_error_t gpgme_error;
vb@200
  1749
    gpgme_key_t key;
vb@200
  1750
    gpgme_data_t output;
vb@201
  1751
    renew_state handle;
vb@203
  1752
    char date_text[12];
vb@200
  1753
vb@200
  1754
    assert(session);
vb@200
  1755
    assert(fpr);
vb@200
  1756
vb@201
  1757
    memset(&handle, 0, sizeof(renew_state));
vb@203
  1758
    snprintf(date_text, 12, "%.4d-%.2d-%.2d\n", ts->tm_year + 1900,
vb@201
  1759
            ts->tm_mon + 1, ts->tm_mday);
vb@201
  1760
    handle.date_ref = date_text;
vb@200
  1761
vb@200
  1762
    status = find_single_key(session, fpr, &key);
vb@200
  1763
    if (status != PEP_STATUS_OK)
vb@200
  1764
        return status;
vb@200
  1765
vb@200
  1766
    struct gpgme_data_cbs data_cbs;
vb@200
  1767
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
vb@200
  1768
    data_cbs.write = _nullwriter;
vb@220
  1769
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
vb@200
  1770
vb@220
  1771
    gpgme_error = gpg.gpgme_op_edit(session->ctx, key, renew_fsm, &handle,
vb@200
  1772
            output);
vb@200
  1773
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@200
  1774
vb@200
  1775
    gpg.gpgme_data_release(output);
vb@200
  1776
    gpg.gpgme_key_unref(key);
vb@200
  1777
vb@200
  1778
    return PEP_STATUS_OK;
vb@200
  1779
}
vb@200
  1780
vb@211
  1781
typedef struct _revoke_state {
vb@211
  1782
    enum {
vb@211
  1783
        revoke_command = 0,
vb@211
  1784
        revoke_approve,
vb@211
  1785
        revoke_reason_code,
vb@211
  1786
        revoke_reason_text,
vb@211
  1787
        revoke_reason_ok,
vb@211
  1788
        revoke_quit,
vb@211
  1789
        revoke_save,
vb@211
  1790
        revoke_exit,
vb@211
  1791
        revoke_error = -1
vb@211
  1792
    } state;
vb@211
  1793
    const char *reason_ref;
vb@211
  1794
} revoke_state;
vb@211
  1795
roker@723
  1796
roker@723
  1797
/*** unused?
vb@211
  1798
static bool isemptystring(const char *str)
vb@211
  1799
{
vb@211
  1800
    if (str == NULL)
vb@211
  1801
        return true;
vb@211
  1802
vb@211
  1803
    for (; str; str++) {
vb@211
  1804
        if (*str != ' ' && *str != '\t' && *str != '\n')
vb@211
  1805
            return false;
vb@211
  1806
    }
vb@211
  1807
vb@211
  1808
    return true;
vb@211
  1809
}
roker@723
  1810
***/
roker@723
  1811
vb@211
  1812
vb@211
  1813
static gpgme_error_t revoke_fsm(
vb@211
  1814
        void *_handle,
vb@211
  1815
        gpgme_status_code_t statuscode,
vb@211
  1816
        const char *args,
vb@211
  1817
        int fd
vb@211
  1818
    )
vb@211
  1819
{
vb@211
  1820
    revoke_state *handle = _handle;
vb@211
  1821
vb@211
  1822
    switch (handle->state) {
vb@211
  1823
        case revoke_command:
vb@211
  1824
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1825
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@211
  1826
                if (strcmp(args, "keyedit.prompt")) {
vb@211
  1827
                    handle->state = revoke_error;
vb@211
  1828
                    return GPG_ERR_GENERAL;
vb@211
  1829
                }
vb@223
  1830
                gpg.gpgme_io_write(fd, "revkey\n", 7);
vb@211
  1831
                handle->state = revoke_approve;
vb@211
  1832
            }
vb@211
  1833
            break;
vb@211
  1834
vb@211
  1835
        case revoke_approve:
vb@211
  1836
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  1837
                assert(strcmp(args, "keyedit.revoke.subkey.okay") == 0);
vb@211
  1838
                if (strcmp(args, "keyedit.revoke.subkey.okay")) {
vb@211
  1839
                    handle->state = revoke_error;
vb@211
  1840
                    return GPG_ERR_GENERAL;
vb@211
  1841
                }
vb@223
  1842
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  1843
                handle->state = revoke_reason_code;
vb@211
  1844
            }
vb@211
  1845
            break;
vb@211
  1846
vb@211
  1847
        case revoke_reason_code:
vb@211
  1848
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1849
                assert(strcmp(args, "ask_revocation_reason.code") == 0);
vb@211
  1850
                if (strcmp(args, "ask_revocation_reason.code")) {
vb@211
  1851
                    handle->state = revoke_error;
vb@211
  1852
                    return GPG_ERR_GENERAL;
vb@211
  1853
                }
vb@223
  1854
                gpg.gpgme_io_write(fd, "1\n", 2);
vb@211
  1855
                handle->state = revoke_reason_text;
vb@211
  1856
            }
vb@211
  1857
            break;
vb@211
  1858
vb@211
  1859
        case revoke_reason_text:
vb@211
  1860
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1861
                assert(strcmp(args, "ask_revocation_reason.text") == 0);
vb@211
  1862
                if (strcmp(args, "ask_revocation_reason.text")) {
vb@211
  1863
                    handle->state = revoke_error;
vb@211
  1864
                    return GPG_ERR_GENERAL;
vb@211
  1865
                }
vb@213
  1866
                // BUG: issues when reason given
vb@213
  1867
                // Assertion failed: (gpg->cmd.code), function command_handler,
vb@213
  1868
                // file engine-gpg.c, line 662.
vb@213
  1869
                //
vb@213
  1870
                // if (isemptystring(handle->reason_ref)) {
vb@223
  1871
                    gpg.gpgme_io_write(fd, "\n", 1);
vb@213
  1872
                // }
vb@213
  1873
                // else {
vb@213
  1874
                //     size_t len = strlen(handle->reason_ref);
vb@223
  1875
                //     gpg.gpgme_io_write(fd, handle->reason_ref, len);
vb@213
  1876
                //     if (handle->reason_ref[len - 1] == '\n')
vb@223
  1877
                //         gpg.gpgme_io_write(fd, "\n", 1);
vb@213
  1878
                //     else
vb@223
  1879
                //         gpg.gpgme_io_write(fd, "\n\n", 2);
vb@213
  1880
                // }
vb@211
  1881
                handle->state = revoke_reason_ok;
vb@211
  1882
            }
vb@211
  1883
            break;
vb@211
  1884
vb@211
  1885
        case revoke_reason_ok:
vb@211
  1886
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  1887
                assert(strcmp(args, "ask_revocation_reason.okay") == 0);
vb@211
  1888
                if (strcmp(args, "ask_revocation_reason.okay")) {
vb@211
  1889
                    handle->state = revoke_error;
vb@211
  1890
                    return GPG_ERR_GENERAL;
vb@211
  1891
                }
vb@223
  1892
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  1893
                handle->state = revoke_quit;
vb@211
  1894
            }
vb@211
  1895
            break;
vb@211
  1896
vb@211
  1897
        case revoke_quit:
vb@211
  1898
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1899
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@211
  1900
                if (strcmp(args, "keyedit.prompt")) {
vb@211
  1901
                    handle->state = revoke_error;
vb@211
  1902
                    return GPG_ERR_GENERAL;
vb@211
  1903
                }
vb@223
  1904
                gpg.gpgme_io_write(fd, "quit\n", 5);
vb@211
  1905
                handle->state = revoke_save;
vb@211
  1906
            }
vb@211
  1907
            break;
vb@211
  1908
vb@211
  1909
        case revoke_save:
vb@211
  1910
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  1911
                assert(strcmp(args, "keyedit.save.okay") == 0);
vb@211
  1912
                if (strcmp(args, "keyedit.save.okay")) {
vb@211
  1913
                    handle->state = revoke_error;
vb@211
  1914
                    return GPG_ERR_GENERAL;
vb@211
  1915
                }
vb@223
  1916
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  1917
                handle->state = revoke_exit;
vb@211
  1918
            }
vb@211
  1919
            break;
vb@211
  1920
vb@211
  1921
        case revoke_exit:
vb@211
  1922
            break;
vb@211
  1923
vb@211
  1924
        case revoke_error:
vb@211
  1925
            return GPG_ERR_GENERAL;
vb@211
  1926
    }
vb@211
  1927
vb@211
  1928
    return GPG_ERR_NO_ERROR;
vb@211
  1929
}
vb@211
  1930
vb@211
  1931
PEP_STATUS pgp_revoke_key(
vb@211
  1932
        PEP_SESSION session,
vb@211
  1933
        const char *fpr,
vb@211
  1934
        const char *reason
vb@211
  1935
    )
vb@200
  1936
{
vb@200
  1937
    PEP_STATUS status = PEP_STATUS_OK;
vb@211
  1938
    gpgme_error_t gpgme_error;
vb@200
  1939
    gpgme_key_t key;
vb@211
  1940
    gpgme_data_t output;
vb@211
  1941
    revoke_state handle;
vb@211
  1942
vb@200
  1943
    assert(session);
vb@200
  1944
    assert(fpr);
vb@200
  1945
vb@211
  1946
    memset(&handle, 0, sizeof(revoke_state));
vb@213
  1947
    handle.reason_ref = reason;
vb@211
  1948
vb@200
  1949
    status = find_single_key(session, fpr, &key);
vb@200
  1950
    if (status != PEP_STATUS_OK)
vb@200
  1951
        return status;
vb@200
  1952
vb@211
  1953
    struct gpgme_data_cbs data_cbs;
vb@211
  1954
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
vb@211
  1955
    data_cbs.write = _nullwriter;
vb@220
  1956
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
vb@211
  1957
vb@220
  1958
    gpgme_error = gpg.gpgme_op_edit(session->ctx, key, revoke_fsm, &handle,
vb@211
  1959
            output);
vb@211
  1960
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@211
  1961
vb@211
  1962
    gpg.gpgme_data_release(output);
vb@211
  1963
    gpg.gpgme_key_unref(key);
vb@211
  1964
vb@200
  1965
    return PEP_STATUS_OK;
vb@200
  1966
}
vb@200
  1967
vb@214
  1968
PEP_STATUS pgp_key_expired(
vb@214
  1969
        PEP_SESSION session,
vb@214
  1970
        const char *fpr,
Edouard@701
  1971
        const time_t when,
vb@214
  1972
        bool *expired
vb@214
  1973
    )
vb@214
  1974
{
vb@214
  1975
    PEP_STATUS status = PEP_STATUS_OK;
vb@214
  1976
    gpgme_key_t key;
vb@214
  1977
vb@214
  1978
    assert(session);
vb@214
  1979
    assert(fpr);
vb@214
  1980
    assert(expired);
vb@214
  1981
vb@214
  1982
    *expired = false;
vb@214
  1983
vb@214
  1984
    status = find_single_key(session, fpr, &key);
vb@214
  1985
    if (status != PEP_STATUS_OK)
vb@214
  1986
        return status;
vb@214
  1987
Edouard@701
  1988
    if ((key && key->expired) ||
Edouard@701
  1989
        (key && key->subkeys && key->subkeys->expired))
Edouard@620
  1990
    {
Edouard@701
  1991
        // Already marked expired
Edouard@701
  1992
        *expired = 1;
Edouard@701
  1993
    }
Edouard@701
  1994
    else if (key)
Edouard@701
  1995
    {
Edouard@701
  1996
        // Detect if will be expired
Edouard@701
  1997
        // i.e. Check that keys capabilities will
Edouard@701
  1998
        // not be expired at given time.
Edouard@701
  1999
        gpgme_subkey_t _sk;
Edouard@701
  2000
        bool crt_available = false;
Edouard@701
  2001
        bool sgn_available = false;
Edouard@701
  2002
        bool enc_available = false;
Edouard@701
  2003
        for (_sk = key->subkeys; _sk; _sk = _sk->next) {
Edouard@701
  2004
            if (_sk->expires > when) // not expired at that date ?
Edouard@701
  2005
            {
Edouard@701
  2006
                if (_sk->can_certify) crt_available = true;
Edouard@701
  2007
                if (_sk->can_sign) sgn_available = true;
Edouard@701
  2008
                if (_sk->can_encrypt) enc_available = true;
Edouard@701
  2009
                // Authenticate is not used here.
Edouard@701
  2010
            }
Edouard@701
  2011
        }
Edouard@701
  2012
        if(!(crt_available && sgn_available && enc_available))
Edouard@701
  2013
        {
Edouard@701
  2014
            *expired = 1;
Edouard@701
  2015
        }
Edouard@620
  2016
    }
Edouard@620
  2017
    else
Edouard@620
  2018
    {
Edouard@620
  2019
        status = PEP_KEY_NOT_FOUND;
Edouard@620
  2020
    }
Edouard@620
  2021
vb@214
  2022
    gpg.gpgme_key_unref(key);
Edouard@620
  2023
    return status;
vb@214
  2024
}
vb@214
  2025
Edouard@662
  2026
PEP_STATUS pgp_key_revoked(
Edouard@662
  2027
        PEP_SESSION session,
Edouard@662
  2028
        const char *fpr,
Edouard@662
  2029
        bool *revoked
Edouard@662
  2030
    )
Edouard@662
  2031
{
Edouard@662
  2032
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@662
  2033
    gpgme_key_t key;
Edouard@662
  2034
Edouard@662
  2035
    assert(session);
Edouard@662
  2036
    assert(fpr);
Edouard@662
  2037
    assert(revoked);
Edouard@662
  2038
Edouard@664
  2039
    *revoked = false;
Edouard@662
  2040
Edouard@662
  2041
    status = find_single_key(session, fpr, &key);
Edouard@662
  2042
    if (status != PEP_STATUS_OK)
Edouard@662
  2043
        return status;
Edouard@662
  2044
Edouard@662
  2045
    if (key && key->subkeys)
Edouard@662
  2046
    {
Edouard@664
  2047
        *revoked = key->subkeys->revoked;
Edouard@662
  2048
    }
Edouard@662
  2049
    else
Edouard@662
  2050
    {
Edouard@662
  2051
        status = PEP_KEY_NOT_FOUND;
Edouard@662
  2052
    }
Edouard@662
  2053
Edouard@662
  2054
    gpg.gpgme_key_unref(key);
Edouard@662
  2055
    return status;
Edouard@662
  2056
}
Edouard@662
  2057
vb@507
  2058
PEP_STATUS pgp_binary(const char **path)
vb@507
  2059
{
vb@507
  2060
    assert(path);
vb@507
  2061
    if (path == NULL)
vb@507
  2062
        return PEP_ILLEGAL_VALUE;
vb@507
  2063
vb@507
  2064
    *path = NULL;
vb@507
  2065
vb@507
  2066
    gpgme_engine_info_t info;
vb@507
  2067
    int err = gpg.gpgme_get_engine_info(&info);
vb@507
  2068
    assert(err == GPG_ERR_NO_ERROR);
vb@507
  2069
    if (err != GPG_ERR_NO_ERROR)
vb@507
  2070
        return PEP_OUT_OF_MEMORY;
vb@507
  2071
vb@507
  2072
    *path = info->file_name;
vb@507
  2073
vb@507
  2074
    return PEP_STATUS_OK;
vb@507
  2075
}
vb@507
  2076