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 "doxmresource.h"
27 #include "psinterface.h"
29 #include "srmresourcestrings.h"
30 #include "securevirtualresourcetypes.h"
33 #include "cainterface.h"
34 #include "credresource.h"
35 #include "ocserverrequest.h"
36 #include "srmutility.h"
37 #include "pinoxmcommon.h"
50 #define TAG PCF("SRM-DOXM")
52 static OicSecDoxm_t *gDoxm = NULL;
53 static OCResourceHandle gDoxmHandle = NULL;
55 static OicSecOxm_t gOicSecDoxmJustWorks = OIC_JUST_WORKS;
56 static OicSecDoxm_t gDefaultDoxm =
58 NULL, /* OicUrn_t *oxmType */
59 0, /* size_t oxmTypeLen */
60 &gOicSecDoxmJustWorks, /* uint16_t *oxm */
61 1, /* size_t oxmLen */
62 OIC_JUST_WORKS, /* uint16_t oxmSel */
63 false, /* bool owned */
64 {.id = {0}}, /* OicUuid_t deviceID */
65 {.id = {0}}, /* OicUuid_t owner */
68 void DeleteDoxmBinData(OicSecDoxm_t* doxm)
73 for (size_t i = 0; i < doxm->oxmTypeLen; i++)
75 OICFree(doxm->oxmType[i]);
77 OICFree(doxm->oxmType);
87 char * BinToDoxmJSON(const OicSecDoxm_t * doxm)
95 cJSON *jsonDoxm = NULL;
96 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {};
98 B64Result b64Ret = B64_OK;
100 cJSON *jsonRoot = cJSON_CreateObject();
101 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
103 jsonDoxm = cJSON_CreateObject();
104 VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
105 cJSON_AddItemToObject(jsonRoot, OIC_JSON_DOXM_NAME, jsonDoxm );
107 //OxmType -- Not Mandatory
108 if(doxm->oxmTypeLen > 0)
110 cJSON *jsonOxmTyArray = cJSON_CreateArray();
111 VERIFY_NON_NULL(TAG, jsonOxmTyArray, ERROR);
112 cJSON_AddItemToObject (jsonDoxm, OIC_JSON_OXM_TYPE_NAME, jsonOxmTyArray );
113 for (size_t i = 0; i < doxm->oxmTypeLen; i++)
115 cJSON_AddItemToArray (jsonOxmTyArray, cJSON_CreateString(doxm->oxmType[i]));
119 //Oxm -- Not Mandatory
122 cJSON *jsonOxmArray = cJSON_CreateArray();
123 VERIFY_NON_NULL(TAG, jsonOxmArray, ERROR);
124 cJSON_AddItemToObject (jsonDoxm, OIC_JSON_OXM_NAME,jsonOxmArray );
125 for (size_t i = 0; i < doxm->oxmLen; i++)
127 cJSON_AddItemToArray (jsonOxmArray, cJSON_CreateNumber(doxm->oxm[i]));
131 //OxmSel -- Mandatory
132 cJSON_AddNumberToObject(jsonDoxm, OIC_JSON_OXM_SEL_NAME, (int)doxm->oxmSel);
135 cJSON_AddBoolToObject(jsonDoxm, OIC_JSON_OWNED_NAME, doxm->owned);
137 //TODO: Need more clarification on deviceIDFormat field type.
139 //DeviceIdFormat -- Mandatory
140 cJSON_AddNumberToObject(jsonDoxm, OIC_JSON_DEVICE_ID_FORMAT_NAME, doxm->deviceIDFormat);
143 //DeviceId -- Mandatory
145 b64Ret = b64Encode(doxm->deviceID.id, sizeof(doxm->deviceID.id), base64Buff,
146 sizeof(base64Buff), &outLen);
147 VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
148 cJSON_AddStringToObject(jsonDoxm, OIC_JSON_DEVICE_ID_NAME, base64Buff);
152 b64Ret = b64Encode(doxm->owner.id, sizeof(doxm->owner.id), base64Buff,
153 sizeof(base64Buff), &outLen);
154 VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
155 cJSON_AddStringToObject(jsonDoxm, OIC_JSON_OWNER_NAME, base64Buff);
157 jsonStr = cJSON_PrintUnformatted(jsonRoot);
162 cJSON_Delete(jsonRoot);
167 OicSecDoxm_t * JSONToDoxmBin(const char * jsonStr)
175 OCStackResult ret = OC_STACK_ERROR;
176 OicSecDoxm_t *doxm = NULL;
177 cJSON *jsonDoxm = NULL;
178 cJSON *jsonObj = NULL;
180 size_t jsonObjLen = 0;
181 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
183 B64Result b64Ret = B64_OK;
185 cJSON *jsonRoot = cJSON_Parse(jsonStr);
186 VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
188 jsonDoxm = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
189 VERIFY_NON_NULL(TAG, jsonDoxm, ERROR);
191 doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
192 VERIFY_NON_NULL(TAG, doxm, ERROR);
194 //OxmType -- not Mandatory
195 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_TYPE_NAME);
196 if ((jsonObj) && (cJSON_Array == jsonObj->type))
198 doxm->oxmTypeLen = cJSON_GetArraySize(jsonObj);
199 VERIFY_SUCCESS(TAG, doxm->oxmTypeLen > 0, ERROR);
201 doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(char *));
202 VERIFY_NON_NULL(TAG, (doxm->oxmType), ERROR);
204 for (size_t i = 0; i < doxm->oxmTypeLen ; i++)
206 cJSON *jsonOxmTy = cJSON_GetArrayItem(jsonObj, i);
207 VERIFY_NON_NULL(TAG, jsonOxmTy, ERROR);
209 jsonObjLen = strlen(jsonOxmTy->valuestring) + 1;
210 doxm->oxmType[i] = (char*)OICMalloc(jsonObjLen);
211 VERIFY_NON_NULL(TAG, doxm->oxmType[i], ERROR);
212 strncpy((char *)doxm->oxmType[i], (char *)jsonOxmTy->valuestring, jsonObjLen);
216 //Oxm -- not Mandatory
217 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_NAME);
218 if (jsonObj && cJSON_Array == jsonObj->type)
220 doxm->oxmLen = cJSON_GetArraySize(jsonObj);
221 VERIFY_SUCCESS(TAG, doxm->oxmLen > 0, ERROR);
223 doxm->oxm = (OicSecOxm_t*)OICCalloc(doxm->oxmLen, sizeof(OicSecOxm_t));
224 VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
226 for (size_t i = 0; i < doxm->oxmLen ; i++)
228 cJSON *jsonOxm = cJSON_GetArrayItem(jsonObj, i);
229 VERIFY_NON_NULL(TAG, jsonOxm, ERROR);
230 doxm->oxm[i] = (OicSecOxm_t)jsonOxm->valueint;
234 //OxmSel -- Mandatory
235 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OXM_SEL_NAME);
238 VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
239 doxm->oxmSel = (OicSecOxm_t)jsonObj->valueint;
241 else // PUT/POST JSON may not have oxmsel so set it to the gDoxm->oxmSel
243 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
244 doxm->oxmSel = gDoxm->oxmSel;
248 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNED_NAME);
251 VERIFY_SUCCESS(TAG, (cJSON_True == jsonObj->type || cJSON_False == jsonObj->type), ERROR);
252 doxm->owned = jsonObj->valueint;
254 else // PUT/POST JSON may not have owned so set it to the gDomx->owned
256 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
257 doxm->owned = gDoxm->owned;
260 //DeviceId -- Mandatory
261 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_DEVICE_ID_NAME);
264 VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
265 if(cJSON_String == jsonObj->type)
267 //Check for empty string, in case DeviceId field has not been set yet
268 if (jsonObj->valuestring[0])
271 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
272 sizeof(base64Buff), &outLen);
273 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(doxm->deviceID.id)),
275 memcpy(doxm->deviceID.id, base64Buff, outLen);
279 else // PUT/POST JSON will not have deviceID so set it to the gDoxm->deviceID.id
281 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
282 memcpy((char *)doxm->deviceID.id, (char *)gDoxm->deviceID.id, sizeof(doxm->deviceID.id));
285 //Owner -- will be empty when device status is unowned.
286 jsonObj = cJSON_GetObjectItem(jsonDoxm, OIC_JSON_OWNER_NAME);
287 if(true == doxm->owned)
289 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
293 VERIFY_SUCCESS(TAG, (cJSON_String == jsonObj->type), ERROR);
295 b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
296 sizeof(base64Buff), &outLen);
297 VERIFY_SUCCESS(TAG, ((b64Ret == B64_OK) && (outLen <= sizeof(doxm->owner.id))), ERROR);
298 memcpy(doxm->owner.id, base64Buff, outLen);
304 cJSON_Delete(jsonRoot);
305 if (OC_STACK_OK != ret)
307 DeleteDoxmBinData(doxm);
315 * @todo document this function including why code might need to call this.
316 * The current suspicion is that it's not being called as much as it should.
318 static bool UpdatePersistentStorage(OicSecDoxm_t * doxm)
324 // Convert Doxm data into JSON for update to persistent storage
325 char *jsonStr = BinToDoxmJSON(doxm);
328 cJSON *jsonDoxm = cJSON_Parse(jsonStr);
332 (OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_DOXM_NAME, jsonDoxm)))
336 cJSON_Delete(jsonDoxm);
343 static bool ValidateQuery(unsigned char * query)
345 // Send doxm resource data if the state of doxm resource
346 // matches with the query parameters.
347 // else send doxm resource data as NULL
348 // TODO Remove this check and rely on Policy Engine
349 // and Provisioning Mode to enforce provisioning-state
350 // access rules. Eventually, the PE and PM code will
351 // not send a request to the /doxm Entity Handler at all
352 // if it should not respond.
353 OC_LOG (INFO, TAG, PCF("In ValidateQuery"));
359 OicParseQueryIter_t parseIter = {.attrPos = NULL};
361 ParseQueryIterInit(query, &parseIter);
363 while(GetNextQuery(&parseIter))
365 if(strncasecmp((char *)parseIter.attrPos, OIC_JSON_OWNED_NAME, parseIter.attrLen) == 0)
367 if((strncasecmp((char *)parseIter.valPos, OIC_SEC_TRUE, parseIter.valLen) == 0) &&
372 else if((strncasecmp((char *)parseIter.valPos, OIC_SEC_FALSE, parseIter.valLen) == 0)
382 static OCEntityHandlerResult HandleDoxmGetRequest (const OCEntityHandlerRequest * ehRequest)
384 char* jsonStr = NULL;
385 OCEntityHandlerResult ehRet = OC_EH_OK;
387 OC_LOG (INFO, TAG, PCF("Doxm EntityHandle processing GET request"));
389 //Checking if Get request is a query.
392 OC_LOG (INFO, TAG, PCF("HandleDoxmGetRequest processing query"));
393 if(!ValidateQuery((unsigned char *)ehRequest->query))
400 * For GET or Valid Query request return doxm resource json payload.
401 * For non-valid query return NULL json payload.
402 * A device will 'always' have a default Doxm, so BinToDoxmJSON will
403 * return valid doxm resource json.
406 jsonStr = (ehRet == OC_EH_OK) ? BinToDoxmJSON(gDoxm) : NULL;
408 // Send response payload to request originator
409 if(OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, jsonStr))
411 OC_LOG (ERROR, TAG, PCF("SendSRMResponse failed in HandleDoxmGetRequest"));
421 * Generating new credential for provisioning tool
425 static OCEntityHandlerResult AddOwnerPSK(const CAEndpoint_t* endpoint,
426 OicSecDoxm_t* ptDoxm,
427 const uint8_t* label, const size_t labelLen)
431 OicSecCred_t *cred = NULL;
432 uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {};
434 CAResult_t pskRet = CAGenerateOwnerPSK(endpoint,
436 ptDoxm->owner.id, sizeof(ptDoxm->owner.id),
437 gDoxm->deviceID.id, sizeof(gDoxm->deviceID.id),
438 ownerPSK, OWNER_PSK_LENGTH_128);
440 VERIFY_SUCCESS(TAG, pskRet == CA_STATUS_OK, ERROR);
442 char base64Buff[B64ENCODE_OUT_SAFESIZE(OWNER_PSK_LENGTH_128) + 1] = {};
443 B64Result b64Ret = b64Encode(ownerPSK, OWNER_PSK_LENGTH_128, base64Buff,
444 sizeof(base64Buff), &outLen);
445 VERIFY_SUCCESS(TAG, b64Ret == B64_OK, ERROR);
447 OC_LOG (INFO, TAG, PCF("Doxm EntityHandle generating Credential"));
448 cred = GenerateCredential(&ptDoxm->owner, SYMMETRIC_PAIR_WISE_KEY,
449 NULL, base64Buff, ownLen, &ptDoxm->owner);
450 VERIFY_NON_NULL(TAG, cred, ERROR);
452 //Adding provisioning tool credential to cred Resource.
453 VERIFY_SUCCESS(TAG, OC_STACK_OK == AddCredential(cred), ERROR);
456 gDoxm->oxmSel = ptDoxm->oxmSel;
457 memcpy(&(gDoxm->owner), &(ptDoxm->owner), sizeof(OicUuid_t));
464 #endif //__WITH_DTLS__
466 static OCEntityHandlerResult HandleDoxmPutRequest (const OCEntityHandlerRequest * ehRequest)
468 OC_LOG (INFO, TAG, PCF("Doxm EntityHandle processing PUT request"));
469 OCEntityHandlerResult ehRet = OC_EH_ERROR;
470 OicUuid_t emptyOwner = {.id = {0}};
473 * Convert JSON Doxm data into binary. This will also validate
474 * the Doxm data received.
476 OicSecDoxm_t* newDoxm = JSONToDoxmBin(((OCSecurityPayload*)ehRequest->payload)->securityData);
480 // Iotivity SRM ONLY supports OIC_JUST_WORKS now
481 if (OIC_JUST_WORKS == newDoxm->oxmSel)
484 * If current state of the device is un-owned, enable
485 * anonymous ECDH cipher in tinyDTLS so that Provisioning
486 * tool can initiate JUST_WORKS ownership transfer process.
488 if ((false == gDoxm->owned) && (false == newDoxm->owned))
490 OC_LOG (INFO, TAG, PCF("Doxm EntityHandle enabling AnonECDHCipherSuite"));
492 ehRet = (CAEnableAnonECDHCipherSuite(true) == CA_STATUS_OK) ? OC_EH_OK : OC_EH_ERROR;
493 #endif //__WITH_DTLS__
498 * When current state of the device is un-owned and Provisioning
499 * Tool is attempting to change the state to 'Owned' with a
500 * qualified value for the field 'Owner'
502 if ((false == gDoxm->owned) && (true == newDoxm->owned) &&
503 (memcmp(&(newDoxm->owner), &emptyOwner, sizeof(OicUuid_t)) != 0))
506 * Generate OwnerPSK and create credential for Provisioning
507 * tool with the generated OwnerPSK.
508 * Update persistent storage and disable anonymous ECDH cipher
512 OCServerRequest *request = (OCServerRequest *)ehRequest->requestHandle;
514 //Generating OwnerPSK
515 OC_LOG (INFO, TAG, PCF("Doxm EntityHandle generating OwnerPSK"));
517 //Generate new credential for provisioning tool
518 ehRet = AddOwnerPSK((CAEndpoint_t *)&request->devAddr, newDoxm,
519 (uint8_t*) OXM_JUST_WORKS, strlen(OXM_JUST_WORKS));
521 VERIFY_SUCCESS(TAG, ehRet == OC_EH_OK, ERROR);
523 // Update new state in persistent storage
524 if (true == UpdatePersistentStorage(gDoxm))
533 * If persistent storage update failed, revert back the state
534 * for global variable.
536 gDoxm->owned = false;
538 memset(&(gDoxm->owner), 0, sizeof(OicUuid_t));
542 * Disable anonymous ECDH cipher in tinyDTLS since device is now
545 CAEnableAnonECDHCipherSuite(false);
547 #define TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 0xC0AE
548 CASelectCipherSuite(TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
549 #endif //__WITH_X509__
550 #endif //__WITH_DTLS__
553 else if(OIC_RANDOM_DEVICE_PIN == newDoxm->oxmSel)
555 //this temp Credential ID is used to track temporal Cred Id
556 static OicUuid_t tmpCredId = {.id={0}};
557 static bool tmpCredGenFlag = false;
559 if ((false == gDoxm->owned) && (false == newDoxm->owned))
562 CAEnableAnonECDHCipherSuite(false);
563 OC_LOG(DEBUG, TAG, "ECDH_ANON CipherSuite is DISABLED");
564 CASelectCipherSuite(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256);
566 char ranPin[OXM_RANDOM_PIN_SIZE + 1] = {0,};
567 if(OC_STACK_OK == GeneratePin(ranPin, OXM_RANDOM_PIN_SIZE + 1))
571 OC_LOG(DEBUG, TAG, "Corrupted PSK is detected!!!");
573 OC_STACK_RESOURCE_DELETED == RemoveCredential(&tmpCredId),
577 OCStackResult res = AddTmpPskWithPIN( &(newDoxm->owner), SYMMETRIC_PAIR_WISE_KEY,
578 ranPin, OXM_RANDOM_PIN_SIZE, 1, &(newDoxm->owner), &tmpCredId);
579 VERIFY_SUCCESS(TAG, res == OC_STACK_OK, ERROR);
580 tmpCredGenFlag = true;
585 OC_LOG(ERROR, TAG, "Failed to generate random PIN");
589 #endif //__WITH_DTLS__
593 * When current state of the device is un-owned and Provisioning
594 * Tool is attempting to change the state to 'Owned' with a
595 * qualified value for the field 'Owner'
597 if ((false == gDoxm->owned) && (true == newDoxm->owned) &&
598 (memcmp(&(newDoxm->owner), &emptyOwner, sizeof(OicUuid_t)) != 0))
601 OCServerRequest * request = (OCServerRequest *)ehRequest->requestHandle;
603 //Remove Temporal Credential resource
607 OC_STACK_RESOURCE_DELETED == RemoveCredential(&tmpCredId),
609 tmpCredGenFlag = false;
612 //Generate new credential for provisioning tool
613 ehRet = AddOwnerPSK((CAEndpoint_t*)(&request->devAddr), newDoxm,
614 (uint8_t*)OXM_RANDOM_DEVICE_PIN, strlen(OXM_RANDOM_DEVICE_PIN));
615 VERIFY_SUCCESS(TAG, OC_EH_OK == ehRet, ERROR);
617 //Update new state in persistent storage
618 if((UpdatePersistentStorage(gDoxm) == true))
625 * If persistent storage update failed, revert back the state
626 * for global variable.
628 gDoxm->owned = false;
630 memset(&(gDoxm->owner), 0, sizeof(OicUuid_t));
641 //Send payload to request originator
642 if(OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL))
644 OC_LOG (ERROR, TAG, PCF("SendSRMResponse failed in HandlePstatPostRequest"));
646 DeleteDoxmBinData(newDoxm);
652 * This internal method is the entity handler for DOXM resources.
654 OCEntityHandlerResult DoxmEntityHandler (OCEntityHandlerFlag flag,
655 OCEntityHandlerRequest * ehRequest,
659 OCEntityHandlerResult ehRet = OC_EH_ERROR;
661 if(NULL == ehRequest)
667 if (flag & OC_REQUEST_FLAG)
669 OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG"));
670 switch (ehRequest->method)
673 ehRet = HandleDoxmGetRequest(ehRequest);
677 ehRet = HandleDoxmPutRequest(ehRequest);
682 SendSRMResponse(ehRequest, ehRet, NULL);
691 * This internal method is used to create '/oic/sec/doxm' resource.
693 OCStackResult CreateDoxmResource()
697 ret = OCCreateResource(&gDoxmHandle,
698 OIC_RSRC_TYPE_SEC_DOXM,
703 OC_OBSERVABLE | OC_SECURE | OC_EXPLICIT_DISCOVERABLE);
705 if (OC_STACK_OK != ret)
707 OC_LOG (FATAL, TAG, PCF("Unable to instantiate Doxm resource"));
708 DeInitDoxmResource();
714 * Checks if DeviceID is generated during provisioning for the new device.
715 * If DeviceID is NULL then generates the new DeviceID.
716 * Once DeviceID is assigned to the device it does not change for the lifetime of the device.
719 static OCStackResult CheckDeviceID()
721 OCStackResult ret = OC_STACK_ERROR;
722 bool validId = false;
723 for (uint8_t i = 0; i < UUID_LENGTH; i++)
725 if (gDoxm->deviceID.id[i] != 0)
734 if (OCGenerateUuid(gDoxm->deviceID.id) != RAND_UUID_OK)
736 OC_LOG(FATAL, TAG, PCF("Generate UUID for Server Instance failed!"));
741 if (UpdatePersistentStorage(gDoxm))
743 //TODO: After registering PSI handler in all samples, do ret = OC_STACK_OK here.
744 OC_LOG(FATAL, TAG, PCF("UpdatePersistentStorage failed!"));
755 * Get the default value.
756 * @retval the gDefaultDoxm pointer;
758 static OicSecDoxm_t* GetDoxmDefault()
760 OC_LOG (INFO, TAG, PCF("GetDoxmToDefault"));
761 return &gDefaultDoxm;
765 * This method is used by SRM to retrieve DOXM resource data.
767 * @retval reference to @ref OicSecDoxm_t, binary format of Doxm resource data
769 const OicSecDoxm_t* GetDoxmResourceData()
775 * Initialize DOXM resource by loading data from persistent storage.
777 * @retval OC_STACK_OK for Success, otherwise some error value
779 OCStackResult InitDoxmResource()
781 OCStackResult ret = OC_STACK_ERROR;
783 //Read DOXM resource from PS
784 char* jsonSVRDatabase = GetSVRDatabase();
787 //Convert JSON DOXM into binary format
788 gDoxm = JSONToDoxmBin(jsonSVRDatabase);
791 * If SVR database in persistent storage got corrupted or
792 * is not available for some reason, a default doxm is created
793 * which allows user to initiate doxm provisioning again.
795 if(!jsonSVRDatabase || !gDoxm)
797 gDoxm = GetDoxmDefault();
799 ret = CheckDeviceID();
800 if (ret == OC_STACK_OK)
802 //Instantiate 'oic.sec.doxm'
803 ret = CreateDoxmResource();
807 OC_LOG (ERROR, TAG, PCF("CheckDeviceID failed"));
809 OICFree(jsonSVRDatabase);
814 * Perform cleanup for DOXM resources.
817 * OC_STACK_OK - no error
818 * OC_STACK_ERROR - stack process error
821 OCStackResult DeInitDoxmResource()
823 OCStackResult ret = OCDeleteResource(gDoxmHandle);
824 if(gDoxm != &gDefaultDoxm)
826 DeleteDoxmBinData(gDoxm);
830 if(OC_STACK_OK == ret)
836 return OC_STACK_ERROR;
842 * This method returns the SRM device ID for this device.
844 * @retval OC_STACK_OK for Success, otherwise some error value
846 OCStackResult GetDoxmDeviceID(OicUuid_t *deviceID)
848 if(deviceID && gDoxm)
850 *deviceID = gDoxm->deviceID;
853 return OC_STACK_ERROR;
857 * @brief Gets the OicUuid_t value for the owner of this device.
859 * @return OC_STACK_OK if devOwner is a valid UUID, otherwise OC_STACK_ERROR.
861 OCStackResult GetDoxmDevOwnerId(OicUuid_t *devOwner)
863 OCStackResult retVal = OC_STACK_ERROR;
867 *devOwner = gDoxm->owner; // TODO change to devOwner when available
868 retVal = OC_STACK_OK;