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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
32 #include "oic_malloc.h"
33 #include "payload_logging.h"
36 #include "ocpayload.h"
37 #include "cainterface.h"
38 #include "ocserverrequest.h"
39 #include "resourcemanager.h"
40 #include "doxmresource.h"
41 #include "pstatresource.h"
42 #include "aclresource.h"
43 #include "amaclresource.h"
44 #include "pconfresource.h"
45 #include "dpairingresource.h"
46 #include "psinterface.h"
47 #include "srmresourcestrings.h"
48 #include "securevirtualresourcetypes.h"
49 #include "credresource.h"
50 #include "srmutility.h"
51 #include "pinoxmcommon.h"
53 #define TAG "SRM-DOXM"
55 /** Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
56 * The value of payload size is increased until reaching belox max cbor size. */
57 static const uint16_t CBOR_SIZE = 512;
59 /** Max cbor size payload. */
60 static const uint16_t CBOR_MAX_SIZE = 4400;
62 /** DOXM Map size - Number of mandatory items. */
63 static const uint8_t DOXM_MAP_SIZE = 7;
65 static OicSecDoxm_t *gDoxm = NULL;
66 static OCResourceHandle gDoxmHandle = NULL;
68 static OicSecOxm_t gOicSecDoxmJustWorks = OIC_JUST_WORKS;
69 static OicSecDoxm_t gDefaultDoxm =
71 NULL, /* OicUrn_t *oxmType */
72 0, /* size_t oxmTypeLen */
73 &gOicSecDoxmJustWorks, /* uint16_t *oxm */
74 1, /* size_t oxmLen */
75 OIC_JUST_WORKS, /* uint16_t oxmSel */
76 SYMMETRIC_PAIR_WISE_KEY,/* OicSecCredType_t sct */
77 false, /* bool owned */
78 {.id = {0}}, /* OicUuid_t deviceID */
80 {.id = {0}}, /* OicUuid_t owner */
81 {.id = {0}}, /* OicUuid_t rownerID */
84 void DeleteDoxmBinData(OicSecDoxm_t* doxm)
89 for (size_t i = 0; i < doxm->oxmTypeLen; i++)
91 OICFree(doxm->oxmType[i]);
93 OICFree(doxm->oxmType);
103 OCStackResult DoxmToCBORPayload(const OicSecDoxm_t *doxm, uint8_t **payload, size_t *size)
105 if (NULL == doxm || NULL == payload || NULL != *payload || NULL == size)
107 return OC_STACK_INVALID_PARAM;
109 size_t cborLen = *size;
117 OCStackResult ret = OC_STACK_ERROR;
121 char* strUuid = NULL;
123 int64_t cborEncoderResult = CborNoError;
124 uint8_t mapSize = DOXM_MAP_SIZE;
125 if (doxm->oxmTypeLen > 0)
129 if (doxm->oxmLen > 0)
134 uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
135 VERIFY_NON_NULL(TAG, outPayload, ERROR);
136 cbor_encoder_init(&encoder, outPayload, cborLen, 0);
138 cborEncoderResult = cbor_encoder_create_map(&encoder, &doxmMap, mapSize);
139 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Map.");
141 //OxmType -- Not Mandatory
142 if (doxm->oxmTypeLen > 0)
145 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OXM_TYPE_NAME,
146 strlen(OIC_JSON_OXM_TYPE_NAME));
147 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmType Tag.");
148 cborEncoderResult = cbor_encoder_create_array(&doxmMap, &oxmType, doxm->oxmTypeLen);
149 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmType Array.");
151 for (size_t i = 0; i < doxm->oxmTypeLen; i++)
153 cborEncoderResult = cbor_encode_text_string(&oxmType, doxm->oxmType[i],
154 strlen(doxm->oxmType[i]));
155 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmType Value.");
157 cborEncoderResult = cbor_encoder_close_container(&doxmMap, &oxmType);
158 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing oxmType.");
161 //Oxm -- Not Mandatory
162 if (doxm->oxmLen > 0)
165 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OXMS_NAME,
166 strlen(OIC_JSON_OXMS_NAME));
167 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmName Tag.");
168 cborEncoderResult = cbor_encoder_create_array(&doxmMap, &oxm, doxm->oxmLen);
169 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmName Array.");
171 for (size_t i = 0; i < doxm->oxmLen; i++)
173 cborEncoderResult = cbor_encode_int(&oxm, doxm->oxm[i]);
174 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmName Value");
176 cborEncoderResult = cbor_encoder_close_container(&doxmMap, &oxm);
177 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing oxmName.");
180 //OxmSel -- Mandatory
181 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OXM_SEL_NAME,
182 strlen(OIC_JSON_OXM_SEL_NAME));
183 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Sel Tag.");
184 cborEncoderResult = cbor_encode_int(&doxmMap, doxm->oxmSel);
185 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Sel Value.");
188 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_SUPPORTED_CRED_TYPE_NAME,
189 strlen(OIC_JSON_SUPPORTED_CRED_TYPE_NAME));
190 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Type Tag");
191 cborEncoderResult = cbor_encode_int(&doxmMap, doxm->sct);
192 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Type Value.");
195 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OWNED_NAME,
196 strlen(OIC_JSON_OWNED_NAME));
197 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owned Tag.");
198 cborEncoderResult = cbor_encode_boolean(&doxmMap, doxm->owned);
199 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owned Value.");
201 //DeviceId -- Mandatory
202 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_DEVICE_ID_NAME,
203 strlen(OIC_JSON_DEVICE_ID_NAME));
204 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Tag.");
205 ret = ConvertUuidToStr(&doxm->deviceID, &strUuid);
206 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
207 cborEncoderResult = cbor_encode_text_string(&doxmMap, strUuid, strlen(strUuid));
208 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Value.");
212 //devownerid -- Mandatory
213 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_DEVOWNERID_NAME,
214 strlen(OIC_JSON_DEVOWNERID_NAME));
215 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owner Id Tag.");
216 ret = ConvertUuidToStr(&doxm->owner, &strUuid);
217 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
218 cborEncoderResult = cbor_encode_text_string(&doxmMap, strUuid, strlen(strUuid));
219 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owner Id Value.");
223 //ROwner -- Mandatory
224 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_ROWNERID_NAME,
225 strlen(OIC_JSON_ROWNERID_NAME));
226 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Tag.");
227 ret = ConvertUuidToStr(&doxm->rownerID, &strUuid);
228 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
229 cborEncoderResult = cbor_encode_text_string(&doxmMap, strUuid, strlen(strUuid));
230 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Value.");
234 //DPC -- not Mandatory, but this type is boolean, so instance always has a value.
235 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_DPC_NAME,
236 strlen(OIC_JSON_DPC_NAME));
237 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DPC Tag.");
238 cborEncoderResult = cbor_encode_boolean(&doxmMap, doxm->dpc);
239 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DPC Value.");
241 cborEncoderResult = cbor_encoder_close_container(&encoder, &doxmMap);
242 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing DoxmMap.");
244 if (CborNoError == cborEncoderResult)
246 *size = encoder.ptr - outPayload;
247 *payload = outPayload;
251 if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
253 OIC_LOG(DEBUG, TAG, "Memory getting reallocated.");
254 // reallocate and try again!
256 // Since the allocated initial memory failed, double the memory.
257 cborLen += encoder.ptr - encoder.end;
258 OIC_LOG_V(DEBUG, TAG, "Doxm reallocation size : %zd.", cborLen);
259 cborEncoderResult = CborNoError;
260 ret = DoxmToCBORPayload(doxm, payload, &cborLen);
264 if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret))
270 ret = OC_STACK_ERROR;
276 OCStackResult CBORPayloadToDoxm(const uint8_t *cborPayload, size_t size,
277 OicSecDoxm_t **secDoxm)
279 if (NULL == cborPayload || NULL == secDoxm || NULL != *secDoxm || 0 == size)
281 return OC_STACK_INVALID_PARAM;
284 OCStackResult ret = OC_STACK_ERROR;
288 CborError cborFindResult = CborNoError;
289 char* strUuid = NULL;
293 cbor_parser_init(cborPayload, size, 0, &parser, &doxmCbor);
295 OicSecDoxm_t *doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(*doxm));
296 VERIFY_NON_NULL(TAG, doxm, ERROR);
298 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_OXM_TYPE_NAME, &doxmMap);
299 //OxmType -- not Mandatory
300 if (CborNoError == cborFindResult && cbor_value_is_array(&doxmMap))
304 cborFindResult = cbor_value_get_array_length(&doxmMap, &doxm->oxmTypeLen);
305 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding oxmTypeLen.")
306 VERIFY_SUCCESS(TAG, doxm->oxmTypeLen != 0, ERROR);
308 doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(*doxm->oxmType));
309 VERIFY_NON_NULL(TAG, doxm->oxmType, ERROR);
311 cborFindResult = cbor_value_enter_container(&doxmMap, &oxmType);
312 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering oxmType Array.")
316 while (cbor_value_is_valid(&oxmType) && cbor_value_is_text_string(&oxmType))
318 cborFindResult = cbor_value_dup_text_string(&oxmType, &doxm->oxmType[i++],
320 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding omxType text string.")
321 cborFindResult = cbor_value_advance(&oxmType);
322 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing oxmType.")
326 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_OXMS_NAME, &doxmMap);
327 //Oxm -- not Mandatory
328 if (CborNoError == cborFindResult && cbor_value_is_array(&doxmMap))
331 cborFindResult = cbor_value_get_array_length(&doxmMap, &doxm->oxmLen);
332 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding oxmName array Length.")
333 VERIFY_SUCCESS(TAG, doxm->oxmLen != 0, ERROR);
335 doxm->oxm = (OicSecOxm_t *)OICCalloc(doxm->oxmLen, sizeof(*doxm->oxm));
336 VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
338 cborFindResult = cbor_value_enter_container(&doxmMap, &oxm);
339 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering oxmName Array.")
342 while (cbor_value_is_valid(&oxm) && cbor_value_is_integer(&oxm))
344 cborFindResult = cbor_value_get_int(&oxm, (int *) &doxm->oxm[i++]);
345 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding oxmName Value")
346 cborFindResult = cbor_value_advance(&oxm);
347 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing oxmName.")
351 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_OXM_SEL_NAME, &doxmMap);
352 if (CborNoError == cborFindResult && cbor_value_is_integer(&doxmMap))
354 cborFindResult = cbor_value_get_int(&doxmMap, (int *) &doxm->oxmSel);
355 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Sel Name Value.")
357 else // PUT/POST JSON may not have oxmsel so set it to the gDoxm->oxmSel
359 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
360 doxm->oxmSel = gDoxm->oxmSel;
363 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_SUPPORTED_CRED_TYPE_NAME, &doxmMap);
364 if (CborNoError == cborFindResult && cbor_value_is_integer(&doxmMap))
366 cborFindResult = cbor_value_get_int(&doxmMap, (int *) &doxm->sct);
367 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Sct Name Value.")
369 else // PUT/POST JSON may not have sct so set it to the gDoxm->sct
371 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
372 doxm->sct = gDoxm->sct;
375 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_OWNED_NAME, &doxmMap);
376 if (CborNoError == cborFindResult && cbor_value_is_boolean(&doxmMap))
378 cborFindResult = cbor_value_get_boolean(&doxmMap, &doxm->owned);
379 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Owned Value.")
381 else // PUT/POST JSON may not have owned so set it to the gDomx->owned
383 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
384 doxm->owned = gDoxm->owned;
387 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_DPC_NAME, &doxmMap);
388 if (CborNoError == cborFindResult && cbor_value_is_boolean(&doxmMap))
390 cborFindResult = cbor_value_get_boolean(&doxmMap, &doxm->dpc);
391 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DPC Value.")
393 else // PUT/POST JSON may not have dpc so set it to the gDomx->dpc
395 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
396 doxm->dpc = gDoxm->dpc;
399 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_DEVICE_ID_NAME, &doxmMap);
400 if (CborNoError == cborFindResult && cbor_value_is_text_string(&doxmMap))
402 cborFindResult = cbor_value_dup_text_string(&doxmMap, &strUuid , &len, NULL);
403 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Device Id Value.");
404 ret = ConvertStrToUuid(strUuid , &doxm->deviceID);
405 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
410 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_DEVOWNERID_NAME, &doxmMap);
411 if (CborNoError == cborFindResult && cbor_value_is_text_string(&doxmMap))
413 cborFindResult = cbor_value_dup_text_string(&doxmMap, &strUuid , &len, NULL);
414 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Owner Value.");
415 ret = ConvertStrToUuid(strUuid , &doxm->owner);
416 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
421 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_ROWNERID_NAME, &doxmMap);
422 if (CborNoError == cborFindResult && cbor_value_is_text_string(&doxmMap))
424 cborFindResult = cbor_value_dup_text_string(&doxmMap, &strUuid , &len, NULL);
425 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Value.");
426 ret = ConvertStrToUuid(strUuid , &doxm->rownerID);
427 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
436 if (CborNoError != cborFindResult)
438 OIC_LOG (ERROR, TAG, "CBORPayloadToDoxm failed!!!");
439 DeleteDoxmBinData(doxm);
442 ret = OC_STACK_ERROR;
448 * @todo document this function including why code might need to call this.
449 * The current suspicion is that it's not being called as much as it should.
451 static bool UpdatePersistentStorage(OicSecDoxm_t * doxm)
457 // Convert Doxm data into CBOR for update to persistent storage
458 uint8_t *payload = NULL;
460 OCStackResult res = DoxmToCBORPayload(doxm, &payload, &size);
461 if (payload && (OC_STACK_OK == res)
462 && (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_DOXM_NAME, payload, size)))
470 if (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_DOXM_NAME, NULL, 0))
479 static bool ValidateQuery(const char * query)
481 // Send doxm resource data if the state of doxm resource
482 // matches with the query parameters.
483 // else send doxm resource data as NULL
484 // TODO Remove this check and rely on Policy Engine
485 // and Provisioning Mode to enforce provisioning-state
486 // access rules. Eventually, the PE and PM code will
487 // not send a request to the /doxm Entity Handler at all
488 // if it should not respond.
489 OIC_LOG (DEBUG, TAG, "In ValidateQuery");
495 bool bOwnedQry = false; // does querystring contains 'owned' query ?
496 bool bOwnedMatch = false; // does 'owned' query value matches with doxm.owned status?
497 bool bDeviceIDQry = false; // does querystring contains 'deviceid' query ?
498 bool bDeviceIDMatch = false; // does 'deviceid' query matches with doxm.deviceid ?
500 OicParseQueryIter_t parseIter = {.attrPos = NULL};
502 ParseQueryIterInit((unsigned char*)query, &parseIter);
504 while (GetNextQuery(&parseIter))
506 if (strncasecmp((char *)parseIter.attrPos, OIC_JSON_OWNED_NAME, parseIter.attrLen) == 0)
509 if ((strncasecmp((char *)parseIter.valPos, OIC_SEC_TRUE, parseIter.valLen) == 0) &&
514 else if ((strncasecmp((char *)parseIter.valPos, OIC_SEC_FALSE, parseIter.valLen) == 0)
521 if (strncasecmp((char *)parseIter.attrPos, OIC_JSON_DEVICE_ID_NAME, parseIter.attrLen) == 0)
524 OicUuid_t subject = {.id={0}};
526 memcpy(subject.id, parseIter.valPos, parseIter.valLen);
527 if (0 == memcmp(&gDoxm->deviceID.id, &subject.id, sizeof(gDoxm->deviceID.id)))
529 bDeviceIDMatch = true;
534 return ((bOwnedQry ? bOwnedMatch : true) && (bDeviceIDQry ? bDeviceIDMatch : true));
537 static OCEntityHandlerResult HandleDoxmGetRequest (const OCEntityHandlerRequest * ehRequest)
539 OCEntityHandlerResult ehRet = OC_EH_OK;
541 OIC_LOG(DEBUG, TAG, "Doxm EntityHandle processing GET request");
543 //Checking if Get request is a query.
544 if (ehRequest->query)
546 OIC_LOG(DEBUG, TAG, "HandleDoxmGetRequest processing query");
547 if (!ValidateQuery(ehRequest->query))
554 * For GET or Valid Query request return doxm resource CBOR payload.
555 * For non-valid query return NULL json payload.
556 * A device will 'always' have a default Doxm, so DoxmToCBORPayload will
557 * return valid doxm resource json.
559 uint8_t *payload = NULL;
562 if (ehRet == OC_EH_OK)
564 if (OC_STACK_OK != DoxmToCBORPayload(gDoxm, &payload, &size))
570 // Send response payload to request originator
571 if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, payload, size))
574 OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandleDoxmGetRequest");
582 static OCEntityHandlerResult HandleDoxmPostRequest(const OCEntityHandlerRequest * ehRequest)
584 OIC_LOG (DEBUG, TAG, "Doxm EntityHandle processing POST request");
585 OCEntityHandlerResult ehRet = OC_EH_ERROR;
586 OicUuid_t emptyOwner = {.id = {0} };
587 static uint16_t previousMsgId = 0;
590 * Convert CBOR Doxm data into binary. This will also validate
591 * the Doxm data received.
593 OicSecDoxm_t *newDoxm = NULL;
595 if (ehRequest->payload)
597 uint8_t *payload = ((OCSecurityPayload *)ehRequest->payload)->securityData;
598 size_t size = ((OCSecurityPayload *)ehRequest->payload)->payloadSize;
599 OCStackResult res = CBORPayloadToDoxm(payload, size, &newDoxm);
601 if (newDoxm && OC_STACK_OK == res)
603 if (OIC_JUST_WORKS == newDoxm->oxmSel)
605 if ((false == gDoxm->owned) && (false == newDoxm->owned))
608 * If current state of the device is un-owned, enable
609 * anonymous ECDH cipher in tinyDTLS so that Provisioning
610 * tool can initiate JUST_WORKS ownership transfer process.
612 if (memcmp(&(newDoxm->owner), &emptyOwner, sizeof(OicUuid_t)) == 0)
614 OIC_LOG (INFO, TAG, "Doxm EntityHandle enabling AnonECDHCipherSuite");
616 ehRet = (CAEnableAnonECDHCipherSuite(true) == CA_STATUS_OK) ? OC_EH_OK : OC_EH_ERROR;
617 #endif //__WITH_DTLS__
623 //Save the owner's UUID to derive owner credential
624 memcpy(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t));
626 // Update new state in persistent storage
627 if (true == UpdatePersistentStorage(gDoxm))
633 OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
638 * Disable anonymous ECDH cipher in tinyDTLS since device is now
641 CAResult_t caRes = CA_STATUS_OK;
642 caRes = CAEnableAnonECDHCipherSuite(false);
643 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
644 OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");
647 #define TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 0xC0AE
648 CASelectCipherSuite(TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
649 #endif //__WITH_X509__
650 #endif //__WITH_DTLS__
654 else if (OIC_RANDOM_DEVICE_PIN == newDoxm->oxmSel)
656 if ((false == gDoxm->owned) && (false == newDoxm->owned))
659 * If current state of the device is un-owned, enable
660 * anonymous ECDH cipher in tinyDTLS so that Provisioning
661 * tool can initiate JUST_WORKS ownership transfer process.
663 if(memcmp(&(newDoxm->owner), &emptyOwner, sizeof(OicUuid_t)) == 0)
665 gDoxm->oxmSel = newDoxm->oxmSel;
666 //Update new state in persistent storage
667 if ((UpdatePersistentStorage(gDoxm) == true))
673 OIC_LOG(WARNING, TAG, "Failed to update DOXM in persistent storage");
678 CAResult_t caRes = CA_STATUS_OK;
680 caRes = CAEnableAnonECDHCipherSuite(false);
681 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
682 OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");
684 caRes = CASelectCipherSuite(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256);
685 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
687 if(previousMsgId != ehRequest->messageID)
689 char ranPin[OXM_RANDOM_PIN_SIZE + 1] = {0,};
690 if(OC_STACK_OK == GeneratePin(ranPin, OXM_RANDOM_PIN_SIZE + 1))
692 //Set the device id to derive temporal PSK
693 SetUuidForRandomPinOxm(&gDoxm->deviceID);
696 * Since PSK will be used directly by DTLS layer while PIN based ownership transfer,
697 * Credential should not be saved into SVR.
698 * For this reason, use a temporary get_psk_info callback to random PIN OxM.
700 caRes = CARegisterDTLSCredentialsHandler(GetDtlsPskForRandomPinOxm);
701 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
706 OIC_LOG(ERROR, TAG, "Failed to generate random PIN");
709 previousMsgId = ehRequest->messageID;
711 #endif //__WITH_DTLS__
716 //Save the owner's UUID to derive owner credential
717 memcpy(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t));
719 //Update new state in persistent storage
720 if (UpdatePersistentStorage(gDoxm) == true)
726 OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
735 * When current state of the device is un-owned and Provisioning
736 * Tool is attempting to change the state to 'Owned' with a
737 * qualified value for the field 'Owner'
739 if ((false == gDoxm->owned) && (true == newDoxm->owned) &&
740 (memcmp(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t)) == 0))
742 //Change the SVR's resource owner as owner device.
743 OCStackResult ownerRes = SetAclRownerId(&gDoxm->owner);
744 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
749 ownerRes = SetAmaclRownerId(&gDoxm->owner);
750 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
755 ownerRes = SetCredRownerId(&gDoxm->owner);
756 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
761 ownerRes = SetPstatRownerId(&gDoxm->owner);
762 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
767 ownerRes = SetDpairingRownerId(&gDoxm->owner);
768 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
773 ownerRes = SetPconfRownerId(&gDoxm->owner);
774 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
781 memcpy(&gDoxm->rownerID, &gDoxm->owner, sizeof(OicUuid_t));
783 // Update new state in persistent storage
784 if (UpdatePersistentStorage(gDoxm))
786 //Update default ACL of security resource to prevent anonymous user access.
787 if(OC_STACK_OK == UpdateDefaultSecProvACL())
793 OIC_LOG(ERROR, TAG, "Failed to remove default ACL for security provisioning");
799 OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
807 if(OC_EH_OK != ehRet)
809 OIC_LOG(WARNING, TAG, "The operation failed during handle DOXM request,"\
810 "DOXM will be reverted.");
813 * If some error is occured while ownership transfer,
814 * ownership transfer related resource should be revert back to initial status.
816 RestoreDoxmToInitState();
817 RestorePstatToInitState();
820 //Send payload to request originator
821 if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
824 OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandleDoxmPostRequest");
826 DeleteDoxmBinData(newDoxm);
831 OCEntityHandlerResult DoxmEntityHandler(OCEntityHandlerFlag flag,
832 OCEntityHandlerRequest * ehRequest,
836 OCEntityHandlerResult ehRet = OC_EH_ERROR;
838 if(NULL == ehRequest)
843 if (flag & OC_REQUEST_FLAG)
845 OIC_LOG(DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
847 switch (ehRequest->method)
850 ehRet = HandleDoxmGetRequest(ehRequest);
854 ehRet = HandleDoxmPostRequest(ehRequest);
859 SendSRMResponse(ehRequest, ehRet, NULL, 0);
867 OCStackResult CreateDoxmResource()
869 OCStackResult ret = OCCreateResource(&gDoxmHandle,
870 OIC_RSRC_TYPE_SEC_DOXM,
871 OC_RSRVD_INTERFACE_DEFAULT,
878 if (OC_STACK_OK != ret)
880 OIC_LOG (FATAL, TAG, "Unable to instantiate Doxm resource");
881 DeInitDoxmResource();
887 * Checks if DeviceID is generated during provisioning for the new device.
888 * If DeviceID is NULL then generates the new DeviceID.
889 * Once DeviceID is assigned to the device it does not change for the lifetime of the device.
891 static OCStackResult CheckDeviceID()
893 OCStackResult ret = OC_STACK_ERROR;
894 bool validId = false;
895 for (uint8_t i = 0; i < UUID_LENGTH; i++)
897 if (gDoxm->deviceID.id[i] != 0)
906 if (OCGenerateUuid(gDoxm->deviceID.id) != RAND_UUID_OK)
908 OIC_LOG(FATAL, TAG, "Generate UUID for Server Instance failed!");
913 if (!UpdatePersistentStorage(gDoxm))
915 //TODO: After registering PSI handler in all samples, do ret = OC_STACK_OK here.
916 OIC_LOG(FATAL, TAG, "UpdatePersistentStorage failed!");
927 * Get the default value.
929 * @return the default value of doxm, @ref OicSecDoxm_t.
931 static OicSecDoxm_t* GetDoxmDefault()
933 OIC_LOG(DEBUG, TAG, "GetDoxmToDefault");
934 return &gDefaultDoxm;
937 const OicSecDoxm_t* GetDoxmResourceData()
942 OCStackResult InitDoxmResource()
944 OCStackResult ret = OC_STACK_ERROR;
946 //Read DOXM resource from PS
947 uint8_t *data = NULL;
949 ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_DOXM_NAME, &data, &size);
950 // If database read failed
951 if (OC_STACK_OK != ret)
953 OIC_LOG (DEBUG, TAG, "ReadSVDataFromPS failed");
957 // Read DOXM resource from PS
958 ret = CBORPayloadToDoxm(data, size, &gDoxm);
961 * If SVR database in persistent storage got corrupted or
962 * is not available for some reason, a default doxm is created
963 * which allows user to initiate doxm provisioning again.
965 if ((OC_STACK_OK != ret) || !data || !gDoxm)
967 gDoxm = GetDoxmDefault();
970 //In case of the server is shut down unintentionally, we should initialize the owner
971 if(false == gDoxm->owned)
973 OicUuid_t emptyUuid = {.id={0}};
974 memcpy(&gDoxm->owner, &emptyUuid, sizeof(OicUuid_t));
977 ret = CheckDeviceID();
978 if (ret == OC_STACK_OK)
980 OIC_LOG_V(DEBUG, TAG, "Initial Doxm Owned = %d", gDoxm->owned);
981 //Instantiate 'oic.sec.doxm'
982 ret = CreateDoxmResource();
986 OIC_LOG (ERROR, TAG, "CheckDeviceID failed");
992 OCStackResult DeInitDoxmResource()
994 OCStackResult ret = OCDeleteResource(gDoxmHandle);
995 if (gDoxm != &gDefaultDoxm)
997 DeleteDoxmBinData(gDoxm);
1001 if (OC_STACK_OK == ret)
1007 return OC_STACK_ERROR;
1011 OCStackResult GetDoxmDeviceID(OicUuid_t *deviceID)
1013 if (deviceID && gDoxm)
1015 *deviceID = gDoxm->deviceID;
1018 return OC_STACK_ERROR;
1021 OCStackResult GetDoxmDevOwnerId(OicUuid_t *devownerid)
1023 OCStackResult retVal = OC_STACK_ERROR;
1026 OIC_LOG_V(DEBUG, TAG, "GetDoxmDevOwnerId(): gDoxm owned = %d.", \
1030 *devownerid = gDoxm->owner;
1031 retVal = OC_STACK_OK;
1037 OCStackResult GetDoxmRownerId(OicUuid_t *rowneruuid)
1039 OCStackResult retVal = OC_STACK_ERROR;
1044 *rowneruuid = gDoxm->rownerID;
1045 retVal = OC_STACK_OK;
1052 * Function to restore doxm resurce to initial status.
1053 * This function will use in case of error while ownership transfer
1055 void RestoreDoxmToInitState()
1059 OIC_LOG(INFO, TAG, "DOXM resource will revert back to initial status.");
1061 OicUuid_t emptyUuid = {.id={0}};
1062 memcpy(&(gDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
1063 gDoxm->owned = false;
1064 gDoxm->oxmSel = OIC_JUST_WORKS;
1066 if(!UpdatePersistentStorage(gDoxm))
1068 OIC_LOG(ERROR, TAG, "Failed to revert DOXM in persistent storage");