src/pgp_gpg.c
author Krista Bennett <krista@pep-project.org>
Tue, 22 May 2018 09:35:15 +0200
changeset 2681 52835b311e85
parent 2553 f66efd53d8b1
child 2690 82d925538dd1
child 2691 6e7b73c67701
permissions -rw-r--r--
allow non-email in find_keys (grrrr)
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);
krista@2681
   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@2681
   630
                // EFail: We should get an MDC warning if there were modifications
krista@2681
   631
                //        and never make it here. So the decrypted text is not
krista@2681
   632
                //        returned regardless.
krista@1579
   633
                gpgme_decrypt_result = gpg.gpgme_op_decrypt_result(session->ctx);
krista@1591
   634
                /* NOW is when we have to process the decrypt_result, period.
krista@1591
   635
                   it is only valid until the next call on the context. */
krista@1591
   636
                   
krista@1591
   637
                gpgme_key_t key;
krista@1591
   638
                memset(&key,0,sizeof(key));
krista@1591
   639
                stringlist_t* recipient_keylist = new_stringlist(NULL);
krista@1592
   640
                if (!recipient_keylist) {
krista@1592
   641
                    gpg.gpgme_data_release(plain);
krista@1592
   642
                    gpg.gpgme_data_release(cipher);
krista@1592
   643
                    if (recipient_keylist)
krista@1592
   644
                        free_stringlist(recipient_keylist);
krista@1591
   645
                    return PEP_OUT_OF_MEMORY;
krista@1592
   646
                }
krista@1591
   647
               
krista@1591
   648
                if (gpgme_decrypt_result != NULL) {
krista@1591
   649
                    stringlist_t* _keylist = recipient_keylist;
krista@1591
   650
                    for (gpgme_recipient_t r = gpgme_decrypt_result->recipients; r != NULL; r = r->next) {
krista@1591
   651
                        // GPGME may give subkey's fpr instead of primary key's fpr.
krista@1591
   652
                        // Therefore we ask for the primary fingerprint instead
krista@1591
   653
                        // we assume that gpgme_get_key can find key by subkey's fpr
krista@1591
   654
                        gpgme_error = gpg.gpgme_get_key(session->ctx,
krista@1591
   655
                            r->keyid, &key, 0);
krista@1591
   656
                        gpgme_error = _GPGERR(gpgme_error);
krista@1591
   657
                        assert(gpgme_error != GPG_ERR_ENOMEM);
krista@1591
   658
                        if (gpgme_error == GPG_ERR_ENOMEM) {
krista@1591
   659
                            free_stringlist(_keylist);
krista@1591
   660
                            result = PEP_OUT_OF_MEMORY;
krista@1591
   661
                        }
krista@1591
   662
                        // Primary key is given as the first subkey
krista@1591
   663
                        if (gpgme_error == GPG_ERR_NO_ERROR &&
krista@1591
   664
                            key && key->subkeys && key->subkeys->fpr
krista@1591
   665
                            && key->subkeys->fpr[0]) {
krista@1591
   666
                            _keylist = stringlist_add(_keylist, key->subkeys->fpr);
krista@1591
   667
 
krista@1591
   668
                            gpg.gpgme_key_unref(key);
krista@1591
   669
 
krista@1591
   670
                        }
krista@1591
   671
                    }
krista@1591
   672
                    assert(_keylist);
krista@1591
   673
                    if (_keylist == NULL) {
krista@1591
   674
                        free_stringlist(recipient_keylist);
krista@1591
   675
                        if (*keylist)
krista@1591
   676
                            free_stringlist(*keylist);
krista@1591
   677
                        *keylist = NULL;
krista@1591
   678
                        result = PEP_OUT_OF_MEMORY;
krista@1591
   679
                    }
krista@1591
   680
                } /* Ok, so now we have any recipients it was encrypted for
krista@1591
   681
                     in recipient_keylist */
krista@1591
   682
            
krista@1591
   683
                   
vb@344
   684
                gpgme_verify_result_t gpgme_verify_result;
vb@344
   685
                char *_buffer = NULL;
vb@344
   686
                size_t reading;
vb@344
   687
                size_t length = gpg.gpgme_data_seek(plain, 0, SEEK_END);
vb@344
   688
                gpgme_signature_t gpgme_signature;
vb@24
   689
vb@344
   690
                assert(length != -1);
vb@344
   691
                gpg.gpgme_data_seek(plain, 0, SEEK_SET);
vb@24
   692
vb@344
   693
                // TODO: make things less memory consuming
vb@344
   694
                // the following algorithm allocates memory for the complete
vb@344
   695
                // text
vb@24
   696
vb@344
   697
                _buffer = malloc(length + 1);
vb@344
   698
                assert(_buffer);
vb@344
   699
                if (_buffer == NULL) {
vb@74
   700
                    gpg.gpgme_data_release(plain);
vb@74
   701
                    gpg.gpgme_data_release(cipher);
krista@1591
   702
                    if (recipient_keylist)
krista@1591
   703
                        free_stringlist(recipient_keylist);
vb@24
   704
                    return PEP_OUT_OF_MEMORY;
vb@24
   705
                }
vb@24
   706
vb@344
   707
                reading = gpg.gpgme_data_read(plain, _buffer, length);
vb@344
   708
                assert(length == reading);
vb@24
   709
krista@1427
   710
                if (dsigtext) {  // Is this safe to do?
krista@1427
   711
                    gpgme_data_t sigdata;
krista@1427
   712
                    gpg.gpgme_data_new_from_mem(&sigdata, dsigtext,
krista@1427
   713
                                                dsigsize, 0);
vb@1434
   714
                    gpg.gpgme_op_verify(session->ctx, sigdata, plain, NULL);
krista@1431
   715
                    gpg.gpgme_data_release(sigdata);
krista@1427
   716
                }
krista@1426
   717
vb@344
   718
                gpgme_verify_result =
vb@344
   719
                    gpg.gpgme_op_verify_result(session->ctx);
vb@344
   720
                assert(gpgme_verify_result);
vb@344
   721
                gpgme_signature = gpgme_verify_result->signatures;
krista@1426
   722
krista@1584
   723
                if (!gpgme_signature) {
krista@1584
   724
                    // try cleartext sig verification
krista@1584
   725
                    gpg.gpgme_op_verify(session->ctx, plain, NULL, plain);
krista@1584
   726
                    gpgme_verify_result =
krista@1584
   727
                        gpg.gpgme_op_verify_result(session->ctx);
krista@1584
   728
                            assert(gpgme_verify_result);
roker@2055
   729
                    gpgme_signature = gpgme_verify_result->signatures;
krista@1584
   730
                }
krista@1584
   731
vb@344
   732
                if (gpgme_signature) {
vb@344
   733
                    stringlist_t *k;
vb@344
   734
                    _keylist = new_stringlist(NULL);
vb@24
   735
                    assert(_keylist);
vb@24
   736
                    if (_keylist == NULL) {
vb@344
   737
                        gpg.gpgme_data_release(plain);
vb@344
   738
                        gpg.gpgme_data_release(cipher);
vb@344
   739
                        free(_buffer);
vb@344
   740
                        return PEP_OUT_OF_MEMORY;
vb@344
   741
                    }
vb@344
   742
                    k = _keylist;
vb@344
   743
vb@344
   744
                    result = PEP_DECRYPTED_AND_VERIFIED;
krista@1427
   745
                    gpg.gpgme_check(NULL);
krista@1591
   746
                    do { /* get all signers and put them at the front off
krista@1591
   747
                            the keylist (likely only one) */
vb@344
   748
                        switch (_GPGERR(gpgme_signature->status)) {
vb@344
   749
                        case GPG_ERR_NO_ERROR:
Edouard@505
   750
                        {
krista@1426
   751
                            // Some versions of gpg returns signer's
Edouard@674
   752
                            // signing subkey fingerprint instead of
Edouard@674
   753
                            // signer's primary key fingerprint.
krista@1426
   754
                            // This is meant to get signer's primary
Edouard@674
   755
                            // key fingerprint, using subkey's.
Edouard@674
   756
Edouard@728
   757
                            gpgme_key_t key = NULL;
Edouard@505
   758
Edouard@505
   759
                            gpgme_error = gpg.gpgme_get_key(session->ctx,
Edouard@505
   760
                                gpgme_signature->fpr, &key, 0);
Edouard@505
   761
                            gpgme_error = _GPGERR(gpgme_error);
Edouard@505
   762
                            assert(gpgme_error != GPG_ERR_ENOMEM);
Edouard@505
   763
                            if (gpgme_error == GPG_ERR_ENOMEM) {
Edouard@505
   764
                                free_stringlist(_keylist);
Edouard@505
   765
                                gpg.gpgme_data_release(plain);
Edouard@505
   766
                                gpg.gpgme_data_release(cipher);
Edouard@505
   767
                                free(_buffer);
Edouard@505
   768
                                return PEP_OUT_OF_MEMORY;
Edouard@505
   769
                            }
Edouard@505
   770
                            // Primary key is given as the first subkey
krista@1426
   771
                            if (gpgme_error == GPG_ERR_NO_ERROR &&
krista@1426
   772
                                key && key->subkeys && key->subkeys->fpr
Edouard@674
   773
                                && key->subkeys->fpr[0])
Edouard@674
   774
                            {
Edouard@505
   775
                                k = stringlist_add(k, key->subkeys->fpr);
Edouard@677
   776
Edouard@674
   777
                                gpg.gpgme_key_unref(key);
Edouard@677
   778
Edouard@677
   779
                                if (k == NULL) {
Edouard@677
   780
                                    free_stringlist(_keylist);
krista@1591
   781
                                    if (recipient_keylist)
krista@1591
   782
                                        free (recipient_keylist);
Edouard@677
   783
                                    gpg.gpgme_data_release(plain);
Edouard@677
   784
                                    gpg.gpgme_data_release(cipher);
Edouard@677
   785
                                    free(_buffer);
Edouard@677
   786
                                    return PEP_OUT_OF_MEMORY;
Edouard@677
   787
                                }
Edouard@674
   788
                            }
krista@1426
   789
                            else
Edouard@674
   790
                            {
Edouard@505
   791
                                result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
Edouard@505
   792
                                break;
Edouard@505
   793
                            }
vb@344
   794
                            break;
Edouard@505
   795
                        }
vb@344
   796
                        case GPG_ERR_CERT_REVOKED:
vb@344
   797
                        case GPG_ERR_BAD_SIGNATURE:
vb@344
   798
                            result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
vb@344
   799
                            break;
vb@344
   800
                        case GPG_ERR_SIG_EXPIRED:
vb@344
   801
                        case GPG_ERR_KEY_EXPIRED:
vb@344
   802
                        case GPG_ERR_NO_PUBKEY:
vb@344
   803
                            k = stringlist_add(k, gpgme_signature->fpr);
Edouard@674
   804
                            if (k == NULL) {
Edouard@674
   805
                                free_stringlist(_keylist);
krista@1591
   806
                                if (recipient_keylist)
krista@1591
   807
                                    free_stringlist(recipient_keylist);
Edouard@674
   808
                                gpg.gpgme_data_release(plain);
Edouard@674
   809
                                gpg.gpgme_data_release(cipher);
Edouard@674
   810
                                free(_buffer);
Edouard@674
   811
                                return PEP_OUT_OF_MEMORY;
Edouard@674
   812
                            }
vb@344
   813
                            if (result == PEP_DECRYPTED_AND_VERIFIED)
vb@344
   814
                                result = PEP_DECRYPTED;
vb@344
   815
                            break;
vb@344
   816
                        case GPG_ERR_GENERAL:
vb@344
   817
                            break;
vb@344
   818
                        default:
vb@344
   819
                            if (result == PEP_DECRYPTED_AND_VERIFIED)
vb@344
   820
                                result = PEP_DECRYPTED;
vb@344
   821
                            break;
vb@344
   822
                        }
vb@344
   823
                    } while ((gpgme_signature = gpgme_signature->next));
vb@344
   824
                }
vb@344
   825
                else {
vb@344
   826
                    result = PEP_DECRYPTED;
vb@344
   827
                }
vb@344
   828
vb@344
   829
                if (result == PEP_DECRYPTED_AND_VERIFIED
vb@344
   830
                    || result == PEP_DECRYPTED) {
vb@344
   831
                    *ptext = _buffer;
vb@344
   832
                    *psize = reading;
vb@344
   833
                    (*ptext)[*psize] = 0; // safeguard for naive users
vb@344
   834
                    *keylist = _keylist;
edouard@1933
   835
                    if (recipient_keylist) {
krista@1591
   836
                        if (!_keylist)
roker@2055
   837
                            *keylist = new_stringlist(""); // no sig
krista@1591
   838
                        if (!(*keylist)) {
krista@1591
   839
                            free_stringlist(_keylist);
krista@1591
   840
                            if (recipient_keylist)
krista@1591
   841
                                free_stringlist(recipient_keylist);
krista@1591
   842
                            gpg.gpgme_data_release(plain);
krista@1591
   843
                            gpg.gpgme_data_release(cipher);
krista@1591
   844
                            free(_buffer);
krista@1591
   845
                            return PEP_OUT_OF_MEMORY;
roker@2055
   846
                        }
krista@1591
   847
                        stringlist_append(*keylist, recipient_keylist);
edouard@1933
   848
                    }
vb@344
   849
                }
vb@344
   850
                else {
vb@344
   851
                    free_stringlist(_keylist);
krista@1591
   852
                    if (recipient_keylist)
krista@1591
   853
                        free_stringlist(recipient_keylist);
vb@344
   854
                    free(_buffer);
vb@344
   855
                }
