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