src/pgp_netpgp.c
author Edouard Tisserant <edouard@pep-project.org>
Fri, 24 Mar 2017 02:51:45 +0100
branchNETPGP-18
changeset 1694 032e59ccdd93
parent 1693 7cbc1b22a250
child 1695 4f4aae28570f
permissions -rw-r--r--
NETPGP-18 adding recipients fpr/key_id to pgp_decrypt_and_verify returned keylist
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
Edouard@174
     4
#include "pEp_internal.h"
Edouard@174
     5
#include "pgp_netpgp.h"
Edouard@174
     6
Edouard@174
     7
#include <limits.h>
Edouard@174
     8
Edouard@174
     9
#include "wrappers.h"
Edouard@174
    10
vb@334
    11
#include "netpgp.h"
dirk@533
    12
#include <netpgp/config.h>
dirk@533
    13
#include <netpgp/memory.h>
dirk@533
    14
#include <netpgp/crypto.h>
dirk@533
    15
#include <netpgp/netpgpsdk.h>
dirk@533
    16
#include <netpgp/validate.h>
dirk@533
    17
#include <netpgp/readerwriter.h>
Edouard@179
    18
Edouard@252
    19
#include <curl/curl.h>
Edouard@252
    20
#include <pthread.h>
Edouard@188
    21
#include <regex.h>
Edouard@188
    22
Edouard@252
    23
static netpgp_t netpgp;
Edouard@254
    24
static pthread_mutex_t netpgp_mutex;
Edouard@252
    25
Edouard@263
    26
static PEP_STATUS init_netpgp()
Edouard@174
    27
{
Edouard@174
    28
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@175
    29
    const char *home = NULL;
Edouard@179
    30
Edouard@252
    31
    if(pthread_mutex_init(&netpgp_mutex, NULL)){
Edouard@252
    32
        return PEP_OUT_OF_MEMORY;
Edouard@252
    33
    }
Edouard@252
    34
Edouard@263
    35
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@252
    36
        return PEP_UNKNOWN_ERROR;
Edouard@252
    37
    }
Edouard@174
    38
Edouard@263
    39
    if (strcmp(setlocale(LC_ALL, NULL), "C") == 0)
Edouard@263
    40
        setlocale(LC_ALL, "");
Edouard@174
    41
Edouard@263
    42
    memset(&netpgp, 0x0, sizeof(netpgp_t));
Edouard@180
    43
Edouard@263
    44
    // netpgp_setvar(&netpgp, "max mem alloc", "4194304");
Edouard@263
    45
    netpgp_setvar(&netpgp, "need seckey", "1");
Edouard@315
    46
    // netpgp_setvar(&netpgp, "need userid", "1");
Edouard@252
    47
Edouard@263
    48
    // NetPGP shares home with GPG
Edouard@263
    49
    home = gpg_home();
Edouard@263
    50
    if(home){
Edouard@263
    51
        netpgp_set_homedir(&netpgp,(char*)home, NULL, 0);
Edouard@263
    52
    }else{
Edouard@263
    53
        status = PEP_INIT_NO_GPG_HOME;
Edouard@263
    54
        goto unlock_netpgp;
Edouard@174
    55
    }
Edouard@174
    56
Edouard@263
    57
    // pair with gpg's cert-digest-algo
Edouard@263
    58
    netpgp_setvar(&netpgp, "hash", "SHA256");
Edouard@263
    59
Edouard@263
    60
    // subset of gpg's personal-cipher-preferences
Edouard@263
    61
    // here only one cipher can be selected
Edouard@263
    62
    netpgp_setvar(&netpgp, "cipher", "CAST5");
Edouard@263
    63
Edouard@263
    64
    if (!netpgp_init(&netpgp)) {
Edouard@263
    65
        status = PEP_INIT_NETPGP_INIT_FAILED;
Edouard@263
    66
        goto unlock_netpgp;
Edouard@263
    67
    }
Edouard@175
    68
Edouard@315
    69
    // netpgp_set_debug("packet-parse.c");
Edouard@315
    70
Edouard@252
    71
unlock_netpgp:
Edouard@252
    72
    pthread_mutex_unlock(&netpgp_mutex);
krista@1435
    73
Edouard@263
    74
    return status;
Edouard@263
    75
}
Edouard@175
    76
Edouard@263
    77
static void release_netpgp()
Edouard@263
    78
{
Edouard@263
    79
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@263
    80
        return;
Edouard@263
    81
    }
Edouard@263
    82
    netpgp_end(&netpgp);
Edouard@263
    83
    memset(&netpgp, 0x0, sizeof(netpgp_t));
Edouard@263
    84
Edouard@263
    85
    pthread_mutex_destroy(&netpgp_mutex);
Edouard@263
    86
Edouard@263
    87
    return;
Edouard@263
    88
}
Edouard@263
    89
Edouard@263
    90
static PEP_STATUS init_curl(
Edouard@263
    91
    pthread_mutex_t *curl_mutex,
Edouard@263
    92
    bool in_first)
Edouard@263
    93
{
Edouard@263
    94
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@263
    95
Edouard@263
    96
    if(pthread_mutex_init(curl_mutex, NULL)){
Edouard@263
    97
        return PEP_OUT_OF_MEMORY;
Edouard@263
    98
    }
Edouard@263
    99
Edouard@263
   100
    if(pthread_mutex_lock(curl_mutex)){
Edouard@263
   101
        return PEP_UNKNOWN_ERROR;
Edouard@263
   102
    }
Edouard@263
   103
Edouard@263
   104
    if(in_first){
Edouard@263
   105
        curl_global_init(CURL_GLOBAL_DEFAULT);
Edouard@263
   106
    }
Edouard@263
   107
Edouard@322
   108
    pthread_mutex_unlock(curl_mutex);
Edouard@322
   109
    return status;
Edouard@322
   110
}
Edouard@322
   111
Edouard@322
   112
static void release_curl(
krista@1435
   113
    pthread_mutex_t *curl_mutex,
Edouard@322
   114
    bool out_last)
Edouard@322
   115
{
Edouard@322
   116
    if(pthread_mutex_lock(curl_mutex)){
Edouard@322
   117
        return;
Edouard@322
   118
    }
Edouard@322
   119
Edouard@322
   120
    if(out_last){
Edouard@322
   121
        curl_global_cleanup();
Edouard@322
   122
    }
Edouard@322
   123
Edouard@322
   124
    pthread_mutex_destroy(curl_mutex);
Edouard@322
   125
Edouard@322
   126
    return;
Edouard@322
   127
}
Edouard@322
   128
Edouard@322
   129
static PEP_STATUS curl_get_ctx(
Edouard@322
   130
    CURL **curl)
Edouard@322
   131
{
Edouard@322
   132
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@322
   133
    struct curl_slist *headers=NULL;
Edouard@322
   134
Edouard@263
   135
    if ((*curl = curl_easy_init()) == NULL) {
Edouard@263
   136
        return PEP_OUT_OF_MEMORY;
Edouard@263
   137
    }
Edouard@263
   138
Edouard@263
   139
    curl_easy_setopt(*curl, CURLOPT_FOLLOWLOCATION, 1L);
Edouard@263
   140
    curl_easy_setopt(*curl, CURLOPT_MAXREDIRS, 3L);
Edouard@263
   141
Edouard@263
   142
    headers=curl_slist_append(headers,"Pragma: no-cache");
Edouard@263
   143
    if(headers)
Edouard@263
   144
        headers=curl_slist_append(headers,"Cache-Control: no-cache");
Edouard@263
   145
Edouard@263
   146
    if(!headers)
Edouard@263
   147
    {
Edouard@322
   148
        return PEP_OUT_OF_MEMORY;
Edouard@174
   149
    }
Edouard@174
   150
Edouard@263
   151
    curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers);
Edouard@263
   152
    curl_slist_free_all(headers);
Edouard@263
   153
Edouard@263
   154
    // TODO curl_easy_setopt(curl,CURLOPT_PROXY,proxy);
Edouard@174
   155
    return status;
Edouard@174
   156
}
Edouard@174
   157
Edouard@322
   158
static void curl_release_ctx(
Edouard@322
   159
    CURL **curl)
Edouard@263
   160
{
Edouard@263
   161
    if(*curl)
Edouard@263
   162
        curl_easy_cleanup(*curl);
Edouard@263
   163
Edouard@263
   164
    *curl = NULL;
Edouard@263
   165
Edouard@263
   166
    return;
Edouard@263
   167
}
Edouard@263
   168
Edouard@263
   169
PEP_STATUS pgp_init(PEP_SESSION session, bool in_first)
Edouard@263
   170
{
Edouard@263
   171
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@263
   172
Edouard@263
   173
    assert(session);
Edouard@661
   174
    if(!session) return PEP_ILLEGAL_VALUE;
Edouard@263
   175
Edouard@263
   176
    if (in_first) {
Edouard@263
   177
        if((status = init_netpgp()) != PEP_STATUS_OK)
Edouard@263
   178
        return status;
Edouard@263
   179
    }
Edouard@263
   180
Edouard@263
   181
    if((status = init_curl(
Edouard@263
   182
                    &session->ctx.curl_mutex,
Edouard@263
   183
                    in_first) != PEP_STATUS_OK)){
Edouard@263
   184
        if(in_first) release_netpgp();
Edouard@263
   185
        return status;
Edouard@263
   186
    }
Edouard@263
   187
Edouard@263
   188
    return PEP_STATUS_OK;
Edouard@263
   189
}
Edouard@263
   190
Edouard@174
   191
void pgp_release(PEP_SESSION session, bool out_last)
Edouard@174
   192
{
Edouard@179
   193
    assert(session);
Edouard@179
   194
    if(!session) return;
Edouard@179
   195
Edouard@263
   196
    if (out_last){
Edouard@263
   197
        release_netpgp();
Edouard@252
   198
    }
Edouard@322
   199
    release_curl(&session->ctx.curl_mutex, out_last);
Edouard@174
   200
}
Edouard@174
   201
Edouard@446
   202
// return 1 if the file contains ascii-armoured text
Edouard@188
   203
static unsigned
Edouard@188
   204
_armoured(const char *buf, size_t size, const char *pattern)
Edouard@188
   205
{
Edouard@188
   206
    unsigned armoured = 0;
Edouard@446
   207
    regex_t r;
Edouard@446
   208
    regcomp(&r, pattern, REG_EXTENDED|REG_NOSUB);
Edouard@446
   209
    if (regnexec(&r, buf, size, 0, NULL, 0) == 0) {
Edouard@446
   210
        armoured = 1;
Edouard@188
   211
    }
Edouard@446
   212
    regfree(&r);
Edouard@188
   213
    return armoured;
Edouard@188
   214
}
Edouard@188
   215
Edouard@393
   216
/* write key fingerprint hexdump as a string */
Edouard@393
   217
static unsigned
Edouard@393
   218
fpr_to_str (char **str, const uint8_t *fpr, size_t length)
Edouard@393
   219
{
Edouard@393
   220
    unsigned i;
Edouard@393
   221
    int	n;
krista@1435
   222
Edouard@491
   223
    /* 4 hexes per short + null */
Edouard@490
   224
    *str = malloc((length / 2) * 4 + 1);
krista@1435
   225
Edouard@393
   226
    if(*str == NULL)
Edouard@393
   227
        return 0;
krista@1435
   228
Edouard@489
   229
    for (n = 0, i = 0 ; i < length; i += 2) {
Edouard@489
   230
        n += snprintf(&((*str)[n]), 5, "%02x%02x", fpr[i], fpr[i+1]);
Edouard@393
   231
    }
krista@1435
   232
Edouard@393
   233
    return 1;
Edouard@393
   234
}
Edouard@393
   235
Edouard@393
   236
/* write key fingerprint bytes read from hex string
Edouard@393
   237
 * accept spaces and hexes */
Edouard@393
   238
static unsigned
Edouard@393
   239
str_to_fpr (const char *str, uint8_t *fpr, size_t *length)
Edouard@393
   240
{
Edouard@393
   241
    unsigned i,j;
krista@1435
   242
Edouard@393
   243
    *length = 0;
krista@1435
   244
Edouard@393
   245
    while(*str && *length < PGP_FINGERPRINT_SIZE){
Edouard@393
   246
        while (*str == ' ') str++;
Edouard@393
   247
        for (j = 0; j < 2; j++) {
Edouard@393
   248
            uint8_t *byte = &fpr[*length];
Edouard@393
   249
            *byte = 0;
Edouard@393
   250
            for (i = 0; i < 2; i++) {
Edouard@393
   251
                if (i > 0)
Edouard@393
   252
                    *byte = *byte << 4;
Edouard@393
   253
                if (*str >= 'a' && *str <= 'f')
Edouard@393
   254
                    *byte += 10 + *str - 'a';
Edouard@393
   255
                else if (*str >= 'A' && *str <= 'F')
Edouard@393
   256
                    *byte += 10 + *str - 'A';
Edouard@393
   257
                else if (*str >= '0' && *str <= '9')
Edouard@393
   258
                    *byte += *str - '0';
Edouard@393
   259
                else
Edouard@393
   260
                    return 0;
Edouard@393
   261
                str++;
Edouard@393
   262
            }
Edouard@393
   263
            (*length)++;
Edouard@393
   264
        }
Edouard@393
   265
    }
Edouard@393
   266
    return 1;
Edouard@393
   267
}
Edouard@393
   268
