Skip to content

Commit

Permalink
Rename pgp_key_provider_t to rnp::KeyProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 committed Aug 29, 2023
1 parent 4927a9c commit 4255f77
Show file tree
Hide file tree
Showing 18 changed files with 58 additions and 57 deletions.
10 changes: 5 additions & 5 deletions include/rekey/rnp_key_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ class KeyStore {
/**
* @brief Try to load key store from path.
*/
bool load(const pgp_key_provider_t *key_provider = nullptr);
bool load(const KeyProvider *key_provider = nullptr);

/**
* @brief Try to load key store from source.
*/
bool load(pgp_source_t &src, const pgp_key_provider_t *key_provider = nullptr);
bool load(pgp_source_t &src, const KeyProvider *key_provider = nullptr);

/**
* @brief Load all keys from the source, assuming openpgp format.
Expand All @@ -117,12 +117,12 @@ class KeyStore {
/**
* @brief Load keystore in kbx format.
*/
bool load_kbx(pgp_source_t &src, const pgp_key_provider_t *key_provider = nullptr);
bool load_kbx(pgp_source_t &src, const KeyProvider *key_provider = nullptr);

/**
* @brief Load keystore in g10 format.
*/
bool load_g10(pgp_source_t &src, const pgp_key_provider_t *key_provider = nullptr);
bool load_g10(pgp_source_t &src, const KeyProvider *key_provider = nullptr);

/**
* @brief Write keystore to the path.
Expand Down Expand Up @@ -168,7 +168,7 @@ class KeyStore {
* @param prov key provider to request needed key.
* @return pointer to the key or nullptr if signer's key was not found.
*/
pgp_key_t *get_signer(const pgp_signature_t &sig, pgp_key_provider_t *prov = nullptr);
pgp_key_t *get_signer(const pgp_signature_t &sig, const KeyProvider *prov = nullptr);

/**
* @brief Add key to the keystore, copying it.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ffi-priv-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct rnp_ffi_st {
void * getkeycb_ctx;
rnp_password_cb getpasscb;
void * getpasscb_ctx;
pgp_key_provider_t key_provider;
rnp::KeyProvider key_provider;
pgp_password_provider_t pass_provider;
rnp::SecurityContext context;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/generate-key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ load_generated_g10_key(pgp_key_t * dst,
// G10 needs the pubkey for copying some attributes (key version, creation time, etc)
key_ptrs.push_back(pubkey);

rnp::MemorySource memsrc(memdst.memory(), memdst.writeb(), false);
pgp_key_provider_t prov(rnp_key_provider_key_ptr_list, &key_ptrs);
rnp::MemorySource memsrc(memdst.memory(), memdst.writeb(), false);
rnp::KeyProvider prov(rnp_key_provider_key_ptr_list, &key_ptrs);
if (!key_store.get()->load_g10(memsrc.src(), &prov)) {
return false;
}
Expand Down
10 changes: 6 additions & 4 deletions src/lib/key-provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
#include "utils.h"
#include <rekey/rnp_key_store.h>

namespace rnp {
pgp_key_t *
pgp_key_provider_t::request_key(const pgp_key_request_ctx_t &ctx) const
KeyProvider::request_key(const pgp_key_request_ctx_t &ctx) const
{
pgp_key_t *key = nullptr;
if (!callback) {
Expand All @@ -49,6 +50,7 @@ pgp_key_provider_t::request_key(const pgp_key_request_ctx_t &ctx) const
}
return key;
}
} // namespace rnp

pgp_key_t *
rnp_key_provider_key_ptr_list(const pgp_key_request_ctx_t *ctx, void *userdata)
Expand All @@ -65,11 +67,11 @@ rnp_key_provider_key_ptr_list(const pgp_key_request_ctx_t *ctx, void *userdata)
pgp_key_t *
rnp_key_provider_chained(const pgp_key_request_ctx_t *ctx, void *userdata)
{
for (pgp_key_provider_t **pprovider = (pgp_key_provider_t **) userdata;
for (rnp::KeyProvider **pprovider = (rnp::KeyProvider **) userdata;
pprovider && *pprovider;
pprovider++) {
pgp_key_provider_t *provider = *pprovider;
pgp_key_t * key = NULL;
auto provider = *pprovider;
pgp_key_t *key = nullptr;
if ((key = provider->callback(ctx, provider->userdata))) {
return key;
}
Expand Down
13 changes: 8 additions & 5 deletions src/lib/key-provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ typedef struct pgp_key_request_ctx_t {

typedef pgp_key_t *pgp_key_callback_t(const pgp_key_request_ctx_t *ctx, void *userdata);

typedef struct pgp_key_provider_t {
namespace rnp {
class KeyProvider {
public:
pgp_key_callback_t *callback;
void * userdata;

pgp_key_provider_t(pgp_key_callback_t *cb = nullptr, void *ud = nullptr)
KeyProvider(pgp_key_callback_t *cb = nullptr, void *ud = nullptr)
: callback(cb), userdata(ud){};

/** @brief request public or secret pgp key, according to information stored in ctx
Expand All @@ -80,7 +82,8 @@ typedef struct pgp_key_provider_t {
* @return a key pointer on success, or nullptr if key was not found otherwise
**/
pgp_key_t *request_key(const pgp_key_request_ctx_t &ctx) const;
} pgp_key_provider_t;
};
} // namespace rnp

