src/pgp_gpg.c
author Roker <roker@pep-project.org>
Tue, 20 Sep 2016 15:54:57 +0200
branchroker-linux
changeset 1168 4d4e3ecbaf8b
parent 1050 a73bf9737993
child 1057 abff13590a4c
child 1126 129b561095a6
child 1187 8bbb80bb4323
permissions -rw-r--r--
merge "default" into my branch
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@1040
  1271
krista@1005
  1272
                // first subkey is primary key
krista@1005
  1273
                char* fpr = key->subkeys->fpr;
krista@1019
  1274
                char* uid = key->uids->uid;
krista@1019
  1275
                
krista@1005
  1276
                assert(fpr);
krista@1040
  1277
                assert(uid); // ??
krista@1031
  1278
                if (!fpr)
krista@1005
  1279
                    return PEP_GET_KEY_FAILED;
krista@1005
  1280
                
krista@1040
  1281
                PEP_STATUS key_status = PEP_GET_KEY_FAILED;
krista@1040
  1282
                
krista@1040
  1283
                bool key_revoked = false;
krista@1050
  1284
                                
krista@1050
  1285
                if (key->subkeys->revoked)
krista@1040
  1286
                    continue;
krista@1040
  1287
                
krista@1031
  1288
                pair = new_stringpair(fpr, uid);
krista@1019
  1289
krista@1031
  1290
                assert(pair);
krista@1005
  1291
                
krista@1031
  1292
                if (pair) {
krista@1031
  1293
                    list_curr = stringpair_list_add(list_curr, pair);
krista@1031
  1294
                    pair = NULL;
krista@1005
  1295
                    
krista@1005
  1296
                    assert(list_curr);
krista@1005
  1297
                    if (list_curr != NULL)
krista@1005
  1298
                        break;
krista@1005
  1299
                    else
krista@1031
  1300
                        free_stringpair(pair);
krista@1005
  1301
                }
krista@1031
  1302
                // else fallthrough (list_curr or pair wasn't allocateable)
krista@1005
  1303
            case GPG_ERR_ENOMEM:
krista@1031
  1304
                free_stringpair_list(_keyinfo_list);
krista@1005
  1305
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1005
  1306
                return PEP_OUT_OF_MEMORY;
krista@1005
  1307
            default:
krista@1005
  1308
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1005
  1309
                return PEP_UNKNOWN_ERROR;
krista@1005
  1310
        }
krista@1005
  1311
    } while (gpgme_error != GPG_ERR_EOF);
krista@1005
  1312
    
krista@1032
  1313
    if (_keyinfo_list->value == NULL) {
krista@1031
  1314
        free_stringpair_list(_keyinfo_list);
krista@1031
  1315
        _keyinfo_list = NULL;
krista@1005
  1316
    }
krista@1005
  1317
    
krista@1031
  1318
    *keyinfo_list = _keyinfo_list;
krista@1005
  1319
    
krista@1005
  1320
    return PEP_STATUS_OK;
krista@1005
  1321
}
krista@1005
  1322
vb@46
  1323
static void _switch_mode(pEpSession *session, gpgme_keylist_mode_t remove_mode,
vb@24
  1324
    gpgme_keylist_mode_t add_mode)
vb@24
  1325
{
vb@24
  1326
    gpgme_error_t gpgme_error;
vb@24
  1327
    gpgme_keylist_mode_t mode;
vb@24
  1328
vb@74
  1329
    mode = gpg.gpgme_get_keylist_mode(session->ctx);
vb@24
  1330
vb@24
  1331
    mode &= ~remove_mode;
vb@24
  1332
    mode |= add_mode;
vb@24
  1333
vb@74
  1334
    gpgme_error = gpg.gpgme_set_keylist_mode(session->ctx, mode);
vb@45
  1335
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1336
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1337
}
vb@24
  1338
vb@24
  1339
PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1340
{
vb@24
  1341
    gpgme_error_t gpgme_error;
vb@24
  1342
    gpgme_key_t key;
vb@24
  1343
vb@24
  1344
    assert(session);
vb@24
  1345
    assert(pattern);
vb@24
  1346
vb@46
  1347
    _switch_mode(session, GPGME_KEYLIST_MODE_LOCAL, GPGME_KEYLIST_MODE_EXTERN);
vb@24
  1348
vb@74
  1349
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
vb@45
  1350
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1351
    switch (gpgme_error) {
vb@24
  1352
    case GPG_ERR_NO_ERROR:
vb@24
  1353
        break;
vb@24
  1354
    case GPG_ERR_INV_VALUE:
vb@24
  1355
        assert(0);
vb@47
  1356
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1357
        return PEP_UNKNOWN_ERROR;
vb@24
  1358
    default:
vb@47
  1359
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1360
        return PEP_GET_KEY_FAILED;
vb@24
  1361
    };
vb@24
  1362
vb@47
  1363
    gpgme_ctx_t import_ctx;
vb@74
  1364
    gpgme_error = gpg.gpgme_new(&import_ctx);
vb@47
  1365
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@47
  1366
vb@24
  1367
    do {
vb@74
  1368
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1369
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1370
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1371
        switch (gpgme_error) {
vb@24
  1372
        case GPG_ERR_EOF:
vb@24
  1373
            break;
vb@24
  1374
        case GPG_ERR_NO_ERROR:
vb@24
  1375
        {
vb@24
  1376
            gpgme_error_t gpgme_error;
vb@24
  1377
            gpgme_key_t keys[2];
vb@24
  1378
vb@24
  1379
            keys[0] = key;
vb@24
  1380
            keys[1] = NULL;
vb@24
  1381
vb@74
  1382
            gpgme_error = gpg.gpgme_op_import_keys(import_ctx, keys);
vb@45
  1383
            gpgme_error = _GPGERR(gpgme_error);
vb@74
  1384
            gpg.gpgme_key_unref(key);
vb@24
  1385
            assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1386
            assert(gpgme_error != GPG_ERR_CONFLICT);
vb@24
  1387
        }
vb@24
  1388
            break;
vb@24
  1389
        case GPG_ERR_ENOMEM:
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@24
  1393
            return PEP_OUT_OF_MEMORY;
vb@24
  1394
        default:
vb@74
  1395
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1396
            gpg.gpgme_release(import_ctx);
vb@47
  1397
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@46
  1398
            return PEP_UNKNOWN_ERROR;
vb@24
  1399
        };
vb@24
  1400
    } while (gpgme_error != GPG_ERR_EOF);
vb@24
  1401
vb@74
  1402
    gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1403
    gpg.gpgme_release(import_ctx);
vb@47
  1404
    _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1405
    return PEP_STATUS_OK;
vb@24
  1406
}
vb@24
  1407
vb@24
  1408
PEP_STATUS pgp_find_keys(
vb@24
  1409
    PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
  1410
    )
vb@24
  1411
{
vb@24
  1412
    gpgme_error_t gpgme_error;
vb@24
  1413
    gpgme_key_t key;
vb@24
  1414
vb@24
  1415
    assert(session);
vb@24
  1416
    assert(pattern);
vb@24
  1417
    assert(keylist);
vb@24
  1418
vb@24
  1419
    *keylist = NULL;
vb@24
  1420
vb@74
  1421
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
vb@45
  1422
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1423
    switch (gpgme_error) {
vb@24
  1424
    case GPG_ERR_NO_ERROR:
vb@24
  1425
        break;
vb@24
  1426
    case GPG_ERR_INV_VALUE:
vb@24
  1427
        assert(0);
vb@24
  1428
        return PEP_UNKNOWN_ERROR;
vb@24
  1429
    default:
vb@74
  1430
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1431
        return PEP_GET_KEY_FAILED;
vb@24
  1432
    };
vb@24
  1433
roker@877
  1434
    stringlist_t *_keylist = new_stringlist(NULL);
vb@24
  1435
    stringlist_t *_k = _keylist;
vb@24
  1436
vb@24
  1437
    do {
vb@74
  1438
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1439
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1440
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1441
        switch (gpgme_error) {
vb@24
  1442
        case GPG_ERR_EOF:
vb@24
  1443
            break;
vb@24
  1444
        case GPG_ERR_NO_ERROR:
vb@24
  1445
            assert(key);
vb@24
  1446
            assert(key->subkeys);
roker@877
  1447
            char *fpr = key->subkeys->fpr;
vb@24
  1448
            assert(fpr);
vb@24
  1449
            _k = stringlist_add(_k, fpr);
vb@24
  1450
            assert(_k);
vb@24
  1451
            if (_k != NULL)
vb@24
  1452
                break;
vb@24
  1453
        case GPG_ERR_ENOMEM:
vb@24
  1454
            free_stringlist(_keylist);
vb@74
  1455
            gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1456
            return PEP_OUT_OF_MEMORY;
vb@24
  1457
        default:
vb@74
  1458
            gpg.gpgme_op_keylist_end(session->ctx);
vb@46
  1459
            return PEP_UNKNOWN_ERROR;
vb@24
  1460
        };
vb@24
  1461
    } while (gpgme_error != GPG_ERR_EOF);
vb@24
  1462
vb@74
  1463
    gpg.gpgme_op_keylist_end(session->ctx);
vb@523
  1464
    if (_keylist->value == NULL) {
vb@523
  1465
        free_stringlist(_keylist);
vb@523
  1466
        _keylist = NULL;
vb@523
  1467
    }
vb@24
  1468
    *keylist = _keylist;
vb@24
  1469
    return PEP_STATUS_OK;
vb@24
  1470
}
vb@24
  1471
