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