2 * Copyright (c) 2017 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
18 #include <klay/dbus/connection.h>
23 #include "engine/encryption/cryptsetup-engine.h"
24 #include "rmi/common.h"
29 const char *PRIVILEGE_PLATFORM = "http://tizen.org/privilege/internal/default/platform";
31 std::map<Luks::Operation, std::string> OPERATION_NAME = {
32 { Luks::Format, "Formatting" },
33 { Luks::Open, "Opening" },
34 { Luks::Close, "Closing" },
37 std::map<CryptsetupEngine::ReturnCode, int> CRYPTSETUP_ERROR_2_ODE_ERROR = {
38 { CryptsetupEngine::ReturnCode::SUCCESS, error::None },
39 { CryptsetupEngine::ReturnCode::WRONG_PARAMS, error::InvalidParameter },
40 { CryptsetupEngine::ReturnCode::NO_PERMISSION, error::WrongPassword },
41 { CryptsetupEngine::ReturnCode::OUT_OF_MEMORY, error::OutOfMemory },
42 { CryptsetupEngine::ReturnCode::WRONG_DEVICE, error::NoSuchFile },
43 { CryptsetupEngine::ReturnCode::DEVICE_BUSY, error::DeviceBusy }
46 } // anonymous namespace
48 LuksServer::LuksServer(ServerContext &srv, KeyServer& key) :
52 server.expose(this, PRIVILEGE_PLATFORM, (int)(LuksServer::format)(bool, std::string, std::string));
53 server.expose(this, PRIVILEGE_PLATFORM, (int)(LuksServer::open)(bool, std::string, std::string, std::string));
54 server.expose(this, PRIVILEGE_PLATFORM, (int)(LuksServer::close)(bool, std::string));
56 server.createNotification(NOTIFICATION);
59 LuksServer::~LuksServer()
64 int LuksServer::execute(bool sync, Luks::Operation op, const F& job)
67 RequestLifetime rl(server);
69 int ret = error::Unknown;
71 std::lock_guard<std::mutex> guardLock(opGuard);
73 } catch (const CryptsetupEngine::Exception& e) {
74 ERROR(SINK, OPERATION_NAME.at(op) + " thread failed: " +
75 std::string(e.what()));
76 auto it = CRYPTSETUP_ERROR_2_ODE_ERROR.find(e.retCode());
77 if (it != CRYPTSETUP_ERROR_2_ODE_ERROR.end())
79 } catch (const std::exception& e) {
80 ERROR(SINK, OPERATION_NAME.at(op) + " thread failed: " +
81 std::string(e.what()));
84 ERROR(SINK, OPERATION_NAME.at(op) +
85 " thread failed: unknown exception.");
91 server.notify(NOTIFICATION, static_cast<int>(op), ret);
99 std::thread work(worker);
105 int LuksServer::format(bool sync,
106 const std::string& device,
107 const std::string& password)
109 return execute(sync, Luks::Format, [=](){
111 int ret = keyServer.initAndGet(device, password, Key::DEFAULT_512BIT, key);
112 if (ret != error::None)
115 CryptsetupEngine engine(device);
116 engine.format(CryptsetupEngine::DeviceType::LUKS, key);
121 int LuksServer::open(bool sync,
122 const std::string& device,
123 const std::string& password,
124 const std::string& mapping)
126 return execute(sync, Luks::Open, [=](){
127 CryptsetupEngine engine(device);
130 int ret = keyServer.get(device, password, key);
131 if (ret != error::None)
134 engine.open(CryptsetupEngine::DeviceType::LUKS, mapping, key);
139 int LuksServer::close(bool sync,
140 const std::string& mapping)
142 return execute(sync, Luks::Close, [=](){
143 CryptsetupEngine::close(mapping);