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