src/pEp_internal.h
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 21 Jan 2020 15:11:58 +0100
branchsync
changeset 4358 0f4c24ab43b6
parent 4312 1d1672a6dfb9
child 4410 a1121e886baa
permissions -rw-r--r--
reset own keys on device that's left group
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
krista@3051
     4
#define PEP_ENGINE_VERSION "1.1.1"
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
krista@2612
    11
#define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
krista@2612
    12
vb@233
    13
// this is 20 trustwords with 79 chars max
vb@251
    14
#define MAX_TRUSTWORDS_SPACE (20 * 80)
vb@24
    15
vb@24
    16
// XML parameters string
vb@24
    17
#define PARMS_MAX 32768
vb@24
    18
vb@24
    19
// maximum busy wait time in ms
krista@3885
    20
#define BUSY_WAIT_TIME 5000
vb@24
    21
vb@24
    22
// maximum line length for reading gpg.conf
vb@24
    23
#define MAX_LINELENGTH 1024
vb@24
    24
vb@24
    25
// default keyserver
vb@450
    26
#ifndef DEFAULT_KEYSERVER
vb@4003
    27
#define DEFAULT_KEYSERVER "hkps://keys.openpgp.org"
vb@450
    28
#endif
vb@450
    29
vb@450
    30
// crashdump constants
vb@450
    31
#ifndef CRASHDUMP_DEFAULT_LINES
vb@450
    32
#define CRASHDUMP_DEFAULT_LINES 100
vb@450
    33
#endif
vb@450
    34
#define CRASHDUMP_MAX_LINES 32767
vb@24
    35
krista@2045
    36
// p≡p full string, NUL-terminated
krista@2045
    37
#ifndef PEP_SUBJ_STRING
krista@2045
    38
#define PEP_SUBJ_STRING {0x70,0xE2,0x89,0xA1,0x70,0x00}
krista@2045
    39
#define PEP_SUBJ_BYTELEN 5
krista@2045
    40
#endif
krista@2045
    41
krista@2078
    42
#ifndef PEP_SUBJ_KEY
krista@2078
    43
#define PEP_SUBJ_KEY "Subject: "
krista@2078
    44
#define PEP_SUBJ_KEY_LC "subject: "
krista@2078
    45
#define PEP_SUBJ_KEY_LEN 9
krista@2078
    46
#endif
krista@2078
    47
krista@2120
    48
#ifndef PEP_MSG_WRAP_KEY
krista@2120
    49
#define PEP_MSG_WRAP_KEY "pEp-Wrapped-Message-Info: "
krista@2120
    50
#define PEP_MSG_WRAP_KEY_LC "pep-wrapped-message-info: "
krista@2120
    51
#define PEP_MSG_WRAP_KEY_LEN 26
krista@2078
    52
#endif
krista@2078
    53
krista@3723
    54
#ifndef X_PEP_MSG_WRAP_KEY
krista@3723
    55
#define X_PEP_MSG_WRAP_KEY "X-pEp-Wrapped-Message-Info"
krista@3723
    56
#endif
krista@2078
    57
krista@3723
    58
#ifndef X_PEP_SNDR_FPR_KEY
krista@3723
    59
#define X_PEP_SNDR_FPR_KEY "X-pEp-Sender-FPR"
krista@3723
    60
#endif
krista@3723
    61
 
vb@130
    62
#include "platform.h"
vb@130
    63
vb@24
    64
#ifdef WIN32
Thomas@3835
    65
#define KEYS_DB windoze_keys_db()
vb@24
    66
#define LOCAL_DB windoze_local_db()
vb@24
    67
#define SYSTEM_DB windoze_system_db()
vb@24
    68
#define LIBGPGME "libgpgme-11.dll"
vb@24
    69
#else // UNIX
vb@24
    70
#define _POSIX_C_SOURCE 200809L
vb@24
    71
#include <dlfcn.h>
krista@2684
    72
