aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/function/gpg/GpgContext.cpp
blob: 30134191c621d3c1a7ac74d0c1d2a82a6769e203 (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
/**
 * Copyright (C) 2021 Saturneric <[email protected]>
 *
 * 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.
 *
 * GpgFrontend 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 GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
 *
 * The initial version of the source code is inherited from
 * the gpg4usb project, which is under GPL-3.0-or-later.
 *
 * All the source code of GpgFrontend was modified and released by
 * Saturneric <[email protected]> starting on May 12, 2021.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#include "core/function/gpg/GpgContext.h"

#include <gpg-error.h>
#include <gpgme.h>

#include <cassert>
#include <mutex>

#include "core/function/CoreSignalStation.h"
#include "core/function/basic/GpgFunctionObject.h"
#include "core/module/ModuleManager.h"
#include "core/utils/GpgUtils.h"
#include "utils/MemoryUtils.h"

#ifdef _WIN32
#include <windows.h>
#endif

namespace GpgFrontend {

class GpgContext::Impl {
 public:
  /**
   * Constructor
   *  Set up gpgme-context, set paths to app-run path
   */
  Impl(GpgContext *parent, const GpgContextInitArgs &args)
      : parent_(parent),
        args_(args),
        good_(default_ctx_initialize(args) && binary_ctx_initialize(args)) {}

  ~Impl() {
    if (ctx_ref_ != nullptr) {
      gpgme_release(ctx_ref_);
    }

    if (binary_ctx_ref_ != nullptr) {
      gpgme_release(binary_ctx_ref_);
    }
  }

  [[nodiscard]] auto BinaryContext() const -> gpgme_ctx_t {
    return binary_ctx_ref_;
  }

  [[nodiscard]] auto DefaultContext() const -> gpgme_ctx_t { return ctx_ref_; }

  [[nodiscard]] auto Good() const -> bool { return good_; }

  auto SetPassphraseCb(const gpgme_ctx_t &ctx, gpgme_passphrase_cb_t cb)
      -> bool {
    if (gpgme_get_pinentry_mode(ctx) != GPGME_PINENTRY_MODE_LOOPBACK) {
      if (CheckGpgError(gpgme_set_pinentry_mode(
              ctx, GPGME_PINENTRY_MODE_LOOPBACK)) != GPG_ERR_NO_ERROR) {
        return false;
      }
    }
    gpgme_set_passphrase_cb(ctx, cb, reinterpret_cast<void *>(parent_));
    return true;
  }

  static auto TestPassphraseCb(void *opaque, const char *uid_hint,
                               const char *passphrase_info, int last_was_bad,
                               int fd) -> gpgme_error_t {
    size_t res;
    std::string pass = "abcdefg\n";
    auto pass_len = pass.size();

    size_t off = 0;

    do {
      res = gpgme_io_write(fd, &pass[off], pass_len - off);
      if (res > 0) off += res;
    } while (res > 0 && off != pass_len);

    return off == pass_len ? 0 : gpgme_error_from_errno(errno);
  }

  static auto CustomPassphraseCb(void *hook, const char *uid_hint,
                                 const char *passphrase_info, int last_was_bad,
                                 int fd) -> gpgme_error_t {
    std::string passphrase;

    GF_CORE_LOG_DEBUG(
        "custom passphrase cb called, uid: {}, info: {}, last_was_bad: {}",
        uid_hint == nullptr ? "<empty>" : std::string{uid_hint},
        passphrase_info == nullptr ? "<empty>" : std::string{passphrase_info},
        last_was_bad);

    emit CoreSignalStation::GetInstance()->SignalNeedUserInputPassphrase();

    QEventLoop looper;
    QObject::connect(
        CoreSignalStation::GetInstance(),
        &CoreSignalStation::SignalUserInputPassphraseCallback, &looper,
        [&](const QByteArray &buffer) { passphrase = buffer.toStdString(); });
    QObject::connect(CoreSignalStation::GetInstance(),
                     &CoreSignalStation::SignalUserInputPassphraseCallback,
                     &looper, &QEventLoop::quit);
    looper.exec();

    auto passpahrase_size = passphrase.size();
    GF_CORE_LOG_DEBUG("get passphrase from pinentry size: {}",
                      passpahrase_size);

    size_t off = 0;
    size_t res = 0;
    do {
      res = gpgme_io_write(fd, &passphrase[off], passpahrase_size - off);
      if (res > 0) off += res;
    } while (res > 0 && off != passpahrase_size);

    res += gpgme_io_write(fd, "\n", 1);

    GF_CORE_LOG_DEBUG("custom passphrase cd is about to return, res: {}", res);
    return res == passpahrase_size + 1
               ? 0
               : gpgme_error_from_errno(GPG_ERR_CANCELED);
  }

  static auto TestStatusCb(void *hook, const char *keyword, const char *args)
      -> gpgme_error_t {
    GF_CORE_LOG_DEBUG("keyword {}", keyword);
    return GPG_ERR_NO_ERROR;
  }

 private:
  GpgContext *parent_;
  GpgContextInitArgs args_{};             ///<
  gpgme_ctx_t ctx_ref_ = nullptr;         ///<
  gpgme_ctx_t binary_ctx_ref_ = nullptr;  ///<
  bool good_ = true;
  std::mutex ctx_ref_lock_;
  std::mutex binary_ctx_ref_lock_;

  static auto set_ctx_key_list_mode(const gpgme_ctx_t &ctx) -> bool {
    assert(ctx != nullptr);

    const auto gpgme_version = Module::RetrieveRTValueTypedOrDefault<>(
        "core", "gpgme.version", std::string{"0.0.0"});
    GF_CORE_LOG_DEBUG("got gpgme version version from rt: {}", gpgme_version);

    if (gpgme_get_keylist_mode(ctx) == 0) {
      GF_CORE_LOG_ERROR(
          "ctx is not a valid pointer, reported by gpgme_get_keylist_mode");
      return false;
    }

    // set keylist mode
    return CheckGpgError(gpgme_set_keylist_mode(
               ctx, GPGME_KEYLIST_MODE_LOCAL | GPGME_KEYLIST_MODE_WITH_SECRET |
                        GPGME_KEYLIST_MODE_SIGS |
                        GPGME_KEYLIST_MODE_SIG_NOTATIONS |
                        GPGME_KEYLIST_MODE_WITH_TOFU)) == GPG_ERR_NO_ERROR;
  }

  static auto set_ctx_openpgp_engine_info(gpgme_ctx_t ctx) -> bool {
    const auto app_path = Module::RetrieveRTValueTypedOrDefault<>(
        "core", "gpgme.ctx.app_path", std::string{});
    const auto database_path = Module::RetrieveRTValueTypedOrDefault<>(
        "core", "gpgme.ctx.database_path", std::string{});

    GF_CORE_LOG_DEBUG("ctx set engine info, db path: {}, app path: {}",
                      database_path, app_path);

    const char *app_path_c_str = app_path.c_str();
    const char *db_path_c_str = database_path.c_str();

    if (app_path.empty()) {
      app_path_c_str = nullptr;
    }

    if (database_path.empty()) {
      db_path_c_str = nullptr;
    }

    auto err = gpgme_ctx_set_engine_info(ctx, gpgme_get_protocol(ctx),
                                         app_path_c_str, db_path_c_str);

    assert(CheckGpgError(err) == GPG_ERR_NO_ERROR);
    return CheckGpgError(err) == GPG_ERR_NO_ERROR;

    return true;
  }

  auto common_ctx_initialize(const gpgme_ctx_t &ctx,
                             const GpgContextInitArgs &args) -> bool {
    assert(ctx != nullptr);

    if (args.custom_gpgconf && !args.custom_gpgconf_path.empty()) {
      GF_CORE_LOG_DEBUG("set custom gpgconf path: {}",
                        args.custom_gpgconf_path);
      auto err =
          gpgme_ctx_set_engine_info(ctx, GPGME_PROTOCOL_GPGCONF,
                                    args.custom_gpgconf_path.c_str(), nullptr);

      assert(CheckGpgError(err) == GPG_ERR_NO_ERROR);
      if (CheckGpgError(err) != GPG_ERR_NO_ERROR) {
        return false;
      }
    }

    // set context offline mode
    GF_CORE_LOG_DEBUG("gpg context offline mode: {}", args_.offline_mode);
    gpgme_set_offline(ctx, args_.offline_mode ? 1 : 0);

    // set option auto import missing key
    // invalid at offline mode
    GF_CORE_LOG_DEBUG("gpg context auto import missing key: {}",
                      args_.offline_mode);
    if (!args.offline_mode && args.auto_import_missing_key) {
      if (CheckGpgError(gpgme_set_ctx_flag(ctx, "auto-key-import", "1")) !=
          GPG_ERR_NO_ERROR) {
        return false;
      }
    }

    if (!set_ctx_key_list_mode(ctx)) {
      GF_CORE_LOG_DEBUG("set ctx key list mode failed");
      return false;
    }

    // for unit test
    if (args_.test_mode) {
      if (!SetPassphraseCb(ctx, TestPassphraseCb)) {
        GF_CORE_LOG_ERROR("set passphrase cb failed, test");
        return false;
      };
    } else if (!args_.use_pinentry) {
      if (!SetPassphraseCb(ctx, CustomPassphraseCb)) {
        GF_CORE_LOG_DEBUG("set passphrase cb failed, custom");
        return false;
      }
    }

    // set custom gpg key db path
    if (!args_.db_path.empty()) {
      Module::UpsertRTValue("core", "gpgme.ctx.database_path",
                            std::string(args_.db_path));
    }

    if (!set_ctx_openpgp_engine_info(ctx)) {
      GF_CORE_LOG_ERROR("set gpgme context openpgp engine info failed");
      return false;
    }

    return true;
  }

  auto binary_ctx_initialize(const GpgContextInitArgs &args) -> bool {
    gpgme_ctx_t p_ctx;
    if (CheckGpgError(gpgme_new(&p_ctx)) != GPG_ERR_NO_ERROR) {
      return false;
    }
    assert(p_ctx != nullptr);
    binary_ctx_ref_ = p_ctx;

    if (!common_ctx_initialize(binary_ctx_ref_, args)) {
      GF_CORE_LOG_ERROR("get new ctx failed, binary");
      return false;
    }

    gpgme_set_armor(binary_ctx_ref_, 0);
    return true;
  }

  auto default_ctx_initialize(const GpgContextInitArgs &args) -> bool {
    gpgme_ctx_t p_ctx;
    if (CheckGpgError(gpgme_new(&p_ctx)) != GPG_ERR_NO_ERROR) {
      GF_CORE_LOG_ERROR("get new ctx failed, default");
      return false;
    }
    assert(p_ctx != nullptr);
    ctx_ref_ = p_ctx;

    if (!common_ctx_initialize(ctx_ref_, args)) {
      return false;
    }

    gpgme_set_armor(ctx_ref_, 1);
    return true;
  }
};

GpgContext::GpgContext(int channel)
    : SingletonFunctionObject<GpgContext>(channel),
      p_(SecureCreateUniqueObject<Impl>(this, GpgContextInitArgs{})) {}

GpgContext::GpgContext(GpgContextInitArgs args, int channel)
    : SingletonFunctionObject<GpgContext>(channel),
      p_(SecureCreateUniqueObject<Impl>(this, args)) {}

auto GpgContext::Good() const -> bool { return p_->Good(); }

auto GpgContext::BinaryContext() -> gpgme_ctx_t { return p_->BinaryContext(); }

auto GpgContext::DefaultContext() -> gpgme_ctx_t {
  return p_->DefaultContext();
}

GpgContext::~GpgContext() = default;

}  // namespace GpgFrontend