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