aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/utils/GpgUtils.cpp
blob: b882467327ae053d9225588d2eb0babe7a0c2adb (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
/**
 * Copyright (C) 2021-2024 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 "GpgUtils.h"

#include "core/function/GlobalSettingStation.h"
#include "core/function/gpg/GpgAbstractKeyGetter.h"
#include "core/model/GpgKey.h"
#include "core/model/GpgKeyGroup.h"
#include "core/model/KeyDatabaseInfo.h"
#include "core/model/SettingsObject.h"
#include "core/module/ModuleManager.h"
#include "core/struct/settings_object/KeyDatabaseListSO.h"
namespace GpgFrontend {

inline auto Trim(QString& s) -> QString { return s.trimmed(); }

auto GetGpgmeErrorString(size_t buffer_size, gpgme_error_t err) -> QString {
  QContainer<char> buffer(buffer_size);

  gpgme_error_t const ret = gpgme_strerror_r(err, buffer.data(), buffer.size());
  if (ret == ERANGE && buffer_size < 1024) {
    return GetGpgmeErrorString(buffer_size * 2, err);
  }

  return {buffer.data()};
}

auto GetGpgmeErrorString(gpgme_error_t err) -> QString {
  return GetGpgmeErrorString(64, err);
}

auto CheckGpgError(GpgError err) -> GpgError {
  auto err_code = gpg_err_code(err);
  if (err_code != GPG_ERR_NO_ERROR) {
    LOG_W() << "gpg operation failed [error code: " << err_code
            << "], source: " << gpgme_strsource(err)
            << " description: " << GetGpgmeErrorString(err);
  }
  return err_code;
}

auto CheckGpgError2ErrCode(GpgError err, GpgError predict) -> GpgErrorCode {
  auto err_code = gpg_err_code(err);
  if (err_code != gpg_err_code(predict)) {
    if (err_code == GPG_ERR_NO_ERROR) {
      LOG_I() << "[Warning " << gpg_err_code(err)
              << "] Source: " << gpgme_strsource(err)
              << " description: " << GetGpgmeErrorString(err)
              << " predict: " << GetGpgmeErrorString(predict);
    } else {
      LOG_W() << "[Error " << gpg_err_code(err)
              << "] Source: " << gpgme_strsource(err)
              << " description: " << GetGpgmeErrorString(err)
              << " predict: " << GetGpgmeErrorString(predict);
    }
  }
  return err_code;
}

auto DescribeGpgErrCode(GpgError err) -> GpgErrorDesc {
  return {gpgme_strsource(err), GetGpgmeErrorString(err)};
}

auto CheckGpgError(GpgError err, const QString& /*comment*/) -> GpgError {
  if (gpg_err_code(err) != GPG_ERR_NO_ERROR) {
    LOG_W() << "[Error " << gpg_err_code(err)
            << "] Source: " << gpgme_strsource(err)
            << " description: " << GetGpgmeErrorString(err);
  }
  return err;
}

auto TextIsSigned(QString text) -> int {
  auto trim_text = Trim(text);
  if (trim_text.startsWith(PGP_SIGNED_BEGIN) &&
      trim_text.endsWith(PGP_SIGNED_END)) {
    return 2;
  }
  if (text.contains(PGP_SIGNED_BEGIN) && text.contains(PGP_SIGNED_END)) {
    return 1;
  }
  return 0;
}

auto SetExtensionOfOutputFile(const QString& path, GpgOperation opera,
                              bool ascii) -> QString {
  auto file_info = QFileInfo(path);
  QString new_extension;
  QString current_extension = file_info.suffix();

  if (ascii) {
    switch (opera) {
      case kENCRYPT:
      case kSIGN:
      case kENCRYPT_SIGN:
        new_extension = current_extension + ".asc";
        break;
      default:
        break;
    }
  } else {
    switch (opera) {
      case kENCRYPT:
      case kENCRYPT_SIGN:
        new_extension = current_extension + ".gpg";
        break;
      case kSIGN:
        new_extension = current_extension + ".sig";
        break;
      default:
        break;
    }
  }

  if (!new_extension.isEmpty()) {
    return file_info.absolutePath() + "/" + file_info.completeBaseName() + "." +
           new_extension;
  }
  return file_info.absolutePath() + "/" + file_info.completeBaseName();
}

auto SetExtensionOfOutputFileForArchive(const QString& path, GpgOperation opera,
                                        bool ascii) -> QString {
  QString extension;
  auto file_info = QFileInfo(path);

  if (ascii) {
    switch (opera) {
      case kENCRYPT:
      case kENCRYPT_SIGN:
        if (file_info.completeSuffix() != "tar") extension += ".tar";
        extension += ".asc";
        return QFileInfo(path).absoluteFilePath() + extension;
        break;
      default:
        break;
    }
  } else {
    switch (opera) {
      case kENCRYPT:
      case kENCRYPT_SIGN:
        if (file_info.completeSuffix() != "tar") extension += ".tar";
        extension += ".gpg";
        return QFileInfo(path).absoluteFilePath() + extension;
        break;
      default:
        break;
    }
  }

  return file_info.absolutePath() + "/" + file_info.baseName();
}

