src/pEpEngine.c
author vb
Mon, 01 Sep 2014 18:36:42 +0200
changeset 26 843e629627bf
parent 24 50887c6ab78f
child 28 7fc9fc3e680c
permissions -rw-r--r--
bugfix: pgp_init
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@26
    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@26
   247
            sqlite3_finalize(_session->set_person);
vb@26
   248
            sqlite3_finalize(_session->set_pgp_keypair);
vb@26
   249
            sqlite3_finalize(_session->set_trust);
vb@26
   250
            sqlite3_finalize(_session->get_trust);
vb@26
   251
vb@0
   252
			sqlite3_close_v2(_session->db);
vb@0
   253
			sqlite3_close_v2(_session->system_db);
vb@0
   254
		}
vb@24
   255
        pgp_release(session);
vb@24
   256
    }
vb@0
   257
	free(_session);
vb@0
   258
}
vb@0
   259
vb@0
   260
stringlist_t *new_stringlist(const char *value)
vb@0
   261
{
vb@0
   262
    stringlist_t *result = (stringlist_t *) calloc(1, sizeof(stringlist_t));
vb@0
   263
    if (result && value) {
vb@0
   264
        result->value = strdup(value);
vb@0
   265
        assert(result->value);
vb@0
   266
        if (result->value == 0) {
vb@0
   267
            free(result);
vb@0
   268
            return NULL;
vb@0
   269
        }
vb@0
   270
    }
vb@0
   271
    return result;
vb@0
   272
}
vb@0
   273
vb@0
   274
stringlist_t *stringlist_add(stringlist_t *stringlist, const char *value)
vb@0
   275
{
vb@0
   276
    assert(value);
vb@0
   277
vb@0
   278
    if (stringlist == NULL)
vb@0
   279
        return new_stringlist(value);
vb@0
   280
vb@0
   281
    if (stringlist->next != NULL)
vb@0
   282
        return stringlist_add(stringlist->next, value);
vb@0
   283
vb@0
   284
    if (stringlist->value == NULL) {
vb@0
   285
        stringlist->value = strdup(value);
vb@0
   286
        assert(stringlist->value);
vb@0
   287
        if (stringlist->value == NULL)
vb@0
   288
            return NULL;
vb@0
   289
        return stringlist;
vb@0
   290
    }
vb@0
   291
vb@0
   292
    stringlist->next = new_stringlist(value);
vb@0
   293
    assert(stringlist->next);
vb@0
   294
    if (stringlist->next == NULL)
vb@0
   295
        return NULL;
vb@0
   296
vb@0
   297
    return stringlist->next;
vb@0
   298
}
vb@0
   299
vb@0
   300
int stringlist_length(const stringlist_t *stringlist)
vb@0
   301
{
vb@0
   302
    int len = 1;
vb@0
   303
    stringlist_t *_stringlist;
vb@0
   304
vb@0
   305
    assert(stringlist);
vb@0
   306
vb@0
   307
    if (stringlist->value == NULL)
vb@0
   308
        return 0;
vb@0
   309
vb@0
   310
    for (_stringlist=stringlist->next; _stringlist!=NULL; _stringlist=_stringlist->next)
vb@0
   311
        len += 1;
vb@0
   312
vb@0
   313
    return len;
vb@0
   314
}
vb@0
   315
vb@0
   316
void free_stringlist(stringlist_t *stringlist)
vb@0
   317
{
vb@0
   318
    if (stringlist) {
vb@0
   319
        free_stringlist(stringlist->next);
vb@0
   320
        free(stringlist->value);
vb@0
   321
        free(stringlist);
vb@0
   322
    }
vb@0
   323
}
vb@0
   324
vb@0
   325
DYNAMIC_API PEP_STATUS log_event(
vb@0
   326
        PEP_SESSION session, const char *title, const char *entity,
vb@0
   327
        const char *description, const char *comment
vb@0
   328
    )
