summaryrefslogtreecommitdiffstats
path: root/src/kgpgme.cpp
blob: 5f6d355d05dd66cba1c9fb8297c9975a105cfcb1 (plain)
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
/***************************************************************************
 *   Copyright (C) 2006 by Petri Damsten                                   *
 *   damu@iki.fi                                                           *
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "kgpgme.h"

#ifdef HAVE_LIBGPGME

#include <kapplication.h>
#include <kmessagebox.h>
#include <kpassdlg.h>
#include <kiconloader.h>
#include <klistview.h>
#include <kdebug.h>
#include <tqcheckbox.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <klocale.h>
#include <locale.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

// KGpgSelKey class based on class in KGpg with the same name

class KGpgSelKey : public KDialogBase
{
	private:
		KListView* keysListpr;

	public:

		KGpgSelKey(TQWidget *tqparent, const char *name, TQString preselected,
			const KGpgMe& gpg):
		KDialogBase( tqparent, name, true,i18n("Private Key List"),Ok | Cancel) {
			TQString keyname;
			TQVBoxLayout* vbox;
			TQWidget* page = new TQWidget(this);
			TQLabel* labeltxt;
			KIconLoader* loader = KGlobal::iconLoader();
			TQPixmap keyPair = loader->loadIcon("kgpg_key2", KIcon::Small, 20);

			setMinimumSize(350,100);
			keysListpr = new KListView(page);
			keysListpr->setRootIsDecorated(true);
			keysListpr->addColumn(i18n("Name"));
			keysListpr->addColumn(i18n("Email"));
			keysListpr->addColumn(i18n("ID"));
			keysListpr->setShowSortIndicator(true);
			keysListpr->setFullWidth(true);
			keysListpr->setAllColumnsShowFocus(true);

			labeltxt = new TQLabel(i18n("Choose a secret key:"),page);
			vbox = new TQVBoxLayout(page);

			KGpgKeyList list = gpg.keys(true);

			for(KGpgKeyList::iterator it = list.begin(); it != list.end(); ++it) {
				TQString name = gpg.checkForUtf8((*it).name);
				KListViewItem *item = new
					KListViewItem(keysListpr, name, (*it).email, (*it).id);
				item->setPixmap(0,keyPair);
				if(preselected == (*it).id) {
					keysListpr->setSelected(item, true);
					keysListpr->setCurrentItem(item);
				}
			}
			if(!keysListpr->selectedItem()) {
				keysListpr->setSelected(keysListpr->firstChild(), true);
				keysListpr->setCurrentItem(keysListpr->firstChild());
			}
			vbox->addWidget(labeltxt);
			vbox->addWidget(keysListpr);
			setMainWidget(page);
		};

		TQString key() {
			TQListViewItem* item = keysListpr->selectedItem();

			if(item)
				return item->text(2);
			return "";
		}
};

KGpgMe::KGpgMe() : m_ctx(0), m_useGnuPGAgent(true)
{
	init(GPGME_PROTOCOL_OpenPGP);
	if(gpgme_new(&m_ctx)) {
		m_ctx = 0;
	}
	else {
		gpgme_set_armor(m_ctx, 1);
		setPassphraseCb();
	}
}

KGpgMe::~KGpgMe()
{
	if(m_ctx)
		gpgme_release(m_ctx);
	clearCache();
}

void KGpgMe::clearCache()
{
	if(m_cache.size() > 0)
	{
		m_cache.fill('\0');
		m_cache.truncate(0);
	}
}

void KGpgMe::init(gpgme_protocol_t proto)
{
	gpgme_error_t err;

	gpgme_check_version(NULL);
	setlocale(LC_ALL, "");
	gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
	gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));

	err = gpgme_engine_check_version(proto);
	if(err) {
		KMessageBox::error(TQT_TQWIDGET(kapp->activeWindow()), TQString("%1: %2")
			.tqarg(gpgme_strsource(err)).tqarg(gpgme_strerror(err)));
	}
}

TQString KGpgMe::checkForUtf8(TQString txt)
{
	// code borrowed from KGpg which borrowed it from gpa
	const char *s;

	// Make sure the encoding is UTF-8.
	// Test structure suggested by Werner Koch
	if(txt.isEmpty())
		return TQString();

	for(s = txt.ascii(); *s && !(*s & 0x80); s++)
		;
	if (*s && !strchr (txt.ascii(), 0xc3) && (txt.tqfind("\\x")==-1))
		return txt;

	// The string is not in UTF-8
	//if (strchr (txt.ascii(), 0xc3)) return (txt+" +++");
	if (txt.tqfind("\\x")==-1)
		return TQString::fromUtf8(txt.ascii());
	//        if (!strchr (txt.ascii(), 0xc3) || (txt.tqfind("\\x")!=-1)) {
	for(int idx = 0 ; (idx = txt.tqfind( "\\x", idx )) >= 0 ; ++idx) {
		char str[2] = "x";
		str[0] = (char)TQString(txt.mid(idx + 2, 2)).toShort(0, 16);
		txt.tqreplace(idx, 4, str);
	}
	if (!strchr (txt.ascii(), 0xc3))
		return TQString::fromUtf8(txt.ascii());
	else
		return TQString::fromUtf8(TQString::fromUtf8(txt.ascii()).ascii());
	// perform Utf8 twice, or some keys display badly
	return txt;
}

TQString KGpgMe::selectKey(TQString previous)
{
	KGpgSelKey dlg(TQT_TQWIDGET(kapp->activeWindow()), "", previous, *this);

	if(dlg.exec())
		return dlg.key();
	return "";
}

// Rest of the code is mainly based in gpgme examples

KGpgKeyList KGpgMe::keys(bool privateKeys /* = false */) const
{
	KGpgKeyList keys;
	gpgme_error_t err = 0, err2 = 0;
	gpgme_key_t key = 0;
	gpgme_keylist_result_t result = 0;

	if(m_ctx) {
		err = gpgme_op_keylist_start(m_ctx, NULL, privateKeys);
		if(!err) {
			while(!(err = gpgme_op_keylist_next(m_ctx, &key))) {
				KGpgKey gpgkey;

				if(!key->subkeys)
					continue;
				gpgkey.id = key->subkeys->keyid;
				if(key->uids) {
					gpgkey.name = key->uids->name;
					gpgkey.email = key->uids->email;
				}
				keys.append(gpgkey);
				gpgme_key_unref(key);
			}

			if (gpg_err_code (err) == GPG_ERR_EOF)
				err = 0;
			err2 = gpgme_op_keylist_end(m_ctx);
			if(!err)
				err = err2;
		}
	}

	if(err) {
		KMessageBox::error(TQT_TQWIDGET(kapp->activeWindow()), TQString("%1: %2")
			.tqarg(gpgme_strsource(err)).tqarg(gpgme_strerror(err)));
	}
	else {
		result = gpgme_op_keylist_result(m_ctx);
		if (result->truncated) {
			KMessageBox::error(TQT_TQWIDGET(kapp->activeWindow()),
				i18n("Key listing unexpectedly truncated."));
		}
	}
	return keys;
}