vb@344
   856
                break;
vb@344
   857
            }
vb@344
   858
            case GPG_ERR_BAD_PASSPHRASE:
krista@2681
   859
            case GPG_ERR_NO_DATA:
Edouard@725
   860
                result = PEP_DECRYPT_NO_KEY;
Edouard@725
   861
                break;
vb@344
   862
            case GPG_ERR_DECRYPT_FAILED:
vb@344
   863
            default:
vb@344
   864
            {
krista@1579
   865
                gpgme_decrypt_result = gpg.gpgme_op_decrypt_result(session->ctx);
vb@344
   866
                result = PEP_DECRYPT_NO_KEY;
vb@344
   867
vb@344
   868
                if (gpgme_decrypt_result != NULL) {
vb@344
   869
                    if (gpgme_decrypt_result->unsupported_algorithm)
vb@344
   870
                        *keylist = new_stringlist(gpgme_decrypt_result->unsupported_algorithm);
vb@344
   871
                    else
vb@344
   872
                        *keylist = new_stringlist("");
vb@344
   873
                    assert(*keylist);
vb@344
   874
                    if (*keylist == NULL) {
vb@24
   875
                        result = PEP_OUT_OF_MEMORY;
vb@24
   876
                        break;
vb@24
   877
                    }
vb@24
   878
                }
vb@24
   879
            }
vb@24
   880
        }
vb@24
   881
        break;
vb@24
   882
vb@24
   883
    default:
vb@24
   884
        result = PEP_DECRYPT_WRONG_FORMAT;
vb@24
   885
    }
vb@24
   886
vb@74
   887
    gpg.gpgme_data_release(plain);
vb@74
   888
    gpg.gpgme_data_release(cipher);
vb@24
   889
    return result;
vb@24
   890
}
vb@24
   891
vb@24
   892
PEP_STATUS pgp_verify_text(
vb@24
   893
    PEP_SESSION session, const char *text, size_t size,
vb@24
   894
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
   895
    )
vb@24
   896
{
vb@24
   897
    PEP_STATUS result;
vb@24
   898
    gpgme_error_t gpgme_error;
vb@24
   899
    gpgme_data_t d_text, d_sig;
vb@24
   900
    stringlist_t *_keylist;
vb@24
   901
vb@24
   902
    assert(session);
vb@24
   903
    assert(text);
vb@24
   904
    assert(size);
vb@24
   905
    assert(signature);
vb@24
   906
    assert(sig_size);
vb@24
   907
    assert(keylist);
vb@24
   908
vb@24
   909
    *keylist = NULL;
vb@24
   910
vb@74
   911
    gpgme_error = gpg.gpgme_data_new_from_mem(&d_text, text, size, 0);
vb@45
   912
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   913
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   914
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@24
   915
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   916
            return PEP_OUT_OF_MEMORY;
vb@24
   917
        else
vb@24
   918
            return PEP_UNKNOWN_ERROR;
vb@24
   919
    }
vb@24
   920
vb@74
   921
    gpgme_error = gpg.gpgme_data_new_from_mem(&d_sig, signature, sig_size, 0);
vb@45
   922
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   923
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
   924
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@74
   925
        gpg.gpgme_data_release(d_text);
vb@24
   926
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
   927
            return PEP_OUT_OF_MEMORY;
vb@24
   928
        else
vb@24
   929
            return PEP_UNKNOWN_ERROR;
vb@24
   930
    }
vb@24
   931
vb@74
   932
    gpgme_error = gpg.gpgme_op_verify(session->ctx, d_sig, d_text, NULL);
vb@45
   933
    gpgme_error = _GPGERR(gpgme_error);
vb@24
   934
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
   935
vb@24
   936
    switch (gpgme_error) {
vb@24
   937
    case GPG_ERR_NO_ERROR:
vb@24
   938
    {
vb@24
   939
        gpgme_verify_result_t gpgme_verify_result;
vb@24
   940
        gpgme_signature_t gpgme_signature;
vb@24
   941
vb@24
   942
        gpgme_verify_result =
vb@74
   943
            gpg.gpgme_op_verify_result(session->ctx);
vb@24
   944
        assert(gpgme_verify_result);
vb@24
   945
        gpgme_signature = gpgme_verify_result->signatures;
vb@24
   946
vb@24
   947
        if (gpgme_signature) {
vb@24
   948
            stringlist_t *k;
vb@24
   949
            _keylist = new_stringlist(NULL);
vb@24
   950
            assert(_keylist);
vb@24
   951
            if (_keylist == NULL) {
vb@74
   952
                gpg.gpgme_data_release(d_text);
vb@74
   953
                gpg.gpgme_data_release(d_sig);
vb@24
   954
                return PEP_OUT_OF_MEMORY;
vb@24
   955
            }
vb@24
   956
            k = _keylist;
vb@24
   957
vb@24
   958
            result = PEP_VERIFIED;
vb@24
   959
            do {
Edouard@502
   960
                gpgme_key_t key;
Edouard@502
   961
                memset(&key,0,sizeof(key));
Edouard@502
   962
krista@1426
   963
                // GPGME may give subkey's fpr instead of primary key's fpr.
Edouard@502
   964
                // Therefore we ask for the primary fingerprint instead
Edouard@502
   965
                // we assume that gpgme_get_key can find key by subkey's fpr
Edouard@502
   966
                gpgme_error = gpg.gpgme_get_key(session->ctx,
Edouard@502
   967
                    gpgme_signature->fpr, &key, 0);
Edouard@502
   968
                gpgme_error = _GPGERR(gpgme_error);
Edouard@502
   969
                assert(gpgme_error != GPG_ERR_ENOMEM);
Edouard@502
   970
                if (gpgme_error == GPG_ERR_ENOMEM) {
vb@24
   971
                    free_stringlist(_keylist);
Edouard@505
   972
                    gpg.gpgme_data_release(d_text);
Edouard@505
   973
                    gpg.gpgme_data_release(d_sig);
vb@24
   974
                    return PEP_OUT_OF_MEMORY;
vb@24
   975
                }
Edouard@502
   976
                // Primary key is given as the first subkey
krista@1426
   977
                if (gpgme_error == GPG_ERR_NO_ERROR &&
krista@1426
   978
                    key && key->subkeys && key->subkeys->fpr
Edouard@674
   979
                    && key->subkeys->fpr[0])
Edouard@674
   980
                {
Edouard@504
   981
                    k = stringlist_add(k, key->subkeys->fpr);
Edouard@677
   982
Edouard@674
   983
                    gpg.gpgme_key_unref(key);
Edouard@677
   984
Edouard@677
   985
                    if (k == NULL) {
Edouard@677
   986
                        free_stringlist(_keylist);
Edouard@677
   987
                        gpg.gpgme_data_release(d_text);
Edouard@677
   988
                        gpg.gpgme_data_release(d_sig);
Edouard@677
   989
                        return PEP_OUT_OF_MEMORY;
Edouard@677
   990
                    }
Edouard@502
   991
                }
Edouard@502
   992
                else {
Edouard@502
   993
                    result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
Edouard@502
   994
                    break;
Edouard@502
   995
                }
Edouard@502
   996
vb@24
   997
                if (gpgme_signature->summary & GPGME_SIGSUM_RED) {
vb@24
   998
                    if (gpgme_signature->summary & GPGME_SIGSUM_KEY_EXPIRED
vb@24
   999
                        || gpgme_signature->summary & GPGME_SIGSUM_SIG_EXPIRED) {
vb@24
  1000
                        if (result == PEP_VERIFIED
vb@24
  1001
                            || result == PEP_VERIFIED_AND_TRUSTED)
vb@24
  1002
                            result = PEP_UNENCRYPTED;
vb@24
  1003
                    }
vb@24
  1004
                    else {
vb@24
  1005
                        result = PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
vb@24
  1006
                        break;
vb@24
  1007
                    }
vb@24
  1008
                }
vb@24
  1009
                else {
vb@24
  1010
                    if (gpgme_signature->summary & GPGME_SIGSUM_VALID) {
vb@24
  1011
                        if (result == PEP_VERIFIED)
vb@24
  1012
                            result = PEP_VERIFIED_AND_TRUSTED;
vb@24
  1013
                    }
vb@24
  1014
                    if (gpgme_signature->summary & GPGME_SIGSUM_GREEN) {
vb@24
  1015
                        // good
vb@24
  1016
                    }
vb@24
  1017
                    else if (gpgme_signature->summary & GPGME_SIGSUM_KEY_MISSING) {
vb@24
  1018
                        result = PEP_VERIFY_NO_KEY;
vb@24
  1019
                    }
vb@24
  1020
                    else if (gpgme_signature->summary & GPGME_SIGSUM_SYS_ERROR) {
vb@24
  1021
                        if (result == PEP_VERIFIED
vb@24
  1022
                            || result == PEP_VERIFIED_AND_TRUSTED)
vb@24
  1023
                            result = PEP_UNENCRYPTED;
vb@24
  1024
                    }
vb@24
  1025
                    else {
vb@24
  1026
                        // do nothing
vb@24
  1027
                    }
vb@24
  1028
                }
vb@24
  1029
            } while ((gpgme_signature = gpgme_signature->next));
vb@24
  1030
            *keylist = _keylist;
vb@24
  1031
        }
vb@24
  1032
        else {
vb@24
  1033
            result = PEP_UNENCRYPTED;
vb@24
  1034
        }
vb@24
  1035
        break;
vb@24
  1036
    }
vb@24
  1037
        break;
vb@24
  1038
    case GPG_ERR_NO_DATA:
vb@24
  1039
        result = PEP_DECRYPT_WRONG_FORMAT;
vb@24
  1040
        break;
vb@24
  1041
    case GPG_ERR_INV_VALUE:
vb@24
  1042
    default:
vb@24
  1043
        result = PEP_UNKNOWN_ERROR;
vb@24
  1044
        break;
vb@24
  1045
    }
vb@24
  1046
vb@74
  1047
    gpg.gpgme_data_release(d_text);
vb@74
  1048
    gpg.gpgme_data_release(d_sig);
vb@24
  1049
vb@24
  1050
    return result;
vb@24
  1051
}
vb@24
  1052
krista@1639
  1053
krista@1639
  1054
static PEP_STATUS pgp_encrypt_sign_optional(    
vb@24
  1055
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
krista@1639
  1056
    size_t psize, char **ctext, size_t *csize, bool sign
krista@1639
  1057
)
roker@2055
  1058
{
vb@24
  1059
    PEP_STATUS result;
vb@24
  1060
    gpgme_error_t gpgme_error;
vb@24
  1061
    gpgme_data_t plain, cipher;
vb@24
  1062
    gpgme_key_t *rcpt;
vb@24
  1063
    gpgme_encrypt_flags_t flags;
vb@24
  1064
    const stringlist_t *_keylist;
vb@24
  1065
    int i, j;
vb@24
  1066
vb@46
  1067
    assert(session);
vb@24
  1068
    assert(keylist);
vb@24
  1069
    assert(ptext);
vb@24
  1070
    assert(psize);
vb@24
  1071
    assert(ctext);
vb@24
  1072
    assert(csize);
vb@24
  1073
vb@24
  1074
    *ctext = NULL;
vb@24
  1075
    *csize = 0;
vb@24
  1076
vb@74
  1077
    gpgme_error = gpg.gpgme_data_new_from_mem(&plain, ptext, psize, 0);
vb@45
  1078
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1079
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1080
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@24
  1081
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
  1082
            return PEP_OUT_OF_MEMORY;
vb@24
  1083
        else
vb@24
  1084
            return PEP_UNKNOWN_ERROR;
vb@24
  1085
    }
vb@24
  1086
vb@74
  1087
    gpgme_error = gpg.gpgme_data_new(&cipher);
vb@45
  1088
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1089
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1090
    if (gpgme_error != GPG_ERR_NO_ERROR) {
vb@74
  1091
        gpg.gpgme_data_release(plain);
vb@24
  1092
        if (gpgme_error == GPG_ERR_ENOMEM)
vb@24
  1093
            return PEP_OUT_OF_MEMORY;
vb@24
  1094
        else
vb@24
  1095
            return PEP_UNKNOWN_ERROR;
vb@24
  1096
    }
vb@24
  1097
vb@109
  1098
    rcpt = calloc(stringlist_length(keylist) + 1, sizeof(gpgme_key_t));
vb@24
  1099
    assert(rcpt);
vb@24
  1100
    if (rcpt == NULL) {
vb@74
  1101
        gpg.gpgme_data_release(plain);
vb@74
  1102
        gpg.gpgme_data_release(cipher);
vb@24
  1103
        return PEP_OUT_OF_MEMORY;
vb@24
  1104
    }
vb@24
  1105
vb@74
  1106
    gpg.gpgme_signers_clear(session->ctx);
vb@24
  1107
