Add get/set encryption state using vconf
[platform/core/security/ode.git] / server / internal-encryption.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 #include <set>
17
18 #include <signal.h>
19 #include <unistd.h>
20 #include <sys/mount.h>
21 #include <sys/reboot.h>
22
23 #include <vconf.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>
29
30 #include "engine/dmcrypt-engine.h"
31 #include "key-manager/key-manager.h"
32
33 #include "rmi/internal-encryption.h"
34
35 #define INTERNAL_STORAGE_PATH   "/opt/usr"
36 #define INTERNAL_STORAGE_VCONF_KEY VCONFKEY_ODE_CRYPTO_STATE
37
38 namespace ode {
39
40 namespace {
41
42 DMCryptEngine engine("/dev/mmcblk0p25", INTERNAL_STORAGE_PATH);
43
44 void stopDependedSystemdServices()
45 {
46         dbus::Connection& systemDBus = dbus::Connection::getSystem();
47         std::set<std::string> servicesToStop;
48
49         for (pid_t pid : runtime::FileUser::getList(INTERNAL_STORAGE_PATH, true)) {
50                 try {
51                         char *service;
52                         systemDBus.methodcall("org.freedesktop.systemd1",
53                                                                         "/org/freedesktop/systemd1",
54                                                                         "org.freedesktop.systemd1.Manager",
55                                                                         "GetUnitByPID",
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));
61                         ::kill(pid, SIGKILL);
62                 }
63         }
64
65         for (const std::string& service : servicesToStop) {
66                 INFO("Close service - " + service);
67                 systemDBus.methodcall("org.freedesktop.systemd1",
68                                                                 service,
69                                                                 "org.freedesktop.systemd1.Unit",
70                                                                 "Stop",
71                                                                 -1, "", "(s)", "flush");
72         }
73 }
74
75 void showProgressUI(const std::string type) {
76         std::vector<std::string> args = {
77                 "ode-gui", "progress", type, "Internal storage encryption"
78         };
79
80         runtime::Process proc("/usr/bin/ode-gui", args);
81         proc.execute();
82 }
83
84 }
85
86 InternalEncryption::InternalEncryption(ODEControlContext& ctx) :
87         context(ctx)
88 {
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));
95 }
96
97 InternalEncryption::~InternalEncryption()
98 {
99 }
100
101 int InternalEncryption::mount(const std::string& password)
102 {
103         if (getState() != State::Encrypted) {
104                 return -1;
105         }
106
107         KeyManager::data pwData(password.begin(), password.end());
108         KeyManager keyManager(engine.getKeyMeta());
109
110         if (!keyManager.verifyPassword(pwData)) {
111                 return -2;
112         }
113
114         engine.mount(keyManager.getMasterKey(pwData));
115         return 0;
116 }
117
118 int InternalEncryption::umount()
119 {
120         if (getState() != State::Encrypted) {
121                 return -1;
122         }
123
124         INFO("Close all processes using internal storage...");
125         stopDependedSystemdServices();
126         INFO("Umount internal storage...");
127         engine.umount();
128
129         return 0;
130 }
131
132 int InternalEncryption::encrypt(const std::string& password)
133 {
134         if (getState() != State::Unencrypted) {
135                 return -1;
136         }
137
138         KeyManager::data pwData(password.begin(), password.end());
139         KeyManager keyManager;
140
141         keyManager.initPassword(pwData);
142         engine.setKeyMeta(keyManager.serialize());
143
144         KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
145         auto encryptWorker = [MasterKey, this]() {
146                 showProgressUI("Encrypting");
147
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) {
153                                 break;
154                         }
155                 }
156
157                 INFO("Encryption started...");
158                 ::vconf_set_str(INTERNAL_STORAGE_VCONF_KEY, "error_partially_encrypted");
159                 engine.encrypt(MasterKey);
160                 INFO("Sync disk...");
161                 sync();
162                 INFO("Encryption completed");
163
164                 ::vconf_set_str(INTERNAL_STORAGE_VCONF_KEY, "encrypted");
165                 ::reboot(RB_AUTOBOOT);
166         };
167
168         std::thread asyncWork(encryptWorker);
169         asyncWork.detach();
170
171         return 0;
172 }
173
174 int InternalEncryption::decrypt(const std::string& password)
175 {
176         if (getState() != State::Encrypted) {
177                 return -1;
178         }
179
180         KeyManager::data pwData(password.begin(), password.end());
181         KeyManager keyManager(engine.getKeyMeta());
182
183         if (!keyManager.verifyPassword(pwData)) {
184                 return -2;
185         }
186
187         KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
188         auto decryptWorker = [MasterKey, this]() {
189                 showProgressUI("Decrypting");
190
191                 INFO("Close all processes using internal storage...");
192                 stopDependedSystemdServices();
193                 INFO("Umount internal storage...");
194                 try {
195                         engine.umount();
196                 } catch (runtime::Exception& e) {}
197
198                 INFO("Decryption started...");
199                 ::vconf_set_str(INTERNAL_STORAGE_VCONF_KEY, "error_partially_encrypted");
200                 engine.decrypt(MasterKey);
201                 INFO("Sync disk...");
202                 sync();
203                 INFO("Decryption completed");
204
205                 ::vconf_set_str(INTERNAL_STORAGE_VCONF_KEY, "unencrypted");
206                 ::reboot(RB_AUTOBOOT);
207         };
208
209         std::thread asyncWork(decryptWorker);
210         asyncWork.detach();
211
212         return 0;
213 }
214
215 int InternalEncryption::changePassword(const std::string& oldPassword,
216                                                                                 const std::string& newPassword)
217 {
218         KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
219         KeyManager::data newPwData(newPassword.begin(), newPassword.end());
220         KeyManager keyManager(engine.getKeyMeta());
221
222         if (!keyManager.verifyPassword(oldPwData)) {
223                 return -2;
224         }
225
226         keyManager.changePassword(oldPwData, newPwData);
227         engine.setKeyMeta(keyManager.serialize());
228
229         return 0;
230 }
231
232 int InternalEncryption::getState()
233 {
234         char *value = ::vconf_get_str(INTERNAL_STORAGE_VCONF_KEY);
235         if (value == NULL) {
236                 throw runtime::Exception("Failed to get vconf value");
237         }
238
239         std::string valueStr(value);
240         free(value);
241
242         if (valueStr == "encrypted") {
243                 return State::Encrypted;
244         } else if (valueStr == "unencrypted") {
245                 return State::Unencrypted;
246         } else {
247                 return State::Corrupted;
248         }
249
250         return 0;
251 }
252
253 } // namespace ode