src/pEpEngine.c
author Volker Birk <vb@pep-project.org>
Wed, 25 Mar 2015 15:20:25 +0100
changeset 125 5119178815d8
parent 113 4bee48270834
child 191 3bb51ee8f90d
permissions -rw-r--r--
Windoze fixes
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@62
     6
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@62
    13
	static const char *sql_safeword;
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@62
    20
    bool in_first = false;
vb@8
    21
vb@62
    22
    assert(sqlite3_threadsafe());
vb@62
    23
    if (!sqlite3_threadsafe())
vb@62
    24
        return PEP_INIT_SQLITE3_WITHOUT_MUTEX;
vb@62
    25
vb@62
    26
    // a little race condition - but still a race condition
vb@113
    27
    // mitigated by calling caveat (see documentation)
vb@62
    28
vb@62
    29
    ++init_count;
vb@67
    30
    if (init_count == 0)
vb@62
    31
        in_first = true;
vb@0
    32
vb@0
    33
	assert(session);
vb@0
    34
	*session = NULL;
vb@0
    35
vb@107
    36
    pEpSession *_session = calloc(1, sizeof(pEpSession));
vb@0
    37
	assert(_session);
vb@0
    38
	if (_session == NULL)
vb@65
    39
		goto enomem;
vb@62
    40
vb@0
    41
	_session->version = PEP_ENGINE_VERSION;
vb@0
    42
vb@0
    43
    assert(LOCAL_DB);
vb@0
    44
    if (LOCAL_DB == NULL) {
vb@65
    45
        status = PEP_INIT_CANNOT_OPEN_DB;
vb@65
    46
        goto pep_error;
vb@0
    47
    }
vb@0
    48
vb@0
    49
	int_result = sqlite3_open_v2(
vb@0
    50
			LOCAL_DB,
vb@0
    51
			&_session->db,
vb@0
    52
			SQLITE_OPEN_READWRITE
vb@0
    53
				| SQLITE_OPEN_CREATE
vb@0
    54
				| SQLITE_OPEN_FULLMUTEX
vb@0
    55
				| SQLITE_OPEN_PRIVATECACHE,
vb@0
    56
			NULL 
vb@0
    57
		);
vb@0
    58
vb@0
    59
	if (int_result != SQLITE_OK) {
vb@65
    60
		status = PEP_INIT_CANNOT_OPEN_DB;
vb@65
    61
        goto pep_error;
vb@0
    62
	}
vb@0
    63
vb@0
    64
	sqlite3_busy_timeout(_session->db, BUSY_WAIT_TIME);
vb@0
    65
vb@0
    66
    assert(SYSTEM_DB);
vb@0
    67
    if (SYSTEM_DB == NULL) {
vb@65
    68
		status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
vb@65
    69
        goto pep_error;
vb@0
    70
    }
vb@0
    71
vb@0
    72
	int_result = sqlite3_open_v2(
vb@0
    73
			SYSTEM_DB, &_session->system_db,
vb@0
    74
			SQLITE_OPEN_READONLY
vb@0
    75
				| SQLITE_OPEN_FULLMUTEX
vb@0
    76
				| SQLITE_OPEN_SHAREDCACHE,
vb@0
    77
			NULL
vb@0
    78
		);
vb@0
    79
vb@0
    80
	if (int_result != SQLITE_OK) {
vb@65
    81
		status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
vb@65
    82
        goto pep_error;
vb@0
    83
	}
vb@0
    84
vb@0
    85
	sqlite3_busy_timeout(_session->system_db, 1000);
vb@0
    86
