src/pEpEngine.c
author Volker Birk <vb@pep-project.org>
Tue, 01 Mar 2016 18:42:07 +0100
changeset 482 cd6a24c7791d
parent 479 e7ba9e9272af
child 494 ddf4130d00a0
permissions -rw-r--r--
peptest_hack added
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@221
     6
static 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@233
    13
	static const char *sql_trustword;
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@251
    20
    static const char *sql_least_trust;
vb@357
    21
    static const char *sql_mark_as_compromized;
vb@450
    22
    static const char *sql_crashdump;
vb@456
    23
    static const char *sql_blacklist_keys;
vb@458
    24
    static const char *sql_languagelist;
vb@458
    25
    static const char *sql_i18n_token;
vb@482
    26
    static const char *sql_peptest_hack;
vb@450
    27
vb@62
    28
    bool in_first = false;
vb@8
    29
vb@62
    30
    assert(sqlite3_threadsafe());
vb@62
    31
    if (!sqlite3_threadsafe())
vb@62
    32
        return PEP_INIT_SQLITE3_WITHOUT_MUTEX;
vb@62
    33
vb@62
    34
    // a little race condition - but still a race condition
vb@113
    35
    // mitigated by calling caveat (see documentation)
vb@62
    36
vb@62
    37
    ++init_count;
vb@67
    38
    if (init_count == 0)
vb@62
    39
        in_first = true;
vb@0
    40
vb@0
    41
	assert(session);
vb@191
    42
    if (session == NULL)
vb@191
    43
        return PEP_ILLEGAL_VALUE;
vb@191
    44
vb@0
    45
	*session = NULL;
vb@0
    46
vb@107
    47
    pEpSession *_session = calloc(1, sizeof(pEpSession));
vb@0
    48
	assert(_session);
vb@0
    49
	if (_session == NULL)
vb@65
    50
		goto enomem;
vb@62
    51
vb@0
    52
	_session->version = PEP_ENGINE_VERSION;
vb@0
    53
vb@0
    54
    assert(LOCAL_DB);
vb@0
    55
    if (LOCAL_DB == NULL) {
vb@65
    56
        status = PEP_INIT_CANNOT_OPEN_DB;
vb@65
    57
        goto pep_error;
vb@0
    58
    }
vb@0
    59
vb@0
    60
	int_result = sqlite3_open_v2(
vb@0
    61
			LOCAL_DB,
vb@0
    62
			&_session->db,
vb@0
    63
			SQLITE_OPEN_READWRITE
vb@0
    64
				| SQLITE_OPEN_CREATE
vb@0
    65
				| SQLITE_OPEN_FULLMUTEX
vb@0
    66
				| SQLITE_OPEN_PRIVATECACHE,
vb@0
    67
			NULL 
vb@0
    68
		);
vb@0
    69
vb@0
    70
	if (int_result != SQLITE_OK) {
vb@65
    71
		status = PEP_INIT_CANNOT_OPEN_DB;
vb@65
    72
        goto pep_error;
vb@0
    73
	}
vb@0
    74
vb@0
    75
	sqlite3_busy_timeout(_session->db, BUSY_WAIT_TIME);
vb@0
    76
vb@0
    77
    assert(SYSTEM_DB);
vb@0
    78
    if (SYSTEM_DB == NULL) {
vb@65
    79
		status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
vb@65
    80
        goto pep_error;
vb@0
    81
    }
vb@0
    82
vb@0
    83
	int_result = sqlite3_open_v2(
vb@0
    84
			SYSTEM_DB, &_session->system_db,
vb@0
    85
			SQLITE_OPEN_READONLY
vb@0
    86
				| SQLITE_OPEN_FULLMUTEX
vb@0
    87
				| SQLITE_OPEN_SHAREDCACHE,
vb@0
    88
			NULL
vb@0
    89
		);
vb@0
    90
vb@0
    91
	if (int_result != SQLITE_OK) {
vb@65
    92
		status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
vb@65
    93
        goto pep_error;
vb@0
    94
	}
vb@0
    95
vb@0
    96
	sqlite3_busy_timeout(_session->system_db, 1000);
vb@0
    97
vb@62
    98
    if (in_first) {
vb@62
    99
        int_result = sqlite3_exec(
vb@62
   100
            _session->db,
vb@456
   101
                "create table if not exists version_info (\n"
vb@456
   102
                "   id integer primary key,\n"
vb@456
   103
                "   timestamp integer default (datetime('now')) ,\n"
vb@456
   104
                "   version text,\n"
vb@456
   105
                "   comment text\n"
vb@456
   106
                ");\n"
vb@456
   107
                "create table if not exists log (\n"
vb@456
   108
                "   timestamp integer default (datetime('now')) ,\n"
vb@456
   109
                "   title text not null,\n"
vb@456
   110
                "   entity text not null,\n"
vb@456
   111
                "   description text,\n"
vb@456
   112
                "   comment text\n"
vb@456
   113
                ");\n"
vb@456
   114
                "create index if not exists log_timestamp on log (\n"
vb@456
   115
                "   timestamp\n"
vb@456
   116
                ");\n"
vb@456
   117
                "create table if not exists pgp_keypair (\n"
vb@456
   118
                "   fpr text primary key,\n"
vb@456
   119
                "   public_id text unique,\n"
vb@456
   120
                "   private_id text,\n"
vb@456
   121
                "   created integer,\n"
vb@456
   122
                "   expires integer,\n"
vb@456
   123
                "   comment text\n"
vb@456
   124
                ");\n"
vb@456
   125
                "create index if not exists pgp_keypair_expires on pgp_keypair (\n"
vb@456
   126
                "   expires\n"
vb@456
   127
                ");\n"
vb@456
   128
                "create table if not exists person (\n"
vb@456
   129
                "   id text primary key,\n"
vb@456
   130
                "   username text not null,\n"
vb@456
   131
                "   main_key_id text\n"
vb@456
   132
                "       references pgp_keypair (fpr)\n"
vb@456
   133
                "       on delete set null,\n"
vb@456
   134
                "   lang text,\n"
vb@456
   135
                "   comment text\n"
vb@456
   136
                ");\n"
vb@456
   137
                "create table if not exists identity (\n"
vb@456
   138
                "   address text primary key,\n"
vb@456
   139
                "   user_id text\n"
vb@456
   140
                "       references person (id)\n"
vb@456
   141
                "       on delete cascade,\n"
vb@456
   142
                "   main_key_id text\n"
vb@456
   143
                "       references pgp_keypair (fpr)\n"
vb@456
   144
                "       on delete set null,\n"
vb@456
   145
                "   comment text\n"
vb@456
   146
                ");\n"
vb@456
   147
                "create table if not exists trust (\n"
vb@456
   148
                "   user_id text not null\n"
vb@456
   149
                "       references person (id)\n"
vb@456
   150
                "       on delete cascade,\n"
vb@456
   151
                "   pgp_keypair_fpr text not null\n"
vb@456
   152
                "       references pgp_keypair (fpr)\n"
vb@456
   153
                "       on delete cascade,\n"
vb@456
   154
                "   comm_type integer not null,\n"
vb@456
   155
                "   comment text\n"
vb@456
   156
                ");\n"
vb@456
   157
                "create unique index if not exists trust_index on trust (\n"
vb@456
   158
                "   user_id,\n"
vb@456
   159
                "   pgp_keypair_fpr\n"
vb@456
   160
                ");\n"
vb@456
   161
                "create table if not exists blacklist_keys (\n"
vb@456
   162
                "   fpr text primary key\n"
vb@456
   163
                ");\n"
vb@456
   164
                ,
vb@62
   165
            NULL,
vb@62
   166
            NULL,
vb@62
   167
            NULL
vb@62
   168
        );
vb@62
   169
        assert(int_result == SQLITE_OK);
vb@0
   170
vb@62
   171
        int_result = sqlite3_exec(
vb@62
   172
            _session->db,
vb@62
   173
            "insert or replace into version_info (id, version) values (1, '1.0');",
vb@62
   174
            NULL,
vb@62
   175
            NULL,
vb@62
   176
            NULL
vb@62
   177
        );
vb@62
   178
        assert(int_result == SQLITE_OK);
vb@0
   179
vb@62
   180
        sql_log = "insert into log (title, entity, description, comment)"
vb@62
   181
                  "values (?1, ?2, ?3, ?4);";
vb@62
   182
vb@62
   183
        sql_get_identity =	"select fpr, identity.user_id, username, comm_type, lang"
vb@62
   184
                            "   from identity"
vb@62
   185
                            "   join person on id = identity.user_id"
vb@62
   186
                            "   join pgp_keypair on fpr = identity.main_key_id"
vb@62
   187
                            "   join trust on id = trust.user_id"
vb@62
   188
                            "       and pgp_keypair_fpr = identity.main_key_id"
vb@62
   189
                            "   where address = ?1 ;";
vb@62
   190
vb@233
   191
        sql_trustword = "select id, word from wordlist where lang = lower(?1) "
vb@62
   192
                       "and id = ?2 ;";
vb@62
   193
vb@62
   194
        sql_set_person = "insert or replace into person (id, username, lang) "
vb@62
   195
                         "values (?1, ?2, ?3) ;";
vb@62
   196
vb@62
   197
        sql_set_pgp_keypair = "insert or replace into pgp_keypair (fpr) "
vb@62
   198
                              "values (?1) ;";
vb@62
   199
vb@62
   200
        sql_set_identity = "insert or replace into identity (address, main_key_id, "
vb@62
   201
                           "user_id) values (?1, ?2, ?3) ;";
vb@62
   202
vb@62
   203
        sql_set_trust = "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type) "
vb@62
   204
                        "values (?1, ?2, ?3) ;";
vb@62
   205
vb@62
   206
        sql_get_trust = "select user_id, comm_type from trust where user_id = ?1 "
vb@62
   207
                        "and pgp_keypair_fpr = ?2 ;";
vb@251
   208
vb@251
   209
        sql_least_trust = "select min(comm_type) from trust where pgp_keypair_fpr = ?1 ;";
vb@357
   210
vb@457
   211
        sql_mark_as_compromized = "update trust not indexed set comm_type = 15"
vb@457
   212
                                  " where pgp_keypair_fpr = ?1 ;";
vb@450
   213
vb@457
   214
        sql_crashdump = "select timestamp, title, entity, description, comment"
vb@457
   215
                        " from log order by timestamp desc limit ?1 ;";
vb@458
   216
vb@458
   217
        sql_languagelist = "select lang, name from i18n_language order by lang ;";
vb@458
   218
vb@461
   219
        sql_i18n_token = "select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
vb@482
   220
vb@482
   221
        sql_peptest_hack = "delete from identity where address like '%@peptest.ch' ;";
vb@62
   222
    }