vb@24
  1108
    for (_keylist = keylist, i = 0; _keylist != NULL; _keylist = _keylist->next, i++) {
vb@24
  1109
        assert(_keylist->value);
vb@74
  1110
        gpgme_error = gpg.gpgme_get_key(session->ctx, _keylist->value,
vb@24
  1111
            &rcpt[i], 0);
vb@45
  1112
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1113
        assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1114
vb@24
  1115
        switch (gpgme_error) {
vb@24
  1116
        case GPG_ERR_ENOMEM:
vb@24
  1117
            for (j = 0; j<i; j++)
vb@74
  1118
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
  1119
            free(rcpt);
vb@74
  1120
            gpg.gpgme_data_release(plain);
vb@74
  1121
            gpg.gpgme_data_release(cipher);
vb@24
  1122
            return PEP_OUT_OF_MEMORY;
vb@24
  1123
        case GPG_ERR_NO_ERROR:
krista@1639
  1124
            if (i == 0 && sign) {
vb@74
  1125
                gpgme_error_t _gpgme_error = gpg.gpgme_signers_add(session->ctx, rcpt[0]);
vb@45
  1126
                _gpgme_error = _GPGERR(_gpgme_error);
vb@24
  1127
                assert(_gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1128
            }
vb@24
  1129
            break;
vb@24
  1130
        case GPG_ERR_EOF:
vb@24
  1131
            for (j = 0; j<i; j++)
vb@74
  1132
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
  1133
            free(rcpt);
vb@74
  1134
            gpg.gpgme_data_release(plain);
vb@74
  1135
            gpg.gpgme_data_release(cipher);
vb@24
  1136
            return PEP_KEY_NOT_FOUND;
vb@24
  1137
        case GPG_ERR_AMBIGUOUS_NAME:
vb@24
  1138
            for (j = 0; j<i; j++)
vb@74
  1139
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
  1140
            free(rcpt);
vb@74
  1141
            gpg.gpgme_data_release(plain);
vb@74
  1142
            gpg.gpgme_data_release(cipher);
vb@24
  1143
            return PEP_KEY_HAS_AMBIG_NAME;
vb@24
  1144
        default: // GPG_ERR_INV_VALUE if CTX or R_KEY is not a valid pointer or
vb@24
  1145
            // FPR is not a fingerprint or key ID
vb@24
  1146
            for (j = 0; j<i; j++)
vb@74
  1147
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
  1148
            free(rcpt);
vb@74
  1149
            gpg.gpgme_data_release(plain);
vb@74
  1150
            gpg.gpgme_data_release(cipher);
vb@24
  1151
            return PEP_GET_KEY_FAILED;
vb@24
  1152
        }
vb@24
  1153
    }
vb@24
  1154
vb@24
  1155
    // TODO: remove that and replace with proper key management
vb@24
  1156
    flags = GPGME_ENCRYPT_ALWAYS_TRUST;
krista@1639
  1157
    
krista@1639
  1158
    if (sign) {
krista@1639
  1159
        gpgme_error = gpg.gpgme_op_encrypt_sign(session->ctx, rcpt, flags,
krista@1639
  1160
            plain, cipher);
krista@1639
  1161
    }
krista@1639
  1162
    else {
krista@1639
  1163
        gpgme_error = gpg.gpgme_op_encrypt(session->ctx, rcpt, flags,
krista@1639
  1164
            plain, cipher);
krista@1639
  1165
    }
krista@1639
  1166
    
vb@45
  1167
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1168
    switch (gpgme_error) {
vb@24
  1169
    case GPG_ERR_NO_ERROR:
vb@24
  1170
    {
vb@24
  1171
        char *_buffer = NULL;
vb@24
  1172
        size_t reading;
vb@74
  1173
        size_t length = gpg.gpgme_data_seek(cipher, 0, SEEK_END);
vb@24
  1174
        assert(length != -1);
vb@74
  1175
        gpg.gpgme_data_seek(cipher, 0, SEEK_SET);
vb@24
  1176
vb@24
  1177
        // TODO: make things less memory consuming
vb@24
  1178
        // the following algorithm allocates a buffer for the complete text
vb@24
  1179
vb@112
  1180
        _buffer = malloc(length + 1);
vb@24
  1181
        assert(_buffer);
vb@24
  1182
        if (_buffer == NULL) {
vb@24
  1183
            for (j = 0; j<stringlist_length(keylist); j++)
vb@74
  1184
                gpg.gpgme_key_unref(rcpt[j]);
vb@24
  1185
            free(rcpt);
vb@74
  1186
            gpg.gpgme_data_release(plain);
vb@74
  1187
            gpg.gpgme_data_release(cipher);
vb@24
  1188
            return PEP_OUT_OF_MEMORY;
vb@24
  1189
        }
vb@24
  1190
vb@74
  1191
        reading = gpg.gpgme_data_read(cipher, _buffer, length);
vb@24
  1192
        assert(length == reading);
vb@24
  1193
vb@24
  1194
        *ctext = _buffer;
vb@24
  1195
        *csize = reading;
vb@24
  1196
        (*ctext)[*csize] = 0; // safeguard for naive users
vb@24
  1197
        result = PEP_STATUS_OK;
vb@24
  1198
        break;
vb@24
  1199
    }
vb@24
  1200
    default:
vb@24
  1201
        result = PEP_UNKNOWN_ERROR;
vb@24
  1202
    }
vb@24
  1203
vb@24
  1204
    for (j = 0; j<stringlist_length(keylist); j++)
vb@74
  1205
        gpg.gpgme_key_unref(rcpt[j]);
vb@24
  1206
    free(rcpt);
vb@74
  1207
    gpg.gpgme_data_release(plain);
vb@74
  1208
    gpg.gpgme_data_release(cipher);
vb@24
  1209
    return result;
vb@24
  1210
}
vb@24
  1211
krista@1639
  1212
PEP_STATUS pgp_encrypt_only(
krista@1639
  1213
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
krista@1639
  1214
    size_t psize, char **ctext, size_t *csize
krista@1639
  1215
    )
krista@1639
  1216
{
krista@1639
  1217
    return pgp_encrypt_sign_optional(session, keylist, ptext,
krista@1639
  1218
        psize, ctext, csize, false);
krista@1639
  1219
}
krista@1639
  1220
krista@1639
  1221
PEP_STATUS pgp_encrypt_and_sign(
krista@1639
  1222
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
krista@1639
  1223
    size_t psize, char **ctext, size_t *csize
krista@1639
  1224
    )
krista@1639
  1225
{
krista@1639
  1226
    return pgp_encrypt_sign_optional(session, keylist, ptext,
krista@1639
  1227
        psize, ctext, csize, true);
krista@1639
  1228
}
krista@1639
  1229
krista@1925
  1230
krista@1925
  1231
static PEP_STATUS find_single_key(
krista@1925
  1232
        PEP_SESSION session,
krista@1925
  1233
        const char *fpr,
krista@1925
  1234
        gpgme_key_t *key
krista@1925
  1235
    )
krista@1925
  1236
{
krista@1925
  1237
    gpgme_error_t gpgme_error;
krista@1925
  1238
krista@1925
  1239
    *key = NULL;
krista@1925
  1240
kgrothoff@1926
  1241
//    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
kgrothoff@1926
  1242
kgrothoff@1926
  1243
    gpgme_error = gpg.gpgme_get_key(session->ctx, fpr, key, 0);
kgrothoff@1926
  1244
krista@1925
  1245
    gpgme_error = _GPGERR(gpgme_error);
krista@1925
  1246
    switch (gpgme_error) {
krista@1925
  1247
    case GPG_ERR_NO_ERROR:
krista@1925
  1248
        break;
krista@1925
  1249
    case GPG_ERR_INV_VALUE:
krista@1925
  1250
        assert(0);
krista@1925
  1251
        return PEP_UNKNOWN_ERROR;
krista@1925
  1252
    default:
krista@1925
  1253
        return PEP_GET_KEY_FAILED;
krista@1925
  1254
    };
krista@1925
  1255
kgrothoff@1926
  1256
//    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, key);
kgrothoff@1926
  1257
//    gpgme_error = _GPGERR(gpgme_error);
kgrothoff@1926
  1258
//    assert(gpgme_error != GPG_ERR_INV_VALUE);
krista@1925
  1259
kgrothoff@1926
  1260
//    gpg.gpgme_op_keylist_end(session->ctx);
krista@1925
  1261
krista@1925
  1262
    return PEP_STATUS_OK;
krista@1925
  1263
}
krista@1925
  1264
krista@1925
  1265
krista@1914
  1266
static PEP_STATUS _pgp_createkey(PEP_SESSION session, pEp_identity *identity) {
krista@1914
  1267
    PEP_STATUS status = PEP_VERSION_MISMATCH;
krista@1914
  1268
roker@2055
  1269
    if (identity && identity->address) {
krista@1914
  1270
#ifdef GPGME_VERSION_NUMBER 
krista@1914
  1271
#if (GPGME_VERSION_NUMBER >= 0x010700)
krista@1920
  1272
        gpgme_error_t gpgme_error;
kgrothoff@1926
  1273
        int userid_size = strlen(identity->address) + 1;
krista@1921
  1274
        char* userid = (char*)(calloc(1, userid_size));
krista@1921
  1275
        if (!userid)
krista@1921
  1276
            return PEP_OUT_OF_MEMORY;
kgrothoff@1926
  1277
        strlcpy(userid, identity->address, userid_size);
krista@1921
  1278
        gpgme_error = gpg.gpgme_op_createkey(session->ctx, userid, "RSA", 
krista@1924
  1279
                                             0, 31536000, NULL, 
krista@1927
  1280
                                             GPGME_CREATE_NOPASSWD | GPGME_CREATE_FORCE);
krista@1920
  1281
        gpgme_error = _GPGERR(gpgme_error);
krista@1921
  1282
krista@1921
  1283
        free(userid);
krista@1921
  1284
krista@1920
  1285
        if (gpgme_error != GPG_ERR_NOT_SUPPORTED) {
krista@1920
  1286
            switch (gpgme_error) {
krista@1924
  1287
                case GPG_ERR_NO_ERROR:
roker@2055
  1288
                    break;
krista@1924
  1289
                case GPG_ERR_INV_VALUE:
krista@1924
  1290
                    return PEP_ILLEGAL_VALUE;
krista@1924
  1291
                case GPG_ERR_GENERAL:
krista@1924
  1292
                    return PEP_CANNOT_CREATE_KEY;
krista@1924
  1293
                default:
krista@1924
  1294
                    assert(0);
krista@1924
  1295
                    return PEP_UNKNOWN_ERROR;
roker@2055
  1296
            }
krista@1924
  1297
krista@1924
  1298
            /* This is the same regardless of whether we got it from genkey or createkey */
krista@1924
  1299
            gpgme_genkey_result_t gpgme_genkey_result = gpg.gpgme_op_genkey_result(session->ctx);
krista@1924
  1300
            assert(gpgme_genkey_result);
krista@1924
  1301
            assert(gpgme_genkey_result->fpr);
krista@1924
  1302
kgrothoff@1926
  1303
            char* fpr = strdup(gpgme_genkey_result->fpr);
krista@1925
  1304
            gpgme_key_t key;
krista@1925
  1305
            PEP_STATUS key_status = find_single_key(session, fpr, &key);
krista@1925
  1306
            if (!key || key_status != PEP_STATUS_OK)
krista@1925
  1307
                return PEP_CANNOT_CREATE_KEY;
roker@2055
  1308
            
krista@1925
  1309
            gpgme_error = gpg.gpgme_op_createsubkey(session->ctx, key, 
krista@1925
  1310
                                                    "RSA", 0, 
krista@1925
  1311
                                                    31536000, GPGME_CREATE_NOPASSWD 
krista@1925
  1312
                                                    | GPGME_CREATE_ENCR);
krista@1925
  1313
krista@1925
  1314
            switch (gpgme_error) {
krista@1925
  1315
                case GPG_ERR_NO_ERROR:
roker@2055
  1316
                    break;
krista@1925
  1317
                case GPG_ERR_INV_VALUE:
krista@1925
  1318
                    return PEP_ILLEGAL_VALUE;
krista@1925
  1319
                case GPG_ERR_GENERAL:
krista@1925
  1320
                    return PEP_CANNOT_CREATE_KEY;
krista@1925
  1321
                default:
krista@1925
  1322
                    assert(0);
krista@1925
  1323
                    return PEP_UNKNOWN_ERROR;
roker@2055
  1324
            }
krista@1925
  1325
            
krista@1924
  1326
            free(identity->fpr);
kgrothoff@1926
  1327
            identity->fpr = fpr;
krista@1924
  1328
            if (identity->fpr == NULL)
krista@1924
  1329
                return PEP_OUT_OF_MEMORY;
krista@1924
  1330
kgrothoff@1926
  1331
//            gpg.gpgme_key_unref(key);
krista@1925
  1332
            
kgrothoff@1926
  1333
            status = pgp_replace_only_uid(session, fpr,
roker@2055
  1334
                        identity->username, identity->address);
krista@1920
  1335
        }
krista@1914
  1336
#endif
krista@1914
  1337
#endif
krista@1914
  1338
    }
krista@1914
  1339
    
krista@1914
  1340
    return status;
krista@1914
  1341
}
krista@1914
  1342
vb@24
  1343
PEP_STATUS pgp_generate_keypair(
vb@24
  1344
    PEP_SESSION session, pEp_identity *identity
vb@24
  1345
    )
