24 #include <unordered_map>
40 struct callbackUserData;
45 template <
typename Curve>
46 class Lime :
public LimeGeneric,
public std::enable_shared_from_this<Lime<Curve>> {
50 std::shared_ptr<RNG> m_RNG;
51 std::string m_selfDeviceId;
55 std::shared_ptr<X3DH> m_X3DH;
58 std::shared_ptr<lime::Db> m_localStorage;
62 std::unordered_map<std::string, std::shared_ptr<DR>> m_DR_sessions_cache;
65 std::shared_ptr<callbackUserData> m_ongoing_encryption;
66 std::queue<std::shared_ptr<callbackUserData>> m_encryption_queue;
69 void cache_DR_sessions(std::vector<RecipientInfos> &internal_recipients, std::vector<std::string> &missing_devices);
70 void get_DRSessions(
const std::string &senderDeviceId,
const long int ignoreThisDRSessionId, std::vector<std::shared_ptr<DR>> &DRSessions);
73 Lime(std::shared_ptr<lime::Db> localStorage,
const std::string &deviceId,
const std::string &url,
const limeX3DHServerPostData &X3DH_post_data,
const long int Uid = 0);
78 void publish_user(
const std::shared_ptr<limeCallback> callback,
const uint16_t OPkInitialBatchSize)
override;
79 void delete_user(
const std::shared_ptr<limeCallback> callback)
override;
81 void update_SPk(
const std::shared_ptr<limeCallback> callback)
override;
82 void update_OPk(
const std::shared_ptr<limeCallback> callback, uint16_t OPkServerLowLimit, uint16_t OPkBatchSize)
override;
83 void get_Ik(std::vector<uint8_t> &Ik)
override;
84 void encrypt(std::shared_ptr<lime::EncryptionContext> encryptionContext,
const std::shared_ptr<limeCallback> callback,
const std::shared_ptr<limeRandomSeedCallback> randomSeedCallback)
override;
85 lime::PeerDeviceStatus decrypt(
const std::vector<uint8_t> &recipientUserId,
const std::string &senderDeviceId,
const std::vector<uint8_t> &DRmessage,
const std::vector<uint8_t> &
cipherMessage, std::vector<uint8_t> &plainMessage)
override;
91 void DRcache_insert(
const std::string &deviceId, std::shared_ptr<DR> DRsession)
override;
92 std::shared_ptr<X3DH>
get_X3DH(
void)
override {
return m_X3DH;}
93 std::unique_lock<std::mutex>
lock(
void)
override {
return std::unique_lock<std::mutex>(m_mutex);}
115 : limeObj{thiz}, callback{callback}, randomSeedCallback{
nullptr},
119 callbackUserData(std::weak_ptr<LimeGeneric> thiz,
const std::shared_ptr<limeCallback> callback, uint16_t OPkServerLowLimit, uint16_t OPkBatchSize)
120 : limeObj{thiz}, callback{callback}, randomSeedCallback{
nullptr},
121 encryptionContext{
nullptr}, OPkServerLowLimit{OPkServerLowLimit}, OPkBatchSize{OPkBatchSize} {};
124 callbackUserData(std::weak_ptr<LimeGeneric> thiz,
const std::shared_ptr<limeCallback> callback,
const std::shared_ptr<limeRandomSeedCallback> randomSeedCallback,
125 std::shared_ptr<lime::EncryptionContext> encryptionContext)
126 : limeObj{thiz}, callback{callback}, randomSeedCallback{randomSeedCallback},
137 #ifdef EC25519_ENABLED
138 extern template class Lime<C255>;
142 extern template class Lime<C448>;
145 #ifdef EC25519_ENABLED
146 extern template class Lime<C255K512>;
147 extern template class Lime<C255MLK512>;
150 extern template class Lime<C448MLK1024>;
constexpr uint16_t OPk_initialBatchSize
default batch size when creating a new user
Definition: lime_settings.hpp:90
void set_x3dhServerUrl(const std::string &x3dhServerUrl) override
Set the X3DH key server URL for this identified user.
Definition: lime.cpp:375
structure holding user data while waiting for callback from X3DH server response processing ...
Definition: lime_impl.hpp:99
callbackUserData(std::weak_ptr< LimeGeneric > thiz, const std::shared_ptr< limeCallback > callback, const std::shared_ptr< limeRandomSeedCallback > randomSeedCallback, std::shared_ptr< lime::EncryptionContext > encryptionContext)
created at encrypt(getPeerBundle)
Definition: lime_impl.hpp:124
~Lime()
Definition: lime.cpp:162
std::weak_ptr< LimeGeneric > limeObj
limeObj is owned by the LimeManager, it shall no be destructed, do not own this with a shared_ptr as ...
Definition: lime_impl.hpp:101
void processEncryptionQueue(void) override
Check if we have queued encryption to process, if yes, do it.
Definition: lime.cpp:396
uint16_t OPkBatchSize
Used when fetching from server self OPk : how many will we upload if needed.
Definition: lime_impl.hpp:111
lime::PeerDeviceStatus decrypt(const std::vector< uint8_t > &recipientUserId, const std::string &senderDeviceId, const std::vector< uint8_t > &DRmessage, const std::vector< uint8_t > &cipherMessage, std::vector< uint8_t > &plainMessage) override
Decrypt the given message.
Definition: lime.cpp:307
std::shared_ptr< X3DH > get_X3DH(void) override
accessor to the internal X3DH engine
Definition: lime_impl.hpp:92
void delete_user(const std::shared_ptr< limeCallback > callback) override
Delete user from local Storage and from X3DH server.
Definition: lime.cpp:178
std::function< void(const std::string &url, const std::string &from, std::vector< uint8_t > &&message, const limeX3DHServerResponseProcess &reponseProcess)> limeX3DHServerPostData
Post a message to the X3DH server.
Definition: lime.hpp:123
Implement the abstract class LimeGeneric.
Definition: lime_impl.hpp:46
uint16_t OPkServerLowLimit
Used when fetching from server self OPk to check if we shall upload more.
Definition: lime_impl.hpp:109
void stale_sessions(const std::string &peerDeviceId) override
Stale all sessions between localDeviceId and peerDevice. If peerDevice keep using this session to enc...
Definition: lime.cpp:380
PeerDeviceStatus
Definition: lime.hpp:59
void get_Ik(std::vector< uint8_t > &Ik) override
Retrieve self public Identity key.
Definition: lime.cpp:215
void publish_user(const std::shared_ptr< limeCallback > callback, const uint16_t OPkInitialBatchSize) override
Publish on X3DH server the user, it is performed just after creation in local storage this will...
Definition: lime.cpp:171
network_state
Definition: lime_impl.hpp:38
void update_OPk(const std::shared_ptr< limeCallback > callback, uint16_t OPkServerLowLimit, uint16_t OPkBatchSize) override
check if we shall upload more OPks on X3DH server
Definition: lime.cpp:206
Lime(std::shared_ptr< lime::Db > localStorage, const std::string &deviceId, const std::string &url, const limeX3DHServerPostData &X3DH_post_data, const long int Uid=0)
Load or Create user constructor.
Definition: lime.cpp:154
callbackUserData(std::weak_ptr< LimeGeneric > thiz, const std::shared_ptr< limeCallback > callback, uint16_t OPkServerLowLimit, uint16_t OPkBatchSize)
created at update: getSelfOPks. EncryptionPolicy is not used, set it to the default value anyway ...
Definition: lime_impl.hpp:119
void delete_peerDevice(const std::string &peerDeviceId) override
Purge cached sessions for a given peer Device (used when a peer device is being deleted) ...
Definition: lime.cpp:188
callbackUserData(std::weak_ptr< LimeGeneric > thiz, const std::shared_ptr< limeCallback > callback, uint16_t OPkInitialBatchSize=lime::settings::OPk_initialBatchSize)
created at user create/delete and keys Post. EncryptionPolicy is not used, set it to the default valu...
Definition: lime_impl.hpp:114
callbackUserData operator=(callbackUserData &a)=delete
do not copy callback data, force passing the pointer around after creation
std::shared_ptr< lime::EncryptionContext > encryptionContext
Encryption context :AD, plain and cipher text, encryption policy and recipients.
Definition: lime_impl.hpp:107
const std::shared_ptr< limeRandomSeedCallback > randomSeedCallback
this is a ptr to a lambda, we may use it several time so we can manage precisely its timelife ...
Definition: lime_impl.hpp:105
std::unique_lock< std::mutex > lock(void) override
Definition: lime_impl.hpp:93
Lime< Curve > & operator=(Lime< Curve > &a)=delete
std::string get_x3dhServerUrl() override
Get the X3DH key server URL for this identified user.
Definition: lime.cpp:370
void DRcache_delete(const std::string &deviceId) override
delete an entry (if found) from the DR session cache
Definition: lime.cpp:407
void encrypt(std::shared_ptr< lime::EncryptionContext > encryptionContext, const std::shared_ptr< limeCallback > callback, const std::shared_ptr< limeRandomSeedCallback > randomSeedCallback) override
Encrypt a buffer (text or file) for a given list of recipient devices.
Definition: lime.cpp:220
const std::shared_ptr< limeCallback > callback
this is a ptr to a lambda, we may use it several time so we can manage precisely its timelife ...
Definition: lime_impl.hpp:103
A pure abstract class defining the API to encrypt/decrypt/manage user and its keys.
Definition: lime_lime.hpp:42
void DRcache_insert(const std::string &deviceId, std::shared_ptr< DR > DRsession) override
insert an entry in the DR session cache if an entry with the same key already exists, do nothing
Definition: lime.cpp:412
void update_SPk(const std::shared_ptr< limeCallback > callback) override
Check if the current SPk needs to be updated, if yes, generate a new one and publish it on server...
Definition: lime.cpp:193