vb@24
  1472
PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)
vb@24
  1473
{
vb@24
  1474
    gpgme_error_t gpgme_error;
vb@24
  1475
vb@24
  1476
    assert(session);
vb@24
  1477
    assert(pattern);
vb@24
  1478
vb@74
  1479
    gpgme_error = gpg.gpgme_op_export(session->ctx, pattern,
vb@24
  1480
        GPGME_EXPORT_MODE_EXTERN, NULL);
vb@45
  1481
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1482
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1483
    if (gpgme_error == GPG_ERR_NO_ERROR)
vb@24
  1484
        return PEP_STATUS_OK;
vb@24
  1485
    else
vb@24
  1486
        return PEP_CANNOT_SEND_KEY;
vb@24
  1487
}
vb@24
  1488
vb@24
  1489
PEP_STATUS pgp_get_key_rating(
vb@24
  1490
    PEP_SESSION session,
vb@24
  1491
    const char *fpr,
vb@24
  1492
    PEP_comm_type *comm_type
vb@24
  1493
    )
vb@24
  1494
{
vb@24
  1495
    PEP_STATUS status = PEP_STATUS_OK;
vb@24
  1496
    gpgme_error_t gpgme_error;
vb@24
  1497
    gpgme_key_t key;
vb@24
  1498
vb@24
  1499
    assert(session);
vb@24
  1500
    assert(fpr);
vb@24
  1501
    assert(comm_type);
vb@24
  1502
vb@24
  1503
    *comm_type = PEP_ct_unknown;
vb@24
  1504
vb@74
  1505
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
vb@45
  1506
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1507
    switch (gpgme_error) {
vb@24
  1508
    case GPG_ERR_NO_ERROR:
vb@24
  1509
        break;
vb@24
  1510
    case GPG_ERR_INV_VALUE:
vb@24
  1511
        assert(0);
vb@24
  1512
        return PEP_UNKNOWN_ERROR;
vb@24
  1513
    default:
vb@24
  1514
        return PEP_GET_KEY_FAILED;
vb@24
  1515
    };
vb@24
  1516
vb@74
  1517
    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1518
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1519
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1520
vb@24
  1521
    if (key == NULL) {
vb@74
  1522
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1523
        return PEP_KEY_NOT_FOUND;
vb@24
  1524
    }
vb@24
  1525
vb@24
  1526
    switch (key->protocol) {
vb@24
  1527
    case GPGME_PROTOCOL_OpenPGP:
vb@24
  1528
    case GPGME_PROTOCOL_DEFAULT:
vb@24
  1529
        *comm_type = PEP_ct_OpenPGP_unconfirmed;
vb@24
  1530
        break;
vb@24
  1531
    case GPGME_PROTOCOL_CMS:
vb@24
  1532
        *comm_type = PEP_ct_CMS_unconfirmed;
vb@24
  1533
        break;
vb@24
  1534
    default:
vb@24
  1535
        *comm_type = PEP_ct_unknown;
vb@74
  1536
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1537
        return PEP_STATUS_OK;
vb@24
  1538
    }
vb@24
  1539
vb@24
  1540
    switch (gpgme_error) {
vb@24
  1541
    case GPG_ERR_EOF:
vb@24
  1542
        break;
vb@24
  1543
    case GPG_ERR_NO_ERROR:
vb@24
  1544
        assert(key);
vb@24
  1545
        assert(key->subkeys);
vb@24
  1546
        for (gpgme_subkey_t sk = key->subkeys; sk != NULL; sk = sk->next) {
vb@24
  1547
            if (sk->length < 1024)
vb@24
  1548
                *comm_type = PEP_ct_key_too_short;
vb@24
  1549
            else if (
vb@24
  1550
                (
vb@24
  1551
                (sk->pubkey_algo == GPGME_PK_RSA)
vb@24
  1552
                || (sk->pubkey_algo == GPGME_PK_RSA_E)
vb@24
  1553
                || (sk->pubkey_algo == GPGME_PK_RSA_S)
vb@24
  1554
                )
vb@24
  1555
                && sk->length == 1024
vb@24
  1556
                )
vb@122
  1557
                *comm_type = PEP_ct_OpenPGP_weak_unconfirmed;
vb@24
  1558
vb@24
  1559
            if (sk->invalid) {
vb@24
  1560
                *comm_type = PEP_ct_key_b0rken;
vb@24
  1561
                break;
vb@24
  1562
            }
vb@24
  1563
            if (sk->expired) {
vb@24
  1564
                *comm_type = PEP_ct_key_expired;
vb@24
  1565
                break;
vb@24
  1566
            }
vb@24
  1567
            if (sk->revoked) {
vb@24
  1568
                *comm_type = PEP_ct_key_revoked;
vb@24
  1569
                break;
vb@24
  1570
            }
vb@24
  1571
        }
vb@24
  1572
        break;
vb@24
  1573
    case GPG_ERR_ENOMEM:
vb@74
  1574
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1575
        *comm_type = PEP_ct_unknown;
vb@24
  1576
        return PEP_OUT_OF_MEMORY;
vb@24
  1577
    default:
vb@74
  1578
        gpg.gpgme_op_keylist_end(session->ctx);
vb@46
  1579
        return PEP_UNKNOWN_ERROR;
vb@24
  1580
    };
vb@24
  1581
vb@74
  1582
    gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  1583
vb@24
  1584
    return status;
vb@24
  1585
}
vb@200
  1586