vb@24
  1346
{
vb@24
  1347
    assert(session);
vb@24
  1348
    assert(identity);
vb@24
  1349
    assert(identity->address);
vb@298
  1350
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@24
  1351
    assert(identity->username);
vb@24
  1352
krista@1914
  1353
    PEP_STATUS status = _pgp_createkey(session, identity);
krista@1914
  1354
    
krista@1924
  1355
    if (status != PEP_VERSION_MISMATCH)
krista@1914
  1356
        return status;
krista@1924
  1357
        
krista@1924
  1358
    gpgme_error_t gpgme_error;
krista@1924
  1359
    char *parms;
krista@1924
  1360
    const char *template =
krista@1924
  1361
        "<GnupgKeyParms format=\"internal\">\n"
krista@1924
  1362
        "Key-Type: RSA\n"
krista@1924
  1363
        "Key-Length: 4096\n"
krista@1924
  1364
        "Subkey-Type: RSA\n"
krista@1924
  1365
        "Subkey-Length: 4096\n"
krista@1924
  1366
        "Name-Real: %s\n"
krista@1924
  1367
        "Name-Email: %s\n"
krista@1924
  1368
        /* "Passphrase: %s\n" */
krista@1924
  1369
        "Expire-Date: 1y\n"
krista@1924
  1370
        "</GnupgKeyParms>\n";
krista@1924
  1371
    int result;
vb@24
  1372
krista@1924
  1373
    parms = calloc(1, PARMS_MAX);
krista@1924
  1374
    assert(parms);
krista@1924
  1375
    if (parms == NULL)
krista@1924
  1376
        return PEP_OUT_OF_MEMORY;
krista@1914
  1377
krista@1924
  1378
    result = snprintf(parms, PARMS_MAX, template, identity->username,
krista@1924
  1379
        identity->address); // , session->passphrase);
krista@1924
  1380
    assert(result < PARMS_MAX);
krista@1924
  1381
    if (result >= PARMS_MAX) {
vb@24
  1382
        free(parms);
krista@1924
  1383
        return PEP_BUFFER_TOO_SMALL;
vb@24
  1384
    }
vb@24
  1385
krista@1924
  1386
    gpgme_error = gpg.gpgme_op_genkey(session->ctx, parms, NULL, NULL);
krista@1924
  1387
    gpgme_error = _GPGERR(gpgme_error);
krista@1924
  1388
    free(parms);
krista@1924
  1389
krista@1924
  1390
    switch (gpgme_error) {
krista@1924
  1391
    case GPG_ERR_NO_ERROR:
krista@1924
  1392
        break;
krista@1924
  1393
    case GPG_ERR_INV_VALUE:
krista@1924
  1394
        return PEP_ILLEGAL_VALUE;
krista@1924
  1395
    case GPG_ERR_GENERAL:
krista@1924
  1396
        return PEP_CANNOT_CREATE_KEY;
krista@1924
  1397
    default:
krista@1924
  1398
        assert(0);
krista@1924
  1399
        return PEP_UNKNOWN_ERROR;
krista@1924
  1400
    }
krista@1924
  1401
krista@1914
  1402
    gpgme_genkey_result_t gpgme_genkey_result = gpg.gpgme_op_genkey_result(session->ctx);
vb@24
  1403
    assert(gpgme_genkey_result);
vb@24
  1404
    assert(gpgme_genkey_result->fpr);
vb@24
  1405
vb@298
  1406
    free(identity->fpr);
vb@24
  1407
    identity->fpr = strdup(gpgme_genkey_result->fpr);
vb@298
  1408
    if (identity->fpr == NULL)
vb@298
  1409
        return PEP_OUT_OF_MEMORY;
vb@24
  1410
vb@24
  1411
    return PEP_STATUS_OK;
vb@24
  1412
}
vb@24
  1413
vb@24
  1414
PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
  1415
{
vb@24
  1416
    gpgme_error_t gpgme_error;
vb@24
  1417
    gpgme_key_t key;
vb@24
  1418
vb@24
  1419
    assert(session);
vb@24
  1420
    assert(fpr);
vb@24
  1421
vb@74
  1422
    gpgme_error = gpg.gpgme_get_key(session->ctx, fpr, &key, 0);
vb@45
  1423
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1424
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1425
    switch (gpgme_error) {
vb@24
  1426
    case GPG_ERR_NO_ERROR:
vb@24
  1427
        break;
vb@24
  1428
    case GPG_ERR_EOF:
vb@24
  1429
        return PEP_KEY_NOT_FOUND;
vb@24
  1430
    case GPG_ERR_INV_VALUE:
vb@24
  1431
        return PEP_ILLEGAL_VALUE;
vb@24
  1432
    case GPG_ERR_AMBIGUOUS_NAME:
vb@24
  1433
        return PEP_KEY_HAS_AMBIG_NAME;
vb@24
  1434
    case GPG_ERR_ENOMEM:
vb@24
  1435
        return PEP_OUT_OF_MEMORY;
vb@24
  1436
    default:
vb@24
  1437
        assert(0);
vb@24
  1438
        return PEP_UNKNOWN_ERROR;
vb@24
  1439
    }
vb@24
  1440
vb@74
  1441
    gpgme_error = gpg.gpgme_op_delete(session->ctx, key, 1);
vb@45
  1442
    gpgme_error = _GPGERR(gpgme_error);
vb@74
  1443
    gpg.gpgme_key_unref(key);
vb@24
  1444
    switch (gpgme_error) {
vb@24
  1445
    case GPG_ERR_NO_ERROR:
vb@24
  1446
        break;
vb@24
  1447
    case GPG_ERR_INV_VALUE:
vb@24
  1448
        assert(0);
vb@24
  1449
        return PEP_UNKNOWN_ERROR;
vb@24
  1450
    case GPG_ERR_NO_PUBKEY:
vb@24
  1451
        assert(0);
vb@24
  1452
        return PEP_KEY_NOT_FOUND;
vb@24
  1453
    case GPG_ERR_AMBIGUOUS_NAME:
vb@24
  1454
        assert(0);
vb@24
  1455
        return PEP_KEY_HAS_AMBIG_NAME;
vb@24
  1456
    default:
vb@24
  1457
        assert(0);
vb@24
  1458
        return PEP_UNKNOWN_ERROR;
vb@24
  1459
    }
vb@24
  1460
vb@24
  1461
    return PEP_STATUS_OK;
vb@24
  1462
}
vb@24
  1463
Edouard@728
  1464
PEP_STATUS pgp_import_keydata(PEP_SESSION session, const char *key_data,
Edouard@728
  1465
                              size_t size, identity_list **private_idents)
vb@24
  1466
{
vb@24
  1467
    gpgme_error_t gpgme_error;
vb@24
  1468
    gpgme_data_t dh;
vb@24
  1469
vb@24
  1470
    assert(session);
vb@24
  1471
    assert(key_data);
krista@1426
  1472
krista@1426
  1473
    if(private_idents)
Edouard@728
  1474
        *private_idents = NULL;
vb@24
  1475
vb@74
  1476
    gpgme_error = gpg.gpgme_data_new_from_mem(&dh, key_data, size, 0);
vb@45
  1477
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1478
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1479
    switch (gpgme_error) {
vb@24
  1480
    case GPG_ERR_NO_ERROR:
vb@24
  1481
        break;
vb@24
  1482
    case GPG_ERR_ENOMEM:
vb@24
  1483
        return PEP_OUT_OF_MEMORY;
vb@24
  1484
    case GPG_ERR_INV_VALUE:
vb@24
  1485
        assert(0);
vb@24
  1486
        return PEP_UNKNOWN_ERROR;
vb@24
  1487
    default:
vb@24
  1488
        assert(0);
vb@24
  1489
        return PEP_UNKNOWN_ERROR;
vb@24
  1490
    }
vb@24
  1491
Edouard@728
  1492
    gpgme_import_result_t gpgme_import_result;
Edouard@728
  1493
vb@74
  1494
    gpgme_error = gpg.gpgme_op_import(session->ctx, dh);
vb@45
  1495
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1496
    switch (gpgme_error) {
vb@24
  1497
    case GPG_ERR_NO_ERROR:
krista@1426
  1498
        if(private_idents)
Edouard@728
  1499
        {
Edouard@728
  1500
            gpgme_import_result =
Edouard@728
  1501
                gpg.gpgme_op_import_result(session->ctx);
Edouard@728
  1502
            assert(gpgme_import_result);
vb@1302
  1503
            if (!gpgme_import_result) {
vb@1302
  1504
                gpg.gpgme_data_release(dh);
vb@1302
  1505
                return PEP_UNKNOWN_ERROR;
vb@1302
  1506
            }
vb@1302
  1507
Edouard@728
  1508
            gpgme_import_status_t import;
krista@1426
  1509
            for (import = gpgme_import_result->imports;
krista@1426
  1510
                 import;
Edouard@728
  1511
                 import = import->next)
Edouard@728
  1512
             {
Edouard@728
  1513
                if (import &&
Edouard@728
  1514
                    import->result == GPG_ERR_NO_ERROR &&
Edouard@728
  1515
                    import->status & GPGME_IMPORT_SECRET )
Edouard@728
  1516
                {
Edouard@728
  1517
                    gpgme_key_t key = NULL;
Edouard@728
  1518
Edouard@728
  1519
                    gpgme_error = gpg.gpgme_get_key(session->ctx,
Edouard@728
  1520
                        import->fpr, &key, 0);
Edouard@728
  1521
                    gpgme_error = _GPGERR(gpgme_error);
Edouard@728
  1522
                    assert(gpgme_error != GPG_ERR_ENOMEM);
Edouard@728
  1523
                    if (gpgme_error == GPG_ERR_ENOMEM) {
Edouard@728
  1524
                        gpg.gpgme_data_release(dh);
Edouard@728
  1525
                        return PEP_OUT_OF_MEMORY;
Edouard@728
  1526
                    }
krista@1426
  1527
krista@1426
  1528
                    if (gpgme_error == GPG_ERR_NO_ERROR &&
krista@1426
  1529
                        key && key->uids &&
Edouard@728
  1530
                        key->uids->email && key->uids->name)
Edouard@728
  1531
                    {
Edouard@728
  1532
                        pEp_identity *ident = new_identity(
Edouard@728
  1533
                             key->uids->email, import->fpr, NULL, key->uids->name);
Edouard@728
  1534
Edouard@728
  1535
                        gpg.gpgme_key_unref(key);
Edouard@728
  1536
Edouard@728
  1537
                        if (ident == NULL) {
Edouard@728
  1538
                            gpg.gpgme_data_release(dh);
Edouard@728
  1539
                            return PEP_OUT_OF_MEMORY;
Edouard@728
  1540
                        }
Edouard@728
  1541
Edouard@728
  1542
                        *private_idents = identity_list_add(*private_idents, ident);
Edouard@728
  1543
Edouard@728
  1544
                        if (*private_idents == NULL) {
Edouard@728
  1545
                            gpg.gpgme_data_release(dh);
Edouard@728
  1546
                            return PEP_OUT_OF_MEMORY;
Edouard@728
  1547
                        }
Edouard@728
  1548
                    }
krista@1426
  1549
                    else
Edouard@728
  1550
                    {
Edouard@728
  1551
                        gpg.gpgme_key_unref(key);
Edouard@728
  1552
                        gpg.gpgme_data_release(dh);
Edouard@728
  1553
                        return PEP_UNKNOWN_ERROR;
Edouard@728
  1554
                    }
Edouard@728
  1555
                }
Edouard@728
  1556
            }
Edouard@728
  1557
        }
vb@24
  1558
        break;
vb@24
  1559
    case GPG_ERR_INV_VALUE:
vb@24
  1560
        assert(0);
vb@74
  1561
        gpg.gpgme_data_release(dh);
vb@24
  1562
        return PEP_UNKNOWN_ERROR;
vb@24
  1563
    case GPG_ERR_NO_DATA:
vb@74
  1564
        gpg.gpgme_data_release(dh);
vb@24
  1565
        return PEP_ILLEGAL_VALUE;
vb@24
  1566
    default:
vb@24
  1567
        assert(0);
vb@74
  1568
        gpg.gpgme_data_release(dh);
vb@24
  1569
        return PEP_UNKNOWN_ERROR;
vb@24
  1570
    }
vb@24
  1571
vb@74
  1572
    gpg.gpgme_data_release(dh);
vb@24
  1573
    return PEP_STATUS_OK;
vb@24
  1574
}
vb@24
  1575
Edouard@170
  1576
PEP_STATUS pgp_export_keydata(
vb@1103
  1577
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size,
vb@1103
  1578
        bool secret
vb@24
  1579
    )
