Add recovery APIs to use when there is something wrong with encryption
[platform/core/security/ode.git] / server / internal-encryption.cpp
index 45533e4..d015184 100644 (file)
 #include <klay/dbus/connection.h>
 #include <klay/audit/logger.h>
 
-#include "engine/dmcrypt-engine.h"
+#include "vconf.h"
+#include "progress-bar.h"
+#include "engine/encryption/dmcrypt-engine.h"
 #include "key-manager/key-manager.h"
 
 #include "rmi/internal-encryption.h"
 
-#define INTERNAL_STORAGE_PATH  "/opt/usr"
-#define INTERNAL_STATE_VCONF_KEY VCONFKEY_ODE_CRYPTO_STATE
-#define INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY VCONFKEY_ODE_FAST_ENCRYPTION
+#define INTERNAL_ENGINE        DMCryptEngine
+#define INTERNAL_DEV   "/dev/mmcblk0p25"
+#define INTERNAL_PATH  "/opt/usr"
+#define INTERNAL_STATE_VCONF_KEY                                       VCONFKEY_ODE_CRYPTO_STATE
+#define INTERNAL_OPTION_ONLY_USED_REGION_VCONF_KEY     VCONFKEY_ODE_FAST_ENCRYPTION
+
+#define PRIVILEGE_PLATFORM "http://tizen.org/privilege/internal/default/platform"
+
+const std::string PROG_FACTORY_RESET = "/usr/bin/dbus-send";
+const std::vector<std::string> wipeCommand = {
+    PROG_FACTORY_RESET,
+    "--system",
+    "--type=signal",
+    "--print-reply",
+    "--dest=com.samsung.factoryreset",
+    "/com/samsung/factoryreset",
+    "com.samsung.factoryreset.start.setting"
+};
 
 namespace ode {
 
 namespace {
 
-VConfBackend vconfBackend(VCONFKEY_ODE_ENCRYPT_PROGRESS);
-ProgressBar progressBar(std::bind(&VConfBackend::update, &vconfBackend, std::placeholders::_1));
-
-DMCryptEngine engine("/dev/mmcblk0p25", INTERNAL_STORAGE_PATH, progressBar);
+std::unique_ptr<INTERNAL_ENGINE> engine;
 
 void stopDependedSystemdServices()
 {
        dbus::Connection& systemDBus = dbus::Connection::getSystem();
        std::set<std::string> servicesToStop;
 
-       for (pid_t pid : runtime::FileUser::getList(INTERNAL_STORAGE_PATH, true)) {
+       for (pid_t pid : runtime::FileUser::getList(INTERNAL_PATH, true)) {
                try {
                        char *service;
                        systemDBus.methodcall("org.freedesktop.systemd1",
@@ -116,17 +130,25 @@ void setOptions(unsigned int options)
 InternalEncryption::InternalEncryption(ODEControlContext& ctx) :
        context(ctx)
 {
-       context.registerParametricMethod(this, "", (int)(InternalEncryption::mount)(std::string));
-       context.registerNonparametricMethod(this, "", (int)(InternalEncryption::umount));
-       context.registerParametricMethod(this, "", (int)(InternalEncryption::encrypt)(std::string, unsigned int));
-       context.registerParametricMethod(this, "", (int)(InternalEncryption::decrypt)(std::string));
-       context.registerNonparametricMethod(this, "", (int)(InternalEncryption::isPasswordInitialized));
-       context.registerParametricMethod(this, "", (int)(InternalEncryption::initPassword)(std::string));
-       context.registerParametricMethod(this, "", (int)(InternalEncryption::cleanPassword)(std::string));
-       context.registerParametricMethod(this, "", (int)(InternalEncryption::changePassword)(std::string, std::string));
-       context.registerParametricMethod(this, "", (int)(InternalEncryption::verifyPassword)(std::string));
-       context.registerNonparametricMethod(this, "", (int)(InternalEncryption::getState));
-       context.registerNonparametricMethod(this, "", (unsigned int)(InternalEncryption::getSupportedOptions));
+       context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::mount)(std::string));
+       context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::umount)());
+       context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::encrypt)(std::string, unsigned int));
+       context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::decrypt)(std::string));
+       context.expose(this, "", (int)(InternalEncryption::isPasswordInitialized)());
+       context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::initPassword)(std::string));
+       context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::cleanPassword)(std::string));
+       context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::changePassword)(std::string, std::string));
+       context.expose(this, PRIVILEGE_PLATFORM, (int)(InternalEncryption::verifyPassword)(std::string));
+       context.expose(this, "", (int)(InternalEncryption::getState)());
+       context.expose(this, "", (unsigned int)(InternalEncryption::getSupportedOptions)());
+
+       engine.reset(new INTERNAL_ENGINE(
+               INTERNAL_DEV, INTERNAL_PATH,
+               ProgressBar([](int v) {
+                       ::vconf_set_str(VCONFKEY_ODE_ENCRYPT_PROGRESS,
+                                                       std::to_string(v).c_str());
+               })
+       ));
 }
 
 InternalEncryption::~InternalEncryption()
@@ -140,13 +162,13 @@ int InternalEncryption::mount(const std::string& password)
        }
 
        KeyManager::data pwData(password.begin(), password.end());
-       KeyManager keyManager(engine.getKeyMeta());
+       KeyManager keyManager(engine->getKeyMeta());
 
        if (!keyManager.verifyPassword(pwData)) {
                return -2;
        }
 
-       engine.mount(keyManager.getMasterKey(pwData), getOptions());
+       engine->mount(keyManager.getMasterKey(pwData), getOptions());
        return 0;
 }
 
