src/pEp_internal.h
author Krista Bennett <krista@pep-project.org>
Thu, 18 May 2017 11:27:07 +0200
branchENGINE-209
changeset 1797 32581fade44d
parent 1791 0253b2e1b8ff
child 1799 1e46b8759248
permissions -rw-r--r--
ENGINE-209: compiling again
     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 *get_identities_by_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 *get_trust;
   113     sqlite3_stmt *get_key_userids;
   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     
   166 #ifdef DEBUG_ERRORSTACK
   167     stringlist_t* errorstack;
   168 #endif
   169 };
   170 
   171 
   172 PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
   173 void release_transport_system(PEP_SESSION session, bool out_last);
   174 
   175 /* NOT to be exposed to the outside!!! */
   176 PEP_STATUS encrypt_only(
   177         PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
   178         size_t psize, char **ctext, size_t *csize
   179 );
   180 
   181 #ifdef NDEBUG
   182 #define DEBUG_LOG(TITLE, ENTITY, DESC)
   183 #else
   184 #ifdef ANDROID
   185 #include <android/log.h>
   186 #define  LOG_MORE(...)  __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s ", __VA_ARGS__);
   187 #else
   188 #include <stdio.h>
   189 #define  LOG_MORE(...)  printf("pEpEngine DEBUG_LOG('%s','%s','%s')\n", __VA_ARGS__);
   190 #endif
   191 #define DEBUG_LOG(TITLE, ENTITY, DESC) {\
   192     log_event(session, (TITLE), (ENTITY), (DESC), "debug");\
   193     LOG_MORE((TITLE), (ENTITY), (DESC))\
   194 }
   195 #endif
   196 
   197 typedef enum _normalize_hex_rest_t {
   198     accept_hex,
   199     ignore_hex,
   200     reject_hex
   201 } normalize_hex_res_t;
   202 
   203 static inline normalize_hex_res_t _normalize_hex(char *hex) 
   204 {
   205     if (*hex >= '0' && *hex <= '9')
   206         return accept_hex;
   207 
   208     if (*hex >= 'A' && *hex <= 'F') {
   209         *hex += 'a' - 'A';
   210         return accept_hex;
   211     }
   212 
   213     if (*hex >= 'a' && *hex <= 'f') 
   214         return accept_hex;
   215 
   216     if (*hex == ' ') 
   217         return ignore_hex;
   218 
   219     return reject_hex;
   220 }
   221 
   222 // Space tolerant and case insensitive fingerprint string compare
   223 static inline PEP_STATUS _compare_fprs(
   224         const char* fpra,
   225         size_t fpras,
   226         const char* fprb,
   227         size_t fprbs,
   228         int* comparison)
   229 {
   230 
   231     size_t ai = 0;
   232     size_t bi = 0;
   233     size_t significant = 0;
   234     int _comparison = 0;
   235     const int _FULL_FINGERPRINT_LENGTH = 40;
   236    
   237     // First compare every non-ignored chars until an end is reached
   238     while(ai < fpras && bi < fprbs)
   239     {
   240         char fprac = fpra[ai];
   241         char fprbc = fprb[bi];
   242         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   243         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   244 
   245         if(fprah == reject_hex || fprbh == reject_hex)
   246             return PEP_ILLEGAL_VALUE;
   247 
   248         if ( fprah == ignore_hex )
   249         {
   250             ai++;
   251         }
   252         else if ( fprbh == ignore_hex )
   253         {
   254             bi++;
   255         }
   256         else
   257         {
   258             if(fprac != fprbc && _comparison == 0 )
   259             {
   260                 _comparison = fprac > fprbc ? 1 : -1;
   261             }
   262 
   263             significant++;
   264             ai++;
   265             bi++;
   266 
   267         } 
   268     }
   269 
   270     // Bail out if we didn't got enough significnt chars
   271     if (significant != _FULL_FINGERPRINT_LENGTH )
   272         return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   273 
   274     // Then purge remaining chars, all must be ignored chars
   275     while ( ai < fpras )
   276     {
   277         char fprac = fpra[ai];
   278         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   279         if( fprah == reject_hex )
   280             return PEP_ILLEGAL_VALUE;
   281         if ( fprah != ignore_hex )
   282             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   283         ai++;
   284     }
   285     while ( bi < fprbs )
   286     {
   287         char fprbc = fprb[bi];
   288         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   289         if( fprbh == reject_hex )
   290             return PEP_ILLEGAL_VALUE;
   291         if ( fprbh != ignore_hex )
   292             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   293         bi++;
   294     }
   295 
   296     *comparison = _comparison;
   297     return PEP_STATUS_OK;
   298 }
   299 
   300 static inline int _same_fpr(
   301         const char* fpra,
   302         size_t fpras,
   303         const char* fprb,
   304         size_t fprbs
   305     )
   306 {
   307     // illegal values are ignored, and considered not same.
   308     int comparison = 1;
   309 
   310     _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
   311 
   312     return comparison == 0;
   313 }
   314 
   315 
   316 #ifdef DEBUG_ERRORSTACK
   317     PEP_STATUS session_add_error(PEP_SESSION session, const char* file, unsigned line, PEP_STATUS status);
   318     #define ERROR(status)   session_add_error(session, __FILE__, __LINE__, (status))
   319     #define GOTO(label)     do{ (void)session_add_error(session, __FILE__, __LINE__, status); goto label; }while(0)
   320 #else
   321     #define ERROR(status)   (status)
   322     #define GOTO(label)     goto label
   323 #endif