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 "resourcemanager.h"
26 #include "pstatresource.h"
27 #include "psinterface.h"
30 #include "srmresourcestrings.h"
31 #include "srmutility.h"
35 #define TAG PCF("SRM-PSTAT")
37 static OicSecDpom_t gSm = SINGLE_SERVICE_CLIENT_DRIVEN;
38 static OicSecPstat_t gDefaultPstat =
40 false, // bool isOwned
41 (OicSecDpm_t)(TAKE_OWNER | BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
42 PROVISION_CREDENTIALS | PROVISION_ACLS), // OicSecDpm_t cm
43 (OicSecDpm_t)(TAKE_OWNER | BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
44 PROVISION_CREDENTIALS | PROVISION_ACLS), // OicSecDpm_t tm
45 {.id = {0}}, // OicUuid_t deviceID
46 SINGLE_SERVICE_CLIENT_DRIVEN, // OicSecDpom_t om */
47 1, // the number of elts in Sms
48 &gSm, // OicSecDpom_t *sm
49 0, // uint16_t commitHash
51 static OicSecPstat_t *gPstat = NULL;
52 static OCResourceHandle gPstatHandle = NULL;
54 void DeletePstatBinData(OicSecPstat_t* pstat)
58 //Clean 'supported modes' field
66 char * BinToPstatJSON(const OicSecPstat_t * pstat)
73 cJSON *jsonPstat = NULL;
75 cJSON *jsonSmArray = NULL;
76 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*) 0)->id)) + 1] = {};
78 B64Result b64Ret = B64_OK;
80 cJSON *jsonRoot = cJSON_CreateObject();
81 VERIFY_NON_NULL(TAG, jsonRoot, INFO);
83 cJSON_AddItemToObject(jsonRoot, OIC_JSON_PSTAT_NAME, jsonPstat=cJSON_CreateObject());
84 cJSON_AddBoolToObject(jsonPstat, OIC_JSON_ISOP_NAME, pstat->isOp);
86 b64Ret = b64Encode(pstat->deviceID.id,
87 sizeof(pstat->deviceID.id), base64Buff, sizeof(base64Buff), &outLen);
88 VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
90 cJSON_AddStringToObject(jsonPstat, OIC_JSON_DEVICE_ID_NAME, base64Buff);
91 cJSON_AddNumberToObject(jsonPstat, OIC_JSON_COMMIT_HASH_NAME, pstat->commitHash);
92 cJSON_AddNumberToObject(jsonPstat, OIC_JSON_CM_NAME, (int)pstat->cm);
93 cJSON_AddNumberToObject(jsonPstat, OIC_JSON_TM_NAME, (int)pstat->tm);
94 cJSON_AddNumberToObject(jsonPstat, OIC_JSON_OM_NAME, (int)pstat->om);
96 cJSON_AddItemToObject(jsonPstat, OIC_JSON_SM_NAME, jsonSmArray = cJSON_CreateArray());
97 VERIFY_NON_NULL(TAG, jsonSmArray, INFO);
98 for (size_t i = 0; i < pstat->smLen; i++)
100 cJSON_AddItemToArray(jsonSmArray, cJSON_CreateNumber((int )pstat->sm[i]));
102 jsonStr = cJSON_Print(jsonRoot);
107 cJSON_Delete(jsonRoot);
112 OicSecPstat_t * JSONToPstatBin(const char * jsonStr)
119 OCStackResult ret = OC_STACK_ERROR;
120 OicSecPstat_t *pstat = NULL;
121 cJSON *jsonPstat = NULL;
122 cJSON *jsonObj = NULL;
124 unsigned char base64Buff[sizeof(((OicUuid_t*) 0)->id)] = {};
126 B64Result b64Ret = B64_OK;
128 cJSON *jsonRoot = cJSON_Parse(jsonStr);
129 VERIFY_NON_NULL(TAG, jsonRoot, INFO);
131 jsonPstat = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
132 VERIFY_NON_NULL(TAG, jsonPstat, INFO);
134 pstat = (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
135 VERIFY_NON_NULL(TAG, pstat, INFO);
136 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_ISOP_NAME);
137 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
138 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type) , ERROR);
139 pstat->isOp = jsonObj->valueint;
141 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_DEVICE_ID_NAME);
142 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
143 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
144 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
145 sizeof(base64Buff), &outLen);
146 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(pstat->deviceID.id)), ERROR);
147 memcpy(pstat->deviceID.id, base64Buff, outLen);
149 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_COMMIT_HASH_NAME);
150 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
151 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
152 pstat->commitHash = jsonObj->valueint;
154 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_CM_NAME);
155 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
156 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
157 pstat->cm = (OicSecDpm_t)jsonObj->valueint;
159 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
160 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
161 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
162 pstat->om = (OicSecDpom_t)jsonObj->valueint;
164 jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_SM_NAME);
165 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
166 if (cJSON_Array == jsonObj->type)
168 pstat->smLen = cJSON_GetArraySize(jsonObj);
170 VERIFY_SUCCESS(TAG, pstat->smLen != 0, ERROR);
171 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
172 VERIFY_NON_NULL(TAG, pstat->sm, ERROR);
175 cJSON *jsonSm = cJSON_GetArrayItem(jsonObj, idxx);
176 VERIFY_NON_NULL(TAG, jsonSm, ERROR);
177 pstat->sm[idxx] = (OicSecDpom_t)jsonSm->valueint;
178 }while ( ++idxx < pstat->smLen);
183 cJSON_Delete(jsonRoot);
184 if (OC_STACK_OK != ret)
186 OC_LOG (ERROR, TAG, PCF("JSONToPstatBin failed"));
187 DeletePstatBinData(pstat);
194 * The entity handler determines how to process a GET request.
196 static OCEntityHandlerResult HandlePstatGetRequest (const OCEntityHandlerRequest * ehRequest)
198 // Convert ACL data into JSON for transmission
199 char* jsonStr = BinToPstatJSON(gPstat);
201 // A device should always have a default pstat. Therefore, jsonStr should never be NULL.
202 OCEntityHandlerResult ehRet = (jsonStr ? OC_EH_OK : OC_EH_ERROR);
204 // Send response payload to request originator
205 SendSRMResponse(ehRequest, ehRet, jsonStr);
211 * The entity handler determines how to process a POST request.
212 * Per the REST paradigm, POST can also be used to update representation of existing
213 * resource or create a new resource.
214 * For pstat, it updates only tm and om.
216 static OCEntityHandlerResult HandlePstatPutRequest(const OCEntityHandlerRequest *ehRequest)
218 OCEntityHandlerResult ehRet = OC_EH_ERROR;
219 cJSON *postJson = NULL;
221 if (ehRequest->resource)
223 postJson = cJSON_Parse(((OCSecurityPayload*)ehRequest->payload)->securityData);
224 VERIFY_NON_NULL(TAG, postJson, INFO);
225 cJSON *jsonPstat = cJSON_GetObjectItem(postJson, OIC_JSON_PSTAT_NAME);
226 VERIFY_NON_NULL(TAG, jsonPstat, INFO);
227 cJSON *commitHashJson = cJSON_GetObjectItem(jsonPstat, OIC_JSON_COMMIT_HASH_NAME);
228 uint16_t commitHash = 0;
231 commitHash = commitHashJson->valueint;
233 cJSON *tmJson = cJSON_GetObjectItem(jsonPstat, OIC_JSON_TM_NAME);
234 if (tmJson && gPstat)
236 gPstat->tm = (OicSecDpm_t)tmJson->valueint;
237 if(0 == tmJson->valueint && gPstat->commitHash == commitHash)
241 OC_LOG (INFO, TAG, PCF("CommitHash is valid and isOp is TRUE"));
245 OC_LOG (INFO, TAG, PCF("CommitHash is not valid"));
248 cJSON *omJson = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
249 if (omJson && gPstat)
252 * Check if the operation mode is in the supported provisioning services
253 * operation mode list.
255 for(size_t i=0; i< gPstat->smLen; i++)
257 if(gPstat->sm[i] == (unsigned int)omJson->valueint)
259 gPstat->om = (OicSecDpom_t)omJson->valueint;
264 // Convert pstat data into JSON for update to persistent storage
265 char *jsonStr = BinToPstatJSON(gPstat);
268 cJSON *jsonPstat = cJSON_Parse(jsonStr);
270 if (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_PSTAT_NAME, jsonPstat))
277 //Send payload to request originator
278 if(OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL))
280 OC_LOG (ERROR, TAG, PCF("SendSRMResponse failed in HandlePstatPostRequest"));
282 cJSON_Delete(postJson);
287 * This internal method is the entity handler for pstat resources.
289 OCEntityHandlerResult PstatEntityHandler(OCEntityHandlerFlag flag,
290 OCEntityHandlerRequest * ehRequest,
294 OCEntityHandlerResult ehRet = OC_EH_ERROR;
295 // This method will handle REST request (GET/POST) for /oic/sec/pstat
296 if (flag & OC_REQUEST_FLAG)
298 OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG"));
299 switch (ehRequest->method)
302 ehRet = HandlePstatGetRequest(ehRequest);
305 ehRet = HandlePstatPutRequest(ehRequest);
309 SendSRMResponse(ehRequest, ehRet, NULL);
317 * This internal method is used to create '/oic/sec/pstat' resource.
319 OCStackResult CreatePstatResource()
323 ret = OCCreateResource(&gPstatHandle,
324 OIC_RSRC_TYPE_SEC_PSTAT,
331 if (ret != OC_STACK_OK)
333 OC_LOG (FATAL, TAG, PCF("Unable to instantiate pstat resource"));
334 DeInitPstatResource();
340 * Post ACL hander update the commitHash during ACL provisioning.
342 void SetCommitHash(uint16_t commitHash)
344 gPstat->commitHash = commitHash;
348 * Get the default value
349 * @retval the gDefaultPstat pointer
351 static OicSecPstat_t* GetPstatDefault()
353 return &gDefaultPstat;
357 * Initialize pstat resource by loading data from persistent storage.
359 * @retval OC_STACK_OK for Success, otherwise some error value
361 OCStackResult InitPstatResource()
363 OCStackResult ret = OC_STACK_ERROR;
365 // Read Pstat resource from PS
366 char* jsonSVRDatabase = GetSVRDatabase();
369 // Convert JSON Pstat into binary format
370 gPstat = JSONToPstatBin(jsonSVRDatabase);
373 * If SVR database in persistent storage got corrupted or
374 * is not available for some reason, a default pstat is created
375 * which allows user to initiate pstat provisioning again.
377 if(!jsonSVRDatabase || !gPstat)
379 gPstat = GetPstatDefault();
381 // Instantiate 'oic.sec.pstat'
382 ret = CreatePstatResource();
384 OICFree(jsonSVRDatabase);
389 * Perform cleanup for pstat resources.
391 * @retval OC_STACK_OK for Success, otherwise some error value
393 OCStackResult DeInitPstatResource()
395 if(gPstat != &gDefaultPstat)
397 DeletePstatBinData(gPstat);
400 return OCDeleteResource(gPstatHandle);