#ifdef NDEBUG
vb@24
    73
#define LOCAL_DB unix_local_db()
krista@2684
    74
#else
krista@2684
    75
#define LOCAL_DB unix_local_db(false)
krista@2684
    76
#endif
vb@4003
    77
#ifdef ANDROID
vb@4003
    78
#define SYSTEM_DB android_system_db()
vb@4003
    79
#else
vb@4003
    80
#define SYSTEM_DB unix_system_db()
vb@24
    81
#endif
vb@24
    82
#ifndef LIBGPGME
vb@24
    83
#define LIBGPGME "libgpgme-pthread.so"
vb@24
    84
#endif
vb@24
    85
#endif
vb@24
    86
vb@24
    87
#include <locale.h>
vb@24
    88
#include <stdlib.h>
vb@24
    89
#include <string.h>
vb@24
    90
#include <assert.h>
vb@24
    91
#include <stdio.h>
edouard@1521
    92
#include <ctype.h>
krista@2176
    93
#include <math.h>
vb@24
    94
damiano@1997
    95
#ifdef SQLITE3_FROM_OS
damiano@1997
    96
#include <sqlite3.h>
damiano@1997
    97
#else
vb@24
    98
#include "sqlite3.h"
damiano@1997
    99
#endif
vb@24
   100
vb@24
   101
#include "pEpEngine.h"
Edouard@168
   102
Edouard@168
   103
// If not specified, build for GPG
neal@3191
   104
#ifndef USE_SEQUOIA
Edouard@168
   105
#ifndef USE_NETPGP
Edouard@168
   106
#ifndef USE_GPG
Edouard@168
   107
#define USE_GPG
Edouard@168
   108
#endif
Edouard@168
   109
#endif
neal@3191
   110
#endif
Edouard@168
   111
vb@3540
   112
#if defined(USE_SEQUOIA)
vb@3540
   113
#include "pgp_sequoia_internal.h"
vb@229
   114
#elif defined(USE_NETPGP)
Edouard@168
   115
#include "pgp_netpgp_internal.h"
vb@3540
   116
#elif defined(USE_GPG)
vb@3540
   117
#include "pgp_gpg_internal.h"
vb@24
   118
#endif
vb@24
   119
vb@292
   120
#include "keymanagement.h"
vb@28
   121
#include "cryptotech.h"
vb@28
   122
#include "transport.h"
vb@2847
   123
#include "sync_api.h"
vb@2830
   124
#include "Sync_func.h"
krista@2787
   125
krista@2947
   126
#include "key_reset.h"
vb@28
   127
vb@125
   128
#define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
vb@24
   129
edouard@1603
   130
struct _pEpSession;
edouard@1603
   131
typedef struct _pEpSession pEpSession;
edouard@1603
   132
