src/pEpEngine.c
author fdik
Thu, 03 Mar 2016 19:27:46 +0100
changeset 494 ddf4130d00a0
parent 482 cd6a24c7791d
child 514 ea9d5c281364
permissions -rw-r--r--
Added blacklist
vb@125
     1
#include "pEp_internal.h"
vb@98
     2
#include "dynamic_api.h"
vb@28
     3
#include "cryptotech.h"
vb@28
     4
#include "transport.h"
vb@0
     5
vb@221
     6
static int init_count = -1;
vb@62
     7
vb@0
     8
DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
vb@0
     9
{
vb@65
    10
    PEP_STATUS status = PEP_STATUS_OK;
vb@0
    11
	int int_result;
vb@62
    12
	static const char *sql_log;
vb@233
    13
	static const char *sql_trustword;
vb@62
    14
	static const char *sql_get_identity;
vb@62
    15
	static const char *sql_set_person;
vb@62
    16
	static const char *sql_set_pgp_keypair;
vb@62
    17
	static const char *sql_set_identity;
vb@62
    18
	static const char *sql_set_trust;
vb@62
    19
    static const char *sql_get_trust;
vb@251
    20
    static const char *sql_least_trust;
vb@357
    21
    static const char *sql_mark_as_compromized;
vb@450
    22
    static const char *sql_crashdump;
vb@458
    23
    static const char *sql_languagelist;
vb@458
    24
    static const char *sql_i18n_token;
fdik@494
    25
fdik@494
    26
    // blacklist
fdik@494
    27
    static const char *sql_blacklist_add;
fdik@494
    28
    static const char *sql_blacklist_delete;
fdik@494
    29
    static const char *sql_blacklist_is_listed;
fdik@494
    30
    static const char *sql_blacklist_retrieve;
vb@450
    31
vb@62
    32
    bool in_first = false;
vb@8
    33
vb@62
    34
    assert(sqlite3_threadsafe());
vb@62
    35
    if (!sqlite3_threadsafe())
vb@62
    36
        return PEP_INIT_SQLITE3_WITHOUT_MUTEX;
vb@62
    37
vb@62
    38
    // a little race condition - but still a race condition
vb@113
    39
    // mitigated by calling caveat (see documentation)
vb@62
    40
vb@62
    41
    ++init_count;
vb@67
    42
    if (init_count == 0)
vb@62
    43
        in_first = true;
vb@0
    44
vb@0
    45
	assert(session);
vb@191
    46
    if (session == NULL)
vb@191
    47
        return PEP_ILLEGAL_VALUE;
vb@191
    48
vb@0
    49
	*session = NULL;
vb@0
    50
vb@107
    51
    pEpSession *_session = calloc(1, sizeof(pEpSession));
vb@0
    52
	assert(_session);
vb@0
    53
	if (_session == NULL)
vb@65
    54
		goto enomem;
vb@62
    55
vb@0
    56
	_session->version = PEP_ENGINE_VERSION;
vb@0
    57
vb@0
    58
    assert(LOCAL_DB);
vb@0
    59
    if (LOCAL_DB == NULL) {
vb@65
    60
        status = PEP_INIT_CANNOT_OPEN_DB;
vb@65
    61
        goto pep_error;
vb@0
    62
    }
vb@0
    63
vb@0
    64
	int_result = sqlite3_open_v2(
vb@0
    65
			LOCAL_DB,
vb@0
    66
			&_session->db,
vb@0
    67
			SQLITE_OPEN_READWRITE
vb@0
    68
				| SQLITE_OPEN_CREATE
vb@0
    69
				| SQLITE_OPEN_FULLMUTEX
vb@0
    70
				| SQLITE_OPEN_PRIVATECACHE,
vb@0
    71
			NULL 
vb@0
    72
		);
vb@0
    73
vb@0
    74
	if (int_result != SQLITE_OK) {
vb@65
    75
		status = PEP_INIT_CANNOT_OPEN_DB;
vb@65
    76
        goto pep_error;
vb@0
    77
	}
vb@0
    78
vb@0
    79
	sqlite3_busy_timeout(_session->db, BUSY_WAIT_TIME);
vb@0
    80
vb@0
    81
    assert(SYSTEM_DB);
vb@0
    82
    if (SYSTEM_DB == NULL) {
vb@65
    83
		status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
vb@65
    84
        goto pep_error;
vb@0
    85
    }
vb@0
    86
vb@0
    87
	int_result = sqlite3_open_v2(
vb@0
    88
			SYSTEM_DB, &_session->system_db,
vb@0
    89
			SQLITE_OPEN_READONLY
vb@0
    90
				| SQLITE_OPEN_FULLMUTEX
vb@0
    91
				| SQLITE_OPEN_SHAREDCACHE,
vb@0
    92
			NULL
vb@0
    93
		);
vb@0
    94
vb@0
    95
	if (int_result != SQLITE_OK) {
vb@65
    96
		status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
vb@65
    97
        goto pep_error;
vb@0
    98
	}
vb@0
    99
vb@0
   100
	sqlite3_busy_timeout(_session->system_db, 1000);
vb@0
   101
vb@62
   102
    if (in_first) {
vb@62
   103
        int_result = sqlite3_exec(
vb@62
   104
            _session->db,
vb@456
   105
                "create table if not exists version_info (\n"
vb@456
   106
                "   id integer primary key,\n"
vb@456
   107
                "   timestamp integer default (datetime('now')) ,\n"
vb@456
   108
                "   version text,\n"
vb@456
   109
                "   comment text\n"
vb@456
   110
                ");\n"
vb@456
   111
                "create table if not exists log (\n"
vb@456
   112
                "   timestamp integer default (datetime('now')) ,\n"
vb@456
   113
                "   title text not null,\n"
vb@456
   114
                "   entity text not null,\n"
vb@456
   115
                "   description text,\n"
vb@456
   116
                "   comment text\n"
vb@456
   117
                ");\n"
vb@456
   118
                "create index if not exists log_timestamp on log (\n"
vb@456
   119
                "   timestamp\n"
vb@456
   120
                ");\n"
vb@456
   121
                "create table if not exists pgp_keypair (\n"
vb@456
   122
                "   fpr text primary key,\n"
vb@456
   123
                "   public_id text unique,\n"
vb@456
   124
                "   private_id text,\n"
vb@456
   125
                "   created integer,\n"
vb@456
   126
                "   expires integer,\n"
vb@456
   127
                "   comment text\n"
vb@456
   128
                ");\n"
vb@456
   129
                "create index if not exists pgp_keypair_expires on pgp_keypair (\n"
vb@456
   130
                "   expires\n"
vb@456
   131
                ");\n"
vb@456
   132
                "create table if not exists person (\n"
vb@456
   133
                "   id text primary key,\n"
vb@456
   134
                "   username text not null,\n"
vb@456
   135
                "   main_key_id text\n"
vb@456
   136
                "       references pgp_keypair (fpr)\n"
vb@456
   137
                "       on delete set null,\n"
vb@456
   138
                "   lang text,\n"
vb@456
   139
                "   comment text\n"
vb@456
   140
                ");\n"
vb@456
   141
                "create table if not exists identity (\n"
vb@456
   142
                "   address text primary key,\n"
vb@456
   143
                "   user_id text\n"
vb@456
   144
                "       references person (id)\n"
vb@456
   145
                "       on delete cascade,\n"
vb@456
   146
                "   main_key_id text\n"
vb@456
   147
                "       references pgp_keypair (fpr)\n"
vb@456
   148
                "       on delete set null,\n"
vb@456
   149
                "   comment text\n"
vb@456
   150
                ");\n"
vb@456
   151
                "create table if not exists trust (\n"
vb@456
   152
                "   user_id text not null\n"
vb@456
   153
                "       references person (id)\n"
vb@456
   154
                "       on delete cascade,\n"
vb@456
   155
                "   pgp_keypair_fpr text not null\n"
vb@456
   156
                "       references pgp_keypair (fpr)\n"
vb@456
   157
                "       on delete cascade,\n"
vb@456
   158
                "   comm_type integer not null,\n"
vb@456
   159
                "   comment text\n"
vb@456
   160
                ");\n"
vb@456
   161
                "create unique index if not exists trust_index on trust (\n"
vb@456
   162
                "   user_id,\n"
vb@456
   163
                "   pgp_keypair_fpr\n"
vb@456
   164
                ");\n"
fdik@494
   165
                // blacklist
vb@456
   166
                "create table if not exists blacklist_keys (\n"
vb@456
   167
                "   fpr text primary key\n"
vb@456
   168
                ");\n"
vb@456
   169
                ,
vb@62
   170
            NULL,
vb@62
   171
            NULL,
vb@62
   172
            NULL
vb@62
   173
        );
vb@62
   174
        assert(int_result == SQLITE_OK);
vb@0
   175
vb@62
   176
        int_result = sqlite3_exec(
vb@62
   177
            _session->db,
fdik@494
   178
            "insert or replace into version_info (id, version) values (1, '1.1');",
vb@62
   179
            NULL,
vb@62
   180
            NULL,
vb@62
   181
            NULL
vb@62
   182
        );
vb@62
   183
        assert(int_result == SQLITE_OK);
vb@0
   184
vb@62
   185
        sql_log = "insert into log (title, entity, description, comment)"
vb@62
   186
                  "values (?1, ?2, ?3, ?4);";
vb@62
   187
vb@62
   188
        sql_get_identity =	"select fpr, identity.user_id, username, comm_type, lang"
vb@62
   189
                            "   from identity"
vb@62
   190
                            "   join person on id = identity.user_id"
vb@62
   191
                            "   join pgp_keypair on fpr = identity.main_key_id"
vb@62
   192
                            "   join trust on id = trust.user_id"
vb@62
   193
                            "       and pgp_keypair_fpr = identity.main_key_id"
vb@62
   194
                            "   where address = ?1 ;";
vb@62
   195
vb@233
   196
        sql_trustword = "select id, word from wordlist where lang = lower(?1) "
vb@62
   197
                       "and id = ?2 ;";
vb@62
   198
vb@62
   199
        sql_set_person = "insert or replace into person (id, username, lang) "
vb@62
   200
                         "values (?1, ?2, ?3) ;";
vb@62
   201
vb@62
   202
        sql_set_pgp_keypair = "insert or replace into pgp_keypair (fpr) "
vb@62
   203
                              "values (?1) ;";
vb@62
   204
vb@62
   205
        sql_set_identity = "insert or replace into identity (address, main_key_id, "
vb@62
   206
                           "user_id) values (?1, ?2, ?3) ;";
vb@62
   207
vb@62
   208
        sql_set_trust = "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type) "
vb@62
   209
                        "values (?1, ?2, ?3) ;";
vb@62
   210
vb@62
   211
        sql_get_trust = "select user_id, comm_type from trust where user_id = ?1 "
vb@62
   212
                        "and pgp_keypair_fpr = ?2 ;";
vb@251
   213
vb@251
   214
        sql_least_trust = "select min(comm_type) from trust where pgp_keypair_fpr = ?1 ;";
vb@357
   215
vb@457
   216
        sql_mark_as_compromized = "update trust not indexed set comm_type = 15"
vb@457
   217
                                  " where pgp_keypair_fpr = ?1 ;";
vb@450
   218
vb@457
   219
        sql_crashdump = "select timestamp, title, entity, description, comment"
vb@457
   220
                        " from log order by timestamp desc limit ?1 ;";
vb@458
   221
vb@458
   222
        sql_languagelist = "select lang, name from i18n_language order by lang ;";
vb@458
   223
vb@461
   224
        sql_i18n_token = "select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
vb@482
   225
fdik@494
   226
        // blacklist
fdik@494
   227
fdik@494
   228
        sql_blacklist_add = "insert or replace into blacklist_keys (fpr) values (?1) ;";
fdik@494
   229
        sql_blacklist_delete = "delete from blacklist_keys where fpr = ?1 ;";
fdik@494
   230
        sql_blacklist_is_listed = "select count(*) from blacklist_keys where fpr = ?1 ;";
fdik@494
   231
        sql_blacklist_retrieve = "select * from blacklist_keys ;";
vb@62
   232
    }
vb@62
   233
Edouard@417
   234
    int_result = sqlite3_prepare_v2(_session->db, sql_log, (int)strlen(sql_log),
vb@0
   235
            &_session->log, NULL);
vb@0
   236
	assert(int_result == SQLITE_OK);
vb@0
   237
vb@233
   238
    int_result = sqlite3_prepare_v2(_session->system_db, sql_trustword,
Edouard@417
   239
            (int)strlen(sql_trustword), &_session->trustword, NULL);
vb@0
   240
	assert(int_result == SQLITE_OK);
vb@0
   241
vb@0
   242
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
Edouard@417
   243
            (int)strlen(sql_get_identity), &_session->get_identity, NULL);
