diff options
author | Saturneric <[email protected]> | 2021-08-10 14:06:45 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2021-08-10 14:06:45 +0000 |
commit | 61ced076e5effd3f8ddc76372242ba5fa67b6303 (patch) | |
tree | 4722a7cea8387c83f470d170d88e5a8164498d0b /src/ui/main_window/MainWindowSlotFunction.cpp | |
parent | Merge branch 'develop-ci' into develop (diff) | |
download | GpgFrontend-61ced076e5effd3f8ddc76372242ba5fa67b6303.tar.gz GpgFrontend-61ced076e5effd3f8ddc76372242ba5fa67b6303.zip |
Add Functions:
GpgFrontend Settings
Service Token
Short Crypto Text
Functions TODO:
Revoke Cert Generation
Diffstat (limited to 'src/ui/main_window/MainWindowSlotFunction.cpp')
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 193 |
1 files changed, 187 insertions, 6 deletions
diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index 4bcee080..736fecb5 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -23,7 +23,12 @@ */ #include "MainWindow.h" +#include "server/ComUtils.h" #include "ui/SendMailDialog.h" +#include "ui/ShowCopyDialog.h" + +#include "rapidjson/document.h" +#include "rapidjson/prettywriter.h" void MainWindow::slotEncrypt() { @@ -77,6 +82,7 @@ void MainWindow::slotEncrypt() { edit->slotFillTextEditWithText(*tmp2); infoBoard->associateTextEdit(edit->curTextPage()); + // check result analyse status if (resultAnalyse->getStatus() < 0) infoBoard->slotRefresh(reportText, INFO_ERROR_CRITICAL); else if (resultAnalyse->getStatus() > 0) @@ -84,10 +90,11 @@ void MainWindow::slotEncrypt() { else infoBoard->slotRefresh(reportText, INFO_ERROR_WARN); + // set optional actions if (resultAnalyse->getStatus() >= 0) { infoBoard->resetOptionActionsMenu(); infoBoard->addOptionalAction("Send Mail", [this]() { - if(settings.value("sendMail/enable", false).toBool()) + if (settings.value("sendMail/enable", false).toBool()) new SendMailDialog(edit->curTextPage()->toPlainText(), this); else { QMessageBox::warning(nullptr, @@ -134,7 +141,7 @@ void MainWindow::slotSign() { gpgme_error_t error; auto thread = QThread::create([&]() { - error = mCtx->sign(keys, edit->curTextPage()->toPlainText().toUtf8(), tmp, false, &result); + error = mCtx->sign(keys, edit->curTextPage()->toPlainText().toUtf8(), tmp, GPGME_SIG_MODE_CLEAR, &result); }); connect(thread, SIGNAL(finished(QPrivateSignal)), thread, SLOT(deleteLater())); thread->start(); @@ -361,7 +368,7 @@ void MainWindow::slotEncryptSign() { if (status >= 0) { infoBoard->resetOptionActionsMenu(); infoBoard->addOptionalAction("Send Mail", [this]() { - if(settings.value("sendMail/enable", false).toBool()) + if (settings.value("sendMail/enable", false).toBool()) new SendMailDialog(edit->curTextPage()->toPlainText(), this); else { QMessageBox::warning(nullptr, @@ -369,6 +376,15 @@ void MainWindow::slotEncryptSign() { tr("Please go to the settings interface to enable and configure this function.")); } }); + infoBoard->addOptionalAction("Shorten Crypt Text", [this]() { + if (settings.value("general/serviceToken").toString().isEmpty()) + QMessageBox::warning(nullptr, + tr("Service Token Empty"), + tr("Please go to the settings interface to set Own Key and get Service Token.")); + else { + shortenCryptText(); + } + }); } delete resultAnalyseEncr; @@ -385,7 +401,18 @@ void MainWindow::slotDecryptVerify() { if (edit->slotCurPageTextEdit() != nullptr) { auto *decrypted = new QByteArray(); - QByteArray text = edit->curTextPage()->toPlainText().toUtf8(); + QString plainText = edit->curTextPage()->toPlainText(); + + + if (plainText.trimmed().startsWith("[GpgFrontend_ShortCrypto]://")) { + auto cryptoText = getCryptText(plainText); + if (!cryptoText.isEmpty()) { + plainText = cryptoText; + } + } + + QByteArray text = plainText.toUtf8(); + GpgME::GpgContext::preventNoDataErr(&text); gpgme_decrypt_result_t d_result = nullptr; @@ -435,6 +462,160 @@ void MainWindow::slotDecryptVerify() { } } +/** + * get full size crypt text from server using short crypto text + * @param shortenCryptoText short crypto text([GpgFrontend_ShortCrypto]://) + * @return + */ +QString MainWindow::getCryptText(const QString &shortenCryptoText) { + QString host = settings.value("general/currentGpgfrontendServer", + "service.gpgfrontend.pub").toString(); + + QString ownKeyId = settings.value("general/ownKeyId").toString(); + + GpgKey key = mCtx->getKeyById(ownKeyId); + if (!key.good) { + QMessageBox::critical(this, tr("Invalid Own Key"), tr("Own Key can not be use to do any operation.")); + return {}; + } + + QString serviceToken = settings.value("general/serviceToken").toString(); + if (serviceToken.isEmpty()) { + QMessageBox::critical(this, tr("Error"), + tr("Please obtain a Service Token from the server in the settings.")); + return {}; + } + + QUrl reqUrl("http://127.0.0.1:9048/text/get"); + QNetworkRequest request(reqUrl); + request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); + + // Sign Shorten Text + QVector keys{key}; + QByteArray outSignText; + mCtx->sign(keys, shortenCryptoText.toUtf8(), &outSignText, GPGME_SIG_MODE_NORMAL); + auto outSignTextBase64 = outSignText.toBase64(); + + rapidjson::Document doc; + doc.SetObject(); + + rapidjson::Value s, t; + + // Signature + s.SetString(outSignTextBase64.constData(), outSignTextBase64.count()); + // Service Token + const auto t_byte_array = serviceToken.toUtf8(); + t.SetString(t_byte_array.constData(), t_byte_array.count()); + + doc.AddMember("signature", s, doc.GetAllocator()); + doc.AddMember("serviceToken", t, doc.GetAllocator()); + + rapidjson::StringBuffer sb; + rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb); + doc.Accept(writer); + + QByteArray postData(sb.GetString()); + qDebug() << "postData" << QString::fromUtf8(postData); + + QNetworkReply *reply = networkAccessManager->post(request, postData); + + auto dialog = new WaitingDialog("Getting Crypt Text From Server", this); + dialog->show(); + + while (reply->isRunning()) { + QApplication::processEvents(); + } + + dialog->close(); + + QByteArray replyData = reply->readAll().constData(); + auto comUtils = new ComUtils(this); + if (comUtils->checkServerReply(replyData)) { + //TODO Logic + } else QMessageBox::critical(this, tr("Error"), tr("Unknown Error")); + + return {}; +} + +void MainWindow::shortenCryptText() { + + QString serviceToken = settings.value("general/serviceToken").toString(); + QString ownKeyId = settings.value("general/ownKeyId").toString(); + + QByteArray cryptoText = edit->curTextPage()->toPlainText().toUtf8(); + + QUrl reqUrl("http://127.0.0.1:9048/text/new"); + QNetworkRequest request(reqUrl); + request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); + + GpgKey key = mCtx->getKeyById(ownKeyId); + if (!key.good) { + QMessageBox::critical(this, tr("Invalid Own Key"), tr("Own Key can not be use to do any operation.")); + return; + } + + QCryptographicHash ch(QCryptographicHash::Md5); + ch.addData(cryptoText); + QString md5 = ch.result().toHex(); + + qDebug() << "md5" << md5; + + QByteArray signText = QString("[%1][%2]").arg(serviceToken, md5).toUtf8(); + + QCryptographicHash sha(QCryptographicHash::Sha256); + sha.addData(signText); + QString shaText = sha.result().toHex(); + + qDebug() << "shaText" << shaText; + + QVector keys{key}; + QByteArray outSignText; + mCtx->sign(keys, signText, &outSignText, GPGME_SIG_MODE_NORMAL); + QByteArray outSignTextBase64 = outSignText.toBase64(); + + rapidjson::Value c, s, m, t; + + rapidjson::Document doc; + doc.SetObject(); + + c.SetString(cryptoText.constData(), cryptoText.count()); + auto m_byte_array = shaText.toUtf8(); + m.SetString(m_byte_array.constData(), m_byte_array.count()); + s.SetString(outSignTextBase64.constData(), outSignTextBase64.count()); + auto t_byte_array = serviceToken.toUtf8(); + t.SetString(t_byte_array.constData(), t_byte_array.count()); + + doc.AddMember("cryptoText", c, doc.GetAllocator()); + doc.AddMember("sha", m, doc.GetAllocator()); + doc.AddMember("sign", s, doc.GetAllocator()); + doc.AddMember("serviceToken", t, doc.GetAllocator()); + + rapidjson::StringBuffer sb; + rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb); + doc.Accept(writer); + + QByteArray postData(sb.GetString()); + qDebug() << "postData" << QString::fromUtf8(postData); + + QNetworkReply *reply = networkAccessManager->post(request, postData); + + while (reply->isRunning()) { + QApplication::processEvents(); + } + + if (reply->error() == QNetworkReply::NoError) { + rapidjson::Document docReply; + docReply.Parse(reply->readAll().constData()); + QString shortenText = docReply["shortenText"].GetString(); + auto *dialog = new ShowCopyDialog(shortenText, this); + dialog->show(); + } else { + QMessageBox::critical(this, tr("Error"), reply->errorString()); + } + + +} + /* * Append the selected (not checked!) Key(s) To Textedit */ @@ -1077,14 +1258,14 @@ void MainWindow::slotOpenFile(QString &path) { } void MainWindow::slotVersionUpgrade(const QString ¤tVersion, const QString &latestVersion) { - if(currentVersion < latestVersion) { + if (currentVersion < latestVersion) { QMessageBox::warning(this, tr("Outdated Version"), tr("This version(%1) is out of date, please update the latest version in time. ").arg( currentVersion) + tr("You can download the latest version(%1) on Github Releases Page.<br/>").arg( latestVersion)); - } else if(currentVersion > latestVersion) { + } else if (currentVersion > latestVersion) { QMessageBox::warning(this, tr("Unreleased Version"), tr("This version(%1) has not been officially released and is not recommended for use in a production environment. <br/>").arg( |