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