vb@0
   244
	assert(int_result == SQLITE_OK);
vb@0
   245
vb@0
   246
    int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
Edouard@417
   247
            (int)strlen(sql_set_person), &_session->set_person, NULL);
vb@0
   248
    assert(int_result == SQLITE_OK);
vb@62
   249
vb@0
   250
    int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
Edouard@417
   251
            (int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair, NULL);
vb@0
   252
	assert(int_result == SQLITE_OK);
vb@62
   253
vb@0
   254
    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity,
Edouard@417
   255
            (int)strlen(sql_set_identity), &_session->set_identity, NULL);
vb@0
   256
	assert(int_result == SQLITE_OK);
vb@62
   257
vb@0
   258
    int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
Edouard@417
   259
            (int)strlen(sql_set_trust), &_session->set_trust, NULL);
vb@0
   260
	assert(int_result == SQLITE_OK);
vb@62
   261
vb@8
   262
    int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
Edouard@417
   263
            (int)strlen(sql_get_trust), &_session->get_trust, NULL);
vb@8
   264
    assert(int_result == SQLITE_OK);
vb@0
   265
vb@251
   266
    int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
Edouard@417
   267
            (int)strlen(sql_least_trust), &_session->least_trust, NULL);
vb@251
   268
    assert(int_result == SQLITE_OK);
vb@251
   269
vb@357
   270
    int_result = sqlite3_prepare_v2(_session->db, sql_mark_as_compromized,
Edouard@417
   271
            (int)strlen(sql_mark_as_compromized), &_session->mark_compromized, NULL);
vb@357
   272
    assert(int_result == SQLITE_OK);
vb@357
   273
vb@453
   274
    int_result = sqlite3_prepare_v2(_session->db, sql_crashdump,
vb@453
   275
            (int)strlen(sql_crashdump), &_session->crashdump, NULL);
vb@453
   276
    assert(int_result == SQLITE_OK);
vb@453
   277
vb@458
   278
    int_result = sqlite3_prepare_v2(_session->system_db, sql_languagelist,
vb@458
   279
            (int)strlen(sql_languagelist), &_session->languagelist, NULL);
vb@458
   280
	assert(int_result == SQLITE_OK);
vb@458
   281
