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