keycodes. boost stil can't handle dates

This commit is contained in:
Kevin Darby committed 2019-05-30 18:24:24 -04:00
1 parent 7d75d87e6b
commit 8a965d41ff
24 files changed
+747 -132

No files matched your search

+261
View File
@@ -0,0 +1,261 @@
#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/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 <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 <ctime>
#include <cstdlib>
#include <vector>
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:<1/23/2050>\n "
<< "DECODE -keyfile:[keyfile] -pass:password -source:[file] \n "
<< "KEYGEN -keyfile:[keyfile_prefix]\n ";
return oss.str();
}
std::string parse_date (std::string const &date)
{
auto the_date = bts::datetime::parse_simple_date_time_string(date);
return bts::datetime::simple_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)
{
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);
}
}
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, bts::config::KeyCode& kc)
{
std::ostringstream out_stream;
auto nusers = (uint32_t) std::atoi(users.c_str());
auto expir = bts::datetime::parse_simple_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);
}
int encrypt(const std::string& keyfile, const std::string& pass, const std::string& users, const std::string& expiration)
{
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, 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;
ekc.set_keycode(encrypted.str());
std::string pb;
google::protobuf::TextFormat::PrintToString(ekc, &pb);
std::cout << "#nusers: " << users << 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 << pb;
return 0;
}
}
int encode(const std::vector<std::string>& args)
{
std::string keyfile, pass, users, expiration;
get_cmd(args, keyfile, pass, users, expiration);
return encrypt(keyfile, pass, users, expiration);
}
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::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);
}
}
std::cout << help() << std::endl;
return -1;
}
std::string KeyCode::help ()
{
return helpstr();
}
}}