Add UI Application for show progress of encrypt/decrypt
[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 <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>
28
29 #include "engine/dmcrypt-engine.h"
30 #include "key-manager/key-manager.h"
31
32 #include "rmi/internal-encryption.h"
33
34 #define INTERNAL_STORAGE_PATH   "/opt/usr"
35
36 namespace ode {
37
38 namespace {
39
40 KeyManager keyManager(INTERNAL_STORAGE_PATH);
41 DMCryptEngine engine("/dev/mmcblk0p25", INTERNAL_STORAGE_PATH);
42
43 void stopDependedSystemdServices()
44 {
45         dbus::Connection& systemDBus = dbus::Connection::getSystem();
46         std::set<std::string> servicesToStop;
47
48         for (pid_t pid : runtime::FileUser::getList(INTERNAL_STORAGE_PATH, true)) {
49                 try {
50                         char *service;
51                         systemDBus.methodcall("org.freedesktop.systemd1",
52                                                                         "/org/freedesktop/systemd1",
53                                                                         "org.freedesktop.systemd1.Manager",
54                                                                         "GetUnitByPID",
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));
60                         ::kill(pid, SIGKILL);
61                 }
62         }
63
64         for (const std::string& service : servicesToStop) {
65                 INFO("Close service - " + service);
66                 systemDBus.methodcall("org.freedesktop.systemd1",
67                                                                 service,
68                                                                 "org.freedesktop.systemd1.Unit",
69                                                                 "Stop",
70                                                                 -1, "", "(s)", "flush");
71         }
72 }
73
74 void showProgressUI(const std::string type) {
75         std::vector<std::string> args = {
76                 "ode-gui", "progress", type, "Internal storage encryption"
77         };
78
79         runtime::Process proc("/usr/bin/ode-gui", args);
80         proc.execute();
81 }
82
83 }
84
85 InternalEncryption::InternalEncryption(ODEControlContext& ctx) :
86         context(ctx)
87 {
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));
94 }
95
96 InternalEncryption::~InternalEncryption()
97 {
98 }
99
100 int InternalEncryption::mount(const std::string& password)
101 {
102         bool isVerified = false;
103         KeyManager::data pwData(password.begin(), password.end());
104
105         try {
106                 isVerified = keyManager.verifyPassword(pwData);
107         } catch (runtime::Exception& e) {}
108
109         if (!isVerified) {
110                 return -1;
111         }
112
113         engine.mount(keyManager.getDEK(pwData));
114         return 0;
115 }
116
117 int InternalEncryption::umount()
118 {
119         INFO("Close all processes using internal storage...");
120         stopDependedSystemdServices();
121         INFO("Umount internal storage...");
122         engine.umount();
123
124         return 0;
125 }
126
127 int InternalEncryption::encrypt(const std::string& password)
128 {
129         KeyManager::data pwData(password.begin(), password.end());
130
131         if (keyManager.isInitialized()) {
132                 bool isVerified = false;
133                 try {
134                         isVerified = keyManager.verifyPassword(pwData);
135                 } catch (runtime::Exception& e) {}
136
137                 if (!isVerified) {
138                         return -2;
139                 }
140         } else {
141                 keyManager.initPassword(pwData);
142         }
143
144         KeyManager::data DEK = keyManager.getDEK(pwData);
145         auto encryptWorker = [DEK, 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                 engine.encrypt(DEK);
159                 INFO("Sync disk...");
160                 sync();
161                 INFO("Encryption completed");
162                 ::reboot(RB_AUTOBOOT);
163         };
164
165         std::thread asyncWork(encryptWorker);
166         asyncWork.detach();
167
168         return 0;
169 }
170
171 int InternalEncryption::decrypt(const std::string& password)
172 {
173         bool isVerified = false;
174         KeyManager::data pwData(password.begin(), password.end());
175
176         try {
177                 isVerified = keyManager.verifyPassword(pwData);
178         } catch (runtime::Exception& e) {}
179
180         if (!isVerified) {
181                 return -1;
182         }
183
184         KeyManager::data DEK = keyManager.getDEK(pwData);
185         auto decryptWorker = [DEK, this]() {
186                 showProgressUI("Decrypting");
187
188                 INFO("Close all processes using internal storage...");
189                 stopDependedSystemdServices();
190                 INFO("Umount internal storage...");
191                 try {
192                         engine.umount();
193                 } catch (runtime::Exception& e) {}
194
195                 INFO("Decryption started...");
196                 engine.decrypt(DEK);
197                 INFO("Sync disk...");
198                 sync();
199                 INFO("Decryption completed");
200                 ::reboot(RB_AUTOBOOT);
201         };
202
203         std::thread asyncWork(decryptWorker);
204         asyncWork.detach();
205
206         keyManager.clearPassword();
207
208         return 0;
209 }
210
211 int InternalEncryption::changePassword(const std::string& oldPassword,
212                                                                                 const std::string& newPassword)
213 {
214         KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
215         KeyManager::data newPwData(newPassword.begin(), oldPassword.end());
216
217         bool isVerified = false;
218         try     {
219                 isVerified = keyManager.verifyPassword(newPwData);
220         } catch (runtime::Exception& e) {}
221
222         if (!isVerified) {
223                 return -1;
224         }
225
226         keyManager.changePassword(oldPwData, newPwData);
227         return 0;
228 }
229
230 int InternalEncryption::getState()
231 {
232         //TODO
233         return 0;
234 }
235
236 } // namespace ode