src/pEpEngine.c
author Volker Birk <vb@pep-project.org>
Wed, 27 Jul 2016 16:20:31 +0200
changeset 934 6ec71cdedec1
parent 932 d35b292fe141
child 939 ed7dfdae3031
child 957 4f10946e3a2a
permissions -rw-r--r--
adding set_identity_flags_test
     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 
   563             if (session->db)
   564                 sqlite3_close_v2(session->db);
   565             if (session->system_db)
   566                 sqlite3_close_v2(session->system_db);
   567         }
   568 
   569         release_transport_system(session, out_last);
   570         release_cryptotech(session, out_last);
   571 
   572         free(session);
   573     }
   574 }
   575 
   576 DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
   577 {
   578     assert(session);
   579     session->passive_mode = enable;
   580 }
   581 
   582 DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
   583 {
   584     assert(session);
   585     session->unencrypted_subject = enable;
   586 }
   587 
   588 DYNAMIC_API void config_use_only_own_private_keys(PEP_SESSION session, bool enable)
   589 {
   590     assert(session);
   591     session->use_only_own_private_keys = enable;
   592 }
   593 
   594 DYNAMIC_API PEP_STATUS log_event(
   595         PEP_SESSION session,
   596         const char *title,
   597         const char *entity,
   598         const char *description,
   599         const char *comment
   600     )
   601 {
   602     PEP_STATUS status = PEP_STATUS_OK;
   603     int result;
   604 
   605     assert(session);
   606     assert(title);
   607     assert(entity);
   608 
   609     if (!(session && title && entity))
   610         return PEP_ILLEGAL_VALUE;
   611 
   612     sqlite3_reset(session->log);
   613     sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
   614     sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
   615     if (description)
   616         sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
   617     else
   618         sqlite3_bind_null(session->log, 3);
   619     if (comment)
   620         sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
   621     else
   622         sqlite3_bind_null(session->log, 4);
   623     do {
   624         result = sqlite3_step(session->log);
   625         assert(result == SQLITE_DONE || result == SQLITE_BUSY);
   626         if (result != SQLITE_DONE && result != SQLITE_BUSY)
   627             status = PEP_UNKNOWN_ERROR;
   628     } while (result == SQLITE_BUSY);
   629     sqlite3_reset(session->log);
   630 
   631     return status;
   632 }
   633 
   634 DYNAMIC_API PEP_STATUS trustword(
   635             PEP_SESSION session, uint16_t value, const char *lang,
   636             char **word, size_t *wsize
   637         )
   638 {
   639     PEP_STATUS status = PEP_STATUS_OK;
   640 
   641     assert(session);
   642     assert(word);
   643     assert(wsize);
   644 
   645     if (!(session && word && wsize))
   646         return PEP_ILLEGAL_VALUE;
   647 
   648     *word = NULL;
   649     *wsize = 0;
   650 
   651     if (lang == NULL)
   652         lang = "en";
   653 
   654     assert((lang[0] >= 'A' && lang[0] <= 'Z')
   655             || (lang[0] >= 'a' && lang[0] <= 'z'));
   656     assert((lang[1] >= 'A' && lang[1] <= 'Z')
   657             || (lang[1] >= 'a' && lang[1] <= 'z'));
   658     assert(lang[2] == 0);
   659 
   660     sqlite3_reset(session->trustword);
   661     sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
   662     sqlite3_bind_int(session->trustword, 2, value);
   663 
   664     const int result = sqlite3_step(session->trustword);
   665     if (result == SQLITE_ROW) {
   666         *word = strdup((const char *) sqlite3_column_text(session->trustword,
   667                     1));
   668         if (*word)
   669             *wsize = sqlite3_column_bytes(session->trustword, 1);
   670         else
   671             status = PEP_OUT_OF_MEMORY;
   672     } else
   673         status = PEP_TRUSTWORD_NOT_FOUND;
   674 
   675     sqlite3_reset(session->trustword);
   676     return status;
   677 }
   678 
   679 DYNAMIC_API PEP_STATUS trustwords(
   680         PEP_SESSION session, const char *fingerprint, const char *lang,
   681         char **words, size_t *wsize, int max_words
   682     )
   683 {
   684     const char *source = fingerprint;
   685     char *buffer;
   686     char *dest;
   687     size_t fsize;
   688     PEP_STATUS _status;
   689 
   690     assert(session);
   691     assert(fingerprint);
   692     assert(words);
   693     assert(wsize);
   694     assert(max_words >= 0);
   695 
   696     if (!(session && fingerprint && words && wsize && max_words >= 0))
   697         return PEP_ILLEGAL_VALUE;
   698 
   699     *words = NULL;
   700     *wsize = 0;
   701 
   702     buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
   703     assert(buffer);
   704     if (buffer == NULL)
   705         return PEP_OUT_OF_MEMORY;
   706     dest = buffer;
   707 
   708     fsize = strlen(fingerprint);
   709 
   710     if (!lang || !lang[0])
   711         lang = "en";
   712 
   713     assert((lang[0] >= 'A' && lang[0] <= 'Z')
   714             || (lang[0] >= 'a' && lang[0] <= 'z'));
   715     assert((lang[1] >= 'A' && lang[1] <= 'Z')
   716             || (lang[1] >= 'a' && lang[1] <= 'z'));
   717     assert(lang[2] == 0);
   718 
   719     int n_words = 0;
   720     while (source < fingerprint + fsize) {
   721         uint16_t value;
   722         char *word;
   723         size_t _wsize;
   724         int j;
   725 
   726         for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
   727             if (*source >= 'a' && *source <= 'f')
   728                 value += (*source - 'a' + 10) << (3 - j++) * 4;
   729             else if (*source >= 'A' && *source <= 'F')
   730                 value += (*source - 'A' + 10) << (3 - j++) * 4;
   731             else if (*source >= '0' && *source <= '9')
   732                 value += (*source - '0') << (3 - j++) * 4;
   733             
   734             source++;
   735         }
   736 
   737         _status = trustword(session, value, lang, &word, &_wsize);
   738         if (_status == PEP_OUT_OF_MEMORY) {
   739             free(buffer);
   740             return PEP_OUT_OF_MEMORY;
   741         }
   742         if (word == NULL) {
   743             free(buffer);
   744             return PEP_TRUSTWORD_NOT_FOUND;
   745         }
   746 
   747         if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
   748             strncpy(dest, word, _wsize);
   749             free(word);
   750             dest += _wsize;
   751         }
   752         else {
   753             free(word);
   754             break; // buffer full
   755         }
   756 
   757         if (source < fingerprint + fsize
   758                 && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
   759             *dest++ = ' ';
   760 
   761         ++n_words;
   762         if (max_words && n_words >= max_words)
   763             break;
   764     }
   765 
   766     *words = buffer;
   767     *wsize = dest - buffer;
   768     return PEP_STATUS_OK;
   769 }
   770 
   771 pEp_identity *new_identity(
   772         const char *address, const char *fpr, const char *user_id,
   773         const char *username
   774     )
   775 {
   776     pEp_identity *result = calloc(1, sizeof(pEp_identity));
   777     assert(result);
   778     if (result) {
   779         if (address) {
   780             result->address = strdup(address);
   781             assert(result->address);
   782             if (result->address == NULL) {
   783                 free(result);
   784                 return NULL;
   785             }
   786         }
   787         if (fpr) {
   788             result->fpr = strdup(fpr);
   789             assert(result->fpr);
   790             if (result->fpr == NULL) {
   791                 free_identity(result);
   792                 return NULL;
   793             }
   794         }
   795         if (user_id) {
   796             result->user_id = strdup(user_id);
   797             assert(result->user_id);
   798             if (result->user_id == NULL) {
   799                 free_identity(result);
   800                 return NULL;
   801             }
   802         }
   803         if (username) {
   804             result->username = strdup(username);
   805             assert(result->username);
   806             if (result->username == NULL) {
   807                 free_identity(result);
   808                 return NULL;
   809             }
   810         }
   811     }
   812     return result;
   813 }
   814 
   815 pEp_identity *identity_dup(const pEp_identity *src)
   816 {
   817     assert(src);
   818 
   819     pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
   820     assert(dup);
   821     if (dup == NULL)
   822         return NULL;
   823     
   824     dup->comm_type = src->comm_type;
   825     dup->lang[0] = src->lang[0];
   826     dup->lang[1] = src->lang[1];
   827     dup->lang[2] = 0;
   828     dup->me = src->me;
   829     dup->flags = src->flags;
   830 
   831     return dup;
   832 }
   833 
   834 void free_identity(pEp_identity *identity)
   835 {
   836     if (identity) {
   837         free(identity->address);
   838         free(identity->fpr);
   839         free(identity->user_id);
   840         free(identity->username);
   841         free(identity);
   842     }
   843 }
   844 
   845 DYNAMIC_API PEP_STATUS get_identity(
   846         PEP_SESSION session,
   847         const char *address,
   848         const char *user_id,
   849         pEp_identity **identity
   850     )
   851 {
   852     PEP_STATUS status = PEP_STATUS_OK;
   853     static pEp_identity *_identity;
   854 
   855     assert(session);
   856     assert(address);
   857     assert(address[0]);
   858     assert(identity);
   859 
   860     if (!(session && address && address[0] && identity))
   861         return PEP_ILLEGAL_VALUE;
   862 
   863     *identity = NULL;
   864 
   865     sqlite3_reset(session->get_identity);
   866     sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
   867     sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
   868 
   869     const int result = sqlite3_step(session->get_identity);
   870     switch (result) {
   871     case SQLITE_ROW:
   872         _identity = new_identity(
   873                 address,
   874                 (const char *) sqlite3_column_text(session->get_identity, 0),
   875                 user_id,
   876                 (const char *) sqlite3_column_text(session->get_identity, 1)
   877                 );
   878         assert(_identity);
   879         if (_identity == NULL)
   880             return PEP_OUT_OF_MEMORY;
   881 
   882         _identity->comm_type = (PEP_comm_type) sqlite3_column_int(session->get_identity, 2);
   883         const char* const _lang = (const char *) sqlite3_column_text(session->get_identity, 3);
   884         if (_lang && _lang[0]) {
   885             assert(_lang[0] >= 'a' && _lang[0] <= 'z');
   886             assert(_lang[1] >= 'a' && _lang[1] <= 'z');
   887             assert(_lang[2] == 0);
   888             _identity->lang[0] = _lang[0];
   889             _identity->lang[1] = _lang[1];
   890             _identity->lang[2] = 0;
   891         }
   892         _identity->flags = (unsigned int) sqlite3_column_int(session->get_identity, 4);
   893         *identity = _identity;
   894         break;
   895     default:
   896         status = PEP_CANNOT_FIND_IDENTITY;
   897         *identity = NULL;
   898     }
   899 
   900     sqlite3_reset(session->get_identity);
   901     return status;
   902 }
   903 
   904 DYNAMIC_API PEP_STATUS set_identity(
   905         PEP_SESSION session, const pEp_identity *identity
   906     )
   907 {
   908     int result;
   909 
   910     assert(session);
   911     assert(identity);
   912     assert(identity->address);
   913     assert(identity->fpr);
   914     assert(identity->user_id);
   915     assert(identity->username);
   916 
   917     if (!(session && identity && identity->address && identity->fpr &&
   918                 identity->user_id && identity->username))
   919         return PEP_ILLEGAL_VALUE;
   920 
   921     bool listed;
   922     PEP_STATUS status = blacklist_is_listed(session, identity->fpr, &listed);
   923     assert(status == PEP_STATUS_OK);
   924     if (status != PEP_STATUS_OK)
   925         return status;
   926 
   927     if (listed)
   928         return PEP_KEY_BLACKLISTED;
   929 
   930     sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
   931 
   932     sqlite3_reset(session->set_person);
   933     sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
   934             SQLITE_STATIC);
   935     sqlite3_bind_text(session->set_person, 2, identity->username, -1,
   936             SQLITE_STATIC);
   937     if (identity->lang[0])
   938         sqlite3_bind_text(session->set_person, 3, identity->lang, 1,
   939                 SQLITE_STATIC);
   940     else
   941         sqlite3_bind_null(session->set_person, 3);
   942     sqlite3_bind_text(session->set_person, 4, identity->fpr, -1,
   943                       SQLITE_STATIC);
   944     result = sqlite3_step(session->set_person);
   945     sqlite3_reset(session->set_person);
   946     if (result != SQLITE_DONE) {
   947         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   948         return PEP_CANNOT_SET_PERSON;
   949     }
   950 
   951     sqlite3_reset(session->set_pgp_keypair);
   952     sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
   953             SQLITE_STATIC);
   954     result = sqlite3_step(session->set_pgp_keypair);
   955     sqlite3_reset(session->set_pgp_keypair);
   956     if (result != SQLITE_DONE) {
   957         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   958         return PEP_CANNOT_SET_PGP_KEYPAIR;
   959     }
   960 
   961     sqlite3_reset(session->set_identity);
   962     sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
   963             SQLITE_STATIC);
   964     sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
   965             SQLITE_STATIC);
   966     sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
   967             SQLITE_STATIC);
   968     sqlite3_bind_int(session->set_identity, 4, identity->flags);
   969     result = sqlite3_step(session->set_identity);
   970     sqlite3_reset(session->set_identity);
   971     if (result != SQLITE_DONE) {
   972         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   973         return PEP_CANNOT_SET_IDENTITY;
   974     }
   975 
   976     sqlite3_reset(session->set_trust);
   977     sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
   978             SQLITE_STATIC);
   979     sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
   980             SQLITE_STATIC);
   981     sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
   982     result = sqlite3_step(session->set_trust);
   983     sqlite3_reset(session->set_trust);
   984     if (result != SQLITE_DONE) {
   985         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   986         return PEP_CANNOT_SET_TRUST;
   987     }
   988 
   989     result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
   990     if (result == SQLITE_OK)
   991         return PEP_STATUS_OK;
   992     else
   993         return PEP_COMMIT_FAILED;
   994 }
   995 
   996 DYNAMIC_API PEP_STATUS set_identity_flags(
   997         PEP_SESSION session,
   998         pEp_identity *identity,
   999         unsigned int flags
  1000     )
  1001 {
  1002     int result;
  1003 
  1004     assert(session);
  1005     assert(identity);
  1006     assert(identity->address);
  1007     assert(identity->user_id);
  1008 
  1009     if (!(session && identity && identity->address && identity->user_id))
  1010         return PEP_ILLEGAL_VALUE;
  1011 
  1012     sqlite3_reset(session->set_identity_flags);
  1013     sqlite3_bind_int(session->set_identity_flags, 1, flags);
  1014     sqlite3_bind_text(session->set_identity_flags, 2, identity->address, -1,
  1015             SQLITE_STATIC);
  1016     sqlite3_bind_text(session->set_identity_flags, 3, identity->user_id, -1,
  1017             SQLITE_STATIC);
  1018     result = sqlite3_step(session->set_identity_flags);
  1019     sqlite3_reset(session->set_identity_flags);
  1020     if (result != SQLITE_DONE)
  1021         return PEP_CANNOT_SET_IDENTITY;
  1022 
  1023     identity->flags = flags;
  1024     return PEP_STATUS_OK;
  1025 }
  1026 
  1027 DYNAMIC_API PEP_STATUS mark_as_compromized(
  1028         PEP_SESSION session,
  1029         const char *fpr
  1030     )
  1031 {
  1032     int result;
  1033 
  1034     assert(session);
  1035     assert(fpr && fpr[0]);
  1036 
  1037     if (!(session && fpr && fpr[0]))
  1038         return PEP_ILLEGAL_VALUE;
  1039 
  1040     sqlite3_reset(session->mark_compromized);
  1041     sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
  1042             SQLITE_STATIC);
  1043     result = sqlite3_step(session->mark_compromized);
  1044     sqlite3_reset(session->mark_compromized);
  1045 
  1046     if (result != SQLITE_DONE)
  1047         return PEP_CANNOT_SET_TRUST;
  1048 
  1049     return PEP_STATUS_OK;
  1050 }
  1051 
  1052 void pEp_free(void *p)
  1053 {
  1054     free(p);
  1055 }
  1056 
  1057 DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
  1058 {
  1059     PEP_STATUS status = PEP_STATUS_OK;
  1060     int result;
  1061 
  1062     assert(session);
  1063     assert(identity);
  1064     assert(identity->user_id);
  1065     assert(identity->user_id[0]);
  1066     assert(identity->fpr);
  1067     assert(identity->fpr[0]);
  1068 
  1069     if (!(session && identity && identity->user_id && identity->user_id[0] &&
  1070                 identity->fpr && identity->fpr[0]))
  1071         return PEP_ILLEGAL_VALUE;
  1072 
  1073     identity->comm_type = PEP_ct_unknown;
  1074 
  1075     sqlite3_reset(session->get_trust);
  1076     sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
  1077     sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
  1078 
  1079     result = sqlite3_step(session->get_trust);
  1080     switch (result) {
  1081     case SQLITE_ROW: {
  1082         int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 0);
  1083         identity->comm_type = comm_type;
  1084         break;
  1085     }
  1086  
  1087     default:
  1088         status = PEP_CANNOT_FIND_IDENTITY;
  1089     }
  1090 
  1091     sqlite3_reset(session->get_trust);
  1092     return status;
  1093 }
  1094 
  1095 DYNAMIC_API PEP_STATUS least_trust(
  1096         PEP_SESSION session,
  1097         const char *fpr,
  1098         PEP_comm_type *comm_type
  1099     )
  1100 {
  1101     PEP_STATUS status = PEP_STATUS_OK;
  1102     int result;
  1103 
  1104     assert(session);
  1105     assert(fpr);
  1106     assert(comm_type);
  1107 
  1108     if (!(session && fpr && comm_type))
  1109         return PEP_ILLEGAL_VALUE;
  1110 
  1111     *comm_type = PEP_ct_unknown;
  1112 
  1113     sqlite3_reset(session->least_trust);
  1114     sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
  1115 
  1116     result = sqlite3_step(session->least_trust);
  1117     switch (result) {
  1118         case SQLITE_ROW: {
  1119             int _comm_type = sqlite3_column_int(session->least_trust, 0);
  1120             *comm_type = (PEP_comm_type) _comm_type;
  1121             break;
  1122         }
  1123         default:
  1124             status = PEP_CANNOT_FIND_IDENTITY;
  1125     }
  1126 
  1127     sqlite3_reset(session->least_trust);
  1128     return status;
  1129 }
  1130 
  1131 DYNAMIC_API PEP_STATUS decrypt_and_verify(
  1132     PEP_SESSION session, const char *ctext, size_t csize,
  1133     char **ptext, size_t *psize, stringlist_t **keylist
  1134     )
  1135 {
  1136     assert(session);
  1137     assert(ctext);
  1138     assert(csize);
  1139     assert(ptext);
  1140     assert(psize);
  1141     assert(keylist);
  1142 
  1143     if (!(session && ctext && csize && ptext && psize && keylist && keylist))
  1144         return PEP_ILLEGAL_VALUE;
  1145 
  1146     return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
  1147 }
  1148 
  1149 DYNAMIC_API PEP_STATUS encrypt_and_sign(
  1150     PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  1151     size_t psize, char **ctext, size_t *csize
  1152     )
  1153 {
  1154     assert(session);
  1155     assert(keylist);
  1156     assert(ptext);
  1157     assert(psize);
  1158     assert(ctext);
  1159     assert(csize);
  1160 
  1161     if (!(session && keylist && ptext && psize && ctext && csize))
  1162         return PEP_ILLEGAL_VALUE;
  1163 
  1164     return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
  1165 }
  1166 
  1167 DYNAMIC_API PEP_STATUS verify_text(
  1168     PEP_SESSION session, const char *text, size_t size,
  1169     const char *signature, size_t sig_size, stringlist_t **keylist
  1170     )
  1171 {
  1172     assert(session);
  1173     assert(text);
  1174     assert(size);
  1175     assert(signature);
  1176     assert(sig_size);
  1177     assert(keylist);
  1178 
  1179     if (!(session && text && size && signature && sig_size && keylist))
  1180         return PEP_ILLEGAL_VALUE;
  1181 
  1182     return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text, size, signature, sig_size, keylist);
  1183 }
  1184 
  1185 DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
  1186 {
  1187     assert(session);
  1188     assert(fpr);
  1189 
  1190     if (!(session && fpr))
  1191         return PEP_ILLEGAL_VALUE;
  1192 
  1193     return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
  1194 }
  1195 
  1196 DYNAMIC_API PEP_STATUS export_key(
  1197         PEP_SESSION session, const char *fpr, char **key_data, size_t *size
  1198     )
  1199 {
  1200     assert(session);
  1201     assert(fpr);
  1202     assert(key_data);
  1203     assert(size);
  1204 
  1205     if (!(session && fpr && key_data && size))
  1206         return PEP_ILLEGAL_VALUE;
  1207 
  1208     return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr, key_data, size);
  1209 }
  1210 
  1211 DYNAMIC_API PEP_STATUS find_keys(
  1212         PEP_SESSION session, const char *pattern, stringlist_t **keylist
  1213     )
  1214 {
  1215     assert(session);
  1216     assert(pattern);
  1217     assert(keylist);
  1218 
  1219     if (!(session && pattern && keylist))
  1220         return PEP_ILLEGAL_VALUE;
  1221 
  1222     return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern, keylist);
  1223 }
  1224 
  1225 DYNAMIC_API PEP_STATUS generate_keypair(
  1226         PEP_SESSION session, pEp_identity *identity
  1227     )
  1228 {
  1229     assert(session);
  1230     assert(identity);
  1231     assert(identity->address);
  1232     assert(identity->fpr == NULL || identity->fpr[0] == 0);
  1233     assert(identity->username);
  1234 
  1235     if (!(session && identity && identity->address &&
  1236         (identity->fpr == NULL || identity->fpr[0] == 0) && identity->username))
  1237         return PEP_ILLEGAL_VALUE;
  1238 
  1239     return session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session, identity);
  1240 }
  1241 
  1242 DYNAMIC_API PEP_STATUS get_key_rating(
  1243         PEP_SESSION session,
  1244         const char *fpr,
  1245         PEP_comm_type *comm_type
  1246     )
  1247 {
  1248     assert(session);
  1249     assert(fpr);
  1250     assert(comm_type);
  1251 
  1252     if (!(session && fpr && comm_type))
  1253         return PEP_ILLEGAL_VALUE;
  1254 
  1255     return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr, comm_type);
  1256 }
  1257 
  1258 DYNAMIC_API PEP_STATUS import_key(
  1259         PEP_SESSION session,
  1260         const char *key_data,
  1261         size_t size,
  1262         identity_list **private_keys
  1263     )
  1264 {
  1265     assert(session);
  1266     assert(key_data);
  1267 
  1268     if (!(session && key_data))
  1269         return PEP_ILLEGAL_VALUE;
  1270 
  1271     return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data, size, private_keys);
  1272 }
  1273 
  1274 DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
  1275 {
  1276     assert(session);
  1277     assert(pattern);
  1278 
  1279     if (!(session && pattern))
  1280         return PEP_ILLEGAL_VALUE;
  1281 
  1282     return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
  1283 }
  1284 
  1285 DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
  1286 {
  1287     assert(session);
  1288     assert(pattern);
  1289 
  1290     if (!(session && pattern))
  1291         return PEP_ILLEGAL_VALUE;
  1292 
  1293     return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
  1294 }
  1295 
  1296 DYNAMIC_API PEP_STATUS renew_key(
  1297         PEP_SESSION session,
  1298         const char *fpr,
  1299         const timestamp *ts
  1300     )
  1301 {
  1302     assert(session);
  1303     assert(fpr);
  1304 
  1305     if (!(session && fpr))
  1306         return PEP_ILLEGAL_VALUE;
  1307 
  1308     return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
  1309 }
  1310 
  1311 DYNAMIC_API PEP_STATUS revoke_key(
  1312         PEP_SESSION session,
  1313         const char *fpr,
  1314         const char *reason
  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].revoke_key(session, fpr,
  1324             reason);
  1325 }
  1326 
  1327 DYNAMIC_API PEP_STATUS key_expired(
  1328         PEP_SESSION session,
  1329         const char *fpr,
  1330         const time_t when,
  1331         bool *expired
  1332     )
  1333 {
  1334     assert(session);
  1335     assert(fpr);
  1336     assert(expired);
  1337 
  1338     if (!(session && fpr && expired))
  1339         return PEP_ILLEGAL_VALUE;
  1340 
  1341     return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
  1342             when, expired);
  1343 }
  1344 
  1345 DYNAMIC_API PEP_STATUS key_revoked(
  1346                                    PEP_SESSION session,
  1347                                    const char *fpr,
  1348                                    bool *revoked
  1349                                    )
  1350 {
  1351     assert(session);
  1352     assert(fpr);
  1353     assert(revoked);
  1354     
  1355     if (!(session && fpr && revoked))
  1356         return PEP_ILLEGAL_VALUE;
  1357     
  1358     return session->cryptotech[PEP_crypt_OpenPGP].key_revoked(session, fpr,
  1359                                                               revoked);
  1360 }
  1361 
  1362 static void _clean_log_value(char *text)
  1363 {
  1364     if (text) {
  1365         for (char *c = text; *c; c++) {
  1366             if (*c < 32 && *c != '\n')
  1367                 *c = 32;
  1368             else if (*c == '"')
  1369                 *c = '\'';
  1370         }
  1371     }
  1372 }
  1373 
  1374 static char *_concat_string(char *str1, const char *str2, char delim)
  1375 {
  1376     str2 = str2 ? str2 : "";
  1377     size_t len1 = str1 ? strlen(str1) : 0;
  1378     size_t len2 = strlen(str2);
  1379     size_t len = len1 + len2 + 3;
  1380     char * result = realloc(str1, len + 1);
  1381 
  1382     if (result) {
  1383         result[len1] = '"';
  1384         strcpy(result + len1 + 1, str2);
  1385         result[len - 2] = '"';
  1386         result[len - 1] = delim;
  1387         result[len] = 0;
  1388     }
  1389     else {
  1390         free(str1);
  1391     }
  1392 
  1393     return result;
  1394 }
  1395 
  1396 DYNAMIC_API PEP_STATUS get_crashdump_log(
  1397         PEP_SESSION session,
  1398         int maxlines,
  1399         char **logdata
  1400     )
  1401 {
  1402     PEP_STATUS status = PEP_STATUS_OK;
  1403     char *_logdata= NULL;
  1404 
  1405     assert(session);
  1406     assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
  1407     assert(logdata);
  1408 
  1409     if (!(session && logdata && maxlines >= 0 && maxlines <=
  1410             CRASHDUMP_MAX_LINES))
  1411         return PEP_ILLEGAL_VALUE;
  1412 
  1413     *logdata = NULL;
  1414 
  1415     int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
  1416     const char *timestamp = NULL;
  1417     const char *title = NULL;
  1418     const char *entity = NULL;
  1419     const char *desc = NULL;
  1420     const char *comment = NULL;
  1421 
  1422     sqlite3_reset(session->crashdump);
  1423     sqlite3_bind_int(session->crashdump, 1, limit);
  1424 
  1425     int result;
  1426 
  1427     do {
  1428         result = sqlite3_step(session->crashdump);
  1429         switch (result) {
  1430         case SQLITE_ROW:
  1431             timestamp = (const char *) sqlite3_column_text(session->crashdump, 0);
  1432             title   = (const char *) sqlite3_column_text(session->crashdump, 1);
  1433             entity  = (const char *) sqlite3_column_text(session->crashdump, 2);
  1434             desc    = (const char *) sqlite3_column_text(session->crashdump, 3);
  1435             comment = (const char *) sqlite3_column_text(session->crashdump, 4);
  1436 
  1437             _logdata = _concat_string(_logdata, timestamp, ',');
  1438             if (_logdata == NULL)
  1439                 goto enomem;
  1440 
  1441             _logdata = _concat_string(_logdata, title, ',');
  1442             if (_logdata == NULL)
  1443                 goto enomem;
  1444 
  1445             _logdata = _concat_string(_logdata, entity, ',');
  1446             if (_logdata == NULL)
  1447                 goto enomem;
  1448 
  1449             _logdata = _concat_string(_logdata, desc, ',');
  1450             if (_logdata == NULL)
  1451                 goto enomem;
  1452 
  1453             _logdata = _concat_string(_logdata, comment, '\n');
  1454             if (_logdata == NULL)
  1455                 goto enomem;
  1456 
  1457             _clean_log_value(_logdata);
  1458             break;
  1459 
  1460         case SQLITE_DONE:
  1461             break;
  1462 
  1463         default:
  1464             status = PEP_UNKNOWN_ERROR;
  1465             result = SQLITE_DONE;
  1466         }
  1467     } while (result != SQLITE_DONE);
  1468 
  1469     sqlite3_reset(session->crashdump);
  1470     if (status == PEP_STATUS_OK)
  1471         *logdata = _logdata;
  1472 
  1473     goto the_end;
  1474 
  1475 enomem:
  1476     status = PEP_OUT_OF_MEMORY;
  1477 
  1478 the_end:
  1479     return status;
  1480 }
  1481 
  1482 DYNAMIC_API PEP_STATUS get_languagelist(
  1483         PEP_SESSION session,
  1484         char **languages
  1485     )
  1486 {
  1487     PEP_STATUS status = PEP_STATUS_OK;
  1488     char *_languages= NULL;
  1489 
  1490     assert(session);
  1491     assert(languages);
  1492 
  1493     if (!(session && languages))
  1494         return PEP_ILLEGAL_VALUE;
  1495 
  1496     *languages = NULL;
  1497 
  1498     const char *lang = NULL;
  1499     const char *name = NULL;
  1500     const char *phrase = NULL;
  1501 
  1502     sqlite3_reset(session->languagelist);
  1503 
  1504     int result;
  1505 
  1506     do {
  1507         result = sqlite3_step(session->languagelist);
  1508         switch (result) {
  1509         case SQLITE_ROW:
  1510             lang = (const char *) sqlite3_column_text(session->languagelist, 0);
  1511             name = (const char *) sqlite3_column_text(session->languagelist, 1);
  1512             phrase = (const char *) sqlite3_column_text(session->languagelist, 2);
  1513 
  1514             _languages = _concat_string(_languages, lang, ',');
  1515             if (_languages == NULL)
  1516                 goto enomem;
  1517 
  1518             _languages = _concat_string(_languages, name, ',');
  1519             if (_languages == NULL)
  1520                 goto enomem;
  1521 
  1522             _languages = _concat_string(_languages, phrase, '\n');
  1523             if (_languages == NULL)
  1524                 goto enomem;
  1525 
  1526             break;
  1527 
  1528         case SQLITE_DONE:
  1529             break;
  1530 
  1531         default:
  1532             status = PEP_UNKNOWN_ERROR;
  1533             result = SQLITE_DONE;
  1534         }
  1535     } while (result != SQLITE_DONE);
  1536 
  1537     sqlite3_reset(session->languagelist);
  1538     if (status == PEP_STATUS_OK)
  1539         *languages = _languages;
  1540 
  1541     goto the_end;
  1542 
  1543 enomem:
  1544     status = PEP_OUT_OF_MEMORY;
  1545 
  1546 the_end:
  1547     return status;
  1548 }
  1549 
  1550 DYNAMIC_API PEP_STATUS get_phrase(
  1551         PEP_SESSION session,
  1552         const char *lang,
  1553         int phrase_id,
  1554         char **phrase
  1555     )
  1556 {
  1557     PEP_STATUS status = PEP_STATUS_OK;
  1558 
  1559     assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
  1560     if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
  1561         return PEP_ILLEGAL_VALUE;
  1562 
  1563     *phrase = NULL;
  1564 
  1565     sqlite3_reset(session->i18n_token);
  1566     sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
  1567     sqlite3_bind_int(session->i18n_token, 2, phrase_id);
  1568 
  1569     const char *_phrase = NULL;
  1570     int result;
  1571 
  1572     result = sqlite3_step(session->i18n_token);
  1573     switch (result) {
  1574     case SQLITE_ROW:
  1575         _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
  1576         break;
  1577 
  1578     case SQLITE_DONE:
  1579         status = PEP_PHRASE_NOT_FOUND;
  1580         break;
  1581 
  1582     default:
  1583         status = PEP_UNKNOWN_ERROR;
  1584     }
  1585 
  1586     if (status == PEP_STATUS_OK) {
  1587         *phrase = strdup(_phrase);
  1588         if (*phrase == NULL)
  1589             goto enomem;
  1590     }
  1591 
  1592     sqlite3_reset(session->i18n_token);
  1593     goto the_end;
  1594 
  1595 enomem:
  1596     status = PEP_OUT_OF_MEMORY;
  1597 
  1598 the_end:
  1599     return status;
  1600 }
  1601 
  1602 DYNAMIC_API PEP_STATUS sequence_value(
  1603         PEP_SESSION session,
  1604         const char *name,
  1605         int32_t *value
  1606     )
  1607 {
  1608     PEP_STATUS status = PEP_STATUS_OK;
  1609     int result;
  1610 
  1611     assert(session);
  1612     assert(name);
  1613     assert(value);
  1614 
  1615     if (!(session && name && value))
  1616         return PEP_ILLEGAL_VALUE;
  1617 
  1618     *value = 0;
  1619 
  1620     sqlite3_reset(session->sequence_value1);
  1621     sqlite3_bind_text(session->sequence_value1, 1, name, -1, SQLITE_STATIC);
  1622     result = sqlite3_step(session->sequence_value1);
  1623     assert(result == SQLITE_DONE);
  1624     sqlite3_reset(session->sequence_value1);
  1625     if (result != SQLITE_DONE) {
  1626         status = PEP_UNKNOWN_ERROR;
  1627     }
  1628     else {
  1629         sqlite3_reset(session->sequence_value2);
  1630         sqlite3_bind_text(session->sequence_value2, 1, name, -1, SQLITE_STATIC);
  1631         result = sqlite3_step(session->sequence_value2);
  1632         switch (result) {
  1633             case SQLITE_ROW: {
  1634                 int32_t _value = (int32_t)
  1635                         sqlite3_column_int64(session->sequence_value2, 0);
  1636                 *value = _value;
  1637                 break;
  1638             }
  1639             default:
  1640                 status = PEP_UNKNOWN_ERROR;
  1641         }
  1642         sqlite3_reset(session->sequence_value2);
  1643     }
  1644     return status;
  1645 }
  1646 
  1647 DYNAMIC_API PEP_STATUS set_revoked(
  1648        PEP_SESSION session,
  1649        const char *revoked_fpr,
  1650        const char *replacement_fpr,
  1651        const uint64_t revocation_date
  1652     )
  1653 {
  1654     PEP_STATUS status = PEP_STATUS_OK;
  1655     
  1656     assert(session &&
  1657            revoked_fpr && revoked_fpr[0] &&
  1658            replacement_fpr && replacement_fpr[0]
  1659           );
  1660     
  1661     if (!(session &&
  1662           revoked_fpr && revoked_fpr[0] &&
  1663           replacement_fpr && replacement_fpr[0]
  1664          ))
  1665         return PEP_ILLEGAL_VALUE;
  1666     
  1667     sqlite3_reset(session->set_revoked);
  1668     sqlite3_bind_text(session->set_revoked, 1, revoked_fpr, -1, SQLITE_STATIC);
  1669     sqlite3_bind_text(session->set_revoked, 2, replacement_fpr, -1, SQLITE_STATIC);
  1670     sqlite3_bind_int64(session->set_revoked, 3, revocation_date);
  1671 
  1672     int result;
  1673     
  1674     result = sqlite3_step(session->set_revoked);
  1675     switch (result) {
  1676         case SQLITE_DONE:
  1677             status = PEP_STATUS_OK;
  1678             break;
  1679             
  1680         default:
  1681             status = PEP_UNKNOWN_ERROR;
  1682     }
  1683     
  1684     sqlite3_reset(session->set_revoked);
  1685     return status;
  1686 }
  1687 
  1688 DYNAMIC_API PEP_STATUS get_revoked(
  1689         PEP_SESSION session,
  1690         const char *fpr,
  1691         char **revoked_fpr,
  1692         uint64_t *revocation_date
  1693     )
  1694 {
  1695     PEP_STATUS status = PEP_STATUS_OK;
  1696 
  1697     assert(session &&
  1698            revoked_fpr &&
  1699            fpr && fpr[0]
  1700           );
  1701     
  1702     if (!(session &&
  1703            revoked_fpr &&
  1704            fpr && fpr[0]
  1705           ))
  1706         return PEP_ILLEGAL_VALUE;
  1707 
  1708     *revoked_fpr = NULL;
  1709     *revocation_date = 0;
  1710 
  1711     sqlite3_reset(session->get_revoked);
  1712     sqlite3_bind_text(session->get_revoked, 1, fpr, -1, SQLITE_STATIC);
  1713 
  1714     int result;
  1715     
  1716     result = sqlite3_step(session->get_revoked);
  1717     switch (result) {
  1718         case SQLITE_ROW: {
  1719             *revoked_fpr = strdup((const char *) sqlite3_column_text(session->get_revoked, 0));
  1720             if(*revoked_fpr)
  1721                 *revocation_date = sqlite3_column_int64(session->get_revoked, 1);
  1722             else
  1723                 status = PEP_OUT_OF_MEMORY;
  1724 
  1725             break;
  1726         }
  1727         default:
  1728             status = PEP_CANNOT_FIND_IDENTITY;
  1729     }
  1730 
  1731     sqlite3_reset(session->get_revoked);
  1732 
  1733     return status;
  1734 }
  1735 
  1736 DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
  1737 {
  1738     assert(session);
  1739 
  1740     if (!session)
  1741         return PEP_ILLEGAL_VALUE;
  1742 
  1743     int int_result = sqlite3_exec(
  1744         session->db,
  1745         "delete from identity where address like '%@peptest.ch' ;",
  1746         NULL,
  1747         NULL,
  1748         NULL
  1749     );
  1750     assert(int_result == SQLITE_OK);
  1751 
  1752     return PEP_STATUS_OK;
  1753 }
  1754