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 <tzplatform_config.h>
25 #include <klay/process.h>
26 #include <klay/file-user.h>
27 #include <klay/filesystem.h>
28 #include <klay/dbus/connection.h>
29 #include <klay/audit/logger.h>
32 #include "progress-bar.h"
33 #include "engine/encryption/dmcrypt-engine.h"
34 #include "key-manager/key-manager.h"
36 #include "rmi/internal-encryption.h"
38 #define INTERNAL_ENGINE DMCryptEngine
39 #define INTERNAL_DEV "/dev/mmcblk0p25"
40 #define INTERNAL_PATH "/opt/usr"
41 #define INTERNAL_STATE_VCONF_KEY VCONFKEY_ODE_CRYPTO_STATE
42 #define INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY VCONFKEY_ODE_FAST_ENCRYPTION
44 #define PRIVILEGE_PLATFORM "http://tizen.org/privilege/internal/default/platform"
46 const std::string PROG_FACTORY_RESET = "/usr/bin/dbus-send";
47 const std::vector<std::string> wipeCommand = {
52 "--dest=com.samsung.factoryreset",
53 "/com/samsung/factoryreset",
54 "com.samsung.factoryreset.start.setting"
61 std::unique_ptr<INTERNAL_ENGINE> engine;
63 void stopDependedSystemdServices()
65 dbus::Connection& systemDBus = dbus::Connection::getSystem();
66 std::set<std::string> servicesToStop;
68 for (pid_t pid : runtime::FileUser::getList(INTERNAL_PATH, true)) {
71 systemDBus.methodcall("org.freedesktop.systemd1",
72 "/org/freedesktop/systemd1",
73 "org.freedesktop.systemd1.Manager",
75 -1, "(o)", "(u)", (unsigned int)pid)
76 .get("(o)", &service);
77 servicesToStop.insert(service);
78 } catch (runtime::Exception &e) {
79 INFO("Close process - " + std::to_string(pid));
84 for (const std::string& service : servicesToStop) {
85 INFO("Close service - " + service);
86 systemDBus.methodcall("org.freedesktop.systemd1",
88 "org.freedesktop.systemd1.Unit",
90 -1, "", "(s)", "flush");
94 void showProgressUI(const std::string type) {
95 ::tzplatform_set_user(::tzplatform_getuid(TZ_SYS_DEFAULT_USER));
96 std::string defaultUserHome(::tzplatform_getenv(TZ_USER_HOME));
97 ::tzplatform_reset_user();
99 ::tzplatform_set_user(::getuid());
100 std::string currentUserHome(::tzplatform_getenv(TZ_USER_HOME));
101 ::tzplatform_reset_user();
103 INFO("Home directory : " + currentUserHome);
105 runtime::File shareDirectory(currentUserHome + "/share");
106 if (!shareDirectory.exists()) {
107 shareDirectory.makeDirectory();
110 runtime::File elmConfigDir(currentUserHome + "/share/.elementary");
111 if (!elmConfigDir.exists()) {
112 runtime::File defaultElmConfigDir(defaultUserHome + "/share/.elementary");
113 defaultElmConfigDir.copyTo(shareDirectory.getPath());
116 std::vector<std::string> args = {
117 "ode", "progress", type, "Internal"
120 runtime::Process proc("/usr/bin/ode", args);
124 unsigned int getOptions()
126 unsigned int result = 0;
130 ::vconf_get_bool(INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY, &value);
132 result |= InternalEncryption::Option::IncludeUnusedRegion;
138 void setOptions(unsigned int options)
142 if (options & InternalEncryption::Option::IncludeUnusedRegion) {
147 ::vconf_set_bool(INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY, value);
152 InternalEncryption::InternalEncryption(ODEControlContext& ctx) :
155 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::mount)(std::string));
156 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::umount)());
157 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::encrypt)(std::string, unsigned int));
158 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::decrypt)(std::string));
159 context.expose(this, "", (int)(InternalEncryption::isPasswordInitialized)());
160 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::initPassword)(std::string));
161 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::cleanPassword)(std::string));
162 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::changePassword)(std::string, std::string));
163 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::verifyPassword)(std::string));
164 context.expose(this, "", (int)(InternalEncryption::getState)());
165 context.expose(this, "", (unsigned int)(InternalEncryption::getSupportedOptions)());
167 engine.reset(new INTERNAL_ENGINE(
168 INTERNAL_DEV, INTERNAL_PATH,
169 ProgressBar([](int v) {
170 ::vconf_set_str(VCONFKEY_ODE_ENCRYPT_PROGRESS,
171 std::to_string(v).c_str());
176 InternalEncryption::~InternalEncryption()
180 int InternalEncryption::mount(const std::string& password)
182 if (getState() != State::Encrypted) {
186 KeyManager::data pwData(password.begin(), password.end());
187 KeyManager keyManager(engine->getKeyMeta());
189 if (!keyManager.verifyPassword(pwData)) {
193 engine->mount(keyManager.getMasterKey(pwData), getOptions());
197 int InternalEncryption::umount()
199 if (getState() != State::Encrypted) {
203 INFO("Close all processes using internal storage...");
204 stopDependedSystemdServices();
205 INFO("Umount internal storage...");
211 int InternalEncryption::encrypt(const std::string& password, unsigned int options)
213 if (getState() != State::Unencrypted) {
217 KeyManager::data pwData(password.begin(), password.end());
218 KeyManager keyManager(engine->getKeyMeta());
220 if (!keyManager.verifyPassword(pwData)) {
224 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
225 auto encryptWorker = [MasterKey, options, this]() {
227 showProgressUI("Encrypting");
229 INFO("Close all processes using internal storage...");
230 stopDependedSystemdServices();
231 INFO("Umount internal storage...");
232 while (::umount(INTERNAL_PATH) == -1) {
233 if (errno != EBUSY) {
234 throw runtime::Exception("Umount error - " + std::to_string(errno));
236 stopDependedSystemdServices();
239 INFO("Encryption started...");
240 engine->encrypt(MasterKey, options);
241 setOptions(options & getSupportedOptions());
242 INFO("Sync disk...");
244 INFO("Encryption completed");
246 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "encrypted");
247 ::reboot(RB_AUTOBOOT);
248 } catch (runtime::Exception &e) {
249 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
250 ERROR("Encryption failed - " + std::string(e.what()));
254 std::thread asyncWork(encryptWorker);
260 int InternalEncryption::decrypt(const std::string& password)
262 if (getState() != State::Encrypted) {
266 KeyManager::data pwData(password.begin(), password.end());
267 KeyManager keyManager(engine->getKeyMeta());
269 if (!keyManager.verifyPassword(pwData)) {
273 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
274 auto decryptWorker = [MasterKey, this]() {
276 showProgressUI("Decrypting");
278 INFO("Close all processes using internal storage...");
279 stopDependedSystemdServices();
280 INFO("Umount internal storage...");
285 } catch (runtime::Exception& e) {
286 stopDependedSystemdServices();
290 INFO("Decryption started...");
291 engine->decrypt(MasterKey, getOptions());
292 INFO("Sync disk...");
294 INFO("Decryption completed");
296 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "unencrypted");
297 ::reboot(RB_AUTOBOOT);
298 } catch (runtime::Exception &e) {
299 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
300 ERROR("Decryption failed - " + std::string(e.what()));
304 std::thread asyncWork(decryptWorker);
310 int InternalEncryption::recovery()
312 if (getState() != State::Unencrypted) {
317 runtime::Process proc(PROG_FACTORY_RESET, wipeCommand);
318 if (proc.execute() == -1) {
319 ERROR("Failed to launch factory-reset");
326 int InternalEncryption::isPasswordInitialized()
328 if (engine->isKeyMetaSet()) {
334 int InternalEncryption::initPassword(const std::string& password)
336 KeyManager::data pwData(password.begin(), password.end());
337 KeyManager keyManager;
339 keyManager.initPassword(pwData);
340 engine->setKeyMeta(keyManager.serialize());
344 int InternalEncryption::cleanPassword(const std::string& password)
346 KeyManager::data pwData(password.begin(), password.end());
347 KeyManager keyManager(engine->getKeyMeta());
349 if (!keyManager.verifyPassword(pwData)) {
353 engine->clearKeyMeta();
357 int InternalEncryption::changePassword(const std::string& oldPassword,
358 const std::string& newPassword)
360 KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
361 KeyManager::data newPwData(newPassword.begin(), newPassword.end());
362 KeyManager keyManager(engine->getKeyMeta());
364 if (!keyManager.verifyPassword(oldPwData)) {
368 keyManager.changePassword(oldPwData, newPwData);
369 engine->setKeyMeta(keyManager.serialize());
374 int InternalEncryption::verifyPassword(const std::string& password)
376 KeyManager::data pwData(password.begin(), password.end());
377 KeyManager keyManager(engine->getKeyMeta());
379 if (keyManager.verifyPassword(pwData)) {
385 int InternalEncryption::getState()
387 char *value = ::vconf_get_str(INTERNAL_STATE_VCONF_KEY);
389 throw runtime::Exception("Failed to get vconf value");
392 std::string valueStr(value);
395 if (valueStr == "encrypted") {
396 return State::Encrypted;
397 } else if (valueStr == "unencrypted") {
398 return State::Unencrypted;
400 return State::Corrupted;
406 unsigned int InternalEncryption::getSupportedOptions()
408 return engine->getSupportedOptions();