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