Skip to content

Commit 171b46c

Browse files
Uriagatdjenczewski
authored andcommitted
Refactor/endpoint encryptors and decryptors (#433)
* feat: IDecryptStrategy * refactor: added DataStrategy for thread module and removed unused V1, V2, V3 formats * refactor: added DataStrategy for store module and removed unused V1, V2, V3 formats * refactor: added DataStrategy for inbox module * refactor: added DataStrategy for kvdb module * refactor: added DataStrategy for event module and removed unused V1 * refactor: added DataStrategy for stream module * refactor: formating * fix: post review changes
1 parent 2381ff3 commit 171b46c

83 files changed

Lines changed: 5710 additions & 3778 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
PrivMX Endpoint.
3+
Copyright © 2024 Simplito sp. z o.o.
4+
5+
This file is part of the PrivMX Platform (https://privmx.dev).
6+
This software is Licensed under the PrivMX Free License.
7+
8+
See the License for the specific language governing permissions and
9+
limitations under the License.
10+
*/
11+
12+
#ifndef _PRIVMXLIB_ENDPOINT_CORE_IDATASCHEMASTRATEGY_HPP_
13+
#define _PRIVMXLIB_ENDPOINT_CORE_IDATASCHEMASTRATEGY_HPP_
14+
15+
#include <privmx/endpoint/core/CoreTypes.hpp>
16+
17+
namespace privmx {
18+
namespace endpoint {
19+
namespace core {
20+
21+
template<typename TServerModel, typename TDomainObject>
22+
class IDataSchemaStrategy {
23+
public:
24+
virtual ~IDataSchemaStrategy() = default;
25+
virtual TDomainObject decryptAndConvert(const TServerModel& model, const DecryptedEncKey& encKey) const = 0;
26+
};
27+
28+
} // namespace core
29+
} // namespace endpoint
30+
} // namespace privmx
31+
32+
#endif // _PRIVMXLIB_ENDPOINT_CORE_IDATASCHEMASTRATEGY_HPP_
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
PrivMX Endpoint.
3+
Copyright © 2024 Simplito sp. z o.o.
4+
5+
This file is part of the PrivMX Platform (https://privmx.dev).
6+
This software is Licensed under the PrivMX Free License.
7+
8+
See the License for the specific language governing permissions and
9+
limitations under the License.
10+
*/
11+
12+
#ifndef _PRIVMXLIB_ENDPOINT_CORE_TYPEDDATASCHEMASTRATEGY_HPP_
13+
#define _PRIVMXLIB_ENDPOINT_CORE_TYPEDDATASCHEMASTRATEGY_HPP_
14+
15+
#include <privmx/endpoint/core/CoreConstants.hpp>
16+
#include <privmx/endpoint/core/ExceptionConverter.hpp>
17+
18+
#include "privmx/endpoint/core/encryptors/IDataSchemaStrategy.hpp"
19+
20+
namespace privmx {
21+
namespace endpoint {
22+
namespace core {
23+
24+
template<typename TServerModel, typename TRawData, typename TDomainObject>
25+
class TypedDataSchemaStrategy : public IDataSchemaStrategy<TServerModel, TDomainObject> {
26+
public:
27+
TDomainObject decryptAndConvert(const TServerModel& model, const DecryptedEncKey& encKey) const override final {
28+
try {
29+
return convert(model, decrypt(model, encKey));
30+
} catch (const core::Exception& e) {
31+
return makeErrorResult(model, e.getCode());
32+
} catch (const privmx::utils::PrivmxException& e) {
33+
return makeErrorResult(model, ExceptionConverter::convert(e).getCode());
34+
} catch (...) { return makeErrorResult(model, ENDPOINT_CORE_EXCEPTION_CODE); }
35+
}
36+
37+
virtual TRawData decrypt(const TServerModel& model, const DecryptedEncKey& encKey) const = 0;
38+
virtual TDomainObject convert(const TServerModel& model, const TRawData& raw) const = 0;
39+
virtual TDomainObject makeErrorResult(const TServerModel& model, int64_t errorCode) const = 0;
40+
};
41+
42+
} // namespace core
43+
} // namespace endpoint
44+
} // namespace privmx
45+
46+
#endif // _PRIVMXLIB_ENDPOINT_CORE_TYPEDDATASCHEMASTRATEGY_HPP_
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
PrivMX Endpoint.
3+
Copyright © 2024 Simplito sp. z o.o.
4+
5+
This file is part of the PrivMX Platform (https://privmx.dev).
6+
This software is Licensed under the PrivMX Free License.
7+
8+
See the License for the specific language governing permissions and
9+
limitations under the License.
10+
*/
11+
12+
#ifndef _PRIVMXLIB_ENDPOINT_CORE_VERSIONSTRATEGYMAPPER_HPP_
13+
#define _PRIVMXLIB_ENDPOINT_CORE_VERSIONSTRATEGYMAPPER_HPP_
14+
15+
#include <cstdint>
16+
#include <memory>
17+
#include <unordered_map>
18+
19+
#include "privmx/endpoint/core/encryptors/IDataSchemaStrategy.hpp"
20+
21+
namespace privmx {
22+
namespace endpoint {
23+
namespace core {
24+
25+
template<typename TServerModel, typename TDomainObject>
26+
class VersionStrategyMapper {
27+
public:
28+
using Strategy = IDataSchemaStrategy<TServerModel, TDomainObject>;
29+
using StrategyPtr = std::shared_ptr<Strategy>;
30+
31+
void registerStrategy(int64_t version, StrategyPtr strategy) { _strategies[version] = std::move(strategy); }
32+
33+
StrategyPtr getStrategy(int64_t version) const {
34+
auto it = _strategies.find(version);
35+
if (it == _strategies.end())
36+
return nullptr;
37+
return it->second;
38+
}
39+
40+
private:
41+
std::unordered_map<int64_t, StrategyPtr> _strategies;
42+
};
43+
44+
} // namespace core
45+
} // namespace endpoint
46+
} // namespace privmx
47+
48+
#endif // _PRIVMXLIB_ENDPOINT_CORE_VERSIONSTRATEGYMAPPER_HPP_

endpoint/event/include/privmx/endpoint/event/EventApiImpl.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#include "privmx/endpoint/event/Events.hpp"
66
#include "privmx/endpoint/event/ServerApi.hpp"
77
#include "privmx/endpoint/event/SubscriberImpl.hpp"
8-
#include "privmx/endpoint/event/encryptors/event/EventDataEncryptorV5.hpp"
9-
#include "privmx/endpoint/event/encryptors/event/OldEventDataDecryptor.hpp"
8+
#include "privmx/endpoint/event/encryptors/event/EventDataSchemaMapper.hpp"
109
#include <atomic>
1110
#include <privmx/crypto/ecc/PrivateKey.hpp>
1211
#include <privmx/endpoint/core/Connection.hpp>
@@ -75,7 +74,6 @@ class EventApiImpl : public privmx::utils::ManualManagedClass<EventApiImpl> {
7574
const std::string& encryptionKey
7675
);
7776
void validateChannelName(const std::string& channelName);
78-
bool verifyDecryptedEventDataV5(const DecryptedEventDataV5& data);
7977
core::Connection _connection;
8078
privmx::crypto::PrivateKey _userPrivKey;
8179
privfs::RpcGateway::Ptr _gateway;
@@ -86,9 +84,8 @@ class EventApiImpl : public privmx::utils::ManualManagedClass<EventApiImpl> {
8684
EventKeyProvider _eventKeyProvider;
8785
SubscriberImpl _subscriber;
8886
int _notificationListenerId, _connectedListenerId, _disconnectedListenerId;
89-
EventDataEncryptorV5 _eventDataEncryptorV5;
90-
OldEventDataDecryptor _oldEventDataDecryptor;
9187
std::shared_ptr<privmx::utils::GuardedExecutor> _guardedExecutor;
88+
EventDataSchemaMapper _eventDataSchemaMapper;
9289
};
9390

9491
} // namespace event
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
PrivMX Endpoint.
3+
Copyright © 2024 Simplito sp. z o.o.
4+
5+
This file is part of the PrivMX Platform (https://privmx.dev).
6+
This software is Licensed under the PrivMX Free License.
7+
8+
See the License for the specific language governing permissions and
9+
limitations under the License.
10+
*/
11+
12+
#ifndef _PRIVMXLIB_ENDPOINT_EVENT_EVENTDATASCHEMAMAPPER_HPP_
13+
#define _PRIVMXLIB_ENDPOINT_EVENT_EVENTDATASCHEMAMAPPER_HPP_
14+
15+
#include <cstdint>
16+
#include <memory>
17+
#include <optional>
18+
#include <string>
19+
20+
#include <Poco/Dynamic/Var.h>
21+
#include <privmx/crypto/ecc/PrivateKey.hpp>
22+
#include <privmx/endpoint/core/Buffer.hpp>
23+
#include <privmx/endpoint/core/Connection.hpp>
24+
#include <privmx/endpoint/core/CoreTypes.hpp>
25+
#include <privmx/endpoint/core/encryptors/VersionStrategyMapper.hpp>
26+
27+
#include "privmx/endpoint/event/Constants.hpp"
28+
#include "privmx/endpoint/event/EventKeyProvider.hpp"
29+
#include "privmx/endpoint/event/EventTypes.hpp"
30+
#include "privmx/endpoint/event/Events.hpp"
31+
#include "privmx/endpoint/event/ServerTypes.hpp"
32+
#include "privmx/endpoint/event/encryptors/event/EventDataEncryptorV5.hpp"
33+
#include "privmx/endpoint/event/encryptors/event/EventDataSchemaStrategyV5.hpp"
34+
35+
namespace privmx {
36+
namespace endpoint {
37+
namespace event {
38+
39+
class EventDataSchemaMapper {
40+
public:
41+
EventDataSchemaMapper(const privmx::crypto::PrivateKey& userPrivKey, const core::Connection& connection);
42+
43+
Poco::Dynamic::Var encrypt(
44+
const std::string& contextId,
45+
const core::Buffer& data,
46+
const std::optional<std::string>& type,
47+
const std::string& key
48+
);
49+
50+
ContextCustomEventData decrypt(const server::ContextCustomEventData& rawEvent);
51+
DecryptedInternalContextEventDataV1 decryptInternal(const server::ContextCustomEventData& rawEvent);
52+
53+
EventDataSchema::Version getDataStructureVersion(const server::ContextCustomEventData& rawEvent);
54+
55+
private:
56+
static ContextCustomEventData makeErrorResult(const server::ContextCustomEventData& rawEvent, int64_t errorCode);
57+
bool verifyDecryptedEventData(const DecryptedEventDataV5& data);
58+
59+
privmx::crypto::PrivateKey _userPrivKey;
60+
core::Connection _connection;
61+
EventKeyProvider _eventKeyProvider;
62+
core::VersionStrategyMapper<server::ContextCustomEventData, ContextCustomEventData> _strategyMapper;
63+
std::shared_ptr<EventDataSchemaStrategyV5> _strategyV5;
64+
EventDataEncryptorV5 _encryptorV5;
65+
};
66+
67+
} // namespace event
68+
} // namespace endpoint
69+
} // namespace privmx
70+
71+
#endif // _PRIVMXLIB_ENDPOINT_EVENT_EVENTDATASCHEMAMAPPER_HPP_
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
PrivMX Endpoint.
3+
Copyright © 2024 Simplito sp. z o.o.
4+
5+
This file is part of the PrivMX Platform (https://privmx.dev).
6+
This software is Licensed under the PrivMX Free License.
7+
8+
See the License for the specific language governing permissions and
9+
limitations under the License.
10+
*/
11+
12+
#ifndef _PRIVMXLIB_ENDPOINT_EVENT_EVENTDATASCHEMASTRATEGYV5_HPP_
13+
#define _PRIVMXLIB_ENDPOINT_EVENT_EVENTDATASCHEMASTRATEGYV5_HPP_
14+
15+
#include <privmx/endpoint/core/CoreTypes.hpp>
16+
#include <privmx/endpoint/core/encryptors/TypedDataSchemaStrategy.hpp>
17+
18+
#include "privmx/endpoint/event/EventTypes.hpp"
19+
#include "privmx/endpoint/event/Events.hpp"
20+
#include "privmx/endpoint/event/ServerTypes.hpp"
21+
#include "privmx/endpoint/event/encryptors/event/EventDataEncryptorV5.hpp"
22+
23+
namespace privmx {
24+
namespace endpoint {
25+
namespace event {
26+
27+
// clang-format off
28+
class EventDataSchemaStrategyV5 : public core::TypedDataSchemaStrategy<
29+
server::ContextCustomEventData,
30+
DecryptedEventDataV5,
31+
ContextCustomEventData
32+
> {
33+
// clang-format on
34+
public:
35+
DecryptedEventDataV5 decrypt(
36+
const server::ContextCustomEventData& model,
37+
const core::DecryptedEncKey& encKey
38+
) const override;
39+
ContextCustomEventData convert(
40+
const server::ContextCustomEventData& model,
41+
const DecryptedEventDataV5& raw
42+
) const override;
43+
ContextCustomEventData makeErrorResult(
44+
const server::ContextCustomEventData& model,
45+
int64_t errorCode
46+
) const override;
47+
48+
static ContextCustomEventData toContextCustomEventData(
49+
const server::ContextCustomEventData& raw,
50+
const core::Buffer& payload,
51+
int64_t statusCode,
52+
int64_t schemaVersion
53+
);
54+
55+
private:
56+
mutable EventDataEncryptorV5 _encryptor;
57+
};
58+
59+
} // namespace event
60+
} // namespace endpoint
61+
} // namespace privmx
62+
63+
#endif // _PRIVMXLIB_ENDPOINT_EVENT_EVENTDATASCHEMASTRATEGYV5_HPP_

endpoint/event/include/privmx/endpoint/event/encryptors/event/OldEventDataDecryptor.hpp

Lines changed: 0 additions & 41 deletions
This file was deleted.

endpoint/event/include_pub/privmx/endpoint/event/EventException.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ DECLARE_ENDPOINT_EXCEPTION(EndpointEventException, NotSubscribedException, "Not
4242
DECLARE_ENDPOINT_EXCEPTION(EndpointEventException, AlreadySubscribedException, "Already subscribed", 0x0005)
4343
DECLARE_ENDPOINT_EXCEPTION(EndpointEventException, InvalidEncryptedEventDataVersionException, "Invalid version of encrypted event data", 0x0005)
4444
DECLARE_ENDPOINT_EXCEPTION(EndpointEventException, InvalidSubscriptionQueryException, "Invalid subscriptionQuery", 0x0006)
45+
DECLARE_ENDPOINT_EXCEPTION(EndpointEventException, UnknownEventFormatException, "Unknown event format", 0x0007)
46+
4547
// clang-format on
4648
} // namespace event
4749
} // namespace endpoint

0 commit comments

Comments
 (0)