Loading...
Searching...
No Matches
lime::Db Class Reference

Database access class. More...

`#include <lime_localStorage.hpp>`

## 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.
| ~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.
void | delete_LimeUser (const std::string &deviceId)
| if exists, delete user
void | clean_DRSessions ()
| Delete old stale sessions and old stored message key. Apply to all users in localStorage.
void | clean_SPk ()
| Delete old stale SPk. Apply to all users in localStorage.
bool | is_updateRequested (const std::string &deviceId)
| checks if a device needs to be updated return true if the device exists and updateTs is older than OPk_updatePeriod
void | set_updateTs (const std::string &deviceId)
| update the update timestamp to now()
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.
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 device's Id matching a local account are always considered as trusted
lime::PeerDeviceStatus | get_peerDeviceStatus (const std::list< std::string > &peerDeviceIds)
| get the status of a list of peer device: unknown, untrusted, trusted, unsafe and return the lowest found, crescent order being unsafe, unknown, untrusted, trusted device's Id matching a local account are always considered as trusted
bool | is_localUser (const std::string &deviceId)
| checks if a device Id exists in the local users table
void | delete_peerDevice (const std::string &peerDeviceId)
| delete a peerDevice from local storage
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.
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.
void | start_transaction ()
| start a transaction on this Db
void | commit_transaction ()
| commit a transaction on this Db
void | rollback_transaction ()
| rollback a transaction on this Db

## Public Attributes

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

## 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] | filename | The path to DB file
  [in] | db_mutex | database 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] | peerDeviceId | The device id to check
  [in] | peerIk | The public EDDSA identity key of this device
  [in] | updateInvalid | When true, will update the Ik with the given one if the stored one is lime:settings::DBInvalidIk and returns its id.

Exceptions

  BCTBX_EXCEPTION | if 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

## commit_transaction()

void lime::Db::commit_transaction | ( |  | ) |

commit a transaction on this Db

## delete_LimeUser()

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

if exists, delete user

Parameters

  [in] | deviceId | a 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] | peerDeviceId | The 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_peerDeviceStatus() [1/2]

lime::PeerDeviceStatus lime::Db::get_peerDeviceStatus | ( | const std::list< std::string > & | peerDeviceIds | ) |

get the status of a list of peer device: unknown, untrusted, trusted, unsafe and return the lowest found, crescent order being unsafe, unknown, untrusted, trusted device's Id matching a local account are always considered as trusted

Parameters

  [in] | peerDeviceIds | A list of devices Id, shall be their GRUUs

Returns
  the lowest status found in the list

## get_peerDeviceStatus() [2/2]

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

get the status of a peer device: unknown, untrusted, trusted, unsafe device's Id matching a local account are always considered as trusted

Parameters

  [in] | peerDeviceId | The 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] | deviceId | The device Id

Returns
  true if it exists, false otherwise

## is_updateRequested()

bool lime::Db::is_updateRequested | ( | const std::string & | deviceId | ) |

checks if a device needs to be updated return true if the device exists and updateTs is older than OPk_updatePeriod

Parameters

  [in] | deviceId | The device Id

Returns
  true the updateTs is older than OPk_updatePeriod, 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] | deviceId | a string holding the user to look for in DB, shall be its GRUU
  [out] | Uid | the DB internal Id matching given userId (if find in DB, 0 if not find, -1 if found but not active)
  [out] | curveId | the curve selected at user creation
  [out] | url | the url of the X3DH server this user is registered on
  [in] | allStatus | allow loading of inactive user if set to true(default is false)

## rollback_transaction()

void lime::Db::rollback_transaction | ( |  | ) |

rollback a transaction on this Db

## 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] | peerDeviceId | The device Id of peer, shall be its GRUU
  [in] | Ik | the EdDSA peer public identity key, formatted as in RFC8032
  [in] | status | value of flag to set: accepted values are trusted, untrusted, unsafe

Exceptions

  BCTBX_EXCEPTION | if 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.

## set_updateTs()

void lime::Db::set_updateTs | ( | const std::string & | deviceId | ) |

update the update timestamp to now()

Parameters

  [in] | deviceId | The device Id

## start_transaction()

void lime::Db::start_transaction | ( |  | ) |

start a transaction on this Db

## 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] | peerDeviceId | The device id to insert
  [in] | peerIk | The 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:

- /build/lime-OjNHTX/lime-5.3.105+dfsg/src/lime_localStorage.hpp
- /build/lime-OjNHTX/lime-5.3.105+dfsg/src/lime_localStorage.cpp