Browse Source

Own keys (SQL and accessors functions)

doc_update_sequoia
Edouard Tisserant 6 years ago
parent
commit
8e953a1aa7
4 changed files with 206 additions and 3 deletions
  1. +125
    -0
      src/keymanagement.c
  2. +37
    -0
      src/keymanagement.h
  3. +38
    -3
      src/pEpEngine.c
  4. +6
    -0
      src/pEp_internal.h

+ 125
- 0
src/keymanagement.c View File

@ -541,3 +541,128 @@ DYNAMIC_API PEP_STATUS trust_personal_key(
return status;
}
DYNAMIC_API PEP_STATUS own_key_add(PEP_SESSION session, const char *fpr)
{
PEP_STATUS status = PEP_STATUS_OK;
assert(session && fpr && fpr[0]);
if (!(session && fpr && fpr[0]))
return PEP_ILLEGAL_VALUE;
sqlite3_reset(session->own_key_add);
sqlite3_bind_text(session->own_key_add, 1, fpr, -1, SQLITE_STATIC);
int result;
result = sqlite3_step(session->own_key_add);
switch (result) {
case SQLITE_DONE:
status = PEP_STATUS_OK;
break;
default:
status = PEP_UNKNOWN_ERROR;
}
sqlite3_reset(session->own_key_add);
return status;
}
DYNAMIC_API PEP_STATUS own_key_is_listed(
PEP_SESSION session,
const char *fpr,
bool *listed
)
{
PEP_STATUS status = PEP_STATUS_OK;
int count;
assert(session && fpr && fpr[0] && listed);
if (!(session && fpr && fpr[0] && listed))
return PEP_ILLEGAL_VALUE;
*listed = false;
sqlite3_reset(session->own_key_is_listed);
sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1, SQLITE_STATIC);
int result;
result = sqlite3_step(session->own_key_is_listed);
switch (result) {
case SQLITE_ROW:
count = sqlite3_column_int(session->own_key_is_listed, 0);
*listed = count > 0;
status = PEP_STATUS_OK;
break;
default:
status = PEP_UNKNOWN_ERROR;
}
sqlite3_reset(session->own_key_is_listed);
return status;
}
DYNAMIC_API PEP_STATUS own_key_retrieve(
PEP_SESSION session,
stringlist_t **own_key
)
{
PEP_STATUS status = PEP_STATUS_OK;
assert(session);
assert(own_key);
if (!(session && own_key))
return PEP_ILLEGAL_VALUE;
*own_key = NULL;
stringlist_t *_own_key = new_stringlist(NULL);
if (_own_key == NULL)
goto enomem;
sqlite3_reset(session->own_key_retrieve);
int result;
const char *fpr = NULL;
stringlist_t *_bl = _own_key;
do {
result = sqlite3_step(session->own_key_retrieve);
switch (result) {
case SQLITE_ROW:
fpr = (const char *) sqlite3_column_text(session->own_key_retrieve, 0);
_bl = stringlist_add(_bl, fpr);
if (_bl == NULL)
goto enomem;
break;
case SQLITE_DONE:
break;
default:
status = PEP_UNKNOWN_ERROR;
result = SQLITE_DONE;
}
} while (result != SQLITE_DONE);
sqlite3_reset(session->own_key_retrieve);
if (status == PEP_STATUS_OK)
*own_key = _own_key;
else
free_stringlist(_own_key);
goto the_end;
enomem:
free_stringlist(_own_key);
status = PEP_OUT_OF_MEMORY;
the_end:
return status;
}

+ 37
- 0
src/keymanagement.h View File