vb@62
    87
    if (in_first) {
vb@62
    88
        int_result = sqlite3_exec(
vb@62
    89
            _session->db,
vb@62
    90
                "create table if not exists version_info ("
vb@62
    91
                "	id integer primary key,"
vb@62
    92
                "	timestamp integer default (datetime('now')) ,"
vb@62
    93
                "	version text,"
vb@62
    94
                "	comment text"
vb@62
    95
                ");"
vb@62
    96
                "create table if not exists log ("
vb@62
    97
                "	timestamp integer default (datetime('now')) ,"
vb@62
    98
                "	title text not null,"
vb@62
    99
                "	entity text not null,"
vb@62
   100
                "	description text,"
vb@62
   101
                "	comment text"
vb@62
   102
                ");"
vb@62
   103
                "create index if not exists log_timestamp on log ("
vb@62
   104
                "	timestamp"
vb@62
   105
                ");"
vb@62
   106
                "create table if not exists pgp_keypair ("
vb@62
   107
                "	fpr text primary key,"
vb@62
   108
                "	public_id text unique,"
vb@62
   109
                "   private_id text,"
vb@62
   110
                "	created integer,"
vb@62
   111
                "	expires integer,"
vb@62
   112
                "	comment text"
vb@62
   113
                ");"
vb@62
   114
                "create index if not exists pgp_keypair_expires on pgp_keypair ("
vb@62
   115
                "	expires"
vb@62
   116
                ");"
vb@62
   117
                "create table if not exists person ("
vb@62
   118
                "	id text primary key,"
vb@62
   119
                "	username text not null,"
vb@62
   120
                "	main_key_id text"
vb@62
   121
                "		references pgp_keypair (fpr)"
vb@62
   122
                "		on delete set null,"
vb@62
   123
                "   lang text,"
vb@62
   124
                "	comment text"
vb@62
   125
                ");"
vb@62
   126
                "create table if not exists identity ("
vb@62
   127
                "	address text primary key,"
vb@62
   128
                "	user_id text"
vb@62
   129
                "		references person (id)"
vb@62
   130
                "		on delete cascade,"
vb@62
   131
                "	main_key_id text"
vb@62
   132
                "		references pgp_keypair (fpr)"
vb@62
   133
                "		on delete set null,"
vb@62
   134
                "	comment text"
vb@62
   135
                ");"
vb@62
   136
                "create table if not exists trust ("
vb@62
   137
                "   user_id text not null"
vb@62
   138
                "       references person (id)"
vb@62
   139
                "		on delete cascade,"
vb@62
   140
                "   pgp_keypair_fpr text not null"
vb@62
   141
                "       references pgp_keypair (fpr)"
vb@62
   142
                "       on delete cascade,"
vb@62
   143
                "   comm_type integer not null,"
vb@62
   144
                "	comment text"
vb@62
   145
                ");"
vb@62
   146
                "create unique index if not exists trust_index on trust ("
vb@62
   147
                "   user_id,"
vb@62
   148
                "   pgp_keypair_fpr"
vb@62
   149
                ");",
vb@62
   150
            NULL,
vb@62
   151
            NULL,
vb@62
   152
            NULL
vb@62
   153
        );
vb@62
   154
        assert(int_result == SQLITE_OK);
vb@0
   155
vb@62
   156
        int_result = sqlite3_exec(
vb@62
   157
            _session->db,
vb@62
   158
            "insert or replace into version_info (id, version) values (1, '1.0');",
vb@62
   159
            NULL,
vb@62
   160
            NULL,
vb@62
   161
            NULL
vb@62
   162
        );
vb@62
   163
        assert(int_result == SQLITE_OK);
vb@0
   164
vb@62
   165
        sql_log = "insert into log (title, entity, description, comment)"
vb@62
   166
                  "values (?1, ?2, ?3, ?4);";
vb@62
   167
vb@62
   168
        sql_get_identity =	"select fpr, identity.user_id, username, comm_type, lang"
vb@62
   169
                            "   from identity"
vb@62
   170
                            "   join person on id = identity.user_id"
vb@62
   171
                            "   join pgp_keypair on fpr = identity.main_key_id"
vb@62
   172
                            "   join trust on id = trust.user_id"
vb@62
   173
                            "       and pgp_keypair_fpr = identity.main_key_id"
vb@62
   174
                            "   where address = ?1 ;";
vb@62
   175
vb@62
   176
        sql_safeword = "select id, word from wordlist where lang = lower(?1) "
vb@62
   177
                       "and id = ?2 ;";
vb@62
   178
vb@62
   179
        sql_set_person = "insert or replace into person (id, username, lang) "
vb@62
   180
                         "values (?1, ?2, ?3) ;";
vb@62
   181
vb@62
   182
        sql_set_pgp_keypair = "insert or replace into pgp_keypair (fpr) "
vb@62
   183
                              "values (?1) ;";
vb@62
   184
vb@62
   185
        sql_set_identity = "insert or replace into identity (address, main_key_id, "
vb@62
   186
                           "user_id) values (?1, ?2, ?3) ;";
vb@62
   187
vb@62
   188
        sql_set_trust = "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type) "
vb@62
   189
                        "values (?1, ?2, ?3) ;";
vb@62
   190
vb@62
   191
        sql_get_trust = "select user_id, comm_type from trust where user_id = ?1 "
vb@62
   192
                        "and pgp_keypair_fpr = ?2 ;";
vb@62
   193
    }
