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
|
/**
* Copyright (C) 2021-2024 Saturneric <[email protected]>
*
* 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 <[email protected]> starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include "KeyPairDetailTab.h"
#include "core/function/GlobalSettingStation.h"
#include "core/function/gpg/GpgKeyGetter.h"
#include "core/model/GpgKey.h"
#include "core/module/ModuleManager.h"
#include "core/utils/CommonUtils.h"
#include "ui/UISignalStation.h"
namespace GpgFrontend::UI {
KeyPairDetailTab::KeyPairDetailTab(int channel, GpgKeyPtr key, QWidget* parent)
: QWidget(parent),
current_gpg_context_channel_(channel),
key_(std::move(key)) {
assert(key_->IsGood());
owner_box_ = new QGroupBox(tr("Owner"));
key_box_ = new QGroupBox(tr("Primary Key"));
fingerprint_box_ = new QGroupBox(tr("Fingerprint"));
additional_uid_box_ = new QGroupBox(tr("Additional UIDs"));
name_var_label_ = new QLabel();
name_var_label_->setTextInteractionFlags(Qt::TextSelectableByMouse);
email_var_label_ = new QLabel();
email_var_label_->setTextInteractionFlags(Qt::TextSelectableByMouse);
comment_var_label_ = new QLabel();
comment_var_label_->setTextInteractionFlags(Qt::TextSelectableByMouse);
key_id_var_label_ = new QLabel();
key_id_var_label_->setTextInteractionFlags(Qt::TextSelectableByMouse);
usage_var_label_ = new QLabel();
actual_usage_var_label_ = new QLabel();
owner_trust_var_label_ = new QLabel();
key_size_var_label_ = new QLabel();
expire_var_label_ = new QLabel();
created_var_label_ = new QLabel();
last_update_var_label_ = new QLabel();
algorithm_var_label_ = new QLabel();
algorithm_detail_var_label_ = new QLabel();
primary_key_exist_var_label_ = new QLabel();
auto* mvbox = new QVBoxLayout();
auto* vbox_kd = new QGridLayout();
auto* vbox_od = new QGridLayout();
vbox_od->addWidget(new QLabel(tr("Name") + ": "), 0, 0);
vbox_od->addWidget(new QLabel(tr("Email Address") + ": "), 1, 0);
vbox_od->addWidget(new QLabel(tr("Comment") + ": "), 2, 0);
vbox_od->addWidget(name_var_label_, 0, 1);
vbox_od->addWidget(email_var_label_, 1, 1);
vbox_od->addWidget(comment_var_label_, 2, 1);
vbox_kd->addWidget(new QLabel(tr("Key ID") + ": "), 0, 0);
vbox_kd->addWidget(new QLabel(tr("Algorithm") + ": "), 1, 0);
vbox_kd->addWidget(new QLabel(tr("Algorithm Detail") + ": "), 2, 0);
vbox_kd->addWidget(new QLabel(tr("Key Size") + ": "), 3, 0);
vbox_kd->addWidget(new QLabel(tr("Nominal Usage") + ": "), 4, 0);
vbox_kd->addWidget(new QLabel(tr("Actual Usage") + ": "), 5, 0);
vbox_kd->addWidget(new QLabel(tr("Owner Trust Level") + ": "), 6, 0);
vbox_kd->addWidget(new QLabel(tr("Create Date (Local Time)") + ": "), 7, 0);
vbox_kd->addWidget(new QLabel(tr("Expires on (Local Time)") + ": "), 8, 0);
vbox_kd->addWidget(new QLabel(tr("Last Update (Local Time)") + ": "), 9, 0);
vbox_kd->addWidget(new QLabel(tr("Primary Key Existence") + ": "), 10, 0);
key_id_var_label_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
vbox_kd->addWidget(key_id_var_label_, 0, 1, 1, 1);
vbox_kd->addWidget(algorithm_var_label_, 1, 1, 1, 2);
vbox_kd->addWidget(algorithm_detail_var_label_, 2, 1, 1, 2);
vbox_kd->addWidget(key_size_var_label_, 3, 1, 1, 2);
vbox_kd->addWidget(usage_var_label_, 4, 1, 1, 2);
vbox_kd->addWidget(actual_usage_var_label_, 5, 1, 1, 2);
vbox_kd->addWidget(owner_trust_var_label_, 6, 1, 1, 2);
vbox_kd->addWidget(created_var_label_, 7, 1, 1, 2);
vbox_kd->addWidget(expire_var_label_, 8, 1, 1, 2);
vbox_kd->addWidget(last_update_var_label_, 9, 1, 1, 2);
vbox_kd->addWidget(primary_key_exist_var_label_, 10, 1, 1, 2);
auto* copy_key_id_button = new QPushButton(tr("Copy"));
copy_key_id_button->setFlat(true);
copy_key_id_button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
vbox_kd->addWidget(copy_key_id_button, 0, 2);
connect(copy_key_id_button, &QPushButton::clicked, this, [=]() {
QString fpr = key_id_var_label_->text().trimmed();
QClipboard* cb = QApplication::clipboard();
cb->setText(fpr);
});
owner_box_->setLayout(vbox_od);
mvbox->addWidget(owner_box_);
key_box_->setLayout(vbox_kd);
mvbox->addWidget(key_box_);
fingerprint_var_label_ = new QLabel();
fingerprint_var_label_->setWordWrap(false);
fingerprint_var_label_->setTextInteractionFlags(Qt::TextSelectableByMouse);
fingerprint_var_label_->setStyleSheet("margin-left: 0; margin-right: 5;");
fingerprint_var_label_->setAlignment(Qt::AlignCenter);
fingerprint_var_label_->setMinimumWidth(400);
auto* hbox_fp = new QHBoxLayout();
hbox_fp->addStretch();
hbox_fp->addWidget(fingerprint_var_label_);
auto* copy_fingerprint_button = new QPushButton(tr("Copy"));
copy_fingerprint_button->setFlat(true);
copy_fingerprint_button->setToolTip(tr("copy fingerprint to clipboard"));
connect(copy_fingerprint_button, &QPushButton::clicked, this,
&KeyPairDetailTab::slot_copy_fingerprint);
hbox_fp->addWidget(copy_fingerprint_button);
hbox_fp->addStretch();
fingerprint_box_->setLayout(hbox_fp);
mvbox->addWidget(fingerprint_box_);
mvbox->addStretch();
auto* exp_box = new QHBoxLayout();
QPixmap pixmap(":/icons/warning.png");
exp_label_ = new QLabel();
icon_label_ = new QLabel();
icon_label_->setPixmap(pixmap.scaled(24, 24, Qt::KeepAspectRatio));
exp_label_->setAlignment(Qt::AlignCenter);
exp_box->addStretch();
exp_box->addWidget(icon_label_);
exp_box->addWidget(exp_label_);
exp_box->addStretch();
mvbox->addLayout(exp_box);
mvbox->setContentsMargins(0, 0, 0, 0);
// when key database updated
connect(UISignalStation::GetInstance(),
&UISignalStation::SignalKeyDatabaseRefreshDone, this,
&KeyPairDetailTab::slot_refresh_key);
slot_refresh_key_info();
setAttribute(Qt::WA_DeleteOnClose, true);
setLayout(mvbox);
}
void KeyPairDetailTab::slot_copy_fingerprint() {
QString fpr =
fingerprint_var_label_->text().trimmed().replace(" ", QString());
QClipboard* cb = QApplication::clipboard();
cb->setText(fpr);
}
void KeyPairDetailTab::slot_refresh_key_info() {
// Show the situation that primary key not exists.
primary_key_exist_var_label_->setText(
key_->IsHasMasterKey() ? tr("Exists") : tr("Not Exists"));
if (!key_->IsHasMasterKey()) {
auto palette_expired = primary_key_exist_var_label_->palette();
palette_expired.setColor(primary_key_exist_var_label_->foregroundRole(),
Qt::red);
primary_key_exist_var_label_->setPalette(palette_expired);
} else {
auto palette_valid = primary_key_exist_var_label_->palette();
palette_valid.setColor(primary_key_exist_var_label_->foregroundRole(),
Qt::darkGreen);
primary_key_exist_var_label_->setPalette(palette_valid);
}
if (key_->IsExpired()) {
auto palette_expired = expire_var_label_->palette();
palette_expired.setColor(expire_var_label_->foregroundRole(), Qt::red);
expire_var_label_->setPalette(palette_expired);
} else {
auto palette_valid = expire_var_label_->palette();
palette_valid.setColor(expire_var_label_->foregroundRole(), Qt::darkGreen);
expire_var_label_->setPalette(palette_valid);
}
name_var_label_->setText(key_->Name());
email_var_label_->setText(key_->Email());
comment_var_label_->setText(key_->Comment());
key_id_var_label_->setText(key_->ID());
QString buffer;
QTextStream usage_steam(&buffer);
if (key_->IsHasCertCap()) {
usage_steam << tr("Certificate") << " ";
}
if (key_->IsHasEncrCap()) usage_steam << tr("Encrypt") << " ";
if (key_->IsHasSignCap()) usage_steam << tr("Sign") << " ";
if (key_->IsHasAuthCap()) usage_steam << tr("Auth") << " ";
usage_var_label_->setText(usage_steam.readAll());
QString buffer_2;
QTextStream actual_usage_steam(&buffer_2);
if (key_->IsHasActualCertCap()) {
actual_usage_steam << tr("Certificate") << " ";
}
if (key_->IsHasActualEncrCap()) {
actual_usage_steam << tr("Encrypt") << " ";
}
if (key_->IsHasActualSignCap()) {
actual_usage_steam << tr("Sign") << " ";
}
if (key_->IsHasActualAuthCap()) {
actual_usage_steam << tr("Auth") << " ";
}
actual_usage_var_label_->setText(actual_usage_steam.readAll());
owner_trust_var_label_->setText(key_->OwnerTrust());
QString key_size_val;
QString key_expire_val;
QString key_create_time_val;
QString key_algo_val;
QString key_algo_detail_val;
QString key_last_update_val;
key_size_val = QString::number(key_->PrimaryKeyLength());
if (key_->ExpirationTime().toSecsSinceEpoch() == 0) {
expire_var_label_->setText(tr("Never Expire"));
} else {
expire_var_label_->setText(QLocale().toString((key_->ExpirationTime())));
}
key_algo_val = key_->PublicKeyAlgo();
key_algo_detail_val = key_->Algo();
created_var_label_->setText(QLocale().toString(key_->CreationTime()));
if (key_->LastUpdateTime().toSecsSinceEpoch() == 0) {
last_update_var_label_->setText(tr("No Data"));
} else {
last_update_var_label_->setText(QLocale().toString(key_->LastUpdateTime()));
}
key_size_var_label_->setText(key_size_val);
algorithm_var_label_->setText(key_algo_val);
algorithm_detail_var_label_->setText(key_algo_detail_val);
fingerprint_var_label_->setText(BeautifyFingerprint(key_->Fingerprint()));
fingerprint_var_label_->setWordWrap(true); // for x448 and ed448
icon_label_->hide();
exp_label_->hide();
if (key_->IsExpired()) {
slot_refresh_notice(":/icons/warning.png",
tr("Warning: The primary key has expired."));
} else if (key_->IsRevoked()) {
slot_refresh_notice(":/icons/warning.png",
tr("Warning: The primary key has been revoked."));
} else if (key_->IsPrivateKey() && !key_->IsHasMasterKey()) {
slot_refresh_notice(":/icons/warning.png",
tr("Warning: The primary key is not exists."));
} else {
slot_query_key_publish_state();
}
}
void KeyPairDetailTab::slot_refresh_key() {
// refresh the key
auto refreshed_key = GpgKeyGetter::GetInstance(current_gpg_context_channel_)
.GetKeyPtr(key_->ID());
assert(refreshed_key != nullptr);
std::swap(this->key_, refreshed_key);
this->slot_refresh_key_info();
}
void KeyPairDetailTab::slot_query_key_publish_state() {
bool forbid_all_gnupg_connection =
GetSettings().value("network/forbid_all_gnupg_connection").toBool();
bool auto_fetch_key_publish_status =
GetSettings().value("network/auto_fetch_key_publish_status").toBool();
if (forbid_all_gnupg_connection || !auto_fetch_key_publish_status) return;
if (!Module::IsModuleActivate(kKeyServerSyncModuleID)) {
return;
}
const auto fpr = key_->Fingerprint();
Module::TriggerEvent(
"REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT",
{
{"fingerprint", QString(fpr)},
},
[=](Module::EventIdentifier i, Module::Event::ListenerIdentifier ei,
Module::Event::Params p) {
LOG_D() << "REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT callback: " << i
<< ei;
if (p["ret"] != "0" || !p["error_msg"].isEmpty()) {
LOG_E() << "An error occurred trying to get data from key:" << fpr
<< "error message: " << p["error_msg"]
<< "reply data: " << p["reply_data"];
} else if (p.contains("key_data")) {
const auto key_data = p["key_data"];
LOG_D() << "got key data of key " << fpr
<< " from key server: " << key_data;
if (!key_data.isEmpty()) {
slot_refresh_notice(
":/icons/publish.png",
tr("Notice: The public key has been published on "
"keys.openpgp.org."));
}
}
});
}
void KeyPairDetailTab::slot_refresh_notice(const QString& icon,
const QString& info) {
icon_label_->hide();
exp_label_->hide();
if (!icon.isEmpty()) {
QPixmap pixmap(icon);
icon_label_->setPixmap(pixmap.scaled(24, 24, Qt::KeepAspectRatio));
icon_label_->show();
}
if (!info.isEmpty()) {
exp_label_->setText(info);
exp_label_->show();
}
}
} // namespace GpgFrontend::UI
|