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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #include "iotivity_config.h"
33 #include "oic_malloc.h"
34 #include "payload_logging.h"
37 #include "ocpayload.h"
38 #include "ocpayloadcbor.h"
39 #include "cainterface.h"
40 #include "ocserverrequest.h"
41 #include "resourcemanager.h"
42 #include "doxmresource.h"
43 #include "pstatresource.h"
44 #include "aclresource.h"
45 #include "amaclresource.h"
46 #include "pconfresource.h"
47 #include "dpairingresource.h"
48 #include "psinterface.h"
49 #include "srmresourcestrings.h"
50 #include "securevirtualresourcetypes.h"
51 #include "credresource.h"
52 #include "srmutility.h"
53 #include "pinoxmcommon.h"
55 #define TAG "OIC_SRM_DOXM"
56 #define CHAR_ZERO ('0')
58 /** Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
59 * The value of payload size is increased until reaching belox max cbor size. */
60 static const uint16_t CBOR_SIZE = 512;
62 /** Max cbor size payload. */
63 static const uint16_t CBOR_MAX_SIZE = 4400;
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 #ifdef _ENABLE_MULTIPLE_OWNER_
82 NULL, /* OicSecSubOwner_t sub-owner list */
83 NULL, /* OicSecMomType_t multiple owner mode */
84 #endif //_ENABLE_MULTIPLE_OWNER_
85 {.id = {0}}, /* OicUuid_t rownerID */
89 * This method is internal method.
90 * the param roParsed is optionally used to know whether cborPayload has
91 * at least read only property value or not.
93 static OCStackResult CBORPayloadToDoxmBin(const uint8_t *cborPayload, size_t size,
94 OicSecDoxm_t **doxm, bool *roParsed);
96 void DeleteDoxmBinData(OicSecDoxm_t* doxm)
101 for (size_t i = 0; i < doxm->oxmTypeLen; i++)
103 OICFree(doxm->oxmType[i]);
105 OICFree(doxm->oxmType);
110 #ifdef _ENABLE_MULTIPLE_OWNER_
114 //clean sub-owner list
115 if(NULL != doxm->subOwners)
117 OicSecSubOwner_t* subowner = NULL;
118 OicSecSubOwner_t* temp = NULL;
119 LL_FOREACH_SAFE(doxm->subOwners, subowner, temp)
121 LL_DELETE(doxm->subOwners, subowner);
125 #endif //_ENABLE_MULTIPLE_OWNER_
132 OCStackResult DoxmToCBORPayload(const OicSecDoxm_t *doxm, uint8_t **payload, size_t *size,
135 if (NULL == doxm || NULL == payload || NULL != *payload || NULL == size)
137 return OC_STACK_INVALID_PARAM;
139 size_t cborLen = *size;
147 OCStackResult ret = OC_STACK_ERROR;
151 char* strUuid = NULL;
153 int64_t cborEncoderResult = CborNoError;
155 uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
156 VERIFY_NON_NULL(TAG, outPayload, ERROR);
157 cbor_encoder_init(&encoder, outPayload, cborLen, 0);
159 cborEncoderResult = cbor_encoder_create_map(&encoder, &doxmMap, CborIndefiniteLength);
160 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Map.");
162 //OxmType -- Not Mandatory
163 if (doxm->oxmTypeLen > 0)
166 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OXM_TYPE_NAME,
167 strlen(OIC_JSON_OXM_TYPE_NAME));
168 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmType Tag.");
169 cborEncoderResult = cbor_encoder_create_array(&doxmMap, &oxmType, doxm->oxmTypeLen);
170 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmType Array.");
172 for (size_t i = 0; i < doxm->oxmTypeLen; i++)
174 cborEncoderResult = cbor_encode_text_string(&oxmType, doxm->oxmType[i],
175 strlen(doxm->oxmType[i]));
176 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmType Value.");
178 cborEncoderResult = cbor_encoder_close_container(&doxmMap, &oxmType);
179 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing oxmType.");
182 //Oxm -- Not Mandatory
183 if (doxm->oxmLen > 0 && false == rwOnly)
186 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OXMS_NAME,
187 strlen(OIC_JSON_OXMS_NAME));
188 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmName Tag.");
189 cborEncoderResult = cbor_encoder_create_array(&doxmMap, &oxm, doxm->oxmLen);
190 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmName Array.");
192 for (size_t i = 0; i < doxm->oxmLen; i++)
194 cborEncoderResult = cbor_encode_int(&oxm, doxm->oxm[i]);
195 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmName Value");
197 cborEncoderResult = cbor_encoder_close_container(&doxmMap, &oxm);
198 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing oxmName.");
201 //OxmSel -- Mandatory
202 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OXM_SEL_NAME,
203 strlen(OIC_JSON_OXM_SEL_NAME));
204 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Sel Tag.");
205 cborEncoderResult = cbor_encode_int(&doxmMap, doxm->oxmSel);
206 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Sel Value.");
211 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_SUPPORTED_CRED_TYPE_NAME,
212 strlen(OIC_JSON_SUPPORTED_CRED_TYPE_NAME));
213 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Type Tag");
214 cborEncoderResult = cbor_encode_int(&doxmMap, doxm->sct);
215 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Type Value.");
219 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OWNED_NAME,
220 strlen(OIC_JSON_OWNED_NAME));
221 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owned Tag.");
222 cborEncoderResult = cbor_encode_boolean(&doxmMap, doxm->owned);
223 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owned Value.");
227 //DeviceId -- Mandatory
228 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_DEVICE_ID_NAME,
229 strlen(OIC_JSON_DEVICE_ID_NAME));
230 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Tag.");
231 ret = ConvertUuidToStr(&doxm->deviceID, &strUuid);
232 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
233 cborEncoderResult = cbor_encode_text_string(&doxmMap, strUuid, strlen(strUuid));
234 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Value.");
239 #ifdef _ENABLE_MULTIPLE_OWNER_
240 //Device SubOwnerID -- Not Mandatory
243 size_t subOwnerLen = 0;
244 OicSecSubOwner_t* subOwner = NULL;
245 LL_FOREACH(doxm->subOwners, subOwner)
250 CborEncoder subOwners;
251 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_SUBOWNERID_NAME,
252 strlen(OIC_JSON_SUBOWNERID_NAME));
253 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SubOwnerId Tag.");
254 cborEncoderResult = cbor_encoder_create_array(&doxmMap, &subOwners, subOwnerLen);
255 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SubOwner Array.");
258 LL_FOREACH(doxm->subOwners, subOwner)
260 char* strUuid = NULL;
261 ret = ConvertUuidToStr(&subOwner->uuid, &strUuid);
262 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
263 cborEncoderResult = cbor_encode_text_string(&subOwners, strUuid, strlen(strUuid));
265 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SubOwnerId Value");
267 cborEncoderResult = cbor_encoder_close_container(&doxmMap, &subOwners);
268 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing SubOwnerId.");
271 //Multiple Owner Mode -- Not Mandatory
274 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_MOM_NAME,
275 strlen(OIC_JSON_MOM_NAME));
276 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding mom Tag");
277 cborEncoderResult = cbor_encode_int(&doxmMap, (int64_t)doxm->mom->mode);
278 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding mom Value.");
280 #endif //_ENABLE_MULTIPLE_OWNER_
282 //devownerid -- Mandatory
283 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_DEVOWNERID_NAME,
284 strlen(OIC_JSON_DEVOWNERID_NAME));
285 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owner Id Tag.");
286 ret = ConvertUuidToStr(&doxm->owner, &strUuid);
287 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
288 cborEncoderResult = cbor_encode_text_string(&doxmMap, strUuid, strlen(strUuid));
289 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owner Id Value.");
293 //ROwner -- Mandatory
294 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_ROWNERID_NAME,
295 strlen(OIC_JSON_ROWNERID_NAME));
296 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Tag.");
297 ret = ConvertUuidToStr(&doxm->rownerID, &strUuid);
298 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
299 cborEncoderResult = cbor_encode_text_string(&doxmMap, strUuid, strlen(strUuid));
300 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Value.");
304 //x.org.iotivity.dpc -- not Mandatory(vendor-specific), but this type is boolean, so instance always has a value.
305 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_DPC_NAME,
306 strlen(OIC_JSON_DPC_NAME));
307 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DPC Tag.");
308 cborEncoderResult = cbor_encode_boolean(&doxmMap, doxm->dpc);
309 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DPC Value.");
313 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_RT_NAME,
314 strlen(OIC_JSON_RT_NAME));
315 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Name Tag.");
316 cborEncoderResult = cbor_encoder_create_array(&doxmMap, &rtArray, 1);
317 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Value.");
318 for (size_t i = 0; i < 1; i++)
320 cborEncoderResult = cbor_encode_text_string(&rtArray, OIC_RSRC_TYPE_SEC_DOXM,
321 strlen(OIC_RSRC_TYPE_SEC_DOXM));
322 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding RT Value.");
324 cborEncoderResult = cbor_encoder_close_container(&doxmMap, &rtArray);
325 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing RT.");
329 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_IF_NAME,
330 strlen(OIC_JSON_IF_NAME));
331 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Name Tag.");
332 cborEncoderResult = cbor_encoder_create_array(&doxmMap, &ifArray, 1);
333 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Value.");
334 for (size_t i = 0; i < 1; i++)
336 cborEncoderResult = cbor_encode_text_string(&ifArray, OC_RSRVD_INTERFACE_DEFAULT,
337 strlen(OC_RSRVD_INTERFACE_DEFAULT));
338 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding IF Value.");
340 cborEncoderResult = cbor_encoder_close_container(&doxmMap, &ifArray);
341 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing IF.");
343 cborEncoderResult = cbor_encoder_close_container(&encoder, &doxmMap);
344 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing DoxmMap.");
346 if (CborNoError == cborEncoderResult)
348 *size = cbor_encoder_get_buffer_size(&encoder, outPayload);
349 *payload = outPayload;
353 if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
355 OIC_LOG(DEBUG, TAG, "Memory getting reallocated.");
356 // reallocate and try again!
358 // Since the allocated initial memory failed, double the memory.
359 cborLen += cbor_encoder_get_buffer_size(&encoder, encoder.end);
360 OIC_LOG_V(DEBUG, TAG, "Doxm reallocation size : %zd.", cborLen);
361 cborEncoderResult = CborNoError;
362 ret = DoxmToCBORPayload(doxm, payload, &cborLen, rwOnly);
366 if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret))
372 ret = OC_STACK_ERROR;
378 OCStackResult CBORPayloadToDoxm(const uint8_t *cborPayload, size_t size,
379 OicSecDoxm_t **secDoxm)
381 return CBORPayloadToDoxmBin(cborPayload, size, secDoxm, NULL);
384 static OCStackResult CBORPayloadToDoxmBin(const uint8_t *cborPayload, size_t size,
385 OicSecDoxm_t **secDoxm, bool *roParsed)
387 if (NULL == cborPayload || NULL == secDoxm || NULL != *secDoxm || 0 == size)
389 return OC_STACK_INVALID_PARAM;
392 OCStackResult ret = OC_STACK_ERROR;
396 CborError cborFindResult = CborNoError;
397 char* strUuid = NULL;
401 cbor_parser_init(cborPayload, size, 0, &parser, &doxmCbor);
403 OicSecDoxm_t *doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(*doxm));
404 VERIFY_NON_NULL(TAG, doxm, ERROR);
406 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_OXM_TYPE_NAME, &doxmMap);
407 //OxmType -- not Mandatory
408 if (CborNoError == cborFindResult && cbor_value_is_array(&doxmMap))
412 cborFindResult = cbor_value_get_array_length(&doxmMap, &doxm->oxmTypeLen);
413 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding oxmTypeLen.");
414 VERIFY_SUCCESS(TAG, doxm->oxmTypeLen != 0, ERROR);
416 doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(*doxm->oxmType));
417 VERIFY_NON_NULL(TAG, doxm->oxmType, ERROR);
419 cborFindResult = cbor_value_enter_container(&doxmMap, &oxmType);
420 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering oxmType Array.")
424 while (cbor_value_is_valid(&oxmType) && cbor_value_is_text_string(&oxmType))
426 cborFindResult = cbor_value_dup_text_string(&oxmType, &doxm->oxmType[i++],
428 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding omxType text string.");
429 cborFindResult = cbor_value_advance(&oxmType);
430 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing oxmType.");
434 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_OXMS_NAME, &doxmMap);
435 //Oxm -- not Mandatory
436 if (CborNoError == cborFindResult && cbor_value_is_array(&doxmMap))
439 cborFindResult = cbor_value_get_array_length(&doxmMap, &doxm->oxmLen);
440 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding oxmName array Length.");
441 VERIFY_SUCCESS(TAG, doxm->oxmLen != 0, ERROR);
443 doxm->oxm = (OicSecOxm_t *)OICCalloc(doxm->oxmLen, sizeof(*doxm->oxm));
444 VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
446 cborFindResult = cbor_value_enter_container(&doxmMap, &oxm);
447 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering oxmName Array.")
450 while (cbor_value_is_valid(&oxm) && cbor_value_is_integer(&oxm))
454 cborFindResult = cbor_value_get_int(&oxm, &tmp);
455 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding oxmName Value")
456 doxm->oxm[i++] = (OicSecOxm_t)tmp;
457 cborFindResult = cbor_value_advance(&oxm);
458 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing oxmName.")
468 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
469 doxm->oxm = (OicSecOxm_t *) OICCalloc(gDoxm->oxmLen, sizeof(*doxm->oxm));
470 VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
471 doxm->oxmLen = gDoxm->oxmLen;
472 for (size_t i = 0; i < gDoxm->oxmLen; i++)
474 doxm->oxm[i] = gDoxm->oxm[i];
478 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_OXM_SEL_NAME, &doxmMap);
479 if (CborNoError == cborFindResult && cbor_value_is_integer(&doxmMap))
483 cborFindResult = cbor_value_get_int(&doxmMap, &oxmSel);
484 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Sel Name Value.")
485 doxm->oxmSel = (OicSecOxm_t)oxmSel;
487 else // PUT/POST JSON may not have oxmsel so set it to the gDoxm->oxmSel
489 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
490 doxm->oxmSel = gDoxm->oxmSel;
493 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_SUPPORTED_CRED_TYPE_NAME, &doxmMap);
494 if (CborNoError == cborFindResult && cbor_value_is_integer(&doxmMap))
498 cborFindResult = cbor_value_get_int(&doxmMap, &sct);
499 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Sct Name Value.")
500 doxm->sct = (OicSecCredType_t)sct;
507 else // PUT/POST JSON may not have sct so set it to the gDoxm->sct
509 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
510 doxm->sct = gDoxm->sct;
513 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_OWNED_NAME, &doxmMap);
514 if (CborNoError == cborFindResult && cbor_value_is_boolean(&doxmMap))
516 cborFindResult = cbor_value_get_boolean(&doxmMap, &doxm->owned);
517 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Owned Value.")
519 else // PUT/POST JSON may not have owned so set it to the gDomx->owned
521 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
522 doxm->owned = gDoxm->owned;
525 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_DPC_NAME, &doxmMap);
526 if (CborNoError == cborFindResult && cbor_value_is_boolean(&doxmMap))
528 cborFindResult = cbor_value_get_boolean(&doxmMap, &doxm->dpc);
529 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DPC Value.")
531 else // PUT/POST JSON may not have dpc so set it to the gDomx->dpc
533 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
534 doxm->dpc = gDoxm->dpc;
537 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_DEVICE_ID_NAME, &doxmMap);
538 if (CborNoError == cborFindResult && cbor_value_is_text_string(&doxmMap))
540 cborFindResult = cbor_value_dup_text_string(&doxmMap, &strUuid , &len, NULL);
541 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Device Id Value.");
542 ret = ConvertStrToUuid(strUuid , &doxm->deviceID);
543 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
554 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
555 memcpy(doxm->deviceID.id, &gDoxm->deviceID.id, sizeof(doxm->deviceID.id));
558 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_DEVOWNERID_NAME, &doxmMap);
559 if (CborNoError == cborFindResult && cbor_value_is_text_string(&doxmMap))
561 cborFindResult = cbor_value_dup_text_string(&doxmMap, &strUuid , &len, NULL);
562 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Owner Value.");
563 ret = ConvertStrToUuid(strUuid , &doxm->owner);
564 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
570 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
571 memcpy(doxm->owner.id, gDoxm->owner.id, sizeof(doxm->owner.id));
574 #ifdef _ENABLE_MULTIPLE_OWNER_
575 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_MOM_NAME, &doxmMap);
576 if(CborNoError == cborFindResult && cbor_value_is_integer(&doxmMap))
579 cborFindResult = cbor_value_get_int(&doxmMap, &mode);
580 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding mom Name Value.")
581 if(NULL == doxm->mom)
583 doxm->mom = (OicSecMom_t*)OICCalloc(1, sizeof(OicSecMom_t));
584 VERIFY_NON_NULL(TAG, doxm->mom, ERROR);
586 doxm->mom->mode = (OicSecMomType_t)mode;
588 else if(NULL != gDoxm && NULL != gDoxm->mom)
590 // PUT/POST JSON may not have 'mom' so set it to the gDomx->mom
591 if(NULL == doxm->mom)
593 doxm->mom = (OicSecMom_t*)OICCalloc(1, sizeof(OicSecMom_t));
594 VERIFY_NON_NULL(TAG, doxm->mom, ERROR);
596 doxm->mom->mode = gDoxm->mom->mode;
599 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_SUBOWNERID_NAME, &doxmMap);
600 if(CborNoError == cborFindResult && cbor_value_is_array(&doxmMap))
602 size_t subOwnerLen = 0;
603 CborValue subOwnerCbor;
604 cborFindResult = cbor_value_get_array_length(&doxmMap, &subOwnerLen);
605 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SubOwner array Length.");
606 VERIFY_SUCCESS(TAG, 0 != subOwnerLen, ERROR);
608 cborFindResult = cbor_value_enter_container(&doxmMap, &subOwnerCbor);
609 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering SubOwner Array.")
611 while (cbor_value_is_valid(&subOwnerCbor) && cbor_value_is_text_string(&subOwnerCbor))
613 OCStackResult convertRes = OC_STACK_ERROR;
614 OicSecSubOwner_t* subOwner = NULL;
615 char* strUuid = NULL;
618 cborFindResult = cbor_value_dup_text_string(&subOwnerCbor, &strUuid, &uuidLen, NULL);
619 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SubOwnerId Value");
621 subOwner = (OicSecSubOwner_t*)OICCalloc(1, sizeof(OicSecSubOwner_t));
622 VERIFY_NON_NULL(TAG, subOwner, ERROR);
624 convertRes = ConvertStrToUuid(strUuid, &subOwner->uuid);
625 VERIFY_SUCCESS(TAG, OC_STACK_OK == convertRes, ERROR);
626 subOwner->status = MOT_STATUS_DONE;
627 LL_APPEND(doxm->subOwners, subOwner);
629 cborFindResult = cbor_value_advance(&subOwnerCbor);
630 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing SubOwnerId.")
633 else if(NULL != gDoxm && NULL != gDoxm->subOwners)
635 // PUT/POST JSON may not have 'subOwners' so set it to the gDomx->subOwners
636 OicSecSubOwner_t* subOwnerItor = NULL;
637 LL_FOREACH(gDoxm->subOwners, subOwnerItor)
639 OicSecSubOwner_t* subOwnerId = (OicSecSubOwner_t*)OICCalloc(1, sizeof(OicSecSubOwner_t));
640 VERIFY_NON_NULL(TAG, subOwnerId, ERROR);
642 memcpy(&subOwnerId->uuid, &subOwnerItor->uuid, sizeof(OicUuid_t));
643 subOwnerId->status = MOT_STATUS_DONE;
645 LL_APPEND(doxm->subOwners, subOwnerId);
648 #endif //_ENABLE_MULTIPLE_OWNER_
650 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_ROWNERID_NAME, &doxmMap);
651 if (CborNoError == cborFindResult && cbor_value_is_text_string(&doxmMap))
653 cborFindResult = cbor_value_dup_text_string(&doxmMap, &strUuid , &len, NULL);
654 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Value.");
655 ret = ConvertStrToUuid(strUuid , &doxm->rownerID);
656 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
662 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
663 memcpy(doxm->rownerID.id, gDoxm->rownerID.id, sizeof(doxm->rownerID.id));
670 if (CborNoError != cborFindResult)
672 OIC_LOG (ERROR, TAG, "CBORPayloadToDoxm failed!!!");
673 DeleteDoxmBinData(doxm);
676 ret = OC_STACK_ERROR;
682 * @todo document this function including why code might need to call this.
683 * The current suspicion is that it's not being called as much as it should.
685 static bool UpdatePersistentStorage(OicSecDoxm_t * doxm)
691 // Convert Doxm data into CBOR for update to persistent storage
692 uint8_t *payload = NULL;
694 OCStackResult res = DoxmToCBORPayload(doxm, &payload, &size, false);
695 if (payload && (OC_STACK_OK == res)
696 && (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_DOXM_NAME, payload, size)))
704 if (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_DOXM_NAME, NULL, 0))
713 static bool ValidateQuery(const char * query)
715 // Send doxm resource data if the state of doxm resource
716 // matches with the query parameters.
717 // else send doxm resource data as NULL
718 // TODO Remove this check and rely on Policy Engine
719 // and Provisioning Mode to enforce provisioning-state
720 // access rules. Eventually, the PE and PM code will
721 // not send a request to the /doxm Entity Handler at all
722 // if it should not respond.
723 OIC_LOG (DEBUG, TAG, "In ValidateQuery");
729 bool bOwnedQry = false; // does querystring contains 'owned' query ?
730 bool bOwnedMatch = false; // does 'owned' query value matches with doxm.owned status?
731 bool bDeviceIDQry = false; // does querystring contains 'deviceid' query ?
732 bool bDeviceIDMatch = false; // does 'deviceid' query matches with doxm.deviceid ?
733 bool bInterfaceQry = false; // does querystring contains 'if' query ?
734 bool bInterfaceMatch = false; // does 'if' query matches with oic.if.baseline ?
735 #ifdef _ENABLE_MULTIPLE_OWNER_
736 bool bMotQry = false; // does querystring contains 'mom' and 'owned' query ?
737 bool bMotMatch = false; // does 'mom' query value is not '0' && does query value matches with doxm.owned status?
738 #endif //_ENABLE_MULTIPLE_OWNER_
740 OicParseQueryIter_t parseIter = {.attrPos = NULL};
742 ParseQueryIterInit((unsigned char*)query, &parseIter);
744 while (GetNextQuery(&parseIter))
746 if (strncasecmp((char *)parseIter.attrPos, OIC_JSON_OWNED_NAME, parseIter.attrLen) == 0)
749 if ((strncasecmp((char *)parseIter.valPos, OIC_SEC_TRUE, parseIter.valLen) == 0) &&
754 else if ((strncasecmp((char *)parseIter.valPos, OIC_SEC_FALSE, parseIter.valLen) == 0)
761 #ifdef _ENABLE_MULTIPLE_OWNER_
762 if (strncasecmp((char *)parseIter.attrPos, OIC_JSON_MOM_NAME, strlen(OIC_JSON_MOM_NAME)) == 0)
765 OicSecMomType_t momMode = (OicSecMomType_t)(parseIter.valPos[0] - CHAR_ZERO);
766 if(NULL != gDoxm->mom && momMode != gDoxm->mom->mode)
768 if(GetNextQuery(&parseIter))
770 if (strncasecmp((char *)parseIter.attrPos, OIC_JSON_OWNED_NAME, parseIter.attrLen) == 0)
772 if ((strncasecmp((char *)parseIter.valPos, OIC_SEC_TRUE, parseIter.valLen) == 0) &&
782 #endif //_ENABLE_MULTIPLE_OWNER_
784 if (strncasecmp((char *)parseIter.attrPos, OIC_JSON_DEVICE_ID_NAME, parseIter.attrLen) == 0)
787 OicUuid_t subject = {.id={0}};
789 memcpy(subject.id, parseIter.valPos, parseIter.valLen);
790 if (0 == memcmp(&gDoxm->deviceID.id, &subject.id, sizeof(gDoxm->deviceID.id)))
792 bDeviceIDMatch = true;
796 if (strncasecmp((char *)parseIter.attrPos, OC_RSRVD_INTERFACE, parseIter.attrLen) == 0)
798 bInterfaceQry = true;
799 if ((strncasecmp((char *)parseIter.valPos, OC_RSRVD_INTERFACE_DEFAULT, parseIter.valLen) == 0))
801 bInterfaceMatch = true;
803 return (bInterfaceQry ? bInterfaceMatch: true);
807 #ifdef _ENABLE_MULTIPLE_OWNER_
808 return ((bOwnedQry ? bOwnedMatch : true) &&
809 (bDeviceIDQry ? bDeviceIDMatch : true) &&
810 (bMotQry ? bMotMatch : true));
812 return ((bOwnedQry ? bOwnedMatch : true) &&
813 (bDeviceIDQry ? bDeviceIDMatch : true));
814 #endif //_ENABLE_MULTIPLE_OWNER_
817 static OCEntityHandlerResult HandleDoxmGetRequest (const OCEntityHandlerRequest * ehRequest)
819 OCEntityHandlerResult ehRet = OC_EH_OK;
821 OIC_LOG(DEBUG, TAG, "Doxm EntityHandle processing GET request");
823 //Checking if Get request is a query.
824 if (ehRequest->query)
826 OIC_LOG_V(DEBUG,TAG,"query:%s",ehRequest->query);
827 OIC_LOG(DEBUG, TAG, "HandleDoxmGetRequest processing query");
828 if (!ValidateQuery(ehRequest->query))
835 * For GET or Valid Query request return doxm resource CBOR payload.
836 * For non-valid query return NULL json payload.
837 * A device will 'always' have a default Doxm, so DoxmToCBORPayload will
838 * return valid doxm resource json.
840 uint8_t *payload = NULL;
843 if (ehRet == OC_EH_OK)
845 if (OC_STACK_OK != DoxmToCBORPayload(gDoxm, &payload, &size, false))
847 OIC_LOG(WARNING, TAG, "DoxmToCBORPayload failed in HandleDoxmGetRequest");
851 OIC_LOG(DEBUG, TAG, "Send payload for doxm GET request");
852 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
854 // Send response payload to request originator
855 ehRet = ((SendSRMResponse(ehRequest, ehRet, payload, size)) == OC_STACK_OK) ?
856 OC_EH_OK : OC_EH_ERROR;
863 static void updateWriteableProperty(const OicSecDoxm_t* src, OicSecDoxm_t* dst)
868 dst->oxmSel = src->oxmSel;
871 memcpy(&(dst->owner), &(src->owner), sizeof(OicUuid_t));
874 memcpy(&(dst->rownerID), &(src->rownerID), sizeof(OicUuid_t));
876 //Update owned status
877 if(dst->owned != src->owned)
879 dst->owned = src->owned;
885 OicSecOxm_t* tempOxm = (OicSecOxm_t*)OICMalloc(sizeof(OicSecOxm_t) * src->oxmLen);
888 for(size_t i = 0; i < src->oxmLen; i++)
890 tempOxm[i] = src->oxm[i];
895 dst->oxmLen = src->oxmLen;
899 #ifdef _ENABLE_MULTIPLE_OWNER_
902 OIC_LOG(DEBUG, TAG, "dectected 'mom' property");
905 dst->mom = (OicSecMom_t*)OICCalloc(1, sizeof(OicSecMom_t));
908 dst->mom->mode = src->mom->mode;
912 #endif //_ENABLE_MULTIPLE_OWNER_
916 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
917 #ifdef _ENABLE_MULTIPLE_OWNER_
919 * Callback function to handle MOT DTLS handshake result.
920 * @param[out] object remote device information.
921 * @param[out] errorInfo CA Error information.
923 void MultipleOwnerDTLSHandshakeCB(const CAEndpoint_t *object,
924 const CAErrorInfo_t *errorInfo)
926 OIC_LOG(DEBUG, TAG, "IN MultipleOwnerDTLSHandshakeCB");
928 if(CA_STATUS_OK == errorInfo->result)
930 const CASecureEndpoint_t* authenticatedSubOwnerInfo = CAGetSecureEndpointData(object);
931 if(authenticatedSubOwnerInfo)
933 OicSecSubOwner_t* subOwnerInst = NULL;
934 LL_FOREACH(gDoxm->subOwners, subOwnerInst)
936 if(0 == memcmp(subOwnerInst->uuid.id,
937 authenticatedSubOwnerInfo->identity.id,
938 authenticatedSubOwnerInfo->identity.id_length))
944 if(NULL == subOwnerInst)
946 subOwnerInst = (OicSecSubOwner_t*)OICCalloc(1, sizeof(OicSecSubOwner_t));
949 OIC_LOG(DEBUG, TAG, "Adding New SubOwner");
950 memcpy(subOwnerInst->uuid.id, authenticatedSubOwnerInfo->identity.id,
951 authenticatedSubOwnerInfo->identity.id_length);
952 LL_APPEND(gDoxm->subOwners, subOwnerInst);
953 if(!UpdatePersistentStorage(gDoxm))
955 OIC_LOG(ERROR, TAG, "Failed to register SubOwner UUID into Doxm");
962 if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskCredentials))
964 OIC_LOG(WARNING, TAG, "Failed to revert the DTLS credential handler");
967 OIC_LOG(DEBUG, TAG, "OUT MultipleOwnerDTLSHandshakeCB");
969 #endif //_ENABLE_MULTIPLE_OWNER_
970 #endif // defined(__WITH_DTLS__) || defined (__WITH_TLS__)
972 static OCEntityHandlerResult HandleDoxmPostRequest(const OCEntityHandlerRequest * ehRequest)
974 OIC_LOG (DEBUG, TAG, "Doxm EntityHandle processing POST request");
975 OCEntityHandlerResult ehRet = OC_EH_ERROR;
976 OicUuid_t emptyOwner = {.id = {0} };
977 static uint16_t previousMsgId = 0;
980 * Convert CBOR Doxm data into binary. This will also validate
981 * the Doxm data received.
983 OicSecDoxm_t *newDoxm = NULL;
985 if (ehRequest->payload)
987 uint8_t *payload = ((OCSecurityPayload *)ehRequest->payload)->securityData;
988 size_t size = ((OCSecurityPayload *)ehRequest->payload)->payloadSize;
989 bool roParsed = false;
990 OCStackResult res = CBORPayloadToDoxmBin(payload, size, &newDoxm, &roParsed);
991 if (newDoxm && OC_STACK_OK == res)
993 // Check request on RO property
994 if (true == roParsed)
996 OIC_LOG(ERROR, TAG, "Not acceptable request because of read-only propertys");
997 ehRet = OC_EH_NOT_ACCEPTABLE;
1002 if (true == gDoxm->owned)
1004 //Update gDoxm based on newDoxm
1005 updateWriteableProperty(newDoxm, gDoxm);
1007 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
1008 #ifdef _ENABLE_MULTIPLE_OWNER_
1012 if(OIC_MULTIPLE_OWNER_DISABLE != gDoxm->mom->mode)
1014 CAResult_t caRes = CA_STATUS_FAILED;
1015 if(OIC_PRECONFIG_PIN == gDoxm->oxmSel || OIC_RANDOM_DEVICE_PIN == gDoxm->oxmSel)
1017 caRes = CAEnableAnonECDHCipherSuite(false);
1018 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1019 OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");
1021 caRes = CASelectCipherSuite((uint16_t)TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256, ehRequest->devAddr.adapter);
1022 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1023 OIC_LOG(INFO, TAG, "ECDHE_PSK CipherSuite will be used for MOT");
1025 //Set the device id to derive temporal PSK
1026 SetUuidForPinBasedOxm(&gDoxm->deviceID);
1030 OIC_LOG(WARNING, TAG, "Unsupported OxM for Multiple Ownership Transfer.");
1033 CAregisterSslHandshakeCallback(MultipleOwnerDTLSHandshakeCB);
1037 //if MOM is disabled, revert the DTLS handshake callback
1038 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(NULL))
1040 OIC_LOG(WARNING, TAG, "Error while revert the DTLS Handshake Callback.");
1045 if(newDoxm->subOwners)
1047 OicSecSubOwner_t* subowner = NULL;
1048 OicSecSubOwner_t* temp = NULL;
1050 OIC_LOG(DEBUG, TAG, "dectected 'subowners' property");
1052 if(gDoxm->subOwners)
1054 LL_FOREACH_SAFE(gDoxm->subOwners, subowner, temp)
1056 LL_DELETE(gDoxm->subOwners, subowner);
1063 LL_FOREACH_SAFE(newDoxm->subOwners, subowner, temp)
1065 LL_DELETE(newDoxm->subOwners, subowner);
1066 LL_APPEND(gDoxm->subOwners, subowner);
1069 #endif //_ENABLE_MULTIPLE_OWNER_
1070 #endif // defined(__WITH_DTLS__) || defined (__WITH_TLS__)
1072 //Update new state in persistent storage
1073 if (UpdatePersistentStorage(gDoxm) == true)
1079 OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
1080 ehRet = OC_EH_ERROR;
1086 if ((false == gDoxm->owned) && (false == newDoxm->owned))
1088 if (OIC_JUST_WORKS == newDoxm->oxmSel)
1091 * If current state of the device is un-owned, enable
1092 * anonymous ECDH cipher in tinyDTLS so that Provisioning
1093 * tool can initiate JUST_WORKS ownership transfer process.
1095 if (memcmp(&(newDoxm->owner), &emptyOwner, sizeof(OicUuid_t)) == 0)
1097 OIC_LOG (INFO, TAG, "Doxm EntityHandle enabling AnonECDHCipherSuite");
1098 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1099 ehRet = (CAEnableAnonECDHCipherSuite(true) == CA_STATUS_OK) ? OC_EH_OK : OC_EH_ERROR;
1100 #endif // __WITH_DTLS__ or __WITH_TLS__
1105 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1106 //Save the owner's UUID to derive owner credential
1107 memcpy(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t));
1109 // Update new state in persistent storage
1110 if (true == UpdatePersistentStorage(gDoxm))
1116 OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
1117 ehRet = OC_EH_ERROR;
1121 * Disable anonymous ECDH cipher in tinyDTLS since device is now
1124 CAResult_t caRes = CA_STATUS_OK;
1125 caRes = CAEnableAnonECDHCipherSuite(false);
1126 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1127 OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");
1129 #endif // __WITH_DTLS__ or __WITH_TLS__
1132 else if (OIC_RANDOM_DEVICE_PIN == newDoxm->oxmSel)
1135 * If current state of the device is un-owned, enable
1136 * anonymous ECDH cipher in tinyDTLS so that Provisioning
1137 * tool can initiate JUST_WORKS ownership transfer process.
1139 if(memcmp(&(newDoxm->owner), &emptyOwner, sizeof(OicUuid_t)) == 0)
1141 gDoxm->oxmSel = newDoxm->oxmSel;
1142 //Update new state in persistent storage
1143 if ((UpdatePersistentStorage(gDoxm) == true))
1149 OIC_LOG(WARNING, TAG, "Failed to update DOXM in persistent storage");
1150 ehRet = OC_EH_ERROR;
1153 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1154 CAResult_t caRes = CA_STATUS_OK;
1156 caRes = CAEnableAnonECDHCipherSuite(false);
1157 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1158 OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");
1160 caRes = CASelectCipherSuite(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256,
1161 ehRequest->devAddr.adapter);
1162 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1164 char ranPin[OXM_RANDOM_PIN_MAX_SIZE + 1] = {0};
1165 //TODO ehRequest->messageID for copa over TCP always is null. Find reason why.
1166 if(ehRequest->devAddr.adapter == OC_ADAPTER_IP && previousMsgId != ehRequest->messageID)
1168 if(OC_STACK_OK == GeneratePin(ranPin, sizeof(ranPin)))
1170 //Set the device id to derive temporal PSK
1171 SetUuidForPinBasedOxm(&gDoxm->deviceID);
1174 * Since PSK will be used directly by DTLS layer while PIN based ownership transfer,
1175 * Credential should not be saved into SVR.
1176 * For this reason, use a temporary get_psk_info callback to random PIN OxM.
1178 caRes = CAregisterPskCredentialsHandler(GetDtlsPskForRandomPinOxm);
1179 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1184 OIC_LOG(ERROR, TAG, "Failed to generate random PIN");
1185 ehRet = OC_EH_ERROR;
1188 else if(previousMsgId != ehRequest->messageID)
1190 if(OC_STACK_OK == GeneratePin(ranPin, sizeof(ranPin)))
1192 //Set the device id to derive temporal PSK
1193 SetUuidForPinBasedOxm(&gDoxm->deviceID);
1196 * Since PSK will be used directly by DTLS layer while PIN based ownership transfer,
1197 * Credential should not be saved into SVR.
1198 * For this reason, use a temporary get_psk_info callback to random PIN OxM.
1200 caRes = CAregisterPskCredentialsHandler(GetDtlsPskForRandomPinOxm);
1201 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1206 OIC_LOG(ERROR, TAG, "Failed to generate random PIN");
1207 ehRet = OC_EH_ERROR;
1211 #endif // __WITH_DTLS__ or __WITH_TLS__
1213 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1216 //Save the owner's UUID to derive owner credential
1217 memcpy(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t));
1219 //Update new state in persistent storage
1220 if (UpdatePersistentStorage(gDoxm) == true)
1226 OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
1227 ehRet = OC_EH_ERROR;
1230 #endif // __WITH_DTLS__ or __WITH_TLS__
1235 * When current state of the device is un-owned and Provisioning
1236 * Tool is attempting to change the state to 'Owned' with a
1237 * qualified value for the field 'Owner'
1239 if ((false == gDoxm->owned) && (true == newDoxm->owned) &&
1240 (memcmp(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t)) == 0))
1242 //Change the SVR's resource owner as owner device.
1243 OCStackResult ownerRes = SetAclRownerId(&gDoxm->owner);
1244 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
1246 ehRet = OC_EH_ERROR;
1249 ownerRes = SetAmaclRownerId(&gDoxm->owner);
1250 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
1252 ehRet = OC_EH_ERROR;
1255 ownerRes = SetCredRownerId(&gDoxm->owner);
1256 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
1258 ehRet = OC_EH_ERROR;
1261 ownerRes = SetPstatRownerId(&gDoxm->owner);
1262 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
1264 ehRet = OC_EH_ERROR;
1267 ownerRes = SetDpairingRownerId(&gDoxm->owner);
1268 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
1270 ehRet = OC_EH_ERROR;
1273 ownerRes = SetPconfRownerId(&gDoxm->owner);
1274 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
1276 ehRet = OC_EH_ERROR;
1280 gDoxm->owned = true;
1281 memcpy(&gDoxm->rownerID, &gDoxm->owner, sizeof(OicUuid_t));
1283 // Update new state in persistent storage
1284 if (UpdatePersistentStorage(gDoxm))
1286 //Update default ACE of security resource to prevent anonymous user access.
1287 if(OC_STACK_OK == UpdateDefaultSecProvACE())
1293 OIC_LOG(ERROR, TAG, "Failed to remove default ACL for security provisioning");
1294 ehRet = OC_EH_ERROR;
1299 OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
1300 ehRet = OC_EH_ERROR;
1307 if(OC_EH_OK != ehRet)
1311 * If some error is occured while ownership transfer,
1312 * ownership transfer related resource should be revert back to initial status.
1316 if(!gDoxm->owned && previousMsgId != ehRequest->messageID)
1318 OIC_LOG(WARNING, TAG, "The operation failed during handle DOXM request,"\
1319 "DOXM will be reverted.");
1320 RestoreDoxmToInitState();
1321 RestorePstatToInitState();
1326 OIC_LOG(ERROR, TAG, "Invalid DOXM resource.");
1331 previousMsgId = ehRequest->messageID;
1334 //Send payload to request originator
1335 ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
1336 OC_EH_OK : OC_EH_ERROR;
1338 DeleteDoxmBinData(newDoxm);
1343 OCEntityHandlerResult DoxmEntityHandler(OCEntityHandlerFlag flag,
1344 OCEntityHandlerRequest * ehRequest,
1345 void* callbackParam)
1347 (void)callbackParam;
1348 OCEntityHandlerResult ehRet = OC_EH_ERROR;
1350 if(NULL == ehRequest)
1355 if (flag & OC_REQUEST_FLAG)
1357 OIC_LOG(DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
1359 switch (ehRequest->method)
1362 ehRet = HandleDoxmGetRequest(ehRequest);
1366 ehRet = HandleDoxmPostRequest(ehRequest);
1370 ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
1371 OC_EH_OK : OC_EH_ERROR;
1379 OCStackResult CreateDoxmResource()
1381 OCStackResult ret = OCCreateResource(&gDoxmHandle,
1382 OIC_RSRC_TYPE_SEC_DOXM,
1383 OC_RSRVD_INTERFACE_DEFAULT,
1390 if (OC_STACK_OK != ret)
1392 OIC_LOG (FATAL, TAG, "Unable to instantiate Doxm resource");
1393 DeInitDoxmResource();
1399 * Checks if DeviceID is generated during provisioning for the new device.
1400 * If DeviceID is NULL then generates the new DeviceID.
1401 * Once DeviceID is assigned to the device it does not change for the lifetime of the device.
1403 static OCStackResult CheckDeviceID()
1405 OCStackResult ret = OC_STACK_ERROR;
1406 bool validId = false;
1407 for (uint8_t i = 0; i < UUID_LENGTH; i++)
1409 if (gDoxm->deviceID.id[i] != 0)
1418 if (OCGenerateUuid(gDoxm->deviceID.id) != RAND_UUID_OK)
1420 OIC_LOG(FATAL, TAG, "Generate UUID for Server Instance failed!");
1425 if (!UpdatePersistentStorage(gDoxm))
1427 //TODO: After registering PSI handler in all samples, do ret = OC_STACK_OK here.
1428 OIC_LOG(FATAL, TAG, "UpdatePersistentStorage failed!");
1439 * Get the default value.
1441 * @return the default value of doxm, @ref OicSecDoxm_t.
1443 static OicSecDoxm_t* GetDoxmDefault()
1445 OIC_LOG(DEBUG, TAG, "GetDoxmToDefault");
1446 return &gDefaultDoxm;
1449 const OicSecDoxm_t* GetDoxmResourceData()
1454 #if defined(__WITH_DTLS__) && defined(_ENABLE_MULTIPLE_OWNER_)
1456 * Internal API to prepare MOT
1458 static void PrepareMOT(const OicSecDoxm_t* doxm)
1460 OIC_LOG(INFO, TAG, "IN PrepareMOT");
1461 VERIFY_NON_NULL(TAG, doxm, ERROR);
1463 if(true == doxm->owned && NULL != doxm->mom && OIC_MULTIPLE_OWNER_DISABLE != doxm->mom->mode)
1465 CAResult_t caRes = CA_STATUS_FAILED;
1467 OIC_LOG(INFO, TAG, "Multiple Ownership Transfer Enabled!");
1469 if(OIC_PRECONFIG_PIN == doxm->oxmSel)
1471 caRes = CAEnableAnonECDHCipherSuite(false);
1472 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1473 OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");
1475 caRes = CASelectCipherSuite((uint16_t)TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256, CA_ADAPTER_IP);
1476 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1478 caRes = CASelectCipherSuite((uint16_t)TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256, CA_ADAPTER_TCP);
1479 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1481 OIC_LOG(INFO, TAG, "ECDHE_PSK CipherSuite will be used for MOT");
1483 //Set the device id to derive temporal PSK
1484 SetUuidForPinBasedOxm(&doxm->deviceID);
1488 OIC_LOG(ERROR, TAG, "Unsupported OxM for Multiple Ownership Transfer.");
1492 CAregisterSslHandshakeCallback(MultipleOwnerDTLSHandshakeCB);
1495 OIC_LOG(INFO, TAG, "OUT PrepareMOT");
1498 OIC_LOG(WARNING, TAG, "Error in PrepareMOT");
1500 #endif //defined(__WITH_DTLS__) && defined(_ENABLE_MULTIPLE_OWNER_)
1502 OCStackResult InitDoxmResource()
1504 OCStackResult ret = OC_STACK_ERROR;
1506 //Read DOXM resource from PS
1507 uint8_t *data = NULL;
1509 ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_DOXM_NAME, &data, &size);
1510 // If database read failed
1511 if (OC_STACK_OK != ret)
1513 OIC_LOG (DEBUG, TAG, "ReadSVDataFromPS failed");
1517 // Read DOXM resource from PS
1518 ret = CBORPayloadToDoxm(data, size, &gDoxm);
1521 * If SVR database in persistent storage got corrupted or
1522 * is not available for some reason, a default doxm is created
1523 * which allows user to initiate doxm provisioning again.
1525 if ((OC_STACK_OK != ret) || !data || !gDoxm)
1527 gDoxm = GetDoxmDefault();
1530 //In case of the server is shut down unintentionally, we should initialize the owner
1531 if(false == gDoxm->owned)
1533 OicUuid_t emptyUuid = {.id={0}};
1534 memcpy(&gDoxm->owner, &emptyUuid, sizeof(OicUuid_t));
1537 ret = CheckDeviceID();
1538 if (ret == OC_STACK_OK)
1540 OIC_LOG_V(DEBUG, TAG, "Initial Doxm Owned = %d", gDoxm->owned);
1541 //Instantiate 'oic.sec.doxm'
1542 ret = CreateDoxmResource();
1546 OIC_LOG (ERROR, TAG, "CheckDeviceID failed");
1550 #if defined(__WITH_DTLS__) && defined(_ENABLE_MULTIPLE_OWNER_)
1551 //if MOT is enabled, MOT should be prepared.
1552 if(gDoxm && gDoxm->owned)
1556 #endif // defined(__WITH_DTLS__) && defined(_ENABLE_MULTIPLE_OWNER_)
1561 OCStackResult DeInitDoxmResource()
1563 OCStackResult ret = OCDeleteResource(gDoxmHandle);
1564 if (gDoxm != &gDefaultDoxm)
1566 DeleteDoxmBinData(gDoxm);
1570 if (OC_STACK_OK == ret)
1576 return OC_STACK_ERROR;
1580 OCStackResult GetDoxmDeviceID(OicUuid_t *deviceID)
1582 if (deviceID && gDoxm)
1584 *deviceID = gDoxm->deviceID;
1587 return OC_STACK_ERROR;
1590 OCStackResult GetDoxmIsOwned(bool *isOwned)
1592 if (isOwned && gDoxm)
1594 *isOwned = gDoxm->owned;
1597 return OC_STACK_ERROR;
1600 OCStackResult SetDoxmDeviceID(const OicUuid_t *deviceID)
1604 if(NULL == deviceID)
1606 return OC_STACK_INVALID_PARAM;
1610 OIC_LOG(ERROR, TAG, "Doxm resource is not initialized.");
1611 return OC_STACK_NO_RESOURCE;
1614 //Check the device's OTM state
1616 #ifdef __WITH_DTLS__
1617 //for normal device.
1618 if(true == gDoxm->owned &&
1619 memcmp(gDoxm->deviceID.id, gDoxm->owner.id, sizeof(gDoxm->owner.id)) != 0)
1621 OIC_LOG(ERROR, TAG, "This device owned by owner's device.");
1622 OIC_LOG(ERROR, TAG, "Device UUID cannot be changed to guarantee the reliability of the connection.");
1623 return OC_STACK_ERROR;
1625 #endif //__WITH_DTLS
1627 //Save the previous UUID
1629 memcpy(tempUuid.id, gDoxm->deviceID.id, sizeof(tempUuid.id));
1632 memcpy(gDoxm->deviceID.id, deviceID->id, sizeof(deviceID->id));
1635 memcpy(gDoxm->owner.id, deviceID->id, sizeof(deviceID->id));
1636 memcpy(gDoxm->rownerID.id, deviceID->id, sizeof(deviceID->id));
1640 if(!UpdatePersistentStorage(gDoxm))
1642 //revert UUID in case of update error
1643 memcpy(gDoxm->deviceID.id, tempUuid.id, sizeof(tempUuid.id));
1646 memcpy(gDoxm->owner.id, tempUuid.id, sizeof(tempUuid.id));
1647 memcpy(gDoxm->rownerID.id, tempUuid.id, sizeof(tempUuid.id));
1650 OIC_LOG(ERROR, TAG, "Failed to update persistent storage");
1651 return OC_STACK_ERROR;
1656 OCStackResult GetDoxmDevOwnerId(OicUuid_t *devownerid)
1658 OCStackResult retVal = OC_STACK_ERROR;
1661 OIC_LOG_V(DEBUG, TAG, "GetDoxmDevOwnerId(): gDoxm owned = %d.", \
1665 *devownerid = gDoxm->owner;
1666 retVal = OC_STACK_OK;
1672 OCStackResult GetDoxmRownerId(OicUuid_t *rowneruuid)
1674 OCStackResult retVal = OC_STACK_ERROR;
1679 *rowneruuid = gDoxm->rownerID;
1680 retVal = OC_STACK_OK;
1686 #ifdef _ENABLE_MULTIPLE_OWNER_
1688 * Compare the UUID to SubOwner.
1690 * @param[in] uuid device UUID
1692 * @return true if context->subjectId exist subowner list, else false.
1694 bool IsSubOwner(const OicUuid_t* uuid)
1696 bool retVal = false;
1703 if (gDoxm && gDoxm->subOwners)
1705 OicSecSubOwner_t* subOwner = NULL;
1706 LL_FOREACH(gDoxm->subOwners, subOwner)
1708 if(memcmp(subOwner->uuid.id, uuid->id, sizeof(uuid->id)) == 0)
1716 #endif //_ENABLE_MULTIPLE_OWNER_
1719 * Function to restore doxm resurce to initial status.
1720 * This function will use in case of error while ownership transfer
1722 void RestoreDoxmToInitState()
1726 OIC_LOG(INFO, TAG, "DOXM resource will revert back to initial status.");
1728 OicUuid_t emptyUuid = {.id={0}};
1729 memcpy(&(gDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
1730 gDoxm->owned = false;
1731 gDoxm->oxmSel = OIC_JUST_WORKS;
1733 if(!UpdatePersistentStorage(gDoxm))
1735 OIC_LOG(ERROR, TAG, "Failed to revert DOXM in persistent storage");