vb@62
   194
vb@0
   195
    int_result = sqlite3_prepare_v2(_session->db, sql_log, strlen(sql_log),
vb@0
   196
            &_session->log, NULL);
vb@0
   197
	assert(int_result == SQLITE_OK);
vb@0
   198
vb@0
   199
    int_result = sqlite3_prepare_v2(_session->system_db, sql_safeword,
vb@0
   200
            strlen(sql_safeword), &_session->safeword, NULL);
vb@0
   201
	assert(int_result == SQLITE_OK);
vb@0
   202
vb@0
   203
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
vb@0
   204
            strlen(sql_get_identity), &_session->get_identity, NULL);
vb@0
   205
	assert(int_result == SQLITE_OK);
vb@0
   206
vb@0
   207
    int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
vb@0
   208
            strlen(sql_set_person), &_session->set_person, NULL);
vb@0
   209
    assert(int_result == SQLITE_OK);
vb@62
   210
vb@0
   211
    int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
vb@0
   212
            strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair, NULL);
vb@0
   213
	assert(int_result == SQLITE_OK);
vb@62
   214
vb@0
   215
    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity,
vb@0
   216
            strlen(sql_set_identity), &_session->set_identity, NULL);
vb@0
   217
	assert(int_result == SQLITE_OK);
vb@62
   218
vb@0
   219
    int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
vb@0
   220
            strlen(sql_set_trust), &_session->set_trust, NULL);
vb@0
   221
	assert(int_result == SQLITE_OK);
vb@62
   222
vb@8
   223
    int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
vb@8
   224
            strlen(sql_get_trust), &_session->get_trust, NULL);
vb@8
   225
    assert(int_result == SQLITE_OK);
vb@0
   226
vb@65
   227
    status = init_cryptotech(_session, in_first);
vb@65
   228
    if (status != PEP_STATUS_OK)
vb@65
   229
        goto pep_error;
vb@0
   230
vb@65
   231
    status = init_transport_system(_session, in_first);
vb@65
   232
    if (status != PEP_STATUS_OK)
vb@65
   233
        goto pep_error;
vb@62
   234
vb@65
   235
    status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
vb@65
   236
    if (status != PEP_STATUS_OK)
vb@65
   237
        goto pep_error;
vb@65
   238
vb@65
   239
	*session = _session;
vb@0
   240
	return PEP_STATUS_OK;
vb@65
   241
vb@65
   242
enomem:
vb@65
   243
    status = PEP_OUT_OF_MEMORY;
vb@65
   244
vb@65
   245
pep_error:
vb@65
   246
    release(_session);
vb@65
   247
    return status;
vb@0
   248
}
vb@0
   249
vb@0
   250
DYNAMIC_API void release(PEP_SESSION session)
vb@0
   251
{
vb@62
   252
    bool out_last = false;
vb@62
   253
vb@62
   254
    assert(init_count >= 0);
vb@0
   255
	assert(session);
vb@0
   256
vb@62
   257
    // a small race condition but still a race condition
vb@113
   258
    // mitigated by calling caveat (see documentation)
vb@62
   259
vb@62
   260
    if (init_count == 0)
vb@62
   261
        out_last = true;
vb@62
   262
    --init_count;
vb@62
   263
vb@46
   264
	if (session) {
vb@46
   265
		if (session->db) {
vb@65
   266
            if (session->safeword)
vb@65
   267
                sqlite3_finalize(session->safeword);
vb@65
   268
            if (session->log)
vb@65
   269
                sqlite3_finalize(session->log);
vb@65
   270
            if (session->get_identity)
vb@65
   271
                sqlite3_finalize(session->get_identity);
vb@65
   272
            if (session->set_identity)
vb@65
   273
                sqlite3_finalize(session->set_identity);
vb@65
   274
            if (session->set_person)
vb@65
   275
                sqlite3_finalize(session->set_person);
vb@65
   276
            if (session->set_pgp_keypair)
vb@65
   277
                sqlite3_finalize(session->set_pgp_keypair);
vb@65
   278
            if (session->set_trust)
vb@65
   279
                sqlite3_finalize(session->set_trust);
vb@65
   280
            if (session->get_trust)
vb@65
   281
                sqlite3_finalize(session->get_trust);
vb@26
   282
vb@65
   283
            if (session->db)
vb@65
   284
                sqlite3_close_v2(session->db);
vb@65
   285
            if (session->system_db)
vb@65
   286
                sqlite3_close_v2(session->system_db);
vb@0
   287
		}
vb@65
   288
vb@65
   289
        release_transport_system(session, out_last);
vb@65
   290
        release_cryptotech(session, out_last);
vb@65
   291
vb@65
   292
        free(session);
vb@62
   293
    }
vb@0
   294
}
vb@0
   295
