/**
* Copyright (C) 2021 Saturneric
*
* 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 .
*
* 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 starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include "core/GpgContext.h"
#include
#include
#include "GpgConstants.h"
#include "core/function/gpg/GpgCommandExecutor.h"
#ifdef _WIN32
#include
#endif
namespace GpgFrontend {
GpgContext::GpgContext(int channel)
: SingletonFunctionObject(channel) {}
/**
* Constructor
* Set up gpgme-context, set paths to app-run path
*/
GpgContext::GpgContext(const GpgContextInitArgs &args) : args_(args) {
static bool _first = true;
if (_first) {
/* Initialize the locale environment. */
SPDLOG_INFO("locale: {}", setlocale(LC_CTYPE, nullptr));
info_.GpgMEVersion = gpgme_check_version(nullptr);
gpgme_set_locale(nullptr, LC_CTYPE, setlocale(LC_CTYPE, nullptr));
#ifdef LC_MESSAGES
gpgme_set_locale(nullptr, LC_MESSAGES, setlocale(LC_MESSAGES, nullptr));
#endif
_first = false;
}
gpgme_ctx_t _p_ctx;
check_gpg_error(gpgme_new(&_p_ctx));
_ctx_ref = CtxRefHandler(_p_ctx);
if (args.gpg_alone) {
info_.AppPath = args.gpg_path;
auto err = gpgme_ctx_set_engine_info(_ctx_ref.get(), GPGME_PROTOCOL_OpenPGP,
info_.AppPath.c_str(),
info_.DatabasePath.c_str());
assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR);
}
auto engine_info = gpgme_ctx_get_engine_info(*this);
// Check ENV before running
bool check_passed = false, find_openpgp = false, find_gpgconf = false,
find_cms = false;
while (engine_info != nullptr) {
if (!strcmp(engine_info->version, "1.0.0")) {
engine_info = engine_info->next;
continue;
}
SPDLOG_INFO(
"engine info: {} {} {} {}",
gpgme_get_protocol_name(engine_info->protocol),
std::string(engine_info->file_name == nullptr ? "null"
: engine_info->file_name),
std::string(engine_info->home_dir == nullptr ? "null"
: engine_info->home_dir),
std::string(engine_info->version ? "null" : engine_info->version));
switch (engine_info->protocol) {
case GPGME_PROTOCOL_OpenPGP:
find_openpgp = true;
info_.AppPath = engine_info->file_name;
info_.GnupgVersion = engine_info->version;
info_.DatabasePath = std::string(engine_info->home_dir == nullptr
? "default"
: engine_info->home_dir);
break;
case GPGME_PROTOCOL_CMS:
find_cms = true;
info_.CMSPath = engine_info->file_name;
break;
case GPGME_PROTOCOL_GPGCONF:
find_gpgconf = true;
info_.GpgConfPath = engine_info->file_name;
break;
case GPGME_PROTOCOL_ASSUAN:
info_.AssuanPath = engine_info->file_name;
break;
case GPGME_PROTOCOL_G13:
break;
case GPGME_PROTOCOL_UISERVER:
break;
case GPGME_PROTOCOL_SPAWN:
break;
case GPGME_PROTOCOL_DEFAULT:
break;
case GPGME_PROTOCOL_UNKNOWN:
break;
}
engine_info = engine_info->next;
}
// set custom key db path
if (!args.db_path.empty()) {
info_.DatabasePath = args.db_path;
auto err = gpgme_ctx_set_engine_info(_ctx_ref.get(), GPGME_PROTOCOL_OpenPGP,
info_.AppPath.c_str(),
info_.DatabasePath.c_str());
SPDLOG_INFO("ctx set custom key db path: {}", info_.DatabasePath);
assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR);
}
// conditional check
if ((info_.GnupgVersion >= "2.0.0" && find_gpgconf && find_openpgp &&
find_cms) ||
(info_.GnupgVersion > "1.0.0" && find_gpgconf))
check_passed = true;
if (!check_passed) {
this->good_ = false;
SPDLOG_ERROR("env check failed");
return;
} else {
SPDLOG_INFO("gnupg version {}", info_.GnupgVersion);
init_ctx();
good_ = true;
}
}
void GpgContext::init_ctx() {
// Set Independent Database
if (info_.GnupgVersion <= "2.0.0" && args_.independent_database) {
info_.DatabasePath = args_.db_path;
SPDLOG_INFO("custom key db path {}", info_.DatabasePath);
auto err = gpgme_ctx_set_engine_info(_ctx_ref.get(), GPGME_PROTOCOL_OpenPGP,
info_.AppPath.c_str(),
info_.DatabasePath.c_str());
assert(check_gpg_error_2_err_code(err) == GPG_ERR_NO_ERROR);
} else {
info_.DatabasePath = "default";
}
if (args_.ascii) {
/** Setting the output type must be done at the beginning */
/** think this means ascii-armor --> ? */
gpgme_set_armor(*this, 1);
} else {
/** Setting the output type must be done at the beginning */
/** think this means ascii-armor --> ? */
gpgme_set_armor(*this, 0);
}
// Speed up loading process
gpgme_set_offline(*this, 1);
if (info_.GnupgVersion >= "2.0.0") {
check_gpg_error(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));
} else {
check_gpg_error(gpgme_set_keylist_mode(
*this, GPGME_KEYLIST_MODE_LOCAL | GPGME_KEYLIST_MODE_SIGS |
GPGME_KEYLIST_MODE_SIG_NOTATIONS |
GPGME_KEYLIST_MODE_WITH_TOFU));
}
// for unit test
if (args_.test_mode) {
if (info_.GnupgVersion >= "2.1.0") SetPassphraseCb(test_passphrase_cb);
gpgme_set_status_cb(*this, test_status_cb, nullptr);
}
}
bool GpgContext::good() const { return good_; }
void GpgContext::SetPassphraseCb(gpgme_passphrase_cb_t cb) const {
if (info_.GnupgVersion >= "2.1.0") {
if (gpgme_get_pinentry_mode(*this) != GPGME_PINENTRY_MODE_LOOPBACK) {
gpgme_set_pinentry_mode(*this, GPGME_PINENTRY_MODE_LOOPBACK);
}
gpgme_set_passphrase_cb(*this, cb, nullptr);
} else {
SPDLOG_ERROR("not supported for gnupg version: {}", info_.GnupgVersion);
}
}
gpgme_error_t GpgContext::test_passphrase_cb(void *opaque, const char *uid_hint,
const char *passphrase_info,
int last_was_bad, int fd) {
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);
}
gpgme_error_t GpgContext::test_status_cb(void *hook, const char *keyword,
const char *args) {
SPDLOG_INFO("keyword {}", keyword);
return GPG_ERR_NO_ERROR;
}
const GpgInfo &GpgContext::GetInfo(bool refresh) {
if (!extend_info_loaded_ || refresh) {
GpgCommandExecutor::GetInstance().Execute(
info_.GpgConfPath, {"--list-components"},
[=](int exit_code, const std::string &p_out, const std::string &p_err) {
SPDLOG_INFO(
"gpgconf components exit_code: {} process stdout size: {}",
exit_code, p_out.size());
if (exit_code != 0) {
SPDLOG_ERROR(
"gpgconf execute error, process stderr: {} ,process stdout: "
"{}",
p_err, p_out);
return;
}
auto &components_info = info_.ComponentsInfo;
components_info["gpgme"] = {"GPG Made Easy", info_.GpgMEVersion,
_("Embedded In")};
components_info["gpgconf"] = {"GPG Configure", "/",
info_.GpgConfPath};
std::vector line_split_list;
boost::split(line_split_list, p_out, boost::is_any_of("\n"));
for (const auto &line : line_split_list) {
std::vector info_split_list;
boost::split(info_split_list, line, boost::is_any_of(":"));
SPDLOG_INFO("gpgconf info line: {} info size: {}", line,
info_split_list.size());
if (info_split_list.size() != 3) continue;
auto component_name = info_split_list[0];
if (component_name == "gpg") {
components_info[component_name] = {
info_split_list[1], info_.GnupgVersion, info_split_list[2]};
} else {
components_info[component_name] = {info_split_list[1], "/",
info_split_list[2]};
}
}
});
GpgCommandExecutor::GetInstance().Execute(
info_.GpgConfPath, {"--list-dirs"},
[=](int exit_code, const std::string &p_out, const std::string &p_err) {
SPDLOG_INFO(
"gpgconf configurations exit_code: {} process stdout size: {}",
exit_code, p_out.size());
if (exit_code != 0) {
SPDLOG_ERROR(
"gpgconf execute error, process stderr: {} process stdout: {}",
p_err, p_out);
return;
}
auto &configurations_info = info_.ConfigurationsInfo;
std::vector line_split_list;
boost::split(line_split_list, p_out, boost::is_any_of("\n"));
for (const auto &line : line_split_list) {
std::vector info_split_list;
boost::split(info_split_list, line, boost::is_any_of(":"));
SPDLOG_INFO("gpgconf info line: {} info size: {}", line,
info_split_list.size());
if (info_split_list.size() != 2) continue;
auto configuration_name = info_split_list[0];
configurations_info[configuration_name] = {info_split_list[1]};
}
});
for (const auto &component : info_.ComponentsInfo) {
SPDLOG_INFO("gpgconf check options ready", "component", component.first);
if (component.first == "gpgme" || component.first == "gpgconf") continue;
GpgCommandExecutor::GetInstance().Execute(
info_.GpgConfPath, {"--check-options", component.first},
[=](int exit_code, const std::string &p_out,
const std::string &p_err) {
SPDLOG_INFO(
"gpgconf options exit_code: {} process stdout size: {} "
"component: {} ",
exit_code, p_out.size(), component.first);
if (exit_code != 0) {
SPDLOG_ERROR(
"gpgconf execute error, process stderr: {} , process stdout:",
p_err, p_out);
return;
}
auto &options_info = info_.OptionsInfo;
std::vector line_split_list;
boost::split(line_split_list, p_out, boost::is_any_of("\n"));
for (const auto &line : line_split_list) {
std::vector info_split_list;
boost::split(info_split_list, line, boost::is_any_of(":"));
SPDLOG_INFO("gpgconf info line: {} info size: {}", line,
info_split_list.size());
if (info_split_list.size() != 6) continue;
auto configuration_name = info_split_list[0];
options_info[configuration_name] = {
info_split_list[1], info_split_list[2], info_split_list[3],
info_split_list[4], info_split_list[5]};
}
});
}
for (const auto &component : info_.ComponentsInfo) {
SPDLOG_INFO("gpgconf list options ready", "component", component.first);
if (component.first == "gpgme" || component.first == "gpgconf") continue;
GpgCommandExecutor::GetInstance().Execute(
info_.GpgConfPath, {"--list-options", component.first},
[=](int exit_code, const std::string &p_out,
const std::string &p_err) {
SPDLOG_INFO(
"gpgconf options exit_code: {} process stdout size: {} "
"component: {} ",
exit_code, p_out.size(), component.first);
if (exit_code != 0) {
SPDLOG_ERROR(
"gpgconf execute error, process stderr: {} , process stdout:",
p_err, p_out);
return;
}
auto &available_options_info = info_.AvailableOptionsInfo;
std::vector line_split_list;
boost::split(line_split_list, p_out, boost::is_any_of("\n"));
for (const auto &line : line_split_list) {
std::vector info_split_list;
boost::split(info_split_list, line, boost::is_any_of(":"));
SPDLOG_INFO("gpgconf info line: {} info size: {}", line,
info_split_list.size());
if (info_split_list.size() != 10) continue;
auto configuration_name = info_split_list[0];
available_options_info[configuration_name] = {
info_split_list[1], info_split_list[2], info_split_list[3],
info_split_list[4], info_split_list[5], info_split_list[6],
info_split_list[7], info_split_list[8], info_split_list[9]};
}
});
}
extend_info_loaded_ = true;
}
return info_;
}
void GpgContext::_ctx_ref_deleter::operator()(gpgme_ctx_t _ctx) {
if (_ctx != nullptr) gpgme_release(_ctx);
}
} // namespace GpgFrontend