diff options
author | saturneric <[email protected]> | 2025-04-12 11:47:01 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2025-04-12 11:47:01 +0000 |
commit | b6e7eee4dfefb70505e29bd22d535ae1fcbed10c (patch) | |
tree | c7dc83074304d8df1232290905cc7757b6cb630a | |
parent | refactor: GpgKey and GpgSubKey (diff) | |
download | GpgFrontend-b6e7eee4dfefb70505e29bd22d535ae1fcbed10c.tar.gz GpgFrontend-b6e7eee4dfefb70505e29bd22d535ae1fcbed10c.zip |
fix: avoid accessing invalid pointers of structs related to gpgme_key
-rw-r--r-- | src/core/model/GpgKey.cpp | 12 | ||||
-rw-r--r-- | src/core/model/GpgKey.h | 3 | ||||
-rw-r--r-- | src/core/model/GpgSubKey.cpp | 52 | ||||
-rw-r--r-- | src/core/model/GpgSubKey.h | 6 | ||||
-rw-r--r-- | src/core/model/GpgUID.cpp | 3 | ||||
-rw-r--r-- | src/core/model/GpgUID.h | 4 |
6 files changed, 41 insertions, 39 deletions
diff --git a/src/core/model/GpgKey.cpp b/src/core/model/GpgKey.cpp index 0c644217..6608d885 100644 --- a/src/core/model/GpgKey.cpp +++ b/src/core/model/GpgKey.cpp @@ -156,7 +156,7 @@ auto GpgKey::SubKeys() const -> QContainer<GpgSubKey> { QContainer<GpgSubKey> ret; auto *next = key_ref_->subkeys; while (next != nullptr) { - ret.push_back(GpgSubKey(next)); + ret.push_back(GpgSubKey(key_ref_, next)); next = next->next; } return ret; @@ -164,10 +164,10 @@ auto GpgKey::SubKeys() const -> QContainer<GpgSubKey> { auto GpgKey::UIDs() const -> QContainer<GpgUID> { QContainer<GpgUID> uids; - auto *uid_next = key_ref_->uids; - while (uid_next != nullptr) { - uids.push_back(GpgUID(uid_next)); - uid_next = uid_next->next; + auto *next = key_ref_->uids; + while (next != nullptr) { + uids.push_back(GpgUID(key_ref_, next)); + next = next->next; } return uids; } @@ -214,7 +214,7 @@ auto GpgKey::IsHasActualEncrCap() const -> bool { } auto GpgKey::PrimaryKey() const -> GpgSubKey { - return GpgSubKey(key_ref_->subkeys); + return GpgSubKey(key_ref_, key_ref_->subkeys); } auto GpgKey::IsSubKey() const -> bool { return false; } diff --git a/src/core/model/GpgKey.h b/src/core/model/GpgKey.h index 2102cdc4..3457b105 100644 --- a/src/core/model/GpgKey.h +++ b/src/core/model/GpgKey.h @@ -332,8 +332,7 @@ class GPGFRONTEND_CORE_EXPORT GpgKey : public GpgAbstractKey { [[nodiscard]] auto PrimaryKey() const -> GpgSubKey; private: - using KeyRefHandler = QSharedPointer<struct _gpgme_key>; ///< - KeyRefHandler key_ref_ = nullptr; ///< + QSharedPointer<struct _gpgme_key> key_ref_ = nullptr; ///< }; } // namespace GpgFrontend diff --git a/src/core/model/GpgSubKey.cpp b/src/core/model/GpgSubKey.cpp index 62f0981a..b4bd94ec 100644 --- a/src/core/model/GpgSubKey.cpp +++ b/src/core/model/GpgSubKey.cpp @@ -27,11 +27,15 @@ */ #include "GpgSubKey.h" +#include <utility> + namespace GpgFrontend { GpgSubKey::GpgSubKey() = default; -GpgSubKey::GpgSubKey(gpgme_subkey_t subkey) : subkey_ref_(subkey) {} +GpgSubKey::GpgSubKey(QSharedPointer<struct _gpgme_key> key_ref, + gpgme_subkey_t s_key) + : key_ref_(std::move(key_ref)), s_key_ref_(s_key) {} GpgSubKey::GpgSubKey(const GpgSubKey&) = default; @@ -39,66 +43,60 @@ GpgSubKey::~GpgSubKey() = default; auto GpgSubKey::operator=(const GpgSubKey&) -> GpgSubKey& = default; -auto GpgSubKey::ID() const -> QString { return subkey_ref_->keyid; } +auto GpgSubKey::ID() const -> QString { return s_key_ref_->keyid; } -auto GpgSubKey::Fingerprint() const -> QString { return subkey_ref_->fpr; } +auto GpgSubKey::Fingerprint() const -> QString { return s_key_ref_->fpr; } auto GpgSubKey::PublicKeyAlgo() const -> QString { - return gpgme_pubkey_algo_name(subkey_ref_->pubkey_algo); + return gpgme_pubkey_algo_name(s_key_ref_->pubkey_algo); } auto GpgSubKey::Algo() const -> QString { - auto* buffer = gpgme_pubkey_algo_string(subkey_ref_); + auto* buffer = gpgme_pubkey_algo_string(s_key_ref_); auto algo = QString(buffer); gpgme_free(buffer); return algo.toUpper(); } -auto GpgSubKey::KeyLength() const -> unsigned int { - return subkey_ref_->length; -} +auto GpgSubKey::KeyLength() const -> unsigned int { return s_key_ref_->length; } -auto GpgSubKey::IsHasEncrCap() const -> bool { - return subkey_ref_->can_encrypt; -} +auto GpgSubKey::IsHasEncrCap() const -> bool { return s_key_ref_->can_encrypt; } -auto GpgSubKey::IsHasSignCap() const -> bool { return subkey_ref_->can_sign; } +auto GpgSubKey::IsHasSignCap() const -> bool { return s_key_ref_->can_sign; } -auto GpgSubKey::IsHasCertCap() const -> bool { - return subkey_ref_->can_certify; -} +auto GpgSubKey::IsHasCertCap() const -> bool { return s_key_ref_->can_certify; } auto GpgSubKey::IsHasAuthCap() const -> bool { - return subkey_ref_->can_authenticate; + return s_key_ref_->can_authenticate; } -auto GpgSubKey::IsPrivateKey() const -> bool { return subkey_ref_->secret; } +auto GpgSubKey::IsPrivateKey() const -> bool { return s_key_ref_->secret; } -auto GpgSubKey::IsExpired() const -> bool { return subkey_ref_->expired; } +auto GpgSubKey::IsExpired() const -> bool { return s_key_ref_->expired; } -auto GpgSubKey::IsRevoked() const -> bool { return subkey_ref_->revoked; } +auto GpgSubKey::IsRevoked() const -> bool { return s_key_ref_->revoked; } -auto GpgSubKey::IsDisabled() const -> bool { return subkey_ref_->disabled; } +auto GpgSubKey::IsDisabled() const -> bool { return s_key_ref_->disabled; } -auto GpgSubKey::IsSecretKey() const -> bool { return subkey_ref_->secret; } +auto GpgSubKey::IsSecretKey() const -> bool { return s_key_ref_->secret; } -auto GpgSubKey::IsCardKey() const -> bool { return subkey_ref_->is_cardkey; } +auto GpgSubKey::IsCardKey() const -> bool { return s_key_ref_->is_cardkey; } auto GpgSubKey::CreationTime() const -> QDateTime { - return QDateTime::fromSecsSinceEpoch(subkey_ref_->timestamp); + return QDateTime::fromSecsSinceEpoch(s_key_ref_->timestamp); } auto GpgSubKey::ExpirationTime() const -> QDateTime { - return QDateTime::fromSecsSinceEpoch(subkey_ref_->expires); + return QDateTime::fromSecsSinceEpoch(s_key_ref_->expires); } -auto GpgSubKey::IsADSK() const -> bool { return subkey_ref_->can_renc; } +auto GpgSubKey::IsADSK() const -> bool { return s_key_ref_->can_renc; } auto GpgSubKey::SmartCardSerialNumber() -> QString { - return subkey_ref_->card_number; + return s_key_ref_->card_number; } auto GpgSubKey::IsSubKey() const -> bool { return true; } -auto GpgSubKey::IsGood() const -> bool { return subkey_ref_ != nullptr; } +auto GpgSubKey::IsGood() const -> bool { return s_key_ref_ != nullptr; } } // namespace GpgFrontend diff --git a/src/core/model/GpgSubKey.h b/src/core/model/GpgSubKey.h index 255d209f..dc92ae1f 100644 --- a/src/core/model/GpgSubKey.h +++ b/src/core/model/GpgSubKey.h @@ -52,7 +52,8 @@ class GPGFRONTEND_CORE_EXPORT GpgSubKey : public GpgAbstractKey { * * @param subkey */ - explicit GpgSubKey(gpgme_subkey_t subkey); + explicit GpgSubKey(QSharedPointer<struct _gpgme_key> key_ref, + gpgme_subkey_t s_key); /** * @brief Construct a new Gpg Sub Key object @@ -234,7 +235,8 @@ class GPGFRONTEND_CORE_EXPORT GpgSubKey : public GpgAbstractKey { [[nodiscard]] auto SmartCardSerialNumber() -> QString; private: - gpgme_subkey_t subkey_ref_ = nullptr; ///< + QSharedPointer<struct _gpgme_key> key_ref_; + gpgme_subkey_t s_key_ref_ = nullptr; ///< }; } // namespace GpgFrontend diff --git a/src/core/model/GpgUID.cpp b/src/core/model/GpgUID.cpp index cd4be820..cfc2784f 100644 --- a/src/core/model/GpgUID.cpp +++ b/src/core/model/GpgUID.cpp @@ -32,7 +32,8 @@ namespace GpgFrontend { GpgUID::GpgUID() = default; -GpgUID::GpgUID(gpgme_user_id_t uid) : uid_ref_(uid) {} +GpgUID::GpgUID(QSharedPointer<struct _gpgme_key> key_ref, gpgme_user_id_t uid) + : key_ref_(std::move(key_ref)), uid_ref_(uid) {} GpgUID::GpgUID(const GpgUID &) = default; diff --git a/src/core/model/GpgUID.h b/src/core/model/GpgUID.h index d8ac47f2..49ddd4c4 100644 --- a/src/core/model/GpgUID.h +++ b/src/core/model/GpgUID.h @@ -109,7 +109,8 @@ class GPGFRONTEND_CORE_EXPORT GpgUID { * * @param uid */ - explicit GpgUID(gpgme_user_id_t uid); + explicit GpgUID(QSharedPointer<struct _gpgme_key> key_ref, + gpgme_user_id_t uid); /** * @brief @@ -125,6 +126,7 @@ class GPGFRONTEND_CORE_EXPORT GpgUID { auto operator=(const GpgUID &) -> GpgUID &; private: + QSharedPointer<struct _gpgme_key> key_ref_; gpgme_user_id_t uid_ref_ = nullptr; ///< }; |