refactor: Clean up KeyCode
This commit is contained in:
1 parent
4eea3194fd
commit
8a96c7441e
5 files changed
+119
-105
No files matched your search
@@ -1,4 +1,5 @@
|
||||
#include "FileUtils.h"
|
||||
#include <sstream>
|
||||
|
||||
namespace bts::spartan {
|
||||
std::string FileUtils::file_name_from_path(const std::string &path) {
|
||||
@@ -58,4 +59,10 @@ std::string FileUtils::change_file_ext(
|
||||
return change_file_ext(path, new_ext);
|
||||
}
|
||||
|
||||
std::string FileUtils::to_string(std::istream &istr) {
|
||||
std::stringstream buff;
|
||||
buff << istr.rdbuf();
|
||||
return buff.str();
|
||||
}
|
||||
|
||||
} // namespace bts::spartan
|
||||
@@ -64,6 +64,8 @@ class FileUtils {
|
||||
static bool read_from_file(
|
||||
const std::string &path, TOper op, ushort version);
|
||||
|
||||
static std::string to_string(std::istream &istr);
|
||||
|
||||
template <class Iterator, class TOper>
|
||||
static void write_to_file(
|
||||
Iterator first, Iterator last, const std::string &path, TOper op);
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#include "lib/utils/crypto/PrivateKey.h"
|
||||
#ifndef _WIN32
|
||||
#include "PublicKey.h"
|
||||
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/FileUtils.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/crypto/iostreambio.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
@@ -15,56 +16,63 @@ namespace openssl {
|
||||
#include <openssl/rsa.h>
|
||||
} // namespace openssl
|
||||
|
||||
namespace bts {
|
||||
namespace crypto {
|
||||
namespace bts::crypto {
|
||||
|
||||
PublicKey::PublicKey(openssl::EVP_PKEY *key) : PrivateKey(key) {}
|
||||
|
||||
PublicKey::PublicKey(const PrivateKey &key) : PrivateKey() {
|
||||
// I don't know how to do this another way
|
||||
openssl::BIO *bio = openssl::BIO_new(openssl::BIO_s_mem());
|
||||
int i = openssl::PEM_write_bio_PUBKEY(bio, key._key);
|
||||
int err = BIO_flush(bio);
|
||||
UNUSED(i);
|
||||
UNUSED(err);
|
||||
auto *bio = openssl::BIO_new(openssl::BIO_s_mem());
|
||||
bts_assert_msg(bio, "Failed to create new BIO");
|
||||
|
||||
int res = openssl::PEM_write_bio_PUBKEY(bio, key._key);
|
||||
bts_assert_msgf(
|
||||
res == 1, "Failed to read public key (PEM_write_bio_PUBKEY returned {})",
|
||||
res);
|
||||
|
||||
res = BIO_flush(bio);
|
||||
bts_assert_msgf(
|
||||
res == 1, "Failed to flush BIO buffer (BIO_flush returned {})", res);
|
||||
|
||||
auto *k = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0);
|
||||
bts_assert_msg(k, "Failed to read public key");
|
||||
|
||||
openssl::EVP_PKEY *k = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0);
|
||||
BIO_free(bio);
|
||||
bts_assert(k);
|
||||
this->_key = k;
|
||||
}
|
||||
|
||||
PublicKey::~PublicKey() {}
|
||||
PublicKey::~PublicKey() = default;
|
||||
|
||||
const PublicKeyPtr PublicKey::create(const PrivateKey &private_key) {
|
||||
PublicKeyPtr ret(new PublicKey(private_key));
|
||||
return ret;
|
||||
}
|
||||
|
||||
const PublicKeyPtr PublicKey::read_file(const std::string &filename) {
|
||||
PublicKeyPtr ret;
|
||||
openssl::BIO *bio = openssl::BIO_new_file(filename.c_str(), "r");
|
||||
bts_assert_msg(bio, "can't load keyfile");
|
||||
openssl::EVP_PKEY *key = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0);
|
||||
if (!key)
|
||||
LOG(Gen, Error, "Invalid public key read");
|
||||
else
|
||||
ret.reset(new PublicKey(key));
|
||||
return ret;
|
||||
const PublicKeyPtr PublicKey::read_file(const std::string &path) {
|
||||
auto *bio = openssl::BIO_new_file(path.c_str(), "r");
|
||||
if (!bio) {
|
||||
LOG(Gen, Error, "Failed to load public key file '{}'", path);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto *key = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0);
|
||||
if (!key) {
|
||||
LOG(Gen, Error, "Failed to read public key file '{}'", path);
|
||||
return nullptr;
|
||||
}
|
||||
return PublicKeyPtr(new PublicKey(key));
|
||||
}
|
||||
|
||||
const PublicKeyPtr PublicKey::read_string(const std::string &public_key) {
|
||||
PublicKeyPtr ret;
|
||||
std::stringstream oss;
|
||||
oss << public_key;
|
||||
bts::crypto::iostreambio biostream(&oss);
|
||||
openssl::EVP_PKEY *key =
|
||||
openssl::PEM_read_bio_PUBKEY(biostream.getBIO(), 0, 0, 0);
|
||||
if (!key)
|
||||
auto *key = openssl::PEM_read_bio_PUBKEY(biostream.getBIO(), 0, 0, 0);
|
||||
if (!key) {
|
||||
LOG(Gen, Error, "Invalid public key read");
|
||||
else
|
||||
ret.reset(new PublicKey(key));
|
||||
return ret;
|
||||
return nullptr;
|
||||
}
|
||||
return PublicKeyPtr(new PublicKey(key));
|
||||
}
|
||||
|
||||
const PublicKeyPtr PublicKey::read_stream(std::istream &stream) {
|
||||
@@ -107,7 +115,6 @@ bool PublicKey::decrypt(
|
||||
return public_decrypt(in_data, out_data);
|
||||
}
|
||||
|
||||
} // namespace crypto
|
||||
} // namespace bts
|
||||
} // namespace bts::crypto
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -1,23 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "PrivateKey.h"
|
||||
#include "lib/utils/bts_no_copy.h"
|
||||
|
||||
#include "PrivateKey.h"
|
||||
|
||||
namespace bts {
|
||||
namespace crypto {
|
||||
namespace bts::crypto {
|
||||
|
||||
class PublicKey : public PrivateKey {
|
||||
BTS_NO_COPY(PublicKey);
|
||||
|
||||
public:
|
||||
PublicKey(const PrivateKey &);
|
||||
PublicKey(const PublicKey &) = delete;
|
||||
PublicKey &operator=(const PublicKey &) = delete;
|
||||
virtual ~PublicKey();
|
||||
|
||||
virtual bool
|
||||
encrypt(const std::iostream &in_dat, std::iostream &out_data) const;
|
||||
virtual bool
|
||||
decrypt(const std::iostream &in_dat, std::iostream &out_data) const;
|
||||
virtual bool encrypt(
|
||||
const std::iostream &in_dat, std::iostream &out_data) const;
|
||||
virtual bool decrypt(
|
||||
const std::iostream &in_dat, std::iostream &out_data) const;
|
||||
|
||||
static const PublicKeyPtr create(const PrivateKey &private_key);
|
||||
static const PublicKeyPtr read_file(const std::string &filename);
|
||||
@@ -31,5 +28,4 @@ class PublicKey : public PrivateKey {
|
||||
PublicKey(openssl::EVP_PKEY *);
|
||||
};
|
||||
|
||||
} // namespace crypto
|
||||
} // namespace bts
|
||||
} // namespace bts::crypto
|
||||
@@ -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() {}
|
||||
|
||||
Reference in new issue
Block a user