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