vb@200
  1587
static PEP_STATUS find_single_key(
vb@200
  1588
        PEP_SESSION session,
vb@200
  1589
        const char *fpr,
vb@200
  1590
        gpgme_key_t *key
vb@200
  1591
    )
vb@200
  1592
{
vb@200
  1593
    gpgme_error_t gpgme_error;
vb@200
  1594
vb@200
  1595
    *key = NULL;
vb@200
  1596
vb@200
  1597
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
vb@200
  1598
    gpgme_error = _GPGERR(gpgme_error);
vb@200
  1599
    switch (gpgme_error) {
vb@200
  1600
    case GPG_ERR_NO_ERROR:
vb@200
  1601
        break;
vb@200
  1602
    case GPG_ERR_INV_VALUE:
vb@200
  1603
        assert(0);
vb@200
  1604
        return PEP_UNKNOWN_ERROR;
vb@200
  1605
    default:
vb@200
  1606
        return PEP_GET_KEY_FAILED;
vb@200
  1607
    };
vb@200
  1608
vb@200
  1609
    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, key);
vb@200
  1610
    gpgme_error = _GPGERR(gpgme_error);
vb@200
  1611
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@200
  1612
vb@200
  1613
    gpg.gpgme_op_keylist_end(session->ctx);
vb@200
  1614
vb@200
  1615
    return PEP_STATUS_OK;
vb@200
  1616
}
vb@200
  1617
vb@201
  1618
typedef struct _renew_state {
vb@211
  1619
    enum {
vb@200
  1620
        renew_command = 0,
vb@200
  1621
        renew_date,
vb@200
  1622
        renew_secret_key,
vb@200
  1623
        renew_command2,
vb@200
  1624
        renew_date2,
vb@200
  1625
        renew_quit,
vb@200
  1626
        renew_save,
vb@200
  1627
        renew_exit,
vb@200
  1628
        renew_error = -1
vb@200
  1629
    } state;
vb@201
  1630
    const char *date_ref;
vb@201
  1631
} renew_state;
vb@200
  1632
vb@201
  1633
static gpgme_error_t renew_fsm(
vb@200
  1634
        void *_handle,
vb@200
  1635
        gpgme_status_code_t statuscode,
vb@200
  1636
        const char *args,
vb@200
  1637
        int fd
vb@200
  1638
    )
vb@200
  1639
{
vb@201
  1640
    renew_state *handle = _handle;
vb@200
  1641
vb@200
  1642
    switch (handle->state) {
vb@200
  1643
        case renew_command:
vb@200
  1644
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1645
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1646
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1647
                    handle->state = renew_error;
vb@201
  1648
                    return GPG_ERR_GENERAL;
vb@201
  1649
                }
vb@223
  1650
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  1651
                handle->state = renew_date;
vb@200
  1652
            }
vb@200
  1653
            break;
vb@200
  1654
vb@200
  1655
        case renew_date:
vb@200
  1656
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1657
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  1658
                if (strcmp(args, "keygen.valid")) {
vb@201
  1659
                    handle->state = renew_error;
vb@201
  1660
                    return GPG_ERR_GENERAL;
vb@201
  1661
                }
vb@223
  1662
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  1663
                handle->state = renew_secret_key;
vb@200
  1664
            }
vb@200
  1665
            break;
vb@200
  1666
vb@200
  1667
        case renew_secret_key:
vb@200
  1668
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1669
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1670
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1671
                    handle->state = renew_error;
vb@201
  1672
                    return GPG_ERR_GENERAL;
vb@201
  1673
                }
vb@223
  1674
                gpg.gpgme_io_write(fd, "key 1\n", 6);
vb@200
  1675
                handle->state = renew_command2;