bool KGpgMe::encrypt(const TQByteArray& inBuffer, TQ_ULONG length,
					 TQByteArray* outBuffer, TQString keyid /* = TQString() */)
{
	gpgme_error_t err = 0;
	gpgme_data_t in = 0, out = 0;
	gpgme_key_t keys[2] = { NULL, NULL };
	gpgme_key_t* key = NULL;
	gpgme_encrypt_result_t result = 0;

	outBuffer->resize(0);
	if(m_ctx) {
		err = gpgme_data_new_from_mem(&in, inBuffer.data(), length, 1);
		if(!err) {
			err = gpgme_data_new(&out);
			if(!err) {
				if(keyid.isNull()) {
					key = NULL;
				}
				else {
					err = gpgme_get_key(m_ctx, keyid.ascii(), &keys[0], 0);
					key = keys;
				}

				if(!err) {
					err = gpgme_op_encrypt(m_ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST,
						in, out);
					if(!err) {
						result = gpgme_op_encrypt_result(m_ctx);
						if (result->invalid_recipients) {
							KMessageBox::error(TQT_TQWIDGET(kapp->activeWindow()), TQString("%1: %2")
								.tqarg(i18n("That public key is not meant for encryption"))
								.tqarg(result->invalid_recipients->fpr));
						}
						else {
							err = readToBuffer(out, outBuffer);
						}
					}
				}
			}
		}
	}
	if(err != GPG_ERR_NO_ERROR && err != GPG_ERR_CANCELED) {
		KMessageBox::error(TQT_TQWIDGET(kapp->activeWindow()), TQString("%1: %2")
			.tqarg(gpgme_strsource(err)).tqarg(gpgme_strerror(err)));
	}
	if(err != GPG_ERR_NO_ERROR)
		clearCache();
	if(keys[0])
		gpgme_key_unref(keys[0]);
	if(in)
		gpgme_data_release(in);
	if(out)
		gpgme_data_release(out);
	return (err == GPG_ERR_NO_ERROR);
}

