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