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_STORAGE_VCONF_KEY VCONFKEY_ODE_CRYPTO_STATE
42 DMCryptEngine engine("/dev/mmcblk0p25", INTERNAL_STORAGE_PATH);
44 void stopDependedSystemdServices()
46 dbus::Connection& systemDBus = dbus::Connection::getSystem();
47 std::set<std::string> servicesToStop;
49 for (pid_t pid : runtime::FileUser::getList(INTERNAL_STORAGE_PATH, true)) {
52 systemDBus.methodcall("org.freedesktop.systemd1",
53 "/org/freedesktop/systemd1",
54 "org.freedesktop.systemd1.Manager",
56 -1, "(o)", "(u)", (unsigned int)pid)
57 .get("(o)", &service);
58 servicesToStop.insert(service);
59 } catch (runtime::Exception &e) {
60 INFO("Close process - " + std::to_string(pid));
65 for (const std::string& service : servicesToStop) {
66 INFO("Close service - " + service);
67 systemDBus.methodcall("org.freedesktop.systemd1",
69 "org.freedesktop.systemd1.Unit",
71 -1, "", "(s)", "flush");
75 void showProgressUI(const std::string type) {
76 std::vector<std::string> args = {
77 "ode-gui", "progress", type, "Internal storage encryption"
80 runtime::Process proc("/usr/bin/ode-gui", args);
86 InternalEncryption::InternalEncryption(ODEControlContext& ctx) :
89 context.registerParametricMethod(this, "", (int)(InternalEncryption::mount)(std::string));
90 context.registerNonparametricMethod(this, "", (int)(InternalEncryption::umount));
91 context.registerParametricMethod(this, "", (int)(InternalEncryption::encrypt)(std::string));
92 context.registerParametricMethod(this, "", (int)(InternalEncryption::decrypt)(std::string));
93 context.registerParametricMethod(this, "", (int)(InternalEncryption::changePassword)(std::string, std::string));
94 context.registerNonparametricMethod(this, "", (int)(InternalEncryption::getState));
97 InternalEncryption::~InternalEncryption()
101 int InternalEncryption::mount(const std::string& password)
103 if (getState() != State::Encrypted) {
107 KeyManager::data pwData(password.begin(), password.end());
108 KeyManager keyManager(engine.getKeyMeta());
110 if (!keyManager.verifyPassword(pwData)) {
114 engine.mount(keyManager.getMasterKey(pwData));
118 int InternalEncryption::umount()
120 if (getState() != State::Encrypted) {
124 INFO("Close all processes using internal storage...");
125 stopDependedSystemdServices();
126 INFO("Umount internal storage...");
132 int InternalEncryption::encrypt(const std::string& password)
134 if (getState() != State::Unencrypted) {
138 KeyManager::data pwData(password.begin(), password.end());
139 KeyManager keyManager;
141 keyManager.initPassword(pwData);
142 engine.setKeyMeta(keyManager.serialize());
144 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
145 auto encryptWorker = [MasterKey, 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...");
158 ::vconf_set_str(INTERNAL_STORAGE_VCONF_KEY, "error_partially_encrypted");
159 engine.encrypt(MasterKey);
160 INFO("Sync disk...");
162 INFO("Encryption completed");
164 ::vconf_set_str(INTERNAL_STORAGE_VCONF_KEY, "encrypted");
165 ::reboot(RB_AUTOBOOT);
168 std::thread asyncWork(encryptWorker);
174 int InternalEncryption::decrypt(const std::string& password)
176 if (getState() != State::Encrypted) {
180 KeyManager::data pwData(password.begin(), password.end());
181 KeyManager keyManager(engine.getKeyMeta());
183 if (!keyManager.verifyPassword(pwData)) {
187 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
188 auto decryptWorker = [MasterKey, this]() {
189 showProgressUI("Decrypting");
191 INFO("Close all processes using internal storage...");
192 stopDependedSystemdServices();
193 INFO("Umount internal storage...");
196 } catch (runtime::Exception& e) {}
198 INFO("Decryption started...");
199 ::vconf_set_str(INTERNAL_STORAGE_VCONF_KEY, "error_partially_encrypted");
200 engine.decrypt(MasterKey);
201 INFO("Sync disk...");
203 INFO("Decryption completed");
205 ::vconf_set_str(INTERNAL_STORAGE_VCONF_KEY, "unencrypted");
206 ::reboot(RB_AUTOBOOT);
209 std::thread asyncWork(decryptWorker);
215 int InternalEncryption::changePassword(const std::string& oldPassword,
216 const std::string& newPassword)
218 KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
219 KeyManager::data newPwData(newPassword.begin(), newPassword.end());
220 KeyManager keyManager(engine.getKeyMeta());
222 if (!keyManager.verifyPassword(oldPwData)) {
226 keyManager.changePassword(oldPwData, newPwData);
227 engine.setKeyMeta(keyManager.serialize());
232 int InternalEncryption::getState()
234 char *value = ::vconf_get_str(INTERNAL_STORAGE_VCONF_KEY);
236 throw runtime::Exception("Failed to get vconf value");
239 std::string valueStr(value);
242 if (valueStr == "encrypted") {
243 return State::Encrypted;
244 } else if (valueStr == "unencrypted") {
245 return State::Unencrypted;
247 return State::Corrupted;