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
|
/**
* Copyright (C) 2021-2024 Saturneric <eric@bktus.com>
*
* 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.
*
* GpgFrontend 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 GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
*
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
* All the source code of GpgFrontend was modified and released by
* Saturneric <eric@bktus.com> starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include "KeyServerSyncModule.h"
#include <GFSDKGpg.h>
#include <QtCore>
#include <QtWidgets>
#include "GFModuleDefine.h"
#include "SearchKeyDialog.h"
#include "VKSInterface.h"
GF_MODULE_API_DEFINE_V2("com.bktus.gpgfrontend.module.key_server_sync",
"KeyServerSync", "1.2.2",
"Sync Information From Trusted Key Server.",
"Saturneric")
DEFINE_TRANSLATIONS_STRUCTURE(ModuleKeyServerSync);
auto GFRegisterModule() -> int {
LOG_DEBUG("key server sync module registering");
return 0;
}
auto GFActiveModule() -> int {
REGISTER_TRANS_READER();
LISTEN("REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT");
LISTEN("REQUEST_GET_PUBLIC_KEY_BY_KEY_ID");
LISTEN("REQUEST_UPLOAD_PUBLIC_KEY");
LISTEN("REQUEST_SEARCH_PUBLIC_KEY_BY_FINGERPRINT");
LISTEN("MAINWINDOW_MENU_MOUNTED");
LISTEN("KEY_PAIR_OPERA_MENU_CREATED");
return 0;
}
namespace {
auto UploadKeyToServer(QWidget* parent, int channel, const QString& key_id)
-> int {
char* key_data = nullptr;
int size = 0;
auto ret = GFGpgExportKey(channel, QDUP(key_id), 1, &key_data, &size);
if (ret != 0 || key_data == nullptr || size <= 0) {
QMessageBox::critical(
parent, QCoreApplication::translate("GTrC", "Key Upload Failed"),
QCoreApplication::translate(
"GTrC",
"Failed to export the public key before uploading.\n"
"Key: %1")
.arg(key_id));
return -1;
}
// UnStrDup takes ownership of key_data and frees it; key_data must not be
// used afterwards.
auto key_text = UDUP(key_data);
auto* vks = new VKSInterface();
QObject::connect(
vks, &VKSInterface::SignalKeyUploaded, QThread::currentThread(),
[parent](const QString& fpr, const QJsonObject& status,
const QString& token) {
// Handle successful response
QString status_message = QCoreApplication::translate(
"GTrC", "The following email addresses have status:\n");
QStringList email_list;
if (!status.isEmpty()) {
for (auto it = status.constBegin(); it != status.constEnd(); ++it) {
status_message +=
QString("%1: %2\n").arg(it.key(), it.value().toString());
email_list.append(it.key());
}
} else {
status_message += QCoreApplication::translate(
"GTrC", "Could not parse status information.");
}
// Notify user of successful upload and status details
QMessageBox::information(
parent,
QCoreApplication::translate("GTrC", "Public Key Upload Successful"),
QCoreApplication::translate(
"GTrC",
"The public key was successfully uploaded to the "
"key server keys.openpgp.org.\n"
"Fingerprint: %1\n\n"
"%2\n"
"Please check your email (%3) for further "
"verification from keys.openpgp.org.\n\n"
"Note: For verification, you can find more "
"information here: "
"https://keys.openpgp.org/about")
.arg(fpr, status_message, email_list.join(", ")));
});
QObject::connect(
vks, &VKSInterface::SignalErrorOccurred, QThread::currentThread(),
[parent, key_id](const QString& error, const QString& data) {
QMessageBox::critical(
parent, QCoreApplication::translate("GTrC", "Key Upload Failed"),
QCoreApplication::translate(
"GTrC",
"Failed to upload public key to the server.\n"
"Fingerprint: %1\n"
"Error: %2")
.arg(key_id, error));
});
QObject::connect(vks, &VKSInterface::SignalKeyUploaded, vks,
&VKSInterface::deleteLater);
QObject::connect(vks, &VKSInterface::SignalErrorOccurred, vks,
&VKSInterface::deleteLater);
vks->UploadKey(key_text);
return 0;
}
auto UpdateKeyFromKeyServer(QWidget* parent, int channel, const QString& fpr)
-> int {
auto* vks = new VKSInterface();
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
QThread::currentThread(),
[parent, channel](const QString& key_data) {
auto data = key_data.toUtf8();
GFGpgImportKeys(channel, parent, data.constData(),
static_cast<int>(data.size()));
});
QObject::connect(
vks, &VKSInterface::SignalErrorOccurred, QThread::currentThread(),
[parent, fpr](const QString& error, const QString& data) {
QMessageBox::critical(
parent, QCoreApplication::translate("GTrC", "Key Update Failed"),
QCoreApplication::translate(
"GTrC",
"Failed to retrieve public key from the server.\n"
"Key ID: %1\n"
"Error: %2")
.arg(fpr, error));
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
vks->GetByFingerprint(fpr);
return 0;
}
} // namespace
REGISTER_EVENT_HANDLER(MAINWINDOW_MENU_MOUNTED, [](const MEvent& event) -> int {
LOG_DEBUG("main window menu mounted event: processing");
if (!event.contains("main_window")) {
LOG_DEBUG("main window menu mounted event: no main_window found");
CB_ERR(event, -1, "no main_window found");
}
auto* main_window = GFUIGetGUIObjectAs<QMainWindow>(event["main_window"]);
if (!main_window) {
LOG_ERROR(
"main window menu mounted: main_window handle invalid or not "
"QMainWindow");
CB_ERR(event, -1, "main_window handle invalid or not QMainWindow");
}
if (!event.contains("import_key_menu")) {
LOG_DEBUG("main window menu mounted event: no import_key_menu found");
CB_ERR(event, -1, "no import_key_menu found");
}
auto* import_key_menu = GFUIGetGUIObjectAs<QMenu>(event["import_key_menu"]);
if (!import_key_menu) {
LOG_ERROR(
"main window menu mounted: import_key_menu handle invalid or not "
"QMenu");
CB_ERR(event, -1, "import_key_menu handle invalid or not QMenu");
}
LOG_DEBUG("adding key server sync actions to import key menu");
QMetaObject::invokeMethod(
QApplication::instance(),
[&]() -> void {
QWidget* parent =
qobject_cast<QWidget*>(static_cast<QObject*>(main_window));
auto* action = new QAction(
QCoreApplication::translate("GTrC", "Key Server"), parent);
action->setToolTip(QCoreApplication::translate(
"GTrC", "Import public keys from a trusted key server."));
action->setIcon(QIcon(":/icons/import_key_from_server.png"));
QObject::connect(action, &QAction::triggered, parent, [=]() {
auto* dialog = new SearchKeyDialog(parent);
dialog->show();
});
import_key_menu->addAction(action);
},
Qt::BlockingQueuedConnection);
CB_SUCC(event);
});
REGISTER_EVENT_HANDLER(
KEY_PAIR_OPERA_MENU_CREATED, [](const MEvent& event) -> int {
auto* tab = GFUIGetGUIObjectAs<QWidget>(event["tab"]);
if (!tab) {
LOG_ERROR(
"key pair opera menu created: tab handle "
"invalid or not KeyPairOperaTab");
CB_ERR(event, -1, "tab handle invalid or not KeyPairOperaTab");
}
auto* layout = GFUIGetGUIObjectAs<QVBoxLayout>(event["opera_layout"]);
if (!layout) {
LOG_ERROR(
"key pair opera menu created: opera_menu handle "
"invalid or not QMenu");
CB_ERR(event, -1, "opera_menu handle invalid or not QMenu");
}
auto is_private_key = event["is_private_key"].toInt() != 0;
auto has_master_key = event["has_master_key"].toInt() != 0;
auto channel = event["channel"].toInt();
auto key_id = event["key_id"];
auto fpr = event["fpr"];
FLOG_DEBUG(
"adding key server sync actions: key id: %1, channel: %2, is "
"private key: %3, has master key: %4",
key_id, channel, static_cast<int>(is_private_key),
static_cast<int>(has_master_key));
QMetaObject::invokeMethod(QApplication::instance(), [=]() -> void {
auto* menu = new QMenu(tab);
auto* key_server_opera_button = new QPushButton(
QCoreApplication::translate("GTrC", "Key Server Operations"));
key_server_opera_button->setStyleSheet("text-align:center;");
key_server_opera_button->setMenu(menu);
// add upload / update key actions
auto* upload_key_pair = new QAction(QCoreApplication::translate(
"GTrC", "Publish Public Key to Key Server"));
QObject::connect(upload_key_pair, &QAction::triggered, tab,
[=]() { UploadKeyToServer(tab, channel, key_id); });
// Any key with a public part can be published, mirroring the Key
// Management "Publish Key to Keyserver" action.
auto* update_key_pair = new QAction(QCoreApplication::translate(
"GTrC", "Refresh Public Key From Key Server"));
QObject::connect(update_key_pair, &QAction::triggered, tab,
[=]() { UpdateKeyFromKeyServer(tab, channel, fpr); });
// Refresh re-imports the latest public key from the server; it is
// valid for any key, including your own.
menu->addAction(upload_key_pair);
menu->addAction(update_key_pair);
layout->addWidget(key_server_opera_button);
});
CB_SUCC(event);
});
REGISTER_EVENT_HANDLER(
REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT, [](const MEvent& event) -> int {
if (event["fingerprint"].isEmpty())
CB_ERR(event, -1, "fingerprint is empty");
QByteArray fingerprint = event["fingerprint"].toLatin1();
FLOG_DEBUG("try to get key info of fingerprint: %1", fingerprint);
auto* vks = new VKSInterface();
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
QThread::currentThread(), [event](const QString& key) {
// callback
CB(event, GFGetModuleID(),
{
{"ret", QString::number(0)},
{"key_data", key},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
QThread::currentThread(),
[event](const QString& error, const QString& data) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(-1)},
{"error_msg", error},
{"reply_data", data},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
vks->GetByFingerprint(fingerprint);
return 0;
});
REGISTER_EVENT_HANDLER(
REQUEST_GET_PUBLIC_KEY_BY_KEY_ID, [](const MEvent& event) -> int {
if (event["key_id"].isEmpty()) CB_ERR(event, -1, "key_id is empty");
QByteArray key_id = event["key_id"].toLatin1();
FLOG_DEBUG("try to get key info of key id: %1", key_id);
auto* vks = new VKSInterface();
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved,
QThread::currentThread(), [event](const QString& key) {
// callback
CB(event, GFGetModuleID(),
{
{"ret", QString::number(0)},
{"key_data", key},
});
});
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
QThread::currentThread(),
[event](const QString& error, const QString& data) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(-1)},
{"error_msg", error},
{"reply_data", data},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
vks->GetByKeyId(key_id);
return 0;
});
REGISTER_EVENT_HANDLER(
REQUEST_UPLOAD_PUBLIC_KEY, [](const MEvent& event) -> int {
if (event["key_text"].isEmpty()) CB_ERR(event, -1, "key_text is empty");
QByteArray key_text = event["key_text"].toLatin1();
FLOG_DEBUG("try to get key info of key id: %1", key_text);
auto* vks = new VKSInterface();
QObject::connect(
vks, &VKSInterface::SignalKeyUploaded, QThread::currentThread(),
[event](const QString& fpr, const QJsonObject& status,
const QString& token) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(0)},
{"fingerprint", fpr},
{"status",
QString::fromUtf8(QJsonDocument(status).toJson())},
{"token", token},
});
});
QObject::connect(vks, &VKSInterface::SignalErrorOccurred,
QThread::currentThread(),
[event](const QString& error, const QString& data) {
CB(event, GFGetModuleID(),
{
{"ret", QString::number(-1)},
{"error_msg", error},
{"reply_data", data},
});
});
QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks,
&VKSInterface::deleteLater);
vks->UploadKey(key_text);
return 0;
});
REGISTER_EVENT_HANDLER(
REQUEST_SEARCH_PUBLIC_KEY_BY_FINGERPRINT, [](const MEvent& event) -> int {
auto fingerprint = event["fingerprint"].trimmed();
QWidget* parent = nullptr;
if (event.contains("parent")) {
parent = GFUIGetGUIObjectAs<QWidget>(event["parent"]);
}
if (parent == nullptr) {
parent = QApplication::activeWindow();
}
FLOG_DEBUG("open key server search dialog with fingerprint: %1",
fingerprint);
QMetaObject::invokeMethod(
QApplication::instance(),
[parent, fingerprint]() {
auto* dialog = new SearchKeyDialog(parent);
// An empty preset opens a blank search dialog; only seed the field
// when a fingerprint was actually supplied (verify-failure flow).
if (!fingerprint.isEmpty()) {
dialog->SetPresetFingerprint(fingerprint);
}
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show();
dialog->raise();
dialog->activateWindow();
},
Qt::QueuedConnection);
CB_SUCC(event);
});
auto GFDeactivateModule() -> int { return 0; }
auto GFUnregisterModule() -> int {
MLogDebug("paper key module unregistering");
return 0;
}
|