|
| 1 | +#include "PwAuth.hpp" |
| 2 | +#include "hyprlock.hpp" |
| 3 | +#include "../helpers/Log.hpp" |
| 4 | +#include "../config/ConfigManager.hpp" |
| 5 | +#include <cstddef> |
| 6 | +#include <cstring> |
| 7 | +#define GCRYPT_NO_DEPRECATED |
| 8 | +#define GCRYPT_NO_MPI_MACROS |
| 9 | +#define NEED_LIBGCRYPT_VERSION nullptr |
| 10 | +#include <gcrypt.h> |
| 11 | + |
| 12 | +using namespace std::chrono_literals; |
| 13 | + |
| 14 | +static std::unique_ptr<unsigned char[]> hex2Bytes(const std::string& hex) noexcept { |
| 15 | + auto bytes = std::make_unique<unsigned char[]>(hex.length() / 2); |
| 16 | + for (std::size_t i = 0; i < hex.length() / 2; ++i) { |
| 17 | + try { |
| 18 | + auto v = std::stoi(hex.substr(2 * i, 2), nullptr, 16); |
| 19 | + if (v >= 0) |
| 20 | + bytes[i] = static_cast<unsigned char>(v); |
| 21 | + else |
| 22 | + throw std::invalid_argument("invalid hex value"); |
| 23 | + } catch (std::invalid_argument const& e) { |
| 24 | + Debug::log(ERR, "auth: invalid password_hash"); |
| 25 | + bytes = nullptr; |
| 26 | + } catch (std::out_of_range const& e) { |
| 27 | + // Should never happen, as 2-byte substrings should never go o-o-r. |
| 28 | + Debug::log(CRIT, "auth: implementation error in hex2Bytes conversion"); |
| 29 | + bytes = nullptr; |
| 30 | + } |
| 31 | + } |
| 32 | + return bytes; |
| 33 | +} |
| 34 | + |
| 35 | +static std::string bytes2Hex(const unsigned char* bytes, std::size_t len) { |
| 36 | + std::stringstream ss; |
| 37 | + ss << std::setw(2) << std::setfill('0') << std::hex; |
| 38 | + for (std::size_t i = 0; i < len; ++i) |
| 39 | + ss << (int)bytes[i]; |
| 40 | + return ss.str(); |
| 41 | +} |
| 42 | + |
| 43 | +CPwAuth::CPwAuth() { |
| 44 | + |
| 45 | + if (gcry_check_version(NEED_LIBGCRYPT_VERSION)) |
| 46 | + gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); |
| 47 | + else |
| 48 | + Debug::log(CRIT, "libgcrypt too old"); |
| 49 | + |
| 50 | + if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P)) { |
| 51 | + |
| 52 | + // Handle the hash algorithm |
| 53 | + static auto const ALGO = *(Hyprlang::STRING*)(g_pConfigManager->getValuePtr("general:hash_algorithm")); |
| 54 | + m_iAlgo = gcry_md_map_name(ALGO); |
| 55 | + m_iDigestLen = gcry_md_get_algo_dlen(m_iAlgo); |
| 56 | + if (m_iAlgo) { |
| 57 | + static auto const err = gcry_err_code(gcry_md_test_algo(m_iAlgo)); |
| 58 | + if (err == GPG_ERR_NO_ERROR) { |
| 59 | + |
| 60 | + // Handle the salt |
| 61 | + static auto* const SALT = (Hyprlang::STRING*)(g_pConfigManager->getValuePtr("general:hash_salt")); |
| 62 | + m_szSalt = std::string(*SALT); |
| 63 | + |
| 64 | + // Handle the expected hash |
| 65 | + static auto* const HASH = (Hyprlang::STRING*)(g_pConfigManager->getValuePtr("general:password_hash")); |
| 66 | + static auto const hash = std::string(*HASH); |
| 67 | + if (hash.empty() || (hash.size() % 2) || (hash.length() != 2uL * m_iDigestLen)) { |
| 68 | + Debug::log(ERR, "auth: password_hash has incorrect length for algorithm {} (got: {}, expected: {})", ALGO, hash.size(), 2uL * m_iDigestLen); |
| 69 | + m_bLibFailed = true; |
| 70 | + } else { |
| 71 | + m_aHash = hex2Bytes(hash); |
| 72 | + if (!m_aHash || hash.empty()) |
| 73 | + m_bLibFailed = true; |
| 74 | + } |
| 75 | + } else { |
| 76 | + // Might be due to FIPS mode |
| 77 | + Debug::log(CRIT, "auth: hash algorithm unavailable: {}", ALGO); |
| 78 | + m_bLibFailed = true; |
| 79 | + } |
| 80 | + } else { |
| 81 | + Debug::log(ERR, "auth: unknown hash algorithm: {}", ALGO); |
| 82 | + m_bLibFailed = true; |
| 83 | + } |
| 84 | + } else { |
| 85 | + Debug::log(CRIT, "libgcrypt could not be initialized"); |
| 86 | + m_bLibFailed = true; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +static void passwordCheckTimerCallback(std::shared_ptr<CTimer> self, void* data) { |
| 91 | + g_pHyprlock->onPasswordCheckTimer(); |
| 92 | +} |
| 93 | + |
| 94 | +void CPwAuth::start() { |
| 95 | + std::thread([this]() { |
| 96 | + reset(); |
| 97 | + |
| 98 | + waitForInput(); |
| 99 | + |
| 100 | + // For grace or SIGUSR1 unlocks |
| 101 | + if (g_pHyprlock->isUnlocked()) |
| 102 | + return; |
| 103 | + |
| 104 | + const auto AUTHENTICATED = auth(); |
| 105 | + m_bAuthenticated = AUTHENTICATED; |
| 106 | + |
| 107 | + if (g_pHyprlock->isUnlocked()) |
| 108 | + return; |
| 109 | + |
| 110 | + g_pHyprlock->addTimer(1ms, passwordCheckTimerCallback, nullptr); |
| 111 | + }).detach(); |
| 112 | +} |
| 113 | + |
| 114 | +bool CPwAuth::auth() { |
| 115 | + if (m_bLibFailed) |
| 116 | + return true; |
| 117 | + |
| 118 | + bool verdict; |
| 119 | + auto digest = std::make_unique<unsigned char[]>(m_iDigestLen); |
| 120 | + auto istr = m_sState.input; |
| 121 | + istr.append(m_szSalt); |
| 122 | + |
| 123 | + gcry_md_hash_buffer(m_iAlgo, digest.get(), istr.c_str(), istr.size()); |
| 124 | + Debug::log(TRACE, "auth: resulting hash {}", bytes2Hex(digest.get(), m_iDigestLen)); |
| 125 | + Debug::log(TRACE, "auth: expected hash {}", bytes2Hex(m_aHash.get(), m_iDigestLen)); |
| 126 | + verdict = !std::memcmp(m_aHash.get(), digest.get(), m_iDigestLen); |
| 127 | + |
| 128 | + if (verdict) |
| 129 | + Debug::log(LOG, "auth: authenticated"); |
| 130 | + else |
| 131 | + Debug::log(ERR, "auth: unsuccessful"); |
| 132 | + |
| 133 | + m_sState.authenticating = false; |
| 134 | + /// DEBUG Code; replace constant with verdict |
| 135 | + return verdict; |
| 136 | +} |
| 137 | + |
| 138 | +bool CPwAuth::isAuthenticated() { |
| 139 | + return m_bAuthenticated; |
| 140 | +} |
| 141 | + |
| 142 | +// clearing the input must be done from the main thread |
| 143 | +static void clearInputTimerCallback(std::shared_ptr<CTimer> self, void* data) { |
| 144 | + g_pHyprlock->clearPasswordBuffer(); |
| 145 | +} |
| 146 | + |
| 147 | +void CPwAuth::waitForInput() { |
| 148 | + g_pHyprlock->addTimer(1ms, clearInputTimerCallback, nullptr); |
| 149 | + if (m_bLibFailed) |
| 150 | + return; |
| 151 | + |
| 152 | + std::unique_lock<std::mutex> lk(m_sState.inputMutex); |
| 153 | + m_bBlockInput = false; |
| 154 | + m_sState.inputRequested = true; |
| 155 | + m_sState.inputSubmittedCondition.wait(lk, [this] { return !m_sState.inputRequested || g_pHyprlock->m_bTerminate; }); |
| 156 | + m_bBlockInput = true; |
| 157 | +} |
| 158 | + |
| 159 | +void CPwAuth::submitInput(std::string input) { |
| 160 | + std::unique_lock<std::mutex> lk(m_sState.inputMutex); |
| 161 | + if (!m_sState.inputRequested) |
| 162 | + Debug::log(ERR, "SubmitInput called, but the auth thread is not waiting for input!"); |
| 163 | + m_sState.input = input; |
| 164 | + m_sState.inputRequested = false; |
| 165 | + m_sState.authenticating = true; |
| 166 | + m_sState.inputSubmittedCondition.notify_all(); |
| 167 | +} |
| 168 | + |
| 169 | +std::optional<std::string> CPwAuth::getLastPrompt() { |
| 170 | + std::string pmpt = "Password: "; |
| 171 | + return pmpt; |
| 172 | +} |
| 173 | + |
| 174 | +std::optional<std::string> CPwAuth::getLastFailText() { |
| 175 | + std::string ret = "Password incorrect"; |
| 176 | + return ret; |
| 177 | +} |
| 178 | + |
| 179 | +bool CPwAuth::checkWaiting() { |
| 180 | + return m_bBlockInput; |
| 181 | +} |
| 182 | + |
| 183 | +void CPwAuth::terminate() { |
| 184 | + m_sState.inputSubmittedCondition.notify_all(); |
| 185 | +} |
| 186 | + |
| 187 | +void CPwAuth::reset() { |
| 188 | + m_sState.input = ""; |
| 189 | + m_sState.inputRequested = false; |
| 190 | + m_sState.authenticating = false; |
| 191 | +} |
0 commit comments