vb@458
   282
    int_result = sqlite3_prepare_v2(_session->system_db, sql_i18n_token,
vb@458
   283
            (int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
vb@458
   284
	assert(int_result == SQLITE_OK);
vb@458
   285
fdik@494
   286
    // blacklist
fdik@494
   287
fdik@494
   288
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_add,
fdik@494
   289
            (int)strlen(sql_blacklist_add), &_session->blacklist_add, NULL);
fdik@494
   290
    assert(int_result == SQLITE_OK);
fdik@494
   291
fdik@494
   292
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_delete,
fdik@494
   293
            (int)strlen(sql_blacklist_delete), &_session->blacklist_delete, NULL);
fdik@494
   294
    assert(int_result == SQLITE_OK);
fdik@494
   295
fdik@494
   296
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_is_listed,
fdik@494
   297
            (int)strlen(sql_blacklist_is_listed), &_session->blacklist_is_listed, NULL);
fdik@494
   298
    assert(int_result == SQLITE_OK);
fdik@494
   299
fdik@494
   300
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_retrieve,
fdik@494
   301
            (int)strlen(sql_blacklist_retrieve), &_session->blacklist_retrieve, NULL);
vb@482
   302
    assert(int_result == SQLITE_OK);
vb@482
   303
vb@65
   304
    status = init_cryptotech(_session, in_first);
vb@65
   305
    if (status != PEP_STATUS_OK)
vb@65
   306
        goto pep_error;
vb@0
   307
vb@65
   308
    status = init_transport_system(_session, in_first);
vb@65
   309
    if (status != PEP_STATUS_OK)
vb@65
   310
        goto pep_error;
vb@62
   311
vb@65
   312
    status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
vb@65
   313
    if (status != PEP_STATUS_OK)
vb@65
   314
        goto pep_error;
vb@65
   315
vb@464
   316
    // runtime config
vb@464
   317
vb@464
   318
    _session->passive_mode = false;
vb@464
   319
    _session->unencrypted_subject = false;
vb@464
   320
vb@65
   321
	*session = _session;
vb@0
   322
	return PEP_STATUS_OK;
vb@65
   323
vb@65
   324
enomem:
vb@65
   325
    status = PEP_OUT_OF_MEMORY;
vb@65
   326
vb@65
   327
pep_error:
vb@65
   328
    release(_session);
vb@65
   329
    return status;
vb@0
   330
}
vb@0
   331
vb@0
   332
DYNAMIC_API void release(PEP_SESSION session)
vb@0
   333
{
vb@62
   334
    bool out_last = false;
vb@62
   335
vb@62
   336
    assert(init_count >= 0);
vb@0
   337
	assert(session);
vb@0
   338
Edouard@385
   339
    if (!((init_count >= 0) && session))
vb@191
   340
        return;
vb@191
   341
vb@62
   342
    // a small race condition but still a race condition
vb@113
   343
    // mitigated by calling caveat (see documentation)
vb@62
   344
vb@62
   345
    if (init_count == 0)
vb@62
   346
        out_last = true;
vb@62
   347
    --init_count;
vb@62
   348
vb@46
   349
	if (session) {
vb@46
   350
		if (session->db) {
vb@233
   351
            if (session->trustword)
vb@233
   352
                sqlite3_finalize(session->trustword);
vb@65
   353
            if (session->log)
vb@65
   354
                sqlite3_finalize(session->log);
vb@65
   355
            if (session->get_identity)
vb@65
   356
                sqlite3_finalize(session->get_identity);
vb@65
   357
            if (session->set_identity)
vb@65
   358
                sqlite3_finalize(session->set_identity);
vb@65
   359
            if (session->set_person)
vb@65
   360
                sqlite3_finalize(session->set_person);
vb@65
   361
            if (session->set_pgp_keypair)
vb@65
   362
                sqlite3_finalize(session->set_pgp_keypair);
vb@65
   363
            if (session->set_trust)
vb@65
   364
                sqlite3_finalize(session->set_trust);
vb@65
   365
            if (session->get_trust)
vb@65
   366
                sqlite3_finalize(session->get_trust);
vb@251
   367
            if (session->least_trust)
vb@251
   368
                sqlite3_finalize(session->least_trust);
vb@26
   369
vb@65
   370
            if (session->db)
vb@65
   371
                sqlite3_close_v2(session->db);
vb@65
   372
            if (session->system_db)
vb@65
   373
                sqlite3_close_v2(session->system_db);
vb@0
   374
		}
vb@65
   375
vb@65
   376
        release_transport_system(session, out_last);
vb@65
   377
        release_cryptotech(session, out_last);
vb@65
   378
vb@65
   379
        free(session);
vb@62
   380
    }
vb@0
   381
}
vb@0
   382
vb@467
   383
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
vb@464
   384
{
vb@464
   385
    assert(session);
vb@467
   386
    session->passive_mode = enable;
vb@464
   387
}
vb@464
   388
vb@467
   389
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
vb@464
   390
{
vb@464
   391
    assert(session);
vb@467
   392
    session->unencrypted_subject = enable;
vb@464
   393
}
vb@464
   394
vb@0
   395
DYNAMIC_API PEP_STATUS log_event(
vb@450
   396
        PEP_SESSION session,
vb@451
   397
        const char *title,
vb@451
   398
        const char *entity,
vb@451
   399
        const char *description,
vb@451
   400
        const char *comment
vb@0
   401
    )
vb@0
   402
{
vb@0
   403
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   404
	int result;
vb@0
   405
vb@46
   406
	assert(session);
vb@0
   407
	assert(title);
vb@0
   408
	assert(entity);
vb@0
   409
vb@191
   410
    if (!(session && title && entity))
vb@191
   411
        return PEP_ILLEGAL_VALUE;
vb@191
   412
vb@46
   413
	sqlite3_reset(session->log);
vb@46
   414
	sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
vb@46
   415
	sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
vb@0
   416
	if (description)
vb@46
   417
        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
vb@0
   418
	else
vb@46
   419
		sqlite3_bind_null(session->log, 3);
vb@0
   420
	if (comment)
vb@46
   421
		sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
vb@0
   422
	else
vb@46
   423
		sqlite3_bind_null(session->log, 4);
vb@0
   424
	do {
vb@46
   425
		result = sqlite3_step(session->log);
vb@0
   426
		assert(result == SQLITE_DONE || result == SQLITE_BUSY);
vb@0
   427
		if (result != SQLITE_DONE && result != SQLITE_BUSY)
vb@0
   428
			status = PEP_UNKNOWN_ERROR;
vb@0
   429
	} while (result == SQLITE_BUSY);
vb@46
   430
	sqlite3_reset(session->log);
vb@0
   431
vb@0
   432
	return status;
vb@0
   433
}
vb@0
   434
vb@233
   435
DYNAMIC_API PEP_STATUS trustword(
vb@0
   436
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
   437
            char **word, size_t *wsize
vb@0
   438
        )
vb@0
   439
{
vb@0
   440
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   441
	int result;
vb@0
   442
vb@46
   443
	assert(session);
vb@0
   444
	assert(word);
vb@0
   445
	assert(wsize);
vb@0
   446
vb@191
   447
    if (!(session && word && wsize))
vb@191
   448
        return PEP_ILLEGAL_VALUE;
vb@191
   449
vb@0
   450
	*word = NULL;
vb@0
   451
	*wsize = 0;
vb@0
   452
vb@0
   453
	if (lang == NULL)
vb@0
   454
		lang = "en";
vb@0
   455
vb@0
   456
	assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   457
            || (lang[0] >= 'a' && lang[0] <= 'z'));
vb@0
   458
	assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   459
            || (lang[1] >= 'a' && lang[1] <= 'z'));
vb@0
   460
	assert(lang[2] == 0);
vb@0
   461
vb@233
   462
	sqlite3_reset(session->trustword);