krista@1435
   269
// Iterate through netpgp' reported valid signatures
Edouard@185
   270
// fill a list of valid figerprints
Edouard@185
   271
// returns PEP_STATUS_OK if all sig reported valid
Edouard@185
   272
// error status otherwise.
Edouard@279
   273
static PEP_STATUS _validation_results(
Edouard@279
   274
        netpgp_t *netpgp,
Edouard@279
   275
        pgp_validation_t *vresult,
edouard@1694
   276
        stringlist_t **keylist
Edouard@279
   277
    )
Edouard@185
   278
{
Edouard@188
   279
    time_t    now;
Edouard@188
   280
    time_t    t;
Edouard@185
   281
edouard@1694
   282
    *keylist = NULL;
edouard@1694
   283
Edouard@188
   284
    now = time(NULL);
Edouard@188
   285
    if (now < vresult->birthtime) {
Edouard@188
   286
        // signature is not valid yet
Edouard@431
   287
        return PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
Edouard@188
   288
    }
Edouard@188
   289
    if (vresult->duration != 0 && now > vresult->birthtime + vresult->duration) {
Edouard@188
   290
        // signature has expired
Edouard@188
   291
        t = vresult->duration + vresult->birthtime;
Edouard@431
   292
        return PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
Edouard@188
   293
    }
Edouard@185
   294
    if (vresult->validc && vresult->valid_sigs &&
Edouard@185
   295
        !vresult->invalidc && !vresult->unknownc ) {
roker@1559
   296
        
edouard@1694
   297
        stringlist_t *_keylist;
edouard@1694
   298
Edouard@185
   299
        // caller responsible to free
edouard@1694
   300
        _keylist = new_stringlist(NULL);
edouard@1694
   301
        assert(_keylist);
edouard@1694
   302
        if (_keylist == NULL) {
Edouard@185
   303
            return PEP_OUT_OF_MEMORY;
Edouard@185
   304
        }
roker@1559
   305
        
edouard@1694
   306
        stringlist_t *k = _keylist;
edouard@1694
   307
        unsigned c = 0;
roker@1559
   308
        for (unsigned n = 0; n < vresult->validc; ++n) {
Edouard@393
   309
            unsigned from = 0;
Edouard@393
   310
            const pgp_key_t	 *signer;
Edouard@393
   311
            char *fprstr = NULL;
Edouard@315
   312
            const uint8_t *keyid = vresult->valid_sigs[n].signer_id;
krista@1435
   313
Edouard@393
   314
            signer = pgp_getkeybyid(netpgp->io, netpgp->pubring,
Edouard@393
   315
                                    keyid, &from, NULL, NULL,
krista@1435
   316
                                    0, 0); /* check neither revocation nor expiry
Edouard@393
   317
                                              as is should be checked already */
Edouard@393
   318
            if(signer)
Edouard@393
   319
                fpr_to_str(&fprstr,
Edouard@393
   320
                           signer->pubkeyfpr.fingerprint,
Edouard@393
   321
                           signer->pubkeyfpr.length);
Edouard@393
   322
            else
edouard@1694
   323
                continue;
krista@1435
   324
edouard@1694
   325
            if (fprstr == NULL){
edouard@1694
   326
                free_stringlist(_keylist);
Edouard@393
   327
                return PEP_OUT_OF_MEMORY;
edouard@1694
   328
            }
krista@1435
   329
Edouard@393
   330
            k = stringlist_add(k, fprstr);
krista@1435
   331
Edouard@393
   332
            free(fprstr);
krista@1435
   333
Edouard@185
   334
            if(!k){
edouard@1694
   335
                free_stringlist(_keylist);
Edouard@185
   336
                return PEP_OUT_OF_MEMORY;
Edouard@185
   337
            }
edouard@1694
   338
edouard@1694
   339
            c++;
Edouard@185
   340
        }
edouard@1694
   341
        if(c > 0) {
edouard@1694
   342
            *keylist = _keylist;
edouard@1694
   343
            return PEP_STATUS_OK;
edouard@1694
   344
        }
edouard@1694
   345
edouard@1694
   346
        free_stringlist(_keylist);
edouard@1694
   347
        return PEP_VERIFY_NO_KEY;
Edouard@185
   348
    }
Edouard@185
   349
    if (vresult->validc + vresult->invalidc + vresult->unknownc == 0) {
Edouard@185
   350
        // No signatures found - is this memory signed?
krista@1435
   351
        return PEP_VERIFY_NO_KEY;
krista@1435
   352
    }
krista@1435
   353
Edouard@185
   354
    if (vresult->invalidc) {
Edouard@185
   355
        // some invalid signatures
Edouard@185
   356
        return PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH;
Edouard@185
   357
    }
krista@1435
   358
Edouard@185
   359
    // only unknown sigs
Edouard@431
   360
    return PEP_DECRYPTED;
Edouard@185
   361
}
Edouard@185
   362
Edouard@389
   363
#define _ENDL    "\\s*(\r\n|\r|\n)"
Edouard@389
   364
#define ARMOR_HEAD    "^-----BEGIN PGP MESSAGE-----"_ENDL
Edouard@174
   365
PEP_STATUS pgp_decrypt_and_verify(
Edouard@174
   366
    PEP_SESSION session, const char *ctext, size_t csize,
krista@1435
   367
    const char *dsigtext, size_t dsigsize,
Edouard@174
   368
    char **ptext, size_t *psize, stringlist_t **keylist
Edouard@174
   369
    )
Edouard@174
   370
{
Edouard@180
   371
    char *_ptext = NULL;
Edouard@179
   372
Edouard@174
   373
    PEP_STATUS result;
Edouard@174
   374
    stringlist_t *_keylist = NULL;
Edouard@174
   375
Edouard@174
   376
    assert(session);
Edouard@174
   377
    assert(ctext);
Edouard@174
   378
    assert(csize);
Edouard@174
   379
    assert(ptext);
Edouard@174
   380
    assert(psize);
Edouard@174
   381
    assert(keylist);
Edouard@174
   382
krista@1435
   383
    if(!session || !ctext || !csize || !ptext || !psize || !keylist)
Edouard@661
   384
        return PEP_ILLEGAL_VALUE;
Edouard@179
   385
Edouard@263
   386
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@252
   387
        return PEP_UNKNOWN_ERROR;
Edouard@252
   388
    }
Edouard@179
   389
Edouard@174
   390
    *ptext = NULL;
Edouard@174
   391
    *psize = 0;
Edouard@174
   392
    *keylist = NULL;
Edouard@174
   393
roker@1559
   394
    pgp_validation_t *vresult = malloc(sizeof(pgp_validation_t));
Edouard@185
   395
    memset(vresult, 0x0, sizeof(pgp_validation_t));
Edouard@182
   396
edouard@1694
   397
    key_id_t *recipients_key_ids = NULL;
edouard@1694
   398
    unsigned recipients_count = 0;
edouard@1694
   399
roker@1559
   400
    pgp_memory_t *mem = pgp_decrypt_and_validate_buf(netpgp.io, vresult, ctext, csize,
Edouard@252
   401
                netpgp.secring, netpgp.pubring,
Edouard@188
   402
                _armoured(ctext, csize, ARMOR_HEAD),
edouard@1694
   403
                 &recipients_key_ids, &recipients_count);
edouard@1694
   404
Edouard@179
   405
    if (mem == NULL) {
Edouard@252
   406
        result = PEP_OUT_OF_MEMORY;
Edouard@252
   407
        goto unlock_netpgp;
Edouard@179
   408
    }
Edouard@179
   409
roker@1559
   410
    const size_t _psize = pgp_mem_len(mem);
Edouard@182
   411
    if (_psize){
Edouard@272
   412
        if ((_ptext = malloc(_psize + 1)) == NULL) {
Edouard@183
   413
            result = PEP_OUT_OF_MEMORY;
Edouard@183
   414
            goto free_pgp;
Edouard@182
   415
        }
Edouard@185
   416
        memcpy(_ptext, pgp_mem_data(mem), _psize);
Edouard@272
   417
        _ptext[_psize] = '\0'; // safeguard for naive users
Edouard@182
   418
        result = PEP_DECRYPTED;
Edouard@182
   419
    }else{
Edouard@183
   420
        result = PEP_DECRYPT_NO_KEY;
Edouard@183
   421
        goto free_pgp;
Edouard@182
   422
    }
Edouard@180
   423
Edouard@185
   424
    if (result == PEP_DECRYPTED) {
Edouard@252
   425
        result = _validation_results(&netpgp, vresult, &_keylist);
edouard@1694
   426
        if (result == PEP_DECRYPTED ||
edouard@1694
   427
            result == PEP_VERIFY_NO_KEY) {
edouard@1694
   428
            if(stringlist_add(_keylist, "") == NULL) {
edouard@1694
   429
                result = PEP_OUT_OF_MEMORY;
edouard@1694
   430
                goto free_keylist;
edouard@1694
   431
            }
Edouard@449
   432
            result = PEP_DECRYPTED;
Edouard@431
   433
        }else if (result != PEP_STATUS_OK) {
Edouard@185
   434
            goto free_ptext;
Edouard@431
   435
        }else{
Edouard@431
   436
            result = PEP_DECRYPTED_AND_VERIFIED;
Edouard@183
   437
        }
Edouard@180
   438
    }
Edouard@174
   439
edouard@1694
   440
    stringlist_t *k = _keylist;
edouard@1694
   441
    for (unsigned n = 0; n < recipients_count; ++n) {
edouard@1694
   442
        unsigned from = 0;
edouard@1694
   443
        const pgp_key_t	 *rcpt;
edouard@1694
   444
        char *fprstr = NULL;
edouard@1694
   445
        key_id_t *keyid = &recipients_key_ids[n];
edouard@1694
   446
edouard@1694
   447
        rcpt = pgp_getkeybyid(netpgp.io, netpgp.pubring,
edouard@1694
   448
                                *keyid, &from, NULL, NULL,
edouard@1694
   449
                                0, 0); /* check neither revocation nor expiry*/
edouard@1694
   450
        if(rcpt)
edouard@1694
   451
            fpr_to_str(&fprstr,
edouard@1694
   452
                       rcpt->pubkeyfpr.fingerprint,
edouard@1694
   453
                       rcpt->pubkeyfpr.length);
edouard@1694
   454
        else
edouard@1694
   455
            // if no key found put ID instead of fpr
edouard@1694
   456
            fpr_to_str(&fprstr,
edouard@1694
   457
                       *keyid,
edouard@1694
   458
                       sizeof(key_id_t));
edouard@1694
   459
edouard@1694
   460
        if (fprstr == NULL){
edouard@1694
   461
            free_stringlist(_keylist);
edouard@1694
   462
            return PEP_OUT_OF_MEMORY;
edouard@1694
   463
        }
edouard@1694
   464
edouard@1694
   465
        k = stringlist_add_unique(k, fprstr);
edouard@1694
   466
edouard@1694
   467
        free(fprstr);
edouard@1694
   468
edouard@1694
   469
        if(!k){
edouard@1694
   470
            free_stringlist(_keylist);
edouard@1694
   471
            return PEP_OUT_OF_MEMORY;
edouard@1694
   472
        }
edouard@1694
   473
    }
edouard@1694
   474
Edouard@180
   475
    if (result == PEP_DECRYPTED_AND_VERIFIED
Edouard@180
   476
        || result == PEP_DECRYPTED) {
Edouard@180
   477
        *ptext = _ptext;
Edouard@180
   478
        *psize = _psize;
Edouard@180
   479
        (*ptext)[*psize] = 0; // safeguard for naive users
Edouard@185
   480
        if (result == PEP_DECRYPTED_AND_VERIFIED) {
Edouard@185
   481
            *keylist = _keylist;
Edouard@185
   482
        }
Edouard@183
   483
Edouard@183
   484
        /* _ptext and _keylist ownership transfer, don't free */
Edouard@183
   485
        goto free_pgp;
Edouard@180
   486
    }
Edouard@183
   487
edouard@1694
   488
free_keylist:
Edouard@183
   489
    free_stringlist(_keylist);
