aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/dialog/key_generate
diff options
context:
space:
mode:
authorSaturn&Eric <[email protected]>2022-07-23 14:54:51 +0000
committerGitHub <[email protected]>2022-07-23 14:54:51 +0000
commitb244320b2d228189767aa6d59febceb6b64527fb (patch)
tree179d34e2a3e84231fad72c4bbe9f74673d0b74d0 /src/ui/dialog/key_generate
parentMerge pull request #68 from saturneric/develop-2.0.8 (diff)
parentfeat(project): add rpm package support (diff)
downloadGpgFrontend-2.0.9.tar.gz
GpgFrontend-2.0.9.zip
Merge pull request #70 from saturneric/dev/2.0.8/saturnericv2.0.9
Develop 2.0.9.1
Diffstat (limited to 'src/ui/dialog/key_generate')
-rw-r--r--src/ui/dialog/key_generate/KeygenDialog.cpp407
-rw-r--r--src/ui/dialog/key_generate/KeygenDialog.h184
-rw-r--r--src/ui/dialog/key_generate/SubkeyGenerateDialog.cpp344
-rw-r--r--src/ui/dialog/key_generate/SubkeyGenerateDialog.h152
4 files changed, 1087 insertions, 0 deletions
diff --git a/src/ui/dialog/key_generate/KeygenDialog.cpp b/src/ui/dialog/key_generate/KeygenDialog.cpp
new file mode 100644
index 00000000..42160ec9
--- /dev/null
+++ b/src/ui/dialog/key_generate/KeygenDialog.cpp
@@ -0,0 +1,407 @@
+/**
+ * Copyright (C) 2021 Saturneric
+ *
+ * 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 "KeygenDialog.h"
+
+#include "core/function/GlobalSettingStation.h"
+#include "core/function/gpg/GpgKeyOpera.h"
+#include "dialog/WaitingDialog.h"
+#include "ui/SignalStation.h"
+
+namespace GpgFrontend::UI {
+
+KeyGenDialog::KeyGenDialog(QWidget* parent)
+ : GeneralDialog(typeid(KeyGenDialog).name(), parent) {
+ button_box_ =
+ new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
+
+ auto& settings = GlobalSettingStation::GetInstance().GetUISettings();
+
+ // max expire date time
+ bool longer_expiration_date = false;
+ try {
+ longer_expiration_date = settings.lookup("general.longer_expiration_date");
+ LOG(INFO) << "longer_expiration_date" << longer_expiration_date;
+
+ } catch (...) {
+ LOG(ERROR) << _("Setting Operation Error") << _("longer_expiration_date");
+ }
+
+ max_date_time_ = longer_expiration_date
+ ? QDateTime::currentDateTime().toLocalTime().addYears(30)
+ : QDateTime::currentDateTime().toLocalTime().addYears(2);
+
+ this->setWindowTitle(_("Generate Key"));
+ this->setModal(true);
+
+ connect(this, &KeyGenDialog::SignalKeyGenerated, SignalStation::GetInstance(),
+ &SignalStation::SignalKeyDatabaseRefresh);
+
+ generate_key_dialog();
+}
+
+void KeyGenDialog::generate_key_dialog() {
+ key_usage_group_box_ = create_key_usage_group_box();
+
+ auto* groupGrid = new QGridLayout(this);
+ groupGrid->addWidget(create_basic_info_group_box(), 0, 0);
+ groupGrid->addWidget(key_usage_group_box_, 1, 0);
+
+ auto* nameList = new QWidget(this);
+ nameList->setLayout(groupGrid);
+
+ auto* vbox2 = new QVBoxLayout();
+ vbox2->addWidget(nameList);
+ vbox2->addWidget(error_label_);
+ vbox2->addWidget(button_box_);
+
+ this->setLayout(vbox2);
+
+ set_signal_slot();
+
+ refresh_widgets_state();
+}
+
+void KeyGenDialog::slot_key_gen_accept() {
+ std::stringstream error_stream;
+
+ /**
+ * check for errors in keygen dialog input
+ */
+ if ((name_edit_->text()).size() < 5) {
+ error_stream << " " << _("Name must contain at least five characters.")
+ << std::endl;
+ }
+ if (email_edit_->text().isEmpty() ||
+ !check_email_address(email_edit_->text())) {
+ error_stream << " " << _("Please give a email address.") << std::endl;
+ }
+
+ /**
+ * primary keys should have a reasonable expiration date (no more than 2 years
+ * in the future)
+ */
+ if (date_edit_->dateTime() > max_date_time_) {
+ error_stream << " " << _("Expiration time too long.") << std::endl;
+ }
+
+ auto err_string = error_stream.str();
+
+ if (err_string.empty()) {
+ /**
+ * create the string for key generation
+ */
+ gen_key_info_->SetName(name_edit_->text().toStdString());
+ gen_key_info_->SetEmail(email_edit_->text().toStdString());
+ gen_key_info_->SetComment(comment_edit_->text().toStdString());
+
+ gen_key_info_->SetKeyLength(key_size_spin_box_->value());
+
+ if (expire_check_box_->checkState()) {
+ gen_key_info_->SetNonExpired(true);
+ } else {
+ gen_key_info_->SetExpireTime(
+ boost::posix_time::from_time_t(date_edit_->dateTime().toTime_t()));
+ }
+
+ GpgGenKeyResult result;
+ gpgme_error_t error = false;
+ auto thread = QThread::create([&]() {
+ error = GpgKeyOpera::GetInstance().GenerateKey(gen_key_info_, result);
+ });
+ thread->start();
+
+ auto* dialog = new WaitingDialog(_("Generating"), this);
+ dialog->show();
+
+ while (thread->isRunning()) {
+ QCoreApplication::processEvents();
+ }
+
+ dialog->close();
+
+ LOG(INFO) << "generate done";
+
+ if (gpgme_err_code(error) == GPG_ERR_NO_ERROR) {
+ auto* msg_box = new QMessageBox((QWidget*)this->parent());
+ msg_box->setAttribute(Qt::WA_DeleteOnClose);
+ msg_box->setStandardButtons(QMessageBox::Ok);
+ msg_box->setWindowTitle(_("Success"));
+ msg_box->setText(_("The new key pair has been generated."));
+ msg_box->setModal(true);
+ msg_box->open();
+
+ LOG(INFO) << "generate success";
+
+ emit SignalKeyGenerated();
+ this->close();
+ } else {
+ QMessageBox::critical(this, _("Failure"), _("Key generation failed."));
+ }
+
+ } else {
+ /**
+ * create error message
+ */
+ error_label_->setAutoFillBackground(true);
+ QPalette error = error_label_->palette();
+ error.setColor(QPalette::Window, "#ff8080");
+ error_label_->setPalette(error);
+ error_label_->setText(err_string.c_str());
+
+ this->show();
+ }
+}
+
+void KeyGenDialog::slot_expire_box_changed() {
+ if (expire_check_box_->checkState()) {
+ date_edit_->setEnabled(false);
+ } else {
+ date_edit_->setEnabled(true);
+ }
+}
+
+QGroupBox* KeyGenDialog::create_key_usage_group_box() {
+ auto* groupBox = new QGroupBox(this);
+ auto* grid = new QGridLayout(this);
+
+ groupBox->setTitle(_("Key Usage"));
+
+ auto* encrypt = new QCheckBox(_("Encryption"), groupBox);
+ encrypt->setTristate(false);
+
+ auto* sign = new QCheckBox(_("Signing"), groupBox);
+ sign->setTristate(false);
+
+ auto* cert = new QCheckBox(_("Certification"), groupBox);
+ cert->setTristate(false);
+
+ auto* auth = new QCheckBox(_("Authentication"), groupBox);
+ auth->setTristate(false);
+
+ key_usage_check_boxes_.push_back(encrypt);
+ key_usage_check_boxes_.push_back(sign);
+ key_usage_check_boxes_.push_back(cert);
+ key_usage_check_boxes_.push_back(auth);
+
+ grid->addWidget(encrypt, 0, 0);
+ grid->addWidget(sign, 0, 1);
+ grid->addWidget(cert, 1, 0);
+ grid->addWidget(auth, 1, 1);
+
+ groupBox->setLayout(grid);
+
+ return groupBox;
+}
+
+void KeyGenDialog::slot_encryption_box_changed(int state) {
+ if (state == 0) {
+ gen_key_info_->SetAllowEncryption(false);
+ } else {
+ gen_key_info_->SetAllowEncryption(true);
+ }
+}
+
+void KeyGenDialog::slot_signing_box_changed(int state) {
+ if (state == 0) {
+ gen_key_info_->SetAllowSigning(false);
+ } else {
+ gen_key_info_->SetAllowSigning(true);
+ }
+}
+
+void KeyGenDialog::slot_certification_box_changed(int state) {
+ if (state == 0) {
+ gen_key_info_->SetAllowCertification(false);
+ } else {
+ gen_key_info_->SetAllowCertification(true);
+ }
+}
+
+void KeyGenDialog::slot_authentication_box_changed(int state) {
+ if (state == 0) {
+ gen_key_info_->SetAllowAuthentication(false);
+ } else {
+ gen_key_info_->SetAllowAuthentication(true);
+ }
+}
+
+void KeyGenDialog::slot_activated_key_type(int index) {
+ qDebug() << "key type index changed " << index;
+
+ gen_key_info_->SetAlgo(
+ this->key_type_combo_box_->itemText(index).toStdString());
+ refresh_widgets_state();
+}
+
+void KeyGenDialog::refresh_widgets_state() {
+ qDebug() << "refresh_widgets_state called";
+
+ if (gen_key_info_->IsAllowEncryption())
+ key_usage_check_boxes_[0]->setCheckState(Qt::CheckState::Checked);
+ else
+ key_usage_check_boxes_[0]->setCheckState(Qt::CheckState::Unchecked);
+
+ if (gen_key_info_->IsAllowChangeEncryption())
+ key_usage_check_boxes_[0]->setDisabled(false);
+ else
+ key_usage_check_boxes_[0]->setDisabled(true);
+
+ if (gen_key_info_->IsAllowSigning())
+ key_usage_check_boxes_[1]->setCheckState(Qt::CheckState::Checked);
+ else
+ key_usage_check_boxes_[1]->setCheckState(Qt::CheckState::Unchecked);
+
+ if (gen_key_info_->IsAllowChangeSigning())
+ key_usage_check_boxes_[1]->setDisabled(false);
+ else
+ key_usage_check_boxes_[1]->setDisabled(true);
+
+ if (gen_key_info_->IsAllowCertification())
+ key_usage_check_boxes_[2]->setCheckState(Qt::CheckState::Checked);
+ else
+ key_usage_check_boxes_[2]->setCheckState(Qt::CheckState::Unchecked);
+
+ if (gen_key_info_->IsAllowChangeCertification())
+ key_usage_check_boxes_[2]->setDisabled(false);
+ else
+ key_usage_check_boxes_[2]->setDisabled(true);
+
+ if (gen_key_info_->IsAllowAuthentication())
+ key_usage_check_boxes_[3]->setCheckState(Qt::CheckState::Checked);
+ else
+ key_usage_check_boxes_[3]->setCheckState(Qt::CheckState::Unchecked);
+
+ if (gen_key_info_->IsAllowChangeAuthentication())
+ key_usage_check_boxes_[3]->setDisabled(false);
+ else
+ key_usage_check_boxes_[3]->setDisabled(true);
+
+ if (gen_key_info_->IsAllowNoPassPhrase())
+ no_pass_phrase_check_box_->setDisabled(false);
+ else
+ no_pass_phrase_check_box_->setDisabled(true);
+
+ key_size_spin_box_->setRange(gen_key_info_->GetSuggestMinKeySize(),
+ gen_key_info_->GetSuggestMaxKeySize());
+ key_size_spin_box_->setValue(gen_key_info_->GetKeyLength());
+ key_size_spin_box_->setSingleStep(gen_key_info_->GetSizeChangeStep());
+}
+
+void KeyGenDialog::set_signal_slot() {
+ connect(button_box_, &QDialogButtonBox::accepted, this,
+ &KeyGenDialog::slot_key_gen_accept);
+ connect(button_box_, &QDialogButtonBox::rejected, this,
+ &KeyGenDialog::reject);
+
+ connect(expire_check_box_, &QCheckBox::stateChanged, this,
+ &KeyGenDialog::slot_expire_box_changed);
+
+ connect(key_usage_check_boxes_[0], &QCheckBox::stateChanged, this,
+ &KeyGenDialog::slot_encryption_box_changed);
+ connect(key_usage_check_boxes_[1], &QCheckBox::stateChanged, this,
+ &KeyGenDialog::slot_signing_box_changed);
+ connect(key_usage_check_boxes_[2], &QCheckBox::stateChanged, this,
+ &KeyGenDialog::slot_certification_box_changed);
+ connect(key_usage_check_boxes_[3], &QCheckBox::stateChanged, this,
+ &KeyGenDialog::slot_authentication_box_changed);
+
+ connect(key_type_combo_box_, qOverload<int>(&QComboBox::currentIndexChanged),
+ this, &KeyGenDialog::slot_activated_key_type);
+
+ connect(no_pass_phrase_check_box_, &QCheckBox::stateChanged, this,
+ [this](int state) -> void {
+ if (state == 0) {
+ gen_key_info_->SetNonPassPhrase(false);
+ } else {
+ gen_key_info_->SetNonPassPhrase(true);
+ }
+ });
+}
+
+bool KeyGenDialog::check_email_address(const QString& str) {
+ return re_email_.match(str).hasMatch();
+}
+
+QGroupBox* KeyGenDialog::create_basic_info_group_box() {
+ error_label_ = new QLabel();
+ name_edit_ = new QLineEdit(this);
+ email_edit_ = new QLineEdit(this);
+ comment_edit_ = new QLineEdit(this);
+ key_size_spin_box_ = new QSpinBox(this);
+ key_type_combo_box_ = new QComboBox(this);
+
+ for (auto& algo : GenKeyInfo::GetSupportedKeyAlgo()) {
+ key_type_combo_box_->addItem(QString::fromStdString(algo));
+ }
+ if (!GenKeyInfo::GetSupportedKeyAlgo().empty()) {
+ key_type_combo_box_->setCurrentIndex(0);
+ }
+
+ date_edit_ =
+ new QDateTimeEdit(QDateTime::currentDateTime().addYears(2), this);
+ date_edit_->setMinimumDateTime(QDateTime::currentDateTime());
+ date_edit_->setMaximumDateTime(max_date_time_);
+ date_edit_->setDisplayFormat("dd/MM/yyyy hh:mm:ss");
+ date_edit_->setCalendarPopup(true);
+ date_edit_->setEnabled(true);
+
+ expire_check_box_ = new QCheckBox(this);
+ expire_check_box_->setCheckState(Qt::Unchecked);
+
+ no_pass_phrase_check_box_ = new QCheckBox(this);
+ no_pass_phrase_check_box_->setCheckState(Qt::Unchecked);
+
+ auto* vbox1 = new QGridLayout;
+
+ vbox1->addWidget(new QLabel(QString(_("Name")) + ": "), 0, 0);
+ vbox1->addWidget(new QLabel(QString(_("Email Address")) + ": "), 1, 0);
+ vbox1->addWidget(new QLabel(QString(_("Comment")) + ": "), 2, 0);
+ vbox1->addWidget(new QLabel(QString(_("Expiration Date")) + ": "), 3, 0);
+ vbox1->addWidget(new QLabel(QString(_("Never Expire")) + ": "), 3, 3);
+ vbox1->addWidget(new QLabel(QString(_("KeySize (in Bit)")) + ": "), 4, 0);
+ vbox1->addWidget(new QLabel(QString(_("Key Type")) + ": "), 5, 0);
+ vbox1->addWidget(new QLabel(QString(_("Non Pass Phrase")) + ": "), 6, 0);
+
+ vbox1->addWidget(name_edit_, 0, 1, 1, 3);
+ vbox1->addWidget(email_edit_, 1, 1, 1, 3);
+ vbox1->addWidget(comment_edit_, 2, 1, 1, 3);
+ vbox1->addWidget(date_edit_, 3, 1);
+ vbox1->addWidget(expire_check_box_, 3, 2);
+ vbox1->addWidget(key_size_spin_box_, 4, 1);
+ vbox1->addWidget(key_type_combo_box_, 5, 1);
+ vbox1->addWidget(no_pass_phrase_check_box_, 6, 1);
+
+ auto basicInfoGroupBox = new QGroupBox();
+ basicInfoGroupBox->setLayout(vbox1);
+ basicInfoGroupBox->setTitle(_("Basic Information"));
+
+ return basicInfoGroupBox;
+}
+
+} // namespace GpgFrontend::UI
diff --git a/src/ui/dialog/key_generate/KeygenDialog.h b/src/ui/dialog/key_generate/KeygenDialog.h
new file mode 100644
index 00000000..baf10dbf
--- /dev/null
+++ b/src/ui/dialog/key_generate/KeygenDialog.h
@@ -0,0 +1,184 @@
+/**
+ * Copyright (C) 2021 Saturneric
+ *
+ * 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.
+ *
+ * The source code version of this software was modified and released
+ * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021.
+ *
+ */
+
+#ifndef __KEYGENDIALOG_H__
+#define __KEYGENDIALOG_H__
+
+#include "core/GpgContext.h"
+#include "core/GpgGenKeyInfo.h"
+#include "ui/GpgFrontendUI.h"
+#include "ui/dialog/GeneralDialog.h"
+
+namespace GpgFrontend::UI {
+
+/**
+ * @brief
+ *
+ */
+class KeyGenDialog : public GeneralDialog {
+ Q_OBJECT
+
+ public:
+ /**
+ * @details Constructor of this class
+ *
+ * @param ctx The current GpgME context
+ * @param key The key to show details of
+ * @param parent The parent of this widget
+ */
+ explicit KeyGenDialog(QWidget* parent = nullptr);
+
+ signals:
+ /**
+ * @brief
+ *
+ */
+ void SignalKeyGenerated();
+
+ private:
+ /**
+ * @brief Create a key usage group box object
+ *
+ * @return QGroupBox*
+ */
+ QGroupBox* create_key_usage_group_box();
+
+ /**
+ * @brief Create a basic info group box object
+ *
+ * @return QGroupBox*
+ */
+ QGroupBox* create_basic_info_group_box();
+
+ /**
+ * @brief
+ *
+ */
+ QRegularExpression re_email_{
+ R"((?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]))"};
+
+ /**
+ * @brief
+ *
+ */
+ QStringList error_messages_; ///< List of errors occurring when checking
+ ///< entries of line edits
+ std::unique_ptr<GenKeyInfo> gen_key_info_ =
+ std::make_unique<GenKeyInfo>(); ///<
+ QDialogButtonBox* button_box_; ///< Box for standard buttons
+ QLabel* error_label_{}; ///< Label containing error message
+ QLineEdit* name_edit_{}; ///< Line edit for the keys name
+ QLineEdit* email_edit_{}; ///< Line edit for the keys email
+ QLineEdit* comment_edit_{}; ///< Line edit for the keys comment
+ QSpinBox* key_size_spin_box_{}; ///< Spinbox for the keys size (in bit)
+ QComboBox* key_type_combo_box_{}; ///< Combobox for Key type
+ QDateTimeEdit* date_edit_{}; ///< Date edit for expiration date
+ QCheckBox* expire_check_box_{}; ///< Checkbox, if key should expire
+ QCheckBox* no_pass_phrase_check_box_{};
+ QGroupBox* key_usage_group_box_{}; ///< Group of Widgets detecting the usage
+ ///< of the Key
+ QDateTime max_date_time_; ///<
+ std::vector<QCheckBox*> key_usage_check_boxes_; ///< ENCR, SIGN, CERT, AUTH
+
+ /**
+ * @brief
+ *
+ */
+ void generate_key_dialog();
+
+ /**
+ * @details Refresh widgets state by GenKeyInfo
+ */
+ void refresh_widgets_state();
+
+ /**
+ * @brief Set the signal slot object
+ *
+ */
+ void set_signal_slot();
+
+ /**
+ * @brief
+ *
+ * @param str
+ * @return true
+ * @return false
+ */
+ bool check_email_address(const QString& str);
+
+ private slots:
+
+ /**
+ * @details when expirebox was checked/unchecked, enable/disable the
+ * expiration date box
+ */
+ void slot_expire_box_changed();
+
+ /**
+ * @details check all lineedits for false entries. Show error, when there is
+ * one, otherwise generate the key
+ */
+ void slot_key_gen_accept();
+
+ /**
+ * @brief
+ *
+ * @param state
+ */
+ void slot_encryption_box_changed(int state);
+
+ /**
+ * @brief
+ *
+ * @param state
+ */
+ void slot_signing_box_changed(int state);
+
+ /**
+ * @brief
+ *
+ * @param state
+ */
+ void slot_certification_box_changed(int state);
+
+ /**
+ * @brief
+ *
+ * @param state
+ */
+ void slot_authentication_box_changed(int state);
+
+ /**
+ * @brief
+ *
+ * @param index
+ */
+ void slot_activated_key_type(int index);
+};
+
+} // namespace GpgFrontend::UI
+
+#endif // __KEYGENDIALOG_H__
diff --git a/src/ui/dialog/key_generate/SubkeyGenerateDialog.cpp b/src/ui/dialog/key_generate/SubkeyGenerateDialog.cpp
new file mode 100644
index 00000000..806c0e50
--- /dev/null
+++ b/src/ui/dialog/key_generate/SubkeyGenerateDialog.cpp
@@ -0,0 +1,344 @@
+/**
+ * Copyright (C) 2021 Saturneric
+ *
+ * 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.
+ *
+ * The source code version of this software was modified and released
+ * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021.
+ *
+ */
+
+#include "SubkeyGenerateDialog.h"
+
+#include "core/function/GlobalSettingStation.h"
+#include "core/function/gpg/GpgKeyGetter.h"
+#include "core/function/gpg/GpgKeyOpera.h"
+#include "dialog/WaitingDialog.h"
+#include "ui/SignalStation.h"
+
+namespace GpgFrontend::UI {
+
+SubkeyGenerateDialog::SubkeyGenerateDialog(const KeyId& key_id, QWidget* parent)
+ : GeneralDialog(typeid(SubkeyGenerateDialog).name(), parent),
+ key_(GpgKeyGetter::GetInstance().GetKey(key_id)) {
+ auto& settings = GlobalSettingStation::GetInstance().GetUISettings();
+
+ // max expire date time
+ bool longer_expiration_date = false;
+ try {
+ longer_expiration_date = settings.lookup("general.longer_expiration_date");
+ LOG(INFO) << "longer_expiration_date" << longer_expiration_date;
+
+ } catch (...) {
+ LOG(ERROR) << _("Setting Operation Error") << _("longer_expiration_date");
+ }
+
+ max_date_time_ = longer_expiration_date
+ ? QDateTime::currentDateTime().toLocalTime().addYears(30)
+ : QDateTime::currentDateTime().toLocalTime().addYears(2);
+
+ button_box_ =
+ new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
+
+ key_usage_group_box_ = create_key_usage_group_box();
+
+ auto* groupGrid = new QGridLayout(this);
+ groupGrid->addWidget(create_basic_info_group_box(), 0, 0);
+ groupGrid->addWidget(key_usage_group_box_, 1, 0);
+
+ auto* nameList = new QWidget(this);
+ nameList->setLayout(groupGrid);
+
+ auto* vbox2 = new QVBoxLayout();
+ vbox2->addWidget(nameList);
+ vbox2->addWidget(error_label_);
+ vbox2->addWidget(button_box_);
+
+ this->setWindowTitle(_("Generate New Subkey"));
+ this->setLayout(vbox2);
+ this->setModal(true);
+
+ connect(this, &SubkeyGenerateDialog::SignalSubKeyGenerated,
+ SignalStation::GetInstance(),
+ &SignalStation::SignalKeyDatabaseRefresh);
+
+ set_signal_slot();
+ refresh_widgets_state();
+}
+
+QGroupBox* SubkeyGenerateDialog::create_key_usage_group_box() {
+ auto* groupBox = new QGroupBox(this);
+ auto* grid = new QGridLayout(this);
+
+ groupBox->setTitle(_("Key Usage"));
+
+ auto* encrypt = new QCheckBox(_("Encryption"), groupBox);
+ encrypt->setTristate(false);
+
+ auto* sign = new QCheckBox(_("Signing"), groupBox);
+ sign->setTristate(false);
+
+ auto* cert = new QCheckBox(_("Certification"), groupBox);
+ cert->setTristate(false);
+
+ auto* auth = new QCheckBox(_("Authentication"), groupBox);
+ auth->setTristate(false);
+
+ key_usage_check_boxes_.push_back(encrypt);
+ key_usage_check_boxes_.push_back(sign);
+ key_usage_check_boxes_.push_back(cert);
+ key_usage_check_boxes_.push_back(auth);
+
+ grid->addWidget(encrypt, 0, 0);
+ grid->addWidget(sign, 0, 1);
+ grid->addWidget(cert, 1, 0);
+ grid->addWidget(auth, 1, 1);
+
+ groupBox->setLayout(grid);
+
+ return groupBox;
+}
+
+QGroupBox* SubkeyGenerateDialog::create_basic_info_group_box() {
+ error_label_ = new QLabel();
+ key_size_spin_box_ = new QSpinBox(this);
+ key_type_combo_box_ = new QComboBox(this);
+
+ for (auto& algo : GenKeyInfo::GetSupportedKeyAlgo()) {
+ key_type_combo_box_->addItem(QString::fromStdString(algo));
+ }
+ if (!GenKeyInfo::GetSupportedKeyAlgo().empty()) {
+ key_type_combo_box_->setCurrentIndex(0);
+ }
+
+ date_edit_ =
+ new QDateTimeEdit(QDateTime::currentDateTime().addYears(2), this);
+ date_edit_->setMinimumDateTime(QDateTime::currentDateTime());
+ date_edit_->setMaximumDateTime(max_date_time_);
+ date_edit_->setDisplayFormat("dd/MM/yyyy hh:mm:ss");
+ date_edit_->setCalendarPopup(true);
+ date_edit_->setEnabled(true);
+
+ expire_check_box_ = new QCheckBox(this);
+ expire_check_box_->setCheckState(Qt::Unchecked);
+
+ auto* vbox1 = new QGridLayout;
+
+ vbox1->addWidget(new QLabel(QString(_("Expiration Date")) + ": "), 2, 0);
+ vbox1->addWidget(new QLabel(QString(_("Never Expire")) + ": "), 2, 3);
+ vbox1->addWidget(new QLabel(QString(_("KeySize (in Bit)")) + ": "), 1, 0);
+ vbox1->addWidget(new QLabel(QString(_("Key Type")) + ": "), 0, 0);
+
+ vbox1->addWidget(date_edit_, 2, 1);
+ vbox1->addWidget(expire_check_box_, 2, 2);
+ vbox1->addWidget(key_size_spin_box_, 1, 1);
+ vbox1->addWidget(key_type_combo_box_, 0, 1);
+
+ auto basicInfoGroupBox = new QGroupBox();
+ basicInfoGroupBox->setLayout(vbox1);
+ basicInfoGroupBox->setTitle(_("Basic Information"));
+
+ return basicInfoGroupBox;
+}
+
+void SubkeyGenerateDialog::set_signal_slot() {
+ connect(button_box_, &QDialogButtonBox::accepted, this,
+ &SubkeyGenerateDialog::slot_key_gen_accept);
+ connect(button_box_, &QDialogButtonBox::rejected, this,
+ &SubkeyGenerateDialog::reject);
+
+ connect(expire_check_box_, &QCheckBox::stateChanged, this,
+ &SubkeyGenerateDialog::slot_expire_box_changed);
+
+ connect(key_usage_check_boxes_[0], &QCheckBox::stateChanged, this,
+ &SubkeyGenerateDialog::slot_encryption_box_changed);
+ connect(key_usage_check_boxes_[1], &QCheckBox::stateChanged, this,
+ &SubkeyGenerateDialog::slot_signing_box_changed);
+ connect(key_usage_check_boxes_[2], &QCheckBox::stateChanged, this,
+ &SubkeyGenerateDialog::slot_certification_box_changed);
+ connect(key_usage_check_boxes_[3], &QCheckBox::stateChanged, this,
+ &SubkeyGenerateDialog::slot_authentication_box_changed);
+
+ connect(key_type_combo_box_, qOverload<int>(&QComboBox::currentIndexChanged),
+ this, &SubkeyGenerateDialog::slot_activated_key_type);
+}
+
+void SubkeyGenerateDialog::slot_expire_box_changed() {
+ if (expire_check_box_->checkState()) {
+ date_edit_->setEnabled(false);
+ } else {
+ date_edit_->setEnabled(true);
+ }
+}
+
+void SubkeyGenerateDialog::refresh_widgets_state() {
+ qDebug() << "refresh_widgets_state called";
+
+ if (gen_key_info_->IsAllowEncryption())
+ key_usage_check_boxes_[0]->setCheckState(Qt::CheckState::Checked);
+ else
+ key_usage_check_boxes_[0]->setCheckState(Qt::CheckState::Unchecked);
+
+ if (gen_key_info_->IsAllowChangeEncryption())
+ key_usage_check_boxes_[0]->setDisabled(false);
+ else
+ key_usage_check_boxes_[0]->setDisabled(true);
+
+ if (gen_key_info_->IsAllowSigning())
+ key_usage_check_boxes_[1]->setCheckState(Qt::CheckState::Checked);
+ else
+ key_usage_check_boxes_[1]->setCheckState(Qt::CheckState::Unchecked);
+
+ if (gen_key_info_->IsAllowChangeSigning())
+ key_usage_check_boxes_[1]->setDisabled(false);
+ else
+ key_usage_check_boxes_[1]->setDisabled(true);
+
+ if (gen_key_info_->IsAllowCertification())
+ key_usage_check_boxes_[2]->setCheckState(Qt::CheckState::Checked);
+ else
+ key_usage_check_boxes_[2]->setCheckState(Qt::CheckState::Unchecked);
+
+ if (gen_key_info_->IsAllowChangeCertification())
+ key_usage_check_boxes_[2]->setDisabled(false);
+ else
+ key_usage_check_boxes_[2]->setDisabled(true);
+
+ if (gen_key_info_->IsAllowAuthentication())
+ key_usage_check_boxes_[3]->setCheckState(Qt::CheckState::Checked);
+ else
+ key_usage_check_boxes_[3]->setCheckState(Qt::CheckState::Unchecked);
+
+ if (gen_key_info_->IsAllowChangeAuthentication())
+ key_usage_check_boxes_[3]->setDisabled(false);
+ else
+ key_usage_check_boxes_[3]->setDisabled(true);
+
+ key_size_spin_box_->setRange(gen_key_info_->GetSuggestMinKeySize(),
+ gen_key_info_->GetSuggestMaxKeySize());
+ key_size_spin_box_->setValue(gen_key_info_->GetKeyLength());
+ key_size_spin_box_->setSingleStep(gen_key_info_->GetSizeChangeStep());
+}
+
+void SubkeyGenerateDialog::slot_key_gen_accept() {
+ std::stringstream err_stream;
+
+ /**
+ * primary keys should have a reasonable expiration date (no more than 2 years
+ * in the future)
+ */
+ if (date_edit_->dateTime() > QDateTime::currentDateTime().addYears(2)) {
+ err_stream << " " << _("Expiration time no more than 2 years.") << " ";
+ }
+
+ auto err_string = err_stream.str();
+
+ if (err_string.empty()) {
+ gen_key_info_->SetKeyLength(key_size_spin_box_->value());
+
+ if (expire_check_box_->checkState()) {
+ gen_key_info_->SetNonExpired(true);
+ } else {
+ gen_key_info_->SetExpireTime(
+ boost::posix_time::from_time_t(date_edit_->dateTime().toTime_t()));
+ }
+
+ GpgError error;
+ auto thread = QThread::create([&]() {
+ LOG(INFO) << "Thread Started";
+ error = GpgKeyOpera::GetInstance().GenerateSubkey(key_, gen_key_info_);
+ });
+ thread->start();
+
+ auto* dialog = new WaitingDialog(_("Generating"), this);
+ dialog->show();
+
+ while (thread->isRunning()) {
+ QCoreApplication::processEvents();
+ }
+ dialog->close();
+
+ if (check_gpg_error_2_err_code(error) == GPG_ERR_NO_ERROR) {
+ auto* msg_box = new QMessageBox((QWidget*)this->parent());
+ msg_box->setAttribute(Qt::WA_DeleteOnClose);
+ msg_box->setStandardButtons(QMessageBox::Ok);
+ msg_box->setWindowTitle(_("Success"));
+ msg_box->setText(_("The new subkey has been generated."));
+ msg_box->setModal(true);
+ msg_box->open();
+
+ emit SignalSubKeyGenerated();
+ this->close();
+ } else
+ QMessageBox::critical(this, _("Failure"), _("Failed to generate key."));
+
+ } else {
+ /**
+ * create error message
+ */
+ error_label_->setAutoFillBackground(true);
+ QPalette error = error_label_->palette();
+ error.setColor(QPalette::Window, "#ff8080");
+ error_label_->setPalette(error);
+ error_label_->setText(err_string.c_str());
+
+ this->show();
+ }
+}
+
+void SubkeyGenerateDialog::slot_encryption_box_changed(int state) {
+ if (state == 0) {
+ gen_key_info_->SetAllowEncryption(false);
+ } else {
+ gen_key_info_->SetAllowEncryption(true);
+ }
+}
+
+void SubkeyGenerateDialog::slot_signing_box_changed(int state) {
+ if (state == 0) {
+ gen_key_info_->SetAllowSigning(false);
+ } else {
+ gen_key_info_->SetAllowSigning(true);
+ }
+}
+
+void SubkeyGenerateDialog::slot_certification_box_changed(int state) {
+ if (state == 0) {
+ gen_key_info_->SetAllowCertification(false);
+ } else {
+ gen_key_info_->SetAllowCertification(true);
+ }
+}
+
+void SubkeyGenerateDialog::slot_authentication_box_changed(int state) {
+ if (state == 0) {
+ gen_key_info_->SetAllowAuthentication(false);
+ } else {
+ gen_key_info_->SetAllowAuthentication(true);
+ }
+}
+
+void SubkeyGenerateDialog::slot_activated_key_type(int index) {
+ qDebug() << "key type index changed " << index;
+ gen_key_info_->SetAlgo(
+ this->key_type_combo_box_->itemText(index).toStdString());
+ refresh_widgets_state();
+}
+
+} // namespace GpgFrontend::UI
diff --git a/src/ui/dialog/key_generate/SubkeyGenerateDialog.h b/src/ui/dialog/key_generate/SubkeyGenerateDialog.h
new file mode 100644
index 00000000..1e6608b2
--- /dev/null
+++ b/src/ui/dialog/key_generate/SubkeyGenerateDialog.h
@@ -0,0 +1,152 @@
+/**
+ * Copyright (C) 2021 Saturneric
+ *
+ * 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.
+ *
+ * The source code version of this software was modified and released
+ * by Saturneric<[email protected]><[email protected]> starting on May 12, 2021.
+ *
+ */
+
+#ifndef GPGFRONTEND_SUBKEYGENERATEDIALOG_H
+#define GPGFRONTEND_SUBKEYGENERATEDIALOG_H
+
+#include "core/GpgContext.h"
+#include "core/GpgGenKeyInfo.h"
+#include "ui/GpgFrontendUI.h"
+#include "ui/dialog/GeneralDialog.h"
+
+namespace GpgFrontend::UI {
+/**
+ * @brief
+ *
+ */
+class SubkeyGenerateDialog : public GeneralDialog {
+ Q_OBJECT
+
+ public:
+ /**
+ * @brief Construct a new Subkey Generate Dialog object
+ *
+ * @param key_id
+ * @param parent
+ */
+ explicit SubkeyGenerateDialog(const KeyId& key_id, QWidget* parent);
+
+ signals:
+ /**
+ * @brief
+ *
+ */
+ void SignalSubKeyGenerated();
+
+ private:
+ GpgKey key_; ///<
+
+ std::unique_ptr<GenKeyInfo> gen_key_info_ =
+ std::make_unique<GenKeyInfo>(true); ///<
+
+ QGroupBox* key_usage_group_box_{};
+ QDialogButtonBox* button_box_; ///< Box for standard buttons
+ QLabel* error_label_{}; ///< Label containing error message
+ QSpinBox* key_size_spin_box_{}; ///< Spinbox for the keys size (in bit)
+ QComboBox* key_type_combo_box_{}; ///< Combobox for Key tpe
+ QDateTimeEdit* date_edit_{}; ///< Date edit for expiration date
+ QCheckBox* expire_check_box_{}; ///< Checkbox, if key should expire
+
+ std::vector<QCheckBox*> key_usage_check_boxes_; ///< ENCR, SIGN, CERT, AUTH
+ QDateTime max_date_time_; ///<
+
+ /**
+ * @brief Create a key usage group box object
+ *
+ * @return QGroupBox*
+ */
+ QGroupBox* create_key_usage_group_box();
+
+ /**
+ * @brief Create a basic info group box object
+ *
+ * @return QGroupBox*
+ */
+ QGroupBox* create_basic_info_group_box();
+ /**
+ * @brief Set the signal slot object
+ *
+ */
+ void set_signal_slot();
+
+ /**
+ * @details Refresh widgets state by GenKeyInfo
+ */
+ void refresh_widgets_state();
+
+ private slots:
+
+ /**
+ * @details when expire box was checked/unchecked, enable/disable the
+ * expiration date box
+ */
+ void slot_expire_box_changed();
+
+ /**
+ * @details check all line edits for false entries. Show error, when there is
+ * one, otherwise generate the key
+ */
+ void slot_key_gen_accept();
+
+ /**
+ * @brief
+ *
+ * @param state
+ */
+ void slot_encryption_box_changed(int state);
+
+ /**
+ * @brief
+ *
+ * @param state
+ */
+ void slot_signing_box_changed(int state);
+
+ /**
+ * @brief
+ *
+ * @param state
+ */
+ void slot_certification_box_changed(int state);
+
+ /**
+ * @brief
+ *
+ * @param state
+ */
+ void slot_authentication_box_changed(int state);
+
+ /**
+ * @brief
+ *
+ * @param index
+ */
+ void slot_activated_key_type(int index);
+};
+
+} // namespace GpgFrontend::UI
+
+#endif // GPGFRONTEND_SUBKEYGENERATEDIALOG_H