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