src/pgp_gpg.c
author Krista Bennett <krista@pep-project.org>
Fri, 04 May 2018 16:30:21 +0200
branchlocal_cpptest
changeset 2652 43b913f99a27
parent 2553 f66efd53d8b1
child 2681 52835b311e85
child 2677 a1ade141a1d1
permissions -rw-r--r--
Shelving broken things to break other things
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
vb@219
     4
#include "platform.h"
vb@130
     5
#include "pEp_internal.h"
vb@24
     6
#include "pgp_gpg.h"
vb@24
     7
vb@78
     8
#include <limits.h>
vb@78
     9
vb@62
    10
#include "wrappers.h"
vb@62
    11
vb@45
    12
#define _GPGERR(X) ((X) & 0xffffL)
vb@45
    13
vb@76
    14
static void *gpgme;
vb@76
    15
static struct gpg_s gpg;
vb@74
    16
krista@763
    17
static bool ensure_config_values(stringlist_t *keys, stringlist_t *values, const char* config_file_path)
vb@24
    18
{
vb@24
    19
    static char buf[MAX_LINELENGTH];
vb@78
    20
    int r;
vb@78
    21
    stringlist_t *_k;
vb@78
    22
    stringlist_t *_v;
vb@79
    23
    unsigned int i;
vb@78
    24
    unsigned int found = 0;
vb@62
    25
roker@1559
    26
    FILE *f = Fopen(config_file_path, "r");
vb@63
    27
    if (f == NULL && errno == ENOMEM)
vb@62
    28
        return false;
vb@24
    29
vb@24
    30
    if (f != NULL) {
vb@78
    31
        int length = stringlist_length(keys);
vb@79
    32
        unsigned int n = (1 << length) - 1;
vb@78
    33
krista@846
    34
        // make sure we 1) have the same number of keys and values
krista@846
    35
        // and 2) we don't have more key/value pairs than
krista@846
    36
        // the size of the bitfield used to hold the indices
krista@846
    37
        // of key/value pairs matching keys in the config file.
vb@78
    38
        assert(length <= sizeof(unsigned int) * CHAR_BIT);
vb@78
    39
        assert(length == stringlist_length(values));
krista@846
    40
        if (!(length == stringlist_length(values) &&
krista@846
    41
              length <= sizeof(unsigned int) * CHAR_BIT)) {
edouard@1803
    42
            Fclose(f);
krista@1426
    43
krista@846
    44
            return false;
krista@846
    45
        }
krista@1426
    46
vb@62
    47
        do {
roker@1559
    48
            char * s = Fgets(buf, MAX_LINELENGTH, f);
vb@78
    49
            if (!feof(f)) {
vb@78
    50
                assert(s);
vb@78
    51
                if (s == NULL)
vb@78
    52
                    return false;
vb@62
    53
vb@78
    54
                if (s && !feof(f)) {
vb@80
    55
                    char * rest;
vb@80
    56
                    char * t = strtok_r(s, " ", &rest);
vb@78
    57
                    for (i = 1, _k = keys, _v = values; _k != NULL;
vb@78
    58
                            _k = _k->next, _v = _v->next, i <<= 1) {
vb@80
    59
                        if (t && strncmp(t, _k->value, strlen(_k->value)) == 0)
vb@78
    60
                            found |= i;
vb@78
    61
vb@78
    62
                        if (i == n) {
vb@78
    63
                            r = Fclose(f);
edouard@1803
    64
                            if (r != 0)
edouard@1803
    65
                                return false;
vb@78
    66
                            return true;
vb@78
    67
                        }
vb@78
    68
                    }
vb@24
    69
                }
vb@24
    70
            }
vb@62
    71
        } while (!feof(f));
krista@763
    72
        f = Freopen(config_file_path, "a", f);
vb@24
    73
    }
vb@24
    74
    else {
krista@763
    75
        f = Fopen(config_file_path, "w");
vb@24
    76
    }
vb@24
    77
vb@24
    78
    assert(f);
vb@24
    79
    if (f == NULL)
vb@24
    80
        return false;
vb@24
    81
vb@78
    82
    for (i = 1, _k = keys, _v = values; _k != NULL; _k = _k->next,
vb@78
    83
            _v = _v->next, i <<= 1) {
vb@78
    84
        if ((found & i) == 0) {
vb@79
    85
            r = Fprintf(f, "%s %s\n", _k->value, _v->value);
vb@79
    86
            assert(r >= 0);
edouard@1803
    87
            if(r<0)
edouard@1803
    88
                return false;
vb@78
    89
        }
vb@78
    90
    }
vb@62
    91
vb@63
    92
    r = Fclose(f);
vb@62
    93
    assert(r == 0);
edouard@1803
    94
    if (r != 0)
edouard@1803
    95
        return false;
vb@24
    96
vb@24
    97
    return true;
vb@24
    98
}
vb@24
    99
krista@2057
   100
char* _undot_address(const char* address) {
krista@2057
   101
    if (!address)
krista@2057
   102
        return NULL;
krista@2057
   103
    
krista@2057
   104
    int addr_len = strlen(address);
krista@2057
   105
    const char* at = strstr(address, "@");
krista@2057
   106
    
krista@2057
   107
    if (!at)
krista@2057
   108
        at = address + addr_len;
krista@2057
   109
        
krista@2066
   110
    char* retval = calloc(1, addr_len + 1);
krista@2057
   111
krista@2057
   112
    const char* addr_curr = address;
krista@2057
   113
    char* retval_curr = retval;
krista@2057
   114
    
krista@2057
   115
    while (addr_curr < at) {
krista@2057
   116
        if (*addr_curr == '.') {
krista@2057
   117
            addr_curr++;
krista@2057
   118
            continue;
krista@2057
   119
        }
krista@2057
   120
        *retval_curr = *addr_curr;
krista@2057
   121
        retval_curr++;
krista@2057
   122
        addr_curr++;
krista@2057
   123
    }
krista@2057
   124
    if (*addr_curr == '@')
krista@2057
   125
        strcat(retval_curr, addr_curr);
krista@2057
   126
    
krista@2057
   127
    return retval;
krista@2057
   128
}
krista@2057
   129
krista@2057
   130
static bool _email_heuristic_match(const char* str1, const char* str2) {
krista@2057
   131
    if (!str1 || !str2)
krista@2057
   132
        return false;
krista@2057
   133
        
krista@2057
   134
    if (strcasecmp(str1, str2) == 0)
krista@2057
   135
        return true;
krista@2057
   136
    
krista@2057
   137
    int len1 = strlen(str1);
krista@2057
   138
    int len2 = strlen(str2);
krista@2057
   139
    
krista@2057
   140
    // otherwise, we work against dotted usernames
krista@2057
   141
    const char* at1 = strstr(str1, "@");
krista@2057
   142
    const char* at2 = strstr(str2, "@");
krista@2057
   143
    
krista@2057
   144
    if (!at1)
krista@2057
   145
        at1 = str1 + len1;
krista@2057
   146
    
krista@2057
   147
    if (!at2)
krista@2057
   148
        at2 = str2 + len2;
krista@2057
   149
        
krista@2057
   150
    // This sucks. And is expensive. Here we go.
krista@2057
   151
    const char* str1_curr = str1;
krista@2057
   152
    const char* str2_curr = str2;
krista@2057
   153
    
krista@2057
   154
    while (str1_curr > at1 && str2_curr > at2) {
krista@2057
   155
        if (*str1_curr == '.') {
krista@2057
   156
            str1_curr++;
krista@2057
   157
            continue;
krista@2057
   158
        }
krista@2057
   159
krista@2057
   160
        if (*str2_curr == '.') {
krista@2057
   161
            str2_curr++;
krista@2057
   162
            continue;
krista@2057
   163
        }
krista@2057
   164
        
krista@2057
   165
        if (tolower(*str1_curr) != tolower(*str2_curr))
krista@2057
   166
            return false;
krista@2057
   167
        
krista@2057
   168
        str1_curr++;
krista@2057
   169
        str2_curr++;
krista@2057
   170
    }
krista@2057
   171
    if (str1_curr == at1 && str2_curr == at2)
krista@2057
   172
        return true;
krista@2057
   173
    
krista@2057
   174
    return false;
krista@2057
   175
}
krista@763
   176
krista@2461
   177
static PEP_STATUS _version_test(const char *s)
krista@2461
   178
{
krista@2461
   179
    char *_s = strdup(s);
krista@2461
   180
    if (!_s)
krista@2461
   181
        return PEP_OUT_OF_MEMORY;
krista@2461
   182
krista@2461
   183
    int major;
krista@2461
   184
    int minor;
krista@2461
   185
    int revision;
krista@2461
   186
krista@2461
   187
    char *lasts = NULL;
krista@2461
   188
    char *p = strtok_r(_s, ".", &lasts);
krista@2461
   189
    if (!p)
krista@2461
   190
        goto unsupported;
krista@2461
   191
    else
krista@2461
   192
        major = atoi(p);
krista@2461
   193
krista@2461
   194
    p = strtok_r(NULL, ".", &lasts);
krista@2461
   195
    if (!p)
krista@2461
   196
        goto unsupported;
krista@2461
   197
    else
krista@2461
   198
        minor = atoi(p);
krista@2461
   199
krista@2461
   200
    p = strtok_r(NULL, ".", &lasts);
krista@2461
   201
    if (!p)
krista@2461
   202
        goto unsupported;
krista@2461
   203
    else
krista@2461
   204
        revision = atoi(p);
krista@2461
   205
krista@2461
   206
    free(_s);
krista@2461
   207
    _s = NULL;
krista@2461
   208
krista@2461
   209
    if (major > 2)
krista@2461
   210
        return PEP_STATUS_OK;
krista@2461
   211
krista@2461
   212
    if (major == 2 && minor > 1)
krista@2461
   213
        return PEP_STATUS_OK;
krista@2461
   214
krista@2461
   215
    if (major == 2 && minor == 0 && revision == 30)
krista@2461
   216
        return PEP_STATUS_OK;
krista@2461
   217
krista@2461
   218
    if (major == 2 && minor == 1 && revision >= 17)
krista@2461
   219
        return PEP_STATUS_OK;
krista@2461
   220
krista@2461
   221
unsupported:
krista@2461
   222
    free(_s);
krista@2461
   223
    return PEP_INIT_UNSUPPORTED_GPG_VERSION;
krista@2461
   224
}
krista@2461
   225
vb@62
   226
