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;
959 * Response handler for update owner uuid request.
961 * @param[in] ctx ctx value passed to callback from calling function.
962 * @param[in] UNUSED handle to an invocation
963 * @param[in] clientResponse Response from queries to remote servers.
964 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
965 * and OC_STACK_KEEP_TRANSACTION to keep it.
967 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
968 OCClientResponse *clientResponse)
970 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
971 VERIFY_NON_NULL(TAG, ctx, WARNING);
973 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
975 OCStackResult res = OC_STACK_OK;
976 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
977 otmCtx->ocDoHandle = NULL;
979 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
981 if(otmCtx && otmCtx->selectedDeviceInfo)
983 //In case of Mutual Verified Just-Works, wait for user confirmation
984 if (OIC_MV_JUST_WORKS == otmCtx->selectedDeviceInfo->doxm->oxmSel)
986 res = VerifyOwnershipTransfer(NULL, USER_CONFIRM);
987 if (OC_STACK_OK != res)
989 if (OC_STACK_OK != SRPResetDevice(otmCtx->selectedDeviceInfo, otmCtx->ctxResultCallback))
991 OIC_LOG(WARNING, TAG, "OwnerUuidUpdateHandler : SRPResetDevice error");
993 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to verify user confirm");
994 SetResult(otmCtx, res);
995 return OC_STACK_DELETE_TRANSACTION;
999 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
1000 if(OC_STACK_OK != res)
1002 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
1003 SetResult(otmCtx, res);
1004 return OC_STACK_DELETE_TRANSACTION;
1007 //POST owner credential to new device according to security spec B.
1008 res = PostOwnerCredential(otmCtx);
1009 if(OC_STACK_OK != res)
1012 "OwnerUuidUpdateHandler:Failed to send PosT request for onwer credential");
1013 SetResult(otmCtx, res);
1014 return OC_STACK_DELETE_TRANSACTION;
1020 if (((OIC_MANUFACTURER_CERTIFICATE == otmCtx->selectedDeviceInfo->doxm->oxmSel) ||
1021 (OIC_CON_MFG_CERT == otmCtx->selectedDeviceInfo->doxm->oxmSel)) &&
1022 OC_STACK_NOT_ACCEPTABLE == clientResponse->result)
1024 res = OC_STACK_USER_DENIED_REQ;
1025 OIC_LOG_V(ERROR, TAG,
1026 "OwnerUuidUpdateHandler : Denied Request(%d)", res);
1028 else if (OC_STACK_GATEWAY_TIMEOUT == clientResponse->result)
1030 res = clientResponse->result;
1031 OIC_LOG_V(ERROR, TAG,
1032 "OwnerUuidUpdateHandler : Timeout:No Response Received(%d)", res);
1036 res = clientResponse->result;
1037 OIC_LOG_V(ERROR, TAG, "OwnerUuidUpdateHandler : Unexpected result(%d)", res);
1039 SetResult(otmCtx, res);
1042 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
1045 return OC_STACK_DELETE_TRANSACTION;
1049 * Invokes Callback to load Random PIN
1051 void *LoadRandomPin(void *ctx)
1053 OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
1054 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1055 OCStackResult res = OC_STACK_ERROR;
1056 res = otmCtx->otmCallback.loadSecretCB(otmCtx);
1058 if(OC_STACK_OK != res)
1060 OIC_LOG_V(ERROR, TAG, "%s : Failed to load secret", __func__);
1061 SetResult(otmCtx, res);
1062 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
1066 //Save the current context instance to use on the dtls handshake callback
1067 if(OC_STACK_OK != AddOTMContext(otmCtx,
1068 otmCtx->selectedDeviceInfo->endpoint.addr,
1069 otmCtx->selectedDeviceInfo->securePort))
1071 OIC_LOG_V(ERROR, TAG, "%s : Failed to add OTM Context into OTM List.", __func__);
1072 SetResult(otmCtx, res);
1073 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
1077 //Try DTLS handshake to generate secure session
1078 if(otmCtx->otmCallback.createSecureSessionCB)
1080 res = otmCtx->otmCallback.createSecureSessionCB(otmCtx);
1081 if(OC_STACK_OK != res)
1083 OIC_LOG_V(ERROR, TAG, "%s : Failed to create DTLS session", __func__);
1084 SetResult(otmCtx, res);
1087 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
1092 * Response handler for update operation mode.
1094 * @param[in] ctx ctx value passed to callback from calling function.
1095 * @param[in] UNUSED handle to an invocation
1096 * @param[in] clientResponse Response from queries to remote servers.
1097 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1098 * and OC_STACK_KEEP_TRANSACTION to keep it.
1100 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
1101 OCClientResponse *clientResponse)
1103 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
1105 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1106 VERIFY_NON_NULL(TAG, ctx, WARNING);
1108 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1109 otmCtx->ocDoHandle = NULL;
1111 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1113 OCStackResult res = OC_STACK_ERROR;
1116 //Load secret for temporal secure session.
1117 if(otmCtx->otmCallback.loadSecretCB)
1119 if (OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
1123 thr_result = pthread_create(&p_thread, NULL, LoadRandomPin, (void *) otmCtx);
1124 if (0 != thr_result)
1126 OIC_LOG_V(ERROR, TAG, "pthread_create Error with code %d", thr_result);
1127 SetResult(otmCtx, res);
1128 return OC_STACK_DELETE_TRANSACTION;
1130 OIC_LOG(INFO, TAG, "Random Pin loadSecretCB Thread Created");
1134 res = otmCtx->otmCallback.loadSecretCB(otmCtx);
1135 if(OC_STACK_OK != res)
1137 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
1138 SetResult(otmCtx, res);
1139 return OC_STACK_DELETE_TRANSACTION;
1142 //Save the current context instance to use on the dtls handshake callback
1143 if(OC_STACK_OK != AddOTMContext(otmCtx,
1144 otmCtx->selectedDeviceInfo->endpoint.addr,
1145 otmCtx->selectedDeviceInfo->securePort))
1147 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to add OTM Context into OTM List.");
1148 SetResult(otmCtx, res);
1149 return OC_STACK_DELETE_TRANSACTION;
1152 //Try DTLS handshake to generate secure session
1153 if(otmCtx->otmCallback.createSecureSessionCB)
1155 res = otmCtx->otmCallback.createSecureSessionCB(otmCtx);
1156 if(OC_STACK_OK != res)
1158 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
1159 SetResult(otmCtx, res);
1160 return OC_STACK_DELETE_TRANSACTION;
1168 OIC_LOG(ERROR, TAG, "Error while update operation mode");
1169 SetResult(otmCtx, clientResponse->result);
1172 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
1175 return OC_STACK_DELETE_TRANSACTION;
1179 * Response handler for update owner crendetial request.
1181 * @param[in] ctx ctx value passed to callback from calling function.
1182 * @param[in] UNUSED handle to an invocation
1183 * @param[in] clientResponse Response from queries to remote servers.
1184 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1185 * and OC_STACK_KEEP_TRANSACTION to keep it.
1187 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
1188 OCClientResponse *clientResponse)
1190 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1191 VERIFY_NON_NULL(TAG, ctx, WARNING);
1193 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
1195 OCStackResult res = OC_STACK_OK;
1196 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1197 otmCtx->ocDoHandle = NULL;
1199 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1201 if(otmCtx && otmCtx->selectedDeviceInfo)
1203 //Close the temporal secure session to verify the owner credential
1204 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
1205 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
1206 CAResult_t caResult = CA_STATUS_OK;
1207 caResult = CAcloseSslConnection(endpoint);
1209 if(CA_STATUS_OK != caResult)
1211 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
1212 SetResult(otmCtx, caResult);
1213 return OC_STACK_DELETE_TRANSACTION;
1217 * If we select NULL cipher,
1218 * client will select appropriate cipher suite according to server's cipher-suite list.
1220 // TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 = 0xC037, /**< see RFC 5489 */
1221 caResult = CASelectCipherSuite(0xC037, endpoint->adapter);
1222 if(CA_STATUS_OK != caResult)
1224 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
1225 SetResult(otmCtx, caResult);
1226 return OC_STACK_DELETE_TRANSACTION;
1230 * in case of random PIN based OxM,
1231 * revert get_psk_info callback of tinyDTLS to use owner credential.
1233 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
1235 OicUuid_t emptyUuid = { .id={0}};
1236 SetUuidForPinBasedOxm(&emptyUuid);
1238 caResult = CAregisterPskCredentialsHandler(GetDtlsPskCredentials);
1240 if(CA_STATUS_OK != caResult)
1242 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
1243 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
1244 return OC_STACK_DELETE_TRANSACTION;
1248 otmCtx->selectedDeviceInfo->connType |= CT_FLAG_SECURE;
1250 res = PostOwnerAcl(otmCtx);
1251 if(OC_STACK_OK != res)
1253 OIC_LOG(ERROR, TAG, "Failed to update owner ACL to new device");
1254 SetResult(otmCtx, res);
1255 return OC_STACK_DELETE_TRANSACTION;
1261 res = clientResponse->result;
1262 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
1263 SetResult(otmCtx, res);
1266 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
1269 return OC_STACK_DELETE_TRANSACTION;
1273 * Response handler for update owner ACL request.
1275 * @param[in] ctx ctx value passed to callback from calling function.
1276 * @param[in] UNUSED handle to an invocation
1277 * @param[in] clientResponse Response from queries to remote servers.
1278 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1279 * and OC_STACK_KEEP_TRANSACTION to keep it.
1281 static OCStackApplicationResult OwnerAclHandler(void *ctx, OCDoHandle UNUSED,
1282 OCClientResponse *clientResponse)
1284 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1285 VERIFY_NON_NULL(TAG, ctx, WARNING);
1287 OIC_LOG(DEBUG, TAG, "IN OwnerAclHandler");
1289 OCStackResult res = OC_STACK_OK;
1290 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1291 otmCtx->ocDoHandle = NULL;
1293 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1295 if(otmCtx && otmCtx->selectedDeviceInfo)
1297 //POST /oic/sec/doxm [{ ..., "owned":"TRUE" }]
1298 res = PostOwnershipInformation(otmCtx);
1299 if(OC_STACK_OK != res)
1301 OIC_LOG(ERROR, TAG, "Failed to update ownership information to new device");
1302 SetResult(otmCtx, res);
1308 res = clientResponse->result;
1309 OIC_LOG_V(ERROR, TAG, "OwnerAclHandler : Unexpected result %d", res);
1310 SetResult(otmCtx, res);
1313 OIC_LOG(DEBUG, TAG, "OUT OwnerAclHandler");
1316 return OC_STACK_DELETE_TRANSACTION;
1321 * Response handler for update owner information request.
1323 * @param[in] ctx ctx value passed to callback from calling function.
1324 * @param[in] UNUSED handle to an invocation
1325 * @param[in] clientResponse Response from queries to remote servers.
1326 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1327 * and OC_STACK_KEEP_TRANSACTION to keep it.
1329 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
1330 OCClientResponse *clientResponse)
1332 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1333 VERIFY_NON_NULL(TAG, ctx, WARNING);
1335 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
1337 OCStackResult res = OC_STACK_OK;
1338 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1339 otmCtx->ocDoHandle = NULL;
1341 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1343 if(otmCtx && otmCtx->selectedDeviceInfo)
1345 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
1346 OIC_LOG(INFO, TAG, "Set Ready for provisioning state .");
1348 res = PostProvisioningStatus(otmCtx);
1349 if(OC_STACK_OK != res)
1351 OIC_LOG(ERROR, TAG, "Failed to update pstat");
1352 SetResult(otmCtx, res);
1358 res = clientResponse->result;
1359 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
1360 SetResult(otmCtx, res);
1363 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
1366 return OC_STACK_DELETE_TRANSACTION;
1370 * Response handler of update provisioning status.
1372 * @param[in] ctx ctx value passed to callback from calling function.
1373 * @param[in] UNUSED handle to an invocation
1374 * @param[in] clientResponse Response from queries to remote servers.
1375 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1376 * and OC_STACK_KEEP_TRANSACTION to keep it.
1378 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
1379 OCClientResponse *clientResponse)
1381 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
1383 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1384 VERIFY_NON_NULL(TAG, ctx, ERROR);
1386 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
1387 otmCtx->ocDoHandle = NULL;
1389 OCStackResult res = OC_STACK_OK;
1391 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1393 if(otmCtx && otmCtx->selectedDeviceInfo)
1395 OIC_LOG(INFO, TAG, "Device state is in Ready for Provisionig.");
1397 res = PostNormalOperationStatus(otmCtx);
1398 if(OC_STACK_OK != res)
1400 OIC_LOG(ERROR, TAG, "Failed to update pstat");
1401 SetResult(otmCtx, res);
1407 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1408 clientResponse->result);
1409 SetResult(otmCtx, clientResponse->result);
1413 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
1414 return OC_STACK_DELETE_TRANSACTION;
1418 * Response handler of update provisioning status to Ready for Normal..
1420 * @param[in] ctx ctx value passed to callback from calling function.
1421 * @param[in] UNUSED handle to an invocation
1422 * @param[in] clientResponse Response from queries to remote servers.
1423 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1424 * and OC_STACK_KEEP_TRANSACTION to keep it.
1426 static OCStackApplicationResult ReadyForNomalStatusHandler(void *ctx, OCDoHandle UNUSED,
1427 OCClientResponse *clientResponse)
1429 OIC_LOG_V(INFO, TAG, "IN ReadyForNomalStatusHandler.");
1431 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1432 VERIFY_NON_NULL(TAG, ctx, ERROR);
1434 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
1435 otmCtx->ocDoHandle = NULL;
1438 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1440 OIC_LOG(INFO, TAG, "Device state is in Ready for Normal Operation.");
1441 OCStackResult res = PDMSetDeviceState(&otmCtx->selectedDeviceInfo->doxm->deviceID,
1443 if (OC_STACK_OK == res)
1445 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
1446 SetResult(otmCtx, OC_STACK_OK);
1447 return OC_STACK_DELETE_TRANSACTION;
1451 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
1456 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1457 clientResponse->result);
1458 SetResult(otmCtx, clientResponse->result);
1462 OIC_LOG_V(INFO, TAG, "OUT ReadyForNomalStatusHandler.");
1463 return OC_STACK_DELETE_TRANSACTION;
1466 static OCStackResult PostOwnerCredential(OTMContext_t* otmCtx)
1468 OIC_LOG(DEBUG, TAG, "IN PostOwnerCredential");
1470 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1472 OIC_LOG(ERROR, TAG, "Invalid parameters");
1473 return OC_STACK_INVALID_PARAM;
1476 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1477 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1479 if(!PMGenerateQuery(true,
1480 deviceInfo->endpoint.addr, deviceInfo->securePort,
1481 deviceInfo->connType,
1482 query, sizeof(query), OIC_RSRC_CRED_URI))
1484 OIC_LOG(ERROR, TAG, "PostOwnerCredential : Failed to generate query");
1485 return OC_STACK_ERROR;
1487 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1488 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1491 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1492 return OC_STACK_NO_MEMORY;
1495 //Generate owner credential for new device
1496 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1497 const OicSecCred_t* ownerCredential = GetCredResourceData(&(deviceInfo->doxm->deviceID));
1498 if(!ownerCredential)
1500 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
1501 return OC_STACK_NO_RESOURCE;
1504 OicUuid_t credSubjectId = {.id={0}};
1505 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
1507 OicSecCred_t newCredential;
1508 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
1509 newCredential.next = NULL;
1511 //Set subject ID as PT's ID
1512 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
1514 //Fill private data as empty string
1515 newCredential.privateData.data = (uint8_t*)"";
1516 newCredential.privateData.len = 0;
1517 newCredential.privateData.encoding = ownerCredential->privateData.encoding;
1519 newCredential.publicData.data = NULL;
1520 newCredential.publicData.len = 0;
1521 newCredential.publicData.encoding = ownerCredential->publicData.encoding;
1524 //Send owner credential to new device : POST /oic/sec/cred [ owner credential ]
1525 if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData,
1526 &secPayload->payloadSize, secureFlag))
1528 OICFree(secPayload);
1529 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
1530 return OC_STACK_ERROR;
1532 OIC_LOG(DEBUG, TAG, "Cred Payload:");
1533 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1535 OCCallbackData cbData;
1536 cbData.cb = &OwnerCredentialHandler;
1537 cbData.context = (void *)otmCtx;
1539 OCStackResult res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query,
1540 &deviceInfo->endpoint, (OCPayload*)secPayload,
1541 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1542 if (res != OC_STACK_OK)
1544 OIC_LOG(ERROR, TAG, "OCStack resource error");
1549 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
1550 return OC_STACK_NO_RESOURCE;
1553 OIC_LOG(DEBUG, TAG, "OUT PostOwnerCredential");
1558 static OicSecAcl_t* GenerateOwnerAcl(const OicUuid_t* owner)
1560 OicSecAcl_t* ownerAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
1561 OicSecAce_t* ownerAce = (OicSecAce_t*)OICCalloc(1, sizeof(OicSecAce_t));
1562 OicSecRsrc_t* wildcardRsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
1563 if(NULL == ownerAcl || NULL == ownerAce || NULL == wildcardRsrc)
1565 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1568 LL_APPEND(ownerAcl->aces, ownerAce);
1569 LL_APPEND(ownerAce->resources, wildcardRsrc);
1571 //Set resource owner as PT
1572 memcpy(ownerAcl->rownerID.id, owner->id, sizeof(owner->id));
1574 //PT has full permission.
1575 ownerAce->permission = PERMISSION_FULL_CONTROL;
1577 //Set subject as PT's UUID
1578 memcpy(ownerAce->subjectuuid.id, owner->id, sizeof(owner->id));
1580 wildcardRsrc->href = OICStrdup(WILDCARD_RESOURCE_URI);
1581 if(NULL == wildcardRsrc->href)
1586 wildcardRsrc->interfaceLen = 1;
1587 wildcardRsrc->interfaces = (char**)OICMalloc(wildcardRsrc->interfaceLen * sizeof(char*));
1588 if(NULL == wildcardRsrc->interfaces)
1592 wildcardRsrc->interfaces[0] = OICStrdup(WILDCARD_RESOURCE_URI);
1593 if(NULL == wildcardRsrc->interfaces[0])
1598 wildcardRsrc->typeLen = 1;
1599 wildcardRsrc->types = (char**)OICMalloc(wildcardRsrc->typeLen * sizeof(char*));
1600 if(NULL == wildcardRsrc->types)
1604 wildcardRsrc->types[0] = OICStrdup(WILDCARD_RESOURCE_URI);
1605 if(NULL == wildcardRsrc->types[0])
1613 //in case of memory allocation failed, each resource should be removed individually.
1614 if(NULL == ownerAcl || NULL == ownerAce || NULL == wildcardRsrc)
1618 OICFree(wildcardRsrc);
1622 DeleteACLList(ownerAcl);
1628 * Function to update the owner ACL to new device.
1630 * @param[in] otmCtx Context value of ownership transfer.
1631 * @return OC_STACK_OK on success
1633 static OCStackResult PostOwnerAcl(OTMContext_t* otmCtx)
1635 OCStackResult res = OC_STACK_ERROR;
1637 OIC_LOG(DEBUG, TAG, "IN PostOwnerAcl");
1639 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1641 OIC_LOG(ERROR, TAG, "Invalid parameters");
1642 return OC_STACK_INVALID_PARAM;
1645 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1646 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1647 OicSecAcl_t* ownerAcl = NULL;
1649 if(!PMGenerateQuery(true,
1650 deviceInfo->endpoint.addr, deviceInfo->securePort,
1651 deviceInfo->connType,
1652 query, sizeof(query), OIC_RSRC_ACL_URI))
1654 OIC_LOG(ERROR, TAG, "Failed to generate query");
1655 return OC_STACK_ERROR;
1657 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1660 res = GetDoxmDeviceID(&ownerID);
1661 if(OC_STACK_OK != res)
1663 OIC_LOG(ERROR, TAG, "Failed to generate owner ACL");
1667 //Generate owner ACL for new device
1668 ownerAcl = GenerateOwnerAcl(&ownerID);
1669 if(NULL == ownerAcl)
1671 OIC_LOG(ERROR, TAG, "Failed to generate owner ACL");
1672 return OC_STACK_NO_MEMORY;
1675 //Generate ACL payload
1676 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1679 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1680 res = OC_STACK_NO_MEMORY;
1684 res = AclToCBORPayload(ownerAcl, &secPayload->securityData, &secPayload->payloadSize);
1685 if (OC_STACK_OK != res)
1687 OICFree(secPayload);
1688 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
1691 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1693 OIC_LOG(DEBUG, TAG, "Owner ACL Payload:");
1694 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1696 //Send owner ACL to new device : POST /oic/sec/cred [ owner credential ]
1697 OCCallbackData cbData;
1698 cbData.cb = &OwnerAclHandler;
1699 cbData.context = (void *)otmCtx;
1701 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query,
1702 &deviceInfo->endpoint, (OCPayload*)secPayload,
1703 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1704 if (res != OC_STACK_OK)
1706 OIC_LOG(ERROR, TAG, "OCStack resource error");
1710 OIC_LOG(DEBUG, TAG, "OUT PostOwnerAcl");
1713 DeleteACLList(ownerAcl);
1718 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx)
1720 OIC_LOG(DEBUG, TAG, "IN PostOwnerTransferModeToResource");
1722 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1724 OIC_LOG(ERROR, TAG, "Invalid parameters");
1725 return OC_STACK_INVALID_PARAM;
1728 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1729 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1731 if(!PMGenerateQuery(false,
1732 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1733 deviceInfo->connType,
1734 query, sizeof(query), OIC_RSRC_DOXM_URI))
1736 OIC_LOG(ERROR, TAG, "PostOwnerTransferModeToResource : Failed to generate query");
1737 return OC_STACK_ERROR;
1739 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1741 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1744 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1745 return OC_STACK_NO_MEMORY;
1748 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1749 OCStackResult res = otmCtx->otmCallback.createSelectOxmPayloadCB(otmCtx,
1750 &secPayload->securityData, &secPayload->payloadSize);
1751 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1753 OCPayloadDestroy((OCPayload *)secPayload);
1754 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor");
1755 return OC_STACK_ERROR;
1758 OCCallbackData cbData;
1759 cbData.cb = &OwnerTransferModeHandler;
1760 cbData.context = (void *)otmCtx;
1762 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query,
1763 &deviceInfo->endpoint, (OCPayload *)secPayload,
1764 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1765 if (res != OC_STACK_OK)
1767 OIC_LOG(ERROR, TAG, "OCStack resource error");
1770 OIC_LOG(DEBUG, TAG, "OUT PostOwnerTransferModeToResource");
1775 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1777 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1779 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1781 OIC_LOG(ERROR, TAG, "Invailed parameters");
1782 return OC_STACK_INVALID_PARAM;
1785 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1786 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1787 if(!PMGenerateQuery(false,
1788 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1789 deviceInfo->connType,
1790 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1792 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1793 return OC_STACK_ERROR;
1795 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1797 OCCallbackData cbData;
1798 cbData.cb = &ListMethodsHandler;
1799 cbData.context = (void *)otmCtx;
1801 OCStackResult res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_GET, query, NULL, NULL,
1802 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1803 if (res != OC_STACK_OK)
1805 OIC_LOG(ERROR, TAG, "OCStack resource error");
1808 OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1813 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx)
1815 OIC_LOG(DEBUG, TAG, "IN PostOwnerUuid");
1817 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1819 OIC_LOG(ERROR, TAG, "Invailed parameters");
1820 return OC_STACK_INVALID_PARAM;
1823 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1824 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1825 if(!PMGenerateQuery(true,
1826 deviceInfo->endpoint.addr, deviceInfo->securePort,
1827 deviceInfo->connType,
1828 query, sizeof(query), OIC_RSRC_DOXM_URI))
1830 OIC_LOG(ERROR, TAG, "PostOwnerUuid : Failed to generate query");
1831 return OC_STACK_ERROR;
1833 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1835 //Post PT's uuid to new device
1836 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1839 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1840 return OC_STACK_NO_MEMORY;
1842 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1843 OCStackResult res = otmCtx->otmCallback.createOwnerTransferPayloadCB(
1844 otmCtx, &secPayload->securityData, &secPayload->payloadSize);
1845 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1847 OCPayloadDestroy((OCPayload *)secPayload);
1848 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to cbor.");
1849 return OC_STACK_INVALID_PARAM;
1851 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1853 OCCallbackData cbData;
1854 cbData.cb = &OwnerUuidUpdateHandler;
1855 cbData.context = (void *)otmCtx;
1858 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1859 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1860 if (res != OC_STACK_OK)
1862 OIC_LOG(ERROR, TAG, "OCStack resource error");
1865 OIC_LOG(DEBUG, TAG, "OUT PostOwnerUuid");
1870 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx)
1872 OIC_LOG(DEBUG, TAG, "IN PostOwnershipInformation");
1874 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1876 OIC_LOG(ERROR, TAG, "Invailed parameters");
1877 return OC_STACK_INVALID_PARAM;
1880 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1881 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1882 if(!PMGenerateQuery(true,
1883 deviceInfo->endpoint.addr, deviceInfo->securePort,
1884 deviceInfo->connType,
1885 query, sizeof(query), OIC_RSRC_DOXM_URI))
1887 OIC_LOG(ERROR, TAG, "PostOwnershipInformation : Failed to generate query");
1888 return OC_STACK_ERROR;
1890 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1892 //OwnershipInformationHandler
1893 OCSecurityPayload *secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1896 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1897 return OC_STACK_NO_MEMORY;
1900 otmCtx->selectedDeviceInfo->doxm->owned = true;
1902 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1903 OCStackResult res = DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm,
1904 &secPayload->securityData, &secPayload->payloadSize, true);
1905 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1907 OCPayloadDestroy((OCPayload *)secPayload);
1908 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1909 return OC_STACK_INVALID_PARAM;
1912 OCCallbackData cbData;
1913 cbData.cb = &OwnershipInformationHandler;
1914 cbData.context = (void *)otmCtx;
1917 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1918 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1919 if (res != OC_STACK_OK)
1921 OIC_LOG(ERROR, TAG, "OCStack resource error");
1924 OIC_LOG(DEBUG, TAG, "OUT PostOwnershipInformation");
1929 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx)
1931 OIC_LOG(DEBUG, TAG, "IN PostUpdateOperationMode");
1933 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1935 return OC_STACK_INVALID_PARAM;
1938 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1939 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1940 if(!PMGenerateQuery(false,
1941 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1942 deviceInfo->connType,
1943 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1945 OIC_LOG(ERROR, TAG, "PostUpdateOperationMode : Failed to generate query");
1946 return OC_STACK_ERROR;
1948 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1950 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1953 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1954 return OC_STACK_NO_MEMORY;
1956 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1957 OCStackResult res = PstatToCBORPayload(deviceInfo->pstat, &secPayload->securityData,
1958 &secPayload->payloadSize, true);
1959 if (OC_STACK_OK != res)
1961 OCPayloadDestroy((OCPayload *)secPayload);
1962 OIC_LOG(ERROR, TAG, "Error while converting pstat to cbor.");
1963 return OC_STACK_INVALID_PARAM;
1966 OCCallbackData cbData;
1967 cbData.cb = &OperationModeUpdateHandler;
1968 cbData.context = (void *)otmCtx;
1970 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1971 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1972 if (res != OC_STACK_OK)
1974 OIC_LOG(ERROR, TAG, "OCStack resource error");
1977 OIC_LOG(DEBUG, TAG, "OUT PostUpdateOperationMode");
1982 static OCStackResult SetupPDM(const OCProvisionDev_t* selectedDevice)
1984 OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
1986 PdmDeviceState_t pdmState = PDM_DEVICE_UNKNOWN;
1987 OCStackResult res = PDMGetDeviceState(&selectedDevice->doxm->deviceID, &pdmState);
1988 if (OC_STACK_OK != res)
1990 OIC_LOG_V(ERROR, TAG, "Internal error in PDMGetDeviceState : %d", res);
1994 char* strUuid = NULL;
1995 bool removeCredReq = false;
1996 if (OC_STACK_OK != ConvertUuidToStr(&selectedDevice->doxm->deviceID, &strUuid))
1998 OIC_LOG(WARNING, TAG, "Failed to covert uuid to string");
1999 return OC_STACK_NO_MEMORY;
2002 if (PDM_DEVICE_UNKNOWN == pdmState && !selectedDevice->doxm->owned)
2004 removeCredReq = true;
2006 else if (PDM_DEVICE_ACTIVE == pdmState && !selectedDevice->doxm->owned)
2008 OIC_LOG_V(WARNING, TAG, "Unowned device[%s] dectected from PDM.", strUuid);
2009 OIC_LOG_V(WARNING, TAG, "[%s] will be removed from PDM.", strUuid);
2010 res = PDMDeleteDevice(&selectedDevice->doxm->deviceID);
2011 if(OC_STACK_OK != res)
2013 OIC_LOG_V(ERROR, TAG, "Failed to remove [%s] information from PDM.", strUuid);
2017 removeCredReq = true;
2022 OIC_LOG_V(WARNING, TAG, "[%s]'s credential will be removed.", strUuid);
2023 res = RemoveCredential(&selectedDevice->doxm->deviceID);
2024 if (OC_STACK_RESOURCE_DELETED != res)
2026 OIC_LOG_V(WARNING, TAG, "Can not find [%s]'s credential.", strUuid);
2030 //Checking duplication of Device ID.
2031 bool isDuplicate = true;
2032 res = PDMIsDuplicateDevice(&selectedDevice->doxm->deviceID, &isDuplicate);
2033 if (OC_STACK_OK != res)
2035 OIC_LOG_V(ERROR, TAG, "Internal error in PDMIsDuplicateDevice : %d", res);
2041 char* strUuid = NULL;
2042 res = ConvertUuidToStr(&selectedDevice->doxm->deviceID, &strUuid);
2043 if (OC_STACK_OK != res)
2045 OIC_LOG_V(ERROR, TAG, "Failed to convert UUID to str : %d", res);
2049 if (PDM_DEVICE_STALE == pdmState)
2051 OIC_LOG(INFO, TAG, "Detected duplicated UUID in stale status, "
2052 "device status will revert back to initial status.");
2053 res = PDMSetDeviceState(&selectedDevice->doxm->deviceID, PDM_DEVICE_INIT);
2054 if (OC_STACK_OK != res)
2056 OIC_LOG_V(ERROR, TAG, "Internal error in PDMSetDeviceState : %d", res);
2060 else if (PDM_DEVICE_INIT == pdmState)
2062 OIC_LOG_V(ERROR, TAG, "[%s]'s ownership transfer process is already started.", strUuid);
2064 res = OC_STACK_DUPLICATE_REQUEST;
2069 OIC_LOG(ERROR, TAG, "Unknow device status while OTM.");
2071 res = OC_STACK_ERROR;
2077 res = PDMAddDevice(&selectedDevice->doxm->deviceID);
2078 if (OC_STACK_OK != res)
2080 OIC_LOG_V(ERROR, TAG, "Internal error in PDMAddDevice : %d", res);
2087 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
2091 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
2093 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
2094 OCStackResult res = OC_STACK_INVALID_PARAM;
2095 OicUuid_t emptyOwner = {.id = {0} };
2097 VERIFY_NON_NULL(TAG, selectedDevice, ERROR);
2098 VERIFY_NON_NULL(TAG, selectedDevice->doxm, ERROR);
2100 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
2101 otmCtx->selectedDeviceInfo = selectedDevice;
2103 //If devowneruuid of selectedDevice is not emtry, PostOwnerUuid does not triggered in DTLSHandshakeCB
2104 if (memcmp(&(selectedDevice->doxm->owner), &emptyOwner, sizeof(OicUuid_t)) != 0)
2106 OIC_LOG(DEBUG, TAG, "Set devowneruuid of selectedDevice to empty for OwnershipTransfer");
2107 memcpy(&(selectedDevice->doxm->owner), &emptyOwner, sizeof(OicUuid_t));
2110 //Setup PDM to perform the OTM, PDM will be cleanup if necessary.
2111 res = SetupPDM(selectedDevice);
2112 if(OC_STACK_OK != res)
2114 OIC_LOG_V(ERROR, TAG, "SetupPDM error : %d", res);
2115 SetResult(otmCtx, res);
2119 //Select the OxM to performing ownership transfer
2120 res = OTMSelectOwnershipTransferMethod(selectedDevice->doxm->oxm,
2121 selectedDevice->doxm->oxmLen,
2122 &selectedDevice->doxm->oxmSel,
2124 if(OC_STACK_OK != res)
2126 OIC_LOG_V(ERROR, TAG, "Failed to select the provisioning method : %d", res);
2127 SetResult(otmCtx, res);
2130 OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
2132 res = OTMSetOTCallback(selectedDevice->doxm->oxmSel, &otmCtx->otmCallback);
2133 if(OC_STACK_OK != res)
2135 OIC_LOG_V(ERROR, TAG, "Error in OTMSetOTCallback : %d", res);
2139 //Send Req: POST /oic/sec/doxm [{..."OxmSel" :g_OTMCbDatas[Index of Selected OxM].OXMString,...}]
2140 res = PostOwnerTransferModeToResource(otmCtx);
2141 if(OC_STACK_OK != res)
2143 OIC_LOG_V(WARNING, TAG, "Failed to select the provisioning method : %d", res);
2144 SetResult(otmCtx, res);
2148 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
2149 //Register TLS event handler to catch the tls event while handshake
2150 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(DTLSHandshakeCB))
2152 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register TLS handshake callback.");
2154 #endif // __WITH_DTLS__ or __WITH_TLS__
2155 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
2161 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
2163 OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
2167 OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
2168 return OC_STACK_INVALID_PARAM;
2170 if(oxmType >= OIC_OXM_COUNT)
2172 OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
2173 return OC_STACK_INVALID_PARAM;
2176 // TODO: Remove this API, Please see the jira ticket IOT-1484
2178 OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
2184 * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
2186 OCStackResult OTMDoOwnershipTransfer(void* ctx,
2187 OCProvisionDev_t *selectedDevicelist,
2188 OCProvisionResultCB resultCallback)
2190 OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
2192 if (NULL == selectedDevicelist)
2194 return OC_STACK_INVALID_PARAM;
2196 if (NULL == resultCallback)
2198 return OC_STACK_INVALID_CALLBACK;
2201 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
2204 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
2205 return OC_STACK_NO_MEMORY;
2207 otmCtx->ctxResultCallback = resultCallback;
2208 otmCtx->ctxHasError = false;
2209 otmCtx->userCtx = ctx;
2210 OCProvisionDev_t* pCurDev = selectedDevicelist;
2212 //Counting number of selected devices.
2213 otmCtx->ctxResultArraySize = 0;
2214 while(NULL != pCurDev)
2216 otmCtx->ctxResultArraySize++;
2217 pCurDev = pCurDev->next;
2220 otmCtx->ctxResultArray =
2221 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
2222 if(NULL == otmCtx->ctxResultArray)
2224 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
2226 return OC_STACK_NO_MEMORY;
2228 pCurDev = selectedDevicelist;
2230 //Fill the device UUID for result array.
2231 for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
2233 memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
2234 pCurDev->doxm->deviceID.id,
2236 otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
2237 pCurDev = pCurDev->next;
2240 OCStackResult res = StartOwnershipTransfer(otmCtx, selectedDevicelist);
2242 OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
2247 OCStackResult OTMSetOxmAllowStatus(const OicSecOxm_t oxm, const bool allowStatus)
2249 OIC_LOG_V(INFO, TAG, "IN %s : oxm=%d, allow status=%s",
2250 __func__, oxm, (allowStatus ? "true" : "false"));
2252 #ifdef MULTIPLE_OWNER
2253 if(OIC_OXM_COUNT <= oxm && OIC_MV_JUST_WORKS != oxm && OIC_PRECONFIG_PIN != oxm && OIC_CON_MFG_CERT != oxm)
2255 if(OIC_OXM_COUNT <= oxm && OIC_MV_JUST_WORKS != oxm && OIC_CON_MFG_CERT != oxm)
2258 return OC_STACK_INVALID_PARAM;
2261 OxmAllowTableIdx_t oxmIdx = GetOxmAllowTableIdx(oxm);
2262 if(OXM_IDX_COUNT <= oxmIdx)
2264 OIC_LOG(ERROR, TAG, "Invalid oxm index to access oxm allow table.");
2265 return OC_STACK_ERROR;
2267 g_OxmAllowStatus[oxmIdx] = (allowStatus ? ALLOWED_OXM : NOT_ALLOWED_OXM);
2269 OIC_LOG_V(INFO, TAG, "OUT %s", __func__);
2274 OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx)
2276 OIC_LOG(INFO, TAG, "IN PostProvisioningStatus");
2278 if(!otmCtx || !otmCtx->selectedDeviceInfo)
2280 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
2281 return OC_STACK_INVALID_PARAM;
2284 //Change the TAKE_OWNER bit of CM to 0.
2285 otmCtx->selectedDeviceInfo->pstat->cm &= (~TAKE_OWNER);
2287 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
2290 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
2291 return OC_STACK_NO_MEMORY;
2293 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2294 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
2295 &secPayload->securityData, &secPayload->payloadSize, true))
2297 OCPayloadDestroy((OCPayload *)secPayload);
2298 return OC_STACK_INVALID_JSON;
2300 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
2301 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2303 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2304 if(!PMGenerateQuery(true,
2305 otmCtx->selectedDeviceInfo->endpoint.addr,
2306 otmCtx->selectedDeviceInfo->securePort,
2307 otmCtx->selectedDeviceInfo->connType,
2308 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2310 OIC_LOG(ERROR, TAG, "PostProvisioningStatus : Failed to generate query");
2311 return OC_STACK_ERROR;
2313 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2315 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2316 cbData.cb = &ProvisioningStatusHandler;
2317 cbData.context = (void*)otmCtx;
2319 OCStackResult ret = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload*)secPayload,
2320 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2321 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
2322 if (ret != OC_STACK_OK)
2324 OIC_LOG(ERROR, TAG, "OCStack resource error");
2327 OIC_LOG(INFO, TAG, "OUT PostProvisioningStatus");
2332 OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx)
2334 OIC_LOG(INFO, TAG, "IN PostNormalOperationStatus");
2336 if(!otmCtx || !otmCtx->selectedDeviceInfo)
2338 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
2339 return OC_STACK_INVALID_PARAM;
2343 otmCtx->selectedDeviceInfo->pstat->isOp = true;
2345 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
2348 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
2349 return OC_STACK_NO_MEMORY;
2351 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2352 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
2353 &secPayload->securityData, &secPayload->payloadSize, true))
2355 OCPayloadDestroy((OCPayload *)secPayload);
2356 return OC_STACK_INVALID_JSON;
2358 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
2359 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2361 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2362 if(!PMGenerateQuery(true,
2363 otmCtx->selectedDeviceInfo->endpoint.addr,
2364 otmCtx->selectedDeviceInfo->securePort,
2365 otmCtx->selectedDeviceInfo->connType,
2366 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2368 OIC_LOG(ERROR, TAG, "PostNormalOperationStatus : Failed to generate query");
2369 return OC_STACK_ERROR;
2371 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2373 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2374 cbData.cb = &ReadyForNomalStatusHandler;
2375 cbData.context = (void*)otmCtx;
2377 OCStackResult ret = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload*)secPayload,
2378 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2379 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
2380 if (ret != OC_STACK_OK)
2382 OIC_LOG(ERROR, TAG, "OCStack resource error");
2385 OIC_LOG(INFO, TAG, "OUT PostNormalOperationStatus");
2390 OCStackResult ConfigSelfOwnership(void)
2392 OIC_LOG(INFO, TAG, "IN ConfigSelfOwnership");
2394 bool isDeviceOwned = true;
2395 if (OC_STACK_OK != GetDoxmIsOwned(&isDeviceOwned))
2397 OIC_LOG (ERROR, TAG, "Unable to retrieve doxm owned state");
2398 return OC_STACK_ERROR;
2400 if( (true == isDeviceOwned) ||(true == GetPstatIsop()) )
2402 OIC_LOG(ERROR, TAG, "The state of device is not Ready for Ownership transfer.");
2403 return OC_STACK_ERROR;
2405 OicUuid_t deviceID = {.id={0}};
2406 if ( OC_STACK_OK != GetDoxmDeviceID(&deviceID) )
2408 OIC_LOG (ERROR, TAG, "Unable to retrieve doxm Device ID");
2409 return OC_STACK_ERROR;
2412 OCStackResult ret = OC_STACK_OK;
2413 //Update the pstat resource as Normal Operation.
2414 ret = SetPstatSelfOwnership(&deviceID);
2415 if(OC_STACK_OK != ret)
2417 OIC_LOG (ERROR, TAG, "Unable to update pstat resource as Normal Operation");
2420 //Update the doxm resource as Normal Operation.
2421 ret = SetDoxmSelfOwnership(&deviceID);
2422 if(OC_STACK_OK != ret)
2424 OIC_LOG (ERROR, TAG, "Unable to update doxm resource as Normal Operation");
2427 //Update default ACE of security resource to prevent anonymous user access.
2428 ret = UpdateDefaultSecProvACE();
2429 if(OC_STACK_OK != ret)
2431 OIC_LOG (ERROR, TAG, "Unable to update default ace in ConfigSelfOwnership");
2434 //Update the acl resource owner as owner device.
2435 ret = SetAclRownerId(&deviceID);
2436 if(OC_STACK_OK != ret)
2438 OIC_LOG (ERROR, TAG, "Unable to update acl resource in ConfigSelfOwnership");
2441 //Update the cred resource owner as owner device.
2442 ret = SetCredRownerId(&deviceID);
2443 if(OC_STACK_OK != ret)
2445 // Cred resouce may be empty in Ready for Ownership transfer state.
2446 if (OC_STACK_NO_RESOURCE == ret)
2448 OIC_LOG (INFO, TAG, "Cred resource is empty");
2452 OIC_LOG (ERROR, TAG, "Unable to update cred resource in ConfigSelfOwnership");
2456 if(OC_STACK_OK != ret)
2459 * If some error is occured while configure self-ownership,
2460 * ownership related resource should be revert back to initial status.
2462 ResetSecureResourceInPS();
2471 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
2472 DeleteOTMContextList();
2474 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
2475 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(NULL))
2477 OIC_LOG(WARNING, TAG, "Failed to register (D)TLS handshake callback.");
2479 #endif // __WITH_DTLS__ or __WITH_TLS__
2481 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);