Loading...
Searching...
No Matches
lime.hpp
Go to the documentation of this file.
/*
lime.hpp
@author Johan Pascal
@copyright Copyright (C) 2017 Belledonne Communications SARL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef lime_hpp
#define lime_hpp
#include <memory> //smart ptrs
#include <unordered_map>
#include <vector>
#include <list>
#include <functional>
#include <string>
#include <mutex>
namespace lime {
enum class CurveId : uint8_t {
unset=0,
c25519=1,
c448=2
};
enum class EncryptionPolicy {
DRMessage,
cipherMessage,
optimizeUploadSize,
optimizeGlobalBandwidth
};
enum class PeerDeviceStatus : uint8_t {
untrusted=0,
trusted=1,
unsafe=2,
fail,
unknown
};
struct RecipientData {
const std::string deviceId;
lime::PeerDeviceStatus peerStatus;
std::vector<uint8_t> DRmessage;
RecipientData(const std::string &deviceId) : deviceId{deviceId}, peerStatus{lime::PeerDeviceStatus::unknown}, DRmessage{} {};
};
enum class CallbackReturn : uint8_t {
success,
fail
};
using limeCallback = std::function<void(const lime::CallbackReturn status, const std::string message)>;
/* X3DH server communication : these functions prototypes are used to post data and get response from/to the X3DH server */
using limeX3DHServerResponseProcess = std::function<void(int responseCode, const std::vector<uint8_t> &responseBody)>;
using limeX3DHServerPostData = std::function<void(const std::string &url, const std::string &from, const std::vector<uint8_t> &message, const limeX3DHServerResponseProcess &reponseProcess)>;
/* Forward declare the class managing one lime user and class managing database */
class LimeGeneric;
class Db;
class LimeManager {
private :
std::unordered_map<std::string, std::shared_ptr<LimeGeneric>> m_users_cache; // cache of already opened Lime Session, identified by user Id (GRUU)
std::mutex m_users_mutex; // m_users_cache mutex
std::shared_ptr<lime::Db> m_localStorage; // DB access information forwarded to SOCI to correctly access database
limeX3DHServerPostData m_X3DH_post_data; // send data to the X3DH key server
void load_user(std::shared_ptr<LimeGeneric> &user, const std::string &localDeviceId, const bool allStatus=false); // helper function, get from m_users_cache of local Storage the requested Lime object
public :
void create_user(const std::string &localDeviceId, const std::string &x3dhServerUrl, const lime::CurveId curve, const uint16_t OPkInitialBatchSize, const limeCallback &callback);
void create_user(const std::string &localDeviceId, const std::string &x3dhServerUrl, const lime::CurveId curve, const limeCallback &callback);
void delete_user(const std::string &localDeviceId, const limeCallback &callback);
bool is_user(const std::string &localDeviceId);
void encrypt(const std::string &localDeviceId, 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, const limeCallback &callback, lime::EncryptionPolicy encryptionPolicy=lime::EncryptionPolicy::optimizeUploadSize);
void encrypt(const std::string &localDeviceId, std::shared_ptr<const std::vector<uint8_t>> associatedData, std::shared_ptr<std::vector<RecipientData>> recipients, std::shared_ptr<const std::vector<uint8_t>> plainMessage, std::shared_ptr<std::vector<uint8_t>> cipherMessage, const limeCallback &callback, lime::EncryptionPolicy encryptionPolicy=lime::EncryptionPolicy::optimizeUploadSize);
lime::PeerDeviceStatus decrypt(const std::string &localDeviceId, 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);
lime::PeerDeviceStatus decrypt(const std::string &localDeviceId, const std::string &recipientUserId, const std::string &senderDeviceId, const std::vector<uint8_t> &DRmessage, std::vector<uint8_t> &plainMessage);
lime::PeerDeviceStatus decrypt(const std::string &localDeviceId, const std::vector<uint8_t> &associatedData, const std::string &senderDeviceId, const std::vector<uint8_t> &DRmessage, const std::vector<uint8_t> &cipherMessage, std::vector<uint8_t> &plainMessage);
lime::PeerDeviceStatus decrypt(const std::string &localDeviceId, const std::vector<uint8_t> &associatedData, const std::string &senderDeviceId, const std::vector<uint8_t> &DRmessage, std::vector<uint8_t> &plainMessage);
void update(const std::string &localDeviceId, const limeCallback &callback, uint16_t OPkServerLowLimit, uint16_t OPkBatchSize);
void update(const std::string &localDeviceId, const limeCallback &callback);
void get_selfIdentityKey(const std::string &localDeviceId, std::vector<uint8_t> &Ik);
void set_peerDeviceStatus(const std::string &peerDeviceId, const std::vector<uint8_t> &Ik, lime::PeerDeviceStatus status);
void set_peerDeviceStatus(const std::string &peerDeviceId, lime::PeerDeviceStatus status);
lime::PeerDeviceStatus get_peerDeviceStatus(const std::string &peerDeviceId);
lime::PeerDeviceStatus get_peerDeviceStatus(const std::list<std::string> &peerDeviceIds);
bool is_localUser(const std::string &deviceId);
void delete_peerDevice(const std::string &peerDeviceId);
void stale_sessions(const std::string &localDeviceId, const std::string &peerDeviceId);
void set_x3dhServerUrl(const std::string &localDeviceId, const std::string &x3dhServerUrl);
std::string get_x3dhServerUrl(const std::string &localDeviceId);
LimeManager() = delete; // no manager without Database and http provider
LimeManager(const LimeManager&) = delete; // no copy constructor
LimeManager operator=(const LimeManager &) = delete; // nor copy operator
LimeManager(const std::string &db_access, const limeX3DHServerPostData &X3DH_post_data, std::shared_ptr<std::recursive_mutex> db_mutex);
LimeManager(const std::string &db_access, const limeX3DHServerPostData &X3DH_post_data);
~LimeManager() = default;
};
} //namespace lime
#endif /* lime_hpp */
lime::Db
Database access class.
Definition lime_localStorage.hpp:34
lime::LimeGeneric
A pure abstract class defining the API to encrypt/decrypt/manage user and its keys.
Definition lime_lime.hpp:35
lime::LimeManager::operator=
LimeManager operator=(const LimeManager &)=delete
lime::LimeManager::delete_user
void delete_user(const std::string &localDeviceId, const limeCallback &callback)
Delete a user from local database and from the X3DH server.
Definition lime_manager.cpp:81
lime::LimeManager::get_peerDeviceStatus
lime::PeerDeviceStatus get_peerDeviceStatus(const std::string &peerDeviceId)
get the status of a peer device: unknown, untrusted, trusted, unsafe device's Id matching a local acc...
Definition lime_manager.cpp:219
lime::LimeManager::set_peerDeviceStatus
void set_peerDeviceStatus(const std::string &peerDeviceId, const std::vector< uint8_t > &Ik, lime::PeerDeviceStatus status)
set the peer device status flag in local storage: unsafe, trusted or untrusted.
Definition lime_manager.cpp:211
lime::LimeManager::delete_peerDevice
void delete_peerDevice(const std::string &peerDeviceId)
delete a peerDevice from local storage
Definition lime_manager.cpp:231
lime::LimeManager::LimeManager
LimeManager()=delete
lime::LimeManager::is_user
bool is_user(const std::string &localDeviceId)
Check if a user is present and active in local storage.
Definition lime_manager.cpp:99
lime::LimeManager::encrypt
void encrypt(const std::string &localDeviceId, 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, const limeCallback &callback, lime::EncryptionPolicy encryptionPolicy=lime::EncryptionPolicy::optimizeUploadSize)
Encrypt a buffer (text or file) for a given list of recipient devices.
Definition lime_manager.cpp:121
lime::LimeManager::get_x3dhServerUrl
std::string get_x3dhServerUrl(const std::string &localDeviceId)
Get the X3DH key server URL for this identified user.
Definition lime_manager.cpp:262
lime::LimeManager::get_selfIdentityKey
void get_selfIdentityKey(const std::string &localDeviceId, std::vector< uint8_t > &Ik)
retrieve self Identity Key, an EdDSA formatted public key
Definition lime_manager.cpp:204
lime::LimeManager::~LimeManager
~LimeManager()=default
lime::LimeManager::update
void update(const std::string &localDeviceId, const limeCallback &callback, uint16_t OPkServerLowLimit, uint16_t OPkBatchSize)
Update: shall be called regularly, once a day at least, performs checks, updates and cleaning operati...
Definition lime_manager.cpp:161
lime::LimeManager::set_x3dhServerUrl
void set_x3dhServerUrl(const std::string &localDeviceId, const std::string &x3dhServerUrl)
Set the X3DH key server URL for this identified user.
Definition lime_manager.cpp:253
lime::LimeManager::decrypt
lime::PeerDeviceStatus decrypt(const std::string &localDeviceId, 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)
Decrypt the given message.
Definition lime_manager.cpp:147
lime::LimeManager::LimeManager
LimeManager(const LimeManager &)=delete
lime::LimeManager::create_user
void create_user(const std::string &localDeviceId, const std::string &x3dhServerUrl, const lime::CurveId curve, const uint16_t OPkInitialBatchSize, const limeCallback &callback)
Create a user in local database and publish it on the given X3DH server.
Definition lime_manager.cpp:60
lime::LimeManager::stale_sessions
void stale_sessions(const std::string &localDeviceId, const std::string &peerDeviceId)
Stale all sessions between localDeviceId and peerDevice. If peerDevice keep using this session to enc...
Definition lime_manager.cpp:241
lime::LimeManager::is_localUser
bool is_localUser(const std::string &deviceId)
checks if a device iD exists in the local users
Definition lime_manager.cpp:227
lime
Definition lime.cpp:30
lime::CallbackReturn
CallbackReturn
Definition lime.hpp:85
lime::CallbackReturn::success
@ success
Definition lime.hpp:86
lime::EncryptionPolicy
EncryptionPolicy
Definition lime.hpp:42
lime::EncryptionPolicy::cipherMessage
@ cipherMessage
Definition lime.hpp:44
lime::EncryptionPolicy::DRMessage
@ DRMessage
Definition lime.hpp:43
lime::EncryptionPolicy::optimizeGlobalBandwidth
@ optimizeGlobalBandwidth
Definition lime.hpp:46
lime::EncryptionPolicy::optimizeUploadSize
@ optimizeUploadSize
Definition lime.hpp:45
lime::CurveId
CurveId
Definition lime.hpp:35
lime::CurveId::c448
@ c448
Definition lime.hpp:38
lime::CurveId::unset
@ unset
Definition lime.hpp:36
lime::CurveId::c25519
@ c25519
Definition lime.hpp:37
lime::limeX3DHServerResponseProcess
std::function< void(int responseCode, const std::vector< uint8_t > &responseBody)> limeX3DHServerResponseProcess
Get the response from server. The external service providing secure communication to the X3DH server ...
Definition lime.hpp:106
lime::PeerDeviceStatus
PeerDeviceStatus
Definition lime.hpp:54
lime::PeerDeviceStatus::untrusted
@ untrusted
Definition lime.hpp:55
lime::PeerDeviceStatus::unsafe
@ unsafe
Definition lime.hpp:57
lime::PeerDeviceStatus::trusted
@ trusted
Definition lime.hpp:56
lime::PeerDeviceStatus::unknown
@ unknown
Definition lime.hpp:60
lime::PeerDeviceStatus::fail
@ fail
Definition lime.hpp:58
lime::limeCallback
std::function< void(const lime::CallbackReturn status, const std::string message)> limeCallback
Callback use to give a status on asynchronous operation.
Definition lime.hpp:96
lime::limeX3DHServerPostData
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:116
lime::RecipientData::DRmessage
std::vector< uint8_t > DRmessage
Definition lime.hpp:76
lime::RecipientData::peerStatus
lime::PeerDeviceStatus peerStatus
Definition lime.hpp:70
lime::RecipientData::deviceId
const std::string deviceId
Definition lime.hpp:69
lime::RecipientData::RecipientData
RecipientData(const std::string &deviceId)
Definition lime.hpp:81