Edouard@183
   490
Edouard@183
   491
free_ptext:
Edouard@183
   492
    free(_ptext);
Edouard@183
   493
Edouard@183
   494
free_pgp:
Edouard@185
   495
    pgp_memory_free(mem);
Edouard@183
   496
    pgp_validate_result_free(vresult);
Edouard@183
   497
Edouard@252
   498
unlock_netpgp:
Edouard@252
   499
    pthread_mutex_unlock(&netpgp_mutex);
Edouard@252
   500
Edouard@174
   501
    return result;
Edouard@174
   502
}
Edouard@174
   503
Edouard@389
   504
#define ARMOR_SIG_HEAD    "^-----BEGIN PGP (SIGNATURE|SIGNED MESSAGE)-----"_ENDL
Edouard@174
   505
PEP_STATUS pgp_verify_text(
Edouard@174
   506
    PEP_SESSION session, const char *text, size_t size,
Edouard@174
   507
    const char *signature, size_t sig_size, stringlist_t **keylist
Edouard@174
   508
    )
Edouard@174
   509
{
Edouard@185
   510
    pgp_memory_t *signedmem;
Edouard@185
   511
    pgp_memory_t *sig;
Edouard@185
   512
    pgp_validation_t *vresult;
Edouard@185
   513
Edouard@174
   514
    PEP_STATUS result;
Edouard@174
   515
    stringlist_t *_keylist;
Edouard@174
   516
Edouard@174
   517
    assert(session);
Edouard@174
   518
    assert(text);
Edouard@174
   519
    assert(size);
Edouard@174
   520
    assert(signature);
Edouard@174
   521
    assert(sig_size);
Edouard@174
   522
    assert(keylist);
Edouard@174
   523
krista@1435
   524
    if(!session || !text || !size || !signature || !sig_size || !keylist)
Edouard@661
   525
        return PEP_ILLEGAL_VALUE;
Edouard@185
   526
Edouard@263
   527
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@252
   528
        return PEP_UNKNOWN_ERROR;
Edouard@252
   529
    }
Edouard@185
   530
Edouard@174
   531
    *keylist = NULL;
Edouard@185
   532
Edouard@185
   533
    vresult = malloc(sizeof(pgp_validation_t));
Edouard@276
   534
    if (vresult == NULL) {
Edouard@276
   535
        result = PEP_OUT_OF_MEMORY;
Edouard@276
   536
        goto unlock_netpgp;
Edouard@276
   537
    }
Edouard@185
   538
    memset(vresult, 0x0, sizeof(pgp_validation_t));
Edouard@185
   539
Edouard@185
   540
    signedmem = pgp_memory_new();
Edouard@185
   541
    if (signedmem == NULL) {
Edouard@252
   542
        result = PEP_OUT_OF_MEMORY;
Edouard@252
   543
        goto unlock_netpgp;
Edouard@185
   544
    }
Edouard@185
   545
    pgp_memory_add(signedmem, (const uint8_t*)text, size);
Edouard@185
   546
Edouard@185
   547
    sig = pgp_memory_new();
Edouard@185
   548
    if (sig == NULL) {
Edouard@185
   549
        pgp_memory_free(signedmem);
Edouard@252
   550
        result = PEP_OUT_OF_MEMORY;
Edouard@252
   551
        goto unlock_netpgp;
Edouard@185
   552
    }
Edouard@185
   553
    pgp_memory_add(sig, (const uint8_t*)signature, sig_size);
Edouard@185
   554
Edouard@252
   555
    pgp_validate_mem_detached(netpgp.io, vresult, sig,
Edouard@185
   556
                NULL,/* output */
Edouard@207
   557
                _armoured(signature, sig_size, ARMOR_SIG_HEAD),
Edouard@252
   558
                netpgp.pubring,
Edouard@185
   559
                signedmem);
Edouard@185
   560
Edouard@252
   561
    result = _validation_results(&netpgp, vresult, &_keylist);
Edouard@185
   562
    if (result != PEP_STATUS_OK) {
Edouard@185
   563
        goto free_pgp;
Edouard@185
   564
    }else{
Edouard@185
   565
        result = PEP_VERIFIED;
Edouard@185
   566
    }
Edouard@185
   567
Edouard@185
   568
    if (result == PEP_VERIFIED) {
Edouard@185
   569
        /* TODO : check trust level */
Edouard@185
   570
        result = PEP_VERIFIED_AND_TRUSTED;
Edouard@185
   571
    }
Edouard@185
   572
Edouard@185
   573
    if (result == PEP_VERIFIED || result == PEP_VERIFIED_AND_TRUSTED) {
Edouard@185
   574
        *keylist = _keylist;
Edouard@185
   575
Edouard@185
   576
        /* _keylist ownership transfer, don't free */
Edouard@185
   577
        goto free_pgp;
Edouard@185
   578
    }
Edouard@185
   579
Edouard@185
   580
    free_stringlist(_keylist);
Edouard@185
   581
Edouard@185
   582
free_pgp:
Edouard@187
   583
    // free done by pgp_validate_mem_detached
Edouard@187
   584
    // pgp_memory_free(sig);
Edouard@187
   585
    // pgp_memory_free(signedmem);
Edouard@185
   586
    pgp_validate_result_free(vresult);
Edouard@185
   587
Edouard@252
   588
unlock_netpgp:
Edouard@252
   589
    pthread_mutex_unlock(&netpgp_mutex);
Edouard@252
   590
Edouard@185
   591
    return result;
Edouard@174
   592
}
Edouard@174
   593
edouard@1693
   594
static PEP_STATUS _encrypt_and_sign(
Edouard@174
   595
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
edouard@1693
   596
    size_t psize, char **ctext, size_t *csize, bool do_sign
Edouard@174
   597
    )
Edouard@174
   598
{
Edouard@412
   599
    pgp_key_t *signer = NULL;
Edouard@412
   600
    pgp_seckey_t *seckey = NULL;
edouard@1693
   601
    pgp_memory_t *signedmem = NULL;
Edouard@209
   602
    pgp_memory_t *cmem;
Edouard@207
   603
    const char *hashalg;
Edouard@209
   604
    pgp_keyring_t *rcpts;
Edouard@207
   605
Edouard@174
   606
    PEP_STATUS result;
Edouard@174
   607
    const stringlist_t *_keylist;
Edouard@174
   608
Edouard@174
   609
    assert(session);
Edouard@174
   610
    assert(keylist);
Edouard@174
   611
    assert(ptext);
Edouard@174
   612
    assert(psize);
Edouard@174
   613
    assert(ctext);
Edouard@174
   614
    assert(csize);
Edouard@174
   615
krista@1435
   616
    if(!session || !ptext || !psize || !ctext || !csize || !keylist)
Edouard@661
   617
        return PEP_ILLEGAL_VALUE;
Edouard@207
   618
Edouard@263
   619
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@252
   620
        return PEP_UNKNOWN_ERROR;
Edouard@252
   621
    }
Edouard@208
   622
Edouard@174
   623
    *ctext = NULL;
Edouard@174
   624
    *csize = 0;
Edouard@174
   625
Edouard@315
   626
    if ((rcpts = calloc(1, sizeof(*rcpts))) == NULL) {
Edouard@315
   627
        result = PEP_OUT_OF_MEMORY;
Edouard@391
   628
        goto unlock_netpgp;
Edouard@315
   629
    }
Edouard@315
   630
    for (_keylist = keylist; _keylist != NULL; _keylist = _keylist->next) {
Edouard@315
   631
        assert(_keylist->value);
krista@1435
   632
Edouard@315
   633
        const pgp_key_t *key;
Edouard@391
   634
        uint8_t fpr[PGP_FINGERPRINT_SIZE];
Edouard@391
   635
        size_t fprlen;
Edouard@315
   636
        unsigned from = 0;
Edouard@315
   637
Edouard@391
   638
        if (str_to_fpr(_keylist->value, fpr, &fprlen)) {
Edouard@391
   639
            if ((key = (pgp_key_t *)pgp_getkeybyfpr(netpgp.io, netpgp.pubring,
Edouard@391
   640
                                                    fpr, fprlen, &from, NULL,
Edouard@391
   641
                                                    /* reject revoked, accept expired */
Edouard@391
   642
                                                    1,0)) == NULL) {
Edouard@391
   643
                result = PEP_KEY_NOT_FOUND;
Edouard@391
   644
                goto free_rcpts;
Edouard@391
   645
            }
Edouard@391
   646
        }else{
Edouard@315
   647
            result = PEP_ILLEGAL_VALUE;
Edouard@315
   648
            goto free_rcpts;
Edouard@315
   649
        }
Edouard@315
   650
Edouard@315
   651
        /* Signer is the first key in the list */
Edouard@315
   652
        if(signer == NULL){
Edouard@315
   653
            from = 0;
Edouard@391
   654
            signer = (pgp_key_t *)pgp_getkeybyfpr(netpgp.io, netpgp.secring,
Edouard@391
   655
                                                  fpr, fprlen,
Edouard@391
   656
                                                  &from,
Edouard@391
   657
                                                  NULL,
Edouard@391
   658
                                                  0,0); /* accept any */
Edouard@315
   659
            if(signer == NULL){
Edouard@315
   660
                result = PEP_KEY_NOT_FOUND;
Edouard@315
   661
                goto free_rcpts;
Edouard@315
   662
            }
Edouard@315
   663
        }
Edouard@315
   664
Edouard@315
   665
        // add key to recipients/signers
Edouard@315
   666
        pgp_keyring_add(rcpts, key);
Edouard@315
   667
        if(rcpts->keys == NULL){
Edouard@315
   668
            result = PEP_OUT_OF_MEMORY;
Edouard@391
   669
            goto free_rcpts;
Edouard@315
   670
        }
Edouard@207
   671
    }
Edouard@209
   672
Edouard@315
   673
    /* Empty keylist ?*/
Edouard@315
   674
    if(rcpts->keyc == 0){
Edouard@315
   675
        result = PEP_ILLEGAL_VALUE;
Edouard@391
   676
        goto free_rcpts;
Edouard@315
   677
    }
Edouard@315
   678
Edouard@315
   679
    seckey = pgp_key_get_certkey(signer);
Edouard@315
   680
Edouard@315
   681
    /* No signig key. Revoked ? */
Edouard@315
   682
    if(seckey == NULL){
Edouard@315
   683
        result = PEP_GET_KEY_FAILED;
Edouard@391
   684
        goto free_rcpts;
Edouard@315
   685
    }
Edouard@286
   686
Edouard@252
   687
    hashalg = netpgp_getvar(&netpgp, "hash");
Edouard@207
   688
edouard@1693
   689
    const char *stext;
edouard@1693
   690
    size_t ssize;
edouard@1693
   691
    unsigned encrypt_raw_packet;
edouard@1693
   692
   
edouard@1693
   693
    if (do_sign) {  
edouard@1693
   694
        // Sign data
edouard@1693
   695
        signedmem = pgp_sign_buf(netpgp.io, ptext, psize, seckey,
edouard@1693
   696
                    time(NULL), /* birthtime */
edouard@1693
   697
                    0 /* duration */,
edouard@1693
   698
                    hashalg,
edouard@1693
   699
                    0 /* armored */,
edouard@1693
   700
                    0 /* cleartext */);
Edouard@207
   701
edouard@1693
   702
        if (!signedmem) {
edouard@1693
   703
            result = PEP_UNENCRYPTED;
edouard@1693
   704
            goto free_rcpts;
edouard@1693
   705
        }
edouard@1693
   706
        stext = (char*) pgp_mem_data(signedmem);
edouard@1693
   707
        ssize = pgp_mem_len(signedmem);
edouard@1693
   708
        encrypt_raw_packet = 1 /* takes raw OpenPGP message */;
edouard@1693
   709
    } else {
edouard@1693
   710
        stext = ptext;
edouard@1693
   711
        ssize = psize;
edouard@1693
   712
        encrypt_raw_packet = 0 /* not a raw OpenPGP message */;
Edouard@207
   713
    }
Edouard@207
   714
edouard@1693
   715
    // Encrypt (maybe) signed data
Edouard@174
   716
edouard@1693
   717
    cmem = pgp_encrypt_buf(netpgp.io, stext,
edouard@1693
   718
            ssize, rcpts, 1 /* armored */,
krista@1435
   719
            netpgp_getvar(&netpgp, "cipher"),
edouard@1693
   720
            encrypt_raw_packet);
Edouard@209
   721
