From 8a1d49733ea8cc50a1a67c5f9e824182bcd88953 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Thu, 6 Jun 2019 16:40:32 -0400 Subject: [PATCH] keycodes work, need some documentation --- src/lib/config/defaults/keycode.conf | 6 +++--- src/lib/utils/AuthSessionManager.cpp | 11 +++++++---- src/lib/utils/crypto/KeyCodeHelper.cpp | 2 +- src/lib/utils/crypto/PrivateKey.cpp | 1 + src/server/sparkd/cmds/KeyCode.cpp | 21 +++++++++++++++++++-- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/lib/config/defaults/keycode.conf b/src/lib/config/defaults/keycode.conf index a136e9c35..7bd2b5a67 100644 --- a/src/lib/config/defaults/keycode.conf +++ b/src/lib/config/defaults/keycode.conf @@ -1,8 +1,8 @@ -#nusers: 2 +#nusers: 1 #expires: 2019-Sep-30 00:00:00 637053984000000000 -#generation_time: 2019-Jun-06 17:30:16.234397 5248640408589731874 +#generation_time: 2019-Jun-06 19:57:36.339686 5248640496990784764 EncryptedKeyCode { value { - keycode: "-----BEGIN BTS-----\njkMxNGzarPWcVi0cMbpXa9KkqNKAj3tLVcb5CtACLu78F6RYdpQOJq8Rrt6TQJ4o\nxJdHyAzPwhr/KR/8EMcqWuc5WdailgqoHq+VNhVMD2lVmdMGrIlvLYWzKqgPIvBL\nMgB9hWCMq8vC1JEXmLWgAg9pLcS0LCvE+ydUm3nLdL3kEmfLmbcYfN0FaSBIFieN\n6BQZyL23vmKGGuG5TzJpW9ClRQ8I/iQUGDu3FDTbaEHlVgBrD5NHzQszQ6JKgvGr\ncLMC9gyaI0oaY4AqT/d/img7NMehLdpG4MPsqQXW1iuRxAnI0aJYwXd9WSpCmR+W\nnZynJGKRzWKwlfKspYzXtQ==\n-----END BTS-----\n" + keycode: "JeCTIruFKQ79vsp7EciymRICr+Hl89z53lFT6+KZGkRRFl0+M0NiXwU9xA3pN9wDya1CfLUHIZtJ0vlH3FmJGknD+AzXeRM97teuyjRAEOxiKtoDTU/vVlPTOdsB2vTv4cdkwDn3mKdaz2uxDs8HPNyJGBCJSv30I/BN/IhfuHMNUo7AfNl0tuCduuqu+ZdMyDuKm1cGXKzORMpOWSHH+XgPleL3ms/tndCtk0eMpNS+sxZ7EHaqT6gNbnxwfDGeifDdGxWKHqmO3mPS34+VW/njdre3tXOZMVi90N9dkoAIrToV0wpRk7JPT6BcQL5+Y45bGWqFktWqH3dp2X3doQ==" } } \ No newline at end of file diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index 4ac788a26..372c56c11 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -23,13 +23,15 @@ namespace api { namespace auth { const std::string AuthSessionManager::PUBLIC_KEY = - "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAseguRgLD88QdhK0agy8f" +"-----BEGIN PUBLIC KEY-----\n" +"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAseguRgLD88QdhK0agy8f" "gz3SXp/hmp377+Q+z3OQqGG5kWyKnQfkAQDFeSv8o1iwSQupN0D1QOXyDnV1jmue" "rqTitasGeq4AwKLhY89O/VuXc+URAO+HZ7EqVWfb3Nv4EOZRiiVsxmVBkld73Rcy" "UAS4OXh4jDtE8IzOoXB7HhlMPBIAkueqdyy/v9zamzKrxPzWd+IW/IYEdCrUS8ES" "mxOkeadSHVeH2nlHVREdktE3wKRd3fZ3OkyGaHnUPxBoUW1Ex57BpwdMGUqLZ3W2" "92yQa/32dsLocoTri4iiImvQ/0JvfhnY9MDTZnmIgb8bPWSvPblQN0RR9OkRDFT9" -"3QIDAQAB"; +"3QIDAQAB" +"\n-----END PUBLIC KEY-----\n"; AuthSessionManager::AuthSessionManager() : _map(), _nusers(), _expiration() { @@ -63,7 +65,8 @@ bool AuthSessionManager::add_or_replace_session(const api::user::TokenPayload& t bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) { - if(datetime::ll_now() < _expiration) + auto now = datetime::ll_now(); + if(datetime::ll_diff_in_us(now, _expiration) > 0) { if(tok.wheel()) return true; @@ -71,7 +74,7 @@ bool AuthSessionManager::check_session(const api::user::TokenPayload& tok) if(it != _map.end()) { return google::protobuf::util::MessageDifferencer::Equivalent(tok, it->second.token) - && (tok.date() < _expiration); + && datetime::ll_diff_in_us(tok.date(), _expiration) > 0; } } return false; diff --git a/src/lib/utils/crypto/KeyCodeHelper.cpp b/src/lib/utils/crypto/KeyCodeHelper.cpp index bd6c1c9db..4997e5158 100644 --- a/src/lib/utils/crypto/KeyCodeHelper.cpp +++ b/src/lib/utils/crypto/KeyCodeHelper.cpp @@ -19,7 +19,7 @@ bool decrypt_keycode(const bts::crypto::PublicKey& public_key, const bts::config bts::config::KeyCode* dk) { std::stringstream kss, out; - kss << kc.keycode(); + kss << "-----BEGIN BTS-----\n" + kc.keycode() + "\n-----END BTS-----\n"; if(public_key.decrypt(kss, out)) { bts::config::KeyCode kc; diff --git a/src/lib/utils/crypto/PrivateKey.cpp b/src/lib/utils/crypto/PrivateKey.cpp index 057e0f87b..a09904a04 100644 --- a/src/lib/utils/crypto/PrivateKey.cpp +++ b/src/lib/utils/crypto/PrivateKey.cpp @@ -154,6 +154,7 @@ bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_da 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) diff --git a/src/server/sparkd/cmds/KeyCode.cpp b/src/server/sparkd/cmds/KeyCode.cpp index 0104db3b5..2a97c6b2f 100644 --- a/src/server/sparkd/cmds/KeyCode.cpp +++ b/src/server/sparkd/cmds/KeyCode.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -147,6 +148,17 @@ void write_keycode(const std::string& users, const std::string& expiration, bts: kc.set_generation_time(now); } +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) { @@ -176,7 +188,12 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri //int ret = openssl::RSA_private_encrypt(sz-1, pay, &buffer[0], key->pkey.rsa, RSA_PKCS1_PADDING); EncryptedKeyCode ekc; - ekc.set_keycode(encrypted.str()); + 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(kcstr, "\n", "\\n"); + ekc.set_keycode(kcstr); std::string pb; google::protobuf::TextFormat::PrintToString(ekc, &pb); std::cout << std::endl << std::endl; @@ -217,7 +234,7 @@ int decode(const std::vector& args) std::fstream fs(source); //google::protobuf::io::IstreamInputStream pay_stream(&fs); //google::protobuf::TextFormat::Parse(&pay_stream, &ekc); - std::string str((std::istreambuf_iterator(fs)), std::istreambuf_iterator()); + std::string str((std::istreambuf_iterator(fs)), std::istreambuf_iterator()); config::internal::initialize_for_test(str); ekc = config::get();