diff options
Diffstat (limited to 'src/core/model/GpgKey.cpp')
-rw-r--r-- | src/core/model/GpgKey.cpp | 196 |
1 files changed, 96 insertions, 100 deletions
diff --git a/src/core/model/GpgKey.cpp b/src/core/model/GpgKey.cpp index 3a167b81..53842249 100644 --- a/src/core/model/GpgKey.cpp +++ b/src/core/model/GpgKey.cpp @@ -1,5 +1,5 @@ /** - * Copyright (C) 2021 Saturneric + * Copyright (C) 2021 Saturneric <[email protected]> * * This file is part of GpgFrontend. * @@ -20,7 +20,7 @@ * the gpg4usb project, which is under GPL-3.0-or-later. * * All the source code of GpgFrontend was modified and released by - * Saturneric<[email protected]> starting on May 12, 2021. + * Saturneric <[email protected]> starting on May 12, 2021. * * SPDX-License-Identifier: GPL-3.0-or-later * @@ -30,70 +30,80 @@ #include <mutex> -GpgFrontend::GpgKey::GpgKey(gpgme_key_t &&key) : key_ref_(std::move(key)) {} +namespace GpgFrontend { -GpgFrontend::GpgKey::GpgKey(GpgKey &&k) noexcept { swap(key_ref_, k.key_ref_); } +GpgKey::GpgKey(gpgme_key_t &&key) : key_ref_(key) {} -GpgFrontend::GpgKey &GpgFrontend::GpgKey::operator=(GpgKey &&k) noexcept { +GpgKey::GpgKey(GpgKey &&k) noexcept { swap(key_ref_, k.key_ref_); } + +auto GpgKey::operator=(GpgKey &&k) noexcept -> GpgKey & { swap(key_ref_, k.key_ref_); return *this; } -bool GpgFrontend::GpgKey::operator==(const GpgKey &o) const { +GpgKey::GpgKey(const GpgKey &key) { + auto *key_ref = key.key_ref_.get(); + gpgme_key_ref(key_ref); + this->key_ref_ = KeyRefHandler(key_ref); +} + +auto GpgKey::operator=(const GpgKey &key) -> GpgKey & { + if (this == &key) { + return *this; + } + + auto *key_ref = key.key_ref_.get(); + gpgme_key_ref(key_ref); + + this->key_ref_ = KeyRefHandler(key_ref); + return *this; +} + +auto GpgKey::operator==(const GpgKey &o) const -> bool { return o.GetId() == this->GetId(); } -bool GpgFrontend::GpgKey::operator<=(const GpgKey &o) const { +auto GpgKey::operator<=(const GpgKey &o) const -> bool { return this->GetId() < o.GetId(); } -GpgFrontend::GpgKey::operator gpgme_key_t() const { return key_ref_.get(); } +GpgKey::operator gpgme_key_t() const { return key_ref_.get(); } -bool GpgFrontend::GpgKey::IsGood() const { return key_ref_ != nullptr; } +auto GpgKey::IsGood() const -> bool { return key_ref_ != nullptr; } -std::string GpgFrontend::GpgKey::GetId() const { - return key_ref_->subkeys->keyid; -} +auto GpgKey::GetId() const -> QString { return key_ref_->subkeys->keyid; } -std::string GpgFrontend::GpgKey::GetName() const { - return key_ref_->uids->name; -}; +auto GpgKey::GetName() const -> QString { return key_ref_->uids->name; }; -std::string GpgFrontend::GpgKey::GetEmail() const { - return key_ref_->uids->email; -} +auto GpgKey::GetEmail() const -> QString { return key_ref_->uids->email; } -std::string GpgFrontend::GpgKey::GetComment() const { - return key_ref_->uids->comment; -} +auto GpgKey::GetComment() const -> QString { return key_ref_->uids->comment; } -std::string GpgFrontend::GpgKey::GetFingerprint() const { - return key_ref_->fpr; -} +auto GpgKey::GetFingerprint() const -> QString { return key_ref_->fpr; } -std::string GpgFrontend::GpgKey::GetProtocol() const { +auto GpgKey::GetProtocol() const -> QString { return gpgme_get_protocol_name(key_ref_->protocol); } -std::string GpgFrontend::GpgKey::GetOwnerTrust() const { +auto GpgKey::GetOwnerTrust() const -> QString { switch (key_ref_->owner_trust) { case GPGME_VALIDITY_UNKNOWN: - return _("Unknown"); + return tr("Unknown"); case GPGME_VALIDITY_UNDEFINED: - return _("Undefined"); + return tr("Undefined"); case GPGME_VALIDITY_NEVER: - return _("Never"); + return tr("Never"); case GPGME_VALIDITY_MARGINAL: - return _("Marginal"); + return tr("Marginal"); case GPGME_VALIDITY_FULL: - return _("Full"); + return tr("Full"); case GPGME_VALIDITY_ULTIMATE: - return _("Ultimate"); + return tr("Ultimate"); } return "Invalid"; } -int GpgFrontend::GpgKey::GetOwnerTrustLevel() const { +auto GpgKey::GetOwnerTrustLevel() const -> int { switch (key_ref_->owner_trust) { case GPGME_VALIDITY_UNKNOWN: return 0; @@ -111,66 +121,72 @@ int GpgFrontend::GpgKey::GetOwnerTrustLevel() const { return 0; } -std::string GpgFrontend::GpgKey::GetPublicKeyAlgo() const { +auto GpgKey::GetPublicKeyAlgo() const -> QString { return gpgme_pubkey_algo_name(key_ref_->subkeys->pubkey_algo); } -boost::posix_time::ptime GpgFrontend::GpgKey::GetLastUpdateTime() const { - return boost::posix_time::from_time_t( +auto GpgKey::GetKeyAlgo() const -> QString { + auto *buffer = gpgme_pubkey_algo_string(key_ref_->subkeys); + auto algo = QString(buffer); + gpgme_free(buffer); + return algo.toUpper(); +} + +auto GpgKey::GetLastUpdateTime() const -> QDateTime { + return QDateTime::fromSecsSinceEpoch( static_cast<time_t>(key_ref_->last_update)); } -boost::posix_time::ptime GpgFrontend::GpgKey::GetExpireTime() const { - return boost::posix_time::from_time_t(key_ref_->subkeys->expires); +auto GpgKey::GetExpireTime() const -> QDateTime { + return QDateTime::fromSecsSinceEpoch(key_ref_->subkeys->expires); }; -boost::posix_time::ptime GpgFrontend::GpgKey::GetCreateTime() const { - return boost::posix_time::from_time_t(key_ref_->subkeys->timestamp); +auto GpgKey::GetCreateTime() const -> QDateTime { + return QDateTime::fromSecsSinceEpoch(key_ref_->subkeys->timestamp); }; -unsigned int GpgFrontend::GpgKey::GetPrimaryKeyLength() const { +auto GpgKey::GetPrimaryKeyLength() const -> unsigned int { return key_ref_->subkeys->length; } -bool GpgFrontend::GpgKey::IsHasEncryptionCapability() const { +auto GpgKey::IsHasEncryptionCapability() const -> bool { return key_ref_->can_encrypt; } -bool GpgFrontend::GpgKey::IsHasSigningCapability() const { +auto GpgKey::IsHasSigningCapability() const -> bool { return key_ref_->can_sign; } -bool GpgFrontend::GpgKey::IsHasCertificationCapability() const { +auto GpgKey::IsHasCertificationCapability() const -> bool { return key_ref_->can_certify; } -bool GpgFrontend::GpgKey::IsHasAuthenticationCapability() const { +auto GpgKey::IsHasAuthenticationCapability() const -> bool { return key_ref_->can_authenticate; } -bool GpgFrontend::GpgKey::IsHasCardKey() const { +auto GpgKey::IsHasCardKey() const -> bool { auto subkeys = GetSubKeys(); return std::any_of( subkeys->begin(), subkeys->end(), [](const GpgSubKey &subkey) -> bool { return subkey.IsCardKey(); }); } -bool GpgFrontend::GpgKey::IsPrivateKey() const { return key_ref_->secret; } +auto GpgKey::IsPrivateKey() const -> bool { return key_ref_->secret; } -bool GpgFrontend::GpgKey::IsExpired() const { return key_ref_->expired; } +auto GpgKey::IsExpired() const -> bool { return key_ref_->expired; } -bool GpgFrontend::GpgKey::IsRevoked() const { return key_ref_->revoked; } +auto GpgKey::IsRevoked() const -> bool { return key_ref_->revoked; } -bool GpgFrontend::GpgKey::IsDisabled() const { return key_ref_->disabled; } +auto GpgKey::IsDisabled() const -> bool { return key_ref_->disabled; } -bool GpgFrontend::GpgKey::IsHasMasterKey() const { +auto GpgKey::IsHasMasterKey() const -> bool { return key_ref_->subkeys->secret; } -std::unique_ptr<std::vector<GpgFrontend::GpgSubKey>> -GpgFrontend::GpgKey::GetSubKeys() const { +auto GpgKey::GetSubKeys() const -> std::unique_ptr<std::vector<GpgSubKey>> { auto p_keys = std::make_unique<std::vector<GpgSubKey>>(); - auto next = key_ref_->subkeys; + auto *next = key_ref_->subkeys; while (next != nullptr) { p_keys->push_back(GpgSubKey(next)); next = next->next; @@ -178,10 +194,9 @@ GpgFrontend::GpgKey::GetSubKeys() const { return p_keys; } -std::unique_ptr<std::vector<GpgFrontend::GpgUID>> GpgFrontend::GpgKey::GetUIDs() - const { +auto GpgKey::GetUIDs() const -> std::unique_ptr<std::vector<GpgUID>> { auto p_uids = std::make_unique<std::vector<GpgUID>>(); - auto uid_next = key_ref_->uids; + auto *uid_next = key_ref_->uids; while (uid_next != nullptr) { p_uids->push_back(GpgUID(uid_next)); uid_next = uid_next->next; @@ -189,32 +204,24 @@ std::unique_ptr<std::vector<GpgFrontend::GpgUID>> GpgFrontend::GpgKey::GetUIDs() return p_uids; } -bool GpgFrontend::GpgKey::IsHasActualSigningCapability() const { +auto GpgKey::IsHasActualSigningCapability() const -> bool { auto subkeys = GetSubKeys(); - if (std::any_of(subkeys->begin(), subkeys->end(), - [](const GpgSubKey &subkey) -> bool { - return subkey.IsSecretKey() && - subkey.IsHasSigningCapability() && - !subkey.IsDisabled() && !subkey.IsRevoked() && - !subkey.IsExpired(); - })) - return true; - else - return false; + return std::any_of( + subkeys->begin(), subkeys->end(), [](const GpgSubKey &subkey) -> bool { + return subkey.IsSecretKey() && subkey.IsHasSigningCapability() && + !subkey.IsDisabled() && !subkey.IsRevoked() && + !subkey.IsExpired(); + }); } -bool GpgFrontend::GpgKey::IsHasActualAuthenticationCapability() const { +auto GpgKey::IsHasActualAuthenticationCapability() const -> bool { auto subkeys = GetSubKeys(); - if (std::any_of(subkeys->begin(), subkeys->end(), - [](const GpgSubKey &subkey) -> bool { - return subkey.IsSecretKey() && - subkey.IsHasAuthenticationCapability() && - !subkey.IsDisabled() && !subkey.IsRevoked() && - !subkey.IsExpired(); - })) - return true; - else - return false; + return std::any_of( + subkeys->begin(), subkeys->end(), [](const GpgSubKey &subkey) -> bool { + return subkey.IsSecretKey() && subkey.IsHasAuthenticationCapability() && + !subkey.IsDisabled() && !subkey.IsRevoked() && + !subkey.IsExpired(); + }); } /** @@ -222,7 +229,7 @@ bool GpgFrontend::GpgKey::IsHasActualAuthenticationCapability() const { * @param key target key * @return if key certify */ -bool GpgFrontend::GpgKey::IsHasActualCertificationCapability() const { +auto GpgKey::IsHasActualCertificationCapability() const -> bool { return IsHasMasterKey() && !IsExpired() && !IsRevoked() && !IsDisabled(); } @@ -231,28 +238,17 @@ bool GpgFrontend::GpgKey::IsHasActualCertificationCapability() const { * @param key target key * @return if key encrypt */ -bool GpgFrontend::GpgKey::IsHasActualEncryptionCapability() const { +auto GpgKey::IsHasActualEncryptionCapability() const -> bool { auto subkeys = GetSubKeys(); - if (std::any_of(subkeys->begin(), subkeys->end(), - [](const GpgSubKey &subkey) -> bool { - return subkey.IsHasEncryptionCapability() && - !subkey.IsDisabled() && !subkey.IsRevoked() && - !subkey.IsExpired(); - })) - return true; - else - return false; -} - -GpgFrontend::GpgKey GpgFrontend::GpgKey::Copy() const { - { - const std::lock_guard<std::mutex> guard(gpgme_key_opera_mutex); - gpgme_key_ref(key_ref_.get()); - } - auto *_new_key_ref = key_ref_.get(); - return GpgKey(std::move(_new_key_ref)); + return std::any_of( + subkeys->begin(), subkeys->end(), [](const GpgSubKey &subkey) -> bool { + return subkey.IsHasEncryptionCapability() && !subkey.IsDisabled() && + !subkey.IsRevoked() && !subkey.IsExpired(); + }); } -void GpgFrontend::GpgKey::_key_ref_deleter::operator()(gpgme_key_t _key) { +void GpgKey::KeyRefDeleter::operator()(gpgme_key_t _key) { if (_key != nullptr) gpgme_key_unref(_key); } + +} // namespace GpgFrontend
\ No newline at end of file |