From 2609a8cdebca4b37ae41f0529a4737fb42f82b83 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Thu, 6 Jan 2022 00:51:56 +0800 Subject: (ui, project): load root certs. 1. add root certs file. 2. loading root certs at startup. 3. store certs in GlobalSettingStation. --- src/init.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index 4f441c29..e774ab6e 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -23,9 +23,34 @@ */ #include +#include #include "ui/settings/GlobalSettingStation.h" +vmime::shared_ptr load_x509_cert( + const boost::filesystem::path& path) { + auto out_buffer = GpgFrontend::read_all_data_in_file(path.string()); + auto cert = vmime::security::cert::X509Certificate::import( + reinterpret_cast(out_buffer.data()), + out_buffer.size()); + return cert; +} + +std::vector get_files_of_directory( + const boost::filesystem::path& _path) { + namespace fs = boost::filesystem; + std::vector path_list; + if (!_path.empty()) { + fs::recursive_directory_iterator end; + + for (fs::recursive_directory_iterator i(_path); i != end; ++i) { + const fs::path cp = (*i); + path_list.push_back(cp); + } + } + return path_list; +} + void init_logging() { using namespace boost::posix_time; using namespace boost::gregorian; @@ -49,7 +74,20 @@ void init_logging() { el::Loggers::reconfigureLogger("default", defaultConf); - LOG(INFO) << _("Logfile Path") << logfile_path; + LOG(INFO) << _("logfile Path") << logfile_path; +} + +void init_certs() { + std::vector> + root_certs; + auto cert_file_paths = get_files_of_directory( + GpgFrontend::UI::GlobalSettingStation::GetInstance().GetCertsDir()); + for (const auto& cert_file_path : cert_file_paths) { + auto _cert = load_x509_cert(cert_file_path); + root_certs.push_back(_cert); + } + LOG(INFO) << _("root certs loaded") << root_certs.size(); + GpgFrontend::UI::GlobalSettingStation::GetInstance().SetRootCerts(root_certs); } void init_locale() { @@ -72,7 +110,7 @@ void init_locale() { // read from settings file std::string lang; if (!general.lookupValue("lang", lang)) { - LOG(ERROR) << _("Could not read properly from configure file"); + LOG(ERROR) << _("could not read properly from configure file"); }; LOG(INFO) << "lang from settings" << lang; -- cgit v1.2.3