@@ -159,7 +181,7 @@ int InternalEncryption::umount()
        INFO("Close all processes using internal storage...");
        stopDependedSystemdServices();
        INFO("Umount internal storage...");
-       engine.umount();
+       engine->umount();
 
        return 0;
 }
@@ -171,7 +193,7 @@ int InternalEncryption::encrypt(const std::string& password, unsigned int option
        }
 
        KeyManager::data pwData(password.begin(), password.end());
-       KeyManager keyManager(engine.getKeyMeta());
+       KeyManager keyManager(engine->getKeyMeta());
 
        if (!keyManager.verifyPassword(pwData)) {
                return -2;
@@ -185,7 +207,7 @@ int InternalEncryption::encrypt(const std::string& password, unsigned int option
                        INFO("Close all processes using internal storage...");
                        stopDependedSystemdServices();
                        INFO("Umount internal storage...");
-                       while (::umount(INTERNAL_STORAGE_PATH) == -1) {
+                       while (::umount(INTERNAL_PATH) == -1) {
                                if (errno != EBUSY) {
                                        throw runtime::Exception("Umount error - " + std::to_string(errno));
                                }
@@ -194,7 +216,7 @@ int InternalEncryption::encrypt(const std::string& password, unsigned int option
 
                        INFO("Encryption started...");
                        ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
-                       engine.encrypt(MasterKey, options);
+                       engine->encrypt(MasterKey, options);
                        setOptions(options & getSupportedOptions());
                        INFO("Sync disk...");
                        sync();
@@ -220,7 +242,7 @@ int InternalEncryption::decrypt(const std::string& password)
        }
 
        KeyManager::data pwData(password.begin(), password.end());
-       KeyManager keyManager(engine.getKeyMeta());
+       KeyManager keyManager(engine->getKeyMeta());
 
        if (!keyManager.verifyPassword(pwData)) {
                return -2;
@@ -236,7 +258,7 @@ int InternalEncryption::decrypt(const std::string& password)
                        INFO("Umount internal storage...");
                        while (1) {
                                try {
-                                       engine.umount();
+                                       engine->umount();
                                        break;
                                } catch (runtime::Exception& e) {
                                        stopDependedSystemdServices();
@@ -245,7 +267,7 @@ int InternalEncryption::decrypt(const std::string& password)
 
                        INFO("Decryption started...");
                        ::vconf_set_str(INTERNAL_STATE_VCONF_KEY, "error_partially_encrypted");
-                       engine.decrypt(MasterKey, getOptions());
+                       engine->decrypt(MasterKey, getOptions());
                        INFO("Sync disk...");
                        sync();
                        INFO("Decryption completed");
@@ -263,9 +285,25 @@ int InternalEncryption::decrypt(const std::string& password)
        return 0;
 }
 
+int InternalEncryption::recovery()
+{
+       if (getState() != State::Unencrypted) {
+               return -1;
+       }
+
+       //TODO
+       runtime::Process proc(PROG_FACTORY_RESET, wipeCommand);
+       if (proc.execute() == -1) {
+               ERROR("Failed to launch factory-reset");
+               return -2;
+       }
+
+       return 0;
+}
+
 int InternalEncryption::isPasswordInitialized()
 {
-       if (engine.isKeyMetaSet()) {
+       if (engine->isKeyMetaSet()) {
                return 1;
        }
        return 0;
@@ -277,20 +315,20 @@ int InternalEncryption::initPassword(const std::string& password)
        KeyManager keyManager;
 
        keyManager.initPassword(pwData);
-       engine.setKeyMeta(keyManager.serialize());
+       engine->setKeyMeta(keyManager.serialize());
        return 0;
 }
 
 int InternalEncryption::cleanPassword(const std::string& password)
 {
        KeyManager::data pwData(password.begin(), password.end());
-       KeyManager keyManager(engine.getKeyMeta());
+       KeyManager keyManager(engine->getKeyMeta());
 
        if (!keyManager.verifyPassword(pwData)) {
                return -2;
        }
 
-       engine.clearKeyMeta();
+       engine->clearKeyMeta();
        return 0;
 }
 
@@ -299,14 +337,14 @@ int InternalEncryption::changePassword(const std::string& oldPassword,
 {
        KeyManager::data oldPwData(oldPassword.begin(), oldPassword.end());
        KeyManager::data newPwData(newPassword.begin(), newPassword.end());
-       KeyManager keyManager(engine.getKeyMeta());
+       KeyManager keyManager(engine->getKeyMeta());
 
        if (!keyManager.verifyPassword(oldPwData)) {
                return -2;
        }
 
        keyManager.changePassword(oldPwData, newPwData);
-       engine.setKeyMeta(keyManager.serialize());
+       engine->setKeyMeta(keyManager.serialize());
 
        return 0;
 }
@@ -314,7 +352,7 @@ int InternalEncryption::changePassword(const std::string& oldPassword,
 int InternalEncryption::verifyPassword(const std::string& password)
 {
        KeyManager::data pwData(password.begin(), password.end());
-       KeyManager keyManager(engine.getKeyMeta());
+       KeyManager keyManager(engine->getKeyMeta());
 
        if (keyManager.verifyPassword(pwData)) {
                return 1;
@@ -345,7 +383,7 @@ int InternalEncryption::getState()
 
 unsigned int InternalEncryption::getSupportedOptions()
 {
-       return engine.getSupportedOptions();
+       return engine->getSupportedOptions();
 }
 
 } // namespace ode