vb@233
   463
    sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
vb@233
   464
	sqlite3_bind_int(session->trustword, 2, value);
vb@0
   465
vb@233
   466
	result = sqlite3_step(session->trustword);
vb@0
   467
	if (result == SQLITE_ROW) {
vb@233
   468
        *word = strdup((const char *) sqlite3_column_text(session->trustword,
vb@0
   469
                    1));
vb@0
   470
		if (*word)
vb@233
   471
            *wsize = sqlite3_column_bytes(session->trustword, 1);
vb@0
   472
		else
vb@251
   473
			status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
   474
	} else
vb@251
   475
		status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
   476
vb@233
   477
	sqlite3_reset(session->trustword);
vb@0
   478
	return status;
vb@0
   479
}
vb@0
   480
vb@233
   481
DYNAMIC_API PEP_STATUS trustwords(
vb@0
   482
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
   483
        char **words, size_t *wsize, int max_words
vb@0
   484
    )
vb@0
   485
{
vb@0
   486
	const char *source = fingerprint;
vb@109
   487
	char *buffer;
vb@125
   488
	char *dest;
vb@0
   489
	size_t fsize;
vb@0
   490
    PEP_STATUS _status;
vb@0
   491
vb@0
   492
	assert(session);
vb@0
   493
	assert(fingerprint);
vb@0
   494
	assert(words);
vb@0
   495
	assert(wsize);
vb@0
   496
	assert(max_words >= 0);
vb@0
   497
vb@191
   498
    if (!(session && fingerprint && words && wsize && max_words >= 0))
vb@191
   499
        return PEP_ILLEGAL_VALUE;
vb@191
   500
vb@0
   501
	*words = NULL;
vb@0
   502
	*wsize = 0;
vb@0
   503
vb@251
   504
    buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
vb@0
   505
    assert(buffer);
vb@0
   506
    if (buffer == NULL)
vb@0
   507
        return PEP_OUT_OF_MEMORY;
vb@125
   508
    dest = buffer;
vb@0
   509
vb@0
   510
	fsize = strlen(fingerprint);
vb@0
   511
vb@339
   512
	if (!lang || !lang[0])
vb@0
   513
		lang = "en";
vb@0
   514
vb@0
   515
	assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   516
            || (lang[0] >= 'a' && lang[0] <= 'z'));
vb@0
   517
	assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   518
            || (lang[1] >= 'a' && lang[1] <= 'z'));
vb@0
   519
	assert(lang[2] == 0);
vb@0
   520
vb@0
   521
	int n_words = 0;
vb@0
   522
	while (source < fingerprint + fsize) {
vb@0
   523
		uint16_t value;
vb@0
   524
		char *word;
vb@0
   525
		size_t _wsize;
vb@0
   526
		int j;
vb@0
   527
vb@0
   528
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
vb@0
   529
			if (*source >= 'a' && *source <= 'f')
vb@0
   530
				value += (*source - 'a' + 10) << (3 - j++) * 4;
vb@0
   531
			else if (*source >= 'A' && *source <= 'F')
vb@0
   532
				value += (*source - 'A' + 10) << (3 - j++) * 4;
vb@0
   533
			else if (*source >= '0' && *source <= '9')
vb@0
   534
				value += (*source - '0') << (3 - j++) * 4;
vb@0
   535
			
vb@0
   536
			source++;
vb@0
   537
		}
vb@0
   538
vb@233
   539
		_status = trustword(session, value, lang, &word, &_wsize);
vb@0
   540
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
   541
            free(buffer);
vb@0
   542
            return PEP_OUT_OF_MEMORY;
vb@0
   543
        }
vb@0
   544
		if (word == NULL) {
vb@0
   545
            free(buffer);
vb@251
   546
			return PEP_TRUSTWORD_NOT_FOUND;
vb@0
   547
        }
vb@0
   548
vb@251
   549
		if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
vb@0
   550
			strncpy(dest, word, _wsize);
vb@0
   551
            free(word);
vb@0
   552
			dest += _wsize;
vb@0
   553
		}
vb@0
   554
		else {
vb@0
   555
            free(word);
vb@0
   556
			break; // buffer full
vb@0
   557
        }
vb@0
   558
vb@0
   559
		if (source < fingerprint + fsize
vb@251
   560
                && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
vb@0
   561
			*dest++ = ' ';
vb@0
   562
vb@0
   563
		++n_words;
vb@0
   564
		if (max_words && n_words >= max_words)
vb@0
   565
			break;
vb@0
   566
	}
vb@0
   567
vb@0
   568
	*words = buffer;
vb@0
   569
	*wsize = dest - buffer;
vb@0
   570
	return PEP_STATUS_OK;
vb@0
   571
}
vb@0
   572
vb@0
   573
pEp_identity *new_identity(
vb@0
   574
        const char *address, const char *fpr, const char *user_id,
vb@0
   575
        const char *username
vb@0
   576
    )
vb@0
   577
{
vb@0
   578
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
   579
    assert(result);
vb@0
   580
    if (result) {
vb@0
   581
        if (address) {
vb@0
   582
            result->address = strdup(address);
vb@0
   583
            assert(result->address);
vb@0
   584
            if (result->address == NULL) {
vb@0
   585
                free(result);
vb@0
   586
                return NULL;
vb@0
   587
            }
vb@0
   588
            result->address_size = strlen(address);
vb@0
   589
        }
vb@0
   590
        if (fpr) {
vb@0
   591
            result->fpr = strdup(fpr);
vb@0
   592
            assert(result->fpr);
vb@0
   593
            if (result->fpr == NULL) {
vb@0
   594
                free_identity(result);
vb@0
   595
                return NULL;
vb@0
   596
            }
vb@0
   597
            result->fpr_size = strlen(fpr);
vb@0
   598
        }
vb@0
   599
        if (user_id) {
vb@0
   600
            result->user_id = strdup(user_id);
vb@0
   601
            assert(result->user_id);
vb@0
   602
            if (result->user_id == NULL) {
vb@0
   603
                free_identity(result);
vb@0
   604
                return NULL;
vb@0
   605
            }
vb@0
   606
            result->user_id_size = strlen(user_id);
vb@0
   607
        }
vb@0
   608
        if (username) {
vb@0
   609
            result->username = strdup(username);
vb@0
   610
            assert(result->username);
vb@0
   611
            if (result->username == NULL) {
vb@0
   612
                free_identity(result);
vb@0
   613
                return NULL;
vb@0
   614
            }
vb@0
   615
            result->username_size = strlen(username);
vb@0
   616
        }
vb@0
   617
        result->struct_size = sizeof(pEp_identity);
vb@0
   618
    }
vb@0
   619
    return result;
vb@0
   620
}
vb@0
   621
vb@37
   622
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
   623
{
vb@37
   624
    assert(src);
vb@37
   625
vb@37
   626
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
vb@37
   627
    assert(dup);
vb@37
   628
    if (dup == NULL)
vb@37
   629
        return NULL;
vb@37
   630
    
vb@37
   631
    dup->comm_type = src->comm_type;
vb@37
   632
    dup->lang[0] = src->lang[0];
vb@37
   633
    dup->lang[1] = src->lang[1];
vb@37
   634
    dup->lang[2] = 0;
vb@37
   635
    dup->me = src->me;
vb@37
   636
vb@37
   637
    return dup;
vb@37
   638
}
vb@37
   639
vb@0
   640
void free_identity(pEp_identity *identity)
vb@0
   641
{
vb@0
   642
    if (identity) {
vb@0
   643
        free(identity->address);
vb@0
   644
        free(identity->fpr);
vb@0
   645
        free(identity->user_id);
vb@0
   646
        free(identity->username);
vb@0
   647
        free(identity);
vb@0
   648
    }
vb@0
   649
}
vb@0
   650