PEP_STATUS pgp_init(PEP_SESSION session, bool in_first)
vb@24
   227
{
vb@66
   228
    PEP_STATUS status = PEP_STATUS_OK;
vb@24
   229
    gpgme_error_t gpgme_error;
vb@62
   230
    bool bResult;
krista@1426
   231
vb@62
   232
    if (in_first) {
vb@78
   233
        stringlist_t *conf_keys   = new_stringlist("keyserver");
vb@78
   234
        stringlist_t *conf_values = new_stringlist("hkp://keys.gnupg.net");
vb@79
   235
vb@78
   236
        stringlist_add(conf_keys, "cert-digest-algo");
vb@78
   237
        stringlist_add(conf_values, "SHA256");
vb@78
   238
vb@80
   239
        stringlist_add(conf_keys, "no-emit-version");
vb@80
   240
        stringlist_add(conf_values, "");
vb@80
   241
vb@80
   242
        stringlist_add(conf_keys, "no-comments");
vb@80
   243
        stringlist_add(conf_values, "");
vb@80
   244
vb@80
   245
        stringlist_add(conf_keys, "personal-cipher-preferences");
vb@80
   246
        stringlist_add(conf_values, "AES AES256 AES192 CAST5");
vb@80
   247
vb@80
   248
        stringlist_add(conf_keys, "personal-digest-preferences");
vb@312
   249
        stringlist_add(conf_values, "SHA256 SHA512 SHA384 SHA224");
krista@763
   250
krista@1569
   251
        stringlist_add(conf_keys, "ignore-time-conflict");
krista@1569
   252
        stringlist_add(conf_values, "");
krista@1569
   253
krista@2461
   254
        stringlist_add(conf_keys, "allow-freeform-uid");
krista@2461
   255
        stringlist_add(conf_values, "");
krista@2461
   256
krista@763
   257
        bResult = ensure_config_values(conf_keys, conf_values, gpg_conf());
vb@24
   258
vb@78
   259
        free_stringlist(conf_keys);
vb@78
   260
        free_stringlist(conf_values);
vb@78
   261
Edouard@175
   262
        assert(bResult);
krista@2461
   263
        if (!bResult) {
Edouard@175
   264
            status = PEP_INIT_NO_GPG_HOME;
Edouard@175
   265
            goto pep_error;
Edouard@175
   266
        }
Edouard@175
   267
krista@763
   268
        conf_keys = new_stringlist("default-cache-ttl");
krista@763
   269
        conf_values = new_stringlist("300");
krista@763
   270
krista@763
   271
        stringlist_add(conf_keys, "max-cache-ttl");
krista@763
   272
        stringlist_add(conf_values, "1200");
krista@763
   273
krista@763
   274
        bResult = ensure_config_values(conf_keys, conf_values, gpg_agent_conf());
krista@763
   275
krista@763
   276
        free_stringlist(conf_keys);
krista@763
   277
        free_stringlist(conf_values);
krista@763
   278
krista@763
   279
        assert(bResult);
krista@2461
   280
        if (!bResult) {
krista@2461
   281
            status = PEP_INIT_CANNOT_CONFIG_GPG_AGENT;
krista@763
   282
            goto pep_error;
krista@763
   283
        }
Edouard@175
   284
vb@62
   285
        gpgme = dlopen(LIBGPGME, RTLD_LAZY);
vb@62
   286
        if (gpgme == NULL) {
vb@66
   287
            status = PEP_INIT_CANNOT_LOAD_GPGME;
vb@66
   288
            goto pep_error;
vb@62
   289
        }
vb@62
   290
vb@62
   291
        memset(&gpg, 0, sizeof(struct gpg_s));
vb@62
   292
krista@2461
   293
        gpg.gpgme_get_engine_info
krista@2461
   294
            = (gpgme_get_engine_info_t) (intptr_t) dlsym(gpgme,
krista@2461
   295
            "gpgme_get_engine_info");
krista@2461
   296
        assert(gpg.gpgme_get_engine_info);
krista@2461
   297
krista@2461
   298
        gpgme_engine_info_t info;
krista@2461
   299
        int err = gpg.gpgme_get_engine_info(&info);
krista@2461
   300
        assert(err == GPG_ERR_NO_ERROR);
krista@2461
   301
        if (err != GPG_ERR_NO_ERROR)
krista@2461
   302
            return PEP_OUT_OF_MEMORY;
krista@2461
   303
krista@2461
   304
        assert(info->version);
krista@2461
   305
        if (!info->version)
krista@2461
   306
            return PEP_INIT_CANNOT_DETERMINE_GPG_VERSION;
krista@2461
   307
krista@2461
   308
        status = _version_test(info->version);
krista@2461
   309
        if (status != PEP_STATUS_OK)
krista@2461
   310
            return status;
krista@2461
   311
vb@62
   312
        gpg.gpgme_set_locale
vb@62
   313
            = (gpgme_set_locale_t) (intptr_t) dlsym(gpgme,
vb@62
   314
            "gpgme_set_locale");
vb@62
   315
        assert(gpg.gpgme_set_locale);
vb@62
   316
vb@62
   317
        gpg.gpgme_check
vb@62
   318
            = (gpgme_check_version_t) (intptr_t) dlsym(gpgme,
vb@62
   319
            "gpgme_check_version");
vb@62
   320
        assert(gpg.gpgme_check);
vb@62
   321
vb@62
   322
        gpg.gpgme_new
vb@62
   323
            = (gpgme_new_t) (intptr_t) dlsym(gpgme, "gpgme_new");
vb@62
   324
        assert(gpg.gpgme_new);
vb@62
   325
vb@62
   326
        gpg.gpgme_release
vb@62
   327
            = (gpgme_release_t) (intptr_t) dlsym(gpgme, "gpgme_release");
vb@62
   328
        assert(gpg.gpgme_release);
vb@62
   329
vb@62
   330
        gpg.gpgme_set_protocol
vb@62
   331
            = (gpgme_set_protocol_t) (intptr_t) dlsym(gpgme,
vb@62
   332
            "gpgme_set_protocol");
vb@62
   333
        assert(gpg.gpgme_set_protocol);
vb@62
   334
vb@62
   335
        gpg.gpgme_set_armor
vb@62
   336
            = (gpgme_set_armor_t) (intptr_t) dlsym(gpgme,
vb@62
   337
            "gpgme_set_armor");
vb@62
   338
        assert(gpg.gpgme_set_armor);
vb@62
   339
vb@62
   340
        gpg.gpgme_data_new
vb@62
   341
            = (gpgme_data_new_t) (intptr_t) dlsym(gpgme,
vb@62
   342
            "gpgme_data_new");
vb@62
   343
        assert(gpg.gpgme_data_new);
vb@62
   344
vb@62
   345
        gpg.gpgme_data_new_from_mem
vb@62
   346
            = (gpgme_data_new_from_mem_t) (intptr_t) dlsym(gpgme,
vb@62
   347
            "gpgme_data_new_from_mem");
vb@62
   348
        assert(gpg.gpgme_data_new_from_mem);
vb@62
   349
vb@221
   350
        gpg.gpgme_data_new_from_cbs
vb@221
   351
            = (gpgme_data_new_from_cbs_t) (intptr_t) dlsym(gpgme,
vb@221
   352
            "gpgme_data_new_from_cbs");
vb@221
   353
        assert(gpg.gpgme_data_new_from_cbs);
vb@221
   354
vb@62
   355
        gpg.gpgme_data_release
vb@62
   356
            = (gpgme_data_release_t) (intptr_t) dlsym(gpgme,
vb@62
   357
            "gpgme_data_release");
vb@62
   358
        assert(gpg.gpgme_data_release);
vb@62
   359
vb@62
   360
        gpg.gpgme_data_identify
vb@62
   361
            = (gpgme_data_identify_t) (intptr_t) dlsym(gpgme,
vb@62
   362
            "gpgme_data_identify");
vb@62
   363
        assert(gpg.gpgme_data_identify);
vb@62
   364
vb@62
   365
        gpg.gpgme_data_seek
vb@62
   366
            = (gpgme_data_seek_t) (intptr_t) dlsym(gpgme,
vb@62
   367
            "gpgme_data_seek");
vb@62
   368
        assert(gpg.gpgme_data_seek);
vb@62
   369
vb@62
   370
        gpg.gpgme_data_read
vb@62
   371
            = (gpgme_data_read_t) (intptr_t) dlsym(gpgme,
vb@62
   372
            "gpgme_data_read");
vb@62
   373
        assert(gpg.gpgme_data_read);
vb@62
   374
vb@62
   375
        gpg.gpgme_op_decrypt
vb@62
   376
            = (gpgme_op_decrypt_t) (intptr_t) dlsym(gpgme,
vb@62
   377
            "gpgme_op_decrypt");
vb@62
   378
        assert(gpg.gpgme_op_decrypt);
vb@62
   379
vb@62
   380
        gpg.gpgme_op_verify
vb@62
   381
            = (gpgme_op_verify_t) (intptr_t) dlsym(gpgme,
vb@62
   382
            "gpgme_op_verify");
vb@62
   383
        assert(gpg.gpgme_op_verify);
vb@62
   384
vb@62
   385
        gpg.gpgme_op_decrypt_verify
vb@62
   386
            = (gpgme_op_decrypt_verify_t) (intptr_t) dlsym(gpgme,
vb@62
   387
            "gpgme_op_decrypt_verify");
vb@62
   388
        assert(gpg.gpgme_op_decrypt_verify);
vb@62
   389
vb@62
   390
        gpg.gpgme_op_decrypt_result
vb@62
   391
            = (gpgme_op_decrypt_result_t) (intptr_t) dlsym(gpgme,
vb@62
   392
            "gpgme_op_decrypt_result");
vb@62
   393
        assert(gpg.gpgme_op_decrypt_result);
vb@62
   394
vb@62
   395
        gpg.gpgme_op_encrypt_sign
vb@62
   396
            = (gpgme_op_encrypt_sign_t) (intptr_t) dlsym(gpgme,
vb@62
   397
            "gpgme_op_encrypt_sign");
vb@62
   398
        assert(gpg.gpgme_op_encrypt_sign);
vb@62
   399
krista@1639
   400
        gpg.gpgme_op_encrypt
krista@1639
   401
            = (gpgme_op_encrypt_t) (intptr_t) dlsym(gpgme,
krista@1639
   402
            "gpgme_op_encrypt");
krista@1639
   403
        assert(gpg.gpgme_op_encrypt);
krista@1639
   404
vb@62
   405
        gpg.gpgme_op_verify_result
vb@62
   406
            = (gpgme_op_verify_result_t) (intptr_t) dlsym(gpgme,
vb@62
   407
            "gpgme_op_verify_result");
vb@62
   408
        assert(gpg.gpgme_op_verify_result);
vb@62
   409
vb@62
   410
        gpg.gpgme_signers_clear
vb@62
   411
            = (gpgme_signers_clear_t) (intptr_t) dlsym(gpgme,
vb@62
   412
            "gpgme_signers_clear");
vb@62
   413
        assert(gpg.gpgme_signers_clear);
vb@62
   414
vb@62
   415
        gpg.gpgme_signers_add
vb@62
   416
            = (gpgme_signers_add_t) (intptr_t) dlsym(gpgme,
vb@62
   417
            "gpgme_signers_add");
vb@62
   418
        assert(gpg.gpgme_signers_add);
vb@62
   419
kgrothoff@1926
   420
        gpg.gpgme_set_passphrase_cb
kgrothoff@1926
   421
            = (gpgme_set_passphrase_cb_t) (intptr_t) dlsym(gpgme,
kgrothoff@1926
   422
            "gpgme_set_passphrase_cb");
kgrothoff@1926
   423
        assert(gpg.gpgme_set_passphrase_cb);
kgrothoff@1926
   424
vb@62
   425
        gpg.gpgme_get_key
vb@62
   426
            = (gpgme_get_key_t) (intptr_t) dlsym(gpgme, "gpgme_get_key");
vb@62
   427
        assert(gpg.gpgme_get_key);
krista@1920
   428
        
roker@2055
   429
        #ifdef GPGME_VERSION_NUMBER
krista@1921
   430
        #if (GPGME_VERSION_NUMBER >= 0x010700)
krista@1921
   431
                gpg.gpgme_op_createkey
krista@1921
   432
                    = (gpgme_op_createkey_t) (intptr_t) dlsym(gpgme,
krista@1921
   433
                    "gpgme_op_createkey");
krista@1921
   434
                assert(gpg.gpgme_op_createkey);
krista@1925
   435
                
krista@1925
   436
                gpg.gpgme_op_createsubkey
krista@1925
   437
                    = (gpgme_op_createsubkey_t) (intptr_t) dlsym(gpgme,
krista@1925
   438
                    "gpgme_op_createsubkey");
krista@1925
   439
                assert(gpg.gpgme_op_createsubkey);
krista@1925
   440
krista@1921
   441
        #endif
krista@1921
   442
        #endif
krista@1921
   443
        
vb@62
   444
        gpg.gpgme_op_genkey
vb@62
   445
            = (gpgme_op_genkey_t) (intptr_t) dlsym(gpgme,
vb@62
   446
            "gpgme_op_genkey");
vb@62
   447
        assert(gpg.gpgme_op_genkey);
vb@62
   448
vb@62
   449
        gpg.gpgme_op_genkey_result
vb@62
   450
            = (gpgme_op_genkey_result_t) (intptr_t) dlsym(gpgme,
vb@62
   451
            "gpgme_op_genkey_result");
vb@62
   452
        assert(gpg.gpgme_op_genkey_result);
vb@62
   453
vb@62
   454
        gpg.gpgme_op_delete = (gpgme_op_delete_t) (intptr_t)
vb@62
   455
            dlsym(gpgme, "gpgme_op_delete");
vb@62
   456
        assert(gpg.gpgme_op_delete);
vb@62
   457
vb@62
   458
        gpg.gpgme_op_import = (gpgme_op_import_t) (intptr_t)
vb@62
   459
            dlsym(gpgme, "gpgme_op_import");
vb@62
   460
        assert(gpg.gpgme_op_import);
vb@62
   461
Edouard@728
   462
        gpg.gpgme_op_import_result
Edouard@728
   463
            = (gpgme_op_import_result_t) (intptr_t) dlsym(gpgme,
Edouard@728
   464
            "gpgme_op_import_result");
Edouard@728
   465
        assert(gpg.gpgme_op_import_result);
Edouard@728
   466
vb@62
   467
        gpg.gpgme_op_export = (gpgme_op_export_t) (intptr_t)
vb@62
   468
            dlsym(gpgme, "gpgme_op_export");
vb@62
   469
        assert(gpg.gpgme_op_export);
vb@62
   470
vb@62
   471
        gpg.gpgme_set_keylist_mode = (gpgme_set_keylist_mode_t) (intptr_t)
vb@62
   472
            dlsym(gpgme, "gpgme_set_keylist_mode");
vb@62
   473
        assert(gpg.gpgme_set_keylist_mode);
vb@62
   474
vb@62
   475
        gpg.gpgme_get_keylist_mode = (gpgme_get_keylist_mode_t) (intptr_t)
vb@62
   476
            dlsym(gpgme, "gpgme_get_keylist_mode");
vb@62
   477
        assert(gpg.gpgme_get_keylist_mode);
vb@62
   478
vb@62
   479
        gpg.gpgme_op_keylist_start = (gpgme_op_keylist_start_t) (intptr_t)
vb@62
   480
            dlsym(gpgme, "gpgme_op_keylist_start");
vb@62
   481
        assert(gpg.gpgme_op_keylist_start);
vb@62
   482
vb@62
   483
        gpg.gpgme_op_keylist_next = (gpgme_op_keylist_next_t) (intptr_t)
vb@62
   484
            dlsym(gpgme, "gpgme_op_keylist_next");
vb@62
   485
        assert(gpg.gpgme_op_keylist_next);
vb@62
   486
vb@62
   487
        gpg.gpgme_op_keylist_end = (gpgme_op_keylist_end_t) (intptr_t)
vb@62
   488
            dlsym(gpgme, "gpgme_op_keylist_end");
vb@62
   489
        assert(gpg.gpgme_op_keylist_end);
vb@62
   490
vb@62
   491
        gpg.gpgme_op_import_keys = (gpgme_op_import_keys_t) (intptr_t)
vb@62
   492
            dlsym(gpgme, "gpgme_op_import_keys");
vb@62
   493
        assert(gpg.gpgme_op_import_keys);
vb@62
   494
vb@62
   495
        gpg.gpgme_key_ref = (gpgme_key_ref_t) (intptr_t)
vb@62
   496
            dlsym(gpgme, "gpgme_key_ref");
vb@62
   497
        assert(gpg.gpgme_key_ref);
vb@62
   498
vb@62
   499
        gpg.gpgme_key_unref = (gpgme_key_unref_t) (intptr_t)
vb@62
   500
            dlsym(gpgme, "gpgme_key_unref");
vb@62
   501
        assert(gpg.gpgme_key_unref);
vb@62
   502
vb@1387
   503
		gpg.gpgme_key_release = (gpgme_key_release_t)(intptr_t)
vb@1387
   504
			dlsym(gpgme, "gpgme_key_release");
vb@1387
   505
		assert(gpg.gpgme_key_release);
vb@1387
   506
vb@221
   507
        gpg.gpgme_op_edit = (gpgme_op_edit_t) (intptr_t)
vb@221
   508
            dlsym(gpgme, "gpgme_op_edit");
vb@221
   509
        assert(gpg.gpgme_op_edit);
vb@221
   510
vb@223
   511
        gpg.gpgme_io_write = (gpgme_io_write_t) (intptr_t)
vb@223
   512
            dlsym(gpgme, "gpgme_io_write");
vb@223
   513
        assert(gpg.gpgme_io_write);
vb@223
   514
vb@62
   515
        gpg.version = gpg.gpgme_check(NULL);
krista@1426
   516
roker@876
   517
        const char * const cLocal = setlocale(LC_ALL, NULL);
Edouard@348
   518
        if (!cLocal || (strcmp(cLocal, "C") == 0))
vb@62
   519
            setlocale(LC_ALL, "");
vb@62
   520
vb@62
   521
        gpg.gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
vb@62
   522
#ifdef LC_MESSAGES // Windoze
vb@62
   523
        gpg.gpgme_set_locale (NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
vb@62
   524
#endif
vb@24
   525
    }
vb@24
   526
krista@1426
   527
    gpg.gpgme_check(NULL);
vb@62
   528
    gpgme_error = gpg.gpgme_new(&session->ctx);
vb@45
   529
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   530
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@66
   531
        status = PEP_INIT_GPGME_INIT_FAILED;
vb@66
   532
        goto pep_error;
vb@24
   533
    }
vb@46
   534
    assert(session->ctx);
vb@24
   535
vb@62
   536
    gpgme_error = gpg.gpgme_set_protocol(session->ctx, GPGME_PROTOCOL_OpenPGP);
vb@45
   537
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   538
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   539
vb@62
   540
    gpg.gpgme_set_armor(session->ctx, 1);
vb@24
   541
vb@24
   542
    return PEP_STATUS_OK;
vb@66
   543
vb@66
   544
pep_error:
vb@66
   545
    pgp_release(session, in_first);
vb@66
   546
    return status;
vb@24
   547
}
vb@24
   548
vb@62
   549
void pgp_release(PEP_SESSION session, bool out_last)
vb@24
   550
{
vb@62
   551
    if (session->ctx) {
vb@74
   552
        gpg.gpgme_release(session->ctx);
vb@62
   553
        session->ctx = NULL;
vb@62
   554
    }
vb@62
   555
vb@74
   556
    if (out_last)
vb@74
   557
        if (gpgme)
vb@74
   558
            dlclose(gpgme);
vb@24
   559
}
vb@24
   560
vb@24
   561
PEP_STATUS pgp_decrypt_and_verify(
vb@24
   562
    PEP_SESSION session, const char *ctext, size_t csize,
krista@1397
   563
    const char *dsigtext, size_t dsigsize,
vb@24
   564
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
   565
    )
vb@24
   566
{
vb@24
   567
    PEP_STATUS result;
vb@24
   568
    gpgme_error_t gpgme_error;
vb@24
   569
    gpgme_data_t cipher, plain;
vb@24
   570
    gpgme_data_type_t dt;
krista@1579
   571
    gpgme_decrypt_result_t gpgme_decrypt_result = NULL;
vb@24
   572
vb@24
   573
    stringlist_t *_keylist = NULL;
roker@723
   574
    //int i_key = 0;
vb@24
   575
vb@46
   576
    assert(session);
vb@24
   577
    assert(ctext);
vb@24
   578
    assert(csize);
vb@24
   579
    assert(ptext);
vb@24
   580
    assert(psize);
vb@24
   581
    assert(keylist);
vb@24
   582
vb@24
   583
    *ptext = NULL;
vb@24
   584
    *psize = 0;
vb@24
   585
    *keylist = NULL;
vb@24
   586
vb@74
   587
    gpgme_error = gpg.gpgme_data_new_from_mem(&cipher, ctext, csize, 0);
vb@45
   588
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   589
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   590
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@24
   591
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   592
            return PEP_OUT_OF_MEMORY;
vb@24
   593
        else
vb@24
   594
            return PEP_UNKNOWN_ERROR;
vb@24
   595
    }
vb@24
   596
vb@74
   597
    gpgme_error = gpg.gpgme_data_new(&plain);
vb@45
   598
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   599
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   600
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@74
   601
        gpg.gpgme_data_release(cipher);
vb@24
   602
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   603
            return PEP_OUT_OF_MEMORY;
vb@24
   604
        else
vb@24
   605
            return PEP_UNKNOWN_ERROR;
vb@24
   606
    }
vb@24
   607
krista@1579
   608
vb@74
   609
    dt = gpg.gpgme_data_identify(cipher);
