src/pEp_internal.h
author Krista Bennett <krista@pep-project.org>
Fri, 15 Jun 2018 20:17:49 +0200
branchENGINE-398
changeset 2752 713b65791080
parent 2747 f1158ffe70e2
child 2799 913e407519e2
permissions -rw-r--r--
shelving to debug other things
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #define PEP_ENGINE_VERSION "0.9.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 #define KEY_EXPIRE_DELTA (60 * 60 * 24 * 365)
    12 
    13 // this is 20 trustwords with 79 chars max
    14 #define MAX_TRUSTWORDS_SPACE (20 * 80)
    15 
    16 // XML parameters string
    17 #define PARMS_MAX 32768
    18 
    19 // maximum busy wait time in ms
    20 #define BUSY_WAIT_TIME 5000
    21 
    22 // maximum line length for reading gpg.conf
    23 #define MAX_LINELENGTH 1024
    24 
    25 // default keyserver
    26 #ifndef DEFAULT_KEYSERVER
    27 #define DEFAULT_KEYSERVER "hkp://keys.gnupg.net"
    28 #endif
    29 
    30 // crashdump constants
    31 #ifndef CRASHDUMP_DEFAULT_LINES
    32 #define CRASHDUMP_DEFAULT_LINES 100
    33 #endif
    34 #define CRASHDUMP_MAX_LINES 32767
    35 
    36 // p≡p full string, NUL-terminated
    37 #ifndef PEP_SUBJ_STRING
    38 #define PEP_SUBJ_STRING {0x70,0xE2,0x89,0xA1,0x70,0x00}
    39 #define PEP_SUBJ_BYTELEN 5
    40 #endif
    41 
    42 #ifndef PEP_SUBJ_KEY
    43 #define PEP_SUBJ_KEY "Subject: "
    44 #define PEP_SUBJ_KEY_LC "subject: "
    45 #define PEP_SUBJ_KEY_LEN 9
    46 #endif
    47 
    48 #ifndef PEP_MSG_WRAP_KEY
    49 #define PEP_MSG_WRAP_KEY "pEp-Wrapped-Message-Info: "
    50 #define PEP_MSG_WRAP_KEY_LC "pep-wrapped-message-info: "
    51 #define PEP_MSG_WRAP_KEY_LEN 26
    52 #endif
    53 
    54 
    55 #include "platform.h"
    56 
    57 #ifdef WIN32
    58 #define LOCAL_DB windoze_local_db()
    59 #define SYSTEM_DB windoze_system_db()
    60 #define LIBGPGME "libgpgme-11.dll"
    61 #else // UNIX
    62 #define _POSIX_C_SOURCE 200809L
    63 #include <dlfcn.h>
    64 #ifdef NDEBUG
    65 #define LOCAL_DB unix_local_db()
    66 #else
    67 #define LOCAL_DB unix_local_db(false)
    68 #endif
    69 #ifndef SYSTEM_DB
    70 #define SYSTEM_DB "/usr/share/pEp/system.db"
    71 #endif
    72 #ifndef LIBGPGME
    73 #define LIBGPGME "libgpgme-pthread.so"
    74 #endif
    75 #endif
    76 
    77 #include <locale.h>
    78 #include <stdlib.h>
    79 #include <string.h>
    80 #include <assert.h>
    81 #include <stdio.h>
    82 #include <ctype.h>
    83 #include <math.h>
    84 
    85 #ifdef SQLITE3_FROM_OS
    86 #include <sqlite3.h>
    87 #else
    88 #include "sqlite3.h"
    89 #endif
    90 
    91 #include "pEpEngine.h"
    92 
    93 // If not specified, build for GPG
    94 #ifndef USE_NETPGP
    95 #ifndef USE_GPG
    96 #define USE_GPG
    97 #endif
    98 #endif
    99 
   100 #ifdef USE_GPG
   101 #include "pgp_gpg_internal.h"
   102 #elif defined(USE_NETPGP)
   103 #include "pgp_netpgp_internal.h"
   104 #endif
   105 
   106 #include "keymanagement.h"
   107 #include "cryptotech.h"
   108 #include "transport.h"
   109 #include "sync.h"
   110 
   111 #define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
   112 
   113 struct _pEpSession;
   114 typedef struct _pEpSession pEpSession;
   115 struct _pEpSession {
   116     const char *version;
   117 #ifdef USE_GPG
   118     gpgme_ctx_t ctx;
   119 #elif defined(USE_NETPGP)
   120     pEpNetPGPSession ctx;
   121 #endif
   122 
   123     PEP_cryptotech_t *cryptotech;
   124     PEP_transport_t *transports;
   125 
   126     sqlite3 *db;
   127     sqlite3 *system_db;
   128 
   129     sqlite3_stmt *log;
   130     sqlite3_stmt *trustword;
   131     sqlite3_stmt *get_identity;
   132     sqlite3_stmt *get_identity_without_trust_check;
   133     sqlite3_stmt *get_identities_by_address;
   134     sqlite3_stmt *replace_identities_fpr;
   135     sqlite3_stmt *replace_main_user_fpr;
   136     sqlite3_stmt *get_main_user_fpr;
   137     sqlite3_stmt *refresh_userid_default_key;
   138     sqlite3_stmt *remove_fpr_as_default;
   139     sqlite3_stmt *set_person;
   140     sqlite3_stmt *update_person;
   141     sqlite3_stmt *exists_person;    
   142     sqlite3_stmt *set_as_pep_user;
   143     sqlite3_stmt *is_pep_user;
   144     sqlite3_stmt *add_into_social_graph;
   145     sqlite3_stmt *get_own_address_binding_from_contact;
   146     sqlite3_stmt *set_revoke_contact_as_notified;
   147     sqlite3_stmt *get_contacted_ids_from_revoke_fpr;
   148     sqlite3_stmt *was_id_for_revoke_contacted;
   149     sqlite3_stmt *set_device_group;
   150     sqlite3_stmt *get_device_group;
   151     sqlite3_stmt *set_pgp_keypair;
   152     sqlite3_stmt *set_identity_entry;
   153     sqlite3_stmt *update_identity_entry;
   154     sqlite3_stmt *exists_identity_entry;        
   155     sqlite3_stmt *set_identity_flags;
   156     sqlite3_stmt *unset_identity_flags;
   157     sqlite3_stmt *set_trust;
   158     sqlite3_stmt *update_trust;
   159     sqlite3_stmt *update_trust_to_pep;    
   160     sqlite3_stmt *exists_trust_entry;
   161     sqlite3_stmt *update_trust_for_fpr;
   162     sqlite3_stmt *get_trust;
   163     sqlite3_stmt *least_trust;
   164     sqlite3_stmt *mark_compromised;
   165     sqlite3_stmt *reset_trust;
   166     sqlite3_stmt *crashdump;
   167     sqlite3_stmt *languagelist;
   168     sqlite3_stmt *i18n_token;
   169     sqlite3_stmt *replace_userid;
   170 
   171     // blacklist
   172     sqlite3_stmt *blacklist_add;
   173     sqlite3_stmt *blacklist_delete;
   174     sqlite3_stmt *blacklist_is_listed;
   175     sqlite3_stmt *blacklist_retrieve;
   176     
   177     // Own keys
   178     sqlite3_stmt *own_key_is_listed;
   179     sqlite3_stmt *own_identities_retrieve;
   180     sqlite3_stmt *own_keys_retrieve;
   181     sqlite3_stmt *get_user_default_key;
   182         
   183     sqlite3_stmt *get_default_own_userid;
   184 
   185 //    sqlite3_stmt *set_own_key;
   186 
   187     // sequence value
   188     sqlite3_stmt *sequence_value1;
   189     sqlite3_stmt *sequence_value2;
   190     sqlite3_stmt *sequence_value3;
   191 
   192     // revoked keys
   193     sqlite3_stmt *set_revoked;
   194     sqlite3_stmt *get_revoked;
   195     sqlite3_stmt *get_replacement_fpr;
   196 
   197     // mistrusted
   198     sqlite3_stmt* add_mistrusted_key;
   199     sqlite3_stmt* is_mistrusted_key;    
   200     sqlite3_stmt* delete_mistrusted_key;
   201     
   202     // aliases
   203     sqlite3_stmt *get_userid_alias_default;
   204     sqlite3_stmt *add_userid_alias;
   205 
   206     // callbacks
   207     examine_identity_t examine_identity;
   208     void *examine_management;
   209     void *sync_management;
   210     void *sync_obj;
   211     messageToSend_t messageToSend;
   212     notifyHandshake_t notifyHandshake;
   213     inject_sync_msg_t inject_sync_msg;
   214     retrieve_next_sync_msg_t retrieve_next_sync_msg;
   215 
   216     // key sync
   217     pEpSession* sync_session;
   218     DeviceState_state sync_state;
   219     void* sync_state_payload;
   220     char sync_uuid[37];
   221     time_t LastCannotDecrypt;
   222     time_t LastUpdateRequest;
   223 
   224     // runtime config
   225 
   226     bool passive_mode;
   227     bool unencrypted_subject;
   228     bool keep_sync_msg;
   229     bool service_log;
   230 
   231     // mistrust undo cache
   232     pEp_identity* cached_mistrusted;
   233     
   234 #ifdef DEBUG_ERRORSTACK
   235     stringlist_t* errorstack;
   236 #endif
   237 };
   238 
   239 
   240 PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
   241 void release_transport_system(PEP_SESSION session, bool out_last);
   242 
   243 /* NOT to be exposed to the outside!!! */
   244 PEP_STATUS encrypt_only(
   245         PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
   246         size_t psize, char **ctext, size_t *csize
   247 );
   248 
   249 #if defined(NDEBUG) || defined(NOLOG)
   250 #define DEBUG_LOG(TITLE, ENTITY, DESC)
   251 #else
   252 #ifdef ANDROID
   253 #include <android/log.h>
   254 #define  LOG_MORE(...)  __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s :: %s ", __VA_ARGS__);
   255 #else
   256 #include <stdio.h>
   257 #define  LOG_MORE(...)  fprintf(stderr, "pEpEngine DEBUG_LOG('%s','%s','%s','%s')\n", __VA_ARGS__);
   258 #endif
   259 #define DEBUG_LOG(TITLE, ENTITY, DESC) {\
   260     log_event(session, (TITLE), (ENTITY), (DESC), "debug " __FILE__ ":" S_LINE);\
   261     LOG_MORE((TITLE), (ENTITY), (DESC), __FILE__ ":" S_LINE)\
   262 }
   263 #endif
   264 
   265 typedef enum _normalize_hex_rest_t {
   266     accept_hex,
   267     ignore_hex,
   268     reject_hex
   269 } normalize_hex_res_t;
   270 
   271 static inline normalize_hex_res_t _normalize_hex(char *hex) 
   272 {
   273     if (*hex >= '0' && *hex <= '9')
   274         return accept_hex;
   275 
   276     if (*hex >= 'A' && *hex <= 'F') {
   277         *hex += 'a' - 'A';
   278         return accept_hex;
   279     }
   280 
   281     if (*hex >= 'a' && *hex <= 'f') 
   282         return accept_hex;
   283 
   284     if (*hex == ' ') 
   285         return ignore_hex;
   286 
   287     return reject_hex;
   288 }
   289 
   290 // Space tolerant and case insensitive fingerprint string compare
   291 static inline PEP_STATUS _compare_fprs(
   292         const char* fpra,
   293         size_t fpras,
   294         const char* fprb,
   295         size_t fprbs,
   296         int* comparison)
   297 {
   298 
   299     size_t ai = 0;
   300     size_t bi = 0;
   301     size_t significant = 0;
   302     int _comparison = 0;
   303     const int _FULL_FINGERPRINT_LENGTH = 40;
   304    
   305     // First compare every non-ignored chars until an end is reached
   306     while(ai < fpras && bi < fprbs)
   307     {
   308         char fprac = fpra[ai];
   309         char fprbc = fprb[bi];
   310         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   311         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   312 
   313         if(fprah == reject_hex || fprbh == reject_hex)
   314             return PEP_ILLEGAL_VALUE;
   315 
   316         if ( fprah == ignore_hex )
   317         {
   318             ai++;
   319         }
   320         else if ( fprbh == ignore_hex )
   321         {
   322             bi++;
   323         }
   324         else
   325         {
   326             if(fprac != fprbc && _comparison == 0 )
   327             {
   328                 _comparison = fprac > fprbc ? 1 : -1;
   329             }
   330 
   331             significant++;
   332             ai++;
   333             bi++;
   334 
   335         } 
   336     }
   337 
   338     // Bail out if we didn't got enough significnt chars
   339     if (significant != _FULL_FINGERPRINT_LENGTH )
   340         return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   341 
   342     // Then purge remaining chars, all must be ignored chars
   343     while ( ai < fpras )
   344     {
   345         char fprac = fpra[ai];
   346         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   347         if( fprah == reject_hex )
   348             return PEP_ILLEGAL_VALUE;
   349         if ( fprah != ignore_hex )
   350             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   351         ai++;
   352     }
   353     while ( bi < fprbs )
   354     {
   355         char fprbc = fprb[bi];
   356         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   357         if( fprbh == reject_hex )
   358             return PEP_ILLEGAL_VALUE;
   359         if ( fprbh != ignore_hex )
   360             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   361         bi++;
   362     }
   363 
   364     *comparison = _comparison;
   365     return PEP_STATUS_OK;
   366 }
   367 
   368 static inline int _same_fpr(
   369         const char* fpra,
   370         size_t fpras,
   371         const char* fprb,
   372         size_t fprbs
   373     )
   374 {
   375     // illegal values are ignored, and considered not same.
   376     int comparison = 1;
   377 
   378     _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
   379 
   380     return comparison == 0;
   381 }
   382 
   383 // size is the length of the bytestr that's coming in. This is really only intended
   384 // for comparing two full strings. If charstr's length is different from bytestr_size,
   385 // we'll return a non-zero value.
   386 static inline int _unsigned_signed_strcmp(const unsigned char* bytestr, const char* charstr, int bytestr_size) {
   387     int charstr_len = strlen(charstr);
   388     if (charstr_len != bytestr_size)
   389         return -1; // we don't actually care except that it's non-zero
   390     return memcmp(bytestr, charstr, bytestr_size);
   391 }
   392 
   393 // This is just a horrible example of C type madness. UTF-8 made me do it.
   394 static inline char* _pep_subj_copy() {
   395 #ifndef WIN32
   396     unsigned char pepstr[] = PEP_SUBJ_STRING;
   397     void* retval = calloc(1, sizeof(unsigned char)*PEP_SUBJ_BYTELEN + 1);
   398     memcpy(retval, pepstr, PEP_SUBJ_BYTELEN);
   399     return (char*)retval;
   400 #else
   401     return strdup("pEp");
   402 #endif
   403 }
   404 
   405 static inline bool is_me(PEP_SESSION session, pEp_identity* test_ident) {
   406     bool retval = false;
   407     if (test_ident && test_ident->user_id) {
   408         char* def_id = NULL;
   409         get_default_own_userid(session, &def_id);
   410         if (test_ident->me || 
   411             (def_id && strcmp(def_id, test_ident->user_id) == 0)) {
   412             retval = true;
   413         }
   414         free(def_id);
   415     }
   416     return retval;
   417 }
   418 
   419 #ifndef EMPTYSTR
   420 #define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
   421 #endif
   422 
   423 #ifndef IS_PGP_CT
   424 #define IS_PGP_CT(CT) (((CT) | PEP_ct_confirmed) == PEP_ct_OpenPGP)
   425 #endif
   426 
   427 #ifndef _MIN
   428 #define _MIN(A, B) ((B) > (A) ? (A) : (B))
   429 #endif
   430 #ifndef _MAX
   431 #define _MAX(A, B) ((B) > (A) ? (B) : (A))
   432 #endif
   433 
   434 
   435 // These are globals used in generating message IDs and should only be
   436 // computed once, as they're either really constants or OS-dependent
   437 
   438 extern int _pEp_rand_max_bits;
   439 extern double _pEp_log2_36;
   440 
   441 static inline void _init_globals() {
   442     _pEp_rand_max_bits = (int) ceil(log2((double) RAND_MAX));
   443     _pEp_log2_36 = log2(36);
   444 }