aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/ExportKeyPackageDialog.cpp
diff options
context:
space:
mode:
authorSaturneric <[email protected]>2022-01-03 17:25:59 +0000
committerSaturneric <[email protected]>2022-01-03 17:25:59 +0000
commitc20c7b9bb81a09d54b288517092fe356bb808de4 (patch)
tree625db0ac06fdf95d5e886ab558e9d240f47730d1 /src/ui/widgets/ExportKeyPackageDialog.cpp
parent<fixed, refactor>(ui): Enhanced sending email function. (diff)
downloadGpgFrontend-c20c7b9bb81a09d54b288517092fe356bb808de4.tar.gz
GpgFrontend-c20c7b9bb81a09d54b288517092fe356bb808de4.zip
<feature, refactor>(core, ui): Key package generate.
1. refactor GpgKeyImportExporter to meet with key package generation. 2. add ExportKeyPackageDialog. 3. add Qt AES ability. 4. refactor Key List to provide menu ability control. 5. add check all and uncheck all ability to key list menu.
Diffstat (limited to 'src/ui/widgets/ExportKeyPackageDialog.cpp')
-rw-r--r--src/ui/widgets/ExportKeyPackageDialog.cpp165
1 files changed, 165 insertions, 0 deletions
diff --git a/src/ui/widgets/ExportKeyPackageDialog.cpp b/src/ui/widgets/ExportKeyPackageDialog.cpp
new file mode 100644
index 00000000..b5eea82d
--- /dev/null
+++ b/src/ui/widgets/ExportKeyPackageDialog.cpp
@@ -0,0 +1,165 @@
+/**
+ * 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.
+ *
+ * Foobar 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 Foobar. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The initial version of the source code is inherited from gpg4usb-team.
+ * Their source code version also complies with GNU General Public License.
+ *
+ * The source code version of this software was modified and released
+ * by Saturneric<[email protected]> starting on May 12, 2021.
+ *
+ */
+
+#include "ExportKeyPackageDialog.h"
+
+#include <boost/format.hpp>
+
+#include "gpg/function/GpgKeyGetter.h"
+#include "gpg/function/GpgKeyImportExporter.h"
+#include "ui/aes/qaesencryption.h"
+#include "ui_ExportKeyPackageDialog.h"
+
+GpgFrontend::UI::ExportKeyPackageDialog::ExportKeyPackageDialog(
+ KeyIdArgsListPtr key_ids, QWidget* parent)
+ : QDialog(parent),
+ ui(std::make_shared<Ui_exportKeyPackageDialog>()),
+ key_ids_(std::move(key_ids)),
+ mt(rd()) {
+ ui->setupUi(this);
+
+ generate_key_package_name();
+
+ connect(ui->gnerateNameButton, &QPushButton::clicked, this,
+ [=]() { generate_key_package_name(); });
+
+ connect(ui->setOutputPathButton, &QPushButton::clicked, this, [=]() {
+ auto file_name = QFileDialog::getSaveFileName(
+ this, _("Export Key Package"), ui->nameValueLabel->text() + ".gfepack",
+ QString(_("Key Package")) + " (*.gfepack);;All Files (*)");
+ ui->outputPathLabel->setText(file_name);
+ });
+
+ connect(ui->generatePassphraseButton, &QPushButton::clicked, this, [=]() {
+ passphrase_ = generate_passphrase(32);
+ auto file_name = QFileDialog::getSaveFileName(
+ this, _("Export Key Package Passphrase"),
+ ui->nameValueLabel->text() + ".key",
+ QString(_("Key File")) + " (*.key);;All Files (*)");
+ ui->passphraseValueLabel->setText(file_name);
+ write_buffer_to_file(file_name.toStdString(), passphrase_);
+ });
+
+ connect(ui->buttonBox, &QDialogButtonBox::accepted, this, [=]() {
+ if (ui->outputPathLabel->text().isEmpty()) {
+ QMessageBox::critical(
+ nullptr, _("Forbidden"),
+ _("Please select an output path before exporting."));
+ return;
+ }
+
+ if (ui->passphraseValueLabel->text().isEmpty()) {
+ QMessageBox::critical(
+ nullptr, _("Forbidden"),
+ _("Please generate a password to protect your key before exporting, "
+ "it is very important. Don't forget to back up your password in a "
+ "safe place."));
+ return;
+ }
+ auto key_id_exported = std::make_unique<KeyIdArgsList>();
+ auto keys = GpgKeyGetter::GetInstance().GetKeys(key_ids_);
+ for (const auto& key : *keys) {
+ if (ui->noPublicKeyCheckBox->isChecked() && !key.is_private_key()) {
+ continue;
+ }
+ key_id_exported->push_back(key.id());
+ }
+
+ ByteArrayPtr key_export_data = nullptr;
+ if (!GpgKeyImportExporter::GetInstance().ExportKeys(
+ key_ids_, key_export_data,
+ ui->includeSecretKeyCheckBox->isChecked())) {
+ QMessageBox::critical(nullptr, _("Error"), _("Export Key(s) Failed."));
+ this->close();
+ return;
+ }
+
+ auto key = passphrase_;
+
+ QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::ECB);
+ auto encoded = encryption.encode(key_export_data->data(), key.data());
+
+ write_buffer_to_file(ui->outputPathLabel->text().toStdString(),
+ encoded.toStdString());
+
+ QMessageBox::information(
+ this, _("Success"),
+ QString(_(
+ "The Key Package has been successfully generated and has been "
+ "protected by encryption algorithms. You can safely transfer your "
+ "Key Package.")) +
+ "<b>" +
+ _("But the key file cannot be leaked under any "
+ "circumstances. Please delete the Key Package and key file as "
+ "soon "
+ "as possible after completing the transfer operation") +
+ "</b>");
+ });
+
+ connect(ui->buttonBox, &QDialogButtonBox::rejected, this,
+ [=]() { this->close(); });
+
+ ui->nameLabel->setText(_("Key Package Name"));
+ ui->selectOutputPathLabel->setText(_("Output Path"));
+ ui->passphraseLabel->setText(_("Passphrase"));
+ ui->tipsLabel->setText(
+ _("Tips: You can use Key Package to safely and conveniently transfer "
+ "your public and private keys between devices."));
+ ui->generatePassphraseButton->setText(_("Generate and Save Passphrase"));
+ ui->gnerateNameButton->setText(_("Generate Key Package Name"));
+ ui->setOutputPathButton->setText(_("Select Output Path"));
+
+ ui->includeSecretKeyCheckBox->setText(
+ _("Include secret key (Think twice before acting)"));
+ ui->noPublicKeyCheckBox->setText(
+ _("Exclude keys that do not have a private key"));
+
+ setAttribute(Qt::WA_DeleteOnClose);
+ setWindowTitle(_("exportKeyPackageDialog"));
+}
+
+std::string GpgFrontend::UI::ExportKeyPackageDialog::generate_passphrase(
+ const int len) {
+ std::uniform_int_distribution<int> dist(999, 99999);
+ static const char alphanum[] =
+ "0123456789"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz";
+ std::string tmp_str;
+ tmp_str.reserve(len);
+
+ for (int i = 0; i < len; ++i) {
+ tmp_str += alphanum[dist(mt) % (sizeof(alphanum) - 1)];
+ }
+
+ return tmp_str;
+}
+
+void GpgFrontend::UI::ExportKeyPackageDialog::generate_key_package_name() {
+ std::uniform_int_distribution<int> dist(999, 99999);
+ auto file_string = boost::format("KeyPackage_%1%") % dist(mt);
+ ui->nameValueLabel->setText(file_string.str().c_str());
+ ui->outputPathLabel->clear();
+ ui->passphraseValueLabel->clear();
+}