struct _pEpSession {
vb@24
   133
    const char *version;
vb@2833
   134
    messageToSend_t messageToSend;
vb@2833
   135
Edouard@168
   136
#ifdef USE_GPG
vb@24
   137
    gpgme_ctx_t ctx;
vb@229
   138
#elif defined(USE_NETPGP)
Edouard@252
   139
    pEpNetPGPSession ctx;
neal@3191
   140
#elif defined(USE_SEQUOIA)
neal@3191
   141
    sqlite3 *key_db;
neal@3212
   142
    struct {
neal@3212
   143
        sqlite3_stmt *begin_transaction;
neal@3212
   144
        sqlite3_stmt *commit_transaction;
neal@3212
   145
        sqlite3_stmt *rollback_transaction;
neal@4235
   146
        sqlite3_stmt *cert_find;
neal@3212
   147
        sqlite3_stmt *tsk_find;
neal@4235
   148
        sqlite3_stmt *cert_find_by_keyid;
neal@3212
   149
        sqlite3_stmt *tsk_find_by_keyid;
neal@4235
   150
        sqlite3_stmt *cert_find_by_email;
neal@3212
   151
        sqlite3_stmt *tsk_find_by_email;
neal@4235
   152
        sqlite3_stmt *cert_all;
neal@3212
   153
        sqlite3_stmt *tsk_all;
neal@4235
   154
        sqlite3_stmt *cert_save_insert_primary;
neal@4235
   155
        sqlite3_stmt *cert_save_insert_subkeys;
neal@4235
   156
        sqlite3_stmt *cert_save_insert_userids;
neal@3797
   157
        sqlite3_stmt *delete_keypair;
neal@3212
   158
    } sq_sql;
vb@24
   159
#endif
vb@24
   160
vb@62
   161
    PEP_cryptotech_t *cryptotech;
vb@3699
   162
    PEP_CIPHER_SUITE cipher_suite;
vb@3689
   163
vb@62
   164
    PEP_transport_t *transports;
vb@28
   165
vb@24
   166
    sqlite3 *db;
vb@24
   167
    sqlite3 *system_db;
vb@24
   168
vb@24
   169
    sqlite3_stmt *log;
vb@233
   170
    sqlite3_stmt *trustword;
vb@24
   171
    sqlite3_stmt *get_identity;
krista@2461
   172
    sqlite3_stmt *get_identity_without_trust_check;
krista@2461
   173
    sqlite3_stmt *get_identities_by_address;
krista@2966
   174
    sqlite3_stmt *get_identities_by_userid;
krista@2893
   175
    sqlite3_stmt *get_identities_by_main_key_id;
krista@1799
   176
    sqlite3_stmt *replace_identities_fpr;
krista@2461
   177
    sqlite3_stmt *replace_main_user_fpr;
krista@2461
   178
    sqlite3_stmt *get_main_user_fpr;
krista@2461
   179
    sqlite3_stmt *refresh_userid_default_key;
krista@2948
   180
    sqlite3_stmt *delete_key;
krista@2461
   181
    sqlite3_stmt *remove_fpr_as_default;
vb@24
   182
    sqlite3_stmt *set_person;
krista@2480
   183
    sqlite3_stmt *update_person;
krista@2965
   184
    sqlite3_stmt *delete_person;
krista@2480
   185
    sqlite3_stmt *exists_person;    
vb@2833
   186
    sqlite3_stmt *set_as_pEp_user;
vb@2833
   187
    sqlite3_stmt *is_pEp_user;
krista@3730
   188
    sqlite3_stmt *upgrade_pEp_version_by_user_id;
krista@2747
   189
    sqlite3_stmt *add_into_social_graph;
krista@2752
   190
    sqlite3_stmt *get_own_address_binding_from_contact;
krista@2752
   191
    sqlite3_stmt *set_revoke_contact_as_notified;
krista@2752
   192
    sqlite3_stmt *get_contacted_ids_from_revoke_fpr;
krista@2752
   193
    sqlite3_stmt *was_id_for_revoke_contacted;
krista@4312
   194
    sqlite3_stmt *has_id_contacted_address;
krista@2800
   195
    sqlite3_stmt *get_last_contacted;
krista@3420
   196
    // sqlite3_stmt *set_device_group;
krista@3420
   197
    // sqlite3_stmt *get_device_group;
vb@24
   198
    sqlite3_stmt *set_pgp_keypair;
krista@2478
   199
    sqlite3_stmt *set_identity_entry;
krista@2480
   200
    sqlite3_stmt *update_identity_entry;
krista@2480
   201
    sqlite3_stmt *exists_identity_entry;        
vb@932
   202
    sqlite3_stmt *set_identity_flags;
edouard@1394
   203
    sqlite3_stmt *unset_identity_flags;
krista@4121
   204
    sqlite3_stmt *set_pEp_version; 
krista@4121
   205
    sqlite3_stmt *clear_trust_info;   
vb@24
   206
    sqlite3_stmt *set_trust;
krista@2542
   207
    sqlite3_stmt *update_trust;
krista@2480
   208
    sqlite3_stmt *exists_trust_entry;
vb@2834
   209
    sqlite3_stmt *update_trust_to_pEp;
krista@1799
   210
    sqlite3_stmt *update_trust_for_fpr;
vb@24
   211
    sqlite3_stmt *get_trust;
krista@2967
   212
    sqlite3_stmt *get_trust_by_userid;
vb@251
   213
    sqlite3_stmt *least_trust;
krista@2593
   214
    sqlite3_stmt *mark_compromised;
Edouard@409
   215
    sqlite3_stmt *reset_trust;
vb@450
   216
    sqlite3_stmt *crashdump;
vb@458
   217
    sqlite3_stmt *languagelist;
vb@458
   218
    sqlite3_stmt *i18n_token;
krista@2461
   219
    sqlite3_stmt *replace_userid;
fdik@494
   220
fdik@494
   221
    // blacklist
fdik@494
   222
    sqlite3_stmt *blacklist_add;
fdik@494
   223
    sqlite3_stmt *blacklist_delete;
fdik@494
   224
    sqlite3_stmt *blacklist_is_listed;
fdik@494
   225
    sqlite3_stmt *blacklist_retrieve;
Edouard@584
   226
    
krista@2893
   227
    // Keys
Edouard@584
   228
    sqlite3_stmt *own_key_is_listed;
krista@3346
   229
    sqlite3_stmt *is_own_address;
vb@955
   230
    sqlite3_stmt *own_identities_retrieve;
edouard@1394
   231
    sqlite3_stmt *own_keys_retrieve;
krista@4245
   232
    sqlite3_stmt *key_identities_retrieve;
krista@2461
   233
    sqlite3_stmt *get_user_default_key;
krista@2893
   234
    sqlite3_stmt *get_all_keys_for_user;
krista@2461
   235
        
krista@2461
   236
    sqlite3_stmt *get_default_own_userid;
krista@2461
   237
krista@2893
   238
krista@2461
   239
//    sqlite3_stmt *set_own_key;
vb@292
   240
vb@632
   241
    // sequence value
vb@633
   242
    sqlite3_stmt *sequence_value1;
vb@633
   243
    sqlite3_stmt *sequence_value2;
vb@632
   244
edouard@1236
   245
    // revoked keys
Edouard@693
   246
    sqlite3_stmt *set_revoked;
Edouard@693
   247
    sqlite3_stmt *get_revoked;
krista@2752
   248
    sqlite3_stmt *get_replacement_fpr;
Edouard@693
   249
krista@2471
   250
    // mistrusted
krista@2471
   251
    sqlite3_stmt* add_mistrusted_key;
krista@2471
   252
    sqlite3_stmt* is_mistrusted_key;    
krista@2471
   253
    sqlite3_stmt* delete_mistrusted_key;
krista@2471
   254
    
krista@2461
   255
    // aliases
krista@2461
   256
    sqlite3_stmt *get_userid_alias_default;
krista@2461
   257
    sqlite3_stmt *add_userid_alias;
krista@2461
   258
Edouard@693
   259
    // callbacks
vb@292
   260
    examine_identity_t examine_identity;
vb@292
   261
    void *examine_management;
krista@2271
   262
    notifyHandshake_t notifyHandshake;
vb@2839
   263
    inject_sync_event_t inject_sync_event;
vb@2839
   264
    retrieve_next_sync_event_t retrieve_next_sync_event;
vb@464
   265
vb@2830
   266
    // pEp Sync
vb@3375
   267
    void *sync_management;
vb@3375
   268
    void *sync_obj;
vb@2830
   269
    struct Sync_state_s sync_state;
vb@2830
   270
vb@2830
   271
//     void* sync_state_payload;
vb@2830
   272
//     char sync_uuid[37];
vb@2830
   273
//     time_t LastCannotDecrypt;
vb@2830
   274
//     time_t LastUpdateRequest;
vb@690
   275
vb@464
   276
    // runtime config
vb@464
   277
vb@464
   278
    bool passive_mode;
vb@464
   279
    bool unencrypted_subject;
vb@1819
   280
    bool service_log;
Edouard@720
   281
    
vb@4134
   282
#ifndef NDEBUG
vb@4134
   283
#   ifdef DEBUG_ERRORSTACK
roker@1722
   284
    stringlist_t* errorstack;
vb@4134
   285
#   endif
vb@4134
   286
    int debug_color;
roker@1722
   287
#endif
edouard@1603
   288
};
vb@48
   289
