aboutsummaryrefslogtreecommitdiffstats
path: root/context.cpp
blob: 9cf06a924d99d67a0e97de2cbd31186e2dcf58ec (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
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
/*
 *      context.cpp
 *
 *      Copyright 2008 gpg4usb-team <[email protected]>
 *
 *      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 "context.h"

#ifdef _WIN32
#include <windows.h>
#include <unistd.h>    /* contains read/write */
#endif


namespace GpgME
{

/** Constructor
 *  Set up gpgme-context, set paths to app-run path
 */
Context::Context()
{

    /** get application path */
    QString appPath = qApp->applicationDirPath();

    /** The function `gpgme_check_version' must be called before any other
     *  function in the library, because it initializes the thread support
     *  subsystem in GPGME. (from the info page) */
    gpgme_check_version(NULL);

    setlocale(LC_ALL, "");
    /** set locale, because tests do also */
    gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
    //qDebug() << "Locale set to" << LC_CTYPE << " - " << setlocale(LC_CTYPE, NULL);
#ifndef _WIN32
    gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
#endif

    err = gpgme_new(&mCtx);
    checkErr(err);
    /** here come the settings, instead of /usr/bin/gpg
     * a executable in the same path as app is used.
     * also lin/win must  be checked, for calling gpg.exe if needed
     */
#ifdef _WIN32
    QString gpgBin = appPath + "/bin/gpg.exe";
#else
    QString gpgBin = appPath + "/bin/gpg";
#endif
    QString gpgKeys = appPath + "/keydb";
/*    err = gpgme_ctx_set_engine_info(mCtx, GPGME_PROTOCOL_OpenPGP,
                                    gpgBin.toUtf8().constData(),
                                    gpgKeys.toUtf8().constData());*/
    err = gpgme_ctx_set_engine_info(mCtx, GPGME_PROTOCOL_OpenPGP,
                                    gpgBin.toLocal8Bit().constData(),
                                    gpgKeys.toLocal8Bit().constData());
    checkErr(err);


    /** Setting the output type must be done at the beginning */
    /** think this means ascii-armor --> ? */
    gpgme_set_armor(mCtx, 1);
    /** passphrase-callback */
    gpgme_set_passphrase_cb(mCtx, passphraseCb, this);

    /** check if app is called with -d from command line */
    if (qApp->arguments().contains("-d")) {
        qDebug() << "gpgme_data_t debug on";
        debug = true;
    } else {
        debug = false;
    }

}

/** Destructor
 *  Release gpgme-context
 */
Context::~Context()
{
    if (mCtx) gpgme_release(mCtx);
    mCtx = 0;
}

/** Import Key from QByteArray
 *
 */
void Context::importKey(QByteArray inBuffer)
{
    err = gpgme_data_new_from_mem(&in, inBuffer.data(), inBuffer.size(), 1);
    checkErr(err);
    err = gpgme_op_import(mCtx, in);
    checkErr(err);
    gpgme_data_release(in);
    emit keyDBChanged();
}

/** Generate New Key with values params
 *
 */
void Context::generateKey(QString *params)
{
    err = gpgme_op_genkey(mCtx, params->toAscii().data(), NULL, NULL);
    checkErr(err);
    emit keyDBChanged();
}

/** Export Key to QByteArray
 *
 */
bool Context::exportKeys(QList<QString> *uidList, QByteArray *outBuffer)
{
    size_t read_bytes;
    gpgme_data_t out = 0;
    outBuffer->resize(0);

    if (uidList->count() == 0) {
        QMessageBox::critical(0, "Export Keys Error", "No Keys Selected");
        return false;
    }

    for (int i = 0; i < uidList->count(); i++) {
        err = gpgme_data_new(&out);
        checkErr(err);

        err = gpgme_op_export(mCtx, uidList->at(i).toAscii().constData(), 0, out);
        checkErr(err);

        read_bytes = gpgme_data_seek(out, 0, SEEK_END);

        err = readToBuffer(out, outBuffer);
        checkErr(err);
        gpgme_data_release(out);
    }
    return true;
}

gpgme_key_t Context::getKeyDetails(QString uid) 
{
	gpgme_key_t key;

    // try secret
    gpgme_get_key (mCtx, uid.toAscii().constData(), &key, 1);
    // ok, its a public key
    if(!key) {
        gpgme_get_key (mCtx, uid.toAscii().constData(), &key, 0);
    }
    
	return key;	
}

/** List all availabe Keys (VERY much like kgpgme)
 */
GpgKeyList Context::listKeys()
{
    gpgme_error_t err;
    gpgme_key_t key;

    GpgKeyList keys;
    //TODO dont run the loop more often than necessary
    // list all keys ( the 0 is for all )
    err = gpgme_op_keylist_start(mCtx, NULL, 0);
    checkErr(err);
    while (!(err = gpgme_op_keylist_next(mCtx, &key))) {
        GpgKey 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);
    }
    gpgme_op_keylist_end(mCtx);

    // list only private keys ( the 1 does )
    gpgme_op_keylist_start(mCtx, NULL, 1);
    while (!(err = gpgme_op_keylist_next(mCtx, &key))) {
        if (!key->subkeys)
            continue;
        // iterate keys, mark privates
        GpgKeyList::iterator it = keys.begin();
        while (it != keys.end()) {
            if (key->subkeys->keyid == it->id.toStdString())
                it->privkey = true;
            it++;
        }

        gpgme_key_unref(key);
    }
    gpgme_op_keylist_end(mCtx);

    return keys;
}

/** Delete keys
 */

void Context::deleteKeys(QList<QString> *uidList)
{

    QString tmp;
    gpgme_key_t key;

    foreach(tmp,  *uidList) {
        gpgme_op_keylist_start(mCtx, tmp.toAscii().constData(), 0);
        gpgme_op_keylist_next(mCtx, &key);
        gpgme_op_keylist_end(mCtx);
        gpgme_op_delete(mCtx, key, 1);
    }
    emit keyDBChanged();
}

/** Encrypt inBuffer for reciepients-uids, write
 *  result to outBuffer
 */
bool Context::encrypt(QList<QString> *uidList, const QByteArray &inBuffer, QByteArray *outBuffer)
{

    gpgme_data_t in = 0, out = 0;
    outBuffer->resize(0);

    if (uidList->count() == 0) {
        QMessageBox::critical(0, "No Key Selected", "No Key Selected");
        return false;
    }

    //gpgme_encrypt_result_t e_result;
    gpgme_key_t recipients[uidList->count()+1];

    /* get key for user */
    for (int i = 0; i < uidList->count(); i++) {
        // the last 0 is for public keys, 1 would return private keys
        gpgme_op_keylist_start(mCtx, uidList->at(i).toAscii().constData(), 0);
        gpgme_op_keylist_next(mCtx, &recipients[i]);
        gpgme_op_keylist_end(mCtx);
    }
    //Last entry in array has to be NULL
    recipients[uidList->count()] = NULL;

    //If the last parameter isnt 0, a private copy of data is made
    err = gpgme_data_new_from_mem(&in, inBuffer.data(), inBuffer.size(), 1);
    checkErr(err);
    err = gpgme_data_new(&out);
    checkErr(err);

    err = gpgme_op_encrypt(mCtx, recipients, GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
    checkErr(err);

    err = readToBuffer(out, outBuffer);
    checkErr(err);

    /* unref all keys */
    for (int i = 0; i <= uidList->count(); i++) {
        gpgme_key_unref(recipients[i]);
    }
    gpgme_data_release(in);
    gpgme_data_release(out);

    return true;
}

/** Decrypt QByteAarray, return QByteArray
 *  mainly from http://basket.kde.org/ (kgpgme.cpp)
 */
bool Context::decrypt(const QByteArray &inBuffer, QByteArray *outBuffer)
{
    gpgme_data_t in = 0, out = 0;
    gpgme_decrypt_result_t result = 0;

    outBuffer->resize(0);
    if (mCtx) {
        err = gpgme_data_new_from_mem(&in, inBuffer.data(), inBuffer.size(), 1);
        checkErr(err);
        if (!err) {
            err = gpgme_data_new(&out);
            checkErr(err);
            if (!err) {
                err = gpgme_op_decrypt(mCtx, in, out);
                checkErr(err);
                if (!err) {
                    result = gpgme_op_decrypt_result(mCtx);
                    if (result->unsupported_algorithm) {
                        QMessageBox::critical(0, "Unsupported algorithm", result->unsupported_algorithm);
                    } else {
                        err = readToBuffer(out, outBuffer);
                        checkErr(err);
                    }
                }
            }
        }
    }
    if (err != GPG_ERR_NO_ERROR && err != GPG_ERR_CANCELED) {
        QMessageBox::critical(0, "Error encrypting:", 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);
}

/**  Read gpgme-Data to QByteArray
 *   mainly from http://basket.kde.org/ (kgpgme.cpp)
 */
#define BUF_SIZE (32 * 1024)
gpgme_error_t Context::readToBuffer(gpgme_data_t in, QByteArray *outBuffer)
{
    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);
        checkErr(err, "failed dataseek in readToBuffer");
    } else {
        char *buf = new char[BUF_SIZE + 2];

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

/** The Passphrase window, if not provided by env-Var GPG_AGENT_INFO
 *  originally copied from http://basket.kde.org/ (kgpgme.cpp), but modified
 */
gpgme_error_t Context::passphraseCb(void *hook, const char *uid_hint,
                                    const char *passphrase_info,
                                    int last_was_bad, int fd)
{
    Context *gpg = static_cast<Context*>(hook);
    return gpg->passphrase(uid_hint, passphrase_info, last_was_bad, fd);
}

gpgme_error_t Context::passphrase(const char *uid_hint,
                                  const char * /*passphrase_info*/,
                                  int last_was_bad, int fd)
{
    gpgme_error_t returnValue = GPG_ERR_CANCELED;
    QString s;
    QString gpg_hint = uid_hint;
    bool result;

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

    /** if uid provided */
    if (!gpg_hint.isEmpty()) {
        // remove UID, leave only username & email
        gpg_hint.remove(0, gpg_hint.indexOf(" "));
        s += "<b>Enter Password for</b><br>\n" + gpg_hint + "\n";
    }

    if (m_cache.isEmpty()) {
        QString password = QInputDialog::getText(0, "Enter Password",
                           s, QLineEdit::Password,
                           "", &result, Qt::Window);

        if (result) m_cache = password.toAscii();
    } else
        result = false;

    if (result) {

#ifndef _WIN32
        if (write(fd, m_cache.data(), m_cache.length()) == -1) {
            qDebug() << "something is terribly broken";
        }
#else
        DWORD written;
        WriteFile((HANDLE) fd, m_cache.data(), m_cache.length(), &written, 0);
#endif

        returnValue = 0;
    }

#ifndef _WIN32
    if (write(fd, "\n", 1) == -1) {
        qDebug() << "something is terribly broken";
    }
#else
    DWORD written;
    WriteFile((HANDLE) fd, "\n", 1, &written, 0);
#endif

    return returnValue;
}

/** also from kgpgme.cpp, seems to clear password from mem */
void Context::clearCache()
{
    if (m_cache.size() > 0) {
        m_cache.fill('\0');
        m_cache.truncate(0);
    }
}

// error-handling
void Context::checkErr(gpgme_error_t err, QString comment) const
{
    if (err != GPG_ERR_NO_ERROR && err != GPG_ERR_CANCELED) {
        qDebug() << "[Error " << comment << "] Source: " << gpgme_strsource(err) << " String: " << gpgme_strerror(err);
    }
}

void Context::checkErr(gpgme_error_t err) const
{
    if (err != GPG_ERR_NO_ERROR && err != GPG_ERR_CANCELED) {
        qDebug() << "[Error] Source: " << gpgme_strsource(err) << " String: " << gpgme_strerror(err);
    }
}


/** export private key, TODO errohandling, e.g. like in seahorse (seahorse-gpg-op.c) **/ 

void Context::exportSecretKey(QString uid, QByteArray *outBuffer)
{
	QStringList arguments;
	arguments << "--armor" << "--export-secret-key" << uid;
	QByteArray *err = new QByteArray();
	executeGpgCommand(arguments, outBuffer, err);
}

/** return type should be gpgme_error_t*/
void Context::executeGpgCommand(QStringList arguments, QByteArray *stdOut, QByteArray *stdErr)
{
	gpgme_engine_info_t engine = gpgme_ctx_get_engine_info(mCtx);

	QStringList args;
	args << "--homedir" << engine->home_dir << "--batch" << arguments;	

	QProcess gpg;
    gpg.start(engine->file_name, args);
    gpg.waitForFinished();

    *stdOut = gpg.readAllStandardOutput();    
    *stdErr = gpg.readAllStandardError();   
}

}