vb@0
   329
{
vb@0
   330
	pEpSession *_session = (pEpSession *) session;
vb@0
   331
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   332
	int result;
vb@0
   333
vb@0
   334
	assert(_session);
vb@0
   335
	assert(title);
vb@0
   336
	assert(entity);
vb@0
   337
vb@0
   338
	sqlite3_reset(_session->log);
vb@0
   339
	sqlite3_bind_text(_session->log, 1, title, -1, SQLITE_STATIC);
vb@0
   340
	sqlite3_bind_text(_session->log, 2, entity, -1, SQLITE_STATIC);
vb@0
   341
	if (description)
vb@0
   342
        sqlite3_bind_text(_session->log, 3, description, -1, SQLITE_STATIC);
vb@0
   343
	else
vb@0
   344
		sqlite3_bind_null(_session->log, 3);
vb@0
   345
	if (comment)
vb@0
   346
		sqlite3_bind_text(_session->log, 4, comment, -1, SQLITE_STATIC);
vb@0
   347
	else
vb@0
   348
		sqlite3_bind_null(_session->log, 4);
vb@0
   349
	do {
vb@0
   350
		result = sqlite3_step(_session->log);
vb@0
   351
		assert(result == SQLITE_DONE || result == SQLITE_BUSY);
vb@0
   352
		if (result != SQLITE_DONE && result != SQLITE_BUSY)
vb@0
   353
			status = PEP_UNKNOWN_ERROR;
vb@0
   354
	} while (result == SQLITE_BUSY);
vb@0
   355
	sqlite3_reset(_session->log);
vb@0
   356
vb@0
   357
	return status;
vb@0
   358
}
vb@0
   359
vb@0
   360
DYNAMIC_API PEP_STATUS safeword(
vb@0
   361
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
   362
            char **word, size_t *wsize
vb@0
   363
        )
vb@0
   364
{
vb@0
   365
	pEpSession *_session = (pEpSession *) session;
vb@0
   366
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   367
	int result;
vb@0
   368
vb@0
   369
	assert(_session);
vb@0
   370
	assert(word);
vb@0
   371
	assert(wsize);
vb@0
   372
vb@0
   373
	*word = NULL;
vb@0
   374
	*wsize = 0;
vb@0
   375
vb@0
   376
	if (lang == NULL)
vb@0
   377
		lang = "en";
vb@0
   378
vb@0
   379
	assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   380
            || (lang[0] >= 'a' && lang[0] <= 'z'));
vb@0
   381
	assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   382
            || (lang[1] >= 'a' && lang[1] <= 'z'));
vb@0
   383
	assert(lang[2] == 0);
vb@0
   384
vb@0
   385
	sqlite3_reset(_session->safeword);
vb@0
   386
    sqlite3_bind_text(_session->safeword, 1, lang, -1, SQLITE_STATIC);
vb@0
   387
	sqlite3_bind_int(_session->safeword, 2, value);
vb@0
   388
vb@0
   389
	result = sqlite3_step(_session->safeword);
vb@0
   390
	if (result == SQLITE_ROW) {
vb@0
   391
        *word = strdup((const char *) sqlite3_column_text(_session->safeword,
vb@0
   392
                    1));
vb@0
   393
		if (*word)
vb@0
   394
            *wsize = sqlite3_column_bytes(_session->safeword, 1);
vb@0
   395
		else
vb@0
   396
			status = PEP_SAFEWORD_NOT_FOUND;
vb@0
   397
	} else
vb@0
   398
		status = PEP_SAFEWORD_NOT_FOUND;
vb@0
   399
vb@0
   400
	sqlite3_reset(_session->safeword);
vb@0
   401
	return status;
vb@0
   402
}
vb@0
   403
vb@0
   404
DYNAMIC_API PEP_STATUS safewords(
vb@0
   405
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
   406
        char **words, size_t *wsize, int max_words
vb@0
   407
    )
vb@0
   408
{
vb@0
   409
	const char *source = fingerprint;
vb@0
   410
	char *buffer = calloc(1, MAX_SAFEWORDS_SPACE);
vb@0
   411
	char *dest = buffer;
vb@0
   412
	size_t fsize;
vb@0
   413
    PEP_STATUS _status;
vb@0
   414
vb@0
   415
	assert(session);
vb@0
   416
	assert(fingerprint);
vb@0
   417
	assert(words);
vb@0
   418
	assert(wsize);
vb@0
   419
	assert(max_words >= 0);
vb@0
   420
vb@0
   421
	*words = NULL;
vb@0
   422
	*wsize = 0;
vb@0
   423
vb@0
   424
    assert(buffer);
vb@0
   425
    if (buffer == NULL)
vb@0
   426
        return PEP_OUT_OF_MEMORY;
vb@0
   427
vb@0
   428
	fsize = strlen(fingerprint);
vb@0
   429
vb@0
   430
	if (lang == NULL)
vb@0
   431
		lang = "en";
vb@0
   432
vb@0
   433
	assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   434
            || (lang[0] >= 'a' && lang[0] <= 'z'));
