moving functionality into pEpEngine

async_key_management
vb 9 years ago
parent 4d2ef20101
commit af6c3b757e

@ -17,13 +17,16 @@
#define MIN(A, B) ((B) > (A) ? (A) : (B))
#endif
#ifndef EMPTY
#define EMPTY(STR) ((STR == NULL) || (STR)[0] == 0)
#endif
DYNAMIC_API PEP_STATUS update_identity(
PEP_SESSION session, pEp_identity * identity
)
{
pEp_identity *stored_identity;
PEP_STATUS status;
bool bDirty;
assert(session);
assert(identity);
@ -35,25 +38,105 @@ DYNAMIC_API PEP_STATUS update_identity(
return PEP_OUT_OF_MEMORY;
if (stored_identity) {
if (identity->username == NULL || identity->username[0] == 0) {
if (EMPTY(identity->fpr)) {
identity->comm_type = PEP_ct_unknown;
stringlist_t *keylist;
status = find_keys(session, stored_identity->fpr, &keylist);
assert(status != PEP_OUT_OF_MEMORY);
if (status == PEP_OUT_OF_MEMORY)
return PEP_OUT_OF_MEMORY;
if (keylist && keylist->value) {
identity->comm_type = stored_identity->comm_type;
}
free_stringlist(keylist);
identity->fpr = strdup(stored_identity->fpr);
assert(identity->fpr);
if (identity->fpr == NULL)
return PEP_OUT_OF_MEMORY;
identity->fpr_size = stored_identity->address_size;
}
else /* !EMPTY(identity->fpr) */ {
stringlist_t *keylist;
status = find_keys(session, identity->fpr, &keylist);
assert(status != PEP_OUT_OF_MEMORY);
if (status == PEP_OUT_OF_MEMORY)
return PEP_OUT_OF_MEMORY;
if (keylist && keylist->value) {
if (identity->comm_type == PEP_ct_unknown) {
if (strcmp(identity->fpr, stored_identity->fpr) == 0) {
identity->comm_type = stored_identity->comm_type;
}
else {
status = get_trust(session, identity);
assert(status != PEP_OUT_OF_MEMORY);
if (status == PEP_OUT_OF_MEMORY)
return PEP_OUT_OF_MEMORY;
}
}
}
else
identity->comm_type = PEP_ct_unknown;
free_stringlist(keylist);
}
if (EMPTY(identity->username)) {
free(identity->username);
identity->username = strdup(stored_identity->username);
assert(identity->username);
if (identity->username == NULL)
return PEP_OUT_OF_MEMORY;
identity->username_size = stored_identity->username_size;
}
if (identity->user_id == NULL || identity->user_id[0] == 0) {
if (EMPTY(identity->user_id)) {
free(identity->user_id);
identity->user_id = strdup(stored_identity->user_id);
assert(identity->user_id);
if (identity->user_id == NULL)
return PEP_OUT_OF_MEMORY;
identity->user_id_size = stored_identity->user_id_size;
}
if (identity->fpr != NULL && identity->fpr[0] != 0) {
if (strcmp(identity->fpr, stored_identity->fpr) != 0)
identity->comm_type = PEP_ct_unknown;
if (identity->lang[0] == 0) {
identity->lang[0] = stored_identity->lang[0];
identity->lang[1] = stored_identity->lang[1];
identity->lang[2] = 0;
}
}
else /* stored_identity == NULL */ {
if (identity->fpr && identity->user_id) {
if (identity->comm_type == PEP_ct_unknown) {
status = get_trust(session, identity);
assert(status != PEP_OUT_OF_MEMORY);
if (status == PEP_OUT_OF_MEMORY)
return PEP_OUT_OF_MEMORY;
}
if (identity->comm_type != PEP_ct_unknown && EMPTY(identity->username)) {
free(identity->username);
identity->username = strdup("anonymous");
identity->username_size = 10;
}
}
else
identity->comm_type = PEP_ct_unknown;
}
else
identity->comm_type = PEP_ct_unknown;
status = set_identity(session, identity);
status = PEP_STATUS_OK;
return PEP_STATUS_OK;
if (identity->comm_type != PEP_ct_unknown) {
status = set_identity(session, identity);
assert(status == PEP_STATUS_OK);
}
return status;
}
DYNAMIC_API PEP_STATUS outgoing_comm_type(
@ -62,7 +145,6 @@ DYNAMIC_API PEP_STATUS outgoing_comm_type(
PEP_comm_type *comm_type
)
{
int i;
const stringlist_t *l;
assert(session);
@ -167,6 +249,7 @@ DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
free_stringlist(keylist);
if (identity->fpr == NULL)
return PEP_OUT_OF_MEMORY;
identity->fpr_size = strlen(identity->fpr);
status = set_identity(session, identity);
assert(status == PEP_STATUS_OK);

@ -13,7 +13,7 @@ extern "C" {
// caller must insert the identity into the asynchronous management
// implementation, so retrieve_next_identity() will return this identity
// later
// at least identity->address must be a valid C string as input
// at least identity->address must be a valid UTF-8 string as input
DYNAMIC_API PEP_STATUS update_identity(
PEP_SESSION session, pEp_identity * identity

@ -130,6 +130,7 @@ typedef struct {
sqlite3_stmt *set_pgp_keypair;
sqlite3_stmt *set_identity;
sqlite3_stmt *set_trust;
sqlite3_stmt *get_trust;
gpgme_check_version_t gpgme_check;
gpgme_set_locale_t gpgme_set_locale;
@ -214,6 +215,8 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
const char *sql_set_pgp_keypair;
const char *sql_set_identity;
const char *sql_set_trust;
const char *sql_get_trust;
bool bResult;
assert(sqlite3_threadsafe());
@ -571,6 +574,8 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
sql_set_trust = "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type)"
"values (?1, ?2, ?3) ;";
sql_get_trust = "select user_id, comm_type from trust where user_id = ?1 and pgp_keypair_fpr = ?2 ;";
int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
strlen(sql_set_person), &_session->set_person, NULL);
assert(int_result == SQLITE_OK);
@ -583,6 +588,9 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
strlen(sql_set_trust), &_session->set_trust, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
strlen(sql_get_trust), &_session->get_trust, NULL);
assert(int_result == SQLITE_OK);
sqlite3_reset(_session->log);
sqlite3_bind_text(_session->log, 1, "init", -1, SQLITE_STATIC);
@ -1354,6 +1362,7 @@ DYNAMIC_API PEP_STATUS get_identity(
assert(session);
assert(address);
assert(address[0]);
sqlite3_reset(_session->get_identity);
sqlite3_bind_text(_session->get_identity, 1, address, -1, SQLITE_STATIC);
@ -1853,6 +1862,9 @@ PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
pEpSession *_session = (pEpSession *) session;
gpgme_error_t gpgme_error;
assert(session);
assert(pattern);
gpgme_error = _session->gpgme_op_export(_session->ctx, pattern,
GPGME_EXPORT_MODE_EXTERN, NULL);
assert(gpgme_error != GPG_ERR_INV_VALUE);
@ -1867,3 +1879,46 @@ void pEp_free(void *p)
free(p);
}
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
{
pEpSession *_session = (pEpSession *) session;
PEP_STATUS status = PEP_STATUS_OK;
int result;
assert(session);
assert(identity);
assert(identity->user_id);
assert(identity->user_id[0]);
assert(identity->fpr);
assert(identity->fpr[0]);
identity->comm_type = PEP_ct_unknown;
sqlite3_reset(_session->get_trust);
sqlite3_bind_text(_session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
sqlite3_bind_text(_session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
result = sqlite3_step(_session->get_trust);
switch (result) {
case SQLITE_ROW: {
const char * user_id = (const char *) sqlite3_column_text(_session->get_trust, 1);
int comm_type = (PEP_comm_type) sqlite3_column_int(_session->get_trust, 2);
if (strcmp(user_id, identity->user_id) != 0) {
free(identity->user_id);
identity->user_id = strdup(user_id);
assert(identity->user_id);
if (identity->user_id == NULL)
return PEP_OUT_OF_MEMORY;
}
identity->comm_type = comm_type;
break;
}
default:
status = PEP_CANNOT_FIND_IDENTITY;
}
sqlite3_reset(_session->get_trust);
return status;
}

@ -578,6 +578,21 @@ DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern);
DYNAMIC_API void pEp_free(void *p);
// get_trust() - get the trust level a key has for a person
//
// parameters:
// session (in) session handle
// identity (inout) user_id and fpr to check as UTF-8 strings (in)
// user_id and comm_type as result (out)
//
// this function modifies the given identity struct; the struct remains in the
// ownership of the caller
// if the trust level cannot be determined identity->comm_type is set to PEP_ct_unknown
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity);
#ifdef __cplusplus
}
#endif

@ -111,7 +111,7 @@ static inline string managementPath(const char *file_path, const char *file_name
tPath, PATH_BUF_SIZE);
assert(length);
if (length == 0)
throw bad_alloc();
throw bad_alloc(); // BUG: there are other errors possible beside out of memory
CreateDirectory(tPath, NULL);
DWORD error = GetLastError();

@ -8,10 +8,6 @@
#include "../src/pEpEngine.h"
#include "../src/keymanagement.h"
#ifdef _WIN32
#define strdup _strdup
#endif
using namespace std;
int main(int argc, char* argv[])
@ -165,10 +161,10 @@ int main(int argc, char* argv[])
pEp_identity *identity;
identity = new_identity(
strdup("leon.schumacher@digitalekho.com"),
strdup("8BD08954C74D830EEFFB5DEB2682A17F7C87F73D"),
strdup("23"),
strdup("Leon Schumacher")
"leon.schumacher@digitalekho.com",
"8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
"23",
"Leon Schumacher"
);
identity->comm_type = PEP_ct_pEp;
@ -179,8 +175,15 @@ int main(int argc, char* argv[])
get_identity(session, "leon.schumacher@digitalekho.com", &identity);
assert(identity);
cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
PEP_STATUS get_trust_result = get_trust(session, identity);
assert(get_trust_result == PEP_STATUS_OK);
cout << "trust of " << identity->user_id << " is " << identity->comm_type << "\n";
free_identity(identity);
// testing key management
stringlist_t *addresses = new_stringlist("leon.schumacher@digitalekho.com");
PEP_comm_type comm_type;
cout << "\nretrieving communication type for leon.schumacher@digitalekho.com\n";
@ -200,10 +203,10 @@ int main(int argc, char* argv[])
cout << "\ngenerating key for testuser\n";
identity = new_identity(
strdup("testuser@pibit.ch"),
"testuser@pibit.ch",
NULL,
strdup("423"),
strdup("Alfred E. Neuman")
"423",
"Alfred E. Neuman"
);
assert(identity);
PEP_STATUS generate_status = generate_keypair(session, identity);

Loading…
Cancel
Save