src/pEp_internal.h
author Krista Bennett <krista@pep-project.org>
Tue, 14 Aug 2018 13:45:34 +0200
branchENGINE-451
changeset 2826 07c080d73a63
parent 2791 9e7e4d84b06c
permissions -rw-r--r--
ENGINE-451: abandoning branch - config files to be dealt with using gpgme (and hotfix will be external to engine
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
krista@2789
     4
krista@2789
     5
// THESE MUST ALL BE CHANGED TOGETHER!!!!
krista@2791
     6
#define PEP_ENGINE_VERSION "1.0.441"
krista@2789
     7
#define PEP_ENGINE_MAJOR 1
krista@2789
     8
#define PEP_ENGINE_MINOR 0
krista@2791
     9
#define PEP_ENGINE_PATCH 441
vb@908
    10
vb@908
    11
// maximum attachment size to import as key 1MB, maximum of 20 attachments
vb@908
    12
vb@908
    13
#define MAX_KEY_SIZE (1024 * 1024)
vb@908
    14
#define MAX_KEYS_TO_IMPORT  20
vb@24
    15
krista@2612
    16
#define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
krista@2612
    17
vb@233
    18
// this is 20 trustwords with 79 chars max
vb@251
    19
#define MAX_TRUSTWORDS_SPACE (20 * 80)
vb@24
    20
vb@24
    21
// XML parameters string
vb@24
    22
#define PARMS_MAX 32768
vb@24
    23
vb@24
    24
// maximum busy wait time in ms
vb@24
    25
#define BUSY_WAIT_TIME 5000
vb@24
    26
vb@24
    27
// maximum line length for reading gpg.conf
vb@24
    28
#define MAX_LINELENGTH 1024
vb@24
    29
vb@24
    30
// default keyserver
vb@450
    31
#ifndef DEFAULT_KEYSERVER
vb@24
    32
#define DEFAULT_KEYSERVER "hkp://keys.gnupg.net"
vb@450
    33
#endif
vb@450
    34
vb@450
    35
// crashdump constants
vb@450
    36
#ifndef CRASHDUMP_DEFAULT_LINES
vb@450
    37
#define CRASHDUMP_DEFAULT_LINES 100
vb@450
    38
#endif
vb@450
    39
#define CRASHDUMP_MAX_LINES 32767
vb@24
    40
krista@2045
    41
// p≡p full string, NUL-terminated
krista@2045
    42
#ifndef PEP_SUBJ_STRING
krista@2045
    43
#define PEP_SUBJ_STRING {0x70,0xE2,0x89,0xA1,0x70,0x00}
krista@2045
    44
#define PEP_SUBJ_BYTELEN 5
krista@2045
    45
#endif
krista@2045
    46
krista@2078
    47
#ifndef PEP_SUBJ_KEY
krista@2078
    48
#define PEP_SUBJ_KEY "Subject: "
krista@2078
    49
#define PEP_SUBJ_KEY_LC "subject: "
krista@2078
    50
#define PEP_SUBJ_KEY_LEN 9
krista@2078
    51
#endif
krista@2078
    52
krista@2120
    53
#ifndef PEP_MSG_WRAP_KEY
krista@2120
    54
#define PEP_MSG_WRAP_KEY "pEp-Wrapped-Message-Info: "
krista@2120
    55
#define PEP_MSG_WRAP_KEY_LC "pep-wrapped-message-info: "
krista@2120
    56
#define PEP_MSG_WRAP_KEY_LEN 26
krista@2078
    57
#endif
krista@2078
    58
krista@2078
    59
vb@130
    60
#include "platform.h"
vb@130
    61
vb@24
    62
#ifdef WIN32
vb@24
    63
#define LOCAL_DB windoze_local_db()
vb@24
    64
#define SYSTEM_DB windoze_system_db()
vb@24
    65
#define LIBGPGME "libgpgme-11.dll"
vb@24
    66
#else // UNIX
vb@24
    67
#define _POSIX_C_SOURCE 200809L
vb@24
    68
#include <dlfcn.h>
krista@2684
    69
#ifdef NDEBUG
vb@24
    70
#define LOCAL_DB unix_local_db()
krista@2684
    71
#else
krista@2684
    72
#define LOCAL_DB unix_local_db(false)
krista@2684
    73
#endif
vb@24
    74
#ifndef SYSTEM_DB
vb@24
    75
#define SYSTEM_DB "/usr/share/pEp/system.db"
vb@24
    76
#endif
vb@24
    77
#ifndef LIBGPGME
vb@24
    78
#define LIBGPGME "libgpgme-pthread.so"
vb@24
    79
#endif
vb@24
    80
#endif
vb@24
    81
vb@24
    82
#include <locale.h>
vb@24
    83
#include <stdlib.h>
vb@24
    84
#include <string.h>
vb@24
    85
#include <assert.h>
vb@24
    86
#include <stdio.h>
edouard@1521
    87
#include <ctype.h>
krista@2176
    88
#include <math.h>
vb@24
    89
damiano@1997
    90
#ifdef SQLITE3_FROM_OS
damiano@1997
    91
#include <sqlite3.h>
damiano@1997
    92
#else
vb@24
    93
#include "sqlite3.h"
damiano@1997
    94
#endif
vb@24
    95
vb@24
    96
#include "pEpEngine.h"
Edouard@168
    97
Edouard@168
    98
// If not specified, build for GPG
Edouard@168
    99
#ifndef USE_NETPGP
Edouard@168
   100
#ifndef USE_GPG
Edouard@168
   101
#define USE_GPG
Edouard@168
   102
#endif
Edouard@168
   103
#endif
Edouard@168
   104
Edouard@168
   105
#ifdef USE_GPG
vb@24
   106
#include "pgp_gpg_internal.h"
vb@229
   107
#elif defined(USE_NETPGP)
Edouard@168
   108
#include "pgp_netpgp_internal.h"
vb@24
   109
#endif
vb@24
   110
vb@292
   111
#include "keymanagement.h"
vb@28
   112
#include "cryptotech.h"
vb@28
   113
#include "transport.h"
krista@2271
   114
#include "sync.h"
vb@28
   115
vb@125
   116
#define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
vb@24
   117
edouard@1603
   118
struct _pEpSession;
edouard@1603
   119
typedef struct _pEpSession pEpSession;
edouard@1603
   120
struct _pEpSession {
vb@24
   121
    const char *version;
Edouard@168
   122
#ifdef USE_GPG
vb@24
   123
    gpgme_ctx_t ctx;
vb@229
   124
#elif defined(USE_NETPGP)
Edouard@252
   125
    pEpNetPGPSession ctx;
vb@24
   126
#endif
vb@24
   127
vb@62
   128
    PEP_cryptotech_t *cryptotech;
vb@62
   129
    PEP_transport_t *transports;
vb@28
   130
vb@24
   131
    sqlite3 *db;
vb@24
   132
    sqlite3 *system_db;
vb@24
   133
vb@24
   134
    sqlite3_stmt *log;
vb@233
   135
    sqlite3_stmt *trustword;
krista@2789
   136
    sqlite3_stmt *get_cached_engine_version;
krista@2789
   137
    sqlite3_stmt *set_cached_engine_version;    
vb@24
   138
    sqlite3_stmt *get_identity;
krista@2461
   139
    sqlite3_stmt *get_identity_without_trust_check;
krista@2461
   140
    sqlite3_stmt *get_identities_by_address;
krista@1799
   141
    sqlite3_stmt *replace_identities_fpr;
krista@2461
   142
    sqlite3_stmt *replace_main_user_fpr;
krista@2461
   143
    sqlite3_stmt *get_main_user_fpr;
krista@2461
   144
    sqlite3_stmt *refresh_userid_default_key;
krista@2461
   145
    sqlite3_stmt *remove_fpr_as_default;
vb@24
   146
    sqlite3_stmt *set_person;
krista@2480
   147
    sqlite3_stmt *update_person;
krista@2480
   148
    sqlite3_stmt *exists_person;    
krista@2468
   149
    sqlite3_stmt *set_as_pep_user;
krista@2468
   150
    sqlite3_stmt *is_pep_user;
edouard@1234
   151
    sqlite3_stmt *set_device_group;
edouard@1235
   152
    sqlite3_stmt *get_device_group;
vb@24
   153
    sqlite3_stmt *set_pgp_keypair;
krista@2478
   154
    sqlite3_stmt *set_identity_entry;
krista@2480
   155
    sqlite3_stmt *update_identity_entry;
krista@2480
   156
    sqlite3_stmt *exists_identity_entry;        
vb@932
   157
    sqlite3_stmt *set_identity_flags;
edouard@1394
   158
    sqlite3_stmt *unset_identity_flags;
vb@24
   159
    sqlite3_stmt *set_trust;
krista@2542
   160
    sqlite3_stmt *update_trust;
krista@2542
   161
    sqlite3_stmt *update_trust_to_pep;    
krista@2480
   162
    sqlite3_stmt *exists_trust_entry;
krista@1799
   163
    sqlite3_stmt *update_trust_for_fpr;
vb@24
   164
    sqlite3_stmt *get_trust;
vb@251
   165
    sqlite3_stmt *least_trust;
krista@2593
   166
    sqlite3_stmt *mark_compromised;
Edouard@409
   167
    sqlite3_stmt *reset_trust;
vb@450
   168
    sqlite3_stmt *crashdump;
vb@458
   169
    sqlite3_stmt *languagelist;
vb@458
   170
    sqlite3_stmt *i18n_token;
krista@2461
   171
    sqlite3_stmt *replace_userid;
fdik@494
   172
fdik@494
   173
    // blacklist
fdik@494
   174
    sqlite3_stmt *blacklist_add;
fdik@494
   175
    sqlite3_stmt *blacklist_delete;
fdik@494
   176
    sqlite3_stmt *blacklist_is_listed;
fdik@494
   177
    sqlite3_stmt *blacklist_retrieve;
Edouard@584
   178
    
Edouard@584
   179
    // Own keys
Edouard@584
   180
    sqlite3_stmt *own_key_is_listed;
vb@955
   181
    sqlite3_stmt *own_identities_retrieve;
edouard@1394
   182
    sqlite3_stmt *own_keys_retrieve;
krista@2461
   183
    sqlite3_stmt *get_user_default_key;
krista@2461
   184
        
krista@2461
   185
    sqlite3_stmt *get_default_own_userid;
krista@2461
   186
krista@2461
   187
//    sqlite3_stmt *set_own_key;
vb@292
   188
vb@632
   189
    // sequence value
vb@633
   190
    sqlite3_stmt *sequence_value1;
vb@633
   191
    sqlite3_stmt *sequence_value2;
vb@1085
   192
    sqlite3_stmt *sequence_value3;
vb@632
   193
edouard@1236
   194
    // revoked keys
Edouard@693
   195
    sqlite3_stmt *set_revoked;
Edouard@693
   196
    sqlite3_stmt *get_revoked;
Edouard@693
   197
krista@2471
   198
    // mistrusted
krista@2471
   199
    sqlite3_stmt* add_mistrusted_key;
krista@2471
   200
    sqlite3_stmt* is_mistrusted_key;    
krista@2471
   201
    sqlite3_stmt* delete_mistrusted_key;
krista@2471
   202
    
krista@2461
   203
    // aliases
krista@2461
   204
    sqlite3_stmt *get_userid_alias_default;
krista@2461
   205
    sqlite3_stmt *add_userid_alias;
krista@2461
   206
Edouard@693
   207
    // callbacks
vb@292
   208
    examine_identity_t examine_identity;
vb@292
   209
    void *examine_management;
edouard@1462
   210
    void *sync_management;
vb@599
   211
    void *sync_obj;
krista@2271
   212
    messageToSend_t messageToSend;
krista@2271
   213
    notifyHandshake_t notifyHandshake;
krista@2271
   214
    inject_sync_msg_t inject_sync_msg;
krista@2271
   215
    retrieve_next_sync_msg_t retrieve_next_sync_msg;
vb@464
   216
edouard@1236
   217
    // key sync
edouard@1603
   218
    pEpSession* sync_session;
krista@2271
   219
    DeviceState_state sync_state;
edouard@1460
   220
    void* sync_state_payload;
edouard@1236
   221
    char sync_uuid[37];
edouard@1316
   222
    time_t LastCannotDecrypt;
edouard@1316
   223
    time_t LastUpdateRequest;
vb@690
   224
vb@464
   225
    // runtime config
vb@464
   226
vb@464
   227
    bool passive_mode;
vb@464
   228
    bool unencrypted_subject;
vb@1110
   229
    bool keep_sync_msg;
vb@1819
   230
    bool service_log;
krista@2129
   231
krista@2129
   232
    // mistrust undo cache
krista@2129
   233
    pEp_identity* cached_mistrusted;
Edouard@720
   234
    
roker@1722
   235
#ifdef DEBUG_ERRORSTACK
roker@1722
   236
    stringlist_t* errorstack;
roker@1722
   237
#endif
edouard@1603
   238
};
vb@48
   239
roker@1722
   240
vb@62
   241
PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
vb@62
   242
void release_transport_system(PEP_SESSION session, bool out_last);
vb@48
   243
krista@1639
   244
/* NOT to be exposed to the outside!!! */
krista@1639
   245
PEP_STATUS encrypt_only(
krista@1639
   246
        PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
krista@1639
   247
        size_t psize, char **ctext, size_t *csize
krista@1639
   248
);
krista@1639
   249
krista@2047
   250
#if defined(NDEBUG) || defined(NOLOG)
vb@216
   251
#define DEBUG_LOG(TITLE, ENTITY, DESC)
vb@216
   252
#else
huss@1571
   253
#ifdef ANDROID
huss@1571
   254
#include <android/log.h>
vb@1819
   255
#define  LOG_MORE(...)  __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s :: %s ", __VA_ARGS__);
edouard@1630
   256
#else
edouard@1630
   257
#include <stdio.h>
vb@1819
   258
#define  LOG_MORE(...)  fprintf(stderr, "pEpEngine DEBUG_LOG('%s','%s','%s','%s')\n", __VA_ARGS__);
huss@1571
   259
#endif
edouard@1630
   260
#define DEBUG_LOG(TITLE, ENTITY, DESC) {\
vb@1819
   261
    log_event(session, (TITLE), (ENTITY), (DESC), "debug " __FILE__ ":" S_LINE);\
vb@1819
   262
    LOG_MORE((TITLE), (ENTITY), (DESC), __FILE__ ":" S_LINE)\
edouard@1630
   263
}
vb@216
   264
