Commit 454f681b authored by johan's avatar johan

Use c++ bctoolbox logging API

+ more variable name consistency
parent c1aa5242
......@@ -51,13 +51,13 @@ namespace lime {
* just load it into Lime class
*
* @param[in/out] localStorage pointer to DB accessor
* @param[in] userId user Id(shall be GRUU), stored in the structure
* @param[in] deviceId device Id(shall be GRUU), stored in the structure
* @param[in] Uid the DB internal Id for this user, speed up DB operations by holding it in DB
* @param[in] url URL of the X3DH key server used to publish our keys(retrieved from DB)
*/
template <typename Curve>
Lime<Curve>::Lime(std::unique_ptr<lime::Db> &&localStorage, const std::string &userId, const std::string &url, belle_http_provider_t *http_provider, const long int Uid)
: m_RNG{bctbx_rng_context_new()}, m_selfDeviceId{userId},
Lime<Curve>::Lime(std::unique_ptr<lime::Db> &&localStorage, const std::string &deviceId, const std::string &url, belle_http_provider_t *http_provider, const long int Uid)
: m_RNG{bctbx_rng_context_new()}, m_selfDeviceId{deviceId},
m_Ik{}, m_Ik_loaded(false),
m_localStorage(std::move(localStorage)), m_db_Uid{Uid},
m_http_provider{http_provider}, m_X3DH_Server_URL{url},
......@@ -70,12 +70,12 @@ namespace lime {
* Create a user in DB, if already existing, throw exception
*
* @param[in/out] localStorage pointer to DB accessor
* @param[in] userId user Id(shall be GRUU), stored in the structure
* @param[in] deviceId device Id(shall be GRUU), stored in the structure
* @param[in] url URL of the X3DH key server used to publish our keys
*/
template <typename Curve>
Lime<Curve>::Lime(std::unique_ptr<lime::Db> &&localStorage, const std::string &userId, const std::string &url, belle_http_provider_t *http_provider)
: m_RNG{bctbx_rng_context_new()}, m_selfDeviceId{userId},
Lime<Curve>::Lime(std::unique_ptr<lime::Db> &&localStorage, const std::string &deviceId, const std::string &url, belle_http_provider_t *http_provider)
: m_RNG{bctbx_rng_context_new()}, m_selfDeviceId{deviceId},
m_Ik{}, m_Ik_loaded(false),
m_localStorage(std::move(localStorage)), m_db_Uid{0},
m_http_provider{http_provider}, m_X3DH_Server_URL{url},
......@@ -130,7 +130,7 @@ namespace lime {
void Lime<Curve>::encrypt(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) {
bctbx_debug("encrypt from %s to %ld recipients", m_selfDeviceId.data(), recipients->size());
/* Check if we have all the Double Ratchet sessions ready or shall we go for an X3DH */
std::vector<std::string> missingPeers; /* vector of userId(GRUU) which are requested to perform X3DH before the encryption can occurs */
std::vector<std::string> missingPeers; /* vector of deviceId(GRUU) which are requested to perform X3DH before the encryption can occurs */
/* Create the appropriate recipient infos and fill it with sessions found in cache */
std::vector<recipientInfos<Curve>> internal_recipients{};
......@@ -247,19 +247,19 @@ namespace lime {
/****************************************************************************/
/**
* @brief : Insert user in database and return a pointer to the control class instanciating the appropriate Lime children class
m* Once created a user cannot be modified, insertion of existing userId will raise an exception.
m* Once created a user cannot be modified, insertion of existing deviceId will raise an exception.
*
* @param[in] dbFilename Path to filename to use
* @param[in] userId User to create in DB, userId shall be the GRUU
* @param[in] deviceId User to create in DB, deviceId shall be the GRUU
* @param[in] url URL of X3DH key server to be used to publish our keys
* @param[in] curve Which curve shall we use for this account, select the implemenation to instanciate when using this user
* @param[in] http_provider An http provider used to communicate with x3dh key server
*
* @return a pointer to the LimeGeneric class allowing access to API declared in lime.hpp
*/
std::shared_ptr<LimeGeneric> insert_LimeUser(const std::string &dbFilename, const std::string &userId, const std::string &url, const lime::CurveId curve, belle_http_provider *http_provider,
std::shared_ptr<LimeGeneric> insert_LimeUser(const std::string &dbFilename, const std::string &deviceId, const std::string &url, const lime::CurveId curve, belle_http_provider *http_provider,
const limeCallback &callback) {
bctbx_message("Create Lime user %s", userId.data());
BCTBX_SLOGI<<"Create Lime user "<<deviceId;
/* first check the requested curve is instanciable and return an exception if not */
#ifndef EC25519_ENABLED
if (curve == lime::CurveId::c25519) {
......@@ -282,7 +282,7 @@ namespace lime {
#ifdef EC25519_ENABLED
{
/* constructor will insert user in Db, if already present, raise an exception*/
auto lime_ptr = std::make_shared<Lime<C255>>(std::move(localStorage), userId, url, http_provider);
auto lime_ptr = std::make_shared<Lime<C255>>(std::move(localStorage), deviceId, url, http_provider);
lime_ptr->publish_user(callback);
return lime_ptr;
}
......@@ -292,7 +292,7 @@ namespace lime {
case lime::CurveId::c448 :
#ifdef EC448_ENABLED
{
auto lime_ptr = std::make_shared<Lime<C448>>(std::move(localStorage), userId, url, http_provider);
auto lime_ptr = std::make_shared<Lime<C448>>(std::move(localStorage), deviceId, url, http_provider);
lime_ptr->publish_user(callback);
return lime_ptr;
}
......@@ -301,7 +301,7 @@ namespace lime {
case lime::CurveId::unset :
default: // asking for an unsupported type
throw BCTBX_EXCEPTION << "Cannot create lime user "<<userId;//<<". Unsupported curve (id <<"static_cast<uint8_t>(curve)") requested";
throw BCTBX_EXCEPTION << "Cannot create lime user "<<deviceId;//<<". Unsupported curve (id <<"static_cast<uint8_t>(curve)") requested";
break;
}
} catch (BctbxException &e) {
......@@ -315,12 +315,12 @@ namespace lime {
* Fail to find the user will raise an exception
*
* @param[in] dbFilename Path to filename to use
* @param[in] userId User to create in DB, userId shall be the GRUU
* @param[in] deviceId User to lookup in DB, deviceId shall be the GRUU
* @param[in] http_provider An http provider used to communicate with x3dh key server
*
* @return a pointer to the LimeGeneric class allowing access to API declared in lime.hpp
*/
std::shared_ptr<LimeGeneric> load_LimeUser(const std::string &dbFilename, const std::string &userId, belle_http_provider *http_provider) {
std::shared_ptr<LimeGeneric> load_LimeUser(const std::string &dbFilename, const std::string &deviceId, belle_http_provider *http_provider) {
/* open DB and load user */
auto localStorage = std::unique_ptr<lime::Db>(new lime::Db(dbFilename)); // create as unique ptr, ownership is then passed to the Lime structure when instanciated
......@@ -328,18 +328,18 @@ namespace lime {
long int Uid=0;
std::string x3dh_server_url;
localStorage->load_LimeUser(userId, Uid, curve, x3dh_server_url); // this one will throw an exception if user is not found, just let it rise
bctbx_message("Load Lime user %s", userId.data());
localStorage->load_LimeUser(deviceId, Uid, curve, x3dh_server_url); // this one will throw an exception if user is not found, just let it rise
BCTBX_SLOGI<<"Load Lime user "<<deviceId;
/* check the curve id retrieved from DB is instanciable and return an exception if not */
#ifndef EC25519_ENABLED
if (curve == lime::CurveId::c25519) {
throw BCTBX_EXCEPTION << "Lime load User "<<userId<<" requests usage of Curve 25519 but it's not supported - change lib lime compile option to enable it";
throw BCTBX_EXCEPTION << "Lime load User "<<deviceId<<" requests usage of Curve 25519 but it's not supported - change lib lime compile option to enable it";
}
#endif
#ifndef EC448_ENABLED
if (curve == lime::CurveId::c448) {
throw BCTBX_EXCEPTION << "Lime load User "<<userId<<" requests usage of Curve 448 but it's not supported - change lib lime compile option to enable it";
throw BCTBX_EXCEPTION << "Lime load User "<<deviceId<<" requests usage of Curve 448 but it's not supported - change lib lime compile option to enable it";
}
#endif
......@@ -348,20 +348,20 @@ namespace lime {
switch (curve) {
case lime::CurveId::c25519 :
#ifdef EC25519_ENABLED
return std::make_shared<Lime<C255>>(std::move(localStorage), userId, x3dh_server_url, http_provider, Uid);
return std::make_shared<Lime<C255>>(std::move(localStorage), deviceId, x3dh_server_url, http_provider, Uid);
#endif
break;
case lime::CurveId::c448 :
#ifdef EC448_ENABLED
return std::make_shared<Lime<C448>>(std::move(localStorage), userId, x3dh_server_url, http_provider, Uid);
return std::make_shared<Lime<C448>>(std::move(localStorage), deviceId, x3dh_server_url, http_provider, Uid);
#endif
break;
case lime::CurveId::unset :
default: // asking for an unsupported type
throw BCTBX_EXCEPTION << "Cannot create load user "<<userId;//<<". Unsupported curve (id <<"static_cast<uint8_t>(curve)") requested";
throw BCTBX_EXCEPTION << "Cannot create load user "<<deviceId;//<<". Unsupported curve (id <<"static_cast<uint8_t>(curve)") requested";
break;
}
} catch (BctbxException &e) {
......
......@@ -480,7 +480,7 @@ namespace lime {
try {
decryptStatus = DRSession->ratchetDecrypt(cipherHeader, AD, randomKey);
} catch (BctbxException &e) { // any bctbx Exception is just considered as decryption failed (it shall occurs only in case of maximum skipped keys reached)
bctbx_warning("Double Ratchet session failed to decrypt message and raised an exception saying : %s", e.what());
BCTBX_SLOGW<<"Double Ratchet session failed to decrypt message and raised an exception saying : "<<e.what();
decryptStatus = false; // lets keep trying with other sessions if provided
}
......
......@@ -96,8 +96,8 @@ namespace lime {
* - one to load the user from db based on provided user Id(which shall be GRUU)
* Note: ownership of localStorage pointer is transfered to a shared pointer, private menber of Lime class
*/
Lime(std::unique_ptr<lime::Db> &&localStorage, const std::string &userId, const std::string &url, belle_http_provider_t *http_provider);
Lime(std::unique_ptr<lime::Db> &&localStorage, const std::string &userId, const std::string &url, belle_http_provider_t *http_provider, const long Uid);
Lime(std::unique_ptr<lime::Db> &&localStorage, const std::string &deviceId, const std::string &url, belle_http_provider_t *http_provider);
Lime(std::unique_ptr<lime::Db> &&localStorage, const std::string &deviceId, const std::string &url, belle_http_provider_t *http_provider, const long Uid);
~Lime();
Lime(Lime<Curve> &a) = delete; // can't copy a session, force usage of shared pointers
Lime<Curve> &operator=(Lime<Curve> &a) = delete; // can't copy a session
......
......@@ -45,24 +45,24 @@ namespace lime {
* @brief Create a local lime user and insert all its needed data in a DB, it will trigger identity key creation and communication of it, SPKs and OPKs to key server
*
* @param[in] dbFilename path to the database to be used
* @param[in] userId a unique identifier to a local user, if not already present in base it will be inserted. Recommended value: device's GRUU
* @param[in] deviceId a unique identifier to a local user, if not already present in base it will be inserted. Recommended value: device's GRUU
* @param[in] keyServer URL of X3DH key server(WARNING : matching between elliptic curve usage of all clients on the same server is responsability of clients)
* @param[in] curve select which Elliptic curve to base X3DH and Double ratchet on: Curve25519 or Curve448,
* this is set once at user creation and can't be modified, it must reflect key server preference.
* @return a unique pointer to the object to be used by this user for any Lime operations
*/
std::shared_ptr<LimeGeneric> insert_LimeUser(const std::string &dbFilename, const std::string &userId, const std::string &url, const lime::CurveId curve,
std::shared_ptr<LimeGeneric> insert_LimeUser(const std::string &dbFilename, const std::string &deviceId, const std::string &url, const lime::CurveId curve,
belle_http_provider_t *http_provider, const limeCallback &callback);
/**
* @brief Load a local user from database
*
* @param[in] dbFilename path to the database to be used
* @param[in] userId a unique identifier to a local user, if not already present in base it will be inserted. Recommended value: device's GRUU
* @param[in] deviceId a unique identifier to a local user, if not already present in base it will be inserted. Recommended value: device's GRUU
*
* @return a unique pointer to the object to be used by this user for any Lime operations
*/
std::shared_ptr<LimeGeneric> load_LimeUser(const std::string &dbFilename, const std::string &userId, belle_http_provider_t *http_provider);
std::shared_ptr<LimeGeneric> load_LimeUser(const std::string &dbFilename, const std::string &deviceId, belle_http_provider_t *http_provider);
}
#endif // lime_lime_hpp
......@@ -304,7 +304,7 @@ bool DR<DHKey>::session_save() {
break;
case DRSessionDbStatus::clean: // Session is clean? So why have we been called?
default:
bctbx_error("Double ratchet session saved call on sessionId %ld but sessions appears to be clean", m_dbSessionId);
BCTBX_SLOGE<<"Double ratchet session saved call on sessionId "<<m_dbSessionId<<" but sessions appears to be clean";
break;
}
} catch (...) {
......@@ -654,7 +654,7 @@ long int Lime<Curve>::store_peerDevice(const std::string &peerDeviceId, const ED
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
bctbx_error("It appears that peer device %s was known with an identity key but is trying to use another one now", peerDeviceId.data());
BCTBX_SLOGE<<"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
......
......@@ -72,7 +72,7 @@ namespace lime {
auto verifyIk = bctbx_EDDSA_verify(EDDSAContext, peerBundle.SPk.data(), peerBundle.SPk.size(), nullptr, 0, peerBundle.SPk_sig.data(), peerBundle.SPk_sig.size());
if (verifyIk != BCTBX_VERIFY_SUCCESS) {
bctbx_DestroyEDDSAContext(EDDSAContext);
bctbx_error("X3DH: SPk signature verification failed for device %s", peerBundle.deviceId.data());
BCTBX_SLOGE<<"X3DH: SPk signature verification failed for device "<<peerBundle.deviceId;
throw BCTBX_EXCEPTION << "Verify signature on SPk failed for deviceId "<<peerBundle.deviceId;
}
......@@ -156,7 +156,7 @@ namespace lime {
}
m_DR_sessions_cache.emplace(peerBundle.deviceId, make_shared<DR<Curve>>(m_localStorage.get(), SK, AD, peerBundle.SPk, peerDid, X3DH_initMessage)); // will just do nothing if this peerDeviceId is already in cache
bctbx_message("X3DH created session with device %s", peerBundle.deviceId.data());
BCTBX_SLOGI<<"X3DH created session with device "<<peerBundle.deviceId;
}
}
......
......@@ -160,7 +160,7 @@ namespace lime {
message.push_back(static_cast<uint8_t>((peer_device_ids.size())&0xFF));
if (peer_device_ids.size()>0xFFFF) { // we're asking for more than 2^16 key bundles, really?
bctbx_warning("We are about to request for more than 2^16 key bundles to the X3DH server, it won't fit in protocol, truncate the request to 2^16 but it's very very unusual");
BCTBX_SLOGW<<"We are about to request for more than 2^16 key bundles to the X3DH server, it won't fit in protocol, truncate the request to 2^16 but it's very very unusual";
peer_device_ids.resize(0xFFFF); // resize to max possible value
}
......@@ -169,7 +169,7 @@ namespace lime {
message.push_back(static_cast<uint8_t>(((peer_device_id.size())>>8)&0xFF));
message.push_back(static_cast<uint8_t>((peer_device_id.size())&0xFF));
message.insert(message.end(),peer_device_id.begin(), peer_device_id.end());
bctbx_message("Request X3DH keys for device %s",peer_device_id.data());
BCTBX_SLOGI<<"Request X3DH keys for device "<<peer_device_id;
}
}
......@@ -186,21 +186,21 @@ namespace lime {
bool parseMessage_getType(const uint8_t *body, const size_t bodySize, x3dh_message_type &message_type, x3dh_error_code &error_code, const limeCallback callback) noexcept {
// check message holds at leat a header before trying to read it
if (body == nullptr || bodySize<X3DH_headerSize) {
bctbx_error("Got an invalid response from X3DH server");
BCTBX_SLOGE<<"Got an invalid response from X3DH server";
if (callback) callback(lime::callbackReturn::fail, "Got an invalid response from X3DH server");
return false;
}
// check X3DH protocol version
if (body[0] != static_cast<uint8_t>(X3DH_protocolVersion)) {
bctbx_error("X3DH server runs an other version of X3DH protocol(server %d - local %d)", body[0], static_cast<uint8_t>(X3DH_protocolVersion));
BCTBX_SLOGE<<"X3DH server runs an other version of X3DH protocol(server "<<int(body[0])<<" - local "<<static_cast<uint8_t>(X3DH_protocolVersion)<<")";
if (callback) callback(lime::callbackReturn::fail, "X3DH server and client protocol version mismatch");
return false;
}
// check curve id
if (body[2] != static_cast<uint8_t>(Curve::curveId())) {
bctbx_error("X3DH server runs curve Id %d while local is set to %d for this server)", body[2], static_cast<uint8_t>(Curve::curveId()));
BCTBX_SLOGE<<"X3DH server runs curve Id "<<int(body[2])<<" while local is set to "<<static_cast<uint8_t>(Curve::curveId())<<" for this server)";
if (callback) callback(lime::callbackReturn::fail, "X3DH server and client curve Id mismatch");
return false;
}
......@@ -239,9 +239,9 @@ namespace lime {
}
if (bodySize==X3DH_headerSize+1) {
bctbx_error("X3DH server respond error : code %x (no error message)", body[X3DH_headerSize]);
BCTBX_SLOGE<<"X3DH server respond error : code "<<int(body[X3DH_headerSize])<<" (no error message)";
} else {
bctbx_error("X3DH server respond error : code %x : %s", body[X3DH_headerSize], body+X3DH_headerSize+1);
BCTBX_SLOGE<<"X3DH server respond error : code "<<int(body[X3DH_headerSize])<<" : "<<std::string(body+X3DH_headerSize+1, body+bodySize);
}
switch (static_cast<uint8_t>(body[X3DH_headerSize])) {
......@@ -425,7 +425,7 @@ namespace lime {
auto thiz = userData->limeObj.lock(); // get a shared pointer to Lime Object from the weak pointer stored in userData
// check it is valid (lock() returns nullptr)
if (!thiz) { // our Lime caller object doesn't exists anymore
bctbx_error("Got response from X3DH server but our Lime Object has been destroyed");
BCTBX_SLOGE<<"Got response from X3DH server but our Lime Object has been destroyed";
delete(userData);
return;
}
......@@ -454,7 +454,7 @@ namespace lime {
if (message_type == lime::x3dh_protocol::x3dh_message_type::peerBundle) {
std::vector<X3DH_peerBundle<Curve>> peersBundle;
if (!x3dh_protocol::parseMessage_getPeerBundles(body, bodySize, peersBundle)) { // parsing went wrong
bctbx_error("Got an invalid peerBundle packet from X3DH server");
BCTBX_SLOGE<<"Got an invalid peerBundle packet from X3DH server";
if (callback) callback(lime::callbackReturn::fail, "Got an invalid peerBundle packet from X3DH server");
thiz->cleanUserData(userData);
return;
......
......@@ -330,7 +330,7 @@ long int get_DRsessionsId(const std::string &dbFilename, const std::string &self
return 0;
} catch (exception &e) { // swallow any error on DB
bctbx_error("Got an error on DB: %s", e.what());
BCTBX_SLOGE<<"Got an error on DB: "<<e.what();
sessionsId.clear();
return 0;
}
......
......@@ -77,12 +77,12 @@ int lime_tester_set_log_file(const char *filename) {
}
log_file = fopen(filename, "w");
if (!log_file) {
bctbx_error("Cannot open file [%s] for writing logs because [%s]", filename, strerror(errno));
BCTBX_SLOGE<<"Cannot open file ["<<std::string{filename}<<"] for writing logs because ["<<std::string{strerror(errno)}<<"]";
return -1;
}
dir = bctbx_dirname(filename);
base = bctbx_basename(filename);
bctbx_message("Redirecting traces to file [%s]", filename);
BCTBX_SLOGI<<"Redirecting traces to file ["<<std::string{filename}<<"]";
filehandler = bctbx_create_file_log_handler(0, dir, base, log_file);
bctbx_add_log_handler(filehandler);
if (dir) bctbx_free(dir);
......
......@@ -116,7 +116,7 @@ static void helloworld_basic_test(const lime::CurveId curve, const std::string &
counters.operation_success++;
} else {
counters.operation_failed++;
bctbx_error("Lime operation failed : %s", anythingToSay.data());
BCTBX_SLOGE<<"Lime operation failed : "<<anythingToSay;
}
});
......
......@@ -75,7 +75,7 @@ static void managersClean(std::unique_ptr<LimeManager> &alice, std::unique_ptr<L
bob = nullptr;
alice = unique_ptr<lime::LimeManager>(new lime::LimeManager(aliceDb, prov));
bob = std::unique_ptr<lime::LimeManager>(new lime::LimeManager(bobDb, prov));
bctbx_message("Trash and reload alice and bob LimeManagers");
BCTBX_SLOGI<<"Trash and reload alice and bob LimeManagers";
}
/* Alice encrypt to bob, bob replies so session is fully established, then alice encrypt more tjan maxSendingChain message so we must start a new session with bob
......@@ -101,7 +101,7 @@ static void x3dh_sending_chain_limit_test(const lime::CurveId curve, const std::
counters.operation_success++;
} else {
counters.operation_failed++;
bctbx_error("Lime operation failed : %s", anythingToSay.data());
BCTBX_SLOGE<<"Lime operation failed : "<<anythingToSay;
}
});
try {
......@@ -252,7 +252,7 @@ static void x3dh_multiple_DRsessions_test(const lime::CurveId curve, const std::
counters.operation_success++;
} else {
counters.operation_failed++;
bctbx_error("Lime operation failed : %s", anythingToSay.data());
BCTBX_SLOGE<<"Lime operation failed : "<<anythingToSay;
}
});
......@@ -428,7 +428,7 @@ static void x3dh_multidev_operation_queue_test(const lime::CurveId curve, const
counters.operation_success++;
} else {
counters.operation_failed++;
bctbx_error("Lime operation failed : %s", anythingToSay.data());
BCTBX_SLOGE<<"Lime operation failed : "<<anythingToSay;
}
});
......@@ -633,7 +633,7 @@ static void x3dh_operation_queue_test(const lime::CurveId curve, const std::stri
counters.operation_success++;
} else {
counters.operation_failed++;
bctbx_error("Lime operation failed : %s", anythingToSay.data());
BCTBX_SLOGE<<"Lime operation failed : "<<anythingToSay;
}
});
......@@ -757,7 +757,7 @@ static void x3dh_basic_test(const lime::CurveId curve, const std::string &dbBase
counters.operation_success++;
} else {
counters.operation_failed++;
bctbx_error("Lime operation failed : %s", anythingToSay.data());
BCTBX_SLOGE<<"Lime operation failed : "<<anythingToSay;
}
});
......
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