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