From ca3bc5059e73d889d162211e081e125dd9230b91 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 27 Apr 2022 02:19:50 -0400 Subject: [PATCH] Read public key from config (for login/auth) --- src/lib/config/protos/keycode.proto | 6 ++++-- src/lib/utils/AuthSessionManager.cpp | 15 +++------------ src/lib/utils/FileUtils.cpp | 8 ++++++++ src/lib/utils/FileUtils.h | 2 ++ src/lib/utils/crypto/PublicKey.cpp | 10 ++++++++++ src/lib/utils/crypto/PublicKey.h | 5 +++++ 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/lib/config/protos/keycode.proto b/src/lib/config/protos/keycode.proto index a0c3d1fc1..2c477a50b 100644 --- a/src/lib/config/protos/keycode.proto +++ b/src/lib/config/protos/keycode.proto @@ -16,5 +16,7 @@ message EncryptedKeyCode { } message PublicKey { - optional string key = 10; -} \ No newline at end of file + // Treat these like a oneof case. If both are defined, 'key_lines' takes precedence + repeated string key_lines = 1; + optional string key_file_path = 2; +} diff --git a/src/lib/utils/AuthSessionManager.cpp b/src/lib/utils/AuthSessionManager.cpp index fb8e9807d..e9268e1b7 100644 --- a/src/lib/utils/AuthSessionManager.cpp +++ b/src/lib/utils/AuthSessionManager.cpp @@ -6,6 +6,7 @@ #include "lib/utils/crypto/PrivateKey.h" #include "lib/utils/crypto/PublicKey.h" #include "lib/utils/datetime.h" +#include "lib/utils/FileUtils.h" #include "lib/config/config.h" #include "lib/config/protos/ports.pb.h" @@ -29,21 +30,11 @@ 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(); - auto pub = bts::crypto::PublicKey::read_string(PUBLIC_KEY); + auto pub = bts::crypto::PublicKey::read(config::get()); + bts::config::KeyCode kc; auto valid = bts::crypto::decrypt_keycode(*pub, ekc, &kc); bts_assert_msg(valid, "Your BTS Keycode is invalid"); diff --git a/src/lib/utils/FileUtils.cpp b/src/lib/utils/FileUtils.cpp index f34db7ade..3bd849c5e 100644 --- a/src/lib/utils/FileUtils.cpp +++ b/src/lib/utils/FileUtils.cpp @@ -59,6 +59,14 @@ std::string FileUtils::change_file_ext( return change_file_ext(path, new_ext); } +std::string FileUtils::read_file(const std::string &path) { + std::ifstream istr(path); + if (!istr) { + return ""; + } + return to_string(istr); +} + std::string FileUtils::to_string(std::istream &istr) { std::stringstream buff; buff << istr.rdbuf(); diff --git a/src/lib/utils/FileUtils.h b/src/lib/utils/FileUtils.h index a89a51144..a6839ef2a 100644 --- a/src/lib/utils/FileUtils.h +++ b/src/lib/utils/FileUtils.h @@ -35,6 +35,8 @@ class FileUtils { template static void write_to_bin(std::ostream &out, T value); + static std::string read_file(const std::string &path); + template static void read_from_bin(std::istream &in, T &value); diff --git a/src/lib/utils/crypto/PublicKey.cpp b/src/lib/utils/crypto/PublicKey.cpp index 42f1268da..44421200e 100644 --- a/src/lib/utils/crypto/PublicKey.cpp +++ b/src/lib/utils/crypto/PublicKey.cpp @@ -1,6 +1,7 @@ #include "lib/utils/crypto/PrivateKey.h" #ifndef _WIN32 #include "PublicKey.h" +#include "lib/config/protos/keycode.pb.h" #include "lib/logging/logging.h" #include "lib/utils/FileUtils.h" #include "lib/utils/bts_assert.h" @@ -63,6 +64,15 @@ const PublicKeyPtr PublicKey::read_file(const std::string &path) { return PublicKeyPtr(new PublicKey(key)); } +const PublicKeyPtr PublicKey::read(const bts::config::PublicKey &public_key) { + if (public_key.key_lines().size() > 0) { + return read_string( + fmt::to_string(fmt::join(public_key.key_lines(), "\n"))); + } + return read_string( + bts::spartan::FileUtils::read_file(public_key.key_file_path())); +} + const PublicKeyPtr PublicKey::read_string(const std::string &public_key) { std::stringstream oss; oss << public_key; diff --git a/src/lib/utils/crypto/PublicKey.h b/src/lib/utils/crypto/PublicKey.h index b07933e20..e4ce54641 100644 --- a/src/lib/utils/crypto/PublicKey.h +++ b/src/lib/utils/crypto/PublicKey.h @@ -2,6 +2,10 @@ #include "PrivateKey.h" #include "lib/utils/bts_no_copy.h" +namespace bts::config { +class PublicKey; +} + namespace bts::crypto { class PublicKey : public PrivateKey { @@ -17,6 +21,7 @@ class PublicKey : public PrivateKey { const std::iostream &in_dat, std::iostream &out_data) const; static const PublicKeyPtr create(const PrivateKey &private_key); + static const PublicKeyPtr read(const bts::config::PublicKey &public_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);