SPARK-959 Implement functions for CME "secure" logon.

This commit is contained in:
Eric Harding committed 2018-04-04 11:01:12 -04:00
1 parent 9abf59170a
commit ac2fa9b7bc
7 files changed
+156 -4

No files matched your search

+126
View File
@@ -0,0 +1,126 @@
#ifndef _WIN32
#include "SecureLogon.h"
#include "lib/utils/bts_assert.h"
#include "lib/config/config.h"
#include "lib/config/protos/system.pb.h"
// libcrypto++-dev
#include <cryptopp/hmac.h>
#include <cryptopp/sha.h>
#include <cryptopp/base64.h>
#include <cryptopp/filters.h>
#include <boost/filesystem.hpp>
#include <boost/tokenizer.hpp>
#include <iostream>
// #include "catch/catch.hpp"
#include <tuple>
using namespace CryptoPP;
using std::string;
namespace glbx {
std::tuple<string, string> load_session_keys(const string& session) {
using namespace boost;
using namespace boost::filesystem;
auto path = bts::config::get<bts::config::System>().data_path() + "glbx_logon/";
if (is_directory(path)) {
// iterate though each file
for (const auto& f : directory_iterator(path)) {
std::ifstream input(f.path().string());
while (input && !input.eof()) {
// iterate though each line
string line;
std::getline(input, line);
char_separator<char> sep(", \t");
tokenizer<char_separator<char>> 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);
return {id,secret};
}
}
}
}
bts_assert(false);
}
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& 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 += 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);
try {
string decoded_key;
StringSink key_sink(decoded_key);
Base64URLDecoder decoder(&key_sink);
StringSource(secret_key, true, &decoder);
HMAC<SHA256> hmac( (const byte*)decoded_key.c_str(), decoded_key.size());
string calculated_hmac;
StringSink hmac_sink(calculated_hmac);
HashFilter filter(hmac, &hmac_sink);
StringSource(canonical, true, &filter);
string encoded_hmac;
StringSink result_sink(encoded_hmac);
Base64URLEncoder url_enc(&result_sink);
StringSource(calculated_hmac, true, &url_enc);
return { access_id, encoded_hmac };
}
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