Skip to content

Commit

Permalink
Rename pgp_sig_id_t to pgp::SigID.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 committed Feb 27, 2025
1 parent 257df1c commit 3046d87
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 84 deletions.
26 changes: 13 additions & 13 deletions src/lib/pgp-key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ pgp_key_set_expiration(pgp_key_t * key,
return false;
}

std::vector<pgp_sig_id_t> sigs;
pgp::SigIDs sigs;
/* update expiration for the latest direct-key signature and self-signature for each userid
*/
auto sig = key->latest_selfsig(rnp::UserID::None);
Expand Down Expand Up @@ -300,7 +300,7 @@ pgp_key_set_expiration(pgp_key_t * key,
}

pgp_signature_t newsig;
pgp_sig_id_t oldsigid = sigid;
auto oldsigid = sigid;
if (!update_sig_expiration(&newsig, &sig.sig, ctx.time(), expiry)) {
return false;
}
Expand Down Expand Up @@ -377,7 +377,7 @@ pgp_subkey_set_expiration(pgp_key_t * sub,
try {
/* update signature and re-sign */
pgp_signature_t newsig;
pgp_sig_id_t oldsigid = subsig->sigid;
auto oldsigid = subsig->sigid;
if (!update_sig_expiration(&newsig, &subsig->sig, ctx.time(), expiry)) {
return false;
}
Expand Down Expand Up @@ -602,13 +602,13 @@ pgp_key_t::get_sig(size_t idx) const
}

bool
pgp_key_t::has_sig(const pgp_sig_id_t &id) const
pgp_key_t::has_sig(const pgp::SigID &id) const
{
return sigs_map_.count(id);
}

rnp::Signature &
pgp_key_t::get_sig(const pgp_sig_id_t &id)
pgp_key_t::get_sig(const pgp::SigID &id)
{
if (!has_sig(id)) {
throw rnp::rnp_exception(RNP_ERROR_BAD_PARAMETERS);
Expand All @@ -617,7 +617,7 @@ pgp_key_t::get_sig(const pgp_sig_id_t &id)
}

const rnp::Signature &
pgp_key_t::get_sig(const pgp_sig_id_t &id) const
pgp_key_t::get_sig(const pgp::SigID &id) const
{
if (!has_sig(id)) {
throw rnp::rnp_exception(RNP_ERROR_BAD_PARAMETERS);
Expand All @@ -626,12 +626,12 @@ pgp_key_t::get_sig(const pgp_sig_id_t &id) const
}

rnp::Signature &
pgp_key_t::replace_sig(const pgp_sig_id_t &id, const pgp_signature_t &newsig)
pgp_key_t::replace_sig(const pgp::SigID &id, const pgp_signature_t &newsig)
{
/* save oldsig's uid */
size_t uid = get_sig(id).uid;
/* delete first old sig since we may have theoretically the same sigid */
pgp_sig_id_t oldid = id;
auto oldid = id;
sigs_map_.erase(oldid);
auto &res = sigs_map_.emplace(std::make_pair(newsig.get_id(), newsig)).first->second;
res.uid = uid;
Expand All @@ -655,7 +655,7 @@ pgp_key_t::replace_sig(const pgp_sig_id_t &id, const pgp_signature_t &newsig)
rnp::Signature &
pgp_key_t::add_sig(const pgp_signature_t &sig, size_t uid, bool begin)
{
const pgp_sig_id_t sigid = sig.get_id();
auto sigid = sig.get_id();
sigs_map_.erase(sigid);
auto &res = sigs_map_.emplace(std::make_pair(sigid, sig)).first->second;
res.uid = uid;
Expand All @@ -680,7 +680,7 @@ pgp_key_t::add_sig(const pgp_signature_t &sig, size_t uid, bool begin)
}

bool
pgp_key_t::del_sig(const pgp_sig_id_t &sigid)
pgp_key_t::del_sig(const pgp::SigID &sigid)
{
if (!has_sig(sigid)) {
return false;
Expand All @@ -704,7 +704,7 @@ pgp_key_t::del_sig(const pgp_sig_id_t &sigid)
}

size_t
pgp_key_t::del_sigs(const std::vector<pgp_sig_id_t> &sigs)
pgp_key_t::del_sigs(const pgp::SigIDs &sigs)
{
/* delete actual signatures */
size_t res = 0;
Expand All @@ -716,7 +716,7 @@ pgp_key_t::del_sigs(const std::vector<pgp_sig_id_t> &sigs)
for (auto &uid : uids_) {
uid.clear_sigs();
}
std::vector<pgp_sig_id_t> newsigs;
pgp::SigIDs newsigs;
newsigs.reserve(sigs_map_.size());
for (auto &sigid : sigs_) {
if (!sigs_map_.count(sigid)) {
Expand Down Expand Up @@ -816,7 +816,7 @@ pgp_key_t::del_uid(size_t idx)
throw std::out_of_range("idx");
}

std::vector<pgp_sig_id_t> newsigs;
pgp::SigIDs newsigs;
/* copy sigs which do not belong to uid */
newsigs.reserve(sigs_.size());
for (auto &id : sigs_) {
Expand Down
86 changes: 43 additions & 43 deletions src/lib/pgp-key.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include "sec_profile.hpp"

namespace rnp {
using SignatureMap = std::unordered_map<pgp_sig_id_t, Signature>;
using SignatureMap = std::unordered_map<pgp::SigID, Signature>;
class KeyStore;
class CertParams;
class BindingParams;
Expand All @@ -52,18 +52,18 @@ class BindingParams;
/* describes a user's key */
struct pgp_key_t {
private:
rnp::SignatureMap sigs_map_; /* map with subsigs stored by their id */
std::vector<pgp_sig_id_t> sigs_{}; /* subsig ids to lookup actual sig in map */
std::vector<pgp_sig_id_t> keysigs_{}; /* direct-key signature ids in the original order */
std::vector<rnp::UserID> uids_{}; /* array of user ids */
pgp_key_pkt_t pkt_{}; /* pubkey/seckey data packet */
uint8_t flags_{}; /* key flags */
uint32_t expiration_{}; /* key expiration time, if available */
pgp_key_id_t keyid_{};
pgp_fingerprint_t fingerprint_{};
pgp_key_grip_t grip_{};
pgp_fingerprint_t primary_fp_{}; /* fingerprint of the primary key (for subkeys) */
bool primary_fp_set_{};
rnp::SignatureMap sigs_map_; /* map with subsigs stored by their id */
pgp::SigIDs sigs_; /* subsig ids to lookup actual sig in map */
pgp::SigIDs keysigs_; /* direct-key signature ids in the original order */
std::vector<rnp::UserID> uids_{}; /* array of user ids */
pgp_key_pkt_t pkt_{}; /* pubkey/seckey data packet */
uint8_t flags_{}; /* key flags */
uint32_t expiration_{}; /* key expiration time, if available */
pgp_key_id_t keyid_{};
pgp_fingerprint_t fingerprint_{};
pgp_key_grip_t grip_{};
pgp_fingerprint_t primary_fp_{}; /* fingerprint of the primary key (for subkeys) */
bool primary_fp_set_{};
std::vector<pgp_fingerprint_t>
subkey_fps_{}; /* array of subkey fingerprints (for primary keys) */
rnp::RawPacket rawpkt_; /* key raw packet */
Expand Down Expand Up @@ -96,36 +96,36 @@ struct pgp_key_t {
pgp_key_t &operator=(const pgp_key_t &) = default;
pgp_key_t &operator=(pgp_key_t &&) = default;

size_t sig_count() const;
rnp::Signature & get_sig(size_t idx);
const rnp::Signature & get_sig(size_t idx) const;
bool has_sig(const pgp_sig_id_t &id) const;
rnp::Signature & replace_sig(const pgp_sig_id_t &id, const pgp_signature_t &newsig);
rnp::Signature & get_sig(const pgp_sig_id_t &id);
const rnp::Signature & get_sig(const pgp_sig_id_t &id) const;
rnp::Signature & add_sig(const pgp_signature_t &sig,
size_t uid = rnp::UserID::None,
bool begin = false);
bool del_sig(const pgp_sig_id_t &sigid);
size_t del_sigs(const std::vector<pgp_sig_id_t> &sigs);
size_t keysig_count() const;
rnp::Signature & get_keysig(size_t idx);
size_t uid_count() const;
rnp::UserID & get_uid(size_t idx);
const rnp::UserID & get_uid(size_t idx) const;
size_t get_uid_idx(const pgp_userid_pkt_t &uid) const;
rnp::UserID & add_uid(const pgp_transferable_userid_t &uid);
bool has_uid(const std::string &uid) const;
uint32_t uid_idx(const pgp_userid_pkt_t &uid) const;
void del_uid(size_t idx);
bool has_primary_uid() const;
uint32_t get_primary_uid() const;
bool revoked() const;
const rnp::Revocation &revocation() const;
void clear_revokes();
void add_revoker(const pgp_fingerprint_t &revoker);
bool has_revoker(const pgp_fingerprint_t &revoker) const;
size_t revoker_count() const;
size_t sig_count() const;
rnp::Signature & get_sig(size_t idx);
const rnp::Signature & get_sig(size_t idx) const;
bool has_sig(const pgp::SigID &id) const;
rnp::Signature & replace_sig(const pgp::SigID &id, const pgp_signature_t &newsig);
rnp::Signature & get_sig(const pgp::SigID &id);
const rnp::Signature & get_sig(const pgp::SigID &id) const;
rnp::Signature & add_sig(const pgp_signature_t &sig,
size_t uid = rnp::UserID::None,
bool begin = false);
bool del_sig(const pgp::SigID &sigid);
size_t del_sigs(const pgp::SigIDs &sigs);
size_t keysig_count() const;
rnp::Signature & get_keysig(size_t idx);
size_t uid_count() const;
rnp::UserID & get_uid(size_t idx);
const rnp::UserID & get_uid(size_t idx) const;
size_t get_uid_idx(const pgp_userid_pkt_t &uid) const;
rnp::UserID & add_uid(const pgp_transferable_userid_t &uid);
bool has_uid(const std::string &uid) const;
uint32_t uid_idx(const pgp_userid_pkt_t &uid) const;
void del_uid(size_t idx);
bool has_primary_uid() const;
uint32_t get_primary_uid() const;
bool revoked() const;
const rnp::Revocation & revocation() const;
void clear_revokes();
void add_revoker(const pgp_fingerprint_t &revoker);
bool has_revoker(const pgp_fingerprint_t &revoker) const;
size_t revoker_count() const;
const pgp_fingerprint_t &get_revoker(size_t idx) const;

const pgp_key_pkt_t & pkt() const noexcept;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/rnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4319,7 +4319,7 @@ remove_key_signatures(rnp_ffi_t ffi,
rnp_key_signatures_cb sigcb,
void * app_ctx)
{
std::vector<pgp_sig_id_t> sigs;
pgp::SigIDs sigs;

for (size_t idx = 0; idx < pub.sig_count(); idx++) {
auto &sig = pub.get_sig(idx);
Expand Down Expand Up @@ -6448,7 +6448,7 @@ try {
if (idx >= uid.sig_count()) {
return RNP_ERROR_BAD_PARAMETERS;
}
const pgp_sig_id_t &sigid = uid.get_sig(idx);
auto &sigid = uid.get_sig(idx);
if (!handle->key->has_sig(sigid)) {
return RNP_ERROR_BAD_STATE;
}
Expand Down Expand Up @@ -6955,8 +6955,8 @@ try {
if (!pkey && !skey) {
return RNP_ERROR_BAD_PARAMETERS;
}
const pgp_sig_id_t sigid = sig->sig->sigid;
bool ok = false;
auto sigid = sig->sig->sigid;
bool ok = false;
if (pkey) {
ok = pkey->del_sig(sigid);
pkey->revalidate(*key->ffi->pubring);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/signature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Signature {
public:
uint32_t uid{}; /* index in userid array in key for certification sig */
pgp_signature_t sig{}; /* signature packet */
pgp_sig_id_t sigid{}; /* signature identifier */
pgp::SigID sigid{}; /* signature identifier */
RawPacket raw; /* signature's rawpacket */
SigValidity validity; /* signature validity information */

Expand All @@ -135,7 +135,7 @@ class Revocation {
uint32_t uid; /* index in uid array */
pgp_revocation_type_t code; /* revocation code */
std::string reason; /* revocation reason */
pgp_sig_id_t sigid; /* id of the corresponding subsig */
pgp::SigID sigid; /* id of the corresponding subsig */

Revocation() : uid(0), code(PGP_REVOCATION_NO_REASON), sigid(){};
Revocation(Signature &sig);
Expand Down
13 changes: 8 additions & 5 deletions src/lib/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ typedef struct pgp_fingerprint_t {
std::vector<uint8_t> vec() const;
} pgp_fingerprint_t;

typedef std::array<uint8_t, PGP_KEY_GRIP_SIZE> pgp_sig_id_t;
namespace pgp {
using SigID = std::array<uint8_t, PGP_SHA1_HASH_SIZE>;
using SigIDs = std::vector<SigID>;
} // namespace pgp

namespace std {
template <> struct hash<pgp_fingerprint_t> {
Expand All @@ -122,14 +125,14 @@ template <> struct hash<pgp_fingerprint_t> {
}
};

template <> struct hash<pgp_sig_id_t> {
template <> struct hash<pgp::SigID> {
std::size_t
operator()(pgp_sig_id_t const &sigid) const noexcept
operator()(pgp::SigID const &sigid) const noexcept
{
/* since signature id value is hash itself, we may use its low bytes */
size_t res = 0;
static_assert(std::tuple_size<pgp_sig_id_t>::value >= sizeof(res),
"pgp_sig_id_t size mismatch");
static_assert(std::tuple_size<pgp::SigID>::value >= sizeof(res),
"pgp::SigID size mismatch");
std::memcpy(&res, sigid.data(), sizeof(res));
return res;
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib/userid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ UserID::sig_count() const
return sigs_.size();
}

const pgp_sig_id_t &
const pgp::SigID &
UserID::get_sig(size_t idx) const
{
if (idx >= sigs_.size()) {
Expand All @@ -59,20 +59,20 @@ UserID::get_sig(size_t idx) const
}

bool
UserID::has_sig(const pgp_sig_id_t &id) const
UserID::has_sig(const pgp::SigID &id) const
{
return std::find(sigs_.begin(), sigs_.end(), id) != sigs_.end();
}

void
UserID::add_sig(const pgp_sig_id_t &sig, bool begin)
UserID::add_sig(const pgp::SigID &sig, bool begin)
{
size_t idx = begin ? 0 : sigs_.size();
sigs_.insert(sigs_.begin() + idx, sig);
}

void
UserID::replace_sig(const pgp_sig_id_t &id, const pgp_sig_id_t &newsig)
UserID::replace_sig(const pgp::SigID &id, const pgp::SigID &newsig)
{
auto it = std::find(sigs_.begin(), sigs_.end(), id);
if (it == sigs_.end()) {
Expand All @@ -82,7 +82,7 @@ UserID::replace_sig(const pgp_sig_id_t &id, const pgp_sig_id_t &newsig)
}

bool
UserID::del_sig(const pgp_sig_id_t &id)
UserID::del_sig(const pgp::SigID &id)
{
auto it = std::find(sigs_.begin(), sigs_.end(), id);
if (it == sigs_.end()) {
Expand Down
16 changes: 8 additions & 8 deletions src/lib/userid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace rnp {
/* userid, built on top of userid packet structure */
class UserID {
private:
std::vector<pgp_sig_id_t> sigs_; /* all signatures related to this userid */
pgp::SigIDs sigs_; /* all signatures related to this userid */
public:
pgp_userid_pkt_t pkt; /* User ID or User Attribute packet as it was loaded */
RawPacket rawpkt; /* Raw packet contents */
Expand All @@ -49,13 +49,13 @@ class UserID {
UserID() : valid(false), revoked(false){};
UserID(const pgp_userid_pkt_t &pkt);

size_t sig_count() const;
const pgp_sig_id_t &get_sig(size_t idx) const;
bool has_sig(const pgp_sig_id_t &id) const;
void add_sig(const pgp_sig_id_t &sig, bool begin = false);
void replace_sig(const pgp_sig_id_t &id, const pgp_sig_id_t &newsig);
bool del_sig(const pgp_sig_id_t &id);
void clear_sigs();
size_t sig_count() const;
const pgp::SigID &get_sig(size_t idx) const;
bool has_sig(const pgp::SigID &id) const;
void add_sig(const pgp::SigID &sig, bool begin = false);
void replace_sig(const pgp::SigID &id, const pgp::SigID &newsig);
bool del_sig(const pgp::SigID &id);
void clear_sigs();

/* No userid, i.e. direct-key signature */
static const uint32_t None = (uint32_t) -1;
Expand Down
6 changes: 3 additions & 3 deletions src/librepgp/stream-sig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@ pgp_signature_t::operator!=(const pgp_signature_t &src) const
return !(*this == src);
}

pgp_sig_id_t
pgp::SigID
pgp_signature_t::get_id() const
{
auto hash = rnp::Hash::create(PGP_HASH_SHA1);
hash->add(hashed_data);
hash->add(material_buf);
pgp_sig_id_t res = {0};
pgp::SigID res = {0};
static_assert(std::tuple_size<decltype(res)>::value == PGP_SHA1_HASH_SIZE,
"pgp_sig_id_t size mismatch");
"pgp::SigID size mismatch");
hash->finish(res.data());
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion src/librepgp/stream-sig.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ typedef struct pgp_signature_t {
};

/** @brief Calculate the unique signature identifier by hashing signature's fields. */
pgp_sig_id_t get_id() const;
pgp::SigID get_id() const;

size_t find_subpkt(uint8_t type, bool hashed = true, size_t skip = 0) const;
size_t find_subpkt(pgp::pkt::sigsub::Type type, bool hashed = true, size_t skip = 0) const;
Expand Down

0 comments on commit 3046d87

Please sign in to comment.