@ -152,6 +152,43 @@ DYNAMIC_API PEP_STATUS key_reset_trust(
pEp_identity *ident
);
// own_key_add() - add to own keys
//
// parameters:
// session (in) session to use
// fpr (in) fingerprint of owned key
DYNAMIC_API PEP_STATUS own_key_add(PEP_SESSION session, const char *fpr);
// own_key_is_listed() - returns true id key is listed as own key
//
// parameters:
// session (in) session to use
// fpr (in) fingerprint of key to test
// bool (out) flags if key is own
DYNAMIC_API PEP_STATUS own_key_is_listed(
PEP_SESSION session,
const char *fpr,
bool *listed
);
// own_key_retrieve() - retrieve all own keys fingerprints
//
// parameters:
// session (in) session to use
// own_key (out) copy of own_key
//
// caveat:
// the ownership of the copy of own_key goes to the caller
DYNAMIC_API PEP_STATUS own_key_retrieve(
PEP_SESSION session,
stringlist_t **own_key
);
#ifdef __cplusplus
}


+ 38
- 3
src/pEpEngine.c View File

@ -30,6 +30,11 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
static const char *sql_blacklist_is_listed;
static const char *sql_blacklist_retrieve;
// Own keys
static const char *sql_own_key_add;
static const char *sql_own_key_is_listed;
static const char *sql_own_key_retrieve;
bool in_first = false;
assert(sqlite3_threadsafe());
@ -168,6 +173,12 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
"create table if not exists blacklist_keys (\n"
" fpr text primary key\n"
");\n"
// Own keys
"create table if not exists own_keys (\n"
" fpr text primary key\n"
" references pgp_keypair (fpr)\n"
" on delete cascade,\n"
");\n"
,
NULL,
NULL,
@ -187,7 +198,7 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
sql_log = "insert into log (title, entity, description, comment)"
"values (?1, ?2, ?3, ?4);";
sql_get_identity = "select fpr, username, comm_type, lang"
sql_get_identity = "select fpr, username, comm_type, lang"
" from identity"
" join person on id = identity.user_id"
" join pgp_keypair on fpr = identity.main_key_id"
@ -236,6 +247,16 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
sql_blacklist_is_listed = "select count(*) from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
sql_blacklist_retrieve = "select * from blacklist_keys ;";
// Own keys
sql_own_key_add = "insert or replace into own_keys (fpr) "
" values (upper(replace(?1,' ',''))) ;";
sql_own_key_is_listed = "select count(*) from own_keys where fpr = upper(replace(?1,' ','')) ;";
sql_own_key_retrieve = "select * from own_keys ;";
}
int_result = sqlite3_prepare_v2(_session->db, sql_log, (int)strlen(sql_log),
@ -308,6 +329,20 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
(int)strlen(sql_blacklist_retrieve), &_session->blacklist_retrieve, NULL);
assert(int_result == SQLITE_OK);
// Own keys
int_result = sqlite3_prepare_v2(_session->db, sql_own_key_add,
(int)strlen(sql_own_key_add), &_session->own_key_add, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
(int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_own_key_retrieve,
(int)strlen(sql_own_key_retrieve), &_session->own_key_retrieve, NULL);
assert(int_result == SQLITE_OK);
status = init_cryptotech(_session, in_first);
if (status != PEP_STATUS_OK)
goto pep_error;
@ -807,7 +842,7 @@ DYNAMIC_API PEP_STATUS set_identity(
sqlite3_reset(session->set_trust);
if (result != SQLITE_DONE) {
sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
return PEP_CANNOT_SET_IDENTITY;
return PEP_CANNOT_SET_TRUST;
}
result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
@ -837,7 +872,7 @@ DYNAMIC_API PEP_STATUS mark_as_compromized(
sqlite3_reset(session->mark_compromized);
if (result != SQLITE_DONE)
return PEP_CANNOT_SET_IDENTITY;
return PEP_CANNOT_SET_TRUST;
return PEP_STATUS_OK;
}


+ 6
- 0
src/pEp_internal.h View File

@ -105,7 +105,13 @@ typedef struct _pEpSession {
sqlite3_stmt *blacklist_delete;
sqlite3_stmt *blacklist_is_listed;
sqlite3_stmt *blacklist_retrieve;
// Own keys
sqlite3_stmt *own_key_add;
sqlite3_stmt *own_key_is_listed;
sqlite3_stmt *own_key_retrieve;
examine_identity_t examine_identity;
void *examine_management;


Loading…
Cancel
Save