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 "oxmrawpublickey.h"
70 #include "oxmmanufacturercert.h"
71 #include "secureresourceprovider.h"
74 #include "oxmpreconfpin.h"
75 #endif //MULTIPLE_OWNER
76 #include "otmcontextlist.h"
78 #include "pmutility.h"
79 #include "srmutility.h"
80 #include "provisioningdatabasemanager.h"
81 #include "ocpayload.h"
82 #include "payload_logging.h"
83 #include "pkix_interface.h"
84 #include "oxmverifycommon.h"
85 #include "psinterface.h"
91 #define NOT_ALLOWED_OXM 0
94 * List of allowed oxm list.
95 * All oxm methods are allowed as default.
98 static uint8_t g_OxmAllowStatus[OXM_IDX_COUNT] = {ALLOWED_OXM, ALLOWED_OXM, ALLOWED_OXM,
99 ALLOWED_OXM, ALLOWED_OXM, ALLOWED_OXM,
100 ALLOWED_OXM, NOT_ALLOWED_OXM};
102 static uint8_t g_OxmAllowStatus[OXM_IDX_COUNT] = {ALLOWED_OXM, ALLOWED_OXM, ALLOWED_OXM,
103 ALLOWED_OXM, ALLOWED_OXM, ALLOWED_OXM,
107 static OTMSelectMethodCallback g_selectOTMCB = NULL;
109 OCStackResult OTMSetOTCallback(OicSecOxm_t oxm, OTMCallbackData_t* callbacks)
111 OCStackResult res = OC_STACK_INVALID_PARAM;
113 OIC_LOG(INFO, TAG, "IN OTMSetOTCallback");
115 VERIFY_NON_NULL(TAG, callbacks, ERROR);
117 #ifdef MULTIPLE_OWNER
118 VERIFY_SUCCESS(TAG, (OIC_OXM_COUNT > oxm || OIC_PRECONFIG_PIN == oxm || OIC_MV_JUST_WORKS == oxm
119 || OIC_CON_MFG_CERT == oxm || OIC_RAW_PUB_KEY == oxm), ERROR);
121 VERIFY_SUCCESS(TAG, (OIC_OXM_COUNT > oxm || OIC_MV_JUST_WORKS == oxm || OIC_CON_MFG_CERT == oxm || OIC_RAW_PUB_KEY == oxm), ERROR);
122 #endif // MULTIPLE_OWNER
127 callbacks->loadSecretCB = LoadSecretJustWorksCallback;
128 callbacks->createSecureSessionCB = CreateSecureSessionJustWorksCallback;
129 callbacks->createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
130 callbacks->createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
132 case OIC_RANDOM_DEVICE_PIN:
133 callbacks->loadSecretCB = InputPinCodeCallback;
134 callbacks->createSecureSessionCB = CreateSecureSessionRandomPinCallback;
135 callbacks->createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
136 callbacks->createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
138 case OIC_MANUFACTURER_CERTIFICATE:
139 callbacks->loadSecretCB = PrepareMCertificateCallback;
140 callbacks->createSecureSessionCB = CreateSecureSessionMCertificateCallback;
141 callbacks->createSelectOxmPayloadCB = CreateMCertificateBasedSelectOxmPayload;
142 callbacks->createOwnerTransferPayloadCB = CreateMCertificateBasedOwnerTransferPayload;
144 case OIC_DECENTRALIZED_PUBLIC_KEY:
145 OIC_LOG(ERROR, TAG, "OIC_DECENTRALIZED_PUBLIC_KEY not supported yet.");
146 return OC_STACK_INVALID_METHOD;
147 #ifdef MULTIPLE_OWNER
148 case OIC_PRECONFIG_PIN:
149 callbacks->loadSecretCB = LoadPreconfigPinCodeCallback;
150 callbacks->createSecureSessionCB = CreateSecureSessionPreconfigPinCallback;
151 callbacks->createSelectOxmPayloadCB = CreatePreconfigPinBasedSelectOxmPayload;
152 callbacks->createOwnerTransferPayloadCB = CreatePreconfigPinBasedOwnerTransferPayload;
154 #endif //MULTIPLE_OWNER
155 case OIC_MV_JUST_WORKS:
156 callbacks->loadSecretCB = LoadSecretJustWorksCallback;
157 callbacks->createSecureSessionCB = CreateSecureSessionJustWorksCallback;
158 callbacks->createSelectOxmPayloadCB = CreateMVJustWorksSelectOxmPayload;
159 callbacks->createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
161 case OIC_CON_MFG_CERT:
162 callbacks->loadSecretCB = PrepareMCertificateCallback;
163 callbacks->createSecureSessionCB = CreateSecureSessionMCertificateCallback;
164 callbacks->createSelectOxmPayloadCB = CreateConMCertificateBasedSelectOxmPayload;
165 callbacks->createOwnerTransferPayloadCB = CreateMCertificateBasedOwnerTransferPayload;
167 case OIC_RAW_PUB_KEY:
168 callbacks->loadSecretCB = GetMasterRPKCallback;
169 callbacks->createSecureSessionCB = CreateSecureSessionRPKCallback;
170 callbacks->createSelectOxmPayloadCB = CreateRPKBasedSelectOxmPayload;
171 callbacks->createOwnerTransferPayloadCB = CreateRPKBasedOwnerTransferPayload;
174 OIC_LOG_V(ERROR, TAG, "Unknown OxM : %d", (int)oxm);
175 return OC_STACK_INVALID_PARAM;
181 OIC_LOG(INFO, TAG, "OUT OTMSetOTCallback");
185 void SetSelectOTMCB(OTMSelectMethodCallback selectOTMcb)
187 g_selectOTMCB = selectOTMcb;
191 void UnsetSelectOTMCB()
193 g_selectOTMCB = NULL;
198 * Internal API to convert OxM value to index of oxm allow table.
200 static OxmAllowTableIdx_t GetOxmAllowTableIdx(OicSecOxm_t oxm)
205 return OXM_IDX_JUST_WORKS;
206 case OIC_RANDOM_DEVICE_PIN:
207 return OXM_IDX_RANDOM_DEVICE_PIN;
208 case OIC_MANUFACTURER_CERTIFICATE:
209 return OXM_IDX_MANUFACTURER_CERTIFICATE;
210 case OIC_DECENTRALIZED_PUBLIC_KEY:
211 return OXM_IDX_DECENTRALIZED_PUBLIC_KEY;
212 case OIC_MV_JUST_WORKS:
213 return OXM_IDX_MV_JUST_WORKS;
214 case OIC_RAW_PUB_KEY:
215 return OXM_IDX_RAW_PUBLIC_KEY;
216 case OIC_CON_MFG_CERT:
217 return OXM_IDX_CON_MFG_CERT;
218 #ifdef MULTIPLE_OWNER
219 case OIC_PRECONFIG_PIN:
220 return OXM_IDX_PRECONFIG_PIN;
223 return OXM_IDX_UNKNOWN;
228 * Function to select appropriate provisioning method.
230 * @param[in] supportedMethods Array of supported methods
231 * @param[in] numberOfMethods number of supported methods
232 * @param[out] selectedMethod Selected methods
233 * @param[in] ownerType type of owner device (SUPER_OWNER or SUB_OWNER)
234 * @return OC_STACK_OK on success
236 OCStackResult OTMSelectOwnershipTransferMethod(const OicSecOxm_t *supportedMethods,
237 size_t numberOfMethods, OicSecOxm_t *selectedMethod, OwnerType_t ownerType)
239 bool isOxmSelected = false;
240 OxmAllowTableIdx_t selectedOxmIdx = OXM_IDX_UNKNOWN;
242 OIC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
244 if (numberOfMethods == 0 || !supportedMethods)
246 OIC_LOG(WARNING, TAG, "Could not find a supported OxM.");
247 return OC_STACK_ERROR;
256 uint32_t methNum = 0;
257 OicSecOxm_t list[10] = {0};
259 for (size_t i = 0; i < numberOfMethods; i++)
261 selectedOxmIdx = GetOxmAllowTableIdx(supportedMethods[i]);
262 if (OXM_IDX_COUNT <= selectedOxmIdx)
264 OIC_LOG(WARNING, TAG, "Invalid oxm index to access OxM allow table");
267 #ifdef MULTIPLE_OWNER
268 if (ALLOWED_OXM == g_OxmAllowStatus[selectedOxmIdx] &&
269 OXM_IDX_PRECONFIG_PIN != selectedOxmIdx)
272 if (ALLOWED_OXM == g_OxmAllowStatus[selectedOxmIdx])
273 #endif //MULTIPLE_OWNER
275 list[methNum] = supportedMethods[i];
279 *selectedMethod = g_selectOTMCB(list, methNum);
280 isOxmSelected = true;
284 for (size_t i = 0; i < numberOfMethods; i++)
286 selectedOxmIdx = GetOxmAllowTableIdx(supportedMethods[i]);
287 if (OXM_IDX_COUNT <= selectedOxmIdx)
289 OIC_LOG(WARNING, TAG, "Invalid oxm index to access OxM allow table");
292 #ifdef MULTIPLE_OWNER
293 if (ALLOWED_OXM == g_OxmAllowStatus[selectedOxmIdx] &&
294 OXM_IDX_PRECONFIG_PIN != selectedOxmIdx)
297 if (ALLOWED_OXM == g_OxmAllowStatus[selectedOxmIdx])
298 #endif //MULTIPLE_OWNER
300 *selectedMethod = supportedMethods[i];
301 isOxmSelected = true;
307 #ifdef MULTIPLE_OWNER
310 for (size_t i = 0; i < numberOfMethods; i++)
312 selectedOxmIdx = GetOxmAllowTableIdx(supportedMethods[i]);
313 if (OXM_IDX_COUNT <= selectedOxmIdx)
315 OIC_LOG(WARNING, TAG, "Invalid oxm index to access OxM allow table");
319 //in case of MOT, only Random PIN & Preconfigured PIN based OxM is allowed
320 if (ALLOWED_OXM == g_OxmAllowStatus[selectedOxmIdx] &&
321 (OXM_IDX_RANDOM_DEVICE_PIN == selectedOxmIdx ||
322 OXM_IDX_PRECONFIG_PIN == selectedOxmIdx))
324 *selectedMethod = supportedMethods[i];
325 isOxmSelected = true;
333 OIC_LOG_V(ERROR, TAG, "Unknown owner type or Not supported owner type : %d", ownerType);
334 return OC_STACK_INVALID_PARAM;
340 OIC_LOG(ERROR, TAG, "Can not find the allowed OxM.");
341 return OC_STACK_NOT_ALLOWED_OXM;
344 OIC_LOG(DEBUG, TAG, "OUT SelectProvisioningMethod");
350 * Function to select operation mode.This function will return most secure common operation mode.
352 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
353 * @param[out] selectedMode selected operation mode
354 * @return OC_STACK_OK on success
356 static void SelectOperationMode(const OCProvisionDev_t *selectedDeviceInfo,
357 OicSecDpom_t *selectedMode)
359 OIC_LOG(DEBUG, TAG, "IN SelectOperationMode");
360 *selectedMode = selectedDeviceInfo->pstat->sm[0];
361 OIC_LOG_V(DEBUG, TAG, "Selected Operation Mode = %d", *selectedMode);
365 * Function to start ownership transfer.
366 * This function will send the first request for provisioning,
367 * The next request message is sent from the response handler for this request.
369 * @param[in] ctx context value passed to callback from calling function.
370 * @param[in] selectedDevice selected device information to performing provisioning.
371 * @return OC_STACK_OK on success
373 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
376 * Internal function to setup & cleanup PDM to performing provisioning.
378 * @param[in] selectedDevice selected device information to performing provisioning.
379 * @return OC_STACK_OK on success
381 static OCStackResult SetupPDM(const OCProvisionDev_t* selectedDevice);
384 * Function to update owner transfer mode
386 * @param[in] otmCtx Context value of ownership transfer.
387 * @return OC_STACK_OK on success
389 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx);
392 * Function to send request to resource to get its pstat resource information.
394 * @param[in] otmCtx Context value of ownership transfer.
395 * @return OC_STACK_OK on success
397 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx);
401 * Function to send uuid of owner device to new device.
402 * This function would update 'owner of doxm' as UUID for provisioning tool.
404 * @param[in] otmCtx Context value of ownership transfer.
405 * @return OC_STACK_OK on success
407 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx);
410 * Function to update the operation mode. As per the spec. Operation mode in client driven
411 * single service provisioning it will be updated to 0x3
413 * @param[in] otmCtx Context value of ownership transfer.
414 * @return OC_STACK_OK on success
416 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx);
419 * Function to update the owner credential to new device
421 * @param[in] otmCtx Context value of ownership transfer.
422 * @param[in] selectedOperationMode selected operation mode
423 * @return OC_STACK_OK on success
425 static OCStackResult PostOwnerCredential(OTMContext_t* otmCtx);
428 * Function to update the owner ACL to new device.
430 * @param[in] otmCtx Context value of ownership transfer.
431 * @return OC_STACK_OK on success
433 static OCStackResult PostOwnerAcl(OTMContext_t* otmCtx);
436 * Function to send ownerShip info.
437 * This function would update 'owned of doxm' as true.
439 * @param[in] otmCtx Context value of ownership transfer.
440 * @return OC_STACK_OK on success
442 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx);
445 * Function to update pstat as Ready for provisioning.
446 * This function would update 'cm' from bx0000,0010 to bx0000,0000.
448 * @param[in] ctx context value passed to callback from calling function.
449 * @param[in] selectedDevice selected device information to performing provisioning.
450 * @return OC_STACK_OK on success
452 static OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx);
455 * Function to update pstat as Ready for Normal Operation.
456 * This function would update 'isop' from false to true.
458 * @param[in] ctx context value passed to callback from calling function.
459 * @param[in] selectedDevice selected device information to performing provisioning.
460 * @return OC_STACK_OK on success
462 static OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx);
464 static bool IsComplete(OTMContext_t* otmCtx)
466 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
468 if(OC_STACK_CONTINUE == otmCtx->ctxResultArray[i].res)
478 * Function to save the result of provisioning.
480 * @param[in,out] otmCtx Context value of ownership transfer.
481 * @param[in] res result of provisioning
483 void SetResult(OTMContext_t* otmCtx, const OCStackResult res)
485 OIC_LOG_V(DEBUG, TAG, "IN SetResult : %d ", res);
487 if(NULL == otmCtx || NULL == otmCtx->selectedDeviceInfo
488 || NULL == otmCtx->selectedDeviceInfo->doxm)
490 OIC_LOG(WARNING, TAG, "OTMContext is NULL");
494 //If OTM Context was removed from previous response handler, just exit the current OTM process.
495 if(NULL == GetOTMContext(otmCtx->selectedDeviceInfo->endpoint.addr,
496 otmCtx->selectedDeviceInfo->securePort))
498 OIC_LOG(WARNING, TAG, "Current OTM Process has already ended.");
501 //Revert psk_info callback and new deivce uuid in case of random PIN OxM
502 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
504 if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskCredentials))
506 OIC_LOG(WARNING, TAG, "Failed to revert is DTLS credential handler.");
508 OicUuid_t emptyUuid = { .id={0}};
509 SetUuidForPinBasedOxm(&emptyUuid);
511 else if(OIC_MANUFACTURER_CERTIFICATE == otmCtx->selectedDeviceInfo->doxm->oxmSel ||
512 OIC_CON_MFG_CERT == otmCtx->selectedDeviceInfo->doxm->oxmSel)
514 //Revert back certificate related callbacks.
515 if(CA_STATUS_OK != CAregisterPkixInfoHandler(GetPkixInfo))
517 OIC_LOG(WARNING, TAG, "Failed to revert PkixInfoHandler.");
519 if(CA_STATUS_OK != CAregisterGetCredentialTypesHandler(InitCipherSuiteList))
521 OIC_LOG(WARNING, TAG, "Failed to revert CredentialTypesHandler.");
525 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
527 if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
528 otmCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
530 otmCtx->ctxResultArray[i].res = res;
531 if(OC_STACK_OK != res && OC_STACK_CONTINUE != res && OC_STACK_DUPLICATE_REQUEST != res)
533 otmCtx->ctxHasError = true;
534 if (OC_STACK_OK != PDMDeleteDevice(&otmCtx->ctxResultArray[i].deviceId))
536 OIC_LOG(WARNING, TAG, "Internal error in PDMDeleteDevice");
538 CAEndpoint_t endpoint;
539 memcpy(&endpoint, &(otmCtx->selectedDeviceInfo->endpoint), sizeof(CAEndpoint_t));
540 endpoint.port = otmCtx->selectedDeviceInfo->securePort;
541 if (CA_STATUS_OK != CAcloseSslConnection(&endpoint))
543 OIC_LOG(WARNING, TAG, "Failed to close Secure session");
549 //In case of duplicated OTM process, OTMContext and OCDoHandle should not be removed.
550 if(OC_STACK_DUPLICATE_REQUEST != res)
552 //Remove the current OTM Context from OTM queue
553 RemoveOTMContext(otmCtx->selectedDeviceInfo->endpoint.addr,
554 otmCtx->selectedDeviceInfo->securePort);
556 //If there is a request being performed, cancel it to prevent retransmission.
557 if(otmCtx->ocDoHandle)
559 OIC_LOG_V(DEBUG, TAG, "OCCancel - %s : %d",
560 otmCtx->selectedDeviceInfo->endpoint.addr,
561 otmCtx->selectedDeviceInfo->securePort);
562 if(OC_STACK_OK != OCCancel(otmCtx->ocDoHandle, OC_HIGH_QOS, NULL, 0))
564 OIC_LOG(WARNING, TAG, "Failed to remove registered callback");
568 otmCtx->ocDoHandle = NULL;
573 //If all OTM process is complete, invoke the user callback.
574 if(IsComplete(otmCtx))
576 if(OC_STACK_OK != res && OC_STACK_CONTINUE != res && OC_STACK_DUPLICATE_REQUEST != res)
578 // Reset doxm and pstat properties to pre-Ownership Transfer state
579 OIC_LOG(DEBUG, TAG, "Resetting doxm and pstat properties");
580 if(otmCtx->selectedDeviceInfo->doxm)
582 OicUuid_t emptyUuid = {.id = {0}};
583 memcpy(&(otmCtx->selectedDeviceInfo->doxm->owner), &emptyUuid, sizeof(OicUuid_t));
584 otmCtx->selectedDeviceInfo->doxm->owned = false;
586 if(otmCtx->selectedDeviceInfo->pstat)
588 otmCtx->selectedDeviceInfo->pstat->isOp = false;
589 otmCtx->selectedDeviceInfo->pstat->cm |= TAKE_OWNER;
593 otmCtx->ctxResultCallback(otmCtx->userCtx, otmCtx->ctxResultArraySize,
594 otmCtx->ctxResultArray, otmCtx->ctxHasError);
595 OICFree(otmCtx->ctxResultArray);
600 if(OC_STACK_OK != StartOwnershipTransfer(otmCtx,
601 otmCtx->selectedDeviceInfo->next))
603 OIC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
607 OIC_LOG(DEBUG, TAG, "OUT SetResult");
611 * Function to handle the handshake result in OTM.
612 * This function will be invoked after DTLS handshake
613 * @param endPoint [IN] The remote endpoint.
614 * @param errorInfo [IN] Error information from the endpoint.
617 void DTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
619 OIC_LOG(DEBUG, TAG, "IN DTLSHandshakeCB");
620 if(NULL != endpoint && NULL != info)
622 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
623 endpoint->addr, endpoint->port, info->result);
625 OTMContext_t* otmCtx = GetOTMContext(endpoint->addr, endpoint->port);
628 OicSecDoxm_t* newDevDoxm = otmCtx->selectedDeviceInfo->doxm;
629 if(NULL != newDevDoxm)
631 OicUuid_t emptyUuid = {.id={0}};
633 //Make sure the address matches.
634 if(strncmp(otmCtx->selectedDeviceInfo->endpoint.addr,
636 sizeof(endpoint->addr)) == 0 &&
637 otmCtx->selectedDeviceInfo->securePort == endpoint->port)
639 OCStackResult res = OC_STACK_ERROR;
641 //If temporal secure sesstion established successfully
642 if(CA_STATUS_OK == info->result &&
643 false == newDevDoxm->owned &&
644 memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) == 0)
646 //In case of Mutual Verified Just-Works, display mutualVerifNum
647 if (OIC_MV_JUST_WORKS == newDevDoxm->oxmSel)
649 uint8_t preMutualVerifNum[OWNER_PSK_LENGTH_128] = {0};
650 uint8_t mutualVerifNum[MUTUAL_VERIF_NUM_LEN] = {0};
651 OicUuid_t deviceID = {.id = {0}};
653 //Generate mutualVerifNum
654 char label[LABEL_LEN] = {0};
655 snprintf(label, LABEL_LEN, "%s%s", MUTUAL_VERIF_NUM, OXM_MV_JUST_WORKS);
656 res = GetDoxmDeviceID(&deviceID);
657 if (OC_STACK_OK != res)
659 OIC_LOG(ERROR, TAG, "Error while retrieving Owner's device ID");
660 SetResult(otmCtx, res);
664 CAResult_t pskRet = CAGenerateOwnerPSK(endpoint,
667 deviceID.id, sizeof(deviceID.id),
668 newDevDoxm->deviceID.id, sizeof(newDevDoxm->deviceID.id),
669 preMutualVerifNum, OWNER_PSK_LENGTH_128);
670 if (CA_STATUS_OK != pskRet)
672 OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
673 SetResult(otmCtx, OC_STACK_ERROR);
677 memcpy(mutualVerifNum, preMutualVerifNum + OWNER_PSK_LENGTH_128 - sizeof(mutualVerifNum),
678 sizeof(mutualVerifNum));
679 res = VerifyOwnershipTransfer(mutualVerifNum, DISPLAY_NUM);
680 if (OC_STACK_OK != res)
682 OIC_LOG(ERROR, TAG, "Error while displaying mutualVerifNum");
683 SetResult(otmCtx, res);
687 //In case of confirmed manufacturer cert, display message
688 else if (OIC_MANUFACTURER_CERTIFICATE == newDevDoxm->oxmSel || OIC_CON_MFG_CERT == newDevDoxm->oxmSel)
690 res = VerifyOwnershipTransfer(NULL, DISPLAY_NUM);
691 if (OC_STACK_OK != res)
693 OIC_LOG(ERROR, TAG, "Error while displaying message");
694 SetResult(otmCtx, res);
699 //Send request : POST /oic/sec/doxm [{... , "devowner":"PT's UUID"}]
700 res = PostOwnerUuid(otmCtx);
701 if(OC_STACK_OK != res)
703 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
704 SetResult(otmCtx, res);
707 //In case of authentication failure
708 else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
710 //in case of error from owner credential
711 if(memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) != 0 &&
712 true == newDevDoxm->owned)
714 OIC_LOG(ERROR, TAG, "The owner credential may incorrect.");
716 if(OC_STACK_OK != RemoveCredential(&(newDevDoxm->deviceID)))
718 OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
720 SetResult(otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
722 //in case of error from wrong PIN, re-start the ownership transfer
723 else if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
725 OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
727 memcpy(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
728 newDevDoxm->owned = false;
729 otmCtx->attemptCnt++;
731 RemoveOTMContext(otmCtx->selectedDeviceInfo->endpoint.addr,
732 otmCtx->selectedDeviceInfo->securePort);
734 // In order to re-start ownership transfer, device information should be deleted from PDM.
735 res = PDMDeleteDevice(&(otmCtx->selectedDeviceInfo->doxm->deviceID));
736 if (OC_STACK_OK != res)
738 SetResult(otmCtx, res);
739 OIC_LOG(ERROR, TAG, "Failed to PDMDeleteDevice");
743 if(WRONG_PIN_MAX_ATTEMP > otmCtx->attemptCnt)
745 res = StartOwnershipTransfer(otmCtx, otmCtx->selectedDeviceInfo);
746 if(OC_STACK_OK != res)
748 SetResult(otmCtx, res);
749 OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
754 OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
755 SetResult(otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
761 OIC_LOG(ERROR, TAG, "Failed to establish secure session.");
762 SetResult(otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
770 OIC_LOG(ERROR, TAG, "Can not find the OTM Context.");
773 OIC_LOG(DEBUG, TAG, "OUT DTLSHandshakeCB");
777 * Function to save the Owner/SubOwner PSK.
779 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
780 * @return OC_STACK_OK on success
782 static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
784 OIC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
786 OCStackResult res = OC_STACK_ERROR;
788 CAEndpoint_t endpoint;
789 memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
790 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
791 endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
792 endpoint.port = selectedDeviceInfo->securePort;
793 endpoint.adapter = selectedDeviceInfo->endpoint.adapter;
795 OicUuid_t ownerDeviceID = {.id={0}};
796 if (OC_STACK_OK != GetDoxmDeviceID(&ownerDeviceID))
798 OIC_LOG(ERROR, TAG, "Error while retrieving Owner's device ID");
802 uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
803 OicSecKey_t ownerKey = {.data=ownerPSK, .len=OWNER_PSK_LENGTH_128, .encoding=OIC_ENCODING_RAW};
805 //Generating OwnerPSK
806 CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
807 (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
808 strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
809 ownerDeviceID.id, sizeof(ownerDeviceID.id),
810 selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
811 ownerPSK, OWNER_PSK_LENGTH_128);
813 if (CA_STATUS_OK == pskRet)
815 OIC_LOG(DEBUG, TAG,"Owner PSK dump:\n");
816 OIC_LOG_BUFFER(DEBUG, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
817 //Generating new credential for provisioning tool
818 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
819 SYMMETRIC_PAIR_WISE_KEY, NULL,
820 &ownerKey, &ownerDeviceID, NULL);
821 OICClearMemory(ownerPSK, sizeof(ownerPSK));
822 VERIFY_NON_NULL(TAG, cred, ERROR);
824 uint32_t outSize = 0;
825 size_t b64BufSize = B64ENCODE_OUT_SAFESIZE((OWNER_PSK_LENGTH_128 + 1));
826 char* b64Buf = (char *)OICCalloc(1, b64BufSize);
827 VERIFY_NON_NULL(TAG, b64Buf, ERROR);
828 b64Encode(cred->privateData.data, cred->privateData.len, b64Buf, b64BufSize, &outSize);
830 OICFree( cred->privateData.data );
831 cred->privateData.data = (uint8_t *)OICCalloc(1, outSize + 1);
832 VERIFY_NON_NULL(TAG, cred->privateData.data, ERROR);
834 strncpy((char*)(cred->privateData.data), b64Buf, outSize);
835 cred->privateData.data[outSize] = '\0';
836 cred->privateData.encoding = OIC_ENCODING_BASE64;
837 cred->privateData.len = outSize;
840 //Finding previous ownerPSK.
841 const OicSecCred_t* credList = GetCredList();
842 const OicSecCred_t* prevCred = NULL;
844 LL_FOREACH(credList, prevCred)
846 //OwnerPSK's type is SYMMETRIC_PAIR_WISE_KEY
847 if (SYMMETRIC_PAIR_WISE_KEY == prevCred->credType &&
848 0 == memcmp(prevCred->subject.id, cred->subject.id, sizeof(cred->subject.id)))
850 credId = prevCred->credId;
855 //If duplicate owner PSK is exists, remove it.
858 OIC_LOG(WARNING, TAG, "Duplicate OwnerPSK was detected.");
859 OIC_LOG(WARNING, TAG, "[Subject] : ");
860 OIC_LOG_BUFFER(WARNING, TAG, prevCred->subject.id, sizeof(prevCred->subject.id));
861 OIC_LOG_V(WARNING, TAG, "[Encoding Type] : %d", prevCred->privateData.encoding);
862 OIC_LOG(DEBUG, TAG, "[Private Data] : ");
863 OIC_LOG_BUFFER(DEBUG, TAG, prevCred->privateData.data, prevCred->privateData.len);
864 OIC_LOG(WARNING, TAG, "Previous OwnerPSK will be removed.");
866 res = RemoveCredentialByCredId(credId);
867 if(OC_STACK_RESOURCE_DELETED != res)
869 OIC_LOG(ERROR, TAG, "Failed to remove the previous OwnerPSK");
870 DeleteCredList(cred);
875 res = AddCredential(cred);
876 if(res != OC_STACK_OK)
878 DeleteCredList(cred);
884 OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
887 OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
893 * Callback handler for OwnerShipTransferModeHandler API.
895 * @param[in] ctx ctx value passed to callback from calling function.
896 * @param[in] UNUSED handle to an invocation
897 * @param[in] clientResponse Response from queries to remote servers.
898 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
899 * and OC_STACK_KEEP_TRANSACTION to keep it.
901 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
902 OCClientResponse *clientResponse)
904 OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
906 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
907 VERIFY_NON_NULL(TAG, ctx, WARNING);
909 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
910 otmCtx->ocDoHandle = NULL;
912 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
914 OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
915 //Send request : GET /oic/sec/pstat
916 OCStackResult res = GetProvisioningStatusResource(otmCtx);
917 if(OC_STACK_OK != res)
919 OIC_LOG(WARNING, TAG, "Failed to get pstat information");
920 SetResult(otmCtx, res);
925 OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
926 clientResponse->result);
927 SetResult(otmCtx, clientResponse->result);
930 OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
933 return OC_STACK_DELETE_TRANSACTION;
937 * Callback handler for ProvisioningStatusResouceHandler API.
939 * @param[in] ctx ctx value passed to callback from calling function.
940 * @param[in] UNUSED handle to an invocation
941 * @param[in] clientResponse Response from queries to remote servers.
942 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
943 * and OC_STACK_KEEP_TRANSACTION to keep it.
945 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
946 OCClientResponse *clientResponse)
948 OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
950 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
951 VERIFY_NON_NULL(TAG, ctx, WARNING);
953 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
954 otmCtx->ocDoHandle = NULL;
956 if (OC_STACK_OK == clientResponse->result)
958 if (NULL == clientResponse->payload)
960 OIC_LOG(INFO, TAG, "Skiping Null payload");
961 SetResult(otmCtx, OC_STACK_ERROR);
962 return OC_STACK_DELETE_TRANSACTION;
965 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
967 OIC_LOG(INFO, TAG, "Unknown payload type");
968 SetResult(otmCtx, OC_STACK_ERROR);
969 return OC_STACK_DELETE_TRANSACTION;
971 OicSecPstat_t* pstat = NULL;
972 OCStackResult result = CBORPayloadToPstat(
973 ((OCSecurityPayload*)clientResponse->payload)->securityData,
974 ((OCSecurityPayload*)clientResponse->payload)->payloadSize,
976 if(NULL == pstat || result != OC_STACK_OK)
978 OIC_LOG(ERROR, TAG, "Error while converting cbor to pstat.");
979 SetResult(otmCtx, OC_STACK_ERROR);
980 return OC_STACK_DELETE_TRANSACTION;
982 if(false == (TAKE_OWNER & pstat->cm))
984 OIC_LOG(ERROR, TAG, "Device pairing mode enabling owner transfer operations is disabled");
985 SetResult(otmCtx, OC_STACK_ERROR);
986 return OC_STACK_DELETE_TRANSACTION;
988 otmCtx->selectedDeviceInfo->pstat = pstat;
990 //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
991 SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
993 //Send request : POST /oic/sec/pstat [{"om":"bx11", .. }]
994 OCStackResult res = PostUpdateOperationMode(otmCtx);
995 if (OC_STACK_OK != res)
997 OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
998 SetResult(otmCtx, res);
1003 OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
1004 clientResponse->result);
1005 SetResult(otmCtx, clientResponse->result);
1008 OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
1010 return OC_STACK_DELETE_TRANSACTION;
1013 static void deleteCallback(void *ctx)
1016 OIC_LOG_V(DEBUG, TAG, "%s: otm context deleted", __func__);
1021 * Response handler for update owner uuid request.
1023 * @param[in] ctx ctx value passed to callback from calling function.
1024 * @param[in] UNUSED handle to an invocation
1025 * @param[in] clientResponse Response from queries to remote servers.
1026 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1027 * and OC_STACK_KEEP_TRANSACTION to keep it.
1029 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
1030 OCClientResponse *clientResponse)
1032 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1033 VERIFY_NON_NULL(TAG, ctx, WARNING);
1035 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
1037 OCStackResult res = OC_STACK_OK;
1038 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1039 otmCtx->ocDoHandle = NULL;
1041 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1043 if(otmCtx && otmCtx->selectedDeviceInfo)
1045 //In case of Mutual Verified Just-Works, wait for user confirmation
1046 if (OIC_MV_JUST_WORKS == otmCtx->selectedDeviceInfo->doxm->oxmSel)
1048 res = VerifyOwnershipTransfer(NULL, USER_CONFIRM);
1049 if (OC_STACK_OK != res)
1051 if (OC_STACK_OK != SRPResetDevice(otmCtx->selectedDeviceInfo, deleteCallback))
1053 OIC_LOG(WARNING, TAG, "OwnerUuidUpdateHandler : SRPResetDevice error");
1055 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to verify user confirm");
1056 SetResult(otmCtx, res);
1057 return OC_STACK_DELETE_TRANSACTION;
1061 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
1062 if(OC_STACK_OK != res)
1064 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
1065 SetResult(otmCtx, res);
1066 return OC_STACK_DELETE_TRANSACTION;
1069 //POST owner credential to new device according to security spec B.
1070 res = PostOwnerCredential(otmCtx);
1071 if(OC_STACK_OK != res)
1074 "OwnerUuidUpdateHandler:Failed to send PosT request for onwer credential");
1075 SetResult(otmCtx, res);
1076 return OC_STACK_DELETE_TRANSACTION;
1082 if (((OIC_MANUFACTURER_CERTIFICATE == otmCtx->selectedDeviceInfo->doxm->oxmSel) ||
1083 (OIC_CON_MFG_CERT == otmCtx->selectedDeviceInfo->doxm->oxmSel)) &&
1084 OC_STACK_NOT_ACCEPTABLE == clientResponse->result)
1086 res = OC_STACK_USER_DENIED_REQ;
1087 OIC_LOG_V(ERROR, TAG,
1088 "OwnerUuidUpdateHandler : Denied Request(%d)", res);
1090 else if (OC_STACK_GATEWAY_TIMEOUT == clientResponse->result)
1092 res = clientResponse->result;
1093 OIC_LOG_V(ERROR, TAG,
1094 "OwnerUuidUpdateHandler : Timeout:No Response Received(%d)", res);
1098 res = clientResponse->result;
1099 OIC_LOG_V(ERROR, TAG, "OwnerUuidUpdateHandler : Unexpected result(%d)", res);
1101 SetResult(otmCtx, res);
1104 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
1107 return OC_STACK_DELETE_TRANSACTION;
1111 * Invokes Callback to load Random PIN
1113 void *LoadRandomPin(void *ctx)
1115 OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
1116 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1117 OCStackResult res = OC_STACK_ERROR;
1118 res = otmCtx->otmCallback.loadSecretCB(otmCtx);
1120 if(OC_STACK_OK != res)
1122 OIC_LOG_V(ERROR, TAG, "%s : Failed to load secret", __func__);
1123 SetResult(otmCtx, res);
1124 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
1128 //Save the current context instance to use on the dtls handshake callback
1129 if(OC_STACK_OK != AddOTMContext(otmCtx,
1130 otmCtx->selectedDeviceInfo->endpoint.addr,
1131 otmCtx->selectedDeviceInfo->securePort))
1133 OIC_LOG_V(ERROR, TAG, "%s : Failed to add OTM Context into OTM List.", __func__);
1134 SetResult(otmCtx, res);
1135 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
1139 //Try DTLS handshake to generate secure session
1140 if(otmCtx->otmCallback.createSecureSessionCB)
1142 res = otmCtx->otmCallback.createSecureSessionCB(otmCtx);
1143 if(OC_STACK_OK != res)
1145 OIC_LOG_V(ERROR, TAG, "%s : Failed to create DTLS session", __func__);
1146 SetResult(otmCtx, res);
1149 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
1154 * Response handler for update operation mode.
1156 * @param[in] ctx ctx value passed to callback from calling function.
1157 * @param[in] UNUSED handle to an invocation
1158 * @param[in] clientResponse Response from queries to remote servers.
1159 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1160 * and OC_STACK_KEEP_TRANSACTION to keep it.
1162 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
1163 OCClientResponse *clientResponse)
1165 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
1167 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1168 VERIFY_NON_NULL(TAG, ctx, WARNING);
1170 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1171 otmCtx->ocDoHandle = NULL;
1173 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1175 OCStackResult res = OC_STACK_ERROR;
1178 //Load secret for temporal secure session.
1179 if(otmCtx->otmCallback.loadSecretCB)
1181 if (OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
1185 thr_result = pthread_create(&p_thread, NULL, LoadRandomPin, (void *) otmCtx);
1186 if (0 != thr_result)
1188 OIC_LOG_V(ERROR, TAG, "pthread_create Error with code %d", thr_result);
1189 SetResult(otmCtx, res);
1190 return OC_STACK_DELETE_TRANSACTION;
1192 OIC_LOG(INFO, TAG, "Random Pin loadSecretCB Thread Created");
1196 res = otmCtx->otmCallback.loadSecretCB(otmCtx);
1197 if(OC_STACK_OK != res)
1199 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
1200 SetResult(otmCtx, res);
1201 return OC_STACK_DELETE_TRANSACTION;
1204 //Save the current context instance to use on the dtls handshake callback
1205 if(OC_STACK_OK != AddOTMContext(otmCtx,
1206 otmCtx->selectedDeviceInfo->endpoint.addr,
1207 otmCtx->selectedDeviceInfo->securePort))
1209 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to add OTM Context into OTM List.");
1210 SetResult(otmCtx, res);
1211 return OC_STACK_DELETE_TRANSACTION;
1214 //Try DTLS handshake to generate secure session
1215 if(otmCtx->otmCallback.createSecureSessionCB)
1217 res = otmCtx->otmCallback.createSecureSessionCB(otmCtx);
1218 if(OC_STACK_OK != res)
1220 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
1221 SetResult(otmCtx, res);
1222 return OC_STACK_DELETE_TRANSACTION;
1230 OIC_LOG(ERROR, TAG, "Error while update operation mode");
1231 SetResult(otmCtx, clientResponse->result);
1234 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
1237 return OC_STACK_DELETE_TRANSACTION;
1241 * Response handler for update owner crendetial request.
1243 * @param[in] ctx ctx value passed to callback from calling function.
1244 * @param[in] UNUSED handle to an invocation
1245 * @param[in] clientResponse Response from queries to remote servers.
1246 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1247 * and OC_STACK_KEEP_TRANSACTION to keep it.
1249 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
1250 OCClientResponse *clientResponse)
1252 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1253 VERIFY_NON_NULL(TAG, ctx, WARNING);
1255 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
1257 OCStackResult res = OC_STACK_OK;
1258 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1259 otmCtx->ocDoHandle = NULL;
1261 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1263 if(otmCtx && otmCtx->selectedDeviceInfo)
1265 //Close the temporal secure session to verify the owner credential
1266 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
1267 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
1268 CAResult_t caResult = CA_STATUS_OK;
1269 caResult = CAcloseSslConnection(endpoint);
1271 if(CA_STATUS_OK != caResult)
1273 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
1274 SetResult(otmCtx, caResult);
1275 return OC_STACK_DELETE_TRANSACTION;
1279 * If we select NULL cipher,
1280 * client will select appropriate cipher suite according to server's cipher-suite list.
1282 // TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 = 0xC037, /**< see RFC 5489 */
1283 caResult = CASelectCipherSuite(0xC037, endpoint->adapter);
1284 if(CA_STATUS_OK != caResult)
1286 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
1287 SetResult(otmCtx, caResult);
1288 return OC_STACK_DELETE_TRANSACTION;
1292 * in case of random PIN based OxM,
1293 * revert get_psk_info callback of tinyDTLS to use owner credential.
1295 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
1297 OicUuid_t emptyUuid = { .id={0}};
1298 SetUuidForPinBasedOxm(&emptyUuid);
1300 caResult = CAregisterPskCredentialsHandler(GetDtlsPskCredentials);
1302 if(CA_STATUS_OK != caResult)
1304 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
1305 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
1306 return OC_STACK_DELETE_TRANSACTION;
1310 otmCtx->selectedDeviceInfo->connType |= CT_FLAG_SECURE;
1312 res = PostOwnerAcl(otmCtx);
1313 if(OC_STACK_OK != res)
1315 OIC_LOG(ERROR, TAG, "Failed to update owner ACL to new device");
1316 SetResult(otmCtx, res);
1317 return OC_STACK_DELETE_TRANSACTION;
1323 res = clientResponse->result;
1324 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
1325 SetResult(otmCtx, res);
1328 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
1331 return OC_STACK_DELETE_TRANSACTION;
1335 * Response handler for update owner ACL request.
1337 * @param[in] ctx ctx value passed to callback from calling function.
1338 * @param[in] UNUSED handle to an invocation
1339 * @param[in] clientResponse Response from queries to remote servers.
1340 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1341 * and OC_STACK_KEEP_TRANSACTION to keep it.
1343 static OCStackApplicationResult OwnerAclHandler(void *ctx, OCDoHandle UNUSED,
1344 OCClientResponse *clientResponse)
1346 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1347 VERIFY_NON_NULL(TAG, ctx, WARNING);
1349 OIC_LOG(DEBUG, TAG, "IN OwnerAclHandler");
1351 OCStackResult res = OC_STACK_OK;
1352 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1353 otmCtx->ocDoHandle = NULL;
1355 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1357 if(otmCtx && otmCtx->selectedDeviceInfo)
1359 //POST /oic/sec/doxm [{ ..., "owned":"TRUE" }]
1360 res = PostOwnershipInformation(otmCtx);
1361 if(OC_STACK_OK != res)
1363 OIC_LOG(ERROR, TAG, "Failed to update ownership information to new device");
1364 SetResult(otmCtx, res);
1370 res = clientResponse->result;
1371 OIC_LOG_V(ERROR, TAG, "OwnerAclHandler : Unexpected result %d", res);
1372 SetResult(otmCtx, res);
1375 OIC_LOG(DEBUG, TAG, "OUT OwnerAclHandler");
1378 return OC_STACK_DELETE_TRANSACTION;
1383 * Response handler for update owner information request.
1385 * @param[in] ctx ctx value passed to callback from calling function.
1386 * @param[in] UNUSED handle to an invocation
1387 * @param[in] clientResponse Response from queries to remote servers.
1388 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1389 * and OC_STACK_KEEP_TRANSACTION to keep it.
1391 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
1392 OCClientResponse *clientResponse)
1394 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1395 VERIFY_NON_NULL(TAG, ctx, WARNING);
1397 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
1399 OCStackResult res = OC_STACK_OK;
1400 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1401 otmCtx->ocDoHandle = NULL;
1403 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1405 if(otmCtx && otmCtx->selectedDeviceInfo)
1407 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
1408 OIC_LOG(INFO, TAG, "Set Ready for provisioning state .");
1410 res = PostProvisioningStatus(otmCtx);
1411 if(OC_STACK_OK != res)
1413 OIC_LOG(ERROR, TAG, "Failed to update pstat");
1414 SetResult(otmCtx, res);
1420 res = clientResponse->result;
1421 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
1422 SetResult(otmCtx, res);
1425 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
1428 return OC_STACK_DELETE_TRANSACTION;
1432 * Response handler of update provisioning status.
1434 * @param[in] ctx ctx value passed to callback from calling function.
1435 * @param[in] UNUSED handle to an invocation
1436 * @param[in] clientResponse Response from queries to remote servers.
1437 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1438 * and OC_STACK_KEEP_TRANSACTION to keep it.
1440 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
1441 OCClientResponse *clientResponse)
1443 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
1445 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1446 VERIFY_NON_NULL(TAG, ctx, ERROR);
1448 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
1449 otmCtx->ocDoHandle = NULL;
1451 OCStackResult res = OC_STACK_OK;
1453 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1455 if(otmCtx && otmCtx->selectedDeviceInfo)
1457 OIC_LOG(INFO, TAG, "Device state is in Ready for Provisionig.");
1459 res = PostNormalOperationStatus(otmCtx);
1460 if(OC_STACK_OK != res)
1462 OIC_LOG(ERROR, TAG, "Failed to update pstat");
1463 SetResult(otmCtx, res);
1469 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1470 clientResponse->result);
1471 SetResult(otmCtx, clientResponse->result);
1475 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
1476 return OC_STACK_DELETE_TRANSACTION;
1480 * Response handler of update provisioning status to Ready for Normal..
1482 * @param[in] ctx ctx value passed to callback from calling function.
1483 * @param[in] UNUSED handle to an invocation
1484 * @param[in] clientResponse Response from queries to remote servers.
1485 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1486 * and OC_STACK_KEEP_TRANSACTION to keep it.
1488 static OCStackApplicationResult ReadyForNomalStatusHandler(void *ctx, OCDoHandle UNUSED,
1489 OCClientResponse *clientResponse)
1491 OIC_LOG_V(INFO, TAG, "IN ReadyForNomalStatusHandler.");
1493 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1494 VERIFY_NON_NULL(TAG, ctx, ERROR);
1496 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
1497 otmCtx->ocDoHandle = NULL;
1500 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1502 OIC_LOG(INFO, TAG, "Device state is in Ready for Normal Operation.");
1503 OCStackResult res = PDMSetDeviceState(&otmCtx->selectedDeviceInfo->doxm->deviceID,
1505 if (OC_STACK_OK == res)
1507 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
1508 SetResult(otmCtx, OC_STACK_OK);
1509 return OC_STACK_DELETE_TRANSACTION;
1513 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
1518 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1519 clientResponse->result);
1520 SetResult(otmCtx, clientResponse->result);
1524 OIC_LOG_V(INFO, TAG, "OUT ReadyForNomalStatusHandler.");
1525 return OC_STACK_DELETE_TRANSACTION;
1528 static OCStackResult PostOwnerCredential(OTMContext_t* otmCtx)
1530 OIC_LOG(DEBUG, TAG, "IN PostOwnerCredential");
1532 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1534 OIC_LOG(ERROR, TAG, "Invalid parameters");
1535 return OC_STACK_INVALID_PARAM;
1538 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1539 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1541 if(!PMGenerateQuery(true,
1542 deviceInfo->endpoint.addr, deviceInfo->securePort,
1543 deviceInfo->connType,
1544 query, sizeof(query), OIC_RSRC_CRED_URI))
1546 OIC_LOG(ERROR, TAG, "PostOwnerCredential : Failed to generate query");
1547 return OC_STACK_ERROR;
1549 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1550 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1553 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1554 return OC_STACK_NO_MEMORY;
1557 //Generate owner credential for new device
1558 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1559 const OicSecCred_t* ownerCredential = GetCredResourceData(&(deviceInfo->doxm->deviceID));
1560 if(!ownerCredential)
1562 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
1563 return OC_STACK_NO_RESOURCE;
1566 OicUuid_t credSubjectId = {.id={0}};
1567 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
1569 OicSecCred_t newCredential;
1570 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
1571 newCredential.next = NULL;
1573 //Set subject ID as PT's ID
1574 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
1576 //Fill private data as empty string
1577 newCredential.privateData.data = (uint8_t*)"";
1578 newCredential.privateData.len = 0;
1579 newCredential.privateData.encoding = ownerCredential->privateData.encoding;
1581 newCredential.publicData.data = NULL;
1582 newCredential.publicData.len = 0;
1583 newCredential.publicData.encoding = ownerCredential->publicData.encoding;
1586 //Send owner credential to new device : POST /oic/sec/cred [ owner credential ]
1587 if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData,
1588 &secPayload->payloadSize, secureFlag))
1590 OICFree(secPayload);
1591 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
1592 return OC_STACK_ERROR;
1594 OIC_LOG(DEBUG, TAG, "Cred Payload:");
1595 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1597 OCCallbackData cbData;
1598 cbData.cb = &OwnerCredentialHandler;
1599 cbData.context = (void *)otmCtx;
1601 OCStackResult res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query,
1602 &deviceInfo->endpoint, (OCPayload*)secPayload,
1603 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1604 if (res != OC_STACK_OK)
1606 OIC_LOG(ERROR, TAG, "OCStack resource error");
1611 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
1612 return OC_STACK_NO_RESOURCE;
1615 OIC_LOG(DEBUG, TAG, "OUT PostOwnerCredential");
1620 static OicSecAcl_t* GenerateOwnerAcl(const OicUuid_t* owner)
1622 OicSecAcl_t* ownerAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
1623 OicSecAce_t* ownerAce = (OicSecAce_t*)OICCalloc(1, sizeof(OicSecAce_t));
1624 OicSecRsrc_t* wildcardRsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
1625 if(NULL == ownerAcl || NULL == ownerAce || NULL == wildcardRsrc)
1627 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1630 LL_APPEND(ownerAcl->aces, ownerAce);
1631 LL_APPEND(ownerAce->resources, wildcardRsrc);
1633 //Set resource owner as PT
1634 memcpy(ownerAcl->rownerID.id, owner->id, sizeof(owner->id));
1636 //PT has full permission.
1637 ownerAce->permission = PERMISSION_FULL_CONTROL;
1639 //Set subject as PT's UUID
1640 memcpy(ownerAce->subjectuuid.id, owner->id, sizeof(owner->id));
1642 wildcardRsrc->href = OICStrdup(WILDCARD_RESOURCE_URI);
1643 if(NULL == wildcardRsrc->href)
1648 wildcardRsrc->interfaceLen = 1;
1649 wildcardRsrc->interfaces = (char**)OICMalloc(wildcardRsrc->interfaceLen * sizeof(char*));
1650 if(NULL == wildcardRsrc->interfaces)
1654 wildcardRsrc->interfaces[0] = OICStrdup(WILDCARD_RESOURCE_URI);
1655 if(NULL == wildcardRsrc->interfaces[0])
1660 wildcardRsrc->typeLen = 1;
1661 wildcardRsrc->types = (char**)OICMalloc(wildcardRsrc->typeLen * sizeof(char*));
1662 if(NULL == wildcardRsrc->types)
1666 wildcardRsrc->types[0] = OICStrdup(WILDCARD_RESOURCE_URI);
1667 if(NULL == wildcardRsrc->types[0])
1675 //in case of memory allocation failed, each resource should be removed individually.
1676 if(NULL == ownerAcl || NULL == ownerAce || NULL == wildcardRsrc)
1680 OICFree(wildcardRsrc);
1684 DeleteACLList(ownerAcl);
1690 * Function to update the owner ACL to new device.
1692 * @param[in] otmCtx Context value of ownership transfer.
1693 * @return OC_STACK_OK on success
1695 static OCStackResult PostOwnerAcl(OTMContext_t* otmCtx)
1697 OCStackResult res = OC_STACK_ERROR;
1699 OIC_LOG(DEBUG, TAG, "IN PostOwnerAcl");
1701 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1703 OIC_LOG(ERROR, TAG, "Invalid parameters");
1704 return OC_STACK_INVALID_PARAM;
1707 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1708 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1709 OicSecAcl_t* ownerAcl = NULL;
1711 if(!PMGenerateQuery(true,
1712 deviceInfo->endpoint.addr, deviceInfo->securePort,
1713 deviceInfo->connType,
1714 query, sizeof(query), OIC_RSRC_ACL_URI))
1716 OIC_LOG(ERROR, TAG, "Failed to generate query");
1717 return OC_STACK_ERROR;
1719 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1722 res = GetDoxmDeviceID(&ownerID);
1723 if(OC_STACK_OK != res)
1725 OIC_LOG(ERROR, TAG, "Failed to generate owner ACL");
1729 //Generate owner ACL for new device
1730 ownerAcl = GenerateOwnerAcl(&ownerID);
1731 if(NULL == ownerAcl)
1733 OIC_LOG(ERROR, TAG, "Failed to generate owner ACL");
1734 return OC_STACK_NO_MEMORY;
1737 //Generate ACL payload
1738 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1741 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1742 res = OC_STACK_NO_MEMORY;
1746 res = AclToCBORPayload(ownerAcl, &secPayload->securityData, &secPayload->payloadSize);
1747 if (OC_STACK_OK != res)
1749 OICFree(secPayload);
1750 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
1753 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1755 OIC_LOG(DEBUG, TAG, "Owner ACL Payload:");
1756 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1758 //Send owner ACL to new device : POST /oic/sec/cred [ owner credential ]
1759 OCCallbackData cbData;
1760 cbData.cb = &OwnerAclHandler;
1761 cbData.context = (void *)otmCtx;
1763 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query,
1764 &deviceInfo->endpoint, (OCPayload*)secPayload,
1765 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1766 if (res != OC_STACK_OK)
1768 OIC_LOG(ERROR, TAG, "OCStack resource error");
1772 OIC_LOG(DEBUG, TAG, "OUT PostOwnerAcl");
1775 DeleteACLList(ownerAcl);
1780 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx)
1782 OIC_LOG(DEBUG, TAG, "IN PostOwnerTransferModeToResource");
1784 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1786 OIC_LOG(ERROR, TAG, "Invalid parameters");
1787 return OC_STACK_INVALID_PARAM;
1790 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1791 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1793 if(!PMGenerateQuery(false,
1794 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1795 deviceInfo->connType,
1796 query, sizeof(query), OIC_RSRC_DOXM_URI))
1798 OIC_LOG(ERROR, TAG, "PostOwnerTransferModeToResource : Failed to generate query");
1799 return OC_STACK_ERROR;
1801 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1803 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1806 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1807 return OC_STACK_NO_MEMORY;
1810 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1811 OCStackResult res = otmCtx->otmCallback.createSelectOxmPayloadCB(otmCtx,
1812 &secPayload->securityData, &secPayload->payloadSize);
1813 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1815 OCPayloadDestroy((OCPayload *)secPayload);
1816 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor");
1817 return OC_STACK_ERROR;
1820 OCCallbackData cbData;
1821 cbData.cb = &OwnerTransferModeHandler;
1822 cbData.context = (void *)otmCtx;
1824 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query,
1825 &deviceInfo->endpoint, (OCPayload *)secPayload,
1826 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1827 if (res != OC_STACK_OK)
1829 OIC_LOG(ERROR, TAG, "OCStack resource error");
1832 OIC_LOG(DEBUG, TAG, "OUT PostOwnerTransferModeToResource");
1837 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1839 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1841 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1843 OIC_LOG(ERROR, TAG, "Invailed parameters");
1844 return OC_STACK_INVALID_PARAM;
1847 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1848 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1849 if(!PMGenerateQuery(false,
1850 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1851 deviceInfo->connType,
1852 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1854 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1855 return OC_STACK_ERROR;
1857 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1859 OCCallbackData cbData;
1860 cbData.cb = &ListMethodsHandler;
1861 cbData.context = (void *)otmCtx;
1863 OCStackResult res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_GET, query, NULL, NULL,
1864 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1865 if (res != OC_STACK_OK)
1867 OIC_LOG(ERROR, TAG, "OCStack resource error");
1870 OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1875 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx)
1877 OIC_LOG(DEBUG, TAG, "IN PostOwnerUuid");
1879 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1881 OIC_LOG(ERROR, TAG, "Invailed parameters");
1882 return OC_STACK_INVALID_PARAM;
1885 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1886 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1887 if(!PMGenerateQuery(true,
1888 deviceInfo->endpoint.addr, deviceInfo->securePort,
1889 deviceInfo->connType,
1890 query, sizeof(query), OIC_RSRC_DOXM_URI))
1892 OIC_LOG(ERROR, TAG, "PostOwnerUuid : Failed to generate query");
1893 return OC_STACK_ERROR;
1895 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1897 //Post PT's uuid to new device
1898 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1901 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1902 return OC_STACK_NO_MEMORY;
1904 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1905 OCStackResult res = otmCtx->otmCallback.createOwnerTransferPayloadCB(
1906 otmCtx, &secPayload->securityData, &secPayload->payloadSize);
1907 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1909 OCPayloadDestroy((OCPayload *)secPayload);
1910 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to cbor.");
1911 return OC_STACK_INVALID_PARAM;
1913 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1915 OCCallbackData cbData;
1916 cbData.cb = &OwnerUuidUpdateHandler;
1917 cbData.context = (void *)otmCtx;
1920 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1921 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1922 if (res != OC_STACK_OK)
1924 OIC_LOG(ERROR, TAG, "OCStack resource error");
1927 OIC_LOG(DEBUG, TAG, "OUT PostOwnerUuid");
1932 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx)
1934 OIC_LOG(DEBUG, TAG, "IN PostOwnershipInformation");
1936 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1938 OIC_LOG(ERROR, TAG, "Invailed parameters");
1939 return OC_STACK_INVALID_PARAM;
1942 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1943 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1944 if(!PMGenerateQuery(true,
1945 deviceInfo->endpoint.addr, deviceInfo->securePort,
1946 deviceInfo->connType,
1947 query, sizeof(query), OIC_RSRC_DOXM_URI))
1949 OIC_LOG(ERROR, TAG, "PostOwnershipInformation : Failed to generate query");
1950 return OC_STACK_ERROR;
1952 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1954 //OwnershipInformationHandler
1955 OCSecurityPayload *secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1958 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1959 return OC_STACK_NO_MEMORY;
1962 otmCtx->selectedDeviceInfo->doxm->owned = true;
1964 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1965 OCStackResult res = DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm,
1966 &secPayload->securityData, &secPayload->payloadSize, true);
1967 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1969 OCPayloadDestroy((OCPayload *)secPayload);
1970 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1971 return OC_STACK_INVALID_PARAM;
1974 OCCallbackData cbData;
1975 cbData.cb = &OwnershipInformationHandler;
1976 cbData.context = (void *)otmCtx;
1979 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1980 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1981 if (res != OC_STACK_OK)
1983 OIC_LOG(ERROR, TAG, "OCStack resource error");
1986 OIC_LOG(DEBUG, TAG, "OUT PostOwnershipInformation");
1991 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx)
1993 OIC_LOG(DEBUG, TAG, "IN PostUpdateOperationMode");
1995 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1997 return OC_STACK_INVALID_PARAM;
2000 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
2001 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2002 if(!PMGenerateQuery(false,
2003 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
2004 deviceInfo->connType,
2005 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2007 OIC_LOG(ERROR, TAG, "PostUpdateOperationMode : Failed to generate query");
2008 return OC_STACK_ERROR;
2010 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2012 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
2015 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
2016 return OC_STACK_NO_MEMORY;
2018 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2019 OCStackResult res = PstatToCBORPayload(deviceInfo->pstat, &secPayload->securityData,
2020 &secPayload->payloadSize, true);
2021 if (OC_STACK_OK != res)
2023 OCPayloadDestroy((OCPayload *)secPayload);
2024 OIC_LOG(ERROR, TAG, "Error while converting pstat to cbor.");
2025 return OC_STACK_INVALID_PARAM;
2028 OCCallbackData cbData;
2029 cbData.cb = &OperationModeUpdateHandler;
2030 cbData.context = (void *)otmCtx;
2032 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload *)secPayload,
2033 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2034 if (res != OC_STACK_OK)
2036 OIC_LOG(ERROR, TAG, "OCStack resource error");
2039 OIC_LOG(DEBUG, TAG, "OUT PostUpdateOperationMode");
2044 static OCStackResult SetupPDM(const OCProvisionDev_t* selectedDevice)
2046 OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
2048 PdmDeviceState_t pdmState = PDM_DEVICE_UNKNOWN;
2049 OCStackResult res = PDMGetDeviceState(&selectedDevice->doxm->deviceID, &pdmState);
2050 if (OC_STACK_OK != res)
2052 OIC_LOG_V(ERROR, TAG, "Internal error in PDMGetDeviceState : %d", res);
2056 char* strUuid = NULL;
2057 bool removeCredReq = false;
2058 if (OC_STACK_OK != ConvertUuidToStr(&selectedDevice->doxm->deviceID, &strUuid))
2060 OIC_LOG(WARNING, TAG, "Failed to covert uuid to string");
2061 return OC_STACK_NO_MEMORY;
2064 if (PDM_DEVICE_UNKNOWN == pdmState && !selectedDevice->doxm->owned)
2066 removeCredReq = true;
2068 else if (PDM_DEVICE_ACTIVE == pdmState && !selectedDevice->doxm->owned)
2070 OIC_LOG_V(WARNING, TAG, "Unowned device[%s] dectected from PDM.", strUuid);
2071 OIC_LOG_V(WARNING, TAG, "[%s] will be removed from PDM.", strUuid);
2072 res = PDMDeleteDevice(&selectedDevice->doxm->deviceID);
2073 if(OC_STACK_OK != res)
2075 OIC_LOG_V(ERROR, TAG, "Failed to remove [%s] information from PDM.", strUuid);
2079 removeCredReq = true;
2084 OIC_LOG_V(WARNING, TAG, "[%s]'s credential will be removed.", strUuid);
2085 res = RemoveCredential(&selectedDevice->doxm->deviceID);
2086 if (OC_STACK_RESOURCE_DELETED != res)
2088 OIC_LOG_V(WARNING, TAG, "Can not find [%s]'s credential.", strUuid);
2092 //Checking duplication of Device ID.
2093 bool isDuplicate = true;
2094 res = PDMIsDuplicateDevice(&selectedDevice->doxm->deviceID, &isDuplicate);
2095 if (OC_STACK_OK != res)
2097 OIC_LOG_V(ERROR, TAG, "Internal error in PDMIsDuplicateDevice : %d", res);
2103 char* strUuid = NULL;
2104 res = ConvertUuidToStr(&selectedDevice->doxm->deviceID, &strUuid);
2105 if (OC_STACK_OK != res)
2107 OIC_LOG_V(ERROR, TAG, "Failed to convert UUID to str : %d", res);
2111 if (PDM_DEVICE_STALE == pdmState)
2113 OIC_LOG(INFO, TAG, "Detected duplicated UUID in stale status, "
2114 "device status will revert back to initial status.");
2115 res = PDMSetDeviceState(&selectedDevice->doxm->deviceID, PDM_DEVICE_INIT);
2116 if (OC_STACK_OK != res)
2118 OIC_LOG_V(ERROR, TAG, "Internal error in PDMSetDeviceState : %d", res);
2122 else if (PDM_DEVICE_INIT == pdmState)
2124 OIC_LOG_V(ERROR, TAG, "[%s]'s ownership transfer process is already started.", strUuid);
2126 res = OC_STACK_DUPLICATE_REQUEST;
2131 OIC_LOG(ERROR, TAG, "Unknow device status while OTM.");
2133 res = OC_STACK_ERROR;
2139 res = PDMAddDevice(&selectedDevice->doxm->deviceID);
2140 if (OC_STACK_OK != res)
2142 OIC_LOG_V(ERROR, TAG, "Internal error in PDMAddDevice : %d", res);
2149 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
2153 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
2155 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
2156 OCStackResult res = OC_STACK_INVALID_PARAM;
2157 OicUuid_t emptyOwner = {.id = {0} };
2159 VERIFY_NON_NULL(TAG, selectedDevice, ERROR);
2160 VERIFY_NON_NULL(TAG, selectedDevice->doxm, ERROR);
2162 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
2163 otmCtx->selectedDeviceInfo = selectedDevice;
2165 //If devowneruuid of selectedDevice is not emtry, PostOwnerUuid does not triggered in DTLSHandshakeCB
2166 if (memcmp(&(selectedDevice->doxm->owner), &emptyOwner, sizeof(OicUuid_t)) != 0)
2168 OIC_LOG(DEBUG, TAG, "Set devowneruuid of selectedDevice to empty for OwnershipTransfer");
2169 memcpy(&(selectedDevice->doxm->owner), &emptyOwner, sizeof(OicUuid_t));
2172 //Setup PDM to perform the OTM, PDM will be cleanup if necessary.
2173 res = SetupPDM(selectedDevice);
2174 if(OC_STACK_OK != res)
2176 OIC_LOG_V(ERROR, TAG, "SetupPDM error : %d", res);
2177 SetResult(otmCtx, res);
2181 //Select the OxM to performing ownership transfer
2182 res = OTMSelectOwnershipTransferMethod(selectedDevice->doxm->oxm,
2183 selectedDevice->doxm->oxmLen,
2184 &selectedDevice->doxm->oxmSel,
2186 if(OC_STACK_OK != res)
2188 OIC_LOG_V(ERROR, TAG, "Failed to select the provisioning method : %d", res);
2189 SetResult(otmCtx, res);
2192 OIC_LOG_V(DEBUG, TAG, "Selected provisioning method = %d", selectedDevice->doxm->oxmSel);
2194 res = OTMSetOTCallback(selectedDevice->doxm->oxmSel, &otmCtx->otmCallback);
2195 if(OC_STACK_OK != res)
2197 OIC_LOG_V(ERROR, TAG, "Error in OTMSetOTCallback : %d", res);
2201 //Send Req: POST /oic/sec/doxm [{..."OxmSel" :g_OTMCbDatas[Index of Selected OxM].OXMString,...}]
2202 res = PostOwnerTransferModeToResource(otmCtx);
2203 if(OC_STACK_OK != res)
2205 OIC_LOG_V(WARNING, TAG, "Failed to select the provisioning method : %d", res);
2206 SetResult(otmCtx, res);
2210 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
2211 //Register TLS event handler to catch the tls event while handshake
2212 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(DTLSHandshakeCB))
2214 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register TLS handshake callback.");
2216 #endif // __WITH_DTLS__ or __WITH_TLS__
2217 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
2223 static OCStackResult StartCustomOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice,const OicSecOxm_t method)
2225 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
2226 OCStackResult res = OC_STACK_INVALID_PARAM;
2228 VERIFY_NON_NULL(TAG, selectedDevice, ERROR);
2229 VERIFY_NON_NULL(TAG, selectedDevice->doxm, ERROR);
2231 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
2232 otmCtx->selectedDeviceInfo = selectedDevice;
2234 //Setup PDM to perform the OTM, PDM will be cleanup if necessary.
2235 res = SetupPDM(selectedDevice);
2236 if(OC_STACK_OK != res)
2238 OIC_LOG_V(ERROR, TAG, "SetupPDM error : %d", res);
2239 SetResult(otmCtx, res);
2243 //Select the OxM to performing ownership transfer
2244 selectedDevice->doxm->oxmSel = method;
2245 OIC_LOG_V(DEBUG, TAG, "Selected provisioning method = %d", selectedDevice->doxm->oxmSel);
2247 res = OTMSetOTCallback(selectedDevice->doxm->oxmSel, &otmCtx->otmCallback);
2248 if(OC_STACK_OK != res)
2250 OIC_LOG_V(ERROR, TAG, "Error in OTMSetOTCallback : %d", res);
2254 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
2255 //Register TLS event handler, to catch the TLS handshake event
2256 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(DTLSHandshakeCB))
2258 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register TLS handshake callback.");
2260 #endif // __WITH_DTLS__ or __WITH_TLS__
2262 //Send Req: POST /oic/sec/doxm [{..."OxmSel" :g_OTMCbDatas[Index of Selected OxM].OXMString,...}]
2263 res = PostOwnerTransferModeToResource(otmCtx);
2264 if(OC_STACK_OK != res)
2266 OIC_LOG_V(WARNING, TAG, "Failed to select the provisioning method : %d", res);
2267 SetResult(otmCtx, res);
2271 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
2277 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
2279 OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
2283 OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
2284 return OC_STACK_INVALID_PARAM;
2286 if(oxmType >= OIC_OXM_COUNT)
2288 OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
2289 return OC_STACK_INVALID_PARAM;
2292 // TODO: Remove this API, Please see the jira ticket IOT-1484
2294 OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
2299 OCStackResult OTMDoCustomOwnershipTransfer(void* ctx,
2300 OCProvisionDev_t *selectedDevice,
2301 OCProvisionResultCB resultCallback,
2302 const OicSecOxm_t method)
2304 OIC_LOG(DEBUG, TAG, "IN OTMDoCustomOwnershipTransfer");
2306 if (NULL == selectedDevice)
2308 return OC_STACK_INVALID_PARAM;
2310 if (NULL == resultCallback)
2312 return OC_STACK_INVALID_CALLBACK;
2315 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
2318 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
2319 return OC_STACK_NO_MEMORY;
2322 otmCtx->ctxResultCallback = resultCallback;
2323 otmCtx->ctxHasError = false;
2324 otmCtx->userCtx = ctx;
2326 //Setting number of selected device.
2327 otmCtx->ctxResultArraySize = 1;
2329 otmCtx->ctxResultArray =
2330 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
2331 if(NULL == otmCtx->ctxResultArray)
2333 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
2335 return OC_STACK_NO_MEMORY;
2338 //Fill the device UUID for result array.
2339 memcpy(otmCtx->ctxResultArray[0].deviceId.id,
2340 selectedDevice->doxm->deviceID.id,
2342 otmCtx->ctxResultArray[0].res = OC_STACK_CONTINUE;
2344 OCStackResult res = StartCustomOwnershipTransfer(otmCtx, selectedDevice, method);
2346 OIC_LOG(DEBUG, TAG, "OUT OTMDoCustomOwnershipTransfer");
2352 * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
2354 OCStackResult OTMDoOwnershipTransfer(void* ctx,
2355 OCProvisionDev_t *selectedDevicelist,
2356 OCProvisionResultCB resultCallback)
2358 OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
2360 if (NULL == selectedDevicelist)
2362 return OC_STACK_INVALID_PARAM;
2364 if (NULL == resultCallback)
2366 return OC_STACK_INVALID_CALLBACK;
2369 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
2372 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
2373 return OC_STACK_NO_MEMORY;
2375 otmCtx->ctxResultCallback = resultCallback;
2376 otmCtx->ctxHasError = false;
2377 otmCtx->userCtx = ctx;
2378 OCProvisionDev_t* pCurDev = selectedDevicelist;
2380 //Counting number of selected devices.
2381 otmCtx->ctxResultArraySize = 0;
2382 while(NULL != pCurDev)
2384 otmCtx->ctxResultArraySize++;
2385 pCurDev = pCurDev->next;
2388 otmCtx->ctxResultArray =
2389 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
2390 if(NULL == otmCtx->ctxResultArray)
2392 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
2394 return OC_STACK_NO_MEMORY;
2396 pCurDev = selectedDevicelist;
2398 //Fill the device UUID for result array.
2399 for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
2401 memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
2402 pCurDev->doxm->deviceID.id,
2404 otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
2405 pCurDev = pCurDev->next;
2408 OCStackResult res = StartOwnershipTransfer(otmCtx, selectedDevicelist);
2410 OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
2415 OCStackResult OTMSetOxmAllowStatus(const OicSecOxm_t oxm, const bool allowStatus)
2417 OIC_LOG_V(INFO, TAG, "IN %s : oxm=%d, allow status=%s",
2418 __func__, oxm, (allowStatus ? "true" : "false"));
2420 #ifdef MULTIPLE_OWNER
2421 if(OIC_OXM_COUNT <= oxm && OIC_MV_JUST_WORKS != oxm && OIC_PRECONFIG_PIN != oxm && OIC_CON_MFG_CERT != oxm)
2423 if(OIC_OXM_COUNT <= oxm && OIC_MV_JUST_WORKS != oxm && OIC_CON_MFG_CERT != oxm)
2426 return OC_STACK_INVALID_PARAM;
2429 OxmAllowTableIdx_t oxmIdx = GetOxmAllowTableIdx(oxm);
2430 if(OXM_IDX_COUNT <= oxmIdx)
2432 OIC_LOG(ERROR, TAG, "Invalid oxm index to access oxm allow table.");
2433 return OC_STACK_ERROR;
2435 g_OxmAllowStatus[oxmIdx] = (allowStatus ? ALLOWED_OXM : NOT_ALLOWED_OXM);
2437 OIC_LOG_V(INFO, TAG, "OUT %s", __func__);
2442 OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx)
2444 OIC_LOG(INFO, TAG, "IN PostProvisioningStatus");
2446 if(!otmCtx || !otmCtx->selectedDeviceInfo)
2448 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
2449 return OC_STACK_INVALID_PARAM;
2452 //Change the TAKE_OWNER bit of CM to 0.
2453 otmCtx->selectedDeviceInfo->pstat->cm &= (~TAKE_OWNER);
2455 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
2458 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
2459 return OC_STACK_NO_MEMORY;
2461 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2462 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
2463 &secPayload->securityData, &secPayload->payloadSize, true))
2465 OCPayloadDestroy((OCPayload *)secPayload);
2466 return OC_STACK_INVALID_JSON;
2468 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
2469 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2471 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2472 if(!PMGenerateQuery(true,
2473 otmCtx->selectedDeviceInfo->endpoint.addr,
2474 otmCtx->selectedDeviceInfo->securePort,
2475 otmCtx->selectedDeviceInfo->connType,
2476 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2478 OIC_LOG(ERROR, TAG, "PostProvisioningStatus : Failed to generate query");
2479 return OC_STACK_ERROR;
2481 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2483 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2484 cbData.cb = &ProvisioningStatusHandler;
2485 cbData.context = (void*)otmCtx;
2487 OCStackResult ret = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload*)secPayload,
2488 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2489 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
2490 if (ret != OC_STACK_OK)
2492 OIC_LOG(ERROR, TAG, "OCStack resource error");
2495 OIC_LOG(INFO, TAG, "OUT PostProvisioningStatus");
2500 OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx)
2502 OIC_LOG(INFO, TAG, "IN PostNormalOperationStatus");
2504 if(!otmCtx || !otmCtx->selectedDeviceInfo)
2506 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
2507 return OC_STACK_INVALID_PARAM;
2511 otmCtx->selectedDeviceInfo->pstat->isOp = true;
2513 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
2516 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
2517 return OC_STACK_NO_MEMORY;
2519 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2520 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
2521 &secPayload->securityData, &secPayload->payloadSize, true))
2523 OCPayloadDestroy((OCPayload *)secPayload);
2524 return OC_STACK_INVALID_JSON;
2526 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
2527 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2529 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2530 if(!PMGenerateQuery(true,
2531 otmCtx->selectedDeviceInfo->endpoint.addr,
2532 otmCtx->selectedDeviceInfo->securePort,
2533 otmCtx->selectedDeviceInfo->connType,
2534 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2536 OIC_LOG(ERROR, TAG, "PostNormalOperationStatus : Failed to generate query");
2537 return OC_STACK_ERROR;
2539 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2541 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2542 cbData.cb = &ReadyForNomalStatusHandler;
2543 cbData.context = (void*)otmCtx;
2545 OCStackResult ret = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload*)secPayload,
2546 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2547 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
2548 if (ret != OC_STACK_OK)
2550 OIC_LOG(ERROR, TAG, "OCStack resource error");
2553 OIC_LOG(INFO, TAG, "OUT PostNormalOperationStatus");
2558 OCStackResult ConfigSelfOwnership(void)
2560 OIC_LOG(INFO, TAG, "IN ConfigSelfOwnership");
2562 bool isDeviceOwned = true;
2563 if (OC_STACK_OK != GetDoxmIsOwned(&isDeviceOwned))
2565 OIC_LOG (ERROR, TAG, "Unable to retrieve doxm owned state");
2566 return OC_STACK_ERROR;
2568 if( (true == isDeviceOwned) ||(true == GetPstatIsop()) )
2570 OIC_LOG(ERROR, TAG, "The state of device is not Ready for Ownership transfer.");
2571 return OC_STACK_ERROR;
2573 OicUuid_t deviceID = {.id={0}};
2574 if ( OC_STACK_OK != GetDoxmDeviceID(&deviceID) )
2576 OIC_LOG (ERROR, TAG, "Unable to retrieve doxm Device ID");
2577 return OC_STACK_ERROR;
2580 OCStackResult ret = OC_STACK_OK;
2581 //Update the pstat resource as Normal Operation.
2582 ret = SetPstatSelfOwnership(&deviceID);
2583 if(OC_STACK_OK != ret)
2585 OIC_LOG (ERROR, TAG, "Unable to update pstat resource as Normal Operation");
2588 //Update the doxm resource as Normal Operation.
2589 ret = SetDoxmSelfOwnership(&deviceID);
2590 if(OC_STACK_OK != ret)
2592 OIC_LOG (ERROR, TAG, "Unable to update doxm resource as Normal Operation");
2595 //Update default ACE of security resource to prevent anonymous user access.
2596 ret = UpdateDefaultSecProvACE();
2597 if(OC_STACK_OK != ret)
2599 OIC_LOG (ERROR, TAG, "Unable to update default ace in ConfigSelfOwnership");
2602 //Update the acl resource owner as owner device.
2603 ret = SetAclRownerId(&deviceID);
2604 if(OC_STACK_OK != ret)
2606 OIC_LOG (ERROR, TAG, "Unable to update acl resource in ConfigSelfOwnership");
2609 //Update the cred resource owner as owner device.
2610 ret = SetCredRownerId(&deviceID);
2611 if(OC_STACK_OK != ret)
2613 // Cred resouce may be empty in Ready for Ownership transfer state.
2614 if (OC_STACK_NO_RESOURCE == ret)
2616 OIC_LOG (INFO, TAG, "Cred resource is empty");
2620 OIC_LOG (ERROR, TAG, "Unable to update cred resource in ConfigSelfOwnership");
2624 if(OC_STACK_OK != ret)
2627 * If some error is occured while configure self-ownership,
2628 * ownership related resource should be revert back to initial status.
2630 ResetSecureResourceInPS();
2639 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
2640 DeleteOTMContextList();
2642 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
2643 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(NULL))
2645 OIC_LOG(WARNING, TAG, "Failed to register (D)TLS handshake callback.");
2647 #endif // __WITH_DTLS__ or __WITH_TLS__
2649 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);