src/pEpEngine.c
author Krista Grothoff <krista@pep-project.org>
Thu, 29 Sep 2016 13:03:56 +0200
branchENGINE-27
changeset 1223 7bc623f2b384
parent 1222 31d0844f3a18
child 1232 8ca2cf56f8af
child 1241 4e6ff02a9257
permissions -rw-r--r--
ENGINE-27: set_identity can be called with a null/empty fpr now per Volker. ensured it does not call blacklist_is_listed on null/empty fprs
     1 #include "pEp_internal.h"
     2 #include "dynamic_api.h"
     3 #include "cryptotech.h"
     4 #include "transport.h"
     5 #include "blacklist.h"
     6 
     7 static int init_count = -1;
     8 
     9 static int user_version(void *_version, int count, char **text, char **name)
    10 {
    11     assert(_version);
    12     assert(count == 1);
    13     assert(text && text[0]);
    14     if (!(_version && count == 1 && text && text[0]))
    15         return -1;
    16 
    17     int *version = (int *) _version;
    18     *version = atoi(text[0]);
    19     return 0;
    20 }
    21 
    22 DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
    23 {
    24     PEP_STATUS status = PEP_STATUS_OK;
    25     int int_result;
    26     static const char *sql_log;
    27     static const char *sql_trustword;
    28     static const char *sql_get_identity;
    29     static const char *sql_set_person;
    30     static const char *sql_set_pgp_keypair;
    31     static const char *sql_set_identity;
    32     static const char *sql_set_identity_flags;
    33     static const char *sql_set_trust;
    34     static const char *sql_get_trust;
    35     static const char *sql_least_trust;
    36     static const char *sql_mark_as_compromized;
    37     static const char *sql_crashdump;
    38     static const char *sql_languagelist;
    39     static const char *sql_i18n_token;
    40 
    41     // blacklist
    42     static const char *sql_blacklist_add;
    43     static const char *sql_blacklist_delete;
    44     static const char *sql_blacklist_is_listed;
    45     static const char *sql_blacklist_retrieve;
    46 
    47     // Own keys
    48     static const char *sql_own_key_is_listed;
    49     static const char *sql_own_key_retrieve;
    50 
    51     // Sequence
    52     static const char *sql_sequence_value1;
    53     static const char *sql_sequence_value2;
    54 
    55     // Revocation tracking
    56     static const char *sql_set_revoked;
    57     static const char *sql_get_revoked;
    58     
    59     bool in_first = false;
    60 
    61     assert(sqlite3_threadsafe());
    62     if (!sqlite3_threadsafe())
    63         return PEP_INIT_SQLITE3_WITHOUT_MUTEX;
    64 
    65     // a little race condition - but still a race condition
    66     // mitigated by calling caveat (see documentation)
    67 
    68     ++init_count;
    69     if (init_count == 0)
    70         in_first = true;
    71 
    72     assert(session);
    73     if (session == NULL)
    74         return PEP_ILLEGAL_VALUE;
    75 
    76     *session = NULL;
    77 
    78     pEpSession *_session = calloc(1, sizeof(pEpSession));
    79     assert(_session);
    80     if (_session == NULL)
    81         goto enomem;
    82 
    83     _session->version = PEP_ENGINE_VERSION;
    84 
    85     assert(LOCAL_DB);
    86     if (LOCAL_DB == NULL) {
    87         status = PEP_INIT_CANNOT_OPEN_DB;
    88         goto pep_error;
    89     }
    90 
    91     int_result = sqlite3_open_v2(
    92             LOCAL_DB,
    93             &_session->db,
    94             SQLITE_OPEN_READWRITE
    95                 | SQLITE_OPEN_CREATE
    96                 | SQLITE_OPEN_FULLMUTEX
    97                 | SQLITE_OPEN_PRIVATECACHE,
    98             NULL 
    99         );
   100 
   101     if (int_result != SQLITE_OK) {
   102         status = PEP_INIT_CANNOT_OPEN_DB;
   103         goto pep_error;
   104     }
   105 
   106     sqlite3_busy_timeout(_session->db, BUSY_WAIT_TIME);
   107 
   108     assert(SYSTEM_DB);
   109     if (SYSTEM_DB == NULL) {
   110         status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
   111         goto pep_error;
   112     }
   113 
   114     int_result = sqlite3_open_v2(
   115             SYSTEM_DB, &_session->system_db,
   116             SQLITE_OPEN_READONLY
   117                 | SQLITE_OPEN_FULLMUTEX
   118                 | SQLITE_OPEN_SHAREDCACHE,
   119             NULL
   120         );
   121 
   122     if (int_result != SQLITE_OK) {
   123         status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
   124         goto pep_error;
   125     }
   126 
   127     sqlite3_busy_timeout(_session->system_db, 1000);
   128 
   129 // increment this when patching DDL
   130 #define _DDL_USER_VERSION "1"
   131 
   132     if (in_first) {
   133         int_result = sqlite3_exec(
   134             _session->db,
   135                 "create table version_info (\n"
   136                 "   id integer primary key,\n"
   137                 "   timestamp integer default (datetime('now')) ,\n"
   138                 "   version text,\n"
   139                 "   comment text\n"
   140                 ");\n",
   141                 NULL,
   142                 NULL,
   143                 NULL
   144         );
   145         if (int_result == SQLITE_OK) {
   146             int_result = sqlite3_exec(
   147                 _session->db,
   148                 "pragma user_version = "_DDL_USER_VERSION";\n"
   149                 "insert or replace into version_info (id, version)"
   150                     "values (1, '" PEP_ENGINE_VERSION "');",
   151                 NULL,
   152                 NULL,
   153                 NULL
   154             );
   155             assert(int_result == SQLITE_OK);
   156         }
   157 
   158         int_result = sqlite3_exec(
   159             _session->db,
   160                 "create table if not exists log (\n"
   161                 "   timestamp integer default (datetime('now')) ,\n"
   162                 "   title text not null,\n"
   163                 "   entity text not null,\n"
   164                 "   description text,\n"
   165                 "   comment text\n"
   166                 ");\n"
   167                 "create index if not exists log_timestamp on log (\n"
   168                 "   timestamp\n"
   169                 ");\n"
   170                 "create table if not exists pgp_keypair (\n"
   171                 "   fpr text primary key,\n"
   172                 "   public_id text unique,\n"
   173                 "   private_id text,\n"
   174                 "   created integer,\n"
   175                 "   expires integer,\n"
   176                 "   comment text\n"
   177                 ");\n"
   178                 "create index if not exists pgp_keypair_expires on pgp_keypair (\n"
   179                 "   expires\n"
   180                 ");\n"
   181                 "create table if not exists person (\n"
   182                 "   id text primary key,\n"
   183                 "   username text not null,\n"
   184                 "   main_key_id text\n"
   185                 "       references pgp_keypair (fpr)\n"
   186                 "       on delete set null,\n"
   187                 "   lang text,\n"
   188                 "   comment text\n"
   189                 ");\n"
   190                 "create table if not exists identity (\n"
   191                 "   address text,\n"
   192                 "   user_id text\n"
   193                 "       references person (id)\n"
   194                 "       on delete cascade,\n"
   195                 "   main_key_id text\n"
   196                 "       references pgp_keypair (fpr)\n"
   197                 "       on delete set null,\n"
   198                 "   comment text,\n"
   199                 "   flags integer default (0),"
   200                 "   primary key (address, user_id)\n"
   201                 ");\n"
   202                 "create table if not exists trust (\n"
   203                 "   user_id text not null\n"
   204                 "       references person (id)\n"
   205                 "       on delete cascade,\n"
   206                 "   pgp_keypair_fpr text not null\n"
   207                 "       references pgp_keypair (fpr)\n"
   208                 "       on delete cascade,\n"
   209                 "   comm_type integer not null,\n"
   210                 "   comment text,\n"
   211                 "   primary key (user_id, pgp_keypair_fpr)\n"
   212                 ");\n"
   213                 // blacklist
   214                 "create table if not exists blacklist_keys (\n"
   215                 "   fpr text primary key\n"
   216                 ");\n"
   217                 // sequences
   218                 "create table if not exists sequences(\n"
   219                 "   name text primary key,\n"
   220                 "   value integer default 0\n"
   221                 ");\n"
   222                 "create table if not exists revoked_keys (\n"
   223                 "   revoked_fpr text primary key,\n"
   224                 "   replacement_fpr text not null\n"
   225                 "       references pgp_keypair (fpr)\n"
   226                 "       on delete cascade,\n"
   227                 "   revocation_date integer\n"
   228                 ");\n"
   229                 ,
   230             NULL,
   231             NULL,
   232             NULL
   233         );
   234         assert(int_result == SQLITE_OK);
   235 
   236         int version;
   237         int_result = sqlite3_exec(
   238             _session->db,
   239             "pragma user_version;",
   240             user_version,
   241             &version,
   242             NULL
   243         );
   244         assert(int_result == SQLITE_OK);
   245 
   246         if (version < 1) {
   247             int_result = sqlite3_exec(
   248                 _session->db,
   249                 "alter table identity\n"
   250                 "   add column flags integer default (0);",
   251                 NULL,
   252                 NULL,
   253                 NULL
   254             );
   255             assert(int_result == SQLITE_OK);
   256         }
   257 
   258         if (version < atoi(_DDL_USER_VERSION)) {
   259             int_result = sqlite3_exec(
   260                 _session->db,
   261                 "pragma user_version = "_DDL_USER_VERSION";\n"
   262                 "insert or replace into version_info (id, version)"
   263                     "values (1, '" PEP_ENGINE_VERSION "');",
   264                 NULL,
   265                 NULL,
   266                 NULL
   267             );
   268             assert(int_result == SQLITE_OK);
   269         }
   270 
   271         sql_log = "insert into log (title, entity, description, comment)"
   272                   "values (?1, ?2, ?3, ?4);";
   273 
   274         sql_get_identity =  "select fpr, username, comm_type, lang, flags"
   275                             "   from identity"
   276                             "   join person on id = identity.user_id"
   277                             "   join pgp_keypair on fpr = identity.main_key_id"
   278                             "   join trust on id = trust.user_id"
   279                             "       and pgp_keypair_fpr = identity.main_key_id"
   280                             "   where address = ?1 and identity.user_id = ?2;";
   281 
   282         sql_trustword = "select id, word from wordlist where lang = lower(?1) "
   283                        "and id = ?2 ;";
   284 
   285         // Set person, but if already exist, only update.
   286         // if main_key_id already set, don't touch.
   287         sql_set_person = "insert or replace into person (id, username, lang, main_key_id)"
   288                          "  values (?1, ?2, ?3,"
   289                          "    (select coalesce((select main_key_id from person "
   290                          "      where id = ?1), upper(replace(?4,' ',''))))) ;";
   291 
   292         sql_set_pgp_keypair = "insert or replace into pgp_keypair (fpr) "
   293                               "values (upper(replace(?1,' ',''))) ;";
   294 
   295         sql_set_identity = "insert or replace into identity (address, main_key_id, "
   296                            "user_id, flags) values (?1, upper(replace(?2,' ','')),"
   297                            "?3, ?4) ;";
   298 
   299         sql_set_identity_flags = "update identity set flags = ?1 "
   300                                  "where address = ?2 and user_id = ?3 ;";
   301 
   302         sql_set_trust = "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type) "
   303                         "values (?1, upper(replace(?2,' ','')), ?3) ;";
   304 
   305         sql_get_trust = "select comm_type from trust where user_id = ?1 "
   306                         "and pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
   307 
   308         sql_least_trust = "select min(comm_type) from trust where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
   309 
   310         sql_mark_as_compromized = "update trust not indexed set comm_type = 15"
   311                                   " where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
   312 
   313         sql_crashdump = "select timestamp, title, entity, description, comment"
   314                         " from log order by timestamp desc limit ?1 ;";
   315 
   316         sql_languagelist = "select i18n_language.lang, name, phrase from i18n_language join i18n_token using (lang) where i18n_token.id = 1000;" ;
   317 
   318         sql_i18n_token = "select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
   319 
   320         // blacklist
   321 
   322         sql_blacklist_add = "insert or replace into blacklist_keys (fpr) values (upper(replace(?1,' ',''))) ;"
   323                             "delete from identity where main_key_id = upper(replace(?1,' ','')) ;"
   324                             "delete from pgp_keypair where fpr = upper(replace(?1,' ','')) ;";
   325 
   326         sql_blacklist_delete = "delete from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
   327 
   328         sql_blacklist_is_listed = "select count(*) from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
   329 
   330         sql_blacklist_retrieve = "select * from blacklist_keys ;";
   331         
   332         // Own keys
   333         
   334         sql_own_key_is_listed =
   335                                 "select count(*) from ("
   336                                 " select main_key_id from person "
   337                                 "   where main_key_id = upper(replace(?1,' ',''))"
   338                                 "    and id = '" PEP_OWN_USERID "' "
   339                                 " union "
   340                                 "  select main_key_id from identity "
   341                                 "   where main_key_id = upper(replace(?1,' ',''))"
   342                                 "    and user_id = '" PEP_OWN_USERID "' );";
   343 
   344         sql_own_key_retrieve = "select main_key_id from person "
   345                                "  where main_key_id is not null"
   346                                "   and id = '" PEP_OWN_USERID "' "
   347                                "union "
   348                                " select main_key_id from identity "
   349                                "  where main_key_id is not null"
   350                                "   and user_id = '" PEP_OWN_USERID "' ;";
   351         
   352         sql_sequence_value1 = "insert or replace into sequences (name, value) "
   353                               "values (?1, "
   354                               "(select coalesce((select value + 1 from sequences "
   355                               "where name = ?1), 1 ))) ; ";
   356         sql_sequence_value2 = "select value from sequences where name = ?1 ;";
   357         
   358         sql_set_revoked =     "insert or replace into revoked_keys ("
   359                               "    revoked_fpr, replacement_fpr, revocation_date) "
   360                               "values (upper(replace(?1,' ','')),"
   361                               "        upper(replace(?2,' ','')),"
   362                               "        ?3) ;";
   363         
   364         sql_get_revoked =     "select revoked_fpr, revocation_date from revoked_keys"
   365                               "    where replacement_fpr = upper(replace(?1,' ','')) ;";
   366     }
   367 
   368     int_result = sqlite3_prepare_v2(_session->db, sql_log, (int)strlen(sql_log),
   369             &_session->log, NULL);
   370     assert(int_result == SQLITE_OK);
   371 
   372     int_result = sqlite3_prepare_v2(_session->system_db, sql_trustword,
   373             (int)strlen(sql_trustword), &_session->trustword, NULL);
   374     assert(int_result == SQLITE_OK);
   375 
   376     int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
   377             (int)strlen(sql_get_identity), &_session->get_identity, NULL);
   378     assert(int_result == SQLITE_OK);
   379 
   380     int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
   381             (int)strlen(sql_set_person), &_session->set_person, NULL);
   382     assert(int_result == SQLITE_OK);
   383 
   384     int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
   385             (int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair, NULL);
   386     assert(int_result == SQLITE_OK);
   387 
   388     int_result = sqlite3_prepare_v2(_session->db, sql_set_identity,
   389             (int)strlen(sql_set_identity), &_session->set_identity, NULL);
   390     assert(int_result == SQLITE_OK);
   391 
   392     int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_flags,
   393             (int)strlen(sql_set_identity_flags), &_session->set_identity_flags, NULL);
   394     assert(int_result == SQLITE_OK);
   395 
   396     int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
   397             (int)strlen(sql_set_trust), &_session->set_trust, NULL);
   398     assert(int_result == SQLITE_OK);
   399 
   400     int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
   401             (int)strlen(sql_get_trust), &_session->get_trust, NULL);
   402     assert(int_result == SQLITE_OK);
   403 
   404     int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
   405             (int)strlen(sql_least_trust), &_session->least_trust, NULL);
   406     assert(int_result == SQLITE_OK);
   407 
   408     int_result = sqlite3_prepare_v2(_session->db, sql_mark_as_compromized,
   409             (int)strlen(sql_mark_as_compromized), &_session->mark_compromized, NULL);
   410     assert(int_result == SQLITE_OK);
   411 
   412     int_result = sqlite3_prepare_v2(_session->db, sql_crashdump,
   413             (int)strlen(sql_crashdump), &_session->crashdump, NULL);
   414     assert(int_result == SQLITE_OK);
   415 
   416     int_result = sqlite3_prepare_v2(_session->system_db, sql_languagelist,
   417             (int)strlen(sql_languagelist), &_session->languagelist, NULL);
   418     assert(int_result == SQLITE_OK);
   419 
   420     int_result = sqlite3_prepare_v2(_session->system_db, sql_i18n_token,
   421             (int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
   422     assert(int_result == SQLITE_OK);
   423 
   424     // blacklist
   425 
   426     int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_add,
   427             (int)strlen(sql_blacklist_add), &_session->blacklist_add, NULL);
   428     assert(int_result == SQLITE_OK);
   429 
   430     int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_delete,
   431             (int)strlen(sql_blacklist_delete), &_session->blacklist_delete, NULL);
   432     assert(int_result == SQLITE_OK);
   433 
   434     int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_is_listed,
   435             (int)strlen(sql_blacklist_is_listed), &_session->blacklist_is_listed, NULL);
   436     assert(int_result == SQLITE_OK);
   437 
   438     int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_retrieve,
   439             (int)strlen(sql_blacklist_retrieve), &_session->blacklist_retrieve, NULL);
   440     assert(int_result == SQLITE_OK);
   441 
   442     // Own keys
   443     
   444     int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
   445             (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed, NULL);
   446     assert(int_result == SQLITE_OK);
   447     
   448     int_result = sqlite3_prepare_v2(_session->db, sql_own_key_retrieve,
   449             (int)strlen(sql_own_key_retrieve), &_session->own_key_retrieve, NULL);
   450     assert(int_result == SQLITE_OK);
   451  
   452     // Sequence
   453 
   454     int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value1,
   455             (int)strlen(sql_sequence_value1), &_session->sequence_value1, NULL);
   456     assert(int_result == SQLITE_OK);
   457 
   458     int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value2,
   459             (int)strlen(sql_sequence_value2), &_session->sequence_value2, NULL);
   460     assert(int_result == SQLITE_OK);
   461 
   462     // Revocation tracking
   463     
   464     int_result = sqlite3_prepare_v2(_session->db, sql_set_revoked,
   465                                     (int)strlen(sql_set_revoked), &_session->set_revoked, NULL);
   466     assert(int_result == SQLITE_OK);
   467     
   468     int_result = sqlite3_prepare_v2(_session->db, sql_get_revoked,
   469                                     (int)strlen(sql_get_revoked), &_session->get_revoked, NULL);
   470     assert(int_result == SQLITE_OK);
   471     
   472     status = init_cryptotech(_session, in_first);
   473     if (status != PEP_STATUS_OK)
   474         goto pep_error;
   475 
   476     status = init_transport_system(_session, in_first);
   477     if (status != PEP_STATUS_OK)
   478         goto pep_error;
   479 
   480     status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
   481     if (status != PEP_STATUS_OK)
   482         goto pep_error;
   483 
   484     // runtime config
   485 
   486     _session->passive_mode = false;
   487     _session->unencrypted_subject = false;
   488 #ifdef ANDROID
   489     _session->use_only_own_private_keys = true;
   490 #elif TARGET_OS_IPHONE
   491     _session->use_only_own_private_keys = true;
   492 #else
   493     _session->use_only_own_private_keys = false;
   494 #endif
   495 
   496     *session = _session;
   497     return PEP_STATUS_OK;
   498 
   499 enomem:
   500     status = PEP_OUT_OF_MEMORY;
   501 
   502 pep_error:
   503     release(_session);
   504     return status;
   505 }
   506 
   507 DYNAMIC_API void release(PEP_SESSION session)
   508 {
   509     bool out_last = false;
   510 
   511     assert(init_count >= 0);
   512     assert(session);
   513 
   514     if (!((init_count >= 0) && session))
   515         return;
   516 
   517     // a small race condition but still a race condition
   518     // mitigated by calling caveat (see documentation)
   519 
   520     if (init_count == 0)
   521         out_last = true;
   522     --init_count;
   523 
   524     if (session) {
   525         if (session->db) {
   526             if (session->log)
   527                 sqlite3_finalize(session->log);
   528             if (session->trustword)
   529                 sqlite3_finalize(session->trustword);
   530             if (session->get_identity)
   531                 sqlite3_finalize(session->get_identity);
   532             if (session->set_person)
   533                 sqlite3_finalize(session->set_person);
   534             if (session->set_pgp_keypair)
   535                 sqlite3_finalize(session->set_pgp_keypair);
   536             if (session->set_identity)
   537                 sqlite3_finalize(session->set_identity);
   538             if (session->set_identity_flags)
   539                 sqlite3_finalize(session->set_identity_flags);
   540             if (session->set_trust)
   541                 sqlite3_finalize(session->set_trust);
   542             if (session->get_trust)
   543                 sqlite3_finalize(session->get_trust);
   544             if (session->least_trust)
   545                 sqlite3_finalize(session->least_trust);
   546             if (session->mark_compromized)
   547                 sqlite3_finalize(session->mark_compromized);
   548             if (session->crashdump)
   549                 sqlite3_finalize(session->crashdump);
   550             if (session->languagelist)
   551                 sqlite3_finalize(session->languagelist);
   552             if (session->i18n_token)
   553                 sqlite3_finalize(session->i18n_token);
   554             if (session->blacklist_add)
   555                 sqlite3_finalize(session->blacklist_add);
   556             if (session->blacklist_delete)
   557                 sqlite3_finalize(session->blacklist_delete);
   558             if (session->blacklist_is_listed)
   559                 sqlite3_finalize(session->blacklist_is_listed);
   560             if (session->blacklist_retrieve)
   561                 sqlite3_finalize(session->blacklist_retrieve);
   562             if (session->own_key_is_listed)
   563                 sqlite3_finalize(session->own_key_is_listed);
   564             if (session->own_key_retrieve)
   565                 sqlite3_finalize(session->own_key_retrieve);
   566             if (session->sequence_value1)
   567                 sqlite3_finalize(session->sequence_value1);
   568             if (session->sequence_value2)
   569                 sqlite3_finalize(session->sequence_value2);
   570             if (session->set_revoked)
   571                 sqlite3_finalize(session->set_revoked);
   572             if (session->get_revoked)
   573                 sqlite3_finalize(session->get_revoked);
   574 
   575             if (session->db)
   576                 sqlite3_close_v2(session->db);
   577             if (session->system_db)
   578                 sqlite3_close_v2(session->system_db);
   579         }
   580 
   581         release_transport_system(session, out_last);
   582         release_cryptotech(session, out_last);
   583 
   584         free(session);
   585     }
   586 }
   587 
   588 DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
   589 {
   590     assert(session);
   591     session->passive_mode = enable;
   592 }
   593 
   594 DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
   595 {
   596     assert(session);
   597     session->unencrypted_subject = enable;
   598 }
   599 
   600 DYNAMIC_API void config_use_only_own_private_keys(PEP_SESSION session, bool enable)
   601 {
   602     assert(session);
   603     session->use_only_own_private_keys = enable;
   604 }
   605 
   606 DYNAMIC_API PEP_STATUS log_event(
   607         PEP_SESSION session,
   608         const char *title,
   609         const char *entity,
   610         const char *description,
   611         const char *comment
   612     )
   613 {
   614     PEP_STATUS status = PEP_STATUS_OK;
   615     int result;
   616 
   617     assert(session);
   618     assert(title);
   619     assert(entity);
   620 
   621     if (!(session && title && entity))
   622         return PEP_ILLEGAL_VALUE;
   623 
   624     sqlite3_reset(session->log);
   625     sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
   626     sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
   627     if (description)
   628         sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
   629     else
   630         sqlite3_bind_null(session->log, 3);
   631     if (comment)
   632         sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
   633     else
   634         sqlite3_bind_null(session->log, 4);
   635     do {
   636         result = sqlite3_step(session->log);
   637         assert(result == SQLITE_DONE || result == SQLITE_BUSY);
   638         if (result != SQLITE_DONE && result != SQLITE_BUSY)
   639             status = PEP_UNKNOWN_ERROR;
   640     } while (result == SQLITE_BUSY);
   641     sqlite3_reset(session->log);
   642 
   643     return status;
   644 }
   645 
   646 DYNAMIC_API PEP_STATUS trustword(
   647             PEP_SESSION session, uint16_t value, const char *lang,
   648             char **word, size_t *wsize
   649         )
   650 {
   651     PEP_STATUS status = PEP_STATUS_OK;
   652 
   653     assert(session);
   654     assert(word);
   655     assert(wsize);
   656 
   657     if (!(session && word && wsize))
   658         return PEP_ILLEGAL_VALUE;
   659 
   660     *word = NULL;
   661     *wsize = 0;
   662 
   663     if (lang == NULL)
   664         lang = "en";
   665 
   666     assert((lang[0] >= 'A' && lang[0] <= 'Z')
   667             || (lang[0] >= 'a' && lang[0] <= 'z'));
   668     assert((lang[1] >= 'A' && lang[1] <= 'Z')
   669             || (lang[1] >= 'a' && lang[1] <= 'z'));
   670     assert(lang[2] == 0);
   671 
   672     sqlite3_reset(session->trustword);
   673     sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
   674     sqlite3_bind_int(session->trustword, 2, value);
   675 
   676     const int result = sqlite3_step(session->trustword);
   677     if (result == SQLITE_ROW) {
   678         *word = strdup((const char *) sqlite3_column_text(session->trustword,
   679                     1));
   680         if (*word)
   681             *wsize = sqlite3_column_bytes(session->trustword, 1);
   682         else
   683             status = PEP_OUT_OF_MEMORY;
   684     } else
   685         status = PEP_TRUSTWORD_NOT_FOUND;
   686 
   687     sqlite3_reset(session->trustword);
   688     return status;
   689 }
   690 
   691 DYNAMIC_API PEP_STATUS trustwords(
   692         PEP_SESSION session, const char *fingerprint, const char *lang,
   693         char **words, size_t *wsize, int max_words
   694     )
   695 {
   696     const char *source = fingerprint;
   697     char *buffer;
   698     char *dest;
   699     size_t fsize;
   700     PEP_STATUS _status;
   701 
   702     assert(session);
   703     assert(fingerprint);
   704     assert(words);
   705     assert(wsize);
   706     assert(max_words >= 0);
   707 
   708     if (!(session && fingerprint && words && wsize && max_words >= 0))
   709         return PEP_ILLEGAL_VALUE;
   710 
   711     *words = NULL;
   712     *wsize = 0;
   713 
   714     buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
   715     assert(buffer);
   716     if (buffer == NULL)
   717         return PEP_OUT_OF_MEMORY;
   718     dest = buffer;
   719 
   720     fsize = strlen(fingerprint);
   721 
   722     if (!lang || !lang[0])
   723         lang = "en";
   724 
   725     assert((lang[0] >= 'A' && lang[0] <= 'Z')
   726             || (lang[0] >= 'a' && lang[0] <= 'z'));
   727     assert((lang[1] >= 'A' && lang[1] <= 'Z')
   728             || (lang[1] >= 'a' && lang[1] <= 'z'));
   729     assert(lang[2] == 0);
   730 
   731     int n_words = 0;
   732     while (source < fingerprint + fsize) {
   733         uint16_t value;
   734         char *word;
   735         size_t _wsize;
   736         int j;
   737 
   738         for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
   739             if (*source >= 'a' && *source <= 'f')
   740                 value += (*source - 'a' + 10) << (3 - j++) * 4;
   741             else if (*source >= 'A' && *source <= 'F')
   742                 value += (*source - 'A' + 10) << (3 - j++) * 4;
   743             else if (*source >= '0' && *source <= '9')
   744                 value += (*source - '0') << (3 - j++) * 4;
   745             
   746             source++;
   747         }
   748 
   749         _status = trustword(session, value, lang, &word, &_wsize);
   750         if (_status == PEP_OUT_OF_MEMORY) {
   751             free(buffer);
   752             return PEP_OUT_OF_MEMORY;
   753         }
   754         if (word == NULL) {
   755             free(buffer);
   756             return PEP_TRUSTWORD_NOT_FOUND;
   757         }
   758 
   759         if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
   760             strncpy(dest, word, _wsize);
   761             free(word);
   762             dest += _wsize;
   763         }
   764         else {
   765             free(word);
   766             break; // buffer full
   767         }
   768 
   769         if (source < fingerprint + fsize
   770                 && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
   771             *dest++ = ' ';
   772 
   773         ++n_words;
   774         if (max_words && n_words >= max_words)
   775             break;
   776     }
   777 
   778     *words = buffer;
   779     *wsize = dest - buffer;
   780     return PEP_STATUS_OK;
   781 }
   782 
   783 pEp_identity *new_identity(
   784         const char *address, const char *fpr, const char *user_id,
   785         const char *username
   786     )
   787 {
   788     pEp_identity *result = calloc(1, sizeof(pEp_identity));
   789     assert(result);
   790     if (result) {
   791         if (address) {
   792             result->address = strdup(address);
   793             assert(result->address);
   794             if (result->address == NULL) {
   795                 free(result);
   796                 return NULL;
   797             }
   798         }
   799         if (fpr) {
   800             result->fpr = strdup(fpr);
   801             assert(result->fpr);
   802             if (result->fpr == NULL) {
   803                 free_identity(result);
   804                 return NULL;
   805             }
   806         }
   807         if (user_id) {
   808             result->user_id = strdup(user_id);
   809             assert(result->user_id);
   810             if (result->user_id == NULL) {
   811                 free_identity(result);
   812                 return NULL;
   813             }
   814         }
   815         if (username) {
   816             result->username = strdup(username);
   817             assert(result->username);
   818             if (result->username == NULL) {
   819                 free_identity(result);
   820                 return NULL;
   821             }
   822         }
   823     }
   824     return result;
   825 }
   826 
   827 pEp_identity *identity_dup(const pEp_identity *src)
   828 {
   829     assert(src);
   830 
   831     pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
   832     assert(dup);
   833     if (dup == NULL)
   834         return NULL;
   835     
   836     dup->comm_type = src->comm_type;
   837     dup->lang[0] = src->lang[0];
   838     dup->lang[1] = src->lang[1];
   839     dup->lang[2] = 0;
   840     dup->me = src->me;
   841     dup->flags = src->flags;
   842 
   843     return dup;
   844 }
   845 
   846 void free_identity(pEp_identity *identity)
   847 {
   848     if (identity) {
   849         free(identity->address);
   850         free(identity->fpr);
   851         free(identity->user_id);
   852         free(identity->username);
   853         free(identity);
   854     }
   855 }
   856 
   857 DYNAMIC_API PEP_STATUS get_identity(
   858         PEP_SESSION session,
   859         const char *address,
   860         const char *user_id,
   861         pEp_identity **identity
   862     )
   863 {
   864     PEP_STATUS status = PEP_STATUS_OK;
   865     static pEp_identity *_identity;
   866 
   867     assert(session);
   868     assert(address);
   869     assert(address[0]);
   870     assert(identity);
   871 
   872     if (!(session && address && address[0] && identity))
   873         return PEP_ILLEGAL_VALUE;
   874 
   875     *identity = NULL;
   876 
   877     sqlite3_reset(session->get_identity);
   878     sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
   879     sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
   880 
   881     const int result = sqlite3_step(session->get_identity);
   882     switch (result) {
   883     case SQLITE_ROW:
   884         _identity = new_identity(
   885                 address,
   886                 (const char *) sqlite3_column_text(session->get_identity, 0),
   887                 user_id,
   888                 (const char *) sqlite3_column_text(session->get_identity, 1)
   889                 );
   890         assert(_identity);
   891         if (_identity == NULL)
   892             return PEP_OUT_OF_MEMORY;
   893 
   894         _identity->comm_type = (PEP_comm_type) sqlite3_column_int(session->get_identity, 2);
   895         const char* const _lang = (const char *) sqlite3_column_text(session->get_identity, 3);
   896         if (_lang && _lang[0]) {
   897             assert(_lang[0] >= 'a' && _lang[0] <= 'z');
   898             assert(_lang[1] >= 'a' && _lang[1] <= 'z');
   899             assert(_lang[2] == 0);
   900             _identity->lang[0] = _lang[0];
   901             _identity->lang[1] = _lang[1];
   902             _identity->lang[2] = 0;
   903         }
   904         _identity->flags = (unsigned int) sqlite3_column_int(session->get_identity, 4);
   905         *identity = _identity;
   906         break;
   907     default:
   908         status = PEP_CANNOT_FIND_IDENTITY;
   909         *identity = NULL;
   910     }
   911 
   912     sqlite3_reset(session->get_identity);
   913     return status;
   914 }
   915 
   916 DYNAMIC_API PEP_STATUS set_identity(
   917         PEP_SESSION session, const pEp_identity *identity
   918     )
   919 {
   920     int result;
   921 
   922     assert(session);
   923     assert(identity);
   924     assert(identity->address);
   925     assert(identity->user_id);
   926     assert(identity->username);
   927 
   928     if (!(session && identity && identity->address &&
   929                 identity->user_id && identity->username))
   930         return PEP_ILLEGAL_VALUE;
   931 
   932     bool listed;
   933     
   934     if (identity->fpr && identity->fpr[0] != '\0') {
   935         PEP_STATUS status = blacklist_is_listed(session, identity->fpr, &listed);
   936         assert(status == PEP_STATUS_OK);
   937         if (status != PEP_STATUS_OK)
   938             return status;
   939 
   940         if (listed)
   941             return PEP_KEY_BLACKLISTED;
   942     }
   943 
   944     sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
   945 
   946     sqlite3_reset(session->set_person);
   947     sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
   948             SQLITE_STATIC);
   949     sqlite3_bind_text(session->set_person, 2, identity->username, -1,
   950             SQLITE_STATIC);
   951     if (identity->lang[0])
   952         sqlite3_bind_text(session->set_person, 3, identity->lang, 1,
   953                 SQLITE_STATIC);
   954     else
   955         sqlite3_bind_null(session->set_person, 3);
   956     sqlite3_bind_text(session->set_person, 4, identity->fpr, -1,
   957                       SQLITE_STATIC);
   958     result = sqlite3_step(session->set_person);
   959     sqlite3_reset(session->set_person);
   960     if (result != SQLITE_DONE) {
   961         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   962         return PEP_CANNOT_SET_PERSON;
   963     }
   964 
   965     sqlite3_reset(session->set_pgp_keypair);
   966     sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
   967             SQLITE_STATIC);
   968     result = sqlite3_step(session->set_pgp_keypair);
   969     sqlite3_reset(session->set_pgp_keypair);
   970     if (result != SQLITE_DONE) {
   971         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   972         return PEP_CANNOT_SET_PGP_KEYPAIR;
   973     }
   974 
   975     sqlite3_reset(session->set_identity);
   976     sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
   977             SQLITE_STATIC);
   978     sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
   979             SQLITE_STATIC);
   980     sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
   981             SQLITE_STATIC);
   982     sqlite3_bind_int(session->set_identity, 4, identity->flags);
   983     result = sqlite3_step(session->set_identity);
   984     sqlite3_reset(session->set_identity);
   985     if (result != SQLITE_DONE) {
   986         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   987         return PEP_CANNOT_SET_IDENTITY;
   988     }
   989 
   990     sqlite3_reset(session->set_trust);
   991     sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
   992             SQLITE_STATIC);
   993     sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
   994             SQLITE_STATIC);
   995     sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
   996     result = sqlite3_step(session->set_trust);
   997     sqlite3_reset(session->set_trust);
   998     if (result != SQLITE_DONE) {
   999         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
  1000         return PEP_CANNOT_SET_TRUST;
  1001     }
  1002 
  1003     result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
  1004     if (result == SQLITE_OK)
  1005         return PEP_STATUS_OK;
  1006     else
  1007         return PEP_COMMIT_FAILED;
  1008 }
  1009 
  1010 DYNAMIC_API PEP_STATUS set_identity_flags(
  1011         PEP_SESSION session,
  1012         pEp_identity *identity,
  1013         unsigned int flags
  1014     )
  1015 {
  1016     int result;
  1017 
  1018     assert(session);
  1019     assert(identity);
  1020     assert(identity->address);
  1021     assert(identity->user_id);
  1022 
  1023     if (!(session && identity && identity->address && identity->user_id))
  1024         return PEP_ILLEGAL_VALUE;
  1025 
  1026     sqlite3_reset(session->set_identity_flags);
  1027     sqlite3_bind_int(session->set_identity_flags, 1, flags);
  1028     sqlite3_bind_text(session->set_identity_flags, 2, identity->address, -1,
  1029             SQLITE_STATIC);
  1030     sqlite3_bind_text(session->set_identity_flags, 3, identity->user_id, -1,
  1031             SQLITE_STATIC);
  1032     result = sqlite3_step(session->set_identity_flags);
  1033     sqlite3_reset(session->set_identity_flags);
  1034     if (result != SQLITE_DONE)
  1035         return PEP_CANNOT_SET_IDENTITY;
  1036 
  1037     identity->flags = flags;
  1038     return PEP_STATUS_OK;
  1039 }
  1040 
  1041 DYNAMIC_API PEP_STATUS mark_as_compromized(
  1042         PEP_SESSION session,
  1043         const char *fpr
  1044     )
  1045 {
  1046     int result;
  1047 
  1048     assert(session);
  1049     assert(fpr && fpr[0]);
  1050 
  1051     if (!(session && fpr && fpr[0]))
  1052         return PEP_ILLEGAL_VALUE;
  1053 
  1054     sqlite3_reset(session->mark_compromized);
  1055     sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
  1056             SQLITE_STATIC);
  1057     result = sqlite3_step(session->mark_compromized);
  1058     sqlite3_reset(session->mark_compromized);
  1059 
  1060     if (result != SQLITE_DONE)
  1061         return PEP_CANNOT_SET_TRUST;
  1062 
  1063     return PEP_STATUS_OK;
  1064 }
  1065 
  1066 void pEp_free(void *p)
  1067 {
  1068     free(p);
  1069 }
  1070 
  1071 DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
  1072 {
  1073     PEP_STATUS status = PEP_STATUS_OK;
  1074     int result;
  1075 
  1076     assert(session);
  1077     assert(identity);
  1078     assert(identity->user_id);
  1079     assert(identity->user_id[0]);
  1080     assert(identity->fpr);
  1081     assert(identity->fpr[0]);
  1082 
  1083     if (!(session && identity && identity->user_id && identity->user_id[0] &&
  1084                 identity->fpr && identity->fpr[0]))
  1085         return PEP_ILLEGAL_VALUE;
  1086 
  1087     identity->comm_type = PEP_ct_unknown;
  1088 
  1089     sqlite3_reset(session->get_trust);
  1090     sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
  1091     sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
  1092 
  1093     result = sqlite3_step(session->get_trust);
  1094     switch (result) {
  1095     case SQLITE_ROW: {
  1096         int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 0);
  1097         identity->comm_type = comm_type;
  1098         break;
  1099     }
  1100  
  1101     default:
  1102         status = PEP_CANNOT_FIND_IDENTITY;
  1103     }
  1104 
  1105     sqlite3_reset(session->get_trust);
  1106     return status;
  1107 }
  1108 
  1109 DYNAMIC_API PEP_STATUS least_trust(
  1110         PEP_SESSION session,
  1111         const char *fpr,
  1112         PEP_comm_type *comm_type
  1113     )
  1114 {
  1115     PEP_STATUS status = PEP_STATUS_OK;
  1116     int result;
  1117 
  1118     assert(session);
  1119     assert(fpr);
  1120     assert(comm_type);
  1121 
  1122     if (!(session && fpr && comm_type))
  1123         return PEP_ILLEGAL_VALUE;
  1124 
  1125     *comm_type = PEP_ct_unknown;
  1126 
  1127     sqlite3_reset(session->least_trust);
  1128     sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
  1129 
  1130     result = sqlite3_step(session->least_trust);
  1131     switch (result) {
  1132         case SQLITE_ROW: {
  1133             int _comm_type = sqlite3_column_int(session->least_trust, 0);
  1134             *comm_type = (PEP_comm_type) _comm_type;
  1135             break;
  1136         }
  1137         default:
  1138             status = PEP_CANNOT_FIND_IDENTITY;
  1139     }
  1140 
  1141     sqlite3_reset(session->least_trust);
  1142     return status;
  1143 }
  1144 
  1145 DYNAMIC_API PEP_STATUS decrypt_and_verify(
  1146     PEP_SESSION session, const char *ctext, size_t csize,
  1147     char **ptext, size_t *psize, stringlist_t **keylist
  1148     )
  1149 {
  1150     assert(session);
  1151     assert(ctext);
  1152     assert(csize);
  1153     assert(ptext);
  1154     assert(psize);
  1155     assert(keylist);
  1156 
  1157     if (!(session && ctext && csize && ptext && psize && keylist && keylist))
  1158         return PEP_ILLEGAL_VALUE;
  1159 
  1160     return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
  1161 }
  1162 
  1163 DYNAMIC_API PEP_STATUS encrypt_and_sign(
  1164     PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  1165     size_t psize, char **ctext, size_t *csize
  1166     )
  1167 {
  1168     assert(session);
  1169     assert(keylist);
  1170     assert(ptext);
  1171     assert(psize);
  1172     assert(ctext);
  1173     assert(csize);
  1174 
  1175     if (!(session && keylist && ptext && psize && ctext && csize))
  1176         return PEP_ILLEGAL_VALUE;
  1177 
  1178     return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
  1179 }
  1180 
  1181 DYNAMIC_API PEP_STATUS verify_text(
  1182     PEP_SESSION session, const char *text, size_t size,
  1183     const char *signature, size_t sig_size, stringlist_t **keylist
  1184     )
  1185 {
  1186     assert(session);
  1187     assert(text);
  1188     assert(size);
  1189     assert(signature);
  1190     assert(sig_size);
  1191     assert(keylist);
  1192 
  1193     if (!(session && text && size && signature && sig_size && keylist))
  1194         return PEP_ILLEGAL_VALUE;
  1195 
  1196     return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text, size, signature, sig_size, keylist);
  1197 }
  1198 
  1199 DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
  1200 {
  1201     assert(session);
  1202     assert(fpr);
  1203 
  1204     if (!(session && fpr))
  1205         return PEP_ILLEGAL_VALUE;
  1206 
  1207     return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
  1208 }
  1209 
  1210 DYNAMIC_API PEP_STATUS export_key(
  1211         PEP_SESSION session, const char *fpr, char **key_data, size_t *size
  1212     )
  1213 {
  1214     assert(session);
  1215     assert(fpr);
  1216     assert(key_data);
  1217     assert(size);
  1218 
  1219     if (!(session && fpr && key_data && size))
  1220         return PEP_ILLEGAL_VALUE;
  1221 
  1222     return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr, key_data, size);
  1223 }
  1224 
  1225 DYNAMIC_API PEP_STATUS find_keys(
  1226         PEP_SESSION session, const char *pattern, stringlist_t **keylist
  1227     )
  1228 {
  1229     assert(session);
  1230     assert(pattern);
  1231     assert(keylist);
  1232 
  1233     if (!(session && pattern && keylist))
  1234         return PEP_ILLEGAL_VALUE;
  1235 
  1236     return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern, keylist);
  1237 }
  1238 
  1239 
  1240 DYNAMIC_API PEP_STATUS generate_keypair(
  1241         PEP_SESSION session, pEp_identity *identity
  1242     )
  1243 {
  1244     assert(session);
  1245     assert(identity);
  1246     assert(identity->address);
  1247     assert(identity->fpr == NULL || identity->fpr[0] == 0);
  1248     assert(identity->username);
  1249 
  1250     if (!(session && identity && identity->address &&
  1251         (identity->fpr == NULL || identity->fpr[0] == 0) && identity->username))
  1252         return PEP_ILLEGAL_VALUE;
  1253 
  1254     return session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session, identity);
  1255 }
  1256 
  1257 DYNAMIC_API PEP_STATUS get_key_rating(
  1258         PEP_SESSION session,
  1259         const char *fpr,
  1260         PEP_comm_type *comm_type
  1261     )
  1262 {
  1263     assert(session);
  1264     assert(fpr);
  1265     assert(comm_type);
  1266 
  1267     if (!(session && fpr && comm_type))
  1268         return PEP_ILLEGAL_VALUE;
  1269 
  1270     return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr, comm_type);
  1271 }
  1272 
  1273 DYNAMIC_API PEP_STATUS import_key(
  1274         PEP_SESSION session,
  1275         const char *key_data,
  1276         size_t size,
  1277         identity_list **private_keys
  1278     )
  1279 {
  1280     assert(session);
  1281     assert(key_data);
  1282 
  1283     if (!(session && key_data))
  1284         return PEP_ILLEGAL_VALUE;
  1285 
  1286     return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data, size, private_keys);
  1287 }
  1288 
  1289 DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
  1290 {
  1291     assert(session);
  1292     assert(pattern);
  1293 
  1294     if (!(session && pattern))
  1295         return PEP_ILLEGAL_VALUE;
  1296 
  1297     return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
  1298 }
  1299 
  1300 DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
  1301 {
  1302     assert(session);
  1303     assert(pattern);
  1304 
  1305     if (!(session && pattern))
  1306         return PEP_ILLEGAL_VALUE;
  1307 
  1308     return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
  1309 }
  1310 
  1311 DYNAMIC_API PEP_STATUS renew_key(
  1312         PEP_SESSION session,
  1313         const char *fpr,
  1314         const timestamp *ts
  1315     )
  1316 {
  1317     assert(session);
  1318     assert(fpr);
  1319 
  1320     if (!(session && fpr))
  1321         return PEP_ILLEGAL_VALUE;
  1322 
  1323     return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
  1324 }
  1325 
  1326 DYNAMIC_API PEP_STATUS revoke_key(
  1327         PEP_SESSION session,
  1328         const char *fpr,
  1329         const char *reason
  1330     )
  1331 {
  1332     assert(session);
  1333     assert(fpr);
  1334 
  1335     if (!(session && fpr))
  1336         return PEP_ILLEGAL_VALUE;
  1337 
  1338     return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
  1339             reason);
  1340 }
  1341 
  1342 DYNAMIC_API PEP_STATUS key_expired(
  1343         PEP_SESSION session,
  1344         const char *fpr,
  1345         const time_t when,
  1346         bool *expired
  1347     )
  1348 {
  1349     assert(session);
  1350     assert(fpr);
  1351     assert(expired);
  1352 
  1353     if (!(session && fpr && expired))
  1354         return PEP_ILLEGAL_VALUE;
  1355 
  1356     return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
  1357             when, expired);
  1358 }
  1359 
  1360 DYNAMIC_API PEP_STATUS key_revoked(
  1361                                    PEP_SESSION session,
  1362                                    const char *fpr,
  1363                                    bool *revoked
  1364                                    )
  1365 {
  1366     assert(session);
  1367     assert(fpr);
  1368     assert(revoked);
  1369     
  1370     if (!(session && fpr && revoked))
  1371         return PEP_ILLEGAL_VALUE;
  1372     
  1373     return session->cryptotech[PEP_crypt_OpenPGP].key_revoked(session, fpr,
  1374                                                               revoked);
  1375 }
  1376 
  1377 static void _clean_log_value(char *text)
  1378 {
  1379     if (text) {
  1380         for (char *c = text; *c; c++) {
  1381             if (*c < 32 && *c != '\n')
  1382                 *c = 32;
  1383             else if (*c == '"')
  1384                 *c = '\'';
  1385         }
  1386     }
  1387 }
  1388 
  1389 static char *_concat_string(char *str1, const char *str2, char delim)
  1390 {
  1391     str2 = str2 ? str2 : "";
  1392     size_t len1 = str1 ? strlen(str1) : 0;
  1393     size_t len2 = strlen(str2);
  1394     size_t len = len1 + len2 + 3;
  1395     char * result = realloc(str1, len + 1);
  1396 
  1397     if (result) {
  1398         result[len1] = '"';
  1399         strcpy(result + len1 + 1, str2);
  1400         result[len - 2] = '"';
  1401         result[len - 1] = delim;
  1402         result[len] = 0;
  1403     }
  1404     else {
  1405         free(str1);
  1406     }
  1407 
  1408     return result;
  1409 }
  1410 
  1411 DYNAMIC_API PEP_STATUS get_crashdump_log(
  1412         PEP_SESSION session,
  1413         int maxlines,
  1414         char **logdata
  1415     )
  1416 {
  1417     PEP_STATUS status = PEP_STATUS_OK;
  1418     char *_logdata= NULL;
  1419 
  1420     assert(session);
  1421     assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
  1422     assert(logdata);
  1423 
  1424     if (!(session && logdata && maxlines >= 0 && maxlines <=
  1425             CRASHDUMP_MAX_LINES))
  1426         return PEP_ILLEGAL_VALUE;
  1427 
  1428     *logdata = NULL;
  1429 
  1430     int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
  1431     const char *timestamp = NULL;
  1432     const char *title = NULL;
  1433     const char *entity = NULL;
  1434     const char *desc = NULL;
  1435     const char *comment = NULL;
  1436 
  1437     sqlite3_reset(session->crashdump);
  1438     sqlite3_bind_int(session->crashdump, 1, limit);
  1439 
  1440     int result;
  1441 
  1442     do {
  1443         result = sqlite3_step(session->crashdump);
  1444         switch (result) {
  1445         case SQLITE_ROW:
  1446             timestamp = (const char *) sqlite3_column_text(session->crashdump, 0);
  1447             title   = (const char *) sqlite3_column_text(session->crashdump, 1);
  1448             entity  = (const char *) sqlite3_column_text(session->crashdump, 2);
  1449             desc    = (const char *) sqlite3_column_text(session->crashdump, 3);
  1450             comment = (const char *) sqlite3_column_text(session->crashdump, 4);
  1451 
  1452             _logdata = _concat_string(_logdata, timestamp, ',');
  1453             if (_logdata == NULL)
  1454                 goto enomem;
  1455 
  1456             _logdata = _concat_string(_logdata, title, ',');
  1457             if (_logdata == NULL)
  1458                 goto enomem;
  1459 
  1460             _logdata = _concat_string(_logdata, entity, ',');
  1461             if (_logdata == NULL)
  1462                 goto enomem;
  1463 
  1464             _logdata = _concat_string(_logdata, desc, ',');
  1465             if (_logdata == NULL)
  1466                 goto enomem;
  1467 
  1468             _logdata = _concat_string(_logdata, comment, '\n');
  1469             if (_logdata == NULL)
  1470                 goto enomem;
  1471 
  1472             _clean_log_value(_logdata);
  1473             break;
  1474 
  1475         case SQLITE_DONE:
  1476             break;
  1477 
  1478         default:
  1479             status = PEP_UNKNOWN_ERROR;
  1480             result = SQLITE_DONE;
  1481         }
  1482     } while (result != SQLITE_DONE);
  1483 
  1484     sqlite3_reset(session->crashdump);
  1485     if (status == PEP_STATUS_OK)
  1486         *logdata = _logdata;
  1487 
  1488     goto the_end;
  1489 
  1490 enomem:
  1491     status = PEP_OUT_OF_MEMORY;
  1492 
  1493 the_end:
  1494     return status;
  1495 }
  1496 
  1497 DYNAMIC_API PEP_STATUS get_languagelist(
  1498         PEP_SESSION session,
  1499         char **languages
  1500     )
  1501 {
  1502     PEP_STATUS status = PEP_STATUS_OK;
  1503     char *_languages= NULL;
  1504 
  1505     assert(session);
  1506     assert(languages);
  1507 
  1508     if (!(session && languages))
  1509         return PEP_ILLEGAL_VALUE;
  1510 
  1511     *languages = NULL;
  1512 
  1513     const char *lang = NULL;
  1514     const char *name = NULL;
  1515     const char *phrase = NULL;
  1516 
  1517     sqlite3_reset(session->languagelist);
  1518 
  1519     int result;
  1520 
  1521     do {
  1522         result = sqlite3_step(session->languagelist);
  1523         switch (result) {
  1524         case SQLITE_ROW:
  1525             lang = (const char *) sqlite3_column_text(session->languagelist, 0);
  1526             name = (const char *) sqlite3_column_text(session->languagelist, 1);
  1527             phrase = (const char *) sqlite3_column_text(session->languagelist, 2);
  1528 
  1529             _languages = _concat_string(_languages, lang, ',');
  1530             if (_languages == NULL)
  1531                 goto enomem;
  1532 
  1533             _languages = _concat_string(_languages, name, ',');
  1534             if (_languages == NULL)
  1535                 goto enomem;
  1536 
  1537             _languages = _concat_string(_languages, phrase, '\n');
  1538             if (_languages == NULL)
  1539                 goto enomem;
  1540 
  1541             break;
  1542 
  1543         case SQLITE_DONE:
  1544             break;
  1545 
  1546         default:
  1547             status = PEP_UNKNOWN_ERROR;
  1548             result = SQLITE_DONE;
  1549         }
  1550     } while (result != SQLITE_DONE);
  1551 
  1552     sqlite3_reset(session->languagelist);
  1553     if (status == PEP_STATUS_OK)
  1554         *languages = _languages;
  1555 
  1556     goto the_end;
  1557 
  1558 enomem:
  1559     status = PEP_OUT_OF_MEMORY;
  1560 
  1561 the_end:
  1562     return status;
  1563 }
  1564 
  1565 DYNAMIC_API PEP_STATUS get_phrase(
  1566         PEP_SESSION session,
  1567         const char *lang,
  1568         int phrase_id,
  1569         char **phrase
  1570     )
  1571 {
  1572     PEP_STATUS status = PEP_STATUS_OK;
  1573 
  1574     assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
  1575     if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
  1576         return PEP_ILLEGAL_VALUE;
  1577 
  1578     *phrase = NULL;
  1579 
  1580     sqlite3_reset(session->i18n_token);
  1581     sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
  1582     sqlite3_bind_int(session->i18n_token, 2, phrase_id);
  1583 
  1584     const char *_phrase = NULL;
  1585     int result;
  1586 
  1587     result = sqlite3_step(session->i18n_token);
  1588     switch (result) {
  1589     case SQLITE_ROW:
  1590         _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
  1591         break;
  1592 
  1593     case SQLITE_DONE:
  1594         status = PEP_PHRASE_NOT_FOUND;
  1595         break;
  1596 
  1597     default:
  1598         status = PEP_UNKNOWN_ERROR;
  1599     }
  1600 
  1601     if (status == PEP_STATUS_OK) {
  1602         *phrase = strdup(_phrase);
  1603         if (*phrase == NULL)
  1604             goto enomem;
  1605     }
  1606 
  1607     sqlite3_reset(session->i18n_token);
  1608     goto the_end;
  1609 
  1610 enomem:
  1611     status = PEP_OUT_OF_MEMORY;
  1612 
  1613 the_end:
  1614     return status;
  1615 }
  1616 
  1617 DYNAMIC_API PEP_STATUS sequence_value(
  1618         PEP_SESSION session,
  1619         const char *name,
  1620         int32_t *value
  1621     )
  1622 {
  1623     PEP_STATUS status = PEP_STATUS_OK;
  1624     int result;
  1625 
  1626     assert(session);
  1627     assert(name);
  1628     assert(value);
  1629 
  1630     if (!(session && name && value))
  1631         return PEP_ILLEGAL_VALUE;
  1632 
  1633     *value = 0;
  1634 
  1635     sqlite3_reset(session->sequence_value1);
  1636     sqlite3_bind_text(session->sequence_value1, 1, name, -1, SQLITE_STATIC);
  1637     result = sqlite3_step(session->sequence_value1);
  1638     assert(result == SQLITE_DONE);
  1639     sqlite3_reset(session->sequence_value1);
  1640     if (result != SQLITE_DONE) {
  1641         status = PEP_UNKNOWN_ERROR;
  1642     }
  1643     else {
  1644         sqlite3_reset(session->sequence_value2);
  1645         sqlite3_bind_text(session->sequence_value2, 1, name, -1, SQLITE_STATIC);
  1646         result = sqlite3_step(session->sequence_value2);
  1647         switch (result) {
  1648             case SQLITE_ROW: {
  1649                 int32_t _value = (int32_t)
  1650                         sqlite3_column_int64(session->sequence_value2, 0);
  1651                 *value = _value;
  1652                 break;
  1653             }
  1654             default:
  1655                 status = PEP_UNKNOWN_ERROR;
  1656         }
  1657         sqlite3_reset(session->sequence_value2);
  1658     }
  1659     return status;
  1660 }
  1661 
  1662 DYNAMIC_API PEP_STATUS set_revoked(
  1663        PEP_SESSION session,
  1664        const char *revoked_fpr,
  1665        const char *replacement_fpr,
  1666        const uint64_t revocation_date
  1667     )
  1668 {
  1669     PEP_STATUS status = PEP_STATUS_OK;
  1670     
  1671     assert(session &&
  1672            revoked_fpr && revoked_fpr[0] &&
  1673            replacement_fpr && replacement_fpr[0]
  1674           );
  1675     
  1676     if (!(session &&
  1677           revoked_fpr && revoked_fpr[0] &&
  1678           replacement_fpr && replacement_fpr[0]
  1679          ))
  1680         return PEP_ILLEGAL_VALUE;
  1681     
  1682     sqlite3_reset(session->set_revoked);
  1683     sqlite3_bind_text(session->set_revoked, 1, revoked_fpr, -1, SQLITE_STATIC);
  1684     sqlite3_bind_text(session->set_revoked, 2, replacement_fpr, -1, SQLITE_STATIC);
  1685     sqlite3_bind_int64(session->set_revoked, 3, revocation_date);
  1686 
  1687     int result;
  1688     
  1689     result = sqlite3_step(session->set_revoked);
  1690     switch (result) {
  1691         case SQLITE_DONE:
  1692             status = PEP_STATUS_OK;
  1693             break;
  1694             
  1695         default:
  1696             status = PEP_UNKNOWN_ERROR;
  1697     }
  1698     
  1699     sqlite3_reset(session->set_revoked);
  1700     return status;
  1701 }
  1702 
  1703 DYNAMIC_API PEP_STATUS get_revoked(
  1704         PEP_SESSION session,
  1705         const char *fpr,
  1706         char **revoked_fpr,
  1707         uint64_t *revocation_date
  1708     )
  1709 {
  1710     PEP_STATUS status = PEP_STATUS_OK;
  1711 
  1712     assert(session &&
  1713            revoked_fpr &&
  1714            fpr && fpr[0]
  1715           );
  1716     
  1717     if (!(session &&
  1718            revoked_fpr &&
  1719            fpr && fpr[0]
  1720           ))
  1721         return PEP_ILLEGAL_VALUE;
  1722 
  1723     *revoked_fpr = NULL;
  1724     *revocation_date = 0;
  1725 
  1726     sqlite3_reset(session->get_revoked);
  1727     sqlite3_bind_text(session->get_revoked, 1, fpr, -1, SQLITE_STATIC);
  1728 
  1729     int result;
  1730     
  1731     result = sqlite3_step(session->get_revoked);
  1732     switch (result) {
  1733         case SQLITE_ROW: {
  1734             *revoked_fpr = strdup((const char *) sqlite3_column_text(session->get_revoked, 0));
  1735             if(*revoked_fpr)
  1736                 *revocation_date = sqlite3_column_int64(session->get_revoked, 1);
  1737             else
  1738                 status = PEP_OUT_OF_MEMORY;
  1739 
  1740             break;
  1741         }
  1742         default:
  1743             status = PEP_CANNOT_FIND_IDENTITY;
  1744     }
  1745 
  1746     sqlite3_reset(session->get_revoked);
  1747 
  1748     return status;
  1749 }
  1750 
  1751 DYNAMIC_API const char* get_engine_version() {
  1752     return PEP_ENGINE_VERSION;
  1753 }
  1754 
  1755 
  1756 DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
  1757 {
  1758     assert(session);
  1759 
  1760     if (!session)
  1761         return PEP_ILLEGAL_VALUE;
  1762 
  1763     int int_result = sqlite3_exec(
  1764         session->db,
  1765         "delete from identity where address like '%@peptest.ch' ;",
  1766         NULL,
  1767         NULL,
  1768         NULL
  1769     );
  1770     assert(int_result == SQLITE_OK);
  1771 
  1772     return PEP_STATUS_OK;
  1773 }
  1774