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