vb@0
   651
DYNAMIC_API PEP_STATUS get_identity(
vb@0
   652
        PEP_SESSION session, const char *address,
vb@0
   653
        pEp_identity **identity
vb@0
   654
    )
vb@0
   655
{
vb@0
   656
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   657
	static pEp_identity *_identity;
vb@0
   658
	int result;
vb@0
   659
	const char *_lang;
vb@0
   660
vb@0
   661
	assert(session);
vb@0
   662
	assert(address);
vb@8
   663
    assert(address[0]);
vb@0
   664
vb@191
   665
    if (!(session && address && address[0]))
vb@191
   666
        return PEP_ILLEGAL_VALUE;
vb@191
   667
vb@46
   668
    sqlite3_reset(session->get_identity);
vb@46
   669
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
vb@0
   670
vb@46
   671
    result = sqlite3_step(session->get_identity);
vb@0
   672
	switch (result) {
vb@0
   673
	case SQLITE_ROW:
vb@0
   674
        _identity = new_identity(
vb@0
   675
                address,
vb@46
   676
                (const char *) sqlite3_column_text(session->get_identity, 0),
vb@46
   677
                (const char *) sqlite3_column_text(session->get_identity, 1),
vb@46
   678
                (const char *) sqlite3_column_text(session->get_identity, 2)
vb@0
   679
                );
vb@0
   680
        assert(_identity);
vb@0
   681
        if (_identity == NULL)
vb@0
   682
            return PEP_OUT_OF_MEMORY;
vb@0
   683
vb@46
   684
        _identity->comm_type = (PEP_comm_type) sqlite3_column_int(session->get_identity, 3);
vb@46
   685
        _lang = (const char *) sqlite3_column_text(session->get_identity, 4);
vb@0
   686
        if (_lang && _lang[0]) {
vb@0
   687
			assert(_lang[0] >= 'a' && _lang[0] <= 'z');
vb@0
   688
			assert(_lang[1] >= 'a' && _lang[1] <= 'z');
vb@0
   689
			assert(_lang[2] == 0);
vb@0
   690
			_identity->lang[0] = _lang[0];
vb@0
   691
			_identity->lang[1] = _lang[1];
vb@0
   692
            _identity->lang[2] = 0;
vb@0
   693
		}
vb@0
   694
		*identity = _identity;
vb@0
   695
		break;
vb@0
   696
	default:
vb@0
   697
        status = PEP_CANNOT_FIND_IDENTITY;
vb@0
   698
		*identity = NULL;
vb@0
   699
	}
vb@0
   700
vb@46
   701
    sqlite3_reset(session->get_identity);
vb@0
   702
	return status;
vb@0
   703
}
vb@0
   704
vb@0
   705
DYNAMIC_API PEP_STATUS set_identity(
vb@0
   706
        PEP_SESSION session, const pEp_identity *identity
vb@0
   707
    )
vb@0
   708
{
vb@0
   709
	int result;
vb@0
   710
vb@0
   711
	assert(session);
vb@0
   712
	assert(identity);
vb@0
   713
	assert(identity->address);
vb@0
   714
	assert(identity->fpr);
vb@0
   715
	assert(identity->user_id);
vb@0
   716
	assert(identity->username);
vb@0
   717
vb@191
   718
    if (!(session && identity && identity->address && identity->fpr &&
vb@191
   719
                identity->user_id && identity->username))
vb@191
   720
        return PEP_ILLEGAL_VALUE;
vb@191
   721
vb@46
   722
	sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
   723
vb@46
   724
	sqlite3_reset(session->set_person);
vb@46
   725
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
   726
            SQLITE_STATIC);
vb@46
   727
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
   728
            SQLITE_STATIC);
vb@0
   729
	if (identity->lang[0])
vb@46
   730
        sqlite3_bind_text(session->set_person, 3, identity->lang, 1,
vb@0
   731
                SQLITE_STATIC);
vb@0
   732
	else
vb@46
   733
		sqlite3_bind_null(session->set_person, 3);
vb@46
   734
	result = sqlite3_step(session->set_person);
vb@46
   735
	sqlite3_reset(session->set_person);
vb@0
   736
	if (result != SQLITE_DONE) {
vb@46
   737
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   738
		return PEP_CANNOT_SET_PERSON;
vb@0
   739
	}
vb@0
   740
vb@46
   741
	sqlite3_reset(session->set_pgp_keypair);
vb@46
   742
    sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
vb@0
   743
            SQLITE_STATIC);
vb@46
   744
	result = sqlite3_step(session->set_pgp_keypair);
vb@46
   745
	sqlite3_reset(session->set_pgp_keypair);
vb@0
   746
	if (result != SQLITE_DONE) {
vb@46
   747
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   748
		return PEP_CANNOT_SET_PGP_KEYPAIR;
vb@0
   749
	}
vb@0
   750
vb@46
   751
	sqlite3_reset(session->set_identity);
vb@46
   752
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
   753
            SQLITE_STATIC);
vb@46
   754
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
   755
            SQLITE_STATIC);
vb@46
   756
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
   757
            SQLITE_STATIC);
vb@46
   758
	result = sqlite3_step(session->set_identity);
vb@46
   759
	sqlite3_reset(session->set_identity);
vb@0
   760
	if (result != SQLITE_DONE) {
vb@46
   761
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   762
		return PEP_CANNOT_SET_IDENTITY;
vb@0
   763
	}
vb@0
   764
vb@46
   765
	sqlite3_reset(session->set_trust);
vb@46
   766
    sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
vb@0
   767
            SQLITE_STATIC);
vb@46
   768
    sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
vb@0
   769
            SQLITE_STATIC);
vb@46
   770
	sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
vb@46
   771
	result = sqlite3_step(session->set_trust);
vb@46
   772
	sqlite3_reset(session->set_trust);
vb@0
   773
	if (result != SQLITE_DONE) {
vb@46
   774
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   775
		return PEP_CANNOT_SET_IDENTITY;
vb@0
   776
	}
vb@0
   777
vb@46
   778
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
vb@0
   779
	if (result == SQLITE_OK)
vb@0
   780
		return PEP_STATUS_OK;
vb@0
   781
	else
vb@0
   782
		return PEP_COMMIT_FAILED;
vb@0
   783
}
vb@0
   784
vb@357
   785
DYNAMIC_API PEP_STATUS mark_as_compromized(
vb@357
   786
        PEP_SESSION session,
vb@357
   787
        const char *fpr
vb@357
   788
    )
vb@357
   789
{
vb@357
   790
	int result;
vb@357
   791
vb@357
   792
	assert(session);
vb@357
   793
    assert(fpr && fpr[0]);
vb@357
   794
vb@357
   795
    if (!(session && fpr && fpr[0]))
vb@357
   796
        return PEP_ILLEGAL_VALUE;
vb@357
   797
vb@357
   798
	sqlite3_reset(session->mark_compromized);
vb@357
   799
    sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
vb@357
   800
            SQLITE_STATIC);
vb@357
   801
    result = sqlite3_step(session->mark_compromized);
vb@357
   802
	sqlite3_reset(session->mark_compromized);
vb@357
   803
vb@357
   804
    if (result != SQLITE_DONE)
vb@357
   805
        return PEP_CANNOT_SET_IDENTITY;
vb@357
   806
vb@357
   807
    return PEP_STATUS_OK;
vb@357
   808
}
vb@357
   809
vb@0
   810
void pEp_free(void *p)
vb@0
   811
{
vb@0
   812
    free(p);
vb@0
   813
}
vb@0
   814
