1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "oic_malloc.h"
25 #include "cainterface.h"
26 #include "secureresourcemanager.h"
27 #include "resourcemanager.h"
28 #include "srmresourcestrings.h"
29 #include "srmutility.h"
33 #define TAG PCF("SRM-PSI")
35 //SVR database buffer block size
36 const size_t DB_FILE_SIZE_BLOCK = 1023;
39 * Gets the Secure Virtual Database size.
41 * @param ps pointer of OCPersistentStorage for the SVR name ("acl", "cred", "pstat" etc).
43 * @retval total size of the SVR database.
45 size_t GetSVRDatabaseSize(OCPersistentStorage* ps)
53 char buffer[DB_FILE_SIZE_BLOCK];
54 FILE* fp = ps->open(SVR_DB_FILE_NAME, "r");
59 bytesRead = ps->read(buffer, 1, DB_FILE_SIZE_BLOCK, fp);
61 } while (bytesRead > 0);
68 * Reads the Secure Virtual Database from PS into dynamically allocated
71 * @note Caller of this method MUST use OICFree() method to release memory
72 * referenced by return value.
74 * @retval reference to memory buffer containing SVR database.
76 char * GetSVRDatabase()
78 char * jsonStr = NULL;
80 OCPersistentStorage* ps = SRMGetPersistentStorageHandler();
81 int size = GetSVRDatabaseSize(ps);
84 OC_LOG (ERROR, TAG, PCF("FindSVRDatabaseSize failed"));
90 // Open default SRM database file. An app could change the path for its server.
91 fp = ps->open(SVR_DB_FILE_NAME, "r");
94 jsonStr = (char*)OICMalloc(size + 1);
95 VERIFY_NON_NULL(TAG, jsonStr, FATAL);
96 size_t bytesRead = ps->read(jsonStr, 1, size, fp);
97 jsonStr[bytesRead] = '\0';
99 OC_LOG_V(INFO, TAG, PCF("Read %d bytes from SVR database file"), bytesRead);
105 OC_LOG (ERROR, TAG, PCF("Unable to open SVR database file!!"));
119 * This method is used by a entity handlers of SVR's to update
122 * @param rsrcName string denoting the SVR name ("acl", "cred", "pstat" etc).
123 * @param jsonObj JSON object containing the SVR contents.
125 * @retval OC_STACK_OK for Success, otherwise some error value
127 OCStackResult UpdateSVRDatabase(const char* rsrcName, cJSON* jsonObj)
129 OCStackResult ret = OC_STACK_ERROR;
130 cJSON *jsonSVRDb = NULL;
131 OCPersistentStorage* ps = NULL;
133 // Read SVR database from PS
134 char* jsonSVRDbStr = GetSVRDatabase();
135 VERIFY_NON_NULL(TAG,jsonSVRDbStr, ERROR);
137 // Use cJSON_Parse to parse the existing SVR database
138 jsonSVRDb = cJSON_Parse(jsonSVRDbStr);
139 VERIFY_NON_NULL(TAG,jsonSVRDb, ERROR);
141 OICFree(jsonSVRDbStr);
144 //If Cred resource gets updated with empty list then delete the Cred
145 //object from database.
146 if(NULL == jsonObj && (0 == strcmp(rsrcName, OIC_JSON_CRED_NAME)))
148 cJSON_DeleteItemFromObject(jsonSVRDb, rsrcName);
150 else if (jsonObj->child )
152 // Create a duplicate of the JSON object which was passed.
153 cJSON* jsonDuplicateObj = cJSON_Duplicate(jsonObj, 1);
154 VERIFY_NON_NULL(TAG,jsonDuplicateObj, ERROR);
156 cJSON* jsonObj = cJSON_GetObjectItem(jsonSVRDb, rsrcName);
159 ACL, PStat & Doxm resources at least have default entries in the database but
160 Cred resource may have no entries. The first cred resource entry (for provisioning tool)
161 is created when the device is owned by provisioning tool and it's ownerpsk is generated.*/
162 if((strcmp(rsrcName, OIC_JSON_CRED_NAME) == 0) && (!jsonObj))
164 // Add the fist cred object in existing SVR database json
165 cJSON_AddItemToObject(jsonSVRDb, rsrcName, jsonDuplicateObj->child);
169 VERIFY_NON_NULL(TAG,jsonObj, ERROR);
171 // Replace the modified json object in existing SVR database json
172 cJSON_ReplaceItemInObject(jsonSVRDb, rsrcName, jsonDuplicateObj->child);
176 // Generate string representation of updated SVR database json object
177 jsonSVRDbStr = cJSON_PrintUnformatted(jsonSVRDb);
178 VERIFY_NON_NULL(TAG,jsonSVRDbStr, ERROR);
180 // Update the persistent storage with new SVR database
181 ps = SRMGetPersistentStorageHandler();
184 FILE* fp = ps->open(SVR_DB_FILE_NAME, "w");
187 size_t bytesWritten = ps->write(jsonSVRDbStr, 1, strlen(jsonSVRDbStr), fp);
188 if (bytesWritten == strlen(jsonSVRDbStr))
192 OC_LOG_V(INFO, TAG, PCF("Written %d bytes into SVR database file"), bytesWritten);
198 OC_LOG (ERROR, TAG, PCF("Unable to open SVR database file!! "));
203 OICFree(jsonSVRDbStr);
204 cJSON_Delete(jsonSVRDb);