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