2020-10-25 17:28:47 +01:00
|
|
|
/*
|
|
|
|
|
* File: KeyEdit.cpp
|
|
|
|
|
* Author: SET - nmset@yandex.com
|
|
|
|
|
* License : GPL v2
|
|
|
|
|
* Copyright SET - © 2019
|
|
|
|
|
*
|
|
|
|
|
* Created on October 25, 2020, 10:38 AM
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "KeyEdit.h"
|
|
|
|
|
#include <Wt/WText.h>
|
|
|
|
|
#include <Wt/WStandardItemModel.h>
|
|
|
|
|
#include <Wt/WStandardItem.h>
|
|
|
|
|
#include "GpgMEWorker.h"
|
2020-11-06 21:42:50 +01:00
|
|
|
#include "Tools.h"
|
2020-10-25 17:28:47 +01:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
KeyEdit::KeyEdit(K7Main * owner)
|
2020-11-03 11:06:25 +01:00
|
|
|
:WObject()
|
2020-10-25 17:28:47 +01:00
|
|
|
{
|
|
|
|
|
m_owner = owner;
|
2020-11-03 11:06:25 +01:00
|
|
|
m_popupUid = NULL;
|
2020-11-07 22:17:44 +01:00
|
|
|
m_popupExpiryTime = NULL;
|
2020-11-07 18:54:38 +01:00
|
|
|
m_targetUidValidityKeyFpr = WString::Empty;
|
2020-11-07 22:17:44 +01:00
|
|
|
m_expiryEditedKeyFpr = WString::Empty;
|
2020-10-25 17:28:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KeyEdit::~KeyEdit()
|
|
|
|
|
{
|
2020-11-03 11:06:25 +01:00
|
|
|
delete m_popupUid;
|
2020-10-25 17:28:47 +01:00
|
|
|
}
|
|
|
|
|
|
2020-11-06 21:42:50 +01:00
|
|
|
void KeyEdit::OnOwnerTrustDoubleClicked(WTreeTableNode * keyNode, bool keyHasSecret)
|
2020-10-25 17:28:47 +01:00
|
|
|
{
|
2020-11-04 21:00:03 +01:00
|
|
|
/*
|
2020-11-06 21:42:50 +01:00
|
|
|
* A private key that a user does not manage will have its public part
|
|
|
|
|
* listed in the public WTreeTableNode. The certification trust level must
|
|
|
|
|
* not be editable by anyone.
|
|
|
|
|
*/
|
|
|
|
|
WText * lblFpr = static_cast<WText*> (keyNode->columnWidget(3));
|
2020-11-14 11:25:00 +01:00
|
|
|
vector<WString> ourKeys = m_owner->m_config->PrivateKeyIds();
|
|
|
|
|
if (!Tools::IsOurKey(lblFpr->text(), ourKeys)
|
|
|
|
|
&& Tools::KeyHasSecret(lblFpr->text())) {
|
2020-11-06 21:42:50 +01:00
|
|
|
m_owner->m_tmwMessage->SetText(TR("OwnerTrustReadOnly"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* We leave a primary key with ultimate trust level for further change.
|
|
|
|
|
* kleopatra does not do that.
|
2020-11-04 21:00:03 +01:00
|
|
|
*/
|
2020-10-25 17:28:47 +01:00
|
|
|
WComboBox * cmbOwnerTrust = new WComboBox();
|
2020-11-06 21:42:50 +01:00
|
|
|
FillOwnerTrustCombo(cmbOwnerTrust, keyHasSecret);
|
|
|
|
|
cmbOwnerTrust->blurred().connect(std::bind(&KeyEdit::OnOwnerTrustBlurred, this, keyNode, keyHasSecret));
|
2020-10-25 17:28:47 +01:00
|
|
|
WText * lblOwnerTrust = static_cast<WText*> (keyNode->columnWidget(2));
|
|
|
|
|
cmbOwnerTrust->setCurrentIndex(cmbOwnerTrust->findText(lblOwnerTrust->text()));
|
2020-11-06 21:42:50 +01:00
|
|
|
/*
|
|
|
|
|
* Prepare to check for change in combobox item.
|
|
|
|
|
* Change is detected by index, not value.
|
|
|
|
|
*/
|
2020-10-25 17:28:47 +01:00
|
|
|
cmbOwnerTrust->setAttributeValue("previousTrustLevel", std::to_string(cmbOwnerTrust->currentIndex()));
|
|
|
|
|
keyNode->setColumnWidget(2, unique_ptr<WComboBox> (cmbOwnerTrust));
|
|
|
|
|
// +++
|
|
|
|
|
cmbOwnerTrust->setFocus();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 21:42:50 +01:00
|
|
|
void KeyEdit::OnOwnerTrustBlurred(WTreeTableNode* keyNode, bool keyHasSecret)
|
2020-10-25 17:28:47 +01:00
|
|
|
{
|
2020-11-06 21:42:50 +01:00
|
|
|
// Get new level (not index)
|
2020-10-25 17:28:47 +01:00
|
|
|
WComboBox * cmbOwnerTrust = static_cast<WComboBox*> (keyNode->columnWidget(2));
|
2020-11-06 21:42:50 +01:00
|
|
|
shared_ptr<WAbstractItemModel> aiModel = cmbOwnerTrust->model();
|
|
|
|
|
WStandardItemModel * iModel = static_cast<WStandardItemModel*> (aiModel.get());
|
|
|
|
|
WStandardItem * item = iModel->item(cmbOwnerTrust->currentIndex(), 0);
|
|
|
|
|
int newLevel = Tools::ToInt(item->text().toUTF8());
|
|
|
|
|
|
2020-10-25 17:28:47 +01:00
|
|
|
WText * lblOwnerTrust = new WText(cmbOwnerTrust->currentText());
|
2020-11-06 21:42:50 +01:00
|
|
|
lblOwnerTrust->doubleClicked().connect(std::bind(&KeyEdit::OnOwnerTrustDoubleClicked, this, keyNode, keyHasSecret));
|
2020-10-25 17:28:47 +01:00
|
|
|
const WText * lblFpr = static_cast<WText*> (keyNode->columnWidget(3));
|
|
|
|
|
const uint newTrustLevel = cmbOwnerTrust->currentIndex();
|
|
|
|
|
const WString previousTrustLevel = cmbOwnerTrust->attributeValue("previousTrustLevel");
|
|
|
|
|
keyNode->setColumnWidget(2, unique_ptr<WText> (lblOwnerTrust));
|
|
|
|
|
// If nothing was changed, don't go to engine.
|
|
|
|
|
if (WString(std::to_string(newTrustLevel)) == previousTrustLevel)
|
|
|
|
|
return;
|
2020-11-06 21:42:50 +01:00
|
|
|
|
2020-10-25 17:28:47 +01:00
|
|
|
GpgMEWorker gpgWorker;
|
2020-11-06 21:42:50 +01:00
|
|
|
GpgME::Error e = gpgWorker.EditOwnerTrust(lblFpr->text().toUTF8().c_str(), (GpgME::Key::OwnerTrust) newLevel);
|
2020-10-25 17:28:47 +01:00
|
|
|
if (e.code() != 0)
|
|
|
|
|
{
|
|
|
|
|
lblOwnerTrust->setText(previousTrustLevel);
|
|
|
|
|
m_owner->m_tmwMessage->SetText(TR("OwnerTrustFailure"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_owner->m_tmwMessage->SetText(TR("OwnerTrustSuccess"));
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 21:42:50 +01:00
|
|
|
void KeyEdit::FillOwnerTrustCombo(WComboBox * cmb, bool keyHasSecret)
|
2020-10-25 17:28:47 +01:00
|
|
|
{
|
|
|
|
|
shared_ptr<WStandardItemModel> siModel = make_shared<WStandardItemModel> ();
|
|
|
|
|
OwnerTrustMap OwnerTrustLevel = m_owner->OwnerTrustLevel;
|
|
|
|
|
vector<unique_ptr < WStandardItem>> colIndex;
|
|
|
|
|
vector<unique_ptr < WStandardItem>> colText;
|
2020-11-06 21:42:50 +01:00
|
|
|
colIndex.push_back(cpp14::make_unique<WStandardItem> (std::to_string(UserID::Validity::Unknown)));
|
|
|
|
|
colText.push_back(cpp14::make_unique<WStandardItem> (TR("UidUnknown")));
|
|
|
|
|
if (keyHasSecret) {
|
|
|
|
|
colIndex.push_back(cpp14::make_unique<WStandardItem> (std::to_string(UserID::Validity::Ultimate)));
|
|
|
|
|
colText.push_back(cpp14::make_unique<WStandardItem> (TR("UidUltimate")));
|
|
|
|
|
} else {
|
|
|
|
|
colIndex.push_back(cpp14::make_unique<WStandardItem> (std::to_string(UserID::Validity::Never)));
|
|
|
|
|
colText.push_back(cpp14::make_unique<WStandardItem> (TR("UidNever")));
|
|
|
|
|
colIndex.push_back(cpp14::make_unique<WStandardItem> (std::to_string(UserID::Validity::Marginal)));
|
|
|
|
|
colText.push_back(cpp14::make_unique<WStandardItem> (TR("UidMarginal")));
|
|
|
|
|
colIndex.push_back(cpp14::make_unique<WStandardItem> (std::to_string(UserID::Validity::Full)));
|
|
|
|
|
colText.push_back(cpp14::make_unique<WStandardItem> (TR("UidFull")));
|
2020-10-25 17:28:47 +01:00
|
|
|
}
|
|
|
|
|
siModel->appendColumn(std::move(colIndex));
|
|
|
|
|
siModel->appendColumn(std::move(colText));
|
|
|
|
|
cmb->clear();
|
|
|
|
|
cmb->setModel(siModel);
|
|
|
|
|
cmb->setModelColumn(1);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 11:06:25 +01:00
|
|
|
void KeyEdit::OnUidValidityClicked(WTreeTableNode* uidNode, vector<WString>& privateKeys, const WString& targetKeyFpr)
|
|
|
|
|
{
|
2020-11-07 18:54:38 +01:00
|
|
|
if (targetKeyFpr != m_targetUidValidityKeyFpr) {
|
2020-11-03 11:06:25 +01:00
|
|
|
bool passwordVisibility = true;
|
|
|
|
|
if (m_popupUid)
|
|
|
|
|
passwordVisibility = m_popupUid->IsPasswordVisible();
|
|
|
|
|
delete m_popupUid;
|
|
|
|
|
WText * lblUidValidity = static_cast<WText*> (uidNode->columnWidget(2));
|
|
|
|
|
m_popupUid = new PopupCertifyUserId(lblUidValidity, m_owner->m_tmwMessage);
|
|
|
|
|
m_popupUid->Create(privateKeys, targetKeyFpr);
|
|
|
|
|
m_popupUid->ShowPassphrase(passwordVisibility);
|
2020-11-07 18:54:38 +01:00
|
|
|
m_targetUidValidityKeyFpr = targetKeyFpr;
|
2020-11-03 11:06:25 +01:00
|
|
|
m_popupUid->GetCertifyButton()->clicked().connect(this, &KeyEdit::CertifyKey);
|
|
|
|
|
}
|
|
|
|
|
m_popupUid->show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KeyEdit::CertifyKey()
|
|
|
|
|
{
|
|
|
|
|
vector<uint>& uidsToSign = m_popupUid->GetUidsToSign();
|
|
|
|
|
if (uidsToSign.size() == 0) {
|
|
|
|
|
m_owner->m_tmwMessage->SetText(TR("NoUidSelected"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const WString signingKey = m_popupUid->GetSelectedKey();
|
|
|
|
|
const WString keyToSign = m_popupUid->GetKeyToSign();
|
|
|
|
|
int options = m_popupUid->GetCertifyOptions();
|
|
|
|
|
GpgMEWorker gpgWorker;
|
|
|
|
|
GpgME::Error e = gpgWorker.CertifyKey(signingKey.toUTF8().c_str(),
|
|
|
|
|
keyToSign.toUTF8().c_str(),
|
|
|
|
|
uidsToSign, options,
|
|
|
|
|
m_popupUid->GetPassphrase());
|
|
|
|
|
if (e.code() != 0)
|
|
|
|
|
{
|
|
|
|
|
m_owner->m_tmwMessage->SetText(TR("CertificationFailure"));
|
|
|
|
|
m_popupUid->ShowPassphrase(true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_owner->m_tmwMessage->SetText(TR("CertificationSuccess"));
|
|
|
|
|
m_popupUid->ShowPassphrase(false);
|
|
|
|
|
m_owner->DisplayUids(keyToSign);
|
|
|
|
|
}
|
2020-11-07 22:17:44 +01:00
|
|
|
|
|
|
|
|
void KeyEdit::OnExpiryClicked(WTreeTableNode* subkeyNode, const WString& keyFpr)
|
|
|
|
|
{
|
|
|
|
|
if (keyFpr != m_expiryEditedKeyFpr) {
|
|
|
|
|
delete m_popupExpiryTime;
|
|
|
|
|
WText * lblExpiry = static_cast<WText*> (subkeyNode->columnWidget(2));
|
|
|
|
|
m_popupExpiryTime = new PopupExpiryTime(lblExpiry, m_owner->m_tmwMessage);
|
|
|
|
|
m_popupExpiryTime->Create(keyFpr);
|
|
|
|
|
m_expiryEditedKeyFpr = keyFpr;
|
|
|
|
|
m_popupExpiryTime->GetApplyButton()->clicked().connect(this, &KeyEdit::SetExpiryTime);
|
|
|
|
|
}
|
|
|
|
|
m_popupExpiryTime->show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KeyEdit::SetExpiryTime()
|
|
|
|
|
{
|
|
|
|
|
GpgMEWorker gpgWorker;
|
|
|
|
|
GpgME::Error e = gpgWorker.SetExpiryTime(m_expiryEditedKeyFpr.toUTF8().c_str(),
|
|
|
|
|
m_popupExpiryTime->GetPassphrase(),
|
|
|
|
|
m_popupExpiryTime->GetExpiryTime());
|
|
|
|
|
if (e.code() != 0)
|
|
|
|
|
{
|
|
|
|
|
m_owner->m_tmwMessage->SetText(TR("SetExpirationTimeFailure"));
|
|
|
|
|
m_popupExpiryTime->ShowPassphrase(true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_owner->m_tmwMessage->SetText(TR("SetExpirationTimeSuccess"));
|
|
|
|
|
m_popupExpiryTime->ShowPassphrase(false);
|
|
|
|
|
m_owner->DisplaySubKeys(m_expiryEditedKeyFpr, true);
|
|
|
|
|
}
|
|
|
|
|
|