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