static QContainer<KeyDatabaseInfo> gpg_key_database_info_cache;

auto GPGFRONTEND_CORE_EXPORT GetGpgKeyDatabaseInfos()
    -> QContainer<KeyDatabaseInfo> {
  if (!gpg_key_database_info_cache.empty()) return gpg_key_database_info_cache;

  auto context_index_list = Module::ListRTChildKeys("core", "gpgme.ctx.list");
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
  gpg_key_database_info_cache.resize(
      static_cast<qsizetype>(context_index_list.size()));
#else
  gpg_key_database_info_cache.reserve(
      static_cast<qsizetype>(context_index_list.size()));
  std::fill_n(std::back_inserter(gpg_key_database_info_cache),
              context_index_list.size(), KeyDatabaseInfo{});
#endif

  for (auto& context_index : context_index_list) {
    LOG_D() << "context grt key: " << context_index;

    const auto grt_key_prefix = QString("gpgme.ctx.list.%1").arg(context_index);
    auto channel = Module::RetrieveRTValueTypedOrDefault(
        "core", grt_key_prefix + ".channel", -1);
    auto database_name = Module::RetrieveRTValueTypedOrDefault(
        "core", grt_key_prefix + ".database_name", QString{});
    auto database_path = Module::RetrieveRTValueTypedOrDefault(
        "core", grt_key_prefix + ".database_path", QString{});

    LOG_D() << "context grt channel: " << channel
            << "GRT key prefix: " << grt_key_prefix
            << "database name: " << database_name;

    auto i = KeyDatabaseInfo();
    i.channel = channel;
    i.name = database_name;
    i.path = database_path;
    gpg_key_database_info_cache[channel] = i;
  }

  return gpg_key_database_info_cache;
}
auto GPGFRONTEND_CORE_EXPORT GetGpgKeyDatabaseName(int channel) -> QString {
  auto info = GetGpgKeyDatabaseInfos();
  if (channel >= info.size()) return {};
  return info[channel].name;
}

auto GetKeyDatabasesBySettings() -> QContainer<KeyDatabaseItemSO> {
  auto key_db_list_so = SettingsObject("key_database_list");
  auto key_db_list = KeyDatabaseListSO(key_db_list_so);
  auto& key_dbs = key_db_list.key_databases;

  if (key_dbs.empty()) {
    KeyDatabaseItemSO key_db;

    auto home_path = Module::RetrieveRTValueTypedOrDefault<>(
        "core", "gpgme.ctx.default_database_path", QString{});

    if (GlobalSettingStation::GetInstance().IsProtableMode()) {
      home_path = QDir(GlobalSettingStation::GetInstance().GetAppDir())
                      .relativeFilePath(home_path);
    }

    key_db.channel = 0;
    key_db.name = "DEFAULT";
    key_db.path = home_path;

    key_dbs.append(key_db);
  }

  // Sort by channel
  std::sort(key_dbs.begin(), key_dbs.end(),
            [](const auto& a, const auto& b) { return a.channel < b.channel; });

  // Resolve duplicate channels by incrementing
  for (auto it = key_dbs.begin(); it != key_dbs.end(); ++it) {
    auto next_it = std::next(it);
    while (next_it != key_dbs.end() && next_it->channel == it->channel) {
      next_it->channel = it->channel + 1;
      ++next_it;
    }
  }

  key_db_list_so.Store(key_db_list.ToJson());

  return key_db_list.key_databases;
}

auto VerifyKeyDatabasePath(const QFileInfo& key_database_fs_path) -> bool {
  return key_database_fs_path.isAbsolute() && key_database_fs_path.exists() &&
         key_database_fs_path.isDir();
}

auto SearchKeyDatabasePath(const QStringList& candidate_paths) -> QString {
  for (const auto& path : candidate_paths) {
    if (VerifyKeyDatabasePath(QFileInfo(path))) {
      // return a unify path
      return QFileInfo(path).absoluteFilePath();
    }
  }
  return {};
}

auto GetCanonicalKeyDatabasePath(const QDir& app_path,
                                 const QString& path) -> QString {
  auto target_path = path;
  if (!QDir::isAbsolutePath(target_path)) {
    target_path = app_path.absoluteFilePath(target_path);
    LOG_D() << "convert relative path: " << path
            << "to absolute path: " << target_path;
  }

  QFileInfo info(target_path);
  if (!info.exists()) {
    LOG_W() << "key database not exists:" << info.canonicalFilePath()
            << ", making a new directory...";
    QDir().mkdir(info.canonicalFilePath());
  }

  if (VerifyKeyDatabasePath(info)) {
    auto key_database_fs_path = info.canonicalFilePath();
    LOG_D() << "load gpg key database:" << key_database_fs_path;

    return key_database_fs_path;
  }

  LOG_W() << "gpg key database path is invalid: " << path;
  return {};
}