#endif
vb@216
   265
edouard@1746
   266
typedef enum _normalize_hex_rest_t {
edouard@1746
   267
    accept_hex,
edouard@1746
   268
    ignore_hex,
edouard@1746
   269
    reject_hex
edouard@1746
   270
} normalize_hex_res_t;
edouard@1746
   271
edouard@1746
   272
static inline normalize_hex_res_t _normalize_hex(char *hex) 
edouard@1746
   273
{
edouard@1746
   274
    if (*hex >= '0' && *hex <= '9')
edouard@1746
   275
        return accept_hex;
edouard@1746
   276
edouard@1746
   277
    if (*hex >= 'A' && *hex <= 'F') {
edouard@1746
   278
        *hex += 'a' - 'A';
edouard@1746
   279
        return accept_hex;
edouard@1746
   280
    }
edouard@1746
   281
edouard@1746
   282
    if (*hex >= 'a' && *hex <= 'f') 
edouard@1746
   283
        return accept_hex;
edouard@1746
   284
edouard@1746
   285
    if (*hex == ' ') 
edouard@1746
   286
        return ignore_hex;
edouard@1746
   287
edouard@1746
   288
    return reject_hex;
edouard@1746
   289
}
edouard@1746
   290
edouard@1521
   291
// Space tolerant and case insensitive fingerprint string compare
edouard@1746
   292
