24 #include <unordered_map>
39 template <
typename Curve>
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;
59 std::shared_ptr<lime::Db> m_localStorage;
64 std::string m_X3DH_Server_URL;
67 std::unordered_map<std::string, std::shared_ptr<DR<Curve>>> m_DR_sessions_cache;
70 std::shared_ptr<callbackUserData<Curve>> m_ongoing_encryption;
71 std::queue<std::shared_ptr<callbackUserData<Curve>>> m_encryption_queue;
80 void get_SelfIdentityKey();
81 void cache_DR_sessions(std::vector<
RecipientInfos<Curve>> &internal_recipients, std::vector<std::string> &missing_devices);
82 void get_DRSessions(
const std::string &senderDeviceId,
const long int ignoreThisDRSessionId, std::vector<std::shared_ptr<
DR<Curve>>> &DRSessions);
86 void X3DH_generate_OPks(std::vector<
X<Curve, lime::Xtype::publicKey>> &publicOPks, std::vector<uint32_t> &OPk_ids,
const uint16_t OPk_number,
const bool load=
false);
88 bool is_currentSPk_valid(
void);
90 void X3DH_updateOPkStatus(
const std::vector<uint32_t> &OPkIds);
93 std::shared_ptr<DR<Curve>> X3DH_init_receiver_session(
const std::vector<uint8_t> X3DH_initMessage,
const std::string &senderDeviceId);
97 void process_response(std::shared_ptr<
callbackUserData<Curve>> userData,
int responseCode,
const std::vector<uint8_t> &responseBody) noexcept;
101 Lime(std::unique_ptr<lime::Db> &&localStorage,
const std::string &deviceId,
const std::string &url,
const limeX3DHServerPostData &X3DH_post_data);
102 Lime(std::unique_ptr<lime::Db> &&localStorage,
const std::string &deviceId,
const std::string &url,
const limeX3DHServerPostData &X3DH_post_data,
const long int Uid);
110 void update_OPk(
const limeCallback &callback, uint16_t OPkServerLowLimit, uint16_t OPkBatchSize)
override;
111 void get_Ik(std::vector<uint8_t> &Ik)
override;
112 void encrypt(std::shared_ptr<const std::string> recipientUserId, std::shared_ptr<std::vector<RecipientData>> recipients, std::shared_ptr<
const std::vector<uint8_t>> plainMessage,
const lime::EncryptionPolicy encryptionPolicy, std::shared_ptr<std::vector<uint8_t>>
cipherMessage,
const limeCallback &callback)
override;
113 lime::PeerDeviceStatus decrypt(
const std::string &recipientUserId,
const std::string &senderDeviceId,
const std::vector<uint8_t> &DRmessage,
const std::vector<uint8_t> &
cipherMessage, std::vector<uint8_t> &plainMessage)
override;
122 template <
typename Curve>
145 : limeObj{thiz}, callback{callbackRef},
146 recipientUserId{
nullptr}, recipients{
nullptr}, plainMessage{
nullptr}, cipherMessage{
nullptr},
151 : limeObj{thiz}, callback{callbackRef},
152 recipientUserId{
nullptr}, recipients{
nullptr}, plainMessage{
nullptr}, cipherMessage{
nullptr},
157 std::shared_ptr<const std::string> recipientUserId, std::shared_ptr<std::vector<RecipientData>> recipients,
158 std::shared_ptr<
const std::vector<uint8_t>> plainMessage, std::shared_ptr<std::vector<uint8_t>> cipherMessage,
160 : limeObj{thiz}, callback{callbackRef},
161 recipientUserId{recipientUserId}, recipients{recipients}, plainMessage{plainMessage}, cipherMessage{cipherMessage},
172 #ifdef EC25519_ENABLED
173 extern template class Lime<C255>;
177 extern template class Lime<C448>;
constexpr uint16_t OPk_initialBatchSize
default batch size when creating a new user
Definition: lime_settings.hpp:81
void set_x3dhServerUrl(const std::string &x3dhServerUrl) override
Set the X3DH key server URL for this identified user.
Definition: lime_localStorage.cpp:1334
std::shared_ptr< const std::vector< uint8_t > > plainMessage
plaintext. Needed for encryption: get a shared ref to keep params alive
Definition: lime_impl.hpp:133
void publish_user(const 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:94
store a Double Rachet session.
Definition: lime_double_ratchet.hpp:82
std::shared_ptr< std::vector< uint8_t > > cipherMessage
ciphertext buffer. Needed for encryption: get a shared ref to keep params alive
Definition: lime_impl.hpp:135
void encrypt(std::shared_ptr< const std::string > recipientUserId, std::shared_ptr< std::vector< RecipientData >> recipients, std::shared_ptr< const std::vector< uint8_t >> plainMessage, const lime::EncryptionPolicy encryptionPolicy, std::shared_ptr< std::vector< uint8_t >> cipherMessage, const limeCallback &callback) override
Encrypt a buffer(text or file) for a given list of recipient devices if specified localDeviceId is no...
Definition: lime.cpp:169
structure holding user data while waiting for callback from X3DH server response processing ...
Definition: lime_impl.hpp:40
Holds everything found in a key bundle received from X3DH server.
Definition: lime_x3dh_protocol.hpp:41
callbackUserData operator=(callbackUserData &a)=delete
do not copy callback data, force passing the pointer around after creation
Key pair structure for DSA algorithm.
Definition: lime_crypto_primitives.hpp:112
Key pair structure for key exchange algorithm.
Definition: lime_crypto_primitives.hpp:71
lime::EncryptionPolicy encryptionPolicy
the encryption policy from the original encryption request(if running an encryption request)...
Definition: lime_impl.hpp:137
uint16_t OPkBatchSize
Used when fetching from server self OPk : how many will we upload if needed.
Definition: lime_impl.hpp:141
~Lime()
Definition: lime.cpp:85
void update_OPk(const limeCallback &callback, uint16_t OPkServerLowLimit, uint16_t OPkBatchSize) override
check if we shall upload more OPks on X3DH server
Definition: lime.cpp:151
lime::PeerDeviceStatus decrypt(const std::string &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:251
std::function< void(const std::string &url, const std::string &from, const std::vector< uint8_t > &message, const limeX3DHServerResponseProcess &reponseProcess)> limeX3DHServerPostData
Post a message to the X3DH server.
Definition: lime.hpp:115
const limeCallback callback
is a lambda closure, not real idea of what is its lifetime but it seems ok to hold it this way ...
Definition: lime_impl.hpp:127
Implement the abstract class LimeGeneric.
Definition: lime_impl.hpp:46
callbackUserData(std::weak_ptr< Lime< Curve >> thiz, const limeCallback &callbackRef, 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:150
std::function< void(const lime::CallbackReturn status, const std::string message)> limeCallback
Callback use to give a status on asynchronous operation.
Definition: lime.hpp:95
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_localStorage.cpp:1352
PeerDeviceStatus
Definition: lime.hpp:53
void get_Ik(std::vector< uint8_t > &Ik) override
Retrieve self public Identity key.
Definition: lime.cpp:162
network_state
Definition: lime_impl.hpp:37
std::shared_ptr< std::vector< RecipientData > > recipients
Recipient data vector. Needed for encryption: get a shared ref to keep params alive.
Definition: lime_impl.hpp:131
callbackUserData(std::weak_ptr< Lime< Curve >> thiz, const limeCallback &callbackRef, std::shared_ptr< const std::string > recipientUserId, std::shared_ptr< std::vector< RecipientData >> recipients, std::shared_ptr< const std::vector< uint8_t >> plainMessage, std::shared_ptr< std::vector< uint8_t >> cipherMessage, lime::EncryptionPolicy policy)
created at encrypt(getPeerBundle)
Definition: lime_impl.hpp:156
std::shared_ptr< const std::string > recipientUserId
Recipient username. Needed for encryption: get a shared ref to keep params alive. ...
Definition: lime_impl.hpp:129
uint16_t OPkServerLowLimit
Used when fetching from server self OPk to check if we shall upload more.
Definition: lime_impl.hpp:139
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:127
void update_SPk(const 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:132
void delete_user(const limeCallback &callback) override
Delete user from local Storage and from X3DH server.
Definition: lime.cpp:115
extend the RecipientData to add a Double Ratchet session shared with the recipient ...
Definition: lime_double_ratchet.hpp:143
Lime(std::unique_ptr< lime::Db > &&localStorage, const std::string &deviceId, const std::string &url, const limeX3DHServerPostData &X3DH_post_data)
Create user constructor.
Definition: lime.cpp:74
std::weak_ptr< Lime< Curve > > 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:125
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:312
EncryptionPolicy
Definition: lime.hpp:41
A pure abstract class defining the API to encrypt/decrypt/manage user and its keys.
Definition: lime_lime.hpp:35
callbackUserData(std::weak_ptr< Lime< Curve >> thiz, const limeCallback &callbackRef, 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:144