diff options
Diffstat (limited to 'src/core/model')
-rw-r--r-- | src/core/model/GpgCardKeyPairInfo.cpp | 73 | ||||
-rw-r--r-- | src/core/model/GpgCardKeyPairInfo.h | 48 | ||||
-rw-r--r-- | src/core/model/GpgKey.cpp | 4 | ||||
-rw-r--r-- | src/core/model/GpgKey.h | 9 | ||||
-rw-r--r-- | src/core/model/GpgKeyTreeModel.cpp | 9 | ||||
-rw-r--r-- | src/core/model/GpgKeyTreeModel.h | 7 | ||||
-rw-r--r-- | src/core/model/GpgOpenPGPCard.cpp | 91 | ||||
-rw-r--r-- | src/core/model/GpgOpenPGPCard.h | 71 | ||||
-rw-r--r-- | src/core/model/GpgSubKey.cpp | 11 | ||||
-rw-r--r-- | src/core/model/GpgSubKey.h | 11 |
10 files changed, 329 insertions, 5 deletions
diff --git a/src/core/model/GpgCardKeyPairInfo.cpp b/src/core/model/GpgCardKeyPairInfo.cpp new file mode 100644 index 00000000..481d88ea --- /dev/null +++ b/src/core/model/GpgCardKeyPairInfo.cpp @@ -0,0 +1,73 @@ +/** + * Copyright (C) 2021-2024 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * 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. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "GpgCardKeyPairInfo.h" + +namespace GpgFrontend { + +GpgCardKeyPairInfo::GpgCardKeyPairInfo(const QString &status) { + const auto values = status.split(QLatin1Char(' ')); + if (values.size() < 2) { + return; + } + + grip = values[0]; + key_ref = values[1]; + if (values.size() >= 3) { + usage = values[2]; + } + + if (values.size() >= 4 && !values[3].isEmpty() && values[3] != "-") { + bool ok; + const qint64 seconds_since_epoch = values[3].toLongLong(&ok); + if (ok) { + time = + QDateTime::fromSecsSinceEpoch(seconds_since_epoch, QTimeZone::utc()); + } + } + + if (values.size() >= 5) { + algorithm = values[4]; + } +} + +auto GpgCardKeyPairInfo::CanAuthenticate() const -> bool { + return usage.contains('a'); +} + +auto GpgCardKeyPairInfo::CanCertify() const -> bool { + return usage.contains('c'); +} + +auto GpgCardKeyPairInfo::CanEncrypt() const -> bool { + return usage.contains('e'); +} + +auto GpgCardKeyPairInfo::CanSign() const -> bool { return usage.contains('s'); } + +} // namespace GpgFrontend diff --git a/src/core/model/GpgCardKeyPairInfo.h b/src/core/model/GpgCardKeyPairInfo.h new file mode 100644 index 00000000..132ded84 --- /dev/null +++ b/src/core/model/GpgCardKeyPairInfo.h @@ -0,0 +1,48 @@ +/** + * Copyright (C) 2021-2024 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * 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. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +namespace GpgFrontend { + +struct GpgCardKeyPairInfo { + explicit GpgCardKeyPairInfo(const QString &status); + + [[nodiscard]] auto CanAuthenticate() const -> bool; + [[nodiscard]] auto CanCertify() const -> bool; + [[nodiscard]] auto CanEncrypt() const -> bool; + [[nodiscard]] auto CanSign() const -> bool; + + QString key_ref; + QString grip; + QString usage; + QDateTime time; + QString algorithm; +}; + +} // namespace GpgFrontend diff --git a/src/core/model/GpgKey.cpp b/src/core/model/GpgKey.cpp index 6608d885..3efb53fc 100644 --- a/src/core/model/GpgKey.cpp +++ b/src/core/model/GpgKey.cpp @@ -37,6 +37,9 @@ GpgKey::GpgKey(gpgme_key_t key) if (ptr != nullptr) gpgme_key_unref(ptr); }) {} +GpgKey::GpgKey(QSharedPointer<struct _gpgme_key> key_ref) + : key_ref_(std::move(key_ref)) {} + GpgKey::operator gpgme_key_t() const { return key_ref_.get(); } GpgKey::GpgKey(const GpgKey &) = default; @@ -218,4 +221,5 @@ auto GpgKey::PrimaryKey() const -> GpgSubKey { } auto GpgKey::IsSubKey() const -> bool { return false; } + } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/model/GpgKey.h b/src/core/model/GpgKey.h index da92de8d..de2e8370 100644 --- a/src/core/model/GpgKey.h +++ b/src/core/model/GpgKey.h @@ -28,6 +28,8 @@ #pragma once +#include <utility> + #include "core/model/GpgAbstractKey.h" #include "core/model/GpgSubKey.h" #include "core/model/GpgUID.h" @@ -57,6 +59,13 @@ class GPGFRONTEND_CORE_EXPORT GpgKey : public GpgAbstractKey { /** * @brief Construct a new Gpg Key object * + * @param key + */ + explicit GpgKey(QSharedPointer<struct _gpgme_key> key_ref); + + /** + * @brief Construct a new Gpg Key object + * * @param k */ GpgKey(const GpgKey&); diff --git a/src/core/model/GpgKeyTreeModel.cpp b/src/core/model/GpgKeyTreeModel.cpp index cb3ee4ba..bbe5d00b 100644 --- a/src/core/model/GpgKeyTreeModel.cpp +++ b/src/core/model/GpgKeyTreeModel.cpp @@ -257,6 +257,15 @@ auto GpgKeyTreeModel::GetAllCheckedSubKey() -> QContainer<GpgSubKey> { return ret; } +auto GpgKeyTreeModel::GetKeyByIndex(QModelIndex index) -> GpgAbstractKey * { + if (!index.isValid()) return nullptr; + + const auto *item = + static_cast<const GpgKeyTreeItem *>(index.internalPointer()); + + return item->Key(); +} + GpgKeyTreeItem::GpgKeyTreeItem(QSharedPointer<GpgAbstractKey> key, QVariantList data) : data_(std::move(data)), key_(std::move(key)) {} diff --git a/src/core/model/GpgKeyTreeModel.h b/src/core/model/GpgKeyTreeModel.h index f56591ea..9e91037d 100644 --- a/src/core/model/GpgKeyTreeModel.h +++ b/src/core/model/GpgKeyTreeModel.h @@ -346,6 +346,13 @@ class GPGFRONTEND_CORE_EXPORT GpgKeyTreeModel : public QAbstractItemModel { */ auto GetAllCheckedSubKey() -> QContainer<GpgSubKey>; + /** + * @brief Get the Key By Index object + * + * @return GpgAbstractKey* + */ + auto GetKeyByIndex(QModelIndex) -> GpgAbstractKey *; + private: int gpg_context_channel_; QVariantList column_headers_; diff --git a/src/core/model/GpgOpenPGPCard.cpp b/src/core/model/GpgOpenPGPCard.cpp new file mode 100644 index 00000000..f0d8c9cd --- /dev/null +++ b/src/core/model/GpgOpenPGPCard.cpp @@ -0,0 +1,91 @@ +/** + * Copyright (C) 2021-2024 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * 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. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "GpgOpenPGPCard.h" + +#include "core/model/GpgCardKeyPairInfo.h" +#include "core/utils/CommonUtils.h" + +namespace GpgFrontend { + +void GpgFrontend::GpgOpenPGPCard::parse_card_info(const QString& name, + const QString& value) { + if (name == "APPVERSION") { + app_version = ParseHexEncodedVersionTuple(value); + } else if (name == "CARDTYPE") { + card_type = value; + } else if (name == "CARDVERSION") { + card_version = ParseHexEncodedVersionTuple(value); + } else if (name == "DISP-NAME") { + auto list = value.split(QStringLiteral("<<"), Qt::SkipEmptyParts); + std::reverse(list.begin(), list.end()); + card_holder = + list.join(QLatin1Char(' ')).replace(QLatin1Char('<'), QLatin1Char(' ')); + } else if (name == "KEYPAIRINFO") { + const GpgCardKeyPairInfo info = GpgCardKeyPairInfo(value); + if (info.grip.isEmpty()) { + LOG_W() << "invalid KEYPAIRINFO status line" << value; + good = false; + } + } else if (name == "KEY-FPR") { + const auto values = value.split(QLatin1Char(' ')); + if (values.size() < 2) { + LOG_W() << "invalid KEY-FPR status line" << value; + good = false; + return; + } + + const auto& key_number = values[0].toInt(); + const auto& fpr = values[1]; + fprs.insert(key_number, fpr); + + } else if (name == "MANUFACTURER") { + // the value of MANUFACTURER is the manufacturer ID as unsigned number + // optionally followed by the name of the manufacturer, e.g. + // 6 Yubico + // 65534 unmanaged S/N range + // for PKCS#15 cards the manufacturer ID is always 0, e.g. + // 0 www.atos.net/cardos [R&S] + auto space_index = value.indexOf(' '); + if (space_index != -1) { + card_infos.insert(name, value.mid(space_index + 1).trimmed()); + } + } else { + card_infos.insert(name, value); + } +} +GpgOpenPGPCard::GpgOpenPGPCard(const QStringList& status) : good(true) { + for (const QString& line : status) { + auto tokens = line.split(' ', Qt::SkipEmptyParts); + auto name = tokens.value(0); + auto value = tokens.mid(1).join(' '); + + parse_card_info(name, value); + } +} +} // namespace GpgFrontend diff --git a/src/core/model/GpgOpenPGPCard.h b/src/core/model/GpgOpenPGPCard.h new file mode 100644 index 00000000..78b7d854 --- /dev/null +++ b/src/core/model/GpgOpenPGPCard.h @@ -0,0 +1,71 @@ +/** + * Copyright (C) 2021-2024 Saturneric <[email protected]> + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from + * 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. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#pragma once + +#include "core/model/GpgCardKeyPairInfo.h" +#include "core/typedef/CoreTypedef.h" + +namespace GpgFrontend { + +struct GPGFRONTEND_CORE_EXPORT GpgOpenPGPCard { + public: + QString reader; + QString serial_number; + QString card_type; + int card_version; + QString app_type; + int app_version; + QString ext_capability; + QString manufacturer; + QString card_holder; + QString display_language; + QString display_sex; + QString chv_status; + int sig_counter = 0; + + QContainer<GpgCardKeyPairInfo> keys; + QMap<int, QString> fprs; + QMap<QString, QString> card_infos; + + QString kdf; + QString uif1; + QString uif2; + QString uif3; + + bool good = false; + + GpgOpenPGPCard() = default; + + explicit GpgOpenPGPCard(const QStringList& status); + + private: + void parse_card_info(const QString& name, const QString& value); +}; + +} // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/model/GpgSubKey.cpp b/src/core/model/GpgSubKey.cpp index b4bd94ec..a4e6582f 100644 --- a/src/core/model/GpgSubKey.cpp +++ b/src/core/model/GpgSubKey.cpp @@ -27,8 +27,7 @@ */ #include "GpgSubKey.h" -#include <utility> - +#include "core/model/GpgKey.h" namespace GpgFrontend { GpgSubKey::GpgSubKey() = default; @@ -92,11 +91,15 @@ auto GpgSubKey::ExpirationTime() const -> QDateTime { auto GpgSubKey::IsADSK() const -> bool { return s_key_ref_->can_renc; } -auto GpgSubKey::SmartCardSerialNumber() -> QString { - return s_key_ref_->card_number; +auto GpgSubKey::SmartCardSerialNumber() const -> QString { + return QString::fromLatin1(s_key_ref_->card_number); } auto GpgSubKey::IsSubKey() const -> bool { return true; } auto GpgSubKey::IsGood() const -> bool { return s_key_ref_ != nullptr; } + +auto GpgSubKey::Convert2GpgKey() const -> QSharedPointer<GpgKey> { + return QSharedPointer<GpgKey>::create(key_ref_); +} } // namespace GpgFrontend diff --git a/src/core/model/GpgSubKey.h b/src/core/model/GpgSubKey.h index fe925f8e..c947b8af 100644 --- a/src/core/model/GpgSubKey.h +++ b/src/core/model/GpgSubKey.h @@ -35,6 +35,8 @@ namespace GpgFrontend { +class GpgKey; + /** * @brief * @@ -232,7 +234,14 @@ class GPGFRONTEND_CORE_EXPORT GpgSubKey : public GpgAbstractKey { * * @return QString */ - [[nodiscard]] auto SmartCardSerialNumber() -> QString; + [[nodiscard]] auto SmartCardSerialNumber() const -> QString; + + /** + * @brief + * + * @return QString + */ + [[nodiscard]] auto Convert2GpgKey() const -> QSharedPointer<GpgKey>; private: QSharedPointer<struct _gpgme_key> key_ref_; |