#ifndef _WIN32 #include "SecureLogon.h" #ifndef BTS_EDGE #include "lib/utils/bts_assert.h" #include "lib/utils/datetime.h" #include "lib/config/config.h" #include "lib/config/protos/system.pb.h" #else #include "bts_assert.h" #include "core/bts_core/config/config.h" #include "core/bts_core/config/system.pb.h" #include "core/bts_core/utils/datetime.h" #endif // libcrypto++-dev #include #include #include #include #include #include #include #include // #include "catch/catch.hpp" #include using namespace CryptoPP; using namespace boost; using std::string; namespace bts { namespace glbx { bool is_expired(const string& expiration) { char_separator sep("-\\"); tokenizer> tokens(expiration, sep); auto t = tokens.begin(); int year = std::stoi(*t); int month = std::stoi(*(++t)); int day = std::stoi(*(++t)); bts::datetime::Date dtexp(year, month, day); auto today = bts::datetime::today(); return dtexp < today; } std::tuple load_session_keys(const string& session) { using namespace boost::filesystem; auto path = bts::config::get().data_path() + "glbx_logon/"; // we try to make it easier to run cert by copying the cert secure // logon file to the right place if in dev if (config::get().type()=="dev") { auto cert_file=os::sparkd_wd() + "/data_files/cert_secure_logon.txt" ; if (exists(cert_file)) { create_directories(path); auto target = path+"cert_secure_logon.txt"; if (exists(target)) remove(target); copy_file(cert_file,target); } } if (is_directory(path)) { // iterate though each file directory_iterator end; for(auto iter = directory_iterator(path); iter != end; ++iter) { auto& f = *iter; std::ifstream input(f.path().string()); while (input && !input.eof()) { // iterate though each line string line; std::getline(input, line); char_separator sep(", \t"); tokenizer> tokens(line, sep); auto t = tokens.begin(); // if first token matches session return the next 2 tokens if (t != tokens.end() && *t == session) { auto id = *(++t); auto secret = *(++t); auto create_date = *(++t); auto expiration_date = *(++t); if (!is_expired(expiration_date)) { return std::make_tuple(id, secret); } } } } } string error = "Failed to find secure logon key for session: " + session; bts_assert_msg(false, error); } LogonData calculate_hmac( const std::string& session_id, const std::string& msg_seq_num, const std::string& sender_comp_id, const std::string& sender_sub_id, const std::string& sending_time, const std::string& target_sub_id, const std::string& heartbeat_interval, const std::string& sender_location_id, const std::string& last_seq_num, const std::string& application_system_name, const std::string& application_system_version, const std::string& application_system_vendor ) { string canonical; canonical += msg_seq_num; canonical += '\n'; canonical += sender_comp_id; canonical += '\n'; canonical += sender_sub_id; canonical += '\n'; canonical += sending_time; canonical += '\n'; canonical += target_sub_id; canonical += '\n'; canonical += heartbeat_interval; canonical += '\n'; canonical += sender_location_id; canonical += '\n'; canonical += last_seq_num; canonical += '\n'; canonical += application_system_name; canonical += '\n'; canonical += application_system_version; canonical += '\n'; canonical += application_system_vendor; string secret_key, access_id; std::tie(access_id, secret_key) = load_session_keys(session_id); // LOG(o2, info, "Loaded access_id {} and secret {} for session {}", access_id, secret_key, session_id); try { string decoded_key; StringSource (secret_key, true, new Base64URLDecoder(new StringSink (decoded_key))); HMAC hmac( (const byte*)decoded_key.c_str(), decoded_key.size()); // NOLINT string calculated_hmac; StringSource(canonical, true, new HashFilter(hmac, new StringSink(calculated_hmac))); string encoded_hmac; StringSource(calculated_hmac, true, new Base64URLEncoder(new StringSink(encoded_hmac))); return { access_id, encoded_hmac, canonical}; } catch(const Exception& e) { bts_assert_msg(false, e.what()); } bts_assert(false); } // TEST_CASE("LoadSessionKeys", "") { // std::string session_id = "1U7"; // string secret_key, access_id; // std::tie(access_id, secret_key) = load_session_keys(session_id); // REQUIRE(access_id == "ozXmORijPtJriTg6Wwny"); // REQUIRE(secret_key == "A1KOwrhH8GojNotjkCfVwHRSi_OS280xihaRZwJLFKo"); // } }} #endif