src/pEp_internal.h
author Thomas
Tue, 25 Jun 2019 14:44:43 +0200
branchsequoia_windows
changeset 3871 d1d515455192
parent 3835 65f3c7d59eb1
parent 3870 018251280dbf
child 3888 99d255a8dc60
permissions -rw-r--r--
Merge with ENGINE-581
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #define PEP_ENGINE_VERSION "1.1.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 #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 10000
    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 KEYS_DB windoze_keys_db()
    59 #define LOCAL_DB windoze_local_db()
    60 #define SYSTEM_DB windoze_system_db()
    61 #define LIBGPGME "libgpgme-11.dll"
    62 #else // UNIX
    63 #define _POSIX_C_SOURCE 200809L
    64 #include <dlfcn.h>
    65 #ifdef NDEBUG
    66 #define LOCAL_DB unix_local_db()
    67 #else
    68 #define LOCAL_DB unix_local_db(false)
    69 #endif
    70 #ifndef SYSTEM_DB
    71 #define SYSTEM_DB "/usr/share/pEp/system.db"
    72 #endif
    73 #ifndef LIBGPGME
    74 #define LIBGPGME "libgpgme-pthread.so"
    75 #endif
    76 #endif
    77 
    78 #include <locale.h>
    79 #include <stdlib.h>
    80 #include <string.h>
    81 #include <assert.h>
    82 #include <stdio.h>
    83 #include <ctype.h>
    84 #include <math.h>
    85 
    86 #ifdef SQLITE3_FROM_OS
    87 #include <sqlite3.h>
    88 #else
    89 #include "sqlite3.h"
    90 #endif
    91 
    92 #include "pEpEngine.h"
    93 
    94 // If not specified, build for GPG
    95 #ifndef USE_SEQUOIA
    96 #ifndef USE_NETPGP
    97 #ifndef USE_GPG
    98 #define USE_GPG
    99 #endif
   100 #endif
   101 #endif
   102 
   103 #if defined(USE_SEQUOIA)
   104 #include "pgp_sequoia_internal.h"
   105 #elif defined(USE_NETPGP)
   106 #include "pgp_netpgp_internal.h"
   107 #elif defined(USE_GPG)
   108 #include "pgp_gpg_internal.h"
   109 #endif
   110 
   111 #include "keymanagement.h"
   112 #include "cryptotech.h"
   113 #include "transport.h"
   114 #include "sync_api.h"
   115 #include "Sync_func.h"
   116 
   117 #include "key_reset.h"
   118 
   119 #define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
   120 
   121 struct _pEpSession;
   122 typedef struct _pEpSession pEpSession;
   123 struct _pEpSession {
   124     const char *version;
   125     messageToSend_t messageToSend;
   126 
   127 #ifdef USE_GPG
   128     gpgme_ctx_t ctx;
   129 #elif defined(USE_NETPGP)
   130     pEpNetPGPSession ctx;
   131 #elif defined(USE_SEQUOIA)
   132     sqlite3 *key_db;
   133     struct {
   134         sqlite3_stmt *begin_transaction;
   135         sqlite3_stmt *begin_transaction_excl;
   136         sqlite3_stmt *commit_transaction;
   137         sqlite3_stmt *rollback_transaction;
   138         sqlite3_stmt *tpk_find;
   139         sqlite3_stmt *tsk_find;
   140         sqlite3_stmt *tpk_find_by_keyid;
   141         sqlite3_stmt *tsk_find_by_keyid;
   142         sqlite3_stmt *tpk_find_by_email;
   143         sqlite3_stmt *tsk_find_by_email;
   144         sqlite3_stmt *tpk_all;
   145         sqlite3_stmt *tsk_all;
   146         sqlite3_stmt *tpk_save_insert_primary;
   147         sqlite3_stmt *tpk_save_insert_subkeys;
   148         sqlite3_stmt *tpk_save_insert_userids;
   149         sqlite3_stmt *delete_keypair;
   150     } sq_sql;
   151 #endif
   152 
   153     PEP_cryptotech_t *cryptotech;
   154     PEP_CIPHER_SUITE cipher_suite;
   155 
   156     PEP_transport_t *transports;
   157 
   158     sqlite3 *db;
   159     sqlite3 *system_db;
   160 
   161     sqlite3_stmt *log;
   162     sqlite3_stmt *trustword;
   163     sqlite3_stmt *get_identity;
   164     sqlite3_stmt *get_identity_without_trust_check;
   165     sqlite3_stmt *get_identities_by_address;
   166     sqlite3_stmt *get_identities_by_userid;
   167     sqlite3_stmt *get_identities_by_main_key_id;
   168     sqlite3_stmt *replace_identities_fpr;
   169     sqlite3_stmt *replace_main_user_fpr;
   170     sqlite3_stmt *get_main_user_fpr;
   171     sqlite3_stmt *refresh_userid_default_key;
   172     sqlite3_stmt *delete_key;
   173     sqlite3_stmt *remove_fpr_as_default;
   174     sqlite3_stmt *set_person;
   175     sqlite3_stmt *update_person;
   176     sqlite3_stmt *delete_person;
   177     sqlite3_stmt *exists_person;    
   178     sqlite3_stmt *set_as_pEp_user;
   179     sqlite3_stmt *is_pEp_user;
   180     sqlite3_stmt *add_into_social_graph;
   181     sqlite3_stmt *get_own_address_binding_from_contact;
   182     sqlite3_stmt *set_revoke_contact_as_notified;
   183     sqlite3_stmt *get_contacted_ids_from_revoke_fpr;
   184     sqlite3_stmt *was_id_for_revoke_contacted;
   185     sqlite3_stmt *get_last_contacted;
   186     // sqlite3_stmt *set_device_group;
   187     // sqlite3_stmt *get_device_group;
   188     sqlite3_stmt *set_pgp_keypair;
   189     sqlite3_stmt *set_identity_entry;
   190     sqlite3_stmt *update_identity_entry;
   191     sqlite3_stmt *exists_identity_entry;        
   192     sqlite3_stmt *set_identity_flags;
   193     sqlite3_stmt *unset_identity_flags;
   194     sqlite3_stmt *set_trust;
   195     sqlite3_stmt *update_trust;
   196     sqlite3_stmt *exists_trust_entry;
   197     sqlite3_stmt *update_trust_to_pEp;
   198     sqlite3_stmt *update_trust_for_fpr;
   199     sqlite3_stmt *get_trust;
   200     sqlite3_stmt *get_trust_by_userid;
   201     sqlite3_stmt *least_trust;
   202     sqlite3_stmt *mark_compromised;
   203     sqlite3_stmt *reset_trust;
   204     sqlite3_stmt *crashdump;
   205     sqlite3_stmt *languagelist;
   206     sqlite3_stmt *i18n_token;
   207     sqlite3_stmt *replace_userid;
   208 
   209     // blacklist
   210     sqlite3_stmt *blacklist_add;
   211     sqlite3_stmt *blacklist_delete;
   212     sqlite3_stmt *blacklist_is_listed;
   213     sqlite3_stmt *blacklist_retrieve;
   214     
   215     // Keys
   216     sqlite3_stmt *own_key_is_listed;
   217     sqlite3_stmt *is_own_address;
   218     sqlite3_stmt *own_identities_retrieve;
   219     sqlite3_stmt *own_keys_retrieve;
   220     sqlite3_stmt *get_user_default_key;
   221     sqlite3_stmt *get_all_keys_for_user;
   222         
   223     sqlite3_stmt *get_default_own_userid;
   224 
   225 
   226 //    sqlite3_stmt *set_own_key;
   227 
   228     // sequence value
   229     sqlite3_stmt *sequence_value1;
   230     sqlite3_stmt *sequence_value2;
   231 
   232     // revoked keys
   233     sqlite3_stmt *set_revoked;
   234     sqlite3_stmt *get_revoked;
   235     sqlite3_stmt *get_replacement_fpr;
   236 
   237     // mistrusted
   238     sqlite3_stmt* add_mistrusted_key;
   239     sqlite3_stmt* is_mistrusted_key;    
   240     sqlite3_stmt* delete_mistrusted_key;
   241     
   242     // aliases
   243     sqlite3_stmt *get_userid_alias_default;
   244     sqlite3_stmt *add_userid_alias;
   245 
   246     // callbacks
   247     examine_identity_t examine_identity;
   248     void *examine_management;
   249     notifyHandshake_t notifyHandshake;
   250     inject_sync_event_t inject_sync_event;
   251     retrieve_next_sync_event_t retrieve_next_sync_event;
   252 
   253     // pEp Sync
   254     void *sync_management;
   255     void *sync_obj;
   256     struct Sync_state_s sync_state;
   257 
   258 //     void* sync_state_payload;
   259 //     char sync_uuid[37];
   260 //     time_t LastCannotDecrypt;
   261 //     time_t LastUpdateRequest;
   262 
   263     // runtime config
   264 
   265     bool passive_mode;
   266     bool unencrypted_subject;
   267     bool service_log;
   268     
   269 #ifdef DEBUG_ERRORSTACK
   270     stringlist_t* errorstack;
   271 #endif
   272 };
   273 
   274 
   275 PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
   276 void release_transport_system(PEP_SESSION session, bool out_last);
   277 
   278 /* NOT to be exposed to the outside!!! */
   279 PEP_STATUS encrypt_only(
   280         PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
   281         size_t psize, char **ctext, size_t *csize
   282 );
   283 
   284 #if defined(NDEBUG) || defined(NOLOG)
   285 #define DEBUG_LOG(TITLE, ENTITY, DESC)
   286 #else
   287 #ifdef ANDROID
   288 #include <android/log.h>
   289 #define  LOG_MORE(...)  __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s :: %s ", __VA_ARGS__);
   290 #else
   291 #include <stdio.h>
   292 #define  LOG_MORE(...)  fprintf(stderr, "pEpEngine DEBUG_LOG('%s','%s','%s','%s')\n", __VA_ARGS__);
   293 #endif
   294 #define DEBUG_LOG(TITLE, ENTITY, DESC) {\
   295     log_event(session, (TITLE), (ENTITY), (DESC), "debug " __FILE__ ":" S_LINE);\
   296     LOG_MORE((TITLE), (ENTITY), (DESC), __FILE__ ":" S_LINE)\
   297 }
   298 #endif
   299 
   300 typedef enum _normalize_hex_rest_t {
   301     accept_hex,
   302     ignore_hex,
   303     reject_hex
   304 } normalize_hex_res_t;
   305 
   306 static inline normalize_hex_res_t _normalize_hex(char *hex) 
   307 {
   308     if (*hex >= '0' && *hex <= '9')
   309         return accept_hex;
   310 
   311     if (*hex >= 'A' && *hex <= 'F') {
   312         *hex += 'a' - 'A';
   313         return accept_hex;
   314     }
   315 
   316     if (*hex >= 'a' && *hex <= 'f') 
   317         return accept_hex;
   318 
   319     if (*hex == ' ') 
   320         return ignore_hex;
   321 
   322     return reject_hex;
   323 }
   324 
   325 // Space tolerant and case insensitive fingerprint string compare
   326 static inline PEP_STATUS _compare_fprs(
   327         const char* fpra,
   328         size_t fpras,
   329         const char* fprb,
   330         size_t fprbs,
   331         int* comparison)
   332 {
   333 
   334     size_t ai = 0;
   335     size_t bi = 0;
   336     size_t significant = 0;
   337     int _comparison = 0;
   338     const int _FULL_FINGERPRINT_LENGTH = 40;
   339    
   340     // First compare every non-ignored chars until an end is reached
   341     while(ai < fpras && bi < fprbs)
   342     {
   343         char fprac = fpra[ai];
   344         char fprbc = fprb[bi];
   345         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   346         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   347 
   348         if(fprah == reject_hex || fprbh == reject_hex)
   349             return PEP_ILLEGAL_VALUE;
   350 
   351         if ( fprah == ignore_hex )
   352         {
   353             ai++;
   354         }
   355         else if ( fprbh == ignore_hex )
   356         {
   357             bi++;
   358         }
   359         else
   360         {
   361             if(fprac != fprbc && _comparison == 0 )
   362             {
   363                 _comparison = fprac > fprbc ? 1 : -1;
   364             }
   365 
   366             significant++;
   367             ai++;
   368             bi++;
   369 
   370         } 
   371     }
   372 
   373     // Bail out if we didn't got enough significnt chars
   374     if (significant != _FULL_FINGERPRINT_LENGTH )
   375         return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   376 
   377     // Then purge remaining chars, all must be ignored chars
   378     while ( ai < fpras )
   379     {
   380         char fprac = fpra[ai];
   381         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   382         if( fprah == reject_hex )
   383             return PEP_ILLEGAL_VALUE;
   384         if ( fprah != ignore_hex )
   385             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   386         ai++;
   387     }
   388     while ( bi < fprbs )
   389     {
   390         char fprbc = fprb[bi];
   391         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   392         if( fprbh == reject_hex )
   393             return PEP_ILLEGAL_VALUE;
   394         if ( fprbh != ignore_hex )
   395             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   396         bi++;
   397     }
   398 
   399     *comparison = _comparison;
   400     return PEP_STATUS_OK;
   401 }
   402 
   403 static inline int _same_fpr(
   404         const char* fpra,
   405         size_t fpras,
   406         const char* fprb,
   407         size_t fprbs
   408     )
   409 {
   410     // illegal values are ignored, and considered not same.
   411     int comparison = 1;
   412 
   413     _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
   414 
   415     return comparison == 0;
   416 }
   417 
   418 // size is the length of the bytestr that's coming in. This is really only intended
   419 // for comparing two full strings. If charstr's length is different from bytestr_size,
   420 // we'll return a non-zero value.
   421 static inline int _unsigned_signed_strcmp(const unsigned char* bytestr, const char* charstr, int bytestr_size) {
   422     int charstr_len = strlen(charstr);
   423     if (charstr_len != bytestr_size)
   424         return -1; // we don't actually care except that it's non-zero
   425     return memcmp(bytestr, charstr, bytestr_size);
   426 }
   427 
   428 // This is just a horrible example of C type madness. UTF-8 made me do it.
   429 static inline char* _pEp_subj_copy() {
   430 #ifndef WIN32
   431     unsigned char pEpstr[] = PEP_SUBJ_STRING;
   432     void* retval = calloc(1, sizeof(unsigned char)*PEP_SUBJ_BYTELEN + 1);
   433     memcpy(retval, pEpstr, PEP_SUBJ_BYTELEN);
   434     return (char*)retval;
   435 #else
   436     return strdup("pEp");
   437 #endif
   438 }
   439 
   440 static inline bool is_me(PEP_SESSION session, pEp_identity* test_ident) {
   441     bool retval = false;
   442     if (test_ident && test_ident->user_id) {
   443         char* def_id = NULL;
   444         get_default_own_userid(session, &def_id);
   445         if (test_ident->me || 
   446             (def_id && strcmp(def_id, test_ident->user_id) == 0)) {
   447             retval = true;
   448         }
   449         free(def_id);
   450     }
   451     return retval;
   452 }
   453 
   454 #ifndef EMPTYSTR
   455 #define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
   456 #endif
   457 
   458 #ifndef IS_PGP_CT
   459 #define IS_PGP_CT(CT) (((CT) | PEP_ct_confirmed) == PEP_ct_OpenPGP)
   460 #endif
   461 
   462 #ifndef _MIN
   463 #define _MIN(A, B) ((B) > (A) ? (A) : (B))
   464 #endif
   465 #ifndef _MAX
   466 #define _MAX(A, B) ((B) > (A) ? (B) : (A))
   467 #endif
   468 
   469 
   470 // These are globals used in generating message IDs and should only be
   471 // computed once, as they're either really constants or OS-dependent
   472 
   473 extern int _pEp_rand_max_bits;
   474 extern double _pEp_log2_36;
   475 
   476 static inline void _init_globals() {
   477     _pEp_rand_max_bits = (int) ceil(log2((double) RAND_MAX));
   478     _pEp_log2_36 = log2(36);
   479 }
   480 
   481 // spinlock implementation
   482 
   483 static inline int Sqlite3_step(sqlite3_stmt* stmt)
   484 {
   485     int rc;
   486     do {
   487         rc = sqlite3_step(stmt);
   488     } while (rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
   489     return rc;
   490 }