vb@0
   296
DYNAMIC_API PEP_STATUS log_event(
vb@0
   297
        PEP_SESSION session, const char *title, const char *entity,
vb@0
   298
        const char *description, const char *comment
vb@0
   299
    )
vb@0
   300
{
vb@0
   301
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   302
	int result;
vb@0
   303
vb@46
   304
	assert(session);
vb@0
   305
	assert(title);
vb@0
   306
	assert(entity);
vb@0
   307
vb@46
   308
	sqlite3_reset(session->log);
vb@46
   309
	sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
vb@46
   310
	sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
vb@0
   311
	if (description)
vb@46
   312
        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
vb@0
   313
	else
vb@46
   314
		sqlite3_bind_null(session->log, 3);
vb@0
   315
	if (comment)
vb@46
   316
		sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
vb@0
   317
	else
vb@46
   318
		sqlite3_bind_null(session->log, 4);
vb@0
   319
	do {
vb@46
   320
		result = sqlite3_step(session->log);
vb@0
   321
		assert(result == SQLITE_DONE || result == SQLITE_BUSY);
vb@0
   322
		if (result != SQLITE_DONE && result != SQLITE_BUSY)
vb@0
   323
			status = PEP_UNKNOWN_ERROR;
vb@0
   324
	} while (result == SQLITE_BUSY);
vb@46
   325
	sqlite3_reset(session->log);
vb@0
   326
vb@0
   327
	return status;
vb@0
   328
}
vb@0
   329
vb@0
   330
DYNAMIC_API PEP_STATUS safeword(
vb@0
   331
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
   332
            char **word, size_t *wsize
vb@0
   333
        )
vb@0
   334
{
vb@0
   335
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   336
	int result;
vb@0
   337
vb@46
   338
	assert(session);
vb@0
   339
	assert(word);
vb@0
   340
	assert(wsize);
vb@0
   341
vb@0
   342
	*word = NULL;
vb@0
   343
	*wsize = 0;
vb@0
   344
vb@0
   345
	if (lang == NULL)
vb@0
   346
		lang = "en";
vb@0
   347
vb@0
   348
	assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   349
            || (lang[0] >= 'a' && lang[0] <= 'z'));
vb@0
   350
	assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   351
            || (lang[1] >= 'a' && lang[1] <= 'z'));
vb@0
   352
	assert(lang[2] == 0);
vb@0
   353
vb@46
   354
	sqlite3_reset(session->safeword);
vb@46
   355
    sqlite3_bind_text(session->safeword, 1, lang, -1, SQLITE_STATIC);
vb@46
   356
	sqlite3_bind_int(session->safeword, 2, value);
vb@0
   357
vb@46
   358
	result = sqlite3_step(session->safeword);
vb@0
   359
	if (result == SQLITE_ROW) {
vb@46
   360
        *word = strdup((const char *) sqlite3_column_text(session->safeword,
vb@0
   361
                    1));
vb@0
   362
		if (*word)
vb@46
   363
            *wsize = sqlite3_column_bytes(session->safeword, 1);
vb@0
   364
		else
vb@0
   365
			status = PEP_SAFEWORD_NOT_FOUND;
vb@0
   366
	} else
vb@0
   367
		status = PEP_SAFEWORD_NOT_FOUND;
vb@0
   368
vb@46
   369
	sqlite3_reset(session->safeword);
vb@0
   370
	return status;
vb@0
   371
}
vb@0
   372
vb@0
   373