vb@0
   435
	assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   436
            || (lang[1] >= 'a' && lang[1] <= 'z'));
vb@0
   437
	assert(lang[2] == 0);
vb@0
   438
vb@0
   439
	int n_words = 0;
vb@0
   440
	while (source < fingerprint + fsize) {
vb@0
   441
		uint16_t value;
vb@0
   442
		char *word;
vb@0
   443
		size_t _wsize;
vb@0
   444
		int j;
vb@0
   445
vb@0
   446
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
vb@0
   447
			if (*source >= 'a' && *source <= 'f')
vb@0
   448
				value += (*source - 'a' + 10) << (3 - j++) * 4;
vb@0
   449
			else if (*source >= 'A' && *source <= 'F')
vb@0
   450
				value += (*source - 'A' + 10) << (3 - j++) * 4;
vb@0
   451
			else if (*source >= '0' && *source <= '9')
vb@0
   452
				value += (*source - '0') << (3 - j++) * 4;
vb@0
   453
			
vb@0
   454
			source++;
vb@0
   455
		}
vb@0
   456
vb@0
   457
		_status = safeword(session, value, lang, &word, &_wsize);
vb@0
   458
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
   459
            free(buffer);
vb@0
   460
            return PEP_OUT_OF_MEMORY;
vb@0
   461
        }
vb@0
   462
		if (word == NULL) {
vb@0
   463
            free(buffer);
vb@0
   464
			return PEP_SAFEWORD_NOT_FOUND;
vb@0
   465
        }
vb@0
   466
vb@0
   467
		if (dest + _wsize < buffer + MAX_SAFEWORDS_SPACE - 1) {
vb@0
   468
			strncpy(dest, word, _wsize);
vb@0
   469
            free(word);
vb@0
   470
			dest += _wsize;
vb@0
   471
		}
vb@0
   472
		else {
vb@0
   473
            free(word);
vb@0
   474
			break; // buffer full
vb@0
   475
        }
vb@0
   476
vb@0
   477
		if (source < fingerprint + fsize
vb@0
   478
                && dest + _wsize < buffer + MAX_SAFEWORDS_SPACE - 1)
vb@0
   479
			*dest++ = ' ';
vb@0
   480
vb@0
   481
		++n_words;
vb@0
   482
		if (max_words && n_words >= max_words)
vb@0
   483
			break;
vb@0
   484
	}
vb@0
   485
vb@0
   486
	*words = buffer;
vb@0
   487
	*wsize = dest - buffer;
vb@0
   488
	return PEP_STATUS_OK;
vb@0
   489
}
vb@0
   490
vb@0
   491
pEp_identity *new_identity(
vb@0
   492
        const char *address, const char *fpr, const char *user_id,
vb@0
   493
        const char *username
vb@0
   494
    )
vb@0
   495
{
vb@0
   496
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
   497
    assert(result);
vb@0
   498
    if (result) {
vb@0
   499
        if (address) {
vb@0
   500
            result->address = strdup(address);
vb@0
   501
            assert(result->address);
vb@0
   502
            if (result->address == NULL) {
vb@0
   503
                free(result);
vb@0
   504
                return NULL;
vb@0
   505
            }
vb@0
   506
            result->address_size = strlen(address);
vb@0
   507
        }
vb@0
   508
        if (fpr) {
vb@0
   509
            result->fpr = strdup(fpr);
vb@0
   510
            assert(result->fpr);
vb@0
   511
            if (result->fpr == NULL) {
vb@0
   512
                free_identity(result);
vb@0
   513
                return NULL;
vb@0
   514
            }
vb@0
   515
            result->fpr_size = strlen(fpr);
vb@0
   516
        }
vb@0
   517
        if (user_id) {
vb@0
   518
            result->user_id = strdup(user_id);
vb@0
   519
            assert(result->user_id);
vb@0
   520
            if (result->user_id == NULL) {
vb@0
   521
                free_identity(result);
vb@0
   522
                return NULL;
vb@0
   523
            }
vb@0
   524
            result->user_id_size = strlen(user_id);
vb@0
   525
        }
vb@0
   526
        if (username) {
vb@0
   527
            result->username = strdup(username);
vb@0
   528
            assert(result->username);
vb@0
   529
            if (result->username == NULL) {
vb@0
   530
                free_identity(result);
vb@0
   531
                return NULL;
vb@0
   532
            }
vb@0
   533
            result->username_size = strlen(username);
vb@0
   534
        }
vb@0
   535
        result->struct_size = sizeof(pEp_identity);
vb@0
   536
    }
vb@0
   537
    return result;
vb@0
   538
}
vb@0
   539