vb@200
  1676
            }
vb@200
  1677
            break;
vb@200
  1678
vb@200
  1679
        case renew_command2:
vb@200
  1680
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1681
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1682
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1683
                    handle->state = renew_error;
vb@201
  1684
                    return GPG_ERR_GENERAL;
vb@201
  1685
                }
vb@223
  1686
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  1687
                handle->state = renew_date2;
vb@200
  1688
            }
vb@200
  1689
            break;
vb@200
  1690
vb@200
  1691
        case renew_date2:
vb@200
  1692
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1693
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  1694
                if (strcmp(args, "keygen.valid")) {
vb@201
  1695
                    handle->state = renew_error;
vb@201
  1696
                    return GPG_ERR_GENERAL;
vb@201
  1697
                }
vb@223
  1698
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  1699
                handle->state = renew_quit;
vb@200
  1700
            }
vb@200
  1701
            break;
vb@200
  1702
vb@200
  1703
        case renew_quit:
vb@200
  1704
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  1705
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  1706
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  1707
                    handle->state = renew_error;
vb@201
  1708
                    return GPG_ERR_GENERAL;
vb@201
  1709
                }
vb@223
  1710
                gpg.gpgme_io_write(fd, "quit\n", 5);
vb@200
  1711
                handle->state = renew_save;
vb@200
  1712
            }
vb@200
  1713
            break;
vb@200
  1714
vb@200
  1715
        case renew_save:
vb@200
  1716
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@201
  1717
                assert(strcmp(args, "keyedit.save.okay") == 0);
vb@201
  1718
                if (strcmp(args, "keyedit.save.okay")) {
vb@201
  1719
                    handle->state = renew_error;
vb@201
  1720
                    return GPG_ERR_GENERAL;
vb@201
  1721
                }
vb@223
  1722
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@200
  1723
                handle->state = renew_exit;
vb@200
  1724
            }
vb@200
  1725
            break;
vb@200
  1726
vb@200
  1727
        case renew_exit:
vb@200
  1728
            break;
vb@200
  1729
vb@200
  1730
        case renew_error:
vb@200
  1731
            return GPG_ERR_GENERAL;
vb@200
  1732
    }
vb@200
  1733
vb@200
  1734
    return GPG_ERR_NO_ERROR;
vb@200
  1735
}
vb@200
  1736
vb@200
  1737
static ssize_t _nullwriter(
vb@200
  1738
        void *_handle,
vb@200
  1739
        const void *buffer,
vb@200
  1740
        size_t size
vb@200
  1741
    )
vb@200
  1742
{
vb@200
  1743
    return size;
vb@200
  1744
}
vb@200
  1745
vb@201
  1746
PEP_STATUS pgp_renew_key(
vb@201
  1747
        PEP_SESSION session,
vb@201
  1748
        const char *fpr,
vb@201
  1749
        const timestamp *ts
vb@201
  1750
    )
vb@200
  1751
{
vb@200
  1752
    PEP_STATUS status = PEP_STATUS_OK;
vb@200
  1753
    gpgme_error_t gpgme_error;
vb@200
  1754
    gpgme_key_t key;
vb@200
  1755
    gpgme_data_t output;
vb@201
  1756
    renew_state handle;
vb@203
  1757
    char date_text[12];
vb@200
  1758
vb@200
  1759
    assert(session);
vb@200
  1760
    assert(fpr);
vb@200
  1761
vb@201
  1762
    memset(&handle, 0, sizeof(renew_state));
vb@203
  1763
    snprintf(date_text, 12, "%.4d-%.2d-%.2d\n", ts->tm_year + 1900,
vb@201
  1764
            ts->tm_mon + 1, ts->tm_mday);
vb@201
  1765
    handle.date_ref = date_text;
vb@200
  1766
vb@200
  1767
    status = find_single_key(session, fpr, &key);
vb@200
  1768
    if (status != PEP_STATUS_OK)
vb@200
  1769
        return status;
vb@200
  1770
vb@200
  1771
    struct gpgme_data_cbs data_cbs;
vb@200
  1772
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
vb@200
  1773
    data_cbs.write = _nullwriter;
vb@220
  1774
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
vb@200
  1775
vb@220
  1776
    gpgme_error = gpg.gpgme_op_edit(session->ctx, key, renew_fsm, &handle,
vb@200
  1777
            output);
vb@200
  1778
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@200
  1779
vb@200
  1780
    gpg.gpgme_data_release(output);
vb@200
  1781
    gpg.gpgme_key_unref(key);
vb@200
  1782
vb@200
  1783
    return PEP_STATUS_OK;
vb@200
  1784
}
vb@200
  1785
vb@211
  1786
