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 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
56 #include "pkix_interface.h"
59 #define TAG "OIC_SRM_DOXM"
60 #define CHAR_ZERO ('0')
62 /** Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
63 * The value of payload size is increased until reaching belox max cbor size. */
64 static const uint16_t CBOR_SIZE = 512;
66 /** Max cbor size payload. */
67 static const uint16_t CBOR_MAX_SIZE = 4400;
69 static OicSecDoxm_t *gDoxm = NULL;
70 static OCResourceHandle gDoxmHandle = NULL;
72 static OicSecOxm_t gOicSecDoxmJustWorks = OIC_JUST_WORKS;
73 static OicSecDoxm_t gDefaultDoxm =
75 NULL, /* OicUrn_t *oxmType */
76 0, /* size_t oxmTypeLen */
77 &gOicSecDoxmJustWorks, /* uint16_t *oxm */
78 1, /* size_t oxmLen */
79 OIC_JUST_WORKS, /* uint16_t oxmSel */
80 SYMMETRIC_PAIR_WISE_KEY,/* OicSecCredType_t sct */
81 false, /* bool owned */
82 {.id = {0}}, /* OicUuid_t deviceID */
84 {.id = {0}}, /* OicUuid_t owner */
85 #ifdef _ENABLE_MULTIPLE_OWNER_
86 NULL, /* OicSecSubOwner_t sub-owner list */
87 NULL, /* OicSecMomType_t multiple owner mode */
88 #endif //_ENABLE_MULTIPLE_OWNER_
89 {.id = {0}}, /* OicUuid_t rownerID */
93 * This method is internal method.
94 * the param roParsed is optionally used to know whether cborPayload has
95 * at least read only property value or not.
97 static OCStackResult CBORPayloadToDoxmBin(const uint8_t *cborPayload, size_t size,
98 OicSecDoxm_t **doxm, bool *roParsed);
100 void DeleteDoxmBinData(OicSecDoxm_t* doxm)
105 for (size_t i = 0; i < doxm->oxmTypeLen; i++)
107 OICFree(doxm->oxmType[i]);
109 OICFree(doxm->oxmType);
114 #ifdef _ENABLE_MULTIPLE_OWNER_
118 //clean sub-owner list
119 if(NULL != doxm->subOwners)
121 OicSecSubOwner_t* subowner = NULL;
122 OicSecSubOwner_t* temp = NULL;
123 LL_FOREACH_SAFE(doxm->subOwners, subowner, temp)
125 LL_DELETE(doxm->subOwners, subowner);
129 #endif //_ENABLE_MULTIPLE_OWNER_
136 OCStackResult DoxmToCBORPayload(const OicSecDoxm_t *doxm, uint8_t **payload, size_t *size,
139 if (NULL == doxm || NULL == payload || NULL != *payload || NULL == size)
141 return OC_STACK_INVALID_PARAM;
143 size_t cborLen = *size;
151 OCStackResult ret = OC_STACK_ERROR;
155 char* strUuid = NULL;
157 int64_t cborEncoderResult = CborNoError;
159 uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
160 VERIFY_NON_NULL(TAG, outPayload, ERROR);
161 cbor_encoder_init(&encoder, outPayload, cborLen, 0);
163 cborEncoderResult = cbor_encoder_create_map(&encoder, &doxmMap, CborIndefiniteLength);
164 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Map.");
166 //OxmType -- Not Mandatory
167 if (doxm->oxmTypeLen > 0)
170 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OXM_TYPE_NAME,
171 strlen(OIC_JSON_OXM_TYPE_NAME));
172 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmType Tag.");
173 cborEncoderResult = cbor_encoder_create_array(&doxmMap, &oxmType, doxm->oxmTypeLen);
174 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmType Array.");
176 for (size_t i = 0; i < doxm->oxmTypeLen; i++)
178 cborEncoderResult = cbor_encode_text_string(&oxmType, doxm->oxmType[i],
179 strlen(doxm->oxmType[i]));
180 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmType Value.");
182 cborEncoderResult = cbor_encoder_close_container(&doxmMap, &oxmType);
183 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing oxmType.");
186 //Oxm -- Not Mandatory
187 if (doxm->oxmLen > 0 && false == rwOnly)
190 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OXMS_NAME,
191 strlen(OIC_JSON_OXMS_NAME));
192 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmName Tag.");
193 cborEncoderResult = cbor_encoder_create_array(&doxmMap, &oxm, doxm->oxmLen);
194 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmName Array.");
196 for (size_t i = 0; i < doxm->oxmLen; i++)
198 cborEncoderResult = cbor_encode_int(&oxm, doxm->oxm[i]);
199 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding oxmName Value");
201 cborEncoderResult = cbor_encoder_close_container(&doxmMap, &oxm);
202 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing oxmName.");
205 //OxmSel -- Mandatory
206 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OXM_SEL_NAME,
207 strlen(OIC_JSON_OXM_SEL_NAME));
208 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Sel Tag.");
209 cborEncoderResult = cbor_encode_int(&doxmMap, doxm->oxmSel);
210 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Sel Value.");
215 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_SUPPORTED_CRED_TYPE_NAME,
216 strlen(OIC_JSON_SUPPORTED_CRED_TYPE_NAME));
217 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Type Tag");
218 cborEncoderResult = cbor_encode_int(&doxmMap, doxm->sct);
219 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Type Value.");
223 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_OWNED_NAME,
224 strlen(OIC_JSON_OWNED_NAME));
225 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owned Tag.");
226 cborEncoderResult = cbor_encode_boolean(&doxmMap, doxm->owned);
227 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owned Value.");
231 //DeviceId -- Mandatory
232 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_DEVICE_ID_NAME,
233 strlen(OIC_JSON_DEVICE_ID_NAME));
234 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Tag.");
235 ret = ConvertUuidToStr(&doxm->deviceID, &strUuid);
236 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
237 cborEncoderResult = cbor_encode_text_string(&doxmMap, strUuid, strlen(strUuid));
238 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Value.");
243 #ifdef _ENABLE_MULTIPLE_OWNER_
244 //Device SubOwnerID -- Not Mandatory
247 size_t subOwnerLen = 0;
248 OicSecSubOwner_t* subOwner = NULL;
249 LL_FOREACH(doxm->subOwners, subOwner)
254 CborEncoder subOwners;
255 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_SUBOWNERID_NAME,
256 strlen(OIC_JSON_SUBOWNERID_NAME));
257 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SubOwnerId Tag.");
258 cborEncoderResult = cbor_encoder_create_array(&doxmMap, &subOwners, subOwnerLen);
259 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SubOwner Array.");
262 LL_FOREACH(doxm->subOwners, subOwner)
264 char* strUuid = NULL;
265 ret = ConvertUuidToStr(&subOwner->uuid, &strUuid);
266 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
267 cborEncoderResult = cbor_encode_text_string(&subOwners, strUuid, strlen(strUuid));
269 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SubOwnerId Value");
271 cborEncoderResult = cbor_encoder_close_container(&doxmMap, &subOwners);
272 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing SubOwnerId.");
275 //Multiple Owner Mode -- Not Mandatory
278 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_MOM_NAME,
279 strlen(OIC_JSON_MOM_NAME));
280 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding mom Tag");
281 cborEncoderResult = cbor_encode_int(&doxmMap, (int64_t)doxm->mom->mode);
282 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding mom Value.");
284 #endif //_ENABLE_MULTIPLE_OWNER_
286 //devownerid -- Mandatory
287 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_DEVOWNERID_NAME,
288 strlen(OIC_JSON_DEVOWNERID_NAME));
289 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owner Id Tag.");
290 ret = ConvertUuidToStr(&doxm->owner, &strUuid);
291 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
292 cborEncoderResult = cbor_encode_text_string(&doxmMap, strUuid, strlen(strUuid));
293 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Owner Id Value.");
297 //ROwner -- Mandatory
298 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_ROWNERID_NAME,
299 strlen(OIC_JSON_ROWNERID_NAME));
300 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Tag.");
301 ret = ConvertUuidToStr(&doxm->rownerID, &strUuid);
302 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
303 cborEncoderResult = cbor_encode_text_string(&doxmMap, strUuid, strlen(strUuid));
304 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Value.");
310 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_RT_NAME,
311 strlen(OIC_JSON_RT_NAME));
312 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Name Tag.");
313 cborEncoderResult = cbor_encoder_create_array(&doxmMap, &rtArray, 1);
314 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Value.");
315 for (size_t i = 0; i < 1; i++)
317 cborEncoderResult = cbor_encode_text_string(&rtArray, OIC_RSRC_TYPE_SEC_DOXM,
318 strlen(OIC_RSRC_TYPE_SEC_DOXM));
319 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding RT Value.");
321 cborEncoderResult = cbor_encoder_close_container(&doxmMap, &rtArray);
322 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing RT.");
326 cborEncoderResult = cbor_encode_text_string(&doxmMap, OIC_JSON_IF_NAME,
327 strlen(OIC_JSON_IF_NAME));
328 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Name Tag.");
329 cborEncoderResult = cbor_encoder_create_array(&doxmMap, &ifArray, 1);
330 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Value.");
331 for (size_t i = 0; i < 1; i++)
333 cborEncoderResult = cbor_encode_text_string(&ifArray, OC_RSRVD_INTERFACE_DEFAULT,
334 strlen(OC_RSRVD_INTERFACE_DEFAULT));
335 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding IF Value.");
337 cborEncoderResult = cbor_encoder_close_container(&doxmMap, &ifArray);
338 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing IF.");
340 cborEncoderResult = cbor_encoder_close_container(&encoder, &doxmMap);
341 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing DoxmMap.");
343 if (CborNoError == cborEncoderResult)
345 *size = cbor_encoder_get_buffer_size(&encoder, outPayload);
346 *payload = outPayload;
350 if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
352 OIC_LOG(DEBUG, TAG, "Memory getting reallocated.");
353 // reallocate and try again!
355 // Since the allocated initial memory failed, double the memory.
356 cborLen += cbor_encoder_get_buffer_size(&encoder, encoder.end);
357 OIC_LOG_V(DEBUG, TAG, "Doxm reallocation size : %zd.", cborLen);
358 cborEncoderResult = CborNoError;
359 ret = DoxmToCBORPayload(doxm, payload, &cborLen, rwOnly);
363 if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret))
369 ret = OC_STACK_ERROR;
375 OCStackResult CBORPayloadToDoxm(const uint8_t *cborPayload, size_t size,
376 OicSecDoxm_t **secDoxm)
378 return CBORPayloadToDoxmBin(cborPayload, size, secDoxm, NULL);
381 static OCStackResult CBORPayloadToDoxmBin(const uint8_t *cborPayload, size_t size,
382 OicSecDoxm_t **secDoxm, bool *roParsed)
384 if (NULL == cborPayload || NULL == secDoxm || NULL != *secDoxm || 0 == size)
386 return OC_STACK_INVALID_PARAM;
389 OCStackResult ret = OC_STACK_ERROR;
393 CborError cborFindResult = CborNoError;
394 char* strUuid = NULL;
398 cbor_parser_init(cborPayload, size, 0, &parser, &doxmCbor);
400 OicSecDoxm_t *doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(*doxm));
401 VERIFY_NON_NULL(TAG, doxm, ERROR);
403 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_OXM_TYPE_NAME, &doxmMap);
404 //OxmType -- not Mandatory
405 if (CborNoError == cborFindResult && cbor_value_is_array(&doxmMap))
409 cborFindResult = cbor_value_get_array_length(&doxmMap, &doxm->oxmTypeLen);
410 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding oxmTypeLen.");
411 VERIFY_SUCCESS(TAG, doxm->oxmTypeLen != 0, ERROR);
413 doxm->oxmType = (OicUrn_t *)OICCalloc(doxm->oxmTypeLen, sizeof(*doxm->oxmType));
414 VERIFY_NON_NULL(TAG, doxm->oxmType, ERROR);
416 cborFindResult = cbor_value_enter_container(&doxmMap, &oxmType);
417 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering oxmType Array.")
421 while (cbor_value_is_valid(&oxmType) && cbor_value_is_text_string(&oxmType))
423 cborFindResult = cbor_value_dup_text_string(&oxmType, &doxm->oxmType[i++],
425 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding omxType text string.");
426 cborFindResult = cbor_value_advance(&oxmType);
427 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing oxmType.");
431 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_OXMS_NAME, &doxmMap);
432 //Oxm -- not Mandatory
433 if (CborNoError == cborFindResult && cbor_value_is_array(&doxmMap))
436 cborFindResult = cbor_value_get_array_length(&doxmMap, &doxm->oxmLen);
437 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding oxmName array Length.");
438 VERIFY_SUCCESS(TAG, doxm->oxmLen != 0, ERROR);
440 doxm->oxm = (OicSecOxm_t *)OICCalloc(doxm->oxmLen, sizeof(*doxm->oxm));
441 VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
443 cborFindResult = cbor_value_enter_container(&doxmMap, &oxm);
444 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering oxmName Array.")
447 while (cbor_value_is_valid(&oxm) && cbor_value_is_integer(&oxm))
451 cborFindResult = cbor_value_get_int(&oxm, &tmp);
452 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding oxmName Value")
453 doxm->oxm[i++] = (OicSecOxm_t)tmp;
454 cborFindResult = cbor_value_advance(&oxm);
455 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing oxmName.")
465 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
466 doxm->oxm = (OicSecOxm_t *) OICCalloc(gDoxm->oxmLen, sizeof(*doxm->oxm));
467 VERIFY_NON_NULL(TAG, doxm->oxm, ERROR);
468 doxm->oxmLen = gDoxm->oxmLen;
469 for (size_t i = 0; i < gDoxm->oxmLen; i++)
471 doxm->oxm[i] = gDoxm->oxm[i];
475 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_OXM_SEL_NAME, &doxmMap);
476 if (CborNoError == cborFindResult && cbor_value_is_integer(&doxmMap))
480 cborFindResult = cbor_value_get_int(&doxmMap, &oxmSel);
481 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Sel Name Value.")
482 doxm->oxmSel = (OicSecOxm_t)oxmSel;
484 else // PUT/POST JSON may not have oxmsel so set it to the gDoxm->oxmSel
486 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
487 doxm->oxmSel = gDoxm->oxmSel;
490 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_SUPPORTED_CRED_TYPE_NAME, &doxmMap);
491 if (CborNoError == cborFindResult && cbor_value_is_integer(&doxmMap))
495 cborFindResult = cbor_value_get_int(&doxmMap, &sct);
496 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Sct Name Value.")
497 doxm->sct = (OicSecCredType_t)sct;
504 else // PUT/POST JSON may not have sct so set it to the gDoxm->sct
506 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
507 doxm->sct = gDoxm->sct;
510 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_OWNED_NAME, &doxmMap);
511 if (CborNoError == cborFindResult && cbor_value_is_boolean(&doxmMap))
513 cborFindResult = cbor_value_get_boolean(&doxmMap, &doxm->owned);
514 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Owned Value.")
516 else // PUT/POST JSON may not have owned so set it to the gDomx->owned
518 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
519 doxm->owned = gDoxm->owned;
522 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_DEVICE_ID_NAME, &doxmMap);
523 if (CborNoError == cborFindResult && cbor_value_is_text_string(&doxmMap))
525 cborFindResult = cbor_value_dup_text_string(&doxmMap, &strUuid , &len, NULL);
526 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Device Id Value.");
527 ret = ConvertStrToUuid(strUuid , &doxm->deviceID);
528 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
534 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
535 memcpy(doxm->deviceID.id, &gDoxm->deviceID.id, sizeof(doxm->deviceID.id));
538 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_DEVOWNERID_NAME, &doxmMap);
539 if (CborNoError == cborFindResult && cbor_value_is_text_string(&doxmMap))
541 cborFindResult = cbor_value_dup_text_string(&doxmMap, &strUuid , &len, NULL);
542 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Owner Value.");
543 ret = ConvertStrToUuid(strUuid , &doxm->owner);
544 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
550 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
551 memcpy(doxm->owner.id, gDoxm->owner.id, sizeof(doxm->owner.id));
554 #ifdef _ENABLE_MULTIPLE_OWNER_
555 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_MOM_NAME, &doxmMap);
556 if(CborNoError == cborFindResult && cbor_value_is_integer(&doxmMap))
559 cborFindResult = cbor_value_get_int(&doxmMap, &mode);
560 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding mom Name Value.")
561 if(NULL == doxm->mom)
563 doxm->mom = (OicSecMom_t*)OICCalloc(1, sizeof(OicSecMom_t));
564 VERIFY_NON_NULL(TAG, doxm->mom, ERROR);
566 doxm->mom->mode = (OicSecMomType_t)mode;
568 else if(NULL != gDoxm && NULL != gDoxm->mom)
570 // PUT/POST JSON may not have 'mom' so set it to the gDomx->mom
571 if(NULL == doxm->mom)
573 doxm->mom = (OicSecMom_t*)OICCalloc(1, sizeof(OicSecMom_t));
574 VERIFY_NON_NULL(TAG, doxm->mom, ERROR);
576 doxm->mom->mode = gDoxm->mom->mode;
579 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_SUBOWNERID_NAME, &doxmMap);
580 if(CborNoError == cborFindResult && cbor_value_is_array(&doxmMap))
582 size_t subOwnerLen = 0;
583 CborValue subOwnerCbor;
584 cborFindResult = cbor_value_get_array_length(&doxmMap, &subOwnerLen);
585 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SubOwner array Length.");
586 VERIFY_SUCCESS(TAG, 0 != subOwnerLen, ERROR);
588 cborFindResult = cbor_value_enter_container(&doxmMap, &subOwnerCbor);
589 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering SubOwner Array.")
591 while (cbor_value_is_valid(&subOwnerCbor) && cbor_value_is_text_string(&subOwnerCbor))
593 OCStackResult convertRes = OC_STACK_ERROR;
594 OicSecSubOwner_t* subOwner = NULL;
595 char* strUuid = NULL;
598 cborFindResult = cbor_value_dup_text_string(&subOwnerCbor, &strUuid, &uuidLen, NULL);
599 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SubOwnerId Value");
601 subOwner = (OicSecSubOwner_t*)OICCalloc(1, sizeof(OicSecSubOwner_t));
602 VERIFY_NON_NULL(TAG, subOwner, ERROR);
604 convertRes = ConvertStrToUuid(strUuid, &subOwner->uuid);
605 VERIFY_SUCCESS(TAG, OC_STACK_OK == convertRes, ERROR);
606 subOwner->status = MOT_STATUS_DONE;
607 LL_APPEND(doxm->subOwners, subOwner);
609 cborFindResult = cbor_value_advance(&subOwnerCbor);
610 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing SubOwnerId.")
613 else if(NULL != gDoxm && NULL != gDoxm->subOwners)
615 // PUT/POST JSON may not have 'subOwners' so set it to the gDomx->subOwners
616 OicSecSubOwner_t* subOwnerItor = NULL;
617 LL_FOREACH(gDoxm->subOwners, subOwnerItor)
619 OicSecSubOwner_t* subOwnerId = (OicSecSubOwner_t*)OICCalloc(1, sizeof(OicSecSubOwner_t));
620 VERIFY_NON_NULL(TAG, subOwnerId, ERROR);
622 memcpy(&subOwnerId->uuid, &subOwnerItor->uuid, sizeof(OicUuid_t));
623 subOwnerId->status = MOT_STATUS_DONE;
625 LL_APPEND(doxm->subOwners, subOwnerId);
628 #endif //_ENABLE_MULTIPLE_OWNER_
630 cborFindResult = cbor_value_map_find_value(&doxmCbor, OIC_JSON_ROWNERID_NAME, &doxmMap);
631 if (CborNoError == cborFindResult && cbor_value_is_text_string(&doxmMap))
633 cborFindResult = cbor_value_dup_text_string(&doxmMap, &strUuid , &len, NULL);
634 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Value.");
635 ret = ConvertStrToUuid(strUuid , &doxm->rownerID);
636 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
642 VERIFY_NON_NULL(TAG, gDoxm, ERROR);
643 memcpy(doxm->rownerID.id, gDoxm->rownerID.id, sizeof(doxm->rownerID.id));
650 if (CborNoError != cborFindResult)
652 OIC_LOG (ERROR, TAG, "CBORPayloadToDoxm failed!!!");
653 DeleteDoxmBinData(doxm);
656 ret = OC_STACK_ERROR;
662 * @todo document this function including why code might need to call this.
663 * The current suspicion is that it's not being called as much as it should.
665 static bool UpdatePersistentStorage(OicSecDoxm_t * doxm)
671 // Convert Doxm data into CBOR for update to persistent storage
672 uint8_t *payload = NULL;
674 OCStackResult res = DoxmToCBORPayload(doxm, &payload, &size, false);
675 if (payload && (OC_STACK_OK == res)
676 && (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_DOXM_NAME, payload, size)))
684 if (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_DOXM_NAME, NULL, 0))
693 static bool ValidateQuery(const char * query)
695 // Send doxm resource data if the state of doxm resource
696 // matches with the query parameters.
697 // else send doxm resource data as NULL
698 // TODO Remove this check and rely on Policy Engine
699 // and Provisioning Mode to enforce provisioning-state
700 // access rules. Eventually, the PE and PM code will
701 // not send a request to the /doxm Entity Handler at all
702 // if it should not respond.
703 OIC_LOG (DEBUG, TAG, "In ValidateQuery");
709 bool bOwnedQry = false; // does querystring contains 'owned' query ?
710 bool bOwnedMatch = false; // does 'owned' query value matches with doxm.owned status?
711 bool bDeviceIDQry = false; // does querystring contains 'deviceid' query ?
712 bool bDeviceIDMatch = false; // does 'deviceid' query matches with doxm.deviceid ?
713 bool bInterfaceQry = false; // does querystring contains 'if' query ?
714 bool bInterfaceMatch = false; // does 'if' query matches with oic.if.baseline ?
715 #ifdef _ENABLE_MULTIPLE_OWNER_
716 bool bMotQry = false; // does querystring contains 'mom' and 'owned' query ?
717 bool bMotMatch = false; // does 'mom' query value is not '0' && does query value matches with doxm.owned status?
718 #endif //_ENABLE_MULTIPLE_OWNER_
720 OicParseQueryIter_t parseIter = {.attrPos = NULL};
722 ParseQueryIterInit((unsigned char*)query, &parseIter);
724 while (GetNextQuery(&parseIter))
726 if (strncasecmp((char *)parseIter.attrPos, OIC_JSON_OWNED_NAME, parseIter.attrLen) == 0)
729 if ((strncasecmp((char *)parseIter.valPos, OIC_SEC_TRUE, parseIter.valLen) == 0) &&
734 else if ((strncasecmp((char *)parseIter.valPos, OIC_SEC_FALSE, parseIter.valLen) == 0)
741 #ifdef _ENABLE_MULTIPLE_OWNER_
742 if (strncasecmp((char *)parseIter.attrPos, OIC_JSON_MOM_NAME, strlen(OIC_JSON_MOM_NAME)) == 0)
745 OicSecMomType_t momMode = (OicSecMomType_t)(parseIter.valPos[0] - CHAR_ZERO);
746 if(NULL != gDoxm->mom && momMode != gDoxm->mom->mode)
748 if(GetNextQuery(&parseIter))
750 if (strncasecmp((char *)parseIter.attrPos, OIC_JSON_OWNED_NAME, parseIter.attrLen) == 0)
752 if ((strncasecmp((char *)parseIter.valPos, OIC_SEC_TRUE, parseIter.valLen) == 0) &&
762 #endif //_ENABLE_MULTIPLE_OWNER_
764 if (strncasecmp((char *)parseIter.attrPos, OIC_JSON_DEVICE_ID_NAME, parseIter.attrLen) == 0)
767 OicUuid_t subject = {.id={0}};
769 memcpy(subject.id, parseIter.valPos, parseIter.valLen);
770 if (0 == memcmp(&gDoxm->deviceID.id, &subject.id, sizeof(gDoxm->deviceID.id)))
772 bDeviceIDMatch = true;
776 if (strncasecmp((char *)parseIter.attrPos, OC_RSRVD_INTERFACE, parseIter.attrLen) == 0)
778 bInterfaceQry = true;
779 if ((strncasecmp((char *)parseIter.valPos, OC_RSRVD_INTERFACE_DEFAULT, parseIter.valLen) == 0))
781 bInterfaceMatch = true;
783 return (bInterfaceQry ? bInterfaceMatch: true);
787 #ifdef _ENABLE_MULTIPLE_OWNER_
788 return ((bOwnedQry ? bOwnedMatch : true) &&
789 (bDeviceIDQry ? bDeviceIDMatch : true) &&
790 (bMotQry ? bMotMatch : true));
792 return ((bOwnedQry ? bOwnedMatch : true) &&
793 (bDeviceIDQry ? bDeviceIDMatch : true));
794 #endif //_ENABLE_MULTIPLE_OWNER_
797 static OCEntityHandlerResult HandleDoxmGetRequest (const OCEntityHandlerRequest * ehRequest)
799 OCEntityHandlerResult ehRet = OC_EH_OK;
801 OIC_LOG(DEBUG, TAG, "Doxm EntityHandle processing GET request");
803 //Checking if Get request is a query.
804 if (ehRequest->query)
806 OIC_LOG_V(DEBUG,TAG,"query:%s",ehRequest->query);
807 OIC_LOG(DEBUG, TAG, "HandleDoxmGetRequest processing query");
808 if (!ValidateQuery(ehRequest->query))
815 * For GET or Valid Query request return doxm resource CBOR payload.
816 * For non-valid query return NULL json payload.
817 * A device will 'always' have a default Doxm, so DoxmToCBORPayload will
818 * return valid doxm resource json.
820 uint8_t *payload = NULL;
823 if (ehRet == OC_EH_OK)
825 if (OC_STACK_OK != DoxmToCBORPayload(gDoxm, &payload, &size, false))
827 OIC_LOG(WARNING, TAG, "DoxmToCBORPayload failed in HandleDoxmGetRequest");
831 OIC_LOG(DEBUG, TAG, "Send payload for doxm GET request");
832 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
834 // Send response payload to request originator
835 ehRet = ((SendSRMResponse(ehRequest, ehRet, payload, size)) == OC_STACK_OK) ?
836 OC_EH_OK : OC_EH_ERROR;
843 static void updateWriteableProperty(const OicSecDoxm_t* src, OicSecDoxm_t* dst)
848 dst->oxmSel = src->oxmSel;
851 memcpy(&(dst->owner), &(src->owner), sizeof(OicUuid_t));
854 memcpy(&(dst->rownerID), &(src->rownerID), sizeof(OicUuid_t));
857 memcpy(&(dst->deviceID), &(src->deviceID), sizeof(OicUuid_t));
859 //Update owned status
860 if(dst->owned != src->owned)
862 dst->owned = src->owned;
868 OicSecOxm_t* tempOxm = (OicSecOxm_t*)OICMalloc(sizeof(OicSecOxm_t) * src->oxmLen);
871 for(size_t i = 0; i < src->oxmLen; i++)
873 tempOxm[i] = src->oxm[i];
878 dst->oxmLen = src->oxmLen;
882 #ifdef _ENABLE_MULTIPLE_OWNER_
885 OIC_LOG(DEBUG, TAG, "dectected 'mom' property");
888 dst->mom = (OicSecMom_t*)OICCalloc(1, sizeof(OicSecMom_t));
891 dst->mom->mode = src->mom->mode;
895 #endif //_ENABLE_MULTIPLE_OWNER_
899 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
900 #ifdef _ENABLE_MULTIPLE_OWNER_
902 * Callback function to handle MOT DTLS handshake result.
903 * @param[out] object remote device information.
904 * @param[out] errorInfo CA Error information.
906 void MultipleOwnerDTLSHandshakeCB(const CAEndpoint_t *object,
907 const CAErrorInfo_t *errorInfo)
909 OIC_LOG(DEBUG, TAG, "IN MultipleOwnerDTLSHandshakeCB");
911 if(CA_STATUS_OK == errorInfo->result)
913 const CASecureEndpoint_t* authenticatedSubOwnerInfo = CAGetSecureEndpointData(object);
914 if(authenticatedSubOwnerInfo)
916 OicSecSubOwner_t* subOwnerInst = NULL;
917 LL_FOREACH(gDoxm->subOwners, subOwnerInst)
919 if(0 == memcmp(subOwnerInst->uuid.id,
920 authenticatedSubOwnerInfo->identity.id,
921 authenticatedSubOwnerInfo->identity.id_length))
927 if(NULL == subOwnerInst)
929 subOwnerInst = (OicSecSubOwner_t*)OICCalloc(1, sizeof(OicSecSubOwner_t));
932 OIC_LOG(DEBUG, TAG, "Adding New SubOwner");
933 memcpy(subOwnerInst->uuid.id, authenticatedSubOwnerInfo->identity.id,
934 authenticatedSubOwnerInfo->identity.id_length);
935 LL_APPEND(gDoxm->subOwners, subOwnerInst);
936 if(!UpdatePersistentStorage(gDoxm))
938 OIC_LOG(ERROR, TAG, "Failed to register SubOwner UUID into Doxm");
945 if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskCredentials))
947 OIC_LOG(WARNING, TAG, "Failed to revert the DTLS credential handler");
950 OIC_LOG(DEBUG, TAG, "OUT MultipleOwnerDTLSHandshakeCB");
952 #endif //_ENABLE_MULTIPLE_OWNER_
953 #endif // defined(__WITH_DTLS__) || defined (__WITH_TLS__)
955 static OCEntityHandlerResult HandleDoxmPostRequest(const OCEntityHandlerRequest * ehRequest)
957 OIC_LOG (DEBUG, TAG, "Doxm EntityHandle processing POST request");
958 OCEntityHandlerResult ehRet = OC_EH_ERROR;
959 OicUuid_t emptyOwner = {.id = {0} };
960 static uint16_t previousMsgId = 0;
963 * Convert CBOR Doxm data into binary. This will also validate
964 * the Doxm data received.
966 OicSecDoxm_t *newDoxm = NULL;
968 if (ehRequest->payload)
970 uint8_t *payload = ((OCSecurityPayload *)ehRequest->payload)->securityData;
971 size_t size = ((OCSecurityPayload *)ehRequest->payload)->payloadSize;
972 bool roParsed = false;
973 OCStackResult res = CBORPayloadToDoxmBin(payload, size, &newDoxm, &roParsed);
974 if (newDoxm && OC_STACK_OK == res)
976 // Check request on RO property
977 if (true == roParsed)
979 OIC_LOG(ERROR, TAG, "Not acceptable request because of read-only propertys");
980 ehRet = OC_EH_NOT_ACCEPTABLE;
985 if (true == gDoxm->owned)
987 //Update gDoxm based on newDoxm
988 updateWriteableProperty(newDoxm, gDoxm);
990 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
991 #ifdef _ENABLE_MULTIPLE_OWNER_
995 if(OIC_MULTIPLE_OWNER_DISABLE != gDoxm->mom->mode)
997 CAResult_t caRes = CA_STATUS_FAILED;
998 if(OIC_PRECONFIG_PIN == gDoxm->oxmSel || OIC_RANDOM_DEVICE_PIN == gDoxm->oxmSel)
1000 caRes = CAEnableAnonECDHCipherSuite(false);
1001 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1002 OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");
1004 caRes = CASelectCipherSuite((uint16_t)TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256, ehRequest->devAddr.adapter);
1005 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1006 OIC_LOG(INFO, TAG, "ECDHE_PSK CipherSuite will be used for MOT");
1008 //Set the device id to derive temporal PSK
1009 SetUuidForPinBasedOxm(&gDoxm->deviceID);
1013 OIC_LOG(WARNING, TAG, "Unsupported OxM for Multiple Ownership Transfer.");
1016 CAregisterSslHandshakeCallback(MultipleOwnerDTLSHandshakeCB);
1020 //if MOM is disabled, revert the DTLS handshake callback
1021 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(NULL))
1023 OIC_LOG(WARNING, TAG, "Error while revert the DTLS Handshake Callback.");
1028 if(newDoxm->subOwners)
1030 OicSecSubOwner_t* subowner = NULL;
1031 OicSecSubOwner_t* temp = NULL;
1033 OIC_LOG(DEBUG, TAG, "dectected 'subowners' property");
1035 if(gDoxm->subOwners)
1037 LL_FOREACH_SAFE(gDoxm->subOwners, subowner, temp)
1039 LL_DELETE(gDoxm->subOwners, subowner);
1046 LL_FOREACH_SAFE(newDoxm->subOwners, subowner, temp)
1048 LL_DELETE(newDoxm->subOwners, subowner);
1049 LL_APPEND(gDoxm->subOwners, subowner);
1052 #endif //_ENABLE_MULTIPLE_OWNER_
1053 #endif // defined(__WITH_DTLS__) || defined (__WITH_TLS__)
1055 //Update new state in persistent storage
1056 if (UpdatePersistentStorage(gDoxm) == true)
1062 OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
1063 ehRet = OC_EH_ERROR;
1069 if ((false == gDoxm->owned) && (false == newDoxm->owned))
1071 if (OIC_JUST_WORKS == newDoxm->oxmSel)
1074 * If current state of the device is un-owned, enable
1075 * anonymous ECDH cipher in tinyDTLS so that Provisioning
1076 * tool can initiate JUST_WORKS ownership transfer process.
1078 if (memcmp(&(newDoxm->owner), &emptyOwner, sizeof(OicUuid_t)) == 0)
1080 OIC_LOG (INFO, TAG, "Doxm EntityHandle enabling AnonECDHCipherSuite");
1081 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1082 ehRet = (CAEnableAnonECDHCipherSuite(true) == CA_STATUS_OK) ? OC_EH_OK : OC_EH_ERROR;
1083 #endif // __WITH_DTLS__ or __WITH_TLS__
1088 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1089 //Save the owner's UUID to derive owner credential
1090 memcpy(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t));
1092 // Update new state in persistent storage
1093 if (true == UpdatePersistentStorage(gDoxm))
1099 OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
1100 ehRet = OC_EH_ERROR;
1104 * Disable anonymous ECDH cipher in tinyDTLS since device is now
1107 CAResult_t caRes = CA_STATUS_OK;
1108 caRes = CAEnableAnonECDHCipherSuite(false);
1109 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1110 OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");
1112 #endif // __WITH_DTLS__ or __WITH_TLS__
1115 else if (OIC_RANDOM_DEVICE_PIN == newDoxm->oxmSel)
1118 * If current state of the device is un-owned, enable
1119 * anonymous ECDH cipher in tinyDTLS so that Provisioning
1120 * tool can initiate JUST_WORKS ownership transfer process.
1122 if(memcmp(&(newDoxm->owner), &emptyOwner, sizeof(OicUuid_t)) == 0)
1124 gDoxm->oxmSel = newDoxm->oxmSel;
1125 //Update new state in persistent storage
1126 if ((UpdatePersistentStorage(gDoxm) == true))
1132 OIC_LOG(WARNING, TAG, "Failed to update DOXM in persistent storage");
1133 ehRet = OC_EH_ERROR;
1136 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1137 CAResult_t caRes = CA_STATUS_OK;
1139 caRes = CAEnableAnonECDHCipherSuite(false);
1140 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1141 OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");
1143 caRes = CASelectCipherSuite(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256,
1144 ehRequest->devAddr.adapter);
1145 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1147 char ranPin[OXM_RANDOM_PIN_MAX_SIZE + 1] = {0};
1148 //TODO ehRequest->messageID for copa over TCP always is null. Find reason why.
1149 if(ehRequest->devAddr.adapter == OC_ADAPTER_IP && previousMsgId != ehRequest->messageID)
1151 if(OC_STACK_OK == GeneratePin(ranPin, sizeof(ranPin)))
1153 //Set the device id to derive temporal PSK
1154 SetUuidForPinBasedOxm(&gDoxm->deviceID);
1157 * Since PSK will be used directly by DTLS layer while PIN based ownership transfer,
1158 * Credential should not be saved into SVR.
1159 * For this reason, use a temporary get_psk_info callback to random PIN OxM.
1161 caRes = CAregisterPskCredentialsHandler(GetDtlsPskForRandomPinOxm);
1162 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1167 OIC_LOG(ERROR, TAG, "Failed to generate random PIN");
1168 ehRet = OC_EH_ERROR;
1171 else if(previousMsgId != ehRequest->messageID)
1173 if(OC_STACK_OK == GeneratePin(ranPin, sizeof(ranPin)))
1175 //Set the device id to derive temporal PSK
1176 SetUuidForPinBasedOxm(&gDoxm->deviceID);
1179 * Since PSK will be used directly by DTLS layer while PIN based ownership transfer,
1180 * Credential should not be saved into SVR.
1181 * For this reason, use a temporary get_psk_info callback to random PIN OxM.
1183 caRes = CAregisterPskCredentialsHandler(GetDtlsPskForRandomPinOxm);
1184 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1189 OIC_LOG(ERROR, TAG, "Failed to generate random PIN");
1190 ehRet = OC_EH_ERROR;
1194 #endif // __WITH_DTLS__ or __WITH_TLS__
1196 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1199 //Save the owner's UUID to derive owner credential
1200 memcpy(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t));
1202 //Update new state in persistent storage
1203 if (UpdatePersistentStorage(gDoxm) == true)
1209 OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
1210 ehRet = OC_EH_ERROR;
1213 #endif // __WITH_DTLS__ or __WITH_TLS__
1215 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
1216 else if (OIC_MANUFACTURER_CERTIFICATE == newDoxm->oxmSel)
1218 //Save the owner's UUID to derive owner credential
1219 memcpy(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t));
1220 gDoxm->oxmSel = newDoxm->oxmSel;
1221 //Update new state in persistent storage
1222 if (UpdatePersistentStorage(gDoxm))
1228 OIC_LOG(WARNING, TAG, "Failed to update DOXM in persistent storage");
1229 ehRet = OC_EH_ERROR;
1231 CAResult_t caRes = CAEnableAnonECDHCipherSuite(false);
1232 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1233 OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");
1235 VERIFY_SUCCESS(TAG, CA_STATUS_OK == CAregisterPkixInfoHandler(GetManufacturerPkixInfo), ERROR);
1236 VERIFY_SUCCESS(TAG, CA_STATUS_OK == CAregisterGetCredentialTypesHandler(InitManufacturerCipherSuiteList), ERROR);
1238 #endif // __WITH_DTLS__ or __WITH_TLS__
1242 * When current state of the device is un-owned and Provisioning
1243 * Tool is attempting to change the state to 'Owned' with a
1244 * qualified value for the field 'Owner'
1246 if ((false == gDoxm->owned) && (true == newDoxm->owned) &&
1247 (memcmp(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t)) == 0))
1249 //Change the SVR's resource owner as owner device.
1250 OCStackResult ownerRes = SetAclRownerId(&gDoxm->owner);
1251 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
1253 ehRet = OC_EH_ERROR;
1256 ownerRes = SetAmaclRownerId(&gDoxm->owner);
1257 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
1259 ehRet = OC_EH_ERROR;
1262 ownerRes = SetCredRownerId(&gDoxm->owner);
1263 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
1265 ehRet = OC_EH_ERROR;
1268 ownerRes = SetPstatRownerId(&gDoxm->owner);
1269 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
1271 ehRet = OC_EH_ERROR;
1274 ownerRes = SetDpairingRownerId(&gDoxm->owner);
1275 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
1277 ehRet = OC_EH_ERROR;
1280 ownerRes = SetPconfRownerId(&gDoxm->owner);
1281 if(OC_STACK_OK != ownerRes && OC_STACK_NO_RESOURCE != ownerRes)
1283 ehRet = OC_EH_ERROR;
1287 gDoxm->owned = true;
1288 memcpy(&gDoxm->rownerID, &gDoxm->owner, sizeof(OicUuid_t));
1290 // Update new state in persistent storage
1291 if (UpdatePersistentStorage(gDoxm))
1293 //Update default ACE of security resource to prevent anonymous user access.
1294 if(OC_STACK_OK == UpdateDefaultSecProvACE())
1300 OIC_LOG(ERROR, TAG, "Failed to remove default ACL for security provisioning");
1301 ehRet = OC_EH_ERROR;
1306 OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
1307 ehRet = OC_EH_ERROR;
1309 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
1310 if (OIC_MANUFACTURER_CERTIFICATE == gDoxm->oxmSel)
1312 CAregisterPkixInfoHandler(GetPkixInfo);
1313 CAregisterGetCredentialTypesHandler(InitCipherSuiteList);
1315 #endif // __WITH_DTLS__ or __WITH_TLS__
1321 if(OC_EH_OK != ehRet)
1325 * If some error is occured while ownership transfer,
1326 * ownership transfer related resource should be revert back to initial status.
1330 if(!gDoxm->owned && previousMsgId != ehRequest->messageID)
1332 OIC_LOG(WARNING, TAG, "The operation failed during handle DOXM request,"\
1333 "DOXM will be reverted.");
1334 RestoreDoxmToInitState();
1335 RestorePstatToInitState();
1340 OIC_LOG(ERROR, TAG, "Invalid DOXM resource.");
1345 previousMsgId = ehRequest->messageID;
1348 //Send payload to request originator
1349 ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
1350 OC_EH_OK : OC_EH_ERROR;
1352 DeleteDoxmBinData(newDoxm);
1357 OCEntityHandlerResult DoxmEntityHandler(OCEntityHandlerFlag flag,
1358 OCEntityHandlerRequest * ehRequest,
1359 void* callbackParam)
1361 (void)callbackParam;
1362 OCEntityHandlerResult ehRet = OC_EH_ERROR;
1364 if(NULL == ehRequest)
1369 if (flag & OC_REQUEST_FLAG)
1371 OIC_LOG(DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
1373 switch (ehRequest->method)
1376 ehRet = HandleDoxmGetRequest(ehRequest);
1380 ehRet = HandleDoxmPostRequest(ehRequest);
1384 ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
1385 OC_EH_OK : OC_EH_ERROR;
1393 OCStackResult CreateDoxmResource()
1395 OCStackResult ret = OCCreateResource(&gDoxmHandle,
1396 OIC_RSRC_TYPE_SEC_DOXM,
1397 OC_RSRVD_INTERFACE_DEFAULT,
1404 if (OC_STACK_OK != ret)
1406 OIC_LOG (FATAL, TAG, "Unable to instantiate Doxm resource");
1407 DeInitDoxmResource();
1413 * Checks if DeviceID is generated during provisioning for the new device.
1414 * If DeviceID is NULL then generates the new DeviceID.
1415 * Once DeviceID is assigned to the device it does not change for the lifetime of the device.
1417 static OCStackResult CheckDeviceID()
1419 OCStackResult ret = OC_STACK_ERROR;
1420 bool validId = false;
1421 for (uint8_t i = 0; i < UUID_LENGTH; i++)
1423 if (gDoxm->deviceID.id[i] != 0)
1432 if (OCGenerateUuid(gDoxm->deviceID.id) != RAND_UUID_OK)
1434 OIC_LOG(FATAL, TAG, "Generate UUID for Server Instance failed!");
1439 if (!UpdatePersistentStorage(gDoxm))
1441 //TODO: After registering PSI handler in all samples, do ret = OC_STACK_OK here.
1442 OIC_LOG(FATAL, TAG, "UpdatePersistentStorage failed!");
1453 * Get the default value.
1455 * @return the default value of doxm, @ref OicSecDoxm_t.
1457 static OicSecDoxm_t* GetDoxmDefault()
1459 OIC_LOG(DEBUG, TAG, "GetDoxmToDefault");
1460 return &gDefaultDoxm;
1463 const OicSecDoxm_t* GetDoxmResourceData()
1468 #if defined(__WITH_DTLS__) && defined(_ENABLE_MULTIPLE_OWNER_)
1470 * Internal API to prepare MOT
1472 static void PrepareMOT(const OicSecDoxm_t* doxm)
1474 OIC_LOG(INFO, TAG, "IN PrepareMOT");
1475 VERIFY_NON_NULL(TAG, doxm, ERROR);
1477 if(true == doxm->owned && NULL != doxm->mom && OIC_MULTIPLE_OWNER_DISABLE != doxm->mom->mode)
1479 CAResult_t caRes = CA_STATUS_FAILED;
1481 OIC_LOG(INFO, TAG, "Multiple Ownership Transfer Enabled!");
1483 if(OIC_PRECONFIG_PIN == doxm->oxmSel)
1485 caRes = CAEnableAnonECDHCipherSuite(false);
1486 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1487 OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");
1489 caRes = CASelectCipherSuite((uint16_t)TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256, CA_ADAPTER_IP);
1490 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1492 caRes = CASelectCipherSuite((uint16_t)TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256, CA_ADAPTER_TCP);
1493 VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
1495 OIC_LOG(INFO, TAG, "ECDHE_PSK CipherSuite will be used for MOT");
1497 //Set the device id to derive temporal PSK
1498 SetUuidForPinBasedOxm(&doxm->deviceID);
1502 OIC_LOG(ERROR, TAG, "Unsupported OxM for Multiple Ownership Transfer.");
1506 CAregisterSslHandshakeCallback(MultipleOwnerDTLSHandshakeCB);
1509 OIC_LOG(INFO, TAG, "OUT PrepareMOT");
1512 OIC_LOG(WARNING, TAG, "Error in PrepareMOT");
1514 #endif //defined(__WITH_DTLS__) && defined(_ENABLE_MULTIPLE_OWNER_)
1516 OCStackResult InitDoxmResource()
1518 OCStackResult ret = OC_STACK_ERROR;
1520 //Read DOXM resource from PS
1521 uint8_t *data = NULL;
1523 ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_DOXM_NAME, &data, &size);
1524 // If database read failed
1525 if (OC_STACK_OK != ret)
1527 OIC_LOG (DEBUG, TAG, "ReadSVDataFromPS failed");
1531 // Read DOXM resource from PS
1532 ret = CBORPayloadToDoxm(data, size, &gDoxm);
1535 * If SVR database in persistent storage got corrupted or
1536 * is not available for some reason, a default doxm is created
1537 * which allows user to initiate doxm provisioning again.
1539 if ((OC_STACK_OK != ret) || !data || !gDoxm)
1541 gDoxm = GetDoxmDefault();
1544 //In case of the server is shut down unintentionally, we should initialize the owner
1545 if(false == gDoxm->owned)
1547 OicUuid_t emptyUuid = {.id={0}};
1548 memcpy(&gDoxm->owner, &emptyUuid, sizeof(OicUuid_t));
1551 ret = CheckDeviceID();
1552 if (ret == OC_STACK_OK)
1554 OIC_LOG_V(DEBUG, TAG, "Initial Doxm Owned = %d", gDoxm->owned);
1555 //Instantiate 'oic.sec.doxm'
1556 ret = CreateDoxmResource();
1560 OIC_LOG (ERROR, TAG, "CheckDeviceID failed");
1564 #if defined(__WITH_DTLS__) && defined(_ENABLE_MULTIPLE_OWNER_)
1565 //if MOT is enabled, MOT should be prepared.
1566 if(gDoxm && gDoxm->owned)
1570 #endif // defined(__WITH_DTLS__) && defined(_ENABLE_MULTIPLE_OWNER_)
1575 OCStackResult DeInitDoxmResource()
1577 OCStackResult ret = OCDeleteResource(gDoxmHandle);
1578 if (gDoxm != &gDefaultDoxm)
1580 DeleteDoxmBinData(gDoxm);
1584 if (OC_STACK_OK == ret)
1590 return OC_STACK_ERROR;
1594 OCStackResult GetDoxmDeviceID(OicUuid_t *deviceID)
1596 if (deviceID && gDoxm)
1598 *deviceID = gDoxm->deviceID;
1601 return OC_STACK_ERROR;
1604 OCStackResult GetDoxmIsOwned(bool *isOwned)
1606 if (isOwned && gDoxm)
1608 *isOwned = gDoxm->owned;
1611 return OC_STACK_ERROR;
1614 OCStackResult SetDoxmDeviceID(const OicUuid_t *deviceID)
1618 if(NULL == deviceID)
1620 return OC_STACK_INVALID_PARAM;
1624 OIC_LOG(ERROR, TAG, "Doxm resource is not initialized.");
1625 return OC_STACK_NO_RESOURCE;
1628 //Check the device's OTM state
1630 #ifdef __WITH_DTLS__
1631 //for normal device.
1632 if(true == gDoxm->owned &&
1633 memcmp(gDoxm->deviceID.id, gDoxm->owner.id, sizeof(gDoxm->owner.id)) != 0)
1635 OIC_LOG(ERROR, TAG, "This device owned by owner's device.");
1636 OIC_LOG(ERROR, TAG, "Device UUID cannot be changed to guarantee the reliability of the connection.");
1637 return OC_STACK_ERROR;
1639 #endif //__WITH_DTLS
1641 //Save the previous UUID
1643 memcpy(tempUuid.id, gDoxm->deviceID.id, sizeof(tempUuid.id));
1646 memcpy(gDoxm->deviceID.id, deviceID->id, sizeof(deviceID->id));
1649 memcpy(gDoxm->owner.id, deviceID->id, sizeof(deviceID->id));
1650 memcpy(gDoxm->rownerID.id, deviceID->id, sizeof(deviceID->id));
1654 if(!UpdatePersistentStorage(gDoxm))
1656 //revert UUID in case of update error
1657 memcpy(gDoxm->deviceID.id, tempUuid.id, sizeof(tempUuid.id));
1660 memcpy(gDoxm->owner.id, tempUuid.id, sizeof(tempUuid.id));
1661 memcpy(gDoxm->rownerID.id, tempUuid.id, sizeof(tempUuid.id));
1664 OIC_LOG(ERROR, TAG, "Failed to update persistent storage");
1665 return OC_STACK_ERROR;
1670 OCStackResult GetDoxmDevOwnerId(OicUuid_t *devownerid)
1672 OCStackResult retVal = OC_STACK_ERROR;
1675 OIC_LOG_V(DEBUG, TAG, "GetDoxmDevOwnerId(): gDoxm owned = %d.", \
1679 *devownerid = gDoxm->owner;
1680 retVal = OC_STACK_OK;
1686 OCStackResult GetDoxmRownerId(OicUuid_t *rowneruuid)
1688 OCStackResult retVal = OC_STACK_ERROR;
1693 *rowneruuid = gDoxm->rownerID;
1694 retVal = OC_STACK_OK;
1700 #ifdef _ENABLE_MULTIPLE_OWNER_
1702 * Compare the UUID to SubOwner.
1704 * @param[in] uuid device UUID
1706 * @return true if context->subjectId exist subowner list, else false.
1708 bool IsSubOwner(const OicUuid_t* uuid)
1710 bool retVal = false;
1717 if (gDoxm && gDoxm->subOwners)
1719 OicSecSubOwner_t* subOwner = NULL;
1720 LL_FOREACH(gDoxm->subOwners, subOwner)
1722 if(memcmp(subOwner->uuid.id, uuid->id, sizeof(uuid->id)) == 0)
1730 #endif //_ENABLE_MULTIPLE_OWNER_
1733 * Function to restore doxm resurce to initial status.
1734 * This function will use in case of error while ownership transfer
1736 void RestoreDoxmToInitState()
1740 OIC_LOG(INFO, TAG, "DOXM resource will revert back to initial status.");
1742 OicUuid_t emptyUuid = {.id={0}};
1743 memcpy(&(gDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
1744 gDoxm->owned = false;
1745 gDoxm->oxmSel = OIC_JUST_WORKS;
1747 if(!UpdatePersistentStorage(gDoxm))
1749 OIC_LOG(ERROR, TAG, "Failed to revert DOXM in persistent storage");