2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2007-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <cctype>
|
2022-01-08 12:46:25 +00:00
|
|
|
#include <iterator>
|
|
|
|
#include <random>
|
2021-12-21 22:14:15 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <vector>
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
2015-01-01 21:16:36 +00:00
|
|
|
#include <b64/cdecode.h>
|
|
|
|
#include <b64/cencode.h>
|
2021-09-12 17:41:49 +00:00
|
|
|
}
|
2015-01-01 21:16:36 +00:00
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-04-07 22:26:59 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/crypto-utils.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/utils.h"
|
2014-12-04 11:27:38 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// ---
|
2022-12-01 04:58:14 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
namespace
|
2014-12-04 20:45:18 +00:00
|
|
|
{
|
2023-01-21 23:53:58 +00:00
|
|
|
constexpr auto TrSha1DigestStrlen = size_t{ 40 };
|
|
|
|
|
|
|
|
constexpr auto TrSha256DigestStrlen = size_t{ 64 };
|
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
namespace ssha1_impl
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-01-21 23:53:58 +00:00
|
|
|
auto constexpr DigestStringSize = TrSha1DigestStrlen;
|
2021-12-21 22:14:15 +00:00
|
|
|
auto constexpr SaltedPrefix = "{"sv;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
std::string tr_salt(std::string_view plaintext, std::string_view salt)
|
|
|
|
{
|
|
|
|
static_assert(DigestStringSize == 40);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
// build a sha1 digest of the original content and the salt
|
2022-07-31 20:58:14 +00:00
|
|
|
auto const digest = tr_sha1::digest(plaintext, salt);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
// convert it to a string. string holds three parts:
|
|
|
|
// DigestPrefix, stringified digest of plaintext + salt, and the salt.
|
2022-07-31 20:58:14 +00:00
|
|
|
return fmt::format(FMT_STRING("{:s}{:s}{:s}"), SaltedPrefix, tr_sha1_to_string(digest), salt);
|
2021-12-21 22:14:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
} // namespace ssha1_impl
|
2021-12-21 22:14:15 +00:00
|
|
|
} // namespace
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
std::string tr_ssha1(std::string_view plaintext)
|
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
using namespace ssha1_impl;
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
// build an array of random Salter chars
|
|
|
|
auto constexpr Salter = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"sv;
|
|
|
|
static_assert(std::size(Salter) == 64);
|
|
|
|
auto constexpr SaltSize = size_t{ 8 };
|
2022-11-25 21:04:37 +00:00
|
|
|
auto salt = tr_rand_obj<std::array<char, SaltSize>>();
|
2021-12-21 22:14:15 +00:00
|
|
|
std::transform(
|
|
|
|
std::begin(salt),
|
|
|
|
std::end(salt),
|
|
|
|
std::begin(salt),
|
|
|
|
[&Salter](auto ch) { return Salter[ch % std::size(Salter)]; });
|
|
|
|
|
|
|
|
return tr_salt(plaintext, std::string_view{ std::data(salt), std::size(salt) });
|
2014-12-04 20:45:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
bool tr_ssha1_test(std::string_view text)
|
2014-12-04 20:45:18 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
using namespace ssha1_impl;
|
|
|
|
|
2023-06-30 14:49:58 +00:00
|
|
|
return tr_strv_starts_with(text, SaltedPrefix) && std::size(text) >= std::size(SaltedPrefix) + DigestStringSize;
|
2021-12-21 22:14:15 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
bool tr_ssha1_matches(std::string_view ssha1, std::string_view plaintext)
|
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
using namespace ssha1_impl;
|
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
if (!tr_ssha1_test(ssha1))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-04 20:45:18 +00:00
|
|
|
|
2021-12-21 22:14:15 +00:00
|
|
|
auto const salt = ssha1.substr(std::size(SaltedPrefix) + DigestStringSize);
|
|
|
|
return tr_salt(plaintext, salt) == ssha1;
|
2014-12-04 20:45:18 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2014-12-04 20:45:18 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace base64_impl
|
|
|
|
{
|
|
|
|
|
|
|
|
constexpr size_t base64AllocSize(std::string_view input)
|
2014-12-04 19:58:34 +00:00
|
|
|
{
|
2022-07-27 14:03:13 +00:00
|
|
|
size_t ret_length = 4 * ((std::size(input) + 2) / 3); // NOLINT misc-const-correctness
|
2015-01-03 21:35:20 +00:00
|
|
|
#ifdef USE_SYSTEM_B64
|
2022-01-08 12:46:25 +00:00
|
|
|
// Additional space is needed for newlines if we're using unpatched libb64
|
|
|
|
ret_length += ret_length / 72 + 1;
|
2015-01-03 21:35:20 +00:00
|
|
|
#endif
|
2022-01-08 12:46:25 +00:00
|
|
|
return ret_length * 8;
|
2014-12-04 19:58:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
} // namespace base64_impl
|
|
|
|
} // namespace
|
|
|
|
|
2022-01-08 12:46:25 +00:00
|
|
|
std::string tr_base64_encode(std::string_view input)
|
2014-12-04 19:58:34 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
using namespace base64_impl;
|
|
|
|
|
|
|
|
auto buf = std::vector<char>(base64AllocSize(input));
|
2022-01-08 12:46:25 +00:00
|
|
|
auto state = base64_encodestate{};
|
|
|
|
base64_init_encodestate(&state);
|
|
|
|
size_t len = base64_encode_block(std::data(input), std::size(input), std::data(buf), &state);
|
|
|
|
len += base64_encode_blockend(std::data(buf) + len, &state);
|
|
|
|
auto str = std::string{};
|
|
|
|
std::copy_if(
|
|
|
|
std::data(buf),
|
|
|
|
std::data(buf) + len,
|
|
|
|
std::back_inserter(str),
|
2023-06-30 14:49:58 +00:00
|
|
|
[](auto ch) { return !tr_strv_contains("\r\n"sv, ch); });
|
2022-01-08 12:46:25 +00:00
|
|
|
return str;
|
2014-12-04 19:58:34 +00:00
|
|
|
}
|
2021-11-10 00:13:47 +00:00
|
|
|
|
2022-01-08 12:46:25 +00:00
|
|
|
std::string tr_base64_decode(std::string_view input)
|
2021-11-15 03:54:48 +00:00
|
|
|
{
|
2022-01-08 12:46:25 +00:00
|
|
|
auto buf = std::vector<char>(std::size(input) + 8);
|
|
|
|
auto state = base64_decodestate{};
|
|
|
|
base64_init_decodestate(&state);
|
|
|
|
size_t const len = base64_decode_block(std::data(input), std::size(input), std::data(buf), &state);
|
|
|
|
return std::string{ std::data(buf), len };
|
2021-11-15 03:54:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2021-11-10 00:13:47 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
namespace hex_impl
|
|
|
|
{
|
|
|
|
|
2023-07-04 18:04:03 +00:00
|
|
|
template<typename InIt, typename OutIt>
|
|
|
|
constexpr void tr_binary_to_hex(InIt begin, InIt end, OutIt out)
|
2021-11-10 00:13:47 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
auto constexpr Hex = "0123456789abcdef"sv;
|
2021-11-10 00:13:47 +00:00
|
|
|
|
2023-07-04 18:04:03 +00:00
|
|
|
while (begin != end)
|
2021-11-10 00:13:47 +00:00
|
|
|
{
|
2023-07-04 18:04:03 +00:00
|
|
|
auto const val = static_cast<unsigned int>(*begin++);
|
|
|
|
*out++ = Hex[val >> 4];
|
|
|
|
*out++ = Hex[val & 0xF];
|
2021-11-10 00:13:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
constexpr void tr_hex_to_binary(char const* input, void* voutput, size_t byte_length)
|
|
|
|
{
|
|
|
|
auto constexpr Hex = "0123456789abcdef"sv;
|
|
|
|
|
|
|
|
auto* output = static_cast<uint8_t*>(voutput);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < byte_length; ++i)
|
|
|
|
{
|
|
|
|
auto const upper_nibble = Hex.find(std::tolower(*input++));
|
|
|
|
auto const lower_nibble = Hex.find(std::tolower(*input++));
|
|
|
|
*output++ = (uint8_t)((upper_nibble << 4) | lower_nibble);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace hex_impl
|
|
|
|
} // namespace
|
|
|
|
|
2023-07-04 18:04:03 +00:00
|
|
|
tr_sha1_string tr_sha1_to_string(tr_sha1_digest_t const& digest)
|
2021-12-21 22:14:15 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
using namespace hex_impl;
|
|
|
|
|
2023-07-04 18:04:03 +00:00
|
|
|
auto str = tr_sha1_string{};
|
|
|
|
tr_binary_to_hex(std::begin(digest), std::end(digest), std::back_inserter(str));
|
|
|
|
TR_ASSERT(std::size(str) == TrSha1DigestStrlen);
|
2021-12-21 22:14:15 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2023-07-04 18:04:03 +00:00
|
|
|
tr_sha256_string tr_sha256_to_string(tr_sha256_digest_t const& digest)
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
using namespace hex_impl;
|
|
|
|
|
2023-07-04 18:04:03 +00:00
|
|
|
auto str = tr_sha256_string{};
|
|
|
|
tr_binary_to_hex(std::begin(digest), std::end(digest), std::back_inserter(str));
|
|
|
|
TR_ASSERT(std::size(str) == TrSha256DigestStrlen);
|
2022-07-01 14:49:33 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2022-02-13 04:16:55 +00:00
|
|
|
std::optional<tr_sha1_digest_t> tr_sha1_from_string(std::string_view hex)
|
2021-11-10 00:13:47 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
using namespace hex_impl;
|
|
|
|
|
2023-01-21 23:53:58 +00:00
|
|
|
if (std::size(hex) != TrSha1DigestStrlen)
|
2022-02-13 04:16:55 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!std::all_of(std::begin(hex), std::end(hex), [](unsigned char ch) { return isxdigit(ch); }))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2021-12-21 22:14:15 +00:00
|
|
|
|
|
|
|
auto digest = tr_sha1_digest_t{};
|
|
|
|
tr_hex_to_binary(std::data(hex), std::data(digest), std::size(digest));
|
|
|
|
return digest;
|
2021-11-10 00:13:47 +00:00
|
|
|
}
|
2022-07-01 14:49:33 +00:00
|
|
|
|
|
|
|
std::optional<tr_sha256_digest_t> tr_sha256_from_string(std::string_view hex)
|
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
using namespace hex_impl;
|
|
|
|
|
2023-01-21 23:53:58 +00:00
|
|
|
if (std::size(hex) != TrSha256DigestStrlen)
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!std::all_of(std::begin(hex), std::end(hex), [](unsigned char ch) { return isxdigit(ch); }))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto digest = tr_sha256_digest_t{};
|
|
|
|
tr_hex_to_binary(std::data(hex), std::data(digest), std::size(digest));
|
|
|
|
return digest;
|
|
|
|
}
|
2022-12-21 15:58:32 +00:00
|
|
|
|
|
|
|
// fallback implementation in case the system crypto library's RNG fails
|
|
|
|
void tr_rand_buffer_std(void* buffer, size_t length)
|
|
|
|
{
|
|
|
|
thread_local auto gen = std::mt19937{ std::random_device{}() };
|
|
|
|
thread_local auto dist = std::uniform_int_distribution<unsigned long long>{};
|
|
|
|
|
|
|
|
for (auto *walk = static_cast<uint8_t*>(buffer), *end = walk + length; walk < end;)
|
|
|
|
{
|
|
|
|
auto const tmp = dist(gen);
|
|
|
|
auto const step = std::min(sizeof(tmp), static_cast<size_t>(end - walk));
|
|
|
|
walk = std::copy_n(reinterpret_cast<uint8_t const*>(&tmp), step, walk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_rand_buffer(void* buffer, size_t length)
|
|
|
|
{
|
|
|
|
if (!tr_rand_buffer_crypto(buffer, length))
|
|
|
|
{
|
|
|
|
tr_rand_buffer_std(buffer, length);
|
|
|
|
}
|
|
|
|
}
|