typedef struct _revoke_state {
vb@211
  1787
    enum {
vb@211
  1788
        revoke_command = 0,
vb@211
  1789
        revoke_approve,
vb@211
  1790
        revoke_reason_code,
vb@211
  1791
        revoke_reason_text,
vb@211
  1792
        revoke_reason_ok,
vb@211
  1793
        revoke_quit,
vb@211
  1794
        revoke_save,
vb@211
  1795
        revoke_exit,
vb@211
  1796
        revoke_error = -1
vb@211
  1797
    } state;
vb@211
  1798
    const char *reason_ref;
vb@211
  1799
} revoke_state;
vb@211
  1800
roker@723
  1801
roker@723
  1802
/*** unused?
vb@211
  1803
static bool isemptystring(const char *str)
vb@211
  1804
{
vb@211
  1805
    if (str == NULL)
vb@211
  1806
        return true;
vb@211
  1807
vb@211
  1808
    for (; str; str++) {
vb@211
  1809
        if (*str != ' ' && *str != '\t' && *str != '\n')
vb@211
  1810
            return false;
vb@211
  1811
    }
vb@211
  1812
vb@211
  1813
    return true;
vb@211
  1814
}
roker@723
  1815
***/
roker@723
  1816
vb@211
  1817
vb@211
  1818
static gpgme_error_t revoke_fsm(
vb@211
  1819
        void *_handle,
vb@211
  1820
        gpgme_status_code_t statuscode,
vb@211
  1821
        const char *args,
vb@211
  1822
        int fd
vb@211
  1823
    )
vb@211
  1824
{
vb@211
  1825
    revoke_state *handle = _handle;
vb@211
  1826
vb@211
  1827
    switch (handle->state) {
vb@211
  1828
        case revoke_command:
vb@211
  1829
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1830
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@211
  1831
                if (strcmp(args, "keyedit.prompt")) {
vb@211
  1832
                    handle->state = revoke_error;
vb@211
  1833
                    return GPG_ERR_GENERAL;
vb@211
  1834
                }
vb@223
  1835
                gpg.gpgme_io_write(fd, "revkey\n", 7);
vb@211
  1836
                handle->state = revoke_approve;
vb@211
  1837
            }
vb@211
  1838
            break;
vb@211
  1839
vb@211
  1840
        case revoke_approve:
vb@211
  1841
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  1842
                assert(strcmp(args, "keyedit.revoke.subkey.okay") == 0);
vb@211
  1843
                if (strcmp(args, "keyedit.revoke.subkey.okay")) {
vb@211
  1844
                    handle->state = revoke_error;
vb@211
  1845
                    return GPG_ERR_GENERAL;
vb@211
  1846
                }
vb@223
  1847
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  1848
                handle->state = revoke_reason_code;
vb@211
  1849
            }
vb@211
  1850
            break;
vb@211
  1851
vb@211
  1852
        case revoke_reason_code:
vb@211
  1853
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1854
                assert(strcmp(args, "ask_revocation_reason.code") == 0);
vb@211
  1855
                if (strcmp(args, "ask_revocation_reason.code")) {
vb@211
  1856
                    handle->state = revoke_error;
vb@211
  1857
                    return GPG_ERR_GENERAL;
vb@211
  1858
                }
vb@223
  1859
                gpg.gpgme_io_write(fd, "1\n", 2);
vb@211
  1860
                handle->state = revoke_reason_text;
vb@211
  1861
            }
vb@211
  1862
            break;
vb@211
  1863
vb@211
  1864
        case revoke_reason_text:
vb@211
  1865
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1866
                assert(strcmp(args, "ask_revocation_reason.text") == 0);
vb@211
  1867
                if (strcmp(args, "ask_revocation_reason.text")) {
vb@211
  1868
                    handle->state = revoke_error;
vb@211
  1869
                    return GPG_ERR_GENERAL;
vb@211
  1870
                }
vb@213
  1871
                // BUG: issues when reason given
vb@213
  1872
                // Assertion failed: (gpg->cmd.code), function command_handler,
vb@213
  1873
                // file engine-gpg.c, line 662.
vb@213
  1874
                //
vb@213
  1875
                // if (isemptystring(handle->reason_ref)) {
vb@223
  1876
                    gpg.gpgme_io_write(fd, "\n", 1);
vb@213
  1877
                // }
vb@213
  1878
                // else {
vb@213
  1879
                //     size_t len = strlen(handle->reason_ref);
vb@223
  1880
                //     gpg.gpgme_io_write(fd, handle->reason_ref, len);
vb@213
  1881
                //     if (handle->reason_ref[len - 1] == '\n')
vb@223
  1882
                //         gpg.gpgme_io_write(fd, "\n", 1);
vb@213
  1883
                //     else
