1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 #include "oic_malloc.h"
28 #include "resourcemanager.h"
29 #include "psinterface.h"
31 #include "srmresourcestrings.h"
32 #include "doxmresource.h"
33 #include "srmutility.h"
35 #include "crlresource.h"
37 #endif /* __WITH_X509__ */
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 "-"
53 static OCResourceHandle gCrlHandle = NULL;
54 static OicSecCrl_t *gCrl = NULL;
57 void DeleteCrlBinData(OicSecCrl_t *crl)
62 OICFree(crl->ThisUpdate.data);
65 OICFree(crl->CrlData.data);
72 char *BinToCrlJSON(const OicSecCrl_t *crl)
79 char *base64Buff = NULL;
81 uint32_t base64CRLLen = 0;
82 B64Result b64Ret = B64_OK;
84 cJSON *jsonRoot = cJSON_CreateObject();
85 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
86 cJSON *jsonCrl = cJSON_CreateObject();
87 VERIFY_NON_NULL(TAG, jsonCrl, ERROR);
89 cJSON_AddItemToObject(jsonRoot, OIC_JSON_CRL_NAME, jsonCrl);
92 cJSON_AddNumberToObject(jsonCrl, OIC_JSON_CRL_ID, (int)crl->CrlId);
94 //ThisUpdate -- Mandatory
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);
104 //CRLData -- Mandatory
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);
113 jsonStr = cJSON_PrintUnformatted(jsonRoot);
119 cJSON_Delete(jsonRoot);
124 OicSecCrl_t *JSONToCrlBin(const char * jsonStr)
131 OCStackResult ret = OC_STACK_ERROR;
132 OicSecCrl_t *crl = NULL;
133 cJSON *jsonCrl = NULL;
134 cJSON *jsonObj = NULL;
136 unsigned char *base64Buff = NULL;
137 uint32_t base64CRLLen = 0;
139 B64Result b64Ret = B64_OK;
141 cJSON *jsonRoot = cJSON_Parse(jsonStr);
142 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
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);
150 jsonObj = cJSON_GetObjectItem(jsonCrl, OIC_JSON_CRL_ID);
153 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
154 crl->CrlId = (uint16_t)jsonObj->valueint;
156 else // PUT/POST JSON may not have CRLId so set it to the gCRList->CRLId
158 VERIFY_NON_NULL(TAG, gCrl, ERROR);
159 crl->CrlId = gCrl->CrlId;
162 //ThisUpdate -- Mandatory
163 jsonObj = cJSON_GetObjectItem(jsonCrl, OIC_JSON_CRL_THIS_UPDATE);
166 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
167 if(cJSON_String == jsonObj->type)
169 //Check for empty string, in case ThisUpdate field has not been set yet
170 if (jsonObj->valuestring[0])
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),
178 crl->ThisUpdate.data = OICMalloc(outLen + 1);
179 memcpy(crl->ThisUpdate.data, base64Buff, outLen);
180 crl->ThisUpdate.len = outLen;
186 else // PUT/POST JSON will not have ThisUpdate so set it to the gCRList->ThisUpdate
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;
195 //CRLData -- Mandatory
196 jsonObj = cJSON_GetObjectItem(jsonCrl, OIC_JSON_CRL_DATA);
199 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
200 if(cJSON_String == jsonObj->type)
202 //Check for empty string, in case CRLData field has not been set yet
203 if (jsonObj->valuestring[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),
212 crl->CrlData.data = OICMalloc(outLen + 1);
213 memcpy(crl->CrlData.data, base64Buff, outLen);
214 crl->CrlData.len = outLen;
220 else // PUT/POST JSON will not have CRLData so set it to the gCRList->CRLData
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;
231 cJSON_Delete(jsonRoot);
234 if (OC_STACK_OK != ret)
236 DeleteCrlBinData(crl);
242 OCStackResult UpdateCRLResource(const OicSecCrl_t *crl)
244 char *jsonStr = NULL;
245 OCStackResult res = OC_STACK_ERROR;
247 jsonStr = BinToCrlJSON((OicSecCrl_t *) crl);
250 return OC_STACK_ERROR;
253 cJSON *jsonObj = cJSON_Parse(jsonStr);
258 return OC_STACK_ERROR;
261 res = UpdateSVRDatabase(OIC_JSON_CRL_NAME, jsonObj);
262 cJSON_Delete(jsonObj);
267 static OCEntityHandlerResult HandleCRLPostRequest(const OCEntityHandlerRequest *ehRequest)
269 OCEntityHandlerResult ehRet = OC_EH_ERROR;
271 char *jsonCRL = (char *)(((OCSecurityPayload *)ehRequest->payload)->securityData);
275 OC_LOG(INFO, TAG, "UpdateSVRDB...");
276 OC_LOG_V(INFO, TAG, "crl: \"%s\"", jsonCRL);
278 cJSON *jsonObj = cJSON_Parse(jsonCRL);
279 OicSecCrl_t *crl = NULL;
280 crl = JSONToCrlBin(jsonCRL);
283 OC_LOG(ERROR, TAG, "Error JSONToCrlBin");
286 gCrl->CrlId = crl->CrlId;
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;
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;
300 if (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_CRL_NAME, jsonObj))
302 OC_LOG(INFO, TAG, "UpdateSVRDB == OK");
303 ehRet = OC_EH_RESOURCE_CREATED;
306 DeleteCrlBinData(crl);
307 cJSON_Delete(jsonObj);
311 // Send payload to request originator
312 SendSRMResponse(ehRequest, ehRet, NULL);
314 OC_LOG_V(INFO, TAG, "%s RetVal %d", __func__, ehRet);
320 * This internal method is the entity handler for CRL resource and
321 * will handle REST request (GET/PUT/POST/DEL) for them.
323 OCEntityHandlerResult CRLEntityHandler(OCEntityHandlerFlag flag,
324 OCEntityHandlerRequest *ehRequest,
325 void *callbackParameter)
327 OCEntityHandlerResult ehRet = OC_EH_ERROR;
328 (void)callbackParameter;
335 OC_LOG(INFO, TAG, "Handle CRL resource");
337 if (flag & OC_REQUEST_FLAG)
339 // TODO : Handle PUT and DEL methods
340 OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
341 switch (ehRequest->method)
344 OC_LOG (INFO, TAG, "Not implemented request method.");
345 //ehRet = HandleCRLGetRequest(ehRequest);
349 ehRet = HandleCRLPostRequest(ehRequest);
354 SendSRMResponse(ehRequest, ehRet, NULL);
362 * This internal method is used to create '/oic/sec/crl' resource.
364 OCStackResult CreateCRLResource()
367 ret = OCCreateResource(&gCrlHandle,
368 OIC_RSRC_TYPE_SEC_CRL,
375 if (OC_STACK_OK != ret)
377 OC_LOG(FATAL, TAG, "Unable to instantiate CRL resource");
384 * Get the default value
385 * @retval NULL for now. Update it when we finalize the default info.
387 static OicSecCrl_t *GetCrlDefault()
389 OicSecCrl_t *defaultCrl = NULL;
390 defaultCrl = (OicSecCrl_t *)OICCalloc(1, sizeof(OicSecCrl_t));
392 defaultCrl->CrlId = CRL_DEFAULT_CRL_ID;
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);
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);
406 * Initialize CRL resource by loading data from persistent storage.
409 * OC_STACK_OK - no errors
410 * OC_STACK_ERROR - stack process error
412 OCStackResult InitCRLResource()
414 OCStackResult ret = OC_STACK_ERROR;
415 char* jsonSVRDatabase;
417 //Read CRL resource from PS
418 jsonSVRDatabase = GetSVRDatabase();
422 //Convert JSON CRL into binary format
423 gCrl = JSONToCrlBin(jsonSVRDatabase);
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.
430 if (!jsonSVRDatabase || !gCrl)
432 gCrl = GetCrlDefault();
435 ret = CreateCRLResource();
436 OICFree(jsonSVRDatabase);
441 * Perform cleanup for ACL resources.
443 void DeInitCRLResource()
445 OCDeleteResource(gCrlHandle);
447 DeleteCrlBinData(gCrl);
451 OicSecCrl_t *GetCRLResource()
453 OicSecCrl_t *crl = NULL;
455 //Read CRL resource from PS
456 char* jsonSVRDatabase = GetSVRDatabase();
460 //Convert JSON CRL into binary format
461 crl = JSONToCrlBin(jsonSVRDatabase);
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.
468 if (!jsonSVRDatabase || !crl)
470 crl = GetCrlDefault();
472 OICFree(jsonSVRDatabase);
479 cJSON *jsonCrl = NULL;
480 cJSON *jsonObj = NULL;
481 char *jsonSVRDatabase = GetSVRDatabase();
484 cJSON *jsonRoot = cJSON_Parse(jsonSVRDatabase);
485 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
487 jsonCrl = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRL_NAME);
488 VERIFY_NON_NULL(TAG, jsonCrl, ERROR);
490 //CRLData -- Mandatory
491 jsonObj = cJSON_GetObjectItem(jsonCrl, OIC_JSON_CRL_DATA);
494 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
495 if(cJSON_String == jsonObj->type)
497 //Check for empty string, in case CRLData field has not been set yet
498 if (jsonObj->valuestring[0])
500 ret = jsonObj->valuestring;
505 OICFree(jsonSVRDatabase);
506 cJSON_Delete(jsonRoot);
510 void GetDerCrl(ByteArray crlArray)
512 OicSecCrl_t * crlRes = GetCRLResource();
513 if (crlRes && crlRes->CrlData.len <= crlArray.len)
515 memcpy(crlArray.data, crlRes->CrlData.data, crlRes->CrlData.len);
516 crlArray.len = crlRes->CrlData.len;
522 DeleteCrlBinData(crlRes);