DYNAMIC_API PEP_STATUS safewords(
vb@0
   374
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
   375
        char **words, size_t *wsize, int max_words
vb@0
   376
    )
vb@0
   377
{
vb@0
   378
	const char *source = fingerprint;
vb@109
   379
	char *buffer;
vb@125
   380
	char *dest;
vb@0
   381
	size_t fsize;
vb@0
   382
    PEP_STATUS _status;
vb@0
   383
vb@0
   384
	assert(session);
vb@0
   385
	assert(fingerprint);
vb@0
   386
	assert(words);
vb@0
   387
	assert(wsize);
vb@0
   388
	assert(max_words >= 0);
vb@0
   389
vb@0
   390
	*words = NULL;
vb@0
   391
	*wsize = 0;
vb@0
   392
vb@109
   393
    buffer = calloc(1, MAX_SAFEWORDS_SPACE);
vb@0
   394
    assert(buffer);
vb@0
   395
    if (buffer == NULL)
vb@0
   396
        return PEP_OUT_OF_MEMORY;
vb@125
   397
    dest = buffer;
vb@0
   398
vb@0
   399
	fsize = strlen(fingerprint);
vb@0
   400
vb@0
   401
	if (lang == NULL)
vb@0
   402
		lang = "en";
vb@0
   403
vb@0
   404
	assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   405
            || (lang[0] >= 'a' && lang[0] <= 'z'));
vb@0
   406
	assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   407
            || (lang[1] >= 'a' && lang[1] <= 'z'));
vb@0
   408
	assert(lang[2] == 0);
vb@0
   409
vb@0
   410
	int n_words = 0;
vb@0
   411
	while (source < fingerprint + fsize) {
vb@0
   412
		uint16_t value;
vb@0
   413
		char *word;
vb@0
   414
		size_t _wsize;
vb@0
   415
		int j;
vb@0
   416
vb@0
   417
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
vb@0
   418
			if (*source >= 'a' && *source <= 'f')
vb@0
   419
				value += (*source - 'a' + 10) << (3 - j++) * 4;
vb@0
   420
			else if (*source >= 'A' && *source <= 'F')
vb@0
   421
				value += (*source - 'A' + 10) << (3 - j++) * 4;
vb@0
   422
			else if (*source >= '0' && *source <= '9')
vb@0
   423
				value += (*source - '0') << (3 - j++) * 4;
vb@0
   424
			
vb@0
   425
			source++;
vb@0
   426
		}
vb@0
   427
vb@0
   428
		_status = safeword(session, value, lang, &word, &_wsize);
vb@0
   429
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
   430
            free(buffer);
vb@0
   431
            return PEP_OUT_OF_MEMORY;
vb@0
   432
        }
vb@0
   433
		if (word == NULL) {
vb@0
   434
            free(buffer);
vb@0
   435
			return PEP_SAFEWORD_NOT_FOUND;
vb@0
   436
        }
vb@0
   437
vb@0
   438
		if (dest + _wsize < buffer + MAX_SAFEWORDS_SPACE - 1) {
vb@0
   439
			strncpy(dest, word, _wsize);
vb@0
   440
            free(word);
vb@0
   441
			dest += _wsize;
vb@0
   442
		}
vb@0
   443
		else {
vb@0
   444
            free(word);
vb@0
   445
			break; // buffer full
vb@0
   446
        }
vb@0
   447
vb@0
   448
		if (source < fingerprint + fsize
vb@0
   449
                && dest + _wsize < buffer + MAX_SAFEWORDS_SPACE - 1)
vb@0
   450
			*dest++ = ' ';
vb@0
   451
vb@0
   452
		++n_words;
vb@0
   453
		if (max_words && n_words >= max_words)
vb@0
   454
			break;
vb@0
   455
	}
vb@0
   456
vb@0
   457
	*words = buffer;
vb@0
   458
	*wsize = dest - buffer;
vb@0
   459
	return PEP_STATUS_OK;
vb@0
   460
}
vb@0
   461
vb@0
   462
pEp_identity *new_identity(
vb@0
   463
        const char *address, const char *fpr, const char *user_id,
vb@0
   464
        const char *username
vb@0
   465
    )
