src/pEp_internal.h
author Krista Bennett <krista@pep-project.org>
Tue, 09 Oct 2018 19:56:07 +0200
changeset 3036 43ab5608555e
parent 3030 e4d80dd0a663
child 3051 084c00652ac9
permissions -rw-r--r--
ENGINE-466 merged in - header key import moves version to 1.1.0
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #define PEP_ENGINE_VERSION "1.1.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 *get_identities_by_userid;
   135     sqlite3_stmt *replace_identities_fpr;
   136     sqlite3_stmt *replace_main_user_fpr;
   137     sqlite3_stmt *get_main_user_fpr;
   138     sqlite3_stmt *refresh_userid_default_key;
   139     sqlite3_stmt *remove_fpr_as_default;
   140     sqlite3_stmt *set_person;
   141     sqlite3_stmt *update_person;
   142     sqlite3_stmt *delete_person;
   143     sqlite3_stmt *exists_person;    
   144     sqlite3_stmt *set_as_pep_user;
   145     sqlite3_stmt *is_pep_user;
   146     sqlite3_stmt *set_device_group;
   147     sqlite3_stmt *get_device_group;
   148     sqlite3_stmt *set_pgp_keypair;
   149     sqlite3_stmt *set_identity_entry;
   150     sqlite3_stmt *update_identity_entry;
   151     sqlite3_stmt *exists_identity_entry;        
   152     sqlite3_stmt *set_identity_flags;
   153     sqlite3_stmt *unset_identity_flags;
   154     sqlite3_stmt *set_trust;
   155     sqlite3_stmt *update_trust;
   156     sqlite3_stmt *update_trust_to_pep;    
   157     sqlite3_stmt *exists_trust_entry;
   158     sqlite3_stmt *update_trust_for_fpr;
   159     sqlite3_stmt *get_trust;
   160     sqlite3_stmt *get_trust_by_userid;
   161     sqlite3_stmt *least_trust;
   162     sqlite3_stmt *mark_compromised;
   163     sqlite3_stmt *reset_trust;
   164     sqlite3_stmt *crashdump;
   165     sqlite3_stmt *languagelist;
   166     sqlite3_stmt *i18n_token;
   167     sqlite3_stmt *replace_userid;
   168 
   169     // blacklist
   170     sqlite3_stmt *blacklist_add;
   171     sqlite3_stmt *blacklist_delete;
   172     sqlite3_stmt *blacklist_is_listed;
   173     sqlite3_stmt *blacklist_retrieve;
   174     
   175     // Own keys
   176     sqlite3_stmt *own_key_is_listed;
   177     sqlite3_stmt *own_identities_retrieve;
   178     sqlite3_stmt *own_keys_retrieve;
   179     sqlite3_stmt *get_user_default_key;
   180         
   181     sqlite3_stmt *get_default_own_userid;
   182 
   183 //    sqlite3_stmt *set_own_key;
   184 
   185     // sequence value
   186     sqlite3_stmt *sequence_value1;
   187     sqlite3_stmt *sequence_value2;
   188     sqlite3_stmt *sequence_value3;
   189 
   190     // revoked keys
   191     sqlite3_stmt *set_revoked;
   192     sqlite3_stmt *get_revoked;
   193 
   194     // mistrusted
   195     sqlite3_stmt* add_mistrusted_key;
   196     sqlite3_stmt* is_mistrusted_key;    
   197     sqlite3_stmt* delete_mistrusted_key;
   198     
   199     // aliases
   200     sqlite3_stmt *get_userid_alias_default;
   201     sqlite3_stmt *add_userid_alias;
   202 
   203     // callbacks
   204     examine_identity_t examine_identity;
   205     void *examine_management;
   206     void *sync_management;
   207     void *sync_obj;
   208     messageToSend_t messageToSend;
   209     notifyHandshake_t notifyHandshake;
   210     inject_sync_msg_t inject_sync_msg;
   211     retrieve_next_sync_msg_t retrieve_next_sync_msg;
   212 
   213     // key sync
   214     pEpSession* sync_session;
   215     DeviceState_state sync_state;
   216     void* sync_state_payload;
   217     char sync_uuid[37];
   218     time_t LastCannotDecrypt;
   219     time_t LastUpdateRequest;
   220 
   221     // runtime config
   222 
   223     bool passive_mode;
   224     bool unencrypted_subject;
   225     bool keep_sync_msg;
   226     bool service_log;
   227 
   228     // mistrust undo cache
   229     pEp_identity* cached_mistrusted;
   230     
   231 #ifdef DEBUG_ERRORSTACK
   232     stringlist_t* errorstack;
   233 #endif
   234 };
   235 
   236 
   237 PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
   238 void release_transport_system(PEP_SESSION session, bool out_last);
   239 
   240 /* NOT to be exposed to the outside!!! */
   241 PEP_STATUS encrypt_only(
   242         PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
   243         size_t psize, char **ctext, size_t *csize
   244 );
   245 
   246 #if defined(NDEBUG) || defined(NOLOG)
   247 #define DEBUG_LOG(TITLE, ENTITY, DESC)
   248 #else
   249 #ifdef ANDROID
   250 #include <android/log.h>
   251 #define  LOG_MORE(...)  __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s :: %s ", __VA_ARGS__);
   252 #else
   253 #include <stdio.h>
   254 #define  LOG_MORE(...)  fprintf(stderr, "pEpEngine DEBUG_LOG('%s','%s','%s','%s')\n", __VA_ARGS__);
   255 #endif
   256 #define DEBUG_LOG(TITLE, ENTITY, DESC) {\
   257     log_event(session, (TITLE), (ENTITY), (DESC), "debug " __FILE__ ":" S_LINE);\
   258     LOG_MORE((TITLE), (ENTITY), (DESC), __FILE__ ":" S_LINE)\
   259 }
   260 #endif
   261 
   262 typedef enum _normalize_hex_rest_t {
   263     accept_hex,
   264     ignore_hex,
   265     reject_hex
   266 } normalize_hex_res_t;
   267 
   268 static inline normalize_hex_res_t _normalize_hex(char *hex) 
   269 {
   270     if (*hex >= '0' && *hex <= '9')
   271         return accept_hex;
   272 
   273     if (*hex >= 'A' && *hex <= 'F') {
   274         *hex += 'a' - 'A';
   275         return accept_hex;
   276     }
   277 
   278     if (*hex >= 'a' && *hex <= 'f') 
   279         return accept_hex;
   280 
   281     if (*hex == ' ') 
   282         return ignore_hex;
   283 
   284     return reject_hex;
   285 }
   286 
   287 // Space tolerant and case insensitive fingerprint string compare
   288 static inline PEP_STATUS _compare_fprs(
   289         const char* fpra,
   290         size_t fpras,
   291         const char* fprb,
   292         size_t fprbs,
   293         int* comparison)
   294 {
   295 
   296     size_t ai = 0;
   297     size_t bi = 0;
   298     size_t significant = 0;
   299     int _comparison = 0;
   300     const int _FULL_FINGERPRINT_LENGTH = 40;
   301    
   302     // First compare every non-ignored chars until an end is reached
   303     while(ai < fpras && bi < fprbs)
   304     {
   305         char fprac = fpra[ai];
   306         char fprbc = fprb[bi];
   307         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   308         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   309 
   310         if(fprah == reject_hex || fprbh == reject_hex)
   311             return PEP_ILLEGAL_VALUE;
   312 
   313         if ( fprah == ignore_hex )
   314         {
   315             ai++;
   316         }
   317         else if ( fprbh == ignore_hex )
   318         {
   319             bi++;
   320         }
   321         else
   322         {
   323             if(fprac != fprbc && _comparison == 0 )
   324             {
   325                 _comparison = fprac > fprbc ? 1 : -1;
   326             }
   327 
   328             significant++;
   329             ai++;
   330             bi++;
   331 
   332         } 
   333     }
   334 
   335     // Bail out if we didn't got enough significnt chars
   336     if (significant != _FULL_FINGERPRINT_LENGTH )
   337         return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   338 
   339     // Then purge remaining chars, all must be ignored chars
   340     while ( ai < fpras )
   341     {
   342         char fprac = fpra[ai];
   343         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   344         if( fprah == reject_hex )
   345             return PEP_ILLEGAL_VALUE;
   346         if ( fprah != ignore_hex )
   347             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   348         ai++;
   349     }
   350     while ( bi < fprbs )
   351     {
   352         char fprbc = fprb[bi];
   353         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   354         if( fprbh == reject_hex )
   355             return PEP_ILLEGAL_VALUE;
   356         if ( fprbh != ignore_hex )
   357             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   358         bi++;
   359     }
   360 
   361     *comparison = _comparison;
   362     return PEP_STATUS_OK;
   363 }
   364 
   365 static inline int _same_fpr(
   366         const char* fpra,
   367         size_t fpras,
   368         const char* fprb,
   369         size_t fprbs
   370     )
   371 {
   372     // illegal values are ignored, and considered not same.
   373     int comparison = 1;
   374 
   375     _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
   376 
   377     return comparison == 0;
   378 }
   379 
   380 // size is the length of the bytestr that's coming in. This is really only intended
   381 // for comparing two full strings. If charstr's length is different from bytestr_size,
   382 // we'll return a non-zero value.
   383 static inline int _unsigned_signed_strcmp(const unsigned char* bytestr, const char* charstr, int bytestr_size) {
   384     int charstr_len = strlen(charstr);
   385     if (charstr_len != bytestr_size)
   386         return -1; // we don't actually care except that it's non-zero
   387     return memcmp(bytestr, charstr, bytestr_size);
   388 }
   389 
   390 // This is just a horrible example of C type madness. UTF-8 made me do it.
   391 static inline char* _pep_subj_copy() {
   392 #ifndef WIN32
   393     unsigned char pepstr[] = PEP_SUBJ_STRING;
   394     void* retval = calloc(1, sizeof(unsigned char)*PEP_SUBJ_BYTELEN + 1);
   395     memcpy(retval, pepstr, PEP_SUBJ_BYTELEN);
   396     return (char*)retval;
   397 #else
   398     return strdup("pEp");
   399 #endif
   400 }
   401 
   402 static inline bool is_me(PEP_SESSION session, pEp_identity* test_ident) {
   403     bool retval = false;
   404     if (test_ident && test_ident->user_id) {
   405         char* def_id = NULL;
   406         get_default_own_userid(session, &def_id);
   407         if (test_ident->me || 
   408             (def_id && strcmp(def_id, test_ident->user_id) == 0)) {
   409             retval = true;
   410         }
   411         free(def_id);
   412     }
   413     return retval;
   414 }
   415 
   416 #ifndef EMPTYSTR
   417 #define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
   418 #endif
   419 
   420 #ifndef IS_PGP_CT
   421 #define IS_PGP_CT(CT) (((CT) | PEP_ct_confirmed) == PEP_ct_OpenPGP)
   422 #endif
   423 
   424 #ifndef _MIN
   425 #define _MIN(A, B) ((B) > (A) ? (A) : (B))
   426 #endif
   427 #ifndef _MAX
   428 #define _MAX(A, B) ((B) > (A) ? (B) : (A))
   429 #endif
   430 
   431 
   432 // These are globals used in generating message IDs and should only be
   433 // computed once, as they're either really constants or OS-dependent
   434 
   435 extern int _pEp_rand_max_bits;
   436 extern double _pEp_log2_36;
   437 
   438 static inline void _init_globals() {
   439     _pEp_rand_max_bits = (int) ceil(log2((double) RAND_MAX));
   440     _pEp_log2_36 = log2(36);
   441 }