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