vb@62
   223
Edouard@417
   224
    int_result = sqlite3_prepare_v2(_session->db, sql_log, (int)strlen(sql_log),
vb@0
   225
            &_session->log, NULL);
vb@0
   226
	assert(int_result == SQLITE_OK);
vb@0
   227
vb@233
   228
    int_result = sqlite3_prepare_v2(_session->system_db, sql_trustword,
Edouard@417
   229
            (int)strlen(sql_trustword), &_session->trustword, NULL);
vb@0
   230
	assert(int_result == SQLITE_OK);
vb@0
   231
vb@0
   232
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
Edouard@417
   233
            (int)strlen(sql_get_identity), &_session->get_identity, NULL);
vb@0
   234
	assert(int_result == SQLITE_OK);
vb@0
   235
vb@0
   236
    int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
Edouard@417
   237
            (int)strlen(sql_set_person), &_session->set_person, NULL);
vb@0
   238
    assert(int_result == SQLITE_OK);
vb@62
   239
vb@0
   240
    int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
Edouard@417
   241
            (int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair, NULL);
vb@0
   242
	assert(int_result == SQLITE_OK);
vb@62
   243
vb@0
   244
    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity,
Edouard@417
   245
            (int)strlen(sql_set_identity), &_session->set_identity, NULL);
vb@0
   246
	assert(int_result == SQLITE_OK);
vb@62
   247
vb@0
   248
    int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
Edouard@417
   249
            (int)strlen(sql_set_trust), &_session->set_trust, NULL);
vb@0
   250
	assert(int_result == SQLITE_OK);
vb@62
   251
vb@8
   252
    int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
Edouard@417
   253
            (int)strlen(sql_get_trust), &_session->get_trust, NULL);
vb@8
   254
    assert(int_result == SQLITE_OK);
vb@0
   255
vb@251
   256
    int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
Edouard@417
   257
            (int)strlen(sql_least_trust), &_session->least_trust, NULL);
vb@251
   258
    assert(int_result == SQLITE_OK);
vb@251
   259
vb@357
   260
    int_result = sqlite3_prepare_v2(_session->db, sql_mark_as_compromized,
Edouard@417
   261
            (int)strlen(sql_mark_as_compromized), &_session->mark_compromized, NULL);
vb@357
   262
    assert(int_result == SQLITE_OK);
vb@357
   263
vb@453
   264
    int_result = sqlite3_prepare_v2(_session->db, sql_crashdump,
vb@453
   265
            (int)strlen(sql_crashdump), &_session->crashdump, NULL);
vb@453
   266
    assert(int_result == SQLITE_OK);
vb@453
   267
vb@458
   268
    int_result = sqlite3_prepare_v2(_session->system_db, sql_languagelist,
vb@458
   269
            (int)strlen(sql_languagelist), &_session->languagelist, NULL);
vb@458
   270
	assert(int_result == SQLITE_OK);
vb@458
   271
