add hacky way to load encrypted tox saves

This commit is contained in:
Green Sky 2023-08-28 15:33:36 +02:00
parent 9d9a486537
commit d131b5a02f
No known key found for this signature in database
7 changed files with 102 additions and 14 deletions

View File

@ -134,6 +134,10 @@ add_library(toxcore STATIC
${TOX_DIR}toxcore/tox_unpack.h
${TOX_DIR}toxcore/util.c
${TOX_DIR}toxcore/util.h
${TOX_DIR}toxencryptsave/defines.h
${TOX_DIR}toxencryptsave/toxencryptsave.c
${TOX_DIR}toxencryptsave/toxencryptsave.h
)
# HACK: "install" api headers into self

View File

@ -6,11 +6,11 @@
#include <memory>
MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::vector<std::string> plugins) :
MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector<std::string> plugins) :
renderer(renderer_),
rmm(cr),
mts(rmm),
tc(save_path),
tc(save_path, save_password),
ad(tc),
tcm(cr, tc, tc),
tmm(rmm, cr, tcm, tc, tc),

View File

@ -56,7 +56,7 @@ struct MainScreen final : public Screen {
ChatGui4 cg;
MainScreen(SDL_Renderer* renderer_, std::string save_path, std::vector<std::string> plugins);
MainScreen(SDL_Renderer* renderer_, std::string save_path, std::string save_password, std::vector<std::string> plugins);
~MainScreen(void);
bool handleEvent(SDL_Event& e) override;

View File

@ -3,6 +3,7 @@
#include "./main_screen.hpp"
#include <imgui/imgui.h>
#include <imgui/misc/cpp/imgui_stdlib.h>
#include <memory>
#include <filesystem>
@ -24,6 +25,10 @@ Screen* StartScreen::poll(bool&) {
if (ImGui::BeginTabBar("view")) {
if (ImGui::BeginTabItem("load profile")) {
_new_save = false;
ImGui::TextUnformatted("profile :");
ImGui::SameLine();
if (ImGui::Button("select")) {
_fss.requestFile(
[](const auto& path) -> bool { return std::filesystem::is_regular_file(path); },
@ -34,16 +39,36 @@ Screen* StartScreen::poll(bool&) {
);
}
ImGui::SameLine();
ImGui::Text("profile: %s", tox_profile_path.c_str());
ImGui::TextUnformatted(tox_profile_path.c_str());
ImGui::Text("TODO: profile password");
ImGui::TextUnformatted("password:");
ImGui::SameLine();
if (_show_password) {
ImGui::InputText("##password", &_password);
} else {
ImGui::InputText("##password", &_password, ImGuiInputTextFlags_Password);
}
ImGui::SameLine();
ImGui::Checkbox("show password", &_show_password);
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("create profile")) {
ImGui::Text("TODO: profile path");
ImGui::Text("TODO: profile name");
ImGui::Text("TODO: profile password");
_new_save = true;
ImGui::TextUnformatted("TODO: profile path");
ImGui::TextUnformatted("TODO: profile name");
ImGui::TextUnformatted("password:");
ImGui::SameLine();
if (_show_password) {
ImGui::InputText("##password", &_password);
} else {
ImGui::InputText("##password", &_password, ImGuiInputTextFlags_Password);
}
ImGui::SameLine();
ImGui::Checkbox("show password", &_show_password);
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("plugins")) {
@ -80,9 +105,31 @@ Screen* StartScreen::poll(bool&) {
ImGui::Separator();
if (ImGui::Button("load", {60, 25})) {
auto new_screen = std::make_unique<MainScreen>(_renderer, tox_profile_path, queued_plugin_paths);
return new_screen.release();
if (!_new_save && !std::filesystem::is_regular_file(tox_profile_path)) {
// load but file missing
ImGui::BeginDisabled();
ImGui::Button("load", {60, 25});
ImGui::EndDisabled();
if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip | ImGuiHoveredFlags_AllowWhenDisabled)) {
ImGui::SetTooltip("file does not exist");
}
} else if (_new_save && std::filesystem::exists(tox_profile_path)) {
// new but file exists
ImGui::BeginDisabled();
ImGui::Button("load", {60, 25});
ImGui::EndDisabled();
if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip | ImGuiHoveredFlags_AllowWhenDisabled)) {
ImGui::SetTooltip("file already exists");
}
} else {
if (ImGui::Button("load", {60, 25})) {
auto new_screen = std::make_unique<MainScreen>(_renderer, tox_profile_path, _password, queued_plugin_paths);
return new_screen.release();
}
}
_fss.render();

View File

@ -16,6 +16,11 @@ struct StartScreen final : public Screen {
SDL_Renderer* _renderer;
FileSelector _fss;
bool _new_save {false};
bool _show_password {false};
std::string _password;
std::string tox_profile_path {"tomato.tox"};
std::vector<std::string> queued_plugin_paths;

View File

@ -1,5 +1,10 @@
#include "./tox_client.hpp"
// meh, change this
#include <exception>
#include <system_error>
#include <toxencryptsave/toxencryptsave.h>
#include <sodium.h>
#include <vector>
@ -7,8 +12,8 @@
#include <iostream>
#include <cassert>
ToxClient::ToxClient(std::string_view save_path) :
_tox_profile_path(save_path)
ToxClient::ToxClient(std::string_view save_path, std::string_view save_password) :
_tox_profile_path(save_path), _tox_profile_password(save_password)
{
TOX_ERR_OPTIONS_NEW err_opt_new;
Tox_Options* options = tox_options_new(&err_opt_new);
@ -34,6 +39,19 @@ ToxClient::ToxClient(std::string_view save_path) :
std::cerr << "empty tox save\n";
} else {
// set options
if (!save_password.empty()) {
std::vector<uint8_t> encrypted_copy(profile_data.begin(), profile_data.end());
//profile_data.clear();
profile_data.resize(encrypted_copy.size() - TOX_PASS_ENCRYPTION_EXTRA_LENGTH);
if (!tox_pass_decrypt(
encrypted_copy.data(), encrypted_copy.size(),
reinterpret_cast<const uint8_t*>(save_password.data()), save_password.size(),
profile_data.data(),
nullptr // TODO: error checking
)) {
throw std::runtime_error("FAILED to decrypt save file!!!!");
}
}
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
tox_options_set_savedata_data(options, profile_data.data(), profile_data.size());
}
@ -122,6 +140,19 @@ void ToxClient::saveToxProfile(void) {
data.resize(tox_get_savedata_size(_tox));
tox_get_savedata(_tox, data.data());
if (!_tox_profile_password.empty()) {
std::vector<uint8_t> unencrypted_copy(data.begin(), data.end());
//profile_data.clear();
data.resize(unencrypted_copy.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH);
if (!tox_pass_encrypt(
unencrypted_copy.data(), unencrypted_copy.size(),
reinterpret_cast<const uint8_t*>(_tox_profile_password.data()), _tox_profile_password.size(),
data.data(),
nullptr // TODO: error checking
)) {
throw std::runtime_error("FAILED to encrypt save file!!!!");
}
}
std::ofstream ofile{_tox_profile_path, std::ios::binary};
// TODO: improve
for (const auto& ch : data) {

View File

@ -20,11 +20,12 @@ class ToxClient : public ToxDefaultImpl, public ToxEventProviderBase {
std::string _self_name;
std::string _tox_profile_path;
std::string _tox_profile_password;
bool _tox_profile_dirty {true}; // set in callbacks
public:
//ToxClient(/*const CommandLine& cl*/);
ToxClient(std::string_view save_path);
ToxClient(std::string_view save_path, std::string_view save_password);
~ToxClient(void);
public: // tox stuff