aboutsummaryrefslogtreecommitdiffstats
path: root/src/gpg/gpg_context/GpgContext.cpp
blob: a892603c5ed041516ba7ef7e24ba2e0774622ff5 (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
/**
 * 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]> starting on May 12, 2021.
 *
 */

#include "gpg/GpgContext.h"
#include "ui/WaitingDialog.h"

#include <functional>
#include <unistd.h>    /* contains read/write */

#ifdef _WIN32

#include <windows.h>

#endif

#define INT2VOIDP(i) (void*)(uintptr_t)(i)

namespace GpgFrontend {

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

        gpgme_ctx_t _p_ctx;
        err = gpgme_new(&_p_ctx);
        check_gpg_error(err);
        _ctx_ref = CtxRefHandler(_p_ctx, [&](gpgme_ctx_t ctx) { gpgme_release(ctx); });

        gpgme_engine_info_t engineInfo;
        engineInfo = gpgme_ctx_get_engine_info(*this);

        // Check ENV before running
        bool check_pass = false, find_openpgp = false, find_gpgconf = false, find_assuan = false, find_cms = false;
        while (engineInfo != nullptr) {
            qDebug() << gpgme_get_protocol_name(engineInfo->protocol) << engineInfo->file_name << engineInfo->protocol
                     << engineInfo->home_dir << engineInfo->version;
            if (engineInfo->protocol == GPGME_PROTOCOL_GPGCONF && strcmp(engineInfo->version, "1.0.0") != 0)
                find_gpgconf = true;
            if (engineInfo->protocol == GPGME_PROTOCOL_OpenPGP && strcmp(engineInfo->version, "1.0.0") != 0)
                find_openpgp = true, info.appPath = engineInfo->file_name;
            if (engineInfo->protocol == GPGME_PROTOCOL_CMS && strcmp(engineInfo->version, "1.0.0") != 0)
                find_cms = true;
            if (engineInfo->protocol == GPGME_PROTOCOL_ASSUAN)
                find_assuan = true;
            engineInfo = engineInfo->next;
        }

        if (find_gpgconf && find_openpgp && find_cms && find_assuan)
            check_pass = true;

        if (!check_pass) {
            good = false;
            return;
        } else good = true;


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

        gpgme_set_keylist_mode(*this,
                               GPGME_KEYLIST_MODE_LOCAL
                               | GPGME_KEYLIST_MODE_WITH_SECRET
                               | GPGME_KEYLIST_MODE_SIGS
                               | GPGME_KEYLIST_MODE_SIG_NOTATIONS
                               | GPGME_KEYLIST_MODE_WITH_TOFU);

        /** 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;

        slotRefreshKeyList();
    }

    bool GpgContext::isGood() const {
        return good;
    }

    /**
     * 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 GpgContext::passphraseCb(void *hook, const char *uid_hint,
                                           const char *passphrase_info,
                                           int last_was_bad, int fd) {
        auto *gpg = static_cast<GpgContext *>(hook);
        return gpg->passphrase(uid_hint, passphrase_info, last_was_bad, fd);
    }

    gpgme_error_t GpgContext::passphrase(const char *uid_hint,
                                         const char * /*passphrase_info*/,
                                         int last_was_bad, int fd) {

        gpgme_error_t returnValue = GPG_ERR_CANCELED;
        QString passwordDialogMessage;
        QString gpgHint = QString::fromUtf8(uid_hint);
        bool result;

#ifdef _WIN32
        DWORD written;
        auto hd = INT2VOIDP(fd);
#endif

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

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

        if (mPasswordCache.isEmpty()) {
            QString password = QInputDialog::getText(QApplication::activeWindow(), "Enter Password",
                                                     passwordDialogMessage, QLineEdit::Password,
                                                     "", &result);

            if (result) mPasswordCache = password.toUtf8();
        } else result = true;

        if (result) {

#ifndef _WIN32
            if (write(fd, mPasswordCache.data(), mPasswordCache.length()) == -1) qDebug() << "something is terribly broken";
#else
            WriteFile(hd, mPasswordCache.data(), mPasswordCache.length(), &written, 0);
#endif
            returnValue = GPG_ERR_NO_ERROR;
        }

#ifndef _WIN32
        if (write(fd, "\n", 1) == -1) qDebug() << "something is terribly broken";
#else
        WriteFile(hd, "\n", 1, &written, nullptr);

        /* program will hang on cancel if hd not closed */
        if (!result) CloseHandle(hd);
#endif

        return returnValue;
    }

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

    // error-handling
    gpgme_error_t check_gpg_error(gpgme_error_t err, const QString &comment) {
        //if (gpgmeError != GPG_ERR_NO_ERROR && gpgmeError != GPG_ERR_CANCELED) {
        if (gpg_err_code(err) != GPG_ERR_NO_ERROR) {
            qDebug() << "[Error " << gpg_err_code(err)
                     << "] Source: " << gpgme_strsource(err) << " Description: " << gpgme_strerror(err);
        }
        return err;
    }

    gpgme_error_t check_gpg_error(gpgme_error_t err) {
        //if (gpgmeError != GPG_ERR_NO_ERROR && gpgmeError != GPG_ERR_CANCELED) {
        if (gpg_err_code(err) != GPG_ERR_NO_ERROR) {
            qDebug() << "[Error " << gpg_err_code(err)
                     << "] Source: " << gpgme_strsource(err) << " Description: " << gpgme_strerror(err);
        }
        return err;
    }

    /** return type should be gpgme_error_t*/



    /*
     * 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;
        else if (text.contains(GpgConstants::PGP_SIGNED_BEGIN) && text.contains(GpgConstants::PGP_SIGNED_END))
            return 1;

        else 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(static_cast<int>(5u * n + 4u), ' ');
        return fingerprint;
    }

    void GpgContext::slotRefreshKeyList() {
        qDebug() << "Refreshing Keys";
        this->fetch_keys();
        emit signalKeyInfoChanged();
    }

    QString GpgContext::getGpgmeVersion() {
        return {gpgme_check_version(nullptr)};
    }

    const GpgKeyList &GpgContext::getKeys() const {
        return mKeyList;
    }

    void GpgContext::slotUpdateKeyList(const QString &key_id) {
        auto it = mKeyMap.find(key_id);
        if (it != mKeyMap.end()) {
            gpgme_key_t new_key_ref;

            auto gpgmeErr = gpgme_get_key(*this, key_id.toUtf8().constData(), &new_key_ref, 0);

            if (gpgme_err_code(gpgmeErr) == GPG_ERR_EOF) {
                gpgmeErr = gpgme_get_key(*this, key_id.toUtf8().constData(), &new_key_ref, 1);

                if (gpgme_err_code(gpgmeErr) == GPG_ERR_EOF)
                    throw std::runtime_error("key_id not found in key database");
            }

            if (new_key_ref != nullptr) {
                it->second = std::move(GpgKey(std::move(new_key_ref)));
                emit signalKeyInfoChanged();
            }

        }
    }

}