vb@24
  1580
{
vb@24
  1581
    gpgme_error_t gpgme_error;
vb@24
  1582
    gpgme_data_t dh;
vb@24
  1583
    size_t _size;
vb@24
  1584
    char *buffer;
vb@24
  1585
    int reading;
vb@24
  1586
vb@24
  1587
    assert(session);
vb@24
  1588
    assert(fpr);
vb@24
  1589
    assert(key_data);
vb@24
  1590
    assert(size);
vb@24
  1591
vb@74
  1592
    gpgme_error = gpg.gpgme_data_new(&dh);
vb@45
  1593
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1594
    assert(gpgme_error != GPG_ERR_ENOMEM);
vb@24
  1595
    switch (gpgme_error) {
vb@24
  1596
    case GPG_ERR_NO_ERROR:
vb@24
  1597
        break;
vb@24
  1598
    case GPG_ERR_ENOMEM:
vb@24
  1599
        return PEP_OUT_OF_MEMORY;
vb@24
  1600
    case GPG_ERR_INV_VALUE:
vb@24
  1601
        assert(0);
vb@24
  1602
        return PEP_UNKNOWN_ERROR;
vb@24
  1603
    default:
vb@24
  1604
        assert(0);
vb@24
  1605
        return PEP_UNKNOWN_ERROR;
vb@24
  1606
    }
vb@24
  1607
vb@1103
  1608
    if (secret)
vb@1103
  1609
        gpgme_error = gpg.gpgme_op_export(session->ctx, fpr,
edouard@1163
  1610
            GPGME_EXPORT_MODE_SECRET, dh);
vb@1103
  1611
    else
vb@1103
  1612
        gpgme_error = gpg.gpgme_op_export(session->ctx, fpr,
vb@1103
  1613
            GPGME_EXPORT_MODE_MINIMAL, dh);
vb@45
  1614
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1615
    switch (gpgme_error) {
vb@24
  1616
    case GPG_ERR_NO_ERROR:
vb@24
  1617
        break;
vb@24
  1618
    case GPG_ERR_EOF:
vb@74
  1619
        gpg.gpgme_data_release(dh);
vb@24
  1620
        return PEP_KEY_NOT_FOUND;
vb@24
  1621
    case GPG_ERR_INV_VALUE:
vb@24
  1622
        assert(0);
vb@74
  1623
        gpg.gpgme_data_release(dh);
vb@24
  1624
        return PEP_UNKNOWN_ERROR;
vb@24
  1625
    default:
vb@24
  1626
        assert(0);
vb@74
  1627
        gpg.gpgme_data_release(dh);
vb@24
  1628
        return PEP_UNKNOWN_ERROR;
vb@24
  1629
    };
vb@24
  1630
vb@74
  1631
    _size = gpg.gpgme_data_seek(dh, 0, SEEK_END);
vb@24
  1632
    assert(_size != -1);
vb@74
  1633
    gpg.gpgme_data_seek(dh, 0, SEEK_SET);
vb@24
  1634
vb@24
  1635
    buffer = malloc(_size + 1);
vb@24
  1636
    assert(buffer);
vb@24
  1637
    if (buffer == NULL) {
vb@74
  1638
        gpg.gpgme_data_release(dh);
vb@24
  1639
        return PEP_OUT_OF_MEMORY;
vb@24
  1640
    }
vb@24
  1641
vb@74
  1642
    reading = gpg.gpgme_data_read(dh, buffer, _size);
vb@24
  1643
    assert(_size == reading);
edouard@1803
  1644
    if(_size != reading)
edouard@1803
  1645
        return PEP_CANNOT_EXPORT_KEY;
vb@24
  1646
vb@24
  1647
    // safeguard for the naive user
vb@24
  1648
    buffer[_size] = 0;
vb@24
  1649
vb@24
  1650
    *key_data = buffer;
vb@24
  1651
    *size = _size;
vb@24
  1652
vb@74
  1653
    gpg.gpgme_data_release(dh);
vb@24
  1654
    return PEP_STATUS_OK;
vb@24
  1655
}
vb@24
  1656
krista@1426
  1657
PEP_STATUS pgp_list_keyinfo(PEP_SESSION session, const char* pattern,
krista@1031
  1658
                            stringpair_list_t** keyinfo_list)
krista@1426
  1659
{
krista@1005
  1660
    gpgme_error_t gpgme_error;
krista@1005
  1661
    assert(session);
krista@1031
  1662
    assert(keyinfo_list);
krista@1426
  1663
krista@1031
  1664
    if (!session || !keyinfo_list)
krista@1005
  1665
        return PEP_ILLEGAL_VALUE;
krista@1426
  1666
krista@1031
  1667
    *keyinfo_list = NULL;
krista@1426
  1668
krista@1031
  1669
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
krista@1005
  1670
    gpgme_error = _GPGERR(gpgme_error);
krista@1426
  1671
krista@1005
  1672
    switch(gpgme_error) {
krista@1005
  1673
        case GPG_ERR_NO_ERROR:
krista@1005
  1674
            break;
krista@1005
  1675
        case GPG_ERR_INV_VALUE:
krista@1005
  1676
            assert(0);
krista@1005
  1677
            return PEP_UNKNOWN_ERROR;
krista@1005
  1678
        default:
krista@1005
  1679
            gpg.gpgme_op_keylist_end(session->ctx);
krista@1426
  1680
            return PEP_GET_KEY_FAILED;
krista@1005
  1681
    };
krista@1426
  1682
krista@1005
  1683
    gpgme_key_t key;
krista@1031
  1684
    stringpair_list_t* _keyinfo_list = new_stringpair_list(NULL);
krista@1031
  1685
    stringpair_list_t* list_curr = _keyinfo_list;
krista@1031
  1686
    stringpair_t* pair = NULL;
krista@1426
  1687
krista@1426
  1688
    do {
krista@1005
  1689
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
krista@1005
  1690
        gpgme_error = _GPGERR(gpgme_error);
krista@1426
  1691
krista@1005
  1692
        switch(gpgme_error) {
krista@1005
  1693
            case GPG_ERR_EOF:
krista@1005
  1694
                break;
krista@1005
  1695
            case GPG_ERR_NO_ERROR:
krista@1005
  1696
                assert(key);
krista@1005
  1697
                assert(key->subkeys);
krista@1005
  1698
                if (!key || !key->subkeys)
krista@1005
  1699
                    return PEP_GET_KEY_FAILED;
krista@1040
  1700
krista@1005
  1701
                // first subkey is primary key
krista@1005
  1702
                char* fpr = key->subkeys->fpr;
roker@1199
  1703
                char* uid = key->uids->uid;
krista@1187
  1704
krista@1005
  1705
                assert(fpr);
roker@1199
  1706
                assert(uid);
krista@1031
  1707
                if (!fpr)
krista@1005
  1708
                    return PEP_GET_KEY_FAILED;
krista@1426
  1709
krista@1050
  1710
                if (key->subkeys->revoked)
krista@1040
  1711
                    continue;
krista@1426
  1712
krista@1031
  1713
                pair = new_stringpair(fpr, uid);
krista@1019
  1714
krista@1031
  1715
                assert(pair);
krista@1426
  1716
krista@1031
  1717
                if (pair) {
krista@1031
  1718
                    list_curr = stringpair_list_add(list_curr, pair);
krista@1031
  1719
                    pair = NULL;
krista@1426
  1720
krista@1005
  1721
                    assert(list_curr);
krista@1005
  1722
                    if (list_curr != NULL)
krista@1005
  1723
                        break;
krista@1005
  1724
                    else
krista@1031
  1725
                        free_stringpair(pair);
krista@1005
  1726
                }
krista@1031
  1727
                // else fallthrough (list_curr or pair wasn't allocateable)
krista@1005
  1728
            case GPG_ERR_ENOMEM:
krista@1031
  1729
                free_stringpair_list(_keyinfo_list);
krista@1005
  1730
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1005
  1731
                return PEP_OUT_OF_MEMORY;
krista@1005
  1732
            default:
krista@1005
  1733
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1005
  1734
                return PEP_UNKNOWN_ERROR;
krista@1005
  1735
        }
krista@1005
  1736
    } while (gpgme_error != GPG_ERR_EOF);
krista@1426
  1737
krista@1032
  1738
    if (_keyinfo_list->value == NULL) {
krista@1031
  1739
        free_stringpair_list(_keyinfo_list);
krista@1031
  1740
        _keyinfo_list = NULL;
krista@1005
  1741
    }
krista@1426
  1742
krista@1031
  1743
    *keyinfo_list = _keyinfo_list;
krista@1426
  1744
krista@1005
  1745
    return PEP_STATUS_OK;
krista@1005
  1746
}
krista@1005
  1747
vb@46
  1748
static void _switch_mode(pEpSession *session, gpgme_keylist_mode_t remove_mode,
vb@24
  1749
    gpgme_keylist_mode_t add_mode)
vb@24
  1750
{
vb@24
  1751
    gpgme_error_t gpgme_error;
vb@24
  1752
    gpgme_keylist_mode_t mode;
vb@24
  1753
vb@74
  1754
    mode = gpg.gpgme_get_keylist_mode(session->ctx);
vb@24
  1755
vb@24
  1756
    mode &= ~remove_mode;
vb@24
  1757
    mode |= add_mode;
vb@24
  1758
vb@74
  1759
    gpgme_error = gpg.gpgme_set_keylist_mode(session->ctx, mode);
vb@45
  1760
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1761
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@24
  1762
}
vb@24
  1763
vb@24
  1764
PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1765
{
vb@24
  1766
    gpgme_error_t gpgme_error;
vb@24
  1767
    gpgme_key_t key;
vb@24
  1768
vb@24
  1769
    assert(session);
vb@24
  1770
    assert(pattern);
vb@24
  1771
vb@46
  1772
    _switch_mode(session, GPGME_KEYLIST_MODE_LOCAL, GPGME_KEYLIST_MODE_EXTERN);
vb@24
  1773
vb@74
  1774
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, 0);
vb@45
  1775
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1776
    switch (gpgme_error) {
vb@24
  1777
    case GPG_ERR_NO_ERROR:
vb@24
  1778
        break;
vb@24
  1779
    case GPG_ERR_INV_VALUE:
vb@24
  1780
        assert(0);
vb@47
  1781
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1782
        return PEP_UNKNOWN_ERROR;
vb@24
  1783
    default:
vb@47
  1784
        _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1785
        return PEP_GET_KEY_FAILED;
vb@24
  1786
    };
vb@24
  1787
vb@47
  1788
    gpgme_ctx_t import_ctx;
vb@74
  1789
    gpgme_error = gpg.gpgme_new(&import_ctx);
vb@47
  1790
    assert(gpgme_error == GPG_ERR_NO_ERROR);
vb@47
  1791
vb@24
  1792
    do {
vb@74
  1793
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1794
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1795
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1796
        switch (gpgme_error) {
vb@24
  1797
        case GPG_ERR_EOF:
vb@24
  1798
            break;
vb@24
  1799
        case GPG_ERR_NO_ERROR:
vb@24
  1800
        {
vb@24
  1801
            gpgme_error_t gpgme_error;
vb@24
  1802
            gpgme_key_t keys[2];
vb@24
  1803
vb@24
  1804
            keys[0] = key;
vb@24
  1805
            keys[1] = NULL;
vb@24
  1806
vb@74
  1807
            gpgme_error = gpg.gpgme_op_import_keys(import_ctx, keys);
vb@45
  1808
            gpgme_error = _GPGERR(gpgme_error);
vb@74
  1809
            gpg.gpgme_key_unref(key);
vb@24
  1810
            assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1811
            assert(gpgme_error != GPG_ERR_CONFLICT);
vb@24
  1812
        }
vb@24
  1813
            break;
vb@24
  1814
        case GPG_ERR_ENOMEM:
vb@74
  1815
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1816
            gpg.gpgme_release(import_ctx);
vb@47
  1817
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1818
            return PEP_OUT_OF_MEMORY;
vb@24
  1819
        default:
vb@74
  1820
            gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1821
            gpg.gpgme_release(import_ctx);
vb@47
  1822
            _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@46
  1823
            return PEP_UNKNOWN_ERROR;
vb@24
  1824
        };
vb@24
  1825
    } while (gpgme_error != GPG_ERR_EOF);
vb@24
  1826
vb@74
  1827
    gpg.gpgme_op_keylist_end(session->ctx);
vb@74
  1828
    gpg.gpgme_release(import_ctx);
vb@47
  1829
    _switch_mode(session, GPGME_KEYLIST_MODE_EXTERN, GPGME_KEYLIST_MODE_LOCAL);
vb@24
  1830
    return PEP_STATUS_OK;
vb@24
  1831
}
vb@24
  1832
krista@1357
  1833
static PEP_STATUS _pgp_search_keys(PEP_SESSION session, const char* pattern,
krista@1357
  1834
                            stringlist_t** keylist,
krista@1357
  1835
                            int private_only) {
vb@24
  1836
    gpgme_error_t gpgme_error;
vb@24
  1837
    gpgme_key_t key;
krista@1426
  1838
vb@24
  1839
    assert(session);
vb@24
  1840
    assert(keylist);
krista@1426
  1841
vb@24
  1842
    *keylist = NULL;
krista@1426
  1843
krista@1357
  1844
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, pattern, private_only);
vb@45
  1845
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  1846
    switch (gpgme_error) {
krista@1357
  1847
        case GPG_ERR_NO_ERROR:
krista@1357
  1848
            break;
krista@1357
  1849
        case GPG_ERR_INV_VALUE:
krista@1357
  1850
            assert(0);
krista@1357
  1851
            return PEP_UNKNOWN_ERROR;
krista@1357
  1852
        default:
krista@1357
  1853
            gpg.gpgme_op_keylist_end(session->ctx);
krista@1357
  1854
            return PEP_GET_KEY_FAILED;
vb@24
  1855
    };
krista@1426
  1856
roker@877
  1857
    stringlist_t *_keylist = new_stringlist(NULL);
vb@24
  1858
    stringlist_t *_k = _keylist;
krista@1426
  1859
