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 "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, "JSONToPstatBin failed");
187 DeletePstatBinData(pstat);
194 * The entity handler determines how to process a GET request.
196 static OCEntityHandlerResult HandlePstatGetRequest (const OCEntityHandlerRequest * ehRequest)
198 OC_LOG (INFO, TAG, "HandlePstatGetRequest processing GET request");
199 // Convert ACL data into JSON for transmission
200 char* jsonStr = BinToPstatJSON(gPstat);
202 // A device should always have a default pstat. Therefore, jsonStr should never be NULL.
203 OCEntityHandlerResult ehRet = (jsonStr ? OC_EH_OK : OC_EH_ERROR);
205 // Send response payload to request originator
206 SendSRMResponse(ehRequest, ehRet, jsonStr);
212 * The entity handler determines how to process a POST request.
213 * Per the REST paradigm, POST can also be used to update representation of existing
214 * resource or create a new resource.
215 * For pstat, it updates only tm and om.
217 static OCEntityHandlerResult HandlePstatPutRequest(const OCEntityHandlerRequest *ehRequest)
219 OCEntityHandlerResult ehRet = OC_EH_ERROR;
220 cJSON *postJson = NULL;
221 OC_LOG (INFO, TAG, "HandlePstatPutRequest processing PUT request");
223 if (ehRequest->resource)
225 postJson = cJSON_Parse(((OCSecurityPayload*)ehRequest->payload)->securityData);
226 VERIFY_NON_NULL(TAG, postJson, INFO);
227 cJSON *jsonPstat = cJSON_GetObjectItem(postJson, OIC_JSON_PSTAT_NAME);
228 VERIFY_NON_NULL(TAG, jsonPstat, INFO);
229 cJSON *commitHashJson = cJSON_GetObjectItem(jsonPstat, OIC_JSON_COMMIT_HASH_NAME);
230 uint16_t commitHash = 0;
233 commitHash = commitHashJson->valueint;
235 cJSON *tmJson = cJSON_GetObjectItem(jsonPstat, OIC_JSON_TM_NAME);
236 if (tmJson && gPstat)
238 gPstat->tm = (OicSecDpm_t)tmJson->valueint;
239 if(0 == tmJson->valueint && gPstat->commitHash == commitHash)
243 OC_LOG (INFO, TAG, "CommitHash is valid and isOp is TRUE");
247 OC_LOG (INFO, TAG, "CommitHash is not valid");
250 cJSON *omJson = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
251 if (omJson && gPstat)
254 * Check if the operation mode is in the supported provisioning services
255 * operation mode list.
257 for(size_t i=0; i< gPstat->smLen; i++)
259 if(gPstat->sm[i] == (unsigned int)omJson->valueint)
261 gPstat->om = (OicSecDpom_t)omJson->valueint;
266 // Convert pstat data into JSON for update to persistent storage
267 char *jsonStr = BinToPstatJSON(gPstat);
270 cJSON *jsonPstat = cJSON_Parse(jsonStr);
272 if (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_PSTAT_NAME, jsonPstat))
279 //Send payload to request originator
280 if(OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL))
282 OC_LOG (ERROR, TAG, "SendSRMResponse failed in HandlePstatPostRequest");
284 cJSON_Delete(postJson);
289 * This internal method is the entity handler for pstat resources.
291 OCEntityHandlerResult PstatEntityHandler(OCEntityHandlerFlag flag,
292 OCEntityHandlerRequest * ehRequest,
296 OCEntityHandlerResult ehRet = OC_EH_ERROR;
297 // This method will handle REST request (GET/POST) for /oic/sec/pstat
298 if (flag & OC_REQUEST_FLAG)
300 OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
301 switch (ehRequest->method)
304 ehRet = HandlePstatGetRequest(ehRequest);
307 ehRet = HandlePstatPutRequest(ehRequest);
311 SendSRMResponse(ehRequest, ehRet, NULL);
319 * This internal method is used to create '/oic/sec/pstat' resource.
321 OCStackResult CreatePstatResource()
325 ret = OCCreateResource(&gPstatHandle,
326 OIC_RSRC_TYPE_SEC_PSTAT,
333 if (ret != OC_STACK_OK)
335 OC_LOG (FATAL, TAG, "Unable to instantiate pstat resource");
336 DeInitPstatResource();
342 * Post ACL hander update the commitHash during ACL provisioning.
344 void SetCommitHash(uint16_t commitHash)
346 gPstat->commitHash = commitHash;
350 * Get the default value
351 * @retval the gDefaultPstat pointer
353 static OicSecPstat_t* GetPstatDefault()
355 return &gDefaultPstat;
359 * Initialize pstat resource by loading data from persistent storage.
361 * @retval OC_STACK_OK for Success, otherwise some error value
363 OCStackResult InitPstatResource()
365 OCStackResult ret = OC_STACK_ERROR;
367 // Read Pstat resource from PS
368 char* jsonSVRDatabase = GetSVRDatabase();
371 // Convert JSON Pstat into binary format
372 gPstat = JSONToPstatBin(jsonSVRDatabase);
375 * If SVR database in persistent storage got corrupted or
376 * is not available for some reason, a default pstat is created
377 * which allows user to initiate pstat provisioning again.
379 if(!jsonSVRDatabase || !gPstat)
381 gPstat = GetPstatDefault();
383 // Instantiate 'oic.sec.pstat'
384 ret = CreatePstatResource();
386 OICFree(jsonSVRDatabase);
391 * Perform cleanup for pstat resources.
393 * @retval OC_STACK_OK for Success, otherwise some error value
395 OCStackResult DeInitPstatResource()
397 if(gPstat != &gDefaultPstat)
399 DeletePstatBinData(gPstat);
402 return OCDeleteResource(gPstatHandle);