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 KeyManager keyManager(INTERNAL_STORAGE_PATH);
41 DMCryptEngine engine("/dev/mmcblk0p25", INTERNAL_STORAGE_PATH);
43 void stopDependedSystemdServices()
45 dbus::Connection& systemDBus = dbus::Connection::getSystem();
46 std::set<std::string> servicesToStop;
48 for (pid_t pid : runtime::FileUser::getList(INTERNAL_STORAGE_PATH, true)) {
51 systemDBus.methodcall("org.freedesktop.systemd1",
52 "/org/freedesktop/systemd1",
53 "org.freedesktop.systemd1.Manager",
55 -1, "(o)", "(u)", (unsigned int)pid)
56 .get("(o)", &service);
57 servicesToStop.insert(service);
58 } catch (runtime::Exception &e) {
59 INFO("Close process - " + std::to_string(pid));
64 for (const std::string& service : servicesToStop) {
65 INFO("Close service - " + service);
66 systemDBus.methodcall("org.freedesktop.systemd1",
68 "org.freedesktop.systemd1.Unit",
70 -1, "", "(s)", "flush");
74 void showProgressUI(const std::string type) {
75 std::vector<std::string> args = {
76 "ode-gui", "progress", type, "Internal storage encryption"
79 runtime::Process proc("/usr/bin/ode-gui", args);
85 InternalEncryption::InternalEncryption(ODEControlContext& ctx) :
88 context.registerParametricMethod(this, "", (int)(InternalEncryption::mount)(std::string));
89 context.registerNonparametricMethod(this, "", (int)(InternalEncryption::umount));
90 context.registerParametricMethod(this, "", (int)(InternalEncryption::encrypt)(std::string));
91 context.registerParametricMethod(this, "", (int)(InternalEncryption::decrypt)(std::string));
92 context.registerParametricMethod(this, "", (int)(InternalEncryption::changePassword)(std::string, std::string));
93 context.registerNonparametricMethod(this, "", (int)(InternalEncryption::getState));
96 InternalEncryption::~InternalEncryption()
100 int InternalEncryption::mount(const std::string& password)
102 bool isVerified = false;
103 KeyManager::data pwData(password.begin(), password.end());
106 isVerified = keyManager.verifyPassword(pwData);
107 } catch (runtime::Exception& e) {}
113 engine.mount(keyManager.getDEK(pwData));
117 int InternalEncryption::umount()
119 INFO("Close all processes using internal storage...");
120 stopDependedSystemdServices();
121 INFO("Umount internal storage...");
127 int InternalEncryption::encrypt(const std::string& password)
129 KeyManager::data pwData(password.begin(), password.end());
131 if (keyManager.isInitialized()) {
132 bool isVerified = false;
134 isVerified = keyManager.verifyPassword(pwData);
135 } catch (runtime::Exception& e) {}
141 keyManager.initPassword(pwData);
144 KeyManager::data DEK = keyManager.getDEK(pwData);
145 auto encryptWorker = [DEK, this]() {
146 showProgressUI("Encrypting");
148 INFO("Close all processes using internal storage...");
149 stopDependedSystemdServices();
150 INFO("Umount internal storage...");
151 while (::umount(INTERNAL_STORAGE_PATH) == -1) {
152 if (errno != EBUSY) {
157 INFO("Encryption started...");
159 INFO("Sync disk...");
161 INFO("Encryption completed");
162 ::reboot(RB_AUTOBOOT);
165 std::thread asyncWork(encryptWorker);
171 int InternalEncryption::decrypt(const std::string& password)
173 bool isVerified = false;
174 KeyManager::data pwData(password.begin(), password.end());
177 isVerified = keyManager.verifyPassword(pwData);
178 } catch (runtime::Exception& e) {}
184 KeyManager::data DEK = keyManager.getDEK(pwData);
185 auto decryptWorker = [DEK, this]() {
186 showProgressUI("Decrypting");
188 INFO("Close all processes using internal storage...");
189 stopDependedSystemdServices();
190 INFO("Umount internal storage...");
193 } catch (runtime::Exception& e) {}
195 INFO("Decryption started...");
197 INFO("Sync disk...");
199 INFO("Decryption completed");
200 ::reboot(RB_AUTOBOOT);
203 std::thread asyncWork(decryptWorker);
206 keyManager.clearPassword();
211 int InternalEncryption::changePassword(const std::string& oldPassword,
212 const std::string& newPassword)
214 KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
215 KeyManager::data newPwData(newPassword.begin(), oldPassword.end());
217 bool isVerified = false;
219 isVerified = keyManager.verifyPassword(newPwData);
220 } catch (runtime::Exception& e) {}
226 keyManager.changePassword(oldPwData, newPwData);
230 int InternalEncryption::getState()