static inline PEP_STATUS _compare_fprs(
edouard@1746
   293
        const char* fpra,
edouard@1746
   294
        size_t fpras,
edouard@1746
   295
        const char* fprb,
edouard@1746
   296
        size_t fprbs,
edouard@1746
   297
        int* comparison)
edouard@1746
   298
{
edouard@1746
   299
edouard@1746
   300
    size_t ai = 0;
edouard@1746
   301
    size_t bi = 0;
edouard@1746
   302
    size_t significant = 0;
edouard@1746
   303
    int _comparison = 0;
edouard@1746
   304
    const int _FULL_FINGERPRINT_LENGTH = 40;
edouard@1746
   305
   
edouard@1746
   306
    // First compare every non-ignored chars until an end is reached
edouard@1746
   307
    while(ai < fpras && bi < fprbs)
edouard@1746
   308
    {
edouard@1746
   309
        char fprac = fpra[ai];
edouard@1746
   310
        char fprbc = fprb[bi];
edouard@1746
   311
        normalize_hex_res_t fprah = _normalize_hex(&fprac);
edouard@1746
   312
        normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
edouard@1746
   313
edouard@1746
   314
        if(fprah == reject_hex || fprbh == reject_hex)
edouard@1746
   315
            return PEP_ILLEGAL_VALUE;
edouard@1746
   316
edouard@1746
   317
        if ( fprah == ignore_hex )
edouard@1746
   318
        {
edouard@1746
   319
            ai++;
edouard@1746
   320
        }
edouard@1746
   321
        else if ( fprbh == ignore_hex )
edouard@1746
   322
        {
edouard@1746
   323
            bi++;
edouard@1746
   324
        }
edouard@1746
   325
        else
edouard@1746
   326
        {
edouard@1746
   327
            if(fprac != fprbc && _comparison == 0 )
edouard@1746
   328
            {
edouard@1746
   329
                _comparison = fprac > fprbc ? 1 : -1;
edouard@1746
   330
            }
edouard@1746
   331
edouard@1746
   332
            significant++;
edouard@1746
   333
            ai++;
edouard@1746
   334
            bi++;
edouard@1746
   335
edouard@1746
   336
        } 
edouard@1746
   337
    }
edouard@1746
   338
edouard@1746
   339
    // Bail out if we didn't got enough significnt chars
edouard@1746
   340
    if (significant != _FULL_FINGERPRINT_LENGTH )
edouard@1746
   341
        return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
edouard@1746
   342
edouard@1746
   343
    // Then purge remaining chars, all must be ignored chars
edouard@1746
   344
    while ( ai < fpras )
edouard@1746
   345
    {
edouard@1746
   346
        char fprac = fpra[ai];
edouard@1746
   347
        normalize_hex_res_t fprah = _normalize_hex(&fprac);
edouard@1746
   348
        if( fprah == reject_hex )
edouard@1746
   349
            return PEP_ILLEGAL_VALUE;
edouard@1746
   350
        if ( fprah != ignore_hex )
edouard@1746
   351
            return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
edouard@1746
   352
        ai++;
edouard@1746
   353
    }
edouard@1746
   354
    while ( bi < fprbs )
edouard@1746
   355
    {
edouard@1746
   356
        char fprbc = fprb[bi];
edouard@1746
   357
        normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
edouard@1746
   358
        if( fprbh == reject_hex )
edouard@1746
   359
            return PEP_ILLEGAL_VALUE;
edouard@1746
   360
        if ( fprbh != ignore_hex )
edouard@1746
   361
            return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
edouard@1746
   362
        bi++;
edouard@1746
   363
    }
edouard@1746
   364
edouard@1746
   365
    *comparison = _comparison;
edouard@1746
   366
    return PEP_STATUS_OK;
edouard@1746
   367
}
edouard@1746
   368