vb@458
   272
    int_result = sqlite3_prepare_v2(_session->system_db, sql_i18n_token,
vb@458
   273
            (int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
vb@458
   274
	assert(int_result == SQLITE_OK);
vb@458
   275
vb@482
   276
    int_result = sqlite3_prepare_v2(_session->db, sql_peptest_hack,
vb@482
   277
            (int)strlen(sql_peptest_hack), &_session->peptest_hack, NULL);
vb@482
   278
    assert(int_result == SQLITE_OK);
vb@482
   279
vb@65
   280
    status = init_cryptotech(_session, in_first);
vb@65
   281
    if (status != PEP_STATUS_OK)
vb@65
   282
        goto pep_error;
vb@0
   283
vb@65
   284
    status = init_transport_system(_session, in_first);
vb@65
   285
    if (status != PEP_STATUS_OK)
vb@65
   286
        goto pep_error;
vb@62
   287
vb@65
   288
    status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
vb@65
   289
    if (status != PEP_STATUS_OK)
vb@65
   290
        goto pep_error;
vb@65
   291
vb@464
   292
    // runtime config
vb@464
   293
vb@464
   294
    _session->passive_mode = false;
vb@464
   295
    _session->unencrypted_subject = false;
vb@464
   296
vb@65
   297
	*session = _session;
vb@0
   298
	return PEP_STATUS_OK;
vb@65
   299
vb@65
   300
enomem:
vb@65
   301
    status = PEP_OUT_OF_MEMORY;
vb@65
   302
vb@65
   303
pep_error:
vb@65
   304
    release(_session);
vb@65
   305
    return status;
vb@0
   306
}
vb@0
   307
vb@0
   308
DYNAMIC_API void release(PEP_SESSION session)
vb@0
   309
{
vb@62
   310
    bool out_last = false;
vb@62
   311
vb@62
   312
    assert(init_count >= 0);
vb@0
   313
	assert(session);
vb@0
   314
Edouard@385
   315
    if (!((init_count >= 0) && session))
vb@191
   316
        return;
vb@191
   317
vb@62
   318
    // a small race condition but still a race condition
vb@113
   319
    // mitigated by calling caveat (see documentation)
vb@62
   320
vb@62
   321
    if (init_count == 0)
vb@62
   322
        out_last = true;
vb@62
   323
    --init_count;
vb@62
   324
vb@46
   325
	if (session) {
vb@46
   326
		if (session->db) {
vb@233
   327
            if (session->trustword)
vb@233
   328
                sqlite3_finalize(session->trustword);
vb@65
   329
            if (session->log)
vb@65
   330
                sqlite3_finalize(session->log);
vb@65
   331
            if (session->get_identity)
vb@65
   332
                sqlite3_finalize(session->get_identity);
vb@65
   333
            if (session->set_identity)
vb@65
   334
                sqlite3_finalize(session->set_identity);
vb@65
   335
            if (session->set_person)
vb@65
   336
                sqlite3_finalize(session->set_person);
vb@65
   337
            if (session->set_pgp_keypair)
vb@65
   338
                sqlite3_finalize(session->set_pgp_keypair);
vb@65
   339
            if (session->set_trust)
vb@65
   340
                sqlite3_finalize(session->set_trust);
vb@65
   341
            if (session->get_trust)
vb@65
   342
                sqlite3_finalize(session->get_trust);
vb@251
   343
            if (session->least_trust)
vb@251
   344
                sqlite3_finalize(session->least_trust);
vb@26
   345
vb@65
   346
            if (session->db)
vb@65
   347
                sqlite3_close_v2(session->db);
vb@65
   348
            if (session->system_db)
vb@65
   349
                sqlite3_close_v2(session->system_db);
vb@0
   350
		}
vb@65
   351
vb@65
   352
        release_transport_system(session, out_last);
vb@65
   353
        release_cryptotech(session, out_last);
vb@65
   354
vb@65
   355
        free(session);
vb@62
   356
    }
vb@0
   357
}
vb@0
   358
vb@467
   359
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
vb@464
   360
{
vb@464
   361
    assert(session);
vb@467
   362
    session->passive_mode = enable;
vb@464
   363
}
vb@464
   364
vb@467
   365
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
vb@464
   366
{
vb@464
   367
    assert(session);
vb@467
   368
    session->unencrypted_subject = enable;
vb@464
   369
}
vb@464
   370
vb@0
   371
DYNAMIC_API PEP_STATUS log_event(
vb@450
   372
        PEP_SESSION session,
vb@451
   373
        const char *title,
vb@451
   374
        const char *entity,
vb@451
   375
        const char *description,
vb@451
   376
        const char *comment
vb@0
   377
    )
vb@0
   378
{
vb@0
   379
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   380
	int result;
vb@0
   381
vb@46
   382
	assert(session);
vb@0
   383
	assert(title);
vb@0
   384
	assert(entity);
vb@0
   385
vb@191
   386
    if (!(session && title && entity))
vb@191
   387
        return PEP_ILLEGAL_VALUE;
vb@191
   388
vb@46
   389
	sqlite3_reset(session->log);
vb@46
   390
	sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
vb@46
   391
	sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
vb@0
   392
	if (description)
vb@46
   393
        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
vb@0
   394
	else
vb@46
   395
		sqlite3_bind_null(session->log, 3);
vb@0
   396
	if (comment)
vb@46
   397
		sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
vb@0
   398
	else
vb@46
   399
		sqlite3_bind_null(session->log, 4);
vb@0
   400
	do {
vb@46
   401
		result = sqlite3_step(session->log);
vb@0
   402
		assert(result == SQLITE_DONE || result == SQLITE_BUSY);
vb@0
   403
		if (result != SQLITE_DONE && result != SQLITE_BUSY)
vb@0
   404
			status = PEP_UNKNOWN_ERROR;
vb@0
   405
	} while (result == SQLITE_BUSY);
vb@46
   406
	sqlite3_reset(session->log);
vb@0
   407
vb@0
   408
	return status;
vb@0
   409
}
vb@0
   410
vb@233
   411
DYNAMIC_API PEP_STATUS trustword(
vb@0
   412
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
   413
            char **word, size_t *wsize
vb@0
   414
        )
vb@0
   415
{
vb@0
   416
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   417
	int result;
vb@0
   418
vb@46
   419
	assert(session);
vb@0
   420
	assert(word);
vb@0
   421
	assert(wsize);
vb@0
   422
vb@191
   423
    if (!(session && word && wsize))
vb@191
   424
        return PEP_ILLEGAL_VALUE;
vb@191
   425
vb@0
   426
	*word = NULL;
vb@0
   427
	*wsize = 0;
vb@0
   428
vb@0
   429
	if (lang == NULL)
vb@0
   430
		lang = "en";
vb@0
   431
vb@0
   432
	assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   433
            || (lang[0] >= 'a' && lang[0] <= 'z'));
vb@0
   434
	assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   435
            || (lang[1] >= 'a' && lang[1] <= 'z'));
vb@0
   436
	assert(lang[2] == 0);
vb@0
   437
vb@233
   438
	sqlite3_reset(session->trustword);
vb@233
   439
    sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
vb@233
   440
	sqlite3_bind_int(session->trustword, 2, value);
vb@0
   441
vb@233
   442
	result = sqlite3_step(session->trustword);
vb@0
   443
	if (result == SQLITE_ROW) {
vb@233
   444
        *word = strdup((const char *) sqlite3_column_text(session->trustword,
vb@0
   445
                    1));
vb@0
   446
		if (*word)
vb@233
   447
            *wsize = sqlite3_column_bytes(session->trustword, 1);
vb@0
   448
		else
vb@251
   449
			status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
   450
	} else
vb@251
   451
		status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
   452
vb@233
   453
	sqlite3_reset(session->trustword);
vb@0
   454
	return status;
vb@0
   455
}
vb@0
   456
vb@233
   457
DYNAMIC_API PEP_STATUS trustwords(
vb@0
   458
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
   459
        char **words, size_t *wsize, int max_words
vb@0
   460
    )
