diff options
author | saturneric <[email protected]> | 2025-04-12 23:03:57 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2025-04-12 23:03:57 +0000 |
commit | 571cfb16ccfd7ac6bc59b5acc77a94d0bdcf0990 (patch) | |
tree | a4efe7ba4d5344897c60513c03172d5de0191658 /src/ui | |
parent | fix: upgrade to gpgme 1.24.2 (diff) | |
download | GpgFrontend-571cfb16ccfd7ac6bc59b5acc77a94d0bdcf0990.tar.gz GpgFrontend-571cfb16ccfd7ac6bc59b5acc77a94d0bdcf0990.zip |
feat: add openpgp smart card support
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/dialog/controller/SmartCardControllerDialog.cpp | 173 | ||||
-rw-r--r-- | src/ui/dialog/controller/SmartCardControllerDialog.h | 85 | ||||
-rw-r--r-- | src/ui/dialog/keypair_details/KeyPairSubkeyTab.cpp | 7 | ||||
-rw-r--r-- | src/ui/main_window/MainWindow.h | 6 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowUI.cpp | 10 | ||||
-rw-r--r-- | src/ui/model/GpgKeyTreeProxyModel.cpp | 5 | ||||
-rw-r--r-- | src/ui/model/GpgKeyTreeProxyModel.h | 25 | ||||
-rw-r--r-- | src/ui/widgets/KeyTreeView.cpp | 87 | ||||
-rw-r--r-- | src/ui/widgets/KeyTreeView.h | 26 |
9 files changed, 402 insertions, 22 deletions
diff --git a/src/ui/dialog/controller/SmartCardControllerDialog.cpp b/src/ui/dialog/controller/SmartCardControllerDialog.cpp new file mode 100644 index 00000000..847836b1 --- /dev/null +++ b/src/ui/dialog/controller/SmartCardControllerDialog.cpp @@ -0,0 +1,173 @@ +/** + * 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 "SmartCardControllerDialog.h" + +#include "core/function/gpg/GpgAssuanHelper.h" +#include "core/utils/GpgUtils.h" + +// +#include "ui_SmartCardControllerDialog.h" + +namespace GpgFrontend::UI { +SmartCardControllerDialog::SmartCardControllerDialog(QWidget* parent) + : GeneralDialog("SmartCardControllerDialog", parent), + ui_(QSharedPointer<Ui_SmartCardControllerDialog>::create()), + channel_(kGpgFrontendDefaultChannel) { + ui_->setupUi(this); + + for (const auto& key_db : GetGpgKeyDatabaseInfos()) { + ui_->keyDBIndexComboBox->insertItem( + key_db.channel, QString("%1: %2").arg(key_db.channel).arg(key_db.name)); + } + + connect(ui_->keyDBIndexComboBox, + qOverload<int>(&QComboBox::currentIndexChanged), this, + [=](int index) { + channel_ = index; + ui_->cardKeysTreeView->SetChannel(channel_); + ui_->cardKeysTreeView->expandAll(); + }); + + slot_refresh(); +} + +void SmartCardControllerDialog::get_smart_card_serial_number() { + auto [ret, status] = GpgAssuanHelper::GetInstance().SendStatusCommand( + GpgComponentType::kGPG_AGENT, "SCD SERIALNO --all openpgp"); + if (!ret || status.isEmpty()) return; + + auto line = status.front(); + auto token = line.split(' '); + + if (token.size() != 2) { + LOG_E() << "invalid response of command SERIALNO: " << line; + return; + } + + serial_number_ = token.back().trimmed(); + LOG_D() << "get smart card serial number: " << serial_number_; +} + +void SmartCardControllerDialog::fetch_smart_card_info() { + if (serial_number_.isEmpty()) return; + + auto [ret, status] = GpgAssuanHelper::GetInstance().SendStatusCommand( + GpgComponentType::kGPG_AGENT, "SCD LEARN --force " + serial_number_); + if (!ret || status.isEmpty()) return; + + LOG_D() << "fetch card raw info: " << status; + card_info_ = GpgOpenPGPCard(status); + + if (!card_info_.good) { + LOG_E() << "parse card raw info failed"; + return; + } +} + +void SmartCardControllerDialog::print_smart_card_info() { + if (!card_info_.good) return; + + QString html; + QTextStream out(&html); + auto card = card_info_; + + out << "<h2>" << tr("OpenPGP Card Information") << "</h2>"; + + out << "<h3>" << tr("Basic Information") << "</h3><ul>"; + out << "<li><b>" << tr("Reader:") << "</b> " << card.reader << "</li>"; + out << "<li><b>" << tr("Serial Number:") << "</b> " << card.serial_number + << "</li>"; + out << "<li><b>" << tr("Card Type:") << "</b> " << card.card_type << "</li>"; + out << "<li><b>" << tr("Card Version:") << "</b> " << card.card_version + << "</li>"; + out << "<li><b>" << tr("App Type:") << "</b> " << card.app_type << "</li>"; + out << "<li><b>" << tr("App Version:") << "</b> " << card.app_version + << "</li>"; + out << "<li><b>" << tr("Manufacturer:") << "</b> " << card.manufacturer + << "</li>"; + out << "<li><b>" << tr("Card Holder:") << "</b> " << card.card_holder + << "</li>"; + out << "<li><b>" << tr("Language:") << "</b> " << card.display_language + << "</li>"; + out << "<li><b>" << tr("Sex:") << "</b> " << card.display_sex << "</li>"; + out << "</ul>"; + + out << "<h3>" << tr("Status") << "</h3><ul>"; + out << "<li><b>" << tr("CHV Status:") << "</b> " << card.chv_status + << "</li>"; + out << "<li><b>" << tr("Signature Counter:") << "</b> " << card.sig_counter + << "</li>"; + out << "<li><b>" << tr("KDF:") << "</b> " << card.kdf << "</li>"; + out << "<li><b>" << tr("UIF1:") << "</b> " << card.uif1 << "</li>"; + out << "<li><b>" << tr("UIF2:") << "</b> " << card.uif2 << "</li>"; + out << "<li><b>" << tr("UIF3:") << "</b> " << card.uif3 << "</li>"; + out << "</ul>"; + + out << "<h3>" << tr("Fingerprints") << "</h3><ul>"; + for (auto it = card.fprs.begin(); it != card.fprs.end(); ++it) { + out << "<li><b>" << tr("Key %1:").arg(it.key()) << "</b> " << it.value() + << "</li>"; + } + out << "</ul>"; + + if (!card.card_infos.isEmpty()) { + out << "<h3>" << tr("Additional Info") << "</h3><ul>"; + for (auto it = card.card_infos.begin(); it != card.card_infos.end(); ++it) { + out << "<li><b>" << tr("%1:").arg(it.key()) << "</b> " << it.value() + << "</li>"; + } + out << "</ul>"; + } + + ui_->cardInfoEdit->setText(html); +} + +void SmartCardControllerDialog::slot_refresh() { + ui_->cardInfoEdit->clear(); + + get_smart_card_serial_number(); + fetch_smart_card_info(); + + print_smart_card_info(); + refresh_key_tree_view(); +} + +void SmartCardControllerDialog::refresh_key_tree_view() { + if (card_info_.fprs.isEmpty()) { + ui_->cardKeysTreeView->SetKeyFilter([](auto) { return false; }); + return; + } + + QStringList card_fprs(card_info_.fprs.begin(), card_info_.fprs.end()); + ui_->cardKeysTreeView->SetKeyFilter([=](const GpgAbstractKey* k) { + return card_fprs.contains(k->Fingerprint()); + }); +} + +} // namespace GpgFrontend::UI diff --git a/src/ui/dialog/controller/SmartCardControllerDialog.h b/src/ui/dialog/controller/SmartCardControllerDialog.h new file mode 100644 index 00000000..9b71bba7 --- /dev/null +++ b/src/ui/dialog/controller/SmartCardControllerDialog.h @@ -0,0 +1,85 @@ +/** + * 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/GpgOpenPGPCard.h" +#include "ui/dialog/GeneralDialog.h" + +class Ui_SmartCardControllerDialog; + +namespace GpgFrontend::UI { +class SmartCardControllerDialog : public GeneralDialog { + Q_OBJECT + public: + /** + * @brief Construct a new Smart Card Controller Dialog object + * + * @param parent + */ + explicit SmartCardControllerDialog(QWidget* parent = nullptr); + + private slots: + + /** + * @brief + * + */ + void slot_refresh(); + + private: + QSharedPointer<Ui_SmartCardControllerDialog> ui_; ///< + int channel_; + QString serial_number_; + GpgOpenPGPCard card_info_; + + /** + * @brief Get the smart card serial number object + * + */ + void get_smart_card_serial_number(); + + /** + * @brief + * + */ + void fetch_smart_card_info(); + + /** + * @brief + * + */ + void print_smart_card_info(); + + /** + * @brief + * + */ + void refresh_key_tree_view(); +}; +} // namespace GpgFrontend::UI diff --git a/src/ui/dialog/keypair_details/KeyPairSubkeyTab.cpp b/src/ui/dialog/keypair_details/KeyPairSubkeyTab.cpp index 79a28176..c4e93b62 100644 --- a/src/ui/dialog/keypair_details/KeyPairSubkeyTab.cpp +++ b/src/ui/dialog/keypair_details/KeyPairSubkeyTab.cpp @@ -389,7 +389,12 @@ void KeyPairSubkeyTab::slot_refresh_subkey_detail() { : tr("Not Exists")); // Show the situation if key in a smart card. - card_key_label_->setText(s_key.IsCardKey() ? tr("Yes") : tr("No")); + auto smart_card_info = s_key.IsCardKey() ? tr("Yes") : tr("No"); + if (s_key.IsCardKey() && !s_key.SmartCardSerialNumber().isEmpty()) { + smart_card_info += " "; + smart_card_info += "(" + s_key.SmartCardSerialNumber() + ")"; + } + card_key_label_->setText(smart_card_info); if (!s_key.IsSecretKey()) { auto palette_expired = master_key_exist_var_label_->palette(); diff --git a/src/ui/main_window/MainWindow.h b/src/ui/main_window/MainWindow.h index 9e5a289c..58aa406d 100644 --- a/src/ui/main_window/MainWindow.h +++ b/src/ui/main_window/MainWindow.h @@ -711,8 +711,10 @@ class MainWindow : public GeneralMainWindow { QAction* clean_double_line_breaks_act_{}; ///< Action to remove double ///< line breaks - QAction* gnupg_controller_open_act_{}; ///< - QAction* module_controller_open_act_{}; ///< + QAction* gnupg_controller_open_act_{}; ///< + QAction* module_controller_open_act_{}; ///< + QAction* smart_card_controller_open_act_{}; ///< + QAction* clean_gpg_password_cache_act_{}; ///< QAction* reload_components_act_{}; ///< QAction* restart_components_act_{}; ///< diff --git a/src/ui/main_window/MainWindowUI.cpp b/src/ui/main_window/MainWindowUI.cpp index a586a43b..6598aede 100644 --- a/src/ui/main_window/MainWindowUI.cpp +++ b/src/ui/main_window/MainWindowUI.cpp @@ -29,9 +29,10 @@ #include "MainWindow.h" #include "core/function/GlobalSettingStation.h" #include "core/module/ModuleManager.h" -#include "dialog/controller/ModuleControllerDialog.h" #include "ui/UserInterfaceUtils.h" #include "ui/dialog/controller/GnuPGControllerDialog.h" +#include "ui/dialog/controller/ModuleControllerDialog.h" +#include "ui/dialog/controller/SmartCardControllerDialog.h" #include "ui/dialog/help/AboutDialog.h" #include "ui/widgets/KeyList.h" #include "ui/widgets/TextEdit.h" @@ -255,6 +256,12 @@ void MainWindow::create_actions() { connect(module_controller_open_act_, &QAction::triggered, this, [this]() { (new ModuleControllerDialog(this))->exec(); }); + smart_card_controller_open_act_ = create_action( + "smart_card_controller_open", tr("Open Smart Card Controller"), + ":/icons/smart-card.png", tr("Open Smart Card Controller Dialog")); + connect(smart_card_controller_open_act_, &QAction::triggered, this, + [this]() { (new SmartCardControllerDialog(this))->exec(); }); + /** * E-Mail Menu */ @@ -462,6 +469,7 @@ void MainWindow::create_menus() { advance_menu_->addSeparator(); advance_menu_->addAction(gnupg_controller_open_act_); advance_menu_->addAction(module_controller_open_act_); + advance_menu_->addAction(smart_card_controller_open_act_); view_menu_ = menuBar()->addMenu(tr("View")); diff --git a/src/ui/model/GpgKeyTreeProxyModel.cpp b/src/ui/model/GpgKeyTreeProxyModel.cpp index e58cee88..2f46f548 100644 --- a/src/ui/model/GpgKeyTreeProxyModel.cpp +++ b/src/ui/model/GpgKeyTreeProxyModel.cpp @@ -125,4 +125,9 @@ void GpgKeyTreeProxyModel::slot_update_favorites_cache() { } } +void GpgKeyTreeProxyModel::SetKeyFilter(const KeyFilter &filter) { + custom_filter_ = filter; + invalidateFilter(); +} + } // namespace GpgFrontend::UI
\ No newline at end of file diff --git a/src/ui/model/GpgKeyTreeProxyModel.h b/src/ui/model/GpgKeyTreeProxyModel.h index 5e75dd84..f41afd4f 100644 --- a/src/ui/model/GpgKeyTreeProxyModel.h +++ b/src/ui/model/GpgKeyTreeProxyModel.h @@ -40,14 +40,39 @@ class GpgKeyTreeProxyModel : public QSortFilterProxyModel { public: using KeyFilter = std::function<bool(const GpgAbstractKey *)>; + /** + * @brief Construct a new Gpg Key Tree Proxy Model object + * + * @param model + * @param display_mode + * @param filter + * @param parent + */ explicit GpgKeyTreeProxyModel(QSharedPointer<GpgKeyTreeModel> model, GpgKeyTreeDisplayMode display_mode, KeyFilter filter, QObject *parent); + /** + * @brief Set the Search Keywords object + * + * @param keywords + */ void SetSearchKeywords(const QString &keywords); + /** + * @brief + * + * @param model + */ void ResetGpgKeyTableModel(QSharedPointer<GpgKeyTreeModel> model); + /** + * @brief Set the Key Filter object + * + * @param filter + */ + void SetKeyFilter(const KeyFilter &filter); + protected: [[nodiscard]] auto filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const -> bool override; diff --git a/src/ui/widgets/KeyTreeView.cpp b/src/ui/widgets/KeyTreeView.cpp index 3dde069a..3df5a440 100644 --- a/src/ui/widgets/KeyTreeView.cpp +++ b/src/ui/widgets/KeyTreeView.cpp @@ -31,10 +31,24 @@ #include <utility> #include "core/function/gpg/GpgKeyGetter.h" -#include "model/GpgKeyTreeProxyModel.h" +#include "core/utils/GpgUtils.h" +#include "ui/dialog/keypair_details/KeyDetailsDialog.h" +#include "ui/model/GpgKeyTreeProxyModel.h" namespace GpgFrontend::UI { +KeyTreeView::KeyTreeView(QWidget* parent) + : QTreeView(parent), + channel_(kGpgFrontendDefaultChannel), + model_(QSharedPointer<GpgKeyTreeModel>::create( + channel_, GpgKeyGetter::GetInstance(channel_).FetchKey(), + [](auto) { return false; }, this)), + proxy_model_( + model_, GpgKeyTreeDisplayMode::kALL, [](auto) { return false; }, + this) { + init(); +} + KeyTreeView::KeyTreeView(int channel, GpgKeyTreeModel::Detector checkable_detector, GpgKeyTreeProxyModel::KeyFilter filter, @@ -42,10 +56,37 @@ KeyTreeView::KeyTreeView(int channel, : QTreeView(parent), channel_(channel), model_(QSharedPointer<GpgKeyTreeModel>::create( - channel, GpgKeyGetter::GetInstance(channel_).FetchKey(), + channel_, GpgKeyGetter::GetInstance(channel_).FetchKey(), checkable_detector, this)), proxy_model_(model_, GpgKeyTreeDisplayMode::kALL, std::move(filter), this) { + init(); +} + +void KeyTreeView::paintEvent(QPaintEvent* event) { + QTreeView::paintEvent(event); + + if (!init_) { + slot_adjust_column_widths(); + init_ = true; + } +} + +void KeyTreeView::slot_adjust_column_widths() { + for (int i = 1; i < model_->columnCount({}); ++i) { + this->resizeColumnToContents(i); + } +} + +auto KeyTreeView::GetAllCheckedKeyIds() -> KeyIdArgsList { + return model_->GetAllCheckedKeyIds(); +} + +auto KeyTreeView::GetAllCheckedSubKey() -> QContainer<GpgSubKey> { + return model_->GetAllCheckedSubKey(); +} + +void KeyTreeView::init() { setModel(&proxy_model_); sortByColumn(2, Qt::AscendingOrder); @@ -64,29 +105,39 @@ KeyTreeView::KeyTreeView(int channel, setFocusPolicy(Qt::NoFocus); setAlternatingRowColors(true); setSortingEnabled(true); -} -void KeyTreeView::paintEvent(QPaintEvent* event) { - QTreeView::paintEvent(event); + connect(this, &QTableView::doubleClicked, this, + [this](const QModelIndex& index) { + if (!index.isValid() || index.column() == 0) return; - if (!init_) { - slot_adjust_column_widths(); - init_ = true; - } -} + QModelIndex source_index = proxy_model_.mapToSource(index); + auto key = + GetGpgKeyByGpgAbstractKey(model_->GetKeyByIndex(source_index)); -void KeyTreeView::slot_adjust_column_widths() { - for (int i = 1; i < model_->columnCount({}); ++i) { - this->resizeColumnToContents(i); - } + if (!key.IsGood()) { + QMessageBox::critical(this, tr("Error"), tr("Key Not Found.")); + return; + } + + new KeyDetailsDialog(model_->GetGpgContextChannel(), key, this); + }); } -auto KeyTreeView::GetAllCheckedKeyIds() -> KeyIdArgsList { - return model_->GetAllCheckedKeyIds(); +void KeyTreeView::SetKeyFilter(const GpgKeyTreeProxyModel::KeyFilter& filter) { + proxy_model_.SetKeyFilter(filter); } -auto KeyTreeView::GetAllCheckedSubKey() -> QContainer<GpgSubKey> { - return model_->GetAllCheckedSubKey(); +void KeyTreeView::SetChannel(int channel) { + if (channel_ == channel) return; + LOG_D() << "new channel for key tree view: " << channel; + + channel_ = channel; + init_ = false; + model_ = QSharedPointer<GpgKeyTreeModel>::create( + channel_, GpgKeyGetter::GetInstance(channel_).FetchKey(), + [](auto) { return false; }, this); + proxy_model_.setSourceModel(model_.get()); + proxy_model_.invalidate(); } } // namespace GpgFrontend::UI diff --git a/src/ui/widgets/KeyTreeView.h b/src/ui/widgets/KeyTreeView.h index a362a8a1..0e12ad0b 100644 --- a/src/ui/widgets/KeyTreeView.h +++ b/src/ui/widgets/KeyTreeView.h @@ -48,6 +48,16 @@ class KeyTreeView : public QTreeView { * @param _info_type * @param _filter */ + explicit KeyTreeView(QWidget* parent = nullptr); + + /** + * @brief Construct a new Key Table object + * + * @param _key_list + * @param _select_type + * @param _info_type + * @param _filter + */ explicit KeyTreeView(int channel, GpgKeyTreeModel::Detector checkable_detector, GpgKeyTreeProxyModel::KeyFilter filter, @@ -67,6 +77,20 @@ class KeyTreeView : public QTreeView { */ auto GetAllCheckedSubKey() -> QContainer<GpgSubKey>; + /** + * @brief Set the Key Filter object + * + * @param filter + */ + void SetKeyFilter(const GpgKeyTreeProxyModel::KeyFilter& filter); + + /** + * @brief + * + * @param channel + */ + void SetChannel(int channel); + protected: /** * @brief @@ -81,6 +105,8 @@ class KeyTreeView : public QTreeView { GpgKeyTreeProxyModel proxy_model_; void slot_adjust_column_widths(); + + void init(); }; } // namespace GpgFrontend::UI
\ No newline at end of file |