replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOcSecurity.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 54821ee..0a194dd
@@ -20,6 +20,7 @@
  ******************************************************************/
 #include "JniOcSecurity.h"
 #include "JniOcStack.h"
+#include "ocstack.h"
 
 /*
  * TODO: Persistant Storage Handling should be done by App.
@@ -29,6 +30,8 @@
 using namespace std;
 namespace PH = std::placeholders;
 namespace OC {
+    static string s_enc_dbpath, s_rescue_path;
+    static int AES_KEY_SZ;
 
     string& JniOcSecurity::store_path()
     {
@@ -36,11 +39,51 @@ namespace OC {
         return s_dbPath;
     }
 
+    string& JniOcSecurity::getEncDbPath()
+    {
+        return s_enc_dbpath;
+    }
+    string& JniOcSecurity::getRescueDbPath()
+    {
+        return s_rescue_path;
+    }
+
     void JniOcSecurity::StoreDbPath(const string &path)
     {
         store_path() = path;
     }
 
+    void JniOcSecurity::StoreDefault_DbPath(const string &path, const string &rescue_path, int key_size)
+    {
+        s_enc_dbpath = path;
+        s_rescue_path = rescue_path;
+        AES_KEY_SZ = key_size;
+    }
+
+    OCPersistentStorage* JniOcSecurity::getOCPersistentStorageEnc()
+    {
+        if (getEncDbPath().empty())
+        {
+            return nullptr;
+        }
+        static OCPersistentStorage s_psEnc { &JniOcSecurity::client_open_enc, fread,
+            fwrite, fclose, unlink, &JniOcSecurity::client_encrypt,
+            &JniOcSecurity::client_decrypt};
+        return &s_psEnc;
+    }
+
+    OCPersistentStorage* JniOcSecurity::getOCPersistentStorageRescue()
+    {
+        if (getRescueDbPath().empty())
+        {
+            return nullptr;
+        }
+        static OCPersistentStorage s_psRescue { &JniOcSecurity::client_open_rescue, fread,
+            fwrite, fclose, unlink, &JniOcSecurity::client_encrypt,
+            &JniOcSecurity::client_decrypt};
+        return &s_psRescue;
+    }
+
     OCPersistentStorage* JniOcSecurity::getOCPersistentStorage()
     {
         if (store_path().empty())
@@ -48,13 +91,173 @@ namespace OC {
             return nullptr;
         }
         static OCPersistentStorage s_ps { &JniOcSecurity::client_open, fread,
-            fwrite, fclose, unlink };
+            fwrite, fclose, &JniOcSecurity::client_unlink, NULL, NULL};
         return &s_ps;
     }
+        int JniOcSecurity::client_encrypt(const unsigned char *pt, size_t pt_len,
+            unsigned char **ct, size_t *ct_len)
+    {
+        if (OC_STACK_OK != OCEncrypt(pt, pt_len, ct, ct_len))
+            return 1;
+        return 0;
+    }
+
+    int JniOcSecurity::client_decrypt(const unsigned char *ct, size_t ct_len,
+            unsigned char **pt, size_t *pt_len)
+    {
+        if (OC_STACK_OK != OCDecrypt(ct, ct_len, pt, pt_len))
+            return 1;
+        return 0;
+    }
 
     FILE* JniOcSecurity::client_open(const char *path, const char *mode)
     {
         LOGI("Opening SVR Database file '%s' with mode '%s'\n", store_path().c_str(), mode);
         return fopen(store_path().c_str(), mode);
     }
+
+
+    FILE* JniOcSecurity::client_open_enc(const char *path, const char *mode)
+    {
+        LOGI("Opening SVR Database file '%s' with mode '%s'\n", getEncDbPath().c_str(), mode);
+        return fopen(getEncDbPath().c_str(), mode);
+    }
+
+    FILE* JniOcSecurity::client_open_rescue(const char *path, const char *mode)
+    {
+        LOGI("Opening SVR Database file '%s' with mode '%s'\n", getRescueDbPath().c_str(), mode);
+        return fopen(getRescueDbPath().c_str(), mode);
+    }
+
+    int JniOcSecurity::client_unlink(const char *path)
+    {
+        LOGI("unlink SVR Database file '%s' \n", store_path().c_str());
+        return unlink(store_path().c_str());
+    }
+
+    size_t JniOcSecurity::getDBSize(const char *filepath)
+    {
+        FILE *fp = fopen(filepath, "rb");
+        if (!fp) return 0;
+
+        size_t size = 0;
+        char buffer[1023];
+        if (fp)
+        {
+            size_t bytesRead = 0;
+            do
+            {
+                bytesRead = fread(buffer, 1, 1023, fp); //we here read encrypted data
+                size += bytesRead;
+            } while (bytesRead);
+        }
+        if (fp) fclose(fp);
+        return size;
+    }
+
+    OCStackResult JniOcSecurity::ResetDBFile(const char *cred_file)
+    {
+
+        size_t fileSize = getDBSize(s_rescue_path.c_str());
+        unsigned char *data  = (unsigned char *)calloc(1, sizeof(unsigned char) * fileSize);
+        unsigned char *ct = NULL;
+        size_t ct_len;
+
+        OCPersistentStorage *ps = JniOcSecurity::getOCPersistentStorage();
+
+        FILE *fp = fopen(s_rescue_path.c_str(), "rb");
+        FILE *filep = fopen(cred_file, "wb");
+        OCStackResult ret = OC_STACK_OK;
+
+        if (!fp || !filep)
+        {
+            LOGE("Failed to open %s\n", s_rescue_path.c_str());
+            ret = OC_STACK_ERROR;
+            goto exit;
+        }
+        if (fread(data, 1, fileSize, fp) != fileSize)
+        {
+            LOGE("Failed to read %s\n", s_rescue_path.c_str());
+            ret = OC_STACK_ERROR;
+            goto exit;
+        }
+        if (OC_STACK_OK != ps->encrypt(data, fileSize, &ct, &ct_len))
+        {
+            LOGE("Encryption Failed %d\n",__LINE__);
+        }
+        if (fwrite(ct, 1, ct_len, filep) != ct_len)
+        {
+            LOGE("Failed to write encrypted data to %s\n", cred_file);
+            ret = OC_STACK_ERROR;
+            goto exit;
+        }
+exit:
+        if (filep) fclose(filep);
+        if (fp) fclose(fp);
+        free(data);
+        return ret;
+    }
+#if 0
+    size_t JniOcSecurity::client_read(void *buffer, size_t size, size_t nmemb, FILE *fp)
+    {
+        size_t fileSize = size * nmemb;
+        size_t pt_len;
+        unsigned char *pt = NULL;
+
+        OCPersistentStorage *ps = JniOcSecurity::getOCPersistentStorage();
+        unsigned char *buf = (unsigned char*)calloc(1, sizeof(unsigned char) * fileSize);
+
+        LOGE("In JniOcSecurity::client_read ps = %p, fileSize = %d\n", ps, fileSize);
+
+        if (fileSize == fread(buf, 1, fileSize, fp))
+        {
+            if (OC_STACK_OK != ps->decrypt(buf, fileSize, &pt, &pt_len))
+            {
+                LOGE( "ps->decrypt() Failed !!");
+                fileSize = 0;
+                goto exit;
+            }
+            else
+            {
+                memcpy(buffer, pt, pt_len);
+                fileSize = pt_len;
+            }
+        }
+        else
+        {
+            LOGE("Failed fileSize == fread(buf, 1, fileSize, fp)");
+            fileSize = 0;
+        }
+exit:
+        free(buf);
+        return fileSize;
+    }
+
+    size_t JniOcSecurity::client_write(const void *buffer, size_t size,
+            size_t nmemb, FILE *fp)
+    {
+        size_t fileSize = size * nmemb;
+        unsigned char *ct = NULL;
+        size_t ct_len = 0;
+        OCPersistentStorage *ps = JniOcSecurity::getOCPersistentStorage();
+
+        if (0 != ps->encrypt((const unsigned char*)buffer, fileSize, &ct, &ct_len))
+        {
+            LOGE( "ps->encrypt() Failed !!");
+            fileSize = 0;
+            goto exit;
+        }
+
+        fileSize = ct_len;
+        if (fileSize != fwrite(ct, 1, fileSize,fp))
+        {
+            LOGE( "fwrite() Failed !!");
+            fileSize = 0;
+            goto exit;
+        }
+exit:
+        free(ct);
+        return fileSize;
+    }
+#endif
 }