Browse Source

ENGINE-928: Ported set_comm_partner_key() from Release_2.1

doxygen_doc
Krista Bennett 4 months ago
parent
commit
c0d9704475
9 changed files with 317 additions and 12 deletions
  1. +7
    -0
      src/engine_sql.c
  2. +4
    -0
      src/engine_sql.h
  3. +19
    -0
      src/keymanagement.c
  4. +29
    -0
      src/keymanagement.h
  5. +32
    -0
      src/pEpEngine.c
  6. +0
    -12
      src/pEpEngine.h
  7. +30
    -0
      src/pEpEngine_internal.h
  8. +1
    -0
      src/pEp_internal.h
  9. +195
    -0
      test/src/TestSetDefaultFPRTest.cc

+ 7
- 0
src/engine_sql.c View File

@ -1736,6 +1736,13 @@ PEP_STATUS pEp_prepare_sql_stmts(PEP_SESSION session) {
if (int_result != SQLITE_OK)
return PEP_UNKNOWN_DB_ERROR;
int_result = sqlite3_prepare_v2(session->db, sql_set_default_identity_fpr,
(int)strlen(sql_set_default_identity_fpr), &session->set_default_identity_fpr, NULL);
assert(int_result == SQLITE_OK);
if (int_result != SQLITE_OK)
return PEP_UNKNOWN_DB_ERROR;
int_result = sqlite3_prepare_v2(session->db, sql_get_default_identity_fpr,
(int)strlen(sql_get_default_identity_fpr), &session->get_default_identity_fpr, NULL);
assert(int_result == SQLITE_OK);


+ 4
- 0
src/engine_sql.h View File

@ -113,6 +113,10 @@ static const char *sql_replace_identities_fpr =
" set main_key_id = ?1 "
" where main_key_id = ?2 ;";
static const char* sql_set_default_identity_fpr =
"update identity set main_key_id = ?3 "
" where user_id = ?1 and address = ?2; ";
static const char *sql_get_default_identity_fpr =
"select main_key_id from identity"
" where (case when (address = ?1) then (1)"


+ 19
- 0
src/keymanagement.c View File

@ -2087,6 +2087,25 @@ PEP_STATUS get_key_sticky_bit_for_user(PEP_SESSION session,
return status;
}
DYNAMIC_API PEP_STATUS set_comm_partner_key(PEP_SESSION session,
pEp_identity *identity,
const char* fpr) {
if (!session || !identity || EMPTYSTR(fpr))
return PEP_ILLEGAL_VALUE;
// update identity upfront - we need the identity to exist in the DB.
PEP_STATUS status = update_identity(session, identity);
if (status != PEP_OUT_OF_MEMORY) {
if (identity->me)
return PEP_ILLEGAL_VALUE;
status = set_default_identity_fpr(session,
identity->user_id,
identity->address,
fpr);
}
return status;
}
// Returns PASSPHRASE errors when necessary
DYNAMIC_API PEP_STATUS set_own_key(
PEP_SESSION session,


+ 29
- 0
src/keymanagement.h View File

@ -442,6 +442,34 @@ DYNAMIC_API PEP_STATUS own_keys_retrieve(
stringlist_t **keylist
);
/**
* <!-- set_comm_partner_key() -->
*
* @brief Mark a key the default for a comm partner
*
* @param[in] session session to use
* @param[in,out] identity partner identity this key is used for
* @param[in] fpr fingerprint of the key to set as the identity default
*
* @retval PEP_STATUS_OK
* @retval PEP_KEY_UNSUITABLE
* @retval PEP_ILLEGAL_VALUE illegal parameter values, including if update_identity determines this is an own identity
* @retval PEP_OUT_OF_MEMORY out of memory
* @retval any other value on error
*
* @warning the key has to be in the key ring already
* identity->address must be set to valid data
* update_identity() is called by this function and will create a TOFU user_id + new entry if none is indicated
* and heuristic fails to match extant identity
* identity->fpr will NOT be updated with the set identity fpr; it is only in,out because update_identity() is called
* before setting it.
*
*/
DYNAMIC_API PEP_STATUS set_comm_partner_key(PEP_SESSION session,
pEp_identity *identity,
const char* fpr);
/**
* <!-- set_own_key() -->
*
@ -475,6 +503,7 @@ DYNAMIC_API PEP_STATUS set_own_key(
const char *fpr
);
/**
* <!-- set_own_imported_key() -->
*


+ 32
- 0
src/pEpEngine.c View File

@ -2531,6 +2531,38 @@ PEP_STATUS get_main_user_fpr(PEP_SESSION session,
return status;
}
PEP_STATUS set_default_identity_fpr(PEP_SESSION session,
const char* user_id,
const char* address,
const char* fpr) {
if (!session || EMPTYSTR(user_id) || EMPTYSTR(address) || EMPTYSTR(fpr))
return PEP_ILLEGAL_VALUE;
// Make sure fpr is in the management DB
PEP_STATUS status = set_pgp_keypair(session, fpr);
if (status != PEP_STATUS_OK)
return status;
int result;
sqlite3_reset(session->set_default_identity_fpr);
sqlite3_bind_text(session->set_default_identity_fpr, 1, user_id, -1,
SQLITE_STATIC);
sqlite3_bind_text(session->set_default_identity_fpr, 2, address, -1,
SQLITE_STATIC);
sqlite3_bind_text(session->set_default_identity_fpr, 3, fpr, -1,
SQLITE_STATIC);
result = sqlite3_step(session->set_default_identity_fpr);
sqlite3_reset(session->set_default_identity_fpr);
if (result != SQLITE_DONE)
return PEP_CANNOT_SET_PGP_KEYPAIR;
return PEP_STATUS_OK;
}
PEP_STATUS get_default_identity_fpr(PEP_SESSION session,
const char* address,
const char* user_id,


+ 0
- 12
src/pEpEngine.h View File

@ -1838,18 +1838,6 @@ DYNAMIC_API PEP_STATUS set_ident_enc_format(PEP_SESSION session,
pEp_identity *identity,
PEP_enc_format format);
/**
*
* @param session
* @param address
* @param user_id
* @param main_fpr
* @return
*/
PEP_STATUS get_default_identity_fpr(PEP_SESSION session,
const char* address,
const char* user_id,
char** main_fpr);
/**
* <!-- reset_pEptest_hack() -->
*


+ 30
- 0
src/pEpEngine_internal.h View File

@ -612,6 +612,36 @@ PEP_STATUS get_identities_by_address(
identity_list** id_list
);
/**
*
* @param session
* @param address
* @param user_id
* @param main_fpr
* @return
*/
PEP_STATUS get_default_identity_fpr(PEP_SESSION session,
const char* address,
const char* user_id,
char** main_fpr);
/**
* <!-- set_default_identity_fpr() -->
* Set the default key fingerprint for the identity identitified by this address and user_id. Will only
* succeed if identity is already in DB.
*
* @param[in] session session handle
* @param[in] user_id user_id for identity - cannot be NULL
* @param[in] address address for identity - cannot be NULL
* @param[in] fpr fingerprint for identity - cannot be NULL
*
* @retval PEP_STATUS_OK if key was set or identity doesn't exists
* @retval error otherwise
*/
PEP_STATUS set_default_identity_fpr(PEP_SESSION session,
const char* user_id,
const char* address,
const char* fpr);
/**
* <!-- sign_only() -->


+ 1
- 0
src/pEp_internal.h View File

@ -192,6 +192,7 @@ struct _pEpSession {
sqlite3_stmt *replace_main_user_fpr;
sqlite3_stmt *replace_main_user_fpr_if_equal;
sqlite3_stmt *get_main_user_fpr;
sqlite3_stmt *set_default_identity_fpr;
sqlite3_stmt *get_default_identity_fpr;
sqlite3_stmt *refresh_userid_default_key;
sqlite3_stmt *delete_key;


+ 195
- 0
test/src/TestSetDefaultFPRTest.cc View File

@ -0,0 +1,195 @@
#include <stdlib.h>
#include <string>
#include <cstring>
#include "pEpEngine.h"
#include "pEpEngine_internal.h"
#include "test_util.h"
#include "TestConstants.h"
#include "Engine.h"
#include <gtest/gtest.h>
namespace {
//The fixture for TestSetDefaultFPRTest
class TestSetDefaultFPRTest : public ::testing::Test {
public:
Engine* engine;
PEP_SESSION session;
const char* carol_fpr = "8DD4F5827B45839E9ACCA94687BDDFFB42A85A42";
const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
protected:
// You can remove any or all of the following functions if its body
// is empty.
TestSetDefaultFPRTest() {
// You can do set-up work for each test here.
test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->GTEST_SUITE_SYM();
test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
}
~TestSetDefaultFPRTest() override {
// You can do clean-up work that doesn't throw exceptions here.
}
// If the constructor and destructor are not enough for setting up
// and cleaning up each test, you can define the following methods:
void SetUp() override {
// Code here will be called immediately after the constructor (right
// before each test).
// Leave this empty if there are no files to copy to the home directory path
std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
// Get a new test Engine.
engine = new Engine(test_path);
ASSERT_NE(engine, nullptr);
// Ok, let's initialize test directories etc.
engine->prep(NULL, NULL, NULL, init_files);
// Ok, try to start this bugger.
engine->start();
ASSERT_NE(engine->session, nullptr);
session = engine->session;
// Engine is up. Keep on truckin'
slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc");
slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
}
void TearDown() override {
// Code here will be called immediately after each test (right
// before the destructor).
engine->shut_down();
delete engine;
engine = NULL;
session = NULL;
}
private:
const char* test_suite_name;
const char* test_name;
string test_path;
};
} // namespace
TEST_F(TestSetDefaultFPRTest, check_test_set_default_fpr) {
pEp_identity* carol = NULL;
PEP_STATUS status = set_up_preset(session, CAROL,
true, true, false, false, false, &carol);
status = update_identity(session, carol);
ASSERT_OK;
ASSERT_EQ(carol->fpr, nullptr);
status = set_default_identity_fpr(session, carol->user_id, carol->address, carol_fpr);
ASSERT_OK;
status = update_identity(session, carol);
ASSERT_OK;
ASSERT_STREQ(carol->fpr, carol_fpr);
status = set_default_identity_fpr(session, carol->user_id, carol->address, bob_fpr);
ASSERT_OK;
status = update_identity(session, carol);
ASSERT_OK;
ASSERT_STREQ(carol->fpr, bob_fpr);
// Now let's set one that we don't have.
status = set_default_identity_fpr(session, carol->user_id, carol->address, alice_fpr);
ASSERT_OK;
status = update_identity(session, carol);
ASSERT_OK;
ASSERT_STREQ(carol->fpr, carol_fpr); // Should be the user default, we don't have alice_fpr's key
ASSERT_EQ(carol->comm_type, PEP_ct_pEp_unconfirmed);
free_identity(carol);
}
TEST_F(TestSetDefaultFPRTest, check_test_set_comm_partner_key) {
pEp_identity* carol = NULL;
PEP_STATUS status = set_up_preset(session, CAROL,
true, true, false, false, false, &carol);
status = set_comm_partner_key(session, carol, carol_fpr);
ASSERT_OK;
status = update_identity(session, carol);
ASSERT_OK;
ASSERT_STREQ(carol->fpr, carol_fpr);
status = set_comm_partner_key(session, carol, bob_fpr);
ASSERT_OK;
status = update_identity(session, carol);
ASSERT_OK;
ASSERT_STREQ(carol->fpr, bob_fpr);
// Now let's set one that we don't have.
status = set_comm_partner_key(session, carol, alice_fpr);
ASSERT_OK;
status = update_identity(session, carol);
ASSERT_OK;
ASSERT_STREQ(carol->fpr, carol_fpr); // Should be the user default, we don't have alice_fpr's key
ASSERT_EQ(carol->comm_type, PEP_ct_pEp_unconfirmed);
free_identity(carol);
}
TEST_F(TestSetDefaultFPRTest, check_test_set_default_no_identity) {
pEp_identity* carol = NULL;
PEP_STATUS status = set_up_preset(session, CAROL,
true, true, false, false, false, &carol);
status = update_identity(session, carol);
ASSERT_OK;
ASSERT_EQ(carol->fpr, nullptr);
status = set_default_identity_fpr(session, carol->user_id, carol->address, carol_fpr);
ASSERT_OK;
status = update_identity(session, carol);
ASSERT_OK;
ASSERT_STREQ(carol->fpr, carol_fpr);
pEp_identity* carol_bob = new_identity(carol->address, NULL, "BOB", "Carol is Bob, but not really");
status = set_default_identity_fpr(session, carol_bob->user_id, carol_bob->address, carol_fpr);
ASSERT_OK;
status = update_identity(session, carol_bob);
ASSERT_OK;
ASSERT_EQ(carol_bob->fpr, nullptr);
status = set_default_identity_fpr(session, carol_bob->user_id, carol_bob->address, carol_fpr);
ASSERT_OK;
status = update_identity(session, carol_bob);
ASSERT_OK;
ASSERT_STREQ(carol_bob->fpr, carol_fpr);
free_identity(carol);
free_identity(carol_bob);
}
TEST_F(TestSetDefaultFPRTest, check_test_set_comm_partner_key_no_set_identity) {
pEp_identity* carol = NULL;
PEP_STATUS status = set_up_preset(session, CAROL,
false, false, false, false, false, &carol);
string user_id_cache = carol->user_id;
status = set_comm_partner_key(session, carol, carol_fpr);
ASSERT_OK;
status = update_identity(session, carol);
ASSERT_OK;
ASSERT_STREQ(carol->fpr, carol_fpr);
ASSERT_STREQ(user_id_cache.c_str(), carol->user_id);
status = set_comm_partner_key(session, carol, bob_fpr);
ASSERT_OK;
status = update_identity(session, carol);
ASSERT_OK;
ASSERT_STREQ(carol->fpr, bob_fpr);
// Now let's set one that we don't have.
status = set_comm_partner_key(session, carol, alice_fpr);
ASSERT_OK;
status = update_identity(session, carol);
ASSERT_OK;
ASSERT_STREQ(carol->fpr, carol_fpr); // Should be the user default, we don't have alice_fpr's key
ASSERT_EQ(carol->comm_type, PEP_ct_OpenPGP_unconfirmed);
pEp_identity* carol_bob = new_identity(carol->address, NULL, "BOB", "Carol is Bob, but not really");
status = set_comm_partner_key(session, carol_bob, carol_fpr);
ASSERT_OK;
status = update_identity(session, carol_bob);
ASSERT_OK;
ASSERT_STREQ(carol_bob->fpr, carol_fpr); // differs from above case because of internal update_identity call
free_identity(carol);
free_identity(carol_bob);
}

Loading…
Cancel
Save