Commit 2ae9e7bb authored by Johan Pascal's avatar Johan Pascal

Nothing written in local storage before encrypt or decrypt is completed

- postpone the insertion of newly discovered peer devices Id and Ik

+ encryption returns accurate peerDeviceStatus
parent ad8d089e
......@@ -129,14 +129,16 @@ namespace lime {
* @param[in] AD The associated data generated by X3DH protocol and permanently part of the DR session(see X3DH spec section 3.3 and lime doc section 5.4.3)
* @param[in] peerPublicKey the public key of message recipient (also obtained through X3DH, shall be peer SPk)
* @param[in] peerDid Id used in local storage for this peer Device this session shall be attached to
* @param[in] peerDeviceId The peer Device Id this session is connected to. Ignored if peerDid is not 0
* @param[in] peerIk The Identity Key of the peer device this session is connected to. Ignored if peerDid is not 0
* @param[in] selfDid Id used in local storage for local user this session shall be attached to
* @param[in] X3DH_initMessage at session creation as sender we shall also store the X3DHInit message to be able to include it in all message until we got a response from peer
* @param[in] RNG_context A Random Number Generator context used for any rndom generation needed by this session
*/
template <typename Curve>
DR<Curve>::DR(lime::Db *localStorage, const DRChainKey &SK, const SharedADBuffer &AD, const X<Curve, lime::Xtype::publicKey> &peerPublicKey, long int peerDid, long int selfDid, const std::vector<uint8_t> &X3DH_initMessage, std::shared_ptr<RNG> RNG_context)
DR<Curve>::DR(lime::Db *localStorage, const DRChainKey &SK, const SharedADBuffer &AD, const X<Curve, lime::Xtype::publicKey> &peerPublicKey, long int peerDid, const std::string &peerDeviceId, const DSA<Curve, lime::DSAtype::publicKey> &peerIk, long int selfDid, const std::vector<uint8_t> &X3DH_initMessage, std::shared_ptr<RNG> RNG_context)
:m_DHr{peerPublicKey},m_DHr_valid{true}, m_DHs{},m_RK(SK),m_CKs{},m_CKr{},m_Ns(0),m_Nr(0),m_PN(0),m_sharedAD(AD),m_mkskipped{},
m_RNG{RNG_context},m_dbSessionId{0},m_usedNr{0},m_usedDHid{0},m_localStorage{localStorage},m_dirty{DRSessionDbStatus::dirty},m_peerDid{peerDid}, m_db_Uid{selfDid},
m_RNG{RNG_context},m_dbSessionId{0},m_usedNr{0},m_usedDHid{0},m_localStorage{localStorage},m_dirty{DRSessionDbStatus::dirty},m_peerDid{peerDid},m_peerDeviceId{},m_peerIk{},m_db_Uid{selfDid},
m_active_status{true}, m_X3DH_initMessage{X3DH_initMessage}
{
// generate a new self key pair
......@@ -155,6 +157,12 @@ namespace lime {
// derive the root key
KDF_RK<Curve>(m_RK, m_CKs, DH->get_sharedSecret());
// If we have no peerDid, copy peer DeviceId and Ik in the session so we can use them to create the peer device in local storage when first saving the session
if (peerDid == 0) {
m_peerDeviceId = peerDeviceId;
m_peerIk = peerIk;
}
}
/**
......@@ -165,15 +173,23 @@ namespace lime {
* @param[in] AD The associated data generated by X3DH protocol and permanently part of the DR session(see X3DH spec section 3.3 and lime doc section 5.4.3)
* @param[in] selfKeyPair the key pair used by sender to establish this DR session
* @param[in] peerDid Id used in local storage for this peer Device this session shall be attached to
* @param[in] peerDeviceId The peer Device Id this session is connected to. Ignored if peerDid is not 0
* @param[in] peerIk The Identity Key of the peer device this session is connected to. Ignored if peerDid is not 0
* @param[in] selfDid Id used in local storage for local user this session shall be attached to
* @param[in] RNG_context A Random Number Generator context used for any rndom generation needed by this session
*/
template <typename Curve>
DR<Curve>::DR(lime::Db *localStorage, const DRChainKey &SK, const SharedADBuffer &AD, const Xpair<Curve> &selfKeyPair, long int peerDid, long int selfDid, std::shared_ptr<RNG> RNG_context)
DR<Curve>::DR(lime::Db *localStorage, const DRChainKey &SK, const SharedADBuffer &AD, const Xpair<Curve> &selfKeyPair, long int peerDid, const std::string &peerDeviceId, const DSA<Curve, lime::DSAtype::publicKey> &peerIk, long int selfDid, std::shared_ptr<RNG> RNG_context)
:m_DHr{},m_DHr_valid{false},m_DHs{selfKeyPair},m_RK(SK),m_CKs{},m_CKr{},m_Ns(0),m_Nr(0),m_PN(0),m_sharedAD(AD),m_mkskipped{},
m_RNG{RNG_context},m_dbSessionId{0},m_usedNr{0},m_usedDHid{0},m_localStorage{localStorage},m_dirty{DRSessionDbStatus::dirty},m_peerDid{peerDid}, m_db_Uid{selfDid},
m_RNG{RNG_context},m_dbSessionId{0},m_usedNr{0},m_usedDHid{0},m_localStorage{localStorage},m_dirty{DRSessionDbStatus::dirty},m_peerDid{peerDid},m_peerDeviceId{},m_peerIk{},m_db_Uid{selfDid},
m_active_status{true}, m_X3DH_initMessage{}
{ }
{
// If we have no peerDid, copy peer DeviceId and Ik in the session so we can use them to create the peer device in local storage when first saving the session
if (peerDid == 0) {
m_peerDeviceId = peerDeviceId;
m_peerIk = peerIk;
}
}
/**
* @brief Create a new DR session to be loaded from db,
......@@ -187,7 +203,7 @@ namespace lime {
template <typename Curve>
DR<Curve>::DR(lime::Db *localStorage, long sessionId, std::shared_ptr<RNG> RNG_context)
:m_DHr{},m_DHr_valid{true},m_DHs{},m_RK{},m_CKs{},m_CKr{},m_Ns(0),m_Nr(0),m_PN(0),m_sharedAD{},m_mkskipped{},
m_RNG{RNG_context},m_dbSessionId{sessionId},m_usedNr{0},m_usedDHid{0},m_localStorage{localStorage},m_dirty{DRSessionDbStatus::clean},m_peerDid{0}, m_db_Uid{0},
m_RNG{RNG_context},m_dbSessionId{sessionId},m_usedNr{0},m_usedDHid{0},m_localStorage{localStorage},m_dirty{DRSessionDbStatus::clean},m_peerDid{0},m_peerDeviceId{},m_peerIk{},m_db_Uid{0},
m_active_status{false}, m_X3DH_initMessage{}
{
session_load();
......
......@@ -91,6 +91,8 @@ namespace lime {
lime::Db *m_localStorage; // enable access to the database holding sessions and skipped message keys, no need to use smart pointers here, Db is not owned by DRsession, it must persist even if no session exists
DRSessionDbStatus m_dirty; // status of the object regarding its instance in local storage, could be: clean, dirty_encrypt, dirty_decrypt or dirty
long int m_peerDid; // used during session creation only to hold the peer device id in DB as we need it to insert the session in local Storage
std::string m_peerDeviceId; // used during session creation only, if the deviceId is not yet in local storage, to hold the peer device Id so we can insert it in DB when session is saved for the first time
DSA<Curve, lime::DSAtype::publicKey> m_peerIk; // used during session creation only, if the deviceId is not yet in local storage, to hold the peer device Ik so we can insert it in DB when session is saved for the first time
long int m_db_Uid; // used to link session to a local device Id
bool m_active_status; // current status of this session, true if it is the active one, false if it is stale
std::vector<uint8_t> m_X3DH_initMessage; // store the X3DH init message to be able to prepend it to any message until we got a first response from peer so we're sure he was able to init the session on his side
......@@ -105,8 +107,8 @@ namespace lime {
public:
DR() = delete; // make sure the Double Ratchet is not initialised without parameters
DR(lime::Db *localStorage, const DRChainKey &SK, const SharedADBuffer &AD, const X<Curve, lime::Xtype::publicKey> &peerPublicKey, long int peerDeviceId, long int selfDeviceId, const std::vector<uint8_t> &X3DH_initMessage, std::shared_ptr<RNG> RNG_context); // call to initialise a session for sender: we have Shared Key and peer Public key
DR(lime::Db *localStorage, const DRChainKey &SK, const SharedADBuffer &AD, const Xpair<Curve> &selfKeyPair, long int peerDeviceId, long int selfDeviceId, std::shared_ptr<RNG> RNG_context); // call at initialisation of a session for receiver: we have Share Key and self key pair
DR(lime::Db *localStorage, const DRChainKey &SK, const SharedADBuffer &AD, const X<Curve, lime::Xtype::publicKey> &peerPublicKey, const long int peerDid, const std::string &peerDeviceId, const DSA<Curve, lime::DSAtype::publicKey> &peerIk, long int selfDeviceId, const std::vector<uint8_t> &X3DH_initMessage, std::shared_ptr<RNG> RNG_context); // call to initialise a session for sender: we have Shared Key and peer Public key
DR(lime::Db *localStorage, const DRChainKey &SK, const SharedADBuffer &AD, const Xpair<Curve> &selfKeyPair, long int peerDid, const std::string &peerDeviceId, const DSA<Curve, lime::DSAtype::publicKey> &peerIk, long int selfDeviceId, std::shared_ptr<RNG> RNG_context); // call at initialisation of a session for receiver: we have Share Key and self key pair
DR(lime::Db *localStorage, long sessionId, std::shared_ptr<RNG> RNG_context); // load session from DB
DR(DR<Curve> &a) = delete; // can't copy a session, force usage of shared pointers
DR<Curve> &operator=(DR<Curve> &a) = delete; // can't copy a session
......
......@@ -74,7 +74,6 @@ namespace lime {
void get_SelfIdentityKey(); // check our Identity key pair is loaded in Lime object, retrieve it from DB if it isn't
void cache_DR_sessions(std::vector<RecipientInfos<Curve>> &internal_recipients, std::vector<std::string> &missing_devices); // loop on internal recipient an try to load in DR session cache the one which have no session attached
void get_DRSessions(const std::string &senderDeviceId, const long int ignoreThisDRSessionId, std::vector<std::shared_ptr<DR<Curve>>> &DRSessions); // load from local storage in DRSessions all DR session matching the peerDeviceId, ignore the one picked by id in 2nd arg
long int store_peerDevice(const std::string &peerDeviceId, const DSA<Curve, lime::DSAtype::publicKey> &Ik); // store given peer Device Id and public identity key, return the Id used in table to store it
/* X3DH related - part related to exchange with server or localStorage - implemented in lime_x3dh_protocol.cpp or lime_localStorage.cpp */
void X3DH_generate_SPk(X<Curve, lime::Xtype::publicKey> &publicSPk, DSA<Curve, lime::DSAtype::signature> &SPk_sig, uint32_t &SPk_id); // generate a new Signed Pre-Key key pair, store it in DB and set its public key, signature and Id in given params
......
......@@ -335,6 +335,77 @@ lime::PeerDeviceStatus Db::get_peerDeviceStatus(const std::string &peerDeviceId)
return lime::PeerDeviceStatus::unknown;
}
/**
* @brief Check peer device information(DeviceId - GRUU -, public Ik, Uid to link it to a user) in local storage
*
* @param[in] peerDeviceId The device id to check
* @param[in] peerIk The public EDDSA identity key of this device
*
* throws an exception if the device is found in local storage but with a different Ik
*
* @return the id internally used by db to store this row, 0 if this device is not in the local storage
*/
template <typename Curve>
long int Db::check_peerDevice(const std::string &peerDeviceId, const DSA<Curve, lime::DSAtype::publicKey> &peerIk) {
try {
blob Ik_blob(sql);
long int Did=0;
// make sure this device wasn't already here, if it was, check they have the same Ik
sql<<"SELECT Ik,Did FROM lime_PeerDevices WHERE DeviceId = :DeviceId LIMIT 1;", into(Ik_blob), into(Did), use(peerDeviceId);
if (sql.got_data()) { // Found one
DSA<Curve, lime::DSAtype::publicKey> stored_Ik;
if (Ik_blob.get_len() != stored_Ik.size()) { // can't match they are not the same size
LIME_LOGE<<"It appears that peer device "<<peerDeviceId<<" was known with an identity key but is trying to use another one now";
throw BCTBX_EXCEPTION << "Peer device "<<peerDeviceId<<" changed its Ik";
}
Ik_blob.read(0, (char *)(stored_Ik.data()), stored_Ik.size()); // Read it to compare it to the given one
if (stored_Ik == peerIk) { // they match, so we just return the Did
return Did;
} else { // Ik are not matching, peer device changed its Ik!?! Reject
LIME_LOGE<<"It appears that peer device "<<peerDeviceId<<" was known with an identity key but is trying to use another one now";
throw BCTBX_EXCEPTION << "Peer device "<<peerDeviceId<<" changed its Ik";
}
} else { // not found in local Storage: return 0
return 0;
}
} catch (exception const &e) {
throw BCTBX_EXCEPTION << "Peer device "<<peerDeviceId<<" check failed: "<<e.what();
}
}
/**
* @brief Store peer device information(DeviceId - GRUU -, public Ik, Uid to link it to a user) in local storage
*
* @param[in] peerDeviceId The device id to insert
* @param[in] peerIk The public EDDSA identity key of this device
*
* @return the id internally used by db to store this row
*/
template <typename Curve>
long int Db::store_peerDevice(const std::string &peerDeviceId, const DSA<Curve, lime::DSAtype::publicKey> &peerIk) {
try {
blob Ik_blob(sql);
long int Did=0;
// make sure this device wasn't already here, if it was, check they have the same Ik
Did = check_peerDevice(peerDeviceId, peerIk); // perform checks on peer device and returns its Id if found in local storage already
if (Did != 0) {
return Did;
} else { // not found in local Storage
Ik_blob.write(0, (char *)(peerIk.data()), peerIk.size());
sql<<"INSERT INTO lime_PeerDevices(DeviceId,Ik) VALUES (:deviceId,:Ik) ", use(peerDeviceId), use(Ik_blob);
sql<<"select last_insert_rowid()",into(Did);
LIME_LOGD<<"store peerDevice "<<peerDeviceId<<" with device id "<<Did;
return Did;
}
} catch (exception const &e) {
throw BCTBX_EXCEPTION << "Peer device "<<peerDeviceId<<" insertion failed: "<<e.what();
}
}
/**
* @brief if exists, delete user
*
......@@ -346,6 +417,17 @@ void Db::delete_LimeUser(const std::string &deviceId)
sql<<"DELETE FROM lime_LocalUsers WHERE UserId = :userId;", use(deviceId);
}
/* template instanciations for Curves 25519 and 448 */
#ifdef EC25519_ENABLED
template long int Db::check_peerDevice<C255>(const std::string &peerDeviceId, const DSA<C255, lime::DSAtype::publicKey> &Ik);
template long int Db::store_peerDevice<C255>(const std::string &peerDeviceId, const DSA<C255, lime::DSAtype::publicKey> &Ik);
#endif
#ifdef EC448_ENABLED
template long int Db::check_peerDevice<C448>(const std::string &peerDeviceId, const DSA<C448, lime::DSAtype::publicKey> &Ik);
template long int Db::store_peerDevice<C448>(const std::string &peerDeviceId, const DSA<C448, lime::DSAtype::publicKey> &Ik);
#endif
/******************************************************************************/
/* */
/* Double ratchet member functions */
......@@ -376,8 +458,13 @@ bool DR<DHKey>::session_save() {
blob AD(m_localStorage->sql);
AD.write(0, (char *)(m_sharedAD.data()), m_sharedAD.size());
// make sure we have no other session active with this pair local,peer DiD
m_localStorage->sql<<"UPDATE DR_sessions SET Status = 0, timeStamp = CURRENT_TIMESTAMP WHERE Did = :Did AND Uid = :Uid", use(m_peerDid), use(m_db_Uid);
// Check if we have a peer device already in storage
if (m_peerDid == 0) { // no : we must insert it(failure will result in exception being thrown, let it flow up then)
m_peerDid = m_localStorage->store_peerDevice(m_peerDeviceId, m_peerIk);
} else {
// make sure we have no other session active with this pair local,peer DiD
m_localStorage->sql<<"UPDATE DR_sessions SET Status = 0, timeStamp = CURRENT_TIMESTAMP WHERE Did = :Did AND Uid = :Uid", use(m_peerDid), use(m_db_Uid);
}
if (m_X3DH_initMessage.size()>0) {
blob X3DH_initMessage(m_localStorage->sql);
......@@ -799,49 +886,6 @@ void Lime<Curve>::cache_DR_sessions(std::vector<RecipientInfos<Curve>> &internal
}
}
/**
* @brief Store peer device information(DeviceId - GRUU -, public Ik, Uid to link it to a user) in local storage
*
* @param[in] peerDeviceId The device id to insert
* @param[in] Ik The public EDDSA identity key of this device
*
* @return the id internally used by db to store this row
*/
template <typename Curve>
long int Lime<Curve>::store_peerDevice(const std::string &peerDeviceId, const DSA<Curve, lime::DSAtype::publicKey> &Ik) {
blob Ik_blob(m_localStorage->sql);
try {
long int Did=0;
// make sure this device wasn't already here, if it was, check they have the same Ik
m_localStorage->sql<<"SELECT Ik,Did FROM lime_PeerDevices WHERE DeviceId = :DeviceId LIMIT 1;", into(Ik_blob), into(Did), use(peerDeviceId);
if (m_localStorage->sql.got_data()) { // Found one
DSA<Curve, lime::DSAtype::publicKey> stored_Ik;
if (Ik_blob.get_len() != stored_Ik.size()) { // can't match they are not the same size
LIME_LOGE<<"It appears that peer device "<<peerDeviceId<<" was known with an identity key but is trying to use another one now";
throw BCTBX_EXCEPTION << "Peer device "<<peerDeviceId<<" changed its Ik";
}
Ik_blob.read(0, (char *)(stored_Ik.data()), stored_Ik.size()); // Read it to compare it to the given one
if (stored_Ik == Ik) { // they match, so we just return the Did
return Did;
} else { // Ik are not matching, peer device changed its Ik!?! Reject
LIME_LOGE<<"It appears that peer device "<<peerDeviceId<<" was known with an identity key but is trying to use another one now";
throw BCTBX_EXCEPTION << "Peer device "<<peerDeviceId<<" changed its Ik";
}
} else { // not found in local Storage
transaction tr(m_localStorage->sql);
Ik_blob.write(0, (char *)(Ik.data()), Ik.size());
m_localStorage->sql<<"INSERT INTO lime_PeerDevices(DeviceId,Ik) VALUES (:deviceId,:Ik) ", use(peerDeviceId), use(Ik_blob);
m_localStorage->sql<<"select last_insert_rowid()",into(Did);
tr.commit();
LIME_LOGD<<"store peerDevice "<<peerDeviceId<<" with device id "<<Did;
return Did;
}
} catch (exception const &e) {
throw BCTBX_EXCEPTION << "Peer device "<<peerDeviceId<<" insertion failed. DB backend says : "<<e.what();
}
}
// load from local storage in DRSessions all DR session matching the peerDeviceId, ignore the one picked by id in 2nd arg
template <typename Curve>
void Lime<Curve>::get_DRSessions(const std::string &senderDeviceId, const long int ignoreThisDRSessionId, std::vector<std::shared_ptr<DR<Curve>>> &DRSessions) {
......@@ -940,7 +984,6 @@ void Lime<Curve>::X3DH_updateOPkStatus(const std::vector<uint32_t> &OPkIds) {
template void Lime<C255>::X3DH_generate_SPk(X<C255, lime::Xtype::publicKey> &publicSPk, DSA<C255, DSAtype::signature> &SPk_sig, uint32_t &SPk_id);
template void Lime<C255>::X3DH_generate_OPks(std::vector<X<C255, lime::Xtype::publicKey>> &publicOPks, std::vector<uint32_t> &OPk_ids, const uint16_t OPk_number);
template void Lime<C255>::cache_DR_sessions(std::vector<RecipientInfos<C255>> &internal_recipients, std::vector<std::string> &missing_devices);
template long int Lime<C255>::store_peerDevice(const std::string &peerDeviceId, const DSA<C255, lime::DSAtype::publicKey> &Ik);
template void Lime<C255>::get_DRSessions(const std::string &senderDeviceId, const long int ignoreThisDBSessionId, std::vector<std::shared_ptr<DR<C255>>> &DRSessions);
template void Lime<C255>::X3DH_get_SPk(uint32_t SPk_id, Xpair<C255> &SPk);
template bool Lime<C255>::is_currentSPk_valid(void);
......@@ -954,7 +997,6 @@ void Lime<Curve>::X3DH_updateOPkStatus(const std::vector<uint32_t> &OPkIds) {
template void Lime<C448>::X3DH_generate_SPk(X<C448, lime::Xtype::publicKey> &publicSPk, DSA<C448, DSAtype::signature> &SPk_sig, uint32_t &SPk_id);
template void Lime<C448>::X3DH_generate_OPks(std::vector<X<C448, lime::Xtype::publicKey>> &publicOPks, std::vector<uint32_t> &OPk_ids, const uint16_t OPk_number);
template void Lime<C448>::cache_DR_sessions(std::vector<RecipientInfos<C448>> &internal_recipients, std::vector<std::string> &missing_devices);
template long int Lime<C448>::store_peerDevice(const std::string &peerDeviceId, const DSA<C448, lime::DSAtype::publicKey> &Ik);
template void Lime<C448>::get_DRSessions(const std::string &senderDeviceId, const long int ignoreThisDBSessionId, std::vector<std::shared_ptr<DR<C448>>> &DRSessions);
template void Lime<C448>::X3DH_get_SPk(uint32_t SPk_id, Xpair<C448> &SPk);
template bool Lime<C448>::is_currentSPk_valid(void);
......
......@@ -21,6 +21,7 @@
#define lime_localStorage_hpp
#include "soci/soci.h"
#include "lime_crypto_primitives.hpp"
namespace lime {
......@@ -107,12 +108,40 @@ namespace lime {
*/
lime::PeerDeviceStatus get_peerDeviceStatus(const std::string &peerDeviceId);
/**
* @brief check if the given peer Device information are in the local storage
* raise an exception if a different Ik is found for the given peer Device Id
*
* @param[in] peerDeviceId The peer's device Id, shall be its GRUU
* @param[in] peerIk The peer's public identity key
*
* @return the Id used internally in local Storage to index the peer device, 0 if not found
*/
template <typename Curve>
long int check_peerDevice(const std::string &peerDeviceId, const DSA<Curve, lime::DSAtype::publicKey> &peerIk);
/**
* @brief insert the given peer Device information in the local storage
* given data are checked (using check_peerDevice) before insertion
*
* @param[in] peerDeviceId The peer's device Id, shall be its GRUU
* @param[in] peerIk The peer's public identity key
*
* @return the Id used internally in local Storage to index the peer device
*/
template <typename Curve>
long int store_peerDevice(const std::string &peerDeviceId, const DSA<Curve, lime::DSAtype::publicKey> &peerIk);
};
/* 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 */
#ifdef EC25519_ENABLED
extern template long int Db::check_peerDevice<C255>(const std::string &peerDeviceId, const DSA<C255, lime::DSAtype::publicKey> &Ik);
extern template long int Db::store_peerDevice<C255>(const std::string &peerDeviceId, const DSA<C255, lime::DSAtype::publicKey> &Ik);
#endif
#ifdef EC448_ENABLED
extern template long int Db::check_peerDevice<C448>(const std::string &peerDeviceId, const DSA<C448, lime::DSAtype::publicKey> &Ik);
extern template long int Db::store_peerDevice<C448>(const std::string &peerDeviceId, const DSA<C448, lime::DSAtype::publicKey> &Ik);
#endif
}
......
......@@ -44,9 +44,9 @@ namespace lime {
throw BCTBX_EXCEPTION << "Verify signature on SPk failed for deviceId "<<peerBundle.deviceId;
}
// insert the new peer device Id in Storage, keep the Id used in table to give it to DR_Session which will need it to save itself into DB.
// before going on, check if peer informations are ok, if the returned Id is 0, it means this peer was not in storage yet
// throw an exception in case of failure, just let it flow up
long int peerDid = store_peerDevice(peerBundle.deviceId, peerBundle.Ik);
long int peerDid = m_localStorage->check_peerDevice(peerBundle.deviceId, peerBundle.Ik);
// Initiate HKDF input : We will compute HKDF with a concat of F and all DH computed, see X3DH spec section 2.2 for what is F
// use sBuffer of size able to hold also DH$ even if we may not use it
......@@ -117,7 +117,8 @@ namespace lime {
if (peerBundle.haveOPk) {
m_DR_sessions_cache.erase(peerBundle.deviceId); // will just do nothing if this peerDeviceId is not in cache
}
m_DR_sessions_cache.emplace(peerBundle.deviceId, make_shared<DR<Curve>>(m_localStorage.get(), SK, AD, peerBundle.SPk, peerDid, m_db_Uid, X3DH_initMessage, m_RNG)); // will just do nothing if this peerDeviceId is already in cache
m_DR_sessions_cache.emplace(peerBundle.deviceId, make_shared<DR<Curve>>(m_localStorage.get(), SK, AD, peerBundle.SPk, peerDid, peerBundle.deviceId, peerBundle.Ik, m_db_Uid, X3DH_initMessage, m_RNG)); // will just do nothing if this peerDeviceId is already in cache
LIME_LOGI<<"X3DH created session with device "<<peerBundle.deviceId;
}
......@@ -204,11 +205,10 @@ namespace lime {
AD_input.insert(AD_input.end(), m_selfDeviceId.cbegin(), m_selfDeviceId.cend());
HMAC_KDF<SHA512>(salt, AD_input, lime::settings::X3DH_AD_info, AD.data(), AD.size()); // use the same salt as for SK computation but a different info string
// insert the new peer device Id in Storage, keep the Id used in table to give it to DR_Session which will need it to save itself into DB.
long int peerDid=0;
peerDid = store_peerDevice(senderDeviceId, peerIk);
// check the new peer device Id in Storage, if it is not found, the DR session will add it when it saves itself after successful decryption
auto peerDid = m_localStorage->check_peerDevice(senderDeviceId, peerIk);
auto DRSession = make_shared<DR<Curve>>(m_localStorage.get(), SK, AD, SPk, peerDid, m_db_Uid, m_RNG);
auto DRSession = make_shared<DR<Curve>>(m_localStorage.get(), SK, AD, SPk, peerDid, senderDeviceId, peerIk, m_db_Uid, m_RNG);
return DRSession;
}
......
......@@ -207,8 +207,9 @@ void dr_sessionsInit(std::shared_ptr<DR<Curve>> &alice, std::shared_ptr<DR<Curve
// create DR sessions
std::vector<uint8_t> X3DH_initMessage{};
alice = std::make_shared<DR<Curve>>(localStorageAlice.get(), SK, AD, bobKeyPair.publicKey(), aliceDid, aliceUid, X3DH_initMessage, RNG_context);
bob = std::make_shared<DR<Curve>>(localStorageBob.get(), SK, AD, bobKeyPair, bobDid, bobUid, RNG_context);
DSA<Curve, lime::DSAtype::publicKey> dummyPeerIk{}; // DR session creation gets the peerDeviceId and peerIk but uses it only if peerDid is 0, give dummy, we're focusing on DR here
alice = std::make_shared<DR<Curve>>(localStorageAlice.get(), SK, AD, bobKeyPair.publicKey(), aliceDid, "dummyPeerDevice", dummyPeerIk, aliceUid, X3DH_initMessage, RNG_context);
bob = std::make_shared<DR<Curve>>(localStorageBob.get(), SK, AD, bobKeyPair, bobDid, "dummyPeerDevice", dummyPeerIk, bobUid, RNG_context);
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment