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