namespace ode {
-LuksClient::LuksClient(RmiClientPtr& ctx) : context(ctx)
+LuksClient::LuksClient(RmiClientPtr& ctx) :
+ context(ctx)
{
}
{
}
-int LuksClient::format(const std::string& device, const std::string& password)
+int LuksClient::format(bool sync,
+ const std::string& device,
+ const std::string& password)
{
try {
- return context->methodCall<int>("LuksServer::format", device, password);
+ return context->methodCall<int>("LuksServer::format",
+ sync,
+ device,
+ password);
} catch (runtime::Exception& e) {
return -1;
}
}
-int LuksClient::open(const std::string& device,
+int LuksClient::open(bool sync,
+ const std::string& device,
const std::string& password,
const std::string& mapping)
{
try {
- return context->methodCall<int>("LuksServer::open", device, password, mapping);
+ return context->methodCall<int>("LuksServer::open",
+ sync,
+ device,
+ password,
+ mapping);
} catch (runtime::Exception& e) {
return -1;
}
}
-int LuksClient::close(const std::string& mapping)
+int LuksClient::close(bool sync, const std::string& mapping)
{
try {
- return context->methodCall<int>("LuksServer::close", mapping);
+ return context->methodCall<int>("LuksServer::close", sync, mapping);
} catch (runtime::Exception& e) {
return -1;
}
explicit LuksClient(RmiClientPtr& ctxt);
~LuksClient();
- int format(const std::string& device, const std::string& password);
+ int format(bool sync,
+ const std::string& device,
+ const std::string& password);
- int open(const std::string& device,
+ int open(bool sync,
+ const std::string& device,
const std::string& password,
const std::string& mapping);
- int close(const std::string& mapping);
+ int close(bool sync, const std::string& mapping);
private:
RmiClientPtr& context;
{ OdeError::DeviceBusy, ODE_ERROR_RESOURCE_BUSY },
{ OdeError::Unknown, ODE_ERROR_UNKNOWN }
};
-} // anonymous namespace
-int ode_luks_format(const char* device, const char* password)
+int toApiError(int error)
+{
+ int ret = ODE_ERROR_UNKNOWN;
+ auto it = ODE_ERROR_TO_API_ERROR.find(static_cast<OdeError>(error));
+ if (it != ODE_ERROR_TO_API_ERROR.end())
+ ret = it->second;
+ return ret;
+}
+
+int luks_format_internal(bool sync, const char* device, const char* password)
{
RET_ON_FAILURE(device, ODE_ERROR_INVALID_PARAMETER);
RET_ON_FAILURE(password, ODE_ERROR_INVALID_PARAMETER);
RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
LuksClient luks = client.createInterface<LuksClient>();
- return luks.format(device, password);
+ return luks.format(sync, device, password);
}
-int ode_luks_open(const char* device, const char* password, const char* mapping)
+int luks_open_internal(bool sync,
+ const char* device,
+ const char* password,
+ const char* mapping)
{
RET_ON_FAILURE(device, ODE_ERROR_INVALID_PARAMETER);
RET_ON_FAILURE(password, ODE_ERROR_INVALID_PARAMETER);
RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
LuksClient luks = client.createInterface<LuksClient>();
- return luks.open(device, password, mapping);
+ return luks.open(sync, device, password, mapping);
}
-int ode_luks_close(const char* mapping)
+int luks_close_internal(bool sync, const char* mapping)
{
RET_ON_FAILURE(mapping, ODE_ERROR_INVALID_PARAMETER);
RET_ON_FAILURE(client.connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
LuksClient luks = client.createInterface<LuksClient>();
- return luks.close(mapping);
+ return luks.close(sync, mapping);
+}
+
+} // anonymous namespace
+
+int ode_luks_format(const char* device, const char* password)
+{
+ return luks_format_internal(false, device, password);
+}
+
+int ode_luks_open(const char* device, const char* password, const char* mapping)
+{
+ return luks_open_internal(false, device, password, mapping);
+}
+
+int ode_luks_close(const char* mapping)
+{
+ return luks_close_internal(false, mapping);
}
int ode_luks_set_event_cb(ode_luks_event_cb callback, void *user_data)
RET_ON_FAILURE(luksEventCallbackContext->connect() == 0, ODE_ERROR_CONNECTION_REFUSED);
std::function<void(std::string, int, int)>cb = [callback, user_data](std::string, int op, int res) {
- int ret = ODE_ERROR_UNKNOWN;
- auto it = ODE_ERROR_TO_API_ERROR.find(static_cast<OdeError>(res));
- if (it != ODE_ERROR_TO_API_ERROR.end())
- ret = it->second;
-
- callback(static_cast<ode_luks_operation_e>(op), ret, user_data);
+ callback(static_cast<ode_luks_operation_e>(op), toApiError(res), user_data);
};
ret = luksEventCallbackContext->subscribeSignal(Luks::NOTIFICATION, cb);
luksEventCallbackContext.reset();
}
+int ode_luks_format_sync(const char* device, const char* password)
+{
+ int err = luks_format_internal(true, device, password);
+ return toApiError(err);
+}
+
+int ode_luks_open_sync(const char* device, const char* password, const char* mapping)
+{
+ int err = luks_open_internal(true, device, password, mapping);
+ return toApiError(err);
+}
+
+int ode_luks_close_sync(const char* mapping)
+{
+ int err = luks_close_internal(true, mapping);
+ return toApiError(err);
+}
/**
* @brief Formats given device as LUKS using given password.
*
- * @details The device will be formatted as LUKS but the password will be
- * stored in internal storage. This function is asynchronous. If a
- * callback was registered using ode_luks_set_event_cb() then it
+ * @details The device will be formatted as LUKS but the encryption key will
+ * be stored in internal storage. This function is asynchronous. If
+ * a callback was registered using ode_luks_set_event_cb() then it
* will be invoked when the operation is finished.
*
* @since_tizen 4.0
*
* @see ode_luks_open()
* @see ode_luks_set_event_cb()
+ * @see ode_luks_format_sync()
*/
ODE_API int ode_luks_format(const char* device, const char* password);
* @see ode_luks_format()
* @see ode_luks_close()
* @see ode_luks_set_event_cb()
+ * @see ode_luks_open_sync()
*/
ODE_API int ode_luks_open(const char* device,
const char* password,
* @see ode_luks_format()
* @see ode_luks_open()
* @see ode_luks_set_event_cb()
+ * @see ode_luks_close_sync()
*/
ODE_API int ode_luks_close(const char* mapping);
ODE_API void ode_luks_unset_event_cb();
/**
+ * @brief Formats given device as LUKS using given password.
+ *
+ * @details The device will be formatted as LUKS but the encryption key will
+ * be stored in internal storage. This function is synchronous.
+ *
+ * @since_tizen 4.0
+ *
+ * @param[in] device The device to be formatted
+ * @param[in] password The password used for formatting
+ *
+ * @return #ODE_ERROR_NONE on success, otherwise a negative value.
+ * @retval #ODE_ERROR_NONE Successful
+ * @retval #ODE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #ODE_ERROR_CONNECTION_REFUSED Connection to the server failed
+ * @retval #ODE_ERROR_OUT_OF_MEMORY Out of memory
+ * @retval #ODE_ERROR_NO_SUCH_FILE Wrong device
+ * @retval #ODE_ERROR_RESOURCE_BUSY Device is busy
+ * @retval #ODE_ERROR_UNKNOWN Unknown error
+ *
+ * @pre The @a device must be present and neither mapped nor mounted.
+ *
+ * @post Formatted device can be later opened using ode_luks_open(). The
+ * mapping will not contain a valid file system. It needs to be
+ * formatted.
+ *
+ * @see ode_luks_open_sync()
+ * @see ode_luks_format()
+ */
+ODE_API int ode_luks_format_sync(const char* device, const char* password);
+
+/**
+ * @brief Creates a mapping with given name backed by a LUKS device.
+ *
+ * @details This function is synchronous.
+ *
+ * @since_tizen 4.0
+ *
+ * @param[in] device The device to be used
+ * @param[in] password The password used for mapping creation that was used
+ * for formatting.
+ * @param[in] mapping The desired name of the mapping
+ *
+ * @return #ODE_ERROR_NONE on success, otherwise a negative value.
+ * @retval #ODE_ERROR_NONE Successful
+ * @retval #ODE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #ODE_ERROR_CONNECTION_REFUSED Connection to the server failed
+ * @retval #ODE_ERROR_KEY_REJECTED Wrong password
+ * @retval #ODE_ERROR_OUT_OF_MEMORY Out of memory
+ * @retval #ODE_ERROR_NO_SUCH_FILE Wrong device or mapping
+ * @retval #ODE_ERROR_RESOURCE_BUSY Device is busy
+ * @retval #ODE_ERROR_UNKNOWN Unknown error
+ *
+ * @pre The @a device must be a LUKS device formatted with
+ * ode_luks_format(). The mapping must not exist.
+ *
+ * @post The created mapping can be later mounted to access the encrypted
+ * storage.
+ * @post To close the mapping call ode_luks_close()
+ *
+ * @see ode_luks_format_sync()
+ * @see ode_luks_close_sync()
+ * @see ode_luks_open()
+ */
+ODE_API int ode_luks_open_sync(const char* device,
+ const char* password,
+ const char* mapping);
+
+/**
+ * @brief Removes a mapping with given name backed by a LUKS device.
+ *
+ * @details This function is synchronous.
+ *
+ * @since_tizen 4.0
+ *
+ * @param[in] mapping The name of the mapping to be closed
+ *
+ * @return #ODE_ERROR_NONE on success, otherwise a negative value.
+ * @retval #ODE_ERROR_NONE Successful
+ * @retval #ODE_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #ODE_ERROR_CONNECTION_REFUSED Connection to the server failed
+ * @retval #ODE_ERROR_OUT_OF_MEMORY Out of memory
+ * @retval #ODE_ERROR_NO_SUCH_FILE Wrong mapping
+ * @retval #ODE_ERROR_RESOURCE_BUSY Device is busy
+ * @retval #ODE_ERROR_UNKNOWN Unknown error
+ *
+ * @pre The mapping must be created by a call to ode_luks_open().
+ * The mapping must not be mounted.
+ *
+ * @see ode_luks_format_sync()
+ * @see ode_luks_open_sync()
+ * @see ode_luks_close()
+ */
+ODE_API int ode_luks_close_sync(const char* mapping);
+
+/**
* @}
*/
class Luks {
public:
- virtual int format(const std::string& device,
+ virtual int format(bool sync,
+ const std::string& device,
const std::string& password) = 0;
- virtual int open(const std::string& device,
+ virtual int open(bool sync,
+ const std::string& device,
const std::string& password,
const std::string& mapping) = 0;
- virtual int close(const std::string& mapping) = 0;
+ virtual int close(bool sync, const std::string& mapping) = 0;
enum Operation {
Format = 1,
LuksServer::LuksServer(ServerContext &srv) : server(srv)
{
- server.expose(this, PRIVILEGE_PLATFORM, (int)(LuksServer::format)(std::string, std::string));
- server.expose(this, PRIVILEGE_PLATFORM, (int)(LuksServer::open)(std::string, std::string, std::string));
- server.expose(this, PRIVILEGE_PLATFORM, (int)(LuksServer::close)(std::string));
+ server.expose(this, PRIVILEGE_PLATFORM, (int)(LuksServer::format)(bool, std::string, std::string));
+ server.expose(this, PRIVILEGE_PLATFORM, (int)(LuksServer::open)(bool, std::string, std::string, std::string));
+ server.expose(this, PRIVILEGE_PLATFORM, (int)(LuksServer::close)(bool, std::string));
server.createNotification(NOTIFICATION);
}
}
template <typename F>
-int LuksServer::asyncJob(Luks::Operation op, const F& job)
+int LuksServer::execute(bool sync, Luks::Operation op, const F& job)
{
auto worker = [=]() {
OdeError ret = OdeError::Unknown;
" thread failed: unknown exception.");
ret = OdeError::Unknown;
}
- server.notify(NOTIFICATION, static_cast<int>(op), static_cast<int>(ret));
+ if (sync) {
+ return static_cast<int>(ret);
+ } else {
+ server.notify(NOTIFICATION, static_cast<int>(op), static_cast<int>(ret));
+ return 0;
+ }
};
- std::thread work(worker);
- work.detach();
- return 0;
+ if (sync) {
+ return worker();
+ } else {
+ std::thread work(worker);
+ work.detach();
+ return 0;
+ }
}
-int LuksServer::format(const std::string& device, const std::string& password)
+int LuksServer::format(bool sync,
+ const std::string& device,
+ const std::string& password)
{
- return asyncJob(Luks::Format, [=](){
+ return execute(sync, Luks::Format, [=](){
KeyManager::data pwData(password.begin(), password.end());
KeyManager keyManager;
keyManager.initPassword(pwData, DEFAULT_KEY_SIZE);
});
}
-int LuksServer::open(const std::string& device,
+int LuksServer::open(bool sync,
+ const std::string& device,
const std::string& password,
const std::string& mapping)
{
- return asyncJob(Luks::Open, [=](){
+ return execute(sync, Luks::Open, [=](){
CryptsetupEngine engine(device);
KeyManager::data pwData(password.begin(), password.end());
});
}
-int LuksServer::close(const std::string& mapping)
+int LuksServer::close(bool sync,
+ const std::string& mapping)
{
- return asyncJob(Luks::Close, [=](){
+ return execute(sync, Luks::Close, [=](){
CryptsetupEngine::close(mapping);
return OdeError::None;
});
explicit LuksServer(ServerContext& srv);
~LuksServer();
- int format(const std::string& device, const std::string& password);
- int open(const std::string& device,
+ int format(bool sync,
+ const std::string& device,
+ const std::string& password);
+
+ int open(bool sync,
+ const std::string& device,
const std::string& password,
const std::string& mapping);
- int close(const std::string& mapping);
+
+ int close(bool sync, const std::string& mapping);
private:
template <typename F>
- int asyncJob(Luks::Operation op, const F& job);
+ int execute(bool sync, Luks::Operation op, const F& job);
ServerContext& server;