roker@1722
   290
vb@62
   291
PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
vb@62
   292
void release_transport_system(PEP_SESSION session, bool out_last);
vb@48
   293
krista@1639
   294
/* NOT to be exposed to the outside!!! */
krista@1639
   295
PEP_STATUS encrypt_only(
krista@1639
   296
        PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
krista@1639
   297
        size_t psize, char **ctext, size_t *csize
krista@1639
   298
);
krista@1639
   299
krista@3963
   300
void decorate_message(
krista@3963
   301
    message *msg,
krista@3963
   302
    PEP_rating rating,
krista@3963
   303
    stringlist_t *keylist,
krista@3963
   304
    bool add_version,
krista@3963
   305
    bool clobber);
krista@3963
   306
krista@2047
   307
#if defined(NDEBUG) || defined(NOLOG)
vb@216
   308
#define DEBUG_LOG(TITLE, ENTITY, DESC)
vb@216
   309
#else
huss@1571
   310
#ifdef ANDROID
huss@1571
   311
#include <android/log.h>
vb@1819
   312
#define  LOG_MORE(...)  __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s :: %s ", __VA_ARGS__);
edouard@1630
   313
#else
edouard@1630
   314
#include <stdio.h>
vb@1819
   315
#define  LOG_MORE(...)  fprintf(stderr, "pEpEngine DEBUG_LOG('%s','%s','%s','%s')\n", __VA_ARGS__);
huss@1571
   316
