diff options
Diffstat (limited to '')
-rw-r--r-- | include/gpg/GpgContext.h | 5 | ||||
-rw-r--r-- | include/gpg/result_analyse/SignResultAnalyse.h | 4 | ||||
-rw-r--r-- | include/server/ComUtils.h | 17 | ||||
-rw-r--r-- | include/server/PubkeyUploader.h | 39 | ||||
-rwxr-xr-x | include/ui/FileEncryptionDialog.h | 14 | ||||
-rwxr-xr-x | include/ui/SettingsDialog.h | 4 | ||||
-rw-r--r-- | include/ui/widgets/SignersPicker.h | 48 | ||||
-rw-r--r-- | src/gpg/GpgFileOpera.cpp | 5 | ||||
-rw-r--r-- | src/gpg/gpg_context/GpgContextBasicOpera.cpp | 8 | ||||
-rw-r--r-- | src/gpg/result_analyse/SignResultAnalyse.cpp | 8 | ||||
-rw-r--r-- | src/server/ComUtils.cpp | 36 | ||||
-rw-r--r-- | src/server/PubkeyUploader.cpp | 92 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowFileSlotFunction.cpp | 4 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowServerSlotFunction.cpp | 41 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 28 | ||||
-rw-r--r-- | src/ui/settings/SettingsGeneral.cpp | 56 | ||||
-rw-r--r-- | src/ui/widgets/SignersPicker.cpp | 59 |
17 files changed, 404 insertions, 64 deletions
diff --git a/include/gpg/GpgContext.h b/include/gpg/GpgContext.h index d59f8072..fd9983bf 100644 --- a/include/gpg/GpgContext.h +++ b/include/gpg/GpgContext.h @@ -95,8 +95,9 @@ namespace GpgME { gpg_error_t encrypt(QVector<GpgKey> &keys, const QByteArray &inBuffer, QByteArray *outBuffer, gpgme_encrypt_result_t *result); - gpgme_error_t encryptSign(QVector<GpgKey> &keys, const QByteArray &inBuffer, QByteArray *outBuffer, - gpgme_encrypt_result_t *encr_result, gpgme_sign_result_t *sign_result); + gpgme_error_t encryptSign(QVector<GpgKey> &keys, QVector<GpgKey> &signers, const QByteArray &inBuffer, + QByteArray *outBuffer, gpgme_encrypt_result_t *encr_result, + gpgme_sign_result_t *sign_result); gpgme_error_t decrypt(const QByteArray &inBuffer, QByteArray *outBuffer, gpgme_decrypt_result_t *result); diff --git a/include/gpg/result_analyse/SignResultAnalyse.h b/include/gpg/result_analyse/SignResultAnalyse.h index 3db5978f..ebb15dfb 100644 --- a/include/gpg/result_analyse/SignResultAnalyse.h +++ b/include/gpg/result_analyse/SignResultAnalyse.h @@ -28,11 +28,13 @@ #include "GpgFrontend.h" #include "ResultAnalyse.h" +#include "gpg/GpgContext.h" class SignResultAnalyse : public ResultAnalyse { Q_OBJECT public: - explicit SignResultAnalyse(gpgme_error_t error, gpgme_sign_result_t result); + + explicit SignResultAnalyse(GpgME::GpgContext *ctx, gpgme_error_t error, gpgme_sign_result_t result); private: diff --git a/include/server/ComUtils.h b/include/server/ComUtils.h index 5f27998e..d7812bd3 100644 --- a/include/server/ComUtils.h +++ b/include/server/ComUtils.h @@ -26,13 +26,14 @@ #define GPGFRONTEND_ZH_CN_TS_COMUTILS_H #include "GpgFrontend.h" +#include "gpg/GpgContext.h" #include "rapidjson/document.h" class ComUtils : public QWidget { Q_OBJECT public: - enum ServiceType { GetServiceToken, ShortenCryptText, GetFullCryptText }; + enum ServiceType { GetServiceToken, ShortenCryptText, GetFullCryptText, UploadPubkey }; explicit ComUtils(QWidget *parent) : QWidget(parent), appPath(qApp->applicationDirPath()), settings(RESOURCE_DIR(appPath) + "/conf/gpgfrontend.ini", @@ -40,18 +41,26 @@ public: } - QString getUrl(ServiceType type); + QString getUrl(ServiceType type) const; bool checkServerReply(const QByteArray &reply); - QString getDataValue(const QString &key); + QString getDataValueStr(const QString &key); + + bool checkDataValueStr(const QString &key); + + rapidjson::Value &getDataValue(const QString &key); bool checkDataValue(const QString &key); bool checkServiceTokenFormat(const QString& serviceToken); + static QByteArray getSignStringBase64(GpgME::GpgContext *ctx, const QString &str, const GpgKey& key); + [[nodiscard]] bool good() const { return is_good; } + QNetworkAccessManager &getNetworkManager() {return networkMgr;} + private: QString appPath; @@ -59,6 +68,8 @@ private: rapidjson::Document replyDoc; rapidjson::Value dataVal; + QNetworkAccessManager networkMgr; + QRegularExpression re_uuid{R"(\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b)"}; bool is_good = false; diff --git a/include/server/PubkeyUploader.h b/include/server/PubkeyUploader.h new file mode 100644 index 00000000..6a2022e9 --- /dev/null +++ b/include/server/PubkeyUploader.h @@ -0,0 +1,39 @@ +/** + * 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. + * + */ + +#ifndef GPGFRONTEND_ZH_CN_TS_PUBKEYUPLOADER_H +#define GPGFRONTEND_ZH_CN_TS_PUBKEYUPLOADER_H + +#include "GpgFrontend.h" +#include "ComUtils.h" + +class PubkeyUploader { +public: + + PubkeyUploader(GpgME::GpgContext *ctx, const QVector<GpgKey> &keys); + +}; + + +#endif //GPGFRONTEND_ZH_CN_TS_PUBKEYUPLOADER_H diff --git a/include/ui/FileEncryptionDialog.h b/include/ui/FileEncryptionDialog.h index f4adb18f..be9ddd22 100755 --- a/include/ui/FileEncryptionDialog.h +++ b/include/ui/FileEncryptionDialog.h @@ -102,14 +102,14 @@ public slots: void slotShowKeyList(); private: - QLineEdit *outputFileEdit; /**< TODO */ - QLineEdit *inputFileEdit; /**< TODO */ - QLineEdit *signFileEdit; /**< TODO */ - DialogAction mAction; /**< TODO */ - QLabel *statusLabel; /**< TODO */ + QLineEdit *outputFileEdit; + QLineEdit *inputFileEdit; + QLineEdit *signFileEdit; + DialogAction mAction; + QLabel *statusLabel; protected: - GpgME::GpgContext *mCtx; /**< TODO */ - KeyList *mKeyList; /**< TODO */ + GpgME::GpgContext *mCtx; + KeyList *mKeyList; }; diff --git a/include/ui/SettingsDialog.h b/include/ui/SettingsDialog.h index 577c4379..17b84aa4 100755 --- a/include/ui/SettingsDialog.h +++ b/include/ui/SettingsDialog.h @@ -54,9 +54,7 @@ private: QVector<QString> keyIdsList; QString serviceToken; KeyList *mKeyList; - GpgME::GpgContext *mCtx; /** The current gpg context */ - - QNetworkAccessManager manager; + GpgME::GpgContext *mCtx; private slots: diff --git a/include/ui/widgets/SignersPicker.h b/include/ui/widgets/SignersPicker.h new file mode 100644 index 00000000..afa95a05 --- /dev/null +++ b/include/ui/widgets/SignersPicker.h @@ -0,0 +1,48 @@ +/** + * 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. + * + */ + +#ifndef GPGFRONTEND_ZH_CN_TS_SIGNERSPIRCKER_H +#define GPGFRONTEND_ZH_CN_TS_SIGNERSPIRCKER_H + +#include "GpgFrontend.h" +#include "gpg/GpgContext.h" + +#include "ui/widgets/KeyList.h" + +class SignersPicker : public QDialog { +Q_OBJECT + +public: + + explicit SignersPicker(GpgME::GpgContext *ctx, QWidget *parent = nullptr); + + void getCheckedSigners(QVector<GpgKey> &keys); + +private: + GpgME::GpgContext *mCtx; + KeyList *mKeyList; +}; + + +#endif //GPGFRONTEND_ZH_CN_TS_SIGNERSPIRCKER_H diff --git a/src/gpg/GpgFileOpera.cpp b/src/gpg/GpgFileOpera.cpp index 5d924349..af50c79a 100644 --- a/src/gpg/GpgFileOpera.cpp +++ b/src/gpg/GpgFileOpera.cpp @@ -185,7 +185,10 @@ gpg_error_t GpgFileOpera::encryptSignFile(GpgME::GpgContext *ctx, QVector<GpgKey auto outBuffer = QByteArray(); infile.close(); - auto error = ctx->encryptSign(keys, inBuffer, &outBuffer, encr_res, sign_res); + QVector<GpgKey> signerKeys; + + // TODO dealing with signer keys + auto error = ctx->encryptSign(keys, signerKeys, inBuffer, &outBuffer, encr_res, sign_res); if (gpg_err_code(error) != GPG_ERR_NO_ERROR) return error; diff --git a/src/gpg/gpg_context/GpgContextBasicOpera.cpp b/src/gpg/gpg_context/GpgContextBasicOpera.cpp index 08c9ce05..f368a6b4 100644 --- a/src/gpg/gpg_context/GpgContextBasicOpera.cpp +++ b/src/gpg/gpg_context/GpgContextBasicOpera.cpp @@ -221,12 +221,14 @@ gpg_error_t GpgME::GpgContext::sign(const QVector<GpgKey> &keys, const QByteArra * @param sign_result sign opera result * @return */ -gpgme_error_t GpgME::GpgContext::encryptSign(QVector<GpgKey> &keys, const QByteArray &inBuffer, QByteArray *outBuffer, - gpgme_encrypt_result_t *encr_result, gpgme_sign_result_t *sign_result) { +gpgme_error_t +GpgME::GpgContext::encryptSign(QVector<GpgKey> &keys, QVector<GpgKey> &signers, const QByteArray &inBuffer, + QByteArray *outBuffer, gpgme_encrypt_result_t *encr_result, + gpgme_sign_result_t *sign_result) { gpgme_data_t data_in = nullptr, data_out = nullptr; outBuffer->resize(0); - setSigners(keys); + setSigners(signers); //gpgme_encrypt_result_t e_result; gpgme_key_t recipients[keys.count() + 1]; diff --git a/src/gpg/result_analyse/SignResultAnalyse.cpp b/src/gpg/result_analyse/SignResultAnalyse.cpp index 9173eaeb..d50017c0 100644 --- a/src/gpg/result_analyse/SignResultAnalyse.cpp +++ b/src/gpg/result_analyse/SignResultAnalyse.cpp @@ -24,7 +24,7 @@ #include "gpg/result_analyse/SignResultAnalyse.h" -SignResultAnalyse::SignResultAnalyse(gpgme_error_t error, gpgme_sign_result_t result) { +SignResultAnalyse::SignResultAnalyse(GpgME::GpgContext *ctx, gpgme_error_t error, gpgme_sign_result_t result) { stream << tr("[#] Sign Operation "); @@ -52,6 +52,12 @@ SignResultAnalyse::SignResultAnalyse(gpgme_error_t error, gpgme_sign_result_t re stream << Qt::endl; + GpgKey singerKey = ctx->getKeyByFpr(new_sign->fpr); + if(singerKey.good) { + stream << tr(" Signer: ") << singerKey.uids.first().uid << Qt::endl; + } else { + stream << tr(" Signer: ") << tr("<unknown>") << Qt::endl; + } stream << tr(" Public Key Algo: ") << gpgme_pubkey_algo_name(new_sign->pubkey_algo) << Qt::endl; stream << tr(" Hash Algo: ") << gpgme_hash_algo_name(new_sign->hash_algo) << Qt::endl; stream << tr(" Date & Time: ") << QDateTime::fromTime_t(new_sign->timestamp).toString() << Qt::endl; diff --git a/src/server/ComUtils.cpp b/src/server/ComUtils.cpp index 2644d40e..9cd48924 100644 --- a/src/server/ComUtils.cpp +++ b/src/server/ComUtils.cpp @@ -97,7 +97,7 @@ bool ComUtils::checkServerReply(const QByteArray &reply) { * @param key key of value * @return value in string format */ -QString ComUtils::getDataValue(const QString &key) { +QString ComUtils::getDataValueStr(const QString &key) { if (is_good) { auto k_byte_array = key.toUtf8(); if (dataVal.HasMember(k_byte_array.data())) { @@ -111,12 +111,12 @@ QString ComUtils::getDataValue(const QString &key) { * @param type service which server provides * @return url */ -QString ComUtils::getUrl(ComUtils::ServiceType type) { +QString ComUtils::getUrl(ComUtils::ServiceType type) const { auto host = settings.value("general/currentGpgfrontendServer", "service.gpgfrontend.pub").toString(); auto protocol = QString(); - // Debug Server + // Localhost Debug Server if (host == "localhost") protocol = "http://"; else protocol = "https://"; @@ -132,6 +132,9 @@ QString ComUtils::getUrl(ComUtils::ServiceType type) { case GetFullCryptText: url += "/text/get"; break; + case UploadPubkey: + url += "/key/upload"; + break; } qDebug() << "ComUtils getUrl" << url; @@ -139,7 +142,7 @@ QString ComUtils::getUrl(ComUtils::ServiceType type) { return url; } -bool ComUtils::checkDataValue(const QString &key) { +bool ComUtils::checkDataValueStr(const QString &key) { auto key_byte_array_data = key.toUtf8().constData(); if (is_good) { return dataVal.HasMember(key_byte_array_data) && dataVal[key_byte_array_data].IsString(); @@ -149,3 +152,28 @@ bool ComUtils::checkDataValue(const QString &key) { bool ComUtils::checkServiceTokenFormat(const QString &uuid) { return re_uuid.match(uuid).hasMatch(); } + +QByteArray ComUtils::getSignStringBase64(GpgME::GpgContext *ctx, const QString &str, const GpgKey &key) { + QVector<GpgKey> keys{key}; + QByteArray outSignText; + auto signData = str.toUtf8(); + ctx->sign(keys, signData, &outSignText, GPGME_SIG_MODE_NORMAL); + return outSignText.toBase64(); +} + +rapidjson::Value &ComUtils::getDataValue(const QString &key) { + if (is_good) { + auto k_byte_array = key.toUtf8(); + if (dataVal.HasMember(k_byte_array.data())) { + return dataVal[k_byte_array.data()]; + } + } + throw std::runtime_error("Inner Error"); +} + +bool ComUtils::checkDataValue(const QString &key){ + auto key_byte_array_data = key.toUtf8().constData(); + if (is_good) { + return dataVal.HasMember(key_byte_array_data); + } else return false; +}
\ No newline at end of file diff --git a/src/server/PubkeyUploader.cpp b/src/server/PubkeyUploader.cpp new file mode 100644 index 00000000..70ba07ec --- /dev/null +++ b/src/server/PubkeyUploader.cpp @@ -0,0 +1,92 @@ +/** + * 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 "server/PubkeyUploader.h" + +#include "rapidjson/prettywriter.h" + +PubkeyUploader::PubkeyUploader(GpgME::GpgContext *ctx, const QVector<GpgKey> &keys) { + auto utils = new ComUtils(nullptr); + QUrl reqUrl(utils->getUrl(ComUtils::UploadPubkey)); + QNetworkRequest request(reqUrl); + + rapidjson::Document publicKeys; + publicKeys.SetArray(); + QStringList keyIds; + + rapidjson::Document::AllocatorType& allocator = publicKeys.GetAllocator(); + + for(const auto &key : keys) { + rapidjson::Value publicKeyObj, pubkey, sha, signedFpr; + + QByteArray keyDataBuf; + keyIds << key.id; + ctx->exportKeys(&keyIds, &keyDataBuf); + + QCryptographicHash shaGen(QCryptographicHash::Sha256); + shaGen.addData(keyDataBuf); + + auto shaStr = shaGen.result().toHex(); + + auto signFprStr = ComUtils::getSignStringBase64(ctx, key.fpr, key); + + pubkey.SetString(keyDataBuf.constData(), keyDataBuf.count()); + + sha.SetString(shaStr.constData(), shaStr.count()); + signedFpr.SetString(signFprStr.constData(), signFprStr.count()); + + publicKeyObj.SetObject(); + + publicKeyObj.AddMember("publicKey", pubkey, allocator); + publicKeyObj.AddMember("sha", sha, allocator); + publicKeyObj.AddMember("signedFpr", signedFpr, allocator); + + publicKeys.PushBack(publicKeyObj, allocator); + keyIds.clear(); + } + + rapidjson::StringBuffer sb; + rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb); + publicKeys.Accept(writer); + + QByteArray postData(sb.GetString()); + qDebug() << "postData" << QString::fromUtf8(postData); + + QNetworkReply *reply = utils->getNetworkManager().post(request, postData); + + while (reply->isRunning()) QApplication::processEvents(); + + QByteArray replyData = reply->readAll().constData(); + if (utils->checkServerReply(replyData)) { + /** + * { + * "strings" : [ + * "...", + * "..." + * ] + * } + */ + } + +} diff --git a/src/ui/main_window/MainWindowFileSlotFunction.cpp b/src/ui/main_window/MainWindowFileSlotFunction.cpp index 914d1b5e..e391c666 100644 --- a/src/ui/main_window/MainWindowFileSlotFunction.cpp +++ b/src/ui/main_window/MainWindowFileSlotFunction.cpp @@ -274,7 +274,7 @@ void MainWindow::slotFileSign() { if (!if_error) { - auto resultAnalyse = new SignResultAnalyse(error, result); + auto resultAnalyse = new SignResultAnalyse(mCtx, error, result); auto &reportText = resultAnalyse->getResultReport(); infoBoard->associateTabWidget(edit->tabWidget); infoBoard->associateFileTreeView(edit->curFilePage()); @@ -479,7 +479,7 @@ void MainWindow::slotFileEncryptSign() { if (!if_error) { auto resultAnalyseEncr = new EncryptResultAnalyse(error, encr_result); - auto resultAnalyseSign = new SignResultAnalyse(error, sign_result); + auto resultAnalyseSign = new SignResultAnalyse(mCtx, error, sign_result); int status = std::min(resultAnalyseEncr->getStatus(), resultAnalyseSign->getStatus()); auto reportText = resultAnalyseEncr->getResultReport() + resultAnalyseSign->getResultReport(); diff --git a/src/ui/main_window/MainWindowServerSlotFunction.cpp b/src/ui/main_window/MainWindowServerSlotFunction.cpp index ed660621..c2300818 100644 --- a/src/ui/main_window/MainWindowServerSlotFunction.cpp +++ b/src/ui/main_window/MainWindowServerSlotFunction.cpp @@ -74,8 +74,10 @@ QString MainWindow::getCryptText(const QString &shortenCryptoText) { 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::Document::AllocatorType& allocator = doc.GetAllocator(); + + doc.AddMember("signature", s, allocator); + doc.AddMember("serviceToken", t, allocator); rapidjson::StringBuffer sb; rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb); @@ -84,7 +86,7 @@ QString MainWindow::getCryptText(const QString &shortenCryptoText) { QByteArray postData(sb.GetString()); qDebug() << "postData" << QString::fromUtf8(postData); - QNetworkReply *reply = networkAccessManager->post(request, postData); + QNetworkReply *reply = utils->getNetworkManager()->post(request, postData); auto dialog = new WaitingDialog(tr("Getting Cpt From Server"), this); dialog->show(); @@ -104,17 +106,17 @@ QString MainWindow::getCryptText(const QString &shortenCryptoText) { * } */ - if (!utils->checkDataValue("cryptoText") - || !utils->checkDataValue("sha") - || !utils->checkDataValue("serviceToken")) { + if (!utils->checkDataValueStr("cryptoText") + || !utils->checkDataValueStr("sha") + || !utils->checkDataValueStr("serviceToken")) { QMessageBox::critical(this, tr("Error"), tr("The communication content with the server does not meet the requirements")); return {}; } - auto cryptoText = utils->getDataValue("cryptoText"); - auto sha = utils->getDataValue("sha"); - auto serviceTokenFromServer = utils->getDataValue("serviceToken"); + auto cryptoText = utils->getDataValueStr("cryptoText"); + auto sha = utils->getDataValueStr("sha"); + auto serviceTokenFromServer = utils->getDataValueStr("serviceToken"); QCryptographicHash sha_generator(QCryptographicHash::Sha256); sha_generator.addData(cryptoText.toUtf8()); @@ -168,10 +170,7 @@ void MainWindow::shortenCryptText() { qDebug() << "shaText" << shaText; - QVector<GpgKey> keys{key}; - QByteArray outSignText; - mCtx->sign(keys, signText, &outSignText, GPGME_SIG_MODE_NORMAL); - QByteArray outSignTextBase64 = outSignText.toBase64(); + QByteArray outSignTextBase64 = ComUtils::getSignStringBase64(mCtx, signText, key); rapidjson::Value c, s, m, t; @@ -185,10 +184,12 @@ void MainWindow::shortenCryptText() { 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::Document::AllocatorType& allocator = doc.GetAllocator(); + + doc.AddMember("cryptoText", c, allocator); + doc.AddMember("sha", m, allocator); + doc.AddMember("sign", s, allocator); + doc.AddMember("serviceToken", t, allocator); rapidjson::StringBuffer sb; rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb); @@ -213,17 +214,17 @@ void MainWindow::shortenCryptText() { * } */ - if (!utils->checkDataValue("shortenText") || !utils->checkDataValue("md5")) { + if (!utils->checkDataValueStr("shortenText") || !utils->checkDataValueStr("md5")) { QMessageBox::critical(this, tr("Error"), tr("The communication content with the server does not meet the requirements")); return; } - QString shortenText = utils->getDataValue("shortenText"); + QString shortenText = utils->getDataValueStr("shortenText"); QCryptographicHash md5_generator(QCryptographicHash::Md5); md5_generator.addData(shortenText.toUtf8()); - if (md5_generator.result().toHex() == utils->getDataValue("md5")) { + if (md5_generator.result().toHex() == utils->getDataValueStr("md5")) { auto *dialog = new ShowCopyDialog(shortenText, tr("Notice: Use Decrypt & Verify operation to decrypt this short crypto text."), this); dialog->show(); } else { diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index d6f1e880..d31ef73b 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -24,6 +24,7 @@ #include "MainWindow.h" #include "ui/SendMailDialog.h" +#include "ui/widgets/SignersPicker.h" /** @@ -154,7 +155,7 @@ void MainWindow::slotSign() { infoBoard->associateTextEdit(edit->curTextPage()); edit->slotFillTextEditWithText(QString::fromUtf8(*tmp)); - auto resultAnalyse = new SignResultAnalyse(error, result); + auto resultAnalyse = new SignResultAnalyse(mCtx, error, result); auto &reportText = resultAnalyse->getResultReport(); if (resultAnalyse->getStatus() < 0) @@ -328,13 +329,33 @@ void MainWindow::slotEncryptSign() { tr("None of the selected key pairs can provide the signature function.")); } + QVector<GpgKey> signerKeys; + + auto signersPicker = new SignersPicker(mCtx, this); + + QEventLoop loop; + connect(signersPicker, SIGNAL(accepted()), &loop, SLOT(quit())); + loop.exec(); + + signersPicker->getCheckedSigners(signerKeys); + + for(const auto &key : keys) { + qDebug() << "Keys " << key.email; + } + + for(const auto &signer : signerKeys) { + qDebug() << "Signers " << signer.email; + } + + auto *tmp = new QByteArray(); gpgme_encrypt_result_t encr_result = nullptr; gpgme_sign_result_t sign_result = nullptr; gpgme_error_t error; auto thread = QThread::create([&]() { - error = mCtx->encryptSign(keys, edit->curTextPage()->toPlainText().toUtf8(), tmp, &encr_result, + error = mCtx->encryptSign(keys, signerKeys, edit->curTextPage()->toPlainText().toUtf8(), tmp, + &encr_result, &sign_result); }); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); @@ -353,7 +374,7 @@ void MainWindow::slotEncryptSign() { } auto resultAnalyseEncr = new EncryptResultAnalyse(error, encr_result); - auto resultAnalyseSign = new SignResultAnalyse(error, sign_result); + auto resultAnalyseSign = new SignResultAnalyse(mCtx, error, sign_result); int status = std::min(resultAnalyseEncr->getStatus(), resultAnalyseSign->getStatus()); auto reportText = resultAnalyseEncr->getResultReport() + resultAnalyseSign->getResultReport(); @@ -523,7 +544,6 @@ void MainWindow::uploadKeyToServer() { } - void MainWindow::slotOpenFile(QString &path) { edit->slotOpenFile(path); } diff --git a/src/ui/settings/SettingsGeneral.cpp b/src/ui/settings/SettingsGeneral.cpp index 9cee3bc8..94f13b8f 100644 --- a/src/ui/settings/SettingsGeneral.cpp +++ b/src/ui/settings/SettingsGeneral.cpp @@ -24,10 +24,8 @@ #include "ui/SettingsDialog.h" #include "ui/WaitingDialog.h" - #include "server/ComUtils.h" -#include "rapidjson/document.h" #include "rapidjson/prettywriter.h" GeneralTab::GeneralTab(GpgME::GpgContext *ctx, QWidget *parent) @@ -241,8 +239,6 @@ void GeneralTab::slotGetServiceToken() { QNetworkRequest request(reqUrl); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); - // Building Post Data - QByteArray keyDataBuf; const auto keyId = keyIdsList[ownKeySelectBox->currentIndex()]; @@ -255,22 +251,55 @@ void GeneralTab::slotGetServiceToken() { } QStringList selectedKeyIds(keyIdsList[ownKeySelectBox->currentIndex()]); + + QByteArray keyDataBuf; mCtx->exportKeys(&selectedKeyIds, &keyDataBuf); + GpgKey key = mCtx->getKeyById(keyId); + + if(!key.good) { + QMessageBox::critical(this, tr("Error"), + tr("Key Not Exists")); + return; + } + qDebug() << "keyDataBuf" << keyDataBuf; - rapidjson::Value p, v; + /** + * { + * "publicKey" : ... + * "sha": ... + * "signedFpr": ... + * "version": ... + * } + */ + + QCryptographicHash shaGen(QCryptographicHash::Sha256); + shaGen.addData(keyDataBuf); + + auto shaStr = shaGen.result().toHex(); + + auto signFprStr = utils->getSignStringBase64(mCtx, key.fpr, key); + + rapidjson::Value pubkey, ver, sha, signFpr; rapidjson::Document doc; doc.SetObject(); - p.SetString(keyDataBuf.constData(), keyDataBuf.count()); + pubkey.SetString(keyDataBuf.constData(), keyDataBuf.count()); auto version = qApp->applicationVersion(); - v.SetString(version.toUtf8().constData(), qApp->applicationVersion().count()); + ver.SetString(version.toUtf8().constData(), qApp->applicationVersion().count()); + + sha.SetString(shaStr.constData(), shaStr.count()); + signFpr.SetString(signFprStr.constData(), signFprStr.count()); + + rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); - doc.AddMember("publicKey", p, doc.GetAllocator()); - doc.AddMember("version", v, doc.GetAllocator()); + doc.AddMember("publicKey", pubkey, allocator); + doc.AddMember("sha", sha, allocator); + doc.AddMember("signedFpr", signFpr, allocator); + doc.AddMember("version", ver, allocator); rapidjson::StringBuffer sb; rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb); @@ -278,8 +307,9 @@ void GeneralTab::slotGetServiceToken() { QByteArray postData(sb.GetString()); - QNetworkReply *reply = manager.post(request, postData); + QNetworkReply *reply = utils->getNetworkManager().post(request, postData); + // Show Waiting Dailog auto dialog = new WaitingDialog("Getting Token From Server", this); dialog->show(); @@ -298,13 +328,13 @@ void GeneralTab::slotGetServiceToken() { * } */ - if(!utils->checkDataValue("serviceToken") || !utils->checkDataValue("fpr")) { + if(!utils->checkDataValueStr("serviceToken") || !utils->checkDataValueStr("fpr")) { QMessageBox::critical(this, tr("Error"), tr("The communication content with the server does not meet the requirements")); return; } - QString serviceTokenTemp = utils->getDataValue("serviceToken"); - QString fpr = utils->getDataValue("fpr"); + QString serviceTokenTemp = utils->getDataValueStr("serviceToken"); + QString fpr = utils->getDataValueStr("fpr"); auto key = mCtx->getKeyByFpr(fpr); if (utils->checkServiceTokenFormat(serviceTokenTemp) && key.good) { serviceToken = serviceTokenTemp; diff --git a/src/ui/widgets/SignersPicker.cpp b/src/ui/widgets/SignersPicker.cpp new file mode 100644 index 00000000..00df5fdd --- /dev/null +++ b/src/ui/widgets/SignersPicker.cpp @@ -0,0 +1,59 @@ +/** + * 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 "ui/widgets/SignersPicker.h" + +SignersPicker::SignersPicker(GpgME::GpgContext *ctx, QWidget *parent) : mCtx(ctx), QDialog(parent) { + auto confirmButton = new QPushButton(tr("Confirm")); + connect(confirmButton, SIGNAL(clicked(bool)), this, SLOT(accept())); + + /*Setup KeyList*/ + mKeyList = new KeyList(mCtx, KeyListRow::ONLY_SECRET_KEY, + KeyListColumn::NAME | KeyListColumn::EmailAddress | KeyListColumn::Usage); + + mKeyList->setFilter([](const GpgKey &key) -> bool { + if (!GpgME::GpgContext::checkIfKeyCanSign(key)) return false; + else return true; + }); + + mKeyList->slotRefresh(); + + auto *vbox2 = new QVBoxLayout(); + vbox2->addWidget(new QLabel("Select Signer(s): ")); + vbox2->addWidget(mKeyList); + vbox2->addWidget(confirmButton); + vbox2->addStretch(0); + setLayout(vbox2); + + this->setModal(true); + this->setWindowTitle("Signers Picker"); + this->setMinimumWidth(480); + this->show(); + + +} + +void SignersPicker::getCheckedSigners(QVector<GpgKey> &keys) { + mKeyList->getPrivateCheckedKeys(keys); +} |