Skip to content

Commit

Permalink
remove binary_dir from sec_module interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sinev-valentine committed Nov 22, 2018
1 parent 269f5ac commit 1ee8ff8
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 87 deletions.
4 changes: 2 additions & 2 deletions keychain_cmd_app/sec_mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ void sec_mod_dummy::print_mnemonic(const string_list& mnemonic) const
{
}

byte_seq_t sec_mod_dummy::get_passwd_trx_raw(const std::string& raw_trx, std::string binary_dir) const
byte_seq_t sec_mod_dummy::get_passwd_trx_raw(const std::string& raw_trx) const
{
std::string str = "blank";
keychain_app::byte_seq_t pass(str.begin(), str.end());
return pass;
}

byte_seq_t sec_mod_dummy::get_passwd_on_create( std::string binary_dir) const
byte_seq_t sec_mod_dummy::get_passwd_on_create() const
{
std::string str = "blank";
keychain_app::byte_seq_t pass(str.begin(), str.end());
Expand Down
4 changes: 2 additions & 2 deletions keychain_cmd_app/sec_mod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class sec_mod_dummy: public secure_dlg_mod_base
public:
sec_mod_dummy();
virtual ~sec_mod_dummy();
virtual keychain_app::byte_seq_t get_passwd_trx_raw(const std::string& raw_trx, std::string binary_dir) const override;
virtual keychain_app::byte_seq_t get_passwd_on_create(std::string binary_dir) const override;
virtual keychain_app::byte_seq_t get_passwd_trx_raw(const std::string& raw_trx) const override;
virtual keychain_app::byte_seq_t get_passwd_on_create() const override;
virtual void print_mnemonic(const string_list& mnemonic) const override;
private:
static constexpr const char* pass_str = "blank_password";
Expand Down
23 changes: 5 additions & 18 deletions keychain_lib/include/keychain_lib/keychain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@

#include "keychain_commands.hpp"

#ifdef __linux__
#define KEY_DEFAULT_PATH "/var/keychain"
#define LOG_DEFAULT_PATH "/var/keychain/logs"
#else

#if defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
//#error "Need to define path to KEYCHAIN_DATA"
#define KEY_DEFAULT_PATH "data/keychain/key_data"
#define LOG_DEFAULT_PATH "data/keychain/logs"
#else
#error "Need to define path to KEYCHAIN_DATA"
#endif
#endif


namespace keychain_app
{
Expand All @@ -45,9 +31,9 @@ class secure_dlg_mod_base
using string_list = std::list<std::wstring>;

virtual ~secure_dlg_mod_base(){}
virtual byte_seq_t get_passwd_trx_raw(const std::string& raw_trx, std::string binary_dir) const = 0;
// virtual std::wstring get_passwd_trx(const graphene::chain::transaction& trx) const = 0;
virtual byte_seq_t get_passwd_on_create(std::string binary_dir) const = 0;
virtual byte_seq_t get_passwd_trx_raw(const std::string& raw_trx) const = 0;
// virtual std::wstring get_passwd_trx(const graphene::chain::transaction& trx) const = 0;
virtual byte_seq_t get_passwd_on_create() const = 0;
virtual void print_mnemonic(const string_list& mnemonic) const = 0;
};

Expand All @@ -59,7 +45,8 @@ class keychain : public keychain_base
virtual ~keychain();
virtual std::string operator()(const fc_light::variant& command) override;
private:
keychain(const secure_dlg_mod_base* , const char* default_key_dir = KEY_DEFAULT_PATH);
bfs::path m_init_path;
keychain(const secure_dlg_mod_base* );
};

struct keychain_commands_singletone
Expand Down
37 changes: 24 additions & 13 deletions keychain_lib/include/keychain_lib/keychain_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,29 @@
#include "keychain_logger.hpp"
#include <ctime>

#ifdef __linux__
#define KEY_DEFAULT_PATH "/var/keychain"
#define LOG_DEFAULT_PATH "/var/keychain/logs"
#define KEY_DEFAULT_PATH_ KEY_DEFAULT_PATH "/key_data"
#else

#if defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
//#error "Need to define path to KEYCHAIN_DATA"
#define KEY_DEFAULT_PATH "data/keychain"
#define LOG_DEFAULT_PATH "data/keychain/logs"
#define KEY_DEFAULT_PATH_ KEY_DEFAULT_PATH "/key_data"
#else
#error "Need to define path to KEYCHAIN_DATA"
#endif
#endif


