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
22 #include <sys/mount.h>
23 #include <sys/reboot.h>
26 #include <tzplatform_config.h>
27 #include <klay/process.h>
28 #include <klay/file-user.h>
29 #include <klay/filesystem.h>
30 #include <klay/dbus/connection.h>
34 #include "progress-bar.h"
35 #include "engine/encryption/dmcrypt-engine.h"
36 #include "key-manager/key-manager.h"
38 #include "rmi/internal-encryption.h"
40 #define INTERNAL_ENGINE DMCryptEngine
41 #define INTERNAL_DEV_PATH "/dev/disk/by-partlabel"
42 #define INTERNAL_DEV_NAME "USER"
43 #define INTERNAL_PATH "/opt/usr"
44 #define INTERNAL_STATE_VCONF_KEY VCONFKEY_ODE_CRYPTO_STATE
45 #define INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY VCONFKEY_ODE_FAST_ENCRYPTION
47 #define PRIVILEGE_PLATFORM "http://tizen.org/privilege/internal/default/platform"
49 const std::string PROG_FACTORY_RESET = "/usr/bin/dbus-send";
50 const std::vector<std::string> wipeCommand = {
55 "--dest=com.samsung.factoryreset",
56 "/com/samsung/factoryreset",
57 "com.samsung.factoryreset.start.setting"
64 std::unique_ptr<INTERNAL_ENGINE> engine;
65 KeyManager::data mountKey;
67 void stopSystemdUserSessions() {
68 std::vector<std::string> userSessionServices;
69 dbus::Connection& systemDBus = dbus::Connection::getSystem();
70 dbus::VariantIterator iter;
72 systemDBus.methodcall("org.freedesktop.systemd1",
73 "/org/freedesktop/systemd1",
74 "org.freedesktop.systemd1.Manager",
76 -1, "(a(ssssssouso))", "")
77 .get("(a(ssssssouso))", &iter);
80 unsigned int dataUint;
84 ret = iter.get("(ssssssouso)", dataStr, dataStr + 1, dataStr + 2,
85 dataStr + 3, dataStr + 4, dataStr + 5,
86 dataStr + 6, &dataUint, dataStr + 7,
93 std::string service(dataStr[0]);
94 if (service.compare(0, 5, "user@") == 0) {
95 userSessionServices.push_back(service);
99 for (const std::string& service : userSessionServices) {
100 INFO(SINK, "Stop service - " + service);
101 systemDBus.methodcall("org.freedesktop.systemd1",
102 "/org/freedesktop/systemd1",
103 "org.freedesktop.systemd1.Manager",
105 -1, "", "(ss)", service.c_str(), "flush");
111 void stopDependedSystemdServices()
113 dbus::Connection& systemDBus = dbus::Connection::getSystem();
114 std::set<std::string> servicesToStop;
116 for (pid_t pid : runtime::FileUser::getList(INTERNAL_PATH, true)) {
119 systemDBus.methodcall("org.freedesktop.systemd1",
120 "/org/freedesktop/systemd1",
121 "org.freedesktop.systemd1.Manager",
123 -1, "(o)", "(u)", (unsigned int)pid)
124 .get("(o)", &service);
125 servicesToStop.insert(service);
126 } catch (runtime::Exception &e) {
127 INFO(SINK, "Close process - " + std::to_string(pid));
128 ::kill(pid, SIGKILL);
132 for (const std::string& service : servicesToStop) {
133 INFO(SINK, "Close service - " + service);
134 systemDBus.methodcall("org.freedesktop.systemd1",
136 "org.freedesktop.systemd1.Unit",
138 -1, "", "(s)", "flush");
142 void showProgressUI(const std::string type) {
143 ::tzplatform_set_user(::tzplatform_getuid(TZ_SYS_DEFAULT_USER));
144 std::string defaultUserHome(::tzplatform_getenv(TZ_USER_HOME));
145 ::tzplatform_reset_user();
148 runtime::File shareDirectory("/opt/home/root/share");
149 if (!shareDirectory.exists()) {
150 shareDirectory.makeDirectory(true);
153 runtime::File elmConfigDir(shareDirectory.getPath() + "/.elementary");
154 if (!elmConfigDir.exists()) {
155 runtime::File defaultElmConfigDir(defaultUserHome + "/share/.elementary");
156 defaultElmConfigDir.copyTo(shareDirectory.getPath());
158 } catch (runtime::Exception &e) {
159 ERROR(SINK, "Failed to set up elm configuration");
162 std::vector<std::string> args = {
163 "ode", "progress", type, "Internal"
166 runtime::Process proc("/usr/bin/ode", args);
170 unsigned int getOptions()
172 unsigned int result = 0;
176 ::vconf_get_bool(INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY, &value);
178 result |= InternalEncryption::Option::IncludeUnusedRegion;
184 void setOptions(unsigned int options)
188 if (options & InternalEncryption::Option::IncludeUnusedRegion) {
193 ::vconf_set_bool(INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY, value);
198 InternalEncryption::InternalEncryption(ODEControlContext& ctx) :
201 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::setMountPassword)(std::string));
202 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::mount)());
203 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::umount)());
204 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::encrypt)(std::string, unsigned int));
205 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::decrypt)(std::string));
206 context.expose(this, "", (int)(InternalEncryption::isPasswordInitialized)());
207 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::initPassword)(std::string));
208 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::cleanPassword)(std::string));
209 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::changePassword)(std::string, std::string));
210 context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::verifyPassword)(std::string));
211 context.expose(this, "", (int)(InternalEncryption::getState)());
212 context.expose(this, "", (unsigned int)(InternalEncryption::getSupportedOptions)());
214 context.createNotification("InternalEncryption::mount");
216 std::string source = INTERNAL_DEV_PATH "/" INTERNAL_DEV_NAME;
218 runtime::DirectoryIterator iter(INTERNAL_DEV_PATH), end;
220 while (iter != end) {
221 const std::string& path = (*iter).getPath();
222 std::string name = path.substr(path.rfind('/') + 1);
224 upper.reserve(name.size());
225 for (char c : name) {
226 upper += std::toupper(c);
228 if (upper == INTERNAL_DEV_NAME) {
234 source = runtime::File(source).readlink();
235 } catch (runtime::Exception &e) {}
237 engine.reset(new INTERNAL_ENGINE(
238 source, INTERNAL_PATH,
239 ProgressBar([](int v) {
240 ::vconf_set_str(VCONFKEY_ODE_ENCRYPT_PROGRESS,
241 std::to_string(v).c_str());
246 InternalEncryption::~InternalEncryption()
250 int InternalEncryption::setMountPassword(const std::string& password)
252 KeyManager::data pwData(password.begin(), password.end());
253 KeyManager keyManager(engine->getKeyMeta());
254 if (!keyManager.verifyPassword(pwData)) {
258 ode::mountKey = keyManager.getMasterKey(pwData);
263 int InternalEncryption::mount()
265 if (getState() != State::Encrypted) {
269 if (engine->isMounted()) {
270 INFO(SINK, "Already mounted");
274 engine->mount(mountKey, getOptions());
277 context.notify("InternalEncryption::mount");
279 runtime::File("/tmp/.lazy_mount").create(O_WRONLY);
280 runtime::File("/tmp/.unlock_mnt").create(O_WRONLY);
285 int InternalEncryption::umount()
287 if (getState() != State::Encrypted) {
291 if (!engine->isMounted()) {
292 INFO(SINK, "Already umounted");
296 INFO(SINK, "Close all processes using internal storage...");
297 stopDependedSystemdServices();
298 INFO(SINK, "Umount internal storage...");
304 int InternalEncryption::encrypt(const std::string& password, unsigned int options)
306 if (getState() != State::Unencrypted) {
310 KeyManager::data pwData(password.begin(), password.end());
311 KeyManager keyManager(engine->getKeyMeta());
313 if (!keyManager.verifyPassword(pwData)) {
317 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
318 auto encryptWorker = [MasterKey, options, this]() {
320 INFO(SINK, "Close all user sessions...");
321 stopSystemdUserSessions();
322 INFO(SINK, "Close all processes using internal storage...");
323 stopDependedSystemdServices();
324 INFO(SINK, "Umount internal storage...");
325 while (::umount(INTERNAL_PATH) == -1) {
326 if (errno != EBUSY) {
327 throw runtime::Exception("Umount error - " + std::to_string(errno));
329 stopDependedSystemdServices();
332 showProgressUI("Encrypting");
334 INFO(SINK, "Encryption started...");
335 engine->encrypt(MasterKey, options);
336 setOptions(options & getSupportedOptions());
337 INFO(SINK, "Sync disk...");
339 INFO(SINK, "Encryption completed");
341 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "encrypted");
342 context.notify("InternalEncryption::mount");
343 ::reboot(RB_AUTOBOOT);
344 } catch (runtime::Exception &e) {
345 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
346 ERROR(SINK, "Encryption failed - " + std::string(e.what()));
350 std::thread asyncWork(encryptWorker);
356 int InternalEncryption::decrypt(const std::string& password)
358 if (getState() != State::Encrypted) {
362 KeyManager::data pwData(password.begin(), password.end());
363 KeyManager keyManager(engine->getKeyMeta());
365 if (!keyManager.verifyPassword(pwData)) {
369 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
370 auto decryptWorker = [MasterKey, this]() {
372 INFO(SINK, "Close all user sessions...");
373 stopSystemdUserSessions();
374 INFO(SINK, "Close all processes using internal storage...");
375 stopDependedSystemdServices();
376 INFO(SINK, "Umount internal storage...");
381 } catch (runtime::Exception& e) {
382 stopDependedSystemdServices();
386 showProgressUI("Decrypting");
388 INFO(SINK, "Decryption started...");
389 engine->decrypt(MasterKey, getOptions());
390 INFO(SINK, "Sync disk...");
392 INFO(SINK, "Decryption completed");
394 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "unencrypted");
395 ::reboot(RB_AUTOBOOT);
396 } catch (runtime::Exception &e) {
397 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
398 ERROR(SINK, "Decryption failed - " + std::string(e.what()));
402 std::thread asyncWork(decryptWorker);
408 int InternalEncryption::recovery()
410 if (getState() != State::Unencrypted) {
415 runtime::Process proc(PROG_FACTORY_RESET, wipeCommand);
416 if (proc.execute() == -1) {
417 ERROR(SINK, "Failed to launch factory-reset");
424 int InternalEncryption::isPasswordInitialized()
426 if (engine->isKeyMetaSet()) {
432 int InternalEncryption::initPassword(const std::string& password)
434 KeyManager::data pwData(password.begin(), password.end());
435 KeyManager keyManager;
437 keyManager.initPassword(pwData);
438 engine->setKeyMeta(keyManager.serialize());
442 int InternalEncryption::cleanPassword(const std::string& password)
444 KeyManager::data pwData(password.begin(), password.end());
445 KeyManager keyManager(engine->getKeyMeta());
447 if (!keyManager.verifyPassword(pwData)) {
451 engine->clearKeyMeta();
455 int InternalEncryption::changePassword(const std::string& oldPassword,
456 const std::string& newPassword)
458 KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
459 KeyManager::data newPwData(newPassword.begin(), newPassword.end());
460 KeyManager keyManager(engine->getKeyMeta());
462 if (!keyManager.verifyPassword(oldPwData)) {
466 keyManager.changePassword(oldPwData, newPwData);
467 engine->setKeyMeta(keyManager.serialize());
472 int InternalEncryption::verifyPassword(const std::string& password)
474 KeyManager::data pwData(password.begin(), password.end());
475 KeyManager keyManager(engine->getKeyMeta());
477 if (keyManager.verifyPassword(pwData)) {
483 int InternalEncryption::getState()
485 char *value = ::vconf_get_str(INTERNAL_STATE_VCONF_KEY);
487 throw runtime::Exception("Failed to get vconf value");
490 std::string valueStr(value);
493 if (valueStr == "encrypted") {
494 return State::Encrypted;
495 } else if (valueStr == "unencrypted") {
496 return State::Unencrypted;
498 return State::Corrupted;
504 unsigned int InternalEncryption::getSupportedOptions()
506 return engine->getSupportedOptions();