Skip to content

Commit

Permalink
Rename pgp_key_t to rnp::Key and move code to key.hpp/key.cpp.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 committed Feb 27, 2025
1 parent 3046d87 commit 73a59e5
Show file tree
Hide file tree
Showing 52 changed files with 760 additions and 767 deletions.
41 changes: 19 additions & 22 deletions include/rekey/rnp_key_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <stdbool.h>
#include "rnp.h"
#include "librepgp/stream-common.h"
#include "pgp-key.h"
#include "key.hpp"
#include <string>
#include <list>
#include <map>
Expand All @@ -55,15 +55,14 @@ typedef enum pgp_sig_import_status_t {
PGP_SIG_IMPORT_STATUS_NEW
} pgp_sig_import_status_t;

typedef std::unordered_map<pgp_fingerprint_t, std::list<pgp_key_t>::iterator> pgp_key_fp_map_t;
typedef std::unordered_map<pgp_fingerprint_t, std::list<rnp::Key>::iterator> pgp_key_fp_map_t;

namespace rnp {
class KeyStore {
private:
pgp_key_t * add_subkey(pgp_key_t &srckey, pgp_key_t *oldkey);
pgp_sig_import_status_t import_subkey_signature(pgp_key_t & key,
const pgp_signature_t &sig);
bool refresh_subkey_grips(pgp_key_t &key);
Key * add_subkey(Key &srckey, Key *oldkey);
pgp_sig_import_status_t import_subkey_signature(Key &key, const pgp_signature_t &sig);
bool refresh_subkey_grips(Key &key);

public:
std::string path;
Expand All @@ -72,7 +71,7 @@ class KeyStore {
bool disable_validation =
false; /* do not automatically validate keys, added to this key store */

std::list<pgp_key_t> keys;
std::list<Key> keys;
pgp_key_fp_map_t keybyfp;
std::vector<std::unique_ptr<kbx_blob_t>> blobs;

Expand Down Expand Up @@ -150,8 +149,8 @@ class KeyStore {

size_t key_count() const;

pgp_key_t * get_key(const pgp_fingerprint_t &fpr);
const pgp_key_t *get_key(const pgp_fingerprint_t &fpr) const;
Key * get_key(const pgp_fingerprint_t &fpr);
const Key *get_key(const pgp_fingerprint_t &fpr) const;

/**
* @brief Get the key's subkey by its index
Expand All @@ -160,7 +159,7 @@ class KeyStore {
* @param idx index of the subkey
* @return pointer to the subkey or nullptr if subkey was found
*/
pgp_key_t *get_subkey(const pgp_key_t &key, size_t idx);
Key *get_subkey(const Key &key, size_t idx);

/**
* @brief Get the signer's key for signature
Expand All @@ -169,13 +168,13 @@ 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, const KeyProvider *prov = nullptr);
Key *get_signer(const pgp_signature_t &sig, const KeyProvider *prov = nullptr);

/**
* @brief Add key to the keystore, copying it.
* @return pointer to the added key or nullptr if failed.
*/
pgp_key_t *add_key(pgp_key_t &key);
Key *add_key(Key &key);

/**
* @brief Add signature of the specific key to the keystore, revalidating and refreshing
Expand Down Expand Up @@ -208,24 +207,22 @@ class KeyStore {
* @param tskey parsed subkey.
* @param pkey primary key, may be nullptr.
*/
bool add_ts_subkey(const pgp_transferable_subkey_t &tskey, pgp_key_t *pkey = nullptr);
bool add_ts_subkey(const pgp_transferable_subkey_t &tskey, Key *pkey = nullptr);

/**
* @brief Import key to the keystore.
*
* @param srckey source key.
* @param pubkey import just public key part.
* @param status if not nullptr then import status will be stored here.
* @return pgp_key_t*
* @return Key*
*/
pgp_key_t *import_key(pgp_key_t & srckey,
bool pubkey,
pgp_key_import_status_t *status = nullptr);
Key *import_key(Key &srckey, bool pubkey, pgp_key_import_status_t *status = nullptr);

/**
* @brief Import signature for the specified key.
*/
pgp_sig_import_status_t import_signature(pgp_key_t &key, const pgp_signature_t &sig);
pgp_sig_import_status_t import_signature(Key &key, const pgp_signature_t &sig);

/**
* @brief Import revocation or direct-key signature to the keystore.
Expand All @@ -235,7 +232,7 @@ class KeyStore {
* @return pointer to the key to which this signature belongs (or nullptr if key was not
* found)
*/
pgp_key_t *import_signature(const pgp_signature_t &sig, pgp_sig_import_status_t *status);
Key *import_signature(const pgp_signature_t &sig, pgp_sig_import_status_t *status);

/**
* @brief Remove key from the keystore.
Expand All @@ -244,14 +241,14 @@ class KeyStore {
* @param subkeys remove subkeys or not.
* @return true if key was successfully removed, or false if key was not found in keystore.
*/
bool remove_key(const pgp_key_t &key, bool subkeys = false);
bool remove_key(const Key &key, bool subkeys = false);

/**
* @brief Get primary key for the subkey, if any.
*/
pgp_key_t *primary_key(const pgp_key_t &subkey);
Key *primary_key(const Key &subkey);

pgp_key_t *search(const KeySearch &search, pgp_key_t *after = nullptr);
Key *search(const KeySearch &search, Key *after = nullptr);
};
} // namespace rnp

Expand Down
2 changes: 1 addition & 1 deletion src/fuzzing/keyring_g10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/

#include <rnp/rnp.h>
#include "../lib/pgp-key.h"
#include "../lib/key.hpp"
#include "../librekey/key_store_g10.h"
#include "../librepgp/stream-common.h"
#include "../include/rekey/rnp_key_store.h"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ add_library(librnp-obj OBJECT
signature.cpp
userid.cpp
keygen.cpp
pgp-key.cpp
key.cpp
rnp.cpp
)

Expand Down
2 changes: 1 addition & 1 deletion src/lib/crypto/kmac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <repgp/repgp_def.h>
#include "types.h"
#include "config.h"
#include "pgp-key.h"
#include "key.hpp"

namespace rnp {
class KMAC256 {
Expand Down
46 changes: 23 additions & 23 deletions src/lib/ffi-priv-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,29 @@
#include "keygen.hpp"

struct rnp_key_handle_st {
rnp_ffi_t ffi;
pgp_key_t *pub;
pgp_key_t *sec;
rnp_ffi_t ffi;
rnp::Key *pub;
rnp::Key *sec;

rnp_key_handle_st(rnp_ffi_t affi, pgp_key_t *apub = nullptr, pgp_key_t *asec = nullptr)
rnp_key_handle_st(rnp_ffi_t affi, rnp::Key *apub = nullptr, rnp::Key *asec = nullptr)
: ffi(affi), pub(apub), sec(asec)
{
}
};

struct rnp_uid_handle_st {
rnp_ffi_t ffi;
pgp_key_t *key;
size_t idx;
rnp_ffi_t ffi;
rnp::Key *key;
size_t idx;
};

struct rnp_signature_handle_st {
rnp_ffi_t ffi;
/**
* @brief Key to which this signature belongs, if available.
*/
const pgp_key_t *key;
rnp::Signature * sig;
const rnp::Key *key;
rnp::Signature *sig;
/**
* @brief sig pointer is owned by structure and should be deallocated.
*/
Expand All @@ -67,11 +67,11 @@ struct rnp_signature_handle_st {
*/
bool new_sig;

rnp_signature_handle_st(rnp_ffi_t affi,
const pgp_key_t *akey = nullptr,
rnp::Signature * asig = nullptr,
bool aown_sig = false,
bool anew_sig = false)
rnp_signature_handle_st(rnp_ffi_t affi,
const rnp::Key *akey = nullptr,
rnp::Signature *asig = nullptr,
bool aown_sig = false,
bool anew_sig = false)
: ffi(affi), key(akey), sig(asig), own_sig(aown_sig), new_sig(anew_sig)
{
}
Expand Down Expand Up @@ -155,12 +155,12 @@ struct rnp_output_st {
};

struct rnp_op_generate_st {
rnp_ffi_t ffi;
bool primary{};
pgp_key_t *primary_sec{};
pgp_key_t *primary_pub{};
pgp_key_t *gen_sec{};
pgp_key_t *gen_pub{};
rnp_ffi_t ffi;
bool primary{};
rnp::Key *primary_sec{};
rnp::Key *primary_pub{};
rnp::Key *gen_sec{};
rnp::Key *gen_pub{};
/* password used to encrypt the key, if specified */
rnp::secure_vector<char> password;
/* request password for key encryption via ffi's password provider */
Expand Down Expand Up @@ -286,7 +286,7 @@ struct rnp_identifier_iterator_st {
rnp_ffi_t ffi;
rnp::KeySearch::Type type;
rnp::KeyStore * store;
std::list<pgp_key_t>::iterator *keyp;
std::list<rnp::Key>::iterator * keyp;
size_t uididx;
std::unordered_set<std::string> tbl;
std::string item;
Expand All @@ -295,7 +295,7 @@ struct rnp_identifier_iterator_st {
: ffi(affi), type(atype)
{
store = nullptr;
keyp = new std::list<pgp_key_t>::iterator();
keyp = new std::list<rnp::Key>::iterator();
uididx = 0;
}

Expand All @@ -308,7 +308,7 @@ struct rnp_identifier_iterator_st {
struct rnp_decryption_kp_param_t {
rnp_op_verify_t op;
bool has_hidden; /* key provider had hidden keyid request */
pgp_key_t * last; /* last key, returned in hidden keyid request */
rnp::Key * last; /* last key, returned in hidden keyid request */

rnp_decryption_kp_param_t(rnp_op_verify_t opobj)
: op(opobj), has_hidden(false), last(NULL){};
Expand Down
28 changes: 14 additions & 14 deletions src/lib/key-provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <string>
#include <map>
#include "key-provider.h"
#include "pgp-key.h"
#include "key.hpp"
#include "fingerprint.h"
#include "types.h"
#include "utils.h"
Expand Down Expand Up @@ -121,7 +121,7 @@ KeySearch::create(const std::string &name, const std::string &value)
}

bool
KeyIDSearch::matches(const pgp_key_t &key) const
KeyIDSearch::matches(const Key &key) const
{
return (key.keyid() == keyid_) || (keyid_ == pgp_key_id_t({}));
}
Expand Down Expand Up @@ -151,7 +151,7 @@ KeyIDSearch::KeyIDSearch(const pgp_key_id_t &keyid)
}

bool
KeyFingerprintSearch::matches(const pgp_key_t &key) const
KeyFingerprintSearch::matches(const Key &key) const
{
return key.fp() == fp_;
}
Expand Down Expand Up @@ -181,7 +181,7 @@ KeyFingerprintSearch::get_fp() const
}

bool
KeyGripSearch::matches(const pgp_key_t &key) const
KeyGripSearch::matches(const Key &key) const
{
return key.grip() == grip_;
}
Expand All @@ -205,7 +205,7 @@ KeyGripSearch::KeyGripSearch(const pgp_key_grip_t &grip)
}

bool
KeyUIDSearch::matches(const pgp_key_t &key) const
KeyUIDSearch::matches(const Key &key) const
{
return key.has_uid(uid_);
}
Expand All @@ -228,10 +228,10 @@ KeyUIDSearch::KeyUIDSearch(const std::string &uid)
uid_ = uid;
}

pgp_key_t *
Key *
KeyProvider::request_key(const KeySearch &search, pgp_op_t op, bool secret) const
{
pgp_key_t *key = nullptr;
Key *key = nullptr;
if (!callback) {
return key;
}
Expand All @@ -247,10 +247,10 @@ KeyProvider::request_key(const KeySearch &search, pgp_op_t op, bool secret) cons
}
} // namespace rnp

pgp_key_t *
rnp::Key *
rnp_key_provider_key_ptr_list(const pgp_key_request_ctx_t *ctx, void *userdata)
{
std::vector<pgp_key_t *> *key_list = (std::vector<pgp_key_t *> *) userdata;
std::vector<rnp::Key *> *key_list = (std::vector<rnp::Key *> *) userdata;
for (auto key : *key_list) {
if (ctx->search.matches(*key) && (key->is_secret() == ctx->secret)) {
return key;
Expand All @@ -259,27 +259,27 @@ rnp_key_provider_key_ptr_list(const pgp_key_request_ctx_t *ctx, void *userdata)
return NULL;
}

pgp_key_t *
rnp::Key *
rnp_key_provider_chained(const pgp_key_request_ctx_t *ctx, void *userdata)
{
for (rnp::KeyProvider **pprovider = (rnp::KeyProvider **) userdata;
pprovider && *pprovider;
pprovider++) {
auto provider = *pprovider;
pgp_key_t *key = nullptr;
auto provider = *pprovider;
rnp::Key *key = nullptr;
if ((key = provider->callback(ctx, provider->userdata))) {
return key;
}
}
return NULL;
}

pgp_key_t *
rnp::Key *
rnp_key_provider_store(const pgp_key_request_ctx_t *ctx, void *userdata)
{
auto ks = (rnp::KeyStore *) userdata;

for (pgp_key_t *key = ks->search(ctx->search); key; key = ks->search(ctx->search, key)) {
for (rnp::Key *key = ks->search(ctx->search); key; key = ks->search(ctx->search, key)) {
if (key->is_secret() == ctx->secret) {
return key;
}
Expand Down
Loading

0 comments on commit 73a59e5

Please sign in to comment.