vb@0
   466
{
vb@0
   467
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
   468
    assert(result);
vb@0
   469
    if (result) {
vb@0
   470
        if (address) {
vb@0
   471
            result->address = strdup(address);
vb@0
   472
            assert(result->address);
vb@0
   473
            if (result->address == NULL) {
vb@0
   474
                free(result);
vb@0
   475
                return NULL;
vb@0
   476
            }
vb@0
   477
            result->address_size = strlen(address);
vb@0
   478
        }
vb@0
   479
        if (fpr) {
vb@0
   480
            result->fpr = strdup(fpr);
vb@0
   481
            assert(result->fpr);
vb@0
   482
            if (result->fpr == NULL) {
vb@0
   483
                free_identity(result);
vb@0
   484
                return NULL;
vb@0
   485
            }
vb@0
   486
            result->fpr_size = strlen(fpr);
vb@0
   487
        }
vb@0
   488
        if (user_id) {
vb@0
   489
            result->user_id = strdup(user_id);
vb@0
   490
            assert(result->user_id);
vb@0
   491
            if (result->user_id == NULL) {
vb@0
   492
                free_identity(result);
vb@0
   493
                return NULL;
vb@0
   494
            }
vb@0
   495
            result->user_id_size = strlen(user_id);
vb@0
   496
        }
vb@0
   497
        if (username) {
vb@0
   498
            result->username = strdup(username);
vb@0
   499
            assert(result->username);
vb@0
   500
            if (result->username == NULL) {
vb@0
   501
                free_identity(result);
vb@0
   502
                return NULL;
vb@0
   503
            }
vb@0
   504
            result->username_size = strlen(username);
vb@0
   505
        }
vb@0
   506
        result->struct_size = sizeof(pEp_identity);
vb@0
   507
    }
vb@0
   508
    return result;
vb@0
   509
}
vb@0
   510
vb@37
   511
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
   512
{
vb@37
   513
    assert(src);
vb@37
   514
vb@37
   515
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
vb@37
   516
    assert(dup);
vb@37
   517
    if (dup == NULL)
vb@37
   518
        return NULL;
vb@37
   519
    
vb@37
   520
    dup->comm_type = src->comm_type;
vb@37
   521
    dup->lang[0] = src->lang[0];
vb@37
   522
    dup->lang[1] = src->lang[1];
vb@37
   523
    dup->lang[2] = 0;
vb@37
   524
    dup->me = src->me;
vb@37
   525
vb@37
   526
    return dup;
vb@37
   527
}
vb@37
   528
vb@0
   529
void free_identity(pEp_identity *identity)
vb@0
   530
{
vb@0
   531
    if (identity) {
vb@0
   532
        free(identity->address);
vb@0
   533
        free(identity->fpr);
vb@0
   534
        free(identity->user_id);
vb@0
   535
        free(identity->username);
vb@0
   536
        free(identity);
vb@0
   537
    }
vb@0
   538
}
vb@0
   539
vb@0
   540
DYNAMIC_API PEP_STATUS get_identity(
vb@0
   541
        PEP_SESSION session, const char *address,
vb@0
   542
        pEp_identity **identity
vb@0
   543
    )
vb@0
   544
{
vb@0
   545
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   546
	static pEp_identity *_identity;
vb@0
   547
	int result;
vb@0
   548
	const char *_lang;
vb@0
   549
vb@0
   550
	assert(session);
vb@0
   551
	assert(address);
vb@8
   552
    assert(address[0]);
vb@0
   553
vb@46
   554
    sqlite3_reset(session->get_identity);
vb@46
   555
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
vb@0
   556
vb@46
   557
    result = sqlite3_step(session->get_identity);
vb@0
   558
	switch (result) {
vb@0
   559
	case SQLITE_ROW:
vb@0
   560
        _identity = new_identity(
vb@0
   561
                address,
vb@46
   562
                (const char *) sqlite3_column_text(session->get_identity, 0),
vb@46
   563
                (const char *) sqlite3_column_text(session->get_identity, 1),
vb@46
   564
                (const char *) sqlite3_column_text(session->get_identity, 2)
vb@0
   565
                );
vb@0
   566
        assert(_identity);
vb@0
   567
        if (_identity == NULL)
vb@0
   568
            return PEP_OUT_OF_MEMORY;
vb@0
   569
vb@46
   570
        _identity->comm_type = (PEP_comm_type) sqlite3_column_int(session->get_identity, 3);
vb@46
   571
        _lang = (const char *) sqlite3_column_text(session->get_identity, 4);
vb@0
   572
        if (_lang && _lang[0]) {
vb@0
   573
			assert(_lang[0] >= 'a' && _lang[0] <= 'z');
vb@0
   574
			assert(_lang[1] >= 'a' && _lang[1] <= 'z');
vb@0
   575
			assert(_lang[2] == 0);
vb@0
   576
			_identity->lang[0] = _lang[0];
vb@0
   577
			_identity->lang[1] = _lang[1];
vb@0
   578
            _identity->lang[2] = 0;
vb@0
   579
		}
vb@0
   580
		*identity = _identity;
vb@0
   581
		break;
vb@0
   582
	default:
vb@0
   583
        status = PEP_CANNOT_FIND_IDENTITY;
vb@0
   584
		*identity = NULL;
vb@0
   585
	}
vb@0
   586
vb@46
   587
    sqlite3_reset(session->get_identity);
vb@0
   588
	return status;
vb@0
   589
}
vb@0
   590
