src/pEp_internal.h
author Krista Bennett <krista@pep-project.org>
Fri, 01 Jun 2018 10:30:19 +0200
branchENGINE-254
changeset 2723 7194b9c8599d
parent 2129 fd3108a623c5
child 2131 334139882a97
permissions -rw-r--r--
close branch
     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 // p≡p full string, NUL-terminated
    35 #ifndef PEP_SUBJ_STRING
    36 #define PEP_SUBJ_STRING {0x70,0xE2,0x89,0xA1,0x70,0x00}
    37 #define PEP_SUBJ_BYTELEN 5
    38 #endif
    39 
    40 #include "platform.h"
    41 
    42 #ifdef WIN32
    43 #define LOCAL_DB windoze_local_db()
    44 #define SYSTEM_DB windoze_system_db()
    45 #define LIBGPGME "libgpgme-11.dll"
    46 #else // UNIX
    47 #define _POSIX_C_SOURCE 200809L
    48 #include <dlfcn.h>
    49 #define LOCAL_DB unix_local_db()
    50 #ifndef SYSTEM_DB
    51 #define SYSTEM_DB "/usr/share/pEp/system.db"
    52 #endif
    53 #ifndef LIBGPGME
    54 #define LIBGPGME "libgpgme-pthread.so"
    55 #endif
    56 #endif
    57 
    58 #include <locale.h>
    59 #include <stdlib.h>
    60 #include <string.h>
    61 #include <assert.h>
    62 #include <stdio.h>
    63 #include <ctype.h>
    64 
    65 #ifdef SQLITE3_FROM_OS
    66 #include <sqlite3.h>
    67 #else
    68 #include "sqlite3.h"
    69 #endif
    70 
    71 #define _EXPORT_PEP_ENGINE_DLL
    72 #include "pEpEngine.h"
    73 
    74 // If not specified, build for GPG
    75 #ifndef USE_NETPGP
    76 #ifndef USE_GPG
    77 #define USE_GPG
    78 #endif
    79 #endif
    80 
    81 #ifdef USE_GPG
    82 #include "pgp_gpg_internal.h"
    83 #elif defined(USE_NETPGP)
    84 #include "pgp_netpgp_internal.h"
    85 #endif
    86 
    87 #include "keymanagement.h"
    88 #include "cryptotech.h"
    89 #include "transport.h"
    90 #include "sync.h"
    91 
    92 #define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
    93 
    94 struct _pEpSession;
    95 typedef struct _pEpSession pEpSession;
    96 struct _pEpSession {
    97     const char *version;
    98 #ifdef USE_GPG
    99     gpgme_ctx_t ctx;
   100 #elif defined(USE_NETPGP)
   101     pEpNetPGPSession ctx;
   102 #endif
   103 
   104     PEP_cryptotech_t *cryptotech;
   105     PEP_transport_t *transports;
   106 
   107     sqlite3 *db;
   108     sqlite3 *system_db;
   109 
   110     sqlite3_stmt *log;
   111     sqlite3_stmt *trustword;
   112     sqlite3_stmt *get_identity;
   113     sqlite3_stmt *replace_identities_fpr;
   114     sqlite3_stmt *set_person;
   115     sqlite3_stmt *set_device_group;
   116     sqlite3_stmt *get_device_group;
   117     sqlite3_stmt *set_pgp_keypair;
   118     sqlite3_stmt *set_identity;
   119     sqlite3_stmt *set_identity_flags;
   120     sqlite3_stmt *unset_identity_flags;
   121     sqlite3_stmt *set_trust;
   122     sqlite3_stmt *update_trust_for_fpr;
   123     sqlite3_stmt *get_trust;
   124     sqlite3_stmt *least_trust;
   125     sqlite3_stmt *mark_compromized;
   126     sqlite3_stmt *reset_trust;
   127     sqlite3_stmt *crashdump;
   128     sqlite3_stmt *languagelist;
   129     sqlite3_stmt *i18n_token;
   130 
   131     // blacklist
   132     sqlite3_stmt *blacklist_add;
   133     sqlite3_stmt *blacklist_delete;
   134     sqlite3_stmt *blacklist_is_listed;
   135     sqlite3_stmt *blacklist_retrieve;
   136     
   137     // Own keys
   138     sqlite3_stmt *own_key_is_listed;
   139     sqlite3_stmt *own_identities_retrieve;
   140     sqlite3_stmt *own_keys_retrieve;
   141     sqlite3_stmt *set_own_key;
   142 
   143     // sequence value
   144     sqlite3_stmt *sequence_value1;
   145     sqlite3_stmt *sequence_value2;
   146     sqlite3_stmt *sequence_value3;
   147 
   148     // revoked keys
   149     sqlite3_stmt *set_revoked;
   150     sqlite3_stmt *get_revoked;
   151 
   152     // callbacks
   153     examine_identity_t examine_identity;
   154     void *examine_management;
   155     void *sync_management;
   156     void *sync_obj;
   157     messageToSend_t messageToSend;
   158     notifyHandshake_t notifyHandshake;
   159     inject_sync_msg_t inject_sync_msg;
   160     retrieve_next_sync_msg_t retrieve_next_sync_msg;
   161 
   162     // key sync
   163     pEpSession* sync_session;
   164     DeviceState_state sync_state;
   165     void* sync_state_payload;
   166     char sync_uuid[37];
   167     time_t LastCannotDecrypt;
   168     time_t LastUpdateRequest;
   169 
   170     // runtime config
   171 
   172     bool passive_mode;
   173     bool unencrypted_subject;
   174     bool keep_sync_msg;
   175     bool service_log;
   176 
   177     // mistrust undo cache
   178     pEp_identity* cached_mistrusted;
   179     
   180 #ifdef DEBUG_ERRORSTACK
   181     stringlist_t* errorstack;
   182 #endif
   183 };
   184 
   185 
   186 PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
   187 void release_transport_system(PEP_SESSION session, bool out_last);
   188 
   189 /* NOT to be exposed to the outside!!! */
   190 PEP_STATUS encrypt_only(
   191         PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
   192         size_t psize, char **ctext, size_t *csize
   193 );
   194 
   195 #if defined(NDEBUG) || defined(NOLOG)
   196 #define DEBUG_LOG(TITLE, ENTITY, DESC)
   197 #else
   198 #ifdef ANDROID
   199 #include <android/log.h>
   200 #define  LOG_MORE(...)  __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s :: %s ", __VA_ARGS__);
   201 #else
   202 #include <stdio.h>
   203 #define  LOG_MORE(...)  fprintf(stderr, "pEpEngine DEBUG_LOG('%s','%s','%s','%s')\n", __VA_ARGS__);
   204 #endif
   205 #define DEBUG_LOG(TITLE, ENTITY, DESC) {\
   206     log_event(session, (TITLE), (ENTITY), (DESC), "debug " __FILE__ ":" S_LINE);\
   207     LOG_MORE((TITLE), (ENTITY), (DESC), __FILE__ ":" S_LINE)\
   208 }
   209 #endif
   210 
   211 typedef enum _normalize_hex_rest_t {
   212     accept_hex,
   213     ignore_hex,
   214     reject_hex
   215 } normalize_hex_res_t;
   216 
   217 static inline normalize_hex_res_t _normalize_hex(char *hex) 
   218 {
   219     if (*hex >= '0' && *hex <= '9')
   220         return accept_hex;
   221 
   222     if (*hex >= 'A' && *hex <= 'F') {
   223         *hex += 'a' - 'A';
   224         return accept_hex;
   225     }
   226 
   227     if (*hex >= 'a' && *hex <= 'f') 
   228         return accept_hex;
   229 
   230     if (*hex == ' ') 
   231         return ignore_hex;
   232 
   233     return reject_hex;
   234 }
   235 
   236 // Space tolerant and case insensitive fingerprint string compare
   237 static inline PEP_STATUS _compare_fprs(
   238         const char* fpra,
   239         size_t fpras,
   240         const char* fprb,
   241         size_t fprbs,
   242         int* comparison)
   243 {
   244 
   245     size_t ai = 0;
   246     size_t bi = 0;
   247     size_t significant = 0;
   248     int _comparison = 0;
   249     const int _FULL_FINGERPRINT_LENGTH = 40;
   250    
   251     // First compare every non-ignored chars until an end is reached
   252     while(ai < fpras && bi < fprbs)
   253     {
   254         char fprac = fpra[ai];
   255         char fprbc = fprb[bi];
   256         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   257         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   258 
   259         if(fprah == reject_hex || fprbh == reject_hex)
   260             return PEP_ILLEGAL_VALUE;
   261 
   262         if ( fprah == ignore_hex )
   263         {
   264             ai++;
   265         }
   266         else if ( fprbh == ignore_hex )
   267         {
   268             bi++;
   269         }
   270         else
   271         {
   272             if(fprac != fprbc && _comparison == 0 )
   273             {
   274                 _comparison = fprac > fprbc ? 1 : -1;
   275             }
   276 
   277             significant++;
   278             ai++;
   279             bi++;
   280 
   281         } 
   282     }
   283 
   284     // Bail out if we didn't got enough significnt chars
   285     if (significant != _FULL_FINGERPRINT_LENGTH )
   286         return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   287 
   288     // Then purge remaining chars, all must be ignored chars
   289     while ( ai < fpras )
   290     {
   291         char fprac = fpra[ai];
   292         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   293         if( fprah == reject_hex )
   294             return PEP_ILLEGAL_VALUE;
   295         if ( fprah != ignore_hex )
   296             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   297         ai++;
   298     }
   299     while ( bi < fprbs )
   300     {
   301         char fprbc = fprb[bi];
   302         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   303         if( fprbh == reject_hex )
   304             return PEP_ILLEGAL_VALUE;
   305         if ( fprbh != ignore_hex )
   306             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   307         bi++;
   308     }
   309 
   310     *comparison = _comparison;
   311     return PEP_STATUS_OK;
   312 }
   313 
   314 static inline int _same_fpr(
   315         const char* fpra,
   316         size_t fpras,
   317         const char* fprb,
   318         size_t fprbs
   319     )
   320 {
   321     // illegal values are ignored, and considered not same.
   322     int comparison = 1;
   323 
   324     _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
   325 
   326     return comparison == 0;
   327 }
   328 
   329 static inline bool _identity_me(
   330         pEp_identity * identity
   331     )
   332 {
   333     return identity->user_id && strcmp(identity->user_id, PEP_OWN_USERID) == 0;
   334 }
   335 
   336 // size is the length of the bytestr that's coming in. This is really only intended
   337 // for comparing two full strings. If charstr's length is different from bytestr_size,
   338 // we'll return a non-zero value.
   339 static inline int _unsigned_signed_strcmp(const unsigned char* bytestr, const char* charstr, int bytestr_size) {
   340     int charstr_len = strlen(charstr);
   341     if (charstr_len != bytestr_size)
   342         return -1; // we don't actually care except that it's non-zero
   343     return memcmp(bytestr, charstr, bytestr_size);
   344 }
   345 
   346 // This is just a horrible example of C type madness. UTF-8 made me do it.
   347 static inline char* _pep_subj_copy() {
   348     unsigned char pepstr[] = PEP_SUBJ_STRING;
   349     void* retval = calloc(1, sizeof(unsigned char)*PEP_SUBJ_BYTELEN + 1);
   350     memcpy(retval, pepstr, PEP_SUBJ_BYTELEN);
   351     return (char*)retval;
   352 }
   353 
   354 #ifdef DEBUG_ERRORSTACK
   355     PEP_STATUS session_add_error(PEP_SESSION session, const char* file, unsigned line, PEP_STATUS status);
   356     #define ADD_TO_LOG(status)   session_add_error(session, __FILE__, __LINE__, (status))
   357     #define GOTO(label)          do{ (void)session_add_error(session, __FILE__, __LINE__, status); goto label; }while(0)
   358 #else
   359     #define ADD_TO_LOG(status)   (status)
   360     #define GOTO(label)          goto label
   361 #endif