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