replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOcSecurity.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  ******************************************************************/
21 #include "JniOcSecurity.h"
22 #include "JniOcStack.h"
23 #include "ocstack.h"
24
25 /*
26  * TODO: Persistant Storage Handling should be done by App.
27  * For 0.9.2 , Handling is done at JNI. As of now Plaform Config only
28  * SVR Database fileName(fullpath) is passed.
29  */
30 using namespace std;
31 namespace PH = std::placeholders;
32 namespace OC {
33     static string s_enc_dbpath, s_rescue_path;
34     static int AES_KEY_SZ;
35
36     string& JniOcSecurity::store_path()
37     {
38         static string s_dbPath;
39         return s_dbPath;
40     }
41
42     string& JniOcSecurity::getEncDbPath()
43     {
44         return s_enc_dbpath;
45     }
46     string& JniOcSecurity::getRescueDbPath()
47     {
48         return s_rescue_path;
49     }
50
51     void JniOcSecurity::StoreDbPath(const string &path)
52     {
53         store_path() = path;
54     }
55
56     void JniOcSecurity::StoreDefault_DbPath(const string &path, const string &rescue_path, int key_size)
57     {
58         s_enc_dbpath = path;
59         s_rescue_path = rescue_path;
60         AES_KEY_SZ = key_size;
61     }
62
63     OCPersistentStorage* JniOcSecurity::getOCPersistentStorageEnc()
64     {
65         if (getEncDbPath().empty())
66         {
67             return nullptr;
68         }
69         static OCPersistentStorage s_psEnc { &JniOcSecurity::client_open_enc, fread,
70             fwrite, fclose, unlink, &JniOcSecurity::client_encrypt,
71             &JniOcSecurity::client_decrypt};
72         return &s_psEnc;
73     }
74
75     OCPersistentStorage* JniOcSecurity::getOCPersistentStorageRescue()
76     {
77         if (getRescueDbPath().empty())
78         {
79             return nullptr;
80         }
81         static OCPersistentStorage s_psRescue { &JniOcSecurity::client_open_rescue, fread,
82             fwrite, fclose, unlink, &JniOcSecurity::client_encrypt,
83             &JniOcSecurity::client_decrypt};
84         return &s_psRescue;
85     }
86
87     OCPersistentStorage* JniOcSecurity::getOCPersistentStorage()
88     {
89         if (store_path().empty())
90         {
91             return nullptr;
92         }
93         static OCPersistentStorage s_ps { &JniOcSecurity::client_open, fread,
94             fwrite, fclose, &JniOcSecurity::client_unlink, NULL, NULL};
95         return &s_ps;
96     }
97         int JniOcSecurity::client_encrypt(const unsigned char *pt, size_t pt_len,
98             unsigned char **ct, size_t *ct_len)
99     {
100         if (OC_STACK_OK != OCEncrypt(pt, pt_len, ct, ct_len))
101             return 1;
102         return 0;
103     }
104
105     int JniOcSecurity::client_decrypt(const unsigned char *ct, size_t ct_len,
106             unsigned char **pt, size_t *pt_len)
107     {
108         if (OC_STACK_OK != OCDecrypt(ct, ct_len, pt, pt_len))
109             return 1;
110         return 0;
111     }
112
113     FILE* JniOcSecurity::client_open(const char *path, const char *mode)
114     {
115         LOGI("Opening SVR Database file '%s' with mode '%s'\n", store_path().c_str(), mode);
116         return fopen(store_path().c_str(), mode);
117     }
118
119
120     FILE* JniOcSecurity::client_open_enc(const char *path, const char *mode)
121     {
122         LOGI("Opening SVR Database file '%s' with mode '%s'\n", getEncDbPath().c_str(), mode);
123         return fopen(getEncDbPath().c_str(), mode);
124     }
125
126     FILE* JniOcSecurity::client_open_rescue(const char *path, const char *mode)
127     {
128         LOGI("Opening SVR Database file '%s' with mode '%s'\n", getRescueDbPath().c_str(), mode);
129         return fopen(getRescueDbPath().c_str(), mode);
130     }
131
132     int JniOcSecurity::client_unlink(const char *path)
133     {
134         LOGI("unlink SVR Database file '%s' \n", store_path().c_str());
135         return unlink(store_path().c_str());
136     }
137
138     size_t JniOcSecurity::getDBSize(const char *filepath)
139     {
140         FILE *fp = fopen(filepath, "rb");
141         if (!fp) return 0;
142
143         size_t size = 0;
144         char buffer[1023];
145         if (fp)
146         {
147             size_t bytesRead = 0;
148             do
149             {
150                 bytesRead = fread(buffer, 1, 1023, fp); //we here read encrypted data
151                 size += bytesRead;
152             } while (bytesRead);
153         }
154         if (fp) fclose(fp);
155         return size;
156     }
157
158     OCStackResult JniOcSecurity::ResetDBFile(const char *cred_file)
159     {
160
161         size_t fileSize = getDBSize(s_rescue_path.c_str());
162         unsigned char *data  = (unsigned char *)calloc(1, sizeof(unsigned char) * fileSize);
163         unsigned char *ct = NULL;
164         size_t ct_len;
165
166         OCPersistentStorage *ps = JniOcSecurity::getOCPersistentStorage();
167
168         FILE *fp = fopen(s_rescue_path.c_str(), "rb");
169         FILE *filep = fopen(cred_file, "wb");
170         OCStackResult ret = OC_STACK_OK;
171
172         if (!fp || !filep)
173         {
174             LOGE("Failed to open %s\n", s_rescue_path.c_str());
175             ret = OC_STACK_ERROR;
176             goto exit;
177         }
178         if (fread(data, 1, fileSize, fp) != fileSize)
179         {
180             LOGE("Failed to read %s\n", s_rescue_path.c_str());
181             ret = OC_STACK_ERROR;
182             goto exit;
183         }
184         if (OC_STACK_OK != ps->encrypt(data, fileSize, &ct, &ct_len))
185         {
186             LOGE("Encryption Failed %d\n",__LINE__);
187         }
188         if (fwrite(ct, 1, ct_len, filep) != ct_len)
189         {
190             LOGE("Failed to write encrypted data to %s\n", cred_file);
191             ret = OC_STACK_ERROR;
192             goto exit;
193         }
194 exit:
195         if (filep) fclose(filep);
196         if (fp) fclose(fp);
197         free(data);
198         return ret;
199     }
200 #if 0
201     size_t JniOcSecurity::client_read(void *buffer, size_t size, size_t nmemb, FILE *fp)
202     {
203         size_t fileSize = size * nmemb;
204         size_t pt_len;
205         unsigned char *pt = NULL;
206
207         OCPersistentStorage *ps = JniOcSecurity::getOCPersistentStorage();
208         unsigned char *buf = (unsigned char*)calloc(1, sizeof(unsigned char) * fileSize);
209
210         LOGE("In JniOcSecurity::client_read ps = %p, fileSize = %d\n", ps, fileSize);
211
212         if (fileSize == fread(buf, 1, fileSize, fp))
213         {
214             if (OC_STACK_OK != ps->decrypt(buf, fileSize, &pt, &pt_len))
215             {
216                 LOGE( "ps->decrypt() Failed !!");
217                 fileSize = 0;
218                 goto exit;
219             }
220             else
221             {
222                 memcpy(buffer, pt, pt_len);
223                 fileSize = pt_len;
224             }
225         }
226         else
227         {
228             LOGE("Failed fileSize == fread(buf, 1, fileSize, fp)");
229             fileSize = 0;
230         }
231 exit:
232         free(buf);
233         return fileSize;
234     }
235
236     size_t JniOcSecurity::client_write(const void *buffer, size_t size,
237             size_t nmemb, FILE *fp)
238     {
239         size_t fileSize = size * nmemb;
240         unsigned char *ct = NULL;
241         size_t ct_len = 0;
242         OCPersistentStorage *ps = JniOcSecurity::getOCPersistentStorage();
243
244         if (0 != ps->encrypt((const unsigned char*)buffer, fileSize, &ct, &ct_len))
245         {
246             LOGE( "ps->encrypt() Failed !!");
247             fileSize = 0;
248             goto exit;
249         }
250
251         fileSize = ct_len;
252         if (fileSize != fwrite(ct, 1, fileSize,fp))
253         {
254             LOGE( "fwrite() Failed !!");
255             fileSize = 0;
256             goto exit;
257         }
258 exit:
259         free(ct);
260         return fileSize;
261     }
262 #endif
263 }