src/pEp_internal.h
author Neal H. Walfield <neal@pep.foundation>
Sat, 01 Jun 2019 10:54:32 +0200
branchsync
changeset 3797 657dc02b1cc3
parent 3772 f4d6efa63ee1
child 3828 8b28b328fd1b
child 3835 65f3c7d59eb1
child 3840 bfef390b48e2
child 3870 018251280dbf
permissions -rw-r--r--
sequoia: Improve pgp_delete_keypair

- Execute the SQL in the same way as everywhere else.

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