aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/main_window/MainWindowServerSlotFunction.cpp
blob: 17491db73cc8d80f360cea0ddf33721b8bb12636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
 * 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 "MainWindow.h"

#ifdef SERVER_SUPPORT
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
#include "server/ComUtils.h"
#endif
#include "ui/ShowCopyDialog.h"

namespace GpgFrontend::UI {

#ifdef SERVER_SUPPORT

/**
 * 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 ownKeyId = settings.value("general/ownKeyId").toString();

  GpgKey key = mCtx->getKeyRefById(ownKeyId);
  if (!key.good) {
    QMessageBox::critical(this, _("Invalid Own Key"),
                          _("Own Key can not be use to do any operation. "
                            "Please go to the setting interface to select an "
                            "OwnKey and get a ServiceToken."));
    return {};
  }

  auto utils = new ComUtils(this);

  QString serviceToken = settings.value("general/serviceToken").toString();
  if (serviceToken.isEmpty() || !utils->checkServiceTokenFormat(serviceToken)) {
    QMessageBox::critical(
        this, _("Error"),
        _("Please obtain a Service Token from the server in the settings."));
    return {};
  }

  QUrl reqUrl(utils->getUrl(ComUtils::GetFullCryptText));
  QNetworkRequest request(reqUrl);
  request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

  // Sign Shorten Text
  auto outSignTextBase64 =
      ComUtils::getSignStringBase64(mCtx, shortenCryptoText, key);

  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());

  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);
  doc.Accept(writer);

  QByteArray postData(sb.GetString());
  qDebug() << "postData" << QString::fromUtf8(postData);

  QNetworkReply* reply = utils->getNetworkManager().post(request, postData);

  auto dialog = new WaitingDialog(_("Getting Cpt From Server"), this);
  dialog->show();

  while (reply->isRunning()) QApplication::processEvents();

  dialog->close();

  QByteArray replyData = reply->readAll().constData();
  if (utils->checkServerReply(replyData)) {
    /**
     * {
     *      "cryptoText" : ...
     *      "sha": ...
     *      "serviceToken": ...
     *      "date": ...
     * }
     */

    if (!utils->checkDataValueStr("cryptoText") ||
        !utils->checkDataValueStr("sha") ||
        !utils->checkDataValueStr("serviceToken")) {
      QMessageBox::critical(this, _("Error"),
                            _("The communication content with the server does "
                              "not meet the requirements"));
      return {};
    }

    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());

    if (sha_generator.result().toHex() == sha &&
        serviceToken == serviceTokenFromServer) {
      return cryptoText;
    } else
      QMessageBox::critical(this, _("Error"), _("Invalid short ciphertext"));

    return {};
  }

  return {};
}

#endif

#ifdef SERVER_SUPPORT

void MainWindow::shortenCryptText() {
  // gather information
  QString serviceToken = settings.value("general/serviceToken").toString();
  QString ownKeyId = settings.value("general/ownKeyId").toString();
  QByteArray cryptoText = edit->curTextPage()->toPlainText().toUtf8();

  auto utils = new ComUtils(this);

  if (serviceToken.isEmpty() || !utils->checkServiceTokenFormat(serviceToken)) {
    QMessageBox::critical(
        this, _("Invalid Service Token"),
        _("Please go to the setting interface to get a ServiceToken."));
    return;
  }

  QUrl reqUrl(utils->getUrl(ComUtils::ShortenCryptText));
  QNetworkRequest request(reqUrl);
  request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

  GpgKey key = mCtx->getKeyRefById(ownKeyId);
  if (!key.good) {
    QMessageBox::critical(this, _("Invalid Own Key"),
                          _("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;

  QByteArray outSignTextBase64 =
      ComUtils::getSignStringBase64(mCtx, signText, key);

  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());

  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);
  doc.Accept(writer);

  QByteArray postData(sb.GetString());
  qDebug() << "postData" << QString::fromUtf8(postData);

  QNetworkReply* reply = networkAccessManager->post(request, postData);

  auto* dialog = new WaitingDialog(_("Getting Scpt From Server"), this);
  dialog->show();
  while (reply->isRunning()) QApplication::processEvents();
  dialog->close();

  if (utils->checkServerReply(reply->readAll().constData())) {
    /**
     * {
     *      "shortenText" : ...
     *      "md5": ...
     * }
     */

    if (!utils->checkDataValueStr("shortenText") ||
        !utils->checkDataValueStr("md5")) {
      QMessageBox::critical(this, _("Error"),
                            _("The communication content with the server does "
                              "not meet the requirements"));
      return;
    }

    QString shortenText = utils->getDataValueStr("shortenText");

    QCryptographicHash md5_generator(QCryptographicHash::Md5);
    md5_generator.addData(shortenText.toUtf8());
    if (md5_generator.result().toHex() == utils->getDataValueStr("md5")) {
      auto* dialog =
          new ShowCopyDialog(shortenText,
                             _("Notice: Use Decrypt & Verify operation to "
                               "decrypt this short crypto text."),
                             this);
      dialog->show();
    } else {
      QMessageBox::critical(
          this, _("Error"),
          _("There is a problem with the communication with the server"));
      return;
    }
  }
}

#endif

}  // namespace GpgFrontend::UI