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