#endif
edouard@1630
   317
#define DEBUG_LOG(TITLE, ENTITY, DESC) {\
vb@1819
   318
    log_event(session, (TITLE), (ENTITY), (DESC), "debug " __FILE__ ":" S_LINE);\
vb@1819
   319
    LOG_MORE((TITLE), (ENTITY), (DESC), __FILE__ ":" S_LINE)\
edouard@1630
   320
}
vb@216
   321
#endif
vb@216
   322
edouard@1746
   323
typedef enum _normalize_hex_rest_t {
edouard@1746
   324
    accept_hex,
edouard@1746
   325
    ignore_hex,
edouard@1746
   326
    reject_hex
edouard@1746
   327
} normalize_hex_res_t;
edouard@1746
   328
edouard@1746
   329
static inline normalize_hex_res_t _normalize_hex(char *hex) 
edouard@1746
   330
{
edouard@1746
   331
    if (*hex >= '0' && *hex <= '9')
edouard@1746
   332
        return accept_hex;
edouard@1746
   333
edouard@1746
   334
    if (*hex >= 'A' && *hex <= 'F') {
edouard@1746
   335
        *hex += 'a' - 'A';
edouard@1746
   336
        return accept_hex;
edouard@1746
   337
    }
edouard@1746
   338
edouard@1746
   339
    if (*hex >= 'a' && *hex <= 'f') 
edouard@1746
   340
        return accept_hex;
edouard@1746
   341
edouard@1746
   342
    if (*hex == ' ') 
edouard@1746
   343
        return ignore_hex;
edouard@1746
   344
edouard@1746
   345
    return reject_hex;
edouard@1746
   346
}
edouard@1746
   347
edouard@1521
   348
// Space tolerant and case insensitive fingerprint string compare
edouard@1746
   349