edouard@1521
   369
static inline int _same_fpr(
edouard@1521
   370
        const char* fpra,
edouard@1521
   371
        size_t fpras,
edouard@1521
   372
        const char* fprb,
edouard@1521
   373
        size_t fprbs
edouard@1521
   374
    )
edouard@1521
   375
{
edouard@1746
   376
    // illegal values are ignored, and considered not same.
edouard@1746
   377
    int comparison = 1;
edouard@1746
   378
edouard@1746
   379
    _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
edouard@1746
   380
edouard@1746
   381
    return comparison == 0;
edouard@1521
   382
}
roker@1722
   383
krista@2045
   384
// size is the length of the bytestr that's coming in. This is really only intended
krista@2045
   385
// for comparing two full strings. If charstr's length is different from bytestr_size,
krista@2045
   386
// we'll return a non-zero value.
krista@2045
   387
static inline int _unsigned_signed_strcmp(const unsigned char* bytestr, const char* charstr, int bytestr_size) {
krista@2045
   388
    int charstr_len = strlen(charstr);
krista@2045
   389
    if (charstr_len != bytestr_size)
krista@2045
   390
        return -1; // we don't actually care except that it's non-zero
krista@2045
   391
    return memcmp(bytestr, charstr, bytestr_size);
krista@2045
   392
}
krista@2045
   393
