src/pEp_internal.h
author Edouard Tisserant <edouard@pep-project.org>
Mon, 13 Mar 2017 15:03:14 +0100
changeset 1648 158e65c83311
parent 1630 a507a5006bea
parent 1639 be84d676f3ef
child 1673 559931685411
child 1722 1d16b6822287
child 1746 498b0671ff4e
permissions -rw-r--r--
merged
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #define PEP_ENGINE_VERSION "0.8.0"
     5 
     6 // maximum attachment size to import as key 1MB, maximum of 20 attachments
     7 
     8 #define MAX_KEY_SIZE (1024 * 1024)
     9 #define MAX_KEYS_TO_IMPORT  20
    10 
    11 // this is 20 trustwords with 79 chars max
    12 #define MAX_TRUSTWORDS_SPACE (20 * 80)
    13 
    14 // XML parameters string
    15 #define PARMS_MAX 32768
    16 
    17 // maximum busy wait time in ms
    18 #define BUSY_WAIT_TIME 5000
    19 
    20 // maximum line length for reading gpg.conf
    21 #define MAX_LINELENGTH 1024
    22 
    23 // default keyserver
    24 #ifndef DEFAULT_KEYSERVER
    25 #define DEFAULT_KEYSERVER "hkp://keys.gnupg.net"
    26 #endif
    27 
    28 // crashdump constants
    29 #ifndef CRASHDUMP_DEFAULT_LINES
    30 #define CRASHDUMP_DEFAULT_LINES 100
    31 #endif
    32 #define CRASHDUMP_MAX_LINES 32767
    33 
    34 #include "platform.h"
    35 
    36 #ifdef WIN32
    37 #define LOCAL_DB windoze_local_db()
    38 #define SYSTEM_DB windoze_system_db()
    39 #define LIBGPGME "libgpgme-11.dll"
    40 #else // UNIX
    41 #define _POSIX_C_SOURCE 200809L
    42 #include <dlfcn.h>
    43 #define LOCAL_DB unix_local_db()
    44 #ifndef SYSTEM_DB
    45 #define SYSTEM_DB "/usr/share/pEp/system.db"
    46 #endif
    47 #ifndef LIBGPGME
    48 #define LIBGPGME "libgpgme-pthread.so"
    49 #endif
    50 #endif
    51 
    52 #include <locale.h>
    53 #include <stdlib.h>
    54 #include <string.h>
    55 #include <assert.h>
    56 #include <stdio.h>
    57 #include <ctype.h>
    58 
    59 #include "sqlite3.h"
    60 
    61 #define _EXPORT_PEP_ENGINE_DLL
    62 #include "pEpEngine.h"
    63 
    64 // If not specified, build for GPG
    65 #ifndef USE_NETPGP
    66 #ifndef USE_GPG
    67 #define USE_GPG
    68 #endif
    69 #endif
    70 
    71 #ifdef USE_GPG
    72 #include "pgp_gpg_internal.h"
    73 #elif defined(USE_NETPGP)
    74 #include "pgp_netpgp_internal.h"
    75 #endif
    76 
    77 #include "keymanagement.h"
    78 #include "cryptotech.h"
    79 #include "transport.h"
    80 #include "sync.h"
    81 
    82 #define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
    83 
    84 struct _pEpSession;
    85 typedef struct _pEpSession pEpSession;
    86 struct _pEpSession {
    87     const char *version;
    88 #ifdef USE_GPG
    89     gpgme_ctx_t ctx;
    90 #elif defined(USE_NETPGP)
    91     pEpNetPGPSession ctx;
    92 #endif
    93 
    94     PEP_cryptotech_t *cryptotech;
    95     PEP_transport_t *transports;
    96 
    97     sqlite3 *db;
    98     sqlite3 *system_db;
    99 
   100     sqlite3_stmt *log;
   101     sqlite3_stmt *trustword;
   102     sqlite3_stmt *get_identity;
   103     sqlite3_stmt *set_person;
   104     sqlite3_stmt *set_device_group;
   105     sqlite3_stmt *get_device_group;
   106     sqlite3_stmt *set_pgp_keypair;
   107     sqlite3_stmt *set_identity;
   108     sqlite3_stmt *set_identity_flags;
   109     sqlite3_stmt *unset_identity_flags;
   110     sqlite3_stmt *set_trust;
   111     sqlite3_stmt *get_trust;
   112     sqlite3_stmt *least_trust;
   113     sqlite3_stmt *mark_compromized;
   114     sqlite3_stmt *reset_trust;
   115     sqlite3_stmt *crashdump;
   116     sqlite3_stmt *languagelist;
   117     sqlite3_stmt *i18n_token;
   118 
   119     // blacklist
   120     sqlite3_stmt *blacklist_add;
   121     sqlite3_stmt *blacklist_delete;
   122     sqlite3_stmt *blacklist_is_listed;
   123     sqlite3_stmt *blacklist_retrieve;
   124     
   125     // Own keys
   126     sqlite3_stmt *own_key_is_listed;
   127     sqlite3_stmt *own_identities_retrieve;
   128     sqlite3_stmt *own_keys_retrieve;
   129     sqlite3_stmt *set_own_key;
   130 
   131     // sequence value
   132     sqlite3_stmt *sequence_value1;
   133     sqlite3_stmt *sequence_value2;
   134     sqlite3_stmt *sequence_value3;
   135 
   136     // revoked keys
   137     sqlite3_stmt *set_revoked;
   138     sqlite3_stmt *get_revoked;
   139 
   140     // callbacks
   141     examine_identity_t examine_identity;
   142     void *examine_management;
   143     void *sync_management;
   144     void *sync_obj;
   145     messageToSend_t messageToSend;
   146     notifyHandshake_t notifyHandshake;
   147     inject_sync_msg_t inject_sync_msg;
   148     retrieve_next_sync_msg_t retrieve_next_sync_msg;
   149 
   150     // key sync
   151     pEpSession* sync_session;
   152     DeviceState_state sync_state;
   153     void* sync_state_payload;
   154     char sync_uuid[37];
   155     time_t LastCannotDecrypt;
   156     time_t LastUpdateRequest;
   157 
   158     // runtime config
   159 
   160     bool passive_mode;
   161     bool unencrypted_subject;
   162     bool use_only_own_private_keys;
   163     bool keep_sync_msg;
   164     
   165 };
   166 
   167 PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
   168 void release_transport_system(PEP_SESSION session, bool out_last);
   169 
   170 /* NOT to be exposed to the outside!!! */
   171 PEP_STATUS encrypt_only(
   172         PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
   173         size_t psize, char **ctext, size_t *csize
   174 );
   175 
   176 #ifdef NDEBUG
   177 #define DEBUG_LOG(TITLE, ENTITY, DESC)
   178 #else
   179 #ifdef ANDROID
   180 #include <android/log.h>
   181 #define  LOG_MORE(...)  __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s ", __VA_ARGS__);
   182 #else
   183 #include <stdio.h>
   184 #define  LOG_MORE(...)  printf("pEpEngine DEBUG_LOG('%s','%s','%s')\n", __VA_ARGS__);
   185 #endif
   186 #define DEBUG_LOG(TITLE, ENTITY, DESC) {\
   187     log_event(session, (TITLE), (ENTITY), (DESC), "debug");\
   188     LOG_MORE((TITLE), (ENTITY), (DESC))\
   189 }
   190 #endif
   191 
   192 // Space tolerant and case insensitive fingerprint string compare
   193 static inline int _same_fpr(
   194         const char* fpra,
   195         size_t fpras,
   196         const char* fprb,
   197         size_t fprbs
   198     )
   199 {
   200     size_t ai = 0;
   201     size_t bi = 0;
   202     
   203     do
   204     {
   205         if(fpra[ai] == 0 || fprb[bi] == 0)
   206         {
   207             return 0;
   208         }
   209         else if(fpra[ai] == ' ')
   210         {
   211             ai++;
   212         }
   213         else if(fprb[bi] == ' ')
   214         {
   215             bi++;
   216         }
   217         else if(toupper(fpra[ai]) == toupper(fprb[bi]))
   218         {
   219             ai++;
   220             bi++;
   221         }
   222         else
   223         {
   224             return 0;
   225         }
   226         
   227     }
   228     while(ai < fpras && bi < fprbs);
   229     
   230     return ai == fpras && bi == fprbs;
   231 }