vb@223
  1884
                //         gpg.gpgme_io_write(fd, "\n\n", 2);
vb@213
  1885
                // }
vb@211
  1886
                handle->state = revoke_reason_ok;
vb@211
  1887
            }
vb@211
  1888
            break;
vb@211
  1889
vb@211
  1890
        case revoke_reason_ok:
vb@211
  1891
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  1892
                assert(strcmp(args, "ask_revocation_reason.okay") == 0);
vb@211
  1893
                if (strcmp(args, "ask_revocation_reason.okay")) {
vb@211
  1894
                    handle->state = revoke_error;
vb@211
  1895
                    return GPG_ERR_GENERAL;
vb@211
  1896
                }
vb@223
  1897
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  1898
                handle->state = revoke_quit;
vb@211
  1899
            }
vb@211
  1900
            break;
vb@211
  1901
vb@211
  1902
        case revoke_quit:
vb@211
  1903
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  1904
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@211
  1905
                if (strcmp(args, "keyedit.prompt")) {
vb@211
  1906
                    handle->state = revoke_error;
vb@211
  1907
                    return GPG_ERR_GENERAL;
vb@211
  1908
                }
vb@223
  1909
                gpg.gpgme_io_write(fd, "quit\n", 5);
vb@211
  1910
                handle->state = revoke_save;
vb@211
  1911
            }
vb@211
  1912
            break;
vb@211
  1913
vb@211
  1914
        case revoke_save:
vb@211
  1915
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@211
  1916
                assert(strcmp(args, "keyedit.save.okay") == 0);
vb@211
  1917
                if (strcmp(args, "keyedit.save.okay")) {
vb@211
  1918
                    handle->state = revoke_error;
vb@211
  1919
                    return GPG_ERR_GENERAL;
vb@211
  1920
                }
vb@223
  1921
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@211
  1922
                handle->state = revoke_exit;
vb@211
  1923
            }
vb@211
  1924
            break;
vb@211
  1925
vb@211
  1926
        case revoke_exit:
vb@211
  1927
            break;
vb@211
  1928
vb@211
  1929
        case revoke_error:
vb@211
  1930
            return GPG_ERR_GENERAL;
vb@211
  1931
    }
vb@211
  1932
vb@211
  1933
    return GPG_ERR_NO_ERROR;
vb@211
  1934
}
vb@211
  1935
vb@211
  1936
PEP_STATUS pgp_revoke_key(
vb@211
  1937
        PEP_SESSION session,
vb@211
  1938
        const char *fpr,
vb@211
  1939
        const char *reason
vb@211
  1940
    )
vb@200
  1941
{
vb@200
  1942
    PEP_STATUS status = PEP_STATUS_OK;
vb@211
  1943
    gpgme_error_t gpgme_error;
vb@200
  1944
    gpgme_key_t key;
vb@211
  1945
    gpgme_data_t output;
vb@211
  1946
    revoke_state handle;
vb@211
  1947
vb@200
  1948
    assert(session);
vb@200
  1949
    assert(fpr);
vb@200
  1950
vb@211
  1951
    memset(&handle, 0, sizeof(revoke_state));
vb@213
  1952
    handle.reason_ref = reason;
vb@211
  1953
vb@200
  1954
    status = find_single_key(session, fpr, &key);
vb@200
  1955
    if (status != PEP_STATUS_OK)
vb@200
  1956
        return status;
vb@200
  1957
vb@211
  1958
    struct gpgme_data_cbs data_cbs;
vb@211
  1959
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
vb@211
  1960
    data_cbs.write = _nullwriter;
vb@220
  1961
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
vb@211
  1962
vb@220
  1963
    gpgme_error = gpg.gpgme_op_edit(session->ctx, key, revoke_fsm, &handle,
vb@211
  1964
            output);
vb@211
  1965
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@211
  1966
vb@211
  1967
    gpg.gpgme_data_release(output);
vb@211
  1968
    gpg.gpgme_key_unref(key);
vb@211
  1969
vb@200
  1970
    return PEP_STATUS_OK;
vb@200
  1971
}
vb@200
  1972
vb@214
  1973
PEP_STATUS pgp_key_expired(
vb@214
  1974
        PEP_SESSION session,
vb@214
  1975
        const char *fpr,
Edouard@701
  1976
        const time_t when,
vb@214
  1977
        bool *expired
vb@214
  1978
    )