auto GetKeyDatabaseInfoBySettings() -> QContainer<KeyDatabaseInfo> {
  auto key_dbs = GetKeyDatabasesBySettings();

  QContainer<KeyDatabaseInfo> key_db_infos;

  const auto app_path = QDir(GlobalSettingStation::GetInstance().GetAppDir());

  // try to use user defined key database
  for (const auto& key_database : key_dbs) {
    if (key_database.path.isEmpty()) continue;

    LOG_D() << "got key database:" << key_database.path;

    auto key_database_fs_path =
        GetCanonicalKeyDatabasePath(app_path, key_database.path);

    if (key_database_fs_path.isEmpty()) {
      LOG_E() << "cannot use target path" << key_database.path
              << "as key database";
      continue;
    }

    KeyDatabaseInfo key_db_info;
    key_db_info.name = key_database.name;
    key_db_info.path = key_database_fs_path;
    key_db_info.origin_path = key_database.path;
    key_db_infos.append(key_db_info);

    LOG_D() << "plan to load gpg key database:" << key_database_fs_path;
  }

  return key_db_infos;
}

auto GPGFRONTEND_CORE_EXPORT ConvertKey2GpgKeyIdList(
    int channel, const GpgAbstractKeyPtrList& keys) -> KeyIdArgsList {
  KeyIdArgsList ret;
  for (const auto& key : ConvertKey2GpgKeyList(channel, keys)) {
    ret.push_back(key->ID());
  }
  return ret;
}

auto GPGFRONTEND_CORE_EXPORT ConvertKey2GpgKeyList(
    int channel, const GpgAbstractKeyPtrList& keys) -> GpgKeyPtrList {
  GpgKeyPtrList recipients;

  QSet<QString> s;
  for (const auto& key : keys) {
    if (key == nullptr || key->IsDisabled() || s.contains(key->ID())) continue;

    if (key->KeyType() == GpgAbstractKeyType::kGPG_KEY) {
      recipients.push_back(qSharedPointerDynamicCast<GpgKey>(key));
    } else if (key->KeyType() == GpgAbstractKeyType::kGPG_KEYGROUP) {
      auto key_ids = qSharedPointerDynamicCast<GpgKeyGroup>(key)->KeyIds();
      recipients += ConvertKey2GpgKeyList(
          channel, GpgAbstractKeyGetter::GetInstance().GetKeys(key_ids));
    }

    s.insert(key->ID());
  }

  assert(std::all_of(keys.begin(), keys.end(),
                     [](const auto& key) { return key->IsGood(); }));

  return recipients;
}

auto GPGFRONTEND_CORE_EXPORT Convert2RawGpgMEKeyList(
    int channel, const GpgAbstractKeyPtrList& keys) -> QContainer<gpgme_key_t> {
  QContainer<gpgme_key_t> recipients;

  auto g_keys = ConvertKey2GpgKeyList(channel, keys);
  for (const auto& key : g_keys) {
    recipients.push_back(
        static_cast<gpgme_key_t>(*qSharedPointerDynamicCast<GpgKey>(key)));
  }

  recipients.push_back(nullptr);
  return recipients;
}

auto GPGFRONTEND_CORE_EXPORT GetUsagesByAbstractKey(const GpgAbstractKey* key)
    -> QString {
  QString usages;
  if (key->IsHasCertCap()) usages += "C";
  if (key->IsHasEncrCap()) usages += "E";
  if (key->IsHasSignCap()) usages += "S";
  if (key->IsHasAuthCap()) usages += "A";

  if (key->KeyType() == GpgAbstractKeyType::kGPG_SUBKEY) {
    if (dynamic_cast<const GpgSubKey*>(key)->IsADSK()) usages += "R";
  }
  return usages;
}

auto GPGFRONTEND_CORE_EXPORT GetGpgKeyByGpgAbstractKey(GpgAbstractKey* ab_key)
    -> GpgKey {
  if (!ab_key->IsGood()) return {};

  if (ab_key->KeyType() == GpgAbstractKeyType::kGPG_SUBKEY) {
    auto* s_key = dynamic_cast<GpgSubKey*>(ab_key);

    assert(s_key != nullptr);
    if (s_key == nullptr) return {};

    return *s_key->Convert2GpgKey();
  }

  auto* key = dynamic_cast<GpgKey*>(ab_key);
  return *key;
}

auto GPGFRONTEND_CORE_EXPORT IsKeyGroupID(const KeyId& id) -> bool {
  return id.startsWith("#&");
}

}  // namespace GpgFrontend