lime
Lime is a C++ library implementing Open Whisper System Signal protocol
Public Member Functions | Public Attributes | List of all members
lime::Db Class Reference

Database access class. More...

Public Member Functions

 Db ()=delete
 
 Db (const std::string &filename, std::shared_ptr< std::recursive_mutex > db_mutex)
 Open and check DB validity, create or update db schema is needed. More...
 
 ~Db ()
 
void load_LimeUser (const std::string &deviceId, long int &Uid, lime::CurveId &curveId, std::string &url, const bool allStatus=false)
 Check for existence, retrieve Uid for local user based on its userId (GRUU) and curve from table lime_LocalUsers. More...
 
void delete_LimeUser (const std::string &deviceId)
 if exists, delete user More...
 
void clean_DRSessions ()
 Delete old stale sessions and old stored message key. Apply to all users in localStorage. More...
 
void clean_SPk ()
 Delete old stale SPk. Apply to all users in localStorage. More...
 
void get_allLocalDevices (std::vector< std::string > &deviceIds)
 Get a list of deviceIds of all local users present in localStorage. More...
 
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. More...
 
void set_peerDeviceStatus (const std::string &peerDeviceId, lime::PeerDeviceStatus status)
 
lime::PeerDeviceStatus get_peerDeviceStatus (const std::string &peerDeviceId)
 get the status of a peer device: unknown, untrusted, trusted, unsafe More...
 
bool is_localUser (const std::string &deviceId)
 checks if a device Id exists in the local users table More...
 
void delete_peerDevice (const std::string &peerDeviceId)
 delete a peerDevice from local storage More...
 
template<typename Curve >
long int check_peerDevice (const std::string &peerDeviceId, const DSA< Curve, lime::DSAtype::publicKey > &peerIk, const bool updateInvalid=false)
 Check peer device information(DeviceId - GRUU -, public Ik, Uid to link it to a user) in local storage. More...
 
template<typename Curve >
long int store_peerDevice (const std::string &peerDeviceId, const DSA< Curve, lime::DSAtype::publicKey > &peerIk)
 Store peer device information(DeviceId - GRUU -, public Ik, Uid to link it to a user) in local storage. More...
 

Public Attributes

soci::session sql
 soci connexion to DB More...
 
std::shared_ptr< std::recursive_mutex > m_db_mutex
 mutex on database access More...
 

Detailed Description

Database access class.

relies on SOCI

Constructor & Destructor Documentation

◆ Db() [1/2]

lime::Db::Db ( )
delete

◆ Db() [2/2]

lime::Db::Db ( const std::string &  filename,
std::shared_ptr< std::recursive_mutex >  db_mutex 
)

Open and check DB validity, create or update db schema is needed.

Parameters
[in]filenameThe path to DB file
[in]db_mutexdatabase access mutex

◆ ~Db()

lime::Db::~Db ( )
inline

Member Function Documentation

◆ check_peerDevice()

template<typename Curve >
long int lime::Db::check_peerDevice ( const std::string &  peerDeviceId,
const DSA< Curve, lime::DSAtype::publicKey > &  peerIk,
const bool  updateInvalid = false 
)

Check peer device information(DeviceId - GRUU -, public Ik, Uid to link it to a user) in local storage.

Parameters
[in]peerDeviceIdThe device id to check
[in]peerIkThe public EDDSA identity key of this device
[in]updateInvalidWhen true, will update the Ik with the given one if the stored one is lime:settings::DBInvalidIk and returns its id.
Exceptions
BCTBX_EXCEPTIONif the device is found in local storage but with a different Ik (if Ik is lime::settings::DBInvalidIk, just pretend we never found the device)
Returns
the id internally used by db to store this row. 0 if this device is not in the local storage or have Ik set to lime::settings::DBInvalidIk

◆ clean_DRSessions()

void lime::Db::clean_DRSessions ( )

Delete old stale sessions and old stored message key. Apply to all users in localStorage.

  • DR Session in stale status for more than DRSession_limboTime are deleted
  • MessageKey stored linked to a session who received more than maxMessagesReceivedAfterSkip are deleted
Note
: The messagekeys count is on a chain, so if we have in a chain
Received1 Skip1 Skip2 Received2 Received3 Skip3 Received4
The counter will be reset to 0 when we insert Skip3 (when Received4 arrives) so Skip1 and Skip2 won't be deleted until we got the counter above max on this chain Once we moved to next chain(as soon as peer got an answer from us and replies), the count won't be reset anymore