vb@24
   610
    switch (dt) {
edouard@1421
   611
#if GPGME_VERSION_NUMBER > 0x010600
vb@1419
   612
    case GPGME_DATA_TYPE_PGP_ENCRYPTED:
edouard@1420
   613
#endif
vb@24
   614
    case GPGME_DATA_TYPE_PGP_SIGNED:
vb@24
   615
    case GPGME_DATA_TYPE_PGP_OTHER:
krista@1426
   616
        if (dsigtext) {
krista@1426
   617
            gpgme_error = gpg.gpgme_op_decrypt(session->ctx, cipher, plain);
krista@1426
   618
        }
krista@1426
   619
        else {
krista@1426
   620
            gpgme_error = gpg.gpgme_op_decrypt_verify(session->ctx, cipher,
krista@1426
   621
                plain);
krista@1426
   622
        }
vb@45
   623
        gpgme_error = _GPGERR(gpgme_error);
vb@24
   624
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
   625
        assert(gpgme_error != GPG_ERR_NO_DATA);
vb@24
   626
vb@24
   627
        switch (gpgme_error) {
vb@344
   628
            case GPG_ERR_NO_ERROR:
vb@344
   629
            {
krista@1579
   630
                gpgme_decrypt_result = gpg.gpgme_op_decrypt_result(session->ctx);
krista@1591
   631
                /* NOW is when we have to process the decrypt_result, period.
krista@1591
   632
                   it is only valid until the next call on the context. */
krista@1591
   633
                   
krista@1591
   634
                gpgme_key_t key;
krista@1591
   635
                memset(&key,0,sizeof(key));
krista@1591
   636
                stringlist_t* recipient_keylist = new_stringlist(NULL);
krista@1592
   637
                if (!recipient_keylist) {
krista@1592
   638
                    gpg.gpgme_data_release(plain);
krista@1592
   639
                    gpg.gpgme_data_release(cipher);
krista@1592
   640
                    if (recipient_keylist)
krista@1592
   641
                        free_stringlist(recipient_keylist);
krista@1591
   642
                    return PEP_OUT_OF_MEMORY;
krista@1592
   643
                }
krista@1591
   644
               
krista@1591
   645
                if (gpgme_decrypt_result != NULL) {
krista@1591
   646
                    stringlist_t* _keylist = recipient_keylist;
krista@1591
   647
                    for (gpgme_recipient_t r = gpgme_decrypt_result->recipients; r != NULL; r = r->next) {
krista@1591
   648
                        // GPGME may give subkey's fpr instead of primary key's fpr.
krista@1591
   649
                        // Therefore we ask for the primary fingerprint instead
krista@1591
   650
                        // we assume that gpgme_get_key can find key by subkey's fpr
krista@1591
   651
                        gpgme_error = gpg.gpgme_get_key(session->ctx,
krista@1591
   652
                            r->keyid, &key, 0);
krista@1591
   653
                        gpgme_error = _GPGERR(gpgme_error);
krista@1591
   654
                        assert(gpgme_error != GPG_ERR_ENOMEM);
krista@1591
   655
                        if (gpgme_error == GPG_ERR_ENOMEM) {
krista@1591
   656
                            free_stringlist(_keylist);
krista@1591
   657
                            result = PEP_OUT_OF_MEMORY;
krista@1591
   658
                        }
krista@1591
   659
                        // Primary key is given as the first subkey
krista@1591
   660
                        if (gpgme_error == GPG_ERR_NO_ERROR &&
krista@1591
   661
                            key && key->subkeys && key->subkeys->fpr
krista@1591
   662
                            && key->subkeys->fpr[0]) {
krista@1591
   663
                            _keylist = stringlist_add(_keylist, key->subkeys->fpr);
krista@1591
   664
 
krista@1591
   665
                            gpg.gpgme_key_unref(key);
krista@1591
   666
 
krista@1591
   667
                        }
krista@1591
   668
                    }
krista@1591
   669
                    assert(_keylist);
krista@1591
   670
                    if (_keylist == NULL) {
krista@1591
   671
                        free_stringlist(recipient_keylist);
krista@1591
   672
                        if (*keylist)
krista@1591
   673
                            free_stringlist(*keylist);
krista@1591
   674
                        *keylist = NULL;
krista@1591
   675
                        result = PEP_OUT_OF_MEMORY;
krista@1591
   676
                    }
krista@1591
   677
                } /* Ok, so now we have any recipients it was encrypted for
krista@1591
   678
                     in recipient_keylist */
krista@1591
   679
            
krista@1591
   680
                   
vb@344
   681
                gpgme_verify_result_t gpgme_verify_result;
vb@344
   682
                char *_buffer = NULL;
vb@344
   683
                size_t reading;
vb@344
   684
                size_t length = gpg.gpgme_data_seek(plain, 0, SEEK_END);
vb@344
   685
                gpgme_signature_t gpgme_signature;
vb@24
   686
vb@344
   687
                assert(length != -1);
vb@344
   688
                gpg.gpgme_data_seek(plain, 0, SEEK_SET);
vb@24
   689
vb@344
   690
                // TODO: make things less memory consuming
vb@344
   691
                // the following algorithm allocates memory for the complete
vb@344
   692
                // text
vb@24
   693
vb@344
   694
                _buffer = malloc(length + 1);
vb@344
   695
                assert(_buffer);
vb@344
   696
                if (_buffer == NULL) {
vb@74
   697
                    gpg.gpgme_data_release(plain);
vb@74
   698
                    gpg.gpgme_data_release(cipher);
krista@1591
   699
                    if (recipient_keylist)
krista@1591
   700
                        free_stringlist(recipient_keylist);
vb@24
   701
                    return PEP_OUT_OF_MEMORY;
vb@24
   702
                }
vb@24
   703
vb@344
   704
                reading = gpg.gpgme_data_read(plain, _buffer, length);
vb@344
   705
                assert(length == reading);
vb@24
   706
krista@1427
   707
                if (dsigtext) {  // Is this safe to do?
krista@1427
   708
                    gpgme_data_t sigdata;
krista@1427
   709
                    gpg.gpgme_data_new_from_mem(&sigdata, dsigtext,
krista@1427
   710
                                                dsigsize, 0);
vb@1434
   711
                    gpg.gpgme_op_verify(session->ctx, sigdata, plain, NULL);
krista@1431
   712
                    gpg.gpgme_data_release(sigdata);
krista@1427
   713
                }
krista@1426
   714
vb@344
   715
                gpgme_verify_result =
vb@344
   716
                    gpg.gpgme_op_verify_result(session->ctx);
vb@344
   717
                assert(gpgme_verify_result);
vb@344
   718
                gpgme_signature = gpgme_verify_result->signatures;
krista@1426
   719
krista@1584
   720
                if (!gpgme_signature) {
krista@1584
   721
                    // try cleartext sig verification
krista@1584
   722
                    gpg.gpgme_op_verify(session->ctx, plain, NULL, plain);
krista@1584
   723
                    gpgme_verify_result =
krista@1584
   724
                        gpg.gpgme_op_verify_result(session->ctx);
krista@1584
   725
                            assert(gpgme_verify_result);
roker@2055
   726
                    gpgme_signature = gpgme_verify_result->signatures;
krista@1584
   727
                }
krista@1584
   728
vb@344
   729
                if (gpgme_signature) {
vb@344
   730
                    stringlist_t *k;
vb@344
   731
                    _keylist = new_stringlist(NULL);
vb@24
   732
                    assert(_keylist);
vb@24
   733
                    if (_keylist == NULL) {
vb@344
   734
                        gpg.gpgme_data_release(plain);
vb@344
   735
                        gpg.gpgme_data_release(cipher);
vb@344
   736
                        free(_buffer);
vb@344
   737
                        return PEP_OUT_OF_MEMORY;
vb@344
   738
                    }
vb@344
   739
                    k = _keylist;
vb@344
   740
vb@344
   741
                    result = PEP_DECRYPTED_AND_VERIFIED;
krista@1427
   742
                    gpg.gpgme_check(NULL);
krista@1591
   743
                    do { /* get all signers and put them at the front off
krista@1591
   744
                            the keylist (likely only one) */
vb@344
   745
                        switch (_GPGERR(gpgme_signature->status)) {
vb@344
   746
                        case GPG_ERR_NO_ERROR:
Edouard@505
   747
                        {
krista@1426
   748
                            // Some versions of gpg returns signer's
Edouard@674
   749
                            // signing subkey fingerprint instead of
Edouard@674
   750
                            // signer's primary key fingerprint.
krista@1426
   751
                            // This is meant to get signer's primary
Edouard@674
   752
                            // key fingerprint, using subkey's.
Edouard@674
   753
Edouard@728
   754
                            gpgme_key_t key = NULL;
Edouard@505
   755
Edouard@505
   756
                            gpgme_error = gpg.gpgme_get_key(session->ctx,
Edouard@505
   757
                                gpgme_signature->fpr, &key, 0);
Edouard@505
   758
                            gpgme_error = _GPGERR(gpgme_error);
Edouard@505
   759
                            assert(gpgme_error != GPG_ERR_ENOMEM);
Edouard@505
   760
                            if (gpgme_error == GPG_ERR_ENOMEM) {
Edouard@505
   761
                                free_stringlist(_keylist);
Edouard@505
   762
                                gpg.gpgme_data_release(plain);
Edouard@505
   763
                                gpg.gpgme_data_release(cipher);
Edouard@505
   764
                                free(_buffer);
Edouard@505
   765
                                return PEP_OUT_OF_MEMORY;
Edouard@505
   766
                            }
Edouard@505
   767
                            // Primary key is given as the first subkey
krista@1426
   768
                            if (gpgme_error == GPG_ERR_NO_ERROR &&
krista@1426
   769
                                key && key->subkeys && key->subkeys->fpr
Edouard@674
   770
                                && key->subkeys->fpr[0])
Edouard@674
   771
                            {
Edouard@505
   772
                                k = stringlist_add(k, key->subkeys->fpr);
Edouard@677
   773
Edouard@674
   774
                                gpg.gpgme_key_unref(key);
Edouard@677
   775
Edouard@677
   776
                                if (k == NULL) {
Edouard@677
   777
                                    free_stringlist(_keylist);
krista@1591
   778
                                    if (recipient_keylist)
krista@1591
   779
                                        free (recipient_keylist);
Edouard@677
   780
                                    gpg.gpgme_data_release(plain);
Edouard@677
   781
                                    gpg.gpgme_data_release(cipher);
Edouard@677
   782
                                    free(_buffer);
Edouard@677
   783
                                    return PEP_OUT_OF_MEMORY;
Edouard@677
   784
                                }
Edouard@674
   785
                            }
krista@1426
   786
                            else
Edouard@674
   787
                            {
Edouard@505
   788
                                result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
Edouard@505
   789
                                break;
Edouard@505
   790
                            }
vb@344
   791
                            break;
Edouard@505
   792
                        }
vb@344
   793
                        case GPG_ERR_CERT_REVOKED:
vb@344
   794
                        case GPG_ERR_BAD_SIGNATURE:
vb@344
   795
                            result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
vb@344
   796
                            break;
vb@344
   797
                        case GPG_ERR_SIG_EXPIRED:
vb@344
   798
                        case GPG_ERR_KEY_EXPIRED:
vb@344
   799
                        case GPG_ERR_NO_PUBKEY:
vb@344
   800
                            k = stringlist_add(k, gpgme_signature->fpr);
Edouard@674
   801
                            if (k == NULL) {
Edouard@674
   802
                                free_stringlist(_keylist);
krista@1591
   803
                                if (recipient_keylist)
krista@1591
   804
                                    free_stringlist(recipient_keylist);
Edouard@674
   805
                                gpg.gpgme_data_release(plain);
Edouard@674
   806
                                gpg.gpgme_data_release(cipher);
Edouard@674
   807
                                free(_buffer);
Edouard@674
   808
                                return PEP_OUT_OF_MEMORY;
Edouard@674
   809
                            }
vb@344
   810
                            if (result == PEP_DECRYPTED_AND_VERIFIED)
vb@344
   811
                                result = PEP_DECRYPTED;
vb@344
   812
                            break;
vb@344
   813
                        case GPG_ERR_GENERAL:
vb@344
   814
                            break;
vb@344
   815
                        default:
vb@344
   816
                            if (result == PEP_DECRYPTED_AND_VERIFIED)
vb@344
   817
                                result = PEP_DECRYPTED;
vb@344
   818
                            break;
vb@344
   819
                        }
vb@344
   820
                    } while ((gpgme_signature = gpgme_signature->next));
vb@344
   821
                }
vb@344
   822
                else {
vb@344
   823
                    result = PEP_DECRYPTED;
vb@344
   824
                }
vb@344
   825
vb@344
   826
                if (result == PEP_DECRYPTED_AND_VERIFIED
vb@344
   827
                    || result == PEP_DECRYPTED) {
vb@344
   828
                    *ptext = _buffer;
vb@344
   829
                    *psize = reading;
vb@344
   830
                    (*ptext)[*psize] = 0; // safeguard for naive users
vb@344
   831
                    *keylist = _keylist;
edouard@1933
   832
                    if (recipient_keylist) {
krista@1591
   833
                        if (!_keylist)
roker@2055
   834
                            *keylist = new_stringlist(""); // no sig
krista@1591
   835
                        if (!(*keylist)) {
krista@1591
   836
                            free_stringlist(_keylist);
krista@1591
   837
                            if (recipient_keylist)
krista@1591
   838
                                free_stringlist(recipient_keylist);
krista@1591
   839
                            gpg.gpgme_data_release(plain);
krista@1591
   840
                            gpg.gpgme_data_release(cipher);
krista@1591
   841
                            free(_buffer);
krista@1591
   842
                            return PEP_OUT_OF_MEMORY;
roker@2055
   843
                        }
krista@1591
   844
                        stringlist_append(*keylist, recipient_keylist);
edouard@1933
   845
                    }
vb@344
   846
                }
vb@344
   847
                else {
vb@344
   848
                    free_stringlist(_keylist);
krista@1591
   849
                    if (recipient_keylist)
krista@1591
   850
                        free_stringlist(recipient_keylist);
vb@344
   851
                    free(_buffer);
vb@344
   852
                }
vb@344
   853
                break;
vb@344
   854
            }
vb@344
   855
            case GPG_ERR_BAD_PASSPHRASE:
Edouard@725
   856
                result = PEP_DECRYPT_NO_KEY;
Edouard@725
   857
                break;
vb@344
   858
            case GPG_ERR_DECRYPT_FAILED:
vb@344
   859
            default:
vb@344
   860
            {
krista@1579
   861
                gpgme_decrypt_result = gpg.gpgme_op_decrypt_result(session->ctx);
vb@344
   862
                result = PEP_DECRYPT_NO_KEY;
vb@344
   863
vb@344
   864
                if (gpgme_decrypt_result != NULL) {
vb@344
   865
                    if (gpgme_decrypt_result->unsupported_algorithm)
vb@344
   866
                        *keylist = new_stringlist(gpgme_decrypt_result->unsupported_algorithm);
vb@344
   867
                    else
vb@344
   868
                        *keylist = new_stringlist("");
vb@344
   869
                    assert(*keylist);
vb@344
   870
                    if (*keylist == NULL) {
vb@24
   871
                        result = PEP_OUT_OF_MEMORY;
vb@24
   872
                        break;
vb@24
   873
                    }
vb@24
   874
                }
vb@24
   875
            }
vb@24
   876
        }
vb@24
   877
        break;
vb@24
   878
vb@24
   879
    default:
vb@24
   880
        result = PEP_DECRYPT_WRONG_FORMAT;
vb@24
   881
    }
vb@24
   882
vb@74
   883
    gpg.gpgme_data_release(plain);
vb@74
   884
    gpg.gpgme_data_release(cipher);
vb@24
   885
    return result;
vb@24
   886
}
vb@24
   887
vb@24
   888
PEP_STATUS pgp_verify_text(
vb@24
   889
    PEP_SESSION session, const char *text, size_t size,
vb@24
   890
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
   891
    )
vb@24
   892
{
vb@24
   893
    PEP_STATUS result;
vb@24
   894
    gpgme_error_t gpgme_error;
vb@24
   895
    gpgme_data_t d_text, d_sig;
vb@24
   896
    stringlist_t *_keylist;
vb@24
   897
vb@24
   898
    assert(session);
vb@24
   899
    assert(text);
vb@24
   900
    assert(size);
vb@24
   901
    assert(signature);
vb@24
   902
    assert(sig_size);
vb@24
   903
    assert(keylist);
vb@24
   904
vb@24
   905
    *keylist = NULL;
vb@24
   906
vb@74
   907
    gpgme_error = gpg.gpgme_data_new_from_mem(&d_text, text, size, 0);
vb@45
   908
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   909
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   910
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@24
   911
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   912
            return PEP_OUT_OF_MEMORY;
vb@24
   913
        else
vb@24
   914
            return PEP_UNKNOWN_ERROR;
vb@24
   915
    }
vb@24
   916
vb@74
   917
    gpgme_error = gpg.gpgme_data_new_from_mem(&d_sig, signature, sig_size, 0);
vb@45
   918
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   919
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   920
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@74
   921
        gpg.gpgme_data_release(d_text);
vb@24
   922
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   923
            return PEP_OUT_OF_MEMORY;
vb@24
   924
        else
vb@24
   925
            return PEP_UNKNOWN_ERROR;
vb@24
   926
    }
vb@24
   927
vb@74
   928
    gpgme_error = gpg.gpgme_op_verify(session->ctx, d_sig, d_text, NULL);
vb@45
   929
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   930
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
   931
vb@24
   932
    switch (gpgme_error) {
vb@24
   933
    case GPG_ERR_NO_ERROR:
vb@24
   934
    {
vb@24
   935
        gpgme_verify_result_t gpgme_verify_result;
vb@24
   936
        gpgme_signature_t gpgme_signature;
vb@24
   937
vb@24
   938
        gpgme_verify_result =
vb@74
   939
            gpg.gpgme_op_verify_result(session->ctx);
vb@24
   940
        assert(gpgme_verify_result);
vb@24
   941
        gpgme_signature = gpgme_verify_result->signatures;
vb@24
   942
vb@24
   943
        if (gpgme_signature) {
vb@24
   944
            stringlist_t *k;
vb@24
   945
            _keylist = new_stringlist(NULL);
vb@24
   946
            assert(_keylist);
vb@24
   947
            if (_keylist == NULL) {
vb@74
   948
                gpg.gpgme_data_release(d_text);
vb@74
   949
                gpg.gpgme_data_release(d_sig);
vb@24
   950
                return PEP_OUT_OF_MEMORY;
vb@24
   951
            }
vb@24
   952
            k = _keylist;
vb@24
   953
vb@24
   954
            result = PEP_VERIFIED;
vb@24
   955
            do {
Edouard@502
   956
                gpgme_key_t key;
Edouard@502
   957
                memset(&key,0,sizeof(key));
Edouard@502
   958
krista@1426
   959
                // GPGME may give subkey's fpr instead of primary key's fpr.
Edouard@502
   960
                // Therefore we ask for the primary fingerprint instead
Edouard@502
   961
                // we assume that gpgme_get_key can find key by subkey's fpr
Edouard@502
   962
                gpgme_error = gpg.gpgme_get_key(session->ctx,
Edouard@502
   963
                    gpgme_signature->fpr, &key, 0);
Edouard@502
   964
                gpgme_error = _GPGERR(gpgme_error);
Edouard@502
   965
                assert(gpgme_error != GPG_ERR_ENOMEM);
Edouard@502
   966
                if (gpgme_error == GPG_ERR_ENOMEM) {
vb@24
   967
                    free_stringlist(_keylist);
Edouard@505
   968
                    gpg.gpgme_data_release(d_text);
Edouard@505
   969
                    gpg.gpgme_data_release(d_sig);
vb@24
   970
                    return PEP_OUT_OF_MEMORY;
vb@24
   971
                }
Edouard@502
   972
                // Primary key is given as the first subkey
krista@1426
   973
                if (gpgme_error == GPG_ERR_NO_ERROR &&
krista@1426
   974
                    key && key->subkeys && key->subkeys->fpr
Edouard@674
   975
                    && key->subkeys->fpr[0])
Edouard@674
   976
                {
Edouard@504
   977
                    k = stringlist_add(k, key->subkeys->fpr);
Edouard@677
   978
Edouard@674
   979
                    gpg.gpgme_key_unref(key);
Edouard@677
   980
Edouard@677
   981
                    if (k == NULL) {
Edouard@677
   982
                        free_stringlist(_keylist);
Edouard@677
   983
                        gpg.gpgme_data_release(d_text);
Edouard@677
   984
                        gpg.gpgme_data_release(d_sig);
Edouard@677
   985
                        return PEP_OUT_OF_MEMORY;
Edouard@677
   986
                    }
Edouard@502
   987
                }
Edouard@502
   988
                else {
Edouard@502
   989
                    result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
Edouard@502
   990
                    break;
Edouard@502
   991
                }
Edouard@502
   992
vb@24
   993
                if (gpgme_signature->summary & GPGME_SIGSUM_RED) {
vb@24
   994
                    if (gpgme_signature->summary & GPGME_SIGSUM_KEY_EXPIRED
vb@24
   995
                        || gpgme_signature->summary & GPGME_SIGSUM_SIG_EXPIRED) {
vb@24
   996
                        if (result == PEP_VERIFIED
vb@24
   997
                            || result == PEP_VERIFIED_AND_TRUSTED)
vb@24
   998
                            result = PEP_UNENCRYPTED;
vb@24
   999
                    }
vb@24
  1000
                    else {
vb@24
  1001
                        result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
vb@24
  1002
                        break;
vb@24
  1003
                    }
vb@24
  1004
                }
vb@24
  1005
                else {
vb@24
  1006
                    if (gpgme_signature->summary & GPGME_SIGSUM_VALID) {
vb@24
  1007
                        if (result == PEP_VERIFIED)
vb@24
  1008
                            result = PEP_VERIFIED_AND_TRUSTED;
vb@24
  1009
                    }
vb@24
  1010
                    if (gpgme_signature->summary & GPGME_SIGSUM_GREEN) {
vb@24
  1011
                        // good
vb@24
  1012
                    }
vb@24
  1013
                    else if (gpgme_signature->summary & GPGME_SIGSUM_KEY_MISSING) {
vb@24
  1014
                        result = PEP_VERIFY_NO_KEY;
vb@24
  1015
                    }
vb@24
  1016
                    else if (gpgme_signature->summary & GPGME_SIGSUM_SYS_ERROR) {
vb@24
  1017
                        if (result == PEP_VERIFIED
vb@24
  1018
                            || result == PEP_VERIFIED_AND_TRUSTED)
vb@24
  1019
                            result = PEP_UNENCRYPTED;
vb@24
  1020
                    }
vb@24
  1021
                    else {
vb@24
  1022
                        // do nothing
vb@24
  1023
                    }
vb@24
  1024
                }
vb@24
  1025
            } while ((gpgme_signature = gpgme_signature->next));
vb@24
  1026
            *keylist = _keylist;
vb@24
  1027
        }
vb@24
  1028
        else {
vb@24
  1029
            result = PEP_UNENCRYPTED;
vb@24
  1030
        }
vb@24
  1031
        break;
vb@24
  1032
    }
vb@24
  1033
        break;
vb@24
  1034
    case GPG_ERR_NO_DATA:
vb@24
  1035
        result = PEP_DECRYPT_WRONG_FORMAT;
vb@24
  1036
        break;
vb@24
  1037
    case GPG_ERR_INV_VALUE:
vb@24
  1038
    default:
vb@24
  1039
        result = PEP_UNKNOWN_ERROR;
vb@24
  1040
        break;
vb@24
  1041
    }
vb@24
  1042
vb@74
  1043
    gpg.gpgme_data_release(d_text);
vb@74
  1044
    gpg.gpgme_data_release(d_sig);
vb@24
  1045
vb@24
  1046
    return result;
vb@24
  1047
}
vb@24
  1048
krista@1639
  1049
krista@1639
  1050
static PEP_STATUS pgp_encrypt_sign_optional(    
vb@24
  1051
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
krista@1639
  1052
    size_t psize, char **ctext, size_t *csize, bool sign
krista@1639
  1053
)
roker@2055
  1054
{
vb@24
  1055
    PEP_STATUS result;
vb@24
  1056
    gpgme_error_t gpgme_error;
vb@24
  1057
    gpgme_data_t plain, cipher;
vb@24
  1058
    gpgme_key_t *rcpt;
vb@24
  1059
    gpgme_encrypt_flags_t flags;
vb@24
  1060
    const stringlist_t *_keylist;
vb@24
  1061
    int i, j;
vb@24
  1062
vb@46
  1063
    assert(session);
vb@24
  1064
    assert(keylist);
vb@24
  1065
    assert(ptext);
vb@24
  1066
    assert(psize);
vb@24
  1067
    assert(ctext);
vb@24
  1068
    assert(csize);
vb@24
  1069
vb@24
  1070
    *ctext = NULL;
vb@24
  1071
    *csize = 0;
vb@24
  1072
vb@74
  1073
    gpgme_error = gpg.gpgme_data_new_from_mem(&plain, ptext, psize, 0);
vb@45
  1074
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1075
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1076
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@24
  1077
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
  1078
            return PEP_OUT_OF_MEMORY;
vb@24
  1079
        else
vb@24
  1080
            return PEP_UNKNOWN_ERROR;
vb@24
  1081
    }
vb@24
  1082
vb@74
  1083
    gpgme_error = gpg.gpgme_data_new(&cipher);
vb@45
  1084
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1085
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1086
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@74
  1087
        gpg.gpgme_data_release(plain);
vb@24
  1088
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
  1089
            return PEP_OUT_OF_MEMORY;
vb@24
  1090
        else
vb@24
  1091
            return PEP_UNKNOWN_ERROR;
vb@24
  1092
    }
vb@24
  1093
vb@109
  1094
    rcpt = calloc(stringlist_length(keylist) + 1, sizeof(gpgme_key_t));
vb@24
  1095
    assert(rcpt);
vb@24
  1096
    if (rcpt == NULL) {
vb@74
  1097
        gpg.gpgme_data_release(plain);
vb@74
  1098
        gpg.gpgme_data_release(cipher);
vb@24
  1099
        return PEP_OUT_OF_MEMORY;
vb@24
  1100
    }
vb@24
  1101
vb@74
  1102
    gpg.gpgme_signers_clear(session->ctx);
vb@24
  1103
vb@24
  1104
    for (_keylist = keylist, i = 0; _keylist != NULL; _keylist = _keylist->next, i++) {
vb@24
  1105
        assert(_keylist->value);
vb@74
  1106
        gpgme_error = gpg.gpgme_get_key(session->ctx, _keylist->value,
vb@24
  1107
            &rcpt[i], 0);
vb@45
  1108
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1109
        assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1110
vb@24
  1111
        switch (gpgme_error) {
vb@24
  1112
        case GPG_ERR_ENOMEM:
vb@24
  1113
            for (j = 0; j<i; j++)
vb@74
  1114
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
  1115
            free(rcpt);
vb@74
  1116
            gpg.gpgme_data_release(plain);
vb@74
  1117
            gpg.gpgme_data_release(cipher);
vb@24
  1118
            return PEP_OUT_OF_MEMORY;
vb@24
  1119
        case GPG_ERR_NO_ERROR:
krista@1639
  1120
            if (i == 0 && sign) {
vb@74
  1121
                gpgme_error_t _gpgme_error = gpg.gpgme_signers_add(session->ctx, rcpt[0]);
vb@45
  1122
                _gpgme_error = _GPGERR(_gpgme_error);
vb@24
  1123
                assert(_gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1124
            }
vb@24
  1125
            break;
vb@24
  1126
        case GPG_ERR_EOF:
vb@24
  1127
            for (j = 0; j<i; j++)
vb@74
  1128
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
  1129
            free(rcpt);
vb@74
  1130
            gpg.gpgme_data_release(plain);
vb@74
  1131
            gpg.gpgme_data_release(cipher);
vb@24
  1132
            return PEP_KEY_NOT_FOUND;
vb@24
  1133
        case GPG_ERR_AMBIGUOUS_NAME:
vb@24
  1134
            for (j = 0; j<i; j++)
vb@74
  1135
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
  1136
            free(rcpt);
vb@74
  1137
            gpg.gpgme_data_release(plain);
vb@74
  1138
            gpg.gpgme_data_release(cipher);
vb@24
  1139
            return PEP_KEY_HAS_AMBIG_NAME;
vb@24
  1140
        default: // GPG_ERR_INV_VALUE if CTX or R_KEY is not a valid pointer or
vb@24
  1141
            // FPR is not a fingerprint or key ID
vb@24
  1142
            for (j = 0; j<i; j++)
vb@74
  1143
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
  1144
            free(rcpt);
vb@74
  1145
            gpg.gpgme_data_release(plain);
vb@74
  1146
            gpg.gpgme_data_release(cipher);
vb@24
  1147
            return PEP_GET_KEY_FAILED;
vb@24
  1148
        }
vb@24
  1149
    }
vb@24
  1150
vb@24
  1151
    // TODO: remove that and replace with proper key management
vb@24
  1152
    flags = GPGME_ENCRYPT_ALWAYS_TRUST;
krista@1639
  1153
    
krista@1639
  1154
    if (sign) {
krista@1639
  1155
        gpgme_error = gpg.gpgme_op_encrypt_sign(session->ctx, rcpt, flags,
krista@1639
  1156
            plain, cipher);
krista@1639
  1157
    }
krista@1639
  1158
    else {
krista@1639
  1159
        gpgme_error = gpg.gpgme_op_encrypt(session->ctx, rcpt, flags,
krista@1639
  1160
            plain, cipher);
krista@1639
  1161
    }
krista@1639
  1162
    
vb@45
  1163
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1164
    switch (gpgme_error) {
vb@24
  1165
    case GPG_ERR_NO_ERROR:
vb@24
  1166
    {
vb@24
  1167
        char *_buffer = NULL;
vb@24
  1168
        size_t reading;
vb@74
  1169
        size_t length = gpg.gpgme_data_seek(cipher, 0, SEEK_END);
vb@24
  1170
        assert(length != -1);
vb@74
  1171
        gpg.gpgme_data_seek(cipher, 0, SEEK_SET);
vb@24
  1172
vb@24
  1173
        // TODO: make things less memory consuming
vb@24
  1174
        // the following algorithm allocates a buffer for the complete text
vb@24
  1175
vb@112
  1176
        _buffer = malloc(length + 1);
vb@24
  1177
        assert(_buffer);
vb@24
  1178
        if (_buffer == NULL) {
vb@24
  1179
            for (j = 0; j<stringlist_length(keylist); j++)
vb@74
  1180
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
  1181
            free(rcpt);
vb@74
  1182
            gpg.gpgme_data_release(plain);
vb@74
  1183
            gpg.gpgme_data_release(cipher);
vb@24
  1184
            return PEP_OUT_OF_MEMORY;
vb@24
  1185
        }
vb@24
  1186
vb@74
  1187
        reading = gpg.gpgme_data_read(cipher, _buffer, length);
vb@24
  1188
        assert(length == reading);
vb@24
  1189
vb@24
  1190
        *ctext = _buffer;
vb@24
  1191
        *csize = reading;
vb@24
  1192
        (*ctext)[*csize] = 0; // safeguard for naive users
vb@24
  1193
        result = PEP_STATUS_OK;
vb@24
  1194
        break;
vb@24
  1195
    }
vb@24
  1196
    default:
vb@24
  1197
        result = PEP_UNKNOWN_ERROR;
vb@24
  1198
    }
vb@24
  1199
vb@24
  1200
    for (j = 0; j<stringlist_length(keylist); j++)
vb@74
  1201
        gpg.gpgme_key_unref(rcpt[j]);
vb@24
  1202
    free(rcpt);
vb@74
  1203
    gpg.gpgme_data_release(plain);
vb@74
  1204
    gpg.gpgme_data_release(cipher);
vb@24
  1205
    return result;
vb@24
  1206
}
vb@24
  1207
krista@1639
  1208
PEP_STATUS pgp_encrypt_only(
krista@1639
  1209
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
krista@1639
  1210
    size_t psize, char **ctext, size_t *csize
krista@1639
  1211
    )
krista@1639
  1212
{
krista@1639
  1213
    return pgp_encrypt_sign_optional(session, keylist, ptext,
krista@1639
  1214
        psize, ctext, csize, false);
krista@1639
  1215
}
krista@1639
  1216
krista@1639
  1217
PEP_STATUS pgp_encrypt_and_sign(
krista@1639
  1218
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
krista@1639
  1219
    size_t psize, char **ctext, size_t *csize
krista@1639
  1220
    )
krista@1639
  1221
{
krista@1639
  1222
    return pgp_encrypt_sign_optional(session, keylist, ptext,
krista@1639
  1223
        psize, ctext, csize, true);
krista@1639
  1224
}
krista@1639
  1225
krista@1925
  1226
krista@1925
  1227
static PEP_STATUS find_single_key(
krista@1925
  1228
        PEP_SESSION session,
krista@1925
  1229
        const char *fpr,
krista@1925
  1230
        gpgme_key_t *key
krista@1925
  1231
    )
krista@1925
  1232
{
krista@1925
  1233
    gpgme_error_t gpgme_error;
krista@1925
  1234
krista@1925
  1235
    *key = NULL;
krista@1925
  1236
kgrothoff@1926
  1237
//    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
kgrothoff@1926
  1238
kgrothoff@1926
  1239
    gpgme_error = gpg.gpgme_get_key(session->ctx, fpr, key, 0);
kgrothoff@1926
  1240
krista@1925
  1241
    gpgme_error = _GPGERR(gpgme_error);
krista@1925
  1242
    switch (gpgme_error) {
krista@1925
  1243
    case GPG_ERR_NO_ERROR:
krista@1925
  1244
        break;
krista@1925
  1245
    case GPG_ERR_INV_VALUE:
krista@1925
  1246
        assert(0);
krista@1925
  1247
        return PEP_UNKNOWN_ERROR;
krista@1925
  1248
    default:
krista@1925
  1249
        return PEP_GET_KEY_FAILED;
krista@1925
  1250
    };
krista@1925
  1251
kgrothoff@1926
  1252
//    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, key);
kgrothoff@1926
  1253
//    gpgme_error = _GPGERR(gpgme_error);
kgrothoff@1926
  1254
//    assert(gpgme_error != GPG_ERR_INV_VALUE);
krista@1925
  1255
kgrothoff@1926
  1256
//    gpg.gpgme_op_keylist_end(session->ctx);
krista@1925
  1257
krista@1925
  1258
    return PEP_STATUS_OK;
krista@1925
  1259
}
krista@1925
  1260
krista@1925
  1261
krista@1914
  1262
