diff options
Diffstat (limited to '')
-rw-r--r-- | src/ui/keypair_details/KeyPairDetailTab.cpp | 346 |
1 files changed, 215 insertions, 131 deletions
diff --git a/src/ui/keypair_details/KeyPairDetailTab.cpp b/src/ui/keypair_details/KeyPairDetailTab.cpp index f88e9edc..de16ccf7 100644 --- a/src/ui/keypair_details/KeyPairDetailTab.cpp +++ b/src/ui/keypair_details/KeyPairDetailTab.cpp @@ -26,6 +26,7 @@ #include "gpg/function/GpgKeyGetter.h" #include "gpg/function/GpgKeyImportExportor.h" +#include "gpg/function/GpgKeyOpera.h" #include "ui/SignalStation.h" #include "ui/UserInterfaceUtils.h" #include "ui/WaitingDialog.h" @@ -33,8 +34,6 @@ namespace GpgFrontend::UI { KeyPairDetailTab::KeyPairDetailTab(const std::string& key_id, QWidget* parent) : QWidget(parent), mKey(GpgKeyGetter::GetInstance().GetKey(key_id)) { - keyid = mKey.id(); - ownerBox = new QGroupBox(_("Owner")); keyBox = new QGroupBox(_("Master Key")); fingerprintBox = new QGroupBox(_("Fingerprint")); @@ -56,31 +55,9 @@ KeyPairDetailTab::KeyPairDetailTab(const std::string& key_id, QWidget* parent) keySizeVarLabel = new QLabel(); expireVarLabel = new QLabel(); createdVarLabel = new QLabel(); + lastUpdateVarLabel = new QLabel(); algorithmVarLabel = new QLabel(); - - // Show the situation that master key not exists. - masterKeyExistVarLabel = - new QLabel(mKey.has_master_key() ? _("Exists") : _("Not Exists")); - if (!mKey.has_master_key()) { - auto paletteExpired = masterKeyExistVarLabel->palette(); - paletteExpired.setColor(masterKeyExistVarLabel->foregroundRole(), Qt::red); - masterKeyExistVarLabel->setPalette(paletteExpired); - } else { - auto paletteValid = masterKeyExistVarLabel->palette(); - paletteValid.setColor(masterKeyExistVarLabel->foregroundRole(), - Qt::darkGreen); - masterKeyExistVarLabel->setPalette(paletteValid); - } - - if (mKey.expired()) { - auto paletteExpired = expireVarLabel->palette(); - paletteExpired.setColor(expireVarLabel->foregroundRole(), Qt::red); - expireVarLabel->setPalette(paletteExpired); - } else { - auto paletteValid = expireVarLabel->palette(); - paletteValid.setColor(expireVarLabel->foregroundRole(), Qt::darkGreen); - expireVarLabel->setPalette(paletteValid); - } + masterKeyExistVarLabel = new QLabel(); auto* mvbox = new QVBoxLayout(); auto* vboxKD = new QGridLayout(); @@ -98,31 +75,35 @@ KeyPairDetailTab::KeyPairDetailTab(const std::string& key_id, QWidget* parent) vboxKD->addWidget(new QLabel(QString(_("Key Size")) + ": "), 2, 0); vboxKD->addWidget(new QLabel(QString(_("Nominal Usage")) + ": "), 3, 0); vboxKD->addWidget(new QLabel(QString(_("Actual Usage")) + ": "), 4, 0); - vboxKD->addWidget(new QLabel(QString(_("Expires on")) + ": "), 5, 0); - vboxKD->addWidget(new QLabel(QString(_("Last Update")) + ": "), 6, 0); - vboxKD->addWidget(new QLabel(QString(_("Secret Key Existence")) + ": "), 7, + vboxKD->addWidget(new QLabel(QString(_("Create Date")) + ": "), 5, 0); + vboxKD->addWidget(new QLabel(QString(_("Expires on")) + ": "), 6, 0); + vboxKD->addWidget(new QLabel(QString(_("Last Update")) + ": "), 7, 0); + vboxKD->addWidget(new QLabel(QString(_("Master Key Existence")) + ": "), 8, 0); - vboxKD->addWidget(keySizeVarLabel, 2, 1); - vboxKD->addWidget(expireVarLabel, 5, 1); - vboxKD->addWidget(algorithmVarLabel, 1, 1); - vboxKD->addWidget(createdVarLabel, 6, 1); vboxKD->addWidget(keyidVarLabel, 0, 1); + vboxKD->addWidget(algorithmVarLabel, 1, 1); + vboxKD->addWidget(keySizeVarLabel, 2, 1); vboxKD->addWidget(usageVarLabel, 3, 1); vboxKD->addWidget(actualUsageVarLabel, 4, 1); - vboxKD->addWidget(masterKeyExistVarLabel, 7, 1); + vboxKD->addWidget(createdVarLabel, 5, 1); + vboxKD->addWidget(expireVarLabel, 6, 1); + vboxKD->addWidget(lastUpdateVarLabel, 7, 1); + vboxKD->addWidget(masterKeyExistVarLabel, 8, 1); ownerBox->setLayout(vboxOD); mvbox->addWidget(ownerBox); keyBox->setLayout(vboxKD); mvbox->addWidget(keyBox); - fingerPrintVarLabel = - new QLabel(beautifyFingerprint(QString::fromStdString(mKey.fpr()))); + fingerPrintVarLabel = new QLabel(); + fingerPrintVarLabel->setWordWrap(false); fingerPrintVarLabel->setTextInteractionFlags(Qt::TextSelectableByMouse); fingerPrintVarLabel->setStyleSheet("margin-left: 0; margin-right: 5;"); + fingerPrintVarLabel->setAlignment(Qt::AlignCenter); auto* hboxFP = new QHBoxLayout(); + hboxFP->addStretch(); hboxFP->addWidget(fingerPrintVarLabel); auto* copyFingerprintButton = new QPushButton(_("Copy")); @@ -132,76 +113,87 @@ KeyPairDetailTab::KeyPairDetailTab(const std::string& key_id, QWidget* parent) SLOT(slotCopyFingerprint())); hboxFP->addWidget(copyFingerprintButton); + hboxFP->addStretch(); fingerprintBox->setLayout(hboxFP); - mvbox->addWidget(fingerprintBox); mvbox->addStretch(); + mvbox->addWidget(fingerprintBox); - if (mKey.is_private_key()) { - auto* privKeyBox = new QGroupBox(_("Operations")); - auto* vboxPK = new QVBoxLayout(); + auto* opera_key_box = new QGroupBox(_("Operations")); + auto* vbox_p_k = new QVBoxLayout(); - auto* exportButton = + auto export_h_box_layout = new QHBoxLayout(); + vbox_p_k->addLayout(export_h_box_layout); + + auto* export_public_button = new QPushButton(_("Export Public Key")); + export_h_box_layout->addWidget(export_public_button); + connect(export_public_button, SIGNAL(clicked()), this, + SLOT(slotExportPublicKey())); + + if (mKey.is_private_key()) { + auto* export_private_button = new QPushButton(_("Export Private Key (Include Subkey)")); - vboxPK->addWidget(exportButton); - connect(exportButton, SIGNAL(clicked()), this, + connect(export_private_button, SIGNAL(clicked()), this, SLOT(slotExportPrivateKey())); + export_h_box_layout->addWidget(export_private_button); if (mKey.has_master_key()) { - auto* editExpiresButton = + auto* edit_expires_button = new QPushButton(_("Modify Expiration Datetime (Master Key)")); - vboxPK->addWidget(editExpiresButton); - connect(editExpiresButton, SIGNAL(clicked()), this, + connect(edit_expires_button, SIGNAL(clicked()), this, SLOT(slotModifyEditDatetime())); - - auto hBoxLayout = new QHBoxLayout(); - auto* keyServerOperaButton = - new QPushButton(_("Key Server Operation (Pubkey)")); - keyServerOperaButton->setStyleSheet("text-align:center;"); - - auto* revokeCertGenButton = - new QPushButton(_("Generate Revoke Certificate")); - connect(revokeCertGenButton, SIGNAL(clicked()), this, - SLOT(slotGenRevokeCert())); - - hBoxLayout->addWidget(keyServerOperaButton); - hBoxLayout->addWidget(revokeCertGenButton); - - vboxPK->addLayout(hBoxLayout); - connect(keyServerOperaButton, SIGNAL(clicked()), this, - SLOT(slotModifyEditDatetime())); - - // Set Menu - createKeyServerOperaMenu(); - keyServerOperaButton->setMenu(keyServerOperaMenu); + auto* edit_password_button = new QPushButton(_("Modify Password")); + connect(edit_password_button, SIGNAL(clicked()), this, + SLOT(slotModifyPassword())); + + auto edit_h_box_layout = new QHBoxLayout(); + edit_h_box_layout->addWidget(edit_expires_button); + edit_h_box_layout->addWidget(edit_password_button); + vbox_p_k->addLayout(edit_h_box_layout); } + } - privKeyBox->setLayout(vboxPK); - mvbox->addWidget(privKeyBox); + auto advance_h_box_layout = new QHBoxLayout(); + auto* key_server_opera_button = + new QPushButton(_("Key Server Operation (Pubkey)")); + key_server_opera_button->setStyleSheet("text-align:center;"); + connect(key_server_opera_button, SIGNAL(clicked()), this, + SLOT(slotModifyEditDatetime())); + // Set Menu + createKeyServerOperaMenu(); + key_server_opera_button->setMenu(keyServerOperaMenu); + advance_h_box_layout->addWidget(key_server_opera_button); + + if (mKey.is_private_key() && mKey.has_master_key()) { + auto* revoke_cert_gen_button = + new QPushButton(_("Generate Revoke Certificate")); + connect(revoke_cert_gen_button, SIGNAL(clicked()), this, + SLOT(slotGenRevokeCert())); + advance_h_box_layout->addWidget(revoke_cert_gen_button); } - if ((mKey.expired()) || (mKey.revoked())) { - auto* expBox = new QHBoxLayout(); - QPixmap pixmap(":warning.png"); + auto* modify_tofu_button = new QPushButton(_("Modify TOFU Policy")); + connect(modify_tofu_button, SIGNAL(clicked()), this, + SLOT(slotModifyTOFUPolicy())); - auto* expLabel = new QLabel(); - auto* iconLabel = new QLabel(); - if (mKey.expired()) { - expLabel->setText(_("Warning: The Master Key has expired.")); - } - if (mKey.revoked()) { - expLabel->setText(_("Warning: The Master Key has been revoked")); - } + vbox_p_k->addLayout(advance_h_box_layout); + opera_key_box->setLayout(vbox_p_k); + mvbox->addWidget(opera_key_box); + vbox_p_k->addWidget(modify_tofu_button); - iconLabel->setPixmap(pixmap.scaled(24, 24, Qt::KeepAspectRatio)); - QFont font = expLabel->font(); - font.setBold(true); - expLabel->setFont(font); - expLabel->setAlignment(Qt::AlignCenter); - expBox->addWidget(iconLabel); - expBox->addWidget(expLabel); - mvbox->addLayout(expBox); - } + auto* expBox = new QHBoxLayout(); + QPixmap pixmap(":warning.png"); + + expLabel = new QLabel(); + iconLabel = new QLabel(); + + iconLabel->setPixmap(pixmap.scaled(24, 24, Qt::KeepAspectRatio)); + expLabel->setAlignment(Qt::AlignCenter); + expBox->addStretch(); + expBox->addWidget(iconLabel); + expBox->addWidget(expLabel); + expBox->addStretch(); + mvbox->addLayout(expBox); // when key database updated connect(SignalStation::GetInstance(), SIGNAL(KeyDatabaseRefresh()), this, @@ -214,6 +206,30 @@ KeyPairDetailTab::KeyPairDetailTab(const std::string& key_id, QWidget* parent) setLayout(mvbox); } +void KeyPairDetailTab::slotExportPublicKey() { + ByteArrayPtr keyArray = nullptr; + + if (!GpgKeyImportExportor::GetInstance().ExportKey(mKey, keyArray)) { + QMessageBox::critical(this, _("Error"), + _("An error occurred during the export operation.")); + return; + } + auto fileString = + mKey.name() + " " + mKey.email() + "(" + mKey.id() + ")_pub.asc"; + auto fileName = + QFileDialog::getSaveFileName( + this, _("Export Key To File"), QString::fromStdString(fileString), + QString(_("Key Files")) + " (*.asc *.txt);;All Files (*)") + .toStdString(); + + if (!write_buffer_to_file(fileName, *keyArray)) { + QMessageBox::critical( + this, _("Export Error"), + QString(_("Couldn't open %1 for writing")).arg(fileName.c_str())); + return; + } +} + void KeyPairDetailTab::slotExportPrivateKey() { // Show a information box with explanation about private key int ret = QMessageBox::information( @@ -234,14 +250,8 @@ void KeyPairDetailTab::slotExportPrivateKey() { _("An error occurred during the export operation.")); return; } - - auto key = GpgKeyGetter::GetInstance().GetKey(keyid); - if (!key.good()) { - QMessageBox::critical(nullptr, _("Error"), _("Key Not Found.")); - return; - } auto fileString = - key.name() + " " + key.email() + "(" + key.id() + ")_secret.asc"; + mKey.name() + " " + mKey.email() + "(" + mKey.id() + ")_secret.asc"; auto fileName = QFileDialog::getSaveFileName( this, _("Export Key To File"), QString::fromStdString(fileString), @@ -250,21 +260,13 @@ void KeyPairDetailTab::slotExportPrivateKey() { if (!write_buffer_to_file(fileName, *keyArray)) { QMessageBox::critical( - nullptr, _("Export Error"), + this, _("Export Error"), QString(_("Couldn't open %1 for writing")).arg(fileName.c_str())); return; } } } -QString KeyPairDetailTab::beautifyFingerprint(QString fingerprint) { - uint len = fingerprint.length(); - if ((len > 0) && (len % 4 == 0)) - for (uint n = 0; 4 * (n + 1) < len; ++n) - fingerprint.insert(static_cast<int>(5u * n + 4u), ' '); - return fingerprint; -} - void KeyPairDetailTab::slotCopyFingerprint() { QString fpr = fingerPrintVarLabel->text().trimmed().replace(" ", QString()); QClipboard* cb = QApplication::clipboard(); @@ -277,54 +279,95 @@ void KeyPairDetailTab::slotModifyEditDatetime() { } void KeyPairDetailTab::slotRefreshKeyInfo() { + // Show the situation that master key not exists. + masterKeyExistVarLabel->setText(mKey.has_master_key() ? _("Exists") + : _("Not Exists")); + if (!mKey.has_master_key()) { + auto paletteExpired = masterKeyExistVarLabel->palette(); + paletteExpired.setColor(masterKeyExistVarLabel->foregroundRole(), Qt::red); + masterKeyExistVarLabel->setPalette(paletteExpired); + } else { + auto paletteValid = masterKeyExistVarLabel->palette(); + paletteValid.setColor(masterKeyExistVarLabel->foregroundRole(), + Qt::darkGreen); + masterKeyExistVarLabel->setPalette(paletteValid); + } + + if (mKey.expired()) { + auto paletteExpired = expireVarLabel->palette(); + paletteExpired.setColor(expireVarLabel->foregroundRole(), Qt::red); + expireVarLabel->setPalette(paletteExpired); + } else { + auto paletteValid = expireVarLabel->palette(); + paletteValid.setColor(expireVarLabel->foregroundRole(), Qt::darkGreen); + expireVarLabel->setPalette(paletteValid); + } + nameVarLabel->setText(QString::fromStdString(mKey.name())); emailVarLabel->setText(QString::fromStdString(mKey.email())); commentVarLabel->setText(QString::fromStdString(mKey.comment())); keyidVarLabel->setText(QString::fromStdString(mKey.id())); - QString usage; - QTextStream usage_steam(&usage); + std::stringstream usage_steam; - if (mKey.can_certify()) usage_steam << _("Cert") << " "; - if (mKey.can_encrypt()) usage_steam << _("Encr") << " "; + if (mKey.can_certify()) usage_steam << _("Certificate") << " "; + if (mKey.can_encrypt()) usage_steam << _("Encrypt") << " "; if (mKey.can_sign()) usage_steam << _("Sign") << " "; if (mKey.can_authenticate()) usage_steam << _("Auth") << " "; - usageVarLabel->setText(usage); + usageVarLabel->setText(usage_steam.str().c_str()); - QString actualUsage; - QTextStream actual_usage_steam(&actualUsage); + std::stringstream actual_usage_steam; - if (mKey.CanCertActual()) actual_usage_steam << _("Cert") << " "; - if (mKey.CanEncrActual()) actual_usage_steam << _("Encr") << " "; + if (mKey.CanCertActual()) actual_usage_steam << _("Certificate") << " "; + if (mKey.CanEncrActual()) actual_usage_steam << _("Encrypt") << " "; if (mKey.CanSignActual()) actual_usage_steam << _("Sign") << " "; if (mKey.CanAuthActual()) actual_usage_steam << _("Auth") << " "; - actualUsageVarLabel->setText(actualUsage); + actualUsageVarLabel->setText(actual_usage_steam.str().c_str()); - QString keySizeVal, keyExpireVal, keyCreateTimeVal, keyAlgoVal; + std::string key_size_val, key_expire_val, key_create_time_val, key_algo_val, + key_last_update_val; - keySizeVal = QString::number(mKey.length()); + key_size_val = std::to_string(mKey.length()); if (to_time_t(boost::posix_time::ptime(mKey.expires())) == 0) { - keyExpireVal = _("Never Expire"); + expireVarLabel->setText(_("Never Expire")); } else { - keyExpireVal = - QString::fromStdString(boost::gregorian::to_iso_string(mKey.expires())); + expireVarLabel->setText(QLocale::system().toString( + QDateTime::fromTime_t(to_time_t(mKey.expires())))); } - keyAlgoVal = QString::fromStdString(mKey.pubkey_algo()); - keyCreateTimeVal = QString::fromStdString(to_iso_string(mKey.create_time())); + key_algo_val = mKey.pubkey_algo(); - keySizeVarLabel->setText(keySizeVal); - expireVarLabel->setText(keyExpireVal); - createdVarLabel->setText(keyCreateTimeVal); - algorithmVarLabel->setText(keyAlgoVal); + createdVarLabel->setText(QLocale::system().toString( + QDateTime::fromTime_t(to_time_t(mKey.create_time())))); - auto key_fpr = mKey.fpr(); - fingerPrintVarLabel->setText( - QString::fromStdString(beautify_fingerprint(key_fpr))); + if (to_time_t(boost::posix_time::ptime(mKey.last_update())) == 0) { + lastUpdateVarLabel->setText(_("No Data")); + } else { + lastUpdateVarLabel->setText(QLocale::system().toString( + QDateTime::fromTime_t(to_time_t(mKey.last_update())))); + } + + keySizeVarLabel->setText(key_size_val.c_str()); + algorithmVarLabel->setText(key_algo_val.c_str()); + fingerPrintVarLabel->setText(beautify_fingerprint(mKey.fpr()).c_str()); + + iconLabel->hide(); + expLabel->hide(); + + if (mKey.expired()) { + iconLabel->show(); + expLabel->show(); + expLabel->setText(_("Warning: The Master Key has expired.")); + } + if (mKey.revoked()) { + iconLabel->show(); + expLabel->show(); + expLabel->setText(_("Warning: The Master Key has been revoked.")); + } } void KeyPairDetailTab::createKeyServerOperaMenu() { @@ -333,9 +376,11 @@ void KeyPairDetailTab::createKeyServerOperaMenu() { auto* uploadKeyPair = new QAction(_("Upload Key Pair to Key Server"), this); connect(uploadKeyPair, SIGNAL(triggered()), this, SLOT(slotUploadKeyToServer())); + if (!mKey.is_private_key()) uploadKeyPair->setDisabled(true); + auto* updateKeyPair = new QAction(_("Update Key Pair"), this); connect(updateKeyPair, SIGNAL(triggered()), this, - SLOT(slotUpdateKeyToServer())); + SLOT(slotUpdateKeyFromServer())); keyServerOperaMenu->addAction(uploadKeyPair); keyServerOperaMenu->addAction(updateKeyPair); @@ -349,7 +394,7 @@ void KeyPairDetailTab::slotUploadKeyToServer() { dialog->slotUpload(); } -void KeyPairDetailTab::slotUpdateKeyToServer() { +void KeyPairDetailTab::slotUpdateKeyFromServer() { auto keys = std::make_unique<KeyIdArgsList>(); keys->push_back(mKey.id()); auto* dialog = new KeyServerImportDialog(this); @@ -403,4 +448,43 @@ void KeyPairDetailTab::slotRefreshKey() { this->slotRefreshKeyInfo(); } +void KeyPairDetailTab::slotModifyPassword() { + auto err = GpgKeyOpera::GetInstance().ModifyPassword(mKey); + if (check_gpg_error_2_err_code(err) != GPG_ERR_NO_ERROR) { + QMessageBox::critical(this, _("Not Successful"), + QString(_("Modify password not successfully."))); + } +} + +void KeyPairDetailTab::slotModifyTOFUPolicy() { + QStringList items; + items << _("Policy Auto") << _("Policy Good") << _("Policy Bad") + << _("Policy Ask") << _("Policy Unknown"); + + bool ok; + QString item = QInputDialog::getItem( + this, _("Modify TOFU Policy(Default is Auto)"), + _("Policy for the Key Pair:"), items, 0, false, &ok); + if (ok && !item.isEmpty()) { + LOG(INFO) << "selected policy" << item.toStdString(); + gpgme_tofu_policy_t tofu_policy = GPGME_TOFU_POLICY_AUTO; + if (item == _("Policy Auto")) { + tofu_policy = GPGME_TOFU_POLICY_AUTO; + } else if (item == _("Policy Good")) { + tofu_policy = GPGME_TOFU_POLICY_GOOD; + } else if (item == _("Policy Bad")) { + tofu_policy = GPGME_TOFU_POLICY_BAD; + } else if (item == _("Policy Ask")) { + tofu_policy = GPGME_TOFU_POLICY_ASK; + } else if (item == _("Policy Unknown")) { + tofu_policy = GPGME_TOFU_POLICY_UNKNOWN; + } + auto err = GpgKeyOpera::GetInstance().ModifyTOFUPolicy(mKey, tofu_policy); + if (check_gpg_error_2_err_code(err) != GPG_ERR_NO_ERROR) { + QMessageBox::critical(this, _("Not Successful"), + QString(_("Modify TOFU policy not successfully."))); + } + } +} + } // namespace GpgFrontend::UI |