◆ clean_SPk()

void lime::Db::clean_SPk ( )

Delete old stale SPk. Apply to all users in localStorage.

SPk in stale status for more than SPK_limboTime_days are deleted

◆ delete_LimeUser()

void lime::Db::delete_LimeUser ( const std::string &  deviceId)

if exists, delete user

Parameters
[in]deviceIda string holding the user to look for in DB, shall be its GRUU

◆ delete_peerDevice()

void lime::Db::delete_peerDevice ( const std::string &  peerDeviceId)

delete a peerDevice from local storage

Parameters
[in]peerDeviceIdThe device Id to be removed from local storage, shall be its GRUU

Call is silently ignored if the device is not found in local storage

◆ get_allLocalDevices()

void lime::Db::get_allLocalDevices ( std::vector< std::string > &  deviceIds)

Get a list of deviceIds of all local users present in localStorage.

Parameters
[out]deviceIdsthe list of all local users (their device Id)

◆ get_peerDeviceStatus()

lime::PeerDeviceStatus lime::Db::get_peerDeviceStatus ( const std::string &  peerDeviceId)

get the status of a peer device: unknown, untrusted, trusted, unsafe

Parameters
[in]peerDeviceIdThe device Id of peer, shall be its GRUU
Returns
unknown if the device is not in localStorage, untrusted, trusted or unsafe according to the stored value of peer device status flag otherwise

◆ is_localUser()

bool lime::Db::is_localUser ( const std::string &  deviceId)

checks if a device Id exists in the local users table

Parameters
[in]deviceIdThe device Id
Returns
true if it exists, false otherwise

◆ load_LimeUser()

void lime::Db::load_LimeUser ( const std::string &  deviceId,
long int &  Uid,
lime::CurveId curveId,
std::string &  url,
const bool  allStatus = false 
)

Check for existence, retrieve Uid for local user based on its userId (GRUU) and curve from table lime_LocalUsers.

Parameters
[in]deviceIda string holding the user to look for in DB, shall be its GRUU
[out]Uidthe DB internal Id matching given userId (if find in DB, 0 if not find, -1 if found but not active)
[out]curveIdthe curve selected at user creation
[out]urlthe url of the X3DH server this user is registered on
[in]allStatusallow loading of inactive user if set to true(default is false)

◆ set_peerDeviceStatus() [1/2]

void lime::Db::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.

Parameters
[in]peerDeviceIdThe device Id of peer, shall be its GRUU
[in]Ikthe EdDSA peer public identity key, formatted as in RFC8032
[in]statusvalue of flag to set: accepted values are trusted, untrusted, unsafe
Exceptions
BCTBX_EXCEPTIONif given key doesn't match the one present in local storage

if the status flag value is unexpected (not one of trusted, untrusted, unsafe), ignore the call

if the status flag is unsafe or untrusted, ignore the value of Ik and call the version of this function without it

if peer Device is not present in local storage and status is trusted or unsafe, it is added, if status is untrusted, it is just ignored

General algorithm followed by the set_peerDeviceStatus functions

  • Status is valid? (not one of trusted, untrusted, unsafe)? No: return
  • status is trusted
    • We have Ik? -> No: return
    • Device is already in storage but Ik differs from the given one : exception
    • Insert/update in local storage
  • status is untrusted
    • Ik is ignored
    • Device already in storage? No: return
    • Device already in storage but current status is unsafe? Yes: return
    • update in local storage

-status is unsafe

  • ignore Ik
  • insert/update the status. If inserted, insert an invalid Ik

◆ set_peerDeviceStatus() [2/2]

void lime::Db::set_peerDeviceStatus ( const std::string &  peerDeviceId,
lime::PeerDeviceStatus  status 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Calls with status unsafe or untrusted are executed by this function as they do not need Ik.

◆ store_peerDevice()

template<typename Curve >
long int lime::Db::store_peerDevice ( const std::string &  peerDeviceId,
const DSA< Curve, lime::DSAtype::publicKey > &  peerIk 
)

Store peer device information(DeviceId - GRUU -, public Ik, Uid to link it to a user) in local storage.

Parameters
[in]peerDeviceIdThe device id to insert
[in]peerIkThe public EDDSA identity key of this device
Returns
the id internally used by db to store this row

Member Data Documentation

◆ m_db_mutex

std::shared_ptr<std::recursive_mutex> lime::Db::m_db_mutex

mutex on database access

◆ sql

soci::session lime::Db::sql

soci connexion to DB


The documentation for this class was generated from the following files: