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