This is a fundamental part of our application suite, so system seems like a sensible home. Also moved a few logging deps (to lib_system): * bts_chrono.h * bts_no_copy.h * defer.h * dispatcher.h/cpp * file_utils.h/cpp * type_name.h
232 lines
7.4 KiB
C++
232 lines
7.4 KiB
C++
#include "KeyCmd.h"
|
|
|
|
#include "spark/config/config.h"
|
|
#include "spark/config/protos/keycode.pb.h"
|
|
#include "spark/utils/AuthToken.h"
|
|
#include "spark/utils/FileUtils.h"
|
|
#include "spark/utils/bts_assert.h"
|
|
#include "spark/utils/crypto/CryptoInit.h"
|
|
#include "spark/utils/crypto/KeyCodeHelper.h"
|
|
#include "spark/utils/crypto/PrivateKey.h"
|
|
#include "spark/utils/crypto/PublicKey.h"
|
|
#include "spark/utils/datetime.h"
|
|
#include "spark/utils/helper_macros.h"
|
|
#include "system/logging/logging.h"
|
|
#include "system/os.h"
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
#include <boost/date_time/gregorian/gregorian.hpp>
|
|
#include <boost/date_time/local_time/local_time.hpp>
|
|
#include <boost/date_time/posix_time/ptime.hpp>
|
|
#include <boost/tokenizer.hpp>
|
|
#include <cstdlib>
|
|
#include <ctime>
|
|
#include <fstream>
|
|
#include <google/protobuf/io/zero_copy_stream_impl.h>
|
|
#include <google/protobuf/text_format.h>
|
|
#include <google/protobuf/timestamp.pb.h>
|
|
#include <sstream>
|
|
#include <stdio.h>
|
|
#include <streambuf>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <arpa/inet.h>
|
|
#include <ifaddrs.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/types.h>
|
|
|
|
namespace bts::cmds {
|
|
|
|
using namespace std;
|
|
using namespace datetime;
|
|
using namespace bts::api::auth;
|
|
using namespace bts::crypto;
|
|
using namespace bts::config;
|
|
|
|
struct EncryptArgs {
|
|
std::string keyfile;
|
|
std::string pass;
|
|
std::string users;
|
|
std::string expiration;
|
|
std::string ip;
|
|
uint16_t port;
|
|
bool allow_concurrent;
|
|
};
|
|
|
|
struct DecryptArgs {
|
|
std::string keyfile;
|
|
std::string source;
|
|
};
|
|
|
|
struct KeyGenArgs {
|
|
std::string out_path_prefix;
|
|
};
|
|
|
|
static void erase_all_substr(std::string &str, const std::string &substr) {
|
|
size_t pos = std::string::npos;
|
|
|
|
// Search for the substring in string in a loop untill nothing is found
|
|
while ((pos = str.find(substr)) != std::string::npos) {
|
|
// If found then erase it from string
|
|
str.erase(pos, substr.length());
|
|
}
|
|
}
|
|
|
|
static void write_keycode(
|
|
const std::string &users,
|
|
const std::string &expiration,
|
|
const std::string &ip,
|
|
uint16_t port,
|
|
bool concurrent,
|
|
bts::config::KeyCode &kc) {
|
|
std::ostringstream out_stream;
|
|
|
|
auto nusers = (uint32_t)std::atoi(users.c_str());
|
|
auto expir = bts::datetime::parse_iso_date_time_string(expiration);
|
|
auto expir_ll = bts::datetime::ptime_to_ll(expir);
|
|
auto now = bts::datetime::ll_now();
|
|
|
|
kc.set_nusers(nusers);
|
|
kc.set_expiration(expir_ll);
|
|
kc.set_generation_time(now);
|
|
bts_assert_msg(
|
|
ip != "127.0.0.1", "you can't generate a keycode for loopback, just "
|
|
"leave the parameter blank");
|
|
kc.set_ip(ip);
|
|
kc.set_port(std::to_string(port));
|
|
kc.set_allow_concurrent(concurrent);
|
|
}
|
|
|
|
static void check_ips() {
|
|
CryptoInit::initialize();
|
|
auto ipmap = bts::os::host_ips();
|
|
for (const auto &x : ipmap)
|
|
std::cout << x.first << " => " << x.second << std::endl;
|
|
}
|
|
|
|
static void encrypt(const EncryptArgs &args) {
|
|
CryptoInit::initialize();
|
|
std::string protobuf;
|
|
std::string kc_string;
|
|
std::stringstream oss, encrypted;
|
|
CachedPassword cache(args.pass);
|
|
PrivateKeyPtr key = PrivateKey::read_file(args.keyfile, &cache);
|
|
|
|
bts::config::KeyCode kc;
|
|
write_keycode(
|
|
args.users, args.expiration, args.ip, args.port, args.allow_concurrent,
|
|
kc);
|
|
if (!google::protobuf::TextFormat::PrintToString(kc, &kc_string)) {
|
|
return;
|
|
}
|
|
|
|
oss << kc_string;
|
|
key->encrypt(oss, encrypted);
|
|
auto kcstr = encrypted.str();
|
|
erase_all_substr(kcstr, "-----BEGIN BTS-----\n");
|
|
erase_all_substr(kcstr, "\n-----END BTS-----\n");
|
|
kcstr.erase(std::remove(kcstr.begin(), kcstr.end(), '\n'), kcstr.end());
|
|
|
|
EncryptedKeyCode ekc;
|
|
ekc.set_keycode(kcstr);
|
|
std::string pb_str;
|
|
google::protobuf::TextFormat::PrintToString(ekc, &pb_str);
|
|
|
|
std::cout << std::endl << std::endl;
|
|
std::cout << "#nusers: " << args.users << std::endl;
|
|
std::cout << "#allow concurrent : "
|
|
<< (kc.allow_concurrent() ? "true" : "false") << std::endl;
|
|
std::cout << "#expires: "
|
|
<< datetime::simple_date_time_string(
|
|
datetime::ll_to_ptime(kc.expiration()))
|
|
<< " " << std::to_string(kc.expiration()) << std::endl;
|
|
std::cout << "#generation_time: "
|
|
<< datetime::simple_date_time_string(
|
|
datetime::ll_to_ptime(kc.generation_time()))
|
|
<< " " << std::to_string(kc.generation_time()) << std::endl;
|
|
std::cout << "EncryptedKeyCode {" << std::endl;
|
|
std::cout << " value {" << std::endl;
|
|
std::cout << " " << pb_str;
|
|
std::cout << " }" << std::endl;
|
|
std::cout << "}" << std::endl;
|
|
std::cout << std::endl << std::endl;
|
|
}
|
|
|
|
static void decrypt(const DecryptArgs &args) {
|
|
CryptoInit::initialize();
|
|
auto pub = bts::crypto::PublicKey::read_file(args.keyfile);
|
|
if (!pub) {
|
|
return;
|
|
}
|
|
std::ifstream fs(args.source);
|
|
if (!fs) {
|
|
LOG(Gen, Error, "Failed to open config file '{}'", args.source);
|
|
return;
|
|
}
|
|
|
|
auto cfg_str = spartan::FileUtils::to_string(fs);
|
|
config::internal::initialize_for_test(cfg_str);
|
|
auto ekc = bts::config::get<bts::config::EncryptedKeyCode>();
|
|
bts::config::KeyCode kc;
|
|
bts::crypto::decrypt_keycode(*pub, ekc, &kc);
|
|
|
|
std::cout << "\n\n";
|
|
std::string kc_str;
|
|
google::protobuf::TextFormat::PrintToString(kc, &kc_str);
|
|
std::cout << kc_str << std::endl;
|
|
std::cout << "\n\n";
|
|
}
|
|
|
|
static void key_gen(const KeyGenArgs &args) {
|
|
CryptoInit::initialize();
|
|
PrivateKeyPtr pri_key = PrivateKey::create(2048, AlgoType::RSA);
|
|
|
|
std::string pri_key_file = (args.out_path_prefix + "_private.pem");
|
|
std::string pub_key_file = (args.out_path_prefix + "_public.pem");
|
|
// I can't get anything but DES to work here.
|
|
pri_key->write_file(pri_key_file, CipherType::DES);
|
|
pri_key->public_key()->write_file(pub_key_file);
|
|
|
|
std::cout << "Key pair generated " << pri_key_file << " and " << pub_key_file
|
|
<< std::endl;
|
|
}
|
|
|
|
struct KeyCmd::Args {
|
|
EncryptArgs encrypt;
|
|
DecryptArgs decrypt;
|
|
KeyGenArgs key_gen;
|
|
};
|
|
|
|
KeyCmd::KeyCmd()
|
|
: CLI::App("Manage keycodes", "key"), args_(new KeyCmd::Args()) {
|
|
// check ips
|
|
add_subcommand("check-ips")->callback(check_ips);
|
|
|
|
// decrypt
|
|
auto *decrypt_cmd = add_subcommand("decrypt");
|
|
decrypt_cmd->add_option("--key-file", args_->decrypt.keyfile)->required();
|
|
decrypt_cmd->add_option("--source", args_->decrypt.source);
|
|
decrypt_cmd->callback([&] { decrypt(args_->decrypt); });
|
|
|
|
// encrypt
|
|
auto *encrypt_cmd = add_subcommand("encrypt");
|
|
encrypt_cmd->add_flag("--allow-concurrent", args_->encrypt.allow_concurrent);
|
|
encrypt_cmd->add_option("--expiration", args_->encrypt.expiration);
|
|
encrypt_cmd->add_option("--ip", args_->encrypt.ip);
|
|
encrypt_cmd->add_option("--key-file", args_->encrypt.keyfile)->required();
|
|
encrypt_cmd->add_option("--pass", args_->encrypt.pass);
|
|
encrypt_cmd->add_option("--port", args_->encrypt.port);
|
|
encrypt_cmd->add_option("--users", args_->encrypt.users);
|
|
encrypt_cmd->callback([&] { encrypt(args_->encrypt); });
|
|
|
|
// gen
|
|
auto *gen_cmd = add_subcommand("gen");
|
|
gen_cmd->add_option("--out", args_->key_gen.out_path_prefix)->required();
|
|
gen_cmd->callback([&] { key_gen(args_->key_gen); });
|
|
}
|
|
|
|
KeyCmd::~KeyCmd() {}
|
|
|
|
} // namespace bts::cmds
|