1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 ******************************************************************/
21 #include "JniOcSecurity.h"
22 #include "JniOcStack.h"
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.
31 namespace PH = std::placeholders;
33 static string s_enc_dbpath, s_rescue_path;
34 static int AES_KEY_SZ;
36 string& JniOcSecurity::store_path()
38 static string s_dbPath;
42 string& JniOcSecurity::getEncDbPath()
46 string& JniOcSecurity::getRescueDbPath()
51 void JniOcSecurity::StoreDbPath(const string &path)
56 void JniOcSecurity::StoreDefault_DbPath(const string &path, const string &rescue_path, int key_size)
59 s_rescue_path = rescue_path;
60 AES_KEY_SZ = key_size;
63 OCPersistentStorage* JniOcSecurity::getOCPersistentStorageEnc()
65 if (getEncDbPath().empty())
69 static OCPersistentStorage s_psEnc { &JniOcSecurity::client_open_enc, fread,
70 fwrite, fclose, unlink, &JniOcSecurity::client_encrypt,
71 &JniOcSecurity::client_decrypt};
75 OCPersistentStorage* JniOcSecurity::getOCPersistentStorageRescue()
77 if (getRescueDbPath().empty())
81 static OCPersistentStorage s_psRescue { &JniOcSecurity::client_open_rescue, fread,
82 fwrite, fclose, unlink, &JniOcSecurity::client_encrypt,
83 &JniOcSecurity::client_decrypt};
87 OCPersistentStorage* JniOcSecurity::getOCPersistentStorage()
89 if (store_path().empty())
93 static OCPersistentStorage s_ps { &JniOcSecurity::client_open, fread,
94 fwrite, fclose, &JniOcSecurity::client_unlink, NULL, NULL};
97 int JniOcSecurity::client_encrypt(const unsigned char *pt, size_t pt_len,
98 unsigned char **ct, size_t *ct_len)
100 if (OC_STACK_OK != OCEncrypt(pt, pt_len, ct, ct_len))
105 int JniOcSecurity::client_decrypt(const unsigned char *ct, size_t ct_len,
106 unsigned char **pt, size_t *pt_len)
108 if (OC_STACK_OK != OCDecrypt(ct, ct_len, pt, pt_len))
113 FILE* JniOcSecurity::client_open(const char *path, const char *mode)
115 LOGI("Opening SVR Database file '%s' with mode '%s'\n", store_path().c_str(), mode);
116 return fopen(store_path().c_str(), mode);
120 FILE* JniOcSecurity::client_open_enc(const char *path, const char *mode)
122 LOGI("Opening SVR Database file '%s' with mode '%s'\n", getEncDbPath().c_str(), mode);
123 return fopen(getEncDbPath().c_str(), mode);
126 FILE* JniOcSecurity::client_open_rescue(const char *path, const char *mode)
128 LOGI("Opening SVR Database file '%s' with mode '%s'\n", getRescueDbPath().c_str(), mode);
129 return fopen(getRescueDbPath().c_str(), mode);
132 int JniOcSecurity::client_unlink(const char *path)
134 LOGI("unlink SVR Database file '%s' \n", store_path().c_str());
135 return unlink(store_path().c_str());
138 size_t JniOcSecurity::getDBSize(const char *filepath)
140 FILE *fp = fopen(filepath, "rb");
147 size_t bytesRead = 0;
150 bytesRead = fread(buffer, 1, 1023, fp); //we here read encrypted data
158 OCStackResult JniOcSecurity::ResetDBFile(const char *cred_file)
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;
166 OCPersistentStorage *ps = JniOcSecurity::getOCPersistentStorage();
168 FILE *fp = fopen(s_rescue_path.c_str(), "rb");
169 FILE *filep = fopen(cred_file, "wb");
170 OCStackResult ret = OC_STACK_OK;
174 LOGE("Failed to open %s\n", s_rescue_path.c_str());
175 ret = OC_STACK_ERROR;
178 if (fread(data, 1, fileSize, fp) != fileSize)
180 LOGE("Failed to read %s\n", s_rescue_path.c_str());
181 ret = OC_STACK_ERROR;
184 if (OC_STACK_OK != ps->encrypt(data, fileSize, &ct, &ct_len))
186 LOGE("Encryption Failed %d\n",__LINE__);
188 if (fwrite(ct, 1, ct_len, filep) != ct_len)
190 LOGE("Failed to write encrypted data to %s\n", cred_file);
191 ret = OC_STACK_ERROR;
195 if (filep) fclose(filep);
201 size_t JniOcSecurity::client_read(void *buffer, size_t size, size_t nmemb, FILE *fp)
203 size_t fileSize = size * nmemb;
205 unsigned char *pt = NULL;
207 OCPersistentStorage *ps = JniOcSecurity::getOCPersistentStorage();
208 unsigned char *buf = (unsigned char*)calloc(1, sizeof(unsigned char) * fileSize);
210 LOGE("In JniOcSecurity::client_read ps = %p, fileSize = %d\n", ps, fileSize);
212 if (fileSize == fread(buf, 1, fileSize, fp))
214 if (OC_STACK_OK != ps->decrypt(buf, fileSize, &pt, &pt_len))
216 LOGE( "ps->decrypt() Failed !!");
222 memcpy(buffer, pt, pt_len);
228 LOGE("Failed fileSize == fread(buf, 1, fileSize, fp)");
236 size_t JniOcSecurity::client_write(const void *buffer, size_t size,
237 size_t nmemb, FILE *fp)
239 size_t fileSize = size * nmemb;
240 unsigned char *ct = NULL;
242 OCPersistentStorage *ps = JniOcSecurity::getOCPersistentStorage();
244 if (0 != ps->encrypt((const unsigned char*)buffer, fileSize, &ct, &ct_len))
246 LOGE( "ps->encrypt() Failed !!");
252 if (fileSize != fwrite(ct, 1, fileSize,fp))
254 LOGE( "fwrite() Failed !!");