src/pEp_internal.h
author Edouard Tisserant <edouard@pep-project.org>
Wed, 03 May 2017 18:11:49 +0200
changeset 1746 498b0671ff4e
parent 1648 158e65c83311
child 1752 6991834e731a
child 1753 01faac16cec6
child 1816 dc9bc5fa3f18
permissions -rw-r--r--
Factorized and reworked fingerprint comparison code
     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 typedef enum _normalize_hex_rest_t {
   193     accept_hex,
   194     ignore_hex,
   195     reject_hex
   196 } normalize_hex_res_t;
   197 
   198 static inline normalize_hex_res_t _normalize_hex(char *hex) 
   199 {
   200     if (*hex >= '0' && *hex <= '9')
   201         return accept_hex;
   202 
   203     if (*hex >= 'A' && *hex <= 'F') {
   204         *hex += 'a' - 'A';
   205         return accept_hex;
   206     }
   207 
   208     if (*hex >= 'a' && *hex <= 'f') 
   209         return accept_hex;
   210 
   211     if (*hex == ' ') 
   212         return ignore_hex;
   213 
   214     return reject_hex;
   215 }
   216 
   217 // Space tolerant and case insensitive fingerprint string compare
   218 static inline PEP_STATUS _compare_fprs(
   219         const char* fpra,
   220         size_t fpras,
   221         const char* fprb,
   222         size_t fprbs,
   223         int* comparison)
   224 {
   225 
   226     size_t ai = 0;
   227     size_t bi = 0;
   228     size_t significant = 0;
   229     int _comparison = 0;
   230     const int _FULL_FINGERPRINT_LENGTH = 40;
   231    
   232     // First compare every non-ignored chars until an end is reached
   233     while(ai < fpras && bi < fprbs)
   234     {
   235         char fprac = fpra[ai];
   236         char fprbc = fprb[bi];
   237         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   238         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   239 
   240         if(fprah == reject_hex || fprbh == reject_hex)
   241             return PEP_ILLEGAL_VALUE;
   242 
   243         if ( fprah == ignore_hex )
   244         {
   245             ai++;
   246         }
   247         else if ( fprbh == ignore_hex )
   248         {
   249             bi++;
   250         }
   251         else
   252         {
   253             if(fprac != fprbc && _comparison == 0 )
   254             {
   255                 _comparison = fprac > fprbc ? 1 : -1;
   256             }
   257 
   258             significant++;
   259             ai++;
   260             bi++;
   261 
   262         } 
   263     }
   264 
   265     // Bail out if we didn't got enough significnt chars
   266     if (significant != _FULL_FINGERPRINT_LENGTH )
   267         return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   268 
   269     // Then purge remaining chars, all must be ignored chars
   270     while ( ai < fpras )
   271     {
   272         char fprac = fpra[ai];
   273         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   274         if( fprah == reject_hex )
   275             return PEP_ILLEGAL_VALUE;
   276         if ( fprah != ignore_hex )
   277             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   278         ai++;
   279     }
   280     while ( bi < fprbs )
   281     {
   282         char fprbc = fprb[bi];
   283         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   284         if( fprbh == reject_hex )
   285             return PEP_ILLEGAL_VALUE;
   286         if ( fprbh != ignore_hex )
   287             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   288         bi++;
   289     }
   290 
   291     *comparison = _comparison;
   292     return PEP_STATUS_OK;
   293 }
   294 
   295 static inline int _same_fpr(
   296         const char* fpra,
   297         size_t fpras,
   298         const char* fprb,
   299         size_t fprbs
   300     )
   301 {
   302     // illegal values are ignored, and considered not same.
   303     int comparison = 1;
   304 
   305     _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
   306 
   307     return comparison == 0;
   308 }