krista@2045
   394
// This is just a horrible example of C type madness. UTF-8 made me do it.
krista@2045
   395
static inline char* _pep_subj_copy() {
krista@2285
   396
#ifndef WIN32
krista@2045
   397
    unsigned char pepstr[] = PEP_SUBJ_STRING;
krista@2045
   398
    void* retval = calloc(1, sizeof(unsigned char)*PEP_SUBJ_BYTELEN + 1);
krista@2045
   399
    memcpy(retval, pepstr, PEP_SUBJ_BYTELEN);
krista@2045
   400
    return (char*)retval;
krista@2285
   401
#else
krista@2285
   402
    return strdup("pEp");
krista@2285
   403
#endif
krista@2045
   404
}
krista@2045
   405
krista@2461
   406
static inline bool is_me(PEP_SESSION session, pEp_identity* test_ident) {
krista@2461
   407
    bool retval = false;
krista@2461
   408
    if (test_ident && test_ident->user_id) {
krista@2461
   409
        char* def_id = NULL;
krista@2461
   410
        get_default_own_userid(session, &def_id);
krista@2461
   411
        if (test_ident->me || 
krista@2461
   412
            (def_id && strcmp(def_id, test_ident->user_id) == 0)) {
krista@2461
   413
            retval = true;
krista@2461
   414
        }
krista@2461
   415
        free(def_id);
krista@2461
   416
    }
krista@2461
   417
    return retval;
krista@2461
   418
}
krista@2461
   419