vb@0
   461
{
vb@0
   462
	const char *source = fingerprint;
vb@109
   463
	char *buffer;
vb@125
   464
	char *dest;
vb@0
   465
	size_t fsize;
vb@0
   466
    PEP_STATUS _status;
vb@0
   467
vb@0
   468
	assert(session);
vb@0
   469
	assert(fingerprint);
vb@0
   470
	assert(words);
vb@0
   471
	assert(wsize);
vb@0
   472
	assert(max_words >= 0);
vb@0
   473
vb@191
   474
    if (!(session && fingerprint && words && wsize && max_words >= 0))
vb@191
   475
        return PEP_ILLEGAL_VALUE;
vb@191
   476
vb@0
   477
	*words = NULL;
vb@0
   478
	*wsize = 0;
vb@0
   479
vb@251
   480
    buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
vb@0
   481
    assert(buffer);
vb@0
   482
    if (buffer == NULL)
vb@0
   483
        return PEP_OUT_OF_MEMORY;
vb@125
   484
    dest = buffer;
vb@0
   485
vb@0
   486
	fsize = strlen(fingerprint);
vb@0
   487
vb@339
   488
	if (!lang || !lang[0])
vb@0
   489
		lang = "en";
vb@0
   490
vb@0
   491
	assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   492
            || (lang[0] >= 'a' && lang[0] <= 'z'));
vb@0
   493
	assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   494
            || (lang[1] >= 'a' && lang[1] <= 'z'));
vb@0
   495
	assert(lang[2] == 0);
vb@0
   496
vb@0
   497
	int n_words = 0;
vb@0
   498
	while (source < fingerprint + fsize) {
vb@0
   499
		uint16_t value;
vb@0
   500
		char *word;
vb@0
   501
		size_t _wsize;
vb@0
   502
		int j;
vb@0
   503
vb@0
   504
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
vb@0
   505
			if (*source >= 'a' && *source <= 'f')
vb@0
   506
				value += (*source - 'a' + 10) << (3 - j++) * 4;
vb@0
   507
			else if (*source >= 'A' && *source <= 'F')
vb@0
   508
				value += (*source - 'A' + 10) << (3 - j++) * 4;
vb@0
   509
			else if (*source >= '0' && *source <= '9')
vb@0
   510
				value += (*source - '0') << (3 - j++) * 4;
vb@0
   511
			
vb@0
   512
			source++;
vb@0
   513
		}
vb@0
   514
vb@233
   515
		_status = trustword(session, value, lang, &word, &_wsize);
vb@0
   516
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
   517
            free(buffer);
vb@0
   518
            return PEP_OUT_OF_MEMORY;
vb@0
   519
        }
vb@0
   520
		if (word == NULL) {
vb@0
   521
            free(buffer);
vb@251
   522
			return PEP_TRUSTWORD_NOT_FOUND;
vb@0
   523
        }
vb@0
   524
vb@251
   525
		if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
vb@0
   526
			strncpy(dest, word, _wsize);
vb@0
   527
            free(word);
vb@0
   528
			dest += _wsize;
vb@0
   529
		}
vb@0
   530
		else {
vb@0
   531
            free(word);
vb@0
   532
			break; // buffer full
vb@0
   533
        }
vb@0
   534
vb@0
   535
		if (source < fingerprint + fsize
vb@251
   536
                && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
vb@0
   537
			*dest++ = ' ';
vb@0
   538
vb@0
   539
		++n_words;
vb@0
   540
		if (max_words && n_words >= max_words)
vb@0
   541
			break;
vb@0
   542
	}
vb@0
   543
vb@0
   544
	*words = buffer;
vb@0
   545
	*wsize = dest - buffer;
vb@0
   546
	return PEP_STATUS_OK;
vb@0
   547
}
vb@0
   548
vb@0
   549
pEp_identity *new_identity(
vb@0
   550
        const char *address, const char *fpr, const char *user_id,
vb@0
   551
        const char *username
vb@0
   552
    )
vb@0
   553
{
vb@0
   554
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
   555
    assert(result);
vb@0
   556
    if (result) {
vb@0
   557
        if (address) {
vb@0
   558
            result->address = strdup(address);
vb@0
   559
            assert(result->address);
vb@0
   560
            if (result->address == NULL) {
vb@0
   561
                free(result);
vb@0
   562
                return NULL;
vb@0
   563
            }
vb@0
   564
            result->address_size = strlen(address);
vb@0
   565
        }
vb@0
   566
        if (fpr) {
vb@0
   567
            result->fpr = strdup(fpr);
vb@0
   568
            assert(result->fpr);
vb@0
   569
            if (result->fpr == NULL) {
vb@0
   570
                free_identity(result);
vb@0
   571
                return NULL;
vb@0
   572
            }
vb@0
   573
            result->fpr_size = strlen(fpr);
vb@0
   574
        }
vb@0
   575
        if (user_id) {
vb@0
   576
            result->user_id = strdup(user_id);
vb@0
   577
            assert(result->user_id);
vb@0
   578
            if (result->user_id == NULL) {
vb@0
   579
                free_identity(result);
vb@0
   580
                return NULL;
vb@0
   581
            }
vb@0
   582
            result->user_id_size = strlen(user_id);
vb@0
   583
        }
vb@0
   584
        if (username) {
vb@0
   585
            result->username = strdup(username);
vb@0
   586
            assert(result->username);
vb@0
   587
            if (result->username == NULL) {
vb@0
   588
                free_identity(result);
vb@0
   589
                return NULL;
vb@0
   590
            }
vb@0
   591
            result->username_size = strlen(username);
vb@0
   592
        }
vb@0
   593
        result->struct_size = sizeof(pEp_identity);
vb@0
   594
    }
vb@0
   595
    return result;
vb@0
   596
}
vb@0
   597
vb@37
   598
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
   599
{
vb@37
   600
    assert(src);
vb@37
   601
vb@37
   602
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
vb@37
   603
    assert(dup);
vb@37
   604
    if (dup == NULL)
vb@37
   605
        return NULL;
vb@37
   606
    
vb@37
   607
    dup->comm_type = src->comm_type;
vb@37
   608
    dup->lang[0] = src->lang[0];
vb@37
   609
    dup->lang[1] = src->lang[1];
vb@37
   610
    dup->lang[2] = 0;
vb@37
   611
    dup->me = src->me;
vb@37
   612
vb@37
   613
    return dup;
vb@37
   614
}
vb@37
   615
vb@0
   616
void free_identity(pEp_identity *identity)
vb@0
   617
{
vb@0
   618
    if (identity) {
vb@0
   619
        free(identity->address);
vb@0
   620
        free(identity->fpr);
vb@0
   621
        free(identity->user_id);
vb@0
   622
        free(identity->username);
vb@0
   623
        free(identity);
vb@0
   624
    }
vb@0
   625
}
vb@0
   626
vb@0
   627
DYNAMIC_API PEP_STATUS get_identity(
vb@0
   628
        PEP_SESSION session, const char *address,
vb@0
   629
        pEp_identity **identity
vb@0
   630
    )
