Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion aether/api_protocol/api_class_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "aether-miscpp/reflect/reflect.h"
#include "aether/api_protocol/api_class.h"
#include "aether/api_protocol/api_pack_parser.h"
#include "aether/tele/tele.h"

namespace ae {
/**
Expand Down Expand Up @@ -245,7 +246,10 @@ class ApiClassImpl : public ApiClass {
void LoadFactory(MessageId message_id, ApiParser& parser) {
auto res = LoadFactoryImpl(static_cast<Api*>(this), message_id, parser);
if (!res) {
assert(false && "Implementation not found");
AE_TELED_WARNING("Dropped packet: unknown API message id {}",
static_cast<std::uint32_t>(message_id));
parser.Cancel();
return;
}
}
};
Expand Down
3 changes: 3 additions & 0 deletions aether/api_protocol/sub_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class SubApiImpl {
template <typename TFunc = PassThrough>
void Parse(TApi& api, TFunc pre_parse = {}) {
auto&& mod_data = pre_parse(data_);
if (mod_data.empty()) {
return;
}
ApiParser parser{api.protocol_context(), mod_data};
parser.Parse(api);
}
Expand Down
25 changes: 19 additions & 6 deletions aether/crypto/hydrogen/hydro_sync_crypto_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# include <vector>

# include "aether/crypto/crypto_definitions.h"
# include "aether/tele/tele.h"

namespace ae {
namespace _internal {
Expand All @@ -49,6 +50,11 @@ inline std::vector<std::uint8_t> EncryptWithSymmetric(
inline std::vector<std::uint8_t> DecryptWithSymmetric(
HydrogenSecretBoxKey const& secret_key,
std::vector<std::uint8_t> const& encrypted_data) {
if (encrypted_data.size() <=
sizeof(std::uint64_t) + hydro_secretbox_HEADERBYTES) {
return {};
}

auto const* msg_id_ptr = encrypted_data.data();
auto msg_id = *reinterpret_cast<std::uint64_t const*>(msg_id_ptr);

Expand All @@ -57,11 +63,13 @@ inline std::vector<std::uint8_t> DecryptWithSymmetric(

auto const* encrypted_ptr = msg_id_ptr + sizeof(msg_id);

[[maybe_unused]] auto r =
hydro_secretbox_decrypt(decrypted_data.data(), encrypted_ptr,
encrypted_data.size() - sizeof(msg_id), msg_id,
HYDRO_CONTEXT, secret_key.key.data());
assert(r == 0);
auto r = hydro_secretbox_decrypt(
decrypted_data.data(), encrypted_ptr,
encrypted_data.size() - sizeof(msg_id), msg_id, HYDRO_CONTEXT,
secret_key.key.data());
if (r != 0) {
return {};
}

return decrypted_data;
}
Expand Down Expand Up @@ -93,7 +101,12 @@ DataBuffer HydroSyncDecryptProvider::Decrypt(DataBuffer const& data) {
auto key = key_provider_->GetKey();
assert(key.Index() == CryptoKeyType::kHydrogenSecretBox);

return _internal::DecryptWithSymmetric(key.Get<HydrogenSecretBoxKey>(), data);
auto decrypted =
_internal::DecryptWithSymmetric(key.Get<HydrogenSecretBoxKey>(), data);
if (decrypted.empty()) {
AE_TELED_WARNING("Dropped packet: sync decrypt failed");
}
return decrypted;
}

} // namespace ae
Expand Down
27 changes: 19 additions & 8 deletions aether/crypto/sodium/sodium_sync_crypto_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

#if AE_CRYPTO_SYNC == AE_CHACHA20_POLY1305

# include <vector>
# include <algorithm>
Comment thread
BartolomeyKant marked this conversation as resolved.
# include <cassert>
# include <utility>
# include <algorithm>
# include <vector>

# include "aether/crypto/crypto_nonce.h"
# include "aether/tele/tele.h"

namespace ae {

Expand Down Expand Up @@ -57,7 +58,10 @@ inline DataBuffer EncryptWithSymmetric(
inline DataBuffer DecryptWithSymmetric(
SodiumChacha20Poly1305Key const& secret_key,
DataBuffer const& encrypted_data) {
assert(encrypted_data.size() > kNonceSize);
if (encrypted_data.size() <=
kNonceSize + crypto_aead_chacha20poly1305_ABYTES) {
return {};
}

auto nonce = CryptoNonce{};
auto encrypted_data_size = encrypted_data.size() - nonce.value.size();
Expand All @@ -69,15 +73,18 @@ inline DataBuffer DecryptWithSymmetric(

auto decrypted_data = std::vector<uint8_t>(
encrypted_data_size - crypto_aead_chacha20poly1305_ABYTES);
unsigned long long decrypted_len;
unsigned long long decrypted_len{0};

[[maybe_unused]] auto r = crypto_aead_chacha20poly1305_decrypt(
auto r = crypto_aead_chacha20poly1305_decrypt(
decrypted_data.data(), &decrypted_len, nullptr, encrypted_data.data(),
encrypted_data_size, nullptr, 0, nonce.value.data(),
secret_key.key.data());

assert(r == 0);
if (r != 0) {
return {};
}

decrypted_data.resize(static_cast<std::size_t>(decrypted_len));
return decrypted_data;
}
} // namespace _internal
Expand Down Expand Up @@ -106,8 +113,12 @@ DataBuffer SodiumSyncDecryptProvider::Decrypt(DataBuffer const& data) {
auto key = key_provider_->GetKey();
assert(key.Index() == CryptoKeyType::kSodiumChacha20Poly1305);

return _internal::DecryptWithSymmetric(key.Get<SodiumChacha20Poly1305Key>(),
data);
auto decrypted = _internal::DecryptWithSymmetric(
key.Get<SodiumChacha20Poly1305Key>(), data);
if (decrypted.empty()) {
AE_TELED_WARNING("Dropped packet: sync decrypt failed");
}
return decrypted;
}

} // namespace ae
Expand Down
4 changes: 4 additions & 0 deletions aether/work_cloud_api/client_api/client_api_unsafe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ ClientApiUnsafe::ClientApiUnsafe(ProtocolContext& protocol_context,
void ClientApiUnsafe::SendSafeApiData(SubApiImpl<ClientApiSafe> sub_api) {
sub_api.Parse(client_safe_api_, [this](auto const& data) {
auto decrypted = Decrypt(data);
if (decrypted.empty()) {
AE_TELED_WARNING("Dropped packet: client safe api decrypt failed");
return decrypted;
}
AE_TELED_DEBUG("Client api unsafe data {}", decrypted);
return decrypted;
});
Expand Down
Loading