Change keystore to have the compatiblity with luks
[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 DMCryptEngine engine("/dev/mmcblk0p25", INTERNAL_STORAGE_PATH);
41
42 void stopDependedSystemdServices()
43 {
44         dbus::Connection& systemDBus = dbus::Connection::getSystem();
45         std::set<std::string> servicesToStop;
46
47         for (pid_t pid : runtime::FileUser::getList(INTERNAL_STORAGE_PATH, true)) {
48                 try {
49                         char *service;
50                         systemDBus.methodcall("org.freedesktop.systemd1",
51                                                                         "/org/freedesktop/systemd1",
52                                                                         "org.freedesktop.systemd1.Manager",
53                                                                         "GetUnitByPID",
54                                                                         -1, "(o)", "(u)", (unsigned int)pid)
55                                                                                 .get("(o)", &service);
56                         servicesToStop.insert(service);
57                 } catch (runtime::Exception &e) {
58                         INFO("Close process - " + std::to_string(pid));
59                         ::kill(pid, SIGKILL);
60                 }
61         }
62
63         for (const std::string& service : servicesToStop) {
64                 INFO("Close service - " + service);
65                 systemDBus.methodcall("org.freedesktop.systemd1",
66                                                                 service,
67                                                                 "org.freedesktop.systemd1.Unit",
68                                                                 "Stop",
69                                                                 -1, "", "(s)", "flush");
70         }
71 }
72
73 void showProgressUI(const std::string type) {
74         std::vector<std::string> args = {
75                 "ode-gui", "progress", type, "Internal storage encryption"
76         };
77
78         runtime::Process proc("/usr/bin/ode-gui", args);
79         proc.execute();
80 }
81
82 }
83
84 InternalEncryption::InternalEncryption(ODEControlContext& ctx) :
85         context(ctx)
86 {
87         context.registerParametricMethod(this, "", (int)(InternalEncryption::mount)(std::string));
88         context.registerNonparametricMethod(this, "", (int)(InternalEncryption::umount));
89         context.registerParametricMethod(this, "", (int)(InternalEncryption::encrypt)(std::string));
90         context.registerParametricMethod(this, "", (int)(InternalEncryption::decrypt)(std::string));
91         context.registerParametricMethod(this, "", (int)(InternalEncryption::changePassword)(std::string, std::string));
92         context.registerNonparametricMethod(this, "", (int)(InternalEncryption::getState));
93 }
94
95 InternalEncryption::~InternalEncryption()
96 {
97 }
98
99 int InternalEncryption::mount(const std::string& password)
100 {
101         KeyManager::data pwData(password.begin(), password.end());
102         KeyManager keyManager(engine.getKeyMeta());
103
104         if (!keyManager.verifyPassword(pwData)) {
105                 return -2;
106         }
107
108         engine.mount(keyManager.getMasterKey(pwData));
109         return 0;
110 }
111
112 int InternalEncryption::umount()
113 {
114         INFO("Close all processes using internal storage...");
115         stopDependedSystemdServices();
116         INFO("Umount internal storage...");
117         engine.umount();
118
119         return 0;
120 }
121
122 int InternalEncryption::encrypt(const std::string& password)
123 {
124         KeyManager::data pwData(password.begin(), password.end());
125         KeyManager keyManager;
126
127         keyManager.initPassword(pwData);
128         engine.setKeyMeta(keyManager.serialize());
129
130         KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
131         auto encryptWorker = [MasterKey, this]() {
132                 showProgressUI("Encrypting");
133
134                 INFO("Close all processes using internal storage...");
135                 stopDependedSystemdServices();
136                 INFO("Umount internal storage...");
137                 while (::umount(INTERNAL_STORAGE_PATH) == -1) {
138                         if (errno != EBUSY) {
139                                 break;
140                         }
141                 }
142
143                 INFO("Encryption started...");
144                 engine.encrypt(MasterKey);
145                 INFO("Sync disk...");
146                 sync();
147                 INFO("Encryption completed");
148                 ::reboot(RB_AUTOBOOT);
149         };
150
151         std::thread asyncWork(encryptWorker);
152         asyncWork.detach();
153
154         return 0;
155 }
156
157 int InternalEncryption::decrypt(const std::string& password)
158 {
159         KeyManager::data pwData(password.begin(), password.end());
160         KeyManager keyManager(engine.getKeyMeta());
161
162         if (!keyManager.verifyPassword(pwData)) {
163                 return -2;
164         }
165
166         KeyManager::data MasterKey = keyManager.getMasterKey(pwData);
167         auto decryptWorker = [MasterKey, this]() {
168                 showProgressUI("Decrypting");
169
170                 INFO("Close all processes using internal storage...");
171                 stopDependedSystemdServices();
172                 INFO("Umount internal storage...");
173                 try {
174                         engine.umount();
175                 } catch (runtime::Exception& e) {}
176
177                 INFO("Decryption started...");
178                 engine.decrypt(MasterKey);
179                 INFO("Sync disk...");
180                 sync();
181                 INFO("Decryption completed");
182                 ::reboot(RB_AUTOBOOT);
183         };
184
185         std::thread asyncWork(decryptWorker);
186         asyncWork.detach();
187
188         return 0;
189 }
190
191 int InternalEncryption::changePassword(const std::string& oldPassword,
192                                                                                 const std::string& newPassword)
193 {
194         KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
195         KeyManager::data newPwData(newPassword.begin(), newPassword.end());
196         KeyManager keyManager(engine.getKeyMeta());
197
198         if (!keyManager.verifyPassword(oldPwData)) {
199                 return -2;
200         }
201
202         keyManager.changePassword(oldPwData, newPwData);
203         engine.setKeyMeta(keyManager.serialize());
204
205         return 0;
206 }
207
208 int InternalEncryption::getState()
209 {
210         //TODO
211         return 0;
212 }
213
214 } // namespace ode