vb@0
   631
{
vb@0
   632
	PEP_STATUS status = PEP_STATUS_OK;
vb@0
   633
	static pEp_identity *_identity;
vb@0
   634
	int result;
vb@0
   635
	const char *_lang;
vb@0
   636
vb@0
   637
	assert(session);
vb@0
   638
	assert(address);
vb@8
   639
    assert(address[0]);
vb@0
   640
vb@191
   641
    if (!(session && address && address[0]))
vb@191
   642
        return PEP_ILLEGAL_VALUE;
vb@191
   643
vb@46
   644
    sqlite3_reset(session->get_identity);
vb@46
   645
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
vb@0
   646
vb@46
   647
    result = sqlite3_step(session->get_identity);
vb@0
   648
	switch (result) {
vb@0
   649
	case SQLITE_ROW:
vb@0
   650
        _identity = new_identity(
vb@0
   651
                address,
vb@46
   652
                (const char *) sqlite3_column_text(session->get_identity, 0),
vb@46
   653
                (const char *) sqlite3_column_text(session->get_identity, 1),
vb@46
   654
                (const char *) sqlite3_column_text(session->get_identity, 2)
vb@0
   655
                );
vb@0
   656
        assert(_identity);
vb@0
   657
        if (_identity == NULL)
vb@0
   658
            return PEP_OUT_OF_MEMORY;
vb@0
   659
vb@46
   660
        _identity->comm_type = (PEP_comm_type) sqlite3_column_int(session->get_identity, 3);
vb@46
   661
        _lang = (const char *) sqlite3_column_text(session->get_identity, 4);
vb@0
   662
        if (_lang && _lang[0]) {
vb@0
   663
			assert(_lang[0] >= 'a' && _lang[0] <= 'z');
vb@0
   664
			assert(_lang[1] >= 'a' && _lang[1] <= 'z');
vb@0
   665
			assert(_lang[2] == 0);
vb@0
   666
			_identity->lang[0] = _lang[0];
vb@0
   667
			_identity->lang[1] = _lang[1];
vb@0
   668
            _identity->lang[2] = 0;
vb@0
   669
		}
vb@0
   670
		*identity = _identity;
vb@0
   671
		break;
vb@0
   672
	default:
vb@0
   673
        status = PEP_CANNOT_FIND_IDENTITY;
vb@0
   674
		*identity = NULL;
vb@0
   675
	}
vb@0
   676
vb@46
   677
    sqlite3_reset(session->get_identity);
vb@0
   678
	return status;
vb@0
   679
}
vb@0
   680
vb@0
   681
DYNAMIC_API PEP_STATUS set_identity(
vb@0
   682
        PEP_SESSION session, const pEp_identity *identity
vb@0
   683
    )
vb@0
   684
{
vb@0
   685
	int result;
vb@0
   686
vb@0
   687
	assert(session);
vb@0
   688
	assert(identity);
vb@0
   689
	assert(identity->address);
vb@0
   690
	assert(identity->fpr);
vb@0
   691
	assert(identity->user_id);
vb@0
   692
	assert(identity->username);
vb@0
   693
vb@191
   694
    if (!(session && identity && identity->address && identity->fpr &&
vb@191
   695
                identity->user_id && identity->username))
vb@191
   696
        return PEP_ILLEGAL_VALUE;
vb@191
   697
vb@46
   698
	sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
   699
vb@46
   700
	sqlite3_reset(session->set_person);
vb@46
   701
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
   702
            SQLITE_STATIC);
vb@46
   703
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
   704
            SQLITE_STATIC);
vb@0
   705
	if (identity->lang[0])
vb@46
   706
        sqlite3_bind_text(session->set_person, 3, identity->lang, 1,
vb@0
   707
                SQLITE_STATIC);
vb@0
   708
	else
vb@46
   709
		sqlite3_bind_null(session->set_person, 3);
vb@46
   710
	result = sqlite3_step(session->set_person);
vb@46
   711
	sqlite3_reset(session->set_person);
vb@0
   712
	if (result != SQLITE_DONE) {
vb@46
   713
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   714
		return PEP_CANNOT_SET_PERSON;
vb@0
   715
	}
vb@0
   716
vb@46
   717
	sqlite3_reset(session->set_pgp_keypair);
vb@46
   718
    sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
vb@0
   719
            SQLITE_STATIC);
vb@46
   720
	result = sqlite3_step(session->set_pgp_keypair);
vb@46
   721
	sqlite3_reset(session->set_pgp_keypair);
vb@0
   722
	if (result != SQLITE_DONE) {
vb@46
   723
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   724
		return PEP_CANNOT_SET_PGP_KEYPAIR;
vb@0
   725
	}
vb@0
   726
vb@46
   727
	sqlite3_reset(session->set_identity);
vb@46
   728
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
   729
            SQLITE_STATIC);
vb@46
   730
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
   731
            SQLITE_STATIC);
vb@46
   732
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
   733
            SQLITE_STATIC);
vb@46
   734
	result = sqlite3_step(session->set_identity);
vb@46
   735
	sqlite3_reset(session->set_identity);
vb@0
   736
	if (result != SQLITE_DONE) {
vb@46
   737
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   738
		return PEP_CANNOT_SET_IDENTITY;
vb@0
   739
	}
vb@0
   740
vb@46
   741
	sqlite3_reset(session->set_trust);
vb@46
   742
    sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
vb@0
   743
            SQLITE_STATIC);
vb@46
   744
    sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
vb@0
   745
            SQLITE_STATIC);
vb@46
   746
	sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
vb@46
   747
	result = sqlite3_step(session->set_trust);
vb@46
   748
	sqlite3_reset(session->set_trust);
vb@0
   749
	if (result != SQLITE_DONE) {
vb@46
   750
		sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@0
   751
		return PEP_CANNOT_SET_IDENTITY;
vb@0
   752
	}
vb@0
   753
vb@46
   754
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
vb@0
   755
	if (result == SQLITE_OK)
vb@0
   756
		return PEP_STATUS_OK;
vb@0
   757
	else
vb@0
   758
		return PEP_COMMIT_FAILED;
vb@0
   759
}
vb@0
   760
vb@357
   761
DYNAMIC_API PEP_STATUS mark_as_compromized(
vb@357
   762
        PEP_SESSION session,
vb@357
   763
        const char *fpr
vb@357
   764
    )
vb@357
   765
{
vb@357
   766
	int result;
vb@357
   767
vb@357
   768
	assert(session);
vb@357
   769
    assert(fpr && fpr[0]);
vb@357
   770
vb@357
   771
    if (!(session && fpr && fpr[0]))
vb@357
   772
        return PEP_ILLEGAL_VALUE;
vb@357
   773
vb@357
   774
	sqlite3_reset(session->mark_compromized);
vb@357
   775
    sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
vb@357
   776
            SQLITE_STATIC);
vb@357
   777
    result = sqlite3_step(session->mark_compromized);
vb@357
   778
	sqlite3_reset(session->mark_compromized);
vb@357
   779
vb@357
   780
    if (result != SQLITE_DONE)
vb@357
   781
        return PEP_CANNOT_SET_IDENTITY;
vb@357
   782
vb@357
   783
    return PEP_STATUS_OK;
vb@357
   784
}
vb@357
   785
vb@0
   786
void pEp_free(void *p)
vb@0
   787
{
vb@0
   788
    free(p);
vb@0
   789
}
vb@0
   790
vb@8
   791
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
   792
{
vb@8
   793
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
   794
    int result;
vb@8
   795
vb@8
   796
    assert(session);
vb@8
   797
    assert(identity);
vb@8
   798
    assert(identity->user_id);
vb@8
   799
    assert(identity->user_id[0]);
vb@8
   800
    assert(identity->fpr);
vb@8
   801
    assert(identity->fpr[0]);
vb@8
   802
vb@191
   803
    if (!(session && identity && identity->user_id && identity->user_id[0] &&
vb@191
   804
                identity->fpr && identity->fpr[0]))
vb@191
   805
        return PEP_ILLEGAL_VALUE;
vb@191
   806
vb@8
   807
    identity->comm_type = PEP_ct_unknown;
vb@8
   808
vb@46
   809
    sqlite3_reset(session->get_trust);
vb@46
   810
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
vb@46
   811
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
   812
vb@46
   813
    result = sqlite3_step(session->get_trust);
vb@8
   814
    switch (result) {
vb@8
   815
    case SQLITE_ROW: {
vb@259
   816
        const char * user_id = (const char *) sqlite3_column_text(session->get_trust, 0);
vb@259
   817
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 1);
vb@8
   818
vb@8
   819
        if (strcmp(user_id, identity->user_id) != 0) {
vb@8
   820
            free(identity->user_id);
vb@8
   821
            identity->user_id = strdup(user_id);
vb@8
   822
            assert(identity->user_id);
vb@8
   823
            if (identity->user_id == NULL)
vb@8
   824
                return PEP_OUT_OF_MEMORY;
vb@8
   825
        }
vb@8
   826
        identity->comm_type = comm_type;
vb@8
   827
        break;
vb@8
   828
    }
vb@8
   829
 
vb@8
   830
    default:
vb@8
   831
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
   832
    }
