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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
PrivMX Endpoint.
Copyright © 2024 Simplito sp. z o.o.

This file is part of the PrivMX Platform (https://privmx.dev).
This software is Licensed under the PrivMX Free License.

See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef _PRIVMXLIB_ENDPOINT_CORE_IDATASCHEMASTRATEGY_HPP_
#define _PRIVMXLIB_ENDPOINT_CORE_IDATASCHEMASTRATEGY_HPP_

#include <privmx/endpoint/core/CoreTypes.hpp>

namespace privmx {
namespace endpoint {
namespace core {

template<typename TServerModel, typename TDomainObject>
class IDataSchemaStrategy {
public:
virtual ~IDataSchemaStrategy() = default;
virtual TDomainObject decryptAndConvert(const TServerModel& model, const DecryptedEncKey& encKey) const = 0;
};

} // namespace core
} // namespace endpoint
} // namespace privmx

#endif // _PRIVMXLIB_ENDPOINT_CORE_IDATASCHEMASTRATEGY_HPP_
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
PrivMX Endpoint.
Copyright © 2024 Simplito sp. z o.o.

This file is part of the PrivMX Platform (https://privmx.dev).
This software is Licensed under the PrivMX Free License.

See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef _PRIVMXLIB_ENDPOINT_CORE_TYPEDDATASCHEMASTRATEGY_HPP_
#define _PRIVMXLIB_ENDPOINT_CORE_TYPEDDATASCHEMASTRATEGY_HPP_

#include <privmx/endpoint/core/CoreConstants.hpp>
#include <privmx/endpoint/core/ExceptionConverter.hpp>

#include "privmx/endpoint/core/encryptors/IDataSchemaStrategy.hpp"

namespace privmx {
namespace endpoint {
namespace core {

template<typename TServerModel, typename TRawData, typename TDomainObject>
class TypedDataSchemaStrategy : public IDataSchemaStrategy<TServerModel, TDomainObject> {
public:
TDomainObject decryptAndConvert(const TServerModel& model, const DecryptedEncKey& encKey) const override final {
try {
return convert(model, decrypt(model, encKey));
} catch (const core::Exception& e) {
return makeErrorResult(model, e.getCode());
} catch (const privmx::utils::PrivmxException& e) {
return makeErrorResult(model, ExceptionConverter::convert(e).getCode());
} catch (...) { return makeErrorResult(model, ENDPOINT_CORE_EXCEPTION_CODE); }
}

virtual TRawData decrypt(const TServerModel& model, const DecryptedEncKey& encKey) const = 0;
virtual TDomainObject convert(const TServerModel& model, const TRawData& raw) const = 0;
virtual TDomainObject makeErrorResult(const TServerModel& model, int64_t errorCode) const = 0;
};

} // namespace core
} // namespace endpoint
} // namespace privmx

#endif // _PRIVMXLIB_ENDPOINT_CORE_TYPEDDATASCHEMASTRATEGY_HPP_
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
PrivMX Endpoint.
Copyright © 2024 Simplito sp. z o.o.

This file is part of the PrivMX Platform (https://privmx.dev).
This software is Licensed under the PrivMX Free License.

See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef _PRIVMXLIB_ENDPOINT_CORE_VERSIONSTRATEGYMAPPER_HPP_
#define _PRIVMXLIB_ENDPOINT_CORE_VERSIONSTRATEGYMAPPER_HPP_

#include <cstdint>
#include <memory>
#include <unordered_map>

#include "privmx/endpoint/core/encryptors/IDataSchemaStrategy.hpp"

namespace privmx {
namespace endpoint {
namespace core {

template<typename TServerModel, typename TDomainObject>
class VersionStrategyMapper {
public:
using Strategy = IDataSchemaStrategy<TServerModel, TDomainObject>;
using StrategyPtr = std::shared_ptr<Strategy>;

void registerStrategy(int64_t version, StrategyPtr strategy) { _strategies[version] = std::move(strategy); }

StrategyPtr getStrategy(int64_t version) const {
auto it = _strategies.find(version);
if (it == _strategies.end())
return nullptr;
return it->second;
}

private:
std::unordered_map<int64_t, StrategyPtr> _strategies;
};

} // namespace core
} // namespace endpoint
} // namespace privmx

#endif // _PRIVMXLIB_ENDPOINT_CORE_VERSIONSTRATEGYMAPPER_HPP_
7 changes: 2 additions & 5 deletions endpoint/event/include/privmx/endpoint/event/EventApiImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include "privmx/endpoint/event/Events.hpp"
#include "privmx/endpoint/event/ServerApi.hpp"
#include "privmx/endpoint/event/SubscriberImpl.hpp"
#include "privmx/endpoint/event/encryptors/event/EventDataEncryptorV5.hpp"
#include "privmx/endpoint/event/encryptors/event/OldEventDataDecryptor.hpp"
#include "privmx/endpoint/event/encryptors/event/EventDataSchemaMapper.hpp"
#include <atomic>
#include <privmx/crypto/ecc/PrivateKey.hpp>
#include <privmx/endpoint/core/Connection.hpp>
Expand Down Expand Up @@ -75,7 +74,6 @@ class EventApiImpl : public privmx::utils::ManualManagedClass<EventApiImpl> {
const std::string& encryptionKey
);
void validateChannelName(const std::string& channelName);
bool verifyDecryptedEventDataV5(const DecryptedEventDataV5& data);
core::Connection _connection;
privmx::crypto::PrivateKey _userPrivKey;
privfs::RpcGateway::Ptr _gateway;
Expand All @@ -86,9 +84,8 @@ class EventApiImpl : public privmx::utils::ManualManagedClass<EventApiImpl> {
EventKeyProvider _eventKeyProvider;
SubscriberImpl _subscriber;
int _notificationListenerId, _connectedListenerId, _disconnectedListenerId;
EventDataEncryptorV5 _eventDataEncryptorV5;
OldEventDataDecryptor _oldEventDataDecryptor;
std::shared_ptr<privmx::utils::GuardedExecutor> _guardedExecutor;
EventDataSchemaMapper _eventDataSchemaMapper;
};

} // namespace event
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
PrivMX Endpoint.
Copyright © 2024 Simplito sp. z o.o.

This file is part of the PrivMX Platform (https://privmx.dev).
This software is Licensed under the PrivMX Free License.

See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef _PRIVMXLIB_ENDPOINT_EVENT_EVENTDATASCHEMAMAPPER_HPP_
#define _PRIVMXLIB_ENDPOINT_EVENT_EVENTDATASCHEMAMAPPER_HPP_

#include <cstdint>
#include <memory>
#include <optional>
#include <string>

#include <Poco/Dynamic/Var.h>
#include <privmx/crypto/ecc/PrivateKey.hpp>
#include <privmx/endpoint/core/Buffer.hpp>
#include <privmx/endpoint/core/Connection.hpp>
#include <privmx/endpoint/core/CoreTypes.hpp>
#include <privmx/endpoint/core/encryptors/VersionStrategyMapper.hpp>

#include "privmx/endpoint/event/Constants.hpp"
#include "privmx/endpoint/event/EventKeyProvider.hpp"
#include "privmx/endpoint/event/EventTypes.hpp"
#include "privmx/endpoint/event/Events.hpp"
#include "privmx/endpoint/event/ServerTypes.hpp"
#include "privmx/endpoint/event/encryptors/event/EventDataEncryptorV5.hpp"
#include "privmx/endpoint/event/encryptors/event/EventDataSchemaStrategyV5.hpp"

namespace privmx {
namespace endpoint {
namespace event {

class EventDataSchemaMapper {
public:
EventDataSchemaMapper(const privmx::crypto::PrivateKey& userPrivKey, const core::Connection& connection);

Poco::Dynamic::Var encrypt(
const std::string& contextId,
const core::Buffer& data,
const std::optional<std::string>& type,
const std::string& key
);

ContextCustomEventData decrypt(const server::ContextCustomEventData& rawEvent);
DecryptedInternalContextEventDataV1 decryptInternal(const server::ContextCustomEventData& rawEvent);

EventDataSchema::Version getDataStructureVersion(const server::ContextCustomEventData& rawEvent);

private:
static ContextCustomEventData makeErrorResult(const server::ContextCustomEventData& rawEvent, int64_t errorCode);
bool verifyDecryptedEventData(const DecryptedEventDataV5& data);

privmx::crypto::PrivateKey _userPrivKey;
core::Connection _connection;
EventKeyProvider _eventKeyProvider;
core::VersionStrategyMapper<server::ContextCustomEventData, ContextCustomEventData> _strategyMapper;
std::shared_ptr<EventDataSchemaStrategyV5> _strategyV5;
EventDataEncryptorV5 _encryptorV5;
};

} // namespace event
} // namespace endpoint
} // namespace privmx

#endif // _PRIVMXLIB_ENDPOINT_EVENT_EVENTDATASCHEMAMAPPER_HPP_
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
PrivMX Endpoint.
Copyright © 2024 Simplito sp. z o.o.

This file is part of the PrivMX Platform (https://privmx.dev).
This software is Licensed under the PrivMX Free License.

See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef _PRIVMXLIB_ENDPOINT_EVENT_EVENTDATASCHEMASTRATEGYV5_HPP_
#define _PRIVMXLIB_ENDPOINT_EVENT_EVENTDATASCHEMASTRATEGYV5_HPP_

#include <privmx/endpoint/core/CoreTypes.hpp>
#include <privmx/endpoint/core/encryptors/TypedDataSchemaStrategy.hpp>

#include "privmx/endpoint/event/EventTypes.hpp"
#include "privmx/endpoint/event/Events.hpp"
#include "privmx/endpoint/event/ServerTypes.hpp"
#include "privmx/endpoint/event/encryptors/event/EventDataEncryptorV5.hpp"

namespace privmx {
namespace endpoint {
namespace event {

// clang-format off
class EventDataSchemaStrategyV5 : public core::TypedDataSchemaStrategy<
server::ContextCustomEventData,
DecryptedEventDataV5,
ContextCustomEventData
> {
// clang-format on
public:
DecryptedEventDataV5 decrypt(
const server::ContextCustomEventData& model,
const core::DecryptedEncKey& encKey
) const override;
ContextCustomEventData convert(
const server::ContextCustomEventData& model,
const DecryptedEventDataV5& raw
) const override;
ContextCustomEventData makeErrorResult(
const server::ContextCustomEventData& model,
int64_t errorCode
) const override;

static ContextCustomEventData toContextCustomEventData(
const server::ContextCustomEventData& raw,
const core::Buffer& payload,
int64_t statusCode,
int64_t schemaVersion
);

private:
mutable EventDataEncryptorV5 _encryptor;
};

} // namespace event
} // namespace endpoint
} // namespace privmx

#endif // _PRIVMXLIB_ENDPOINT_EVENT_EVENTDATASCHEMASTRATEGYV5_HPP_

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ DECLARE_ENDPOINT_EXCEPTION(EndpointEventException, NotSubscribedException, "Not
DECLARE_ENDPOINT_EXCEPTION(EndpointEventException, AlreadySubscribedException, "Already subscribed", 0x0005)
DECLARE_ENDPOINT_EXCEPTION(EndpointEventException, InvalidEncryptedEventDataVersionException, "Invalid version of encrypted event data", 0x0005)
DECLARE_ENDPOINT_EXCEPTION(EndpointEventException, InvalidSubscriptionQueryException, "Invalid subscriptionQuery", 0x0006)
DECLARE_ENDPOINT_EXCEPTION(EndpointEventException, UnknownEventFormatException, "Unknown event format", 0x0007)

// clang-format on
} // namespace event
} // namespace endpoint
Expand Down
Loading
Loading