Stop units with the same pid and pgid
[platform/core/security/ode.git] / server / internal-encryption.cpp
1 /*
2  *  Copyright (c) 2015-2019 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 #include <algorithm>
18 #include <memory>
19 #include <mutex>
20 #include <condition_variable>
21 #include <list>
22
23 #include <fstream>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <sys/mount.h>
28 #include <sys/reboot.h>
29 #include <limits.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <vconf.h>
34 #include <tzplatform_config.h>
35 #include <device/power.h>
36 #include <klay/process.h>
37 #include <klay/file-user.h>
38 #include <klay/filesystem.h>
39 #include <klay/dbus/connection.h>
40 #include <klay/error.h>
41 #include <systemd/sd-bus.h>
42
43 #include "misc.h"
44 #include "logger.h"
45 #include "progress-bar.h"
46 #include "rmi/common.h"
47
48 #include "ext4-tool.h"
49 #include "internal-encryption.h"
50 #include "internal-encryption-common.h"
51 #include "upgrade-support.h"
52 #include "file-footer.h"
53
54 namespace ode {
55
56 namespace {
57
58 const char *PRIVILEGE_PLATFORM  = "http://tizen.org/privilege/internal/default/platform";
59
60 // watches systemd jobs
61 class JobWatch {
62 public:
63         explicit JobWatch(dbus::Connection& systemDBus);
64         ~JobWatch();
65
66         bool waitForJob(const std::string& job);
67
68 private:
69         void jobRemoved(const dbus::Variant& parameters);
70
71         struct Job {
72                 Job(uint32_t id,
73                         const std::string& job,
74                         const std::string& unit,
75                         const std::string& result) : id(id), job(job), unit(unit), result(result) {}
76                 uint32_t id;
77                 std::string job;
78                 std::string unit;
79                 std::string result;
80         };
81
82         dbus::Connection::SubscriptionId id;
83         dbus::Connection& systemDBus;
84         std::list<Job> removedJobs;
85         std::mutex jobsMutex;
86         std::condition_variable jobsCv;
87 };
88
89 JobWatch::JobWatch(dbus::Connection& systemDBus) : systemDBus(systemDBus) {
90         auto callback = [this](dbus::Variant parameters) {
91                 this->jobRemoved(parameters);
92         };
93
94         id = systemDBus.subscribeSignal("",
95                                                                         "/org/freedesktop/systemd1",
96                                                                         "org.freedesktop.systemd1.Manager",
97                                                                         "JobRemoved",
98                                                                         callback);
99 }
100
101 JobWatch::~JobWatch() {
102         systemDBus.unsubscribeSignal(id);
103 }
104
105 bool JobWatch::waitForJob(const std::string& job) {
106         while(true) {
107                 std::unique_lock<std::mutex> lock(jobsMutex);
108                 bool timeout = true;
109                 auto sec = std::chrono::seconds(1);
110                 jobsCv.wait_for(lock, 5*sec, [this, &timeout]{
111                                 if (!removedJobs.empty()) {
112                                         timeout = false;
113                                         return true;
114                                 }
115                                 return false;});
116
117                 if (timeout) {
118                         ERROR(SINK, "job: " + job + ", result: time out");
119                         return false;
120                 }
121
122                 while(!removedJobs.empty()) {
123                         bool match = (removedJobs.front().job == job);
124                         bool done = (removedJobs.front().result == "done");
125                         removedJobs.pop_front();
126                         if (match)
127                                 return done;
128                 }
129         };
130 }
131
132 void JobWatch::jobRemoved(const dbus::Variant& parameters)
133 {
134         uint32_t id;
135         const char* job;
136         const char* unit;
137         const char* result;
138         parameters.get("(uoss)", &id, &job, &unit, &result);
139         WARN(SINK, "id:" + std::to_string(id) + " job:" + job + " unit:" + unit + " result:" + result);
140
141         {
142                 std::lock_guard<std::mutex> guard(jobsMutex);
143                 removedJobs.emplace_back(id, job, unit, result);
144         }
145         jobsCv.notify_one();
146 }
147
148 void stopSystemdUnits()
149 {
150         dbus::Connection& systemDBus = dbus::Connection::getSystem();
151         dbus::VariantIterator iter;
152         std::vector<std::string> preprocessUnits;
153         std::set<std::string> unitsToStop;
154
155         auto stopUnit = [&systemDBus](const std::string &unit) {
156                 JobWatch watch(systemDBus);
157                 WARN(SINK, "Stopping unit: " + unit);
158                 const char* job = NULL;
159                 try {
160                         systemDBus.methodcall("org.freedesktop.systemd1",
161                                                                         "/org/freedesktop/systemd1",
162                                                                         "org.freedesktop.systemd1.Manager",
163                                                                         "StopUnit",
164                                                                         -1, "(o)", "(ss)", unit.c_str(), "flush").get("(o)", &job);
165                         WARN(SINK, "Waiting for job: " + std::string(job));
166                         if (!watch.waitForJob(job))
167                                 ERROR(SINK, "Stopping unit: " + unit + " failed");
168                 } catch (runtime::Exception &e) {
169                         ERROR(SINK, std::string(e.what()));
170                 }
171         };
172
173         try {
174                 systemDBus.methodcall("org.freedesktop.systemd1",
175                                                                 "/org/freedesktop/systemd1",
176                                                                 "org.freedesktop.systemd1.Manager",
177                                                                 "ListUnits",
178                                                                 -1, "(a(ssssssouso))", "").get("(a(ssssssouso))", &iter);
179         } catch (runtime::Exception &e) {
180                 ERROR(SINK, "Get list of systemd unit : " + std::string(e.what()));
181         }
182
183         while (1) {
184                 unsigned int dataUnit;
185                 char *dataStr[9];
186                 int ret;
187                 ret = iter.get("(ssssssouso)", dataStr, dataStr + 1, dataStr + 2,
188                                                 dataStr + 3, dataStr + 4, dataStr + 5,
189                                                 dataStr + 6, &dataUnit, dataStr + 7, dataStr + 8);
190                 if (!ret)
191                         break;
192
193                 std::string unitName(dataStr[0]);
194                 if (unitName == "security-manager.socket" ||
195                                 unitName == "connman.socket" ||
196                                 unitName == "msg-server.socket") {
197                         preprocessUnits.insert(preprocessUnits.begin(), unitName);
198                 } else if (unitName.compare(0, 5, "user@") == 0 ||
199                                 unitName == "tlm.service" ||
200                                 unitName == "resourced.service" ||
201                                 unitName == "security-manager.service") {
202                         preprocessUnits.push_back(unitName);
203                 }
204         }
205
206         for (auto unit : preprocessUnits) {
207                 stopUnit(unit);
208         }
209
210         for (pid_t pid : runtime::FileUser::getList(INTERNAL_PATH, true)) {
211                 auto pgid = ::getpgid(pid);
212                 if (pgid > 0 && pid != pgid) {
213                         WARN(SINK, "PGID doesn't match - pgid : " + std::to_string(pgid) + ", pid : " + std::to_string(pid));
214                         continue;
215                 }
216
217                 try {
218                         char *unit = nullptr;
219                         systemDBus.methodcall("org.freedesktop.systemd1",
220                                                                         "/org/freedesktop/systemd1",
221                                                                         "org.freedesktop.systemd1.Manager",
222                                                                         "GetUnitByPID",
223                                                                         -1, "(o)", "(u)", (unsigned int)pid)
224                                                                                 .get("(o)", &unit);
225
226                         char* tmp  = NULL;
227                         int err = sd_bus_path_decode(unit, "/org/freedesktop/systemd1/unit", &tmp);
228                         if (err <= 0 || tmp == NULL) {
229                                 ERROR(SINK, "Failed to decode unit name: " + std::string(unit) + " error: " +
230                                             std::to_string(err));
231                                 continue;
232                         }
233                         std::string unescapedName(tmp);
234                         free(tmp);
235
236                         unitsToStop.insert(unescapedName);
237                 } catch (runtime::Exception &e) {
238                         ERROR(SINK, "Killing process: " + std::to_string(pid));
239                         ::kill(pid, SIGKILL);
240                 }
241         }
242
243         for (auto unit : unitsToStop) {
244                 stopUnit(unit);
245         }
246 }
247
248 void showProgressUI(const std::string type)
249 {
250         dbus::Connection& systemDBus = dbus::Connection::getSystem();
251         std::string unit("ode-progress-ui@"+type+".service");
252
253         JobWatch watch(systemDBus);
254         INFO(SINK, "Start unit: " + unit);
255
256         const char* job = NULL;
257         systemDBus.methodcall("org.freedesktop.systemd1",
258                                                         "/org/freedesktop/systemd1",
259                                                         "org.freedesktop.systemd1.Manager",
260                                                         "StartUnit",
261                                                         -1, "(o)", "(ss)", unit.c_str(), "replace").get("(o)", &job);
262
263         INFO(SINK, "Waiting for job: " + std::string(job));
264         if (!watch.waitForJob(job))
265                 ERROR(SINK, "Starting unit: " + unit + " failed");
266 }
267
268 unsigned int getOptions()
269 {
270         unsigned int result = 0;
271         int value;
272
273         value = 0;
274         ::vconf_get_bool(VCONFKEY_ODE_FAST_ENCRYPTION, &value);
275         if (value) {
276                 result |= InternalEncryption::Option::IncludeUnusedRegion;
277         }
278
279         return result;
280 }
281
282 void setOptions(unsigned int options)
283 {
284         bool value;
285
286         if (options & InternalEncryption::Option::IncludeUnusedRegion) {
287                 value = true;
288         } else {
289                 value = false;
290         }
291         ::vconf_set_bool(VCONFKEY_ODE_FAST_ENCRYPTION, value);
292 }
293
294 void execAndWait(const std::string &path, std::vector<std::string> &args)
295 {
296         runtime::Process proc(path, args);
297         int ret = proc.execute();
298         if (ret < 0)
299                 ERROR(SINK, "Executing " + path + " failed for " + args.back());
300
301         ret = proc.waitForFinished();
302         if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0)
303                 ERROR(SINK, "Waiting for " + path + " failed for " + args.back());
304 }
305
306 bool isPartitionTerminated(const std::string &partition)
307 {
308         bool ret = true;
309         const std::string cmd("fuser -m " + partition + " | grep -o '[0-9]*'");
310         char *line = nullptr;
311         size_t len = 0;
312
313         FILE *fp = ::popen(cmd.c_str(), "r");
314         if (fp == nullptr) {
315                 ERROR(SINK, "Failed to get processes on partition");
316                 return false;
317         }
318
319         if (::getline(&line, &len, fp) != -1)
320                 ret = false;
321
322         ::free(line);
323         ::pclose(fp);
324
325         return ret;
326 }
327
328 void unmountInternalStorage(const std::string& source)
329 {
330         if (::umount2("/opt/usr", MNT_DETACH) == -1) {
331                 if (errno != EBUSY && errno != EINVAL) {
332                         throw runtime::Exception("umount() error : " + runtime::GetSystemErrorMessage());
333                 }
334         }
335
336         do {
337                 ::sync();
338                 static const char *fuserPath = "/usr/bin/fuser";
339                 std::vector<std::string> args = {
340                         fuserPath, "-m", "-k", "-s", "-SIGTERM", source,
341                 };
342                 execAndWait(fuserPath, args);
343                 ::usleep((useconds_t)((unsigned int)(500)*1000));
344
345                 args[4] = "-SIGKILL";
346                 execAndWait(fuserPath, args);
347                 ::usleep((useconds_t)((unsigned int)(200)*1000));
348         } while (!isPartitionTerminated(source));
349 }
350
351 }
352
353 InternalEncryptionServer::InternalEncryptionServer(ServerContext& srv,
354                                                                                                    KeyServer& key) :
355         server(srv),
356         keyServer(key)
357 {
358         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::setMountPassword)(std::string));
359         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::mount)(std::vector<unsigned char>, unsigned int));
360         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::umount)());
361         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::isMounted)());
362         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::encrypt)(std::string, unsigned int));
363         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::decrypt)(std::string));
364         server.expose(this, "", (int)(InternalEncryptionServer::isPasswordInitialized)());
365         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::recovery)());
366         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::initPassword)(std::string));
367         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::cleanPassword)(std::string));
368         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::changePassword)(std::string, std::string));
369         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::verifyPassword)(std::string));
370         server.expose(this, "", (int)(InternalEncryptionServer::getState)());
371         server.expose(this, "", (unsigned int)(InternalEncryptionServer::getSupportedOptions)());
372         server.expose(this, "", (std::string)(InternalEncryptionServer::getDevicePath)());
373
374         server.createNotification("InternalEncryptionServer::mount");
375
376         std::string source = findDevPath();
377
378         if (getState() == State::Encrypted) {
379                 //"error_partially_encrypted"
380                 if (!FileFooter::exist(source) && !UpgradeSupport::checkUpgradeFlag()) {
381                         // Trigger key migration process
382                         UpgradeSupport::createUpgradeFlag();
383                 }
384         }
385
386         engine.reset(new INTERNAL_ENGINE(
387                 source, INTERNAL_PATH,
388                 ProgressBar([](int v) {
389                         ::vconf_set_str(VCONFKEY_ODE_ENCRYPT_PROGRESS,
390                                                         std::to_string(v).c_str());
391                 })
392         ));
393 }
394
395 InternalEncryptionServer::~InternalEncryptionServer()
396 {
397 }
398
399 int InternalEncryptionServer::migrateMasterKey(const std::string& dev, const std::string& password)
400 {
401         try {
402                 BinaryData masterKey = UpgradeSupport::loadMasterKey(dev);
403
404                 // encrypt the master key with given password
405                 return keyServer.changePassword2(dev, masterKey, password);
406         } catch (const runtime::Exception&) {
407                 ERROR(SINK, "Failed to load the master key stored during upgrade.");
408         }
409
410         return error::Unknown;
411 }
412
413 int InternalEncryptionServer::setMountPassword(const std::string& password)
414 {
415         const std::string& dev = engine->getSource();
416
417         // check if upgrade flag exists
418         if (UpgradeSupport::checkUpgradeFlag()) {
419                 INFO(SINK, "Upgrade flag detected.");
420
421                 int rc = migrateMasterKey(dev, password);
422                 if (rc != error::None)
423                         return rc;
424                 UpgradeSupport::removeUpgradeFlag();
425         }
426
427         return keyServer.get(dev, password, mountKey);
428 }
429
430 int InternalEncryptionServer::mount(const std::vector<unsigned char> &mk, unsigned int options)
431 {
432         if (mountKey.empty() && mk.empty()) {
433                 ERROR(SINK, "You need to set master key first.");
434                 return error::NoData;
435         }
436
437         BinaryData key = mk.empty() ? mountKey : mk;
438         mountKey.clear();
439
440         if (getState() != State::Encrypted) {
441                 ERROR(SINK, "Cannot mount, SD partition's state incorrect.");
442                 return error::NoSuchDevice;
443         }
444
445         if (engine->isMounted()) {
446                 ERROR(SINK, "Partition already mounted.");
447                 return error::None;
448         }
449
450         INFO(SINK, "Mounting internal storage.");
451         try {
452                 engine->mount(key, getOptions());
453
454                 server.notify("InternalEncryptionServer::mount");
455
456                 runtime::File("/tmp/.lazy_mount").create(O_WRONLY);
457                 runtime::File("/tmp/.unlock_mnt").create(O_WRONLY);
458         } catch (runtime::Exception &e) {
459                 ERROR(SINK, "Mount failed: " + std::string(e.what()));
460                 return error::Unknown;
461         }
462
463         return error::None;
464 }
465
466 int InternalEncryptionServer::isMounted()
467 {
468         int ret = 0;
469         try {
470                 ret = engine->isMounted() ? 1 : 0;
471         } catch (runtime::Exception &e) {
472                 ERROR(SINK, "Failed to access the mount flag");
473                 return error::Unknown;
474         }
475         return ret;
476 }
477
478 int InternalEncryptionServer::umount()
479 {
480         if (getState() != State::Encrypted) {
481                 ERROR(SINK, "Cannot umount, partition's state incorrect.");
482                 return error::NoSuchDevice;
483         }
484
485         if (!engine->isMounted()) {
486                 ERROR(SINK, "Partition already umounted.");
487                 return error::None;
488         }
489
490         INFO(SINK, "Closing all processes using internal storage.");
491         try {
492                 stopSystemdUnits();
493                 INFO(SINK, "Umounting internal storage.");
494                 unmountInternalStorage("/dev/mapper/userdata");
495                 engine->umount();
496         } catch (runtime::Exception &e) {
497                 ERROR(SINK, "Umount failed: " + std::string(e.what()));
498                 return error::Unknown;
499         }
500
501         return error::None;
502 }
503
504 int InternalEncryptionServer::encrypt(const std::string& password, unsigned int options)
505 {
506         if (getState() != State::Unencrypted) {
507                 ERROR(SINK, "Cannot encrypt, partition's state incorrect.");
508                 return error::NoSuchDevice;
509         }
510
511         BinaryData masterKey;
512         int ret = keyServer.get(engine->getSource(), password, masterKey);
513         if (ret != error::None)
514                 return ret;
515
516         auto encryptWorker = [masterKey, options, this]() {
517                 try {
518                         if (::device_power_request_lock(POWER_LOCK_DISPLAY, 0) != 0)
519                                 ERROR(SINK, "Failed to request to lock display");
520
521                         showProgressUI("encrypt");
522                         ::sleep(1);
523
524                         runtime::File file("/opt/etc/.odeprogress");
525                         file.create(0640);
526
527                         std::string source = engine->getSource();
528                         auto mntPaths = findMountPointsByDevice(source);
529
530                         if (!mntPaths.empty()) {
531                                 INFO(SINK, "Closing all processes using internal storage.");
532                                 stopSystemdUnits();
533
534                                 INFO(SINK, "Unmounting internal storage.");
535                                 unmountInternalStorage(source);
536                         }
537
538                         INFO(SINK, "Encryption started.");
539                         ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "error_partially_encrypted");
540                         try {
541                                 engine->encrypt(masterKey, options);
542                         } catch (runtime::Exception &e) {
543                                 ERROR(SINK, e.what());
544                                 if (!engine->isStarted()) {
545                                         ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "unencrypted");
546                                         file.remove();
547                                 }
548                                 ::sync();
549                                 ::reboot(RB_AUTOBOOT);
550                         }
551                         setOptions(options & getSupportedOptions());
552
553                         INFO(SINK, "Encryption completed.");
554                         ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "encrypted");
555                         server.notify("InternalEncryptionServer::mount");
556
557                         file.remove();
558
559                         INFO(SINK, "Syncing disk and rebooting.");
560                         ::sync();
561                         ::reboot(RB_AUTOBOOT);
562                 } catch (runtime::Exception &e) {
563                         ERROR(SINK, "Encryption failed: " + std::string(e.what()));
564                 }
565         };
566
567         std::thread asyncWork(encryptWorker);
568         asyncWork.detach();
569
570         return error::None;
571 }
572
573 int InternalEncryptionServer::decrypt(const std::string& password)
574 {
575         if (getState() != State::Encrypted) {
576                 ERROR(SINK, "Cannot decrypt, partition's state incorrect.");
577                 return error::NoSuchDevice;
578         }
579
580         // check if key migration is needed
581         if (UpgradeSupport::checkUpgradeFlag()) {
582                 INFO(SINK, "Upgrade flag detected.");
583                 const std::string& dev = engine->getSource();
584                 int rc = migrateMasterKey(dev, password);
585                 if (rc == error::None)
586                         UpgradeSupport::removeUpgradeFlag();
587         }
588
589         BinaryData masterKey;
590         int ret = keyServer.get(engine->getSource(), password, masterKey);
591         if (ret != error::None)
592                 return ret;
593
594         auto decryptWorker = [masterKey, this]() {
595                 try {
596                         if (::device_power_request_lock(POWER_LOCK_DISPLAY, 0) != 0)
597                                 ERROR(SINK, "Failed to request to lock display");
598
599                         showProgressUI("decrypt");
600                         ::sleep(1);
601
602                         runtime::File file("/opt/etc/.odeprogress");
603                         file.create(0640);
604
605                         if (engine->isMounted()) {
606                                 INFO(SINK, "Closing all processes using internal storage.");
607                                 stopSystemdUnits();
608
609                                 INFO(SINK, "Umounting internal storage.");
610                                 unmountInternalStorage("/dev/mapper/userdata");
611                                 engine->umount();
612                         }
613
614                         INFO(SINK, "Decryption started.");
615                         ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "error_partially_decrypted");
616                         try {
617                                 engine->decrypt(masterKey, getOptions());
618                         } catch (runtime::Exception &e) {
619                                 ERROR(SINK, e.what());
620                                 if (!engine->isStarted()) {
621                                         ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "encrypted");
622                                         file.remove();
623                                 }
624                                 ::sync();
625                                 ::reboot(RB_AUTOBOOT);
626                         }
627
628                         INFO(SINK, "Decryption complete.");
629                         ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "unencrypted");
630
631                         file.remove();
632
633                         INFO(SINK, "Syncing disk and rebooting.");
634                         ::sync();
635                         ::reboot(RB_AUTOBOOT);
636                 } catch (runtime::Exception &e) {
637                         ERROR(SINK, "Decryption failed: " + std::string(e.what()));
638                 }
639         };
640
641         std::thread asyncWork(decryptWorker);
642         asyncWork.detach();
643
644         return error::None;
645 }
646
647 int InternalEncryptionServer::recovery()
648 {
649         int state = getState();
650
651         if (state == State::Unencrypted)
652                 return error::NoSuchDevice;
653
654         runtime::File file("/opt/.factoryreset");
655         file.create(0640);
656
657         ::sync();
658         try {
659                 dbus::Connection& systemDBus = dbus::Connection::getSystem();
660                 systemDBus.methodcall("org.tizen.system.deviced",
661                                                                 "/Org/Tizen/System/DeviceD/Power",
662                                                                 "org.tizen.system.deviced.power",
663                                                                 "reboot",
664                                                                 -1, "()", "(si)", "reboot", 0);
665         } catch (runtime::Exception &e) {
666                 ::reboot(RB_AUTOBOOT);
667         }
668         return error::None;
669 }
670
671 int InternalEncryptionServer::isPasswordInitialized()
672 {
673         return keyServer.isInitialized(engine->getSource());
674 }
675
676 int InternalEncryptionServer::initPassword(const std::string& password)
677 {
678         return keyServer.init(engine->getSource(), password, Key::DEFAULT_256BIT);
679 }
680
681 int InternalEncryptionServer::cleanPassword(const std::string& password)
682 {
683         return keyServer.remove(engine->getSource(), password);
684 }
685
686 int InternalEncryptionServer::changePassword(const std::string& oldPassword,
687                                                                                 const std::string& newPassword)
688 {
689         return keyServer.changePassword(engine->getSource(), oldPassword, newPassword);
690 }
691
692 int InternalEncryptionServer::verifyPassword(const std::string& password)
693 {
694         return keyServer.verifyPassword(engine->getSource(), password);
695 }
696
697 int InternalEncryptionServer::getState()
698 {
699         char *value = ::vconf_get_str(VCONFKEY_ODE_CRYPTO_STATE);
700         if (value == NULL) {
701                 throw runtime::Exception("Failed to get vconf value.");
702         }
703
704         std::string valueStr(value);
705         free(value);
706
707         if (valueStr == "encrypted")
708                 return State::Encrypted;
709         else if (valueStr == "unencrypted")
710                 return State::Unencrypted;
711         else if (valueStr == "error_partially_encrypted" || valueStr == "error_partially_decrypted")
712                 return State::Corrupted;
713
714         return State::NotSupported;
715 }
716
717 unsigned int InternalEncryptionServer::getSupportedOptions()
718 {
719         return engine->getSupportedOptions();
720 }
721
722 std::string InternalEncryptionServer::getDevicePath() const
723 {
724         return engine->getSource();
725 }
726
727 } // namespace ode