lime
Lime is a C++ library implementing Open Whisper System Signal protocol
lime_localStorage.hpp
Go to the documentation of this file.
1 /*
2  lime_localStorage.hpp
3  @author Johan Pascal
4  @copyright Copyright (C) 2017 Belledonne Communications SARL
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef lime_localStorage_hpp
21 #define lime_localStorage_hpp
22 
23 #include "soci/soci.h"
25 #include <mutex>
26 
27 namespace lime {
28 
34  class Db {
35  public:
37  soci::session sql;
39  std::shared_ptr<std::recursive_mutex> m_db_mutex;
40 
41  Db()=delete; // we can't create a new DB holder without DB filename
42 
49  Db(const std::string &filename, std::shared_ptr<std::recursive_mutex> db_mutex);
50  ~Db(){sql.close();};
51 
52  void load_LimeUser(const std::string &deviceId, long int &Uid, lime::CurveId &curveId, std::string &url, const bool allStatus=false);
53  void delete_LimeUser(const std::string &deviceId);
54  void clean_DRSessions();
55  void clean_SPk();
56  bool is_updateRequested(const std::string &deviceId);
57  void set_updateTs(const std::string &deviceId);
58  void set_peerDeviceStatus(const std::string &peerDeviceId, const std::vector<uint8_t> &Ik, lime::PeerDeviceStatus status);
59  void set_peerDeviceStatus(const std::string &peerDeviceId, lime::PeerDeviceStatus status);
60  lime::PeerDeviceStatus get_peerDeviceStatus(const std::string &peerDeviceId);
61  lime::PeerDeviceStatus get_peerDeviceStatus(const std::list<std::string> &peerDeviceIds);
62  bool is_localUser(const std::string &deviceId);
63  void delete_peerDevice(const std::string &peerDeviceId);
64  template <typename Curve>
65  long int check_peerDevice(const std::string &peerDeviceId, const DSA<Curve, lime::DSAtype::publicKey> &peerIk, const bool updateInvalid=false);
66  template <typename Curve>
67  long int store_peerDevice(const std::string &peerDeviceId, const DSA<Curve, lime::DSAtype::publicKey> &peerIk);
68  void start_transaction();
69  void commit_transaction();
70  void rollback_transaction();
71  };
72 
73  /* this templates are instanciated once in the lime_localStorage.cpp file, explicitly tell anyone including this header that there is no need to re-instanciate them */
74 #ifdef EC25519_ENABLED
75  extern template long int Db::check_peerDevice<C255>(const std::string &peerDeviceId, const DSA<C255, lime::DSAtype::publicKey> &Ik, const bool updateInvalid);
76  extern template long int Db::store_peerDevice<C255>(const std::string &peerDeviceId, const DSA<C255, lime::DSAtype::publicKey> &Ik);
77 #endif
78 
79 #ifdef EC448_ENABLED
80  extern template long int Db::check_peerDevice<C448>(const std::string &peerDeviceId, const DSA<C448, lime::DSAtype::publicKey> &Ik, const bool updateInvalid);
81  extern template long int Db::store_peerDevice<C448>(const std::string &peerDeviceId, const DSA<C448, lime::DSAtype::publicKey> &Ik);
82 #endif
83 
84 }
85 
86 #endif /* lime_localStorage_hpp */
bool is_localUser(const std::string &deviceId)
checks if a device Id exists in the local users table
Definition: lime_localStorage.cpp:553
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_localStorage.cpp:339
lime::PeerDeviceStatus get_peerDeviceStatus(const std::string &peerDeviceId)
get the status of a peer device: unknown, untrusted, trusted, unsafe device&#39;s Id matching a local acc...
Definition: lime_localStorage.cpp:445
void commit_transaction()
commit a transaction on this Db
Definition: lime_localStorage.cpp:717
Db()=delete
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 OP...
Definition: lime_localStorage.cpp:568
void delete_peerDevice(const std::string &peerDeviceId)
delete a peerDevice from local storage
Definition: lime_localStorage.cpp:594
void clean_DRSessions()
Delete old stale sessions and old stored message key. Apply to all users in localStorage.
Definition: lime_localStorage.cpp:284
void start_transaction()
start a transaction on this Db
Definition: lime_localStorage.cpp:708
void delete_LimeUser(const std::string &deviceId)
if exists, delete user
Definition: lime_localStorage.cpp:698
PeerDeviceStatus
Definition: lime.hpp:54
~Db()
Definition: lime_localStorage.hpp:50
std::shared_ptr< std::recursive_mutex > m_db_mutex
mutex on database access
Definition: lime_localStorage.hpp:39
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...
Definition: lime_localStorage.cpp:238
Definition: lime.cpp:30
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 storag...
Definition: lime_localStorage.cpp:611
void clean_SPk()
Delete old stale SPk. Apply to all users in localStorage.
Definition: lime_localStorage.cpp:299
void set_updateTs(const std::string &deviceId)
update the update timestamp to now()
Definition: lime_localStorage.cpp:581
void rollback_transaction()
rollback a transaction on this Db
Definition: lime_localStorage.cpp:726
Database access class.
Definition: lime_localStorage.hpp:34
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 storag...
Definition: lime_localStorage.cpp:669
soci::session sql
soci connexion to DB
Definition: lime_localStorage.hpp:37
CurveId
Definition: lime.hpp:35