vb@0
   591
DYNAMIC_API PEP_STATUS set_identity(
vb@0
   592
        PEP_SESSION session, const pEp_identity *identity
vb@0
   593
    )
vb@0
   594
{
vb@0
   595
	int result;
vb@0
   596
vb@0
   597
	assert(session);
vb@0
   598
	assert(identity);
vb@0
   599
	assert(identity->address);
vb@0
   600
	assert(identity->fpr);
vb@0
   601
	assert(identity->user_id);
vb@0
   602
	assert(identity->username);
vb@0
   603
vb@46
   604
	sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
   605
vb@46
   606
	sqlite3_reset(session->set_person);
vb@46
   607
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
   608
            SQLITE_STATIC);
vb@46
   609
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
   610
            SQLITE_STATIC);
vb@0
   611
	if (identity->lang[0])
vb@46
   612
        sqlite3_bind_text(session->set_person, 3, identity->lang, 1,
vb@0
   613
                SQLITE_STATIC);
vb@0
   614
	else
vb@46
   615
		sqlite3_bind_null(session->set_person, 3);
vb@46
   616
	result = sqlite3_step(session->set_person);
vb@46
   617
	sqlite3_reset(session->set_person);
vb@0
   618
	if (result != SQLITE_DONE) {
vb@46
   619
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   620
		return PEP_CANNOT_SET_PERSON;
vb@0
   621
	}
vb@0
   622
vb@46
   623
	sqlite3_reset(session->set_pgp_keypair);
vb@46
   624
    sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
vb@0
   625
            SQLITE_STATIC);
vb@46
   626
	result = sqlite3_step(session->set_pgp_keypair);
vb@46
   627
	sqlite3_reset(session->set_pgp_keypair);
vb@0
   628
	if (result != SQLITE_DONE) {
vb@46
   629
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   630
		return PEP_CANNOT_SET_PGP_KEYPAIR;
vb@0
   631
	}
vb@0
   632
vb@46
   633
	sqlite3_reset(session->set_identity);
vb@46
   634
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
   635
            SQLITE_STATIC);
vb@46
   636
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
   637
            SQLITE_STATIC);
vb@46
   638
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
   639
            SQLITE_STATIC);
vb@46
   640
	result = sqlite3_step(session->set_identity);
vb@46
   641
	sqlite3_reset(session->set_identity);
vb@0
   642
	if (result != SQLITE_DONE) {
vb@46
   643
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   644
		return PEP_CANNOT_SET_IDENTITY;
vb@0
   645
	}
vb@0
   646
vb@46
   647
	sqlite3_reset(session->set_trust);
vb@46
   648
    sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
vb@0
   649
            SQLITE_STATIC);
vb@46
   650
    sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
vb@0
   651
            SQLITE_STATIC);
vb@46
   652
	sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
vb@46
   653
	result = sqlite3_step(session->set_trust);
vb@46
   654
	sqlite3_reset(session->set_trust);
vb@0
   655
	if (result != SQLITE_DONE) {
vb@46
   656
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   657
		return PEP_CANNOT_SET_IDENTITY;
vb@0
   658
	}
vb@0
   659
vb@46
   660
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
vb@0
   661
	if (result == SQLITE_OK)
vb@0
   662
		return PEP_STATUS_OK;
vb@0
   663
	else
vb@0
   664
		return PEP_COMMIT_FAILED;
