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
|
/**
* 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]><[email protected]> starting on May 12, 2021.
*
*/
#include "ui/keypair_details/KeyPairDetailTab.h"
#include "ui/WaitingDialog.h"
KeyPairDetailTab::KeyPairDetailTab(GpgME::GpgContext *ctx, const GpgKey &mKey, QWidget *parent) : mKey(mKey),
QWidget(parent) {
mCtx = ctx;
keyid = new QString(mKey.id);
ownerBox = new QGroupBox(tr("Owner"));
keyBox = new QGroupBox(tr("Master Key"));
fingerprintBox = new QGroupBox(tr("Fingerprint"));
additionalUidBox = new QGroupBox(tr("Additional UIDs"));
nameVarLabel = new QLabel();
nameVarLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
emailVarLabel = new QLabel();
emailVarLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
commentVarLabel = new QLabel();
commentVarLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
keyidVarLabel = new QLabel();
keyidVarLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
usageVarLabel = new QLabel();
actualUsageVarLabel = new QLabel();
keySizeVarLabel = new QLabel();
expireVarLabel = new QLabel();
createdVarLabel = new QLabel();
algorithmVarLabel = new QLabel();
// Show the situation that master key not exists.
masterKeyExistVarLabel = new QLabel(mKey.has_master_key ? tr("Exists") : tr("Not Exists"));
if (!mKey.has_master_key) {
auto paletteExpired = masterKeyExistVarLabel->palette();
paletteExpired.setColor(masterKeyExistVarLabel->foregroundRole(), Qt::red);
masterKeyExistVarLabel->setPalette(paletteExpired);
} else {
auto paletteValid = masterKeyExistVarLabel->palette();
paletteValid.setColor(masterKeyExistVarLabel->foregroundRole(), Qt::darkGreen);
masterKeyExistVarLabel->setPalette(paletteValid);
}
if (mKey.expired) {
auto paletteExpired = expireVarLabel->palette();
paletteExpired.setColor(expireVarLabel->foregroundRole(), Qt::red);
expireVarLabel->setPalette(paletteExpired);
} else {
auto paletteValid = expireVarLabel->palette();
paletteValid.setColor(expireVarLabel->foregroundRole(), Qt::darkGreen);
expireVarLabel->setPalette(paletteValid);
}
auto *mvbox = new QVBoxLayout();
auto *vboxKD = new QGridLayout();
auto *vboxOD = new QGridLayout();
vboxOD->addWidget(new QLabel(tr("Name:")), 0, 0);
vboxOD->addWidget(new QLabel(tr("Email Address:")), 1, 0);
vboxOD->addWidget(new QLabel(tr("Comment:")), 2, 0);
vboxOD->addWidget(nameVarLabel, 0, 1);
vboxOD->addWidget(emailVarLabel, 1, 1);
vboxOD->addWidget(commentVarLabel, 2, 1);
vboxKD->addWidget(new QLabel(tr("Key ID: ")), 0, 0);
vboxKD->addWidget(new QLabel(tr("Algorithm: ")), 1, 0);
vboxKD->addWidget(new QLabel(tr("Key Size:")), 2, 0);
vboxKD->addWidget(new QLabel(tr("Nominal Usage: ")), 3, 0);
vboxKD->addWidget(new QLabel(tr("Actual Usage: ")), 4, 0);
vboxKD->addWidget(new QLabel(tr("Expires on: ")), 5, 0);
vboxKD->addWidget(new QLabel(tr("Last Update: ")), 6, 0);
vboxKD->addWidget(new QLabel(tr("Secret Key Existence: ")), 7, 0);
vboxKD->addWidget(keySizeVarLabel, 2, 1);
vboxKD->addWidget(expireVarLabel, 5, 1);
vboxKD->addWidget(algorithmVarLabel, 1, 1);
vboxKD->addWidget(createdVarLabel, 6, 1);
vboxKD->addWidget(keyidVarLabel, 0, 1);
vboxKD->addWidget(usageVarLabel, 3, 1);
vboxKD->addWidget(actualUsageVarLabel, 4, 1);
vboxKD->addWidget(masterKeyExistVarLabel, 7, 1);
ownerBox->setLayout(vboxOD);
mvbox->addWidget(ownerBox);
keyBox->setLayout(vboxKD);
mvbox->addWidget(keyBox);
fingerPrintVarLabel = new QLabel(beautifyFingerprint(mKey.fpr));
fingerPrintVarLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
fingerPrintVarLabel->setStyleSheet("margin-left: 0; margin-right: 5;");
auto *hboxFP = new QHBoxLayout();
hboxFP->addWidget(fingerPrintVarLabel);
auto *copyFingerprintButton = new QPushButton(tr("Copy"));
copyFingerprintButton->setFlat(true);
copyFingerprintButton->setToolTip(tr("copy fingerprint to clipboard"));
connect(copyFingerprintButton, SIGNAL(clicked()), this, SLOT(slotCopyFingerprint()));
hboxFP->addWidget(copyFingerprintButton);
fingerprintBox->setLayout(hboxFP);
mvbox->addWidget(fingerprintBox);
mvbox->addStretch();
if (mKey.is_private_key) {
auto *privKeyBox = new QGroupBox(tr("Operations"));
auto *vboxPK = new QVBoxLayout();
auto *exportButton = new QPushButton(tr("Export Private Key (Include Subkey)"));
vboxPK->addWidget(exportButton);
connect(exportButton, SIGNAL(clicked()), this, SLOT(slotExportPrivateKey()));
if (mKey.has_master_key) {
auto *editExpiresButton = new QPushButton(tr("Modify Expiration Datetime (Master Key)"));
vboxPK->addWidget(editExpiresButton);
connect(editExpiresButton, SIGNAL(clicked()), this, SLOT(slotModifyEditDatetime()));
auto hBoxLayout = new QHBoxLayout();
auto *keyServerOperaButton = new QPushButton(tr("Key Server Operation (Pubkey)"));
keyServerOperaButton->setStyleSheet("text-align:center;");
auto *revokeCertGenButton = new QPushButton(tr("Generate Revoke Certificate"));
revokeCertGenButton->setDisabled(true);
connect(revokeCertGenButton, SIGNAL(clicked()), this, SLOT(slotGenRevokeCert()));
hBoxLayout->addWidget(keyServerOperaButton);
hBoxLayout->addWidget(revokeCertGenButton);
vboxPK->addLayout(hBoxLayout);
connect(keyServerOperaButton, SIGNAL(clicked()), this, SLOT(slotModifyEditDatetime()));
// Set Menu
createKeyServerOperaMenu();
keyServerOperaButton->setMenu(keyServerOperaMenu);
}
privKeyBox->setLayout(vboxPK);
mvbox->addWidget(privKeyBox);
}
if ((mKey.expired) || (mKey.revoked)) {
auto *expBox = new QHBoxLayout();
QPixmap pixmap(":warning.png");
auto *expLabel = new QLabel();
auto *iconLabel = new QLabel();
if (mKey.expired) {
expLabel->setText(tr("Warning: The Master Key has expired."));
}
if (mKey.revoked) {
expLabel->setText(tr("Warning: The Master Key has been revoked"));
}
iconLabel->setPixmap(pixmap.scaled(24, 24, Qt::KeepAspectRatio));
QFont font = expLabel->font();
font.setBold(true);
expLabel->setFont(font);
expLabel->setAlignment(Qt::AlignCenter);
expBox->addWidget(iconLabel);
expBox->addWidget(expLabel);
mvbox->addLayout(expBox);
}
mvbox->setContentsMargins(0, 0, 0, 0);
connect(mCtx, SIGNAL(signalKeyInfoChanged()), this, SLOT(slotRefreshKeyInfo()));
slotRefreshKeyInfo();
setAttribute(Qt::WA_DeleteOnClose, true);
setLayout(mvbox);
}
void KeyPairDetailTab::slotExportPrivateKey() {
// Show a information box with explanation about private key
int ret = QMessageBox::information(this, tr("Exporting private Key"),
"<h3>" + tr("You are about to export your") + "<font color=\"red\">" +
tr("PRIVATE KEY") + "</font>!</h3>\n" +
tr("This is NOT your Public Key, so DON'T give it away.") + "<br />" +
tr("Do you REALLY want to export your PRIVATE KEY?"),
QMessageBox::Cancel | QMessageBox::Ok);
// export key, if ok was clicked
if (ret == QMessageBox::Ok) {
auto *keyArray = new QByteArray();
if (!mCtx->exportSecretKey(mKey, keyArray)) {
QMessageBox::critical(this, "Error", "An error occurred during the export operation.");
return;
}
auto &key = mCtx->getKeyById(*keyid);
QString fileString = key.name + " " + key.email + "(" +
key.id + ")_secret.asc";
QString fileName = QFileDialog::getSaveFileName(this, tr("Export Key To File"), fileString,
tr("Key Files") + " (*.asc *.txt);;All Files (*)");
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::critical(nullptr, tr("Export Error"), tr("Couldn't open %1 for writing").arg(fileName));
return;
}
QTextStream stream(&file);
stream << *keyArray;
file.close();
delete keyArray;
}
}
QString KeyPairDetailTab::beautifyFingerprint(QString fingerprint) {
uint len = fingerprint.length();
if ((len > 0) && (len % 4 == 0))
for (uint n = 0; 4 * (n + 1) < len; ++n)
fingerprint.insert(static_cast<int>(5u * n + 4u), ' ');
return fingerprint;
}
void KeyPairDetailTab::slotCopyFingerprint() {
QString fpr = fingerPrintVarLabel->text().trimmed().replace(" ", "");
QClipboard *cb = QApplication::clipboard();
cb->setText(fpr);
}
void KeyPairDetailTab::slotModifyEditDatetime() {
auto dialog = new KeySetExpireDateDialog(mCtx, mKey, nullptr, this);
dialog->show();
}
void KeyPairDetailTab::slotRefreshKeyInfo() {
nameVarLabel->setText(mKey.name);
emailVarLabel->setText(mKey.email);
commentVarLabel->setText(mKey.comment);
keyidVarLabel->setText(mKey.id);
QString usage;
QTextStream usage_steam(&usage);
if (mKey.can_certify)
usage_steam << "Cert ";
if (mKey.can_encrypt)
usage_steam << "Encr ";
if (mKey.can_sign)
usage_steam << "Sign ";
if (mKey.can_authenticate)
usage_steam << "Auth ";
usageVarLabel->setText(usage);
QString actualUsage;
QTextStream actual_usage_steam(&actualUsage);
if (GpgME::GpgContext::checkIfKeyCanCert(mKey))
actual_usage_steam << "Cert ";
if (GpgME::GpgContext::checkIfKeyCanEncr(mKey))
actual_usage_steam << "Encr ";
if (GpgME::GpgContext::checkIfKeyCanSign(mKey))
actual_usage_steam << "Sign ";
if (GpgME::GpgContext::checkIfKeyCanAuth(mKey))
actual_usage_steam << "Auth ";
actualUsageVarLabel->setText(actualUsage);
QString keySizeVal, keyExpireVal, keyCreateTimeVal, keyAlgoVal;
keySizeVal = QString::number(mKey.length);
if (mKey.expires.toTime_t() == 0) {
keyExpireVal = tr("Never Expire");
} else {
keyExpireVal = mKey.expires.toString();
}
keyAlgoVal = mKey.pubkey_algo;
keyCreateTimeVal = mKey.create_time.toString();
keySizeVarLabel->setText(keySizeVal);
expireVarLabel->setText(keyExpireVal);
createdVarLabel->setText(keyCreateTimeVal);
algorithmVarLabel->setText(keyAlgoVal);
fingerPrintVarLabel->setText(beautifyFingerprint(mKey.fpr));
}
void KeyPairDetailTab::createKeyServerOperaMenu() {
keyServerOperaMenu = new QMenu(this);
auto *uploadKeyPair = new QAction(tr("Upload Key Pair to Key Server"), this);
connect(uploadKeyPair, SIGNAL(triggered()), this, SLOT(slotUploadKeyToServer()));
auto *updateKeyPair = new QAction(tr("Update Key Pair"), this);
connect(updateKeyPair, SIGNAL(triggered()), this, SLOT(slotUpdateKeyToServer()));
keyServerOperaMenu->addAction(uploadKeyPair);
// TODO Solve Refresh Problem
// keyServerOperaMenu->addAction(updateKeyPair);
}
void KeyPairDetailTab::slotUploadKeyToServer() {
QVector<GpgKey> keys;
keys.append(mKey);
auto *dialog = new KeyUploadDialog(mCtx, keys);
}
void KeyPairDetailTab::slotUpdateKeyToServer() {
QVector<GpgKey> keys;
keys.append(mKey);
auto *dialog = new KeyServerImportDialog(mCtx, this);
dialog->show();
dialog->slotImportKey(keys);
}
void KeyPairDetailTab::slotGenRevokeCert() {
auto mOutputFileName = QFileDialog::getSaveFileName(this, tr("Generate revocation certificate"),
QString(),
QStringLiteral("%1 (*.rev)").arg(
tr("Revocation Certificates")));
auto process = mCtx->generateRevokeCert(mKey, mOutputFileName);
auto *dialog = new WaitingDialog("Generating", this);
while (process->state() == QProcess::Running) {
QApplication::processEvents();
}
dialog->close();
}
|