From f13a3325bcd489678e432650a3f6284d089ef36b Mon Sep 17 00:00:00 2001 From: Michael Harding Date: Tue, 28 May 2019 09:44:29 -0400 Subject: [PATCH 01/11] Add Crypto --- src/lib/utils/crypto/AlgoType.h | 18 ++ src/lib/utils/crypto/CipherType.cpp | 12 + src/lib/utils/crypto/CipherType.h | 20 ++ src/lib/utils/crypto/CryptoInit.cpp | 55 +++++ src/lib/utils/crypto/CryptoInit.h | 20 ++ src/lib/utils/crypto/Digest.cpp | 66 +++++ src/lib/utils/crypto/Digest.h | 24 ++ src/lib/utils/crypto/PasswordCallback.cpp | 63 +++++ src/lib/utils/crypto/PasswordCallback.h | 45 ++++ src/lib/utils/crypto/PrivateKey.cpp | 287 ++++++++++++++++++++++ src/lib/utils/crypto/PrivateKey.h | 70 ++++++ src/lib/utils/crypto/PublicKey.cpp | 121 +++++++++ src/lib/utils/crypto/PublicKey.h | 37 +++ src/lib/utils/crypto/iostreambio.cpp | 219 +++++++++++++++++ src/lib/utils/crypto/iostreambio.h | 30 +++ 15 files changed, 1087 insertions(+) create mode 100644 src/lib/utils/crypto/AlgoType.h create mode 100644 src/lib/utils/crypto/CipherType.cpp create mode 100644 src/lib/utils/crypto/CipherType.h create mode 100644 src/lib/utils/crypto/CryptoInit.cpp create mode 100644 src/lib/utils/crypto/CryptoInit.h create mode 100644 src/lib/utils/crypto/Digest.cpp create mode 100644 src/lib/utils/crypto/Digest.h create mode 100644 src/lib/utils/crypto/PasswordCallback.cpp create mode 100644 src/lib/utils/crypto/PasswordCallback.h create mode 100644 src/lib/utils/crypto/PrivateKey.cpp create mode 100644 src/lib/utils/crypto/PrivateKey.h create mode 100644 src/lib/utils/crypto/PublicKey.cpp create mode 100644 src/lib/utils/crypto/PublicKey.h create mode 100644 src/lib/utils/crypto/iostreambio.cpp create mode 100644 src/lib/utils/crypto/iostreambio.h diff --git a/src/lib/utils/crypto/AlgoType.h b/src/lib/utils/crypto/AlgoType.h new file mode 100644 index 000000000..0f6680b11 --- /dev/null +++ b/src/lib/utils/crypto/AlgoType.h @@ -0,0 +1,18 @@ +#pragma once + +namespace bts +{ +namespace crypto +{ + +class AlgoType +{ +public: + enum type + { + RSA = 0 + }; +}; + +} +} diff --git a/src/lib/utils/crypto/CipherType.cpp b/src/lib/utils/crypto/CipherType.cpp new file mode 100644 index 000000000..e54cbe7af --- /dev/null +++ b/src/lib/utils/crypto/CipherType.cpp @@ -0,0 +1,12 @@ +#include "CipherType.h" + +namespace bts +{ +namespace crypto +{ + +const char* CipherType::name[] = { "None", "DES-EDE3-CBC", "3des", "idea", "aes128" }; + +} +} + diff --git a/src/lib/utils/crypto/CipherType.h b/src/lib/utils/crypto/CipherType.h new file mode 100644 index 000000000..6447c19b5 --- /dev/null +++ b/src/lib/utils/crypto/CipherType.h @@ -0,0 +1,20 @@ +#pragma once + +namespace bts +{ +namespace crypto +{ + +class CipherType +{ +public: + static const char *name[]; + enum type + { + None = 0, DES = 1, TripleDES = 2, IDEA = 3, AES128 = 4 + }; + +}; + +} +} diff --git a/src/lib/utils/crypto/CryptoInit.cpp b/src/lib/utils/crypto/CryptoInit.cpp new file mode 100644 index 000000000..d1fdd9882 --- /dev/null +++ b/src/lib/utils/crypto/CryptoInit.cpp @@ -0,0 +1,55 @@ +#include "CryptoInit.h" + +#include + +namespace openssl +{ +#include +#include +#include +#include +#include +} + +namespace bts +{ +namespace crypto +{ + +static const char rnd_seed[] = "string to make the random number generator think it has entropy"; +bool CryptoInit::init = false; + +tbb::spin_mutex _lock; + +void CryptoInit::initialize() +{ + if(!init) + { + tbb::spin_mutex::scoped_lock lock(_lock); + if(!init) + { + openssl::OpenSSL_add_all_algorithms(); + openssl::OpenSSL_add_all_ciphers(); + openssl::OpenSSL_add_all_digests(); + openssl::RAND_seed(rnd_seed, sizeof rnd_seed); + init = true; + } + } +} +void CryptoInit::cleanup() +{ + if(init) + { + tbb::spin_mutex::scoped_lock lock(_lock); + if(init) + { + //openssl::EVP_cleanup(); + init = false; + } + } +} + + +} +} + diff --git a/src/lib/utils/crypto/CryptoInit.h b/src/lib/utils/crypto/CryptoInit.h new file mode 100644 index 000000000..06e4c5b1a --- /dev/null +++ b/src/lib/utils/crypto/CryptoInit.h @@ -0,0 +1,20 @@ +#pragma once + +#include "lib/utils/bts_no_copy.h" + +namespace bts +{ +namespace crypto +{ + +struct CryptoInit +{ + BTS_NO_COPY(CryptoInit); + static bool init; + static void initialize(); + static void cleanup(); +}; + +} +} + diff --git a/src/lib/utils/crypto/Digest.cpp b/src/lib/utils/crypto/Digest.cpp new file mode 100644 index 000000000..147fd0c94 --- /dev/null +++ b/src/lib/utils/crypto/Digest.cpp @@ -0,0 +1,66 @@ +#include "Digest.h" + +#include "PrivateKey.h" +#include "lib/utils/bts_assert.h" + +namespace openssl +{ +#include +#include +} + +namespace bts +{ +namespace crypto +{ + +const std::string digest_algo = "sha256"; + +void Digest::allocate_array(unsigned char** out_digest, unsigned int* out_len) +{ + *out_len = EVP_MAX_MD_SIZE; + *out_digest = new unsigned char[*out_len]; + +} + +void Digest::deallocate_array(unsigned char* out_digest) +{ + delete[] out_digest; +} + +void Digest::compute_digest(const unsigned char* data, unsigned long size, unsigned char* out_digest, unsigned int* out_len) +{ + bts_assert(*out_len >= EVP_MAX_MD_SIZE); + const openssl::EVP_MD* md = openssl::EVP_get_digestbyname(digest_algo.c_str()); + bts_assert(md); + + openssl::EVP_MD_CTX* mdctx = openssl::EVP_MD_CTX_create(); + openssl::EVP_DigestInit_ex(mdctx, md, NULL); + openssl::EVP_DigestUpdate(mdctx, data, size); + openssl::EVP_DigestFinal_ex(mdctx, out_digest, out_len); + openssl::EVP_MD_CTX_destroy(mdctx); +} + +void Digest::compute_digest(const std::string& data, std::string* out_digest) +{ + unsigned int len = EVP_MAX_MD_SIZE; + unsigned char ret[EVP_MAX_MD_SIZE]; + char str[EVP_MAX_MD_SIZE+1]; + compute_digest(reinterpret_cast(data.c_str()), data.size(), &ret[0], &len); + for(size_t i = 0; i < len; i++) + sprintf(&str[i*2], "%02x", ret[i]); + std::string val(str, len*2); + *out_digest = val; +} + +std::string Digest::compute_digest(const std::string& data) +{ + std::string ret; + compute_digest(data, &ret); + return ret; +} + + +} +} + diff --git a/src/lib/utils/crypto/Digest.h b/src/lib/utils/crypto/Digest.h new file mode 100644 index 000000000..36cb5aefd --- /dev/null +++ b/src/lib/utils/crypto/Digest.h @@ -0,0 +1,24 @@ +#pragma once + +#include "lib/utils/bts_no_copy.h" + +#include + +namespace bts +{ +namespace crypto +{ + +class Digest +{ +public: + static void allocate_array(unsigned char** out_digest, unsigned int* out_len); + static void deallocate_array(unsigned char* out_digest); + + static void compute_digest(const unsigned char* data, unsigned long size, unsigned char* out_digest, unsigned int* out_len); + static void compute_digest(const std::string& data, std::string* out_digest); + static std::string compute_digest(const std::string& data); +}; + +} +} diff --git a/src/lib/utils/crypto/PasswordCallback.cpp b/src/lib/utils/crypto/PasswordCallback.cpp new file mode 100644 index 000000000..62dfacd03 --- /dev/null +++ b/src/lib/utils/crypto/PasswordCallback.cpp @@ -0,0 +1,63 @@ +#include "PasswordCallback.h" + +#include +#include + +namespace bts +{ +namespace crypto +{ + +PasswordCallback::PasswordCallback() : _callback(0) +{ +} + +PasswordCallback::PasswordCallback(password_callback cb) : _callback(cb) +{ +} + +PasswordCallback::~PasswordCallback() +{ +} + +void PasswordCallback::prompt(const std::string& user_info, std::string& password) +{ + if(user_info == "") + std::cout << "Enter Private Key Passphrase:"; + else + std::cout << "Enter Private Key Passphrase (" + user_info + "): "; + std::cin >> password; +} + +void PasswordCallback::request_password(const std::string& user_info, std::string& password) const +{ + if(_callback) + _callback(user_info, password); + else + { + prompt(user_info, password); + } +} + +CachedPassword::CachedPassword() : _pass() +{ + PasswordCallback::prompt("", _pass); +} + +CachedPassword::CachedPassword(const std::string& pass) : _pass(pass) +{ + if(pass == "") + PasswordCallback::prompt("", _pass); +} + +CachedPassword::~CachedPassword() +{ +} + +void CachedPassword::request_password(const std::string& , std::string& password) const +{ + password = _pass; +} + +} +} diff --git a/src/lib/utils/crypto/PasswordCallback.h b/src/lib/utils/crypto/PasswordCallback.h new file mode 100644 index 000000000..ff85fbb21 --- /dev/null +++ b/src/lib/utils/crypto/PasswordCallback.h @@ -0,0 +1,45 @@ +#pragma once + +#include "lib/utils/bts_no_copy.h" + +#include + +namespace bts +{ +namespace crypto +{ + +class PasswordCallback +{ + BTS_NO_COPY(PasswordCallback); +public: + typedef void password_callback (const std::string& user_info, std::string& password); +public: + PasswordCallback(); + PasswordCallback(password_callback cb); + virtual ~PasswordCallback(); + + static void prompt(const std::string& user_info, std::string& password); + virtual void request_password(const std::string& user_info, std::string& password) const; + + static void prerequest_password(const std::string& user_info, std::string& password); +protected: + password_callback* _callback; +}; + +class CachedPassword : public PasswordCallback +{ + BTS_NO_COPY(CachedPassword); +public: + CachedPassword(); + CachedPassword(const std::string&); + virtual ~CachedPassword(); + virtual void request_password(const std::string& user_info, std::string& password) const; + +protected: + std::string _pass; +}; + + +} +} diff --git a/src/lib/utils/crypto/PrivateKey.cpp b/src/lib/utils/crypto/PrivateKey.cpp new file mode 100644 index 000000000..b9566f91a --- /dev/null +++ b/src/lib/utils/crypto/PrivateKey.cpp @@ -0,0 +1,287 @@ +#include "PrivateKey.h" + +#include "PublicKey.h" +#include "CryptoInit.h" +#include "iostreambio.h" +#include "lib/logging/logging.h" +#include +#include + +namespace openssl +{ +#include +#include +#include +#include +#include +} + +namespace bts +{ +namespace crypto +{ + +struct PrivateKey::Pimp +{ + BTS_NO_COPY(Pimp); + + const PasswordCallback* password; + PasswordCallback default_password; + PublicKeyPtr public_key; + + Pimp(const PasswordCallback* pass) : password(pass), default_password(), public_key() + { + initialize(); + if(!password) + password = &default_password; + } + + ~Pimp() + { + } + static void initialize() + { + CryptoInit::initialize(); + } +}; + +int password_callback(char *buf, int size, int rwflag, void *u) +{ + UNUSED(rwflag); + std::string data; + if(u) + { + PasswordCallback* pwd = reinterpret_cast(u); + pwd->request_password(data, data); + } + else + { + PasswordCallback ddefault; + ddefault.request_password(data, data); + } + int sz = data.size(); + if((int)data.size() > size) + sz = size; + + std::memcpy(buf, data.c_str(), sz); + return sz; +} + +const PublicKeyPtr PrivateKey::public_key() const +{ + if(!me->public_key) + me->public_key = PublicKey::create(*this); + return me->public_key; +} + + +const PrivateKeyPtr PrivateKey::create(int bits, AlgoType::type at, const PasswordCallback* pass) +{ + openssl::EVP_PKEY* key = openssl::EVP_PKEY_new(); + if(at == AlgoType::RSA) + { + openssl::RSA* rsa = openssl::RSA_new(); + openssl::BIGNUM* bn = openssl::BN_new(); + openssl::BN_set_word(bn, RSA_F4); + int keyok = openssl::RSA_generate_key_ex(rsa, bits, bn, 0); + openssl::BN_free(bn); + bts_assert_msg(keyok != -1, "Unable to create key"); + int i = openssl::EVP_PKEY_assign_RSA(key, rsa); + bts_assert_msg(1 == i, "Unable to create key"); + } + else + bts_assert_msg(false, "only support RSA"); + return PrivateKeyPtr(new PrivateKey(key, pass)); +} + + +const PrivateKeyPtr PrivateKey::read_file(const std::string& filename, const PasswordCallback* pass) +{ + PrivateKey::Pimp::initialize(); + PrivateKeyPtr 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_PrivateKey(bio, 0, password_callback, (void*)pass); + if(!key) + //LOG(Error, Config) << "Invalid private key passphrase"; + else + ret.reset(new PrivateKey(key, pass)); + return ret; +} + +const PrivateKeyPtr PrivateKey::read_stream(const std::istream& stream, const PasswordCallback* pass) +{ + PrivateKey::Pimp::initialize(); + PrivateKeyPtr ret; + const_cast(stream).seekg(0, std::ios::end); + int length = const_cast(stream).tellg(); + const_cast(stream).seekg(0, std::ios::beg); + + char* data = new char[length]; + const_cast(stream).read(data, length); + openssl::BIO* bio = openssl::BIO_new(openssl::BIO_s_mem()); + BIO_write(bio, data, length); + delete[] data; + + openssl::EVP_PKEY* key = openssl::PEM_read_bio_PrivateKey(bio, 0, password_callback, (void*)pass); + if(!key) + //LOG(Error, Config) << "Invalid private key passphrase"; + else + ret.reset(new PrivateKey(key, pass)); + return ret; +} + + +PrivateKey::PrivateKey(openssl::EVP_PKEY* key, const PasswordCallback* pass) : me(new Pimp(pass)), _key(key) +{ +} + +PrivateKey::~PrivateKey() +{ + if(_key) + EVP_PKEY_free(_key); +} + +typedef int encrypt_decrypt_init(openssl::EVP_PKEY_CTX *ctx); +typedef int encrypt_decrypt(openssl::EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen); + + +bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_data, + encrypt_decrypt_init* init_function, encrypt_decrypt* function, + openssl::EVP_PKEY* key, bool encrypt) +{ + openssl::EVP_PKEY_CTX* ctx = openssl::EVP_PKEY_CTX_new(key, 0); + bts_assert(ctx); + if(init_function) + bts_assert(init_function(ctx)); + bts_assert(openssl::EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING)); + + + char *name, *header; + unsigned char *in, *out; long inlen; size_t outlen; int i = 0; + iostreambio biostream(const_cast(&in_dat)); + if(!encrypt) + i = openssl::PEM_read_bio(biostream.getBIO(), &name, &header, &in, &inlen); + else + { + std::iostream seeker(in_dat.rdbuf()); + seeker.seekg(0, std::ios::end); + inlen = seeker.tellg(); + seeker.seekg(0, std::ios::beg); + + in = new unsigned char[inlen]; + i = openssl::BIO_read(biostream.getBIO(), in, inlen); + } + //BIO_flush(biostream.getBIO()); + BIO_free(biostream.getBIO()); + + bts_assert_msg(i > 0, "file not read, too short?"); + bts_assert(function(ctx, NULL, &outlen, in, inlen)); //to get size + out = new unsigned char[outlen]; + bts_assert(function(ctx, out, &outlen, in, inlen)); + //if(encrypt) + // delete[] in; + + std::string header_name = "BTS", header_header = ""; + iostreambio biostream_out(const_cast(&out_data)); + if(encrypt) + i = openssl::PEM_write_bio(biostream_out.getBIO(), header_name.c_str(), const_cast(header_header.c_str()), out, outlen); + else + i = openssl::BIO_write(biostream_out.getBIO(), out, outlen); + //BIO_flush(biostream_out.getBIO()); + BIO_free(biostream_out.getBIO()); + //delete[] out; + + return (i > 0); +} + +int private_encrypt_func(openssl::EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen) +{ + openssl::EVP_PKEY *pk = openssl::EVP_PKEY_CTX_get0_pkey(ctx); + if(out == 0) + { + *outlen = openssl::RSA_size(pk->pkey.rsa); + return -1; + } + else + { + return RSA_private_encrypt(inlen, const_cast(in), out, pk->pkey.rsa, RSA_PKCS1_PADDING); + } +} + +int public_decrypt_func(openssl::EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen) +{ + openssl::EVP_PKEY *pk = openssl::EVP_PKEY_CTX_get0_pkey(ctx); + if(out == 0) + { + *outlen = openssl::RSA_size(pk->pkey.rsa); + return -1; + } + else + { + int i = RSA_public_decrypt(inlen, const_cast(in), out, pk->pkey.rsa, RSA_PKCS1_PADDING); + out[i] = '\0'; + *outlen = i; + return i; + } +} + +bool PrivateKey::private_encrypt(const std::iostream& in_data, std::iostream& out_data) const +{ + return encrypt_decrypt_function(in_data, out_data, 0, private_encrypt_func, _key, true); +} + +bool PrivateKey::private_decrypt(const std::iostream& in_data, std::iostream& out_data) const +{ + return encrypt_decrypt_function(in_data, out_data, openssl::EVP_PKEY_decrypt_init, openssl::EVP_PKEY_decrypt, _key, false); +} + +bool PrivateKey::public_encrypt(const std::iostream& in_data, std::iostream& out_data) const +{ + return encrypt_decrypt_function(in_data, out_data, openssl::EVP_PKEY_encrypt_init, openssl::EVP_PKEY_encrypt, _key, true); +} + +bool PrivateKey::public_decrypt(const std::iostream& in_data, std::iostream& out_data) const +{ + return encrypt_decrypt_function(in_data, out_data, 0, public_decrypt_func, _key, false); +} + + +bool PrivateKey::encrypt(const std::iostream& in_data, std::iostream& out_data) const +{ + return private_encrypt(in_data, out_data); +} + +bool PrivateKey::decrypt(const std::iostream& in_data, std::iostream& out_data) const +{ + return private_decrypt(in_data, out_data); +} + +void PrivateKey::write(openssl::BIO* bio, CipherType::type cipher_type) const +{ + //see rsa.c ln 213 + const openssl::EVP_CIPHER* ciph = openssl::EVP_get_cipherbyname(CipherType::name[(int)cipher_type]); + bts_assert_msg(cipher_type == CipherType::None || ciph, "unknown cipher type"); + int nbytes = openssl::PEM_write_bio_PKCS8PrivateKey(bio, _key, ciph, 0, 0, password_callback, (void*)me->password); + UNUSED(nbytes); + int err = BIO_flush(bio); + UNUSED(err); +} + +void PrivateKey::write_file(const std::string& filename, CipherType::type cipher_type) const +{ + openssl::BIO* bio = openssl::BIO_new_file(filename.c_str(), "w+"); + write(bio, cipher_type); + BIO_free(bio); +} + + +void PrivateKey::write_stream(std::iostream& stream, CipherType::type cipher_type) const +{ + iostreambio biostream_out(&stream); + write(biostream_out.getBIO(), cipher_type); + BIO_free(biostream_out.getBIO()); +} + +} +} diff --git a/src/lib/utils/crypto/PrivateKey.h b/src/lib/utils/crypto/PrivateKey.h new file mode 100644 index 000000000..dbae5eb4d --- /dev/null +++ b/src/lib/utils/crypto/PrivateKey.h @@ -0,0 +1,70 @@ +#pragma once + +#include "lib/utils/bts_no_copy.h" + +#include "AlgoType.h" +#include "CipherType.h" +#include "PasswordCallback.h" + #include + +#include +#include + +namespace openssl +{ + struct evp_pkey_st; + typedef evp_pkey_st EVP_PKEY; + struct bio_st; + typedef struct bio_st BIO; +} + +namespace bts +{ +namespace crypto +{ + +class PrivateKey; +class PublicKey; +typedef std::shared_ptr PrivateKeyPtr; +typedef std::shared_ptr PublicKeyPtr; + +class PrivateKey +{ + BTS_NO_COPY(PrivateKey); +public: + virtual ~PrivateKey(); + static const PrivateKeyPtr create(int bits, AlgoType::type, const PasswordCallback* pass = 0); + static const PrivateKeyPtr read_file(const std::string& filename, const PasswordCallback* pass = 0); + static const PrivateKeyPtr read_stream(const std::istream& stream, const PasswordCallback* pass = 0); + +public: + virtual void write_file(const std::string& filename, CipherType::type cipher_type = CipherType::None) const; + virtual void write_stream(std::iostream& stream, CipherType::type cipher_type = CipherType::None) 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; + + + const PublicKeyPtr public_key() const; +protected: + friend class PublicKey; + friend int password_callback(char *buf, int size, int rwflag, void *u); + PrivateKey(openssl::EVP_PKEY* key = 0, const PasswordCallback* pass = 0); + virtual void write(openssl::BIO* bio, CipherType::type cipher_type) const; + + bool private_encrypt(const std::iostream& in_dat, std::iostream& out_data) const; + bool private_decrypt(const std::iostream& in_dat, std::iostream& out_data) const; + bool public_encrypt(const std::iostream& in_dat, std::iostream& out_data) const; + bool public_decrypt(const std::iostream& in_dat, std::iostream& out_data) const; + +protected: + struct Pimp; + std::shared_ptr me; + + openssl::EVP_PKEY* _key; +}; + + +} +} + diff --git a/src/lib/utils/crypto/PublicKey.cpp b/src/lib/utils/crypto/PublicKey.cpp new file mode 100644 index 000000000..16ae2adfa --- /dev/null +++ b/src/lib/utils/crypto/PublicKey.cpp @@ -0,0 +1,121 @@ +#include "PublicKey.h" + +#include "lib/logging/logging.h" +#include "lib/utils/crypto/iostreambio.h" + +namespace openssl +{ +#include +#include +#include +#include +#include +} + +namespace bts +{ +namespace 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); + + openssl::EVP_PKEY* k = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0); + BIO_free(bio); + //bts_assert(k); + this->_key = k; +} + +PublicKey::~PublicKey() +{ +} + +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(Error, Config) << "Invalid public key read"; + else + ret.reset(new PublicKey(key)); + return ret; +} + + +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) + //LOG(Error, Config) << "Invalid public key read"; + else + ret.reset(new PublicKey(key)); + return ret; +} + +const PublicKeyPtr PublicKey::read_stream(std::istream& stream) +{ + PublicKeyPtr ret; + stream.seekg(0, std::ios::end); + int length = stream.tellg(); + stream.seekg(0, std::ios::beg); + + char* data = new char[length]; + stream.read(data, length); + openssl::BIO* bio = openssl::BIO_new(openssl::BIO_s_mem()); + BIO_write(bio, data, length); + delete[] data; + + openssl::EVP_PKEY* key = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0); + if(!key) + // LOG(Error, Config) << "Invalid public key read"; + else + ret.reset(new PublicKey(key)); + return ret; +} + +void PublicKey::write(openssl::BIO* bio, CipherType::type) const +{ + //const openssl::EVP_CIPHER* ciph = openssl::EVP_get_cipherbyname(CipherType::name[(int)cipher_type]); + //bts_assert_msg(ciph, "unknown cipher type"); + int nbytes = openssl::PEM_write_bio_PUBKEY(bio, _key); + UNUSED(nbytes); + int err = BIO_flush(bio); + UNUSED(err); +} + +bool PublicKey::encrypt(const std::iostream& in_data, std::iostream& out_data) const +{ + return public_encrypt(in_data, out_data); +} + +bool PublicKey::decrypt(const std::iostream& in_data, std::iostream& out_data) const +{ + return public_decrypt(in_data, out_data); +} + + +} +} + diff --git a/src/lib/utils/crypto/PublicKey.h b/src/lib/utils/crypto/PublicKey.h new file mode 100644 index 000000000..4650aae86 --- /dev/null +++ b/src/lib/utils/crypto/PublicKey.h @@ -0,0 +1,37 @@ +#pragma once + +#include "lib/utils/bts_no_copy.h" + +#include "PrivateKey.h" + +namespace bts +{ +namespace crypto +{ + +class PublicKey : public PrivateKey +{ + BTS_NO_COPY(PublicKey); +public: + PublicKey(const PrivateKey&); + 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; + + static const PublicKeyPtr create(const PrivateKey& private_key); + static const PublicKeyPtr read_file(const std::string& filename); + static const PublicKeyPtr read_stream(std::istream& stream); + static const PublicKeyPtr read_string(const std::string& public_key); + + +protected: + virtual void write(openssl::BIO* bio, CipherType::type cipher_type) const; +protected: + PublicKey(openssl::EVP_PKEY*); + +}; + +} +} + diff --git a/src/lib/utils/crypto/iostreambio.cpp b/src/lib/utils/crypto/iostreambio.cpp new file mode 100644 index 000000000..9b221a04f --- /dev/null +++ b/src/lib/utils/crypto/iostreambio.cpp @@ -0,0 +1,219 @@ +#include "iostreambio.h" + +#include "lib/logging/logging.h" + +namespace openssl +{ +#include +} + +using namespace std; +using namespace openssl; + +namespace bts +{ +namespace crypto +{ + +const char * const iostreambio::name = "iostreambio"; +const int iostreambio::type = BIO_TYPE_SOURCE_SINK; + +/* callback functions for openssl */ +static int bwrite(BIO *bio, const char *buf, int len); +static int bread(BIO *bio, char *buf, int len); +//static int bputs(BIO *bio, const char *); +static int bgets(BIO *bio, char *, int); +static long ctrl(BIO *bio, int cmd, long num, void *ptr); +static int create(BIO *bio); +//static int destroy(BIO *bio); +//static long (*callback_ctrl)(BIO *, int, bio_info_cb *); + +// the new way of producing the BIO_Method is with BIO_meth_new() +// https://www.openssl.org/docs/man1.1.1/man3/BIO_get_new_index.html +// int bio_type = BIO_get_new_index(void); +// static BIO_METHOD iostream_method = BIO_meth_new(bio_type, "iostream_method"); + +// static BIO_METHOD iostream_method = +// { +// iostreambio::type, +// iostreambio::name, +// bwrite, +// bread, +// NULL,//bputs +// bgets,//bgets +// ctrl, +// create, +// NULL,//destroy, +// NULL//callback_ctrl +// }; + +// iostreambio::iostreambio(iostream *stream) : _bio(0) +// { +// //_bio = BIO_new(&iostream_method); +// //bts_assert(_bio); +// //_bio->ptr = stream; +// } + +// BIO* iostreambio::getBIO() +// { +// //return _bio; +// } + +static int create(BIO *bio) +{ + bio->ptr = NULL; + /* (from openssl memory bio) */ + bio->shutdown=1; + bio->init=1; + /* from bss_mem.c (openssl): + * bio->num is used to hold the value to return on 'empty', if it is + * 0, should_retry is not set + * + * => -1 means the caller can retry, 0: retry is useless + * it is set to 0 since the underlying stream is blocking + */ + bio->num= 0; + + return 1; +} + + + +static long ctrl(BIO *bio, int cmd, long num, void *ptr) +{ + UNUSED(ptr); + long ret; + iostream *stream = reinterpret_cast(bio->ptr); + + //LOG(Error, Config) << "iostreambio: ctrl called, cmd: " << cmd << ", num: " << num << "." ; + + switch(cmd){ + case BIO_CTRL_PUSH: + case BIO_CTRL_POP: + ret = 0; + break; + // case BIO_CTRL_GET_CLOSE: + // ret = bio->shutdown; + // break; + // case BIO_CTRL_SET_CLOSE: + // bio->shutdown = (int)num; + // ret = 1; + // break; + case BIO_CTRL_FLUSH: + /* try to flush the underlying stream */ + ret = 1; + try{ + stream->flush(); + } catch(ios_base::failure &){ + /* ignore, the badbit is checked instead */ + } + // catch(ConnectionClosedException &ex){ + // throw; + // } + if(stream->bad()){ + //LOG(Error, Config) << "iostreambio: underlying Stream went bad while flushing." ; + ret = 0; + } + break; + default: + // LOG(Warn, Config) << "iostreambio: ctrl called with unhandled cmd: " << cmd << "." ; + ret = 0; + break; + } + + return ret; +} + + + +static int bread(BIO *bio, char *buf, int len) +{ + iostream *stream = reinterpret_cast(bio->ptr); + streamsize num_bytes = bio->num; + try + { + /* make sure to read at least 1 byte and then read as much as we can */ + //num_bytes = stream->read(buf, 1).readsome(buf+1, len-1) + 1; + int i = 0; + while(stream->good() && i < len) + { + buf[i++] = stream->get(); + } + num_bytes = i; + } + catch(ios_base::failure &) + { + } + return num_bytes; +} + +static int bgets(BIO *bio, char *buf, int len) +{ + iostream *stream = reinterpret_cast(bio->ptr); + streamsize num_bytes = bio->num; + try + { + /* make sure to read at least 1 byte and then read as much as we can */ + //num_bytes = stream->read(buf, 1).readsome(buf+1, len-1) + 1; + int i = 0; + while(stream->good() && i < len) + { + buf[i] = stream->get(); + if(buf[i] == '\n') + { + i++; + break; + } + i++; + } + buf[i] = '\0'; + num_bytes = i; + } + catch(ios_base::failure &) + { + } + return num_bytes; +} + + +static int bwrite(BIO *bio, const char *buf, int len) +{ + if(len == 0){ + return 0; + } + iostream *stream = reinterpret_cast(bio->ptr); + + /* write the data */ + try{ + stream->write(buf, len); + } catch(ios_base::failure &){ + /* ignore, the badbit is checked instead */ + } + // catch(ConnectionClosedException &ex){ + // throw; + // } + if(stream->bad()){ + //LOG(Error, Config) << "iostreambio: underlying Stream went bad while writing." ; + return 0; + } + + /* flush the underlying stream */ + try{ + stream->flush(); + } catch(ios_base::failure &){ + /* ignore, the badbit is checked instead */ + } + // catch(ConnectionClosedException &ex){ + // throw; + // } + if(stream->bad()){ + //LOG(Error, Config) << "iostreambio: underlying Stream went bad while flushing (bwrite)." ; + return 0; + } + + return len; +} + +} +} + diff --git a/src/lib/utils/crypto/iostreambio.h b/src/lib/utils/crypto/iostreambio.h new file mode 100644 index 000000000..cec9a3dc7 --- /dev/null +++ b/src/lib/utils/crypto/iostreambio.h @@ -0,0 +1,30 @@ +#pragma once + +namespace openssl +{ + struct bio_st; + typedef struct bio_st BIO; +} + +#include + +namespace bts +{ +namespace crypto +{ + +class iostreambio +{ +public: + explicit iostreambio(std::iostream* stream); + openssl::BIO* getBIO(); + + static const char * const name; + static const int type; +private: + openssl::BIO *_bio; +}; + +} +} + From 8a965d41ff685b2bbf976024fdab416465982a8d Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Thu, 30 May 2019 18:24:24 -0400 Subject: [PATCH 02/11] keycodes. boost stil can't handle dates --- .vscode/launch.json | 2 +- src/lib/CMakeDeps_win.cmake | 2 +- src/lib/api/services/AuthService.proto | 2 +- src/lib/config/defaults/keycode.conf | 9 + src/lib/config/protos/keycode.proto | 19 ++ src/lib/utils/AuthSessionManager.cpp | 79 ++++++++ src/lib/utils/AuthSessionManager.h | 34 ++++ src/lib/utils/AuthToken.cpp | 42 ++-- src/lib/utils/AuthToken.h | 1 - src/lib/utils/crypto/CryptoInit.cpp | 1 - src/lib/utils/crypto/KeyCodeHelper.cpp | 36 ++++ src/lib/utils/crypto/KeyCodeHelper.h | 14 ++ src/lib/utils/crypto/PrivateKey.cpp | 63 +++--- src/lib/utils/crypto/PrivateKey.h | 2 +- src/lib/utils/crypto/PublicKey.cpp | 31 +-- src/lib/utils/crypto/iostreambio.cpp | 167 +++++++++------- src/lib/utils/crypto/openssl_compat.h | 37 ++++ src/lib/utils/crypto/spark_private.pem | 30 +++ src/lib/utils/crypto/spark_public.pem | 9 + src/lib/utils/datetime.cpp | 5 + src/lib/utils/datetime.h | 1 + src/server/sparkd/cmds/KeyCode.cpp | 261 +++++++++++++++++++++++++ src/server/sparkd/cmds/KeyCode.h | 30 +++ src/server/sparkd/main.cpp | 2 + 24 files changed, 747 insertions(+), 132 deletions(-) create mode 100644 src/lib/config/defaults/keycode.conf create mode 100644 src/lib/config/protos/keycode.proto create mode 100644 src/lib/utils/AuthSessionManager.cpp create mode 100644 src/lib/utils/AuthSessionManager.h create mode 100644 src/lib/utils/crypto/KeyCodeHelper.cpp create mode 100644 src/lib/utils/crypto/KeyCodeHelper.h create mode 100644 src/lib/utils/crypto/openssl_compat.h create mode 100644 src/lib/utils/crypto/spark_private.pem create mode 100644 src/lib/utils/crypto/spark_public.pem create mode 100644 src/server/sparkd/cmds/KeyCode.cpp create mode 100644 src/server/sparkd/cmds/KeyCode.h diff --git a/.vscode/launch.json b/.vscode/launch.json index 71b5a8e9e..d39933c9d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,7 +7,7 @@ "request": "launch", "program": "/home/bts/spark/build/ninja_debug/server/sparkd", "args": [ - "run" + "keycode", "encode", "-keyfile:spark_private.pem", "-pass:BT5u53r", "-users:3", "-expiration:01/22/2020" ], "stopAtEntry": false, "cwd": "/home/bts/spark/build/ninja_debug/server/", diff --git a/src/lib/CMakeDeps_win.cmake b/src/lib/CMakeDeps_win.cmake index c332cbc98..f2ba0e019 100644 --- a/src/lib/CMakeDeps_win.cmake +++ b/src/lib/CMakeDeps_win.cmake @@ -1 +1 @@ -set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/thread_manager.cpp) \ No newline at end of file +set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/crypto/CipherType.cpp;utils/crypto/CryptoInit.cpp;utils/crypto/Digest.cpp;utils/crypto/PasswordCallback.cpp;utils/crypto/PrivateKey.cpp;utils/crypto/PublicKey.cpp;utils/crypto/iostreambio.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/thread_manager.cpp) \ No newline at end of file diff --git a/src/lib/api/services/AuthService.proto b/src/lib/api/services/AuthService.proto index 174ef7dff..2dd9997d2 100644 --- a/src/lib/api/services/AuthService.proto +++ b/src/lib/api/services/AuthService.proto @@ -27,7 +27,7 @@ message User { } message TokenPayload { - uint32 date = 1; + uint64 date = 1; uint32 id = 2; bool wheel = 3; } diff --git a/src/lib/config/defaults/keycode.conf b/src/lib/config/defaults/keycode.conf new file mode 100644 index 000000000..55b88708e --- /dev/null +++ b/src/lib/config/defaults/keycode.conf @@ -0,0 +1,9 @@ + + +EncryptedKeyCode { + value { + keycode: "" + } +} + + diff --git a/src/lib/config/protos/keycode.proto b/src/lib/config/protos/keycode.proto new file mode 100644 index 000000000..93e32133f --- /dev/null +++ b/src/lib/config/protos/keycode.proto @@ -0,0 +1,19 @@ +syntax='proto2'; +package bts.config; + +message KeyCode +{ + optional uint32 nusers = 10; + optional uint64 expiration = 20; + optional uint64 generation_time = 30; +} + +message EncryptedKeyCode +{ + optional string keycode = 10; +} + +message PublicKey +{ + optional string key = 10; +} \ No newline at end of file diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp new file mode 100644 index 000000000..276f92a01 --- /dev/null +++ b/src/lib/utils/AuthSessionManager.cpp @@ -0,0 +1,79 @@ +#include "AuthSessionManager.h" + +#include "lib/utils/datetime.h" +#include "lib/utils/crypto/KeyCodeHelper.h" +#include "lib/utils/crypto/CryptoInit.h" +#include "lib/utils/crypto/PrivateKey.h" +#include "lib/utils/crypto/PublicKey.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 +#include +#include +#include + +namespace bts { +namespace api { +namespace auth { + +const std::string AuthSessionManager::PUBLIC_KEY = + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAseguRgLD88QdhK0agy8f" +"gz3SXp/hmp377+Q+z3OQqGG5kWyKnQfkAQDFeSv8o1iwSQupN0D1QOXyDnV1jmue" +"rqTitasGeq4AwKLhY89O/VuXc+URAO+HZ7EqVWfb3Nv4EOZRiiVsxmVBkld73Rcy" +"UAS4OXh4jDtE8IzOoXB7HhlMPBIAkueqdyy/v9zamzKrxPzWd+IW/IYEdCrUS8ES" +"mxOkeadSHVeH2nlHVREdktE3wKRd3fZ3OkyGaHnUPxBoUW1Ex57BpwdMGUqLZ3W2" +"92yQa/32dsLocoTri4iiImvQ/0JvfhnY9MDTZnmIgb8bPWSvPblQN0RR9OkRDFT9" +"3QIDAQAB"; + +AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() +{ + const auto& ekc = config::get(); + auto pub = bts::crypto::PublicKey::read_string(PUBLIC_KEY); + bts::config::KeyCode kc; + auto valid = bts::crypto::decrypt_keycode(*pub, ekc, &kc); + bts_assert_msg(valid, "Your BTS Keycode is invalid"); + + _nusers = kc.nusers(); + _expiration = kc.expiration(); +} + +bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok) +{ + if(tok.wheel()) + return true; + auto it = _map.find(tok.id()); + if(it != _map.end()) + { + _map[tok.id()] = Session{tok}; + return true; + } + else if(_map.size() < _nusers) + { + _map[tok.id()] = Session{tok}; + return true; + } + return false; +} + +bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) +{ + if(datetime::ll_now() < _expiration) + { + if(tok.wheel()) + return true; + auto it = _map.find(tok.id()); + if(it != _map.end()) + { + return google::protobuf::util::MessageDifferencer::Equivalent(tok, it->second.token) + && (tok.date() < _expiration); + } + } + return false; +} + +}}} \ No newline at end of file diff --git a/src/lib/utils/AuthSessionManager.h b/src/lib/utils/AuthSessionManager.h new file mode 100644 index 000000000..8da8815ea --- /dev/null +++ b/src/lib/utils/AuthSessionManager.h @@ -0,0 +1,34 @@ +#pragma once +#include "lib/api/services/AuthService.pb.h" +#include "lib/api/services/AdminService.client.pb.h" +#include "lib/utils/datetime.h" +#include + +namespace bts { +namespace api { +namespace auth { + +struct Session +{ + api::user::TokenPayload token; +}; + +class AuthSessionManager +{ +public: + AuthSessionManager(); + + bool add_or_replace_session(const api::user::TokenPayload& tok); + bool check_session(const api::user::TokenPayload& tok); + +private: + static const std::string PUBLIC_KEY; + typedef std::map SessionMap; + SessionMap _map; + uint32_t _nusers; + datetime::LLDateTime _expiration; +}; + + + +}}} \ No newline at end of file diff --git a/src/lib/utils/AuthToken.cpp b/src/lib/utils/AuthToken.cpp index 6d70a1c93..1a89bf903 100644 --- a/src/lib/utils/AuthToken.cpp +++ b/src/lib/utils/AuthToken.cpp @@ -5,6 +5,8 @@ #include "datetime.h" #include "base64.hpp" +#include "AuthSessionManager.h" + // cryptopp #include //temp #include @@ -13,6 +15,7 @@ #include #include #include +#include namespace bts { namespace api { @@ -42,45 +45,56 @@ std::string decrypt(const std::string& buffer) { return result; } -AuthToken generate_token(const admin::BTSUser& user) { +AuthSessionManager& session_manager() +{ + static AuthSessionManager mgr; + return mgr; +} + +AuthToken generate_token(const admin::BTSUser& user) +{ api::user::TokenPayload payload; payload.set_date(datetime::ll_now()); payload.set_id(user.id()); - if(user.name() == "bts") { + if(user.name() == "bts") payload.set_wheel(true); - } + session_manager().add_or_replace_session(payload); auto token = encrypt(payload.SerializeAsString()); bts_assert_d(token.size() < 255-8); AuthToken result; result.set_token(token); return result; } -AuthToken generate_wheel_token() { + +AuthToken generate_wheel_token() +{ api::user::TokenPayload payload; payload.set_date(datetime::ll_now()); payload.set_id(99999); payload.set_wheel(true); + session_manager().add_or_replace_session(payload); auto token = encrypt(payload.SerializeAsString()); AuthToken result; result.set_token(token); return result; } -bool decode_token(const api::AuthToken& token, user::TokenPayload* payload) { - if (token.token().empty()) return false; + +bool decode_token(const api::AuthToken& token, user::TokenPayload* payload) +{ + if (token.token().empty()) + return false; std::string temp = decrypt(token.token()); - if (payload) { - return payload->ParseFromString(temp) && !is_expired(*payload); + if(payload) + { + return payload->ParseFromString(temp) && session_manager().check_session(*payload); } - else { + else + { // validate the token but don't return it user::TokenPayload payload; - return payload.ParseFromString(temp) && !is_expired(payload); + return payload.ParseFromString(temp) && session_manager().check_session(payload); } } -bool is_expired(const user::TokenPayload&) { - return false; -} - #else namespace bts { diff --git a/src/lib/utils/AuthToken.h b/src/lib/utils/AuthToken.h index cc704696c..71b1c4da0 100644 --- a/src/lib/utils/AuthToken.h +++ b/src/lib/utils/AuthToken.h @@ -10,7 +10,6 @@ namespace auth { AuthToken generate_token(const admin::BTSUser&); AuthToken generate_wheel_token(); bool decode_token(const api::AuthToken& token, user::TokenPayload* payload); -bool is_expired(const user::TokenPayload&); }}} diff --git a/src/lib/utils/crypto/CryptoInit.cpp b/src/lib/utils/crypto/CryptoInit.cpp index d1fdd9882..34eeef97b 100644 --- a/src/lib/utils/crypto/CryptoInit.cpp +++ b/src/lib/utils/crypto/CryptoInit.cpp @@ -43,7 +43,6 @@ void CryptoInit::cleanup() tbb::spin_mutex::scoped_lock lock(_lock); if(init) { - //openssl::EVP_cleanup(); init = false; } } diff --git a/src/lib/utils/crypto/KeyCodeHelper.cpp b/src/lib/utils/crypto/KeyCodeHelper.cpp new file mode 100644 index 000000000..b3a53bc2e --- /dev/null +++ b/src/lib/utils/crypto/KeyCodeHelper.cpp @@ -0,0 +1,36 @@ +#include "KeyCodeHelper.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/config/protos/keycode.pb.h" +#include +#include +#include +#include + +namespace bts { +namespace crypto { + +bool decrypt_keycode(const bts::crypto::PublicKey& public_key, const bts::config::EncryptedKeyCode& kc, + bts::config::KeyCode* dk) +{ + std::stringstream kss, out; + kss << kc.keycode(); + if(public_key.decrypt(kss, out)) + { + bts::config::KeyCode kc; + google::protobuf::io::IstreamInputStream kc_stream(&kss); + google::protobuf::TextFormat::Parse(&kc_stream, &kc); + if(dk) + { + *dk = kc; + return true; + } + } + return false; +} + +}} \ No newline at end of file diff --git a/src/lib/utils/crypto/KeyCodeHelper.h b/src/lib/utils/crypto/KeyCodeHelper.h new file mode 100644 index 000000000..ce4d0d24d --- /dev/null +++ b/src/lib/utils/crypto/KeyCodeHelper.h @@ -0,0 +1,14 @@ +#pragma once + +namespace bts { +namespace config { + class EncryptedKeyCode; + class KeyCode; +} + +namespace crypto +{ + class PublicKey; + bool decrypt_keycode(const bts::crypto::PublicKey& public_key, const bts::config::EncryptedKeyCode& kc, bts::config::KeyCode* dk); +}} + diff --git a/src/lib/utils/crypto/PrivateKey.cpp b/src/lib/utils/crypto/PrivateKey.cpp index b9566f91a..f9c1b06ab 100644 --- a/src/lib/utils/crypto/PrivateKey.cpp +++ b/src/lib/utils/crypto/PrivateKey.cpp @@ -4,6 +4,8 @@ #include "CryptoInit.h" #include "iostreambio.h" #include "lib/logging/logging.h" +#include "lib/utils/bts_assert.h" +#include "lib/utils/helper_macros.h" #include #include @@ -13,6 +15,7 @@ namespace openssl #include #include #include +#include #include } @@ -26,9 +29,9 @@ struct PrivateKey::Pimp BTS_NO_COPY(Pimp); const PasswordCallback* password; - PasswordCallback default_password; + PasswordCallback default_password; PublicKeyPtr public_key; - + Pimp(const PasswordCallback* pass) : password(pass), default_password(), public_key() { initialize(); @@ -51,7 +54,7 @@ int password_callback(char *buf, int size, int rwflag, void *u) std::string data; if(u) { - PasswordCallback* pwd = reinterpret_cast(u); + PasswordCallback* pwd = reinterpret_cast(u); pwd->request_password(data, data); } else @@ -67,7 +70,7 @@ int password_callback(char *buf, int size, int rwflag, void *u) return sz; } -const PublicKeyPtr PrivateKey::public_key() const +const PublicKeyPtr PrivateKey::public_key() const { if(!me->public_key) me->public_key = PublicKey::create(*this); @@ -83,7 +86,7 @@ const PrivateKeyPtr PrivateKey::create(int bits, AlgoType::type at, const Passwo openssl::RSA* rsa = openssl::RSA_new(); openssl::BIGNUM* bn = openssl::BN_new(); openssl::BN_set_word(bn, RSA_F4); - int keyok = openssl::RSA_generate_key_ex(rsa, bits, bn, 0); + int keyok = openssl::RSA_generate_key_ex(rsa, bits, bn, 0); openssl::BN_free(bn); bts_assert_msg(keyok != -1, "Unable to create key"); int i = openssl::EVP_PKEY_assign_RSA(key, rsa); @@ -93,7 +96,7 @@ const PrivateKeyPtr PrivateKey::create(int bits, AlgoType::type at, const Passwo bts_assert_msg(false, "only support RSA"); return PrivateKeyPtr(new PrivateKey(key, pass)); } - + const PrivateKeyPtr PrivateKey::read_file(const std::string& filename, const PasswordCallback* pass) { @@ -101,11 +104,11 @@ const PrivateKeyPtr PrivateKey::read_file(const std::string& filename, const Pas PrivateKeyPtr 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_PrivateKey(bio, 0, password_callback, (void*)pass); + openssl::EVP_PKEY* key = openssl::PEM_read_bio_PrivateKey(bio, 0, password_callback, (void*)pass); if(!key) - //LOG(Error, Config) << "Invalid private key passphrase"; + LOG(Gen, Error, "Invalid private key passphrase"); else - ret.reset(new PrivateKey(key, pass)); + ret.reset(new PrivateKey(key, pass)); return ret; } @@ -125,7 +128,7 @@ const PrivateKeyPtr PrivateKey::read_stream(const std::istream& stream, const Pa openssl::EVP_PKEY* key = openssl::PEM_read_bio_PrivateKey(bio, 0, password_callback, (void*)pass); if(!key) - //LOG(Error, Config) << "Invalid private key passphrase"; + LOG(Gen, Error, "Invalid private key passphrase"); else ret.reset(new PrivateKey(key, pass)); return ret; @@ -135,7 +138,7 @@ const PrivateKeyPtr PrivateKey::read_stream(const std::istream& stream, const Pa PrivateKey::PrivateKey(openssl::EVP_PKEY* key, const PasswordCallback* pass) : me(new Pimp(pass)), _key(key) { } - + PrivateKey::~PrivateKey() { if(_key) @@ -146,8 +149,8 @@ typedef int encrypt_decrypt_init(openssl::EVP_PKEY_CTX *ctx); typedef int encrypt_decrypt(openssl::EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen); -bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_data, - encrypt_decrypt_init* init_function, encrypt_decrypt* function, +bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_data, + encrypt_decrypt_init* init_function, encrypt_decrypt* function, openssl::EVP_PKEY* key, bool encrypt) { openssl::EVP_PKEY_CTX* ctx = openssl::EVP_PKEY_CTX_new(key, 0); @@ -171,7 +174,7 @@ bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_da in = new unsigned char[inlen]; i = openssl::BIO_read(biostream.getBIO(), in, inlen); - } + } //BIO_flush(biostream.getBIO()); BIO_free(biostream.getBIO()); @@ -183,7 +186,7 @@ bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_da // delete[] in; std::string header_name = "BTS", header_header = ""; - iostreambio biostream_out(const_cast(&out_data)); + iostreambio biostream_out(const_cast(&out_data)); if(encrypt) i = openssl::PEM_write_bio(biostream_out.getBIO(), header_name.c_str(), const_cast(header_header.c_str()), out, outlen); else @@ -198,28 +201,30 @@ bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_da int private_encrypt_func(openssl::EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen) { openssl::EVP_PKEY *pk = openssl::EVP_PKEY_CTX_get0_pkey(ctx); + auto rsa = openssl::EVP_PKEY_get0_RSA(pk); if(out == 0) - { - *outlen = openssl::RSA_size(pk->pkey.rsa); + { + *outlen = openssl::RSA_size(rsa); return -1; } else - { - return RSA_private_encrypt(inlen, const_cast(in), out, pk->pkey.rsa, RSA_PKCS1_PADDING); + { + return RSA_private_encrypt(inlen, const_cast(in), out, rsa, RSA_PKCS1_PADDING); } } int public_decrypt_func(openssl::EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char *in, size_t inlen) { openssl::EVP_PKEY *pk = openssl::EVP_PKEY_CTX_get0_pkey(ctx); + auto rsa = openssl::EVP_PKEY_get0_RSA(pk); if(out == 0) { - *outlen = openssl::RSA_size(pk->pkey.rsa); + *outlen = openssl::RSA_size(rsa); return -1; } else - { - int i = RSA_public_decrypt(inlen, const_cast(in), out, pk->pkey.rsa, RSA_PKCS1_PADDING); + { + int i = RSA_public_decrypt(inlen, const_cast(in), out, rsa, RSA_PKCS1_PADDING); out[i] = '\0'; *outlen = i; return i; @@ -230,20 +235,20 @@ bool PrivateKey::private_encrypt(const std::iostream& in_data, std::iostream& ou { return encrypt_decrypt_function(in_data, out_data, 0, private_encrypt_func, _key, true); } - + bool PrivateKey::private_decrypt(const std::iostream& in_data, std::iostream& out_data) const { - return encrypt_decrypt_function(in_data, out_data, openssl::EVP_PKEY_decrypt_init, openssl::EVP_PKEY_decrypt, _key, false); + return encrypt_decrypt_function(in_data, out_data, openssl::EVP_PKEY_decrypt_init, openssl::EVP_PKEY_decrypt, _key, false); } - + bool PrivateKey::public_encrypt(const std::iostream& in_data, std::iostream& out_data) const { return encrypt_decrypt_function(in_data, out_data, openssl::EVP_PKEY_encrypt_init, openssl::EVP_PKEY_encrypt, _key, true); } - + bool PrivateKey::public_decrypt(const std::iostream& in_data, std::iostream& out_data) const { - return encrypt_decrypt_function(in_data, out_data, 0, public_decrypt_func, _key, false); + return encrypt_decrypt_function(in_data, out_data, 0, public_decrypt_func, _key, false); } @@ -251,7 +256,7 @@ bool PrivateKey::encrypt(const std::iostream& in_data, std::iostream& out_data) { return private_encrypt(in_data, out_data); } - + bool PrivateKey::decrypt(const std::iostream& in_data, std::iostream& out_data) const { return private_decrypt(in_data, out_data); @@ -274,7 +279,7 @@ void PrivateKey::write_file(const std::string& filename, CipherType::type cipher write(bio, cipher_type); BIO_free(bio); } - + void PrivateKey::write_stream(std::iostream& stream, CipherType::type cipher_type) const { diff --git a/src/lib/utils/crypto/PrivateKey.h b/src/lib/utils/crypto/PrivateKey.h index dbae5eb4d..b37eb4a03 100644 --- a/src/lib/utils/crypto/PrivateKey.h +++ b/src/lib/utils/crypto/PrivateKey.h @@ -13,7 +13,7 @@ namespace openssl { struct evp_pkey_st; - typedef evp_pkey_st EVP_PKEY; + typedef struct evp_pkey_st EVP_PKEY; struct bio_st; typedef struct bio_st BIO; } diff --git a/src/lib/utils/crypto/PublicKey.cpp b/src/lib/utils/crypto/PublicKey.cpp index 16ae2adfa..5fc983546 100644 --- a/src/lib/utils/crypto/PublicKey.cpp +++ b/src/lib/utils/crypto/PublicKey.cpp @@ -1,7 +1,10 @@ #include "PublicKey.h" +#include "lib/utils/bts_assert.h" +#include "lib/utils/helper_macros.h" #include "lib/logging/logging.h" #include "lib/utils/crypto/iostreambio.h" +#include namespace openssl { @@ -25,14 +28,14 @@ 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); - + int i = openssl::PEM_write_bio_PUBKEY(bio, key._key); + int err = BIO_flush(bio); + UNUSED(i); + UNUSED(err); + openssl::EVP_PKEY* k = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0); BIO_free(bio); - //bts_assert(k); + bts_assert(k); this->_key = k; } @@ -45,7 +48,7 @@ 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; @@ -53,13 +56,13 @@ const PublicKeyPtr PublicKey::read_file(const std::string& filename) bts_assert_msg(bio, "can't load keyfile"); openssl::EVP_PKEY* key = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0); if(!key) - //LOG(Error, Config) << "Invalid public key read"; + LOG(Gen, Error, "Invalid public key read"); else - ret.reset(new PublicKey(key)); + ret.reset(new PublicKey(key)); return ret; } - + const PublicKeyPtr PublicKey::read_string(const std::string& public_key) { PublicKeyPtr ret; @@ -68,9 +71,9 @@ const PublicKeyPtr PublicKey::read_string(const std::string& public_key) bts::crypto::iostreambio biostream(&oss); openssl::EVP_PKEY* key = openssl::PEM_read_bio_PUBKEY(biostream.getBIO(), 0, 0, 0); if(!key) - //LOG(Error, Config) << "Invalid public key read"; + LOG(Gen, Error, "Invalid public key read"); else - ret.reset(new PublicKey(key)); + ret.reset(new PublicKey(key)); return ret; } @@ -89,7 +92,7 @@ const PublicKeyPtr PublicKey::read_stream(std::istream& stream) openssl::EVP_PKEY* key = openssl::PEM_read_bio_PUBKEY(bio, 0, 0, 0); if(!key) - // LOG(Error, Config) << "Invalid public key read"; + LOG(Gen, Error, "Invalid public key read"); else ret.reset(new PublicKey(key)); return ret; @@ -109,7 +112,7 @@ bool PublicKey::encrypt(const std::iostream& in_data, std::iostream& out_data) c { return public_encrypt(in_data, out_data); } - + bool PublicKey::decrypt(const std::iostream& in_data, std::iostream& out_data) const { return public_decrypt(in_data, out_data); diff --git a/src/lib/utils/crypto/iostreambio.cpp b/src/lib/utils/crypto/iostreambio.cpp index 9b221a04f..69943e0e7 100644 --- a/src/lib/utils/crypto/iostreambio.cpp +++ b/src/lib/utils/crypto/iostreambio.cpp @@ -1,10 +1,15 @@ #include "iostreambio.h" +#include "lib/utils/helper_macros.h" #include "lib/logging/logging.h" +#include "lib/utils/bts_assert.h" + namespace openssl { +#include "openssl_compat.h" #include +#include } using namespace std; @@ -22,49 +27,63 @@ const int iostreambio::type = BIO_TYPE_SOURCE_SINK; static int bwrite(BIO *bio, const char *buf, int len); static int bread(BIO *bio, char *buf, int len); //static int bputs(BIO *bio, const char *); -static int bgets(BIO *bio, char *, int); +//static int bgets(BIO *bio, char *, int); static long ctrl(BIO *bio, int cmd, long num, void *ptr); static int create(BIO *bio); //static int destroy(BIO *bio); //static long (*callback_ctrl)(BIO *, int, bio_info_cb *); -// the new way of producing the BIO_Method is with BIO_meth_new() -// https://www.openssl.org/docs/man1.1.1/man3/BIO_get_new_index.html -// int bio_type = BIO_get_new_index(void); -// static BIO_METHOD iostream_method = BIO_meth_new(bio_type, "iostream_method"); +#if OPENSSL_VERSION_NUMBER >= 0x10100000L +BIO_METHOD * BIO_iostream_method() +{ + static BIO_METHOD *iostream_method = NULL; + if (iostream_method) { + iostream_method = BIO_meth_new(iostreambio::type, iostreambio::name); + BIO_meth_set_write(iostream_method, bwrite); + BIO_meth_set_read(iostream_method, bread); + BIO_meth_set_ctrl(iostream_method, ctrl); + BIO_meth_set_create(iostream_method, create); + } + return iostream_method; +} +#else +static BIO_METHOD iostream_method = +{ + iostreambio::type, + iostreambio::name, + bwrite, + bread, + NULL,//bputs + bgets,//bgets + ctrl, + create, + NULL,//destroy, + NULL//callback_ctrl +}; +BIO_METHOD * BIO_iostream_method() +{ + return &iostream_method; +} +#endif -// static BIO_METHOD iostream_method = -// { -// iostreambio::type, -// iostreambio::name, -// bwrite, -// bread, -// NULL,//bputs -// bgets,//bgets -// ctrl, -// create, -// NULL,//destroy, -// NULL//callback_ctrl -// }; +iostreambio::iostreambio(iostream *stream) : _bio(0) +{ + _bio = BIO_new(BIO_iostream_method()); + bts_assert(_bio); + BIO_set_data(_bio, (void *) stream); +} -// iostreambio::iostreambio(iostream *stream) : _bio(0) -// { -// //_bio = BIO_new(&iostream_method); -// //bts_assert(_bio); -// //_bio->ptr = stream; -// } - -// BIO* iostreambio::getBIO() -// { -// //return _bio; -// } +BIO* iostreambio::getBIO() +{ + return _bio; +} static int create(BIO *bio) { - bio->ptr = NULL; - /* (from openssl memory bio) */ - bio->shutdown=1; - bio->init=1; + BIO_set_data(bio, NULL); + BIO_set_shutdown(bio, 1); + BIO_set_init(bio, 1); +#if OPENSSL_VERSION_NUMBER < 0x10100000L /* from bss_mem.c (openssl): * bio->num is used to hold the value to return on 'empty', if it is * 0, should_retry is not set @@ -73,6 +92,7 @@ static int create(BIO *bio) * it is set to 0 since the underlying stream is blocking */ bio->num= 0; +#endif return 1; } @@ -82,10 +102,11 @@ static int create(BIO *bio) static long ctrl(BIO *bio, int cmd, long num, void *ptr) { UNUSED(ptr); + UNUSED(num); long ret; - iostream *stream = reinterpret_cast(bio->ptr); + iostream *stream = reinterpret_cast(BIO_get_data(bio)); - //LOG(Error, Config) << "iostreambio: ctrl called, cmd: " << cmd << ", num: " << num << "." ; + //LOG(Gen, Error) << "iostreambio: ctrl called, cmd: " << cmd << ", num: " << num << "." ; switch(cmd){ case BIO_CTRL_PUSH: @@ -111,12 +132,12 @@ static long ctrl(BIO *bio, int cmd, long num, void *ptr) // throw; // } if(stream->bad()){ - //LOG(Error, Config) << "iostreambio: underlying Stream went bad while flushing." ; + //LOG(Gen, Error) << "iostreambio: underlying Stream went bad while flushing." ; ret = 0; } break; default: - // LOG(Warn, Config) << "iostreambio: ctrl called with unhandled cmd: " << cmd << "." ; + //LOG(Gen, Error) << "iostreambio: ctrl called with unhandled cmd: " << cmd << "." ; ret = 0; break; } @@ -128,8 +149,12 @@ static long ctrl(BIO *bio, int cmd, long num, void *ptr) static int bread(BIO *bio, char *buf, int len) { - iostream *stream = reinterpret_cast(bio->ptr); + iostream *stream = reinterpret_cast(BIO_get_data(bio)); +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + int num_bytes = 0; +#else streamsize num_bytes = bio->num; +#endif try { /* make sure to read at least 1 byte and then read as much as we can */ @@ -140,40 +165,44 @@ static int bread(BIO *bio, char *buf, int len) buf[i++] = stream->get(); } num_bytes = i; - } + } catch(ios_base::failure &) { } return num_bytes; } -static int bgets(BIO *bio, char *buf, int len) -{ - iostream *stream = reinterpret_cast(bio->ptr); - streamsize num_bytes = bio->num; - try - { - /* make sure to read at least 1 byte and then read as much as we can */ - //num_bytes = stream->read(buf, 1).readsome(buf+1, len-1) + 1; - int i = 0; - while(stream->good() && i < len) - { - buf[i] = stream->get(); - if(buf[i] == '\n') - { - i++; - break; - } - i++; - } - buf[i] = '\0'; - num_bytes = i; - } - catch(ios_base::failure &) - { - } - return num_bytes; -} +// static int bgets(BIO *bio, char *buf, int len) +// { +// iostream *stream = reinterpret_cast(BIO_get_data(bio)); +// #if OPENSSL_VERSION_NUMBER >= 0x10100000L +// int num_bytes = 0; +// #else +// streamsize num_bytes = bio->num; +// #endif +// try +// { +// /* make sure to read at least 1 byte and then read as much as we can */ +// //num_bytes = stream->read(buf, 1).readsome(buf+1, len-1) + 1; +// int i = 0; +// while(stream->good() && i < len) +// { +// buf[i] = stream->get(); +// if(buf[i] == '\n') +// { +// i++; +// break; +// } +// i++; +// } +// buf[i] = '\0'; +// num_bytes = i; +// } +// catch(ios_base::failure &) +// { +// } +// return num_bytes; +// } static int bwrite(BIO *bio, const char *buf, int len) @@ -181,7 +210,7 @@ static int bwrite(BIO *bio, const char *buf, int len) if(len == 0){ return 0; } - iostream *stream = reinterpret_cast(bio->ptr); + iostream *stream = reinterpret_cast(BIO_get_data(bio)); /* write the data */ try{ @@ -193,7 +222,7 @@ static int bwrite(BIO *bio, const char *buf, int len) // throw; // } if(stream->bad()){ - //LOG(Error, Config) << "iostreambio: underlying Stream went bad while writing." ; + //LOG(Gen, Error) << "iostreambio: underlying Stream went bad while writing." ; return 0; } @@ -207,7 +236,7 @@ static int bwrite(BIO *bio, const char *buf, int len) // throw; // } if(stream->bad()){ - //LOG(Error, Config) << "iostreambio: underlying Stream went bad while flushing (bwrite)." ; + //LOG(Gen, Error) << "iostreambio: underlying Stream went bad while flushing (bwrite)." ; return 0; } diff --git a/src/lib/utils/crypto/openssl_compat.h b/src/lib/utils/crypto/openssl_compat.h new file mode 100644 index 000000000..472d1d236 --- /dev/null +++ b/src/lib/utils/crypto/openssl_compat.h @@ -0,0 +1,37 @@ +#ifndef OPENSSL_COMPAT_H +#define OPENSSL_COMPAT_H + +#include +#if OPENSSL_VERSION_NUMBER < 0x10100000L + +#include +#include + +static inline EVP_MD_CTX * EVP_MD_CTX_new() +{ + EVP_MD_CTX *ctx; + + ctx = (EVP_MD_CTX *) OPENSSL_malloc(sizeof(EVP_MD_CTX)); + EVP_MD_CTX_init(ctx); + return ctx; +} +#define EVP_MD_CTX_free(c) if (c != NULL) OPENSSL_free(c) + +static inline HMAC_CTX * HMAC_CTX_new() +{ + HMAC_CTX *ctx; + + ctx = (HMAC_CTX *) OPENSSL_malloc(sizeof(HMAC_CTX)); + HMAC_CTX_init(ctx); + return ctx; +} +#define HMAC_CTX_free(c) if (c != NULL) OPENSSL_free(c) + +#define BIO_get_data(b) b->ptr +#define BIO_set_data(b, v) b->ptr=v +#define BIO_set_shutdown(b, v) b->shutdown=v +#define BIO_set_init(b, v) b->init=v + +#endif /* OPENSSL_VERSION_NUMBER */ + +#endif /* OPENSSL_COMPAT_H */ \ No newline at end of file diff --git a/src/lib/utils/crypto/spark_private.pem b/src/lib/utils/crypto/spark_private.pem new file mode 100644 index 000000000..a5c28d642 --- /dev/null +++ b/src/lib/utils/crypto/spark_private.pem @@ -0,0 +1,30 @@ +-----BEGIN ENCRYPTED PRIVATE KEY----- +MIIFHDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQIy86RzoZQncUCAggA +MAwGCCqGSIb3DQIJBQAwFAYIKoZIhvcNAwcECLhqVDGW5OQVBIIEyJuKgt5yosx2 +O/yVFcF+lyQzPYpPpik99/VwLV9EOTCV4s8rr1AruJCf8QWFBOIFtRbIvPKqUfDY +OVvRU7qFBfTc4K7l8YXWahb7lVvxQTeYBdxrYOVQYdq//pkPXBCMPnCEUvJ5RK8H +wjuKZUbg4zwo2Cqmlu4qIEz/lcoc94W3g87yd2ZeXjmypO6wcijmHYeQ0pZRRW+h +NWIbXjC4hIFL5knO4d7Z+/6E54h4QcZRIn9e2PaQY1sIBRu8x3jA37rwV10ip8IZ +tZ2cTfvV1JMKH6NAIsTHYLjeityUoY0jVqFCj/kNcnSRculwj7QHFkAh79Vuy/3b +/Jwwtf5W294bbjS2qTh7wJNV9DA4/EBNXmMQKywyt+cZO6vPQE03p3UO/A7j9CnG +rTP5lT+hmxGeV7+f1Ik6tvL1XtaVUsY9UIklBZmv49Cn/ISr44o4x/sWX0NiS2XF +Up/GCJvA3F5M+PDzdcFiCr3n/vmdknLBzU6OPkjTlf78aKNDGkS+VXy9Geo1sROO +FmAa7F/ngTLXBDKWLYJqlxROtdIx58c0oNQ2RNrPNHE+zlh9bldc6gpB6wKdAjJ0 +0vA3cpZ3wioqPOiXrvjlcGw69Z3/kQjKtqvGcjlB1sxBZIdnW5KLvXgOgGWgHpdV +kPIWa/q8Toij6qNRdM/3cbvlWcvJxfwClPe84r5+a3SrkDsz8nn+fR5H3/uGUvbr +kDtYxfE+qdD8NIwJIniSYjBC9AU8WbD0TzRt6/D72QsWv4Z1GalsQ1VTjvXTw6AQ +Hp1SBP6wAIxpoC0/fYB9k511NzSFQYCcUXtym53JRlzqhJ/ov17mNZB8ZPegOOHM +Xvo1vquxZtlg8l08keHC3HtLtrUiei8xwe0ID4hZN+dDMD4EMmujKqY8IUEBsVfw +iUT2nLFXM4ElyHRl+C5cszbHgTP3a3W5pSmMGbtNGShn4ueeUqYWcAIsZwWWgsc9 +8R3sCLOyJLJxAVQOCd9Ih4WoINivntJA+eQiPZ7N3jWHUSqJ+SV2goGxPJgtEKBh +KUloUgsEm33DXbvtpr1ei6CDILaapJucxum9eijiFMhGXiZcLj3xTijy0EargkOw +CwHOf3a2hXlXZxTf3/1cdN5WVD8ywnj4116MYW5wYsDojo8D70pE4r2b+76XU/J6 +wt23HQj5EAJvxUzRMw8oG4pc7+r90X4KKcb5/gki2zlwgjKcMYNPdc/yEfYO6hlN +OHD+7IHO5LUD72fIUKvfZpLFqmXHCq2D9fQX2rPWbO6HO5WTKaROgnbs79nnbjfz +JbQMGsvb2sutSESPD6GCDhMa6vmgGJrwIwBqWHIlz8x2+OA9zFSKMsy9Mn1NmaMC +dUlwvxI/Wiu8oiuJ3Nw4c5/7Z2k1P1modJiJkHoQbV+Rn/lmVU1fWA+2Ztv2/Ncn +UZw1Qq6nEJ65k5bpz5iB5L3HMtINsjbFZd8HWfWe5GlpNcvVgn1WmzoKpmhvi4kx +1DmhCLEWk9cSnhoJZ/0BukfOhONYf20K3fsfUl68ZBqSCTzkUIRlcc3ZViaQ5AjU +ohuc1FYLG9FN+p9nzSjj9EkAv+xXBK1H7Evazaby9G4pUfWziySj1YKUIj/AMlX8 +vws8TcB/c30oidvXwDCpVQ== +-----END ENCRYPTED PRIVATE KEY----- diff --git a/src/lib/utils/crypto/spark_public.pem b/src/lib/utils/crypto/spark_public.pem new file mode 100644 index 000000000..55f20b8a0 --- /dev/null +++ b/src/lib/utils/crypto/spark_public.pem @@ -0,0 +1,9 @@ +-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAseguRgLD88QdhK0agy8f +gz3SXp/hmp377+Q+z3OQqGG5kWyKnQfkAQDFeSv8o1iwSQupN0D1QOXyDnV1jmue +rqTitasGeq4AwKLhY89O/VuXc+URAO+HZ7EqVWfb3Nv4EOZRiiVsxmVBkld73Rcy +UAS4OXh4jDtE8IzOoXB7HhlMPBIAkueqdyy/v9zamzKrxPzWd+IW/IYEdCrUS8ES +mxOkeadSHVeH2nlHVREdktE3wKRd3fZ3OkyGaHnUPxBoUW1Ex57BpwdMGUqLZ3W2 +92yQa/32dsLocoTri4iiImvQ/0JvfhnY9MDTZnmIgb8bPWSvPblQN0RR9OkRDFT9 +3QIDAQAB +-----END PUBLIC KEY----- diff --git a/src/lib/utils/datetime.cpp b/src/lib/utils/datetime.cpp index 8c33965e4..c8d68f42d 100755 --- a/src/lib/utils/datetime.cpp +++ b/src/lib/utils/datetime.cpp @@ -183,6 +183,11 @@ std::string simple_date_time_string (PTime const &t) return boost::posix_time::to_simple_string(t); } +PTime parse_simple_date_time_string (std::string const &t) +{ + return boost::posix_time::time_from_string(t); +} + std::string FIX_date_time_string (PTime const &t) { std::string result (strlen ("20121204-14:44:02"),' '); diff --git a/src/lib/utils/datetime.h b/src/lib/utils/datetime.h index 133555edc..dc8f7185b 100755 --- a/src/lib/utils/datetime.h +++ b/src/lib/utils/datetime.h @@ -62,6 +62,7 @@ BTS_EXPORT std::string time_string (PTime const &t); BTS_EXPORT std::string date_string (PTime const &t); BTS_EXPORT std::string iso_date_time_string (PTime const &t); BTS_EXPORT std::string simple_date_time_string (PTime const &t); +BTS_EXPORT PTime parse_simple_date_time_string (std::string const &t); BTS_EXPORT std::string FIX_date_time_string (PTime const &t); BTS_EXPORT PTime parse_FIX_timestamp (const std::string &ts); BTS_EXPORT std::string FIX_timestamp (PTime const &t); diff --git a/src/server/sparkd/cmds/KeyCode.cpp b/src/server/sparkd/cmds/KeyCode.cpp new file mode 100644 index 000000000..a6ac1e71d --- /dev/null +++ b/src/server/sparkd/cmds/KeyCode.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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: -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& 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& 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& 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(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& 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& 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& 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(); +} + + + +}} \ No newline at end of file diff --git a/src/server/sparkd/cmds/KeyCode.h b/src/server/sparkd/cmds/KeyCode.h new file mode 100644 index 000000000..23259a652 --- /dev/null +++ b/src/server/sparkd/cmds/KeyCode.h @@ -0,0 +1,30 @@ +#pragma once + +#include "ICmd.h" + +namespace bts +{ +namespace crypto +{ + class PublicKey; +} +namespace config +{ + class EncryptedKeyCode; + class KeyCode; +} +namespace cmd +{ + +bool decrypt(const bts::crypto::PublicKey& public_key, const bts::config::EncryptedKeyCode& kc, bts::config::KeyCode* dk); +class KeyCode : public ICmd +{ +public: + enum class PublicPrivate {Public, Private}; + int execute(std::vector const &args); + std::string help(); + + static std::string helpstr(); +}; + +}} \ No newline at end of file diff --git a/src/server/sparkd/main.cpp b/src/server/sparkd/main.cpp index 524a8a1f3..6796c3a01 100644 --- a/src/server/sparkd/main.cpp +++ b/src/server/sparkd/main.cpp @@ -7,6 +7,7 @@ #include "server/sparkd/cmds/AutoCert.h" #include "server/sparkd/cmds/Benchmark.h" #include "server/sparkd/cmds/ConfigCheck.h" +#include "server/sparkd/cmds/KeyCode.h" #include "server/sparkd/cmds/Help.h" #include "server/sparkd/cmds/Md.h" #include "server/sparkd/cmds/PrintHedgeOrders.h" @@ -31,6 +32,7 @@ int main(int argc, char **argv) { REGISTER(Md); REGISTER(TestCeloxica); REGISTER(ConfigCheck); + REGISTER(KeyCode); REGISTER(RunSim); REGISTER(Benchmark); REGISTER(QuickTest); From 6647b28a9f5025aa20a9d1f7db8c558931c976ae Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Thu, 6 Jun 2019 13:30:54 -0400 Subject: [PATCH 03/11] keycode generation, encrypt, decrypt works --- src/lib/utils/crypto/KeyCodeHelper.cpp | 2 +- src/lib/utils/crypto/PrivateKey.cpp | 5 +- src/lib/utils/crypto/iostreambio.cpp | 67 +++++++++++++------------- src/lib/utils/datetime.cpp | 13 ++++- src/lib/utils/datetime.h | 1 + src/server/sparkd/cmds/KeyCode.cpp | 26 +++++++--- 6 files changed, 70 insertions(+), 44 deletions(-) diff --git a/src/lib/utils/crypto/KeyCodeHelper.cpp b/src/lib/utils/crypto/KeyCodeHelper.cpp index b3a53bc2e..c15858478 100644 --- a/src/lib/utils/crypto/KeyCodeHelper.cpp +++ b/src/lib/utils/crypto/KeyCodeHelper.cpp @@ -22,7 +22,7 @@ bool decrypt_keycode(const bts::crypto::PublicKey& public_key, const bts::config if(public_key.decrypt(kss, out)) { bts::config::KeyCode kc; - google::protobuf::io::IstreamInputStream kc_stream(&kss); + google::protobuf::io::IstreamInputStream kc_stream(&out); google::protobuf::TextFormat::Parse(&kc_stream, &kc); if(dk) { diff --git a/src/lib/utils/crypto/PrivateKey.cpp b/src/lib/utils/crypto/PrivateKey.cpp index f9c1b06ab..16cc4c89e 100644 --- a/src/lib/utils/crypto/PrivateKey.cpp +++ b/src/lib/utils/crypto/PrivateKey.cpp @@ -164,7 +164,10 @@ bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_da unsigned char *in, *out; long inlen; size_t outlen; int i = 0; iostreambio biostream(const_cast(&in_dat)); if(!encrypt) - i = openssl::PEM_read_bio(biostream.getBIO(), &name, &header, &in, &inlen); + { + auto bio = biostream.getBIO(); + i = openssl::PEM_read_bio(bio, &name, &header, &in, &inlen); + } else { std::iostream seeker(in_dat.rdbuf()); diff --git a/src/lib/utils/crypto/iostreambio.cpp b/src/lib/utils/crypto/iostreambio.cpp index 69943e0e7..d26bc9350 100644 --- a/src/lib/utils/crypto/iostreambio.cpp +++ b/src/lib/utils/crypto/iostreambio.cpp @@ -27,7 +27,7 @@ const int iostreambio::type = BIO_TYPE_SOURCE_SINK; static int bwrite(BIO *bio, const char *buf, int len); static int bread(BIO *bio, char *buf, int len); //static int bputs(BIO *bio, const char *); -//static int bgets(BIO *bio, char *, int); +static int bgets(BIO *bio, char *, int); static long ctrl(BIO *bio, int cmd, long num, void *ptr); static int create(BIO *bio); //static int destroy(BIO *bio); @@ -37,12 +37,13 @@ static int create(BIO *bio); BIO_METHOD * BIO_iostream_method() { static BIO_METHOD *iostream_method = NULL; - if (iostream_method) { + if (!iostream_method) { iostream_method = BIO_meth_new(iostreambio::type, iostreambio::name); BIO_meth_set_write(iostream_method, bwrite); BIO_meth_set_read(iostream_method, bread); BIO_meth_set_ctrl(iostream_method, ctrl); BIO_meth_set_create(iostream_method, create); + BIO_meth_set_gets(iostream_method, bgets); } return iostream_method; } @@ -172,37 +173,37 @@ static int bread(BIO *bio, char *buf, int len) return num_bytes; } -// static int bgets(BIO *bio, char *buf, int len) -// { -// iostream *stream = reinterpret_cast(BIO_get_data(bio)); -// #if OPENSSL_VERSION_NUMBER >= 0x10100000L -// int num_bytes = 0; -// #else -// streamsize num_bytes = bio->num; -// #endif -// try -// { -// /* make sure to read at least 1 byte and then read as much as we can */ -// //num_bytes = stream->read(buf, 1).readsome(buf+1, len-1) + 1; -// int i = 0; -// while(stream->good() && i < len) -// { -// buf[i] = stream->get(); -// if(buf[i] == '\n') -// { -// i++; -// break; -// } -// i++; -// } -// buf[i] = '\0'; -// num_bytes = i; -// } -// catch(ios_base::failure &) -// { -// } -// return num_bytes; -// } +static int bgets(BIO *bio, char *buf, int len) +{ + iostream *stream = reinterpret_cast(BIO_get_data(bio)); +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + int num_bytes = 0; +#else + streamsize num_bytes = bio->num; +#endif + try + { + /* make sure to read at least 1 byte and then read as much as we can */ + //num_bytes = stream->read(buf, 1).readsome(buf+1, len-1) + 1; + int i = 0; + while(stream->good() && i < len) + { + buf[i] = stream->get(); + if(buf[i] == '\n') + { + i++; + break; + } + i++; + } + buf[i] = '\0'; + num_bytes = i; + } + catch(ios_base::failure &) + { + } + return num_bytes; +} static int bwrite(BIO *bio, const char *buf, int len) diff --git a/src/lib/utils/datetime.cpp b/src/lib/utils/datetime.cpp index c8d68f42d..4e9d52b3d 100755 --- a/src/lib/utils/datetime.cpp +++ b/src/lib/utils/datetime.cpp @@ -178,6 +178,14 @@ std::string iso_date_time_string (PTime const &t) return boost::posix_time::to_iso_extended_string(t); } +PTime parse_iso_date_time_string (std::string const &t) +{ + if(t.find('T') == t.npos) + return boost::posix_time::from_iso_extended_string(t + "T00:00:00"); + else + return boost::posix_time::from_iso_extended_string(t); +} + std::string simple_date_time_string (PTime const &t) { return boost::posix_time::to_simple_string(t); @@ -185,7 +193,10 @@ std::string simple_date_time_string (PTime const &t) PTime parse_simple_date_time_string (std::string const &t) { - return boost::posix_time::time_from_string(t); + if(t.find(' ') == t.npos) + return boost::posix_time::time_from_string(t + " 00:00:00"); + else + return boost::posix_time::time_from_string(t); } std::string FIX_date_time_string (PTime const &t) diff --git a/src/lib/utils/datetime.h b/src/lib/utils/datetime.h index dc8f7185b..b9ba82dca 100755 --- a/src/lib/utils/datetime.h +++ b/src/lib/utils/datetime.h @@ -61,6 +61,7 @@ BTS_EXPORT long long msecs_since (PTime const &t1); BTS_EXPORT std::string time_string (PTime const &t); BTS_EXPORT std::string date_string (PTime const &t); BTS_EXPORT std::string iso_date_time_string (PTime const &t); +BTS_EXPORT PTime parse_iso_date_time_string (std::string const &t); BTS_EXPORT std::string simple_date_time_string (PTime const &t); BTS_EXPORT PTime parse_simple_date_time_string (std::string const &t); BTS_EXPORT std::string FIX_date_time_string (PTime const &t); diff --git a/src/server/sparkd/cmds/KeyCode.cpp b/src/server/sparkd/cmds/KeyCode.cpp index a6ac1e71d..0104db3b5 100644 --- a/src/server/sparkd/cmds/KeyCode.cpp +++ b/src/server/sparkd/cmds/KeyCode.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -40,7 +41,7 @@ std::string KeyCode::helpstr () { std::ostringstream oss; oss << "\n" - << "ENCODE -keyfile:[keyfile] -pass:password -users: -expiration:<1/23/2050>\n " + << "ENCODE -keyfile:[keyfile] -pass:password -users: -expiration:<2020-05-18>\n " << "DECODE -keyfile:[keyfile] -pass:password -source:[file] \n " << "KEYGEN -keyfile:[keyfile_prefix]\n "; @@ -49,8 +50,8 @@ std::string KeyCode::helpstr () 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); + auto the_date = bts::datetime::parse_iso_date_time_string(date); + return bts::datetime::iso_date_time_string(the_date); } @@ -137,7 +138,7 @@ void write_keycode(const std::string& users, const std::string& expiration, bts: 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 = bts::datetime::parse_iso_date_time_string(expiration); auto expir_ll = bts::datetime::ptime_to_ll(expir); auto now = bts::datetime::ll_now(); @@ -178,10 +179,16 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri ekc.set_keycode(encrypted.str()); std::string pb; google::protobuf::TextFormat::PrintToString(ekc, &pb); + std::cout << std::endl << std::endl; 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; + 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; } } @@ -208,10 +215,13 @@ int decode(const std::vector& args) //KeycodePayloadHelper helper; bts::config::EncryptedKeyCode ekc; std::fstream fs(source); - google::protobuf::io::IstreamInputStream pay_stream(&fs); - google::protobuf::TextFormat::Parse(&pay_stream, &ekc); + //google::protobuf::io::IstreamInputStream pay_stream(&fs); + //google::protobuf::TextFormat::Parse(&pay_stream, &ekc); + std::string str((std::istreambuf_iterator(fs)), std::istreambuf_iterator()); + config::internal::initialize_for_test(str); + ekc = config::get(); + 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); From 8afb35e564d2df3f27294778f376af40d88de22a Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Thu, 6 Jun 2019 14:54:33 -0400 Subject: [PATCH 04/11] don't compile crypto on win --- .vscode/launch.json | 2 +- src/lib/CMakeDeps_win.cmake | 2 +- src/lib/config/defaults/keycode.conf | 11 +++++------ src/lib/utils/AuthSessionManager.cpp | 4 +++- src/lib/utils/crypto/CryptoInit.cpp | 3 ++- src/lib/utils/crypto/Digest.cpp | 3 ++- src/lib/utils/crypto/KeyCodeHelper.cpp | 4 +++- src/lib/utils/crypto/PrivateKey.cpp | 2 ++ src/lib/utils/crypto/PublicKey.cpp | 2 ++ src/lib/utils/crypto/iostreambio.cpp | 3 ++- 10 files changed, 23 insertions(+), 13 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index d39933c9d..71b5a8e9e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,7 +7,7 @@ "request": "launch", "program": "/home/bts/spark/build/ninja_debug/server/sparkd", "args": [ - "keycode", "encode", "-keyfile:spark_private.pem", "-pass:BT5u53r", "-users:3", "-expiration:01/22/2020" + "run" ], "stopAtEntry": false, "cwd": "/home/bts/spark/build/ninja_debug/server/", diff --git a/src/lib/CMakeDeps_win.cmake b/src/lib/CMakeDeps_win.cmake index f2ba0e019..13884c08d 100644 --- a/src/lib/CMakeDeps_win.cmake +++ b/src/lib/CMakeDeps_win.cmake @@ -1 +1 @@ -set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/crypto/CipherType.cpp;utils/crypto/CryptoInit.cpp;utils/crypto/Digest.cpp;utils/crypto/PasswordCallback.cpp;utils/crypto/PrivateKey.cpp;utils/crypto/PublicKey.cpp;utils/crypto/iostreambio.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/thread_manager.cpp) \ No newline at end of file +set(sources api/apicore.proto;api/apigateway.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/service_helpers/AdminServiceUtils.cpp;api/service_helpers/AlgoServiceUtils.cpp;api/services/AdminService.proto;api/services/AlgoDefinitionService.proto;api/services/AlgoService.proto;api/services/ApiUnitTestService.proto;api/services/AuthService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MBOService.proto;api/services/MarketDataAuxiliaryService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/PositionService.proto;api/services/ShellDiagnosticService.proto;api/services/ShellLatencyService.proto;api/services/SyntheticSecurityService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/cme_reference_data.conf;config/defaults/edge.conf;config/defaults/jetfix.conf;config/defaults/keycode.conf;config/defaults/logging.conf;config/defaults/orderman_cfg.conf;config/defaults/orders_fills_cleanup.conf;config/defaults/spreader.conf;config/defaults/testkeys.conf;config/defaults/threads.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/ExchangeSimCfg.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/cme_reference_data.proto;config/protos/jetfix.proto;config/protos/keycode.proto;config/protos/logging.proto;config/protos/mercury_cfg.proto;config/protos/orderman_cfg.proto;config/protos/orders_fills_cleanup.proto;config/protos/perflog_cfg.proto;config/protos/quickbus_cfg.proto;config/protos/spreader.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/threads.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;domain_model/SecurityTests.cpp;domain_model/tests/domain_tests.cpp;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPeriodicDumper.cpp;md/Diag/ShmDumper.cpp;md/MBOProcessor.cpp;md/MBOServiceServer.cpp;md/MDClient.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/PriceSize.cpp;md/SecurityCache.cpp;md/SecurityDB.cpp;md/SecurityIDRecorder.cpp;md/SecurityOracle.cpp;md/SettlementRecorder.cpp;md/Shm/FixedSecurity.cpp;md/SyntheticSecurityService.cpp;md/cme/MDP3Utils.cpp;md/common.cpp;md/ice/ICEUtils.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2ManagedTestHelper.cpp;md/test/Co2TestInstrument.cpp;md/test/mbo_tests.cpp;md/test/md2_tests.cpp;md/test/md_test_utils.cpp;md/test/synthetic_book_merge_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/catch_test_runner.cpp;utils/AuthSessionManager.cpp;utils/AuthToken.cpp;utils/Dispatcher.cpp;utils/GrowableSPSCQueue.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SecureLogon.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/crypto/CipherType.cpp;utils/crypto/CryptoInit.cpp;utils/crypto/Digest.cpp;utils/crypto/KeyCodeHelper.cpp;utils/crypto/PasswordCallback.cpp;utils/crypto/PrivateKey.cpp;utils/crypto/PublicKey.cpp;utils/crypto/iostreambio.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp;utils/stats.cpp;utils/tests/AuthTokenTests.cpp;utils/thread_manager.cpp) \ No newline at end of file diff --git a/src/lib/config/defaults/keycode.conf b/src/lib/config/defaults/keycode.conf index 55b88708e..a136e9c35 100644 --- a/src/lib/config/defaults/keycode.conf +++ b/src/lib/config/defaults/keycode.conf @@ -1,9 +1,8 @@ - - +#nusers: 2 +#expires: 2019-Sep-30 00:00:00 637053984000000000 +#generation_time: 2019-Jun-06 17:30:16.234397 5248640408589731874 EncryptedKeyCode { value { - keycode: "" + keycode: "-----BEGIN BTS-----\njkMxNGzarPWcVi0cMbpXa9KkqNKAj3tLVcb5CtACLu78F6RYdpQOJq8Rrt6TQJ4o\nxJdHyAzPwhr/KR/8EMcqWuc5WdailgqoHq+VNhVMD2lVmdMGrIlvLYWzKqgPIvBL\nMgB9hWCMq8vC1JEXmLWgAg9pLcS0LCvE+ydUm3nLdL3kEmfLmbcYfN0FaSBIFieN\n6BQZyL23vmKGGuG5TzJpW9ClRQ8I/iQUGDu3FDTbaEHlVgBrD5NHzQszQ6JKgvGr\ncLMC9gyaI0oaY4AqT/d/img7NMehLdpG4MPsqQXW1iuRxAnI0aJYwXd9WSpCmR+W\nnZynJGKRzWKwlfKspYzXtQ==\n-----END BTS-----\n" } -} - - +} \ No newline at end of file diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index 276f92a01..4ac788a26 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -1,3 +1,4 @@ +#ifndef _WIN32 #include "AuthSessionManager.h" #include "lib/utils/datetime.h" @@ -76,4 +77,5 @@ bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) return false; } -}}} \ No newline at end of file +}}} +#endif \ No newline at end of file diff --git a/src/lib/utils/crypto/CryptoInit.cpp b/src/lib/utils/crypto/CryptoInit.cpp index 34eeef97b..308ef8d43 100644 --- a/src/lib/utils/crypto/CryptoInit.cpp +++ b/src/lib/utils/crypto/CryptoInit.cpp @@ -1,3 +1,4 @@ +#ifndef _WIN32 #include "CryptoInit.h" #include @@ -51,4 +52,4 @@ void CryptoInit::cleanup() } } - +#endif \ No newline at end of file diff --git a/src/lib/utils/crypto/Digest.cpp b/src/lib/utils/crypto/Digest.cpp index 147fd0c94..d846e3d34 100644 --- a/src/lib/utils/crypto/Digest.cpp +++ b/src/lib/utils/crypto/Digest.cpp @@ -1,3 +1,4 @@ +#ifndef _WIN32 #include "Digest.h" #include "PrivateKey.h" @@ -63,4 +64,4 @@ std::string Digest::compute_digest(const std::string& data) } } - +#endif \ No newline at end of file diff --git a/src/lib/utils/crypto/KeyCodeHelper.cpp b/src/lib/utils/crypto/KeyCodeHelper.cpp index c15858478..bd6c1c9db 100644 --- a/src/lib/utils/crypto/KeyCodeHelper.cpp +++ b/src/lib/utils/crypto/KeyCodeHelper.cpp @@ -1,3 +1,4 @@ +#ifndef _WIN32 #include "KeyCodeHelper.h" #include "lib/utils/datetime.h" @@ -33,4 +34,5 @@ bool decrypt_keycode(const bts::crypto::PublicKey& public_key, const bts::config return false; } -}} \ No newline at end of file +}} +#endif \ No newline at end of file diff --git a/src/lib/utils/crypto/PrivateKey.cpp b/src/lib/utils/crypto/PrivateKey.cpp index 16cc4c89e..057e0f87b 100644 --- a/src/lib/utils/crypto/PrivateKey.cpp +++ b/src/lib/utils/crypto/PrivateKey.cpp @@ -1,3 +1,4 @@ +#ifndef _WIN32 #include "PrivateKey.h" #include "PublicKey.h" @@ -293,3 +294,4 @@ void PrivateKey::write_stream(std::iostream& stream, CipherType::type cipher_typ } } +#endif \ No newline at end of file diff --git a/src/lib/utils/crypto/PublicKey.cpp b/src/lib/utils/crypto/PublicKey.cpp index 5fc983546..14ca60cf1 100644 --- a/src/lib/utils/crypto/PublicKey.cpp +++ b/src/lib/utils/crypto/PublicKey.cpp @@ -1,3 +1,4 @@ +#ifndef _WIN32 #include "PublicKey.h" #include "lib/utils/bts_assert.h" @@ -122,3 +123,4 @@ bool PublicKey::decrypt(const std::iostream& in_data, std::iostream& out_data) c } } +#endif \ No newline at end of file diff --git a/src/lib/utils/crypto/iostreambio.cpp b/src/lib/utils/crypto/iostreambio.cpp index d26bc9350..65aa218c2 100644 --- a/src/lib/utils/crypto/iostreambio.cpp +++ b/src/lib/utils/crypto/iostreambio.cpp @@ -1,3 +1,4 @@ +#ifndef _WIN32 #include "iostreambio.h" #include "lib/utils/helper_macros.h" @@ -246,4 +247,4 @@ static int bwrite(BIO *bio, const char *buf, int len) } } - +#endif \ No newline at end of file From 8a1d49733ea8cc50a1a67c5f9e824182bcd88953 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Thu, 6 Jun 2019 16:40:32 -0400 Subject: [PATCH 05/11] keycodes work, need some documentation --- src/lib/config/defaults/keycode.conf | 6 +++--- src/lib/utils/AuthSessionManager.cpp | 11 +++++++---- src/lib/utils/crypto/KeyCodeHelper.cpp | 2 +- src/lib/utils/crypto/PrivateKey.cpp | 1 + src/server/sparkd/cmds/KeyCode.cpp | 21 +++++++++++++++++++-- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/lib/config/defaults/keycode.conf b/src/lib/config/defaults/keycode.conf index a136e9c35..7bd2b5a67 100644 --- a/src/lib/config/defaults/keycode.conf +++ b/src/lib/config/defaults/keycode.conf @@ -1,8 +1,8 @@ -#nusers: 2 +#nusers: 1 #expires: 2019-Sep-30 00:00:00 637053984000000000 -#generation_time: 2019-Jun-06 17:30:16.234397 5248640408589731874 +#generation_time: 2019-Jun-06 19:57:36.339686 5248640496990784764 EncryptedKeyCode { value { - keycode: "-----BEGIN BTS-----\njkMxNGzarPWcVi0cMbpXa9KkqNKAj3tLVcb5CtACLu78F6RYdpQOJq8Rrt6TQJ4o\nxJdHyAzPwhr/KR/8EMcqWuc5WdailgqoHq+VNhVMD2lVmdMGrIlvLYWzKqgPIvBL\nMgB9hWCMq8vC1JEXmLWgAg9pLcS0LCvE+ydUm3nLdL3kEmfLmbcYfN0FaSBIFieN\n6BQZyL23vmKGGuG5TzJpW9ClRQ8I/iQUGDu3FDTbaEHlVgBrD5NHzQszQ6JKgvGr\ncLMC9gyaI0oaY4AqT/d/img7NMehLdpG4MPsqQXW1iuRxAnI0aJYwXd9WSpCmR+W\nnZynJGKRzWKwlfKspYzXtQ==\n-----END BTS-----\n" + keycode: "JeCTIruFKQ79vsp7EciymRICr+Hl89z53lFT6+KZGkRRFl0+M0NiXwU9xA3pN9wDya1CfLUHIZtJ0vlH3FmJGknD+AzXeRM97teuyjRAEOxiKtoDTU/vVlPTOdsB2vTv4cdkwDn3mKdaz2uxDs8HPNyJGBCJSv30I/BN/IhfuHMNUo7AfNl0tuCduuqu+ZdMyDuKm1cGXKzORMpOWSHH+XgPleL3ms/tndCtk0eMpNS+sxZ7EHaqT6gNbnxwfDGeifDdGxWKHqmO3mPS34+VW/njdre3tXOZMVi90N9dkoAIrToV0wpRk7JPT6BcQL5+Y45bGWqFktWqH3dp2X3doQ==" } } \ No newline at end of file diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index 4ac788a26..372c56c11 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -23,13 +23,15 @@ namespace api { namespace auth { const std::string AuthSessionManager::PUBLIC_KEY = - "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAseguRgLD88QdhK0agy8f" +"-----BEGIN PUBLIC KEY-----\n" +"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAseguRgLD88QdhK0agy8f" "gz3SXp/hmp377+Q+z3OQqGG5kWyKnQfkAQDFeSv8o1iwSQupN0D1QOXyDnV1jmue" "rqTitasGeq4AwKLhY89O/VuXc+URAO+HZ7EqVWfb3Nv4EOZRiiVsxmVBkld73Rcy" "UAS4OXh4jDtE8IzOoXB7HhlMPBIAkueqdyy/v9zamzKrxPzWd+IW/IYEdCrUS8ES" "mxOkeadSHVeH2nlHVREdktE3wKRd3fZ3OkyGaHnUPxBoUW1Ex57BpwdMGUqLZ3W2" "92yQa/32dsLocoTri4iiImvQ/0JvfhnY9MDTZnmIgb8bPWSvPblQN0RR9OkRDFT9" -"3QIDAQAB"; +"3QIDAQAB" +"\n-----END PUBLIC KEY-----\n"; AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() { @@ -63,7 +65,8 @@ bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& t bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) { - if(datetime::ll_now() < _expiration) + auto now = datetime::ll_now(); + if(datetime::ll_diff_in_us(now, _expiration) > 0) { if(tok.wheel()) return true; @@ -71,7 +74,7 @@ bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) if(it != _map.end()) { return google::protobuf::util::MessageDifferencer::Equivalent(tok, it->second.token) - && (tok.date() < _expiration); + && datetime::ll_diff_in_us(tok.date(), _expiration) > 0; } } return false; diff --git a/src/lib/utils/crypto/KeyCodeHelper.cpp b/src/lib/utils/crypto/KeyCodeHelper.cpp index bd6c1c9db..4997e5158 100644 --- a/src/lib/utils/crypto/KeyCodeHelper.cpp +++ b/src/lib/utils/crypto/KeyCodeHelper.cpp @@ -19,7 +19,7 @@ bool decrypt_keycode(const bts::crypto::PublicKey& public_key, const bts::config bts::config::KeyCode* dk) { std::stringstream kss, out; - kss << kc.keycode(); + kss << "-----BEGIN BTS-----\n" + kc.keycode() + "\n-----END BTS-----\n"; if(public_key.decrypt(kss, out)) { bts::config::KeyCode kc; diff --git a/src/lib/utils/crypto/PrivateKey.cpp b/src/lib/utils/crypto/PrivateKey.cpp index 057e0f87b..a09904a04 100644 --- a/src/lib/utils/crypto/PrivateKey.cpp +++ b/src/lib/utils/crypto/PrivateKey.cpp @@ -154,6 +154,7 @@ bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_da encrypt_decrypt_init* init_function, encrypt_decrypt* function, openssl::EVP_PKEY* key, bool encrypt) { + UNUSED(encrypt); openssl::EVP_PKEY_CTX* ctx = openssl::EVP_PKEY_CTX_new(key, 0); bts_assert(ctx); if(init_function) diff --git a/src/server/sparkd/cmds/KeyCode.cpp b/src/server/sparkd/cmds/KeyCode.cpp index 0104db3b5..2a97c6b2f 100644 --- a/src/server/sparkd/cmds/KeyCode.cpp +++ b/src/server/sparkd/cmds/KeyCode.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -147,6 +148,17 @@ void write_keycode(const std::string& users, const std::string& expiration, bts: kc.set_generation_time(now); } +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) { @@ -176,7 +188,12 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri //int ret = openssl::RSA_private_encrypt(sz-1, pay, &buffer[0], key->pkey.rsa, RSA_PKCS1_PADDING); EncryptedKeyCode ekc; - ekc.set_keycode(encrypted.str()); + 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(kcstr, "\n", "\\n"); + ekc.set_keycode(kcstr); std::string pb; google::protobuf::TextFormat::PrintToString(ekc, &pb); std::cout << std::endl << std::endl; @@ -217,7 +234,7 @@ int decode(const std::vector& args) std::fstream fs(source); //google::protobuf::io::IstreamInputStream pay_stream(&fs); //google::protobuf::TextFormat::Parse(&pay_stream, &ekc); - std::string str((std::istreambuf_iterator(fs)), std::istreambuf_iterator()); + std::string str((std::istreambuf_iterator(fs)), std::istreambuf_iterator()); config::internal::initialize_for_test(str); ekc = config::get(); From a28d5883f7259016a6f22460b33a4c5fd05be338 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Thu, 6 Jun 2019 17:19:35 -0400 Subject: [PATCH 06/11] better to zero out the market data if not entitled so they don't trade off of it --- src/lib/md/relay/QueryServer.cpp | 19 ++++++++++++++++++- src/lib/utils/AuthSessionManager.cpp | 14 +++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index 4fb3ba95e..84f15b4c9 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -246,7 +246,24 @@ public: pupd->mutable_security()->CopyFrom(handle.get_normalized_security()); snapshot = true; } - if (entitled) { + if (!entitled) { + if(chan.entitled()) snapshot = true; + if (HASFLAG(flags, MDRequest::BBO) || HASFLAG(flags, MDRequest::DIRECT)) { + auto* p = pupd->mutable_direct(); + for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + convert(NO_MARKET, p->add_bids()); + convert(NO_MARKET, p->add_asks()); + } + } + if ((HASFLAG(flags, MDRequest::IMPLIED) || HASFLAG(flags, MDRequest::BBO)) && !handle.is_synthetic()) { + auto* p = pupd->mutable_implied(); + for (size_t x = 0; x < COCONUT_MAX_IMPLIED_DEPTH; ++x) { + convert(NO_MARKET, p->add_bids()); + convert(NO_MARKET, p->add_asks()); + } + } + } + else { // If we are entitled now but this is the first request where we are ensure we send all values if(!chan.entitled()) snapshot = true; diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index 372c56c11..012d180ab 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -43,8 +43,15 @@ AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() _nusers = kc.nusers(); _expiration = kc.expiration(); + + auto current = datetime::ll_diff_in_us(datetime::ll_now(), _expiration) > 0; + if(!current) + LOG(Gen,Error, "Your BTS Keycode is expired"); + bts_assert_msg(current, "Your BTS Keycode is EXPIRED"); } + + bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok) { if(tok.wheel()) @@ -71,10 +78,11 @@ bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) if(tok.wheel()) return true; auto it = _map.find(tok.id()); - if(it != _map.end()) + if(it != _map.end()) { - return google::protobuf::util::MessageDifferencer::Equivalent(tok, it->second.token) - && datetime::ll_diff_in_us(tok.date(), _expiration) > 0; + //return google::protobuf::util::MessageDifferencer::Equivalent(tok, it->second.token) //this is probably slow + return tok.date() == it->second.token.date() && //ids are equal + datetime::ll_diff_in_us(tok.date(), _expiration) > 0; } } return false; From af365177d0983d65ddb8fe84218e397a772bc804 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Mon, 10 Jun 2019 12:46:37 -0400 Subject: [PATCH 07/11] PR feedback and a manual test --- src/lib/md/relay/QueryServer.cpp | 6 +- src/lib/utils/AuthSessionManager.cpp | 19 +++--- src/lib/utils/AuthToken.cpp | 1 - src/lib/utils/crypto/iostreambio.cpp | 97 +++++++++++++--------------- testing/manual/keycodes/keycode.txt | 40 ++++++++++++ 5 files changed, 97 insertions(+), 66 deletions(-) create mode 100644 testing/manual/keycodes/keycode.txt diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index 84f15b4c9..f0ede96a5 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -246,8 +246,8 @@ public: pupd->mutable_security()->CopyFrom(handle.get_normalized_security()); snapshot = true; } - if (!entitled) { - if(chan.entitled()) snapshot = true; + //if we are not entitled AND this is the first request since we lost our entitlement, zero out books + if (!entitled && chan.entitled() ) { if (HASFLAG(flags, MDRequest::BBO) || HASFLAG(flags, MDRequest::DIRECT)) { auto* p = pupd->mutable_direct(); for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { @@ -263,7 +263,7 @@ public: } } } - else { + if(entitled) { // If we are entitled now but this is the first request where we are ensure we send all values if(!chan.entitled()) snapshot = true; diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index 012d180ab..c51f019fe 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -42,12 +42,7 @@ AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() bts_assert_msg(valid, "Your BTS Keycode is invalid"); _nusers = kc.nusers(); - _expiration = kc.expiration(); - - auto current = datetime::ll_diff_in_us(datetime::ll_now(), _expiration) > 0; - if(!current) - LOG(Gen,Error, "Your BTS Keycode is expired"); - bts_assert_msg(current, "Your BTS Keycode is EXPIRED"); + _expiration = kc.expiration(); } @@ -56,6 +51,11 @@ bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& t { if(tok.wheel()) return true; + if(datetime::ll_diff_in_us(datetime::ll_now(), _expiration) < 0) + { + LOG(Gen,Error, "Your BTS Keycode is expired!"); + return false; + } auto it = _map.find(tok.id()); if(it != _map.end()) { @@ -72,11 +72,10 @@ bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& t bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) { - auto now = datetime::ll_now(); - if(datetime::ll_diff_in_us(now, _expiration) > 0) + if(tok.wheel()) + return true; + if(datetime::ll_diff_in_us(datetime::ll_now(), _expiration) > 0) { - if(tok.wheel()) - return true; auto it = _map.find(tok.id()); if(it != _map.end()) { diff --git a/src/lib/utils/AuthToken.cpp b/src/lib/utils/AuthToken.cpp index 1a89bf903..5b4be4dbd 100644 --- a/src/lib/utils/AuthToken.cpp +++ b/src/lib/utils/AuthToken.cpp @@ -72,7 +72,6 @@ AuthToken generate_wheel_token() payload.set_date(datetime::ll_now()); payload.set_id(99999); payload.set_wheel(true); - session_manager().add_or_replace_session(payload); auto token = encrypt(payload.SerializeAsString()); AuthToken result; result.set_token(token); diff --git a/src/lib/utils/crypto/iostreambio.cpp b/src/lib/utils/crypto/iostreambio.cpp index 65aa218c2..9026a0c4c 100644 --- a/src/lib/utils/crypto/iostreambio.cpp +++ b/src/lib/utils/crypto/iostreambio.cpp @@ -56,7 +56,7 @@ static BIO_METHOD iostream_method = bwrite, bread, NULL,//bputs - bgets,//bgets + bgets, ctrl, create, NULL,//destroy, @@ -108,40 +108,38 @@ static long ctrl(BIO *bio, int cmd, long num, void *ptr) long ret; iostream *stream = reinterpret_cast(BIO_get_data(bio)); - //LOG(Gen, Error) << "iostreambio: ctrl called, cmd: " << cmd << ", num: " << num << "." ; + LOG(Gen, Warn, "iostreambio: ctrl called, cmd: {}, num: {}", cmd, num); switch(cmd){ case BIO_CTRL_PUSH: case BIO_CTRL_POP: - ret = 0; - break; - // case BIO_CTRL_GET_CLOSE: - // ret = bio->shutdown; - // break; - // case BIO_CTRL_SET_CLOSE: - // bio->shutdown = (int)num; - // ret = 1; - // break; + ret = 0; + break; +// case BIO_CTRL_GET_CLOSE: +// ret = bio->shutdown; +// break; +// case BIO_CTRL_SET_CLOSE: +// bio->shutdown = (int)num; +// ret = 1; +// break; case BIO_CTRL_FLUSH: - /* try to flush the underlying stream */ - ret = 1; - try{ - stream->flush(); - } catch(ios_base::failure &){ - /* ignore, the badbit is checked instead */ - } - // catch(ConnectionClosedException &ex){ - // throw; - // } - if(stream->bad()){ - //LOG(Gen, Error) << "iostreambio: underlying Stream went bad while flushing." ; - ret = 0; - } - break; - default: - //LOG(Gen, Error) << "iostreambio: ctrl called with unhandled cmd: " << cmd << "." ; + /* try to flush the underlying stream */ + ret = 1; + try + { + stream->flush(); + } + catch(ios_base::failure &){/* ignore, the badbit is checked instead */} + if(stream->bad()) + { + LOG(Gen, Error, "iostreambio: underlying Stream went bad while flushing."); ret = 0; - break; + } + break; + default: + LOG(Gen, Error, "iostreambio: ctrl called with unhandled cmd: {}", cmd); + ret = 0; + break; } return ret; @@ -215,33 +213,28 @@ static int bwrite(BIO *bio, const char *buf, int len) iostream *stream = reinterpret_cast(BIO_get_data(bio)); /* write the data */ - try{ - stream->write(buf, len); - } catch(ios_base::failure &){ - /* ignore, the badbit is checked instead */ - } - // catch(ConnectionClosedException &ex){ - // throw; - // } - if(stream->bad()){ - //LOG(Gen, Error) << "iostreambio: underlying Stream went bad while writing." ; - return 0; + try + { + stream->write(buf, len); + } + catch(ios_base::failure &){/* ignore, the badbit is checked instead */} + if(stream->bad()) + { + LOG(Gen, Error, "iostreambio: underlying Stream went bad while writing."); + return 0; } /* flush the underlying stream */ - try{ - stream->flush(); - } catch(ios_base::failure &){ - /* ignore, the badbit is checked instead */ + try + { + stream->flush(); + } + catch(ios_base::failure &) {/* ignore, the badbit is checked instead */} + if(stream->bad()) + { + LOG(Gen, Error, "iostreambio: underlying Stream went bad while flushing (bwrite)."); + return 0; } - // catch(ConnectionClosedException &ex){ - // throw; - // } - if(stream->bad()){ - //LOG(Gen, Error) << "iostreambio: underlying Stream went bad while flushing (bwrite)." ; - return 0; - } - return len; } diff --git a/testing/manual/keycodes/keycode.txt b/testing/manual/keycodes/keycode.txt new file mode 100644 index 000000000..8c955d293 --- /dev/null +++ b/testing/manual/keycodes/keycode.txt @@ -0,0 +1,40 @@ +Test Author: Kevin +If it breaks: Kevin + +This will test our ability to generate and enforce keycodes. + +1) Find spark_private.pem and spark_public.pem in bts\spark\src\lib\utils\crypto copy them to your sparkd binary location + +2) Now generate a keycode to a file: + +./sparkd keycode encode -keyfile:spark_private.pem -pass:BT5u53r -users:1 -expiration:2020-05-18 > test.pem + +3) Now decode the keycode + +./sparkd keycode decode -keyfile:spark_public.pem -source:test.pem + +You should see keycode details printed to screen + +4) Now test concurrent logins. Start a sparkd. + a) Start a spark -multiple login as bts and go to where you would see some market data + b) Start another spark -multiple but log in as bts_2 and display some data + c) Start a third spark -multiple, log in as bts_2 and display some data. + + -You should see the data zero out on the first instance logged in as bts_2 and data ticking on the bts instance as well as + the second bts_2 instance + + d)Kill your sparkd + + +5) Test expiration. Generate a keycode in the past: + +./sparkd keycode encode -keyfile:spark_private.pem -pass:BT5u53r -users:1 -expiration:2018-05-18 > ~/sparkdata/conf/keycode.conf + + a) start sparkd again. + b) Start a spark -multiple login as bts_2 and go to where you would see some market data. You should NOT see data + c) Start a spark -multiple login as bts, you should see data. + d) remove the expired keycode you just generated by: + rm ~/sparkdata/conf/keycode.conf + e) restart sparkd, you should see data as normal in bts_2 + + From 95a579186725282f38540c09698f561591fa5400 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Mon, 10 Jun 2019 14:51:07 -0400 Subject: [PATCH 08/11] Jake was worried about using the same keycode for multiple servers. To combat this I look at ifconfig for an ip to appear on the list that matches what is optionally encoded in the keycode --- src/lib/config/protos/keycode.proto | 3 ++ src/lib/utils/AuthSessionManager.cpp | 64 ++++++++++++++++++++++- src/lib/utils/AuthSessionManager.h | 3 ++ src/server/sparkd/cmds/KeyCode.cpp | 76 ++++++++++++++++++++++++---- 4 files changed, 136 insertions(+), 10 deletions(-) diff --git a/src/lib/config/protos/keycode.proto b/src/lib/config/protos/keycode.proto index 93e32133f..3243e1c99 100644 --- a/src/lib/config/protos/keycode.proto +++ b/src/lib/config/protos/keycode.proto @@ -6,6 +6,9 @@ message KeyCode optional uint32 nusers = 10; optional uint64 expiration = 20; optional uint64 generation_time = 30; + + optional string ip = 40; + optional string port = 50; } message EncryptedKeyCode diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index c51f019fe..c7c567a53 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -11,6 +11,7 @@ #include "lib/utils/bts_assert.h" #include "lib/utils/helper_macros.h" #include "lib/config/config.h" +#include "lib/config/protos/Ports.pb.h" #include "lib/config/protos/keycode.pb.h" #include @@ -18,6 +19,12 @@ #include #include +//ip/port stuff +#include +#include +#include +#include + namespace bts { namespace api { namespace auth { @@ -43,9 +50,64 @@ AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() _nusers = kc.nusers(); _expiration = kc.expiration(); + + bts_assert_msg(validate_ip_port(kc), "invalid ip or port in keycode"); } - +bool AuthSessionManager::validate_ip_port(const bts::config::KeyCode& kc) +{ + bool portgood = true; + if(kc.has_port() && kc.port() != "") + { + const auto& ports = config::get(); + portgood = std::to_string(ports.config_port()) == kc.port(); + } + bool ipgood = true; + if(kc.has_ip() && kc.ip() != "") + { + struct ifaddrs* ifAddrStruct = nullptr; + struct ifaddrs* ifa = nullptr; + void * tmpAddrPtr = nullptr; + getifaddrs(&ifAddrStruct); + + for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) + { + if (!ifa->ifa_addr) + continue; + if (ifa->ifa_addr->sa_family == AF_INET) // check it is IP4 + { + // is a valid IP4 Address + tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; + char addressBuffer[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); + if(std::string(addressBuffer) == kc.ip()) + { + ipgood = true; + break; + } + else + ipgood = false; + } + else if (ifa->ifa_addr->sa_family == AF_INET6) // check it is IP6 + { + // is a valid IP6 Address + tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; + char addressBuffer[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN); + if(std::string(addressBuffer) == kc.ip()) + { + ipgood = true; + break; + } + else + ipgood = false; + } + } + if (ifAddrStruct!=NULL) + freeifaddrs(ifAddrStruct); + } + return portgood && ipgood; +} bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok) { diff --git a/src/lib/utils/AuthSessionManager.h b/src/lib/utils/AuthSessionManager.h index 8da8815ea..d353bef6f 100644 --- a/src/lib/utils/AuthSessionManager.h +++ b/src/lib/utils/AuthSessionManager.h @@ -5,6 +5,7 @@ #include namespace bts { +namespace config { class KeyCode; } namespace api { namespace auth { @@ -21,6 +22,8 @@ public: bool add_or_replace_session(const api::user::TokenPayload& tok); bool check_session(const api::user::TokenPayload& tok); +private: + bool validate_ip_port(const bts::config::KeyCode& kc); private: static const std::string PUBLIC_KEY; typedef std::map SessionMap; diff --git a/src/server/sparkd/cmds/KeyCode.cpp b/src/server/sparkd/cmds/KeyCode.cpp index 2a97c6b2f..3c8ce0323 100644 --- a/src/server/sparkd/cmds/KeyCode.cpp +++ b/src/server/sparkd/cmds/KeyCode.cpp @@ -29,6 +29,11 @@ #include #include +#include +#include +#include +#include + namespace bts { namespace cmd { @@ -42,13 +47,48 @@ std::string KeyCode::helpstr () { std::ostringstream oss; oss << "\n" - << "ENCODE -keyfile:[keyfile] -pass:password -users: -expiration:<2020-05-18>\n " + << "ENCODE -keyfile:[keyfile] -pass:password -users: -expiration:<2020-05-18> [-ip:] [-port:]\n " << "DECODE -keyfile:[keyfile] -pass:password -source:[file] \n " - << "KEYGEN -keyfile:[keyfile_prefix]\n "; + << "KEYGEN -keyfile:[keyfile_prefix]\n " + << "CHECKIPS \n "; return oss.str(); } +int checkips() +{ + struct ifaddrs * ifAddrStruct=NULL; + struct ifaddrs * ifa=NULL; + void * tmpAddrPtr=NULL; + + getifaddrs(&ifAddrStruct); + + for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) + { + if (!ifa->ifa_addr) + continue; + if (ifa->ifa_addr->sa_family == AF_INET) // check it is IP4 + { + // is a valid IP4 Address + tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; + char addressBuffer[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); + printf("%s IP Address => %s\n", ifa->ifa_name, addressBuffer); + } + else if (ifa->ifa_addr->sa_family == AF_INET6) // check it is IP6 + { + // is a valid IP6 Address + tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; + char addressBuffer[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN); + printf("%s IP Address => %s\n", ifa->ifa_name, addressBuffer); + } + } + if (ifAddrStruct!=NULL) + freeifaddrs(ifAddrStruct); + return 0; +} + std::string parse_date (std::string const &date) { auto the_date = bts::datetime::parse_iso_date_time_string(date); @@ -57,7 +97,8 @@ std::string parse_date (std::string const &date) void get_cmd(const std::vector& args, std::string& keyfile, std::string& pass, - std::string& users, std::string& expiration, std::string* source = nullptr) + std::string& users, std::string& expiration, std::string* source = nullptr, + std::string* ip = nullptr, std::string* port = nullptr) { for(size_t i = 1; i < args.size(); i++) { @@ -80,6 +121,14 @@ void get_cmd(const std::vector& args, std::string& keyfile, std::st 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); } } @@ -134,7 +183,8 @@ int keygen(const std::vector& args) // } // } -void write_keycode(const std::string& users, const std::string& expiration, bts::config::KeyCode& kc) +void write_keycode(const std::string& users, const std::string& expiration, + const std::string& ip, const std::string& port, bts::config::KeyCode& kc) { std::ostringstream out_stream; @@ -146,6 +196,9 @@ void write_keycode(const std::string& users, const std::string& expiration, bts: 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); } void eraseAllSubStr(std::string& mainStr, const std::string& toErase) @@ -160,7 +213,8 @@ void eraseAllSubStr(std::string& mainStr, const std::string& toErase) } } -int encrypt(const std::string& keyfile, const std::string& pass, const std::string& users, const std::string& expiration) +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) { std::string protobuf; @@ -177,7 +231,7 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri PrivateKeyPtr key = PrivateKey::read_file(keyfile, &cache); bts::config::KeyCode kc; - write_keycode(users, expiration, kc); + write_keycode(users, expiration, ip, port, kc); if(google::protobuf::TextFormat::PrintToString(kc, &kc_string)) { oss << kc_string; @@ -213,10 +267,10 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri int encode(const std::vector& args) { - std::string keyfile, pass, users, expiration; - get_cmd(args, keyfile, pass, users, expiration); + std::string keyfile, pass, users, expiration, ip, port; + get_cmd(args, keyfile, pass, users, expiration, nullptr, &ip, &port); - return encrypt(keyfile, pass, users, expiration); + return encrypt(keyfile, pass, users, expiration, ip, port); } int decode(const std::vector& args) @@ -273,6 +327,10 @@ int KeyCode::execute(const std::vector& args) { return decode(args); } + if(boost::to_lower_copy(args[0]) == "checkips") + { + return checkips(); + } } std::cout << help() << std::endl; return -1; From 62c7886807d11989f2f8711c5c095e74373cf637 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Mon, 10 Jun 2019 15:57:53 -0400 Subject: [PATCH 09/11] used os::host_ips() --- src/lib/utils/AuthSessionManager.cpp | 47 ++++++---------------------- src/server/sparkd/cmds/KeyCode.cpp | 35 +++------------------ 2 files changed, 14 insertions(+), 68 deletions(-) diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index c7c567a53..05ea6f422 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -65,46 +65,17 @@ bool AuthSessionManager::validate_ip_port(const bts::config::KeyCode& kc) bool ipgood = true; if(kc.has_ip() && kc.ip() != "") { - struct ifaddrs* ifAddrStruct = nullptr; - struct ifaddrs* ifa = nullptr; - void * tmpAddrPtr = nullptr; - getifaddrs(&ifAddrStruct); - - for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) + auto ipmap = bts::os::host_ips(); + for(const auto& x: ipmap) { - if (!ifa->ifa_addr) - continue; - if (ifa->ifa_addr->sa_family == AF_INET) // check it is IP4 - { - // is a valid IP4 Address - tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; - char addressBuffer[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); - if(std::string(addressBuffer) == kc.ip()) - { - ipgood = true; - break; - } - else - ipgood = false; - } - else if (ifa->ifa_addr->sa_family == AF_INET6) // check it is IP6 - { - // is a valid IP6 Address - tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; - char addressBuffer[INET6_ADDRSTRLEN]; - inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN); - if(std::string(addressBuffer) == kc.ip()) - { - ipgood = true; - break; - } - else - ipgood = false; - } + if(x.second == kc.ip()) + { + ipgood = true; + break; + } + else + ipgood = false; } - if (ifAddrStruct!=NULL) - freeifaddrs(ifAddrStruct); } return portgood && ipgood; } diff --git a/src/server/sparkd/cmds/KeyCode.cpp b/src/server/sparkd/cmds/KeyCode.cpp index 3c8ce0323..baccaa12b 100644 --- a/src/server/sparkd/cmds/KeyCode.cpp +++ b/src/server/sparkd/cmds/KeyCode.cpp @@ -7,6 +7,7 @@ #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" @@ -57,36 +58,10 @@ std::string KeyCode::helpstr () int checkips() { - struct ifaddrs * ifAddrStruct=NULL; - struct ifaddrs * ifa=NULL; - void * tmpAddrPtr=NULL; - - getifaddrs(&ifAddrStruct); - - for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) - { - if (!ifa->ifa_addr) - continue; - if (ifa->ifa_addr->sa_family == AF_INET) // check it is IP4 - { - // is a valid IP4 Address - tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; - char addressBuffer[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); - printf("%s IP Address => %s\n", ifa->ifa_name, addressBuffer); - } - else if (ifa->ifa_addr->sa_family == AF_INET6) // check it is IP6 - { - // is a valid IP6 Address - tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; - char addressBuffer[INET6_ADDRSTRLEN]; - inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN); - printf("%s IP Address => %s\n", ifa->ifa_name, addressBuffer); - } - } - if (ifAddrStruct!=NULL) - freeifaddrs(ifAddrStruct); - return 0; + 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) From c09ef31061fee5859590c3b7d148b5ad1dac3e3a Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Tue, 18 Jun 2019 15:07:08 -0400 Subject: [PATCH 10/11] Added a warning message and facility to allow concurrent logins in a keycode --- src/client/spark_api/api/ServiceContext.cs | 3 ++- src/lib/config/defaults/keycode.conf | 5 ++-- src/lib/config/protos/keycode.proto | 1 + src/lib/utils/AuthSessionManager.cpp | 21 +++++++++++----- src/lib/utils/AuthSessionManager.h | 3 ++- src/lib/utils/AuthToken.cpp | 4 +-- src/lib/utils/AuthToken.h | 2 +- src/lib/utils/datetime.cpp | 11 +++++++++ src/lib/utils/datetime.h | 1 + src/server/sparkd/cmds/KeyCode.cpp | 26 ++++++++++++-------- src/server/sparkd/user/AuthServiceServer.cpp | 6 +++-- testing/manual/keycodes/keycode.txt | 1 + 12 files changed, 59 insertions(+), 25 deletions(-) diff --git a/src/client/spark_api/api/ServiceContext.cs b/src/client/spark_api/api/ServiceContext.cs index 89d53f482..eb30f7726 100644 --- a/src/client/spark_api/api/ServiceContext.cs +++ b/src/client/spark_api/api/ServiceContext.cs @@ -48,7 +48,7 @@ namespace bts.api.core { _token = result.value.Token; user = username; user_id = result.value.UserId; - return new LoginResult(); + return new LoginResult(result.value.Reason, true); } else { return new LoginResult(result.value.Reason); @@ -89,6 +89,7 @@ namespace bts.api.core { public class LoginResult { public LoginResult() { success = true; } public LoginResult(string reason) { success = false; this.reason = reason; } + public LoginResult(string reason, bool success) { this.success = success; this.reason = reason; } public bool success { get; } public string reason { get; } } diff --git a/src/lib/config/defaults/keycode.conf b/src/lib/config/defaults/keycode.conf index 7bd2b5a67..49832fb86 100644 --- a/src/lib/config/defaults/keycode.conf +++ b/src/lib/config/defaults/keycode.conf @@ -1,8 +1,9 @@ #nusers: 1 +#allow concurrent : false #expires: 2019-Sep-30 00:00:00 637053984000000000 -#generation_time: 2019-Jun-06 19:57:36.339686 5248640496990784764 +#generation_time: 2019-Jun-18 18:25:44.333280 5248650809870720704 EncryptedKeyCode { value { - keycode: "JeCTIruFKQ79vsp7EciymRICr+Hl89z53lFT6+KZGkRRFl0+M0NiXwU9xA3pN9wDya1CfLUHIZtJ0vlH3FmJGknD+AzXeRM97teuyjRAEOxiKtoDTU/vVlPTOdsB2vTv4cdkwDn3mKdaz2uxDs8HPNyJGBCJSv30I/BN/IhfuHMNUo7AfNl0tuCduuqu+ZdMyDuKm1cGXKzORMpOWSHH+XgPleL3ms/tndCtk0eMpNS+sxZ7EHaqT6gNbnxwfDGeifDdGxWKHqmO3mPS34+VW/njdre3tXOZMVi90N9dkoAIrToV0wpRk7JPT6BcQL5+Y45bGWqFktWqH3dp2X3doQ==" + keycode: "pgtv/OmievnpmNlgxTYuN801Hbetgu7ZGkHnlGWtK+DBrKSEgKxr5lGvCW1ld/jsztkWhKHDh0iediMDjrPm4+C5qmXlt4JJlAFvKolMN8HVC59m82fECvakcqKq28WSEjf6/DuPJNP1ZwxjcmKouxx1LZ15VlNkECtNrC9pkCnifazbfQZGV3kD08zMEyw/RPmOaVLFFeav8lwI5UpcjPYBIK1rJowXdGSvm1CJViwG5Z2JbdHR39QbKjVx112d5VbVtBq9nO3i0FEKvobjN/Gzcz/5x4TgQtEShKsbmgEN+MBVL3AItQlcFzZvE/05JLmfACUFmk5Eu0a3DlLs3A==" } } \ No newline at end of file diff --git a/src/lib/config/protos/keycode.proto b/src/lib/config/protos/keycode.proto index 3243e1c99..e4301a6df 100644 --- a/src/lib/config/protos/keycode.proto +++ b/src/lib/config/protos/keycode.proto @@ -9,6 +9,7 @@ message KeyCode optional string ip = 40; optional string port = 50; + optional bool allow_concurrent = 60; } message EncryptedKeyCode diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index 05ea6f422..cefcc24ff 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -40,7 +40,7 @@ const std::string AuthSessionManager::PUBLIC_KEY = "3QIDAQAB" "\n-----END PUBLIC KEY-----\n"; -AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() +AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration(), _concurrent(false) { const auto& ekc = config::get(); auto pub = bts::crypto::PublicKey::read_string(PUBLIC_KEY); @@ -50,7 +50,7 @@ AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() _nusers = kc.nusers(); _expiration = kc.expiration(); - + _concurrent = kc.allow_concurrent(); bts_assert_msg(validate_ip_port(kc), "invalid ip or port in keycode"); } @@ -80,15 +80,23 @@ bool AuthSessionManager::validate_ip_port(const bts::config::KeyCode& kc) return portgood && ipgood; } -bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok) +bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& tok, std::string* msg) { if(tok.wheel()) return true; if(datetime::ll_diff_in_us(datetime::ll_now(), _expiration) < 0) { LOG(Gen,Error, "Your BTS Keycode is expired!"); + *msg = "Your BTS Keycode is EXPIRED! Please contact sales@bluetradesys.com"; return false; - } + } + if(datetime::ll_diff_in_seconds(tok.date(), _expiration) < 86400) //1d + *msg = "*** YOUR BTS KEYCODE WILL EXPIRE TOMORROW!!, PLEASE CONTACT sales@bluetradesys.com ***"; + else if(datetime::ll_diff_in_seconds(tok.date(), _expiration) < 259200) //3d + *msg = "Your BTS Keycode EXPIRES IN LESS THAN 3 DAYS, please contact sales@bluetradesys.com"; + else if(datetime::ll_diff_in_seconds(tok.date(), _expiration) < 432000) //5d + *msg = "Your BTS Keycode expires in less than one week, please contact sales@bluetradesys.com"; + auto it = _map.find(tok.id()); if(it != _map.end()) { @@ -113,8 +121,9 @@ bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) if(it != _map.end()) { //return google::protobuf::util::MessageDifferencer::Equivalent(tok, it->second.token) //this is probably slow - return tok.date() == it->second.token.date() && //ids are equal - datetime::ll_diff_in_us(tok.date(), _expiration) > 0; + bool not_expired = datetime::ll_diff_in_us(tok.date(), _expiration) > 0; + bool same_token = _concurrent || (tok.date() == it->second.token.date()); + return not_expired && same_token; } } return false; diff --git a/src/lib/utils/AuthSessionManager.h b/src/lib/utils/AuthSessionManager.h index d353bef6f..08e9d03cd 100644 --- a/src/lib/utils/AuthSessionManager.h +++ b/src/lib/utils/AuthSessionManager.h @@ -19,7 +19,7 @@ class AuthSessionManager public: AuthSessionManager(); - bool add_or_replace_session(const api::user::TokenPayload& tok); + bool add_or_replace_session(const api::user::TokenPayload& tok, std::string* msg = nullptr); bool check_session(const api::user::TokenPayload& tok); private: @@ -30,6 +30,7 @@ private: SessionMap _map; uint32_t _nusers; datetime::LLDateTime _expiration; + bool _concurrent; }; diff --git a/src/lib/utils/AuthToken.cpp b/src/lib/utils/AuthToken.cpp index 5b4be4dbd..e1292b595 100644 --- a/src/lib/utils/AuthToken.cpp +++ b/src/lib/utils/AuthToken.cpp @@ -51,14 +51,14 @@ AuthSessionManager& session_manager() return mgr; } -AuthToken generate_token(const admin::BTSUser& user) +AuthToken generate_token(const admin::BTSUser& user, std::string* msg) { api::user::TokenPayload payload; payload.set_date(datetime::ll_now()); payload.set_id(user.id()); if(user.name() == "bts") payload.set_wheel(true); - session_manager().add_or_replace_session(payload); + session_manager().add_or_replace_session(payload, msg); auto token = encrypt(payload.SerializeAsString()); bts_assert_d(token.size() < 255-8); AuthToken result; diff --git a/src/lib/utils/AuthToken.h b/src/lib/utils/AuthToken.h index 71b1c4da0..a85605a0b 100644 --- a/src/lib/utils/AuthToken.h +++ b/src/lib/utils/AuthToken.h @@ -7,7 +7,7 @@ namespace bts { namespace api { namespace auth { -AuthToken generate_token(const admin::BTSUser&); +AuthToken generate_token(const admin::BTSUser&, std::string* msg = nullptr); AuthToken generate_wheel_token(); bool decode_token(const api::AuthToken& token, user::TokenPayload* payload); diff --git a/src/lib/utils/datetime.cpp b/src/lib/utils/datetime.cpp index 4e9d52b3d..9fb4db93f 100755 --- a/src/lib/utils/datetime.cpp +++ b/src/lib/utils/datetime.cpp @@ -61,6 +61,17 @@ long long ll_diff_in_us (unsigned long long ll1, unsigned long long ll2) #endif } +long long ll_diff_in_seconds(unsigned long long ll1, unsigned long long ll2) +{ +#ifndef NO_CPP0X + auto p1 = ll_to_ptime (ll1); + auto p2 = ll_to_ptime (ll2); // NOLINT + return (p2 - p1).total_seconds(); +#else + return 0; +#endif +} + long long ptime_diff_in_us (PTime const &t1, PTime const &t2) { diff --git a/src/lib/utils/datetime.h b/src/lib/utils/datetime.h index b9ba82dca..62384ed69 100755 --- a/src/lib/utils/datetime.h +++ b/src/lib/utils/datetime.h @@ -51,6 +51,7 @@ BTS_EXPORT LLDateTime ptime_to_ll (PTime const &t); BTS_EXPORT PTime ll_to_ptime (LLDateTime ll); extern "C" BTS_EXPORT int ll_to_cstr (LLDateTime ll, char*buffer, int buffer_sz); BTS_EXPORT long long ll_diff_in_us (unsigned long long start, unsigned long long stop); +BTS_EXPORT long long ll_diff_in_seconds(unsigned long long start, unsigned long long stop); BTS_EXPORT long long ptime_diff_in_us (PTime const &t1, PTime const &t2); BTS_EXPORT long long ptime_diff_in_ms (PTime const &t1, PTime const &t2); BTS_EXPORT long long ptime_diff_in_secs (PTime const &t1, PTime const &t2); diff --git a/src/server/sparkd/cmds/KeyCode.cpp b/src/server/sparkd/cmds/KeyCode.cpp index baccaa12b..380370659 100644 --- a/src/server/sparkd/cmds/KeyCode.cpp +++ b/src/server/sparkd/cmds/KeyCode.cpp @@ -48,7 +48,7 @@ std::string KeyCode::helpstr () { std::ostringstream oss; oss << "\n" - << "ENCODE -keyfile:[keyfile] -pass:password -users: -expiration:<2020-05-18> [-ip:] [-port:]\n " + << "ENCODE -keyfile:[keyfile] -pass:password -users: -expiration:<2020-05-18> [-ip:] [-port:] [-allow_concurrent]\n " << "DECODE -keyfile:[keyfile] -pass:password -source:[file] \n " << "KEYGEN -keyfile:[keyfile_prefix]\n " << "CHECKIPS \n "; @@ -73,7 +73,7 @@ std::string parse_date (std::string const &date) void get_cmd(const std::vector& 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) + std::string* ip = nullptr, std::string* port = nullptr, bool* allow_concurrent = nullptr) { for(size_t i = 1; i < args.size(); i++) { @@ -103,7 +103,11 @@ void get_cmd(const std::vector& args, std::string& keyfile, std::st size_t p_pos = args[i].find("-port:"); if(port && p_pos != args[i].npos) - *port = args[i].substr(p_pos+6); + *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; } } @@ -159,7 +163,7 @@ int keygen(const std::vector& args) // } void write_keycode(const std::string& users, const std::string& expiration, - const std::string& ip, const std::string& port, bts::config::KeyCode& kc) + const std::string& ip, const std::string& port, bool concurrent, bts::config::KeyCode& kc) { std::ostringstream out_stream; @@ -174,6 +178,7 @@ void write_keycode(const std::string& users, const std::string& expiration, 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) @@ -189,7 +194,7 @@ void eraseAllSubStr(std::string& mainStr, const std::string& toErase) } 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) + const std::string& expiration, const std::string& ip, const std::string& port, bool concurrent) { std::string protobuf; @@ -206,7 +211,7 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri PrivateKeyPtr key = PrivateKey::read_file(keyfile, &cache); bts::config::KeyCode kc; - write_keycode(users, expiration, ip, port, kc); + write_keycode(users, expiration, ip, port, concurrent, kc); if(google::protobuf::TextFormat::PrintToString(kc, &kc_string)) { oss << kc_string; @@ -227,7 +232,8 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri google::protobuf::TextFormat::PrintToString(ekc, &pb); std::cout << std::endl << std::endl; 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 << "#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; @@ -242,10 +248,10 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri int encode(const std::vector& args) { - std::string keyfile, pass, users, expiration, ip, port; - get_cmd(args, keyfile, pass, users, expiration, nullptr, &ip, &port); + 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); + return encrypt(keyfile, pass, users, expiration, ip, port, concurrent); } int decode(const std::vector& args) diff --git a/src/server/sparkd/user/AuthServiceServer.cpp b/src/server/sparkd/user/AuthServiceServer.cpp index 13c05abf7..357a683c9 100644 --- a/src/server/sparkd/user/AuthServiceServer.cpp +++ b/src/server/sparkd/user/AuthServiceServer.cpp @@ -63,7 +63,8 @@ void AuthServiceServer::authenticate_login(const api::user::AuthenticateLoginReq if (admin_data.success()) { for (const auto& user : admin_data.value()->users()) { if (user.name() == req.login() && user.password() == req.password() && user.is_present()) { - resp.mutable_token()->CopyFrom(api::auth::generate_token(user)); + std::string msg; + resp.mutable_token()->CopyFrom(api::auth::generate_token(user, &msg)); api::user::TokenPayload payload; api::auth::decode_token(resp.token(), &payload); @@ -72,7 +73,8 @@ void AuthServiceServer::authenticate_login(const api::user::AuthenticateLoginReq if(!resp.is_valid()) resp.set_reason("User login privledges restricted, please contact your risk manager"); resp.set_user_id(user.id()); - + if(msg != "") + resp.set_reason(msg); respond(resp); return; } diff --git a/testing/manual/keycodes/keycode.txt b/testing/manual/keycodes/keycode.txt index 8c955d293..ffd573a26 100644 --- a/testing/manual/keycodes/keycode.txt +++ b/testing/manual/keycodes/keycode.txt @@ -25,6 +25,7 @@ You should see keycode details printed to screen d)Kill your sparkd +4.5) For extra credit generate a keycode with -allow_concurrent and make sure we don't zero the market data for concurrent logins 5) Test expiration. Generate a keycode in the past: From 914ec09b4f082345aaafcacaf881b969046c3f82 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Tue, 18 Jun 2019 16:44:54 -0400 Subject: [PATCH 11/11] fix auth tests --- src/lib/utils/AuthSessionManager.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index cefcc24ff..11d620282 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -87,15 +87,19 @@ bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& t if(datetime::ll_diff_in_us(datetime::ll_now(), _expiration) < 0) { LOG(Gen,Error, "Your BTS Keycode is expired!"); - *msg = "Your BTS Keycode is EXPIRED! Please contact sales@bluetradesys.com"; + if(msg) + *msg = "Your BTS Keycode is EXPIRED! Please contact sales@bluetradesys.com"; return false; } - if(datetime::ll_diff_in_seconds(tok.date(), _expiration) < 86400) //1d - *msg = "*** YOUR BTS KEYCODE WILL EXPIRE TOMORROW!!, PLEASE CONTACT sales@bluetradesys.com ***"; - else if(datetime::ll_diff_in_seconds(tok.date(), _expiration) < 259200) //3d - *msg = "Your BTS Keycode EXPIRES IN LESS THAN 3 DAYS, please contact sales@bluetradesys.com"; - else if(datetime::ll_diff_in_seconds(tok.date(), _expiration) < 432000) //5d - *msg = "Your BTS Keycode expires in less than one week, please contact sales@bluetradesys.com"; + if(msg) + { + if(datetime::ll_diff_in_seconds(tok.date(), _expiration) < 86400) //1d + *msg = "*** YOUR BTS KEYCODE WILL EXPIRE TOMORROW!!, PLEASE CONTACT sales@bluetradesys.com ***"; + else if(datetime::ll_diff_in_seconds(tok.date(), _expiration) < 259200) //3d + *msg = "Your BTS Keycode EXPIRES IN LESS THAN 3 DAYS, please contact sales@bluetradesys.com"; + else if(datetime::ll_diff_in_seconds(tok.date(), _expiration) < 432000) //5d + *msg = "Your BTS Keycode expires in less than one week, please contact sales@bluetradesys.com"; + } auto it = _map.find(tok.id()); if(it != _map.end())