ENGINE-398: bug fixes

generate_api
Krista Bennett 5 years ago
parent 886f8bb94a
commit 727abbbbd0

@ -1193,6 +1193,10 @@ DYNAMIC_API PEP_STATUS key_mistrusted(
if (!(session && ident && ident->fpr))
return PEP_ILLEGAL_VALUE;
// ident is INPUT ONLY, so we need to preserve the input fpr
char* preserve_fpr = ident->fpr;
ident->fpr = strdup(preserve_fpr);
if (ident->me)
{
revoke_key(session, ident->fpr, NULL);
@ -1226,7 +1230,8 @@ DYNAMIC_API PEP_STATUS key_mistrusted(
if (status == PEP_STATUS_OK)
status = add_mistrusted_key(session, ident->fpr);
}
free(ident->fpr);
ident->fpr = preserve_fpr;
return status;
}

@ -1574,9 +1574,7 @@ PEP_STATUS send_key_reset_to_recents(PEP_SESSION session,
assert(revoke_fpr);
assert(new_fpr);
assert(session);
assert(session->sync_session);
assert(session->sync_session->inject_sync_msg);
assert(session->sync_session->sync_management);
assert(session->messageToSend || session->sync_session->messageToSend);
if (!session || !revoke_fpr || !new_fpr)
return PEP_ILLEGAL_VALUE;
@ -1600,7 +1598,7 @@ PEP_STATUS send_key_reset_to_recents(PEP_SESSION session,
identity_list* curr_id_ptr = recent_contacts;
while (curr_id_ptr) {
for (curr_id_ptr = recent_contacts; curr_id_ptr; curr_id_ptr = curr_id_ptr->next) {
pEp_identity* curr_id = curr_id_ptr->ident;
if (!curr_id)
@ -1642,9 +1640,7 @@ PEP_STATUS send_key_reset_to_recents(PEP_SESSION session,
// Put into notified DB
status = set_reset_contact_notified(session, revoke_fpr, user_id);
if (status != PEP_STATUS_OK)
goto pep_free;
curr_id_ptr = curr_id_ptr->next;
goto pep_free;
}
pep_free:
@ -3956,27 +3952,24 @@ DYNAMIC_API PEP_STATUS key_reset(
PEP_STATUS status = PEP_STATUS_OK;
char* fpr = NULL;
char* fpr_copy = NULL;
char* own_id = NULL;
identity_list* key_idents = NULL;
stringlist_t* keys = NULL;
if (key_id) {
fpr = strdup(key_id);
if (!fpr)
if (!EMPTYSTR(key_id)) {
fpr_copy = strdup(key_id);
if (!fpr_copy)
return PEP_OUT_OF_MEMORY;
}
if (!ident) {
// Get list of own identities
status = get_default_own_userid(session, &own_id);
if (status != PEP_STATUS_OK) {
free(fpr);
return status;
}
if (fpr)
status = get_identities_by_main_key_id(session, fpr, &key_idents);
else {
if (status != PEP_STATUS_OK)
goto pep_free;
if (EMPTYSTR(fpr_copy)) {
status = get_all_keys_for_user(session, own_id, &keys);
if (status == PEP_STATUS_OK) {
stringlist_t* curr_key;
@ -3987,7 +3980,11 @@ DYNAMIC_API PEP_STATUS key_reset(
}
}
goto pep_free;
}
} // otherwise, we have a specific fpr to process
// fpr_copy exists, so... let's go.
// Process own identities with this fpr
status = get_identities_by_main_key_id(session, fpr_copy, &key_idents);
if (status == PEP_STATUS_OK) {
// have ident list, or should
@ -3995,86 +3992,84 @@ DYNAMIC_API PEP_STATUS key_reset(
for (curr_ident = key_idents; curr_ident && curr_ident->ident;
curr_ident = curr_ident->next) {
pEp_identity* this_identity = curr_ident->ident;
status = key_reset(session, fpr, this_identity);
status = key_reset(session, fpr_copy, this_identity);
if (status != PEP_STATUS_OK)
break;
}
}
goto pep_free;
}
else {
else { // an identity was specified.
if (is_me(session, ident)) {
// FIXME: make sure this IS our fpr?
if (!fpr) {
if (ident->fpr)
fpr = strdup(ident->fpr);
// If it got sent in with an empty fpr...
if (EMPTYSTR(fpr_copy)) {
//
// if (!EMPTYSTR(ident->fpr))
// fpr_copy = strdup(ident->fpr);
status = _myself(session, ident, false, true);
if (status == PEP_STATUS_OK && ident->fpr)
fpr_copy = strdup(ident->fpr);
else {
// Note: this will choke if we've already revoked. Is that
// Ok? Or do we need per-identity revokes? Best practice
// is to always send in a damned fpr! ;)
status = _myself(session, ident, false, true);
if (status == PEP_STATUS_OK && ident->fpr) {
fpr = strdup(ident->fpr);
}
else {
// last resort?
// Get list of own identities
char* own_id = NULL;
status = get_default_own_userid(session, &own_id);
if (status == PEP_STATUS_OK)
status = get_user_default_key(session, own_id, &fpr);
if (status != PEP_STATUS_OK || !fpr) {
free(own_id);
return (status == PEP_STATUS_OK ? PEP_KEY_NOT_FOUND : status);
}
// last resort?
// Get list of own identities
char* own_id = NULL;
status = get_default_own_userid(session, &own_id);
if (status == PEP_STATUS_OK)
status = get_user_default_key(session, own_id, &fpr_copy);
if (status != PEP_STATUS_OK || EMPTYSTR(fpr_copy)) {
free(own_id);
return (status == PEP_STATUS_OK ? PEP_KEY_NOT_FOUND : status);
}
}
}
// We now have an fpr. Be careful - it may have been an input fpr,
// and it may be connected to the ident
char* fpr_backup = ident->fpr;
ident->fpr = fpr;
// char* fpr_backup = ident->fpr;
free(ident->fpr);
ident->fpr = fpr_copy;
// Create revocation
status = revoke_key(session, fpr, NULL);
status = revoke_key(session, fpr_copy, NULL);
// mistrust fpr from trust
if (status == PEP_STATUS_OK)
status = key_mistrusted(session, ident);
// Remove fpr from ALL identities
// Remove fpr from ALL users
if (status == PEP_STATUS_OK)
status = remove_fpr_as_default(session, ident->fpr);
status = remove_fpr_as_default(session, fpr_copy);
if (status == PEP_STATUS_OK)
status = add_mistrusted_key(session, ident->fpr);
status = add_mistrusted_key(session, fpr_copy);
// generate new key
if (status == PEP_STATUS_OK)
if (status == PEP_STATUS_OK) {
ident->fpr = NULL;
status = generate_keypair(session, ident);
// add to revocation list
}
// add to revocation list (ident->fpr is now the NEW key)
if (status == PEP_STATUS_OK)
status = set_revoked(session, fpr, ident->fpr, time(NULL));
status = set_revoked(session, fpr_copy, ident->fpr, time(NULL));
// for all active communication partners:
// active_send revocation
if (status == PEP_STATUS_OK)
status = send_key_reset_to_recents(session, fpr, ident->fpr);
status = send_key_reset_to_recents(session, fpr_copy, ident->fpr);
ident->fpr = fpr_backup;
// ident->fpr = fpr_backup;
}
else { // not is_me
// remove fpr from all identities
// remove fpr from all users
if (status == PEP_STATUS_OK)
status = remove_fpr_as_default(session, fpr);
status = remove_fpr_as_default(session, fpr_copy);
// delete key from key ring
if (status == PEP_STATUS_OK)
status = delete_keypair(session, fpr);
status = delete_keypair(session, fpr_copy);
// N.B. If this key is being replaced by something else, it
// is done outside of this function.
}
}
pep_free:
free(fpr);
free(fpr_copy);
free(own_id);
free_identity_list(key_idents);
free_stringlist(keys);

@ -111,7 +111,7 @@ static const char *sql_get_identities_by_userid =
" timestamp desc; ";
static const char *sql_get_identities_by_main_key_id =
"select address, user_id, username, comm_type, lang,"
"select address, identity.user_id, username, comm_type, lang,"
" identity.flags | pgp_keypair.flags,"
" is_own"
" from identity"
@ -119,7 +119,7 @@ static const char *sql_get_identities_by_main_key_id =
" join pgp_keypair on fpr = identity.main_key_id"
" join trust on id = trust.user_id"
" and pgp_keypair_fpr = identity.main_key_id"
" where main_key_id = ?1"
" where identity.main_key_id = ?1"
" order by is_own desc, "
" timestamp desc; ";
@ -3693,6 +3693,15 @@ DYNAMIC_API PEP_STATUS revoke_key(
if (!(session && fpr))
return PEP_ILLEGAL_VALUE;
// Check to see first if it is revoked
bool revoked = false;
PEP_STATUS status = key_revoked(session, fpr, &revoked);
if (status != PEP_STATUS_OK)
return status;
if (revoked)
return PEP_STATUS_OK;
return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
reason);
}

@ -48,7 +48,7 @@ else
LLDB_BIN=lldb
endif
LDLIBS += -lcpptest
LDLIBS+= -lcpptest
# Create a list of the extra library paths for the loader. I do not assume that the engine (and its dependencies) are installed for testing.

@ -6,17 +6,35 @@
#include <string>
#include "EngineTestIndividualSuite.h"
#include "sync.h"
#include "pEpEngine.h"
using namespace std;
class KeyResetMessageTests : public EngineTestIndividualSuite {
public:
KeyResetMessageTests(string test_suite, string test_home_dir);
static PEP_STATUS message_send_callback(void *obj, message *msg);
vector<message*> m_queue;
static constexpr const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
protected:
char* sender_revoked_key_fpr;
char* recip_revoked_key_fpr;
void setup();
private:
void check_key_reset_message();
void check_reset_key_and_notify();
void check_receive_revoked();
void check_receive_key_reset_private();
void check_receive_key_reset_wrong_signer();
void check_receive_key_reset_unsigned();
void check_receive_message_to_revoked_key();
void send_setup();
void receive_setup();
};
#endif

@ -13,6 +13,16 @@ void test_init();
bool file_exists(std::string filename);
PEP_STATUS read_file_and_import_key(PEP_SESSION session, const char* fname);
PEP_STATUS set_up_ident_from_scratch(PEP_SESSION session,
const char* key_fname,
const char* address,
const char* fpr,
const char* user_id,
const char* username,
pEp_identity** ret_ident,
bool is_priv);
// string equality (case and non-case sensitive)
bool _streq(const char* str1, const char* str2);
bool _strceq(const char* str1, const char* str2);

@ -47,6 +47,7 @@
#include "EncryptAttachPrivateKeyTests.h"
#include "DecryptAttachPrivateKeyTrustedTests.h"
#include "IdentityListTests.h"
#include "KeyResetMessageTests.h"
#include "UserIDAliasTests.h"
@ -86,11 +87,12 @@ const char* SuiteMaker::all_suites[] = {
"EncryptAttachPrivateKeyTests",
"DecryptAttachPrivateKeyTrustedTests",
"IdentityListTests",
"KeyResetMessageTests",
"UserIDAliasTests",
};
// This file is generated, so magic constants are ok.
int SuiteMaker::num_suites = 36;
int SuiteMaker::num_suites = 37;
void SuiteMaker::suitemaker_build(const char* test_class_name, const char* test_home, Test::Suite** test_suite) {
if (strcmp(test_class_name, "MapAsn1Tests") == 0)
@ -163,6 +165,8 @@ void SuiteMaker::suitemaker_build(const char* test_class_name, const char* test_
*test_suite = new DecryptAttachPrivateKeyTrustedTests(test_class_name, test_home);
else if (strcmp(test_class_name, "IdentityListTests") == 0)
*test_suite = new IdentityListTests(test_class_name, test_home);
else if (strcmp(test_class_name, "KeyResetMessageTests") == 0)
*test_suite = new KeyResetMessageTests(test_class_name, test_home);
else if (strcmp(test_class_name, "UserIDAliasTests") == 0)
*test_suite = new UserIDAliasTests(test_class_name, test_home);
}

@ -3,9 +3,12 @@
#include <stdlib.h>
#include <string>
#include <assert.h>
#include "pEpEngine.h"
#include "pEp_internal.h"
#include "test_util.h"
#include "EngineTestIndividualSuite.h"
#include "KeyResetMessageTests.h"
@ -15,9 +18,167 @@ KeyResetMessageTests::KeyResetMessageTests(string suitename, string test_home_di
EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_key_reset_message"),
static_cast<Func>(&KeyResetMessageTests::check_key_reset_message)));
add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_reset_key_and_notify"),
static_cast<Func>(&KeyResetMessageTests::check_reset_key_and_notify)));
add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_revoked"),
static_cast<Func>(&KeyResetMessageTests::check_receive_revoked)));
add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_key_reset_private"),
static_cast<Func>(&KeyResetMessageTests::check_receive_key_reset_private)));
add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_key_reset_wrong_signer"),
static_cast<Func>(&KeyResetMessageTests::check_receive_key_reset_wrong_signer)));
add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_key_reset_unsigned"),
static_cast<Func>(&KeyResetMessageTests::check_receive_key_reset_unsigned)));
add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_message_to_revoked_key"),
static_cast<Func>(&KeyResetMessageTests::check_receive_message_to_revoked_key)));
}
PEP_STATUS KeyResetMessageTests::message_send_callback(void* obj, message* msg) {
((KeyResetMessageTests*)obj)->m_queue.push_back(msg);
return PEP_STATUS_OK;
}
void KeyResetMessageTests::setup() {
EngineTestIndividualSuite::setup();
session->sync_obj = this;
session->messageToSend = &KeyResetMessageTests::message_send_callback;
}
void KeyResetMessageTests::send_setup() {
// Setup own identity
PEP_STATUS status = read_file_and_import_key(session,
"test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
assert(status == PEP_STATUS_OK);
status = set_up_ident_from_scratch(session,
"test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc",
"pep.test.alice@pep-project.org", alice_fpr,
PEP_OWN_USERID, "Alice in Wonderland", NULL, true
);
assert(status == PEP_STATUS_OK);
status = set_up_ident_from_scratch(session,
"test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc",
"pep.test.bob@pep-project.org", NULL, "BobId", "Bob's Burgers",
NULL, false
);
assert(status == PEP_STATUS_OK);
status = set_up_ident_from_scratch(session,
"test_keys/pub/pep-test-carol-0x42A85A42_pub.asc",
"pep-test-carol@pep-project.org", NULL, "carolId", "Carol Burnett",
NULL, false
);
assert(status == PEP_STATUS_OK);
status = set_up_ident_from_scratch(session,
"test_keys/pub/pep-test-dave-0xBB5BCCF6_pub.asc",
"pep-test-dave@pep-project.org", NULL, "DaveId",
"David Hasselhoff (Germans Love Me)", NULL, false
);
assert(status == PEP_STATUS_OK);
status = set_up_ident_from_scratch(session,
"test_keys/pub/pep-test-erin-0x9F8D7CBA_pub.asc",
"pep-test-erin@pep-project.org", NULL, "ErinErinErin",
"Éirinn go Brách", NULL, false
);
assert(status == PEP_STATUS_OK);
status = set_up_ident_from_scratch(session,
"test_keys/pub/pep.test.fenris-0x4F3D2900_pub.asc",
"pep.test.fenris@thisstilldoesntwork.lu", NULL, "BadWolf",
"Fenris Leto Hawke", NULL, false
);
assert(status == PEP_STATUS_OK);
}
void KeyResetMessageTests::receive_setup() {
}
void KeyResetMessageTests::check_key_reset_message() {
TEST_ASSERT(true);
}
void KeyResetMessageTests::check_reset_key_and_notify() {
send_setup();
pEp_identity* from_ident = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
PEP_STATUS status = myself(session, from_ident);
TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
TEST_ASSERT_MSG(from_ident->fpr && strcasecmp(from_ident->fpr, alice_fpr) == 0,
from_ident->fpr);
TEST_ASSERT(from_ident->me);
// "send" some messages to update the social graph entries
identity_list* send_idents =
new_identity_list(
new_identity("pep.test.bob@pep-project.org",
NULL, "BobId", "Bob's Burgers"));
identity_list_add(send_idents, new_identity("pep-test-carol@pep-project.org", NULL, NULL, NULL));
identity_list_add(send_idents, new_identity("pep-test-dave@pep-project.org", NULL, NULL, NULL));
identity_list_add(send_idents, new_identity("pep-test-erin@pep-project.org", NULL, NULL, NULL));
identity_list_add(send_idents, new_identity("pep.test.fenris@thisstilldoesntwork.lu", NULL, NULL, NULL));
cout << "Creating outgoing message to update DB" << endl;
message* outgoing_msg = new_message(PEP_dir_outgoing);
TEST_ASSERT(outgoing_msg);
outgoing_msg->from = from_ident;
outgoing_msg->to = send_idents;
outgoing_msg->shortmsg = strdup("Well isn't THIS a useless message...");
outgoing_msg->longmsg = strdup("Hi Mom...\n");
outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
cout << "Message created.\n\n";
cout << "Encrypting message as MIME multipart…\n";
message* enc_outgoing_msg = nullptr;
cout << "Calling encrypt_message()\n";
status = encrypt_message(session, outgoing_msg, NULL, &enc_outgoing_msg, PEP_enc_PGP_MIME, 0);
TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
TEST_ASSERT(enc_outgoing_msg);
cout << "Message encrypted.\n";
// If this all worked, we should have a list of recent guys in our DB which, when we reset Alice's
// key, will get sent some nice key reset messages.
// But... we need to have one look like an older message. So. Time to mess with the DB.
// Dave is our victim. Because friend called Dave, who is actually a nice dude, but it amuses me.
// (Note: said friend is NOT David Hasselhoff. To my knowledge. Hi Dave!)
//
// update identity
// set timestamp = 661008730
// where address = "pep-test-dave@pep-project.org"
int int_result = sqlite3_exec(
session->db,
"update identity "
" set timestamp = 661008730 "
" where address = 'pep-test-dave@pep-project.org' ;",
NULL,
NULL,
NULL
);
TEST_ASSERT(int_result == SQLITE_OK);
status = key_reset(session, alice_fpr, from_ident);
TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
TEST_ASSERT(true);
}
void KeyResetMessageTests::check_receive_revoked() {
TEST_ASSERT(true);
}
void KeyResetMessageTests::check_receive_key_reset_private() {
TEST_ASSERT(true);
}
void KeyResetMessageTests::check_receive_key_reset_wrong_signer() {
TEST_ASSERT(true);
}
void KeyResetMessageTests::check_receive_key_reset_unsigned() {
TEST_ASSERT(true);
}
void KeyResetMessageTests::check_receive_message_to_revoked_key() {
TEST_ASSERT(true);
}

@ -2,6 +2,7 @@
#include "pEpEngine.h"
#include "pEp_internal.h"
#include "message_api.h"
#include "test_util.h"
#include <fstream>
#include <sstream>
@ -13,6 +14,52 @@
#include <unistd.h>
#include <ftw.h>
PEP_STATUS read_file_and_import_key(PEP_SESSION session, const char* fname) {
const std::string key = slurp(fname);
PEP_STATUS status = (key.empty() ? PEP_KEY_NOT_FOUND : PEP_STATUS_OK);
if (status == PEP_STATUS_OK)
status = import_key(session, key.c_str(), key.size(), NULL);
return status;
}
PEP_STATUS set_up_ident_from_scratch(PEP_SESSION session,
const char* key_fname,
const char* address,
const char* fpr,
const char* user_id,
const char* username,
pEp_identity** ret_ident,
bool is_priv) {
PEP_STATUS status = read_file_and_import_key(session,key_fname);
if (status != PEP_STATUS_OK)
return status;
pEp_identity* ident = new_identity(address, fpr, user_id, username);
if (is_priv && fpr) {
status = set_own_key(session, ident, fpr);
if (status == PEP_STATUS_OK)
status = myself(session, ident);
}
else
status = update_identity(session, ident);
if (status != PEP_STATUS_OK)
goto pep_free;
if (!ident || !ident->fpr) {
status = PEP_CANNOT_FIND_IDENTITY;
goto pep_free;
}
if (ret_ident)
*ret_ident = ident;
pep_free:
if (!ret_ident)
free_identity(ident);
}
bool file_exists(std::string filename) {
struct stat buffer;
return (stat(filename.c_str(), &buffer) == 0);

Loading…
Cancel
Save