vb@24
  1860
    do {
vb@74
  1861
        gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  1862
        gpgme_error = _GPGERR(gpgme_error);
vb@24
  1863
        assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  1864
        switch (gpgme_error) {
krista@1357
  1865
            case GPG_ERR_EOF:
vb@24
  1866
                break;
krista@1357
  1867
            case GPG_ERR_NO_ERROR:
krista@1357
  1868
                assert(key);
krista@1357
  1869
                assert(key->subkeys);
edouard@1525
  1870
                if(!key->subkeys)
krista@1357
  1871
                    break;
edouard@1525
  1872
                assert(key->uids);
edouard@1525
  1873
                gpgme_user_id_t kuid = key->uids;
krista@2057
  1874
                // check that at least one uid's email matches pattern exactly,
krista@2057
  1875
                // modulo the email-diff heuristic
edouard@1525
  1876
                while(kuid) {
krista@2681
  1877
                    if((pattern == NULL) ||
krista@2681
  1878
                       (strstr(pattern, "@") == NULL) || // not an email
krista@2681
  1879
                       (kuid->email && _email_heuristic_match(kuid->email, pattern)))
edouard@1760
  1880
                    { 
edouard@1525
  1881
                        char *fpr = key->subkeys->fpr;
edouard@1525
  1882
                        assert(fpr);
edouard@1525
  1883
                        _k = stringlist_add(_k, fpr);
edouard@1525
  1884
                        assert(_k);
edouard@1525
  1885
                        if (_k == NULL){
edouard@1525
  1886
                            free_stringlist(_keylist);
edouard@1525
  1887
                            gpg.gpgme_op_keylist_end(session->ctx);
edouard@1525
  1888
                            return PEP_OUT_OF_MEMORY;
edouard@1525
  1889
                        }
edouard@1525
  1890
                        break;
edouard@1525
  1891
                    }
edouard@1525
  1892
                    kuid = kuid->next;
edouard@1525
  1893
                }
edouard@1525
  1894
                break;
krista@1357
  1895
            case GPG_ERR_ENOMEM:
krista@1357
  1896
                free_stringlist(_keylist);
krista@1357
  1897
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1357
  1898
                return PEP_OUT_OF_MEMORY;
krista@1357
  1899
            default:
krista@1357
  1900
                gpg.gpgme_op_keylist_end(session->ctx);
krista@1357
  1901
                return PEP_UNKNOWN_ERROR;
vb@24
  1902
        };
vb@24
  1903
    } while (gpgme_error != GPG_ERR_EOF);
krista@1426
  1904
vb@74
  1905
    gpg.gpgme_op_keylist_end(session->ctx);
krista@2098
  1906
    if (_keylist->value == NULL) {
vb@523
  1907
        free_stringlist(_keylist);
vb@523
  1908
        _keylist = NULL;
krista@2057
  1909
        
krista@2098
  1910
        if (pattern != NULL) {
krista@2098
  1911
            // If match failed, check to see if we've got a dotted address in the pattern.
krista@2098
  1912
            // (last chance of the heuristic, really)
krista@2098
  1913
            // If so, try again without any dots.
krista@2098
  1914
            const char* dotpos = strstr(pattern, ".");
krista@2098
  1915
            const char* atpos = strstr(pattern, "@");
krista@2098
  1916
            if (dotpos && atpos && (dotpos < atpos)) {
krista@2098
  1917
                char* undotted = _undot_address(pattern);
krista@2098
  1918
                if (undotted) {
krista@2098
  1919
                    PEP_STATUS status = _pgp_search_keys(session, undotted,
krista@2098
  1920
                                                         keylist, private_only);
krista@2098
  1921
                    free(undotted);
krista@2098
  1922
                    return status;
krista@2098
  1923
                }
krista@2057
  1924
            }
krista@2057
  1925
        }
krista@2057
  1926
    }    
krista@2057
  1927
    
vb@24
  1928
    *keylist = _keylist;
vb@24
  1929
    return PEP_STATUS_OK;
vb@24
  1930
}
vb@24
  1931
krista@1357
  1932
PEP_STATUS pgp_find_keys(
krista@1357
  1933
    PEP_SESSION session, const char *pattern, stringlist_t **keylist
krista@1357
  1934
    )
krista@1357
  1935
{
krista@1357
  1936
    return _pgp_search_keys(session, pattern, keylist, 0);
krista@1426
  1937
}
krista@1357
  1938
krista@1357
  1939
PEP_STATUS pgp_find_private_keys(
krista@1357
  1940
    PEP_SESSION session, const char *pattern, stringlist_t **keylist
krista@1357
  1941
)
krista@1357
  1942
{
krista@1357
  1943
    return _pgp_search_keys(session, pattern, keylist, 1);
krista@1357
  1944
}
krista@1357
  1945
vb@2539
  1946
// this function is delivering a list of triples with fpr, email, name of all
vb@2539
  1947
// ultimatedly trusted private keys
vb@2539
  1948
vb@2539
  1949
PEP_STATUS pgp_find_trusted_private_keys(
vb@2539
  1950
        PEP_SESSION session, stringlist_t **keylist
vb@2539
  1951
    )
vb@2539
  1952
{
vb@2539
  1953
    assert(session && keylist);
vb@2539
  1954
    if (!session || !keylist)
krista@2458
  1955
        return PEP_ILLEGAL_VALUE;
krista@2458
  1956
vb@2539
  1957
    *keylist = NULL;
vb@2539
  1958
krista@2416
  1959
    gpgme_key_t key;
krista@2458
  1960
    gpgme_error_t gpgme_error;
vb@2539
  1961
vb@2539
  1962
    stringlist_t *private_keylist = NULL;
vb@2539
  1963
    PEP_STATUS status = pgp_find_private_keys(session, NULL, &private_keylist);
vb@2539
  1964
    if (status)
vb@2539
  1965
        return status;
vb@2539
  1966
    if (!private_keylist || !private_keylist->value)
vb@2539
  1967
        return status;
vb@2539
  1968
vb@2539
  1969
    stringlist_t *result_list = new_stringlist(NULL);
vb@2539
  1970
    if (!result_list)
vb@2539
  1971
        return PEP_OUT_OF_MEMORY;
vb@2539
  1972
    stringlist_t *_result_list = result_list;
vb@2539
  1973
vb@2539
  1974
    stringlist_t *keylist_curr;
vb@2539
  1975
    for (keylist_curr = private_keylist; keylist_curr && keylist_curr->value; keylist_curr = keylist_curr->next) {
vb@2539
  1976
        // a. get key data
vb@2539
  1977
        gpgme_error = gpg.gpgme_get_key(session->ctx, keylist_curr->value, &key, 1);
vb@2539
  1978
        gpgme_error = _GPGERR(gpgme_error);
vb@2539
  1979
        assert(gpgme_error != GPG_ERR_ENOMEM);
vb@2539
  1980
        switch (gpgme_error) {
vb@2539
  1981
            case GPG_ERR_NO_ERROR:
vb@2539
  1982
                break;
vb@2539
  1983
            case GPG_ERR_EOF:
vb@2539
  1984
                status = PEP_KEY_NOT_FOUND;
vb@2539
  1985
                break;
vb@2539
  1986
            case GPG_ERR_INV_VALUE:
vb@2539
  1987
                status = PEP_ILLEGAL_VALUE;
vb@2539
  1988
                break;
vb@2539
  1989
            case GPG_ERR_AMBIGUOUS_NAME:
vb@2539
  1990
                status = PEP_KEY_HAS_AMBIG_NAME;
vb@2539
  1991
                break;
vb@2539
  1992
            case GPG_ERR_ENOMEM:
vb@2539
  1993
                free_stringlist(result_list);
vb@2539
  1994
                free_stringlist(private_keylist);
vb@2539
  1995
                return PEP_OUT_OF_MEMORY;
vb@2539
  1996
            default:
vb@2539
  1997
                assert(0);
vb@2539
  1998
                status = PEP_UNKNOWN_ERROR;
vb@2539
  1999
        }
vb@2539
  2000
        if (key && gpgme_error == GPG_ERR_NO_ERROR) {
vb@2539
  2001
            if (key->revoked || key->disabled) {
vb@2539
  2002
                status = PEP_KEY_UNSUITABLE;
krista@2416
  2003
            }
vb@2539
  2004
            else {
vb@2539
  2005
                if (key->fpr && key->secret && key->can_encrypt && key->can_sign) {
vb@2539
  2006
                    if (key->owner_trust == GPGME_VALIDITY_ULTIMATE &&
vb@2539
  2007
                            key->uids && key->uids->email && key->uids->name) { 
vb@2539
  2008
                        _result_list = stringlist_add(_result_list, key->fpr);
vb@2539
  2009
                        if (!_result_list) {
vb@2539
  2010
                            free_stringlist(result_list);
vb@2539
  2011
                            free_stringlist(private_keylist);
vb@2539
  2012
                            return PEP_OUT_OF_MEMORY;
vb@2539
  2013
                        }
vb@2539
  2014
                        _result_list = stringlist_add(_result_list, key->uids->email);
vb@2539
  2015
                        if (!_result_list) {
vb@2539
  2016
                            free_stringlist(result_list);
vb@2539
  2017
                            free_stringlist(private_keylist);
vb@2539
  2018
                            return PEP_OUT_OF_MEMORY;
vb@2539
  2019
                        }
vb@2539
  2020
                        _result_list = stringlist_add(_result_list, key->uids->name);
vb@2539
  2021
                        if (!_result_list) {
vb@2539
  2022
                            free_stringlist(result_list);
vb@2539
  2023
                            free_stringlist(private_keylist);
vb@2539
  2024
                            return PEP_OUT_OF_MEMORY;
krista@2457
  2025
                        }
krista@2457
  2026
                    }
krista@2457
  2027
                }
krista@2416
  2028
            }
krista@2416
  2029
        }
krista@2416
  2030
    }
vb@2539
  2031
vb@2539
  2032
    free_stringlist(private_keylist);
vb@2539
  2033
    *keylist = result_list;
vb@2539
  2034
    return PEP_STATUS_OK;
krista@2416
  2035
}
krista@2416
  2036
vb@24
  2037
PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)
vb@24
  2038
{
vb@24
  2039
    gpgme_error_t gpgme_error;
vb@24
  2040
vb@24
  2041
    assert(session);
vb@24
  2042
    assert(pattern);
vb@24
  2043
vb@74
  2044
    gpgme_error = gpg.gpgme_op_export(session->ctx, pattern,
vb@24
  2045
        GPGME_EXPORT_MODE_EXTERN, NULL);
vb@45
  2046
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  2047
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  2048
    if (gpgme_error == GPG_ERR_NO_ERROR)
vb@24
  2049
        return PEP_STATUS_OK;
vb@24
  2050
    else
vb@24
  2051
        return PEP_CANNOT_SEND_KEY;
vb@24
  2052
}
vb@24
  2053
vb@24
  2054
PEP_STATUS pgp_get_key_rating(
vb@24
  2055
    PEP_SESSION session,
vb@24
  2056
    const char *fpr,
vb@24
  2057
    PEP_comm_type *comm_type
vb@24
  2058
    )
vb@24
  2059
{
vb@24
  2060
    PEP_STATUS status = PEP_STATUS_OK;
vb@24
  2061
    gpgme_error_t gpgme_error;
vb@24
  2062
    gpgme_key_t key;
vb@24
  2063
vb@24
  2064
    assert(session);
vb@24
  2065
    assert(fpr);
vb@24
  2066
    assert(comm_type);
vb@24
  2067
vb@24
  2068
    *comm_type = PEP_ct_unknown;
vb@24
  2069
vb@74
  2070
    gpgme_error = gpg.gpgme_op_keylist_start(session->ctx, fpr, 0);
vb@45
  2071
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  2072
    switch (gpgme_error) {
vb@24
  2073
    case GPG_ERR_NO_ERROR:
vb@24
  2074
        break;
vb@24
  2075
    case GPG_ERR_INV_VALUE:
vb@24
  2076
        assert(0);
vb@24
  2077
        return PEP_UNKNOWN_ERROR;
vb@24
  2078
    default:
vb@24
  2079
        return PEP_GET_KEY_FAILED;
vb@24
  2080
    };
vb@24
  2081
vb@74
  2082
    gpgme_error = gpg.gpgme_op_keylist_next(session->ctx, &key);
vb@45
  2083
    gpgme_error = _GPGERR(gpgme_error);
vb@24
  2084
    assert(gpgme_error != GPG_ERR_INV_VALUE);
vb@24
  2085
vb@24
  2086
    if (key == NULL) {
vb@74
  2087
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  2088
        return PEP_KEY_NOT_FOUND;
vb@24
  2089
    }
vb@24
  2090
vb@24
  2091
    switch (key->protocol) {
vb@24
  2092
    case GPGME_PROTOCOL_OpenPGP:
vb@24
  2093
    case GPGME_PROTOCOL_DEFAULT:
vb@24
  2094
        *comm_type = PEP_ct_OpenPGP_unconfirmed;
vb@24
  2095
        break;
vb@24
  2096
    case GPGME_PROTOCOL_CMS:
vb@24
  2097
        *comm_type = PEP_ct_CMS_unconfirmed;
vb@24
  2098
        break;
vb@24
  2099
    default:
vb@24
  2100
        *comm_type = PEP_ct_unknown;
vb@74
  2101
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  2102
        return PEP_STATUS_OK;
vb@24
  2103
    }
vb@24
  2104
vb@24
  2105
    switch (gpgme_error) {
vb@24
  2106
    case GPG_ERR_EOF:
vb@24
  2107
        break;
vb@24
  2108
    case GPG_ERR_NO_ERROR:
vb@24
  2109
        assert(key);
vb@24
  2110
        assert(key->subkeys);
vb@24
  2111
        for (gpgme_subkey_t sk = key->subkeys; sk != NULL; sk = sk->next) {
vb@24
  2112
            if (sk->length < 1024)
vb@24
  2113
                *comm_type = PEP_ct_key_too_short;
vb@24
  2114
            else if (
vb@24
  2115
                (
vb@24
  2116
                (sk->pubkey_algo == GPGME_PK_RSA)
vb@24
  2117
                || (sk->pubkey_algo == GPGME_PK_RSA_E)
vb@24
  2118
                || (sk->pubkey_algo == GPGME_PK_RSA_S)
vb@24
  2119
                )
vb@24
  2120
                && sk->length == 1024
vb@24
  2121
                )
vb@122
  2122
                *comm_type = PEP_ct_OpenPGP_weak_unconfirmed;
vb@24
  2123
vb@24
  2124
            if (sk->invalid) {
vb@24
  2125
                *comm_type = PEP_ct_key_b0rken;
vb@24
  2126
                break;
vb@24
  2127
            }
vb@24
  2128
            if (sk->expired) {
vb@24
  2129
                *comm_type = PEP_ct_key_expired;
vb@24
  2130
                break;
vb@24
  2131
            }
vb@24
  2132
            if (sk->revoked) {
vb@24
  2133
                *comm_type = PEP_ct_key_revoked;
vb@24
  2134
                break;
vb@24
  2135
            }
vb@24
  2136
        }
vb@24
  2137
        break;
vb@24
  2138
    case GPG_ERR_ENOMEM:
vb@74
  2139
        gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  2140
        *comm_type = PEP_ct_unknown;
vb@24
  2141
        return PEP_OUT_OF_MEMORY;
vb@24
  2142
    default:
vb@74
  2143
        gpg.gpgme_op_keylist_end(session->ctx);
vb@46
  2144
        return PEP_UNKNOWN_ERROR;
vb@24
  2145
    };
vb@24
  2146
vb@74
  2147
    gpg.gpgme_op_keylist_end(session->ctx);
vb@24
  2148
vb@24
  2149
    return status;
vb@24
  2150
}
vb@200
  2151
