1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 * *****************************************************************/
21 // Defining _POSIX_C_SOURCE macro with 199309L (or greater) as value
22 // causes header files to expose definitions
23 // corresponding to the POSIX.1b, Real-time extensions
24 // (IEEE Std 1003.1b-1993) specification
26 // For this specific file, see use of clock_gettime,
27 // Refer to http://pubs.opengroup.org/stage7tc1/functions/clock_gettime.html
28 // and to http://man7.org/linux/man-pages/man2/clock_gettime.2.html
29 #ifndef _POSIX_C_SOURCE
30 #define _POSIX_C_SOURCE 200809L
33 #include "iotivity_config.h"
40 #ifdef HAVE_SYS_TIME_H
49 #include "oic_malloc.h"
50 #include "oic_string.h"
52 #include "cainterface.h"
54 #if defined (__TIZENRT__)
55 #include <apps/netutils/cJSON.h>
60 #include "srmresourcestrings.h"
61 #include "doxmresource.h"
62 #include "pstatresource.h"
63 #include "credresource.h"
64 #include "aclresource.h"
65 #include "ownershiptransfermanager.h"
66 #include "securevirtualresourcetypes.h"
67 #include "oxmjustworks.h"
68 #include "oxmrandompin.h"
69 #include "oxmmanufacturercert.h"
70 #include "secureresourceprovider.h"
73 #include "oxmpreconfpin.h"
74 #endif //MULTIPLE_OWNER
75 #include "otmcontextlist.h"
77 #include "pmutility.h"
78 #include "srmutility.h"
79 #include "provisioningdatabasemanager.h"
80 #include "ocpayload.h"
81 #include "payload_logging.h"
82 #include "pkix_interface.h"
83 #include "oxmverifycommon.h"
84 #include "psinterface.h"
90 #define NOT_ALLOWED_OXM 0
93 * List of allowed oxm list.
94 * All oxm methods are allowed as default.
97 static uint8_t g_OxmAllowStatus[OXM_IDX_COUNT] = {ALLOWED_OXM, ALLOWED_OXM, ALLOWED_OXM,
98 ALLOWED_OXM, ALLOWED_OXM, ALLOWED_OXM,
101 static uint8_t g_OxmAllowStatus[OXM_IDX_COUNT] = {ALLOWED_OXM, ALLOWED_OXM, ALLOWED_OXM,
102 ALLOWED_OXM, ALLOWED_OXM, NOT_ALLOWED_OXM};
105 OCStackResult OTMSetOTCallback(OicSecOxm_t oxm, OTMCallbackData_t* callbacks)
107 OCStackResult res = OC_STACK_INVALID_PARAM;
109 OIC_LOG(INFO, TAG, "IN OTMSetOTCallback");
111 VERIFY_NON_NULL(TAG, callbacks, ERROR);
113 #ifdef MULTIPLE_OWNER
114 VERIFY_SUCCESS(TAG, (OIC_OXM_COUNT > oxm || OIC_PRECONFIG_PIN == oxm || OIC_MV_JUST_WORKS == oxm
115 || OIC_CON_MFG_CERT == oxm), ERROR);
117 VERIFY_SUCCESS(TAG, (OIC_OXM_COUNT > oxm || OIC_MV_JUST_WORKS == oxm || OIC_CON_MFG_CERT == oxm), ERROR);
118 #endif // MULTIPLE_OWNER
123 callbacks->loadSecretCB = LoadSecretJustWorksCallback;
124 callbacks->createSecureSessionCB = CreateSecureSessionJustWorksCallback;
125 callbacks->createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
126 callbacks->createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
128 case OIC_RANDOM_DEVICE_PIN:
129 callbacks->loadSecretCB = InputPinCodeCallback;
130 callbacks->createSecureSessionCB = CreateSecureSessionRandomPinCallback;
131 callbacks->createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
132 callbacks->createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
134 case OIC_MANUFACTURER_CERTIFICATE:
135 callbacks->loadSecretCB = PrepareMCertificateCallback;
136 callbacks->createSecureSessionCB = CreateSecureSessionMCertificateCallback;
137 callbacks->createSelectOxmPayloadCB = CreateMCertificateBasedSelectOxmPayload;
138 callbacks->createOwnerTransferPayloadCB = CreateMCertificateBasedOwnerTransferPayload;
140 case OIC_DECENTRALIZED_PUBLIC_KEY:
141 OIC_LOG(ERROR, TAG, "OIC_DECENTRALIZED_PUBLIC_KEY not supported yet.");
142 return OC_STACK_INVALID_METHOD;
143 #ifdef MULTIPLE_OWNER
144 case OIC_PRECONFIG_PIN:
145 callbacks->loadSecretCB = LoadPreconfigPinCodeCallback;
146 callbacks->createSecureSessionCB = CreateSecureSessionPreconfigPinCallback;
147 callbacks->createSelectOxmPayloadCB = CreatePreconfigPinBasedSelectOxmPayload;
148 callbacks->createOwnerTransferPayloadCB = CreatePreconfigPinBasedOwnerTransferPayload;
150 #endif //MULTIPLE_OWNER
151 case OIC_MV_JUST_WORKS:
152 callbacks->loadSecretCB = LoadSecretJustWorksCallback;
153 callbacks->createSecureSessionCB = CreateSecureSessionJustWorksCallback;
154 callbacks->createSelectOxmPayloadCB = CreateMVJustWorksSelectOxmPayload;
155 callbacks->createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
157 case OIC_CON_MFG_CERT:
158 callbacks->loadSecretCB = PrepareMCertificateCallback;
159 callbacks->createSecureSessionCB = CreateSecureSessionMCertificateCallback;
160 callbacks->createSelectOxmPayloadCB = CreateConMCertificateBasedSelectOxmPayload;
161 callbacks->createOwnerTransferPayloadCB = CreateMCertificateBasedOwnerTransferPayload;
164 OIC_LOG_V(ERROR, TAG, "Unknown OxM : %d", (int)oxm);
165 return OC_STACK_INVALID_PARAM;
171 OIC_LOG(INFO, TAG, "OUT OTMSetOTCallback");
176 * Internal API to convert OxM value to index of oxm allow table.
178 static OxmAllowTableIdx_t GetOxmAllowTableIdx(OicSecOxm_t oxm)
183 return OXM_IDX_JUST_WORKS;
184 case OIC_RANDOM_DEVICE_PIN:
185 return OXM_IDX_RANDOM_DEVICE_PIN;
186 case OIC_MANUFACTURER_CERTIFICATE:
187 return OXM_IDX_MANUFACTURER_CERTIFICATE;
188 case OIC_DECENTRALIZED_PUBLIC_KEY:
189 return OXM_IDX_DECENTRALIZED_PUBLIC_KEY;
190 case OIC_MV_JUST_WORKS:
191 return OXM_IDX_MV_JUST_WORKS;
192 case OIC_CON_MFG_CERT:
193 return OXM_IDX_CON_MFG_CERT;
194 #ifdef MULTIPLE_OWNER
195 case OIC_PRECONFIG_PIN:
196 return OXM_IDX_PRECONFIG_PIN;
199 return OXM_IDX_UNKNOWN;
204 * Function to select appropriate provisioning method.
206 * @param[in] supportedMethods Array of supported methods
207 * @param[in] numberOfMethods number of supported methods
208 * @param[out] selectedMethod Selected methods
209 * @param[in] ownerType type of owner device (SUPER_OWNER or SUB_OWNER)
210 * @return OC_STACK_OK on success
212 OCStackResult OTMSelectOwnershipTransferMethod(const OicSecOxm_t *supportedMethods,
213 size_t numberOfMethods, OicSecOxm_t *selectedMethod, OwnerType_t ownerType)
215 bool isOxmSelected = false;
216 OxmAllowTableIdx_t selectedOxmIdx = OXM_IDX_UNKNOWN;
218 OIC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
220 if (numberOfMethods == 0 || !supportedMethods)
222 OIC_LOG(WARNING, TAG, "Could not find a supported OxM.");
223 return OC_STACK_ERROR;
230 for (size_t i = 0; i < numberOfMethods; i++)
232 selectedOxmIdx = GetOxmAllowTableIdx(supportedMethods[i]);
233 if (OXM_IDX_COUNT <= selectedOxmIdx)
235 OIC_LOG(WARNING, TAG, "Invalid oxm index to access OxM allow table");
239 #ifdef MULTIPLE_OWNER
240 if (ALLOWED_OXM == g_OxmAllowStatus[selectedOxmIdx] &&
241 OXM_IDX_PRECONFIG_PIN != selectedOxmIdx)
243 if (ALLOWED_OXM == g_OxmAllowStatus[selectedOxmIdx])
244 #endif //MULTIPLE_OWNER
246 *selectedMethod = supportedMethods[i];
247 isOxmSelected = true;
252 #ifdef MULTIPLE_OWNER
255 for (size_t i = 0; i < numberOfMethods; i++)
257 selectedOxmIdx = GetOxmAllowTableIdx(supportedMethods[i]);
258 if (OXM_IDX_COUNT <= selectedOxmIdx)
260 OIC_LOG(WARNING, TAG, "Invalid oxm index to access OxM allow table");
264 //in case of MOT, only Random PIN & Preconfigured PIN based OxM is allowed
265 if (ALLOWED_OXM == g_OxmAllowStatus[selectedOxmIdx] &&
266 (OXM_IDX_RANDOM_DEVICE_PIN == selectedOxmIdx ||
267 OXM_IDX_PRECONFIG_PIN == selectedOxmIdx))
269 *selectedMethod = supportedMethods[i];
270 isOxmSelected = true;
278 OIC_LOG_V(ERROR, TAG, "Unknown owner type or Not supported owner type : %d", ownerType);
279 return OC_STACK_INVALID_PARAM;
285 OIC_LOG(ERROR, TAG, "Can not find the allowed OxM.");
286 return OC_STACK_NOT_ALLOWED_OXM;
289 OIC_LOG(DEBUG, TAG, "OUT SelectProvisioningMethod");
295 * Function to select operation mode.This function will return most secure common operation mode.
297 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
298 * @param[out] selectedMode selected operation mode
299 * @return OC_STACK_OK on success
301 static void SelectOperationMode(const OCProvisionDev_t *selectedDeviceInfo,
302 OicSecDpom_t *selectedMode)
304 OIC_LOG(DEBUG, TAG, "IN SelectOperationMode");
305 *selectedMode = selectedDeviceInfo->pstat->sm[0];
306 OIC_LOG_V(DEBUG, TAG, "Selected Operation Mode = %d", *selectedMode);
310 * Function to start ownership transfer.
311 * This function will send the first request for provisioning,
312 * The next request message is sent from the response handler for this request.
314 * @param[in] ctx context value passed to callback from calling function.
315 * @param[in] selectedDevice selected device information to performing provisioning.
316 * @return OC_STACK_OK on success
318 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
321 * Internal function to setup & cleanup PDM to performing provisioning.
323 * @param[in] selectedDevice selected device information to performing provisioning.
324 * @return OC_STACK_OK on success
326 static OCStackResult SetupPDM(const OCProvisionDev_t* selectedDevice);
329 * Function to update owner transfer mode
331 * @param[in] otmCtx Context value of ownership transfer.
332 * @return OC_STACK_OK on success
334 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx);
337 * Function to send request to resource to get its pstat resource information.
339 * @param[in] otmCtx Context value of ownership transfer.
340 * @return OC_STACK_OK on success
342 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx);
346 * Function to send uuid of owner device to new device.
347 * This function would update 'owner of doxm' as UUID for provisioning tool.
349 * @param[in] otmCtx Context value of ownership transfer.
350 * @return OC_STACK_OK on success
352 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx);
355 * Function to update the operation mode. As per the spec. Operation mode in client driven
356 * single service provisioning it will be updated to 0x3
358 * @param[in] otmCtx Context value of ownership transfer.
359 * @return OC_STACK_OK on success
361 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx);
364 * Function to update the owner credential to new device
366 * @param[in] otmCtx Context value of ownership transfer.
367 * @param[in] selectedOperationMode selected operation mode
368 * @return OC_STACK_OK on success
370 static OCStackResult PostOwnerCredential(OTMContext_t* otmCtx);
373 * Function to update the owner ACL to new device.
375 * @param[in] otmCtx Context value of ownership transfer.
376 * @return OC_STACK_OK on success
378 static OCStackResult PostOwnerAcl(OTMContext_t* otmCtx);
381 * Function to send ownerShip info.
382 * This function would update 'owned of doxm' as true.
384 * @param[in] otmCtx Context value of ownership transfer.
385 * @return OC_STACK_OK on success
387 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx);
390 * Function to update pstat as Ready for provisioning.
391 * This function would update 'cm' from bx0000,0010 to bx0000,0000.
393 * @param[in] ctx context value passed to callback from calling function.
394 * @param[in] selectedDevice selected device information to performing provisioning.
395 * @return OC_STACK_OK on success
397 static OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx);
400 * Function to update pstat as Ready for Normal Operation.
401 * This function would update 'isop' from false to true.
403 * @param[in] ctx context value passed to callback from calling function.
404 * @param[in] selectedDevice selected device information to performing provisioning.
405 * @return OC_STACK_OK on success
407 static OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx);
409 static bool IsComplete(OTMContext_t* otmCtx)
411 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
413 if(OC_STACK_CONTINUE == otmCtx->ctxResultArray[i].res)
423 * Function to save the result of provisioning.
425 * @param[in,out] otmCtx Context value of ownership transfer.
426 * @param[in] res result of provisioning
428 void SetResult(OTMContext_t* otmCtx, const OCStackResult res)
430 OIC_LOG_V(DEBUG, TAG, "IN SetResult : %d ", res);
432 if(NULL == otmCtx || NULL == otmCtx->selectedDeviceInfo
433 || NULL == otmCtx->selectedDeviceInfo->doxm)
435 OIC_LOG(WARNING, TAG, "OTMContext is NULL");
439 //If OTM Context was removed from previous response handler, just exit the current OTM process.
440 if(NULL == GetOTMContext(otmCtx->selectedDeviceInfo->endpoint.addr,
441 otmCtx->selectedDeviceInfo->securePort))
443 OIC_LOG(WARNING, TAG, "Current OTM Process has already ended.");
446 //Revert psk_info callback and new deivce uuid in case of random PIN OxM
447 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
449 if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskCredentials))
451 OIC_LOG(WARNING, TAG, "Failed to revert is DTLS credential handler.");
453 OicUuid_t emptyUuid = { .id={0}};
454 SetUuidForPinBasedOxm(&emptyUuid);
456 else if(OIC_MANUFACTURER_CERTIFICATE == otmCtx->selectedDeviceInfo->doxm->oxmSel ||
457 OIC_CON_MFG_CERT == otmCtx->selectedDeviceInfo->doxm->oxmSel)
459 //Revert back certificate related callbacks.
460 if(CA_STATUS_OK != CAregisterPkixInfoHandler(GetPkixInfo))
462 OIC_LOG(WARNING, TAG, "Failed to revert PkixInfoHandler.");
464 if(CA_STATUS_OK != CAregisterGetCredentialTypesHandler(InitCipherSuiteList))
466 OIC_LOG(WARNING, TAG, "Failed to revert CredentialTypesHandler.");
470 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
472 if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
473 otmCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
475 otmCtx->ctxResultArray[i].res = res;
476 if(OC_STACK_OK != res && OC_STACK_CONTINUE != res && OC_STACK_DUPLICATE_REQUEST != res)
478 otmCtx->ctxHasError = true;
479 if (OC_STACK_OK != PDMDeleteDevice(&otmCtx->ctxResultArray[i].deviceId))
481 OIC_LOG(WARNING, TAG, "Internal error in PDMDeleteDevice");
483 CAEndpoint_t endpoint;
484 memcpy(&endpoint, &(otmCtx->selectedDeviceInfo->endpoint), sizeof(CAEndpoint_t));
485 endpoint.port = otmCtx->selectedDeviceInfo->securePort;
486 if (CA_STATUS_OK != CAcloseSslConnection(&endpoint))
488 OIC_LOG(WARNING, TAG, "Failed to close Secure session");
494 //In case of duplicated OTM process, OTMContext and OCDoHandle should not be removed.
495 if(OC_STACK_DUPLICATE_REQUEST != res)
497 //Remove the current OTM Context from OTM queue
498 RemoveOTMContext(otmCtx->selectedDeviceInfo->endpoint.addr,
499 otmCtx->selectedDeviceInfo->securePort);
501 //If there is a request being performed, cancel it to prevent retransmission.
502 if(otmCtx->ocDoHandle)
504 OIC_LOG_V(DEBUG, TAG, "OCCancel - %s : %d",
505 otmCtx->selectedDeviceInfo->endpoint.addr,
506 otmCtx->selectedDeviceInfo->securePort);
507 if(OC_STACK_OK != OCCancel(otmCtx->ocDoHandle, OC_HIGH_QOS, NULL, 0))
509 OIC_LOG(WARNING, TAG, "Failed to remove registered callback");
513 otmCtx->ocDoHandle = NULL;
518 //If all OTM process is complete, invoke the user callback.
519 if(IsComplete(otmCtx))
521 if(OC_STACK_OK != res && OC_STACK_CONTINUE != res && OC_STACK_DUPLICATE_REQUEST != res)
523 // Reset doxm and pstat properties to pre-Ownership Transfer state
524 OIC_LOG(DEBUG, TAG, "Resetting doxm and pstat properties");
525 if(otmCtx->selectedDeviceInfo->doxm)
527 OicUuid_t emptyUuid = {.id = {0}};
528 memcpy(&(otmCtx->selectedDeviceInfo->doxm->owner), &emptyUuid, sizeof(OicUuid_t));
529 otmCtx->selectedDeviceInfo->doxm->owned = false;
531 if(otmCtx->selectedDeviceInfo->pstat)
533 otmCtx->selectedDeviceInfo->pstat->isOp = false;
534 otmCtx->selectedDeviceInfo->pstat->cm |= TAKE_OWNER;
538 otmCtx->ctxResultCallback(otmCtx->userCtx, otmCtx->ctxResultArraySize,
539 otmCtx->ctxResultArray, otmCtx->ctxHasError);
540 OICFree(otmCtx->ctxResultArray);
545 if(OC_STACK_OK != StartOwnershipTransfer(otmCtx,
546 otmCtx->selectedDeviceInfo->next))
548 OIC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
552 OIC_LOG(DEBUG, TAG, "OUT SetResult");
556 * Function to handle the handshake result in OTM.
557 * This function will be invoked after DTLS handshake
558 * @param endPoint [IN] The remote endpoint.
559 * @param errorInfo [IN] Error information from the endpoint.
562 void DTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
564 OIC_LOG(DEBUG, TAG, "IN DTLSHandshakeCB");
565 if(NULL != endpoint && NULL != info)
567 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
568 endpoint->addr, endpoint->port, info->result);
570 OTMContext_t* otmCtx = GetOTMContext(endpoint->addr, endpoint->port);
573 OicSecDoxm_t* newDevDoxm = otmCtx->selectedDeviceInfo->doxm;
574 if(NULL != newDevDoxm)
576 OicUuid_t emptyUuid = {.id={0}};
578 //Make sure the address matches.
579 if(strncmp(otmCtx->selectedDeviceInfo->endpoint.addr,
581 sizeof(endpoint->addr)) == 0 &&
582 otmCtx->selectedDeviceInfo->securePort == endpoint->port)
584 OCStackResult res = OC_STACK_ERROR;
586 //If temporal secure sesstion established successfully
587 if(CA_STATUS_OK == info->result &&
588 false == newDevDoxm->owned &&
589 memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) == 0)
591 //In case of Mutual Verified Just-Works, display mutualVerifNum
592 if (OIC_MV_JUST_WORKS == newDevDoxm->oxmSel)
594 uint8_t preMutualVerifNum[OWNER_PSK_LENGTH_128] = {0};
595 uint8_t mutualVerifNum[MUTUAL_VERIF_NUM_LEN] = {0};
596 OicUuid_t deviceID = {.id = {0}};
598 //Generate mutualVerifNum
599 char label[LABEL_LEN] = {0};
600 snprintf(label, LABEL_LEN, "%s%s", MUTUAL_VERIF_NUM, OXM_MV_JUST_WORKS);
601 res = GetDoxmDeviceID(&deviceID);
602 if (OC_STACK_OK != res)
604 OIC_LOG(ERROR, TAG, "Error while retrieving Owner's device ID");
605 SetResult(otmCtx, res);
609 CAResult_t pskRet = CAGenerateOwnerPSK(endpoint,
612 deviceID.id, sizeof(deviceID.id),
613 newDevDoxm->deviceID.id, sizeof(newDevDoxm->deviceID.id),
614 preMutualVerifNum, OWNER_PSK_LENGTH_128);
615 if (CA_STATUS_OK != pskRet)
617 OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
618 SetResult(otmCtx, OC_STACK_ERROR);
622 memcpy(mutualVerifNum, preMutualVerifNum + OWNER_PSK_LENGTH_128 - sizeof(mutualVerifNum),
623 sizeof(mutualVerifNum));
624 res = VerifyOwnershipTransfer(mutualVerifNum, DISPLAY_NUM);
625 if (OC_STACK_OK != res)
627 OIC_LOG(ERROR, TAG, "Error while displaying mutualVerifNum");
628 SetResult(otmCtx, res);
632 //In case of confirmed manufacturer cert, display message
633 else if (OIC_MANUFACTURER_CERTIFICATE == newDevDoxm->oxmSel || OIC_CON_MFG_CERT == newDevDoxm->oxmSel)
635 res = VerifyOwnershipTransfer(NULL, DISPLAY_NUM);
636 if (OC_STACK_OK != res)
638 OIC_LOG(ERROR, TAG, "Error while displaying message");
639 SetResult(otmCtx, res);
644 //Send request : POST /oic/sec/doxm [{... , "devowner":"PT's UUID"}]
645 res = PostOwnerUuid(otmCtx);
646 if(OC_STACK_OK != res)
648 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
649 SetResult(otmCtx, res);
652 //In case of authentication failure
653 else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
655 //in case of error from owner credential
656 if(memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) != 0 &&
657 true == newDevDoxm->owned)
659 OIC_LOG(ERROR, TAG, "The owner credential may incorrect.");
661 if(OC_STACK_OK != RemoveCredential(&(newDevDoxm->deviceID)))
663 OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
665 SetResult(otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
667 //in case of error from wrong PIN, re-start the ownership transfer
668 else if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
670 OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
672 memcpy(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
673 newDevDoxm->owned = false;
674 otmCtx->attemptCnt++;
676 RemoveOTMContext(otmCtx->selectedDeviceInfo->endpoint.addr,
677 otmCtx->selectedDeviceInfo->securePort);
679 // In order to re-start ownership transfer, device information should be deleted from PDM.
680 res = PDMDeleteDevice(&(otmCtx->selectedDeviceInfo->doxm->deviceID));
681 if (OC_STACK_OK != res)
683 SetResult(otmCtx, res);
684 OIC_LOG(ERROR, TAG, "Failed to PDMDeleteDevice");
688 if(WRONG_PIN_MAX_ATTEMP > otmCtx->attemptCnt)
690 res = StartOwnershipTransfer(otmCtx, otmCtx->selectedDeviceInfo);
691 if(OC_STACK_OK != res)
693 SetResult(otmCtx, res);
694 OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
699 OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
700 SetResult(otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
706 OIC_LOG(ERROR, TAG, "Failed to establish secure session.");
707 SetResult(otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
715 OIC_LOG(ERROR, TAG, "Can not find the OTM Context.");
718 OIC_LOG(DEBUG, TAG, "OUT DTLSHandshakeCB");
722 * Function to save the Owner/SubOwner PSK.
724 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
725 * @return OC_STACK_OK on success
727 static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
729 OIC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
731 OCStackResult res = OC_STACK_ERROR;
733 CAEndpoint_t endpoint;
734 memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
735 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
736 endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
737 endpoint.port = selectedDeviceInfo->securePort;
738 endpoint.adapter = selectedDeviceInfo->endpoint.adapter;
740 OicUuid_t ownerDeviceID = {.id={0}};
741 if (OC_STACK_OK != GetDoxmDeviceID(&ownerDeviceID))
743 OIC_LOG(ERROR, TAG, "Error while retrieving Owner's device ID");
747 uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
748 OicSecKey_t ownerKey = {.data=ownerPSK, .len=OWNER_PSK_LENGTH_128, .encoding=OIC_ENCODING_RAW};
750 //Generating OwnerPSK
751 CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
752 (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
753 strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
754 ownerDeviceID.id, sizeof(ownerDeviceID.id),
755 selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
756 ownerPSK, OWNER_PSK_LENGTH_128);
758 if (CA_STATUS_OK == pskRet)
760 OIC_LOG(DEBUG, TAG,"Owner PSK dump:\n");
761 OIC_LOG_BUFFER(DEBUG, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
762 //Generating new credential for provisioning tool
763 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
764 SYMMETRIC_PAIR_WISE_KEY, NULL,
765 &ownerKey, &ownerDeviceID, NULL);
766 OICClearMemory(ownerPSK, sizeof(ownerPSK));
767 VERIFY_NON_NULL(TAG, cred, ERROR);
769 uint32_t outSize = 0;
770 size_t b64BufSize = B64ENCODE_OUT_SAFESIZE((OWNER_PSK_LENGTH_128 + 1));
771 char* b64Buf = (char *)OICCalloc(1, b64BufSize);
772 VERIFY_NON_NULL(TAG, b64Buf, ERROR);
773 b64Encode(cred->privateData.data, cred->privateData.len, b64Buf, b64BufSize, &outSize);
775 OICFree( cred->privateData.data );
776 cred->privateData.data = (uint8_t *)OICCalloc(1, outSize + 1);
777 VERIFY_NON_NULL(TAG, cred->privateData.data, ERROR);
779 strncpy((char*)(cred->privateData.data), b64Buf, outSize);
780 cred->privateData.data[outSize] = '\0';
781 cred->privateData.encoding = OIC_ENCODING_BASE64;
782 cred->privateData.len = outSize;
785 //Finding previous ownerPSK.
786 const OicSecCred_t* credList = GetCredList();
787 const OicSecCred_t* prevCred = NULL;
789 LL_FOREACH(credList, prevCred)
791 //OwnerPSK's type is SYMMETRIC_PAIR_WISE_KEY
792 if (SYMMETRIC_PAIR_WISE_KEY == prevCred->credType &&
793 0 == memcmp(prevCred->subject.id, cred->subject.id, sizeof(cred->subject.id)))
795 credId = prevCred->credId;
800 //If duplicate owner PSK is exists, remove it.
803 OIC_LOG(WARNING, TAG, "Duplicate OwnerPSK was detected.");
804 OIC_LOG(WARNING, TAG, "[Subject] : ");
805 OIC_LOG_BUFFER(WARNING, TAG, prevCred->subject.id, sizeof(prevCred->subject.id));
806 OIC_LOG_V(WARNING, TAG, "[Encoding Type] : %d", prevCred->privateData.encoding);
807 OIC_LOG(DEBUG, TAG, "[Private Data] : ");
808 OIC_LOG_BUFFER(DEBUG, TAG, prevCred->privateData.data, prevCred->privateData.len);
809 OIC_LOG(WARNING, TAG, "Previous OwnerPSK will be removed.");
811 res = RemoveCredentialByCredId(credId);
812 if(OC_STACK_RESOURCE_DELETED != res)
814 OIC_LOG(ERROR, TAG, "Failed to remove the previous OwnerPSK");
815 DeleteCredList(cred);
820 res = AddCredential(cred);
821 if(res != OC_STACK_OK)
823 DeleteCredList(cred);
829 OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
832 OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
838 * Callback handler for OwnerShipTransferModeHandler API.
840 * @param[in] ctx ctx value passed to callback from calling function.
841 * @param[in] UNUSED handle to an invocation
842 * @param[in] clientResponse Response from queries to remote servers.
843 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
844 * and OC_STACK_KEEP_TRANSACTION to keep it.
846 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
847 OCClientResponse *clientResponse)
849 OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
851 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
852 VERIFY_NON_NULL(TAG, ctx, WARNING);
854 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
855 otmCtx->ocDoHandle = NULL;
857 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
859 OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
860 //Send request : GET /oic/sec/pstat
861 OCStackResult res = GetProvisioningStatusResource(otmCtx);
862 if(OC_STACK_OK != res)
864 OIC_LOG(WARNING, TAG, "Failed to get pstat information");
865 SetResult(otmCtx, res);
870 OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
871 clientResponse->result);
872 SetResult(otmCtx, clientResponse->result);
875 OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
878 return OC_STACK_DELETE_TRANSACTION;
882 * Callback handler for ProvisioningStatusResouceHandler API.
884 * @param[in] ctx ctx value passed to callback from calling function.
885 * @param[in] UNUSED handle to an invocation
886 * @param[in] clientResponse Response from queries to remote servers.
887 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
888 * and OC_STACK_KEEP_TRANSACTION to keep it.
890 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
891 OCClientResponse *clientResponse)
893 OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
895 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
896 VERIFY_NON_NULL(TAG, ctx, WARNING);
898 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
899 otmCtx->ocDoHandle = NULL;
901 if (OC_STACK_OK == clientResponse->result)
903 if (NULL == clientResponse->payload)
905 OIC_LOG(INFO, TAG, "Skiping Null payload");
906 SetResult(otmCtx, OC_STACK_ERROR);
907 return OC_STACK_DELETE_TRANSACTION;
910 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
912 OIC_LOG(INFO, TAG, "Unknown payload type");
913 SetResult(otmCtx, OC_STACK_ERROR);
914 return OC_STACK_DELETE_TRANSACTION;
916 OicSecPstat_t* pstat = NULL;
917 OCStackResult result = CBORPayloadToPstat(
918 ((OCSecurityPayload*)clientResponse->payload)->securityData,
919 ((OCSecurityPayload*)clientResponse->payload)->payloadSize,
921 if(NULL == pstat || result != OC_STACK_OK)
923 OIC_LOG(ERROR, TAG, "Error while converting cbor to pstat.");
924 SetResult(otmCtx, OC_STACK_ERROR);
925 return OC_STACK_DELETE_TRANSACTION;
927 if(false == (TAKE_OWNER & pstat->cm))
929 OIC_LOG(ERROR, TAG, "Device pairing mode enabling owner transfer operations is disabled");
930 SetResult(otmCtx, OC_STACK_ERROR);
931 return OC_STACK_DELETE_TRANSACTION;
933 otmCtx->selectedDeviceInfo->pstat = pstat;
935 //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
936 SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
938 //Send request : POST /oic/sec/pstat [{"om":"bx11", .. }]
939 OCStackResult res = PostUpdateOperationMode(otmCtx);
940 if (OC_STACK_OK != res)
942 OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
943 SetResult(otmCtx, res);
948 OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
949 clientResponse->result);
950 SetResult(otmCtx, clientResponse->result);
953 OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
955 return OC_STACK_DELETE_TRANSACTION;
958 static void deleteCallback(void *ctx)
961 OIC_LOG_V(DEBUG, TAG, "%s: otm context deleted", __func__);
966 * Response handler for update owner uuid request.
968 * @param[in] ctx ctx value passed to callback from calling function.
969 * @param[in] UNUSED handle to an invocation
970 * @param[in] clientResponse Response from queries to remote servers.
971 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
972 * and OC_STACK_KEEP_TRANSACTION to keep it.
974 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
975 OCClientResponse *clientResponse)
977 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
978 VERIFY_NON_NULL(TAG, ctx, WARNING);
980 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
982 OCStackResult res = OC_STACK_OK;
983 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
984 otmCtx->ocDoHandle = NULL;
986 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
988 if(otmCtx && otmCtx->selectedDeviceInfo)
990 //In case of Mutual Verified Just-Works, wait for user confirmation
991 if (OIC_MV_JUST_WORKS == otmCtx->selectedDeviceInfo->doxm->oxmSel)
993 res = VerifyOwnershipTransfer(NULL, USER_CONFIRM);
994 if (OC_STACK_OK != res)
996 if (OC_STACK_OK != SRPResetDevice(otmCtx->selectedDeviceInfo, deleteCallback))
998 OIC_LOG(WARNING, TAG, "OwnerUuidUpdateHandler : SRPResetDevice error");
1000 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to verify user confirm");
1001 SetResult(otmCtx, res);
1002 return OC_STACK_DELETE_TRANSACTION;
1006 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
1007 if(OC_STACK_OK != res)
1009 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
1010 SetResult(otmCtx, res);
1011 return OC_STACK_DELETE_TRANSACTION;
1014 //POST owner credential to new device according to security spec B.
1015 res = PostOwnerCredential(otmCtx);
1016 if(OC_STACK_OK != res)
1019 "OwnerUuidUpdateHandler:Failed to send PosT request for onwer credential");
1020 SetResult(otmCtx, res);
1021 return OC_STACK_DELETE_TRANSACTION;
1027 if (((OIC_MANUFACTURER_CERTIFICATE == otmCtx->selectedDeviceInfo->doxm->oxmSel) ||
1028 (OIC_CON_MFG_CERT == otmCtx->selectedDeviceInfo->doxm->oxmSel)) &&
1029 OC_STACK_NOT_ACCEPTABLE == clientResponse->result)
1031 res = OC_STACK_USER_DENIED_REQ;
1032 OIC_LOG_V(ERROR, TAG,
1033 "OwnerUuidUpdateHandler : Denied Request(%d)", res);
1035 else if (OC_STACK_GATEWAY_TIMEOUT == clientResponse->result)
1037 res = clientResponse->result;
1038 OIC_LOG_V(ERROR, TAG,
1039 "OwnerUuidUpdateHandler : Timeout:No Response Received(%d)", res);
1043 res = clientResponse->result;
1044 OIC_LOG_V(ERROR, TAG, "OwnerUuidUpdateHandler : Unexpected result(%d)", res);
1046 SetResult(otmCtx, res);
1049 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
1052 return OC_STACK_DELETE_TRANSACTION;
1056 * Invokes Callback to load Random PIN
1058 void *LoadRandomPin(void *ctx)
1060 OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
1061 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1062 OCStackResult res = OC_STACK_ERROR;
1063 res = otmCtx->otmCallback.loadSecretCB(otmCtx);
1065 if(OC_STACK_OK != res)
1067 OIC_LOG_V(ERROR, TAG, "%s : Failed to load secret", __func__);
1068 SetResult(otmCtx, res);
1069 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
1073 //Save the current context instance to use on the dtls handshake callback
1074 if(OC_STACK_OK != AddOTMContext(otmCtx,
1075 otmCtx->selectedDeviceInfo->endpoint.addr,
1076 otmCtx->selectedDeviceInfo->securePort))
1078 OIC_LOG_V(ERROR, TAG, "%s : Failed to add OTM Context into OTM List.", __func__);
1079 SetResult(otmCtx, res);
1080 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
1084 //Try DTLS handshake to generate secure session
1085 if(otmCtx->otmCallback.createSecureSessionCB)
1087 res = otmCtx->otmCallback.createSecureSessionCB(otmCtx);
1088 if(OC_STACK_OK != res)
1090 OIC_LOG_V(ERROR, TAG, "%s : Failed to create DTLS session", __func__);
1091 SetResult(otmCtx, res);
1094 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
1099 * Response handler for update operation mode.
1101 * @param[in] ctx ctx value passed to callback from calling function.
1102 * @param[in] UNUSED handle to an invocation
1103 * @param[in] clientResponse Response from queries to remote servers.
1104 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1105 * and OC_STACK_KEEP_TRANSACTION to keep it.
1107 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
1108 OCClientResponse *clientResponse)
1110 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
1112 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1113 VERIFY_NON_NULL(TAG, ctx, WARNING);
1115 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1116 otmCtx->ocDoHandle = NULL;
1118 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1120 OCStackResult res = OC_STACK_ERROR;
1123 //Load secret for temporal secure session.
1124 if(otmCtx->otmCallback.loadSecretCB)
1126 if (OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
1130 thr_result = pthread_create(&p_thread, NULL, LoadRandomPin, (void *) otmCtx);
1131 if (0 != thr_result)
1133 OIC_LOG_V(ERROR, TAG, "pthread_create Error with code %d", thr_result);
1134 SetResult(otmCtx, res);
1135 return OC_STACK_DELETE_TRANSACTION;
1137 OIC_LOG(INFO, TAG, "Random Pin loadSecretCB Thread Created");
1141 res = otmCtx->otmCallback.loadSecretCB(otmCtx);
1142 if(OC_STACK_OK != res)
1144 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
1145 SetResult(otmCtx, res);
1146 return OC_STACK_DELETE_TRANSACTION;
1149 //Save the current context instance to use on the dtls handshake callback
1150 if(OC_STACK_OK != AddOTMContext(otmCtx,
1151 otmCtx->selectedDeviceInfo->endpoint.addr,
1152 otmCtx->selectedDeviceInfo->securePort))
1154 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to add OTM Context into OTM List.");
1155 SetResult(otmCtx, res);
1156 return OC_STACK_DELETE_TRANSACTION;
1159 //Try DTLS handshake to generate secure session
1160 if(otmCtx->otmCallback.createSecureSessionCB)
1162 res = otmCtx->otmCallback.createSecureSessionCB(otmCtx);
1163 if(OC_STACK_OK != res)
1165 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
1166 SetResult(otmCtx, res);
1167 return OC_STACK_DELETE_TRANSACTION;
1175 OIC_LOG(ERROR, TAG, "Error while update operation mode");
1176 SetResult(otmCtx, clientResponse->result);
1179 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
1182 return OC_STACK_DELETE_TRANSACTION;
1186 * Response handler for update owner crendetial request.
1188 * @param[in] ctx ctx value passed to callback from calling function.
1189 * @param[in] UNUSED handle to an invocation
1190 * @param[in] clientResponse Response from queries to remote servers.
1191 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1192 * and OC_STACK_KEEP_TRANSACTION to keep it.
1194 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
1195 OCClientResponse *clientResponse)
1197 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1198 VERIFY_NON_NULL(TAG, ctx, WARNING);
1200 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
1202 OCStackResult res = OC_STACK_OK;
1203 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1204 otmCtx->ocDoHandle = NULL;
1206 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1208 if(otmCtx && otmCtx->selectedDeviceInfo)
1210 //Close the temporal secure session to verify the owner credential
1211 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
1212 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
1213 CAResult_t caResult = CA_STATUS_OK;
1214 caResult = CAcloseSslConnection(endpoint);
1216 if(CA_STATUS_OK != caResult)
1218 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
1219 SetResult(otmCtx, caResult);
1220 return OC_STACK_DELETE_TRANSACTION;
1224 * If we select NULL cipher,
1225 * client will select appropriate cipher suite according to server's cipher-suite list.
1227 // TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 = 0xC037, /**< see RFC 5489 */
1228 caResult = CASelectCipherSuite(0xC037, endpoint->adapter);
1229 if(CA_STATUS_OK != caResult)
1231 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
1232 SetResult(otmCtx, caResult);
1233 return OC_STACK_DELETE_TRANSACTION;
1237 * in case of random PIN based OxM,
1238 * revert get_psk_info callback of tinyDTLS to use owner credential.
1240 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
1242 OicUuid_t emptyUuid = { .id={0}};
1243 SetUuidForPinBasedOxm(&emptyUuid);
1245 caResult = CAregisterPskCredentialsHandler(GetDtlsPskCredentials);
1247 if(CA_STATUS_OK != caResult)
1249 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
1250 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
1251 return OC_STACK_DELETE_TRANSACTION;
1255 otmCtx->selectedDeviceInfo->connType |= CT_FLAG_SECURE;
1257 res = PostOwnerAcl(otmCtx);
1258 if(OC_STACK_OK != res)
1260 OIC_LOG(ERROR, TAG, "Failed to update owner ACL to new device");
1261 SetResult(otmCtx, res);
1262 return OC_STACK_DELETE_TRANSACTION;
1268 res = clientResponse->result;
1269 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
1270 SetResult(otmCtx, res);
1273 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
1276 return OC_STACK_DELETE_TRANSACTION;
1280 * Response handler for update owner ACL request.
1282 * @param[in] ctx ctx value passed to callback from calling function.
1283 * @param[in] UNUSED handle to an invocation
1284 * @param[in] clientResponse Response from queries to remote servers.
1285 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1286 * and OC_STACK_KEEP_TRANSACTION to keep it.
1288 static OCStackApplicationResult OwnerAclHandler(void *ctx, OCDoHandle UNUSED,
1289 OCClientResponse *clientResponse)
1291 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1292 VERIFY_NON_NULL(TAG, ctx, WARNING);
1294 OIC_LOG(DEBUG, TAG, "IN OwnerAclHandler");
1296 OCStackResult res = OC_STACK_OK;
1297 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1298 otmCtx->ocDoHandle = NULL;
1300 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1302 if(otmCtx && otmCtx->selectedDeviceInfo)
1304 //POST /oic/sec/doxm [{ ..., "owned":"TRUE" }]
1305 res = PostOwnershipInformation(otmCtx);
1306 if(OC_STACK_OK != res)
1308 OIC_LOG(ERROR, TAG, "Failed to update ownership information to new device");
1309 SetResult(otmCtx, res);
1315 res = clientResponse->result;
1316 OIC_LOG_V(ERROR, TAG, "OwnerAclHandler : Unexpected result %d", res);
1317 SetResult(otmCtx, res);
1320 OIC_LOG(DEBUG, TAG, "OUT OwnerAclHandler");
1323 return OC_STACK_DELETE_TRANSACTION;
1328 * Response handler for update owner information request.
1330 * @param[in] ctx ctx value passed to callback from calling function.
1331 * @param[in] UNUSED handle to an invocation
1332 * @param[in] clientResponse Response from queries to remote servers.
1333 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1334 * and OC_STACK_KEEP_TRANSACTION to keep it.
1336 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
1337 OCClientResponse *clientResponse)
1339 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1340 VERIFY_NON_NULL(TAG, ctx, WARNING);
1342 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
1344 OCStackResult res = OC_STACK_OK;
1345 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1346 otmCtx->ocDoHandle = NULL;
1348 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1350 if(otmCtx && otmCtx->selectedDeviceInfo)
1352 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
1353 OIC_LOG(INFO, TAG, "Set Ready for provisioning state .");
1355 res = PostProvisioningStatus(otmCtx);
1356 if(OC_STACK_OK != res)
1358 OIC_LOG(ERROR, TAG, "Failed to update pstat");
1359 SetResult(otmCtx, res);
1365 res = clientResponse->result;
1366 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
1367 SetResult(otmCtx, res);
1370 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
1373 return OC_STACK_DELETE_TRANSACTION;
1377 * Response handler of update provisioning status.
1379 * @param[in] ctx ctx value passed to callback from calling function.
1380 * @param[in] UNUSED handle to an invocation
1381 * @param[in] clientResponse Response from queries to remote servers.
1382 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1383 * and OC_STACK_KEEP_TRANSACTION to keep it.
1385 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
1386 OCClientResponse *clientResponse)
1388 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
1390 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1391 VERIFY_NON_NULL(TAG, ctx, ERROR);
1393 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
1394 otmCtx->ocDoHandle = NULL;
1396 OCStackResult res = OC_STACK_OK;
1398 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1400 if(otmCtx && otmCtx->selectedDeviceInfo)
1402 OIC_LOG(INFO, TAG, "Device state is in Ready for Provisionig.");
1404 res = PostNormalOperationStatus(otmCtx);
1405 if(OC_STACK_OK != res)
1407 OIC_LOG(ERROR, TAG, "Failed to update pstat");
1408 SetResult(otmCtx, res);
1414 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1415 clientResponse->result);
1416 SetResult(otmCtx, clientResponse->result);
1420 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
1421 return OC_STACK_DELETE_TRANSACTION;
1425 * Response handler of update provisioning status to Ready for Normal..
1427 * @param[in] ctx ctx value passed to callback from calling function.
1428 * @param[in] UNUSED handle to an invocation
1429 * @param[in] clientResponse Response from queries to remote servers.
1430 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1431 * and OC_STACK_KEEP_TRANSACTION to keep it.
1433 static OCStackApplicationResult ReadyForNomalStatusHandler(void *ctx, OCDoHandle UNUSED,
1434 OCClientResponse *clientResponse)
1436 OIC_LOG_V(INFO, TAG, "IN ReadyForNomalStatusHandler.");
1438 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1439 VERIFY_NON_NULL(TAG, ctx, ERROR);
1441 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
1442 otmCtx->ocDoHandle = NULL;
1445 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1447 OIC_LOG(INFO, TAG, "Device state is in Ready for Normal Operation.");
1448 OCStackResult res = PDMSetDeviceState(&otmCtx->selectedDeviceInfo->doxm->deviceID,
1450 if (OC_STACK_OK == res)
1452 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
1453 SetResult(otmCtx, OC_STACK_OK);
1454 return OC_STACK_DELETE_TRANSACTION;
1458 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
1463 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1464 clientResponse->result);
1465 SetResult(otmCtx, clientResponse->result);
1469 OIC_LOG_V(INFO, TAG, "OUT ReadyForNomalStatusHandler.");
1470 return OC_STACK_DELETE_TRANSACTION;
1473 static OCStackResult PostOwnerCredential(OTMContext_t* otmCtx)
1475 OIC_LOG(DEBUG, TAG, "IN PostOwnerCredential");
1477 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1479 OIC_LOG(ERROR, TAG, "Invalid parameters");
1480 return OC_STACK_INVALID_PARAM;
1483 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1484 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1486 if(!PMGenerateQuery(true,
1487 deviceInfo->endpoint.addr, deviceInfo->securePort,
1488 deviceInfo->connType,
1489 query, sizeof(query), OIC_RSRC_CRED_URI))
1491 OIC_LOG(ERROR, TAG, "PostOwnerCredential : Failed to generate query");
1492 return OC_STACK_ERROR;
1494 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1495 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1498 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1499 return OC_STACK_NO_MEMORY;
1502 //Generate owner credential for new device
1503 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1504 const OicSecCred_t* ownerCredential = GetCredResourceData(&(deviceInfo->doxm->deviceID));
1505 if(!ownerCredential)
1507 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
1508 return OC_STACK_NO_RESOURCE;
1511 OicUuid_t credSubjectId = {.id={0}};
1512 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
1514 OicSecCred_t newCredential;
1515 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
1516 newCredential.next = NULL;
1518 //Set subject ID as PT's ID
1519 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
1521 //Fill private data as empty string
1522 newCredential.privateData.data = (uint8_t*)"";
1523 newCredential.privateData.len = 0;
1524 newCredential.privateData.encoding = ownerCredential->privateData.encoding;
1526 newCredential.publicData.data = NULL;
1527 newCredential.publicData.len = 0;
1528 newCredential.publicData.encoding = ownerCredential->publicData.encoding;
1531 //Send owner credential to new device : POST /oic/sec/cred [ owner credential ]
1532 if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData,
1533 &secPayload->payloadSize, secureFlag))
1535 OICFree(secPayload);
1536 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
1537 return OC_STACK_ERROR;
1539 OIC_LOG(DEBUG, TAG, "Cred Payload:");
1540 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1542 OCCallbackData cbData;
1543 cbData.cb = &OwnerCredentialHandler;
1544 cbData.context = (void *)otmCtx;
1546 OCStackResult res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query,
1547 &deviceInfo->endpoint, (OCPayload*)secPayload,
1548 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1549 if (res != OC_STACK_OK)
1551 OIC_LOG(ERROR, TAG, "OCStack resource error");
1556 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
1557 return OC_STACK_NO_RESOURCE;
1560 OIC_LOG(DEBUG, TAG, "OUT PostOwnerCredential");
1565 static OicSecAcl_t* GenerateOwnerAcl(const OicUuid_t* owner)
1567 OicSecAcl_t* ownerAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
1568 OicSecAce_t* ownerAce = (OicSecAce_t*)OICCalloc(1, sizeof(OicSecAce_t));
1569 OicSecRsrc_t* wildcardRsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
1570 if(NULL == ownerAcl || NULL == ownerAce || NULL == wildcardRsrc)
1572 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1575 LL_APPEND(ownerAcl->aces, ownerAce);
1576 LL_APPEND(ownerAce->resources, wildcardRsrc);
1578 //Set resource owner as PT
1579 memcpy(ownerAcl->rownerID.id, owner->id, sizeof(owner->id));
1581 //PT has full permission.
1582 ownerAce->permission = PERMISSION_FULL_CONTROL;
1584 //Set subject as PT's UUID
1585 memcpy(ownerAce->subjectuuid.id, owner->id, sizeof(owner->id));
1587 wildcardRsrc->href = OICStrdup(WILDCARD_RESOURCE_URI);
1588 if(NULL == wildcardRsrc->href)
1593 wildcardRsrc->interfaceLen = 1;
1594 wildcardRsrc->interfaces = (char**)OICMalloc(wildcardRsrc->interfaceLen * sizeof(char*));
1595 if(NULL == wildcardRsrc->interfaces)
1599 wildcardRsrc->interfaces[0] = OICStrdup(WILDCARD_RESOURCE_URI);
1600 if(NULL == wildcardRsrc->interfaces[0])
1605 wildcardRsrc->typeLen = 1;
1606 wildcardRsrc->types = (char**)OICMalloc(wildcardRsrc->typeLen * sizeof(char*));
1607 if(NULL == wildcardRsrc->types)
1611 wildcardRsrc->types[0] = OICStrdup(WILDCARD_RESOURCE_URI);
1612 if(NULL == wildcardRsrc->types[0])
1620 //in case of memory allocation failed, each resource should be removed individually.
1621 if(NULL == ownerAcl || NULL == ownerAce || NULL == wildcardRsrc)
1625 OICFree(wildcardRsrc);
1629 DeleteACLList(ownerAcl);
1635 * Function to update the owner ACL to new device.
1637 * @param[in] otmCtx Context value of ownership transfer.
1638 * @return OC_STACK_OK on success
1640 static OCStackResult PostOwnerAcl(OTMContext_t* otmCtx)
1642 OCStackResult res = OC_STACK_ERROR;
1644 OIC_LOG(DEBUG, TAG, "IN PostOwnerAcl");
1646 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1648 OIC_LOG(ERROR, TAG, "Invalid parameters");
1649 return OC_STACK_INVALID_PARAM;
1652 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1653 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1654 OicSecAcl_t* ownerAcl = NULL;
1656 if(!PMGenerateQuery(true,
1657 deviceInfo->endpoint.addr, deviceInfo->securePort,
1658 deviceInfo->connType,
1659 query, sizeof(query), OIC_RSRC_ACL_URI))
1661 OIC_LOG(ERROR, TAG, "Failed to generate query");
1662 return OC_STACK_ERROR;
1664 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1667 res = GetDoxmDeviceID(&ownerID);
1668 if(OC_STACK_OK != res)
1670 OIC_LOG(ERROR, TAG, "Failed to generate owner ACL");
1674 //Generate owner ACL for new device
1675 ownerAcl = GenerateOwnerAcl(&ownerID);
1676 if(NULL == ownerAcl)
1678 OIC_LOG(ERROR, TAG, "Failed to generate owner ACL");
1679 return OC_STACK_NO_MEMORY;
1682 //Generate ACL payload
1683 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1686 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1687 res = OC_STACK_NO_MEMORY;
1691 res = AclToCBORPayload(ownerAcl, &secPayload->securityData, &secPayload->payloadSize);
1692 if (OC_STACK_OK != res)
1694 OICFree(secPayload);
1695 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
1698 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1700 OIC_LOG(DEBUG, TAG, "Owner ACL Payload:");
1701 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1703 //Send owner ACL to new device : POST /oic/sec/cred [ owner credential ]
1704 OCCallbackData cbData;
1705 cbData.cb = &OwnerAclHandler;
1706 cbData.context = (void *)otmCtx;
1708 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query,
1709 &deviceInfo->endpoint, (OCPayload*)secPayload,
1710 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1711 if (res != OC_STACK_OK)
1713 OIC_LOG(ERROR, TAG, "OCStack resource error");
1717 OIC_LOG(DEBUG, TAG, "OUT PostOwnerAcl");
1720 DeleteACLList(ownerAcl);
1725 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx)
1727 OIC_LOG(DEBUG, TAG, "IN PostOwnerTransferModeToResource");
1729 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1731 OIC_LOG(ERROR, TAG, "Invalid parameters");
1732 return OC_STACK_INVALID_PARAM;
1735 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1736 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1738 if(!PMGenerateQuery(false,
1739 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1740 deviceInfo->connType,
1741 query, sizeof(query), OIC_RSRC_DOXM_URI))
1743 OIC_LOG(ERROR, TAG, "PostOwnerTransferModeToResource : Failed to generate query");
1744 return OC_STACK_ERROR;
1746 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1748 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1751 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1752 return OC_STACK_NO_MEMORY;
1755 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1756 OCStackResult res = otmCtx->otmCallback.createSelectOxmPayloadCB(otmCtx,
1757 &secPayload->securityData, &secPayload->payloadSize);
1758 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1760 OCPayloadDestroy((OCPayload *)secPayload);
1761 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor");
1762 return OC_STACK_ERROR;
1765 OCCallbackData cbData;
1766 cbData.cb = &OwnerTransferModeHandler;
1767 cbData.context = (void *)otmCtx;
1769 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query,
1770 &deviceInfo->endpoint, (OCPayload *)secPayload,
1771 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1772 if (res != OC_STACK_OK)
1774 OIC_LOG(ERROR, TAG, "OCStack resource error");
1777 OIC_LOG(DEBUG, TAG, "OUT PostOwnerTransferModeToResource");
1782 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1784 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1786 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1788 OIC_LOG(ERROR, TAG, "Invailed parameters");
1789 return OC_STACK_INVALID_PARAM;
1792 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1793 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1794 if(!PMGenerateQuery(false,
1795 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1796 deviceInfo->connType,
1797 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1799 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1800 return OC_STACK_ERROR;
1802 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1804 OCCallbackData cbData;
1805 cbData.cb = &ListMethodsHandler;
1806 cbData.context = (void *)otmCtx;
1808 OCStackResult res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_GET, query, NULL, NULL,
1809 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1810 if (res != OC_STACK_OK)
1812 OIC_LOG(ERROR, TAG, "OCStack resource error");
1815 OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1820 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx)
1822 OIC_LOG(DEBUG, TAG, "IN PostOwnerUuid");
1824 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1826 OIC_LOG(ERROR, TAG, "Invailed parameters");
1827 return OC_STACK_INVALID_PARAM;
1830 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1831 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1832 if(!PMGenerateQuery(true,
1833 deviceInfo->endpoint.addr, deviceInfo->securePort,
1834 deviceInfo->connType,
1835 query, sizeof(query), OIC_RSRC_DOXM_URI))
1837 OIC_LOG(ERROR, TAG, "PostOwnerUuid : Failed to generate query");
1838 return OC_STACK_ERROR;
1840 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1842 //Post PT's uuid to new device
1843 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1846 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1847 return OC_STACK_NO_MEMORY;
1849 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1850 OCStackResult res = otmCtx->otmCallback.createOwnerTransferPayloadCB(
1851 otmCtx, &secPayload->securityData, &secPayload->payloadSize);
1852 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1854 OCPayloadDestroy((OCPayload *)secPayload);
1855 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to cbor.");
1856 return OC_STACK_INVALID_PARAM;
1858 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1860 OCCallbackData cbData;
1861 cbData.cb = &OwnerUuidUpdateHandler;
1862 cbData.context = (void *)otmCtx;
1865 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1866 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1867 if (res != OC_STACK_OK)
1869 OIC_LOG(ERROR, TAG, "OCStack resource error");
1872 OIC_LOG(DEBUG, TAG, "OUT PostOwnerUuid");
1877 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx)
1879 OIC_LOG(DEBUG, TAG, "IN PostOwnershipInformation");
1881 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1883 OIC_LOG(ERROR, TAG, "Invailed parameters");
1884 return OC_STACK_INVALID_PARAM;
1887 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1888 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1889 if(!PMGenerateQuery(true,
1890 deviceInfo->endpoint.addr, deviceInfo->securePort,
1891 deviceInfo->connType,
1892 query, sizeof(query), OIC_RSRC_DOXM_URI))
1894 OIC_LOG(ERROR, TAG, "PostOwnershipInformation : Failed to generate query");
1895 return OC_STACK_ERROR;
1897 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1899 //OwnershipInformationHandler
1900 OCSecurityPayload *secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1903 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1904 return OC_STACK_NO_MEMORY;
1907 otmCtx->selectedDeviceInfo->doxm->owned = true;
1909 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1910 OCStackResult res = DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm,
1911 &secPayload->securityData, &secPayload->payloadSize, true);
1912 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1914 OCPayloadDestroy((OCPayload *)secPayload);
1915 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1916 return OC_STACK_INVALID_PARAM;
1919 OCCallbackData cbData;
1920 cbData.cb = &OwnershipInformationHandler;
1921 cbData.context = (void *)otmCtx;
1924 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1925 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1926 if (res != OC_STACK_OK)
1928 OIC_LOG(ERROR, TAG, "OCStack resource error");
1931 OIC_LOG(DEBUG, TAG, "OUT PostOwnershipInformation");
1936 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx)
1938 OIC_LOG(DEBUG, TAG, "IN PostUpdateOperationMode");
1940 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1942 return OC_STACK_INVALID_PARAM;
1945 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1946 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1947 if(!PMGenerateQuery(false,
1948 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1949 deviceInfo->connType,
1950 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1952 OIC_LOG(ERROR, TAG, "PostUpdateOperationMode : Failed to generate query");
1953 return OC_STACK_ERROR;
1955 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1957 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1960 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1961 return OC_STACK_NO_MEMORY;
1963 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1964 OCStackResult res = PstatToCBORPayload(deviceInfo->pstat, &secPayload->securityData,
1965 &secPayload->payloadSize, true);
1966 if (OC_STACK_OK != res)
1968 OCPayloadDestroy((OCPayload *)secPayload);
1969 OIC_LOG(ERROR, TAG, "Error while converting pstat to cbor.");
1970 return OC_STACK_INVALID_PARAM;
1973 OCCallbackData cbData;
1974 cbData.cb = &OperationModeUpdateHandler;
1975 cbData.context = (void *)otmCtx;
1977 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1978 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1979 if (res != OC_STACK_OK)
1981 OIC_LOG(ERROR, TAG, "OCStack resource error");
1984 OIC_LOG(DEBUG, TAG, "OUT PostUpdateOperationMode");
1989 static OCStackResult SetupPDM(const OCProvisionDev_t* selectedDevice)
1991 OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
1993 PdmDeviceState_t pdmState = PDM_DEVICE_UNKNOWN;
1994 OCStackResult res = PDMGetDeviceState(&selectedDevice->doxm->deviceID, &pdmState);
1995 if (OC_STACK_OK != res)
1997 OIC_LOG_V(ERROR, TAG, "Internal error in PDMGetDeviceState : %d", res);
2001 char* strUuid = NULL;
2002 bool removeCredReq = false;
2003 if (OC_STACK_OK != ConvertUuidToStr(&selectedDevice->doxm->deviceID, &strUuid))
2005 OIC_LOG(WARNING, TAG, "Failed to covert uuid to string");
2006 return OC_STACK_NO_MEMORY;
2009 if (PDM_DEVICE_UNKNOWN == pdmState && !selectedDevice->doxm->owned)
2011 removeCredReq = true;
2013 else if (PDM_DEVICE_ACTIVE == pdmState && !selectedDevice->doxm->owned)
2015 OIC_LOG_V(WARNING, TAG, "Unowned device[%s] dectected from PDM.", strUuid);
2016 OIC_LOG_V(WARNING, TAG, "[%s] will be removed from PDM.", strUuid);
2017 res = PDMDeleteDevice(&selectedDevice->doxm->deviceID);
2018 if(OC_STACK_OK != res)
2020 OIC_LOG_V(ERROR, TAG, "Failed to remove [%s] information from PDM.", strUuid);
2024 removeCredReq = true;
2029 OIC_LOG_V(WARNING, TAG, "[%s]'s credential will be removed.", strUuid);
2030 res = RemoveCredential(&selectedDevice->doxm->deviceID);
2031 if (OC_STACK_RESOURCE_DELETED != res)
2033 OIC_LOG_V(WARNING, TAG, "Can not find [%s]'s credential.", strUuid);
2037 //Checking duplication of Device ID.
2038 bool isDuplicate = true;
2039 res = PDMIsDuplicateDevice(&selectedDevice->doxm->deviceID, &isDuplicate);
2040 if (OC_STACK_OK != res)
2042 OIC_LOG_V(ERROR, TAG, "Internal error in PDMIsDuplicateDevice : %d", res);
2048 char* strUuid = NULL;
2049 res = ConvertUuidToStr(&selectedDevice->doxm->deviceID, &strUuid);
2050 if (OC_STACK_OK != res)
2052 OIC_LOG_V(ERROR, TAG, "Failed to convert UUID to str : %d", res);
2056 if (PDM_DEVICE_STALE == pdmState)
2058 OIC_LOG(INFO, TAG, "Detected duplicated UUID in stale status, "
2059 "device status will revert back to initial status.");
2060 res = PDMSetDeviceState(&selectedDevice->doxm->deviceID, PDM_DEVICE_INIT);
2061 if (OC_STACK_OK != res)
2063 OIC_LOG_V(ERROR, TAG, "Internal error in PDMSetDeviceState : %d", res);
2067 else if (PDM_DEVICE_INIT == pdmState)
2069 OIC_LOG_V(ERROR, TAG, "[%s]'s ownership transfer process is already started.", strUuid);
2071 res = OC_STACK_DUPLICATE_REQUEST;
2076 OIC_LOG(ERROR, TAG, "Unknow device status while OTM.");
2078 res = OC_STACK_ERROR;
2084 res = PDMAddDevice(&selectedDevice->doxm->deviceID);
2085 if (OC_STACK_OK != res)
2087 OIC_LOG_V(ERROR, TAG, "Internal error in PDMAddDevice : %d", res);
2094 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
2098 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
2100 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
2101 OCStackResult res = OC_STACK_INVALID_PARAM;
2102 OicUuid_t emptyOwner = {.id = {0} };
2104 VERIFY_NON_NULL(TAG, selectedDevice, ERROR);
2105 VERIFY_NON_NULL(TAG, selectedDevice->doxm, ERROR);
2107 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
2108 otmCtx->selectedDeviceInfo = selectedDevice;
2110 //If devowneruuid of selectedDevice is not emtry, PostOwnerUuid does not triggered in DTLSHandshakeCB
2111 if (memcmp(&(selectedDevice->doxm->owner), &emptyOwner, sizeof(OicUuid_t)) != 0)
2113 OIC_LOG(DEBUG, TAG, "Set devowneruuid of selectedDevice to empty for OwnershipTransfer");
2114 memcpy(&(selectedDevice->doxm->owner), &emptyOwner, sizeof(OicUuid_t));
2117 //Setup PDM to perform the OTM, PDM will be cleanup if necessary.
2118 res = SetupPDM(selectedDevice);
2119 if(OC_STACK_OK != res)
2121 OIC_LOG_V(ERROR, TAG, "SetupPDM error : %d", res);
2122 SetResult(otmCtx, res);
2126 //Select the OxM to performing ownership transfer
2127 res = OTMSelectOwnershipTransferMethod(selectedDevice->doxm->oxm,
2128 selectedDevice->doxm->oxmLen,
2129 &selectedDevice->doxm->oxmSel,
2131 if(OC_STACK_OK != res)
2133 OIC_LOG_V(ERROR, TAG, "Failed to select the provisioning method : %d", res);
2134 SetResult(otmCtx, res);
2137 OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
2139 res = OTMSetOTCallback(selectedDevice->doxm->oxmSel, &otmCtx->otmCallback);
2140 if(OC_STACK_OK != res)
2142 OIC_LOG_V(ERROR, TAG, "Error in OTMSetOTCallback : %d", res);
2146 //Send Req: POST /oic/sec/doxm [{..."OxmSel" :g_OTMCbDatas[Index of Selected OxM].OXMString,...}]
2147 res = PostOwnerTransferModeToResource(otmCtx);
2148 if(OC_STACK_OK != res)
2150 OIC_LOG_V(WARNING, TAG, "Failed to select the provisioning method : %d", res);
2151 SetResult(otmCtx, res);
2155 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
2156 //Register TLS event handler to catch the tls event while handshake
2157 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(DTLSHandshakeCB))
2159 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register TLS handshake callback.");
2161 #endif // __WITH_DTLS__ or __WITH_TLS__
2162 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
2168 static OCStackResult StartCustomOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice,const OicSecOxm_t method)
2170 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
2171 OCStackResult res = OC_STACK_INVALID_PARAM;
2173 VERIFY_NON_NULL(TAG, selectedDevice, ERROR);
2174 VERIFY_NON_NULL(TAG, selectedDevice->doxm, ERROR);
2176 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
2177 otmCtx->selectedDeviceInfo = selectedDevice;
2179 //Setup PDM to perform the OTM, PDM will be cleanup if necessary.
2180 res = SetupPDM(selectedDevice);
2181 if(OC_STACK_OK != res)
2183 OIC_LOG_V(ERROR, TAG, "SetupPDM error : %d", res);
2184 SetResult(otmCtx, res);
2188 //Select the OxM to performing ownership transfer
2189 selectedDevice->doxm->oxmSel = method;
2190 OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
2192 res = OTMSetOTCallback(selectedDevice->doxm->oxmSel, &otmCtx->otmCallback);
2193 if(OC_STACK_OK != res)
2195 OIC_LOG_V(ERROR, TAG, "Error in OTMSetOTCallback : %d", res);
2199 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
2200 //Register TLS event handler, to catch the TLS handshake event
2201 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(DTLSHandshakeCB))
2203 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register TLS handshake callback.");
2205 #endif // __WITH_DTLS__ or __WITH_TLS__
2207 //Send Req: POST /oic/sec/doxm [{..."OxmSel" :g_OTMCbDatas[Index of Selected OxM].OXMString,...}]
2208 res = PostOwnerTransferModeToResource(otmCtx);
2209 if(OC_STACK_OK != res)
2211 OIC_LOG_V(WARNING, TAG, "Failed to select the provisioning method : %d", res);
2212 SetResult(otmCtx, res);
2216 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
2222 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
2224 OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
2228 OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
2229 return OC_STACK_INVALID_PARAM;
2231 if(oxmType >= OIC_OXM_COUNT)
2233 OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
2234 return OC_STACK_INVALID_PARAM;
2237 // TODO: Remove this API, Please see the jira ticket IOT-1484
2239 OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
2244 OCStackResult OTMDoCustomOwnershipTransfer(void* ctx,
2245 OCProvisionDev_t *selectedDevice,
2246 OCProvisionResultCB resultCallback,
2247 const OicSecOxm_t method)
2249 OIC_LOG(DEBUG, TAG, "IN OTMDoCustomOwnershipTransfer");
2251 if (NULL == selectedDevice)
2253 return OC_STACK_INVALID_PARAM;
2255 if (NULL == resultCallback)
2257 return OC_STACK_INVALID_CALLBACK;
2260 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
2263 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
2264 return OC_STACK_NO_MEMORY;
2267 otmCtx->ctxResultCallback = resultCallback;
2268 otmCtx->ctxHasError = false;
2269 otmCtx->userCtx = ctx;
2271 //Setting number of selected device.
2272 otmCtx->ctxResultArraySize = 1;
2274 otmCtx->ctxResultArray =
2275 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
2276 if(NULL == otmCtx->ctxResultArray)
2278 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
2280 return OC_STACK_NO_MEMORY;
2283 //Fill the device UUID for result array.
2284 memcpy(otmCtx->ctxResultArray[0].deviceId.id,
2285 selectedDevice->doxm->deviceID.id,
2287 otmCtx->ctxResultArray[0].res = OC_STACK_CONTINUE;
2289 OCStackResult res = StartCustomOwnershipTransfer(otmCtx, selectedDevice, method);
2291 OIC_LOG(DEBUG, TAG, "OUT OTMDoCustomOwnershipTransfer");
2297 * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
2299 OCStackResult OTMDoOwnershipTransfer(void* ctx,
2300 OCProvisionDev_t *selectedDevicelist,
2301 OCProvisionResultCB resultCallback)
2303 OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
2305 if (NULL == selectedDevicelist)
2307 return OC_STACK_INVALID_PARAM;
2309 if (NULL == resultCallback)
2311 return OC_STACK_INVALID_CALLBACK;
2314 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
2317 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
2318 return OC_STACK_NO_MEMORY;
2320 otmCtx->ctxResultCallback = resultCallback;
2321 otmCtx->ctxHasError = false;
2322 otmCtx->userCtx = ctx;
2323 OCProvisionDev_t* pCurDev = selectedDevicelist;
2325 //Counting number of selected devices.
2326 otmCtx->ctxResultArraySize = 0;
2327 while(NULL != pCurDev)
2329 otmCtx->ctxResultArraySize++;
2330 pCurDev = pCurDev->next;
2333 otmCtx->ctxResultArray =
2334 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
2335 if(NULL == otmCtx->ctxResultArray)
2337 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
2339 return OC_STACK_NO_MEMORY;
2341 pCurDev = selectedDevicelist;
2343 //Fill the device UUID for result array.
2344 for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
2346 memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
2347 pCurDev->doxm->deviceID.id,
2349 otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
2350 pCurDev = pCurDev->next;
2353 OCStackResult res = StartOwnershipTransfer(otmCtx, selectedDevicelist);
2355 OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
2360 OCStackResult OTMSetOxmAllowStatus(const OicSecOxm_t oxm, const bool allowStatus)
2362 OIC_LOG_V(INFO, TAG, "IN %s : oxm=%d, allow status=%s",
2363 __func__, oxm, (allowStatus ? "true" : "false"));
2365 #ifdef MULTIPLE_OWNER
2366 if(OIC_OXM_COUNT <= oxm && OIC_MV_JUST_WORKS != oxm && OIC_PRECONFIG_PIN != oxm && OIC_CON_MFG_CERT != oxm)
2368 if(OIC_OXM_COUNT <= oxm && OIC_MV_JUST_WORKS != oxm && OIC_CON_MFG_CERT != oxm)
2371 return OC_STACK_INVALID_PARAM;
2374 OxmAllowTableIdx_t oxmIdx = GetOxmAllowTableIdx(oxm);
2375 if(OXM_IDX_COUNT <= oxmIdx)
2377 OIC_LOG(ERROR, TAG, "Invalid oxm index to access oxm allow table.");
2378 return OC_STACK_ERROR;
2380 g_OxmAllowStatus[oxmIdx] = (allowStatus ? ALLOWED_OXM : NOT_ALLOWED_OXM);
2382 OIC_LOG_V(INFO, TAG, "OUT %s", __func__);
2387 OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx)
2389 OIC_LOG(INFO, TAG, "IN PostProvisioningStatus");
2391 if(!otmCtx || !otmCtx->selectedDeviceInfo)
2393 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
2394 return OC_STACK_INVALID_PARAM;
2397 //Change the TAKE_OWNER bit of CM to 0.
2398 otmCtx->selectedDeviceInfo->pstat->cm &= (~TAKE_OWNER);
2400 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
2403 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
2404 return OC_STACK_NO_MEMORY;
2406 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2407 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
2408 &secPayload->securityData, &secPayload->payloadSize, true))
2410 OCPayloadDestroy((OCPayload *)secPayload);
2411 return OC_STACK_INVALID_JSON;
2413 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
2414 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2416 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2417 if(!PMGenerateQuery(true,
2418 otmCtx->selectedDeviceInfo->endpoint.addr,
2419 otmCtx->selectedDeviceInfo->securePort,
2420 otmCtx->selectedDeviceInfo->connType,
2421 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2423 OIC_LOG(ERROR, TAG, "PostProvisioningStatus : Failed to generate query");
2424 return OC_STACK_ERROR;
2426 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2428 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2429 cbData.cb = &ProvisioningStatusHandler;
2430 cbData.context = (void*)otmCtx;
2432 OCStackResult ret = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload*)secPayload,
2433 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2434 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
2435 if (ret != OC_STACK_OK)
2437 OIC_LOG(ERROR, TAG, "OCStack resource error");
2440 OIC_LOG(INFO, TAG, "OUT PostProvisioningStatus");
2445 OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx)
2447 OIC_LOG(INFO, TAG, "IN PostNormalOperationStatus");
2449 if(!otmCtx || !otmCtx->selectedDeviceInfo)
2451 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
2452 return OC_STACK_INVALID_PARAM;
2456 otmCtx->selectedDeviceInfo->pstat->isOp = true;
2458 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
2461 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
2462 return OC_STACK_NO_MEMORY;
2464 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2465 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
2466 &secPayload->securityData, &secPayload->payloadSize, true))
2468 OCPayloadDestroy((OCPayload *)secPayload);
2469 return OC_STACK_INVALID_JSON;
2471 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
2472 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2474 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2475 if(!PMGenerateQuery(true,
2476 otmCtx->selectedDeviceInfo->endpoint.addr,
2477 otmCtx->selectedDeviceInfo->securePort,
2478 otmCtx->selectedDeviceInfo->connType,
2479 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2481 OIC_LOG(ERROR, TAG, "PostNormalOperationStatus : Failed to generate query");
2482 return OC_STACK_ERROR;
2484 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2486 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2487 cbData.cb = &ReadyForNomalStatusHandler;
2488 cbData.context = (void*)otmCtx;
2490 OCStackResult ret = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload*)secPayload,
2491 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2492 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
2493 if (ret != OC_STACK_OK)
2495 OIC_LOG(ERROR, TAG, "OCStack resource error");
2498 OIC_LOG(INFO, TAG, "OUT PostNormalOperationStatus");
2503 OCStackResult ConfigSelfOwnership(void)
2505 OIC_LOG(INFO, TAG, "IN ConfigSelfOwnership");
2507 bool isDeviceOwned = true;
2508 if (OC_STACK_OK != GetDoxmIsOwned(&isDeviceOwned))
2510 OIC_LOG (ERROR, TAG, "Unable to retrieve doxm owned state");
2511 return OC_STACK_ERROR;
2513 if( (true == isDeviceOwned) ||(true == GetPstatIsop()) )
2515 OIC_LOG(ERROR, TAG, "The state of device is not Ready for Ownership transfer.");
2516 return OC_STACK_ERROR;
2518 OicUuid_t deviceID = {.id={0}};
2519 if ( OC_STACK_OK != GetDoxmDeviceID(&deviceID) )
2521 OIC_LOG (ERROR, TAG, "Unable to retrieve doxm Device ID");
2522 return OC_STACK_ERROR;
2525 OCStackResult ret = OC_STACK_OK;
2526 //Update the pstat resource as Normal Operation.
2527 ret = SetPstatSelfOwnership(&deviceID);
2528 if(OC_STACK_OK != ret)
2530 OIC_LOG (ERROR, TAG, "Unable to update pstat resource as Normal Operation");
2533 //Update the doxm resource as Normal Operation.
2534 ret = SetDoxmSelfOwnership(&deviceID);
2535 if(OC_STACK_OK != ret)
2537 OIC_LOG (ERROR, TAG, "Unable to update doxm resource as Normal Operation");
2540 //Update default ACE of security resource to prevent anonymous user access.
2541 ret = UpdateDefaultSecProvACE();
2542 if(OC_STACK_OK != ret)
2544 OIC_LOG (ERROR, TAG, "Unable to update default ace in ConfigSelfOwnership");
2547 //Update the acl resource owner as owner device.
2548 ret = SetAclRownerId(&deviceID);
2549 if(OC_STACK_OK != ret)
2551 OIC_LOG (ERROR, TAG, "Unable to update acl resource in ConfigSelfOwnership");
2554 //Update the cred resource owner as owner device.
2555 ret = SetCredRownerId(&deviceID);
2556 if(OC_STACK_OK != ret)
2558 // Cred resouce may be empty in Ready for Ownership transfer state.
2559 if (OC_STACK_NO_RESOURCE == ret)
2561 OIC_LOG (INFO, TAG, "Cred resource is empty");
2565 OIC_LOG (ERROR, TAG, "Unable to update cred resource in ConfigSelfOwnership");
2569 if(OC_STACK_OK != ret)
2572 * If some error is occured while configure self-ownership,
2573 * ownership related resource should be revert back to initial status.
2575 ResetSecureResourceInPS();
2584 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
2585 DeleteOTMContextList();
2587 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
2588 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(NULL))
2590 OIC_LOG(WARNING, TAG, "Failed to register (D)TLS handshake callback.");
2592 #endif // __WITH_DTLS__ or __WITH_TLS__
2594 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);