static PEP_STATUS _pgp_createkey(PEP_SESSION session, pEp_identity *identity) {
krista@1914
  1263
    PEP_STATUS status = PEP_VERSION_MISMATCH;
krista@1914
  1264
roker@2055
  1265
    if (identity && identity->address) {
krista@1914
  1266
#ifdef GPGME_VERSION_NUMBER 
krista@1914
  1267
#if (GPGME_VERSION_NUMBER >= 0x010700)
krista@1920
  1268
        gpgme_error_t gpgme_error;
kgrothoff@1926
  1269
        int userid_size = strlen(identity->address) + 1;
krista@1921
  1270
        char* userid = (char*)(calloc(1, userid_size));
krista@1921
  1271
        if (!userid)
krista@1921
  1272
            return PEP_OUT_OF_MEMORY;
kgrothoff@1926
  1273
        strlcpy(userid, identity->address, userid_size);
krista@1921
  1274
        gpgme_error = gpg.gpgme_op_createkey(session->ctx, userid, "RSA", 
krista@1924
  1275
                                             0, 31536000, NULL, 
krista@1927
  1276
                                             GPGME_CREATE_NOPASSWD | GPGME_CREATE_FORCE);
krista@1920
  1277
        gpgme_error = _GPGERR(gpgme_error);
krista@1921
  1278
krista@1921
  1279
        free(userid);
krista@1921
  1280
krista@1920
  1281
        if (gpgme_error != GPG_ERR_NOT_SUPPORTED) {
krista@1920
  1282
            switch (gpgme_error) {
krista@1924
  1283
                case GPG_ERR_NO_ERROR:
roker@2055
  1284
                    break;
krista@1924
  1285
                case GPG_ERR_INV_VALUE:
krista@1924
  1286
                    return PEP_ILLEGAL_VALUE;
krista@1924
  1287
                case GPG_ERR_GENERAL:
krista@1924
  1288
                    return PEP_CANNOT_CREATE_KEY;
krista@1924
  1289
                default:
krista@1924
  1290
                    assert(0);
krista@1924
  1291
                    return PEP_UNKNOWN_ERROR;
roker@2055
  1292
            }
krista@1924
  1293
krista@1924
  1294
            /* This is the same regardless of whether we got it from genkey or createkey */
krista@1924
  1295
            gpgme_genkey_result_t gpgme_genkey_result = gpg.gpgme_op_genkey_result(session->ctx);
krista@1924
  1296
            assert(gpgme_genkey_result);
krista@1924
  1297
            assert(gpgme_genkey_result->fpr);
krista@1924
  1298
kgrothoff@1926
  1299
            char* fpr = strdup(gpgme_genkey_result->fpr);
krista@1925
  1300
            gpgme_key_t key;
krista@1925
  1301
            PEP_STATUS key_status = find_single_key(session, fpr, &key);
krista@1925
  1302
            if (!key || key_status != PEP_STATUS_OK)
krista@1925
  1303
                return PEP_CANNOT_CREATE_KEY;
roker@2055
  1304
            
krista@1925
  1305
            gpgme_error = gpg.gpgme_op_createsubkey(session->ctx, key, 
krista@1925
  1306
                                                    "RSA", 0, 
krista@1925
  1307
                                                    31536000, GPGME_CREATE_NOPASSWD 
krista@1925
  1308
                                                    | GPGME_CREATE_ENCR);
krista@1925
  1309
krista@1925
  1310
            switch (gpgme_error) {
krista@1925
  1311
                case GPG_ERR_NO_ERROR:
roker@2055
  1312
                    break;
krista@1925
  1313
                case GPG_ERR_INV_VALUE:
krista@1925
  1314
                    return PEP_ILLEGAL_VALUE;
krista@1925
  1315
                case GPG_ERR_GENERAL:
krista@1925
  1316
                    return PEP_CANNOT_CREATE_KEY;
krista@1925
  1317
                default:
krista@1925
  1318
                    assert(0);
krista@1925
  1319
                    return PEP_UNKNOWN_ERROR;
roker@2055
  1320
            }
krista@1925
  1321
            
krista@1924
  1322
            free(identity->fpr);
kgrothoff@1926
  1323
            identity->fpr = fpr;
krista@1924
  1324
            if (identity->fpr == NULL)
krista@1924
  1325
                return PEP_OUT_OF_MEMORY;
krista@1924
  1326
kgrothoff@1926
  1327
//            gpg.gpgme_key_unref(key);
krista@1925
  1328
            
kgrothoff@1926
  1329
            status = pgp_replace_only_uid(session, fpr,
roker@2055
  1330
                        identity->username, identity->address);
krista@1920
  1331
        }
krista@1914
  1332
#endif
krista@1914
  1333
#endif
krista@1914
  1334
    }
krista@1914
  1335
    
krista@1914
  1336
    return status;
krista@1914
  1337
}
krista@1914
  1338
vb@24
  1339
PEP_STATUS pgp_generate_keypair(
vb@24
  1340
    PEP_SESSION session, pEp_identity *identity
vb@24
  1341
    )
vb@24
  1342
{
vb@24
  1343
    assert(session);
vb@24
  1344
    assert(identity);
vb@24
  1345
    assert(identity->address);
vb@298
  1346
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@24
  1347
    assert(identity->username);
vb@24
  1348
krista@1914
  1349
    PEP_STATUS status = _pgp_createkey(session, identity);
krista@1914
  1350
    
krista@1924
  1351
    if (status != PEP_VERSION_MISMATCH)
krista@1914
  1352
        return status;
krista@1924
  1353
        
krista@1924
  1354
    gpgme_error_t gpgme_error;
krista@1924
  1355
    char *parms;
krista@1924
  1356
    const char *template =
krista@1924
  1357
        "<GnupgKeyParms format=\"internal\">\n"
krista@1924
  1358
        "Key-Type: RSA\n"
krista@1924
  1359
        "Key-Length: 4096\n"
krista@1924
  1360
        "Subkey-Type: RSA\n"
krista@1924
  1361
        "Subkey-Length: 4096\n"
krista@1924
  1362
        "Name-Real: %s\n"
krista@1924
  1363
        "Name-Email: %s\n"
krista@1924
  1364
        /* "Passphrase: %s\n" */
krista@1924
  1365
        "Expire-Date: 1y\n"
krista@1924
  1366
        "</GnupgKeyParms>\n";
krista@1924
  1367
    int result;
vb@24
  1368
krista@1924
  1369
    parms = calloc(1, PARMS_MAX);
krista@1924
  1370
    assert(parms);
krista@1924
  1371
    if (parms == NULL)
krista@1924
  1372
        return PEP_OUT_OF_MEMORY;
krista@1914
  1373
krista@1924
  1374
    result = snprintf(parms, PARMS_MAX, template, identity->username,
krista@1924
  1375
        identity->address); // , session->passphrase);
krista@1924
  1376
    assert(result < PARMS_MAX);
krista@1924
  1377
    if (result >= PARMS_MAX) {
vb@24
  1378
        free(parms);
krista@1924
  1379
        return PEP_BUFFER_TOO_SMALL;
vb@24
  1380
    }
vb@24
  1381
krista@1924
  1382
    gpgme_error = gpg.gpgme_op_genkey(session->ctx, parms, NULL, NULL);
krista@1924
  1383
    gpgme_error = _GPGERR(gpgme_error);
krista@1924
  1384
    free(parms);
krista@1924
  1385
krista@1924
  1386
    switch (gpgme_error) {
krista@1924
  1387
    case GPG_ERR_NO_ERROR:
krista@1924
  1388
        break;
krista@1924
  1389
    case GPG_ERR_INV_VALUE:
krista@1924
  1390
        return PEP_ILLEGAL_VALUE;
krista@1924
  1391
    case GPG_ERR_GENERAL:
krista@1924
  1392
        return PEP_CANNOT_CREATE_KEY;
krista@1924
  1393
    default:
krista@1924
  1394
        assert(0);
krista@1924
  1395
        return PEP_UNKNOWN_ERROR;
krista@1924
  1396
    }
krista@1924
  1397
krista@1914
  1398
    gpgme_genkey_result_t gpgme_genkey_result = gpg.gpgme_op_genkey_result(session->ctx);
vb@24
  1399
    assert(gpgme_genkey_result);
vb@24
  1400
    assert(gpgme_genkey_result->fpr);
vb@24
  1401
vb@298
  1402
    free(identity->fpr);
vb@24
  1403
    identity->fpr = strdup(gpgme_genkey_result->fpr);
vb@298
  1404
    if (identity->fpr == NULL)
vb@298
  1405
        return PEP_OUT_OF_MEMORY;
vb@24
  1406
vb@24
  1407
    return PEP_STATUS_OK;
vb@24
  1408
}
vb@24
  1409
vb@24
  1410
PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
  1411
{
vb@24
  1412
    gpgme_error_t gpgme_error;
vb@24
  1413
    gpgme_key_t key;
vb@24
  1414
vb@24
  1415
    assert(session);
vb@24
  1416
    assert(fpr);
vb@24
  1417
vb@74
  1418
    gpgme_error = gpg.gpgme_get_key(session->ctx, fpr, &key, 0);
vb@45
  1419
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1420
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1421
    switch (gpgme_error) {
vb@24
  1422
    case GPG_ERR_NO_ERROR:
vb@24
  1423
        break;
vb@24
  1424
    case GPG_ERR_EOF:
vb@24
  1425
        return PEP_KEY_NOT_FOUND;
vb@24
  1426
    case GPG_ERR_INV_VALUE:
vb@24
  1427
        return PEP_ILLEGAL_VALUE;
vb@24
  1428
    case GPG_ERR_AMBIGUOUS_NAME:
vb@24
  1429
        return PEP_KEY_HAS_AMBIG_NAME;
vb@24
  1430
    case GPG_ERR_ENOMEM:
vb@24
  1431
        return PEP_OUT_OF_MEMORY;
vb@24
  1432
    default:
vb@24
  1433
        assert(0);
vb@24
  1434
        return PEP_UNKNOWN_ERROR;
vb@24
  1435
    }
vb@24
  1436
vb@74
  1437
    gpgme_error = gpg.gpgme_op_delete(session->ctx, key, 1);
vb@45
  1438
    gpgme_error = _GPGERR(gpgme_error);
vb@74
  1439
    gpg.gpgme_key_unref(key);
vb@24
  1440
    switch (gpgme_error) {
vb@24
  1441
    case GPG_ERR_NO_ERROR:
vb@24
  1442
        break;
vb@24
  1443
    case GPG_ERR_INV_VALUE:
vb@24
  1444
        assert(0);
vb@24
  1445
        return PEP_UNKNOWN_ERROR;
vb@24
  1446
    case GPG_ERR_NO_PUBKEY:
vb@24
  1447
        assert(0);
vb@24
  1448
        return PEP_KEY_NOT_FOUND;
vb@24
  1449
    case GPG_ERR_AMBIGUOUS_NAME:
vb@24
  1450
        assert(0);
vb@24
  1451
        return PEP_KEY_HAS_AMBIG_NAME;
vb@24
  1452
    default:
vb@24
  1453
        assert(0);
vb@24
  1454
        return PEP_UNKNOWN_ERROR;
vb@24
  1455
    }
vb@24
  1456
vb@24
  1457
    return PEP_STATUS_OK;
vb@24
  1458
}
vb@24
  1459
Edouard@728
  1460
PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data,
Edouard@728
  1461
                              size_t size, identity_list **private_idents)
vb@24
  1462
{
vb@24
  1463
    gpgme_error_t gpgme_error;
vb@24
  1464
    gpgme_data_t dh;
vb@24
  1465
vb@24
  1466
    assert(session);
vb@24
  1467
    assert(key_data);
krista@1426
  1468
krista@1426
  1469
    if(private_idents)
Edouard@728
  1470
        *private_idents = NULL;
vb@24
  1471
vb@74
  1472
    gpgme_error = gpg.gpgme_data_new_from_mem(&dh, key_data, size, 0);
vb@45
  1473
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1474
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1475
    switch (gpgme_error) {
vb@24
  1476
    case GPG_ERR_NO_ERROR:
vb@24
  1477
        break;
vb@24
  1478
    case GPG_ERR_ENOMEM:
vb@24
  1479
        return PEP_OUT_OF_MEMORY;
vb@24
  1480
    case GPG_ERR_INV_VALUE:
vb@24
  1481
        assert(0);
vb@24
  1482
        return PEP_UNKNOWN_ERROR;
vb@24
  1483
    default:
vb@24
  1484
        assert(0);
vb@24
  1485
        return PEP_UNKNOWN_ERROR;
vb@24
  1486
    }
vb@24
  1487
Edouard@728
  1488
    gpgme_import_result_t gpgme_import_result;
Edouard@728
  1489
vb@74
  1490
    gpgme_error = gpg.gpgme_op_import(session->ctx, dh);
vb@45
  1491
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1492
    switch (gpgme_error) {
vb@24
  1493
    case GPG_ERR_NO_ERROR:
krista@1426
  1494
        if(private_idents)
Edouard@728
  1495
        {
Edouard@728
  1496
            gpgme_import_result =
Edouard@728
  1497
                gpg.gpgme_op_import_result(session->ctx);
Edouard@728
  1498
            assert(gpgme_import_result);
vb@1302
  1499
            if (!gpgme_import_result) {
vb@1302
  1500
                gpg.gpgme_data_release(dh);
vb@1302
  1501
                return PEP_UNKNOWN_ERROR;
vb@1302
  1502
            }
vb@1302
  1503
Edouard@728
  1504
            gpgme_import_status_t import;
krista@1426
  1505
            for (import = gpgme_import_result->imports;
krista@1426
  1506
                 import;
Edouard@728
  1507
                 import = import->next)
Edouard@728
  1508
             {
Edouard@728
  1509
                if (import &&
Edouard@728
  1510
                    import->result == GPG_ERR_NO_ERROR &&
Edouard@728
  1511
                    import->status & GPGME_IMPORT_SECRET )
Edouard@728
  1512
                {
Edouard@728
  1513
                    gpgme_key_t key = NULL;
Edouard@728
  1514
Edouard@728
  1515
                    gpgme_error = gpg.gpgme_get_key(session->ctx,
Edouard@728
  1516
                        import->fpr, &key, 0);
Edouard@728
  1517
                    gpgme_error = _GPGERR(gpgme_error);
Edouard@728
  1518
                    assert(gpgme_error != GPG_ERR_ENOMEM);
Edouard@728
  1519
                    if (gpgme_error == GPG_ERR_ENOMEM) {
Edouard@728
  1520
                        gpg.gpgme_data_release(dh);
Edouard@728
  1521
                        return PEP_OUT_OF_MEMORY;
Edouard@728
  1522
                    }
krista@1426
  1523
krista@1426
  1524
                    if (gpgme_error == GPG_ERR_NO_ERROR &&
krista@1426
  1525
                        key && key->uids &&
Edouard@728
  1526
                        key->uids->email && key->uids->name)
Edouard@728
  1527
                    {
Edouard@728
  1528
                        pEp_identity *ident = new_identity(
Edouard@728
  1529
                             key->uids->email, import->fpr, NULL, key->uids->name);
Edouard@728
  1530
Edouard@728
  1531
                        gpg.gpgme_key_unref(key);
Edouard@728
  1532
Edouard@728
  1533
                        if (ident == NULL) {
Edouard@728
  1534
                            gpg.gpgme_data_release(dh);
Edouard@728
  1535
                            return PEP_OUT_OF_MEMORY;
Edouard@728
  1536
                        }
Edouard@728
  1537
Edouard@728
  1538
                        *private_idents = identity_list_add(*private_idents, ident);
Edouard@728
  1539
Edouard@728
  1540
                        if (*private_idents == NULL) {
Edouard@728
  1541
                            gpg.gpgme_data_release(dh);
Edouard@728
  1542
                            return PEP_OUT_OF_MEMORY;
Edouard@728
  1543
                        }
Edouard@728
  1544
                    }
krista@1426
  1545
                    else
Edouard@728
  1546
                    {
Edouard@728
  1547
                        gpg.gpgme_key_unref(key);
Edouard@728
  1548
                        gpg.gpgme_data_release(dh);
Edouard@728
  1549
                        return PEP_UNKNOWN_ERROR;
Edouard@728
  1550
                    }
Edouard@728
  1551
                }
Edouard@728
  1552
            }
Edouard@728
  1553
        }
vb@24
  1554
        break;
vb@24
  1555
    case GPG_ERR_INV_VALUE:
vb@24
  1556
        assert(0);
vb@74
  1557
        gpg.gpgme_data_release(dh);
vb@24
  1558
        return PEP_UNKNOWN_ERROR;
vb@24
  1559
    case GPG_ERR_NO_DATA:
vb@74
  1560
        gpg.gpgme_data_release(dh);
vb@24
  1561
        return PEP_ILLEGAL_VALUE;
vb@24
  1562
    default:
vb@24
  1563
        assert(0);
vb@74
  1564
        gpg.gpgme_data_release(dh);
vb@24
  1565
        return PEP_UNKNOWN_ERROR;
vb@24
  1566
    }
vb@24
  1567
vb@74
  1568
    gpg.gpgme_data_release(dh);
vb@24
  1569
    return PEP_STATUS_OK;
vb@24
  1570
}
vb@24
  1571
Edouard@170
  1572
PEP_STATUS pgp_export_keydata(
vb@1103
  1573
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size,
vb@1103
  1574
        bool secret
vb@24
  1575
    )
