src/pEp_internal.h
author Edouard Tisserant <edouard@pep-project.org>
Thu, 05 Jan 2017 13:07:31 +0100
changeset 1521 48efea61cfcd
parent 1517 f29830f1c407
child 1571 e22a159b12e3
permissions -rw-r--r--
SYNC added fpr/address spoofing protection
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
vb@130
    34
#include "platform.h"
vb@130
    35
vb@24
    36
#ifdef WIN32
vb@24
    37
#define LOCAL_DB windoze_local_db()
vb@24
    38
#define SYSTEM_DB windoze_system_db()
vb@24
    39
#define LIBGPGME "libgpgme-11.dll"
vb@24
    40
#else // UNIX
vb@24
    41
#define _POSIX_C_SOURCE 200809L
vb@24
    42
#include <dlfcn.h>
vb@24
    43
#define LOCAL_DB unix_local_db()
vb@24
    44
#ifndef SYSTEM_DB
vb@24
    45
#define SYSTEM_DB "/usr/share/pEp/system.db"
vb@24
    46
#endif
vb@24
    47
#ifndef LIBGPGME
vb@24
    48
#define LIBGPGME "libgpgme-pthread.so"
vb@24
    49
#endif
vb@24
    50
#endif
vb@24
    51
vb@24
    52
#include <locale.h>
vb@24
    53
#include <stdlib.h>
vb@24
    54
#include <string.h>
vb@24
    55
#include <assert.h>
vb@24
    56
#include <stdio.h>
edouard@1521
    57
#include <ctype.h>
vb@24
    58
vb@24
    59
#include "sqlite3.h"
vb@24
    60
vb@24
    61
#define _EXPORT_PEP_ENGINE_DLL
vb@24
    62
#include "pEpEngine.h"
Edouard@168
    63
Edouard@168
    64
// If not specified, build for GPG
Edouard@168
    65
#ifndef USE_NETPGP
Edouard@168
    66
#ifndef USE_GPG
Edouard@168
    67
#define USE_GPG
Edouard@168
    68
#endif
Edouard@168
    69
#endif
Edouard@168
    70
Edouard@168
    71
#ifdef USE_GPG
vb@24
    72
#include "pgp_gpg_internal.h"
vb@229
    73
#elif defined(USE_NETPGP)
Edouard@168
    74
#include "pgp_netpgp_internal.h"
vb@24
    75
#endif
vb@24
    76
vb@292
    77
#include "keymanagement.h"
vb@28
    78
#include "cryptotech.h"
vb@28
    79
#include "transport.h"
vb@604
    80
#include "sync.h"
vb@28
    81
vb@125
    82
#define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
vb@24
    83
vb@46
    84
