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