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