vb@8
   815
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
   816
{
vb@8
   817
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
   818
    int result;
vb@8
   819
vb@8
   820
    assert(session);
vb@8
   821
    assert(identity);
vb@8
   822
    assert(identity->user_id);
vb@8
   823
    assert(identity->user_id[0]);
vb@8
   824
    assert(identity->fpr);
vb@8
   825
    assert(identity->fpr[0]);
vb@8
   826
vb@191
   827
    if (!(session && identity && identity->user_id && identity->user_id[0] &&
vb@191
   828
                identity->fpr && identity->fpr[0]))
vb@191
   829
        return PEP_ILLEGAL_VALUE;
vb@191
   830
vb@8
   831
    identity->comm_type = PEP_ct_unknown;
vb@8
   832
vb@46
   833
    sqlite3_reset(session->get_trust);
vb@46
   834
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
vb@46
   835
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
   836
vb@46
   837
    result = sqlite3_step(session->get_trust);
vb@8
   838
    switch (result) {
vb@8
   839
    case SQLITE_ROW: {
vb@259
   840
        const char * user_id = (const char *) sqlite3_column_text(session->get_trust, 0);
vb@259
   841
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 1);
vb@8
   842
vb@8
   843
        if (strcmp(user_id, identity->user_id) != 0) {
vb@8
   844
            free(identity->user_id);
vb@8
   845
            identity->user_id = strdup(user_id);
vb@8
   846
            assert(identity->user_id);
vb@8
   847
            if (identity->user_id == NULL)
vb@8
   848
                return PEP_OUT_OF_MEMORY;
vb@8
   849
        }
vb@8
   850
        identity->comm_type = comm_type;
vb@8
   851
        break;
vb@8
   852
    }
vb@8
   853
 
vb@8
   854
    default:
vb@8
   855
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
   856
    }
vb@8
   857
vb@46
   858
    sqlite3_reset(session->get_trust);
vb@8
   859
    return status;
vb@9
   860
}
vb@9
   861
vb@251
   862
DYNAMIC_API PEP_STATUS least_trust(
vb@251
   863
        PEP_SESSION session,
vb@251
   864
        const char *fpr,
vb@251
   865
        PEP_comm_type *comm_type
vb@251
   866
    )
vb@251
   867
{
vb@251
   868
    PEP_STATUS status = PEP_STATUS_OK;
vb@251
   869
    int result;
vb@251
   870
vb@251
   871
    assert(session);
vb@251
   872
    assert(fpr);
vb@251
   873
    assert(comm_type);
vb@251
   874
vb@259
   875
    *comm_type = PEP_ct_unknown;
vb@259
   876
vb@251
   877
    if (!(session && fpr && comm_type))
vb@251
   878
        return PEP_ILLEGAL_VALUE;
vb@251
   879
vb@251
   880
    sqlite3_reset(session->least_trust);
vb@251
   881
    sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
vb@251
   882
vb@251
   883
    result = sqlite3_step(session->least_trust);
vb@251
   884
    switch (result) {
vb@251
   885
        case SQLITE_ROW: {
vb@259
   886
            int _comm_type = sqlite3_column_int(session->least_trust, 0);
vb@259
   887
            *comm_type = (PEP_comm_type) _comm_type;
vb@251
   888
            break;
vb@251
   889
        }
vb@251
   890
        default:
vb@251
   891
            status = PEP_CANNOT_FIND_IDENTITY;
vb@251
   892
    }
vb@251
   893
vb@251
   894
    sqlite3_reset(session->least_trust);
vb@251
   895
    return status;
vb@251
   896
}
vb@251
   897
vb@24
   898
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
   899
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
   900
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
   901
    )
vb@24
   902
{
vb@191
   903
    assert(session);
vb@191
   904
    assert(ctext);
vb@191
   905
    assert(csize);
vb@191
   906
    assert(ptext);
vb@191
   907
    assert(psize);
vb@191
   908
    assert(keylist);
vb@191
   909
vb@191
   910
    if (!(session && ctext && csize && ptext && psize && keylist && keylist))
vb@191
   911
        return PEP_ILLEGAL_VALUE;
vb@191
   912
vb@46
   913
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
vb@24
   914
}
vb@24
   915
vb@24
   916
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
   917
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
   918
    size_t psize, char **ctext, size_t *csize
vb@24
   919
    )
vb@24
   920
{
vb@191
   921
    assert(session);
vb@191
   922
    assert(keylist);
vb@191
   923
    assert(ptext);
vb@191
   924
    assert(psize);
vb@191
   925
    assert(ctext);
vb@191
   926
    assert(csize);
vb@191
   927
vb@191
   928
    if (!(session && keylist && ptext && psize && ctext && csize))
vb@191
   929
        return PEP_ILLEGAL_VALUE;
vb@191
   930
vb@46
   931
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
vb@24
   932
}
vb@24
   933
vb@24
   934
DYNAMIC_API PEP_STATUS verify_text(
vb@24
   935
    PEP_SESSION session, const char *text, size_t size,
vb@24
   936
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
   937
    )
vb@24
   938
{
vb@191
   939
    assert(session);
vb@191
   940
    assert(text);
vb@191
   941
    assert(size);
vb@191
   942
    assert(signature);
vb@191
   943
    assert(sig_size);
vb@191
   944
    assert(keylist);
vb@191
   945
vb@191
   946
    if (!(session && text && size && signature && sig_size && keylist))
vb@191
   947
        return PEP_ILLEGAL_VALUE;
vb@191
   948
vb@46
   949
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text, size, signature, sig_size, keylist);
vb@24
   950
}
vb@24
   951
vb@24
   952
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
   953
{
vb@191
   954
    assert(session);
vb@191
   955
    assert(fpr);
vb@191
   956
vb@191
   957
    if (!(session && fpr))
vb@191
   958
        return PEP_ILLEGAL_VALUE;
vb@191
   959
vb@46
   960
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
   961
}
vb@24
   962
vb@24
   963
DYNAMIC_API PEP_STATUS export_key(
vb@46
   964
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
   965
    )
vb@24
   966
{
vb@191
   967
    assert(session);
vb@191
   968
    assert(fpr);
vb@191
   969
    assert(key_data);
vb@191
   970
    assert(size);
vb@191
   971
vb@191
   972
    if (!(session && fpr && key_data && size))
vb@191
   973
        return PEP_ILLEGAL_VALUE;
vb@191
   974
vb@46
   975
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr, key_data, size);
vb@24
   976
}
vb@24
   977
vb@24
   978
DYNAMIC_API PEP_STATUS find_keys(
vb@46
   979
        PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
   980
    )
vb@24
   981
{
vb@191
   982
    assert(session);
vb@191
   983
    assert(pattern);
vb@191
   984
    assert(keylist);
vb@191
   985
vb@191
   986
    if (!(session && pattern && keylist))
vb@191
   987
        return PEP_ILLEGAL_VALUE;
vb@191
   988
vb@46
   989
    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern, keylist);
vb@24
   990
}
vb@24
   991
vb@24
   992
DYNAMIC_API PEP_STATUS generate_keypair(
vb@46
   993
        PEP_SESSION session, pEp_identity *identity
vb@24
   994
    )
vb@24
   995
{
vb@191
   996
    assert(session);
vb@191
   997
    assert(identity);
vb@191
   998
    assert(identity->address);
vb@298
   999
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@191
  1000
    assert(identity->username);
vb@191
  1001
vb@298
  1002
    if (!(session && identity && identity->address &&
vb@298
  1003
        (identity->fpr == NULL || identity->fpr[0] == 0) && identity->username))
vb@191
  1004
        return PEP_ILLEGAL_VALUE;
vb@191
  1005
vb@46
  1006
    return session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session, identity);