typedef struct _pEpSession {
vb@24
    85
    const char *version;
Edouard@168
    86
#ifdef USE_GPG
vb@24
    87
    gpgme_ctx_t ctx;
vb@229
    88
#elif defined(USE_NETPGP)
Edouard@252
    89
    pEpNetPGPSession ctx;
vb@24
    90
#endif
vb@24
    91
vb@62
    92
    PEP_cryptotech_t *cryptotech;
vb@62
    93
    PEP_transport_t *transports;
vb@28
    94
vb@24
    95
    sqlite3 *db;
vb@24
    96
    sqlite3 *system_db;
vb@24
    97
vb@24
    98
    sqlite3_stmt *log;
vb@233
    99
    sqlite3_stmt *trustword;
vb@24
   100
    sqlite3_stmt *get_identity;
vb@24
   101
    sqlite3_stmt *set_person;
edouard@1234
   102
    sqlite3_stmt *set_device_group;
edouard@1235
   103
    sqlite3_stmt *get_device_group;
vb@24
   104
    sqlite3_stmt *set_pgp_keypair;
vb@24
   105
    sqlite3_stmt *set_identity;
vb@932
   106
    sqlite3_stmt *set_identity_flags;
edouard@1394
   107
    sqlite3_stmt *unset_identity_flags;
vb@24
   108
    sqlite3_stmt *set_trust;
vb@24
   109
    sqlite3_stmt *get_trust;
vb@251
   110
    sqlite3_stmt *least_trust;
vb@357
   111
    sqlite3_stmt *mark_compromized;
Edouard@409
   112
    sqlite3_stmt *reset_trust;
vb@450
   113
    sqlite3_stmt *crashdump;
vb@458
   114
    sqlite3_stmt *languagelist;
vb@458
   115
    sqlite3_stmt *i18n_token;
fdik@494
   116
fdik@494
   117
    // blacklist
fdik@494
   118
    sqlite3_stmt *blacklist_add;
fdik@494
   119
    sqlite3_stmt *blacklist_delete;
fdik@494
   120
    sqlite3_stmt *blacklist_is_listed;
fdik@494
   121
    sqlite3_stmt *blacklist_retrieve;
Edouard@584
   122
    
Edouard@584
   123
    // Own keys
Edouard@584
   124
    sqlite3_stmt *own_key_is_listed;
vb@955
   125
    sqlite3_stmt *own_identities_retrieve;
edouard@1394
   126
    sqlite3_stmt *own_keys_retrieve;
edouard@1394
   127
    sqlite3_stmt *set_own_key;
vb@292
   128
vb@632
   129
    // sequence value
vb@633
   130
    sqlite3_stmt *sequence_value1;
vb@633
   131
    sqlite3_stmt *sequence_value2;
vb@1085
   132
    sqlite3_stmt *sequence_value3;
vb@632
   133
edouard@1236
   134
    // revoked keys
Edouard@693
   135
    sqlite3_stmt *set_revoked;
Edouard@693
   136
    sqlite3_stmt *get_revoked;
Edouard@693
   137
Edouard@693
   138
    // callbacks
vb@292
   139
    examine_identity_t examine_identity;
vb@292
   140
    void *examine_management;
edouard@1462
   141
    void *sync_management;
vb@599
   142
    void *sync_obj;
vb@604
   143
    messageToSend_t messageToSend;
edouard@1459
   144
    notifyHandshake_t notifyHandshake;
vb@1043
   145
    inject_sync_msg_t inject_sync_msg;
vb@1043
   146
    retrieve_next_sync_msg_t retrieve_next_sync_msg;
vb@464
   147
edouard@1236
   148
    // key sync
vb@690
   149
    DeviceState_state sync_state;
edouard@1460
   150
    void* sync_state_payload;
edouard@1236
   151
    char sync_uuid[37];
edouard@1316
   152
    time_t LastCannotDecrypt;
edouard@1316
   153
    time_t LastUpdateRequest;
vb@690
   154
vb@464
   155
    // runtime config
vb@464
   156
vb@464
   157
    bool passive_mode;
vb@464
   158
    bool unencrypted_subject;
Edouard@720
   159
    bool use_only_own_private_keys;
vb@1110
   160
    bool keep_sync_msg;
Edouard@720
   161
    
vb@24
   162
} pEpSession;
vb@48
   163
vb@62
   164
PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
vb@62
   165
void release_transport_system(PEP_SESSION session, bool out_last);
vb@48
   166
vb@216
   167
#ifdef NDEBUG
vb@216
   168
#define DEBUG_LOG(TITLE, ENTITY, DESC)
vb@216
   169
#else
vb@216
   170
#define DEBUG_LOG(TITLE, ENTITY, DESC) \
vb@216
   171
    log_event(session, (TITLE), (ENTITY), (DESC), "debug");
vb@216
   172
#endif
vb@216
   173
edouard@1521
   174
// Space tolerant and case insensitive fingerprint string compare
edouard@1521
   175
static inline int _same_fpr(
edouard@1521
   176
        const char* fpra,
edouard@1521
   177
        size_t fpras,
edouard@1521
   178
        const char* fprb,
edouard@1521
   179
        size_t fprbs
edouard@1521
   180
    )
edouard@1521
   181
{
edouard@1521
   182
    size_t ai = 0;
edouard@1521
   183
    size_t bi = 0;
edouard@1521
   184
    
edouard@1521
   185
    do
edouard@1521
   186
    {
edouard@1521
   187
        if(fpra[ai] == 0 || fprb[bi] == 0)
edouard@1521
   188
        {
edouard@1521
   189
            return 0;
edouard@1521
   190
        }
edouard@1521
   191
        else if(fpra[ai] == ' ')
edouard@1521
   192
        {
edouard@1521
   193
            ai++;
edouard@1521
   194
        }
edouard@1521
   195
        else if(fprb[bi] == ' ')
edouard@1521
   196
        {
edouard@1521
   197
            bi++;
edouard@1521
   198
        }
edouard@1521
   199
        else if(toupper(fpra[ai]) == toupper(fprb[bi]))
edouard@1521
   200
        {
edouard@1521
   201
            ai++;
edouard@1521
   202
            bi++;
edouard@1521
   203
        }
edouard@1521
   204
        else
edouard@1521
   205
        {
edouard@1521
   206
            return 0;
edouard@1521
   207
        }
edouard@1521
   208
        
edouard@1521
   209
    }
edouard@1521
   210
    while(ai < fpras && bi < fprbs);
edouard@1521
   211
    
edouard@1521
   212
    return ai == fpras && bi == fprbs;
edouard@1521
   213
}