vb@0
   540
void free_identity(pEp_identity *identity)
vb@0
   541
{
vb@0
   542
    if (identity) {
vb@0
   543
        free(identity->address);
vb@0
   544
        free(identity->fpr);
vb@0
   545
        free(identity->user_id);
vb@0
   546
        free(identity->username);
vb@0
   547
        free(identity);
vb@0
   548
    }
vb@0
   549
}
vb@0
   550
vb@0
   551
DYNAMIC_API PEP_STATUS get_identity(
vb@0
   552
        PEP_SESSION session, const char *address,
vb@0
   553
        pEp_identity **identity
vb@0
   554
    )
vb@0
   555
{
vb@0
   556
	pEpSession *_session = (pEpSession *) session;
vb@0
   557
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   558
	static pEp_identity *_identity;
vb@0
   559
	int result;
vb@0
   560
	const char *_lang;
vb@0
   561
vb@0
   562
	assert(session);
vb@0
   563
	assert(address);
vb@8
   564
    assert(address[0]);
vb@0
   565
vb@0
   566
    sqlite3_reset(_session->get_identity);
vb@0
   567
    sqlite3_bind_text(_session->get_identity, 1, address, -1, SQLITE_STATIC);
vb@0
   568
vb@0
   569
    result = sqlite3_step(_session->get_identity);
vb@0
   570
	switch (result) {
vb@0
   571
	case SQLITE_ROW:
vb@0
   572
        _identity = new_identity(
vb@0
   573
                address,
vb@0
   574
                (const char *) sqlite3_column_text(_session->get_identity, 0),
vb@0
   575
                (const char *) sqlite3_column_text(_session->get_identity, 1),
vb@0
   576
                (const char *) sqlite3_column_text(_session->get_identity, 2)
vb@0
   577
                );
vb@0
   578
        assert(_identity);
vb@0
   579
        if (_identity == NULL)
vb@0
   580
            return PEP_OUT_OF_MEMORY;
vb@0
   581
vb@0
   582
        _identity->comm_type = (PEP_comm_type) sqlite3_column_int(_session->get_identity, 3);
vb@0
   583
        _lang = (const char *) sqlite3_column_text(_session->get_identity, 4);
vb@0
   584
        if (_lang && _lang[0]) {
vb@0
   585
			assert(_lang[0] >= 'a' && _lang[0] <= 'z');
vb@0
   586
			assert(_lang[1] >= 'a' && _lang[1] <= 'z');
vb@0
   587
			assert(_lang[2] == 0);
vb@0
   588
			_identity->lang[0] = _lang[0];
vb@0
   589
			_identity->lang[1] = _lang[1];
vb@0
   590
            _identity->lang[2] = 0;
vb@0
   591
		}
vb@0
   592
		*identity = _identity;
vb@0
   593
		break;
vb@0
   594
	default:
vb@0
   595
        status = PEP_CANNOT_FIND_IDENTITY;
vb@0
   596
		*identity = NULL;
vb@0
   597
	}
vb@0
   598
vb@0
   599
    sqlite3_reset(_session->get_identity);
vb@0
   600
	return status;
vb@0
   601
}
vb@0
   602
vb@0
   603
DYNAMIC_API PEP_STATUS set_identity(
vb@0
   604
        PEP_SESSION session, const pEp_identity *identity
vb@0
   605
    )
