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