bool KGpgMe::decrypt(const TQByteArray& inBuffer, TQByteArray* outBuffer)
{
	gpgme_error_t err = 0;
	gpgme_data_t in = 0, out = 0;
	gpgme_decrypt_result_t result = 0;

	outBuffer->resize(0);
	if(m_ctx) {
		err = gpgme_data_new_from_mem(&in, inBuffer.data(), inBuffer.size(), 1);
		if(!err) {
			err = gpgme_data_new(&out);
			if(!err) {
				err = gpgme_op_decrypt(m_ctx, in, out);
				if(!err) {
					result = gpgme_op_decrypt_result(m_ctx);
					if(result->unsupported_algorithm) {
						KMessageBox::error(TQT_TQWIDGET(kapp->activeWindow()), TQString("%1: %2")
							.tqarg(i18n("Unsupported algorithm"))
							.tqarg(result->unsupported_algorithm));
					}
					else {
						err = readToBuffer(out, outBuffer);
					}
				}
			}
		}
	}
	if(err != GPG_ERR_NO_ERROR && err != GPG_ERR_CANCELED) {
		KMessageBox::error(TQT_TQWIDGET(kapp->activeWindow()), TQString("%1: %2")
			.tqarg(gpgme_strsource(err)).tqarg(gpgme_strerror(err)));
	}
	if(err != GPG_ERR_NO_ERROR)
		clearCache();
	if(in)
		gpgme_data_release(in);
	if(out)
		gpgme_data_release(out);
	return (err == GPG_ERR_NO_ERROR);
}

#define BUF_SIZE (32 * 1024)

gpgme_error_t KGpgMe::readToBuffer(gpgme_data_t in, TQByteArray* outBuffer) const
{
	int ret;
	gpgme_error_t err = GPG_ERR_NO_ERROR;

	ret = gpgme_data_seek(in, 0, SEEK_SET);
	if(ret) {
		err = gpgme_err_code_from_errno(errno);
	}
	else {
		char* buf = new char[BUF_SIZE + 2];

		if(buf) {
			while((ret = gpgme_data_read(in, buf, BUF_SIZE)) > 0) {
				uint size = outBuffer->size();
				if(outBuffer->tqresize(size + ret))
					memcpy(outBuffer->data() + size, buf, ret);
			}
			if(ret < 0)
				err = gpgme_err_code_from_errno(errno);
			delete[] buf;
		}
	}
	return err;
}

bool KGpgMe::isGnuPGAgentAvailable()
{
	TQString agent_info = getenv("GPG_AGENT_INFO");

	if (agent_info.tqfind(':') > 0)
		return true;
	return false;
}

void KGpgMe::setPassphraseCb()
{
	bool agent = false;
	TQString agent_info;

	agent_info = getenv("GPG_AGENT_INFO");

	if(m_useGnuPGAgent)
	{
		if (agent_info.tqfind(':'))
			agent = true;
		if(agent_info.startsWith("disable:"))
			setenv("GPG_AGENT_INFO", agent_info.mid(8), 1);
	}
	else
	{
		if(!agent_info.startsWith("disable:"))
			setenv("GPG_AGENT_INFO", "disable:" + agent_info, 1);
	}
	if (agent)
		gpgme_set_passphrase_cb(m_ctx, 0, 0);
	else
		gpgme_set_passphrase_cb(m_ctx, passphraseCb, this);
}

gpgme_error_t KGpgMe::passphraseCb(void* hook, const char* uid_hint,
								   const char* passphrase_info,
								   int last_was_bad, int fd)
{
	KGpgMe* gpg = static_cast<KGpgMe*>(hook);
	return gpg->passphrase(uid_hint, passphrase_info, last_was_bad, fd);
}

gpgme_error_t KGpgMe::passphrase(const char* uid_hint,
								 const char* /*passphrase_info*/,
								 int last_was_bad, int fd)
{
	gpgme_error_t res = GPG_ERR_CANCELED;
	TQString s;
	TQString gpg_hint = checkForUtf8(uid_hint);
	int result;

	if(last_was_bad){
		s += "<b>" + i18n("Wrong password.") + "</b><br><br>\n\n";
		clearCache();
	}

	if(!m_text.isEmpty())
		s += m_text + "<br>";

	if(!gpg_hint.isEmpty())
		s += gpg_hint;

	if(m_cache.isEmpty()){
		TQCString password;

		if(m_saving)
			result = KPasswordDialog::getNewPassword(password, s);
		else
			result = KPasswordDialog::getPassword(password, s);

		if(result == KPasswordDialog::Accepted)
			m_cache = password;
	}
	else
		result = KPasswordDialog::Accepted;

	if(result == KPasswordDialog::Accepted) {
		write(fd, m_cache.data(), m_cache.length());
		res = 0;
	}
	write(fd, "\n", 1);
	return res;
}
#endif	// HAVE_LIBGPGME