Merge branch 'master' of bitbucket.org:bluetradesys/spark
This commit is contained in:
commit
aa5b88e1a2
37 files changed
+1947
-22
No files matched your search
@@ -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; }
|
||||
}
|
||||
|
||||
@@ -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/tests/SingleWriterConcurrentVectorTests.cpp;utils/thread_manager.cpp)
|
||||
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;utLine truncated
|
||||
@@ -27,7 +27,7 @@ message User {
|
||||
}
|
||||
|
||||
message TokenPayload {
|
||||
uint32 date = 1;
|
||||
uint64 date = 1;
|
||||
uint32 id = 2;
|
||||
bool wheel = 3;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#nusers: 1
|
||||
#allow concurrent : false
|
||||
#expires: 2019-Sep-30 00:00:00 637053984000000000
|
||||
#generation_time: 2019-Jun-18 18:25:44.333280 5248650809870720704
|
||||
EncryptedKeyCode {
|
||||
value {
|
||||
keycode: "pgtv/OmievnpmNlgxTYuN801Hbetgu7ZGkHnlGWtK+DBrKSEgKxr5lGvCW1ld/jsztkWhKHDh0iediMDjrPm4+C5qmXlt4JJlAFvKolMN8HVC59m82fECvakcqKq28WSEjf6/DuPJNP1ZwxjcmKouxx1LZ15VlNkECtNrC9pkCnifazbfQZGV3kD08zMEyw/RPmOaVLFFeav8lwI5UpcjPYBIK1rJowXdGSvm1CJViwG5Z2JbdHR39QbKjVx112d5VbVtBq9nO3i0FEKvobjN/Gzcz/5x4TgQtEShKsbmgEN+MBVL3AItQlcFzZvE/05JLmfACUFmk5Eu0a3DlLs3A=="
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
syntax='proto2';
|
||||
package bts.config;
|
||||
|
||||
message KeyCode
|
||||
{
|
||||
optional uint32 nusers = 10;
|
||||
optional uint64 expiration = 20;
|
||||
optional uint64 generation_time = 30;
|
||||
|
||||
optional string ip = 40;
|
||||
optional string port = 50;
|
||||
optional bool allow_concurrent = 60;
|
||||
}
|
||||
|
||||
message EncryptedKeyCode
|
||||
{
|
||||
optional string keycode = 10;
|
||||
}
|
||||
|
||||
message PublicKey
|
||||
{
|
||||
optional string key = 10;
|
||||
}
|
||||
@@ -246,7 +246,24 @@ public:
|
||||
pupd->mutable_security()->CopyFrom(handle.get_normalized_security());
|
||||
snapshot = true;
|
||||
}
|
||||
if (entitled) {
|
||||
//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) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
#ifndef _WIN32
|
||||
#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/Ports.pb.h"
|
||||
#include "lib/config/protos/keycode.pb.h"
|
||||
|
||||
#include <google/protobuf/text_format.h>
|
||||
#include <google/protobuf/util/message_differencer.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
//ip/port stuff
|
||||
#include <sys/types.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
namespace bts {
|
||||
namespace api {
|
||||
namespace auth {
|
||||
|
||||
const std::string AuthSessionManager::PUBLIC_KEY =
|
||||
"-----BEGIN PUBLIC KEY-----\n"
|
||||
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAseguRgLD88QdhK0agy8f"
|
||||
"gz3SXp/hmp377+Q+z3OQqGG5kWyKnQfkAQDFeSv8o1iwSQupN0D1QOXyDnV1jmue"
|
||||
"rqTitasGeq4AwKLhY89O/VuXc+URAO+HZ7EqVWfb3Nv4EOZRiiVsxmVBkld73Rcy"
|
||||
"UAS4OXh4jDtE8IzOoXB7HhlMPBIAkueqdyy/v9zamzKrxPzWd+IW/IYEdCrUS8ES"
|
||||
"mxOkeadSHVeH2nlHVREdktE3wKRd3fZ3OkyGaHnUPxBoUW1Ex57BpwdMGUqLZ3W2"
|
||||
"92yQa/32dsLocoTri4iiImvQ/0JvfhnY9MDTZnmIgb8bPWSvPblQN0RR9OkRDFT9"
|
||||
"3QIDAQAB"
|
||||
"\n-----END PUBLIC KEY-----\n";
|
||||
|
||||
AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration(), _concurrent(false)
|
||||
{
|
||||
const auto& ekc = config::get<config::EncryptedKeyCode>();
|
||||
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();
|
||||
_concurrent = kc.allow_concurrent();
|
||||
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<config::Ports>();
|
||||
portgood = std::to_string(ports.config_port()) == kc.port();
|
||||
}
|
||||
bool ipgood = true;
|
||||
if(kc.has_ip() && kc.ip() != "")
|
||||
{
|
||||
auto ipmap = bts::os::host_ips();
|
||||
for(const auto& x: ipmap)
|
||||
{
|
||||
if(x.second == kc.ip())
|
||||
{
|
||||
ipgood = true;
|
||||
break;
|
||||
}
|
||||
else
|
||||
ipgood = false;
|
||||
}
|
||||
}
|
||||
return portgood && ipgood;
|
||||
}
|
||||
|
||||
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!");
|
||||
if(msg)
|
||||
*msg = "Your BTS Keycode is EXPIRED! Please contact sales@bluetradesys.com";
|
||||
return false;
|
||||
}
|
||||
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())
|
||||
{
|
||||
_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(tok.wheel())
|
||||
return true;
|
||||
if(datetime::ll_diff_in_us(datetime::ll_now(), _expiration) > 0)
|
||||
{
|
||||
auto it = _map.find(tok.id());
|
||||
if(it != _map.end())
|
||||
{
|
||||
//return google::protobuf::util::MessageDifferencer::Equivalent(tok, it->second.token) //this is probably slow
|
||||
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;
|
||||
}
|
||||
|
||||
}}}
|
||||
#endif
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
#include "lib/api/services/AuthService.pb.h"
|
||||
#include "lib/api/services/AdminService.client.pb.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
#include <string>
|
||||
|
||||
namespace bts {
|
||||
namespace config { class KeyCode; }
|
||||
namespace api {
|
||||
namespace auth {
|
||||
|
||||
struct Session
|
||||
{
|
||||
api::user::TokenPayload token;
|
||||
};
|
||||
|
||||
class AuthSessionManager
|
||||
{
|
||||
public:
|
||||
AuthSessionManager();
|
||||
|
||||
bool add_or_replace_session(const api::user::TokenPayload& tok, std::string* msg = nullptr);
|
||||
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<uint32_t, Session> SessionMap;
|
||||
SessionMap _map;
|
||||
uint32_t _nusers;
|
||||
datetime::LLDateTime _expiration;
|
||||
bool _concurrent;
|
||||
};
|
||||
|
||||
|
||||
|
||||
}}}
|
||||
+27
-14
@@ -5,6 +5,8 @@
|
||||
#include "datetime.h"
|
||||
#include "base64.hpp"
|
||||
|
||||
#include "AuthSessionManager.h"
|
||||
|
||||
// cryptopp
|
||||
#include <cryptopp/osrng.h>//temp
|
||||
#include <cryptopp/cryptlib.h>
|
||||
@@ -13,6 +15,7 @@
|
||||
#include <cryptopp/twofish.h>
|
||||
#include <cryptopp/eax.h>
|
||||
#include <cryptopp/secblock.h>
|
||||
#include <map>
|
||||
|
||||
namespace bts {
|
||||
namespace api {
|
||||
@@ -42,20 +45,29 @@ 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, std::string* msg)
|
||||
{
|
||||
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, msg);
|
||||
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);
|
||||
@@ -65,22 +77,23 @@ AuthToken generate_wheel_token() {
|
||||
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 {
|
||||
|
||||
@@ -7,10 +7,9 @@ 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);
|
||||
bool is_expired(const user::TokenPayload&);
|
||||
|
||||
}}}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
namespace bts
|
||||
{
|
||||
namespace crypto
|
||||
{
|
||||
|
||||
class AlgoType
|
||||
{
|
||||
public:
|
||||
enum type
|
||||
{
|
||||
RSA = 0
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "CipherType.h"
|
||||
|
||||
namespace bts
|
||||
{
|
||||
namespace crypto
|
||||
{
|
||||
|
||||
const char* CipherType::name[] = { "None", "DES-EDE3-CBC", "3des", "idea", "aes128" };
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
#ifndef _WIN32
|
||||
#include "CryptoInit.h"
|
||||
|
||||
#include <tbb/spin_mutex.h>
|
||||
|
||||
namespace openssl
|
||||
{
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/pem.h>
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
init = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
#ifndef _WIN32
|
||||
#include "Digest.h"
|
||||
|
||||
#include "PrivateKey.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
|
||||
namespace openssl
|
||||
{
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/pem.h>
|
||||
}
|
||||
|
||||
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<const unsigned char*>(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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/utils/bts_no_copy.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef _WIN32
|
||||
#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 <google/protobuf/text_format.h>
|
||||
#include <google/protobuf/io/zero_copy_stream_impl.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
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 << "-----BEGIN BTS-----\n" + kc.keycode() + "\n-----END BTS-----\n";
|
||||
if(public_key.decrypt(kss, out))
|
||||
{
|
||||
bts::config::KeyCode kc;
|
||||
google::protobuf::io::IstreamInputStream kc_stream(&out);
|
||||
google::protobuf::TextFormat::Parse(&kc_stream, &kc);
|
||||
if(dk)
|
||||
{
|
||||
*dk = kc;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}}
|
||||
#endif
|
||||
@@ -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);
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
#include "PasswordCallback.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/utils/bts_no_copy.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,298 @@
|
||||
#ifndef _WIN32
|
||||
#include "PrivateKey.h"
|
||||
|
||||
#include "PublicKey.h"
|
||||
#include "CryptoInit.h"
|
||||
#include "iostreambio.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
#include <tbb/spin_mutex.h>
|
||||
#include <cstring>
|
||||
|
||||
namespace openssl
|
||||
{
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/pem.h>
|
||||
}
|
||||
|
||||
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<PasswordCallback*>(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(Gen, Error, "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<std::istream&>(stream).seekg(0, std::ios::end);
|
||||
int length = const_cast<std::istream&>(stream).tellg();
|
||||
const_cast<std::istream&>(stream).seekg(0, std::ios::beg);
|
||||
|
||||
char* data = new char[length];
|
||||
const_cast<std::istream&>(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(Gen, Error, "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)
|
||||
{
|
||||
UNUSED(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<std::iostream*>(&in_dat));
|
||||
if(!encrypt)
|
||||
{
|
||||
auto bio = biostream.getBIO();
|
||||
i = openssl::PEM_read_bio(bio, &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<std::iostream*>(&out_data));
|
||||
if(encrypt)
|
||||
i = openssl::PEM_write_bio(biostream_out.getBIO(), header_name.c_str(), const_cast<char*>(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);
|
||||
auto rsa = openssl::EVP_PKEY_get0_RSA(pk);
|
||||
if(out == 0)
|
||||
{
|
||||
*outlen = openssl::RSA_size(rsa);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return RSA_private_encrypt(inlen, const_cast<unsigned char *>(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(rsa);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int i = RSA_public_decrypt(inlen, const_cast<unsigned char *>(in), out, 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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/utils/bts_no_copy.h"
|
||||
|
||||
#include "AlgoType.h"
|
||||
#include "CipherType.h"
|
||||
#include "PasswordCallback.h"
|
||||
#include <memory>
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
namespace openssl
|
||||
{
|
||||
struct evp_pkey_st;
|
||||
typedef struct 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<PrivateKey> PrivateKeyPtr;
|
||||
typedef std::shared_ptr<PublicKey> 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<Pimp> me;
|
||||
|
||||
openssl::EVP_PKEY* _key;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
#ifndef _WIN32
|
||||
#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 <sstream>
|
||||
|
||||
namespace openssl
|
||||
{
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/pem.h>
|
||||
}
|
||||
|
||||
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(Gen, Error, "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(Gen, Error, "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(Gen, Error, "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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -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*);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
#ifndef _WIN32
|
||||
#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 <openssl/bio.h>
|
||||
#include <openssl/err.h>
|
||||
}
|
||||
|
||||
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 *);
|
||||
|
||||
#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);
|
||||
BIO_meth_set_gets(iostream_method, bgets);
|
||||
}
|
||||
return iostream_method;
|
||||
}
|
||||
#else
|
||||
static BIO_METHOD iostream_method =
|
||||
{
|
||||
iostreambio::type,
|
||||
iostreambio::name,
|
||||
bwrite,
|
||||
bread,
|
||||
NULL,//bputs
|
||||
bgets,
|
||||
ctrl,
|
||||
create,
|
||||
NULL,//destroy,
|
||||
NULL//callback_ctrl
|
||||
};
|
||||
BIO_METHOD * BIO_iostream_method()
|
||||
{
|
||||
return &iostream_method;
|
||||
}
|
||||
#endif
|
||||
|
||||
iostreambio::iostreambio(iostream *stream) : _bio(0)
|
||||
{
|
||||
_bio = BIO_new(BIO_iostream_method());
|
||||
bts_assert(_bio);
|
||||
BIO_set_data(_bio, (void *) stream);
|
||||
}
|
||||
|
||||
BIO* iostreambio::getBIO()
|
||||
{
|
||||
return _bio;
|
||||
}
|
||||
|
||||
static int create(BIO *bio)
|
||||
{
|
||||
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
|
||||
*
|
||||
* => -1 means the caller can retry, 0: retry is useless
|
||||
* it is set to 0 since the underlying stream is blocking
|
||||
*/
|
||||
bio->num= 0;
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static long ctrl(BIO *bio, int cmd, long num, void *ptr)
|
||||
{
|
||||
UNUSED(ptr);
|
||||
UNUSED(num);
|
||||
long ret;
|
||||
iostream *stream = reinterpret_cast<iostream*>(BIO_get_data(bio));
|
||||
|
||||
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;
|
||||
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 */}
|
||||
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);
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int bread(BIO *bio, char *buf, int len)
|
||||
{
|
||||
iostream *stream = reinterpret_cast<iostream*>(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();
|
||||
}
|
||||
num_bytes = i;
|
||||
}
|
||||
catch(ios_base::failure &)
|
||||
{
|
||||
}
|
||||
return num_bytes;
|
||||
}
|
||||
|
||||
static int bgets(BIO *bio, char *buf, int len)
|
||||
{
|
||||
iostream *stream = reinterpret_cast<iostream*>(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)
|
||||
{
|
||||
if(len == 0){
|
||||
return 0;
|
||||
}
|
||||
iostream *stream = reinterpret_cast<iostream*>(BIO_get_data(bio));
|
||||
|
||||
/* write the data */
|
||||
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 */}
|
||||
if(stream->bad())
|
||||
{
|
||||
LOG(Gen, Error, "iostreambio: underlying Stream went bad while flushing (bwrite).");
|
||||
return 0;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
namespace openssl
|
||||
{
|
||||
struct bio_st;
|
||||
typedef struct bio_st BIO;
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef OPENSSL_COMPAT_H
|
||||
#define OPENSSL_COMPAT_H
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/hmac.h>
|
||||
|
||||
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 */
|
||||
@@ -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-----
|
||||
@@ -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-----
|
||||
@@ -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)
|
||||
{
|
||||
@@ -178,11 +189,27 @@ 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);
|
||||
}
|
||||
|
||||
PTime parse_simple_date_time_string (std::string const &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)
|
||||
{
|
||||
std::string result (strlen ("20121204-14:44:02"),' ');
|
||||
|
||||
@@ -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);
|
||||
@@ -61,7 +62,9 @@ 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);
|
||||
BTS_EXPORT PTime parse_FIX_timestamp (const std::string &ts);
|
||||
BTS_EXPORT std::string FIX_timestamp (PTime const &t);
|
||||
|
||||
@@ -0,0 +1,327 @@
|
||||
#include "KeyCode.h"
|
||||
|
||||
#include "lib/utils/AuthToken.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
#include "lib/utils/crypto/CryptoInit.h"
|
||||
#include "lib/utils/crypto/PrivateKey.h"
|
||||
#include "lib/utils/crypto/PublicKey.h"
|
||||
#include "lib/utils/crypto/KeyCodeHelper.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/helper_macros.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/keycode.pb.h"
|
||||
|
||||
#include <boost/date_time/posix_time/ptime.hpp>
|
||||
#include <boost/date_time/local_time/local_time.hpp>
|
||||
#include <boost/tokenizer.hpp>
|
||||
#include <boost/date_time/gregorian/gregorian.hpp>
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
#include <google/protobuf/text_format.h>
|
||||
#include <google/protobuf/io/zero_copy_stream_impl.h>
|
||||
#include <google/protobuf/timestamp.pb.h>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <sstream>
|
||||
#include <streambuf>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
namespace bts {
|
||||
namespace cmd {
|
||||
|
||||
using namespace std;
|
||||
using namespace datetime;
|
||||
using namespace bts::api::auth;
|
||||
using namespace bts::crypto;
|
||||
using namespace bts::config;
|
||||
|
||||
std::string KeyCode::helpstr ()
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "\n"
|
||||
<< "ENCODE -keyfile:[keyfile] -pass:password -users:<nusers> -expiration:<2020-05-18> [-ip:<ip_address>] [-port:<spark_port>] [-allow_concurrent]\n "
|
||||
<< "DECODE -keyfile:[keyfile] -pass:password -source:[file] \n "
|
||||
<< "KEYGEN -keyfile:[keyfile_prefix]\n "
|
||||
<< "CHECKIPS \n ";
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
int checkips()
|
||||
{
|
||||
auto ipmap = bts::os::host_ips();
|
||||
for(const auto& x: ipmap)
|
||||
std::cout << x.first << " => " << x.second << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string parse_date (std::string const &date)
|
||||
{
|
||||
auto the_date = bts::datetime::parse_iso_date_time_string(date);
|
||||
return bts::datetime::iso_date_time_string(the_date);
|
||||
}
|
||||
|
||||
|
||||
void get_cmd(const std::vector<std::string>& args, std::string& keyfile, std::string& pass,
|
||||
std::string& users, std::string& expiration, std::string* source = nullptr,
|
||||
std::string* ip = nullptr, std::string* port = nullptr, bool* allow_concurrent = nullptr)
|
||||
{
|
||||
for(size_t i = 1; i < args.size(); i++)
|
||||
{
|
||||
size_t pass_pos = args[i].find("-pass:");
|
||||
if(pass_pos != args[i].npos)
|
||||
pass = args[i].substr(pass_pos+6);
|
||||
|
||||
size_t file_pos = args[i].find("-keyfile:");
|
||||
if(file_pos != args[i].npos)
|
||||
keyfile = args[i].substr(file_pos+9);
|
||||
|
||||
size_t u_pos = args[i].find("-users:");
|
||||
if(u_pos != args[i].npos)
|
||||
users = args[i].substr(u_pos+7);
|
||||
|
||||
size_t e_pos = args[i].find("-expiration:");
|
||||
if(e_pos != args[i].npos)
|
||||
expiration = parse_date (args[i].substr(e_pos+12));
|
||||
|
||||
size_t pay_pos = args[i].find("-source:");
|
||||
if(source && pay_pos != args[i].npos)
|
||||
*source = args[i].substr(pay_pos+8);
|
||||
|
||||
size_t i_pos = args[i].find("-ip:");
|
||||
if(ip && i_pos != args[i].npos)
|
||||
*ip = args[i].substr(i_pos+4);
|
||||
|
||||
size_t p_pos = args[i].find("-port:");
|
||||
if(port && p_pos != args[i].npos)
|
||||
*port = args[i].substr(p_pos+6);
|
||||
|
||||
size_t c_pos = args[i].find("-allow_concurrent");
|
||||
if(allow_concurrent && c_pos != args[i].npos)
|
||||
*allow_concurrent = true;
|
||||
}
|
||||
}
|
||||
|
||||
int keygen(const std::vector<std::string>& args)
|
||||
{
|
||||
std::string keyfile, pass, users, expiration;
|
||||
get_cmd(args, keyfile, pass, users, expiration);
|
||||
if(keyfile != "")
|
||||
{
|
||||
PrivateKeyPtr priv = PrivateKey::create(2048, AlgoType::RSA);
|
||||
|
||||
std::string n1 = (keyfile + "_private.pem"), n2 = (keyfile + "_public.pem");
|
||||
//I can't get anything but DES to work here.
|
||||
priv->write_file(n1, CipherType::DES);
|
||||
priv->public_key()->write_file(n2);
|
||||
|
||||
std::cout << "Key pair generated " << n1 << " and " << n2 << std::endl;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << KeyCode::helpstr();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// int Logon::digest(const std::vector<std::string>& args)
|
||||
// {
|
||||
// std::string pass, keyfile, payload, user, expiration, extra_data;
|
||||
// get_cmd(args, pass, keyfile, payload, user, expiration, extra_data);
|
||||
// if(extra_data != "")
|
||||
// {
|
||||
// unsigned int len;
|
||||
// //unsigned char* digest;
|
||||
// //Digest::allocate_array(&digest, &len);
|
||||
// //Digest::compute_digest(reinterpret_cast<const unsigned char*>(payload.c_str()), payload.size(), digest, &len);
|
||||
|
||||
// std::string digest = Digest::compute_digest(extra_data);
|
||||
// len = digest.size();
|
||||
// UNUSED(len);
|
||||
// std::cout << std::endl << std::endl;
|
||||
// //for(size_t i = 0; i < len; i++)
|
||||
// // printf("%02x", digest[i]);
|
||||
// std::cout << digest;
|
||||
// std::cout << std::endl << std::endl;
|
||||
// return 0;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// std::cout << help();
|
||||
// return -1;
|
||||
// }
|
||||
// }
|
||||
|
||||
void write_keycode(const std::string& users, const std::string& expiration,
|
||||
const std::string& ip, const std::string& port, bool concurrent, bts::config::KeyCode& kc)
|
||||
{
|
||||
std::ostringstream out_stream;
|
||||
|
||||
auto nusers = (uint32_t) std::atoi(users.c_str());
|
||||
auto expir = bts::datetime::parse_iso_date_time_string(expiration);
|
||||
auto expir_ll = bts::datetime::ptime_to_ll(expir);
|
||||
auto now = bts::datetime::ll_now();
|
||||
|
||||
kc.set_nusers(nusers);
|
||||
kc.set_expiration(expir_ll);
|
||||
kc.set_generation_time(now);
|
||||
bts_assert_msg(ip != "127.0.0.1", "you can't generate a keycode for loopback, just leave the parameter blank");
|
||||
kc.set_ip(ip);
|
||||
kc.set_port(port);
|
||||
kc.set_allow_concurrent(concurrent);
|
||||
}
|
||||
|
||||
void eraseAllSubStr(std::string& mainStr, const std::string& toErase)
|
||||
{
|
||||
size_t pos = std::string::npos;
|
||||
|
||||
// Search for the substring in string in a loop untill nothing is found
|
||||
while ((pos = mainStr.find(toErase) )!= std::string::npos)
|
||||
{
|
||||
// If found then erase it from string
|
||||
mainStr.erase(pos, toErase.length());
|
||||
}
|
||||
}
|
||||
|
||||
int encrypt(const std::string& keyfile, const std::string& pass, const std::string& users,
|
||||
const std::string& expiration, const std::string& ip, const std::string& port, bool concurrent)
|
||||
{
|
||||
std::string protobuf;
|
||||
|
||||
if(keyfile == "")
|
||||
{
|
||||
std::cout << KeyCode::helpstr();
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string kc_string;
|
||||
std::stringstream oss, encrypted;
|
||||
CachedPassword cache(pass);
|
||||
PrivateKeyPtr key = PrivateKey::read_file(keyfile, &cache);
|
||||
|
||||
bts::config::KeyCode kc;
|
||||
write_keycode(users, expiration, ip, port, concurrent, kc);
|
||||
if(google::protobuf::TextFormat::PrintToString(kc, &kc_string))
|
||||
{
|
||||
oss << kc_string;
|
||||
key->encrypt(oss, encrypted);
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
//int ret = openssl::RSA_private_encrypt(sz-1, pay, &buffer[0], key->pkey.rsa, RSA_PKCS1_PADDING);
|
||||
|
||||
EncryptedKeyCode ekc;
|
||||
auto kcstr = encrypted.str();
|
||||
eraseAllSubStr(kcstr,"-----BEGIN BTS-----\n");
|
||||
eraseAllSubStr(kcstr, "\n-----END BTS-----\n");
|
||||
kcstr.erase( std::remove(kcstr.begin(), kcstr.end(), '\n'), kcstr.end() );
|
||||
//kcstr = boost::replace_all_copy<string>(kcstr, "\n", "\\n");
|
||||
ekc.set_keycode(kcstr);
|
||||
std::string pb;
|
||||
google::protobuf::TextFormat::PrintToString(ekc, &pb);
|
||||
std::cout << std::endl << std::endl;
|
||||
std::cout << "#nusers: " << users << std::endl;
|
||||
std::cout << "#allow concurrent : " << (kc.allow_concurrent() ? "true" : "false") << std::endl;
|
||||
std::cout << "#expires: " << datetime::simple_date_time_string(datetime::ll_to_ptime(kc.expiration())) << " " << std::to_string(kc.expiration()) << std::endl;
|
||||
std::cout << "#generation_time: " << datetime::simple_date_time_string(datetime::ll_to_ptime(kc.generation_time())) << " " << std::to_string(kc.generation_time()) << std::endl;
|
||||
std::cout << "EncryptedKeyCode {" << std::endl;
|
||||
std::cout << " value {" << std::endl;
|
||||
std::cout << " " << pb;
|
||||
std::cout << " }" << std::endl;
|
||||
std::cout << "}" << std::endl;
|
||||
std::cout << std::endl << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int encode(const std::vector<std::string>& args)
|
||||
{
|
||||
std::string keyfile, pass, users, expiration, ip, port; bool concurrent = false;
|
||||
get_cmd(args, keyfile, pass, users, expiration, nullptr, &ip, &port, &concurrent);
|
||||
|
||||
return encrypt(keyfile, pass, users, expiration, ip, port, concurrent);
|
||||
}
|
||||
|
||||
int decode(const std::vector<std::string>& args)
|
||||
{
|
||||
std::string keyfile, pass, users, expiration, source;
|
||||
get_cmd(args, keyfile, pass, users, expiration, &source);
|
||||
|
||||
if(keyfile != "")
|
||||
{
|
||||
PublicKeyPtr pub = bts::crypto::PublicKey::read_file(keyfile);
|
||||
if(pub && source != "")
|
||||
{
|
||||
//KeycodePayloadHelper helper;
|
||||
bts::config::EncryptedKeyCode ekc;
|
||||
std::fstream fs(source);
|
||||
//google::protobuf::io::IstreamInputStream pay_stream(&fs);
|
||||
//google::protobuf::TextFormat::Parse(&pay_stream, &ekc);
|
||||
std::string str((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>());
|
||||
config::internal::initialize_for_test(str);
|
||||
ekc = config::get<bts::config::EncryptedKeyCode>();
|
||||
|
||||
std::cout << std::endl << std::endl;
|
||||
bts::config::KeyCode kc; std::string string_out;
|
||||
bts::crypto::decrypt_keycode(*pub, ekc, &kc);
|
||||
google::protobuf::TextFormat::PrintToString(kc, &string_out);
|
||||
std::cout << string_out << std::endl;
|
||||
}
|
||||
|
||||
//int ret = openssl::RSA_public_decrypt(sz, data, &buffer[0], key->pkey.rsa, RSA_PKCS1_PADDING);
|
||||
std::cout << std::endl << std::endl;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << KeyCode::helpstr();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int KeyCode::execute(const std::vector<std::string>& args)
|
||||
{
|
||||
CryptoInit::initialize();
|
||||
if(args.size() > 0)
|
||||
{
|
||||
if(boost::to_lower_copy(args[0]) == "keycode" || boost::to_lower_copy(args[0]) == "encode")
|
||||
{
|
||||
return encode(args);
|
||||
}
|
||||
if(boost::to_lower_copy(args[0]) == "keygen")
|
||||
{
|
||||
return keygen(args);
|
||||
}
|
||||
if(boost::to_lower_copy(args[0]) == "decode")
|
||||
{
|
||||
return decode(args);
|
||||
}
|
||||
if(boost::to_lower_copy(args[0]) == "checkips")
|
||||
{
|
||||
return checkips();
|
||||
}
|
||||
}
|
||||
std::cout << help() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string KeyCode::help ()
|
||||
{
|
||||
return helpstr();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}}
|
||||
@@ -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<std::string> const &args);
|
||||
std::string help();
|
||||
|
||||
static std::string helpstr();
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
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
|
||||
|
||||
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:
|
||||
|
||||
./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
|
||||
|
||||
|
||||
Reference in new issue
Block a user