static inline PEP_STATUS _compare_fprs(
edouard@1746
   350
        const char* fpra,
edouard@1746
   351
        size_t fpras,
edouard@1746
   352
        const char* fprb,
edouard@1746
   353
        size_t fprbs,
edouard@1746
   354
        int* comparison)
edouard@1746
   355
{
edouard@1746
   356
edouard@1746
   357
    size_t ai = 0;
edouard@1746
   358
    size_t bi = 0;
edouard@1746
   359
    size_t significant = 0;
edouard@1746
   360
    int _comparison = 0;
edouard@1746
   361
    const int _FULL_FINGERPRINT_LENGTH = 40;
edouard@1746
   362
   
edouard@1746
   363
    // First compare every non-ignored chars until an end is reached
edouard@1746
   364
    while(ai < fpras && bi < fprbs)
edouard@1746
   365
    {
edouard@1746
   366
        char fprac = fpra[ai];
edouard@1746
   367
        char fprbc = fprb[bi];
edouard@1746
   368
        normalize_hex_res_t fprah = _normalize_hex(&fprac);
edouard@1746
   369
        normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
edouard@1746
   370
edouard@1746
   371
        if(fprah == reject_hex || fprbh == reject_hex)
edouard@1746
   372
            return PEP_ILLEGAL_VALUE;
edouard@1746
   373
edouard@1746
   374
        if ( fprah == ignore_hex )
edouard@1746
   375
        {
edouard@1746
   376
            ai++;
edouard@1746
   377
        }
edouard@1746
   378
        else if ( fprbh == ignore_hex )
edouard@1746
   379
        {
edouard@1746
   380
            bi++;
edouard@1746
   381
        }
edouard@1746
   382
        else
edouard@1746
   383
        {
edouard@1746
   384
            if(fprac != fprbc && _comparison == 0 )
edouard@1746
   385
            {
edouard@1746
   386
                _comparison = fprac > fprbc ? 1 : -1;
edouard@1746
   387
            }
edouard@1746
   388
edouard@1746
   389
            significant++;
edouard@1746
   390
            ai++;
edouard@1746
   391
            bi++;
edouard@1746
   392
edouard@1746
   393
        } 
edouard@1746
   394
    }
edouard@1746
   395
edouard@1746
   396
    // Bail out if we didn't got enough significnt chars
edouard@1746
   397
    if (significant != _FULL_FINGERPRINT_LENGTH )
edouard@1746
   398
        return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
edouard@1746
   399
edouard@1746
   400
    // Then purge remaining chars, all must be ignored chars
edouard@1746
   401
    while ( ai < fpras )
edouard@1746
   402
    {
edouard@1746
   403
        char fprac = fpra[ai];
edouard@1746
   404
        normalize_hex_res_t fprah = _normalize_hex(&fprac);
edouard@1746
   405
        if( fprah == reject_hex )
edouard@1746
   406
            return PEP_ILLEGAL_VALUE;
edouard@1746
   407
        if ( fprah != ignore_hex )
edouard@1746
   408
            return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
edouard@1746
   409
        ai++;
edouard@1746
   410
    }
edouard@1746
   411
    while ( bi < fprbs )
edouard@1746
   412
    {
edouard@1746
   413
        char fprbc = fprb[bi];
edouard@1746
   414
        normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
edouard@1746
   415
        if( fprbh == reject_hex )
edouard@1746
   416
            return PEP_ILLEGAL_VALUE;
edouard@1746
   417
        if ( fprbh != ignore_hex )
edouard@1746
   418
            return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
edouard@1746
   419
        bi++;
edouard@1746
   420
    }
edouard@1746
   421
edouard@1746
   422
    *comparison = _comparison;
edouard@1746
   423
    return PEP_STATUS_OK;
edouard@1746
   424
}
edouard@1746
   425
edouard@1521
   426
