145 lines
4.4 KiB
C++
145 lines
4.4 KiB
C++
#ifndef _WIN32
|
|
#include "SecureLogon.h"
|
|
|
|
#ifndef BTS_EDGE
|
|
#include "lib/config/config.h"
|
|
#include "lib/config/protos/system.pb.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/utils/datetime.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 <cryptopp/base64.h>
|
|
#include <cryptopp/filters.h>
|
|
#include <cryptopp/hmac.h>
|
|
#include <cryptopp/sha.h>
|
|
|
|
#include <boost/algorithm/string/join.hpp>
|
|
#include <boost/filesystem.hpp>
|
|
#include <boost/tokenizer.hpp>
|
|
#include <fstream>
|
|
// #include "catch/catch.hpp"
|
|
|
|
#include <tuple>
|
|
|
|
using namespace CryptoPP;
|
|
using namespace boost;
|
|
using std::string;
|
|
|
|
namespace bts {
|
|
namespace glbx {
|
|
|
|
bool is_expired(const string &expiration) {
|
|
char_separator<char> sep("-\\");
|
|
tokenizer<char_separator<char>> 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<string, string> load_session_keys(const string &session) {
|
|
using namespace boost::filesystem;
|
|
auto path =
|
|
bts::config::get<bts::config::System>().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<config::DeploymentType>().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<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);
|
|
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);
|
|
return {"", ""};
|
|
}
|
|
|
|
LogonData calculate_hmac(
|
|
const std::string &session_id, const std::vector<std::string> &values) {
|
|
string canonical = boost::algorithm::join(values, "\n");
|
|
|
|
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<SHA256> 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 LogonData{access_id, encoded_hmac, calculated_hmac, canonical};
|
|
} catch (const Exception &e) {
|
|
bts_assert_msg(false, e.what());
|
|
}
|
|
bts_assert(false);
|
|
return {};
|
|
}
|
|
|
|
// 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");
|
|
// }
|
|
|
|
} // namespace glbx
|
|
} // namespace bts
|
|
#endif
|