refactor: Clean up KeyCode

This commit is contained in:
John Colvin committed 2022-05-02 12:29:01 -04:00
1 parent 4eea3194fd
commit 8a96c7441e
5 files changed
+119 -105

No files matched your search

+63 -61
View File
@@ -5,6 +5,7 @@
#include "lib/logging/logging.h"
#include "lib/os/os.h"
#include "lib/utils/AuthToken.h"
#include "lib/utils/FileUtils.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/crypto/CryptoInit.h"
#include "lib/utils/crypto/KeyCodeHelper.h"
@@ -58,26 +59,21 @@ struct DecryptArgs {
std::string source;
};
struct KeygenArgs {
std::string keyfile;
struct KeyGenArgs {
std::string out_path_prefix;
};
std::string parse_date(std::string const &date) {
auto the_date = bts::datetime::parse_iso_date_time_string(date);
return bts::datetime::iso_date_time_string(the_date);
}
void eraseAllSubStr(std::string &mainStr, const std::string &toErase) {
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 = mainStr.find(toErase)) != std::string::npos) {
while ((pos = str.find(substr)) != std::string::npos) {
// If found then erase it from string
mainStr.erase(pos, toErase.length());
str.erase(pos, substr.length());
}
}
void write_keycode(
static void write_keycode(
const std::string &users,
const std::string &expiration,
const std::string &ip,
@@ -102,14 +98,14 @@ void write_keycode(
kc.set_allow_concurrent(concurrent);
}
void check_ips() {
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;
}
void encrypt(const EncryptArgs &args) {
static void encrypt(const EncryptArgs &args) {
CryptoInit::initialize();
std::string protobuf;
std::string kc_string;
@@ -121,18 +117,22 @@ void encrypt(const EncryptArgs &args) {
write_keycode(
args.users, args.expiration, args.ip, args.port, args.allow_concurrent,
kc);
if (!google::protobuf::TextFormat::PrintToString(kc, &kc_string))
if (!google::protobuf::TextFormat::PrintToString(kc, &kc_string)) {
return;
}
oss << kc_string;
key->encrypt(oss, encrypted);
EncryptedKeyCode ekc;
auto kcstr = encrypted.str();
eraseAllSubStr(kcstr, "-----BEGIN BTS-----\n");
eraseAllSubStr(kcstr, "\n-----END BTS-----\n");
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;
google::protobuf::TextFormat::PrintToString(ekc, &pb);
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 : "
@@ -146,70 +146,71 @@ void encrypt(const EncryptArgs &args) {
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;
std::cout << " }" << 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;
}
void decrypt(const DecryptArgs &args) {
static void decrypt(const DecryptArgs &args) {
CryptoInit::initialize();
if (args.keyfile != "") {
PublicKeyPtr pub = bts::crypto::PublicKey::read_file(args.keyfile);
if (pub && args.source != "") {
bts::config::EncryptedKeyCode ekc;
std::fstream fs(args.source);
std::string str(
(std::istreambuf_iterator<char>(fs)),
std::istreambuf_iterator<char>());
config::internal::initialize_for_test(str);
ekc = config::get<bts::config::EncryptedKeyCode>();
std::cout << std::endl << std::endl;
bts::config::KeyCode kc;
std::string string_out;
bts::crypto::decrypt_keycode(*pub, ekc, &kc);
google::protobuf::TextFormat::PrintToString(kc, &string_out);
std::cout << string_out << std::endl;
}
std::cout << "\n\n";
} else {
std::cout << "\nFailed\n";
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";
}
void keygen(const KeygenArgs &args) {
static void key_gen(const KeyGenArgs &args) {
CryptoInit::initialize();
PrivateKeyPtr priv = PrivateKey::create(2048, AlgoType::RSA);
PrivateKeyPtr pri_key = PrivateKey::create(2048, AlgoType::RSA);
std::string n1 = (args.keyfile + "_private.pem"),
n2 = (args.keyfile + "_public.pem");
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.
priv->write_file(n1, CipherType::DES);
priv->public_key()->write_file(n2);
pri_key->write_file(pri_key_file, CipherType::DES);
pri_key->public_key()->write_file(pub_key_file);
std::cout << "Key pair generated " << n1 << " and " << n2 << std::endl;
std::cout << "Key pair generated " << pri_key_file << " and " << pub_key_file
<< std::endl;
}
struct KeyCode::Args {
EncryptArgs encrypt;
DecryptArgs decrypt;
KeygenArgs keygen;
KeyGenArgs key_gen;
};
KeyCode::KeyCode() : CLI::App("key-code"), _args(new KeyCode::Args()) {
require_subcommand();
auto *cmd = this;
KeyCode::KeyCode()
: CLI::App("Manage keycodes", "key-code"), _args(new KeyCode::Args()) {
// check ips
cmd->add_subcommand("check-ips")->callback(check_ips);
add_subcommand("check-ips")->callback(check_ips);
// decrypt
auto *decrypt_cmd = cmd->add_subcommand("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 = cmd->add_subcommand("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);
@@ -218,10 +219,11 @@ KeyCode::KeyCode() : CLI::App("key-code"), _args(new KeyCode::Args()) {
encrypt_cmd->add_option("--port", _args->encrypt.port);
encrypt_cmd->add_option("--users", _args->encrypt.users);
encrypt_cmd->callback([&] { encrypt(_args->encrypt); });
// key-gen
auto *keygen_cmd = cmd->add_subcommand("key-gen");
keygen_cmd->add_option("--key-file", _args->keygen.keyfile)->required();
keygen_cmd->callback([&] { keygen(_args->keygen); });
auto *keygen_cmd = add_subcommand("key-gen");
keygen_cmd->add_option("--out", _args->key_gen.out_path_prefix)->required();
keygen_cmd->callback([&] { key_gen(_args->key_gen); });
}
KeyCode::~KeyCode() {}