static inline int _same_fpr(
edouard@1521
   427
        const char* fpra,
edouard@1521
   428
        size_t fpras,
edouard@1521
   429
        const char* fprb,
edouard@1521
   430
        size_t fprbs
edouard@1521
   431
    )
edouard@1521
   432
{
edouard@1746
   433
    // illegal values are ignored, and considered not same.
edouard@1746
   434
    int comparison = 1;
edouard@1746
   435
edouard@1746
   436
    _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
edouard@1746
   437
edouard@1746
   438
    return comparison == 0;
edouard@1521
   439
}
roker@1722
   440
krista@2045
   441
// size is the length of the bytestr that's coming in. This is really only intended
krista@2045
   442
// for comparing two full strings. If charstr's length is different from bytestr_size,
krista@2045
   443
// we'll return a non-zero value.
krista@2045
   444
static inline int _unsigned_signed_strcmp(const unsigned char* bytestr, const char* charstr, int bytestr_size) {
krista@2045
   445
    int charstr_len = strlen(charstr);
krista@2045
   446
    if (charstr_len != bytestr_size)
krista@2045
   447
        return -1; // we don't actually care except that it's non-zero
krista@2045
   448
    return memcmp(bytestr, charstr, bytestr_size);
krista@2045
   449
}
krista@2045
   450
krista@2045
   451
// This is just a horrible example of C type madness. UTF-8 made me do it.
vb@2834
   452
static inline char* _pEp_subj_copy() {
krista@2285
   453
#ifndef WIN32
vb@2834
   454
    unsigned char pEpstr[] = PEP_SUBJ_STRING;
krista@2045
   455
    void* retval = calloc(1, sizeof(unsigned char)*PEP_SUBJ_BYTELEN + 1);
vb@2834
   456
    memcpy(retval, pEpstr, PEP_SUBJ_BYTELEN);
krista@2045
   457
    return (char*)retval;
krista@2285
   458
#else
krista@2285
   459
    return strdup("pEp");
krista@2285
   460
#endif
krista@2045
   461
}
krista@2045
   462
krista@2461
   463
static inline bool is_me(PEP_SESSION session, pEp_identity* test_ident) {
krista@2461
   464
    bool retval = false;
krista@2461
   465
    if (test_ident && test_ident->user_id) {
krista@2461
   466
        char* def_id = NULL;
krista@2461
   467
        get_default_own_userid(session, &def_id);
krista@2461
   468
        if (test_ident->me || 
krista@2461
   469
            (def_id && strcmp(def_id, test_ident->user_id) == 0)) {
krista@2461
   470
            retval = true;
krista@2461
   471
        }
krista@2461
   472
        free(def_id);
krista@2461
   473
    }
krista@2461
   474
    return retval;
krista@2461
   475
}
krista@2461
   476
krista@3730
   477
static inline float pEp_version_numeric(const char* version_str) {
krista@3730
   478
    float retval = 0;    
krista@3730
   479
        
krista@3730
   480
    if (!version_str || sscanf(version_str, "%f", &retval) != 1)
krista@3730
   481
        return 0;
krista@3730
   482
        
krista@3730
   483
    return retval;    
krista@3730
   484
}
krista@3730
   485
krista@3730
   486
static inline void pEp_version_major_minor(const char* version_str, unsigned int* major, unsigned int* minor) {
krista@3730
   487
    if (!major || !minor)
krista@3730
   488
        return;
krista@3730
   489
                
krista@3730
   490
    if (!version_str || sscanf(version_str, "%u.%u", major, minor) != 2) {
krista@3730
   491
        *major = 0;
krista@3730
   492
        *minor = 0;
krista@3730
   493
    }
krista@3730
   494
        
krista@3730
   495
    return;    
krista@3730
   496
}
krista@3730
   497
krista@3730
   498
