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