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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
/*
* gpgcontext.cpp
*
* Copyright 2008 gpg4usb-team <[email protected]>
*
* This file is part of gpg4usb.
*
* Gpg4usb 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.
*
* Gpg4usb 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 gpg4usb. If not, see <http://www.gnu.org/licenses/>
*/
#include "gpgcontext.h"
#include "kgpg/gpgproc.h"
#include "kgpg/kgpginterface.h"
#include "kgpg/klinebufferedprocess.h"
#include "kgpg/core/kgpgkey.h"
#include "kgpg/transactions/kgpgencrypt.h"
#ifdef _WIN32
#include <windows.h>
#include <unistd.h> /* contains read/write */
#endif
class QMessageBox;
namespace GpgME
{
/** Constructor
* Set up gpgme-context, set paths to app-run path
*/
GpgContext::GpgContext()
{
connect(this,SIGNAL(keyDBChanged()),this,SLOT(slotRefreshKeyList()));
slotRefreshKeyList();
}
/** Destructor
*/
GpgContext::~GpgContext()
{
}
KgpgCore::KgpgKey GpgContext::getKeyDetails(QString uid) {
KgpgCore::KgpgKeyList keys;
keys = KgpgInterface::readPublicKeys(QStringList() << uid);
KgpgCore::KgpgKey key = keys.first();
return key;
}
/** List all availabe Keys (VERY much like kgpgme)
*/
GpgKeyList GpgContext::listKeys()
{
GpgKeyList keys;
foreach(KgpgCore::KgpgKey kkey, KgpgInterface::readPublicKeys()) {
GpgKey key;
key.email = kkey.email();
//key.expired = kkey.expirationDate().toString();
key.expired = (kkey.expirationDate().date() < QDate::currentDate() &! kkey.expirationDate().isNull());
key.fpr = kkey.fingerprint();
key.id = kkey.id();
key.fullid = kkey.fullId();
key.name = kkey.name();
key.revoked = !kkey.valid();
keys.append(key);
}
foreach(KgpgCore::KgpgKey skey, KgpgInterface::readSecretKeys()) {
// iterate keys, mark privates
GpgKeyList::iterator it = keys.begin();
while (it != keys.end()) {
if (skey.id() == it->id) {
it->privkey = true;
}
it++;
}
}
return keys;
}
// TODO: do we really need to call gpg or could we use an already existing list
bool GpgContext::isSecretKey(QString uid) {
foreach(KgpgCore::KgpgKey skey, KgpgInterface::readSecretKeys()) {
if(skey.id() == uid) return true;
}
return false;
}
/** also from kgpgme.cpp, seems to clear password from mem */
void GpgContext::clearPasswordCache()
{
if (mPasswordCache.size() > 0) {
mPasswordCache.fill('\0');
mPasswordCache.truncate(0);
}
}
/*
* if there is no '\n' before the PGP-Begin-Block, but for example a whitespace,
* GPGME doesn't recognise the Message as encrypted. This function adds '\n'
* before the PGP-Begin-Block, if missing.
*/
void GpgContext::preventNoDataErr(QByteArray *in)
{
int block_start = in->indexOf(GpgConstants::PGP_CRYPT_BEGIN);
if (block_start > 0 && in->at(block_start - 1) != '\n') {
in->insert(block_start, '\n');
}
block_start = in->indexOf(GpgConstants::PGP_SIGNED_BEGIN);
if (block_start > 0 && in->at(block_start - 1) != '\n') {
in->insert(block_start, '\n');
}
}
/*
* isSigned returns:
* - 0, if text isn't signed at all
* - 1, if text is partially signed
* - 2, if text is completly signed
*/
int GpgContext::textIsSigned(const QByteArray &text) {
if (text.trimmed().startsWith(GpgConstants::PGP_SIGNED_BEGIN) && text.trimmed().endsWith(GpgConstants::PGP_SIGNED_END)) {
return 2;
}
if (text.contains(GpgConstants::PGP_SIGNED_BEGIN) && text.contains(GpgConstants::PGP_SIGNED_END)) {
return 1;
}
return 0;
}
QString GpgContext::beautifyFingerprint(QString fingerprint)
{
uint len = fingerprint.length();
if ((len > 0) && (len % 4 == 0))
for (uint n = 0; 4 *(n + 1) < len; ++n)
fingerprint.insert(5 * n + 4, ' ');
return fingerprint;
}
void GpgContext::slotRefreshKeyList() {
mKeyList = this->listKeys();
}
/**
* note: privkey status is not returned
*/
GpgKey GpgContext::getKeyByFpr(QString fpr) {
foreach (GpgKey key, mKeyList) {
if(key.fpr == fpr) {
return key;
}
}
return GpgKey();
}
/**
* note: privkey status is not returned
* TODO: Long Ids also should be supported
*/
GpgKey GpgContext::getKeyById(QString id) {
foreach (GpgKey key, mKeyList) {
if(key.id == id || key.fullid == id) {
return key;
}
}
return GpgKey();
}
void GpgContext::emitKeyDBChanged() {
emit keyDBChanged();
}
// TODO: from kgpgverify, merge back
static QString
sigTimeMessage(const QString &sigtime)
{
QDateTime stamp;
if (sigtime.contains(QLatin1Char('T'))) {
stamp = QDateTime::fromString(sigtime, Qt::ISODate);
} else {
bool ok;
qint64 secs = sigtime.toLongLong(&ok);
if (ok)
stamp = QDateTime::fromMSecsSinceEpoch(secs * 1000);
}
if (!stamp.isValid())
return QString();
return QObject::tr("The signature was created at %1 %2")
.arg(stamp.date().toString())
.arg(stamp.time().toString() + QLatin1String("<br/>")
, "first argument is formatted date, second argument is formatted time");
}
void GpgContext::exportKeyToFile(const QStringList &keyList)
{
if (keyList.size() < 1) return;
QStringList expopts;
KGpgExport *exp = new KGpgExport(this, keyList, expopts);
connect(exp, SIGNAL(done(int)), SLOT(slotExportKeyToFileReady(int)));
exp->start();
}
void GpgContext::slotExportKeyToFileReady(int result)
{
KGpgExport *exp = qobject_cast<KGpgExport *>(sender());
Q_ASSERT(exp != NULL);
if (result == KGpgTransaction::TS_OK) {
GpgKey key = this->getKeyById(exp->getKeyIds().first());
QString fileString = key.name + " " + key.email + "(" + key.id+ ")_pub.asc";
QString fileName = QFileDialog::getSaveFileName(NULL, tr("Export Key To File"), fileString, tr("Key Files") + " (*.asc *.txt);;All Files (*)");
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream stream(&file);
QByteArray keyArray = exp->getOutputData();
qDebug() << *keyArray;
stream << keyArray;
file.close();
//emit statusBarChanged(QString(tr("key(s) exported")));
} else {
//KMessageBox::sorry(this, i18n("Your public key could not be exported\nCheck the key."));
qDebug() << "Your public key could not be exported\nCheck the key.";
}
exp->deleteLater();
}
void GpgContext::exportPrivateKey(const QString &keyid)
{
// Show a information box with explanation about private key
int ret = QMessageBox::information(NULL, tr("Exporting private Key"),
tr("You are about to export your private key.\n"
"This is NOT your public key, so don't give it away.\n"
"Make sure you keep it save."
"Do you really want to export your private key?"),
QMessageBox::Cancel | QMessageBox::Ok);
// export key, if ok was clicked
if (ret == QMessageBox::Ok) {
/* QByteArray *keyArray = new QByteArray();
mCtx->exportSecretKey(*keyid, keyArray);
KgpgCore::KgpgKey key = mCtx->getKeyDetails(*keyid);
QString fileString = key.name() + " " + key.email() + "(" + key.id()+ ")_pub_sec.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(0,tr("Export error"),tr("Couldn't open %1 for writing").arg(fileName));
return;
}
QTextStream stream(&file);
stream << *keyArray;
file.close();
delete keyArray;*/
KgpgCore::KgpgKey key = this->getKeyDetails(keyid);
QString fileString = key.name() + " " + key.email() + "(" + key.id()+ ")_pub_sec.asc";
QString fileName = QFileDialog::getSaveFileName(NULL, tr("Export Key To File"), fileString, tr("Key Files") + " (*.asc *.txt);;All Files (*)");
QStringList expopts;
expopts.append(QLatin1String( "--armor" ));
KGpgExport *exp = new KGpgExport(this, QStringList() << keyid, fileName, expopts, true);
connect(exp, SIGNAL(done(int)), SLOT(slotExportPrivateKeyDone(int)));
exp->start();
}
}
void GpgContext::slotExportPrivateKeyDone(int result) {
KGpgExport *exp = qobject_cast<KGpgExport *>(sender());
Q_ASSERT(exp != NULL);
if (result == KGpgTransaction::TS_OK) {
qDebug() << "export seems ok";
} else {
qDebug() << "Your key could not be exported\nCheck the key.";
}
exp->deleteLater();
}
// import for text based keys
void GpgContext::slotImportKeys(QString text)
{
KGpgImport *imp = new KGpgImport(this, text);
connect(imp, SIGNAL(done(int)), SLOT(slotImportDone(int)));
imp->start();
}
void GpgContext::slotImportDone(int result)
{
KGpgImport *import = qobject_cast<KGpgImport *>(sender());
Q_ASSERT(import != NULL);
const QStringList rawmsgs(import->getMessages());
if (result != 0) {
/*KMessageBox::detailedSorry(this, i18n("Key importing failed. Please see the detailed log for more information."),
rawmsgs.join( QLatin1String( "\n")) , i18n("Key Import" ));*/
qDebug() << "Key importing failed. Please see the detailed log for more information." << rawmsgs.join( QLatin1String( "\n"));
}
QStringList keys(import->getImportedIds(0x1f));
const bool needsRefresh = !keys.isEmpty();
keys << import->getImportedIds(0);
/*
if (!keys.isEmpty()) {
const QString msg(import->getImportMessage());
const QStringList keynames(import->getImportedKeys());
new KgpgDetailedInfo(this, msg, rawmsgs.join( QLatin1String( "\n") ), keynames, i18n("Key Import" ));
if (needsRefresh)
imodel->refreshKeys(keys);
else
changeMessage(i18nc("Application ready for user input", "Ready"));
} else{
changeMessage(i18nc("Application ready for user input", "Ready"));
}
*/
//changeMessage(tr("Application ready for user input", "Ready"));
emitKeyDBChanged();
import->deleteLater();
}
void GpgContext::slotimportKeyFromFile()
{
QString fileName = QFileDialog::getOpenFileName(NULL, tr("Open Key"), "", tr("Key Files") + " (*.asc *.txt);;"+tr("Keyring files")+" (*.gpg);;All Files (*)");
if (! fileName.isNull()) {
QList<QUrl> urlList;
urlList << QUrl::fromLocalFile(fileName);
KGpgImport *imp = new KGpgImport(this, urlList);
connect(imp, SIGNAL(done(int)), SLOT(slotImportDone(int)));
imp->start();
}
}
void GpgContext::slotImportKeyFromClipboard()
{
QClipboard *cb = QApplication::clipboard();
slotImportKeys(cb->text(QClipboard::Clipboard).toAscii());
}
QString GpgContext::getReport(const QStringList &log)
{
QString result;
// newer versions of GnuPG emit both VALIDSIG and GOODSIG
// for a good signature. Since VALIDSIG has more information
// we use that.
const QRegExp validsig(QLatin1String("^\\[GNUPG:\\] VALIDSIG([ ]+[^ ]+){10,}.*$"));
//const bool useGoodSig = (model != NULL) && (log.indexOf(validsig) == -1);
const bool useGoodSig = log.indexOf(validsig) == -1;
QString sigtime; // timestamp of signature creation
foreach (const QString &line, log) {
if (!line.startsWith(QLatin1String("[GNUPG:] ")))
continue;
const QString msg = line.mid(9);
if (msg.startsWith(QLatin1String("VALIDSIG ")) && !useGoodSig) {
// from GnuPG source, doc/DETAILS:
// VALIDSIG <fingerprint in hex> <sig_creation_date> <sig-timestamp>
// <expire-timestamp> <sig-version> <reserved> <pubkey-algo>
// <hash-algo> <sig-class> <primary-key-fpr>
const QStringList vsig = msg.mid(9).split(QLatin1Char(' '), QString::SkipEmptyParts);
Q_ASSERT(vsig.count() >= 10);
//result += vsig[9];
GpgKey key = getKeyByFpr(vsig[9]);
// ignore for now if this is signed with the primary id (vsig[0] == vsig[9]) or not
if (key.email.isEmpty()) {
result += tr("<qt>Good signature from:<br /><b>%1</b><br />Key ID: %2<br /></qt>")
.arg(key.name).arg(vsig[9]);
} else {
result += tr("Good signature from: NAME <EMAIL>, Key ID: HEXID",
"<qt>Good signature from:<br /><b>%1 <%2></b><br />Key ID: %3<br /></qt>")
.arg(key.name).arg(key.email).arg(vsig[9]);
}
result += sigTimeMessage(vsig[2]);
/* const KGpgKeyNode *node = model->findKeyNode(vsig[9]);
if (node != NULL) {
// ignore for now if this is signed with the primary id (vsig[0] == vsig[9]) or not
if (node->getEmail().isEmpty())
result += tr("<qt>Good signature from:<br /><b>%1</b><br />Key ID: %2<br /></qt>")
.arg(node->getName()).arg(vsig[9]);
else
result += tr("Good signature from: NAME <EMAIL>, Key ID: HEXID",
"<qt>Good signature from:<br /><b>%1 <%2></b><br />Key ID: %3<br /></qt>")
.arg(node->getName()).arg(node->getEmail()).arg(vsig[9]);
result += sigTimeMessage(vsig[2]);
} else {
// this should normally never happen, but one could delete
// the key just after the verification. Brute force solution:
// do the whole report generation again, but this time make
// sure GOODSIG is used.
return getReport(log, NULL);
}*/
} else if (msg.startsWith(QLatin1String("UNEXPECTED")) ||
msg.startsWith(QLatin1String("NODATA"))) {
result += tr("No signature found.") + QLatin1Char('\n');
} else if (useGoodSig && msg.startsWith(QLatin1String("GOODSIG "))) {
int sigpos = msg.indexOf( ' ' , 8);
const QString keyid = msg.mid(8, sigpos - 8);
// split the name/email pair to give translators more power to handle this
QString email;
QString name = msg.mid(sigpos + 1);
int oPos = name.indexOf(QLatin1Char('<'));
int cPos = name.indexOf(QLatin1Char('>'));
if ((oPos >= 0) && (cPos >= 0)) {
email = name.mid(oPos + 1, cPos - oPos - 1);
name = name.left(oPos).simplified();
}
if (email.isEmpty())
result += tr("<qt>Good signature from:<br /><b>%1</b><br />Key ID: %2<br /></qt>")
.arg(name).arg(keyid);
else
result += tr("Good signature from: NAME <EMAIL>, Key ID: HEXID",
"<qt>Good signature from:<br /><b>%1 <%2></b><br />Key ID: %3<br /></qt>")
.arg(name).arg(email).arg(keyid);
if (!sigtime.isEmpty()) {
result += sigTimeMessage(sigtime);
sigtime.clear();
}
} else if (msg.startsWith(QLatin1String("SIG_ID "))) {
const QStringList parts = msg.simplified().split(QLatin1Char(' '));
if (parts.count() > 2)
sigtime = parts[2];
} else if (msg.startsWith(QLatin1String("BADSIG"))) {
int sigpos = msg.indexOf( ' ', 7);
result += tr("<qt><b>BAD signature</b> from:<br /> %1<br />Key id: %2<br /><br /><b>The file is corrupted</b><br /></qt>")
.arg(msg.mid(sigpos + 1).replace(QLatin1Char('<'), QLatin1String("<")))
.arg(msg.mid(7, sigpos - 7));
} else if (msg.startsWith(QLatin1String("TRUST_UNDEFINED"))) {
result += tr("<qt>The signature is valid, but the key is untrusted<br /></qt>");
} else if (msg.startsWith(QLatin1String("TRUST_ULTIMATE"))) {
result += tr("<qt>The signature is valid, and the key is ultimately trusted<br /></qt>");
}
}
return result;
}
}
|