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>
23 #include <klay/process.h>
24 #include <klay/file-user.h>
25 #include <klay/filesystem.h>
26 #include <klay/dbus/connection.h>
27 #include <klay/audit/logger.h>
29 #include "engine/dmcrypt-engine.h"
30 #include "key-manager/key-manager.h"
32 #include "rmi/internal-encryption.h"
34 #define INTERNAL_STORAGE_PATH "/opt/usr"
40 DMCryptEngine engine("/dev/mmcblk0p25", INTERNAL_STORAGE_PATH);
42 void stopDependedSystemdServices()
44 dbus::Connection& systemDBus = dbus::Connection::getSystem();
45 std::set<std::string> servicesToStop;
47 for (pid_t pid : runtime::FileUser::getList(INTERNAL_STORAGE_PATH, true)) {
50 systemDBus.methodcall("org.freedesktop.systemd1",
51 "/org/freedesktop/systemd1",
52 "org.freedesktop.systemd1.Manager",
54 -1, "(o)", "(u)", (unsigned int)pid)
55 .get("(o)", &service);
56 servicesToStop.insert(service);
57 } catch (runtime::Exception &e) {
58 INFO("Close process - " + std::to_string(pid));
63 for (const std::string& service : servicesToStop) {
64 INFO("Close service - " + service);
65 systemDBus.methodcall("org.freedesktop.systemd1",
67 "org.freedesktop.systemd1.Unit",
69 -1, "", "(s)", "flush");
73 void showProgressUI(const std::string type) {
74 std::vector<std::string> args = {
75 "ode-gui", "progress", type, "Internal storage encryption"
78 runtime::Process proc("/usr/bin/ode-gui", args);
84 InternalEncryption::InternalEncryption(ODEControlContext& ctx) :
87 context.registerParametricMethod(this, "", (int)(InternalEncryption::mount)(std::string));
88 context.registerNonparametricMethod(this, "", (int)(InternalEncryption::umount));
89 context.registerParametricMethod(this, "", (int)(InternalEncryption::encrypt)(std::string));
90 context.registerParametricMethod(this, "", (int)(InternalEncryption::decrypt)(std::string));
91 context.registerParametricMethod(this, "", (int)(InternalEncryption::changePassword)(std::string, std::string));
92 context.registerNonparametricMethod(this, "", (int)(InternalEncryption::getState));
95 InternalEncryption::~InternalEncryption()
99 int InternalEncryption::mount(const std::string& password)
101 KeyManager::data pwData(password.begin(), password.end());
102 KeyManager keyManager(engine.getKeyMeta());
104 if (!keyManager.verifyPassword(pwData)) {
108 engine.mount(keyManager.getMasterKey(pwData));
112 int InternalEncryption::umount()
114 INFO("Close all processes using internal storage...");
115 stopDependedSystemdServices();
116 INFO("Umount internal storage...");
122 int InternalEncryption::encrypt(const std::string& password)
124 KeyManager::data pwData(password.begin(), password.end());
125 KeyManager keyManager;
127 keyManager.initPassword(pwData);
128 engine.setKeyMeta(keyManager.serialize());
130 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
131 auto encryptWorker = [MasterKey, this]() {
132 showProgressUI("Encrypting");
134 INFO("Close all processes using internal storage...");
135 stopDependedSystemdServices();
136 INFO("Umount internal storage...");
137 while (::umount(INTERNAL_STORAGE_PATH) == -1) {
138 if (errno != EBUSY) {
143 INFO("Encryption started...");
144 engine.encrypt(MasterKey);
145 INFO("Sync disk...");
147 INFO("Encryption completed");
148 ::reboot(RB_AUTOBOOT);
151 std::thread asyncWork(encryptWorker);
157 int InternalEncryption::decrypt(const std::string& password)
159 KeyManager::data pwData(password.begin(), password.end());
160 KeyManager keyManager(engine.getKeyMeta());
162 if (!keyManager.verifyPassword(pwData)) {
166 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
167 auto decryptWorker = [MasterKey, this]() {
168 showProgressUI("Decrypting");
170 INFO("Close all processes using internal storage...");
171 stopDependedSystemdServices();
172 INFO("Umount internal storage...");
175 } catch (runtime::Exception& e) {}
177 INFO("Decryption started...");
178 engine.decrypt(MasterKey);
179 INFO("Sync disk...");
181 INFO("Decryption completed");
182 ::reboot(RB_AUTOBOOT);
185 std::thread asyncWork(decryptWorker);
191 int InternalEncryption::changePassword(const std::string& oldPassword,
192 const std::string& newPassword)
194 KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
195 KeyManager::data newPwData(newPassword.begin(), newPassword.end());
196 KeyManager keyManager(engine.getKeyMeta());
198 if (!keyManager.verifyPassword(oldPwData)) {
202 keyManager.changePassword(oldPwData, newPwData);
203 engine.setKeyMeta(keyManager.serialize());
208 int InternalEncryption::getState()