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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
/**
* 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 "SettingsGeneral.h"
#ifdef SERVER_SUPPORT
#include "server/ComUtils.h"
#endif
#ifdef MULTI_LANG_SUPPORT
#include "SettingsDialog.h"
#endif
#include "GlobalSettingStation.h"
#include "gpg/function/GpgKeyGetter.h"
#include "ui/widgets/KeyList.h"
namespace GpgFrontend::UI {
GeneralTab::GeneralTab(QWidget* parent) : QWidget(parent) {
#ifdef SERVER_SUPPORT
/*****************************************
* GpgFrontend Server
*****************************************/
auto* serverBox = new QGroupBox(_("GpgFrontend Server"));
auto* serverBoxLayout = new QVBoxLayout();
serverSelectBox = new QComboBox();
serverBoxLayout->addWidget(serverSelectBox);
serverBoxLayout->addWidget(new QLabel(
_("Server that provides short key and key exchange services")));
serverBox->setLayout(serverBoxLayout);
#endif
/*****************************************
* Save-Checked-Keys-Box
*****************************************/
auto* saveCheckedKeysBox = new QGroupBox(_("Save Checked Keys"));
auto* saveCheckedKeysBoxLayout = new QHBoxLayout();
saveCheckedKeysCheckBox = new QCheckBox(
_("Save checked private keys on exit and restore them on next start."),
this);
saveCheckedKeysBoxLayout->addWidget(saveCheckedKeysCheckBox);
saveCheckedKeysBox->setLayout(saveCheckedKeysBoxLayout);
/*****************************************
* Longer-Expire-Date-Box
*****************************************/
auto* longerKeyExpirationDateBox =
new QGroupBox(_("Longer Key Expiration Date"));
auto* longerKeyExpirationDateBoxLayout = new QHBoxLayout();
longerKeyExpirationDateCheckBox = new QCheckBox(
_("Unlock key expiration date setting up to 30 years."), this);
longerKeyExpirationDateBoxLayout->addWidget(longerKeyExpirationDateCheckBox);
longerKeyExpirationDateBox->setLayout(longerKeyExpirationDateBoxLayout);
/*****************************************
* Key-Impport-Confirmation Box
*****************************************/
auto* importConfirmationBox =
new QGroupBox(_("Confirm drag'n'drop key import"));
auto* importConfirmationBoxLayout = new QHBoxLayout();
importConfirmationCheckBox = new QCheckBox(
_("Import files dropped on the Key List without confirmation."), this);
importConfirmationBoxLayout->addWidget(importConfirmationCheckBox);
importConfirmationBox->setLayout(importConfirmationBoxLayout);
#ifdef MULTI_LANG_SUPPORT
/*****************************************
* Language Select Box
*****************************************/
auto* langBox = new QGroupBox(_("Language"));
auto* langBoxLayout = new QVBoxLayout();
langSelectBox = new QComboBox();
langSelectBox->setMaxVisibleItems(8);
langSelectBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
lang = SettingsDialog::listLanguages();
for (const auto& l : lang) {
langSelectBox->addItem(l);
}
langSelectBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
langBoxLayout->addWidget(langSelectBox);
langBoxLayout->addWidget(new QLabel(
"<b>" + QString(_("NOTE")) + _(": ") + "</b>" +
_("GpgFrontend will restart automatically if you change the language!")));
langBox->setLayout(langBoxLayout);
connect(langSelectBox, SIGNAL(currentIndexChanged(int)), this,
SLOT(slotLanguageChanged()));
#endif
#ifdef SERVER_SUPPORT
/*****************************************
* Own Key Select Box
*****************************************/
auto* ownKeyBox = new QGroupBox(_("Own key"));
auto* ownKeyBoxLayout = new QVBoxLayout();
auto* ownKeyServiceTokenLayout = new QHBoxLayout();
ownKeySelectBox = new QComboBox;
getServiceTokenButton = new QPushButton(_("Get Service Token"));
serviceTokenLabel = new QLabel(_("No Service Token Found"));
serviceTokenLabel->setAlignment(Qt::AlignCenter);
ownKeyBox->setLayout(ownKeyBoxLayout);
mKeyList = new KeyList();
// Fill the keyid hashmap
keyIds.insert({QString(), "<none>"});
auto private_keys = mKeyList->getAllPrivateKeys();
for (const auto& keyid : *private_keys) {
auto key = GpgKeyGetter::GetInstance().GetKey(keyid);
if (!key.good()) continue;
keyIds.insert({key.id(), key.uids()->front().uid()});
}
for (const auto& k : keyIds) {
ownKeySelectBox->addItem(QString::fromStdString(k.second));
keyIdsList.push_back(k.first);
}
connect(ownKeySelectBox, SIGNAL(currentIndexChanged(int)), this,
SLOT(slotOwnKeyIdChanged()));
connect(getServiceTokenButton, SIGNAL(clicked(bool)), this,
SLOT(slotGetServiceToken()));
ownKeyBoxLayout->addWidget(new QLabel(
_("Key pair for synchronization and identity authentication")));
ownKeyBoxLayout->addWidget(ownKeySelectBox);
ownKeyBoxLayout->addLayout(ownKeyServiceTokenLayout);
ownKeyServiceTokenLayout->addWidget(getServiceTokenButton);
ownKeyServiceTokenLayout->addWidget(serviceTokenLabel);
ownKeyServiceTokenLayout->stretch(0);
#endif
/*****************************************
* Mainlayout
*****************************************/
auto* mainLayout = new QVBoxLayout;
#ifdef SERVER_SUPPORT
mainLayout->addWidget(serverBox);
#endif
mainLayout->addWidget(longerKeyExpirationDateBox);
mainLayout->addWidget(saveCheckedKeysBox);
mainLayout->addWidget(importConfirmationBox);
#ifdef MULTI_LANG_SUPPORT
mainLayout->addWidget(langBox);
#endif
#ifdef SERVER_SUPPORT
mainLayout->addWidget(ownKeyBox);
#endif
setSettings();
mainLayout->addStretch(1);
setLayout(mainLayout);
}
/**********************************
* Read the settings from config
* and set the buttons and checkboxes
* appropriately
**********************************/
void GeneralTab::setSettings() {
auto& settings = GlobalSettingStation::GetInstance().GetUISettings();
try {
bool save_key_checked = settings.lookup("general.save_key_checked");
if (save_key_checked) saveCheckedKeysCheckBox->setCheckState(Qt::Checked);
} catch (...) {
LOG(ERROR) << _("Setting Operation Error") << _("save_key_checked");
}
try {
bool longer_expiration_date =
settings.lookup("general.longer_expiration_date");
LOG(INFO) << "longer_expiration_date" << longer_expiration_date;
if (longer_expiration_date)
longerKeyExpirationDateCheckBox->setCheckState(Qt::Checked);
} catch (...) {
LOG(ERROR) << _("Setting Operation Error") << _("longer_expiration_date");
}
#ifdef SERVER_SUPPORT
auto serverList =
settings.value("general/gpgfrontendServerList").toStringList();
if (serverList.empty()) {
serverList.append("service.gpgfrontend.pub");
serverList.append("localhost");
}
for (const auto& s : serverList) serverSelectBox->addItem(s);
qDebug() << "Current Gpgfrontend Server"
<< settings.value("general/currentGpgfrontendServer").toString();
serverSelectBox->setCurrentText(
settings
.value("general/currentGpgfrontendServer", "service.gpgfrontend.pub")
.toString());
connect(serverSelectBox,
QOverload<const QString&>::of(&QComboBox::currentTextChanged), this,
[&](const QString& current) -> void {
settings.setValue("general/currentGpgfrontendServer", current);
});
#endif
#ifdef MULTI_LANG_SUPPORT
try {
std::string lang_key = settings.lookup("general.lang");
QString lang_value = lang.value(lang_key.c_str());
LOG(INFO) << "lang settings current" << lang_value.toStdString();
if (!lang.empty()) {
langSelectBox->setCurrentIndex(langSelectBox->findText(lang_value));
} else {
langSelectBox->setCurrentIndex(0);
}
} catch (...) {
LOG(ERROR) << _("Setting Operation Error") << _("lang");
}
#endif
#ifdef SERVER_SUPPORT
auto own_key_id = settings.value("general/ownKeyId").toString().toStdString();
if (own_key_id.empty()) {
ownKeySelectBox->setCurrentText("<none>");
} else {
const auto uid = keyIds.find(own_key_id)->second;
ownKeySelectBox->setCurrentText(QString::fromStdString(uid));
}
serviceToken =
settings.value("general/serviceToken").toString().toStdString();
if (!serviceToken.empty()) {
serviceTokenLabel->setText(QString::fromStdString(serviceToken));
}
#endif
try {
bool confirm_import_keys = settings.lookup("general.confirm_import_keys");
LOG(INFO) << "confirm_import_keys" << confirm_import_keys;
if (confirm_import_keys)
importConfirmationCheckBox->setCheckState(Qt::Checked);
} catch (...) {
LOG(ERROR) << _("Setting Operation Error") << _("confirm_import_keys");
}
}
/***********************************
* get the values of the buttons and
* write them to settings-file
*************************************/
void GeneralTab::applySettings() {
auto& settings =
GpgFrontend::UI::GlobalSettingStation::GetInstance().GetUISettings();
if (!settings.exists("general") ||
settings.lookup("general").getType() != libconfig::Setting::TypeGroup)
settings.add("general", libconfig::Setting::TypeGroup);
auto& general = settings["general"];
if (!general.exists("longer_expiration_date"))
general.add("longer_expiration_date", libconfig::Setting::TypeBoolean) =
longerKeyExpirationDateCheckBox->isChecked();
else {
general["longer_expiration_date"] =
longerKeyExpirationDateCheckBox->isChecked();
}
if (!general.exists("save_key_checked"))
general.add("save_key_checked", libconfig::Setting::TypeBoolean) =
saveCheckedKeysCheckBox->isChecked();
else {
general["save_key_checked"] = saveCheckedKeysCheckBox->isChecked();
}
#ifdef SERVER_SUPPORT
qDebug() << "serverSelectBox currentText" << serverSelectBox->currentText();
settings.setValue("general/currentGpgfrontendServer",
serverSelectBox->currentText());
auto* serverList = new QStringList();
for (int i = 0; i < serverSelectBox->count(); i++)
serverList->append(serverSelectBox->itemText(i));
settings.setValue("general/gpgfrontendServerList", *serverList);
delete serverList;
#endif
#ifdef MULTI_LANG_SUPPORT
if (!general.exists("lang"))
general.add("lang", libconfig::Setting::TypeBoolean) =
lang.key(langSelectBox->currentText()).toStdString();
else {
general["lang"] = lang.key(langSelectBox->currentText()).toStdString();
}
#endif
#ifdef SERVER_SUPPORT
settings.setValue(
"general/ownKeyId",
QString::fromStdString(keyIdsList[ownKeySelectBox->currentIndex()]));
settings.setValue("general/serviceToken",
QString::fromStdString(serviceToken));
#endif
if (!general.exists("confirm_import_keys"))
general.add("confirm_import_keys", libconfig::Setting::TypeBoolean) =
importConfirmationCheckBox->isChecked();
else {
general["confirm_import_keys"] = importConfirmationCheckBox->isChecked();
}
}
#ifdef MULTI_LANG_SUPPORT
void GeneralTab::slotLanguageChanged() { emit signalRestartNeeded(true); }
#endif
#ifdef SERVER_SUPPORT
void GeneralTab::slotOwnKeyIdChanged() {
// Set ownKeyId to currently selected
this->serviceTokenLabel->setText(_("No Service Token Found"));
serviceToken.clear();
}
#endif
#ifdef SERVER_SUPPORT
void GeneralTab::slotGetServiceToken() {
auto utils = new ComUtils(this);
QUrl reqUrl(utils->getUrl(ComUtils::GetServiceToken));
QNetworkRequest request(reqUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
const auto keyId = keyIdsList[ownKeySelectBox->currentIndex()];
qDebug() << "KeyId" << keyIdsList[ownKeySelectBox->currentIndex()];
if (keyId.isEmpty()) {
QMessageBox::critical(
this, _("Invalid Operation"),
_("Own Key can not be None while getting service token."));
return;
}
QStringList selectedKeyIds(keyIdsList[ownKeySelectBox->currentIndex()]);
QByteArray keyDataBuf;
mCtx->exportKeys(&selectedKeyIds, &keyDataBuf);
GpgKey key = mCtx->getKeyRefById(keyId);
if (!key.good) {
QMessageBox::critical(this, _("Error"), _("Key Not Exists"));
return;
}
qDebug() << "keyDataBuf" << keyDataBuf;
/**
* {
* "publicKey" : ...
* "sha": ...
* "signedFpr": ...
* "version": ...
* }
*/
QCryptographicHash shaGen(QCryptographicHash::Sha256);
shaGen.addData(keyDataBuf);
auto shaStr = shaGen.result().toHex();
auto signFprStr = ComUtils::getSignStringBase64(mCtx, key.fpr, key);
rapidjson::Value pubkey, ver, sha, signFpr;
rapidjson::Document doc;
doc.SetObject();
pubkey.SetString(keyDataBuf.constData(), keyDataBuf.count());
auto version = qApp->applicationVersion();
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", 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);
doc.Accept(writer);
QByteArray postData(sb.GetString());
QNetworkReply* reply = utils->getNetworkManager().post(request, postData);
// Show Waiting Dailog
auto dialog = new WaitingDialog("Getting Token From Server", this);
dialog->show();
while (reply->isRunning()) {
QApplication::processEvents();
}
dialog->close();
if (utils->checkServerReply(reply->readAll().constData())) {
/**
* {
* "serviceToken" : ...
* "fpr": ...
* }
*/
if (!utils->checkDataValueStr("serviceToken") ||
!utils->checkDataValueStr("fpr")) {
QMessageBox::critical(this, _("Error"),
_("The communication content with the server does "
"not meet the requirements"));
return;
}
QString serviceTokenTemp = utils->getDataValueStr("serviceToken");
QString fpr = utils->getDataValueStr("fpr");
auto key = mCtx->getKeyRefByFpr(fpr);
if (utils->checkServiceTokenFormat(serviceTokenTemp) && key.good) {
serviceToken = serviceTokenTemp;
qDebug() << "Get Service Token" << serviceToken;
// Auto update settings
settings.setValue("general/serviceToken", serviceToken);
serviceTokenLabel->setText(serviceToken);
QMessageBox::information(this, _("Notice"),
_("Succeed in getting service token"));
} else {
QMessageBox::critical(
this, _("Error"),
_("There is a problem with the communication with the server"));
}
}
}
#endif
} // namespace GpgFrontend::UI
|