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