vb@24
  1007
}
vb@24
  1008
vb@9
  1009
DYNAMIC_API PEP_STATUS get_key_rating(
vb@46
  1010
        PEP_SESSION session,
vb@46
  1011
        const char *fpr,
vb@46
  1012
        PEP_comm_type *comm_type
vb@9
  1013
    )
vb@9
  1014
{
vb@191
  1015
    assert(session);
vb@191
  1016
    assert(fpr);
vb@191
  1017
    assert(comm_type);
vb@191
  1018
vb@191
  1019
    if (!(session && fpr && comm_type))
vb@191
  1020
        return PEP_ILLEGAL_VALUE;
vb@191
  1021
vb@46
  1022
    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr, comm_type);
vb@24
  1023
}
vb@9
  1024
vb@24
  1025
DYNAMIC_API PEP_STATUS import_key(PEP_SESSION session, const char *key_data, size_t size)
vb@24
  1026
{
vb@191
  1027
    assert(session);
vb@191
  1028
    assert(key_data);
vb@191
  1029
vb@191
  1030
    if (!(session && key_data))
vb@191
  1031
        return PEP_ILLEGAL_VALUE;
vb@191
  1032
vb@46
  1033
    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data, size);
vb@24
  1034
}
vb@9
  1035
vb@24
  1036
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1037
{
vb@191
  1038
    assert(session);
vb@191
  1039
    assert(pattern);
vb@191
  1040
vb@191
  1041
    if (!(session && pattern))
vb@191
  1042
        return PEP_ILLEGAL_VALUE;
vb@191
  1043
vb@46
  1044
    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
vb@24
  1045
}
vb@9
  1046
vb@24
  1047
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
  1048
{
vb@191
  1049
    assert(session);
vb@191
  1050
    assert(pattern);
vb@191
  1051
vb@191
  1052
    if (!(session && pattern))
vb@191
  1053
        return PEP_ILLEGAL_VALUE;
vb@191
  1054
vb@46
  1055
    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
vb@14
  1056
}
vb@196
  1057
vb@201
  1058
DYNAMIC_API PEP_STATUS renew_key(
vb@201
  1059
        PEP_SESSION session,
vb@201
  1060
        const char *fpr,
vb@201
  1061
        const timestamp *ts
vb@201
  1062
    )
vb@196
  1063
{
vb@196
  1064
    assert(session);
vb@200
  1065
    assert(fpr);
vb@196
  1066
vb@200
  1067
    if (!(session && fpr))
vb@196
  1068
        return PEP_ILLEGAL_VALUE;
vb@196
  1069
vb@201
  1070
    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
vb@196
  1071
}
vb@196
  1072
vb@211
  1073
DYNAMIC_API PEP_STATUS revoke_key(
vb@211
  1074
        PEP_SESSION session,
vb@211
  1075
        const char *fpr,
vb@211
  1076
        const char *reason
vb@211
  1077
    )
vb@197
  1078
{
vb@197
  1079
    assert(session);
vb@200
  1080
    assert(fpr);
vb@197
  1081
vb@200
  1082
    if (!(session && fpr))
vb@197
  1083
        return PEP_ILLEGAL_VALUE;
vb@197
  1084
vb@214
  1085
    return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
vb@214
  1086
            reason);
vb@197
  1087
}
vb@197
  1088
vb@214
  1089
DYNAMIC_API PEP_STATUS key_expired(
vb@214
  1090
        PEP_SESSION session,
vb@214
  1091
        const char *fpr,
vb@214
  1092
        bool *expired
vb@214
  1093
    )
vb@214
  1094
{
vb@214
  1095
    assert(session);
vb@214
  1096
    assert(fpr);
vb@214
  1097
    assert(expired);
vb@214
  1098
vb@214
  1099
    if (!(session && fpr && expired))
vb@214
  1100
        return PEP_ILLEGAL_VALUE;
vb@214
  1101
vb@214
  1102
    return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
vb@214
  1103
            expired);
vb@214
  1104
}
vb@214
  1105
vb@451
  1106
static void _clean_log_value(char *text)
vb@451
  1107
{
vb@451
  1108
    if (text) {
vb@451
  1109
        for (char *c = text; *c; c++) {
vb@451
  1110
            if (*c < 32)
vb@451
  1111
                *c = 32;
vb@451
  1112
            else if (*c == '"')
vb@451
  1113
                *c = '\'';
vb@451
  1114
        }
vb@451
  1115
    }
vb@451
  1116
}
vb@451
  1117
vb@450
  1118
static char *_concat_string(char *str1, const char *str2, char delim)
vb@450
  1119
{
vb@454
  1120
    str2 = str2 ? str2 : "";
vb@450
  1121
    size_t len1 = str1 ? strlen(str1) : 0;
vb@450
  1122
    size_t len2 = strlen(str2);
vb@450
  1123
    size_t len = len1 + len2 + 3;
vb@450
  1124
    char * result = realloc(str1, len + 1);
vb@450
  1125
vb@450
  1126
    if (result) {
vb@450
  1127
        result[len1] = '"';
vb@450
  1128
        strcpy(result + len1 + 1, str2);
vb@450
  1129
        _clean_log_value(result + len1 + 1);
vb@450
  1130
        result[len - 2] = '"';
vb@450
  1131
        result[len - 1] = delim;
vb@450
  1132
        result[len] = 0;
vb@450
  1133
    }
vb@450
  1134
    else {
vb@450
  1135
        free(str1);
vb@450
  1136
    }
vb@450
  1137
vb@450
  1138
    return result;
vb@450
  1139
}
vb@450
  1140
vb@450
  1141
DYNAMIC_API PEP_STATUS get_crashdump_log(
vb@450
  1142
        PEP_SESSION session,
vb@450
  1143
        int maxlines,
vb@450
  1144
        char **logdata
vb@450
  1145
    )
