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_STATE_VCONF_KEY VCONFKEY_ODE_CRYPTO_STATE
37 #define INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY VCONFKEY_ODE_FAST_ENCRYPTION
43 VConfBackend vconfBackend(VCONFKEY_ODE_ENCRYPT_PROGRESS);
44 ProgressBar progressBar(std::bind(&VConfBackend::update, &vconfBackend, std::placeholders::_1));
46 DMCryptEngine engine("/dev/mmcblk0p25", INTERNAL_STORAGE_PATH, progressBar);
48 void stopDependedSystemdServices()
50 dbus::Connection& systemDBus = dbus::Connection::getSystem();
51 std::set<std::string> servicesToStop;
53 for (pid_t pid : runtime::FileUser::getList(INTERNAL_STORAGE_PATH, true)) {
56 systemDBus.methodcall("org.freedesktop.systemd1",
57 "/org/freedesktop/systemd1",
58 "org.freedesktop.systemd1.Manager",
60 -1, "(o)", "(u)", (unsigned int)pid)
61 .get("(o)", &service);
62 servicesToStop.insert(service);
63 } catch (runtime::Exception &e) {
64 INFO("Close process - " + std::to_string(pid));
69 for (const std::string& service : servicesToStop) {
70 INFO("Close service - " + service);
71 systemDBus.methodcall("org.freedesktop.systemd1",
73 "org.freedesktop.systemd1.Unit",
75 -1, "", "(s)", "flush");
79 void showProgressUI(const std::string type) {
80 std::vector<std::string> args = {
81 "ode", "progress", type, "Internal"
84 runtime::Process proc("/usr/bin/ode", args);
88 unsigned int getOptions()
90 unsigned int result = 0;
94 ::vconf_get_bool(INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY, &value);
96 result |= InternalEncryption::Option::IncludeUnusedRegion;
102 void setOptions(unsigned int options)
106 if (options & InternalEncryption::Option::IncludeUnusedRegion) {
111 ::vconf_set_bool(INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY, value);
116 InternalEncryption::InternalEncryption(ODEControlContext& ctx) :
119 context.expose(this, "", (int)(InternalEncryption::mount)(std::string));
120 context.expose(this, "", (int)(InternalEncryption::umount)());
121 context.expose(this, "", (int)(InternalEncryption::encrypt)(std::string, unsigned int));
122 context.expose(this, "", (int)(InternalEncryption::decrypt)(std::string));
123 context.expose(this, "", (int)(InternalEncryption::isPasswordInitialized)());
124 context.expose(this, "", (int)(InternalEncryption::initPassword)(std::string));
125 context.expose(this, "", (int)(InternalEncryption::cleanPassword)(std::string));
126 context.expose(this, "", (int)(InternalEncryption::changePassword)(std::string, std::string));
127 context.expose(this, "", (int)(InternalEncryption::verifyPassword)(std::string));
128 context.expose(this, "", (int)(InternalEncryption::getState)());
129 context.expose(this, "", (unsigned int)(InternalEncryption::getSupportedOptions)());
132 InternalEncryption::~InternalEncryption()
136 int InternalEncryption::mount(const std::string& password)
138 if (getState() != State::Encrypted) {
142 KeyManager::data pwData(password.begin(), password.end());
143 KeyManager keyManager(engine.getKeyMeta());
145 if (!keyManager.verifyPassword(pwData)) {
149 engine.mount(keyManager.getMasterKey(pwData), getOptions());
153 int InternalEncryption::umount()
155 if (getState() != State::Encrypted) {
159 INFO("Close all processes using internal storage...");
160 stopDependedSystemdServices();
161 INFO("Umount internal storage...");
167 int InternalEncryption::encrypt(const std::string& password, unsigned int options)
169 if (getState() != State::Unencrypted) {
173 KeyManager::data pwData(password.begin(), password.end());
174 KeyManager keyManager(engine.getKeyMeta());
176 if (!keyManager.verifyPassword(pwData)) {
180 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
181 auto encryptWorker = [MasterKey, options, this]() {
183 showProgressUI("Encrypting");
185 INFO("Close all processes using internal storage...");
186 stopDependedSystemdServices();
187 INFO("Umount internal storage...");
188 while (::umount(INTERNAL_STORAGE_PATH) == -1) {
189 if (errno != EBUSY) {
190 throw runtime::Exception("Umount error - " + std::to_string(errno));
192 stopDependedSystemdServices();
195 INFO("Encryption started...");
196 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
197 engine.encrypt(MasterKey, options);
198 setOptions(options & getSupportedOptions());
199 INFO("Sync disk...");
201 INFO("Encryption completed");
203 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "encrypted");
204 } catch (runtime::Exception &e) {
205 ERROR("Encryption failed - " + std::string(e.what()));
207 ::reboot(RB_AUTOBOOT);
210 std::thread asyncWork(encryptWorker);
216 int InternalEncryption::decrypt(const std::string& password)
218 if (getState() != State::Encrypted) {
222 KeyManager::data pwData(password.begin(), password.end());
223 KeyManager keyManager(engine.getKeyMeta());
225 if (!keyManager.verifyPassword(pwData)) {
229 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
230 auto decryptWorker = [MasterKey, this]() {
232 showProgressUI("Decrypting");
234 INFO("Close all processes using internal storage...");
235 stopDependedSystemdServices();
236 INFO("Umount internal storage...");
241 } catch (runtime::Exception& e) {
242 stopDependedSystemdServices();
246 INFO("Decryption started...");
247 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
248 engine.decrypt(MasterKey, getOptions());
249 INFO("Sync disk...");
251 INFO("Decryption completed");
253 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "unencrypted");
254 } catch (runtime::Exception &e) {
255 ERROR("Decryption failed - " + std::string(e.what()));
257 ::reboot(RB_AUTOBOOT);
260 std::thread asyncWork(decryptWorker);
266 int InternalEncryption::isPasswordInitialized()
268 if (engine.isKeyMetaSet()) {
274 int InternalEncryption::initPassword(const std::string& password)
276 KeyManager::data pwData(password.begin(), password.end());
277 KeyManager keyManager;
279 keyManager.initPassword(pwData);
280 engine.setKeyMeta(keyManager.serialize());
284 int InternalEncryption::cleanPassword(const std::string& password)
286 KeyManager::data pwData(password.begin(), password.end());
287 KeyManager keyManager(engine.getKeyMeta());
289 if (!keyManager.verifyPassword(pwData)) {
293 engine.clearKeyMeta();
297 int InternalEncryption::changePassword(const std::string& oldPassword,
298 const std::string& newPassword)
300 KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
301 KeyManager::data newPwData(newPassword.begin(), newPassword.end());
302 KeyManager keyManager(engine.getKeyMeta());
304 if (!keyManager.verifyPassword(oldPwData)) {
308 keyManager.changePassword(oldPwData, newPwData);
309 engine.setKeyMeta(keyManager.serialize());
314 int InternalEncryption::verifyPassword(const std::string& password)
316 KeyManager::data pwData(password.begin(), password.end());
317 KeyManager keyManager(engine.getKeyMeta());
319 if (keyManager.verifyPassword(pwData)) {
325 int InternalEncryption::getState()
327 char *value = ::vconf_get_str(INTERNAL_STATE_VCONF_KEY);
329 throw runtime::Exception("Failed to get vconf value");
332 std::string valueStr(value);
335 if (valueStr == "encrypted") {
336 return State::Encrypted;
337 } else if (valueStr == "unencrypted") {
338 return State::Unencrypted;
340 return State::Corrupted;
346 unsigned int InternalEncryption::getSupportedOptions()
348 return engine.getSupportedOptions();