static inline int compare_versions(unsigned int first_maj, unsigned int first_min,
krista@3730
   499
                                   unsigned int second_maj, unsigned int second_min) {
krista@3730
   500
    if (first_maj > second_maj)
krista@3730
   501
        return 1;
krista@3730
   502
    if (first_maj < second_maj)
krista@3730
   503
        return -1;
krista@3730
   504
    if (first_min > second_min)
krista@3730
   505
        return 1;
krista@3730
   506
    if (first_min < second_min)
krista@3730
   507
        return -1;
krista@3730
   508
    return 0;    
krista@3730
   509
}
krista@3730
   510
krista@3730
   511
static inline void set_min_version(unsigned int first_maj, unsigned int first_minor,
krista@3730
   512
                                   unsigned int second_maj, unsigned int second_minor,
krista@3730
   513
                                   unsigned int* result_maj, unsigned int* result_minor) {
krista@3730
   514
    int result = compare_versions(first_maj, first_minor, second_maj, second_minor);
krista@3730
   515
    if (result < 0) {
krista@3730
   516
        *result_maj = first_maj;
krista@3730
   517
        *result_minor = first_minor;
krista@3730
   518
    }
krista@3730
   519
    else {
krista@3730
   520
        *result_maj = second_maj;
krista@3730
   521
        *result_minor = second_minor;
krista@3730
   522
    }    
krista@3730
   523
}
krista@3730
   524
krista@3730
   525
static inline void set_max_version(unsigned int first_maj, unsigned int first_minor,
krista@3730
   526
                                   unsigned int second_maj, unsigned int second_minor,
krista@3730
   527
                                   unsigned int* result_maj, unsigned int* result_minor) {
krista@3730
   528
    int result = compare_versions(first_maj, first_minor, second_maj, second_minor);
krista@3730
   529
    if (result > 0) {
krista@3730
   530
        *result_maj = first_maj;
krista@3730
   531
        *result_minor = first_minor;
krista@3730
   532
    }
krista@3730
   533
    else {
krista@3730
   534
        *result_maj = second_maj;
krista@3730
   535
        *result_minor = second_minor;
krista@3730
   536
    }    
krista@3730
   537
}
krista@3730
   538
krista@2461
   539
#ifndef EMPTYSTR
krista@2461
   540
#define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
krista@2461
   541
#endif
krista@2461
   542
krista@2549
   543
#ifndef IS_PGP_CT
krista@2549
   544
#define IS_PGP_CT(CT) (((CT) | PEP_ct_confirmed) == PEP_ct_OpenPGP)
krista@2549
   545
#endif
krista@2549
   546
krista@2461
   547
#ifndef _MIN
krista@2461
   548
#define _MIN(A, B) ((B) > (A) ? (A) : (B))
krista@2461
   549
#endif
krista@2461
   550
#ifndef _MAX
krista@2461
   551
#define _MAX(A, B) ((B) > (A) ? (B) : (A))
krista@2461
   552
#endif
krista@2461
   553
krista@2176
   554
// These are globals used in generating message IDs and should only be
krista@2176
   555
// computed once, as they're either really constants or OS-dependent
krista@2176
   556
krista@2467
   557
extern int _pEp_rand_max_bits;
krista@2467
   558
extern double _pEp_log2_36;
krista@2176
   559
krista@2176
   560
static inline void _init_globals() {
vb@2571
   561
    _pEp_rand_max_bits = (int) ceil(log2((double) RAND_MAX));
krista@2176
   562
    _pEp_log2_36 = log2(36);
krista@2176
   563
}
vb@3739
   564
vb@3739
   565
// spinlock implementation
vb@3739
   566
vb@3739
   567
static inline int Sqlite3_step(sqlite3_stmt* stmt)
vb@3739
   568
{
vb@3739
   569
    int rc;
vb@3739
   570
    do {
vb@3739
   571
        rc = sqlite3_step(stmt);
vb@3739
   572
    } while (rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
vb@3739
   573
    return rc;
vb@3739
   574
}