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.registerParametricMethod(this, "", (int)(InternalEncryption::mount)(std::string));
120 context.registerNonparametricMethod(this, "", (int)(InternalEncryption::umount));
121 context.registerParametricMethod(this, "", (int)(InternalEncryption::encrypt)(std::string, unsigned int));
122 context.registerParametricMethod(this, "", (int)(InternalEncryption::decrypt)(std::string));
123 context.registerNonparametricMethod(this, "", (int)(InternalEncryption::isPasswordInitialized));
124 context.registerParametricMethod(this, "", (int)(InternalEncryption::initPassword)(std::string));
125 context.registerParametricMethod(this, "", (int)(InternalEncryption::cleanPassword)(std::string));
126 context.registerParametricMethod(this, "", (int)(InternalEncryption::changePassword)(std::string, std::string));
127 context.registerParametricMethod(this, "", (int)(InternalEncryption::verifyPassword)(std::string));
128 context.registerNonparametricMethod(this, "", (int)(InternalEncryption::getState));
129 context.registerNonparametricMethod(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]() {
182 showProgressUI("Encrypting");
184 INFO("Close all processes using internal storage...");
185 stopDependedSystemdServices();
186 INFO("Umount internal storage...");
187 while (::umount(INTERNAL_STORAGE_PATH) == -1) {
188 if (errno != EBUSY) {
193 INFO("Encryption started...");
194 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
195 engine.encrypt(MasterKey, options);
196 setOptions(options & getSupportedOptions());
197 INFO("Sync disk...");
199 INFO("Encryption completed");
201 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "encrypted");
202 ::reboot(RB_AUTOBOOT);
205 std::thread asyncWork(encryptWorker);
211 int InternalEncryption::decrypt(const std::string& password)
213 if (getState() != State::Encrypted) {
217 KeyManager::data pwData(password.begin(), password.end());
218 KeyManager keyManager(engine.getKeyMeta());
220 if (!keyManager.verifyPassword(pwData)) {
224 KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
225 auto decryptWorker = [MasterKey, this]() {
226 showProgressUI("Decrypting");
228 INFO("Close all processes using internal storage...");
229 stopDependedSystemdServices();
230 INFO("Umount internal storage...");
233 } catch (runtime::Exception& e) {}
235 INFO("Decryption started...");
236 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
237 engine.decrypt(MasterKey, getOptions());
238 INFO("Sync disk...");
240 INFO("Decryption completed");
242 ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "unencrypted");
243 ::reboot(RB_AUTOBOOT);
246 std::thread asyncWork(decryptWorker);
252 int InternalEncryption::isPasswordInitialized()
254 if (engine.isKeyMetaSet()) {
260 int InternalEncryption::initPassword(const std::string& password)
262 KeyManager::data pwData(password.begin(), password.end());
263 KeyManager keyManager;
265 keyManager.initPassword(pwData);
266 engine.setKeyMeta(keyManager.serialize());
270 int InternalEncryption::cleanPassword(const std::string& password)
272 KeyManager::data pwData(password.begin(), password.end());
273 KeyManager keyManager(engine.getKeyMeta());
275 if (!keyManager.verifyPassword(pwData)) {
279 engine.clearKeyMeta();
283 int InternalEncryption::changePassword(const std::string& oldPassword,
284 const std::string& newPassword)
286 KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
287 KeyManager::data newPwData(newPassword.begin(), newPassword.end());
288 KeyManager keyManager(engine.getKeyMeta());
290 if (!keyManager.verifyPassword(oldPwData)) {
294 keyManager.changePassword(oldPwData, newPwData);
295 engine.setKeyMeta(keyManager.serialize());
300 int InternalEncryption::verifyPassword(const std::string& password)
302 KeyManager::data pwData(password.begin(), password.end());
303 KeyManager keyManager(engine.getKeyMeta());
305 if (keyManager.verifyPassword(pwData)) {
313 int InternalEncryption::getState()
315 char *value = ::vconf_get_str(INTERNAL_STATE_VCONF_KEY);
317 throw runtime::Exception("Failed to get vconf value");
320 std::string valueStr(value);
323 if (valueStr == "encrypted") {
324 return State::Encrypted;
325 } else if (valueStr == "unencrypted") {
326 return State::Unencrypted;
328 return State::Corrupted;
334 unsigned int InternalEncryption::getSupportedOptions()
336 return engine.getSupportedOptions();