vb@0
   606
{
vb@0
   607
	pEpSession *_session = (pEpSession *) session;
vb@0
   608
	int result;
vb@0
   609
vb@0
   610
	assert(session);
vb@0
   611
	assert(identity);
vb@0
   612
	assert(identity->address);
vb@0
   613
	assert(identity->fpr);
vb@0
   614
	assert(identity->user_id);
vb@0
   615
	assert(identity->username);
vb@0
   616
vb@0
   617
	sqlite3_exec(_session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
   618
vb@0
   619
	sqlite3_reset(_session->set_person);
vb@0
   620
    sqlite3_bind_text(_session->set_person, 1, identity->user_id, -1,
vb@0
   621
            SQLITE_STATIC);
vb@0
   622
    sqlite3_bind_text(_session->set_person, 2, identity->username, -1,
vb@0
   623
            SQLITE_STATIC);
vb@0
   624
	if (identity->lang[0])
vb@0
   625
        sqlite3_bind_text(_session->set_person, 3, identity->lang, 1,
vb@0
   626
                SQLITE_STATIC);
vb@0
   627
	else
vb@0
   628
		sqlite3_bind_null(_session->set_person, 3);
vb@0
   629
	result = sqlite3_step(_session->set_person);
vb@0
   630
	sqlite3_reset(_session->set_person);
vb@0
   631
	if (result != SQLITE_DONE) {
vb@0
   632
		sqlite3_exec(_session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   633
		return PEP_CANNOT_SET_PERSON;
vb@0
   634
	}
vb@0
   635
vb@0
   636
	sqlite3_reset(_session->set_pgp_keypair);
vb@0
   637
    sqlite3_bind_text(_session->set_pgp_keypair, 1, identity->fpr, -1,
vb@0
   638
            SQLITE_STATIC);
vb@0
   639
	result = sqlite3_step(_session->set_pgp_keypair);
vb@0
   640
	sqlite3_reset(_session->set_pgp_keypair);
vb@0
   641
	if (result != SQLITE_DONE) {
vb@0
   642
		sqlite3_exec(_session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   643
		return PEP_CANNOT_SET_PGP_KEYPAIR;
vb@0
   644
	}
vb@0
   645
vb@0
   646
	sqlite3_reset(_session->set_identity);
vb@0
   647
    sqlite3_bind_text(_session->set_identity, 1, identity->address, -1,
vb@0
   648
            SQLITE_STATIC);
vb@0
   649
    sqlite3_bind_text(_session->set_identity, 2, identity->fpr, -1,
vb@0
   650
            SQLITE_STATIC);
vb@0
   651
    sqlite3_bind_text(_session->set_identity, 3, identity->user_id, -1,
vb@0
   652
            SQLITE_STATIC);
vb@0
   653
	result = sqlite3_step(_session->set_identity);
vb@0
   654
	sqlite3_reset(_session->set_identity);
vb@0
   655
	if (result != SQLITE_DONE) {
vb@0
   656
		sqlite3_exec(_session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   657
		return PEP_CANNOT_SET_IDENTITY;
vb@0
   658
	}
vb@0
   659
vb@0
   660
	sqlite3_reset(_session->set_trust);
vb@0
   661
    sqlite3_bind_text(_session->set_trust, 1, identity->user_id, -1,
vb@0
   662
            SQLITE_STATIC);
vb@0
   663
    sqlite3_bind_text(_session->set_trust, 2, identity->fpr, -1,
vb@0
   664
            SQLITE_STATIC);
vb@0
   665
	sqlite3_bind_int(_session->set_trust, 3, identity->comm_type);
vb@0
   666
	result = sqlite3_step(_session->set_trust);
vb@0
   667
	sqlite3_reset(_session->set_trust);
vb@0
   668
	if (result != SQLITE_DONE) {
vb@0
   669
		sqlite3_exec(_session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   670
		return PEP_CANNOT_SET_IDENTITY;
vb@0
   671
	}
vb@0
   672
vb@0
   673
    result = sqlite3_exec(_session->db, "COMMIT ;", NULL, NULL, NULL);
vb@0
   674
	if (result == SQLITE_OK)
vb@0
   675
		return PEP_STATUS_OK;
vb@0
   676
	else
vb@0
   677
		return PEP_COMMIT_FAILED;
vb@0
   678
}
vb@0
   679
vb@0
   680
void pEp_free(void *p)
vb@0
   681
{
vb@0
   682
    free(p);
vb@0
   683
}
vb@0
   684
vb@8
   685
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
   686
{
vb@8
   687
    pEpSession *_session = (pEpSession *) session;
vb@8
   688
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
   689
    int result;
vb@8
   690
vb@8
   691
    assert(session);
vb@8
   692
    assert(identity);
vb@8
   693
    assert(identity->user_id);
vb@8
   694
    assert(identity->user_id[0]);
vb@8
   695
    assert(identity->fpr);
vb@8
   696
    assert(identity->fpr[0]);
vb@8
   697
vb@8
   698
    identity->comm_type = PEP_ct_unknown;
vb@8
   699
vb@8
   700
    sqlite3_reset(_session->get_trust);
vb@8
   701
    sqlite3_bind_text(_session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
vb@8
   702
    sqlite3_bind_text(_session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
   703
vb@8
   704
    result = sqlite3_step(_session->get_trust);
vb@8
   705
    switch (result) {
vb@8
   706
    case SQLITE_ROW: {
vb@8
   707
        const char * user_id = (const char *) sqlite3_column_text(_session->get_trust, 1);
vb@8
   708
        int comm_type = (PEP_comm_type) sqlite3_column_int(_session->get_trust, 2);
vb@8
   709
vb@8
   710
        if (strcmp(user_id, identity->user_id) != 0) {
vb@8
   711
            free(identity->user_id);
vb@8
   712
            identity->user_id = strdup(user_id);
vb@8
   713
            assert(identity->user_id);
vb@8
   714
            if (identity->user_id == NULL)
vb@8
   715
                return PEP_OUT_OF_MEMORY;
vb@8
   716
        }
vb@8
   717
        identity->comm_type = comm_type;
vb@8
   718
        break;
vb@8
   719
    }
vb@8
   720
 
vb@8
   721
    default:
vb@8
   722
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
   723
    }
vb@8
   724
vb@8
   725
    sqlite3_reset(_session->get_trust);
vb@8
   726
    return status;
vb@9
   727
}
vb@9
   728
vb@24
   729
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
   730
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
   731
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
   732
    )
vb@24
   733
{
vb@24
   734
    return pgp_decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
vb@24
   735
}
vb@24
   736
vb@24
   737
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
   738
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
   739
    size_t psize, char **ctext, size_t *csize
vb@24
   740
    )
vb@24
   741
{
vb@24
   742
    return pgp_encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
vb@24
   743
}
vb@24
   744
vb@24
   745
DYNAMIC_API PEP_STATUS verify_text(
vb@24
   746
    PEP_SESSION session, const char *text, size_t size,
vb@24
   747
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
   748
    )
vb@24
   749
{
vb@24
   750
    return pgp_verify_text(session, text, size, signature, sig_size, keylist);
vb@24
   751
}
vb@24
   752
vb@24
   753
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
   754
{
vb@24
   755
    return pgp_delete_keypair(session, fpr);
vb@24
   756
}
vb@24
   757
vb@24
   758
DYNAMIC_API PEP_STATUS export_key(
vb@24
   759
    PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
   760
    )
vb@24
   761
{
vb@24
   762
    return pgp_export_key(session, fpr, key_data, size);
vb@24
   763
}
vb@24
   764
vb@24
   765
DYNAMIC_API PEP_STATUS find_keys(
vb@24
   766
    PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
   767
    )
vb@24
   768
{
vb@24
   769
    return pgp_find_keys(session, pattern, keylist);
vb@24
   770
}
vb@24
   771
vb@24
   772
DYNAMIC_API PEP_STATUS generate_keypair(
vb@24
   773
    PEP_SESSION session, pEp_identity *identity
vb@24
   774
    )
vb@24
   775
{
vb@24
   776
    return pgp_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@24
   785
    return pgp_get_key_rating(session, fpr, comm_type);
vb@24
   786
}
vb@9
   787
vb@24
   788
DYNAMIC_API PEP_STATUS import_key(PEP_SESSION session, const char *key_data, size_t size)
vb@24
   789
{
vb@24
   790
    return pgp_import_key(session, key_data, size);
vb@24
   791
}
vb@9
   792
vb@24
   793
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
   794
{
vb@24
   795
    return pgp_recv_key(session, pattern);
vb@24
   796
}
vb@9
   797
vb@24
   798
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
   799
{
vb@24
   800
    return pgp_send_key(session, pattern);
vb@14
   801
}