vb@8
   833
vb@46
   834
    sqlite3_reset(session->get_trust);
vb@8
   835
    return status;
vb@9
   836
}
vb@9
   837
vb@251
   838
DYNAMIC_API PEP_STATUS least_trust(
vb@251
   839
        PEP_SESSION session,
vb@251
   840
        const char *fpr,
vb@251
   841
        PEP_comm_type *comm_type
vb@251
   842
    )
vb@251
   843
{
vb@251
   844
    PEP_STATUS status = PEP_STATUS_OK;
vb@251
   845
    int result;
vb@251
   846
vb@251
   847
    assert(session);
vb@251
   848
    assert(fpr);
vb@251
   849
    assert(comm_type);
vb@251
   850
vb@259
   851
    *comm_type = PEP_ct_unknown;
vb@259
   852
vb@251
   853
    if (!(session && fpr && comm_type))
vb@251
   854
        return PEP_ILLEGAL_VALUE;
vb@251
   855
vb@251
   856
    sqlite3_reset(session->least_trust);
vb@251
   857
    sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
vb@251
   858
vb@251
   859
    result = sqlite3_step(session->least_trust);
vb@251
   860
    switch (result) {
vb@251
   861
        case SQLITE_ROW: {
vb@259
   862
            int _comm_type = sqlite3_column_int(session->least_trust, 0);
vb@259
   863
            *comm_type = (PEP_comm_type) _comm_type;
vb@251
   864
            break;
vb@251
   865
        }
vb@251
   866
        default:
vb@251
   867
            status = PEP_CANNOT_FIND_IDENTITY;
vb@251
   868
    }
vb@251
   869
vb@251
   870
    sqlite3_reset(session->least_trust);
vb@251
   871
    return status;
vb@251
   872
}
vb@251
   873
vb@24
   874
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
   875
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
   876
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
   877
    )
vb@24
   878
{
vb@191
   879
    assert(session);
vb@191
   880
    assert(ctext);
vb@191
   881
    assert(csize);
vb@191
   882
    assert(ptext);
vb@191
   883
    assert(psize);
vb@191
   884
    assert(keylist);
vb@191
   885
vb@191
   886
    if (!(session && ctext && csize && ptext && psize && keylist && keylist))
vb@191
   887
        return PEP_ILLEGAL_VALUE;
vb@191
   888
vb@46
   889
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
vb@24
   890
}
vb@24
   891
vb@24
   892
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
   893
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
   894
    size_t psize, char **ctext, size_t *csize
vb@24
   895
    )
vb@24
   896
{
vb@191
   897
    assert(session);
vb@191
   898
    assert(keylist);
vb@191
   899
    assert(ptext);
vb@191
   900
    assert(psize);
vb@191
   901
    assert(ctext);
vb@191
   902
    assert(csize);
vb@191
   903
vb@191
   904
    if (!(session && keylist && ptext && psize && ctext && csize))
vb@191
   905
        return PEP_ILLEGAL_VALUE;
vb@191
   906
vb@46
   907
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
vb@24
   908
}
vb@24
   909
vb@24
   910
DYNAMIC_API PEP_STATUS verify_text(
vb@24
   911
    PEP_SESSION session, const char *text, size_t size,
vb@24
   912
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
   913
    )
vb@24
   914
{
vb@191
   915
    assert(session);
vb@191
   916
    assert(text);
vb@191
   917
    assert(size);
vb@191
   918
    assert(signature);
vb@191
   919
    assert(sig_size);
vb@191
   920
    assert(keylist);
vb@191
   921
vb@191
   922
    if (!(session && text && size && signature && sig_size && keylist))
vb@191
   923
        return PEP_ILLEGAL_VALUE;
vb@191
   924
vb@46
   925
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text, size, signature, sig_size, keylist);
vb@24
   926
}
vb@24
   927
vb@24
   928
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
   929
{
vb@191
   930
    assert(session);
vb@191
   931
    assert(fpr);
vb@191
   932
vb@191
   933
    if (!(session && fpr))
vb@191
   934
        return PEP_ILLEGAL_VALUE;
vb@191
   935
vb@46
   936
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
   937
}
vb@24
   938
vb@24
   939
DYNAMIC_API PEP_STATUS export_key(
vb@46
   940
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
   941
    )
vb@24
   942
{
vb@191
   943
    assert(session);
vb@191
   944
    assert(fpr);
vb@191
   945
    assert(key_data);
vb@191
   946
    assert(size);
vb@191
   947
vb@191
   948
    if (!(session && fpr && key_data && size))
vb@191
   949
        return PEP_ILLEGAL_VALUE;
vb@191
   950
vb@46
   951
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr, key_data, size);
vb@24
   952
}
vb@24
   953
vb@24
   954
DYNAMIC_API PEP_STATUS find_keys(
vb@46
   955
        PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
   956
    )
vb@24
   957
{
vb@191
   958
    assert(session);
vb@191
   959
    assert(pattern);
vb@191
   960
    assert(keylist);
vb@191
   961
vb@191
   962
    if (!(session && pattern && keylist))
vb@191
   963
        return PEP_ILLEGAL_VALUE;
vb@191
   964
vb@46
   965
    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern, keylist);
vb@24
   966
}
vb@24
   967
vb@24
   968
DYNAMIC_API PEP_STATUS generate_keypair(
vb@46
   969
        PEP_SESSION session, pEp_identity *identity
vb@24
   970
    )
vb@24
   971
{
vb@191
   972
    assert(session);
vb@191
   973
    assert(identity);
vb@191
   974
    assert(identity->address);
vb@298
   975
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@191
   976
    assert(identity->username);
vb@191
   977
vb@298
   978
    if (!(session && identity && identity->address &&
vb@298
   979
        (identity->fpr == NULL || identity->fpr[0] == 0) && identity->username))
vb@191
   980
        return PEP_ILLEGAL_VALUE;
vb@191
   981
vb@46
   982
    return session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session, identity);
vb@24
   983
}
vb@24
   984
vb@9
   985
DYNAMIC_API PEP_STATUS get_key_rating(
vb@46
   986
        PEP_SESSION session,
vb@46
   987
        const char *fpr,
vb@46
   988
        PEP_comm_type *comm_type
vb@9
   989
    )
vb@9
   990
{
vb@191
   991
    assert(session);
vb@191
   992
    assert(fpr);
vb@191
   993
    assert(comm_type);
vb@191
   994
vb@191
   995
    if (!(session && fpr && comm_type))
vb@191
   996
        return PEP_ILLEGAL_VALUE;
vb@191
   997
vb@46
   998
    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr, comm_type);
vb@24
   999
}
vb@9
  1000
vb@24
  1001