krista@2461
   420
#ifndef EMPTYSTR
krista@2461
   421
#define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
krista@2461
   422
#endif
krista@2461
   423
krista@2549
   424
#ifndef IS_PGP_CT
krista@2549
   425
#define IS_PGP_CT(CT) (((CT) | PEP_ct_confirmed) == PEP_ct_OpenPGP)
krista@2549
   426
#endif
krista@2549
   427
krista@2461
   428
#ifndef _MIN
krista@2461
   429
#define _MIN(A, B) ((B) > (A) ? (A) : (B))
krista@2461
   430
#endif
krista@2461
   431
#ifndef _MAX
krista@2461
   432
#define _MAX(A, B) ((B) > (A) ? (B) : (A))
krista@2461
   433
#endif
krista@2461
   434
krista@2461
   435
krista@2176
   436
// These are globals used in generating message IDs and should only be
krista@2176
   437
// computed once, as they're either really constants or OS-dependent
krista@2176
   438
krista@2467
   439
extern int _pEp_rand_max_bits;
krista@2467
   440
extern double _pEp_log2_36;
krista@2176
   441
krista@2176
   442
static inline void _init_globals() {
vb@2571
   443
    _pEp_rand_max_bits = (int) ceil(log2((double) RAND_MAX));
krista@2176
   444
    _pEp_log2_36 = log2(36);
krista@2176
   445
}