Edouard@209
   722
    if (cmem == NULL) {
Edouard@209
   723
        result = PEP_OUT_OF_MEMORY;
Edouard@209
   724
        goto free_signedmem;
Edouard@209
   725
    }else{
Edouard@209
   726
Edouard@209
   727
        char *_buffer = NULL;
Edouard@209
   728
        size_t length = pgp_mem_len(cmem);
Edouard@174
   729
Edouard@209
   730
        // Allocate transferable buffer
Edouard@209
   731
        _buffer = malloc(length + 1);
Edouard@209
   732
        assert(_buffer);
Edouard@209
   733
        if (_buffer == NULL) {
Edouard@209
   734
            result = PEP_OUT_OF_MEMORY;
Edouard@209
   735
            goto free_cmem;
Edouard@209
   736
        }
Edouard@209
   737
Edouard@209
   738
        memcpy(_buffer, pgp_mem_data(cmem), length);
Edouard@209
   739
Edouard@209
   740
        *ctext = _buffer;
Edouard@209
   741
        *csize = length;
Edouard@209
   742
        (*ctext)[*csize] = 0; // safeguard for naive users
Edouard@209
   743
        result = PEP_STATUS_OK;
Edouard@174
   744
    }
Edouard@174
   745
Edouard@209
   746
free_cmem :
Edouard@209
   747
    pgp_memory_free(cmem);
Edouard@315
   748
free_signedmem :
edouard@1693
   749
    if (do_sign) {
edouard@1693
   750
        pgp_memory_free(signedmem);
edouard@1693
   751
    }
Edouard@209
   752
free_rcpts :
Edouard@209
   753
    pgp_keyring_free(rcpts);
Edouard@252
   754
unlock_netpgp:
Edouard@252
   755
    pthread_mutex_unlock(&netpgp_mutex);
Edouard@175
   756
Edouard@174
   757
    return result;
Edouard@174
   758
}
Edouard@174
   759
edouard@1693
   760
PEP_STATUS pgp_encrypt_and_sign(
edouard@1693
   761
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
edouard@1693
   762
    size_t psize, char **ctext, size_t *csize
edouard@1693
   763
    )
edouard@1693
   764
{
edouard@1693
   765
    PEP_STATUS result;
edouard@1693
   766
    result = _encrypt_and_sign(session, keylist, ptext, psize, ctext, csize,
edouard@1693
   767
                               true);
edouard@1693
   768
    return result;
edouard@1693
   769
}
edouard@1693
   770
krista@1674
   771
PEP_STATUS pgp_encrypt_only(
krista@1674
   772
        PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
krista@1674
   773
        size_t psize, char **ctext, size_t *csize
krista@1674
   774
    )
krista@1674
   775
{
edouard@1693
   776
    PEP_STATUS result;
edouard@1693
   777
    result = _encrypt_and_sign(session, keylist, ptext, psize, ctext, csize,
edouard@1693
   778
                               false);
edouard@1693
   779
    return result;
krista@1674
   780
}
krista@1674
   781
krista@1674
   782
Edouard@174
   783
PEP_STATUS pgp_generate_keypair(
Edouard@174
   784
    PEP_SESSION session, pEp_identity *identity
Edouard@174
   785
    )
Edouard@174
   786
{
Edouard@313
   787
    pgp_key_t	newseckey;
Edouard@316
   788
    pgp_key_t	*newpubkey;
Edouard@225
   789
Edouard@225
   790
    PEP_STATUS result;
Edouard@228
   791
    char newid[1024];
Edouard@225
   792
    const char *hashalg;
Edouard@225
   793
    const char *cipher;
Edouard@174
   794
Edouard@174
   795
    assert(session);
Edouard@174
   796
    assert(identity);
Edouard@174
   797
    assert(identity->address);
Edouard@174
   798
    assert(identity->fpr == NULL);
Edouard@174
   799
    assert(identity->username);
Edouard@174
   800
krista@1435
   801
    if(!session || !identity ||
Edouard@225
   802
       !identity->address || identity->fpr || !identity->username)
Edouard@661
   803
        return PEP_ILLEGAL_VALUE;
Edouard@174
   804
Edouard@263
   805
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@252
   806
        return PEP_UNKNOWN_ERROR;
Edouard@252
   807
    }
Edouard@225
   808
Edouard@225
   809
    if(snprintf(newid, sizeof(newid),
Edouard@225
   810
        "%s <%s>", identity->username, identity->address) >= sizeof(newid)){
Edouard@252
   811
        result =  PEP_BUFFER_TOO_SMALL;
Edouard@252
   812
        goto unlock_netpgp;
Edouard@174
   813
    }
krista@1435
   814
Edouard@252
   815
    hashalg = netpgp_getvar(&netpgp, "hash");
Edouard@252
   816
    cipher = netpgp_getvar(&netpgp, "cipher");
Edouard@174
   817
Edouard@313
   818
    bzero(&newseckey, sizeof(newseckey));
Edouard@225
   819
Edouard@313
   820
    // Generate the key
Edouard@313
   821
    if (!pgp_rsa_generate_keypair(&newseckey, 4096, 65537UL, hashalg, cipher,
Edouard@327
   822
                                  (const uint8_t *) "", (const size_t) 0))
Edouard@327
   823
    {
Edouard@313
   824
        result = PEP_CANNOT_CREATE_KEY;
Edouard@313
   825
        goto free_seckey;
Edouard@225
   826
    }
Edouard@174
   827
Edouard@316
   828
    /* make a public key out of generated secret key */
Edouard@327
   829
    if((newpubkey = pgp_ensure_pubkey(
Edouard@316
   830
            netpgp.pubring,
Edouard@316
   831
            &newseckey.key.seckey.pubkey,
Edouard@327
   832
            newseckey.pubkeyid))==NULL)
Edouard@327
   833
    {
Edouard@316
   834
        result = PEP_OUT_OF_MEMORY;
Edouard@316
   835
        goto free_seckey;
Edouard@316
   836
    }
Edouard@316
   837
Edouard@328
   838
    // "Expire-Date: 1y\n";
krista@1435
   839
    if (!pgp_add_selfsigned_userid(&newseckey, newpubkey,
Edouard@329
   840
                                  (uint8_t *)newid, 365*24*3600))
Edouard@327
   841
    {
Edouard@327
   842
        result = PEP_CANNOT_CREATE_KEY;
Edouard@327
   843
        goto delete_pubkey;
Edouard@327
   844
    }
Edouard@313
   845
Edouard@327
   846
    if (newpubkey == NULL)
Edouard@327
   847
    {
Edouard@327
   848
        result = PEP_OUT_OF_MEMORY;
Edouard@327
   849
        goto delete_pubkey;
Edouard@327
   850
    }
Edouard@313
   851
Edouard@313
   852
    // Append key to netpgp's rings (key ownership transfered)
Edouard@313
   853
    if (!pgp_keyring_add(netpgp.secring, &newseckey)){
Edouard@313
   854
        result = PEP_OUT_OF_MEMORY;
Edouard@316
   855
        goto delete_pubkey;
krista@1435
   856
    }
Edouard@313
   857
krista@1435
   858
    // save rings
Edouard@313
   859
    if (netpgp_save_pubring(&netpgp) && netpgp_save_secring(&netpgp))
Edouard@313
   860
    {
Edouard@316
   861
        char *fprstr = NULL;
Edouard@316
   862
        fpr_to_str(&fprstr,
Edouard@316
   863
                   newseckey.pubkeyfpr.fingerprint,
Edouard@316
   864
                   newseckey.pubkeyfpr.length);
Edouard@316
   865
Edouard@316
   866
        if (fprstr == NULL) {
Edouard@316
   867
            result = PEP_OUT_OF_MEMORY;
Edouard@316
   868
            goto pop_secring;
krista@1435
   869
        }
Edouard@316
   870
Edouard@316
   871
        /* keys saved, pass fingerprint back */
Edouard@313
   872
        identity->fpr = fprstr;
Edouard@313
   873
        result = PEP_STATUS_OK;
Edouard@316
   874
Edouard@313
   875
        /* free nothing, everything transfered */
Edouard@313
   876
        goto unlock_netpgp;
Edouard@313
   877
    } else {
Edouard@313
   878
        /* XXX in case only pubring save succeed
Edouard@313
   879
         * pubring file is left as-is, but backup restore
Edouard@313
   880
         * could be attempted if such corner case matters */
Edouard@313
   881
        result = PEP_UNKNOWN_ERROR;
Edouard@313
   882
    }
Edouard@313
   883
Edouard@313
   884
pop_secring:
Edouard@313
   885
    ((pgp_keyring_t *)netpgp.secring)->keyc--;
Edouard@316
   886
delete_pubkey:
Edouard@316
   887
    pgp_deletekeybyfpr(netpgp.io,
krista@1435
   888
                    (pgp_keyring_t *)netpgp.pubring,
Edouard@316
   889
                    newseckey.pubkeyfpr.fingerprint,
Edouard@316
   890
                    newseckey.pubkeyfpr.length);
Edouard@313
   891
free_seckey:
Edouard@313
   892
    pgp_key_free(&newseckey);
Edouard@252
   893
unlock_netpgp:
Edouard@252
   894
    pthread_mutex_unlock(&netpgp_mutex);
Edouard@228
   895
Edouard@225
   896
    return result;
Edouard@174
   897
}
Edouard@174
   898
Edouard@227
   899
PEP_STATUS pgp_delete_keypair(PEP_SESSION session, const char *fprstr)
Edouard@174
   900
{
Edouard@227
   901
    uint8_t fpr[PGP_FINGERPRINT_SIZE];
Edouard@227
   902
    size_t length;
Edouard@227
   903
Edouard@227
   904
    PEP_STATUS result;
Edouard@227
   905
Edouard@174
   906
    assert(session);
Edouard@386
   907
    assert(fprstr);
Edouard@174
   908
Edouard@386
   909
    if (!session || !fprstr)
Edouard@661
   910
        return PEP_ILLEGAL_VALUE;
Edouard@174
   911
Edouard@263
   912
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@252
   913
        return PEP_UNKNOWN_ERROR;
Edouard@252
   914
    }
krista@1435
   915
Edouard@227
   916
    if (str_to_fpr(fprstr, fpr, &length)) {
Edouard@252
   917
        unsigned insec = pgp_deletekeybyfpr(netpgp.io,
krista@1435
   918
                                (pgp_keyring_t *)netpgp.secring,
Edouard@244
   919
                                (const uint8_t *)fpr, length);
Edouard@252
   920
        unsigned inpub = pgp_deletekeybyfpr(netpgp.io,
krista@1435
   921
                                (pgp_keyring_t *)netpgp.pubring,
Edouard@244
   922
                                (const uint8_t *)fpr, length);
Edouard@244
   923
        if(!insec && !inpub){
Edouard@244
   924
            result = PEP_KEY_NOT_FOUND;
Edouard@252
   925
            goto unlock_netpgp;
Edouard@244
   926
        } else {
Edouard@244
   927
            result = PEP_STATUS_OK;
Edouard@227
   928
        }
Edouard@227
   929
    }else{
Edouard@252
   930
        result = PEP_OUT_OF_MEMORY;
Edouard@252
   931
        goto unlock_netpgp;
Edouard@227
   932
    }
Edouard@174
   933
krista@1435
   934
    // save rings
krista@1435
   935
    if (netpgp_save_pubring(&netpgp) &&
Edouard@252
   936
        netpgp_save_secring(&netpgp))
Edouard@227
   937
    {
Edouard@227
   938
        result = PEP_STATUS_OK;
Edouard@227
   939
    }else{
Edouard@227
   940
        result = PEP_UNKNOWN_ERROR;
Edouard@227
   941
    }
Edouard@227
   942
Edouard@252
   943
unlock_netpgp:
Edouard@252
   944
    pthread_mutex_unlock(&netpgp_mutex);
Edouard@252
   945
Edouard@227
   946
    return result;
Edouard@174
   947
}
Edouard@174
   948
Edouard@389
   949
#define ARMOR_KEY_HEAD    "^-----BEGIN PGP (PUBLIC|PRIVATE) KEY BLOCK-----"_ENDL
Edouard@279
   950
PEP_STATUS pgp_import_keydata(
Edouard@279
   951
        PEP_SESSION session,
krista@1435
   952
        const char *key_data,
edouard@1677
   953
        size_t size,
edouard@1677
   954
        identity_list **private_idents
Edouard@279
   955
    )
Edouard@174
   956
{
Edouard@228
   957
    pgp_memory_t *mem;
Edouard@228
   958
Edouard@271
   959
    PEP_STATUS result = PEP_STATUS_OK;
Edouard@228
   960
Edouard@174
   961
    assert(session);
Edouard@174
   962
    assert(key_data);
Edouard@174
   963
edouard@1677
   964
    // reporting imported private keys not supported
edouard@1677
   965
    // stub code to be reomoved
edouard@1677
   966
    if(private_idents)
edouard@1677
   967
        *private_idents = NULL;
edouard@1677
   968
krista@1435
   969
    if(!session || !key_data)
Edouard@661
   970
        return PEP_ILLEGAL_VALUE;
Edouard@228
   971
Edouard@263
   972
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@252
   973
        return PEP_UNKNOWN_ERROR;
Edouard@252
   974
    }
