2 * Copyright (c) 2015 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
20 #include <sys/mount.h>
21 #include <sys/reboot.h>
24 #include <klay/process.h>
25 #include <klay/file-user.h>
26 #include <klay/filesystem.h>
27 #include <klay/dbus/connection.h>
28 #include <klay/audit/logger.h>
30 #include "engine/dmcrypt-engine.h"
31 #include "key-manager/key-manager.h"
33 #include "rmi/internal-encryption.h"
35 #define INTERNAL_STORAGE_PATH "/opt/usr"
36 #define INTERNAL_STATE_VCONF_KEY VCONFKEY_ODE_CRYPTO_STATE
37 #define INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY VCONFKEY_ODE_FAST_ENCRYPTION
43 VConfBackend vconfBackend(VCONFKEY_ODE_ENCRYPT_PROGRESS);
44 ProgressBar progressBar(std::bind(&VConfBackend::update, &vconfBackend, std::placeholders::_1));
46 DMCryptEngine engine("/dev/mmcblk0p25", INTERNAL_STORAGE_PATH, progressBar);
48 void stopDependedSystemdServices()
50 dbus::Connection& systemDBus = dbus::Connection::getSystem();
51 std::set<std::string> servicesToStop;
53 for (pid_t pid : runtime::FileUser::getList(INTERNAL_STORAGE_PATH, true)) {
56 systemDBus.methodcall("org.freedesktop.systemd1",
57 "/org/freedesktop/systemd1",
58 "org.freedesktop.systemd1.Manager",
60 -1, "(o)", "(u)", (unsigned int)pid)
61 .get("(o)", &service);
62 servicesToStop.insert(service);
63 } catch (runtime::Exception &e) {
64 INFO("Close process - " + std::to_string(pid));
69 for (const std::string& service : servicesToStop) {
70 INFO("Close service - " + service);
71 systemDBus.methodcall("org.freedesktop.systemd1",
73 "org.freedesktop.systemd1.Unit",
75 -1, "", "(s)", "flush");
79 void showProgressUI(const std::string type) {
80 std::vector<std::string> args = {
81 "ode", "progress", type, "Internal"
84 runtime::Process proc("/usr/bin/ode", args);
88 unsigned int getOptions()
90 unsigned int result = 0;
94 ::vconf_get_bool(INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY, &value);
96 result |= InternalEncryption::Option::IncludeUnusedRegion;
102 void setOptions(unsigned int options)
106 if (options & InternalEncryption::Option::IncludeUnusedRegion) {
111 ::vconf_set_bool(INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY, value);
116 InternalEncryption::InternalEncryption(ODEControlContext& ctx) :
119 context.registerNonparametricMethod(this, "", (int)(InternalEncryption::getState));
120 context.registerNonparametricMethod(this, "", (unsigned int)(InternalEncryption::getSupportedOptions));
121 context.registerParametricMethod(this, "", (int)(InternalEncryption::mount)(std::string));
122 context.registerNonparametricMethod(this, "", (int)(InternalEncryption::umount));
123 context.registerParametricMethod(this, "", (int)(InternalEncryption::encrypt)(std::string, unsigned int));
124 context.registerParametricMethod(this, "", (int)(InternalEncryption::decrypt)(std::string));
125 context.registerParametricMethod(this, "", (int)(InternalEncryption::changePassword)(std::string, std::string));
128 InternalEncryption::~InternalEncryption()
132 int InternalEncryption::mount(const std::string& password)
134 if (getState() != State::Encrypted) {
138 KeyManager::data pwData(password.begin(), password.end());
139 KeyManager keyManager(engine.getKeyMeta());
141 if (!keyManager.verifyPassword(pwData)) {
145 engine.mount(keyManager.getMasterKey(pwData), getOptions());
149 int InternalEncryption::umount()
151 if (getState() != State::Encrypted) {
155 INFO("Close all processes using internal storage...");
156 stopDependedSystemdServices();
157 INFO("Umount internal storage...");
163 int InternalEncryption::encrypt(const std::string& password, unsigned int options)
165 if (getState() != State::Unencrypted) {
169 KeyManager::data pwData(password.begin(), password.end());
170 KeyManager keyManager;
172 keyManager.initPassword(pwData);
173 engine.setKeyMeta(keyManager.serialize());
175 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
176 auto encryptWorker = [&MasterKey, options, this]() {
177 showProgressUI("Encrypting");
179 INFO("Close all processes using internal storage...");
180 stopDependedSystemdServices();
181 INFO("Umount internal storage...");
182 while (::umount(INTERNAL_STORAGE_PATH) == -1) {
183 if (errno != EBUSY) {
188 INFO("Encryption started...");
189 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
190 engine.encrypt(MasterKey, options);
191 setOptions(options & getSupportedOptions());
192 INFO("Sync disk...");
194 INFO("Encryption completed");
196 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "encrypted");
197 ::reboot(RB_AUTOBOOT);
200 std::thread asyncWork(encryptWorker);
206 int InternalEncryption::decrypt(const std::string& password)
208 if (getState() != State::Encrypted) {
212 KeyManager::data pwData(password.begin(), password.end());
213 KeyManager keyManager(engine.getKeyMeta());
215 if (!keyManager.verifyPassword(pwData)) {
219 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
220 auto decryptWorker = [MasterKey, this]() {
221 showProgressUI("Decrypting");
223 INFO("Close all processes using internal storage...");
224 stopDependedSystemdServices();
225 INFO("Umount internal storage...");
228 } catch (runtime::Exception& e) {}
230 INFO("Decryption started...");
231 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
232 engine.decrypt(MasterKey, getOptions());
233 INFO("Sync disk...");
235 INFO("Decryption completed");
237 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "unencrypted");
238 ::reboot(RB_AUTOBOOT);
241 std::thread asyncWork(decryptWorker);
247 int InternalEncryption::verifyPassword(const std::string& password)
249 KeyManager::data data(password.begin(), password.end());
250 KeyManager keyManager(engine.getKeyMeta());
252 if (keyManager.verifyPassword(data)) {
258 int InternalEncryption::changePassword(const std::string& oldPassword,
259 const std::string& newPassword)
261 KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
262 KeyManager::data newPwData(newPassword.begin(), newPassword.end());
263 KeyManager keyManager(engine.getKeyMeta());
265 if (!keyManager.verifyPassword(oldPwData)) {
269 keyManager.changePassword(oldPwData, newPwData);
270 engine.setKeyMeta(keyManager.serialize());
275 int InternalEncryption::getState()
277 char *value = ::vconf_get_str(INTERNAL_STATE_VCONF_KEY);
279 throw runtime::Exception("Failed to get vconf value");
282 std::string valueStr(value);
285 if (valueStr == "encrypted") {
286 return State::Encrypted;
287 } else if (valueStr == "unencrypted") {
288 return State::Unencrypted;
290 return State::Corrupted;
296 unsigned int InternalEncryption::getSupportedOptions()
298 return engine.getSupportedOptions();