Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / security / src / pstatresource.c
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "ocstack.h"
22 #include "logger.h"
23 #include "oic_malloc.h"
24 #include "cJSON.h"
25 #include "resourcemanager.h"
26 #include "pstatresource.h"
27 #include "psinterface.h"
28 #include "utlist.h"
29 #include "base64.h"
30 #include "srmresourcestrings.h"
31 #include "srmutility.h"
32 #include <stdlib.h>
33 #include <string.h>
34
35 #define TAG  PCF("SRM-PSTAT")
36
37 static OicSecDpom_t gSm = SINGLE_SERVICE_CLIENT_DRIVEN;
38 static OicSecPstat_t gDefaultPstat =
39 {
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     {},                                       // 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
50 };
51 static OicSecPstat_t    *gPstat = NULL;
52 static OCResourceHandle gPstatHandle = NULL;
53
54 void DeletePstatBinData(OicSecPstat_t* pstat)
55 {
56     if (pstat)
57     {
58         //Clean 'supported modes' field
59         OICFree(pstat->sm);
60
61         //Clean pstat itself
62         OICFree(pstat);
63     }
64 }
65
66 char * BinToPstatJSON(const OicSecPstat_t * pstat)
67 {
68     if(NULL == pstat)
69     {
70         return NULL;
71     }
72
73     cJSON *jsonPstat = NULL;
74     char *jsonStr = NULL;
75     cJSON *jsonSmArray = NULL;
76     char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*) 0)->id)) + 1] = {};
77     uint32_t outLen = 0;
78     B64Result b64Ret = B64_OK;
79
80     cJSON *jsonRoot = cJSON_CreateObject();
81     VERIFY_NON_NULL(TAG, jsonRoot, INFO);
82
83     cJSON_AddItemToObject(jsonRoot, OIC_JSON_PSTAT_NAME, jsonPstat=cJSON_CreateObject());
84     cJSON_AddBoolToObject(jsonPstat, OIC_JSON_ISOP_NAME, pstat->isOp);
85
86     b64Ret = b64Encode(pstat->deviceID.id,
87             sizeof(pstat->deviceID.id), base64Buff, sizeof(base64Buff), &outLen);
88     VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
89
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);
95
96     cJSON_AddItemToObject(jsonPstat, OIC_JSON_SM_NAME, jsonSmArray = cJSON_CreateArray());
97     VERIFY_NON_NULL(TAG, jsonSmArray, INFO);
98     for (int i = 0; i < pstat->smLen; i++)
99     {
100         cJSON_AddItemToArray(jsonSmArray, cJSON_CreateNumber((int )pstat->sm[i]));
101     }
102     jsonStr = cJSON_Print(jsonRoot);
103
104 exit:
105     if (jsonRoot)
106     {
107         cJSON_Delete(jsonRoot);
108     }
109     return jsonStr;
110 }
111
112 OicSecPstat_t * JSONToPstatBin(const char * jsonStr)
113 {
114     if(NULL == jsonStr)
115     {
116         return NULL;
117     }
118
119     OCStackResult ret = OC_STACK_ERROR;
120     OicSecPstat_t *pstat = NULL;
121     cJSON *jsonPstat = NULL;
122     cJSON *jsonObj = NULL;
123
124     unsigned char base64Buff[sizeof(((OicUuid_t*) 0)->id)] = {};
125     uint32_t outLen = 0;
126     B64Result b64Ret = B64_OK;
127
128     cJSON *jsonRoot = cJSON_Parse(jsonStr);
129     VERIFY_NON_NULL(TAG, jsonRoot, INFO);
130
131     jsonPstat = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
132     VERIFY_NON_NULL(TAG, jsonPstat, INFO);
133
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;
140
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);
148
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;
153
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;
158
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;
163
164     jsonObj = cJSON_GetObjectItem(jsonPstat, OIC_JSON_SM_NAME);
165     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
166     if (cJSON_Array == jsonObj->type)
167     {
168         pstat->smLen = cJSON_GetArraySize(jsonObj);
169         int idxx = 0;
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);
173         do
174         {
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);
179     }
180     ret = OC_STACK_OK;
181
182 exit:
183     cJSON_Delete(jsonRoot);
184     if (OC_STACK_OK != ret)
185     {
186         OC_LOG (ERROR, TAG, PCF("JSONToPstatBin failed"));
187         DeletePstatBinData(pstat);
188         pstat = NULL;
189     }
190     return pstat;
191 }
192
193 /**
194  * The entity handler determines how to process a GET request.
195  */
196 static OCEntityHandlerResult HandlePstatGetRequest (const OCEntityHandlerRequest * ehRequest)
197 {
198     // Convert ACL data into JSON for transmission
199     char* jsonStr = BinToPstatJSON(gPstat);
200
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);
203
204     // Send response payload to request originator
205     SendSRMResponse(ehRequest, ehRet, jsonStr);
206     OICFree(jsonStr);
207     return ehRet;
208 }
209
210 /**
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.
215  */
216 static OCEntityHandlerResult HandlePstatPutRequest(const OCEntityHandlerRequest *ehRequest)
217 {
218     OCEntityHandlerResult ehRet = OC_EH_ERROR;
219     cJSON *postJson = NULL;
220
221     if (ehRequest->resource)
222     {
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;
229         if (commitHashJson)
230         {
231             commitHash = commitHashJson->valueint;
232         }
233         cJSON *tmJson = cJSON_GetObjectItem(jsonPstat, OIC_JSON_TM_NAME);
234         if (tmJson && gPstat)
235         {
236             gPstat->tm = (OicSecDpm_t)tmJson->valueint;
237             if(0 == tmJson->valueint && gPstat->commitHash == commitHash)
238             {
239                 gPstat->isOp = true;
240                 gPstat->cm = NORMAL;
241                 OC_LOG (INFO, TAG, PCF("CommitHash is valid and isOp is TRUE"));
242             }
243             else
244             {
245                 OC_LOG (INFO, TAG, PCF("CommitHash is not valid"));
246             }
247         }
248         cJSON *omJson = cJSON_GetObjectItem(jsonPstat, OIC_JSON_OM_NAME);
249         if (omJson && gPstat)
250         {
251             /*
252              * Check if the operation mode is in the supported provisioning services
253              * operation mode list.
254              */
255             for(size_t i=0; i< gPstat->smLen; i++)
256             {
257                 if(gPstat->sm[i] == omJson->valueint)
258                 {
259                     gPstat->om = (OicSecDpom_t)omJson->valueint;
260                     break;
261                 }
262             }
263         }
264         // Convert pstat data into JSON for update to persistent storage
265         char *jsonStr = BinToPstatJSON(gPstat);
266         if (jsonStr)
267         {
268             cJSON *jsonPstat = cJSON_Parse(jsonStr);
269             OICFree(jsonStr);
270             if (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_PSTAT_NAME, jsonPstat))
271             {
272                 ehRet = OC_EH_OK;
273             }
274         }
275     }
276  exit:
277     //Send payload to request originator
278     if(OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL))
279     {
280         OC_LOG (ERROR, TAG, PCF("SendSRMResponse failed in HandlePstatPostRequest"));
281     }
282     cJSON_Delete(postJson);
283     return ehRet;
284 }
285
286 /**
287  * This internal method is the entity handler for pstat resources.
288  */
289 OCEntityHandlerResult PstatEntityHandler(OCEntityHandlerFlag flag,
290         OCEntityHandlerRequest * ehRequest,
291         void *callbackParam)
292 {
293     OCEntityHandlerResult ehRet = OC_EH_ERROR;
294     // This method will handle REST request (GET/POST) for /oic/sec/pstat
295     if (flag & OC_REQUEST_FLAG)
296     {
297         OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG"));
298         switch (ehRequest->method)
299         {
300             case OC_REST_GET:
301                 ehRet = HandlePstatGetRequest(ehRequest);
302                 break;
303             case OC_REST_PUT:
304                 ehRet = HandlePstatPutRequest(ehRequest);
305                 break;
306             default:
307                 ehRet = OC_EH_ERROR;
308                 SendSRMResponse(ehRequest, ehRet, NULL);
309                 break;
310         }
311     }
312     return ehRet;
313 }
314
315 /**
316  * This internal method is used to create '/oic/sec/pstat' resource.
317  */
318 OCStackResult CreatePstatResource()
319 {
320     OCStackResult ret;
321
322     ret = OCCreateResource(&gPstatHandle,
323                            OIC_RSRC_TYPE_SEC_PSTAT,
324                            OIC_MI_DEF,
325                            OIC_RSRC_PSTAT_URI,
326                            PstatEntityHandler,
327                            NULL,
328                            OC_RES_PROP_NONE);
329
330     if (ret != OC_STACK_OK)
331     {
332         OC_LOG (FATAL, TAG, PCF("Unable to instantiate pstat resource"));
333         DeInitPstatResource();
334     }
335     return ret;
336 }
337
338 /**
339  * Post ACL hander update the commitHash during ACL provisioning.
340  */
341 void SetCommitHash(uint16_t commitHash)
342 {
343     gPstat->commitHash = commitHash;
344 }
345
346 /**
347  * Get the default value
348  * @retval  the gDefaultPstat pointer
349  */
350 static OicSecPstat_t* GetPstatDefault()
351 {
352     return &gDefaultPstat;
353 }
354
355 /**
356  * Initialize pstat resource by loading data from persistent storage.
357  *
358  * @retval  OC_STACK_OK for Success, otherwise some error value
359  */
360 OCStackResult InitPstatResource()
361 {
362     OCStackResult ret = OC_STACK_ERROR;
363
364     // Read Pstat resource from PS
365     char* jsonSVRDatabase = GetSVRDatabase();
366     if (jsonSVRDatabase)
367     {
368         // Convert JSON Pstat into binary format
369         gPstat = JSONToPstatBin(jsonSVRDatabase);
370     }
371     /*
372      * If SVR database in persistent storage got corrupted or
373      * is not available for some reason, a default pstat is created
374      * which allows user to initiate pstat provisioning again.
375      */
376     if(!jsonSVRDatabase || !gPstat)
377     {
378         gPstat = GetPstatDefault();
379     }
380     // Instantiate 'oic.sec.pstat'
381     ret = CreatePstatResource();
382
383     OICFree(jsonSVRDatabase);
384     return ret;
385 }
386
387 /**
388  * Perform cleanup for pstat resources.
389  *
390  * @retval  OC_STACK_OK for Success, otherwise some error value
391  */
392 OCStackResult DeInitPstatResource()
393 {
394     if(gPstat != &gDefaultPstat)
395     {
396         DeletePstatBinData(gPstat);
397         gPstat = NULL;
398     }
399     return OCDeleteResource(gPstatHandle);
400 }
401