vb@24
  1576
{
vb@24
  1577
    gpgme_error_t gpgme_error;
vb@24
  1578
    gpgme_data_t dh;
vb@24
  1579
    size_t _size;
vb@24
  1580
    char *buffer;
vb@24
  1581
    int reading;
vb@24
  1582
vb@24
  1583
    assert(session);
vb@24
  1584
    assert(fpr);
vb@24
  1585
    assert(key_data);
vb@24
  1586
    assert(size);
vb@24
  1587
vb@74
  1588
    gpgme_error = gpg.gpgme_data_new(&dh);
vb@45
  1589
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1590
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1591
    switch (gpgme_error) {
vb@24
  1592
    case GPG_ERR_NO_ERROR:
vb@24
  1593
        break;
vb@24
  1594
    case GPG_ERR_ENOMEM:
vb@24
  1595
        return PEP_OUT_OF_MEMORY;
vb@24
  1596
    case GPG_ERR_INV_VALUE:
vb@24
  1597
        assert(0);
vb@24
  1598
        return PEP_UNKNOWN_ERROR;
vb@24
  1599
    default:
vb@24
  1600
        assert(0);
vb@24
  1601
        return PEP_UNKNOWN_ERROR;
vb@24
  1602
    }
vb@24
  1603
vb@1103
  1604
    if (secret)
vb@1103
  1605
        gpgme_error = gpg.gpgme_op_export(session->ctx, fpr,
edouard@1163
  1606
            GPGME_EXPORT_MODE_SECRET, dh);
vb@1103
  1607
    else
vb@1103
  1608
        gpgme_error = gpg.gpgme_op_export(session->ctx, fpr,
vb@1103
  1609
            GPGME_EXPORT_MODE_MINIMAL, dh);
vb@45
  1610
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1611
    switch (gpgme_error) {
vb@24
  1612
    case GPG_ERR_NO_ERROR:
vb@24
  1613
        break;
vb@24
  1614
    case GPG_ERR_EOF:
vb@74
  1615
        gpg.gpgme_data_release(dh);
vb@24
  1616
        return PEP_KEY_NOT_FOUND;
vb@24
  1617
    case GPG_ERR_INV_VALUE:
vb@24
  1618
        assert(0);
vb@74
  1619
        gpg.gpgme_data_release(dh);
vb@24
  1620
        return PEP_UNKNOWN_ERROR;
vb@24
  1621
    default:
vb@24
  1622
        assert(0);
vb@74
  1623
        gpg.gpgme_data_release(dh);
vb@24
  1624
        return PEP_UNKNOWN_ERROR;
vb@24
  1625
    };
vb@24
  1626
vb@74
  1627
    _size = gpg.gpgme_data_seek(dh, 0, SEEK_END);
vb@24
  1628
    assert(_size != -1);
vb@74
  1629
    gpg.gpgme_data_seek(dh, 0, SEEK_SET);
vb@24
  1630
vb@24
  1631
    buffer = malloc(_size + 1);
vb@24
  1632
    assert(buffer);
vb@24
  1633
    if (buffer == NULL) {
vb@74
  1634
        gpg.gpgme_data_release(dh);
vb@24
  1635
        return PEP_OUT_OF_MEMORY;
vb@24
  1636
    }
vb@24
  1637
vb@74
  1638
    reading = gpg.gpgme_data_read(dh, buffer, _size);
vb@24
  1639
    assert(_size == reading);
edouard@1803
  1640
    if(_size != reading)
edouard@1803
  1641
        return PEP_CANNOT_EXPORT_KEY;
vb@24
  1642
vb@24
  1643
    // safeguard for the naive user
vb@24
  1644
    buffer[_size] = 0;
vb@24
  1645
vb@24
  1646
    *key_data = buffer;
vb@24
  1647
    *size = _size;
vb@24
  1648
vb@74
  1649
    gpg.gpgme_data_release(dh);
vb@24
  1650
    return PEP_STATUS_OK;
vb@24
  1651
}
vb@24
  1652
krista@1426
  1653
PEP_STATUS pgp_list_keyinfo(PEP_SESSION session, const char* pattern,
krista@1031
  1654
                            stringpair_list_t** keyinfo_list)
krista@1426
  1655
{
krista@1005
  1656
    gpgme_error_t gpgme_error;
krista@1005
  1657
    assert(session);
krista@1031
  1658
    assert(keyinfo_list);
krista@1426
  1659
krista@1031
  1660
    if (!session || !keyinfo_list)
krista@1005
  1661
        return PEP_ILLEGAL_VALUE;
krista@1426
  1662
krista@1031
  1663
    *keyinfo_list = NULL;
krista@1426
  1664
krista@1031
  1665
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
krista@1005
  1666
    gpgme_error = _GPGERR(gpgme_error);
krista@1426
  1667
krista@1005
  1668
    switch(gpgme_error) {
krista@1005
  1669
        case GPG_ERR_NO_ERROR:
krista@1005
  1670
            break;
krista@1005
  1671
        case GPG_ERR_INV_VALUE:
krista@1005
  1672
            assert(0);
krista@1005
  1673
            return PEP_UNKNOWN_ERROR;
krista@1005
  1674
        default:
krista@1005
  1675
            gpg.gpgme_op_keylist_end(session->ctx);
krista@1426
  1676
            return PEP_GET_KEY_FAILED;
krista@1005
  1677
    };
krista@1426
  1678
krista@1005
  1679
    gpgme_key_t key;
krista@1031
  1680
    stringpair_list_t* _keyinfo_list = new_stringpair_list(NULL);
krista@1031
  1681
    stringpair_list_t* list_curr = _keyinfo_list;
krista@1031
  1682
    stringpair_t* pair = NULL;
krista@1426
  1683
krista@1426
  1684
    do {
krista@1005
  1685
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
krista@1005
  1686
        gpgme_error = _GPGERR(gpgme_error);
krista@1426
  1687
krista@1005
  1688
        switch(gpgme_error) {
krista@1005
  1689
            case GPG_ERR_EOF:
krista@1005
  1690
                break;
krista@1005
  1691
            case GPG_ERR_NO_ERROR:
krista@1005
  1692
                assert(key);
krista@1005
  1693
                assert(key->subkeys);
krista@1005
  1694
                if (!key || !key->subkeys)
krista@1005
  1695
                    return PEP_GET_KEY_FAILED;
krista@1040
  1696
krista@1005
  1697
                // first subkey is primary key
krista@1005
  1698
                char* fpr = key->subkeys->fpr;
roker@1199
  1699
                char* uid = key->uids->uid;
krista@1187
  1700
krista@1005
  1701
                assert(fpr);
roker@1199
  1702
                assert(uid);
krista@1031
  1703
                if (!fpr)
krista@1005
  1704
                    return PEP_GET_KEY_FAILED;
krista@1426
  1705
krista@1050
  1706
                if (key->subkeys->revoked)
krista@1040
  1707
                    continue;
krista@1426
  1708
krista@1031
  1709
                pair = new_stringpair(fpr, uid);
krista@1019
  1710
krista@1031
  1711
                assert(pair);
krista@1426
  1712
krista@1031
  1713
                if (pair) {
krista@1031
  1714
                    list_curr = stringpair_list_add(list_curr, pair);
krista@1031
  1715
                    pair = NULL;
krista@1426
  1716
krista@1005
  1717
                    assert(list_curr);
krista@1005
  1718
                    if (list_curr != NULL)
krista@1005
  1719
                        break;
krista@1005
  1720
                    else
krista@1031
  1721
                        free_stringpair(pair);
krista@1005
  1722
                }
krista@1031
  1723
                // else fallthrough (list_curr or pair wasn't allocateable)
krista@1005
  1724
            case GPG_ERR_ENOMEM:
krista@1031
  1725
                free_stringpair_list(_keyinfo_list);
krista@1005
  1726
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1005
  1727
                return PEP_OUT_OF_MEMORY;
krista@1005
  1728
            default:
krista@1005
  1729
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1005
  1730
                return PEP_UNKNOWN_ERROR;
krista@1005
  1731
        }
krista@1005
  1732
    } while (gpgme_error != GPG_ERR_EOF);
krista@1426
  1733
krista@1032
  1734
    if (_keyinfo_list->value == NULL) {
krista@1031
  1735
        free_stringpair_list(_keyinfo_list);
krista@1031
  1736
        _keyinfo_list = NULL;
krista@1005
  1737
    }
krista@1426
  1738
krista@1031
  1739
    *keyinfo_list = _keyinfo_list;
krista@1426
  1740
krista@1005
  1741
    return PEP_STATUS_OK;
krista@1005
  1742
}
krista@1005
  1743
vb@46
  1744
static void _switch_mode(pEpSession *session, gpgme_keylist_mode_t remove_mode,
vb@24
  1745
    gpgme_keylist_mode_t add_mode)
vb@24
  1746
{
vb@24
  1747
    gpgme_error_t gpgme_error;
vb@24
  1748
    gpgme_keylist_mode_t mode;
vb@24
  1749
vb@74
  1750
    mode = gpg.gpgme_get_keylist_mode(session->ctx);
vb@24
  1751
vb@24
  1752
    mode &= ~remove_mode;
vb@24
  1753
    mode |= add_mode;
vb@24
  1754
vb@74
  1755
    gpgme_error = gpg.gpgme_set_keylist_mode(session->ctx, mode);
vb@45
  1756
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1757
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1758
}
vb@24
  1759
vb@24
  1760
PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1761
{
vb@24
  1762
    gpgme_error_t gpgme_error;
vb@24
  1763
    gpgme_key_t key;
vb@24
  1764
vb@24
  1765
    assert(session);
vb@24
  1766
    assert(pattern);
vb@24
  1767
vb@46
  1768
    _switch_mode(session, GPGME_KEYLIST_MODE_LOCAL, GPGME_KEYLIST_MODE_EXTERN);
vb@24
  1769
vb@74
  1770
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
vb@45
  1771
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1772
    switch (gpgme_error) {
vb@24
  1773
    case GPG_ERR_NO_ERROR:
vb@24
  1774
        break;
vb@24
  1775
    case GPG_ERR_INV_VALUE:
vb@24
  1776
        assert(0);
vb@47
  1777
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1778
        return PEP_UNKNOWN_ERROR;
vb@24
  1779
    default:
vb@47
  1780
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1781
        return PEP_GET_KEY_FAILED;
vb@24
  1782
    };
vb@24
  1783
vb@47
  1784
    gpgme_ctx_t import_ctx;
vb@74
  1785
    gpgme_error = gpg.gpgme_new(&import_ctx);
vb@47
  1786
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@47
  1787
vb@24
  1788
    do {
vb@74
  1789
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1790
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1791
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1792
        switch (gpgme_error) {
vb@24
  1793
        case GPG_ERR_EOF:
vb@24
  1794
            break;
vb@24
  1795
        case GPG_ERR_NO_ERROR:
vb@24
  1796
        {
vb@24
  1797
            gpgme_error_t gpgme_error;
vb@24
  1798
            gpgme_key_t keys[2];
vb@24
  1799
vb@24
  1800
            keys[0] = key;
vb@24
  1801
            keys[1] = NULL;
vb@24
  1802
vb@74
  1803
            gpgme_error = gpg.gpgme_op_import_keys(import_ctx, keys);
vb@45
  1804
            gpgme_error = _GPGERR(gpgme_error);
vb@74
  1805
            gpg.gpgme_key_unref(key);
vb@24
  1806
            assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1807
            assert(gpgme_error != GPG_ERR_CONFLICT);
vb@24
  1808
        }
vb@24
  1809
            break;
vb@24
  1810
        case GPG_ERR_ENOMEM:
vb@74
  1811
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1812
            gpg.gpgme_release(import_ctx);
vb@47
  1813
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1814
            return PEP_OUT_OF_MEMORY;
vb@24
  1815
        default:
vb@74
  1816
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1817
            gpg.gpgme_release(import_ctx);
vb@47
  1818
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@46
  1819
            return PEP_UNKNOWN_ERROR;
vb@24
  1820
        };
vb@24
  1821
    } while (gpgme_error != GPG_ERR_EOF);
vb@24
  1822
vb@74
  1823
    gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1824
    gpg.gpgme_release(import_ctx);
vb@47
  1825
    _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1826
    return PEP_STATUS_OK;
vb@24
  1827
}
vb@24
  1828
krista@1357
  1829
krista@1357
  1830
static PEP_STATUS _pgp_search_keys(PEP_SESSION session, const char* pattern,
krista@1357
  1831
                            stringlist_t** keylist,
krista@1357
  1832
                            int private_only) {
vb@24
  1833
    gpgme_error_t gpgme_error;
vb@24
  1834
    gpgme_key_t key;
krista@1426
  1835
vb@24
  1836
    assert(session);
vb@24
  1837
    assert(keylist);
krista@1426
  1838
vb@24
  1839
    *keylist = NULL;
krista@1426
  1840
krista@1357
  1841
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, private_only);
vb@45
  1842
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1843
    switch (gpgme_error) {
krista@1357
  1844
        case GPG_ERR_NO_ERROR:
krista@1357
  1845
            break;
krista@1357
  1846
        case GPG_ERR_INV_VALUE:
krista@1357
  1847
            assert(0);
krista@1357
  1848
            return PEP_UNKNOWN_ERROR;
krista@1357
  1849
        default:
krista@1357
  1850
            gpg.gpgme_op_keylist_end(session->ctx);
krista@1357
  1851
            return PEP_GET_KEY_FAILED;
vb@24
  1852
    };
krista@1426
  1853
roker@877
  1854
    stringlist_t *_keylist = new_stringlist(NULL);
vb@24
  1855
    stringlist_t *_k = _keylist;
krista@1426
  1856
vb@24
  1857
    do {
vb@74
  1858
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1859
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1860
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1861
        switch (gpgme_error) {
krista@1357
  1862
            case GPG_ERR_EOF:
vb@24
  1863
                break;
krista@1357
  1864
            case GPG_ERR_NO_ERROR:
krista@1357
  1865
                assert(key);
krista@1357
  1866
                assert(key->subkeys);
edouard@1525
  1867
                if(!key->subkeys)
krista@1357
  1868
                    break;
edouard@1525
  1869
                assert(key->uids);
edouard@1525
  1870
                gpgme_user_id_t kuid = key->uids;
krista@2057
  1871
                // check that at least one uid's email matches pattern exactly,
krista@2057
  1872
                // modulo the email-diff heuristic
edouard@1525
  1873
                while(kuid) {
krista@2057
  1874
                    if((pattern && kuid->email && _email_heuristic_match(kuid->email, pattern)) ||
edouard@1760
  1875
                       pattern == NULL /* match all */ )
edouard@1760
  1876
                    { 
edouard@1525
  1877
                        char *fpr = key->subkeys->fpr;
edouard@1525
  1878
                        assert(fpr);
edouard@1525
  1879
                        _k = stringlist_add(_k, fpr);
edouard@1525
  1880
                        assert(_k);
edouard@1525
  1881
                        if (_k == NULL){
edouard@1525
  1882
                            free_stringlist(_keylist);
edouard@1525
  1883
                            gpg.gpgme_op_keylist_end(session->ctx);
edouard@1525
  1884
                            return PEP_OUT_OF_MEMORY;
edouard@1525
  1885
                        }
edouard@1525
  1886
                        break;
edouard@1525
  1887
                    }
edouard@1525
  1888
                    kuid = kuid->next;
edouard@1525
  1889
                }
edouard@1525
  1890
                break;
krista@1357
  1891
            case GPG_ERR_ENOMEM:
krista@1357
  1892
                free_stringlist(_keylist);
krista@1357
  1893
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1357
  1894
                return PEP_OUT_OF_MEMORY;
krista@1357
  1895
            default:
krista@1357
  1896
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1357
  1897
                return PEP_UNKNOWN_ERROR;
vb@24
  1898
        };
vb@24
  1899
    } while (gpgme_error != GPG_ERR_EOF);
krista@1426
  1900
vb@74
  1901
    gpg.gpgme_op_keylist_end(session->ctx);
krista@2098
  1902
    if (_keylist->value == NULL) {
vb@523
  1903
        free_stringlist(_keylist);
vb@523
  1904
        _keylist = NULL;
krista@2057
  1905
        
krista@2098
  1906
        if (pattern != NULL) {
krista@2098
  1907
            // If match failed, check to see if we've got a dotted address in the pattern.
krista@2098
  1908
            // (last chance of the heuristic, really)
krista@2098
  1909
            // If so, try again without any dots.
krista@2098
  1910
            const char* dotpos = strstr(pattern, ".");
krista@2098
  1911
            const char* atpos = strstr(pattern, "@");
krista@2098
  1912
            if (dotpos && atpos && (dotpos < atpos)) {
krista@2098
  1913
                char* undotted = _undot_address(pattern);
krista@2098
  1914
                if (undotted) {
krista@2098
  1915
                    PEP_STATUS status = _pgp_search_keys(session, undotted,
krista@2098
  1916
                                                         keylist, private_only);
krista@2098
  1917
                    free(undotted);
krista@2098
  1918
                    return status;
krista@2098
  1919
                }
krista@2057
  1920
            }
krista@2057
  1921
        }
krista@2057
  1922
    }    
krista@2057
  1923
    
vb@24
  1924
    *keylist = _keylist;
vb@24
  1925
    return PEP_STATUS_OK;
vb@24
  1926
}
vb@24
  1927
krista@1357
  1928
PEP_STATUS pgp_find_keys(
krista@1357
  1929
    PEP_SESSION session, const char *pattern, stringlist_t **keylist
krista@1357
  1930
    )
krista@1357
  1931
{
krista@1357
  1932
    return _pgp_search_keys(session, pattern, keylist, 0);
krista@1426
  1933
}
krista@1357
  1934
krista@1357
  1935
PEP_STATUS pgp_find_private_keys(
krista@1357
  1936
    PEP_SESSION session, const char *pattern, stringlist_t **keylist
krista@1357
  1937
)
krista@1357
  1938
{
krista@1357
  1939
    return _pgp_search_keys(session, pattern, keylist, 1);
krista@1357
  1940
}
krista@1357
  1941
vb@2539
  1942
// this function is delivering a list of triples with fpr, email, name of all
vb@2539
  1943
// ultimatedly trusted private keys
vb@2539
  1944
vb@2539
  1945
PEP_STATUS pgp_find_trusted_private_keys(
vb@2539
  1946
        PEP_SESSION session, stringlist_t **keylist
vb@2539
  1947
    )