/** key provider callback that searches a list of pgp_key_t pointers
*
Expand All @@ -99,8 +102,8 @@ pgp_key_t *rnp_key_provider_store(const pgp_key_request_ctx_t *ctx, void *userda
/** key provider that calls other key providers
*
* @param ctx
* @param userdata must be an array pgp_key_provider_t pointers,
* ending with a NULL.
* @param userdata must be an array rnp::KeyProvider pointers,
* ending with a nullptr.
*/
pgp_key_t *rnp_key_provider_chained(const pgp_key_request_ctx_t *ctx, void *userdata);

Expand Down
5 changes: 1 addition & 4 deletions src/lib/pgp-key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,7 @@ pgp_subkey_set_expiration(pgp_key_t * sub,
}

pgp_key_t *
find_suitable_key(pgp_op_t op,
pgp_key_t * key,
pgp_key_provider_t *key_provider,
bool no_primary)
find_suitable_key(pgp_op_t op, pgp_key_t *key, rnp::KeyProvider *key_provider, bool no_primary)
{
if (!key || !key_provider) {
return NULL;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/pgp-key.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,10 @@ bool pgp_subkey_set_expiration(pgp_key_t * sub,
* @returns key or last created subkey with desired usage flag
* set or NULL if not found
*/
pgp_key_t *find_suitable_key(pgp_op_t op,
pgp_key_t * key,
pgp_key_provider_t *key_provider,
bool no_primary = false);
pgp_key_t *find_suitable_key(pgp_op_t op,
pgp_key_t * key,
rnp::KeyProvider *key_provider,
bool no_primary = false);

/*
* Picks up hash algorithm according to domain parameters set
Expand Down
13 changes: 6 additions & 7 deletions src/lib/rnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1366,9 +1366,9 @@ FFI_GUARD
static rnp_result_t
load_keys_from_input(rnp_ffi_t ffi, rnp_input_t input, rnp::KeyStore *store)
{
pgp_key_provider_t chained(rnp_key_provider_store, store);
const pgp_key_provider_t *key_providers[] = {&chained, &ffi->key_provider, NULL};
const pgp_key_provider_t key_provider(rnp_key_provider_chained, key_providers);
rnp::KeyProvider chained(rnp_key_provider_store, store);
const rnp::KeyProvider *key_providers[] = {&chained, &ffi->key_provider, NULL};
const rnp::KeyProvider key_provider(rnp_key_provider_chained, key_providers);

if (!input->src_directory.empty()) {
// load the keys
Expand Down Expand Up @@ -2723,10 +2723,9 @@ static pgp_write_handler_t
pgp_write_handler(pgp_password_provider_t *pass_provider,
rnp_ctx_t * rnpctx,
void * param,
pgp_key_provider_t * key_provider)
rnp::KeyProvider * key_provider)
{
pgp_write_handler_t handler;
memset(&handler, 0, sizeof(handler));
pgp_write_handler_t handler{};
handler.password_provider = pass_provider;
handler.ctx = rnpctx;
handler.param = param;
Expand Down Expand Up @@ -3276,7 +3275,7 @@ try {
handler.password_provider = &op->ffi->pass_provider;

rnp_decryption_kp_param_t kparam(op);
pgp_key_provider_t kprov = {ffi_decrypt_key_provider, &kparam};
rnp::KeyProvider kprov(ffi_decrypt_key_provider, &kparam);

handler.key_provider = &kprov;
handler.on_signatures = rnp_op_verify_on_signatures;
Expand Down
4 changes: 2 additions & 2 deletions src/librekey/key_store_g10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,11 +895,11 @@ copy_secret_fields(pgp_key_pkt_t &dst, const pgp_key_pkt_t &src)

namespace rnp {
bool
KeyStore::load_g10(pgp_source_t &src, const pgp_key_provider_t *key_provider)
KeyStore::load_g10(pgp_source_t &src, const KeyProvider *key_provider)
{
try {
/* read src to the memory */
rnp::MemorySource memsrc(src);
MemorySource memsrc(src);
/* parse secret key: fills material and sec_protection only */
pgp_key_pkt_t seckey;
if (!g23_parse_seckey(seckey, (uint8_t *) memsrc.memory(), memsrc.size(), NULL)) {
Expand Down
14 changes: 7 additions & 7 deletions src/librekey/key_store_kbx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,12 @@ kbx_parse_blob(const uint8_t *image, size_t image_len)
} // namespace

bool
KeyStore::load_kbx(pgp_source_t &src, const pgp_key_provider_t *key_provider)
KeyStore::load_kbx(pgp_source_t &src, const KeyProvider *key_provider)
{
try {
rnp::MemorySource mem(src);
size_t has_bytes = mem.size();
uint8_t * buf = (uint8_t *) mem.memory();
MemorySource mem(src);
size_t has_bytes = mem.size();
uint8_t * buf = (uint8_t *) mem.memory();

if (has_bytes < BLOB_FIRST_SIZE) {
RNP_LOG("Too few bytes for valid KBX");
Expand Down Expand Up @@ -425,9 +425,9 @@ KeyStore::load_kbx(pgp_source_t &src, const pgp_key_provider_t *key_provider)
return false;
}

rnp::MemorySource blsrc(pgp_blob.image().data() + pgp_blob.keyblock_offset(),
pgp_blob.keyblock_length(),
false);
MemorySource blsrc(pgp_blob.image().data() + pgp_blob.keyblock_offset(),
pgp_blob.keyblock_length(),
false);
if (load_pgp(blsrc.src())) {
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/librekey/rnp_key_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

namespace rnp {
bool
KeyStore::load(const pgp_key_provider_t *key_provider)
KeyStore::load(const KeyProvider *key_provider)
{
pgp_source_t src = {};

Expand Down Expand Up @@ -101,7 +101,7 @@ KeyStore::load(const pgp_key_provider_t *key_provider)
}

bool
KeyStore::load(pgp_source_t &src, const pgp_key_provider_t *key_provider)
KeyStore::load(pgp_source_t &src, const KeyProvider *key_provider)
{
switch (format) {
case PGP_KEY_STORE_GPG:
Expand Down Expand Up @@ -622,7 +622,7 @@ KeyStore::search(const pgp_key_search_t &search, pgp_key_t *after)
}

pgp_key_t *
KeyStore::get_signer(const pgp_signature_t &sig, pgp_key_provider_t *prov)
KeyStore::get_signer(const pgp_signature_t &sig, const KeyProvider *prov)
{
pgp_key_request_ctx_t ctx(PGP_OP_VERIFY, false, PGP_KEY_SEARCH_UNKNOWN);
/* if we have fingerprint let's check it */
Expand Down
6 changes: 3 additions & 3 deletions src/librepgp/stream-parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ typedef void pgp_decryption_done_func_t(bool validated, void *param);
/* handler used to return needed information during pgp source processing */
typedef struct pgp_parse_handler_t {
pgp_password_provider_t *password_provider; /* if NULL then default will be used */
pgp_key_provider_t * key_provider; /* must be set when key is required, i.e. during
signing/verification/public key encryption and
deryption */
rnp::KeyProvider * key_provider; /* must be set when key is required, i.e. during
signing/verification/public key encryption and
deryption */
pgp_destination_func_t *dest_provider; /* called when destination stream is required */
pgp_source_func_t * src_provider; /* required to provider source during the detached
signature verification */
Expand Down
8 changes: 4 additions & 4 deletions src/librepgp/stream-write.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
#include "stream-ctx.h"

typedef struct pgp_write_handler_t {
pgp_password_provider_t *password_provider;
pgp_key_provider_t * key_provider;
rnp_ctx_t * ctx;
pgp_password_provider_t *password_provider{};
rnp::KeyProvider * key_provider{};
rnp_ctx_t * ctx{};

void *param;
void *param{};
} pgp_write_handler_t;

/** @brief sign the input data, producing attached, detached or cleartext signature.
Expand Down
2 changes: 1 addition & 1 deletion src/tests/key-grip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ TEST_F(rnp_tests, key_grip)

auto sec_store = new rnp::KeyStore(
PGP_KEY_STORE_G10, "data/test_stream_key_load/g10/private-keys-v1.d", global_ctx);
pgp_key_provider_t key_provider(rnp_key_provider_store, pub_store);
rnp::KeyProvider key_provider(rnp_key_provider_store, pub_store);
assert_true(sec_store->load(&key_provider));

const pgp_key_t *key = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/key-store-search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ TEST_F(rnp_tests, test_key_store_search_by_name)
// load secring
auto sec_store =
new rnp::KeyStore(PGP_KEY_STORE_G10, "data/keyrings/3/private-keys-v1.d", global_ctx);
pgp_key_provider_t key_provider(rnp_key_provider_store, pub_store);
rnp::KeyProvider key_provider(rnp_key_provider_store, pub_store);
assert_true(sec_store->load(&key_provider));

/* Main key fingerprint and id:
Expand Down
2 changes: 1 addition & 1 deletion src/tests/key-validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ TEST_F(rnp_tests, test_key_validate)

secring =
new rnp::KeyStore(PGP_KEY_STORE_G10, "data/keyrings/3/private-keys-v1.d", global_ctx);
pgp_key_provider_t key_provider(rnp_key_provider_store, pubring);
rnp::KeyProvider key_provider(rnp_key_provider_store, pubring);
assert_true(secring->load(&key_provider));
assert_true(all_keys_valid(secring));
delete pubring;
Expand Down
4 changes: 2 additions & 2 deletions src/tests/load-g10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

TEST_F(rnp_tests, test_invalid_g10)
{
pgp_key_provider_t key_provider(rnp_key_provider_store);
rnp::KeyProvider key_provider(rnp_key_provider_store);
// load pubring
auto pub_store =
new rnp::KeyStore(PGP_KEY_STORE_KBX, "data/keyrings/3/pubring.kbx", global_ctx);
Expand All @@ -53,7 +53,7 @@ TEST_F(rnp_tests, test_invalid_g10)
*/
TEST_F(rnp_tests, test_load_g10)
{
pgp_key_provider_t key_provider(rnp_key_provider_store);
rnp::KeyProvider key_provider(rnp_key_provider_store);

// load pubring
auto pub_store =
Expand Down
2 changes: 1 addition & 1 deletion src/tests/load-g23.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
TEST_F(rnp_tests, test_load_g23)
{
pgp_key_provider_t key_provider(rnp_key_provider_store);
rnp::KeyProvider key_provider(rnp_key_provider_store);

/* another store */
auto pub_store = new rnp::KeyStore(
Expand Down

0 comments on commit 4255f77

Please sign in to comment.