vb@200
  2152
krista@1924
  2153
static ssize_t _nullwriter(
krista@1924
  2154
        void *_handle,
krista@1924
  2155
        const void *buffer,
krista@1924
  2156
        size_t size
krista@1924
  2157
    )
krista@1924
  2158
{
krista@1924
  2159
    return size;
krista@1924
  2160
}
krista@1924
  2161
krista@1924
  2162
typedef struct _replace_only_uid_state {
krista@1924
  2163
    enum {
krista@1924
  2164
        replace_uid_command = 0,
krista@1924
  2165
        replace_uid_realname,
krista@1924
  2166
        replace_uid_email,
kgrothoff@1926
  2167
        replace_uid_comment,
krista@1924
  2168
        replace_uid_adduid_ok,
krista@1924
  2169
        replace_uid_select_for_delete,
krista@1924
  2170
        replace_uid_delete,
krista@1924
  2171
        replace_uid_delete_confirm,
krista@1924
  2172
        replace_uid_select_for_trust,
krista@1924
  2173
        replace_uid_trust,
krista@1924
  2174
        replace_uid_trust_ultimate,
krista@1924
  2175
        replace_uid_trust_ultimate_confirm,
krista@1924
  2176
        replace_uid_quit,
krista@1924
  2177
        replace_uid_save_okay,
krista@1924
  2178
        replace_uid_exit,
krista@1924
  2179
        replace_uid_error = -1
krista@1924
  2180
    } state;
krista@1924
  2181
const char *realname;
krista@1924
  2182
const char *email;
krista@1924
  2183
} replace_only_uid_state;
krista@1924
  2184
kgrothoff@1926
  2185
krista@1924
  2186
static gpgme_error_t replace_only_uid_fsm(
krista@1924
  2187
    void *_handle,
krista@1924
  2188
    gpgme_status_code_t statuscode,
krista@1924
  2189
    const char *args,
krista@1924
  2190
    int fd
krista@1924
  2191
)
krista@1924
  2192
{
krista@1924
  2193
    replace_only_uid_state *handle = _handle;
krista@1924
  2194
        
krista@1924
  2195
    switch (handle->state) {
krista@1924
  2196
        case replace_uid_command:
krista@1924
  2197
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2198
                assert(strcmp(args, "keyedit.prompt") == 0);
krista@1924
  2199
                if (strcmp(args, "keyedit.prompt")) {
krista@1924
  2200
                    handle->state = replace_uid_error;
krista@1924
  2201
                    return GPG_ERR_GENERAL;
krista@1924
  2202
                }
krista@1924
  2203
                gpg.gpgme_io_write(fd, "adduid\n", 7);
krista@1924
  2204
                handle->state = replace_uid_realname;
krista@1924
  2205
            }
krista@1924
  2206
            break;
krista@1924
  2207
            
krista@1924
  2208
        case replace_uid_realname:
krista@1924
  2209
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2210
                assert(strcmp(args, "keygen.name") == 0);
krista@1924
  2211
                assert(handle->realname);
krista@1924
  2212
                if (strcmp(args, "keygen.name") || !handle->realname) {
krista@1924
  2213
                    handle->state = replace_uid_error;
krista@1924
  2214
                    return GPG_ERR_GENERAL;
krista@1924
  2215
                }
krista@1924
  2216
                size_t realname_strlen = strlen(handle->realname);
krista@1924
  2217
                char* realname = (char*)calloc(1, realname_strlen + 2); // \n + \0
krista@1924
  2218
                if (!realname) {
krista@1924
  2219
                    handle->state = replace_uid_error;
krista@1924
  2220
                    return GPG_ERR_ENOMEM;
krista@1924
  2221
                }
kgrothoff@1926
  2222
                strlcpy(realname, handle->realname, realname_strlen + 1);
roker@2055
  2223
                realname[realname_strlen] = '\n';
krista@1924
  2224
                gpg.gpgme_io_write(fd, realname, realname_strlen + 1);
krista@1924
  2225
                handle->state = replace_uid_email;
krista@1924
  2226
                free(realname);
krista@1924
  2227
            }
krista@1924
  2228
            break;
krista@1924
  2229
            
krista@1924
  2230
        case replace_uid_email:
krista@1924
  2231
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2232
                assert(strcmp(args, "keygen.email") == 0);
krista@1924
  2233
                assert(handle->email);
krista@1924
  2234
                if (strcmp(args, "keygen.email") || !handle->email) {
krista@1924
  2235
                    handle->state = replace_uid_error;
krista@1924
  2236
                    return GPG_ERR_GENERAL;
krista@1924
  2237
                }
krista@1924
  2238
                size_t email_strlen = strlen(handle->email);
krista@1924
  2239
                char* email = (char*)calloc(1, email_strlen + 2); // \n + \0
krista@1924
  2240
                if (!email) {
krista@1924
  2241
                    handle->state = replace_uid_error;
krista@1924
  2242
                    return GPG_ERR_ENOMEM;
krista@1924
  2243
                }
kgrothoff@1926
  2244
                strlcpy(email, handle->email, email_strlen + 1);
roker@2055
  2245
                email[email_strlen] = '\n';
krista@1924
  2246
                gpg.gpgme_io_write(fd, email, email_strlen + 1);
kgrothoff@1926
  2247
                handle->state = replace_uid_comment;
krista@1924
  2248
                free(email);
krista@1924
  2249
            }
krista@1924
  2250
            break;
krista@1924
  2251
kgrothoff@1926
  2252
        case replace_uid_comment:
kgrothoff@1926
  2253
            if (statuscode == GPGME_STATUS_GET_LINE) {
kgrothoff@1926
  2254
                assert(strcmp(args, "keygen.comment") == 0);
kgrothoff@1926
  2255
                if (strcmp(args, "keygen.comment") || !handle->email) {
kgrothoff@1926
  2256
                    handle->state = replace_uid_error;
kgrothoff@1926
  2257
                    return GPG_ERR_GENERAL;
kgrothoff@1926
  2258
                }
kgrothoff@1926
  2259
                gpg.gpgme_io_write(fd, "\n", 1);
kgrothoff@1926
  2260
                //handle->state = replace_uid_adduid_ok;
kgrothoff@1926
  2261
                handle->state = replace_uid_select_for_delete;
kgrothoff@1926
  2262
            }
kgrothoff@1926
  2263
            break;
kgrothoff@1926
  2264
/*
krista@1924
  2265
        case replace_uid_adduid_ok:
krista@1924
  2266
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2267
                assert(strcmp(args, "keygen.userid.cmd") == 0);
krista@1924
  2268
                if (strcmp(args, "keygen.userid.cmd")) {
krista@1924
  2269
                    handle->state = replace_uid_error;
krista@1924
  2270
                    return GPG_ERR_GENERAL;
krista@1924
  2271
                }
krista@1924
  2272
                gpg.gpgme_io_write(fd, "O\n", 2);
krista@1924
  2273
                handle->state = replace_uid_select_for_delete;
krista@1924
  2274
            }
krista@1924
  2275
            break;
kgrothoff@1926
  2276
	    */
krista@1924
  2277
krista@1924
  2278
        case replace_uid_select_for_delete:
krista@1924
  2279
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2280
                assert(strcmp(args, "keyedit.prompt") == 0);
krista@1924
  2281
                if (strcmp(args, "keyedit.prompt")) {
krista@1924
  2282
                    handle->state = replace_uid_error;
krista@1924
  2283
                    return GPG_ERR_GENERAL;
krista@1924
  2284
                }
krista@1924
  2285
                gpg.gpgme_io_write(fd, "uid 1\n", 6);
krista@1924
  2286
                handle->state = replace_uid_delete;
krista@1924
  2287
            }
krista@1924
  2288
            break;
krista@1924
  2289
krista@1924
  2290
        case replace_uid_delete:
krista@1924
  2291
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2292
                assert(strcmp(args, "keyedit.prompt") == 0);
krista@1924
  2293
                if (strcmp(args, "keyedit.prompt")) {
krista@1924
  2294
                    handle->state = replace_uid_error;
krista@1924
  2295
                    return GPG_ERR_GENERAL;
krista@1924
  2296
                }
krista@1924
  2297
                gpg.gpgme_io_write(fd, "deluid\n", 7);
krista@1924
  2298
                handle->state = replace_uid_delete_confirm;
krista@1924
  2299
            }
krista@1924
  2300
            break;
krista@1924
  2301
krista@1924
  2302
        case replace_uid_delete_confirm:
kgrothoff@1926
  2303
            if (statuscode == GPGME_STATUS_GET_BOOL) {
krista@1924
  2304
                assert(strcmp(args, "keyedit.remove.uid.okay") == 0);
krista@1924
  2305
                if (strcmp(args, "keyedit.remove.uid.okay")) {
krista@1924
  2306
                    handle->state = replace_uid_error;
krista@1924
  2307
                    return GPG_ERR_GENERAL;
krista@1924
  2308
                }
krista@1924
  2309
                gpg.gpgme_io_write(fd, "Y\n", 2);
krista@1924
  2310
                handle->state = replace_uid_select_for_trust;
krista@1924
  2311
            }
krista@1924
  2312
            break;
krista@1924
  2313
krista@1924
  2314
        case replace_uid_select_for_trust:
krista@1924
  2315
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2316
                assert(strcmp(args, "keyedit.prompt") == 0);
krista@1924
  2317
                if (strcmp(args, "keyedit.prompt")) {
krista@1924
  2318
                    handle->state = replace_uid_error;
krista@1924
  2319
                    return GPG_ERR_GENERAL;
krista@1924
  2320
                }
kgrothoff@1926
  2321
                gpg.gpgme_io_write(fd, "uid 1\n", 6);
krista@1924
  2322
                handle->state = replace_uid_trust;
krista@1924
  2323
            }
krista@1924
  2324
            break;
krista@1924
  2325
krista@1924
  2326
        case replace_uid_trust:
krista@1924
  2327
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2328
                assert(strcmp(args, "keyedit.prompt") == 0);
krista@1924
  2329
                if (strcmp(args, "keyedit.prompt")) {
krista@1924
  2330
                    handle->state = replace_uid_error;
krista@1924
  2331
                    return GPG_ERR_GENERAL;
krista@1924
  2332
                }
kgrothoff@1926
  2333
                gpg.gpgme_io_write(fd, "trust\n", 6);
krista@1924
  2334
                handle->state = replace_uid_trust_ultimate;
krista@1924
  2335
            }
krista@1924
  2336
            break;
krista@1924
  2337
krista@1924
  2338
        case replace_uid_trust_ultimate:
krista@1924
  2339
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2340
                assert(strcmp(args, "edit_ownertrust.value") == 0);
krista@1924
  2341
                if (strcmp(args, "edit_ownertrust.value")) {
krista@1924
  2342
                    handle->state = replace_uid_error;
krista@1924
  2343
                    return GPG_ERR_GENERAL;
krista@1924
  2344
                }
krista@1924
  2345
                gpg.gpgme_io_write(fd, "5\n", 2);
krista@1924
  2346
                handle->state = replace_uid_trust_ultimate_confirm;
krista@1924
  2347
            }
krista@1924
  2348
            break;
krista@1924
  2349
krista@1924
  2350
        case replace_uid_trust_ultimate_confirm:
kgrothoff@1926
  2351
            if (statuscode == GPGME_STATUS_GET_BOOL) {
krista@1924
  2352
                assert(strcmp(args, "edit_ownertrust.set_ultimate.okay") == 0);
krista@1924
  2353
                if (strcmp(args, "edit_ownertrust.set_ultimate.okay")) {
krista@1924
  2354
                    handle->state = replace_uid_error;
krista@1924
  2355
                    return GPG_ERR_GENERAL;
krista@1924
  2356
                }
krista@1924
  2357
                gpg.gpgme_io_write(fd, "Y\n", 2);
krista@1924
  2358
                handle->state = replace_uid_quit;
krista@1924
  2359
            }
krista@1924
  2360
            break;
krista@1924
  2361
krista@1924
  2362
        case replace_uid_quit:
krista@1924
  2363
            if (statuscode == GPGME_STATUS_GET_LINE) {
krista@1924
  2364
                assert(strcmp(args, "keyedit.prompt") == 0);
krista@1924
  2365
                if (strcmp(args, "keyedit.prompt")) {
krista@1924
  2366
                    handle->state = replace_uid_error;
krista@1924
  2367
                    return GPG_ERR_GENERAL;
krista@1924
  2368
                }
krista@1924
  2369
                gpg.gpgme_io_write(fd, "quit\n", 5);
krista@1924
  2370
                handle->state = replace_uid_save_okay;
krista@1924
  2371
            }