vb@2539
  1948
{
vb@2539
  1949
    assert(session && keylist);
vb@2539
  1950
    if (!session || !keylist)
krista@2458
  1951
        return PEP_ILLEGAL_VALUE;
krista@2458
  1952
vb@2539
  1953
    *keylist = NULL;
vb@2539
  1954
krista@2416
  1955
    gpgme_key_t key;
krista@2458
  1956
    gpgme_error_t gpgme_error;
vb@2539
  1957
vb@2539
  1958
    stringlist_t *private_keylist = NULL;
vb@2539
  1959
    PEP_STATUS status = pgp_find_private_keys(session, NULL, &private_keylist);
vb@2539
  1960
    if (status)
vb@2539
  1961
        return status;
vb@2539
  1962
    if (!private_keylist || !private_keylist->value)
vb@2539
  1963
        return status;
vb@2539
  1964
vb@2539
  1965
    stringlist_t *result_list = new_stringlist(NULL);
vb@2539
  1966
    if (!result_list)
vb@2539
  1967
        return PEP_OUT_OF_MEMORY;
vb@2539
  1968
    stringlist_t *_result_list = result_list;
vb@2539
  1969
vb@2539
  1970
    stringlist_t *keylist_curr;
vb@2539
  1971
    for (keylist_curr = private_keylist; keylist_curr && keylist_curr->value; keylist_curr = keylist_curr->next) {
vb@2539
  1972
        // a. get key data
vb@2539
  1973
        gpgme_error = gpg.gpgme_get_key(session->ctx, keylist_curr->value, &key, 1);
vb@2539
  1974
        gpgme_error = _GPGERR(gpgme_error);
vb@2539
  1975
        assert(gpgme_error != GPG_ERR_ENOMEM);
vb@2539
  1976
        switch (gpgme_error) {
vb@2539
  1977
            case GPG_ERR_NO_ERROR:
vb@2539
  1978
                break;
vb@2539
  1979
            case GPG_ERR_EOF:
vb@2539
  1980
                status = PEP_KEY_NOT_FOUND;
vb@2539
  1981
                break;
vb@2539
  1982
            case GPG_ERR_INV_VALUE:
vb@2539
  1983
                status = PEP_ILLEGAL_VALUE;
vb@2539
  1984
                break;
vb@2539
  1985
            case GPG_ERR_AMBIGUOUS_NAME:
vb@2539
  1986
                status = PEP_KEY_HAS_AMBIG_NAME;
vb@2539
  1987
                break;
vb@2539
  1988
            case GPG_ERR_ENOMEM:
vb@2539
  1989
                free_stringlist(result_list);
vb@2539
  1990
                free_stringlist(private_keylist);
vb@2539
  1991
                return PEP_OUT_OF_MEMORY;
vb@2539
  1992
            default:
vb@2539
  1993
                assert(0);
vb@2539
  1994
                status = PEP_UNKNOWN_ERROR;
vb@2539
  1995
        }
vb@2539
  1996
        if (key && gpgme_error == GPG_ERR_NO_ERROR) {
vb@2539
  1997
            if (key->revoked || key->disabled) {
vb@2539
  1998
                status = PEP_KEY_UNSUITABLE;
krista@2416
  1999
            }
vb@2539
  2000
            else {
vb@2539
  2001
                if (key->fpr && key->secret && key->can_encrypt && key->can_sign) {
vb@2539
  2002
                    if (key->owner_trust == GPGME_VALIDITY_ULTIMATE &&
vb@2539
  2003
                            key->uids && key->uids->email && key->uids->name) { 
vb@2539
  2004
                        _result_list = stringlist_add(_result_list, key->fpr);
vb@2539
  2005
                        if (!_result_list) {
vb@2539
  2006
                            free_stringlist(result_list);
vb@2539
  2007
                            free_stringlist(private_keylist);
vb@2539
  2008
                            return PEP_OUT_OF_MEMORY;
vb@2539
  2009
                        }
vb@2539
  2010
                        _result_list = stringlist_add(_result_list, key->uids->email);
vb@2539
  2011
                        if (!_result_list) {
vb@2539
  2012
                            free_stringlist(result_list);
vb@2539
  2013
                            free_stringlist(private_keylist);
vb@2539
  2014
                            return PEP_OUT_OF_MEMORY;
vb@2539
  2015
                        }
vb@2539
  2016
                        _result_list = stringlist_add(_result_list, key->uids->name);
vb@2539
  2017
                        if (!_result_list) {
vb@2539
  2018
                            free_stringlist(result_list);
vb@2539
  2019
                            free_stringlist(private_keylist);
vb@2539
  2020
                            return PEP_OUT_OF_MEMORY;
krista@2457
  2021
                        }
krista@2457
  2022
                    }
krista@2457
  2023
                }
krista@2416
  2024
            }
krista@2416
  2025
        }
krista@2416
  2026
    }
vb@2539
  2027
vb@2539
  2028
    free_stringlist(private_keylist);
vb@2539
  2029
    *keylist = result_list;
vb@2539
  2030
    return PEP_STATUS_OK;
krista@2416
  2031
}
krista@2416
  2032
vb@24
  2033
PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)
vb@24
  2034
{
vb@24
  2035
    gpgme_error_t gpgme_error;
vb@24
  2036
vb@24
  2037
    assert(session);
vb@24
  2038
    assert(pattern);
vb@24
  2039
vb@74
  2040
    gpgme_error = gpg.gpgme_op_export(session->ctx, pattern,
vb@24
  2041
        GPGME_EXPORT_MODE_EXTERN, NULL);
vb@45
  2042
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  2043
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  2044
    if (gpgme_error == GPG_ERR_NO_ERROR)
vb@24
  2045
        return PEP_STATUS_OK;
vb@24
  2046
    else
vb@24
  2047
        return PEP_CANNOT_SEND_KEY;
vb@24
  2048
}
vb@24
  2049
vb@24
  2050
PEP_STATUS pgp_get_key_rating(
vb@24
  2051
    PEP_SESSION session,
vb@24
  2052
    const char *fpr,
vb@24
  2053
    PEP_comm_type *comm_type
vb@24
  2054
    )
vb@24
  2055
{
vb@24
  2056
    PEP_STATUS status = PEP_STATUS_OK;
vb@24
  2057
    gpgme_error_t gpgme_error;
vb@24
  2058
    gpgme_key_t key;
vb@24
  2059
vb@24
  2060
    assert(session);
vb@24
  2061
    assert(fpr);
vb@24
  2062
    assert(comm_type);
vb@24
  2063
vb@24
  2064
    *comm_type = PEP_ct_unknown;
vb@24
  2065
vb@74
  2066
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
vb@45
  2067
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  2068
    switch (gpgme_error) {
vb@24
  2069
    case GPG_ERR_NO_ERROR:
vb@24
  2070
        break;
vb@24
  2071
    case GPG_ERR_INV_VALUE:
vb@24
  2072
        assert(0);
vb@24
  2073
        return PEP_UNKNOWN_ERROR;
vb@24
  2074
    default:
vb@24
  2075
        return PEP_GET_KEY_FAILED;
vb@24
  2076
    };
vb@24
  2077
vb@74
  2078
    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  2079
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  2080
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  2081
vb@24
  2082
    if (key == NULL) {
vb@74
  2083
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  2084
        return PEP_KEY_NOT_FOUND;
vb@24
  2085
    }
vb@24
  2086
vb@24
  2087
    switch (key->protocol) {
vb@24
  2088
    case GPGME_PROTOCOL_OpenPGP:
vb@24
  2089
    case GPGME_PROTOCOL_DEFAULT:
vb@24
  2090
        *comm_type = PEP_ct_OpenPGP_unconfirmed;
vb@24
  2091
        break;
vb@24
  2092
    case GPGME_PROTOCOL_CMS:
vb@24
  2093
        *comm_type = PEP_ct_CMS_unconfirmed;
vb@24
  2094
        break;
vb@24
  2095
    default:
vb@24
  2096
        *comm_type = PEP_ct_unknown;
vb@74
  2097
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  2098
        return PEP_STATUS_OK;
vb@24
  2099
    }
vb@24
  2100
vb@24
  2101
    switch (gpgme_error) {
vb@24
  2102
    case GPG_ERR_EOF:
vb@24
  2103
        break;
vb@24
  2104
    case GPG_ERR_NO_ERROR:
vb@24
  2105
        assert(key);
vb@24
  2106
        assert(key->subkeys);
vb@24
  2107
        for (gpgme_subkey_t sk = key->subkeys; sk != NULL; sk = sk->next) {
vb@24
  2108
            if (sk->length < 1024)
vb@24
  2109
                *comm_type = PEP_ct_key_too_short;
vb@24
  2110
            else if (
vb@24
  2111
                (
vb@24
  2112
                (sk->pubkey_algo == GPGME_PK_RSA)
vb@24
  2113
                || (sk->pubkey_algo == GPGME_PK_RSA_E)
vb@24
  2114
                || (sk->pubkey_algo == GPGME_PK_RSA_S)
vb@24
  2115
                )
vb@24
  2116
                && sk->length == 1024
vb@24
  2117
                )
vb@122
  2118
                *comm_type = PEP_ct_OpenPGP_weak_unconfirmed;
vb@24
  2119
vb@24
  2120
            if (sk->invalid) {
vb@24
  2121
                *comm_type = PEP_ct_key_b0rken;
vb@24
  2122
                break;
vb@24
  2123
            }
vb@24
  2124
            if (sk->expired) {
vb@24
  2125
                *comm_type = PEP_ct_key_expired;
vb@24
  2126
                break;
vb@24
  2127
            }
vb@24
  2128
            if (sk->revoked) {
vb@24
  2129
                *comm_type = PEP_ct_key_revoked;
vb@24
  2130
                break;
vb@24
  2131
            }
vb@24
  2132
        }
vb@24
  2133
        break;
vb@24
  2134
    case GPG_ERR_ENOMEM:
vb@74
  2135
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  2136
        *comm_type = PEP_ct_unknown;
vb@24
  2137
        return PEP_OUT_OF_MEMORY;
vb@24
  2138
    default:
vb@74
  2139
        gpg.gpgme_op_keylist_end(session->ctx);
vb@46
  2140
        return PEP_UNKNOWN_ERROR;
vb@24
  2141
    };
vb@24
  2142
vb@74
  2143
    gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  2144
vb@24
  2145
    return status;
vb@24
  2146
}
vb@200
  2147
vb@200
  2148
krista@1924
  2149
static ssize_t _nullwriter(
krista@1924
  2150
        void *_handle,
krista@1924
  2151
        const void *buffer,
krista@1924
  2152
        size_t size
krista@1924
  2153
    )
krista@1924
  2154
{
krista@1924
  2155
    return size;
krista@1924
  2156
}
krista@1924
  2157
krista@1924
  2158
typedef struct _replace_only_uid_state {
krista@1924
  2159
    enum {
krista@1924
  2160
        replace_uid_command = 0,
krista@1924
  2161
        replace_uid_realname,
krista@1924
  2162
        replace_uid_email,
kgrothoff@1926
  2163
        replace_uid_comment,
krista@1924
  2164
        replace_uid_adduid_ok,
krista@1924
  2165
        replace_uid_select_for_delete,
krista@1924
  2166
        replace_uid_delete,
krista@1924
  2167
        replace_uid_delete_confirm,
krista@1924
  2168
        replace_uid_select_for_trust,
krista@1924
  2169
        replace_uid_trust,
krista@1924
  2170
        replace_uid_trust_ultimate,
krista@1924
  2171
        replace_uid_trust_ultimate_confirm,
krista@1924
  2172
        replace_uid_quit,
krista@1924
  2173
        replace_uid_save_okay,
krista@1924
  2174
        replace_uid_exit,
krista@1924
  2175
        replace_uid_error = -1
krista@1924
  2176
    } state;
krista@1924
  2177
const char *realname;
krista@1924
  2178
const char *email;
krista@1924
  2179
} replace_only_uid_state;
krista@1924
  2180
kgrothoff@1926
  2181
krista@1924
  2182
static gpgme_error_t replace_only_uid_fsm(
krista@1924
  2183
    void *_handle,
krista@1924
  2184
    gpgme_status_code_t statuscode,
krista@1924
  2185
    const char *args,
krista@1924
  2186
    int fd
krista@1924
  2187
)
krista@1924
  2188
{
krista@1924
  2189
    replace_only_uid_state *handle = _handle;
krista@1924
  2190
        
krista@1924
  2191
    switch (handle->state) {
krista@1924
  2192
        case replace_uid_command:
krista@1924
  2193
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2194
                assert(strcmp(args, "keyedit.prompt") == 0);
krista@1924
  2195
                if (strcmp(args, "keyedit.prompt")) {
krista@1924
  2196
                    handle->state = replace_uid_error;
krista@1924
  2197
                    return GPG_ERR_GENERAL;
krista@1924
  2198
                }
krista@1924
  2199
                gpg.gpgme_io_write(fd, "adduid\n", 7);
krista@1924
  2200
                handle->state = replace_uid_realname;
krista@1924
  2201
            }
krista@1924
  2202
            break;
krista@1924
  2203
            
krista@1924
  2204
        case replace_uid_realname:
krista@1924
  2205
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2206
                assert(strcmp(args, "keygen.name") == 0);
krista@1924
  2207
                assert(handle->realname);
krista@1924
  2208
                if (strcmp(args, "keygen.name") || !handle->realname) {
krista@1924
  2209
                    handle->state = replace_uid_error;
krista@1924
  2210
                    return GPG_ERR_GENERAL;
krista@1924
  2211
                }
krista@1924
  2212
                size_t realname_strlen = strlen(handle->realname);
krista@1924
  2213
                char* realname = (char*)calloc(1, realname_strlen + 2); // \n + \0
krista@1924
  2214
                if (!realname) {
krista@1924
  2215
                    handle->state = replace_uid_error;
krista@1924
  2216
                    return GPG_ERR_ENOMEM;
krista@1924
  2217
                }
kgrothoff@1926
  2218
                strlcpy(realname, handle->realname, realname_strlen + 1);
roker@2055
  2219
                realname[realname_strlen] = '\n';
krista@1924
  2220
                gpg.gpgme_io_write(fd, realname, realname_strlen + 1);
krista@1924
  2221
                handle->state = replace_uid_email;
krista@1924
  2222
                free(realname);
krista@1924
  2223
            }
krista@1924
  2224
            break;
krista@1924
  2225
            
krista@1924
  2226
        case replace_uid_email:
krista@1924
  2227
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2228
                assert(strcmp(args, "keygen.email") == 0);
krista@1924
  2229
                assert(handle->email);
krista@1924
  2230
                if (strcmp(args, "keygen.email") || !handle->email) {
krista@1924
  2231
                    handle->state = replace_uid_error;
krista@1924
  2232
                    return GPG_ERR_GENERAL;
krista@1924
  2233
                }
krista@1924
  2234
                size_t email_strlen = strlen(handle->email);
krista@1924
  2235
                char* email = (char*)calloc(1, email_strlen + 2); // \n + \0
krista@1924
  2236
                if (!email) {
krista@1924
  2237
                    handle->state = replace_uid_error;
krista@1924
  2238
                    return GPG_ERR_ENOMEM;
krista@1924
  2239
                }
kgrothoff@1926
  2240
                strlcpy(email, handle->email, email_strlen + 1);
roker@2055
  2241
                email[email_strlen] = '\n';
krista@1924
  2242
                gpg.gpgme_io_write(fd, email, email_strlen + 1);
kgrothoff@1926
  2243
                handle->state = replace_uid_comment;
krista@1924
  2244
                free(email);
krista@1924
  2245
            }
krista@1924
  2246
            break;
krista@1924
  2247
kgrothoff@1926
  2248
        case replace_uid_comment:
kgrothoff@1926
  2249
            if (statuscode == GPGME_STATUS_GET_LINE) {
kgrothoff@1926
  2250
                assert(strcmp(args, "keygen.comment") == 0);
kgrothoff@1926
  2251
                if (strcmp(args, "keygen.comment") || !handle->email) {
kgrothoff@1926
  2252
                    handle->state = replace_uid_error;
kgrothoff@1926
  2253
                    return GPG_ERR_GENERAL;
kgrothoff@1926
  2254
                }
kgrothoff@1926
  2255
                gpg.gpgme_io_write(fd, "\n", 1);
kgrothoff@1926
  2256
                //handle->state = replace_uid_adduid_ok;
kgrothoff@1926
  2257
                handle->state = replace_uid_select_for_delete;
kgrothoff@1926
  2258
            }
kgrothoff@1926
  2259
            break;
kgrothoff@1926
  2260
/*
krista@1924
  2261
        case replace_uid_adduid_ok:
krista@1924
  2262
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2263
                assert(strcmp(args, "keygen.userid.cmd") == 0);
krista@1924
  2264
                if (strcmp(args, "keygen.userid.cmd")) {
krista@1924
  2265
                    handle->state = replace_uid_error;
krista@1924
  2266
                    return GPG_ERR_GENERAL;
krista@1924
  2267
                }
krista@1924
  2268
                gpg.gpgme_io_write(fd, "O\n", 2);
krista@1924
  2269
                handle->state = replace_uid_select_for_delete;
krista@1924
  2270
            }
krista@1924
  2271
            break;
kgrothoff@1926
  2272
	    */
krista@1924
  2273
krista@1924
  2274
        case replace_uid_select_for_delete:
krista@1924
  2275
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2276
                assert(strcmp(args, "keyedit.prompt") == 0);
krista@1924
  2277
                if (strcmp(args, "keyedit.prompt")) {
krista@1924
  2278
                    handle->state = replace_uid_error;
krista@1924
  2279
                    return GPG_ERR_GENERAL;
krista@1924
  2280
                }
krista@1924
  2281
                gpg.gpgme_io_write(fd, "uid 1\n", 6);
krista@1924
  2282
                handle->state = replace_uid_delete;
krista@1924
  2283
            }
krista@1924
  2284
            break;
krista@1924
  2285
krista@1924
  2286
        case replace_uid_delete:
krista@1924
  2287
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2288
                assert(strcmp(args, "keyedit.prompt") == 0);