vb@214
  1979
{
vb@214
  1980
    PEP_STATUS status = PEP_STATUS_OK;
vb@214
  1981
    gpgme_key_t key;
vb@214
  1982
vb@214
  1983
    assert(session);
vb@214
  1984
    assert(fpr);
vb@214
  1985
    assert(expired);
vb@214
  1986
vb@214
  1987
    *expired = false;
vb@214
  1988
vb@214
  1989
    status = find_single_key(session, fpr, &key);
vb@214
  1990
    if (status != PEP_STATUS_OK)
vb@214
  1991
        return status;
vb@214
  1992
Edouard@701
  1993
    if ((key && key->expired) ||
Edouard@701
  1994
        (key && key->subkeys && key->subkeys->expired))
Edouard@620
  1995
    {
Edouard@701
  1996
        // Already marked expired
Edouard@701
  1997
        *expired = 1;
Edouard@701
  1998
    }
Edouard@701
  1999
    else if (key)
Edouard@701
  2000
    {
Edouard@701
  2001
        // Detect if will be expired
Edouard@701
  2002
        // i.e. Check that keys capabilities will
Edouard@701
  2003
        // not be expired at given time.
Edouard@701
  2004
        gpgme_subkey_t _sk;
Edouard@701
  2005
        bool crt_available = false;
Edouard@701
  2006
        bool sgn_available = false;
Edouard@701
  2007
        bool enc_available = false;
Edouard@701
  2008
        for (_sk = key->subkeys; _sk; _sk = _sk->next) {
Edouard@701
  2009
            if (_sk->expires > when) // not expired at that date ?
Edouard@701
  2010
            {
Edouard@701
  2011
                if (_sk->can_certify) crt_available = true;
Edouard@701
  2012
                if (_sk->can_sign) sgn_available = true;
Edouard@701
  2013
                if (_sk->can_encrypt) enc_available = true;
Edouard@701
  2014
                // Authenticate is not used here.
Edouard@701
  2015
            }
Edouard@701
  2016
        }
Edouard@701
  2017
        if(!(crt_available && sgn_available && enc_available))
Edouard@701
  2018
        {
Edouard@701
  2019
            *expired = 1;
Edouard@701
  2020
        }
Edouard@620
  2021
    }
Edouard@620
  2022
    else
Edouard@620
  2023
    {
Edouard@620
  2024
        status = PEP_KEY_NOT_FOUND;
Edouard@620
  2025
    }
Edouard@620
  2026
vb@214
  2027
    gpg.gpgme_key_unref(key);
Edouard@620
  2028
    return status;
vb@214
  2029
}
vb@214
  2030
Edouard@662
  2031
PEP_STATUS pgp_key_revoked(
Edouard@662
  2032
        PEP_SESSION session,
Edouard@662
  2033
        const char *fpr,
Edouard@662
  2034
        bool *revoked
Edouard@662
  2035
    )
Edouard@662
  2036
{
Edouard@662
  2037
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@662
  2038
    gpgme_key_t key;
Edouard@662
  2039
Edouard@662
  2040
    assert(session);
Edouard@662
  2041
    assert(fpr);
Edouard@662
  2042
    assert(revoked);
Edouard@662
  2043
Edouard@664
  2044
    *revoked = false;
Edouard@662
  2045
Edouard@662
  2046
    status = find_single_key(session, fpr, &key);
Edouard@662
  2047
    if (status != PEP_STATUS_OK)
Edouard@662
  2048
        return status;
Edouard@662
  2049
Edouard@662
  2050
    if (key && key->subkeys)
Edouard@662
  2051
    {
Edouard@664
  2052
        *revoked = key->subkeys->revoked;
Edouard@662
  2053
    }
Edouard@662
  2054
    else
Edouard@662
  2055
    {
Edouard@662
  2056
        status = PEP_KEY_NOT_FOUND;
Edouard@662
  2057
    }
Edouard@662
  2058
Edouard@662
  2059
    gpg.gpgme_key_unref(key);
Edouard@662
  2060
    return status;
Edouard@662
  2061
}
Edouard@662
  2062
vb@507
  2063
PEP_STATUS pgp_binary(const char **path)
vb@507
  2064
{
vb@507
  2065
    assert(path);
vb@507
  2066
    if (path == NULL)
vb@507
  2067
        return PEP_ILLEGAL_VALUE;
vb@507
  2068
vb@507
  2069
    *path = NULL;
vb@507
  2070
vb@507
  2071
    gpgme_engine_info_t info;
vb@507
  2072
    int err = gpg.gpgme_get_engine_info(&info);
vb@507
  2073
    assert(err == GPG_ERR_NO_ERROR);
vb@507
  2074
    if (err != GPG_ERR_NO_ERROR)
vb@507
  2075
        return PEP_OUT_OF_MEMORY;
vb@507
  2076
vb@507
  2077
    *path = info->file_name;
vb@507
  2078
vb@507
  2079
    return PEP_STATUS_OK;
vb@507
  2080
}
vb@507
  2081