// after password entry the decrypted private key stored in memory during this time
// This allow sing transaction without password entry.
#define DEF_UNLOCK_SECONDS 0

namespace keychain_app {

namespace bfs = boost::filesystem;

using byte_seq_t = std::vector<char>;

enum struct blockchain_te {unknown=0, bitshares, array, ethereum, bitcoin};
Expand Down Expand Up @@ -110,11 +125,10 @@ class keychain_base
keychain_base();
virtual ~keychain_base();
virtual std::string operator()(const fc_light::variant& command) = 0;
boost::signals2::signal<byte_seq_t(const std::string&, std::string)> get_passwd_trx_raw;
boost::signals2::signal<byte_seq_t(std::string)> get_passwd_on_create;
boost::signals2::signal<byte_seq_t(const std::string&)> get_passwd_trx_raw;
boost::signals2::signal<byte_seq_t(void)> get_passwd_on_create;
boost::signals2::signal<void(const string_list&)> print_mnemonic;
int unlock_time;
bfs::path binary_dir;

std::unordered_map<std::string, std::pair<std::string, std::time_t>> key_map;
};
Expand Down Expand Up @@ -149,7 +163,6 @@ size_t from_hex(const std::string& hex_str, unsigned char* out_data, size_t out_
std::string to_hex(const uint8_t* data, size_t length);
std::string read_private_key(keychain_base *, std::string , std::string);
std::pair<std::string, std::string> read_private_key_file( keychain_base * , std::string , std::string );
std::string keyname_to_filename (std::string);

/*{
using out_map = std::map<std::string, nlohmann::json>;
Expand Down Expand Up @@ -212,16 +225,14 @@ struct find_keyfile_by_username
{
if (!bfs::is_regular_file(unit.status()))
return false;
const auto &file_path = unit.path().filename();

auto j_keyfile = open_keyfile(file_path.c_str());

auto j_keyfile = open_keyfile(unit.path().c_str());
auto keyfile = j_keyfile.as<keyfile_format::keyfile_t>();
if(m_keyfile)
*m_keyfile = keyfile;//NOTE: move semantic is not implemented in fc_light::variant in fact
return strcmp(m_keyname, keyname_to_filename(keyfile.keyname).c_str()) == 0;
return strcmp(m_keyname, keyfile.keyname.c_str()) == 0;
}
const char* m_keyname;
keychain_base* m_pkeychain;
keyfile_format::keyfile_t* m_keyfile;
};

Expand Down Expand Up @@ -477,7 +488,7 @@ struct keychain_command<command_te::create>: keychain_command_base

if (params.encrypted)
{
auto passwd = *keychain->get_passwd_on_create(keychain->binary_dir.string());
auto passwd = *keychain->get_passwd_on_create();
if (passwd.empty())
throw std::runtime_error("Error: can't get password");
auto& encryptor = encryptor_singletone::instance();
Expand All @@ -499,7 +510,7 @@ struct keychain_command<command_te::create>: keychain_command_base
if(filename.empty())
throw std::runtime_error("Error: keyname (filename) is empty");

auto first = bfs::directory_iterator(bfs::path("./"));
auto first = bfs::directory_iterator(bfs::path(KEY_DEFAULT_PATH_));
auto it = std::find_if(first, bfs::directory_iterator(),find_keyfile_by_username(keyfile.keyname.c_str()));
if(it != bfs::directory_iterator())
throw std::runtime_error("Error: keyfile for this user is already exist");
Expand Down
26 changes: 15 additions & 11 deletions keychain_lib/src/keychain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@

using namespace keychain_app;


keychain_base::keychain_base()
{
unlock_time =DEF_UNLOCK_SECONDS;
}

keychain_base::~keychain_base(){}


keychain_commands_singletone::keychain_commands_singletone()
{
m_command_list.reserve(32);//TODO: it is may be possible to get size info from boost::hana::tuple
Expand All @@ -39,14 +48,11 @@ keychain& keychain::instance(const secure_dlg_mod_base* secure_dlg )
}


keychain::keychain(const secure_dlg_mod_base* secure_dlg, const char* default_key_dir)
: keychain_base()
keychain::keychain(const secure_dlg_mod_base* secure_dlg)
: keychain_base(),
m_init_path(bfs::current_path())
{
binary_dir = bfs::current_path();
std::string dir(default_key_dir);
bfs::path key_dir(dir);

key_dir += "/key_data";
bfs::path key_dir(KEY_DEFAULT_PATH_);

if(!bfs::exists(key_dir))
{
Expand All @@ -56,17 +62,15 @@ keychain::keychain(const secure_dlg_mod_base* secure_dlg, const char* default_ke
}

get_passwd_trx_raw.connect(std::bind(&secure_dlg_mod_base::get_passwd_trx_raw, secure_dlg,
std::placeholders::_1, std::placeholders::_2));
get_passwd_on_create.connect(std::bind(&secure_dlg_mod_base::get_passwd_on_create, secure_dlg,
std::placeholders::_1));
get_passwd_on_create.connect(std::bind(&secure_dlg_mod_base::get_passwd_on_create, secure_dlg));
print_mnemonic.connect(std::bind(&secure_dlg_mod_base::print_mnemonic, secure_dlg,
std::placeholders::_1));
bfs::current_path(key_dir);
}

keychain::~keychain()
{
bfs::current_path(binary_dir);
bfs::current_path(m_init_path);
}

std::string keychain::operator()(const fc_light::variant& command) {
Expand Down
36 changes: 9 additions & 27 deletions keychain_lib/src/keychain_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
#include <fc_light/io/json.hpp>

#include "keychain_commands.hpp"
#include "keychain.hpp"


std::pair<std::string, std::string> keychain_app::read_private_key_file(
keychain_base * keychain, std::string filename, std::string text)
keychain_base * keychain, std::string keyname, std::string text)
{
keyfile_format::keyfile_t keyfile;
auto curdir = bfs::current_path();
auto first = bfs::directory_iterator(bfs::path("./"));
auto it = std::find_if(first, bfs::directory_iterator(),find_keyfile_by_username(filename.c_str(), &keyfile));
auto first = bfs::directory_iterator(bfs::path(KEY_DEFAULT_PATH_));
auto it = std::find_if(first, bfs::directory_iterator(),find_keyfile_by_username(keyname.c_str(), &keyfile));
if (it == bfs::directory_iterator())
throw std::runtime_error("Error: keyfile could not found by keyname");

Expand All @@ -29,7 +30,7 @@ std::pair<std::string, std::string> keychain_app::read_private_key_file(
// If we can parse transaction we need to use get_passwd_trx function
// else use get_passwd_trx_raw()
// At this moment parsing of transaction is not implemented
byte_seq_t passwd = *(keychain->get_passwd_trx_raw(text.empty() ? keyfile.keyname: text, keychain->binary_dir.string()));
byte_seq_t passwd = *(keychain->get_passwd_trx_raw(text.empty() ? keyfile.keyname: text));
if (passwd.empty())
throw std::runtime_error("Error: can't get password");
return std::make_pair(encryptor.decrypt_keydata(passwd, encrypted_data), keyfile.keyname);
Expand All @@ -52,7 +53,7 @@ std::string keychain_app::read_private_key(keychain_base * keychain, std::string
}

if (locked)
return read_private_key_file(keychain, keyname_to_filename(keyname), text).first;
return read_private_key_file(keychain, keyname, text).first;
else
{
// reset key timer after each key use
Expand All @@ -61,15 +62,6 @@ std::string keychain_app::read_private_key(keychain_base * keychain, std::string
}
}

std::string keychain_app::keyname_to_filename(std::string keyname)
{
auto delim = keyname.find('@');
if (delim == std::string::npos || delim == 0 || delim == keyname.length()-1 )
throw std::runtime_error("Invalid keyname: "+keyname);
return keyname.substr(delim +1) + ".json";

}

std::string keychain_app::to_hex(const uint8_t* data, size_t length)
{
std::string r;
Expand Down Expand Up @@ -112,28 +104,18 @@ namespace bfs = keychain_app::bfs;

void keychain_app::create_keyfile(const char* filename, const fc_light::variant& keyfile_var)
{
bfs::path filepath(filename);
bfs::path filepath(std::string(KEY_DEFAULT_PATH_"/") +std::string(filename));
if(bfs::exists(filepath))
throw std::runtime_error("Error: can not create keyfile, file is currently exist");
auto path = bfs::current_path();
path += bfs::path("/");
path += filepath;
auto fout = std::ofstream(filename);

auto fout = std::ofstream(filepath.c_str());
if(!fout.is_open())
throw std::runtime_error("Error: cannot open keyfile");
fout << fc_light::json::to_pretty_string(keyfile_var) << std::endl;
}

using namespace keychain_app;

keychain_base::keychain_base()
{
unlock_time =DEF_UNLOCK_SECONDS;
}


keychain_base::~keychain_base(){}


sha2_256_encoder::sha2_256_encoder()
{
Expand Down
8 changes: 3 additions & 5 deletions keychain_linux/passentry_cmd/pass_entry_term.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "pass_entry_term.hpp"
#include "cmd.hpp"

#define path_ "../../passentry_gui"
#define path_ "./passentry_gui"

pass_entry_term::pass_entry_term()
{
Expand Down Expand Up @@ -166,8 +166,7 @@ std::list<std::string> pass_entry_term::parse_device_file()
return std::move( devices);
}

keychain_app::byte_seq_t pass_entry_term::fork_gui(const KeySym * map, const std::string& raw_trx,
std::string binary_dir ){
keychain_app::byte_seq_t pass_entry_term::fork_gui(const KeySym * map, const std::string& raw_trx ){
int sockets[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) throw std::runtime_error("opening stream socket pair");
switch (fork())
Expand All @@ -181,8 +180,7 @@ keychain_app::byte_seq_t pass_entry_term::fork_gui(const KeySym * map, const std
if (close(sockets[0]) == -1) throw std::runtime_error("close socket[0]");
}
if (setresuid(oruid, oruid, oruid) == -1) throw std::runtime_error("GUI: setresuid()");
std::string path = binary_dir+"/passentry_gui";
execlp(path.c_str(), path.c_str(), (char *) NULL);
execlp(path_, path_, (char *) NULL);
throw std::runtime_error("execlp()");
}
default: break;
Expand Down
2 changes: 1 addition & 1 deletion keychain_linux/passentry_cmd/pass_entry_term.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class pass_entry_term
pass_entry_term();
~pass_entry_term();

keychain_app::byte_seq_t fork_gui(const KeySym * map, const std::string& raw_trx, std::string path );
keychain_app::byte_seq_t fork_gui(const KeySym * map, const std::string& raw_trx );
Display* _display = NULL;
private:
std::wstring input_password(const KeySym *, int);
Expand Down
8 changes: 4 additions & 4 deletions keychain_linux/passentry_cmd/sec_mod_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ void sec_mod_linux::print_mnemonic(const string_list& mnemonic) const
{
}

byte_seq_t sec_mod_linux::get_passwd_trx_raw(const std::string& raw_trx, std::string binary_dir) const
byte_seq_t sec_mod_linux::get_passwd_trx_raw(const std::string& raw_trx) const
{
auto pass_entry = pass_entry_term();
auto map_instance = map_translate_singletone::instance(pass_entry._display);
auto pass = pass_entry.fork_gui(map_instance.map, raw_trx, binary_dir);
auto pass = pass_entry.fork_gui(map_instance.map, raw_trx);
return pass;
}

byte_seq_t sec_mod_linux::get_passwd_on_create(std::string binary_dir) const
byte_seq_t sec_mod_linux::get_passwd_on_create() const
{
auto pass_entry = pass_entry_term();
auto map_instance = map_translate_singletone::instance(pass_entry._display);
auto pass = pass_entry.fork_gui(map_instance.map, "", binary_dir);
auto pass = pass_entry.fork_gui(map_instance.map, "");
return pass;
}
4 changes: 2 additions & 2 deletions keychain_linux/passentry_cmd/sec_mod_linux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace keychain_app
public:
sec_mod_linux();
virtual ~sec_mod_linux();
virtual keychain_app::byte_seq_t get_passwd_trx_raw(const std::string& raw_trx, std::string binary_dir) const override;
virtual keychain_app::byte_seq_t get_passwd_on_create(std::string binary_dir) const override;
virtual keychain_app::byte_seq_t get_passwd_trx_raw(const std::string& raw_trx) const override;
virtual keychain_app::byte_seq_t get_passwd_on_create() const override;
virtual void print_mnemonic(const string_list& mnemonic) const override;
private:
static constexpr const char* pass_str = "blank_password";
Expand Down
4 changes: 2 additions & 2 deletions keychain_mac/sec_mod_mac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace keychain_app
public:
sec_mod_mac();
virtual ~sec_mod_mac();
virtual keychain_app::byte_seq_t get_passwd_trx_raw(const std::string& raw_trx, std::string binary_dir) const override;
virtual keychain_app::byte_seq_t get_passwd_on_create(std::string binary_dir) const override;
virtual keychain_app::byte_seq_t get_passwd_trx_raw(const std::string& raw_trx) const override;
virtual keychain_app::byte_seq_t get_passwd_on_create() const override;
virtual void print_mnemonic(const string_list& mnemonic) const override;
private:
static constexpr const char* pass_str = "blank_password";
Expand Down

0 comments on commit 1ee8ff8

Please sign in to comment.