krista@1924
  2372
            break;
krista@1924
  2373
krista@1924
  2374
        case replace_uid_save_okay:
kgrothoff@1926
  2375
            if (statuscode == GPGME_STATUS_GET_BOOL) {
krista@1924
  2376
                assert(strcmp(args, "keyedit.save.okay") == 0);
krista@1924
  2377
                if (strcmp(args, "keyedit.save.okay")) {
krista@1924
  2378
                    handle->state = replace_uid_error;
krista@1924
  2379
                    return GPG_ERR_GENERAL;
krista@1924
  2380
                }
krista@1924
  2381
                gpg.gpgme_io_write(fd, "Y\n", 2);
krista@1924
  2382
                handle->state = replace_uid_exit;
krista@1924
  2383
            }
krista@1924
  2384
            break;
krista@1924
  2385
krista@1924
  2386
        case replace_uid_exit:
krista@1924
  2387
            break;
krista@1924
  2388
krista@1924
  2389
        case replace_uid_error:
krista@1924
  2390
            return GPG_ERR_GENERAL;
krista@1924
  2391
            
krista@1924
  2392
        default:
roker@2055
  2393
            break;
krista@1924
  2394
    }
krista@1924
  2395
    return GPG_ERR_NO_ERROR;
krista@1924
  2396
}
krista@1924
  2397
krista@1924
  2398
PEP_STATUS pgp_replace_only_uid(
krista@1924
  2399
        PEP_SESSION session,
krista@1924
  2400
        const char* fpr,
krista@1924
  2401
        const char* realname,
krista@1924
  2402
        const char* email
krista@1924
  2403
    )
krista@1924
  2404
{
krista@1924
  2405
    PEP_STATUS status = PEP_STATUS_OK;
krista@1924
  2406
    gpgme_error_t gpgme_error;
krista@1924
  2407
    gpgme_key_t key;
krista@1924
  2408
    gpgme_data_t output;
krista@1924
  2409
    replace_only_uid_state handle;
krista@1924
  2410
krista@1924
  2411
    assert(session);
krista@1924
  2412
    assert(fpr);
krista@1924
  2413
    assert(realname);
krista@1924
  2414
    assert(email);
krista@1924
  2415
    
krista@1924
  2416
    memset(&handle, 0, sizeof(replace_only_uid_state));
krista@1924
  2417
    handle.realname = realname;
krista@1924
  2418
    handle.email = email;
krista@1924
  2419
krista@1924
  2420
    status = find_single_key(session, fpr, &key);
krista@1924
  2421
    if (status != PEP_STATUS_OK)
krista@1924
  2422
        return status;
krista@1924
  2423
krista@1924
  2424
    struct gpgme_data_cbs data_cbs;
krista@1924
  2425
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
krista@1924
  2426
    data_cbs.write = _nullwriter;
krista@1924
  2427
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
krista@1924
  2428
krista@2553
  2429
    gpgme_error = _GPGERR(gpg.gpgme_op_edit(session->ctx, key, replace_only_uid_fsm, &handle,
krista@2553
  2430
            output));
krista@1924
  2431
    assert(gpgme_error == GPG_ERR_NO_ERROR);
krista@1924
  2432
    if(gpgme_error != GPG_ERR_NO_ERROR) {
krista@1924
  2433
        status = PEP_CANNOT_EDIT_KEY;
krista@1924
  2434
    }
krista@1924
  2435
krista@1924
  2436
    gpg.gpgme_data_release(output);
krista@1924
  2437
    gpg.gpgme_key_unref(key);
krista@1924
  2438
krista@1924
  2439
    return status;
krista@1924
  2440
}
krista@1924
  2441
krista@1924
  2442
vb@201
  2443
typedef struct _renew_state {
vb@211
  2444
    enum {
vb@200
  2445
        renew_command = 0,
vb@200
  2446
        renew_date,
vb@200
  2447
        renew_secret_key,
vb@200
  2448
        renew_command2,
vb@200
  2449
        renew_date2,
vb@200
  2450
        renew_quit,
vb@200
  2451
        renew_save,
vb@200
  2452
        renew_exit,
vb@200
  2453
        renew_error = -1
vb@200
  2454
    } state;
vb@201
  2455
    const char *date_ref;
vb@201
  2456
} renew_state;
vb@200
  2457
vb@201
  2458
static gpgme_error_t renew_fsm(
vb@200
  2459
        void *_handle,
vb@200
  2460
        gpgme_status_code_t statuscode,
vb@200
  2461
        const char *args,
vb@200
  2462
        int fd
vb@200
  2463
    )
vb@200
  2464
{
vb@201
  2465
    renew_state *handle = _handle;
vb@200
  2466
vb@200
  2467
    switch (handle->state) {
vb@200
  2468
        case renew_command:
vb@200
  2469
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  2470
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  2471
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  2472
                    handle->state = renew_error;
vb@201
  2473
                    return GPG_ERR_GENERAL;
vb@201
  2474
                }
vb@223
  2475
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  2476
                handle->state = renew_date;
vb@200
  2477
            }
vb@200
  2478
            break;
vb@200
  2479
vb@200
  2480
        case renew_date:
vb@200
  2481
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  2482
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  2483
                if (strcmp(args, "keygen.valid")) {
vb@201
  2484
                    handle->state = renew_error;
vb@201
  2485
                    return GPG_ERR_GENERAL;
vb@201
  2486
                }
vb@223
  2487
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  2488
                handle->state = renew_secret_key;
vb@200
  2489
            }
vb@200
  2490
            break;
vb@200
  2491
vb@200
  2492
        case renew_secret_key:
vb@200
  2493
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  2494
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  2495
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  2496
                    handle->state = renew_error;
vb@201
  2497
                    return GPG_ERR_GENERAL;
vb@201
  2498
                }
vb@223
  2499
                gpg.gpgme_io_write(fd, "key 1\n", 6);
vb@200
  2500
                handle->state = renew_command2;
vb@200
  2501
            }
vb@200
  2502
            break;
vb@200
  2503
vb@200
  2504
        case renew_command2:
vb@200
  2505
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  2506
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  2507
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  2508
                    handle->state = renew_error;
vb@201
  2509
                    return GPG_ERR_GENERAL;
vb@201
  2510
                }
vb@223
  2511
                gpg.gpgme_io_write(fd, "expire\n", 7);
vb@200
  2512
                handle->state = renew_date2;
vb@200
  2513
            }
vb@200
  2514
            break;
vb@200
  2515
vb@200
  2516
        case renew_date2:
vb@200
  2517
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  2518
                assert(strcmp(args, "keygen.valid") == 0);
vb@201
  2519
                if (strcmp(args, "keygen.valid")) {
vb@201
  2520
                    handle->state = renew_error;
vb@201
  2521
                    return GPG_ERR_GENERAL;
vb@201
  2522
                }
vb@223
  2523
                gpg.gpgme_io_write(fd, handle->date_ref, 11);
vb@200
  2524
                handle->state = renew_quit;
vb@200
  2525
            }
vb@200
  2526
            break;
vb@200
  2527
vb@200
  2528
        case renew_quit:
vb@200
  2529
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@201
  2530
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@201
  2531
                if (strcmp(args, "keyedit.prompt")) {
vb@201
  2532
                    handle->state = renew_error;
vb@201
  2533
                    return GPG_ERR_GENERAL;
vb@201
  2534
                }
vb@223
  2535
                gpg.gpgme_io_write(fd, "quit\n", 5);
vb@200
  2536
                handle->state = renew_save;
vb@200
  2537
            }
vb@200
  2538
            break;
vb@200
  2539
vb@200
  2540
        case renew_save:
vb@200
  2541
            if (statuscode == GPGME_STATUS_GET_BOOL) {
vb@201
  2542
                assert(strcmp(args, "keyedit.save.okay") == 0);
vb@201
  2543
                if (strcmp(args, "keyedit.save.okay")) {
vb@201
  2544
                    handle->state = renew_error;
vb@201
  2545
                    return GPG_ERR_GENERAL;
vb@201
  2546
                }
vb@223
  2547
                gpg.gpgme_io_write(fd, "Y\n", 2);
vb@200
  2548
                handle->state = renew_exit;
vb@200
  2549
            }
vb@200
  2550
            break;
vb@200
  2551
vb@200
  2552
        case renew_exit:
vb@200
  2553
            break;
vb@200
  2554
vb@200
  2555
        case renew_error:
vb@200
  2556
            return GPG_ERR_GENERAL;
vb@200
  2557
    }
vb@200
  2558
vb@200
  2559
    return GPG_ERR_NO_ERROR;
vb@200
  2560
}
vb@200
  2561
vb@200
  2562
vb@201
  2563
PEP_STATUS pgp_renew_key(
vb@201
  2564
        PEP_SESSION session,
vb@201
  2565
        const char *fpr,
vb@201
  2566
        const timestamp *ts
vb@201
  2567
    )
vb@200
  2568
{
vb@200
  2569
    PEP_STATUS status = PEP_STATUS_OK;
vb@200
  2570
    gpgme_error_t gpgme_error;
vb@200
  2571
    gpgme_key_t key;
vb@200
  2572
    gpgme_data_t output;
vb@201
  2573
    renew_state handle;
vb@203
  2574
    char date_text[12];
vb@200
  2575
vb@200
  2576
    assert(session);
vb@200
  2577
    assert(fpr);
vb@200
  2578
vb@201
  2579
    memset(&handle, 0, sizeof(renew_state));
vb@203
  2580
    snprintf(date_text, 12, "%.4d-%.2d-%.2d\n", ts->tm_year + 1900,
vb@201
  2581
            ts->tm_mon + 1, ts->tm_mday);
vb@201
  2582
    handle.date_ref = date_text;
vb@200
  2583
vb@200
  2584
    status = find_single_key(session, fpr, &key);
vb@200
  2585
    if (status != PEP_STATUS_OK)
vb@200
  2586
        return status;
vb@200
  2587
vb@200
  2588
    struct gpgme_data_cbs data_cbs;
vb@200
  2589
    memset(&data_cbs, 0, sizeof(struct gpgme_data_cbs));
vb@200
  2590
    data_cbs.write = _nullwriter;
vb@220
  2591
    gpg.gpgme_data_new_from_cbs(&output, &data_cbs, &handle);
vb@200
  2592
krista@2553
  2593
    gpgme_error = _GPGERR(gpg.gpgme_op_edit(session->ctx, key, renew_fsm, &handle,
krista@2553
  2594
            output));
vb@200
  2595
    assert(gpgme_error == GPG_ERR_NO_ERROR);
edouard@1802
  2596
    if(gpgme_error != GPG_ERR_NO_ERROR) {
edouard@1802
  2597
        status = PEP_CANNOT_EDIT_KEY;
edouard@1802
  2598
    }
vb@200
  2599
vb@200
  2600
    gpg.gpgme_data_release(output);
vb@200
  2601
    gpg.gpgme_key_unref(key);
vb@200
  2602
edouard@1802
  2603
    return status;
vb@200
  2604
}
vb@200
  2605
vb@211
  2606
typedef struct _revoke_state {
vb@211
  2607
    enum {
vb@211
  2608
        revoke_command = 0,
vb@211
  2609
        revoke_approve,
vb@211
  2610
        revoke_reason_code,
vb@211
  2611
        revoke_reason_text,
vb@211
  2612
        revoke_reason_ok,
vb@211
  2613
        revoke_quit,
vb@211
  2614
        revoke_save,
vb@211
  2615
        revoke_exit,
vb@211
  2616
        revoke_error = -1
vb@211
  2617
    } state;
vb@211
  2618
    const char *reason_ref;
vb@211
  2619
} revoke_state;
vb@211
  2620
roker@723
  2621
roker@723
  2622
/*** unused?
vb@211
  2623
static bool isemptystring(const char *str)
vb@211
  2624
{
vb@211
  2625
    if (str == NULL)
vb@211
  2626
        return true;
vb@211
  2627
vb@211
  2628
    for (; str; str++) {
vb@211
  2629
        if (*str != ' ' && *str != '\t' && *str != '\n')
vb@211
  2630
            return false;
vb@211
  2631
    }
vb@211
  2632
vb@211
  2633
    return true;
vb@211
  2634
}
roker@723
  2635
***/
roker@723
  2636
vb@211
  2637
vb@211
  2638
static gpgme_error_t revoke_fsm(
vb@211
  2639
        void *_handle,
vb@211
  2640
        gpgme_status_code_t statuscode,
vb@211
  2641
        const char *args,
vb@211
  2642
        int fd
vb@211
  2643
    )
vb@211
  2644
{
vb@211
  2645
    revoke_state *handle = _handle;
vb@211
  2646
vb@211
  2647
    switch (handle->state) {
vb@211
  2648
        case revoke_command:
vb@211
  2649
            if (statuscode == GPGME_STATUS_GET_LINE) {
vb@211
  2650
                assert(strcmp(args, "keyedit.prompt") == 0);
vb@211
  2651
                if (strcmp(args, "keyedit.prompt")) {
vb@211
  2652
                    handle->state = revoke_error;
vb@211
  2653
                    return GPG_ERR_GENERAL;
vb@211
  2654
                }
vb@223
  2655
                gpg.gpgme_io_write(fd, "revkey\n", 7);
vb@211
  2656
                handle->state = revoke_approve;