vb@0
   665
}
vb@0
   666
vb@0
   667
void pEp_free(void *p)
vb@0
   668
{
vb@0
   669
    free(p);
vb@0
   670
}
vb@0
   671
vb@8
   672
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
   673
{
vb@8
   674
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
   675
    int result;
vb@8
   676
vb@8
   677
    assert(session);
vb@8
   678
    assert(identity);
vb@8
   679
    assert(identity->user_id);
vb@8
   680
    assert(identity->user_id[0]);
vb@8
   681
    assert(identity->fpr);
vb@8
   682
    assert(identity->fpr[0]);
vb@8
   683
vb@8
   684
    identity->comm_type = PEP_ct_unknown;
vb@8
   685
vb@46
   686
    sqlite3_reset(session->get_trust);
vb@46
   687
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
vb@46
   688
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
   689
vb@46
   690
    result = sqlite3_step(session->get_trust);
vb@8
   691
    switch (result) {
vb@8
   692
    case SQLITE_ROW: {
vb@46
   693
        const char * user_id = (const char *) sqlite3_column_text(session->get_trust, 1);
vb@46
   694
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 2);
vb@8
   695
vb@8
   696
        if (strcmp(user_id, identity->user_id) != 0) {
vb@8
   697
            free(identity->user_id);
vb@8
   698
            identity->user_id = strdup(user_id);
vb@8
   699
            assert(identity->user_id);
vb@8
   700
            if (identity->user_id == NULL)
vb@8
   701
                return PEP_OUT_OF_MEMORY;
vb@8
   702
        }
vb@8
   703
        identity->comm_type = comm_type;
vb@8
   704
        break;
vb@8
   705
    }
vb@8
   706
 
vb@8
   707
    default:
vb@8
   708
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
   709
    }
vb@8
   710
vb@46
   711
    sqlite3_reset(session->get_trust);
vb@8
   712
    return status;
vb@9
   713
}
vb@9
   714
vb@24
   715
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
   716
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
   717
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
   718
    )
vb@24
   719
{
vb@46
   720
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
vb@24
   721
}
vb@24
   722
vb@24
   723
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
   724
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
   725
    size_t psize, char **ctext, size_t *csize
vb@24
   726
    )
vb@24
   727
{
vb@46
   728
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
vb@24
   729
}
vb@24
   730
vb@24
   731
DYNAMIC_API PEP_STATUS verify_text(
vb@24
   732
    PEP_SESSION session, const char *text, size_t size,
vb@24
   733
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
   734
    )
vb@24
   735
{
vb@46
   736
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text, size, signature, sig_size, keylist);
vb@24
   737
}
vb@24
   738
vb@24
   739
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
   740
{
vb@46
   741
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
   742
}
vb@24
   743
vb@24
   744
DYNAMIC_API PEP_STATUS export_key(
vb@46
   745
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
   746
    )
vb@24
   747
{
vb@46
   748
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr, key_data, size);
vb@24
   749
}
vb@24
   750
vb@24
   751
DYNAMIC_API PEP_STATUS find_keys(
vb@46
   752
        PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
   753
    )
vb@24
   754
{
vb@46
   755
    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern, keylist);
vb@24
   756
}
vb@24
   757
vb@24
   758
DYNAMIC_API PEP_STATUS generate_keypair(
vb@46
   759
        PEP_SESSION session, pEp_identity *identity
vb@24
   760
    )
vb@24
   761
{
vb@46
   762
    return session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session, identity);
vb@24
   763
}
vb@24
   764
vb@9
   765
DYNAMIC_API PEP_STATUS get_key_rating(
vb@46
   766
        PEP_SESSION session,
vb@46
   767
        const char *fpr,
vb@46
   768
        PEP_comm_type *comm_type
vb@9
   769
    )
vb@9
   770
{
vb@46
   771
    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr, comm_type);
vb@24
   772
}
vb@9
   773
vb@24
   774
DYNAMIC_API PEP_STATUS import_key(PEP_SESSION session, const char *key_data, size_t size)
vb@24
   775
{
vb@46
   776
    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data, size);
vb@24
   777
}
vb@9
   778
vb@24
   779
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
   780
{
vb@46
   781
    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
vb@24
   782
}
vb@9
   783
vb@24
   784
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
   785
{
vb@46
   786
    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
vb@14
   787
}