vb@450
  1146
{
vb@450
  1147
    PEP_STATUS status = PEP_STATUS_OK;
vb@450
  1148
    char *_logdata= NULL;
vb@450
  1149
vb@450
  1150
    assert(session);
vb@450
  1151
    assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
vb@450
  1152
    assert(logdata);
vb@450
  1153
vb@450
  1154
    if (!(session && logdata && maxlines >= 0 && maxlines <=
vb@450
  1155
            CRASHDUMP_MAX_LINES))
vb@450
  1156
        return PEP_ILLEGAL_VALUE;
vb@450
  1157
vb@450
  1158
    int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
vb@458
  1159
    const char *timestamp = NULL;
vb@458
  1160
    const char *title = NULL;
vb@458
  1161
    const char *entity = NULL;
vb@458
  1162
    const char *desc = NULL;
vb@458
  1163
    const char *comment = NULL;
vb@450
  1164
vb@450
  1165
    sqlite3_reset(session->crashdump);
vb@452
  1166
    sqlite3_bind_int(session->crashdump, 1, limit);
vb@450
  1167
vb@450
  1168
    int result;
vb@450
  1169
vb@450
  1170
    do {
vb@450
  1171
        result = sqlite3_step(session->crashdump);
vb@450
  1172
        switch (result) {
vb@450
  1173
        case SQLITE_ROW:
vb@452
  1174
            timestamp = (const char *) sqlite3_column_text(session->crashdump, 0);
vb@452
  1175
            title   = (const char *) sqlite3_column_text(session->crashdump, 1);
vb@452
  1176
            entity  = (const char *) sqlite3_column_text(session->crashdump, 2);
vb@452
  1177
            desc    = (const char *) sqlite3_column_text(session->crashdump, 3);
vb@452
  1178
            comment = (const char *) sqlite3_column_text(session->crashdump, 4);
vb@452
  1179
vb@452
  1180
            _logdata = _concat_string(_logdata, timestamp, ',');
vb@452
  1181
            if (_logdata == NULL)
vb@452
  1182
                goto enomem;
vb@450
  1183
vb@450
  1184
            _logdata = _concat_string(_logdata, title, ',');
vb@450
  1185
            if (_logdata == NULL)
vb@450
  1186
                goto enomem;
vb@450
  1187
vb@450
  1188
            _logdata = _concat_string(_logdata, entity, ',');
vb@450
  1189
            if (_logdata == NULL)
vb@450
  1190
                goto enomem;
vb@450
  1191
vb@450
  1192
            _logdata = _concat_string(_logdata, desc, ',');
vb@450
  1193
            if (_logdata == NULL)
vb@450
  1194
                goto enomem;
vb@450
  1195
vb@450
  1196
            _logdata = _concat_string(_logdata, comment, '\n');
vb@450
  1197
            if (_logdata == NULL)
vb@450
  1198
                goto enomem;
vb@450
  1199
vb@450
  1200
            break;
vb@450
  1201
vb@450
  1202
        case SQLITE_DONE:
vb@450
  1203
            break;
vb@450
  1204
vb@450
  1205
        default:
vb@450
  1206
            status = PEP_UNKNOWN_ERROR;
vb@450
  1207
            result = SQLITE_DONE;
vb@450
  1208
        }
vb@450
  1209
    } while (result != SQLITE_DONE);
vb@450
  1210
vb@450
  1211
    sqlite3_reset(session->crashdump);
vb@450
  1212
    if (status == PEP_STATUS_OK)
vb@450
  1213
        *logdata = _logdata;
vb@450
  1214
vb@450
  1215
    goto the_end;
vb@450
  1216
vb@450
  1217
enomem:
vb@450
  1218
    status = PEP_OUT_OF_MEMORY;
vb@450
  1219
vb@450
  1220
the_end:
vb@450
  1221
    return status;
vb@450
  1222
}
vb@450
  1223
vb@458
  1224
DYNAMIC_API PEP_STATUS get_languagelist(
vb@458
  1225
        PEP_SESSION session,
vb@458
  1226
        char **languages
vb@458
  1227
    )
vb@458
  1228
{
vb@458
  1229
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1230
    char *_languages= NULL;
vb@458
  1231
vb@458
  1232
    assert(session);
vb@458
  1233
    assert(languages);
vb@458
  1234
vb@458
  1235
    if (!(session && languages))
vb@458
  1236
        return PEP_ILLEGAL_VALUE;
vb@458
  1237
vb@458
  1238
    const char *lang = NULL;
vb@458
  1239
    const char *name = NULL;
vb@458
  1240
vb@458
  1241
    sqlite3_reset(session->languagelist);
vb@458
  1242
vb@458
  1243
    int result;
vb@458
  1244
vb@458
  1245
    do {
vb@458
  1246
        result = sqlite3_step(session->languagelist);
vb@458
  1247
        switch (result) {
vb@458
  1248
        case SQLITE_ROW:
vb@458
  1249
            lang = (const char *) sqlite3_column_text(session->languagelist, 0);
vb@458
  1250
            name = (const char *) sqlite3_column_text(session->languagelist, 1);
vb@458
  1251
vb@458
  1252
            _languages = _concat_string(_languages, lang, ',');
vb@458
  1253
            if (_languages == NULL)
vb@458
  1254
                goto enomem;
vb@458
  1255
vb@458
  1256
            _languages = _concat_string(_languages, name, '\n');
vb@458
  1257
            if (_languages == NULL)
vb@458
  1258
                goto enomem;
vb@458
  1259
vb@458
  1260
            break;
vb@458
  1261
vb@458
  1262
        case SQLITE_DONE:
vb@458
  1263
            break;
vb@458
  1264
vb@458
  1265
        default:
vb@458
  1266
            status = PEP_UNKNOWN_ERROR;
vb@458
  1267
            result = SQLITE_DONE;
vb@458
  1268
        }
vb@458
  1269
    } while (result != SQLITE_DONE);
vb@458
  1270
vb@458
  1271
    sqlite3_reset(session->languagelist);
vb@458
  1272
    if (status == PEP_STATUS_OK)
vb@458
  1273
        *languages = _languages;
vb@458
  1274
vb@458
  1275
    goto the_end;
vb@458
  1276
vb@458
  1277
enomem:
vb@458
  1278
    status = PEP_OUT_OF_MEMORY;
vb@458
  1279
vb@458
  1280
the_end:
vb@458
  1281
    return status;
vb@458
  1282
}
vb@458
  1283
vb@458
  1284
DYNAMIC_API PEP_STATUS get_phrase(
vb@458
  1285
        PEP_SESSION session,
vb@458
  1286
        const char *lang,
vb@458
  1287
        int phrase_id,
vb@458
  1288
        char **phrase
vb@458
  1289
    )
vb@458
  1290
{
vb@458
  1291
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1292
    *phrase = NULL;
vb@458
  1293
vb@479
  1294
    assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
vb@458
  1295
vb@479
  1296
    if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
vb@458
  1297
        return PEP_ILLEGAL_VALUE;
vb@458
  1298
vb@458
  1299
    sqlite3_reset(session->i18n_token);
vb@458
  1300
	sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
vb@458
  1301
	sqlite3_bind_int(session->i18n_token, 2, phrase_id);
vb@458
  1302
vb@458
  1303
    const char *_phrase = NULL;
vb@458
  1304
    int result;
vb@458
  1305
vb@458
  1306
    result = sqlite3_step(session->i18n_token);
vb@458
  1307
    switch (result) {
vb@458
  1308
    case SQLITE_ROW:
vb@458
  1309
        _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
vb@458
  1310
        break;
vb@458
  1311
vb@458
  1312
    case SQLITE_DONE:
vb@458
  1313
        status = PEP_PHRASE_NOT_FOUND;
vb@458
  1314
        break;
vb@458
  1315
vb@458
  1316
    default:
vb@458
  1317
        status = PEP_UNKNOWN_ERROR;
vb@458
  1318
    }
vb@458
  1319
vb@458
  1320
    if (status == PEP_STATUS_OK) {
vb@458
  1321
        *phrase = strdup(_phrase);
vb@458
  1322
        if (*phrase == NULL)
vb@458
  1323
            goto enomem;
vb@458
  1324
    }
vb@459
  1325
vb@459
  1326
    sqlite3_reset(session->i18n_token);
vb@458
  1327
    goto the_end;
vb@458
  1328
vb@458
  1329
enomem:
vb@458
  1330
    status = PEP_OUT_OF_MEMORY;
vb@458
  1331
vb@458
  1332
the_end:
vb@458
  1333
    return status;
vb@458
  1334
}
vb@458
  1335
vb@482
  1336
DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
vb@482
  1337
{
vb@482
  1338
    assert(session);
vb@482
  1339
vb@482
  1340
    if (!session)
vb@482
  1341
        return PEP_ILLEGAL_VALUE;
vb@482
  1342
fdik@494
  1343
    int int_result = sqlite3_exec(
fdik@494
  1344
        session->db,
fdik@494
  1345
        "delete from identity where address like '%@peptest.ch' ;",
fdik@494
  1346
        NULL,
fdik@494
  1347
        NULL,
fdik@494
  1348
        NULL
fdik@494
  1349
    );
fdik@494
  1350
    assert(int_result == SQLITE_OK);
vb@482
  1351
fdik@494
  1352
    return PEP_STATUS_OK;
vb@482
  1353
}
vb@482
  1354