DYNAMIC_API PEP_STATUS import_key(PEP_SESSION session, const char *key_data, size_t size)
vb@24
  1002
{
vb@191
  1003
    assert(session);
vb@191
  1004
    assert(key_data);
vb@191
  1005
vb@191
  1006
    if (!(session && key_data))
vb@191
  1007
        return PEP_ILLEGAL_VALUE;
vb@191
  1008
vb@46
  1009
    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data, size);
vb@24
  1010
}
vb@9
  1011
vb@24
  1012
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1013
{
vb@191
  1014
    assert(session);
vb@191
  1015
    assert(pattern);
vb@191
  1016
vb@191
  1017
    if (!(session && pattern))
vb@191
  1018
        return PEP_ILLEGAL_VALUE;
vb@191
  1019
vb@46
  1020
    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
vb@24
  1021
}
vb@9
  1022
vb@24
  1023
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
  1024
{
vb@191
  1025
    assert(session);
vb@191
  1026
    assert(pattern);
vb@191
  1027
vb@191
  1028
    if (!(session && pattern))
vb@191
  1029
        return PEP_ILLEGAL_VALUE;
vb@191
  1030
vb@46
  1031
    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
vb@14
  1032
}
vb@196
  1033
vb@201
  1034
DYNAMIC_API PEP_STATUS renew_key(
vb@201
  1035
        PEP_SESSION session,
vb@201
  1036
        const char *fpr,
vb@201
  1037
        const timestamp *ts
vb@201
  1038
    )
vb@196
  1039
{
vb@196
  1040
    assert(session);
vb@200
  1041
    assert(fpr);
vb@196
  1042
vb@200
  1043
    if (!(session && fpr))
vb@196
  1044
        return PEP_ILLEGAL_VALUE;
vb@196
  1045
vb@201
  1046
    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
vb@196
  1047
}
vb@196
  1048
vb@211
  1049
DYNAMIC_API PEP_STATUS revoke_key(
vb@211
  1050
        PEP_SESSION session,
vb@211
  1051
        const char *fpr,
vb@211
  1052
        const char *reason
vb@211
  1053
    )
vb@197
  1054
{
vb@197
  1055
    assert(session);
vb@200
  1056
    assert(fpr);
vb@197
  1057
vb@200
  1058
    if (!(session && fpr))
vb@197
  1059
        return PEP_ILLEGAL_VALUE;
vb@197
  1060
vb@214
  1061
    return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
vb@214
  1062
            reason);
vb@197
  1063
}
vb@197
  1064
vb@214
  1065
DYNAMIC_API PEP_STATUS key_expired(
vb@214
  1066
        PEP_SESSION session,
vb@214
  1067
        const char *fpr,
vb@214
  1068
        bool *expired
vb@214
  1069
    )
vb@214
  1070
{
vb@214
  1071
    assert(session);
vb@214
  1072
    assert(fpr);
vb@214
  1073
    assert(expired);
vb@214
  1074
vb@214
  1075
    if (!(session && fpr && expired))
vb@214
  1076
        return PEP_ILLEGAL_VALUE;
vb@214
  1077
vb@214
  1078
    return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
vb@214
  1079
            expired);
vb@214
  1080
}
vb@214
  1081
vb@451
  1082
static void _clean_log_value(char *text)
vb@451
  1083
{
vb@451
  1084
    if (text) {
vb@451
  1085
        for (char *c = text; *c; c++) {
vb@451
  1086
            if (*c < 32)
vb@451
  1087
                *c = 32;
vb@451
  1088
            else if (*c == '"')
vb@451
  1089
                *c = '\'';
vb@451
  1090
        }
vb@451
  1091
    }
vb@451
  1092
}
vb@451
  1093
vb@450
  1094
static char *_concat_string(char *str1, const char *str2, char delim)
vb@450
  1095
{
vb@454
  1096
    str2 = str2 ? str2 : "";
vb@450
  1097
    size_t len1 = str1 ? strlen(str1) : 0;
vb@450
  1098
    size_t len2 = strlen(str2);
vb@450
  1099
    size_t len = len1 + len2 + 3;
vb@450
  1100
    char * result = realloc(str1, len + 1);
vb@450
  1101
vb@450
  1102
    if (result) {
vb@450
  1103
        result[len1] = '"';
vb@450
  1104
        strcpy(result + len1 + 1, str2);
vb@450
  1105
        _clean_log_value(result + len1 + 1);
vb@450
  1106
        result[len - 2] = '"';
vb@450
  1107
        result[len - 1] = delim;
vb@450
  1108
        result[len] = 0;
vb@450
  1109
    }
vb@450
  1110
    else {
vb@450
  1111
        free(str1);
vb@450
  1112
    }
vb@450
  1113
vb@450
  1114
    return result;
vb@450
  1115
}
vb@450
  1116
vb@450
  1117
DYNAMIC_API PEP_STATUS get_crashdump_log(
vb@450
  1118
        PEP_SESSION session,
vb@450
  1119
        int maxlines,
vb@450
  1120
        char **logdata
vb@450
  1121
    )
vb@450
  1122
{
vb@450
  1123
    PEP_STATUS status = PEP_STATUS_OK;
vb@450
  1124
    char *_logdata= NULL;
vb@450
  1125
vb@450
  1126
    assert(session);
vb@450
  1127
    assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
vb@450
  1128
    assert(logdata);
vb@450
  1129
vb@450
  1130
    if (!(session && logdata && maxlines >= 0 && maxlines <=
vb@450
  1131
            CRASHDUMP_MAX_LINES))
vb@450
  1132
        return PEP_ILLEGAL_VALUE;
vb@450
  1133
vb@450
  1134
    int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
vb@458
  1135
    const char *timestamp = NULL;
vb@458
  1136
    const char *title = NULL;
vb@458
  1137
    const char *entity = NULL;
vb@458
  1138
    const char *desc = NULL;
vb@458
  1139
    const char *comment = NULL;
vb@450
  1140
vb@450
  1141
    sqlite3_reset(session->crashdump);
vb@452
  1142
    sqlite3_bind_int(session->crashdump, 1, limit);
vb@450
  1143
vb@450
  1144
    int result;
vb@450
  1145
vb@450
  1146
    do {
vb@450
  1147
        result = sqlite3_step(session->crashdump);
vb@450
  1148
        switch (result) {
vb@450
  1149
        case SQLITE_ROW:
vb@452
  1150
            timestamp = (const char *) sqlite3_column_text(session->crashdump, 0);
vb@452
  1151
            title   = (const char *) sqlite3_column_text(session->crashdump, 1);
vb@452
  1152
            entity  = (const char *) sqlite3_column_text(session->crashdump, 2);
vb@452
  1153
            desc    = (const char *) sqlite3_column_text(session->crashdump, 3);
vb@452
  1154
            comment = (const char *) sqlite3_column_text(session->crashdump, 4);
vb@452
  1155
vb@452
  1156
            _logdata = _concat_string(_logdata, timestamp, ',');
vb@452
  1157
            if (_logdata == NULL)
vb@452
  1158
                goto enomem;
vb@450
  1159
vb@450
  1160
            _logdata = _concat_string(_logdata, title, ',');
vb@450
  1161
            if (_logdata == NULL)
vb@450
  1162
                goto enomem;
vb@450
  1163
vb@450
  1164
            _logdata = _concat_string(_logdata, entity, ',');
vb@450
  1165
            if (_logdata == NULL)
vb@450
  1166
                goto enomem;
vb@450
  1167
vb@450
  1168
            _logdata = _concat_string(_logdata, desc, ',');
vb@450
  1169
            if (_logdata == NULL)
vb@450
  1170
                goto enomem;
vb@450
  1171
vb@450
  1172
            _logdata = _concat_string(_logdata, comment, '\n');
vb@450
  1173
            if (_logdata == NULL)
vb@450
  1174
                goto enomem;
vb@450
  1175
vb@450
  1176
            break;
vb@450
  1177
vb@450
  1178
        case SQLITE_DONE:
vb@450
  1179
            break;
vb@450
  1180
vb@450
  1181
        default:
vb@450
  1182
            status = PEP_UNKNOWN_ERROR;
vb@450
  1183
            result = SQLITE_DONE;
vb@450
  1184
        }
vb@450
  1185
    } while (result != SQLITE_DONE);
vb@450
  1186
vb@450
  1187
    sqlite3_reset(session->crashdump);
vb@450
  1188
    if (status == PEP_STATUS_OK)
vb@450
  1189
        *logdata = _logdata;
vb@450
  1190
vb@450
  1191
    goto the_end;
vb@450
  1192
vb@450
  1193
enomem:
vb@450
  1194
    status = PEP_OUT_OF_MEMORY;
vb@450
  1195
vb@450
  1196
the_end:
vb@450
  1197
    return status;
vb@450
  1198
}
vb@450
  1199
