327 lines
11 KiB
C++
327 lines
11 KiB
C++
#include "KeyCode.h"
|
|
|
|
#include "lib/utils/AuthToken.h"
|
|
#include "lib/utils/datetime.h"
|
|
#include "lib/utils/crypto/CryptoInit.h"
|
|
#include "lib/utils/crypto/PrivateKey.h"
|
|
#include "lib/utils/crypto/PublicKey.h"
|
|
#include "lib/utils/crypto/KeyCodeHelper.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/os/os.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/utils/helper_macros.h"
|
|
#include "lib/config/config.h"
|
|
#include "lib/config/protos/keycode.pb.h"
|
|
|
|
#include <boost/date_time/posix_time/ptime.hpp>
|
|
#include <boost/date_time/local_time/local_time.hpp>
|
|
#include <boost/tokenizer.hpp>
|
|
#include <boost/date_time/gregorian/gregorian.hpp>
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
#include <google/protobuf/text_format.h>
|
|
#include <google/protobuf/io/zero_copy_stream_impl.h>
|
|
#include <google/protobuf/timestamp.pb.h>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <stdio.h>
|
|
#include <sstream>
|
|
#include <streambuf>
|
|
#include <ctime>
|
|
#include <cstdlib>
|
|
#include <vector>
|
|
|
|
#include <sys/types.h>
|
|
#include <ifaddrs.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
|
|
namespace bts {
|
|
namespace cmd {
|
|
|
|
using namespace std;
|
|
using namespace datetime;
|
|
using namespace bts::api::auth;
|
|
using namespace bts::crypto;
|
|
using namespace bts::config;
|
|
|
|
std::string KeyCode::helpstr ()
|
|
{
|
|
std::ostringstream oss;
|
|
oss << "\n"
|
|
<< "ENCODE -keyfile:[keyfile] -pass:password -users:<nusers> -expiration:<2020-05-18> [-ip:<ip_address>] [-port:<spark_port>] [-allow_concurrent]\n "
|
|
<< "DECODE -keyfile:[keyfile] -pass:password -source:[file] \n "
|
|
<< "KEYGEN -keyfile:[keyfile_prefix]\n "
|
|
<< "CHECKIPS \n ";
|
|
|
|
return oss.str();
|
|
}
|
|
|
|
int checkips()
|
|
{
|
|
auto ipmap = bts::os::host_ips();
|
|
for(const auto& x: ipmap)
|
|
std::cout << x.first << " => " << x.second << std::endl;
|
|
return 0;
|
|
}
|
|
|
|
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 get_cmd(const std::vector<std::string>& args, std::string& keyfile, std::string& pass,
|
|
std::string& users, std::string& expiration, std::string* source = nullptr,
|
|
std::string* ip = nullptr, std::string* port = nullptr, bool* allow_concurrent = nullptr)
|
|
{
|
|
for(size_t i = 1; i < args.size(); i++)
|
|
{
|
|
size_t pass_pos = args[i].find("-pass:");
|
|
if(pass_pos != args[i].npos)
|
|
pass = args[i].substr(pass_pos+6);
|
|
|
|
size_t file_pos = args[i].find("-keyfile:");
|
|
if(file_pos != args[i].npos)
|
|
keyfile = args[i].substr(file_pos+9);
|
|
|
|
size_t u_pos = args[i].find("-users:");
|
|
if(u_pos != args[i].npos)
|
|
users = args[i].substr(u_pos+7);
|
|
|
|
size_t e_pos = args[i].find("-expiration:");
|
|
if(e_pos != args[i].npos)
|
|
expiration = parse_date (args[i].substr(e_pos+12));
|
|
|
|
size_t pay_pos = args[i].find("-source:");
|
|
if(source && pay_pos != args[i].npos)
|
|
*source = args[i].substr(pay_pos+8);
|
|
|
|
size_t i_pos = args[i].find("-ip:");
|
|
if(ip && i_pos != args[i].npos)
|
|
*ip = args[i].substr(i_pos+4);
|
|
|
|
size_t p_pos = args[i].find("-port:");
|
|
if(port && p_pos != args[i].npos)
|
|
*port = args[i].substr(p_pos+6);
|
|
|
|
size_t c_pos = args[i].find("-allow_concurrent");
|
|
if(allow_concurrent && c_pos != args[i].npos)
|
|
*allow_concurrent = true;
|
|
}
|
|
}
|
|
|
|
int keygen(const std::vector<std::string>& args)
|
|
{
|
|
std::string keyfile, pass, users, expiration;
|
|
get_cmd(args, keyfile, pass, users, expiration);
|
|
if(keyfile != "")
|
|
{
|
|
PrivateKeyPtr priv = PrivateKey::create(2048, AlgoType::RSA);
|
|
|
|
std::string n1 = (keyfile + "_private.pem"), n2 = (keyfile + "_public.pem");
|
|
//I can't get anything but DES to work here.
|
|
priv->write_file(n1, CipherType::DES);
|
|
priv->public_key()->write_file(n2);
|
|
|
|
std::cout << "Key pair generated " << n1 << " and " << n2 << std::endl;
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
std::cout << KeyCode::helpstr();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
// int Logon::digest(const std::vector<std::string>& args)
|
|
// {
|
|
// std::string pass, keyfile, payload, user, expiration, extra_data;
|
|
// get_cmd(args, pass, keyfile, payload, user, expiration, extra_data);
|
|
// if(extra_data != "")
|
|
// {
|
|
// unsigned int len;
|
|
// //unsigned char* digest;
|
|
// //Digest::allocate_array(&digest, &len);
|
|
// //Digest::compute_digest(reinterpret_cast<const unsigned char*>(payload.c_str()), payload.size(), digest, &len);
|
|
|
|
// std::string digest = Digest::compute_digest(extra_data);
|
|
// len = digest.size();
|
|
// UNUSED(len);
|
|
// std::cout << std::endl << std::endl;
|
|
// //for(size_t i = 0; i < len; i++)
|
|
// // printf("%02x", digest[i]);
|
|
// std::cout << digest;
|
|
// std::cout << std::endl << std::endl;
|
|
// return 0;
|
|
// }
|
|
// else
|
|
// {
|
|
// std::cout << help();
|
|
// return -1;
|
|
// }
|
|
// }
|
|
|
|
void write_keycode(const std::string& users, const std::string& expiration,
|
|
const std::string& ip, const std::string& 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(port);
|
|
kc.set_allow_concurrent(concurrent);
|
|
}
|
|
|
|
void eraseAllSubStr(std::string& mainStr, const std::string& toErase)
|
|
{
|
|
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)
|
|
{
|
|
// If found then erase it from string
|
|
mainStr.erase(pos, toErase.length());
|
|
}
|
|
}
|
|
|
|
int encrypt(const std::string& keyfile, const std::string& pass, const std::string& users,
|
|
const std::string& expiration, const std::string& ip, const std::string& port, bool concurrent)
|
|
{
|
|
std::string protobuf;
|
|
|
|
if(keyfile == "")
|
|
{
|
|
std::cout << KeyCode::helpstr();
|
|
return -1;
|
|
}
|
|
else
|
|
{
|
|
std::string kc_string;
|
|
std::stringstream oss, encrypted;
|
|
CachedPassword cache(pass);
|
|
PrivateKeyPtr key = PrivateKey::read_file(keyfile, &cache);
|
|
|
|
bts::config::KeyCode kc;
|
|
write_keycode(users, expiration, ip, port, concurrent, kc);
|
|
if(google::protobuf::TextFormat::PrintToString(kc, &kc_string))
|
|
{
|
|
oss << kc_string;
|
|
key->encrypt(oss, encrypted);
|
|
}
|
|
else
|
|
return -1;
|
|
//int ret = openssl::RSA_private_encrypt(sz-1, pay, &buffer[0], key->pkey.rsa, RSA_PKCS1_PADDING);
|
|
|
|
EncryptedKeyCode ekc;
|
|
auto kcstr = encrypted.str();
|
|
eraseAllSubStr(kcstr,"-----BEGIN BTS-----\n");
|
|
eraseAllSubStr(kcstr, "\n-----END BTS-----\n");
|
|
kcstr.erase( std::remove(kcstr.begin(), kcstr.end(), '\n'), kcstr.end() );
|
|
//kcstr = boost::replace_all_copy<string>(kcstr, "\n", "\\n");
|
|
ekc.set_keycode(kcstr);
|
|
std::string pb;
|
|
google::protobuf::TextFormat::PrintToString(ekc, &pb);
|
|
std::cout << std::endl << std::endl;
|
|
std::cout << "#nusers: " << 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;
|
|
std::cout << " }" << std::endl;
|
|
std::cout << "}" << std::endl;
|
|
std::cout << std::endl << std::endl;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
int encode(const std::vector<std::string>& args)
|
|
{
|
|
std::string keyfile, pass, users, expiration, ip, port; bool concurrent = false;
|
|
get_cmd(args, keyfile, pass, users, expiration, nullptr, &ip, &port, &concurrent);
|
|
|
|
return encrypt(keyfile, pass, users, expiration, ip, port, concurrent);
|
|
}
|
|
|
|
int decode(const std::vector<std::string>& args)
|
|
{
|
|
std::string keyfile, pass, users, expiration, source;
|
|
get_cmd(args, keyfile, pass, users, expiration, &source);
|
|
|
|
if(keyfile != "")
|
|
{
|
|
PublicKeyPtr pub = bts::crypto::PublicKey::read_file(keyfile);
|
|
if(pub && source != "")
|
|
{
|
|
//KeycodePayloadHelper helper;
|
|
bts::config::EncryptedKeyCode ekc;
|
|
std::fstream fs(source);
|
|
//google::protobuf::io::IstreamInputStream pay_stream(&fs);
|
|
//google::protobuf::TextFormat::Parse(&pay_stream, &ekc);
|
|
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;
|
|
}
|
|
|
|
//int ret = openssl::RSA_public_decrypt(sz, data, &buffer[0], key->pkey.rsa, RSA_PKCS1_PADDING);
|
|
std::cout << std::endl << std::endl;
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
std::cout << KeyCode::helpstr();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
int KeyCode::execute(const std::vector<std::string>& args)
|
|
{
|
|
CryptoInit::initialize();
|
|
if(args.size() > 0)
|
|
{
|
|
if(boost::to_lower_copy(args[0]) == "keycode" || boost::to_lower_copy(args[0]) == "encode")
|
|
{
|
|
return encode(args);
|
|
}
|
|
if(boost::to_lower_copy(args[0]) == "keygen")
|
|
{
|
|
return keygen(args);
|
|
}
|
|
if(boost::to_lower_copy(args[0]) == "decode")
|
|
{
|
|
return decode(args);
|
|
}
|
|
if(boost::to_lower_copy(args[0]) == "checkips")
|
|
{
|
|
return checkips();
|
|
}
|
|
}
|
|
std::cout << help() << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
std::string KeyCode::help ()
|
|
{
|
|
return helpstr();
|
|
}
|
|
|
|
|
|
|
|
}} |