src/pEp_internal.h
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Sun, 22 Sep 2019 21:41:35 +0200
branchENGINE-633
changeset 4079 da3b0627a59c
parent 4065 7f0cf93bc2af
permissions -rw-r--r--
No idea of any of this WORKS, but it doesn't break anything OLD.
     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 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 "hkps://keys.openpgp.org"
    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 #ifndef X_PEP_MSG_WRAP_KEY
    55 #define X_PEP_MSG_WRAP_KEY "X-pEp-Wrapped-Message-Info"
    56 #endif
    57 
    58 #ifndef X_PEP_SNDR_FPR_KEY
    59 #define X_PEP_SNDR_FPR_KEY "X-pEp-Sender-FPR"
    60 #endif
    61  
    62 #include "platform.h"
    63 
    64 #ifdef WIN32
    65 #define KEYS_DB windoze_keys_db()
    66 #define LOCAL_DB windoze_local_db()
    67 #define SYSTEM_DB windoze_system_db()
    68 #define LIBGPGME "libgpgme-11.dll"
    69 #else // UNIX
    70 #define _POSIX_C_SOURCE 200809L
    71 #include <dlfcn.h>
    72 #ifdef NDEBUG
    73 #define LOCAL_DB unix_local_db()
    74 #else
    75 #define LOCAL_DB unix_local_db(false)
    76 #endif
    77 #ifdef ANDROID
    78 #define SYSTEM_DB android_system_db()
    79 #else
    80 #define SYSTEM_DB unix_system_db()
    81 #endif
    82 #ifndef LIBGPGME
    83 #define LIBGPGME "libgpgme-pthread.so"
    84 #endif
    85 #endif
    86 
    87 #include <locale.h>
    88 #include <stdlib.h>
    89 #include <string.h>
    90 #include <assert.h>
    91 #include <stdio.h>
    92 #include <ctype.h>
    93 #include <math.h>
    94 
    95 #ifdef SQLITE3_FROM_OS
    96 #include <sqlite3.h>
    97 #else
    98 #include "sqlite3.h"
    99 #endif
   100 
   101 #include "pEpEngine.h"
   102 
   103 // If not specified, build for GPG
   104 #ifndef USE_SEQUOIA
   105 #ifndef USE_NETPGP
   106 #ifndef USE_GPG
   107 #define USE_GPG
   108 #endif
   109 #endif
   110 #endif
   111 
   112 #if defined(USE_SEQUOIA)
   113 #include "pgp_sequoia_internal.h"
   114 #elif defined(USE_NETPGP)
   115 #include "pgp_netpgp_internal.h"
   116 #elif defined(USE_GPG)
   117 #include "pgp_gpg_internal.h"
   118 #endif
   119 
   120 #include "keymanagement.h"
   121 #include "cryptotech.h"
   122 #include "transport.h"
   123 #include "sync_api.h"
   124 #include "Sync_func.h"
   125 
   126 #include "key_reset.h"
   127 
   128 #define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
   129 
   130 struct _pEpSession;
   131 typedef struct _pEpSession pEpSession;
   132 struct _pEpSession {
   133     const char *version;
   134     messageToSend_t messageToSend;
   135 
   136 #ifdef USE_GPG
   137     gpgme_ctx_t ctx;
   138 #elif defined(USE_NETPGP)
   139     pEpNetPGPSession ctx;
   140 #elif defined(USE_SEQUOIA)
   141     sqlite3 *key_db;
   142     struct {
   143         sqlite3_stmt *begin_transaction;
   144         sqlite3_stmt *commit_transaction;
   145         sqlite3_stmt *rollback_transaction;
   146         sqlite3_stmt *tpk_find;
   147         sqlite3_stmt *tsk_find;
   148         sqlite3_stmt *tpk_find_by_keyid;
   149         sqlite3_stmt *tsk_find_by_keyid;
   150         sqlite3_stmt *tpk_find_by_email;
   151         sqlite3_stmt *tsk_find_by_email;
   152         sqlite3_stmt *tpk_all;
   153         sqlite3_stmt *tsk_all;
   154         sqlite3_stmt *tpk_save_insert_primary;
   155         sqlite3_stmt *tpk_save_insert_subkeys;
   156         sqlite3_stmt *tpk_save_insert_userids;
   157         sqlite3_stmt *delete_keypair;
   158     } sq_sql;
   159 #endif
   160 
   161     PEP_cryptotech_t *cryptotech;
   162     PEP_CIPHER_SUITE cipher_suite;
   163 
   164     PEP_transport_t *transports;
   165 
   166     sqlite3 *db;
   167     sqlite3 *system_db;
   168 
   169     sqlite3_stmt *log;
   170     sqlite3_stmt *trustword;
   171     sqlite3_stmt *get_identity;
   172     sqlite3_stmt *get_identity_without_trust_check;
   173     sqlite3_stmt *get_identities_by_address;
   174     sqlite3_stmt *get_identities_by_userid;
   175     sqlite3_stmt *get_identities_by_main_key_id;
   176     sqlite3_stmt *replace_identities_fpr;
   177     sqlite3_stmt *replace_main_user_fpr;
   178     sqlite3_stmt *get_main_user_fpr;
   179     sqlite3_stmt *refresh_userid_default_key;
   180     sqlite3_stmt *delete_key;
   181     sqlite3_stmt *remove_fpr_as_default;
   182     sqlite3_stmt *set_person;
   183     sqlite3_stmt *update_person;
   184     sqlite3_stmt *delete_person;
   185     sqlite3_stmt *exists_person;    
   186     sqlite3_stmt *set_as_pEp_user;
   187     sqlite3_stmt *is_pEp_user;
   188     sqlite3_stmt *upgrade_pEp_version_by_user_id;
   189     sqlite3_stmt *add_into_social_graph;
   190     sqlite3_stmt *get_own_address_binding_from_contact;
   191     sqlite3_stmt *set_revoke_contact_as_notified;
   192     sqlite3_stmt *get_contacted_ids_from_revoke_fpr;
   193     sqlite3_stmt *was_id_for_revoke_contacted;
   194     sqlite3_stmt *get_last_contacted;
   195     // sqlite3_stmt *set_device_group;
   196     // sqlite3_stmt *get_device_group;
   197     sqlite3_stmt *set_pgp_keypair;
   198     sqlite3_stmt *set_identity_entry;
   199     sqlite3_stmt *update_identity_entry;
   200     sqlite3_stmt *exists_identity_entry;        
   201     sqlite3_stmt *set_identity_flags;
   202     sqlite3_stmt *unset_identity_flags;
   203     sqlite3_stmt *set_pEp_version;    
   204     sqlite3_stmt *set_trust;
   205     sqlite3_stmt *update_trust;
   206     sqlite3_stmt *exists_trust_entry;
   207     sqlite3_stmt *update_trust_to_pEp;
   208     sqlite3_stmt *update_trust_for_fpr;
   209     sqlite3_stmt *get_trust;
   210     sqlite3_stmt *get_trust_by_userid;
   211     sqlite3_stmt *least_trust;
   212     sqlite3_stmt *mark_compromised;
   213     sqlite3_stmt *reset_trust;
   214     sqlite3_stmt *crashdump;
   215     sqlite3_stmt *languagelist;
   216     sqlite3_stmt *i18n_token;
   217     sqlite3_stmt *replace_userid;
   218 
   219     // blacklist
   220     sqlite3_stmt *blacklist_add;
   221     sqlite3_stmt *blacklist_delete;
   222     sqlite3_stmt *blacklist_is_listed;
   223     sqlite3_stmt *blacklist_retrieve;
   224     
   225     // Keys
   226     sqlite3_stmt *own_key_is_listed;
   227     sqlite3_stmt *is_own_address;
   228     sqlite3_stmt *own_identities_retrieve;
   229     sqlite3_stmt *own_keys_retrieve;
   230     sqlite3_stmt *get_user_default_key;
   231     sqlite3_stmt *get_all_keys_for_user;
   232         
   233     sqlite3_stmt *get_default_own_userid;
   234 
   235 
   236 //    sqlite3_stmt *set_own_key;
   237 
   238     // sequence value
   239     sqlite3_stmt *sequence_value1;
   240     sqlite3_stmt *sequence_value2;
   241 
   242     // revoked keys
   243     sqlite3_stmt *set_revoked;
   244     sqlite3_stmt *get_revoked;
   245     sqlite3_stmt *get_replacement_fpr;
   246 
   247     // mistrusted
   248     sqlite3_stmt* add_mistrusted_key;
   249     sqlite3_stmt* is_mistrusted_key;    
   250     sqlite3_stmt* delete_mistrusted_key;
   251     
   252     // aliases
   253     sqlite3_stmt *get_userid_alias_default;
   254     sqlite3_stmt *add_userid_alias;
   255 
   256     // callbacks
   257     examine_identity_t examine_identity;
   258     void *examine_management;
   259     notifyHandshake_t notifyHandshake;
   260     inject_sync_event_t inject_sync_event;
   261     retrieve_next_sync_event_t retrieve_next_sync_event;
   262 
   263     // pEp Sync
   264     void *sync_management;
   265     void *sync_obj;
   266     struct Sync_state_s sync_state;
   267 
   268 //     void* sync_state_payload;
   269 //     char sync_uuid[37];
   270 //     time_t LastCannotDecrypt;
   271 //     time_t LastUpdateRequest;
   272 
   273     // runtime config
   274 
   275     bool passive_mode;
   276     bool unencrypted_subject;
   277     bool service_log;
   278     
   279 #ifdef DEBUG_ERRORSTACK
   280     stringlist_t* errorstack;
   281 #endif
   282 };
   283 
   284 
   285 PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first);
   286 void release_transport_system(PEP_SESSION session, bool out_last);
   287 
   288 /* NOT to be exposed to the outside!!! */
   289 PEP_STATUS encrypt_only(
   290         PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
   291         size_t psize, char **ctext, size_t *csize
   292 );
   293 
   294 void decorate_message(
   295     message *msg,
   296     PEP_rating rating,
   297     stringlist_t *keylist,
   298     bool add_version,
   299     bool clobber);
   300 
   301 PEP_STATUS _import_key_with_fpr_return(
   302         PEP_SESSION session,
   303         const char *key_data,
   304         size_t size,
   305         identity_list **private_idents,
   306         stringlist_t** imported_keys);
   307 
   308 #if defined(NDEBUG) || defined(NOLOG)
   309 #define DEBUG_LOG(TITLE, ENTITY, DESC)
   310 #else
   311 #ifdef ANDROID
   312 #include <android/log.h>
   313 #define  LOG_MORE(...)  __android_log_print(ANDROID_LOG_DEBUG, "pEpEngine", " %s :: %s :: %s :: %s ", __VA_ARGS__);
   314 #else
   315 #include <stdio.h>
   316 #define  LOG_MORE(...)  fprintf(stderr, "pEpEngine DEBUG_LOG('%s','%s','%s','%s')\n", __VA_ARGS__);
   317 #endif
   318 #define DEBUG_LOG(TITLE, ENTITY, DESC) {\
   319     log_event(session, (TITLE), (ENTITY), (DESC), "debug " __FILE__ ":" S_LINE);\
   320     LOG_MORE((TITLE), (ENTITY), (DESC), __FILE__ ":" S_LINE)\
   321 }
   322 #endif
   323 
   324 typedef enum _normalize_hex_rest_t {
   325     accept_hex,
   326     ignore_hex,
   327     reject_hex
   328 } normalize_hex_res_t;
   329 
   330 static inline normalize_hex_res_t _normalize_hex(char *hex) 
   331 {
   332     if (*hex >= '0' && *hex <= '9')
   333         return accept_hex;
   334 
   335     if (*hex >= 'A' && *hex <= 'F') {
   336         *hex += 'a' - 'A';
   337         return accept_hex;
   338     }
   339 
   340     if (*hex >= 'a' && *hex <= 'f') 
   341         return accept_hex;
   342 
   343     if (*hex == ' ') 
   344         return ignore_hex;
   345 
   346     return reject_hex;
   347 }
   348 
   349 // Space tolerant and case insensitive fingerprint string compare
   350 static inline PEP_STATUS _compare_fprs(
   351         const char* fpra,
   352         size_t fpras,
   353         const char* fprb,
   354         size_t fprbs,
   355         int* comparison)
   356 {
   357 
   358     size_t ai = 0;
   359     size_t bi = 0;
   360     size_t significant = 0;
   361     int _comparison = 0;
   362     const int _FULL_FINGERPRINT_LENGTH = 40;
   363    
   364     // First compare every non-ignored chars until an end is reached
   365     while(ai < fpras && bi < fprbs)
   366     {
   367         char fprac = fpra[ai];
   368         char fprbc = fprb[bi];
   369         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   370         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   371 
   372         if(fprah == reject_hex || fprbh == reject_hex)
   373             return PEP_ILLEGAL_VALUE;
   374 
   375         if ( fprah == ignore_hex )
   376         {
   377             ai++;
   378         }
   379         else if ( fprbh == ignore_hex )
   380         {
   381             bi++;
   382         }
   383         else
   384         {
   385             if(fprac != fprbc && _comparison == 0 )
   386             {
   387                 _comparison = fprac > fprbc ? 1 : -1;
   388             }
   389 
   390             significant++;
   391             ai++;
   392             bi++;
   393 
   394         } 
   395     }
   396 
   397     // Bail out if we didn't got enough significnt chars
   398     if (significant != _FULL_FINGERPRINT_LENGTH )
   399         return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   400 
   401     // Then purge remaining chars, all must be ignored chars
   402     while ( ai < fpras )
   403     {
   404         char fprac = fpra[ai];
   405         normalize_hex_res_t fprah = _normalize_hex(&fprac);
   406         if( fprah == reject_hex )
   407             return PEP_ILLEGAL_VALUE;
   408         if ( fprah != ignore_hex )
   409             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   410         ai++;
   411     }
   412     while ( bi < fprbs )
   413     {
   414         char fprbc = fprb[bi];
   415         normalize_hex_res_t fprbh = _normalize_hex(&fprbc);
   416         if( fprbh == reject_hex )
   417             return PEP_ILLEGAL_VALUE;
   418         if ( fprbh != ignore_hex )
   419             return PEP_TRUSTWORDS_FPR_WRONG_LENGTH;
   420         bi++;
   421     }
   422 
   423     *comparison = _comparison;
   424     return PEP_STATUS_OK;
   425 }
   426 
   427 static inline int _same_fpr(
   428         const char* fpra,
   429         size_t fpras,
   430         const char* fprb,
   431         size_t fprbs
   432     )
   433 {
   434     // illegal values are ignored, and considered not same.
   435     int comparison = 1;
   436 
   437     _compare_fprs(fpra, fpras, fprb, fprbs, &comparison);
   438 
   439     return comparison == 0;
   440 }
   441 
   442 // size is the length of the bytestr that's coming in. This is really only intended
   443 // for comparing two full strings. If charstr's length is different from bytestr_size,
   444 // we'll return a non-zero value.
   445 static inline int _unsigned_signed_strcmp(const unsigned char* bytestr, const char* charstr, int bytestr_size) {
   446     int charstr_len = strlen(charstr);
   447     if (charstr_len != bytestr_size)
   448         return -1; // we don't actually care except that it's non-zero
   449     return memcmp(bytestr, charstr, bytestr_size);
   450 }
   451 
   452 // This is just a horrible example of C type madness. UTF-8 made me do it.
   453 static inline char* _pEp_subj_copy() {
   454 #ifndef WIN32
   455     unsigned char pEpstr[] = PEP_SUBJ_STRING;
   456     void* retval = calloc(1, sizeof(unsigned char)*PEP_SUBJ_BYTELEN + 1);
   457     memcpy(retval, pEpstr, PEP_SUBJ_BYTELEN);
   458     return (char*)retval;
   459 #else
   460     return strdup("pEp");
   461 #endif
   462 }
   463 
   464 static inline bool is_me(PEP_SESSION session, pEp_identity* test_ident) {
   465     bool retval = false;
   466     if (test_ident && test_ident->user_id) {
   467         char* def_id = NULL;
   468         get_default_own_userid(session, &def_id);
   469         if (test_ident->me || 
   470             (def_id && strcmp(def_id, test_ident->user_id) == 0)) {
   471             retval = true;
   472         }
   473         free(def_id);
   474     }
   475     return retval;
   476 }
   477 
   478 static inline float pEp_version_numeric(const char* version_str) {
   479     float retval = 0;    
   480         
   481     if (!version_str || sscanf(version_str, "%f", &retval) != 1)
   482         return 0;
   483         
   484     return retval;    
   485 }
   486 
   487 static inline void pEp_version_major_minor(const char* version_str, unsigned int* major, unsigned int* minor) {
   488     if (!major || !minor)
   489         return;
   490                 
   491     if (!version_str || sscanf(version_str, "%u.%u", major, minor) != 2) {
   492         *major = 0;
   493         *minor = 0;
   494     }
   495         
   496     return;    
   497 }
   498 
   499 static inline int compare_versions(unsigned int first_maj, unsigned int first_min,
   500                                    unsigned int second_maj, unsigned int second_min) {
   501     if (first_maj > second_maj)
   502         return 1;
   503     if (first_maj < second_maj)
   504         return -1;
   505     if (first_min > second_min)
   506         return 1;
   507     if (first_min < second_min)
   508         return -1;
   509     return 0;    
   510 }
   511 
   512 static inline void set_min_version(unsigned int first_maj, unsigned int first_minor,
   513                                    unsigned int second_maj, unsigned int second_minor,
   514                                    unsigned int* result_maj, unsigned int* result_minor) {
   515     int result = compare_versions(first_maj, first_minor, second_maj, second_minor);
   516     if (result < 0) {
   517         *result_maj = first_maj;
   518         *result_minor = first_minor;
   519     }
   520     else {
   521         *result_maj = second_maj;
   522         *result_minor = second_minor;
   523     }    
   524 }
   525 
   526 static inline void set_max_version(unsigned int first_maj, unsigned int first_minor,
   527                                    unsigned int second_maj, unsigned int second_minor,
   528                                    unsigned int* result_maj, unsigned int* result_minor) {
   529     int result = compare_versions(first_maj, first_minor, second_maj, second_minor);
   530     if (result > 0) {
   531         *result_maj = first_maj;
   532         *result_minor = first_minor;
   533     }
   534     else {
   535         *result_maj = second_maj;
   536         *result_minor = second_minor;
   537     }    
   538 }
   539 
   540 #ifndef EMPTYSTR
   541 #define EMPTYSTR(STR) ((STR) == NULL || (STR)[0] == '\0')
   542 #endif
   543 
   544 #ifndef IS_PGP_CT
   545 #define IS_PGP_CT(CT) (((CT) | PEP_ct_confirmed) == PEP_ct_OpenPGP)
   546 #endif
   547 
   548 #ifndef _MIN
   549 #define _MIN(A, B) ((B) > (A) ? (A) : (B))
   550 #endif
   551 #ifndef _MAX
   552 #define _MAX(A, B) ((B) > (A) ? (B) : (A))
   553 #endif
   554 
   555 // These are globals used in generating message IDs and should only be
   556 // computed once, as they're either really constants or OS-dependent
   557 
   558 extern int _pEp_rand_max_bits;
   559 extern double _pEp_log2_36;
   560 
   561 static inline void _init_globals() {
   562     _pEp_rand_max_bits = (int) ceil(log2((double) RAND_MAX));
   563     _pEp_log2_36 = log2(36);
   564 }
   565 
   566 // spinlock implementation
   567 
   568 static inline int Sqlite3_step(sqlite3_stmt* stmt)
   569 {
   570     int rc;
   571     do {
   572         rc = sqlite3_step(stmt);
   573     } while (rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
   574     return rc;
   575 }
   576 
   577 static inline const char* get_micalg_string(PEP_HASH_ALGO micalg) {
   578     switch (micalg) {
   579         case MD5:
   580             return "pgp-md5";
   581         case SHA1:    
   582             return "pgp-sha1";
   583         case RMD160:
   584             return "pgp-ripemd160";
   585         case MD2:    
   586             return "pgp-md2";
   587         case TIGER:    
   588             return "pgp-tiger192";
   589         case HAVAL:    
   590             return "pgp-haval";
   591         case SHA256:    
   592             return "pgp-sha256";
   593         case SHA384:    
   594             return "pgp-sha384";
   595         case SHA512:    
   596             return "pgp-sha512";
   597         case SHA224:    
   598             return "pgp-sha224";
   599         case MD4:                
   600             return "pgp-md4";
   601         case CRC32:    
   602             return "pgp-crc32";
   603         case CRC32_RFC1510:    
   604             return "pgp-crc32rfc1510";
   605         case CRC32_RFC2440:    
   606             return "pgp-crc24rfc2440";
   607         default:
   608             return NULL;
   609     }
   610 }