vb@458
  1200
DYNAMIC_API PEP_STATUS get_languagelist(
vb@458
  1201
        PEP_SESSION session,
vb@458
  1202
        char **languages
vb@458
  1203
    )
vb@458
  1204
{
vb@458
  1205
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1206
    char *_languages= NULL;
vb@458
  1207
vb@458
  1208
    assert(session);
vb@458
  1209
    assert(languages);
vb@458
  1210
vb@458
  1211
    if (!(session && languages))
vb@458
  1212
        return PEP_ILLEGAL_VALUE;
vb@458
  1213
vb@458
  1214
    const char *lang = NULL;
vb@458
  1215
    const char *name = NULL;
vb@458
  1216
vb@458
  1217
    sqlite3_reset(session->languagelist);
vb@458
  1218
vb@458
  1219
    int result;
vb@458
  1220
vb@458
  1221
    do {
vb@458
  1222
        result = sqlite3_step(session->languagelist);
vb@458
  1223
        switch (result) {
vb@458
  1224
        case SQLITE_ROW:
vb@458
  1225
            lang = (const char *) sqlite3_column_text(session->languagelist, 0);
vb@458
  1226
            name = (const char *) sqlite3_column_text(session->languagelist, 1);
vb@458
  1227
vb@458
  1228
            _languages = _concat_string(_languages, lang, ',');
vb@458
  1229
            if (_languages == NULL)
vb@458
  1230
                goto enomem;
vb@458
  1231
vb@458
  1232
            _languages = _concat_string(_languages, name, '\n');
vb@458
  1233
            if (_languages == NULL)
vb@458
  1234
                goto enomem;
vb@458
  1235
vb@458
  1236
            break;
vb@458
  1237
vb@458
  1238
        case SQLITE_DONE:
vb@458
  1239
            break;
vb@458
  1240
vb@458
  1241
        default:
vb@458
  1242
            status = PEP_UNKNOWN_ERROR;
vb@458
  1243
            result = SQLITE_DONE;
vb@458
  1244
        }
vb@458
  1245
    } while (result != SQLITE_DONE);
vb@458
  1246
vb@458
  1247
    sqlite3_reset(session->languagelist);
vb@458
  1248
    if (status == PEP_STATUS_OK)
vb@458
  1249
        *languages = _languages;
vb@458
  1250
vb@458
  1251
    goto the_end;
vb@458
  1252
vb@458
  1253
enomem:
vb@458
  1254
    status = PEP_OUT_OF_MEMORY;
vb@458
  1255
vb@458
  1256
the_end:
vb@458
  1257
    return status;
vb@458
  1258
}
vb@458
  1259
vb@458
  1260
DYNAMIC_API PEP_STATUS get_phrase(
vb@458
  1261
        PEP_SESSION session,
vb@458
  1262
        const char *lang,
vb@458
  1263
        int phrase_id,
vb@458
  1264
        char **phrase
vb@458
  1265
    )
vb@458
  1266
{
vb@458
  1267
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1268
    *phrase = NULL;
vb@458
  1269
vb@479
  1270
    assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
vb@458
  1271
vb@479
  1272
    if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
vb@458
  1273
        return PEP_ILLEGAL_VALUE;
vb@458
  1274
vb@458
  1275
    sqlite3_reset(session->i18n_token);
vb@458
  1276
	sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
vb@458
  1277
	sqlite3_bind_int(session->i18n_token, 2, phrase_id);
vb@458
  1278
vb@458
  1279
    const char *_phrase = NULL;
vb@458
  1280
    int result;
vb@458
  1281
vb@458
  1282
    result = sqlite3_step(session->i18n_token);
vb@458
  1283
    switch (result) {
vb@458
  1284
    case SQLITE_ROW:
vb@458
  1285
        _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
vb@458
  1286
        break;
vb@458
  1287
vb@458
  1288
    case SQLITE_DONE:
vb@458
  1289
        status = PEP_PHRASE_NOT_FOUND;
vb@458
  1290
        break;
vb@458
  1291
vb@458
  1292
    default:
vb@458
  1293
        status = PEP_UNKNOWN_ERROR;
vb@458
  1294
    }
vb@458
  1295
vb@458
  1296
    if (status == PEP_STATUS_OK) {
vb@458
  1297
        *phrase = strdup(_phrase);
vb@458
  1298
        if (*phrase == NULL)
vb@458
  1299
            goto enomem;
vb@458
  1300
    }
vb@459
  1301
vb@459
  1302
    sqlite3_reset(session->i18n_token);
vb@458
  1303
    goto the_end;
vb@458
  1304
vb@458
  1305
enomem:
vb@458
  1306
    status = PEP_OUT_OF_MEMORY;
vb@458
  1307
vb@458
  1308
the_end:
vb@458
  1309
    return status;
vb@458
  1310
}
vb@458
  1311
vb@482
  1312
DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
vb@482
  1313
{
vb@482
  1314
    PEP_STATUS status = PEP_STATUS_OK;
vb@482
  1315
vb@482
  1316
    assert(session);
vb@482
  1317
vb@482
  1318
    if (!session)
vb@482
  1319
        return PEP_ILLEGAL_VALUE;
vb@482
  1320
vb@482
  1321
    sqlite3_reset(session->peptest_hack);
vb@482
  1322
vb@482
  1323
    int result;
vb@482
  1324
vb@482
  1325
    result = sqlite3_step(session->peptest_hack);
vb@482
  1326
    switch (result) {
vb@482
  1327
    case SQLITE_ROW:
vb@482
  1328
    case SQLITE_DONE:
vb@482
  1329
        status = PEP_STATUS_OK;
vb@482
  1330
        break;
vb@482
  1331
vb@482
  1332
    default:
vb@482
  1333
        status = PEP_UNKNOWN_ERROR;
vb@482
  1334
    }
vb@482
  1335
vb@482
  1336
    sqlite3_reset(session->peptest_hack);
vb@482
  1337
    goto the_end;
vb@482
  1338
vb@482
  1339
enomem:
vb@482
  1340
    status = PEP_OUT_OF_MEMORY;
vb@482
  1341
vb@482
  1342
the_end:
vb@482
  1343
    return status;
vb@482
  1344
}
vb@482
  1345