Edouard@228
   975
Edouard@228
   976
    mem = pgp_memory_new();
Edouard@228
   977
    if (mem == NULL) {
Edouard@252
   978
        result = PEP_OUT_OF_MEMORY;
Edouard@252
   979
        goto unlock_netpgp;
Edouard@228
   980
    }
Edouard@228
   981
    pgp_memory_add(mem, (const uint8_t*)key_data, size);
Edouard@228
   982
krista@1435
   983
    if (pgp_keyring_read_from_mem(netpgp.io, netpgp.pubring, netpgp.secring,
Edouard@228
   984
                                  _armoured(key_data, size, ARMOR_KEY_HEAD),
Edouard@228
   985
                                  mem) == 0){
Edouard@228
   986
        result = PEP_ILLEGAL_VALUE;
krista@1435
   987
    }
Edouard@313
   988
Edouard@228
   989
    pgp_memory_free(mem);
Edouard@228
   990
krista@1435
   991
    // save rings
krista@1435
   992
    if (netpgp_save_pubring(&netpgp) &&
Edouard@325
   993
        netpgp_save_secring(&netpgp))
Edouard@325
   994
    {
Edouard@325
   995
        result = PEP_STATUS_OK;
Edouard@325
   996
    }else{
Edouard@325
   997
        result = PEP_UNKNOWN_ERROR;
Edouard@325
   998
    }
Edouard@325
   999
Edouard@252
  1000
unlock_netpgp:
Edouard@252
  1001
    pthread_mutex_unlock(&netpgp_mutex);
Edouard@252
  1002
Edouard@228
  1003
    return result;
Edouard@174
  1004
}
Edouard@174
  1005
Edouard@318
  1006
static PEP_STATUS _export_keydata(
Edouard@318
  1007
    pgp_key_t *key,
Edouard@318
  1008
    char **buffer,
Edouard@318
  1009
    size_t *buflen
Edouard@318
  1010
    )
Edouard@318
  1011
{
Edouard@318
  1012
    PEP_STATUS result;
Edouard@318
  1013
	pgp_output_t *output;
Edouard@318
  1014
    pgp_memory_t *mem;
Edouard@318
  1015
	pgp_setup_memory_write(&output, &mem, 128);
Edouard@318
  1016
Edouard@318
  1017
    if (mem == NULL || output == NULL) {
Edouard@661
  1018
        return PEP_ILLEGAL_VALUE;
Edouard@318
  1019
    }
Edouard@318
  1020
Edouard@318
  1021
    if (!pgp_write_xfer_key(output, key, 1)) {
Edouard@318
  1022
        result = PEP_UNKNOWN_ERROR;
Edouard@318
  1023
        goto free_mem;
Edouard@318
  1024
    }
Edouard@318
  1025
Edouard@318
  1026
    *buffer = NULL;
Edouard@318
  1027
    *buflen = pgp_mem_len(mem);
Edouard@318
  1028
Edouard@318
  1029
    // Allocate transferable buffer
Edouard@318
  1030
    *buffer = malloc(*buflen + 1);
Edouard@318
  1031
    assert(*buffer);
Edouard@318
  1032
    if (*buffer == NULL) {
Edouard@318
  1033
        result = PEP_OUT_OF_MEMORY;
Edouard@318
  1034
        goto free_mem;
Edouard@318
  1035
    }
Edouard@318
  1036
Edouard@318
  1037
    memcpy(*buffer, pgp_mem_data(mem), *buflen);
Edouard@318
  1038
    (*buffer)[*buflen] = 0; // safeguard for naive users
Edouard@318
  1039
Edouard@318
  1040
    return PEP_STATUS_OK;
Edouard@318
  1041
Edouard@318
  1042
free_mem :
Edouard@318
  1043
	pgp_teardown_memory_write(output, mem);
Edouard@318
  1044
Edouard@318
  1045
    return result;
Edouard@318
  1046
}
Edouard@318
  1047
Edouard@179
  1048
PEP_STATUS pgp_export_keydata(
edouard@1677
  1049
    PEP_SESSION session, const char *fprstr, char **key_data, size_t *size,
edouard@1677
  1050
    bool secret
Edouard@174
  1051
    )
Edouard@174
  1052
{
Edouard@228
  1053
    pgp_key_t *key;
Edouard@228
  1054
    uint8_t fpr[PGP_FINGERPRINT_SIZE];
Edouard@228
  1055
    size_t fprlen;
Edouard@228
  1056
Edouard@228
  1057
    PEP_STATUS result;
Edouard@174
  1058
    char *buffer;
Edouard@228
  1059
    size_t buflen;
Edouard@174
  1060
Edouard@174
  1061
    assert(session);
Edouard@243
  1062
    assert(fprstr);
Edouard@174
  1063
    assert(key_data);
Edouard@174
  1064
    assert(size);
Edouard@174
  1065
edouard@1677
  1066
    // TODO : support export secret key
edouard@1677
  1067
    // crashing stub until export secret supported
edouard@1677
  1068
    assert(!secret);
edouard@1677
  1069
    if (secret)
edouard@1677
  1070
        return PEP_ILLEGAL_VALUE;
edouard@1677
  1071
Edouard@243
  1072
    if (!session || !fprstr || !key_data || !size)
Edouard@661
  1073
        return PEP_ILLEGAL_VALUE;
Edouard@174
  1074
Edouard@263
  1075
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@252
  1076
        return PEP_UNKNOWN_ERROR;
Edouard@252
  1077
    }
Edouard@252
  1078
Edouard@228
  1079
    if (str_to_fpr(fprstr, fpr, &fprlen)) {
Edouard@391
  1080
        unsigned from = 0;
Edouard@391
  1081
Edouard@391
  1082
        if ((key = (pgp_key_t *)pgp_getkeybyfpr(netpgp.io, netpgp.pubring,
Edouard@391
  1083
                                                fpr, fprlen, &from,
Edouard@391
  1084
                                                NULL,0,0)) == NULL) {
Edouard@252
  1085
            result = PEP_KEY_NOT_FOUND;
Edouard@252
  1086
            goto unlock_netpgp;
Edouard@228
  1087
        }
Edouard@228
  1088
    }else{
Edouard@252
  1089
        result = PEP_OUT_OF_MEMORY;
Edouard@252
  1090
        goto unlock_netpgp;
Edouard@228
  1091
    }
krista@1435
  1092
Edouard@318
  1093
    result = _export_keydata(key, &buffer, &buflen);
krista@1435
  1094
Edouard@318
  1095
    if(result == PEP_STATUS_OK)
Edouard@318
  1096
    {
Edouard@318
  1097
        *key_data = buffer;
Edouard@318
  1098
        *size = buflen;
Edouard@318
  1099
        result = PEP_STATUS_OK;
Edouard@228
  1100
    }
Edouard@228
  1101
Edouard@252
  1102
unlock_netpgp:
Edouard@252
  1103
    pthread_mutex_unlock(&netpgp_mutex);
Edouard@228
  1104
Edouard@228
  1105
    return result;
Edouard@174
  1106
}
Edouard@174
  1107
Edouard@271
  1108
struct HKP_answer {
Edouard@271
  1109
  char *memory;
Edouard@271
  1110
  size_t size;
Edouard@271
  1111
};
krista@1435
  1112
Edouard@271
  1113
static size_t
Edouard@271
  1114
HKPAnswerWriter(void *contents, size_t size, size_t nmemb, void *userp)
Edouard@271
  1115
{
Edouard@271
  1116
  size_t realsize = size * nmemb;
Edouard@271
  1117
  struct HKP_answer *mem = (struct HKP_answer *)userp;
krista@1435
  1118
Edouard@271
  1119
  mem->memory = realloc(mem->memory, mem->size + realsize + 1);
Edouard@271
  1120
  if(mem->memory == NULL) {
Edouard@271
  1121
    mem->size = 0;
Edouard@271
  1122
    return 0;
Edouard@271
  1123
  }
krista@1435
  1124
Edouard@271
  1125
  memcpy(&(mem->memory[mem->size]), contents, realsize);
Edouard@271
  1126
  mem->size += realsize;
Edouard@271
  1127
  mem->memory[mem->size] = 0;
krista@1435
  1128
Edouard@271
  1129
  return realsize;
Edouard@271
  1130
}
Edouard@271
  1131
Edouard@323
  1132
#define HKP_SERVER "http://keys.gnupg.net:11371"
Edouard@323
  1133
// #define HKP_SERVER "http://127.0.0.1:11371"
Edouard@321
  1134
Edouard@174
  1135
PEP_STATUS pgp_recv_key(PEP_SESSION session, const char *pattern)
Edouard@174
  1136
{
Edouard@321
  1137
    static const char *ks_cmd = HKP_SERVER
Edouard@321
  1138
                                "/pks/lookup?"
Edouard@271
  1139
                                "op=get&options=mr&exact=on&"
Edouard@271
  1140
                                "search=";
Edouard@271
  1141
    char *encoded_pattern;
Edouard@271
  1142
    char *request = NULL;
Edouard@271
  1143
    struct HKP_answer answer;
Edouard@271
  1144
    CURLcode curlres;
krista@1435
  1145
Edouard@271
  1146
    PEP_STATUS result;
Edouard@271
  1147
Edouard@271
  1148
    CURL *curl;
Edouard@271
  1149
Edouard@174
  1150
    assert(session);
Edouard@174
  1151
    assert(pattern);
Edouard@174
  1152
Edouard@271
  1153
    if (!session || !pattern )
Edouard@661
  1154
        return PEP_ILLEGAL_VALUE;
Edouard@271
  1155
Edouard@271
  1156
    if(pthread_mutex_lock(&session->ctx.curl_mutex)){
Edouard@271
  1157
        return PEP_UNKNOWN_ERROR;
Edouard@271
  1158
    }
Edouard@271
  1159
Edouard@322
  1160
    result = curl_get_ctx(&curl);
Edouard@322
  1161
    if(result != PEP_STATUS_OK){
Edouard@322
  1162
        goto unlock_curl;
Edouard@322
  1163
    }
Edouard@252
  1164
Edouard@271
  1165
    encoded_pattern = curl_easy_escape(curl, (char*)pattern, 0);
Edouard@271
  1166
    if(!encoded_pattern){
Edouard@271
  1167
        result = PEP_OUT_OF_MEMORY;
Edouard@322
  1168
        goto release_curl_ctx;
Edouard@271
  1169
    }
Edouard@271
  1170
Edouard@271
  1171
    if((request = malloc(strlen(ks_cmd) + strlen(encoded_pattern) + 1))==NULL){
Edouard@271
  1172
        result = PEP_OUT_OF_MEMORY;
Edouard@271
  1173
        goto free_encoded_pattern;
Edouard@271
  1174
    }
Edouard@174
  1175
Edouard@271
  1176
    //(*stpcpy(stpcpy(request, ks_cmd), encoded_pattern)) = '\0';
Edouard@271
  1177
    stpcpy(stpcpy(request, ks_cmd), encoded_pattern);
Edouard@271
  1178
Edouard@271
  1179
    curl_easy_setopt(curl, CURLOPT_URL,request);
Edouard@271
  1180
Edouard@271
  1181
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HKPAnswerWriter);
Edouard@271
  1182
Edouard@271
  1183
    answer.memory = NULL;
Edouard@271
  1184
    answer.size = 0;
Edouard@271
  1185
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&answer);
Edouard@174
  1186
Edouard@271
  1187
    curlres = curl_easy_perform(curl);
Edouard@271
  1188
    if(curlres != CURLE_OK) {
Edouard@271
  1189
        result = PEP_GET_KEY_FAILED;
Edouard@271
  1190
        goto free_request;
Edouard@271
  1191
    }
Edouard@271
  1192
Edouard@271
  1193
    if(!answer.memory || !answer.size) {
Edouard@271
  1194
        result = PEP_OUT_OF_MEMORY;
Edouard@271
  1195
        goto free_request;
Edouard@271
  1196
    }
Edouard@174
  1197
krista@1435
  1198
    result = pgp_import_keydata(session,
krista@1435
  1199
                                answer.memory,
edouard@1677
  1200
                                answer.size,
edouard@1677
  1201
                                NULL);
Edouard@271
  1202
Edouard@271
  1203
    free(answer.memory);
Edouard@271
  1204
free_request:
Edouard@271
  1205
    free(request);
Edouard@271
  1206
free_encoded_pattern:
Edouard@271
  1207
    curl_free(encoded_pattern);
Edouard@322
  1208
release_curl_ctx:
Edouard@322
  1209
    curl_release_ctx(&curl);
Edouard@271
  1210
unlock_curl:
Edouard@271
  1211
    pthread_mutex_unlock(&session->ctx.curl_mutex);
Edouard@271
  1212
Edouard@271
  1213
    return result;
Edouard@174
  1214
}
Edouard@174
  1215
Edouard@277
  1216
typedef PEP_STATUS (*find_key_cb_t)(void*, pgp_key_t *);
Edouard@277
  1217
Edouard@277
  1218
static PEP_STATUS find_keys_do(
Edouard@277
  1219
        const char *pattern, find_key_cb_t cb, void* cb_arg)
Edouard@254
  1220
{
Edouard@277
  1221
    uint8_t fpr[PGP_FINGERPRINT_SIZE];
Edouard@277
  1222
    size_t length;
Edouard@277
  1223
    pgp_key_t *key;
Edouard@277
  1224
Edouard@277
  1225
    PEP_STATUS result;
Edouard@277
  1226
Edouard@277
  1227
    // Try find a fingerprint in pattern
Edouard@277
  1228
    if (str_to_fpr(pattern, fpr, &length)) {
Edouard@391
  1229
        unsigned from = 0;
Edouard@391
  1230
Edouard@277
  1231
Edouard@277
  1232
        // Only one fingerprint can match
Edouard@277
  1233
        if ((key = (pgp_key_t *)pgp_getkeybyfpr(
Edouard@277
  1234
                        netpgp.io,
krista@1435
  1235
                        (pgp_keyring_t *)netpgp.pubring,
Edouard@277
  1236
                        (const uint8_t *)fpr, length,
Edouard@391
  1237
                        &from,
Edouard@391
  1238
                        NULL, 0, 0)) == NULL) {
Edouard@277
  1239
Edouard@277
  1240
            return PEP_KEY_NOT_FOUND;
Edouard@277
  1241
        }
Edouard@277
  1242
Edouard@277
  1243
        result = cb(cb_arg, key);
Edouard@277
  1244
Edouard@277
  1245
    } else {
Edouard@277
  1246
        // Search by name for pattern. Can match many.
Edouard@277
  1247
        unsigned from = 0;
Edouard@277
  1248
        result = PEP_KEY_NOT_FOUND;
Edouard@277
  1249
        while((key = (pgp_key_t *)pgp_getnextkeybyname(
Edouard@277
  1250
                        netpgp.io,
krista@1435
  1251
                        (pgp_keyring_t *)netpgp.pubring,
Edouard@277
  1252
			            (const char *)pattern,
Edouard@277
  1253
                        &from)) != NULL) {
Edouard@277
  1254
Edouard@277
  1255
            result = cb(cb_arg, key);
Edouard@277
  1256
            if (result != PEP_STATUS_OK)
Edouard@277
  1257
                break;
Edouard@277
  1258
Edouard@277
  1259
            from++;
Edouard@277
  1260
        }
Edouard@277
  1261
    }
Edouard@277
  1262
Edouard@277
  1263
    return result;
Edouard@277
  1264
}
Edouard@277
  1265
Edouard@277
  1266
static PEP_STATUS add_key_fpr_to_stringlist(void *arg, pgp_key_t *key)
Edouard@277
  1267
{
Edouard@277
  1268
    stringlist_t **keylist = arg;
Edouard@254
  1269
    char *newfprstr = NULL;
Edouard@254
  1270
Edouard@254
  1271
    fpr_to_str(&newfprstr,
Edouard@313
  1272
               key->pubkeyfpr.fingerprint,
Edouard@313
  1273
               key->pubkeyfpr.length);
Edouard@254
  1274
Edouard@254
  1275
    if (newfprstr == NULL) {
Edouard@254
  1276
        return PEP_OUT_OF_MEMORY;
krista@1435
  1277
    } else {
Edouard@254
  1278
krista@1033
  1279
        stringlist_add(*keylist, newfprstr);
roker@869
  1280
        free(newfprstr);
Edouard@254
  1281
        if (*keylist == NULL) {
Edouard@254
  1282
            return PEP_OUT_OF_MEMORY;
Edouard@254
  1283
        }
Edouard@254
  1284
    }
Edouard@254
  1285
    return PEP_STATUS_OK;
Edouard@254
  1286
}
Edouard@254
  1287
krista@1362
  1288
static PEP_STATUS add_secret_key_fpr_to_stringlist(void *arg, pgp_key_t *key)
krista@1362
  1289
{
krista@1362
  1290
    if (pgp_is_key_secret(key)) {
krista@1362
  1291
        stringlist_t **keylist = arg;
krista@1362
  1292
        char *newfprstr = NULL;
krista@1435
  1293
krista@1362
  1294
        fpr_to_str(&newfprstr,
krista@1362
  1295
                key->pubkeyfpr.fingerprint,
krista@1362
  1296
                key->pubkeyfpr.length);
krista@1435
  1297
krista@1362
  1298
        if (newfprstr == NULL) {
krista@1362
  1299
            return PEP_OUT_OF_MEMORY;
krista@1435
  1300
        } else {
krista@1362
  1301
            stringlist_add(*keylist, newfprstr);
krista@1362
  1302
            free(newfprstr);
krista@1362
  1303
            if (*keylist == NULL) {
krista@1362
  1304
                return PEP_OUT_OF_MEMORY;
krista@1362
  1305
            }
krista@1362
  1306
        }
krista@1362
  1307
    }
krista@1362
  1308
    return PEP_STATUS_OK;
krista@1362
  1309
}
krista@1362
  1310
krista@1031
  1311
static PEP_STATUS add_keyinfo_to_stringpair_list(void* arg, pgp_key_t *key) {
krista@1031
  1312
    stringpair_list_t** keyinfo_list = (stringpair_list_t**)arg;
krista@1033
  1313
    stringpair_t* pair = NULL;
krista@1031
  1314
    char* id_fpr = NULL;
krista@1031
  1315
    char* primary_userid = (char*)pgp_key_get_primary_userid(key);
krista@1435
  1316
hernani@1390
  1317
// Unused:
hernani@1389
  1318
//    bool key_revoked = false;
krista@1435
  1319
krista@1040
  1320
//    PEP_STATUS key_status = pgp_key_revoked(session, id_fpr, &key_revoked);
krista@1435
  1321
krista@1040
  1322
//    if (key_revoked || key_status == PEP_GET_KEY_FAILED)
krista@1040
  1323
//        return PEP_STATUS_OK; // we just move on
krista@1435
  1324
krista@1031
  1325
    fpr_to_str(&id_fpr, key->pubkeyfpr.fingerprint,
krista@1031
  1326
                key->pubkeyfpr.length);
krista@1031
  1327
krista@1031
  1328
    pair = new_stringpair(id_fpr, primary_userid);
krista@1435
  1329
krista@1031
  1330
    if (pair == NULL)
krista@1031
  1331
        return PEP_OUT_OF_MEMORY;
krista@1435
  1332
krista@1031
  1333
    *keyinfo_list = stringpair_list_add(*keyinfo_list, pair);
krista@1031
  1334
    free(id_fpr);
krista@1031
  1335
    if (*keyinfo_list == NULL)
krista@1031
  1336
        return PEP_OUT_OF_MEMORY;
krista@1031
  1337
    return PEP_STATUS_OK;
krista@1031
  1338
}
krista@1031
  1339
Edouard@174
  1340
PEP_STATUS pgp_find_keys(
Edouard@174
  1341
    PEP_SESSION session, const char *pattern, stringlist_t **keylist
Edouard@174
  1342
    )
Edouard@174
  1343
{
Edouard@254
  1344
    stringlist_t *_keylist, *_k;
Edouard@254
  1345
Edouard@254
  1346
    PEP_STATUS result;
Edouard@174
  1347
Edouard@174
  1348
    assert(session);
Edouard@174
  1349
    assert(pattern);
Edouard@174
  1350
    assert(keylist);
Edouard@174
  1351
Edouard@254
  1352
    if (!session || !pattern || !keylist )
Edouard@661
  1353
    {
Edouard@661
  1354
        return PEP_ILLEGAL_VALUE;
Edouard@661
  1355
    }
Edouard@174
  1356
Edouard@661
  1357
    if (pthread_mutex_lock(&netpgp_mutex))
Edouard@661
  1358
    {
Edouard@174
  1359
        return PEP_UNKNOWN_ERROR;
Edouard@254
  1360
    }
Edouard@254
  1361
Edouard@254
  1362
    *keylist = NULL;
Edouard@254
  1363
    _keylist = new_stringlist(NULL);
Edouard@384
  1364
    if (_keylist == NULL) {
Edouard@254
  1365
        result = PEP_OUT_OF_MEMORY;
Edouard@254
  1366
        goto unlock_netpgp;
Edouard@254
  1367
    }
Edouard@254
  1368
    _k = _keylist;
Edouard@254
  1369
Edouard@277
  1370
    result = find_keys_do(pattern, &add_key_fpr_to_stringlist, &_k);
Edouard@254
  1371
Edouard@254
  1372
    if (result == PEP_STATUS_OK) {
Edouard@254
  1373
        *keylist = _keylist;
Edouard@254
  1374
        // Transfer ownership, no free
Edouard@254
  1375
        goto unlock_netpgp;
Edouard@254
  1376
    }
Edouard@254
  1377
Edouard@254
  1378
    free_stringlist(_keylist);
Edouard@254
  1379
Edouard@254
  1380
unlock_netpgp:
Edouard@254
  1381
    pthread_mutex_unlock(&netpgp_mutex);
Edouard@254
  1382
Edouard@254
  1383
    return result;
Edouard@174
  1384
}
Edouard@174
  1385
Edouard@321
  1386
#define HKP_REQ_PREFIX "keytext="
Edouard@321
  1387
#define HKP_REQ_PREFIX_LEN 8
Edouard@321
  1388
Edouard@277
  1389
static PEP_STATUS send_key_cb(void *arg, pgp_key_t *key)
Edouard@277
  1390
{
Edouard@318
  1391
    char *buffer = NULL;
Edouard@318
  1392
    size_t buflen = 0;
Edouard@318
  1393
    PEP_STATUS result;
Edouard@318
  1394
    stringlist_t *encoded_keys;
Edouard@318
  1395
    encoded_keys = (stringlist_t*)arg;
Edouard@277
  1396
Edouard@318
  1397
    result = _export_keydata(key, &buffer, &buflen);
krista@1435
  1398
Edouard@318
  1399
    if(result == PEP_STATUS_OK){
roker@869
  1400
        char *encoded_key = curl_escape(buffer, (int)buflen);
Edouard@318
  1401
        if(!encoded_key){
Edouard@321
  1402
            result = PEP_OUT_OF_MEMORY;
Edouard@321
  1403
            goto free_buffer;
Edouard@318
  1404
        }
roker@869
  1405
        size_t encoded_key_len = strlen(encoded_key);
Edouard@321
  1406
roker@869
  1407
        char *request = calloc(1, HKP_REQ_PREFIX_LEN + encoded_key_len + 1);
Edouard@321
  1408
        if(!request){
Edouard@321
  1409
            result = PEP_OUT_OF_MEMORY;
Edouard@321
  1410
            goto free_encoded_key;
Edouard@321
  1411
        }
krista@1435
  1412
Edouard@321
  1413
        memcpy(request, HKP_REQ_PREFIX, HKP_REQ_PREFIX_LEN);
Edouard@321
  1414
        memcpy(request + HKP_REQ_PREFIX_LEN, encoded_key, encoded_key_len);
Edouard@321
  1415
        request[HKP_REQ_PREFIX_LEN + encoded_key_len] = '\0';
Edouard@321
  1416
Edouard@321
  1417
        if(!stringlist_add(encoded_keys, request)){
roker@869
  1418
            free(request);
Edouard@318
  1419
            result = PEP_OUT_OF_MEMORY;
Edouard@318
  1420
        }
Edouard@321
  1421
Edouard@321
  1422
        free(request);
Edouard@321
  1423
Edouard@321
  1424
free_encoded_key:
Edouard@318
  1425
        curl_free(encoded_key);
Edouard@321
  1426
krista@1435
  1427
free_buffer:
Edouard@321
  1428
        free(buffer);
Edouard@318
  1429
    }
Edouard@277
  1430
Edouard@318
  1431
    return result;
Edouard@277
  1432
}
Edouard@277
  1433
Edouard@174
  1434
PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)
Edouard@174
  1435
{
Edouard@321
  1436
    static const char *ks_cmd = HKP_SERVER "/pks/add";
Edouard@318
  1437
Edouard@277
  1438
    PEP_STATUS result;
roker@869
  1439
    CURL *curl = NULL;
Edouard@322
  1440
Edouard@174
  1441
    assert(session);
Edouard@174
  1442
    assert(pattern);
Edouard@174
  1443
Edouard@277
  1444
    if (!session || !pattern )
Edouard@661
  1445
        return PEP_ILLEGAL_VALUE;
Edouard@277
  1446
roker@869
  1447
    stringlist_t *encoded_keys = new_stringlist(NULL);
Edouard@318
  1448
    assert(encoded_keys);
Edouard@318
  1449
    if (encoded_keys == NULL) {
Edouard@318
  1450
        return PEP_OUT_OF_MEMORY;
Edouard@318
  1451
    }
Edouard@318
  1452
Edouard@277
  1453
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@322
  1454
        result = PEP_UNKNOWN_ERROR;
Edouard@322
  1455
        goto free_encoded_keys;
Edouard@277
  1456
    }
Edouard@174
  1457
Edouard@318
  1458
    result = find_keys_do(pattern, &send_key_cb, (void*)encoded_keys);
Edouard@277
  1459
Edouard@277
  1460
    pthread_mutex_unlock(&netpgp_mutex);
Edouard@277
  1461
Edouard@322
  1462
    if(result != PEP_STATUS_OK){
Edouard@322
  1463
        goto free_encoded_keys;
Edouard@322
  1464
    }
Edouard@322
  1465
Edouard@318
  1466
    if(pthread_mutex_lock(&session->ctx.curl_mutex)){
Edouard@322
  1467
        result = PEP_UNKNOWN_ERROR;
Edouard@322
  1468
        goto free_encoded_keys;
Edouard@322
  1469
    }
Edouard@322
  1470
Edouard@322
  1471
    result = curl_get_ctx(&curl);
Edouard@322
  1472
    if(result != PEP_STATUS_OK){
Edouard@322
  1473
        goto unlock_curl;
Edouard@318
  1474
    }
Edouard@318
  1475
Edouard@318
  1476
    if(result == PEP_STATUS_OK){
Edouard@318
  1477
        CURLcode curlres;
Edouard@318
  1478
roker@869
  1479
        for (const stringlist_t *post = encoded_keys; post != NULL; post = post->next) {
Edouard@318
  1480
            assert(post->value);
Edouard@318
  1481
Edouard@318
  1482
            curl_easy_setopt(curl, CURLOPT_URL, ks_cmd);
Edouard@318
  1483
            curl_easy_setopt(curl, CURLOPT_POST, 1L);
Edouard@318
  1484
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post->value);
Edouard@318
  1485
            curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
Edouard@318
  1486
Edouard@323
  1487
            // Uncomment if debugging
Edouard@323
  1488
            // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
Edouard@318
  1489
Edouard@318
  1490
            curlres = curl_easy_perform(curl);
Edouard@318
  1491
Edouard@318
  1492
            if(curlres != CURLE_OK) {
Edouard@318
  1493
Edouard@318
  1494
                result = PEP_CANNOT_SEND_KEY;
Edouard@322
  1495
                goto release_curl_ctx;
Edouard@318
  1496
            }
Edouard@318
  1497
        }
Edouard@318
  1498
    }
Edouard@318
  1499
Edouard@322
  1500
release_curl_ctx:
Edouard@322
  1501
    curl_release_ctx(&curl);
Edouard@322
  1502
unlock_curl:
Edouard@322
  1503
    pthread_mutex_unlock(&session->ctx.curl_mutex);
Edouard@318
  1504
free_encoded_keys:
Edouard@318
  1505
    free_stringlist(encoded_keys);
Edouard@318
  1506
Edouard@277
  1507
    return result;
Edouard@174
  1508
}
Edouard@174
  1509
Edouard@174
  1510
Edouard@174
  1511
PEP_STATUS pgp_get_key_rating(
Edouard@174
  1512
    PEP_SESSION session,
Edouard@384
  1513
    const char *fprstr,
Edouard@174
  1514
    PEP_comm_type *comm_type
Edouard@174
  1515
    )
Edouard@174
  1516
{
Edouard@412
  1517
    pgp_key_t *key;
Edouard@384
  1518
    uint8_t fpr[PGP_FINGERPRINT_SIZE];
Edouard@324
  1519
    unsigned from = 0;
Edouard@384
  1520
    size_t length;
Edouard@384
  1521
Edouard@324
  1522
Edouard@174
  1523
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@174
  1524
Edouard@174
  1525
    assert(session);
Edouard@384
  1526
    assert(fprstr);
Edouard@174
  1527
    assert(comm_type);
Edouard@174
  1528
Edouard@384
  1529
    if (!session || !fprstr || !comm_type )
Edouard@661
  1530
        return PEP_ILLEGAL_VALUE;
Edouard@324
  1531
Edouard@384
  1532
    *comm_type = PEP_ct_unknown;
Edouard@384
  1533
Edouard@324
  1534
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@324
  1535
        return PEP_UNKNOWN_ERROR;
Edouard@324
  1536
    }
Edouard@324
  1537
Edouard@384
  1538
    if (!str_to_fpr(fprstr, fpr, &length)) {
Edouard@324
  1539
        status = PEP_ILLEGAL_VALUE;
Edouard@324
  1540
        goto unlock_netpgp;
Edouard@324
  1541
    }
krista@1435
  1542
Edouard@384
  1543
    key = pgp_getkeybyfpr(
Edouard@384
  1544
           netpgp.io,
Edouard@384
  1545
           netpgp.pubring,
Edouard@391
  1546
           fpr, length, &from, NULL,0,0);
Edouard@174
  1547
Edouard@324
  1548
    if(key == NULL)
Edouard@324
  1549
    {
Edouard@324
  1550
        status = PEP_KEY_NOT_FOUND;
Edouard@324
  1551
        goto unlock_netpgp;
Edouard@324
  1552
    }
Edouard@324
  1553
Edouard@324
  1554
    switch(pgp_key_get_rating(key)){
Edouard@324
  1555
	case PGP_VALID:
Edouard@174
  1556
        *comm_type = PEP_ct_OpenPGP_unconfirmed;
Edouard@174
  1557
        break;
Edouard@324
  1558
    case PGP_WEAK:
Edouard@324
  1559
        *comm_type = PEP_ct_OpenPGP_weak_unconfirmed;
Edouard@324
  1560
        break;
Edouard@324
  1561
    case PGP_TOOSHORT:
Edouard@324
  1562
        *comm_type = PEP_ct_key_too_short;
Edouard@324
  1563
        break;
Edouard@324
  1564
	case PGP_INVALID:
Edouard@324
  1565
        *comm_type = PEP_ct_key_b0rken;
Edouard@324
  1566
        break;
Edouard@324
  1567
	case PGP_EXPIRED:
Edouard@324
  1568
        *comm_type = PEP_ct_key_expired;
Edouard@324
  1569
        break;
Edouard@324
  1570
    case PGP_REVOKED:
Edouard@324
  1571
        *comm_type = PEP_ct_key_revoked;
Edouard@174
  1572
        break;
Edouard@174
  1573
    default:
Edouard@324
  1574
        break;
Edouard@174
  1575
    }
Edouard@174
  1576
Edouard@324
  1577
unlock_netpgp:
Edouard@324
  1578
    pthread_mutex_unlock(&netpgp_mutex);
Edouard@174
  1579
Edouard@174
  1580
    return status;
Edouard@174
  1581
}
Edouard@210
  1582
Edouard@210
  1583
PEP_STATUS pgp_renew_key(
Edouard@210
  1584
        PEP_SESSION session,
Edouard@511
  1585
        const char *fprstr,
Edouard@210
  1586
        const timestamp *ts
Edouard@210
  1587
    )
Edouard@210
  1588
{
Edouard@329
  1589
    pgp_key_t *pkey;
Edouard@329
  1590
    pgp_key_t *skey;
Edouard@511
  1591
    uint8_t fpr[PGP_FINGERPRINT_SIZE];
Edouard@511
  1592
    size_t length;
Edouard@329
  1593
    unsigned from = 0;
Edouard@412
  1594
    time_t duration;
Edouard@412
  1595
    const uint8_t *primid;
Edouard@329
  1596
Edouard@210
  1597
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@210
  1598
Edouard@210
  1599
    assert(session);
Edouard@511
  1600
    assert(fprstr);
Edouard@329
  1601
Edouard@511
  1602
    if (!session || !fprstr )
Edouard@661
  1603
        return PEP_ILLEGAL_VALUE;
Edouard@329
  1604
Edouard@329
  1605
    if(ts)
Edouard@329
  1606
    {
Edouard@329
  1607
        time_t    now, when;
Edouard@329
  1608
        now = time(NULL);
Edouard@329
  1609
        when = mktime((struct tm*)ts);
Edouard@329
  1610
        if(now && when && when > now){
Edouard@412
  1611
            duration = when - now;
Edouard@329
  1612
        }else{
Edouard@329
  1613
            return PEP_ILLEGAL_VALUE;
Edouard@329
  1614
        }
Edouard@329
  1615
    }else{
Edouard@329
  1616
        /* Default 1 year from now */
Edouard@329
  1617
        duration = 365*24*3600;
Edouard@329
  1618
    }
Edouard@329
  1619
Edouard@329
  1620
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@329
  1621
        return PEP_UNKNOWN_ERROR;
Edouard@329
  1622
    }
Edouard@210
  1623
krista@1435
  1624
Edouard@511
  1625
    if (!str_to_fpr(fprstr, fpr, &length)) {
Edouard@329
  1626
        status = PEP_ILLEGAL_VALUE;
Edouard@329
  1627
        goto unlock_netpgp;
Edouard@329
  1628
    }
krista@1435
  1629
Edouard@511
  1630
    pkey = pgp_getkeybyfpr(
Edouard@511
  1631
                          netpgp.io,
Edouard@511
  1632
                          netpgp.pubring,
Edouard@511
  1633
                          fpr, length, &from, NULL,
Edouard@511
  1634
                          1, 0); /* reject revoked, accept expired */
Edouard@329
  1635
Edouard@329
  1636
    if(pkey == NULL)
Edouard@329
  1637
    {
Edouard@329
  1638
        status = PEP_KEY_NOT_FOUND;
Edouard@329
  1639
        goto unlock_netpgp;
Edouard@329
  1640
    }
Edouard@329
  1641
Edouard@329
  1642
    from = 0;
Edouard@511
  1643
    skey = pgp_getkeybyfpr(
Edouard@511
  1644
                           netpgp.io,
Edouard@521
  1645
                           netpgp.secring,
Edouard@511
  1646
                           fpr, length, &from, NULL,
Edouard@511
  1647
                           1, 0); /* reject revoked, accept expired */
Edouard@329
  1648
Edouard@329
  1649
    if(skey == NULL)
Edouard@329
  1650
    {
Edouard@329
  1651
        status = PEP_KEY_NOT_FOUND;
Edouard@329
  1652
        goto unlock_netpgp;
Edouard@329
  1653
    }
Edouard@329
  1654
Edouard@412
  1655
    if((primid = pgp_key_get_primary_userid(skey)) == NULL)
Edouard@329
  1656
    {
Edouard@329
  1657
        status = PEP_KEY_HAS_AMBIG_NAME;
Edouard@329
  1658
        goto unlock_netpgp;
Edouard@329
  1659
    }
Edouard@210
  1660
Edouard@522
  1661
    // FIXME : renew in a more gentle way
Edouard@329
  1662
    if (!pgp_add_selfsigned_userid(skey, pkey, primid, duration))
Edouard@329
  1663
    {
Edouard@329
  1664
        status = PEP_CANNOT_CREATE_KEY;
Edouard@329
  1665
        goto unlock_netpgp;
Edouard@329
  1666
    }
Edouard@329
  1667
Edouard@521
  1668
    // save rings
Edouard@521
  1669
    if (netpgp_save_pubring(&netpgp) &&
Edouard@521
  1670
        netpgp_save_secring(&netpgp))
Edouard@521
  1671
    {
Edouard@521
  1672
        status = PEP_STATUS_OK;
Edouard@521
  1673
    }else{
Edouard@521
  1674
        status = PEP_UNKNOWN_ERROR;
Edouard@521
  1675
    }
krista@1435
  1676
Edouard@329
  1677
unlock_netpgp:
Edouard@329
  1678
    pthread_mutex_unlock(&netpgp_mutex);
Edouard@329
  1679
Edouard@329
  1680
    return status;
Edouard@210
  1681
}
Edouard@210
  1682
Edouard@226
  1683
PEP_STATUS pgp_revoke_key(
Edouard@226
  1684
        PEP_SESSION session,
Edouard@522
  1685
        const char *fprstr,
Edouard@226
  1686
        const char *reason
Edouard@226
  1687
    )
Edouard@210
  1688
{
Edouard@522
  1689
    uint8_t fpr[PGP_FINGERPRINT_SIZE];
Edouard@522
  1690
    size_t length;
Edouard@326
  1691
    unsigned from = 0;
Edouard@326
  1692
Edouard@210
  1693
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@326
  1694
Edouard@210
  1695
    assert(session);
Edouard@522
  1696
    assert(fprstr);
Edouard@210
  1697
Edouard@522
  1698
    if (!session || !fprstr)
Edouard@210
  1699
        return PEP_UNKNOWN_ERROR;
Edouard@210
  1700
Edouard@326
  1701
    if(pthread_mutex_lock(&netpgp_mutex)){
Edouard@326
  1702
        return PEP_UNKNOWN_ERROR;
Edouard@326
  1703
    }
Edouard@326
  1704
Edouard@522
  1705
    // FIXME : deduplicate that code w/ renew
Edouard@522
  1706
    if (!str_to_fpr(fprstr, fpr, &length)) {
Edouard@326
  1707
        status = PEP_ILLEGAL_VALUE;
Edouard@326
  1708
        goto unlock_netpgp;
Edouard@326
  1709
    }
krista@1435
  1710
roker@869
  1711
    pgp_key_t *pkey = pgp_getkeybyfpr(
Edouard@522
  1712
                           netpgp.io,
Edouard@522
  1713
                           netpgp.pubring,
Edouard@522
  1714
                           fpr, length, &from, NULL,
Edouard@522
  1715
                           1, 0); /* reject revoked, accept expired */
krista@1435
  1716
Edouard@326
  1717
    if(pkey == NULL)
Edouard@326
  1718
    {
Edouard@326
  1719
        status = PEP_KEY_NOT_FOUND;
Edouard@326
  1720
        goto unlock_netpgp;
Edouard@326
  1721
    }
krista@1435
  1722
Edouard@326
  1723
    from = 0;
roker@869
  1724
    pgp_key_t *skey = pgp_getkeybyfpr(
Edouard@522
  1725
                           netpgp.io,
Edouard@522
  1726
                           netpgp.secring,
Edouard@522
  1727
                           fpr, length, &from, NULL,
Edouard@522
  1728
                           1, 0); /* reject revoked, accept expired */
krista@1435
  1729
Edouard@326
  1730
    if(skey == NULL)
Edouard@326
  1731
    {
Edouard@326
  1732
        status = PEP_KEY_NOT_FOUND;
Edouard@326
  1733
        goto unlock_netpgp;
Edouard@326
  1734
    }
Edouard@326
  1735
Edouard@326
  1736
    pgp_key_revoke(skey, pkey,
Edouard@326
  1737
                   0, /* no reason code specified */
Edouard@326
  1738
                   reason);
Edouard@326
  1739
Edouard@326
  1740
unlock_netpgp:
Edouard@326
  1741
    pthread_mutex_unlock(&netpgp_mutex);
Edouard@326
  1742
Edouard@326
  1743
    return status;
Edouard@210
  1744
}
Edouard@210
  1745
Edouard@226
  1746
PEP_STATUS pgp_key_expired(
Edouard@226
  1747
        PEP_SESSION session,
Edouard@661
  1748
        const char *fprstr,
Edouard@701
  1749
        const time_t when,
Edouard@226
  1750
        bool *expired
Edouard@226
  1751
    )
Edouard@226
  1752
{
Edouard@226
  1753
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@325
  1754
    PEP_comm_type comm_type;
Edouard@226
  1755
Edouard@226
  1756
    assert(session);
Edouard@661
  1757
    assert(fprstr);
Edouard@226
  1758
    assert(expired);
Edouard@226
  1759
Edouard@661
  1760
    if (!session || !fprstr || !expired)
Edouard@661
  1761
        return PEP_UNKNOWN_ERROR;
Edouard@661
  1762
krista@1435
  1763
    // TODO : take "when" in account
Edouard@661
  1764
Edouard@226
  1765
    *expired = false;
Edouard@226
  1766
Edouard@661
  1767
    status = pgp_get_key_rating(session, fprstr, &comm_type);
Edouard@325
  1768
Edouard@226
  1769
    if (status != PEP_STATUS_OK)
Edouard@226
  1770
        return status;
Edouard@226
  1771
Edouard@325
  1772
    if (comm_type == PEP_ct_key_expired){
Edouard@325
  1773
        *expired = true;
Edouard@325
  1774
    }
Edouard@325
  1775
Edouard@226
  1776
    return PEP_STATUS_OK;
Edouard@226
  1777
}
Edouard@226
  1778
Edouard@661
  1779
PEP_STATUS pgp_key_revoked(
Edouard@661
  1780
        PEP_SESSION session,
Edouard@661
  1781
        const char *fprstr,
Edouard@661
  1782
        bool *revoked
Edouard@661
  1783
    )
Edouard@661
  1784
{
Edouard@661
  1785
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@661
  1786
    PEP_comm_type comm_type;
krista@1435
  1787
Edouard@661
  1788
    assert(session);
Edouard@661
  1789
    assert(fprstr);
Edouard@661
  1790
    assert(revoked);
krista@1435
  1791
Edouard@661
  1792
    *revoked = false;
krista@1435
  1793
Edouard@661
  1794
    status = pgp_get_key_rating(session, fprstr, &comm_type);
krista@1435
  1795
Edouard@661
  1796
    if (status != PEP_STATUS_OK)
Edouard@661
  1797
        return status;
krista@1435
  1798
Edouard@661
  1799
    if (comm_type == PEP_ct_key_revoked){
Edouard@661
  1800
        *revoked = true;
Edouard@661
  1801
    }
krista@1435
  1802
Edouard@661
  1803
    return PEP_STATUS_OK;
Edouard@661
  1804
}
krista@1010
  1805
dirk@973
  1806
PEP_STATUS pgp_key_created(
dirk@973
  1807
        PEP_SESSION session,
dirk@973
  1808
        const char *fprstr,
dirk@973
  1809
        time_t *created
dirk@973
  1810
    )
dirk@973
  1811
{
dirk@973
  1812
    uint8_t fpr[PGP_FINGERPRINT_SIZE];
dirk@973
  1813
    pgp_key_t *key;
dirk@973
  1814
    size_t length;
dirk@973
  1815
    unsigned from = 0;
dirk@973
  1816
dirk@973
  1817
    PEP_STATUS status = PEP_STATUS_OK;
dirk@973
  1818
dirk@973
  1819
    assert(session);
dirk@973
  1820
    assert(fprstr);
dirk@973
  1821
    assert(created);
dirk@973
  1822
dirk@973
  1823
    if (!session || !fprstr || !created)
dirk@973
  1824
        return PEP_UNKNOWN_ERROR;
dirk@973
  1825
dirk@973
  1826
    *created = 0;
dirk@973
  1827
dirk@973
  1828
    if(pthread_mutex_lock(&netpgp_mutex)){
dirk@973
  1829
        return PEP_UNKNOWN_ERROR;
dirk@973
  1830
    }
dirk@973
  1831
dirk@973
  1832
    if (!str_to_fpr(fprstr, fpr, &length)) {
dirk@973
  1833
        status = PEP_ILLEGAL_VALUE;
dirk@973
  1834
        goto unlock_netpgp;
dirk@973
  1835
    }
krista@1435
  1836
dirk@973
  1837
    key = pgp_getkeybyfpr(
dirk@973
  1838
           netpgp.io,
dirk@973
  1839
           netpgp.pubring,
dirk@973
  1840
           fpr, length, &from, NULL,0,0);
dirk@973
  1841
dirk@973
  1842
    if (key)
dirk@973
  1843
    {
dirk@973
  1844
        *created = (time_t) key->key.pubkey.birthtime;
dirk@973
  1845
    }
dirk@973
  1846
    else
dirk@973
  1847
    {
dirk@973
  1848
        status = PEP_KEY_NOT_FOUND;
dirk@973
  1849
        goto unlock_netpgp;
dirk@973
  1850
    }
krista@1435
  1851
dirk@973
  1852
dirk@973
  1853
dirk@973
  1854
unlock_netpgp:
dirk@973
  1855
    pthread_mutex_unlock(&netpgp_mutex);
dirk@973
  1856
dirk@973
  1857
    return status;
dirk@973
  1858
}
roker@1057
  1859
krista@1018
  1860
krista@1030
  1861
PEP_STATUS pgp_list_keyinfo(
krista@1030
  1862
        PEP_SESSION session, const char* pattern, stringpair_list_t** keyinfo_list)
krista@1030
  1863
{
krista@1435
  1864
krista@1030
  1865
    if (!session || !keyinfo_list)
krista@1030
  1866
        return PEP_UNKNOWN_ERROR;
krista@1435
  1867
krista@1030
  1868
    if (pthread_mutex_lock(&netpgp_mutex))
krista@1030
  1869
    {
krista@1030
  1870
        return PEP_UNKNOWN_ERROR;
krista@1018
  1871
    }
krista@1435
  1872
hernani@1390
  1873
// Unused:
hernani@1390
  1874
//    pgp_key_t *key;
krista@1010
  1875
krista@1010
  1876
    PEP_STATUS result;
krista@1435
  1877
krista@1031
  1878
    result = find_keys_do(pattern, &add_keyinfo_to_stringpair_list, (void*)keyinfo_list);
krista@1435
  1879
krista@1031
  1880
    if (!keyinfo_list)
krista@1031
  1881
        result = PEP_KEY_NOT_FOUND;
krista@1435
  1882
krista@1030
  1883
    pthread_mutex_unlock(&netpgp_mutex);
krista@1435
  1884
krista@1018
  1885
    return result;
krista@1018
  1886
}
krista@1362
  1887
krista@1362
  1888
/* copied from find_keys, but we need to use a callback that filters. */
krista@1363
  1889
PEP_STATUS pgp_find_private_keys(
krista@1362
  1890
    PEP_SESSION session, const char *pattern, stringlist_t **keylist)
krista@1362
  1891
{
krista@1362
  1892
    stringlist_t *_keylist, *_k;
krista@1435
  1893
krista@1362
  1894
    PEP_STATUS result;
krista@1435
  1895
krista@1362
  1896
    assert(session);
krista@1362
  1897
    assert(pattern);
krista@1362
  1898
    assert(keylist);
krista@1435
  1899
krista@1362
  1900
    if (!session || !pattern || !keylist )
krista@1362
  1901
    {
krista@1362
  1902
        return PEP_ILLEGAL_VALUE;
krista@1362
  1903
    }
krista@1435
  1904
krista@1362
  1905
    if (pthread_mutex_lock(&netpgp_mutex))
krista@1362
  1906
    {
krista@1362
  1907
        return PEP_UNKNOWN_ERROR;
krista@1362
  1908
    }
krista@1435
  1909
krista@1362
  1910
    *keylist = NULL;
krista@1362
  1911
    _keylist = new_stringlist(NULL);
krista@1362
  1912
    if (_keylist == NULL) {
krista@1362
  1913
        result = PEP_OUT_OF_MEMORY;
krista@1362
  1914
        goto unlock_netpgp;
krista@1362
  1915
    }
krista@1362
  1916
    _k = _keylist;
krista@1435
  1917
krista@1362
  1918
    result = find_keys_do(pattern, &add_secret_key_fpr_to_stringlist, &_k);
krista@1435
  1919
krista@1362
  1920
    if (result == PEP_STATUS_OK) {
krista@1362
  1921
        *keylist = _keylist;
krista@1362
  1922
        // Transfer ownership, no free
krista@1362
  1923
        goto unlock_netpgp;
krista@1362
  1924
    }
krista@1435
  1925
krista@1362
  1926
    free_stringlist(_keylist);
krista@1435
  1927
krista@1362
  1928
unlock_netpgp:
krista@1362
  1929
    pthread_mutex_unlock(&netpgp_mutex);
krista@1435
  1930
krista@1435
  1931
    return result;
krista@1362
  1932
}
krista@1362
  1933
krista@1362
  1934
PEP_STATUS pgp_contains_priv_key(
krista@1435
  1935
    PEP_SESSION session,
krista@1362
  1936
    const char *fpr,
krista@1362
  1937
    bool *has_private) {
krista@1362
  1938
    stringlist_t* keylist = NULL;
krista@1363
  1939
    PEP_STATUS status = pgp_find_private_keys(session, fpr, &keylist);
krista@1362
  1940
    if (status == PEP_STATUS_OK && keylist) {
krista@1362
  1941
        free_stringlist(keylist);
krista@1362
  1942
        *has_private = true;
krista@1362
  1943
    }
krista@1362
  1944
    else {
krista@1362
  1945
        has_private = false;
krista@1362
  1946
    }
krista@1362
  1947
    return status;
krista@1362
  1948
}