Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / security / src / crlresource.c
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics 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 <stdlib.h>
22 #include <string.h>
23 #include "ocstack.h"
24 #include "logger.h"
25 #include "oic_malloc.h"
26 #include "cJSON.h"
27 #include "base64.h"
28 #include "resourcemanager.h"
29 #include "psinterface.h"
30 #include "utlist.h"
31 #include "srmresourcestrings.h"
32 #include "doxmresource.h"
33 #include "srmutility.h"
34 #ifdef __WITH_X509__
35 #include "crlresource.h"
36 #include "crl.h"
37 #endif /* __WITH_X509__ */
38
39 #define TAG  "SRM-CRL"
40
41 #define SEPARATOR                   ":"
42 #define SEPARATOR_LEN               (1)
43 #define JSON_CRL_NAME               "\"CRL\""
44 #define JSON_CRL_NAME_LEN           (5)
45 #define OIC_JSON_CRL_NAME           "crl"
46 #define OIC_JSON_CRL_ID             "CRLId"
47 #define OIC_JSON_CRL_THIS_UPDATE    "ThisUpdate"
48 #define OIC_JSON_CRL_DATA           "CRLData"
49 #define CRL_DEFAULT_CRL_ID           1
50 #define CRL_DEFAULT_THIS_UPDATE     "150101000000Z"
51 #define CRL_DEFAULT_CRL_DATA        "-"
52
53 static OCResourceHandle     gCrlHandle  = NULL;
54 static OicSecCrl_t         *gCrl        = NULL;
55
56
57 void DeleteCrlBinData(OicSecCrl_t *crl)
58 {
59     if (crl)
60     {
61         //Clean ThisUpdate
62         OICFree(crl->ThisUpdate.data);
63
64         //clean CrlData
65         OICFree(crl->CrlData.data);
66
67         //Clean crl itself
68         OICFree(crl);
69     }
70 }
71
72 char *BinToCrlJSON(const OicSecCrl_t *crl)
73 {
74     if (NULL == crl)
75     {
76         return NULL;
77     }
78
79     char *base64Buff = NULL;
80     uint32_t outLen = 0;
81     uint32_t base64CRLLen = 0;
82     B64Result b64Ret = B64_OK;
83     char *jsonStr = NULL;
84     cJSON *jsonRoot = cJSON_CreateObject();
85     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
86     cJSON *jsonCrl = cJSON_CreateObject();
87     VERIFY_NON_NULL(TAG, jsonCrl, ERROR);
88
89     cJSON_AddItemToObject(jsonRoot, OIC_JSON_CRL_NAME, jsonCrl);
90
91     //CRLId -- Mandatory
92     cJSON_AddNumberToObject(jsonCrl, OIC_JSON_CRL_ID, (int)crl->CrlId);
93
94     //ThisUpdate -- Mandatory
95     outLen = 0;
96     base64CRLLen = B64ENCODE_OUT_SAFESIZE(crl->ThisUpdate.len);
97     base64Buff = OICMalloc(base64CRLLen);
98     b64Ret = b64Encode(crl->ThisUpdate.data, crl->ThisUpdate.len, base64Buff,
99              base64CRLLen, &outLen);
100     VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
101     cJSON_AddStringToObject(jsonCrl, OIC_JSON_CRL_THIS_UPDATE, base64Buff);
102     OICFree(base64Buff);
103
104     //CRLData -- Mandatory
105     outLen = 0;
106     base64CRLLen = B64ENCODE_OUT_SAFESIZE(crl->CrlData.len);
107     base64Buff = OICMalloc(base64CRLLen);
108     b64Ret = b64Encode(crl->CrlData.data, crl->CrlData.len, base64Buff,
109              base64CRLLen, &outLen);
110     VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
111     cJSON_AddStringToObject(jsonCrl, OIC_JSON_CRL_DATA, base64Buff);
112
113     jsonStr = cJSON_PrintUnformatted(jsonRoot);
114
115 exit:
116     OICFree(base64Buff);
117     if (jsonRoot)
118     {
119         cJSON_Delete(jsonRoot);
120     }
121     return jsonStr;
122 }
123
124 OicSecCrl_t *JSONToCrlBin(const char * jsonStr)
125 {
126     if (NULL == jsonStr)
127     {
128         return NULL;
129     }
130
131     OCStackResult ret = OC_STACK_ERROR;
132     OicSecCrl_t *crl =  NULL;
133     cJSON *jsonCrl = NULL;
134     cJSON *jsonObj = NULL;
135
136     unsigned char *base64Buff = NULL;
137     uint32_t base64CRLLen = 0;
138     uint32_t outLen = 0;
139     B64Result b64Ret = B64_OK;
140
141     cJSON *jsonRoot = cJSON_Parse(jsonStr);
142     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
143
144     jsonCrl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRL_NAME);
145     VERIFY_NON_NULL(TAG, jsonCrl, ERROR);
146     crl = (OicSecCrl_t *)OICCalloc(1, sizeof(OicSecCrl_t));
147     VERIFY_NON_NULL(TAG, crl, ERROR);
148
149     //CRLId -- Mandatory
150     jsonObj = cJSON_GetObjectItem(jsonCrl, OIC_JSON_CRL_ID);
151     if(jsonObj)
152     {
153         VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
154         crl->CrlId = (uint16_t)jsonObj->valueint;
155     }
156     else // PUT/POST JSON may not have CRLId so set it to the gCRList->CRLId
157     {
158         VERIFY_NON_NULL(TAG, gCrl, ERROR);
159         crl->CrlId = gCrl->CrlId;
160     }
161
162     //ThisUpdate -- Mandatory
163     jsonObj = cJSON_GetObjectItem(jsonCrl, OIC_JSON_CRL_THIS_UPDATE);
164     if(jsonObj)
165     {
166         VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
167         if(cJSON_String == jsonObj->type)
168         {
169             //Check for empty string, in case ThisUpdate field has not been set yet
170             if (jsonObj->valuestring[0])
171             {
172                 base64CRLLen = B64ENCODE_OUT_SAFESIZE(strlen(jsonObj->valuestring));
173                 base64Buff = OICMalloc(base64CRLLen);
174                 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
175                         base64CRLLen, &outLen);
176                 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= base64CRLLen),
177                                 ERROR);
178                 crl->ThisUpdate.data = OICMalloc(outLen + 1);
179                 memcpy(crl->ThisUpdate.data, base64Buff, outLen);
180                 crl->ThisUpdate.len = outLen;
181                 OICFree(base64Buff);
182                 base64Buff = NULL;
183             }
184         }
185     }
186     else // PUT/POST JSON will not have ThisUpdate so set it to the gCRList->ThisUpdate
187     {
188         VERIFY_NON_NULL(TAG, gCrl, ERROR);
189         outLen = gCrl->ThisUpdate.len;
190         crl->ThisUpdate.data = OICMalloc(outLen + 1);
191         memcpy(crl->ThisUpdate.data, gCrl->ThisUpdate.data, outLen);
192         crl->ThisUpdate.len = outLen;
193     }
194
195     //CRLData -- Mandatory
196     jsonObj = cJSON_GetObjectItem(jsonCrl, OIC_JSON_CRL_DATA);
197     if(jsonObj)
198     {
199         VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
200         if(cJSON_String == jsonObj->type)
201         {
202             //Check for empty string, in case CRLData field has not been set yet
203             if (jsonObj->valuestring[0])
204             {
205                 outLen = 0;
206                 base64CRLLen = B64ENCODE_OUT_SAFESIZE(strlen(jsonObj->valuestring));
207                 base64Buff = OICMalloc(base64CRLLen);
208                 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
209                         base64CRLLen, &outLen);
210                 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= base64CRLLen),
211                                 ERROR);
212                 crl->CrlData.data = OICMalloc(outLen + 1);
213                 memcpy(crl->CrlData.data, base64Buff, outLen);
214                 crl->CrlData.len = outLen;
215                 OICFree(base64Buff);
216                 base64Buff = NULL;
217             }
218         }
219     }
220     else // PUT/POST JSON will not have CRLData so set it to the gCRList->CRLData
221     {
222         VERIFY_NON_NULL(TAG, gCrl, ERROR);
223         outLen = gCrl->CrlData.len;
224         crl->CrlData.data = OICMalloc(outLen + 1);
225         memcpy(crl->CrlData.data, gCrl->CrlData.data, outLen);
226         crl->CrlData.len = outLen;
227     }
228
229     ret = OC_STACK_OK;
230 exit:
231     cJSON_Delete(jsonRoot);
232     OICFree(base64Buff);
233     base64Buff = NULL;
234     if (OC_STACK_OK != ret)
235     {
236         DeleteCrlBinData(crl);
237         crl = NULL;
238     }
239     return crl;
240 }
241
242 OCStackResult UpdateCRLResource(const OicSecCrl_t *crl)
243 {
244     char *jsonStr = NULL;
245     OCStackResult res = OC_STACK_ERROR;
246
247     jsonStr = BinToCrlJSON((OicSecCrl_t *) crl);
248     if (!jsonStr)
249     {
250         return OC_STACK_ERROR;
251     }
252
253     cJSON *jsonObj = cJSON_Parse(jsonStr);
254     OICFree(jsonStr);
255
256     if (jsonObj == NULL)
257     {
258         return OC_STACK_ERROR;
259     }
260
261     res = UpdateSVRDatabase(OIC_JSON_CRL_NAME, jsonObj);
262     cJSON_Delete(jsonObj);
263
264     return res;
265 }
266
267 static OCEntityHandlerResult HandleCRLPostRequest(const OCEntityHandlerRequest *ehRequest)
268 {
269     OCEntityHandlerResult ehRet = OC_EH_ERROR;
270
271     char *jsonCRL = (char *)(((OCSecurityPayload *)ehRequest->payload)->securityData);
272
273     if (jsonCRL)
274     {
275         OC_LOG(INFO, TAG, "UpdateSVRDB...");
276         OC_LOG_V(INFO, TAG, "crl: \"%s\"", jsonCRL);
277
278         cJSON *jsonObj = cJSON_Parse(jsonCRL);
279         OicSecCrl_t *crl = NULL;
280         crl = JSONToCrlBin(jsonCRL);
281         if (!crl)
282         {
283             OC_LOG(ERROR, TAG, "Error JSONToCrlBin");
284         }
285
286         gCrl->CrlId = crl->CrlId;
287
288         OICFree(gCrl->ThisUpdate.data);
289         gCrl->ThisUpdate.data = NULL;
290         gCrl->ThisUpdate.data = OICMalloc(crl->ThisUpdate.len);
291         memcpy(gCrl->ThisUpdate.data, crl->ThisUpdate.data, crl->ThisUpdate.len);
292         gCrl->ThisUpdate.len = crl->ThisUpdate.len;
293
294         OICFree(gCrl->CrlData.data);
295         gCrl->CrlData.data = NULL;
296         gCrl->CrlData.data = OICMalloc(crl->CrlData.len);
297         memcpy(gCrl->CrlData.data, crl->CrlData.data, crl->CrlData.len);
298         gCrl->CrlData.len = crl->CrlData.len;
299
300         if (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_CRL_NAME, jsonObj))
301         {
302             OC_LOG(INFO, TAG, "UpdateSVRDB == OK");
303             ehRet = OC_EH_RESOURCE_CREATED;
304         }
305
306         DeleteCrlBinData(crl);
307         cJSON_Delete(jsonObj);
308
309     }
310
311     // Send payload to request originator
312     SendSRMResponse(ehRequest, ehRet, NULL);
313
314     OC_LOG_V(INFO, TAG, "%s RetVal %d", __func__, ehRet);
315     return ehRet;
316 }
317
318
319 /*
320  * This internal method is the entity handler for CRL resource and
321  * will handle REST request (GET/PUT/POST/DEL) for them.
322  */
323 OCEntityHandlerResult CRLEntityHandler(OCEntityHandlerFlag flag,
324                                        OCEntityHandlerRequest *ehRequest,
325                                        void *callbackParameter)
326 {
327     OCEntityHandlerResult ehRet = OC_EH_ERROR;
328     (void)callbackParameter;
329
330     if (!ehRequest)
331     {
332         return ehRet;
333     }
334
335     OC_LOG(INFO, TAG, "Handle CRL resource");
336
337     if (flag & OC_REQUEST_FLAG)
338     {
339         // TODO :  Handle PUT and DEL methods
340         OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
341         switch (ehRequest->method)
342         {
343             case OC_REST_GET:
344                 OC_LOG (INFO, TAG, "Not implemented request method.");
345                 //ehRet = HandleCRLGetRequest(ehRequest);
346                 break;
347
348             case OC_REST_POST:
349                 ehRet = HandleCRLPostRequest(ehRequest);
350                 break;
351
352             default:
353                 ehRet = OC_EH_ERROR;
354                 SendSRMResponse(ehRequest, ehRet, NULL);
355         }
356     }
357
358     return ehRet;
359 }
360
361 /*
362  * This internal method is used to create '/oic/sec/crl' resource.
363  */
364 OCStackResult CreateCRLResource()
365 {
366     OCStackResult ret;
367     ret = OCCreateResource(&gCrlHandle,
368                            OIC_RSRC_TYPE_SEC_CRL,
369                            OIC_MI_DEF,
370                            OIC_RSRC_CRL_URI,
371                            CRLEntityHandler,
372                            NULL,
373                            OC_OBSERVABLE);
374
375     if (OC_STACK_OK != ret)
376     {
377         OC_LOG(FATAL, TAG, "Unable to instantiate CRL resource");
378         DeInitCRLResource();
379     }
380     return ret;
381 }
382
383 /**
384  * Get the default value
385  * @retval  NULL for now. Update it when we finalize the default info.
386  */
387 static OicSecCrl_t *GetCrlDefault()
388 {
389     OicSecCrl_t *defaultCrl = NULL;
390     defaultCrl = (OicSecCrl_t *)OICCalloc(1, sizeof(OicSecCrl_t));
391
392     defaultCrl->CrlId = CRL_DEFAULT_CRL_ID;
393
394     defaultCrl->CrlData.len = strlen(CRL_DEFAULT_CRL_DATA);
395     defaultCrl->CrlData.data = OICMalloc(defaultCrl->CrlData.len);
396     memcpy(defaultCrl->CrlData.data, CRL_DEFAULT_CRL_DATA, defaultCrl->CrlData.len);
397
398     defaultCrl->ThisUpdate.len = strlen(CRL_DEFAULT_THIS_UPDATE);
399     defaultCrl->ThisUpdate.data = OICMalloc(defaultCrl->ThisUpdate.len);
400     memcpy(defaultCrl->ThisUpdate.data, CRL_DEFAULT_THIS_UPDATE, defaultCrl->ThisUpdate.len);
401
402     return defaultCrl;
403 }
404
405 /**
406  * Initialize CRL resource by loading data from persistent storage.
407  *
408  * @retval
409  *     OC_STACK_OK    - no errors
410  *     OC_STACK_ERROR - stack process error
411  */
412 OCStackResult InitCRLResource()
413 {
414     OCStackResult ret = OC_STACK_ERROR;
415     char* jsonSVRDatabase;
416
417     //Read CRL resource from PS
418     jsonSVRDatabase = GetSVRDatabase();
419
420     if (jsonSVRDatabase)
421     {
422         //Convert JSON CRL into binary format
423         gCrl = JSONToCrlBin(jsonSVRDatabase);
424     }
425     /*
426      * If SVR database in persistent storage got corrupted or
427      * is not available for some reason, a default CrlResource is created
428      * which allows user to initiate CrlResource provisioning again.
429      */
430     if (!jsonSVRDatabase || !gCrl)
431     {
432         gCrl = GetCrlDefault();
433     }
434
435     ret = CreateCRLResource();
436     OICFree(jsonSVRDatabase);
437     return ret;
438 }
439
440 /**
441  * Perform cleanup for ACL resources.
442  */
443 void DeInitCRLResource()
444 {
445     OCDeleteResource(gCrlHandle);
446     gCrlHandle = NULL;
447     DeleteCrlBinData(gCrl);
448     gCrl = NULL;
449 }
450
451 OicSecCrl_t *GetCRLResource()
452 {
453     OicSecCrl_t *crl =  NULL;
454
455     //Read CRL resource from PS
456     char* jsonSVRDatabase = GetSVRDatabase();
457
458     if (jsonSVRDatabase)
459     {
460         //Convert JSON CRL into binary format
461         crl = JSONToCrlBin(jsonSVRDatabase);
462     }
463     /*
464      * If SVR database in persistent storage got corrupted or
465      * is not available for some reason, a default CrlResource is created
466      * which allows user to initiate CrlResource provisioning again.
467      */
468     if (!jsonSVRDatabase || !crl)
469     {
470         crl = GetCrlDefault();
471     }
472     OICFree(jsonSVRDatabase);
473
474     return crl;
475 }
476
477 char *GetBase64CRL()
478 {
479     cJSON *jsonCrl = NULL;
480     cJSON *jsonObj = NULL;
481     char *jsonSVRDatabase = GetSVRDatabase();
482     char* ret = NULL;
483
484     cJSON *jsonRoot = cJSON_Parse(jsonSVRDatabase);
485     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
486
487     jsonCrl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRL_NAME);
488     VERIFY_NON_NULL(TAG, jsonCrl, ERROR);
489
490     //CRLData -- Mandatory
491     jsonObj = cJSON_GetObjectItem(jsonCrl, OIC_JSON_CRL_DATA);
492     if(jsonObj)
493     {
494         VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
495         if(cJSON_String == jsonObj->type)
496         {
497             //Check for empty string, in case CRLData field has not been set yet
498             if (jsonObj->valuestring[0])
499             {
500                 ret = jsonObj->valuestring;
501             }
502         }
503     }
504 exit:
505     OICFree(jsonSVRDatabase);
506     cJSON_Delete(jsonRoot);
507     return ret;
508 }
509
510 void  GetDerCrl(ByteArray crlArray)
511 {
512     OicSecCrl_t * crlRes = GetCRLResource();
513     if (crlRes && crlRes->CrlData.len <= crlArray.len)
514     {
515         memcpy(crlArray.data, crlRes->CrlData.data, crlRes->CrlData.len);
516         crlArray.len = crlRes->CrlData.len;
517     }
518     else
519     {
520         crlArray.len = 0;
521     }
522     DeleteCrlBinData(crlRes);
523 }