Use systemd API to decode unit name
[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         INFO(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                 INFO(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                         INFO(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                 try {
212                         char *unit = nullptr;
213                         systemDBus.methodcall("org.freedesktop.systemd1",
214                                                                         "/org/freedesktop/systemd1",
215                                                                         "org.freedesktop.systemd1.Manager",
216                                                                         "GetUnitByPID",
217                                                                         -1, "(o)", "(u)", (unsigned int)pid)
218                                                                                 .get("(o)", &unit);
219
220                         char* tmp  = NULL;
221                         int err = sd_bus_path_decode(unit, "/org/freedesktop/systemd1/unit", &tmp);
222                         if (err <= 0 || tmp == NULL) {
223                                 ERROR(SINK, "Failed to decode unit name: " + std::string(unit) + " error: " +
224                                             std::to_string(err));
225                                 continue;
226                         }
227                         std::string unescapedName(tmp);
228                         free(tmp);
229
230                         if (unescapedName.find("dbus", 0, 4) == std::string::npos)
231                                 unitsToStop.insert(unescapedName);
232                 } catch (runtime::Exception &e) {
233                         ERROR(SINK, "Killing process: " + std::to_string(pid));
234                         ::kill(pid, SIGKILL);
235                 }
236         }
237
238         for (auto unit : unitsToStop) {
239                 stopUnit(unit);
240         }
241 }
242
243 void showProgressUI(const std::string type)
244 {
245         dbus::Connection& systemDBus = dbus::Connection::getSystem();
246         std::string unit("ode-progress-ui@"+type+".service");
247
248         JobWatch watch(systemDBus);
249         INFO(SINK, "Start unit: " + unit);
250
251         const char* job = NULL;
252         systemDBus.methodcall("org.freedesktop.systemd1",
253                                                         "/org/freedesktop/systemd1",
254                                                         "org.freedesktop.systemd1.Manager",
255                                                         "StartUnit",
256                                                         -1, "(o)", "(ss)", unit.c_str(), "replace").get("(o)", &job);
257
258         INFO(SINK, "Waiting for job: " + std::string(job));
259         if (!watch.waitForJob(job))
260                 ERROR(SINK, "Starting unit: " + unit + " failed");
261 }
262
263 unsigned int getOptions()
264 {
265         unsigned int result = 0;
266         int value;
267
268         value = 0;
269         ::vconf_get_bool(VCONFKEY_ODE_FAST_ENCRYPTION, &value);
270         if (value) {
271                 result |= InternalEncryption::Option::IncludeUnusedRegion;
272         }
273
274         return result;
275 }
276
277 void setOptions(unsigned int options)
278 {
279         bool value;
280
281         if (options & InternalEncryption::Option::IncludeUnusedRegion) {
282                 value = true;
283         } else {
284                 value = false;
285         }
286         ::vconf_set_bool(VCONFKEY_ODE_FAST_ENCRYPTION, value);
287 }
288
289 void execAndWait(const std::string &path, std::vector<std::string> &args)
290 {
291         runtime::Process proc(path, args);
292         int ret = proc.execute();
293         if (ret < 0)
294                 ERROR(SINK, path + " failed for " + args.back());
295
296         ret = proc.waitForFinished();
297         if (ret < 0 || !WIFEXITED(ret) || WEXITSTATUS(ret) != 0)
298                 ERROR(SINK, path + " failed for " + args.back());
299 }
300
301 bool isPartitionTerminated(const std::string &partition)
302 {
303         bool ret = true;
304         const std::string cmd("fuser -m " + partition + " | grep -o '[0-9]*'");
305         char *line = nullptr;
306         size_t len = 0;
307
308         FILE *fp = ::popen(cmd.c_str(), "r");
309         if (fp == nullptr) {
310                 ERROR(SINK, "Failed to get processes on partition");
311                 return false;
312         }
313
314         if (::getline(&line, &len, fp) != -1)
315                 ret = false;
316
317         ::free(line);
318         ::pclose(fp);
319
320         return ret;
321 }
322
323 void unmountInternalStorage(const std::string& source)
324 {
325         if (::umount2("/opt/usr", MNT_DETACH) == -1) {
326                 if (errno != EBUSY && errno != EINVAL) {
327                         throw runtime::Exception("umount() error : " + runtime::GetSystemErrorMessage());
328                 }
329         }
330
331         do {
332                 ::sync();
333                 static const char *fuserPath = "/usr/bin/fuser";
334                 std::vector<std::string> args = {
335                         fuserPath, "-m", "-k", "-s", "-SIGTERM", source,
336                 };
337                 execAndWait(fuserPath, args);
338                 ::usleep((useconds_t)((unsigned int)(500)*1000));
339
340                 args[4] = "-SIGKILL";
341                 execAndWait(fuserPath, args);
342                 ::usleep((useconds_t)((unsigned int)(200)*1000));
343         } while (!isPartitionTerminated(source));
344 }
345
346 }
347
348 InternalEncryptionServer::InternalEncryptionServer(ServerContext& srv,
349                                                                                                    KeyServer& key) :
350         server(srv),
351         keyServer(key)
352 {
353         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::setMountPassword)(std::string));
354         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::mount)(std::vector<unsigned char>, unsigned int));
355         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::umount)());
356         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::isMounted)());
357         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::encrypt)(std::string, unsigned int));
358         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::decrypt)(std::string));
359         server.expose(this, "", (int)(InternalEncryptionServer::isPasswordInitialized)());
360         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::recovery)());
361         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::initPassword)(std::string));
362         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::cleanPassword)(std::string));
363         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::changePassword)(std::string, std::string));
364         server.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryptionServer::verifyPassword)(std::string));
365         server.expose(this, "", (int)(InternalEncryptionServer::getState)());
366         server.expose(this, "", (unsigned int)(InternalEncryptionServer::getSupportedOptions)());
367         server.expose(this, "", (std::string)(InternalEncryptionServer::getDevicePath)());
368
369         server.createNotification("InternalEncryptionServer::mount");
370
371         std::string source = findDevPath();
372
373         if (getState() == State::Encrypted) {
374                 //"error_partially_encrypted"
375                 if (!FileFooter::exist(source) && !UpgradeSupport::checkUpgradeFlag()) {
376                         // Trigger key migration process
377                         UpgradeSupport::createUpgradeFlag();
378                 }
379         }
380
381         engine.reset(new INTERNAL_ENGINE(
382                 source, INTERNAL_PATH,
383                 ProgressBar([](int v) {
384                         ::vconf_set_str(VCONFKEY_ODE_ENCRYPT_PROGRESS,
385                                                         std::to_string(v).c_str());
386                 })
387         ));
388 }
389
390 InternalEncryptionServer::~InternalEncryptionServer()
391 {
392 }
393
394 int InternalEncryptionServer::migrateMasterKey(const std::string& dev, const std::string& password)
395 {
396         try {
397                 BinaryData masterKey = UpgradeSupport::loadMasterKey(dev);
398
399                 // encrypt the master key with given password
400                 return keyServer.changePassword2(dev, masterKey, password);
401         } catch (const runtime::Exception&) {
402                 ERROR(SINK, "Failed to load the master key stored during upgrade.");
403         }
404
405         return error::Unknown;
406 }
407
408 int InternalEncryptionServer::setMountPassword(const std::string& password)
409 {
410         const std::string& dev = engine->getSource();
411
412         // check if upgrade flag exists
413         if (UpgradeSupport::checkUpgradeFlag()) {
414                 INFO(SINK, "Upgrade flag detected.");
415
416                 int rc = migrateMasterKey(dev, password);
417                 if (rc != error::None)
418                         return rc;
419                 UpgradeSupport::removeUpgradeFlag();
420         }
421
422         return keyServer.get(dev, password, mountKey);
423 }
424
425 int InternalEncryptionServer::mount(const std::vector<unsigned char> &mk, unsigned int options)
426 {
427         if (mountKey.empty() && mk.empty()) {
428                 ERROR(SINK, "You need to set master key first.");
429                 return error::NoData;
430         }
431
432         BinaryData key = mk.empty() ? mountKey : mk;
433         mountKey.clear();
434
435         if (getState() != State::Encrypted) {
436                 ERROR(SINK, "Cannot mount, SD partition's state incorrect.");
437                 return error::NoSuchDevice;
438         }
439
440         if (engine->isMounted()) {
441                 ERROR(SINK, "Partition already mounted.");
442                 return error::None;
443         }
444
445         INFO(SINK, "Mounting internal storage.");
446         try {
447                 engine->mount(key, getOptions());
448
449                 server.notify("InternalEncryptionServer::mount");
450
451                 runtime::File("/tmp/.lazy_mount").create(O_WRONLY);
452                 runtime::File("/tmp/.unlock_mnt").create(O_WRONLY);
453         } catch (runtime::Exception &e) {
454                 ERROR(SINK, "Mount failed: " + std::string(e.what()));
455                 return error::Unknown;
456         }
457
458         return error::None;
459 }
460
461 int InternalEncryptionServer::isMounted()
462 {
463         int ret = 0;
464         try {
465                 ret = engine->isMounted() ? 1 : 0;
466         } catch (runtime::Exception &e) {
467                 ERROR(SINK, "Failed to access the mount flag");
468                 return error::Unknown;
469         }
470         return ret;
471 }
472
473 int InternalEncryptionServer::umount()
474 {
475         if (getState() != State::Encrypted) {
476                 ERROR(SINK, "Cannot umount, partition's state incorrect.");
477                 return error::NoSuchDevice;
478         }
479
480         if (!engine->isMounted()) {
481                 ERROR(SINK, "Partition already umounted.");
482                 return error::None;
483         }
484
485         INFO(SINK, "Closing all processes using internal storage.");
486         try {
487                 stopSystemdUnits();
488                 INFO(SINK, "Umounting internal storage.");
489                 unmountInternalStorage("/dev/mapper/userdata");
490                 engine->umount();
491         } catch (runtime::Exception &e) {
492                 ERROR(SINK, "Umount failed: " + std::string(e.what()));
493                 return error::Unknown;
494         }
495
496         return error::None;
497 }
498
499 int InternalEncryptionServer::encrypt(const std::string& password, unsigned int options)
500 {
501         if (getState() != State::Unencrypted) {
502                 ERROR(SINK, "Cannot encrypt, partition's state incorrect.");
503                 return error::NoSuchDevice;
504         }
505
506         BinaryData masterKey;
507         int ret = keyServer.get(engine->getSource(), password, masterKey);
508         if (ret != error::None)
509                 return ret;
510
511         auto encryptWorker = [masterKey, options, this]() {
512                 try {
513                         if (::device_power_request_lock(POWER_LOCK_DISPLAY, 0) != 0)
514                                 ERROR(SINK, "Failed to request to lock display");
515
516                         showProgressUI("encrypt");
517                         ::sleep(1);
518
519                         runtime::File file("/opt/etc/.odeprogress");
520                         file.create(0640);
521
522                         std::string source = engine->getSource();
523                         auto mntPaths = findMountPointsByDevice(source);
524
525                         if (!mntPaths.empty()) {
526                                 INFO(SINK, "Closing all processes using internal storage.");
527                                 stopSystemdUnits();
528
529                                 INFO(SINK, "Unmounting internal storage.");
530                                 unmountInternalStorage(source);
531                         }
532
533                         INFO(SINK, "Encryption started.");
534                         ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "error_partially_encrypted");
535                         try {
536                                 engine->encrypt(masterKey, options);
537                         } catch (runtime::Exception &e) {
538                                 ERROR(SINK, e.what());
539                                 if (!engine->isStarted()) {
540                                         ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "unencrypted");
541                                         file.remove();
542                                 }
543                                 ::sync();
544                                 ::reboot(RB_AUTOBOOT);
545                         }
546                         setOptions(options & getSupportedOptions());
547
548                         INFO(SINK, "Encryption completed.");
549                         ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "encrypted");
550                         server.notify("InternalEncryptionServer::mount");
551
552                         file.remove();
553
554                         INFO(SINK, "Syncing disk and rebooting.");
555                         ::sync();
556                         ::reboot(RB_AUTOBOOT);
557                 } catch (runtime::Exception &e) {
558                         ERROR(SINK, "Encryption failed: " + std::string(e.what()));
559                 }
560         };
561
562         std::thread asyncWork(encryptWorker);
563         asyncWork.detach();
564
565         return error::None;
566 }
567
568 int InternalEncryptionServer::decrypt(const std::string& password)
569 {
570         if (getState() != State::Encrypted) {
571                 ERROR(SINK, "Cannot decrypt, partition's state incorrect.");
572                 return error::NoSuchDevice;
573         }
574
575         // check if key migration is needed
576         if (UpgradeSupport::checkUpgradeFlag()) {
577                 INFO(SINK, "Upgrade flag detected.");
578                 const std::string& dev = engine->getSource();
579                 int rc = migrateMasterKey(dev, password);
580                 if (rc == error::None)
581                         UpgradeSupport::removeUpgradeFlag();
582         }
583
584         BinaryData masterKey;
585         int ret = keyServer.get(engine->getSource(), password, masterKey);
586         if (ret != error::None)
587                 return ret;
588
589         auto decryptWorker = [masterKey, this]() {
590                 try {
591                         if (::device_power_request_lock(POWER_LOCK_DISPLAY, 0) != 0)
592                                 ERROR(SINK, "Failed to request to lock display");
593
594                         showProgressUI("decrypt");
595                         ::sleep(1);
596
597                         runtime::File file("/opt/etc/.odeprogress");
598                         file.create(0640);
599
600                         if (engine->isMounted()) {
601                                 INFO(SINK, "Closing all processes using internal storage.");
602                                 stopSystemdUnits();
603
604                                 INFO(SINK, "Umounting internal storage.");
605                                 unmountInternalStorage("/dev/mapper/userdata");
606                                 engine->umount();
607                         }
608
609                         INFO(SINK, "Decryption started.");
610                         ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "error_partially_decrypted");
611                         try {
612                                 engine->decrypt(masterKey, getOptions());
613                         } catch (runtime::Exception &e) {
614                                 ERROR(SINK, e.what());
615                                 if (!engine->isStarted()) {
616                                         ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "encrypted");
617                                         file.remove();
618                                 }
619                                 ::sync();
620                                 ::reboot(RB_AUTOBOOT);
621                         }
622
623                         INFO(SINK, "Decryption complete.");
624                         ::vconf_set_str(VCONFKEY_ODE_CRYPTO_STATE, "unencrypted");
625
626                         file.remove();
627
628                         INFO(SINK, "Syncing disk and rebooting.");
629                         ::sync();
630                         ::reboot(RB_AUTOBOOT);
631                 } catch (runtime::Exception &e) {
632                         ERROR(SINK, "Decryption failed: " + std::string(e.what()));
633                 }
634         };
635
636         std::thread asyncWork(decryptWorker);
637         asyncWork.detach();
638
639         return error::None;
640 }
641
642 int InternalEncryptionServer::recovery()
643 {
644         int state = getState();
645
646         if (state == State::Unencrypted)
647                 return error::NoSuchDevice;
648
649         runtime::File file("/opt/.factoryreset");
650         file.create(0640);
651
652         ::sync();
653         try {
654                 dbus::Connection& systemDBus = dbus::Connection::getSystem();
655                 systemDBus.methodcall("org.tizen.system.deviced",
656                                                                 "/Org/Tizen/System/DeviceD/Power",
657                                                                 "org.tizen.system.deviced.power",
658                                                                 "reboot",
659                                                                 -1, "()", "(si)", "reboot", 0);
660         } catch (runtime::Exception &e) {
661                 ::reboot(RB_AUTOBOOT);
662         }
663         return error::None;
664 }
665
666 int InternalEncryptionServer::isPasswordInitialized()
667 {
668         return keyServer.isInitialized(engine->getSource());
669 }
670
671 int InternalEncryptionServer::initPassword(const std::string& password)
672 {
673         return keyServer.init(engine->getSource(), password, Key::DEFAULT_256BIT);
674 }
675
676 int InternalEncryptionServer::cleanPassword(const std::string& password)
677 {
678         return keyServer.remove(engine->getSource(), password);
679 }
680
681 int InternalEncryptionServer::changePassword(const std::string& oldPassword,
682                                                                                 const std::string& newPassword)
683 {
684         return keyServer.changePassword(engine->getSource(), oldPassword, newPassword);
685 }
686
687 int InternalEncryptionServer::verifyPassword(const std::string& password)
688 {
689         return keyServer.verifyPassword(engine->getSource(), password);
690 }
691
692 int InternalEncryptionServer::getState()
693 {
694         char *value = ::vconf_get_str(VCONFKEY_ODE_CRYPTO_STATE);
695         if (value == NULL) {
696                 throw runtime::Exception("Failed to get vconf value.");
697         }
698
699         std::string valueStr(value);
700         free(value);
701
702         if (valueStr == "encrypted")
703                 return State::Encrypted;
704         else if (valueStr == "unencrypted")
705                 return State::Unencrypted;
706         else if (valueStr == "error_partially_encrypted" || valueStr == "error_partially_decrypted")
707                 return State::Corrupted;
708
709         return State::NotSupported;
710 }
711
712 unsigned int InternalEncryptionServer::getSupportedOptions()
713 {
714         return engine->getSupportedOptions();
715 }
716
717 std::string InternalEncryptionServer::getDevicePath() const
718 {
719         return engine->getSource();
720 }
721
722 } // namespace ode