if (0 == m_userDataMap.count(cred.uid))
return KEY_MANAGER_API_ERROR_DB_LOCKED;
- DBRow row = { alias, cred.smackLabel, policy.restricted,
+ DBRow row = { alias, cred.smackLabel, policy.restricted,
policy.extractable, dataType, DBCMAlgType::NONE,
0, RawBuffer(10, 'c'), key.size(), key };
auto &handler = m_userDataMap[cred.uid];
if (!handler.crypto.haveKey(cred.smackLabel)) {
RawBuffer key;
- int status = handler.database.getKey(cred.smackLabel, key);
- if (KEY_MANAGER_API_ERROR_DB_ALIAS_UNKNOWN == status) {
- LogDebug("No Key in database found. Generating new one for label: " << cred.smackLabel);
+ auto key_optional = handler.database.getKey(cred.smackLabel);
+ if(!key_optional) {
+ LogDebug("No Key in database found. Generating new one for label: "
+ << cred.smackLabel);
key = handler.keyProvider.generateDEK(cred.smackLabel);
- if (KEY_MANAGER_API_SUCCESS != handler.database.saveKey(cred.smackLabel, key)) {
- LogError("Failed to save key for smack label: " << cred.smackLabel);
- return KEY_MANAGER_API_ERROR_DB_ERROR;
- }
+ } else {
+ key = *key_optional;
}
+
key = handler.keyProvider.getPureDEK(key);
handler.crypto.pushKey(cred.smackLabel, key);
+ handler.database.saveKey(cred.smackLabel, key);
}
handler.crypto.encryptRow(policy.password, row);
- return handler.database.saveDBRow(row);
+ handler.database.saveDBRow(row);
+ return KEY_MANAGER_API_SUCCESS;
}
RawBuffer CKMLogic::saveData(
const PolicySerializable &policy)
{
int retCode = KEY_MANAGER_API_SUCCESS;
-
try {
retCode = saveDataHelper(cred, dataType, alias, key, policy);
LogDebug("SaveDataHelper returned: " << retCode);
} catch (const DBCryptoModule::Exception::Base &e) {
LogError("DBCryptoModule failed with message: " << e.GetMessage());
retCode = KEY_MANAGER_API_ERROR_SERVER_ERROR;
+ } catch (const DBCrypto::Exception::InternalError &e) {
+ LogError("DBCrypto failed with message: " << e.GetMessage());
+ retCode = KEY_MANAGER_API_ERROR_DB_ERROR;
+ } catch (const DBCrypto::Exception::AliasExists &e) {
+ LogError("DBCrypto couldn't save duplicate alias");
+ retCode = KEY_MANAGER_API_ERROR_DB_ALIAS_EXISTS;
}
MessageBuffer response;
int retCode = KEY_MANAGER_API_SUCCESS;
if (0 < m_userDataMap.count(cred.uid)) {
- retCode = m_userDataMap[cred.uid].database.deleteDBRow(alias, cred.smackLabel);
+ Try {
+ m_userDataMap[cred.uid].database.deleteDBRow(alias, cred.smackLabel);
+ } Catch (CKM::Exception) {
+ LogError("Error in deleting row!");
+ retCode = KEY_MANAGER_API_ERROR_DB_ERROR;
+ }
} else {
retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
}
const std::string &password,
DBRow &row)
{
- int retCode = KEY_MANAGER_API_SUCCESS;
if (0 == m_userDataMap.count(cred.uid))
return KEY_MANAGER_API_ERROR_DB_LOCKED;
auto &handler = m_userDataMap[cred.uid];
+ DBCrypto::DBRowOptional row_optional;
if (dataType == DBDataType::CERTIFICATE || dataType == DBDataType::BINARY_DATA) {
- retCode = handler.database.getDBRow(alias, cred.smackLabel, dataType, row);
+ row_optional = handler.database.getDBRow(alias, cred.smackLabel, dataType);
} else if ((static_cast<int>(dataType) >= static_cast<int>(DBDataType::DB_KEY_FIRST))
&& (static_cast<int>(dataType) <= static_cast<int>(DBDataType::DB_KEY_LAST)))
{
- retCode = handler.database.getKeyDBRow(alias, cred.smackLabel, row);
+ row_optional = handler.database.getKeyDBRow(alias, cred.smackLabel);
} else {
LogError("Unknown type of requested data" << (int)dataType);
return KEY_MANAGER_API_ERROR_BAD_REQUEST;
}
-
- if (KEY_MANAGER_API_SUCCESS != retCode){
- LogDebug("DBCrypto::getDBRow failed with code: " << retCode);
- return retCode;
+ if(!row_optional) {
+ LogError("No row for given alias, label and type");
+ return KEY_MANAGER_API_ERROR_DB_ALIAS_UNKNOWN;
+ } else {
+ row = *row_optional;
}
if (!handler.crypto.haveKey(row.smackLabel)) {
RawBuffer key;
- retCode = handler.database.getKey(row.smackLabel, key);
- if (KEY_MANAGER_API_SUCCESS != retCode) {
- LogDebug("DBCrypto::getKey failed with: " << retCode);
- return retCode;
+ auto key_optional = handler.database.getKey(row.smackLabel);
+ if(!key_optional) {
+ LogError("No key for given label in database");
+ return KEY_MANAGER_API_ERROR_DB_ERROR;
}
+ key = *key_optional;
key = handler.keyProvider.getPureDEK(key);
handler.crypto.pushKey(cred.smackLabel, key);
}
} catch (const DBCryptoModule::Exception::Base &e) {
LogError("DBCryptoModule failed with message: " << e.GetMessage());
retCode = KEY_MANAGER_API_ERROR_SERVER_ERROR;
+ } catch (const DBCrypto::Exception::Base &e) {
+ LogError("DBCrypto failed with message: " << e.GetMessage());
+ retCode = KEY_MANAGER_API_ERROR_DB_ERROR;
}
if (KEY_MANAGER_API_SUCCESS != retCode) {
if (0 < m_userDataMap.count(cred.uid)) {
auto &handler = m_userDataMap[cred.uid];
- if (dataType == DBDataType::CERTIFICATE || dataType == DBDataType::BINARY_DATA) {
- retCode = handler.database.getAliases(dataType, cred.smackLabel, aliasVector);
- } else {
- retCode = handler.database.getKeyAliases(cred.smackLabel, aliasVector);
+ Try {
+ if (dataType == DBDataType::CERTIFICATE || dataType == DBDataType::BINARY_DATA) {
+ handler.database.getAliases(dataType, cred.smackLabel, aliasVector);
+ } else {
+ handler.database.getKeyAliases(cred.smackLabel, aliasVector);
+ }
+ } Catch (CKM::Exception) {
+ LogError("Failed to get aliases");
+ retCode = KEY_MANAGER_API_ERROR_DB_ERROR;
}
} else {
retCode = KEY_MANAGER_API_ERROR_DB_LOCKED;
DBCrypto::DBCrypto(const std::string& path,
const RawBuffer &rawPass) {
m_connection = NULL;
- m_init = false;
Try {
m_connection = new SqlConnection(path, SqlConnection::Flag::Option::CRW);
m_connection->SetKey(rawPass);
initDatabase();
- m_init = true;
} Catch(SqlConnection::Exception::ConnectionBroken) {
LogError("Couldn't connect to database: " << path);
+ ReThrow(DBCrypto::Exception::InternalError);
} Catch(SqlConnection::Exception::InvalidArguments) {
LogError("Couldn't set the key for database");
+ ReThrow(DBCrypto::Exception::InternalError);
} Catch(SqlConnection::Exception::SyntaxError) {
LogError("Couldn't initiate the database");
+ ReThrow(DBCrypto::Exception::InternalError);
+ } Catch(SqlConnection::Exception::InternalError) {
+ LogError("Couldn't create the database");
+ ReThrow(DBCrypto::Exception::InternalError);
}
}
DBCrypto::DBCrypto(DBCrypto &&other) :
- m_connection(other.m_connection),
- m_init(other.m_init) {
+ m_connection(other.m_connection) {
other.m_connection = NULL;
- other.m_init = false;
}
DBCrypto::~DBCrypto() {
m_connection = other.m_connection;
other.m_connection = NULL;
- m_init = other.m_init;
- other.m_init = false;
-
return *this;
}
- void DBCrypto::createTable(const char* create_cmd) {
+ void DBCrypto::createTable(
+ const char* create_cmd,
+ const char *table_name)
+ {
Try {
m_connection->ExecCommand(create_cmd);
} Catch(SqlConnection::Exception::SyntaxError) {
- LogError("Couldn't create the main table!");
+ LogError("Couldn't create table : " << table_name << "!");
throw;
}
}
void DBCrypto::initDatabase() {
if(!m_connection->CheckTableExist(main_table)) {
- createTable(db_create_main_cmd);
+ createTable(db_create_main_cmd, main_table);
}
if(!m_connection->CheckTableExist(key_table)) {
- createTable(db_create_key_cmd);
+ createTable(db_create_key_cmd, key_table);
}
}
return false;
}
- int DBCrypto::saveDBRow(const DBRow &row){
- if(!m_init)
- return KEY_MANAGER_API_ERROR_DB_ERROR;
+ void DBCrypto::saveDBRow(const DBRow &row){
Try {
//Sqlite does not support partial index in our version,
//so we do it by hand
if((row.restricted == 1 && checkAliasExist(row.alias, row.smackLabel)) ||
(row.restricted == 0 && checkGlobalAliasExist(row.alias))) {
- return KEY_MANAGER_API_ERROR_DB_ALIAS_EXISTS;
+ ThrowMsg(DBCrypto::Exception::AliasExists,
+ "Alias exists for alias: " << row.alias
+ << ", label: " << row.smackLabel);
}
SqlConnection::DataCommandAutoPtr insertCommand =
insertCommand->BindBlob(10, row.data);
insertCommand->Step();
+ return;
} Catch(SqlConnection::Exception::SyntaxError) {
LogError("Couldn't prepare insert statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch(SqlConnection::Exception::InternalError) {
LogError("Couldn't execute insert statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
}
-
- return KEY_MANAGER_API_SUCCESS;
+ ThrowMsg(DBCrypto::Exception::InternalError,
+ "Couldn't save DBRow");
}
DBRow DBCrypto::getRow(const SqlConnection::DataCommandAutoPtr &selectCommand) {
return row;
}
- int DBCrypto::getDBRow(
+ DBCrypto::DBRowOptional DBCrypto::getDBRow(
const Alias &alias,
const std::string &label,
- DBDataType type,
- DBRow &row)
+ DBDataType type)
{
- if(!m_init)
- return KEY_MANAGER_API_ERROR_DB_ERROR;
Try {
- SqlConnection::DataCommandAutoPtr selectCommand =
- m_connection->PrepareDataCommand(select_alias_cmd);
- selectCommand->BindString(1, alias.c_str());
- selectCommand->BindInteger(2, static_cast<int>(type));
- selectCommand->BindString(3, label.c_str());
- selectCommand->BindString(4, alias.c_str());
- selectCommand->BindInteger(5, static_cast<int>(type));
-
-
- if(selectCommand->Step()) {
- row = getRow(selectCommand);
- } else {
- return KEY_MANAGER_API_ERROR_DB_ALIAS_UNKNOWN;
- }
+ SqlConnection::DataCommandAutoPtr selectCommand =
+ m_connection->PrepareDataCommand(select_alias_cmd);
+ selectCommand->BindString(1, alias.c_str());
+ selectCommand->BindInteger(2, static_cast<int>(type));
+ selectCommand->BindString(3, label.c_str());
+ selectCommand->BindString(4, alias.c_str());
+ selectCommand->BindInteger(5, static_cast<int>(type));
- AssertMsg(!selectCommand->Step(),
- "Select returned multiple rows for unique column.");
+ if(selectCommand->Step()) {
+ return DBRowOptional(getRow(selectCommand));
+ } else {
+ return DBRowOptional();
+ }
} Catch (SqlConnection::Exception::InvalidColumn) {
LogError("Select statement invalid column error");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::SyntaxError) {
LogError("Couldn't prepare select statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::InternalError) {
LogError("Couldn't execute select statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
}
- return KEY_MANAGER_API_SUCCESS;
+ ThrowMsg(DBCrypto::Exception::InternalError,
+ "Couldn't get row for type " << static_cast<int>(type) <<
+ " alias " << alias << " and label " << label);
}
- int DBCrypto::getKeyDBRow(
+ DBCrypto::DBRowOptional DBCrypto::getKeyDBRow(
const Alias &alias,
- const std::string &label,
- DBRow &row) {
- if (!m_init)
- return KEY_MANAGER_API_ERROR_DB_ERROR;
+ const std::string &label)
+ {
Try{
SqlConnection::DataCommandAutoPtr selectCommand =
m_connection->PrepareDataCommand(select_key_alias_cmd);
selectCommand->BindInteger(3, static_cast<int>(DBDataType::DB_KEY_LAST));
selectCommand->BindString(4, label.c_str());
- if(selectCommand->Step()){
- row = getRow(selectCommand);
+ if(selectCommand->Step()) {
+ return DBRowOptional(getRow(selectCommand));
} else {
- return KEY_MANAGER_API_ERROR_DB_ALIAS_UNKNOWN;
+ return DBRowOptional();
}
} Catch (SqlConnection::Exception::InvalidColumn) {
LogError("Select statement invalid column error");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::SyntaxError) {
LogError("Couldn't prepare select statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::InternalError) {
LogError("Couldn't execute select statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
}
- return KEY_MANAGER_API_SUCCESS;
+ ThrowMsg(DBCrypto::Exception::InternalError,
+ "Couldn't get Key for alias " << alias
+ << " and label " << label);
}
- int DBCrypto::getSingleType(
+ void DBCrypto::getSingleType(
DBDataType type,
const std::string& label,
AliasVector& aliases)
alias = selectCommand->GetColumnString(0);
aliases.push_back(alias);
}
+ return;
} Catch (SqlConnection::Exception::InvalidColumn) {
LogError("Select statement invalid column error");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::SyntaxError) {
LogError("Couldn't prepare select statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::InternalError) {
LogError("Couldn't execute select statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
}
- return KEY_MANAGER_API_SUCCESS;
+ ThrowMsg(DBCrypto::Exception::InternalError,
+ "Couldn't get type " << static_cast<int>(type)
+ << " for label " << label);
}
- int DBCrypto::getAliases(
+ void DBCrypto::getAliases(
DBDataType type,
const std::string& label,
AliasVector& aliases)
{
- if(!m_init)
- return KEY_MANAGER_API_ERROR_DB_ERROR;
-
- return getSingleType(type, label, aliases);
+ getSingleType(type, label, aliases);
}
- int DBCrypto::getKeyAliases(
+ void DBCrypto::getKeyAliases(
const std::string &label,
AliasVector &aliases)
{
- if (!m_init)
- return KEY_MANAGER_API_ERROR_DB_ERROR;
-
Try{
SqlConnection::DataCommandAutoPtr selectCommand =
m_connection->PrepareDataCommand(select_key_type_cmd);
alias = selectCommand->GetColumnString(0);
aliases.push_back(alias);
}
+ return;
} Catch (SqlConnection::Exception::InvalidColumn) {
LogError("Select statement invalid column error");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::SyntaxError) {
LogError("Couldn't prepare select statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::InternalError) {
LogError("Couldn't execute select statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
}
- return KEY_MANAGER_API_SUCCESS;
+ ThrowMsg(DBCrypto::Exception::InternalError,
+ "Couldn't get key aliases for label " << label);
}
- int DBCrypto::deleteDBRow(
+ void DBCrypto::deleteDBRow(
const Alias &alias,
const std::string &label)
{
deleteCommand->BindString(1, alias.c_str());
deleteCommand->BindString(2, label.c_str());
deleteCommand->Step();
+ return;
} Catch (SqlConnection::Exception::SyntaxError) {
LogError("Couldn't prepare delete statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::InternalError) {
LogError("Couldn't execute delete statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
}
- return KEY_MANAGER_API_SUCCESS;
+ ThrowMsg(DBCrypto::Exception::InternalError,
+ "Couldn't delete DBRow for alias " << alias
+ << " and label " << label);
}
- int DBCrypto::saveKey(
+ void DBCrypto::saveKey(
const std::string& label,
const RawBuffer &key)
{
- if (!m_init)
- return KEY_MANAGER_API_ERROR_DB_ERROR;
-
Try {
SqlConnection::DataCommandAutoPtr insertCommand =
m_connection->PrepareDataCommand(insert_key_cmd);
insertCommand->BindString(1, label.c_str());
insertCommand->BindBlob(2, key);
-
insertCommand->Step();
-
+ return;
} Catch (SqlConnection::Exception::SyntaxError) {
LogError("Couldn't prepare insert key statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::InternalError) {
LogError("Couldn't execute insert statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
}
-
- return KEY_MANAGER_API_SUCCESS;
+ ThrowMsg(DBCrypto::Exception::InternalError,
+ "Couldn't save key for label " << label);
}
- int DBCrypto::getKey(
- const std::string& label,
- RawBuffer &key)
- {
- if (!m_init)
- return KEY_MANAGER_API_ERROR_DB_ERROR;
+ DBCrypto::RawBufferOptional DBCrypto::getKey(
+ const std::string& label)
+ {
Try {
SqlConnection::DataCommandAutoPtr selectCommand =
m_connection->PrepareDataCommand(select_key_cmd);
selectCommand->BindString(1, label.c_str());
if (selectCommand->Step()) {
- key = selectCommand->GetColumnBlob(0);
+ return RawBufferOptional(
+ selectCommand->GetColumnBlob(0));
} else {
- return KEY_MANAGER_API_ERROR_DB_ALIAS_UNKNOWN;
+ return RawBufferOptional();
}
- AssertMsg(!selectCommand->Step(),
- "Select returned multiple rows for unique column.");
-
} Catch (SqlConnection::Exception::InvalidColumn) {
LogError("Select statement invalid column error");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::SyntaxError) {
LogError("Couldn't prepare insert key statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::InternalError) {
LogError("Couldn't execute insert statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
}
- return KEY_MANAGER_API_SUCCESS;
+ ThrowMsg(DBCrypto::Exception::InternalError,
+ "Couldn't get key for label " << label);
}
- int DBCrypto::deleteKey(const std::string& label) {
- if (!m_init)
- return KEY_MANAGER_API_ERROR_DB_ERROR;
+ void DBCrypto::deleteKey(const std::string& label) {
Try {
SqlConnection::DataCommandAutoPtr deleteCommand =
m_connection->PrepareDataCommand(delete_key_cmd);
deleteCommand->BindString(1, label.c_str());
-
deleteCommand->Step();
-
+ return;
} Catch (SqlConnection::Exception::SyntaxError) {
LogError("Couldn't prepare insert key statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
} Catch (SqlConnection::Exception::InternalError) {
LogError("Couldn't execute insert statement");
- return KEY_MANAGER_API_ERROR_DB_ERROR;
}
- return KEY_MANAGER_API_SUCCESS;
+ ThrowMsg(DBCrypto::Exception::InternalError,
+ "Couldn't delete key for label " << label);
}
} // CKM