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