src/pEp_internal.h
author Roker <roker@pep-project.org>
Tue, 13 Jun 2017 21:15:16 +0200
changeset 1853 a86d6290d493
parent 1819 b3e57d741b15
child 1860 1975a3b9bc37
child 1945 9503c962f746
permissions -rw-r--r--
rename macro ERROR(x) into ADD_TO_LOG(x) to avoid problems on MSVC
     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 *replace_identities_fpr;
   104     sqlite3_stmt *set_person;
   105     sqlite3_stmt *set_device_group;
   106     sqlite3_stmt *get_device_group;
   107     sqlite3_stmt *set_pgp_keypair;
   108     sqlite3_stmt *set_identity;
   109     sqlite3_stmt *set_identity_flags;
   110     sqlite3_stmt *unset_identity_flags;
   111     sqlite3_stmt *set_trust;
   112     sqlite3_stmt *update_trust_for_fpr;
   113     sqlite3_stmt *get_trust;
   114     sqlite3_stmt *least_trust;
   115     sqlite3_stmt *mark_compromized;
   116     sqlite3_stmt *reset_trust;
   117     sqlite3_stmt *crashdump;
   118     sqlite3_stmt *languagelist;
   119     sqlite3_stmt *i18n_token;
   120 
   121     // blacklist
   122     sqlite3_stmt *blacklist_add;
   123     sqlite3_stmt *blacklist_delete;
   124     sqlite3_stmt *blacklist_is_listed;
   125     sqlite3_stmt *blacklist_retrieve;
   126     
   127     // Own keys
   128     sqlite3_stmt *own_key_is_listed;
   129     sqlite3_stmt *own_identities_retrieve;
   130     sqlite3_stmt *own_keys_retrieve;
   131     sqlite3_stmt *set_own_key;
   132 
   133     // sequence value
   134     sqlite3_stmt *sequence_value1;
   135     sqlite3_stmt *sequence_value2;
   136     sqlite3_stmt *sequence_value3;
   137 
   138     // revoked keys
   139     sqlite3_stmt *set_revoked;
   140     sqlite3_stmt *get_revoked;
   141 
   142     // callbacks
   143     examine_identity_t examine_identity;
   144     void *examine_management;
   145     void *sync_management;
   146     void *sync_obj;
   147     messageToSend_t messageToSend;
   148     notifyHandshake_t notifyHandshake;
   149     inject_sync_msg_t inject_sync_msg;
   150     retrieve_next_sync_msg_t retrieve_next_sync_msg;
   151 
   152     // key sync
   153     pEpSession* sync_session;
   154     DeviceState_state sync_state;
   155     void* sync_state_payload;
   156     char sync_uuid[37];
   157     time_t LastCannotDecrypt;
   158     time_t LastUpdateRequest;
   159 
   160     // runtime config
   161 
   162     bool passive_mode;
   163     bool unencrypted_subject;
   164     bool keep_sync_msg;
   165     bool service_log;
   166     
   167 #ifdef DEBUG_ERRORSTACK
   168     stringlist_t* errorstack;
   169 #endif
   170 };
   171 
   172 
   173 PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
   174 void release_transport_system(PEP_SESSION session, bool out_last);
   175 
   176 /* NOT to be exposed to the outside!!! */
   177 PEP_STATUS encrypt_only(
   178         PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
   179         size_t psize, char **ctext, size_t *csize
   180 );
   181 
   182 #ifdef NDEBUG
   183 #define DEBUG_LOG(TITLE, ENTITY, DESC)
   184 #else
   185 #ifdef ANDROID
   186 #include <android/log.h>
   187 #define  LOG_MORE(...)  __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s :: %s ", __VA_ARGS__);
   188 #else
   189 #include <stdio.h>
   190 #define  LOG_MORE(...)  fprintf(stderr, "pEpEngine DEBUG_LOG('%s','%s','%s','%s')\n", __VA_ARGS__);
   191 #endif
   192 #define DEBUG_LOG(TITLE, ENTITY, DESC) {\
   193     log_event(session, (TITLE), (ENTITY), (DESC), "debug " __FILE__ ":" S_LINE);\
   194     LOG_MORE((TITLE), (ENTITY), (DESC), __FILE__ ":" S_LINE)\
   195 }
   196 #endif
   197 
   198 typedef enum _normalize_hex_rest_t {
   199     accept_hex,
   200     ignore_hex,
   201     reject_hex
   202 } normalize_hex_res_t;
   203 
   204 static inline normalize_hex_res_t _normalize_hex(char *hex) 
   205 {
   206     if (*hex >= '0' && *hex <= '9')
   207         return accept_hex;
   208 
   209     if (*hex >= 'A' && *hex <= 'F') {
   210         *hex += 'a' - 'A';
   211         return accept_hex;
   212     }
   213 
   214     if (*hex >= 'a' && *hex <= 'f') 
   215         return accept_hex;
   216 
   217     if (*hex == ' ') 
   218         return ignore_hex;
   219 
   220     return reject_hex;
   221 }
   222 
   223 // Space tolerant and case insensitive fingerprint string compare
   224 static inline PEP_STATUS _compare_fprs(
   225         const char* fpra,
   226         size_t fpras,
   227         const char* fprb,
   228         size_t fprbs,
   229         int* comparison)
   230 {
   231 
   232     size_t ai = 0;
   233     size_t bi = 0;
   234     size_t significant = 0;
   235     int _comparison = 0;
   236     const int _FULL_FINGERPRINT_LENGTH = 40;
   237    
   238     // First compare every non-ignored chars until an end is reached
   239     while(ai < fpras && bi < fprbs)
   240     {
   241         char fprac = fpra[ai];
   242         char fprbc = fprb[bi];
   243         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   244         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   245 
   246         if(fprah == reject_hex || fprbh == reject_hex)
   247             return PEP_ILLEGAL_VALUE;
   248 
   249         if ( fprah == ignore_hex )
   250         {
   251             ai++;
   252         }
   253         else if ( fprbh == ignore_hex )
   254         {
   255             bi++;
   256         }
   257         else
   258         {
   259             if(fprac != fprbc && _comparison == 0 )
   260             {
   261                 _comparison = fprac > fprbc ? 1 : -1;
   262             }
   263 
   264             significant++;
   265             ai++;
   266             bi++;
   267 
   268         } 
   269     }
   270 
   271     // Bail out if we didn't got enough significnt chars
   272     if (significant != _FULL_FINGERPRINT_LENGTH )
   273         return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   274 
   275     // Then purge remaining chars, all must be ignored chars
   276     while ( ai < fpras )
   277     {
   278         char fprac = fpra[ai];
   279         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   280         if( fprah == reject_hex )
   281             return PEP_ILLEGAL_VALUE;
   282         if ( fprah != ignore_hex )
   283             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   284         ai++;
   285     }
   286     while ( bi < fprbs )
   287     {
   288         char fprbc = fprb[bi];
   289         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   290         if( fprbh == reject_hex )
   291             return PEP_ILLEGAL_VALUE;
   292         if ( fprbh != ignore_hex )
   293             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   294         bi++;
   295     }
   296 
   297     *comparison = _comparison;
   298     return PEP_STATUS_OK;
   299 }
   300 
   301 static inline int _same_fpr(
   302         const char* fpra,
   303         size_t fpras,
   304         const char* fprb,
   305         size_t fprbs
   306     )
   307 {
   308     // illegal values are ignored, and considered not same.
   309     int comparison = 1;
   310 
   311     _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
   312 
   313     return comparison == 0;
   314 }
   315 
   316 
   317 #ifdef DEBUG_ERRORSTACK
   318     PEP_STATUS session_add_error(PEP_SESSION session, const char* file, unsigned line, PEP_STATUS status);
   319     #define ADD_TO_LOG(status)   session_add_error(session, __FILE__, __LINE__, (status))
   320     #define GOTO(label)          do{ (void)session_add_error(session, __FILE__, __LINE__, status); goto label; }while(0)
   321 #else
   322     #define ADD_TO_LOG(status)   (status)
   323     #define GOTO(label)          goto label
   324 #endif