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);