krista@1924
  2289
                if (strcmp(args, "keyedit.prompt")) {
krista@1924
  2290
                    handle->state = replace_uid_error;
krista@1924
  2291
                    return GPG_ERR_GENERAL;
krista@1924
  2292
                }
krista@1924
  2293
                gpg.gpgme_io_write(fd, "deluid\n", 7);
krista@1924
  2294
                handle->state = replace_uid_delete_confirm;
krista@1924
  2295
            }
krista@1924
  2296
            break;
krista@1924
  2297
krista@1924
  2298
        case replace_uid_delete_confirm:
kgrothoff@1926
  2299
            if (statuscode == GPGME_STATUS_GET_BOOL) {
krista@1924
  2300
                assert(strcmp(args, "keyedit.remove.uid.okay") == 0);
krista@1924
  2301
                if (strcmp(args, "keyedit.remove.uid.okay")) {
krista@1924
  2302
                    handle->state = replace_uid_error;
krista@1924
  2303
                    return GPG_ERR_GENERAL;
krista@1924
  2304
                }
krista@1924
  2305
                gpg.gpgme_io_write(fd, "Y\n", 2);
krista@1924
  2306
                handle->state = replace_uid_select_for_trust;
krista@1924
  2307
            }
krista@1924
  2308
            break;
krista@1924
  2309
krista@1924
  2310
        case replace_uid_select_for_trust:
krista@1924
  2311
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2312
                assert(strcmp(args, "keyedit.prompt") == 0);
krista@1924
  2313
                if (strcmp(args, "keyedit.prompt")) {
krista@1924
  2314
                    handle->state = replace_uid_error;
krista@1924
  2315
                    return GPG_ERR_GENERAL;
krista@1924
  2316
                }
kgrothoff@1926
  2317
                gpg.gpgme_io_write(fd, "uid 1\n", 6);
krista@1924
  2318
                handle->state = replace_uid_trust;
krista@1924
  2319
            }
krista@1924
  2320
            break;
krista@1924
  2321
krista@1924
  2322
        case replace_uid_trust:
krista@1924
  2323
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2324
                assert(strcmp(args, "keyedit.prompt") == 0);
krista@1924
  2325
                if (strcmp(args, "keyedit.prompt")) {
krista@1924
  2326
                    handle->state = replace_uid_error;
krista@1924
  2327
                    return GPG_ERR_GENERAL;
krista@1924
  2328
                }
kgrothoff@1926
  2329
                gpg.gpgme_io_write(fd, "trust\n", 6);
krista@1924
  2330
                handle->state = replace_uid_trust_ultimate;
krista@1924
  2331
            }
krista@1924
  2332
            break;
krista@1924
  2333
krista@1924
  2334
        case replace_uid_trust_ultimate:
krista@1924
  2335
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2336
                assert(strcmp(args, "edit_ownertrust.value") == 0);
krista@1924
  2337
                if (strcmp(args, "edit_ownertrust.value")) {
krista@1924
  2338
                    handle->state = replace_uid_error;
krista@1924
  2339
                    return GPG_ERR_GENERAL;
krista@1924
  2340
                }
krista@1924
  2341
                gpg.gpgme_io_write(fd, "5\n", 2);
krista@1924
  2342
                handle->state = replace_uid_trust_ultimate_confirm;
krista@1924
  2343
            }
krista@1924
  2344
            break;
krista@1924
  2345
krista@1924
  2346
        case replace_uid_trust_ultimate_confirm:
kgrothoff@1926
  2347
            if (statuscode == GPGME_STATUS_GET_BOOL) {
krista@1924
  2348
                assert(strcmp(args, "edit_ownertrust.set_ultimate.okay") == 0);
krista@1924
  2349
                if (strcmp(args, "edit_ownertrust.set_ultimate.okay")) {
krista@1924
  2350
                    handle->state = replace_uid_error;
krista@1924
  2351
                    return GPG_ERR_GENERAL;
krista@1924
  2352
                }
krista@1924
  2353
                gpg.gpgme_io_write(fd, "Y\n", 2);
krista@1924
  2354
                handle->state = replace_uid_quit;
krista@1924
  2355
            }
krista@1924
  2356
            break;
krista@1924
  2357
krista@1924
  2358
        case replace_uid_quit:
krista@1924
  2359
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2360
                assert(strcmp(args, "keyedit.prompt") == 0);
krista@1924
  2361
                if (strcmp(args, "keyedit.prompt")) {
krista@1924
  2362
                    handle->state = replace_uid_error;
krista@1924
  2363
                    return GPG_ERR_GENERAL;
krista@1924
  2364
                }
krista@1924
  2365
                gpg.gpgme_io_write(fd, "quit\n", 5);
krista@1924
  2366
                handle->state = replace_uid_save_okay;
krista@1924
  2367
            }
krista@1924
  2368
            break;
krista@1924
  2369
krista@1924
  2370
        case replace_uid_save_okay:
kgrothoff@1926
  2371
            if (statuscode == GPGME_STATUS_GET_BOOL) {
krista@1924
  2372
                assert(strcmp(args, "keyedit.save.okay") == 0);
krista@1924
  2373
                if (strcmp(args, "keyedit.save.okay")) {
krista@1924
  2374
                    handle->state = replace_uid_error;
krista@1924
  2375
                    return GPG_ERR_GENERAL;
krista@1924
  2376
                }
krista@1924
  2377
                gpg.gpgme_io_write(fd, "Y\n", 2);
krista@1924
  2378
                handle->state = replace_uid_exit;
krista@1924
  2379
            }
krista@1924
  2380
            break;
krista@1924
  2381
krista@1924
  2382
        case replace_uid_exit:
krista@1924
  2383
            break;
krista@1924
  2384
krista@1924
  2385
        case replace_uid_error:
krista@1924
  2386
            return GPG_ERR_GENERAL;
krista@1924
  2387
            
krista@1924
  2388
        default:
roker@2055
  2389
            break;
krista@1924
  2390
    }
krista@1924
  2391
    return GPG_ERR_NO_ERROR;
krista@1924
  2392
}
krista@1924
  2393
krista@1924
  2394
PEP_STATUS pgp_replace_only_uid(
krista@1924
  2395
        PEP_SESSION session,
krista@1924
  2396
        const char* fpr,
krista@1924
  2397
        const char* realname,
krista@1924
  2398
        const char* email
krista@1924
  2399
    )
krista@1924
  2400
{
krista@1924
  2401
    PEP_STATUS status = PEP_STATUS_OK;
krista@1924
  2402
    gpgme_error_t gpgme_error;
krista@1924
  2403
    gpgme_key_t key;
krista@1924
  2404
    gpgme_data_t output;
krista@1924
  2405
    replace_only_uid_state handle;
krista@1924
  2406
krista@1924
  2407
    assert(session);
krista@1924
  2408
    assert(fpr);
krista@1924
  2409
    assert(realname);
krista@1924
  2410
    assert(email);
krista@1924
  2411
    
krista@1924
  2412
    memset(&handle, 0, sizeof(replace_only_uid_state));
krista@1924
  2413
    handle.realname = realname;
krista@1924
  2414
    handle.email = email;
krista@1924
  2415
krista@1924
  2416
    status = find_single_key(session, fpr, &key);
krista@1924
  2417
    if (status != PEP_STATUS_OK)
krista@1924
  2418
        return status;
krista@1924
  2419
krista@1924
  2420
    struct gpgme_data_cbs data_cbs;
krista@1924
  2421
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
krista@1924
  2422
    data_cbs.write = _nullwriter;
krista@1924
  2423
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
krista@1924
  2424
krista@2553
  2425
    gpgme_error = _GPGERR(gpg.gpgme_op_edit(session->ctx, key, replace_only_uid_fsm, &handle,
krista@2553
  2426
            output));
krista@1924
  2427
    assert(gpgme_error == GPG_ERR_NO_ERROR);
krista@1924
  2428
    if(gpgme_error != GPG_ERR_NO_ERROR) {
krista@1924
  2429
        status = PEP_CANNOT_EDIT_KEY;
krista@1924
  2430
    }
krista@1924
  2431
krista@1924
  2432
    gpg.gpgme_data_release(output);
krista@1924
  2433
    gpg.gpgme_key_unref(key);
krista@1924
  2434
krista@1924
  2435
    return status;
krista@1924
  2436
}
krista@1924
  2437
krista@1924
  2438
vb@201
  2439
typedef struct _renew_state {
vb@211
  2440
    enum {
vb@200
  2441
        renew_command = 0,
vb@200
  2442
        renew_date,
vb@200
  2443
        renew_secret_key,
vb@200
  2444
        renew_command2,
vb@200
  2445
        renew_date2,
vb@200
  2446
        renew_quit,
vb@200
  2447
        renew_save,
vb@200
  2448
        renew_exit,
vb@200
  2449
        renew_error = -1
vb@200
  2450
    } state;
vb@201
  2451
    const char *date_ref;
vb@201
  2452
} renew_state;
vb@200
  2453
vb@201
  2454
static gpgme_error_t renew_fsm(
vb@200
  2455
        void *_handle,
vb@200
  2456
        gpgme_status_code_t statuscode,
vb@200
  2457
        const char *args,
vb@200
  2458
        int fd
vb@200
  2459
    )
vb@200
  2460
{
vb@201
  2461
    renew_state *handle = _handle;
vb@200
  2462
vb@200
  2463
    switch (handle->state) {
vb@200
  2464
        case renew_command:
vb@200
  2465
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  2466
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  2467
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  2468
                    handle->state = renew_error;
vb@201
  2469
                    return GPG_ERR_GENERAL;
vb@201
  2470
                }
vb@223
  2471
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  2472
                handle->state = renew_date;
vb@200
  2473
            }
vb@200
  2474
            break;
vb@200
  2475
vb@200
  2476
        case renew_date:
vb@200
  2477
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  2478
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  2479
                if (strcmp(args, "keygen.valid")) {
vb@201
  2480
                    handle->state = renew_error;
vb@201
  2481
                    return GPG_ERR_GENERAL;
vb@201
  2482
                }
vb@223
  2483
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  2484
                handle->state = renew_secret_key;
vb@200
  2485
            }
vb@200
  2486
            break;
vb@200
  2487
vb@200
  2488
        case renew_secret_key:
vb@200
  2489
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  2490
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  2491
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  2492
                    handle->state = renew_error;
vb@201
  2493
                    return GPG_ERR_GENERAL;
vb@201
  2494
                }
vb@223
  2495
                gpg.gpgme_io_write(fd, "key 1\n", 6);
vb@200
  2496
                handle->state = renew_command2;
vb@200
  2497
            }
vb@200
  2498
            break;
vb@200
  2499
vb@200
  2500
        case renew_command2:
vb@200
  2501
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  2502
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  2503
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  2504
                    handle->state = renew_error;
vb@201
  2505
                    return GPG_ERR_GENERAL;
vb@201
  2506
                }
vb@223
  2507
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  2508
                handle->state = renew_date2;
vb@200
  2509
            }
vb@200
  2510
            break;
vb@200
  2511
vb@200
  2512
        case renew_date2:
vb@200
  2513
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  2514
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  2515
                if (strcmp(args, "keygen.valid")) {
vb@201
  2516
                    handle->state = renew_error;
vb@201
  2517
                    return GPG_ERR_GENERAL;
vb@201
  2518
                }
vb@223
  2519
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  2520
                handle->state = renew_quit;
vb@200
  2521
            }
vb@200
  2522
            break;
vb@200
  2523
vb@200
  2524
        case renew_quit:
vb@200
  2525
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  2526
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  2527
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  2528
                    handle->state = renew_error;
vb@201
  2529
                    return GPG_ERR_GENERAL;
vb@201
  2530
                }
vb@223
  2531
                gpg.gpgme_io_write(fd, "quit\n", 5);
vb@200
  2532
                handle->state = renew_save;
vb@200
  2533
            }
vb@200
  2534
            break;
vb@200
  2535
vb@200
  2536
        case renew_save:
vb@200
  2537
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@201
  2538
                assert(strcmp(args, "keyedit.save.okay") == 0);
vb@201
  2539
                if (strcmp(args, "keyedit.save.okay")) {
vb@201
  2540
                    handle->state = renew_error;
vb@201
  2541
                    return GPG_ERR_GENERAL;
vb@201
  2542
                }
vb@223
  2543
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@200
  2544
                handle->state = renew_exit;
vb@200
  2545
            }
vb@200
  2546
            break;
vb@200
  2547
vb@200
  2548
        case renew_exit:
vb@200
  2549
            break;
vb@200
  2550
vb@200
  2551
        case renew_error:
vb@200
  2552
            return GPG_ERR_GENERAL;
vb@200
  2553
    }
vb@200
  2554
vb@200
  2555
    return GPG_ERR_NO_ERROR;
vb@200
  2556
}
vb@200
  2557
vb@200
  2558
vb@201
  2559
PEP_STATUS pgp_renew_key(
vb@201
  2560
        PEP_SESSION session,
vb@201
  2561
        const char *fpr,
vb@201
  2562
        const timestamp *ts
vb@201
  2563
    )
vb@200
  2564
{
vb@200
  2565
    PEP_STATUS status = PEP_STATUS_OK;
vb@200
  2566
    gpgme_error_t gpgme_error;
vb@200
  2567
    gpgme_key_t key;
vb@200
  2568
    gpgme_data_t output;
vb@201
  2569
    renew_state handle;
vb@203
  2570
    char date_text[12];
vb@200
  2571
vb@200
  2572
    assert(session);
vb@200
  2573
    assert(fpr);
vb@200
  2574
vb@201
  2575
    memset(&handle, 0, sizeof(renew_state));
vb@203
  2576
    snprintf(date_text, 12, "%.4d-%.2d-%.2d\n", ts->tm_year + 1900,
vb@201
  2577
            ts->tm_mon + 1, ts->tm_mday);
vb@201
  2578
    handle.date_ref = date_text;
vb@200
  2579
vb@200
  2580
    status = find_single_key(session, fpr, &key);
vb@200
  2581
    if (status != PEP_STATUS_OK)
vb@200
  2582
        return status;
vb@200
  2583
vb@200
  2584
    struct gpgme_data_cbs data_cbs;
vb@200
  2585
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
vb@200
  2586
    data_cbs.write = _nullwriter;
vb@220
  2587
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
vb@200
  2588
krista@2553
  2589
    gpgme_error = _GPGERR(gpg.gpgme_op_edit(session->ctx, key, renew_fsm, &handle,
krista@2553
  2590
            output));
vb@200
  2591
    assert(gpgme_error == GPG_ERR_NO_ERROR);
edouard@1802
  2592
    if(gpgme_error != GPG_ERR_NO_ERROR) {
edouard@1802
  2593
        status = PEP_CANNOT_EDIT_KEY;
edouard@1802
  2594
    }
vb@200
  2595
vb@200
  2596
    gpg.gpgme_data_release(output);
vb@200
  2597
    gpg.gpgme_key_unref(key);
vb@200
  2598
edouard@1802
  2599
    return status;
vb@200
  2600
}
vb@200
  2601
vb@211
  2602
typedef struct _revoke_state {
vb@211
  2603
    enum {
vb@211
  2604
        revoke_command = 0,
vb@211
  2605
        revoke_approve,
vb@211
  2606
        revoke_reason_code,
vb@211
  2607
        revoke_reason_text,
vb@211
  2608
        revoke_reason_ok,
vb@211
  2609
        revoke_quit,
vb@211
  2610
        revoke_save,
vb@211
  2611
        revoke_exit,
vb@211
  2612
        revoke_error = -1
vb@211
  2613
    } state;
vb@211
  2614
    const char *reason_ref;
vb@211
  2615
} revoke_state;
vb@211
  2616
roker@723
  2617
roker@723
  2618
/*** unused?
vb@211
  2619
static bool isemptystring(const char *str)
vb@211
  2620
{
vb@211
  2621
    if (str == NULL)
vb@211
  2622
        return true;
vb@211
  2623
vb@211
  2624
    for (; str; str++) {
vb@211
  2625
        if (*str != ' ' && *str != '\t' && *str != '\n')
vb@211
  2626
            return false;
vb@211
  2627
    }
vb@211
  2628
vb@211
  2629
    return true;
vb@211
  2630
}
roker@723
  2631
***/
roker@723
  2632
vb@211
  2633
vb@211
  2634
static gpgme_error_t revoke_fsm(
vb@211
  2635
        void *_handle,
vb@211
  2636
        gpgme_status_code_t statuscode,
vb@211
  2637
        const char *args,
vb@211
  2638
        int fd
vb@211
  2639
    )
vb@211
  2640
{
vb@211
  2641
    revoke_state *handle = _handle;
vb@211
  2642
vb@211
  2643
    switch (handle->state) {
vb@211
  2644
        case revoke_command:
vb@211
  2645
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  2646
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@211
  2647
                if (strcmp(args, "keyedit.prompt")) {
vb@211
  2648
                    handle->state = revoke_error;
vb@211
  2649
                    return GPG_ERR_GENERAL;
vb@211
  2650
                }
vb@223
  2651
                gpg.gpgme_io_write(fd, "revkey\n", 7);
vb@211
  2652
                handle->state = revoke_approve;
vb@211
  2653
            }
vb@211
  2654
            break;
vb@211
  2655
vb@211
  2656
        case revoke_approve: