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
39 #ifdef HAVE_SYS_TIME_H
46 #include "oic_malloc.h"
47 #include "oic_string.h"
49 #include "cainterface.h"
54 #include "srmresourcestrings.h"
55 #include "doxmresource.h"
56 #include "pstatresource.h"
57 #include "credresource.h"
58 #include "aclresource.h"
59 #include "ownershiptransfermanager.h"
60 #include "securevirtualresourcetypes.h"
61 #include "oxmjustworks.h"
63 #include "pmutility.h"
64 #include "srmutility.h"
65 #include "provisioningdatabasemanager.h"
66 #include "oxmrandompin.h"
67 #include "ocpayload.h"
68 #include "payload_logging.h"
73 * Array to store the callbacks for each owner transfer method.
75 static OTMCallbackData_t g_OTMDatas[OIC_OXM_COUNT];
78 * Variables for pointing the OTMContext to be used in the DTLS handshake result callback.
80 static OTMContext_t* g_otmCtx = NULL;
83 * Function to select appropriate provisioning method.
85 * @param[in] supportedMethods Array of supported methods
86 * @param[in] numberOfMethods number of supported methods
87 * @param[out] selectedMethod Selected methods
88 * @return OC_STACK_OK on success
90 static OCStackResult SelectProvisioningMethod(const OicSecOxm_t *supportedMethods,
91 size_t numberOfMethods, OicSecOxm_t *selectedMethod)
93 OIC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
95 if(numberOfMethods == 0 || !supportedMethods)
97 OIC_LOG(WARNING, TAG, "Could not find a supported OxM.");
98 return OC_STACK_ERROR;
101 *selectedMethod = supportedMethods[0];
102 for(size_t i = 0; i < numberOfMethods; i++)
104 if(*selectedMethod < supportedMethods[i])
106 *selectedMethod = supportedMethods[i];
114 * Function to select operation mode.This function will return most secure common operation mode.
116 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
117 * @param[out] selectedMode selected operation mode
118 * @return OC_STACK_OK on success
120 static void SelectOperationMode(const OCProvisionDev_t *selectedDeviceInfo,
121 OicSecDpom_t *selectedMode)
123 OIC_LOG(DEBUG, TAG, "IN SelectOperationMode");
124 *selectedMode = selectedDeviceInfo->pstat->sm[0];
125 OIC_LOG_V(DEBUG, TAG, "Selected Operation Mode = %d", *selectedMode);
129 * Function to start ownership transfer.
130 * This function will send the first request for provisioning,
131 * The next request message is sent from the response handler for this request.
133 * @param[in] ctx context value passed to callback from calling function.
134 * @param[in] selectedDevice selected device information to performing provisioning.
135 * @return OC_STACK_OK on success
137 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
140 * Function to update owner transfer mode
142 * @param[in] otmCtx Context value of ownership transfer.
143 * @return OC_STACK_OK on success
145 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx);
148 * Function to send request to resource to get its pstat resource information.
150 * @param[in] otmCtx Context value of ownership transfer.
151 * @return OC_STACK_OK on success
153 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx);
157 * Function to send uuid of owner device to new device.
158 * This function would update 'owner of doxm' as UUID for provisioning tool.
160 * @param[in] otmCtx Context value of ownership transfer.
161 * @return OC_STACK_OK on success
163 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx);
166 * Function to update the operation mode. As per the spec. Operation mode in client driven
167 * single service provisioning it will be updated to 0x3
169 * @param[in] otmCtx Context value of ownership transfer.
170 * @return OC_STACK_OK on success
172 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx);
175 * Function to update the owner credential to new device
177 * @param[in] otmCtx Context value of ownership transfer.
178 * @param[in] selectedOperationMode selected operation mode
179 * @return OC_STACK_OK on success
181 static OCStackResult PostOwnerCredential(OTMContext_t* otmCtx);
184 * Function to send ownerShip info.
185 * This function would update 'owned of doxm' as true.
187 * @param[in] otmCtx Context value of ownership transfer.
188 * @return OC_STACK_OK on success
190 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx);
193 * Function to update pstat as Ready for provisioning.
194 * This function would update 'cm' from bx0000,0010 to bx0000,0000.
196 * @param[in] ctx context value passed to callback from calling function.
197 * @param[in] selectedDevice selected device information to performing provisioning.
198 * @return OC_STACK_OK on success
200 static OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx);
203 * Function to update pstat as Ready for Normal Operation.
204 * This function would update 'isop' from false to true.
206 * @param[in] ctx context value passed to callback from calling function.
207 * @param[in] selectedDevice selected device information to performing provisioning.
208 * @return OC_STACK_OK on success
210 static OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx);
212 static bool IsComplete(OTMContext_t* otmCtx)
214 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
216 if(OC_STACK_CONTINUE == otmCtx->ctxResultArray[i].res)
226 * Function to save the result of provisioning.
228 * @param[in,out] otmCtx Context value of ownership transfer.
229 * @param[in] res result of provisioning
231 static void SetResult(OTMContext_t* otmCtx, const OCStackResult res)
233 OIC_LOG_V(DEBUG, TAG, "IN SetResult : %d ", res);
237 OIC_LOG(WARNING, TAG, "OTMContext is NULL");
241 if(otmCtx->selectedDeviceInfo)
243 //Revert psk_info callback and new deivce uuid in case of random PIN OxM
244 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
247 if(CA_STATUS_OK != CAregisterTlsCredentialsHandler(GetDtlsPskCredentials))
249 OIC_LOG(WARNING, TAG, "Failed to revert is TLS credential handler.");
252 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
254 OIC_LOG(WARNING, TAG, "Failed to revert is DTLS credential handler.");
256 OicUuid_t emptyUuid = { .id={0}};
257 SetUuidForRandomPinOxm(&emptyUuid);
260 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
262 if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
263 otmCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
265 otmCtx->ctxResultArray[i].res = res;
266 if(OC_STACK_OK != res)
268 otmCtx->ctxHasError = true;
275 //If all request is completed, invoke the user callback.
276 if(IsComplete(otmCtx))
278 otmCtx->ctxResultCallback(otmCtx->userCtx, otmCtx->ctxResultArraySize,
279 otmCtx->ctxResultArray, otmCtx->ctxHasError);
280 OICFree(otmCtx->ctxResultArray);
285 if(OC_STACK_OK != StartOwnershipTransfer(otmCtx,
286 otmCtx->selectedDeviceInfo->next))
288 OIC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
293 OIC_LOG(DEBUG, TAG, "OUT SetResult");
297 * Function to handle the handshake result in OTM.
298 * This function will be invoked after DTLS handshake
299 * @param endPoint [IN] The remote endpoint.
300 * @param errorInfo [IN] Error information from the endpoint.
303 void DTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
305 if(NULL != g_otmCtx && NULL != g_otmCtx->selectedDeviceInfo &&
306 NULL != endpoint && NULL != info)
308 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
309 endpoint->addr, endpoint->port, info->result);
311 OicSecDoxm_t* newDevDoxm = g_otmCtx->selectedDeviceInfo->doxm;
313 if(NULL != newDevDoxm)
315 OicUuid_t emptyUuid = {.id={0}};
317 //Make sure the address matches.
318 if(strncmp(g_otmCtx->selectedDeviceInfo->endpoint.addr,
320 sizeof(endpoint->addr)) == 0 &&
321 g_otmCtx->selectedDeviceInfo->securePort == endpoint->port)
323 OCStackResult res = OC_STACK_ERROR;
325 //If temporal secure sesstion established successfully
326 if(CA_STATUS_OK == info->result &&
327 false == newDevDoxm->owned &&
328 memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) == 0)
330 //Send request : POST /oic/sec/doxm [{... , "devowner":"PT's UUID"}]
331 res = PostOwnerUuid(g_otmCtx);
332 if(OC_STACK_OK != res)
334 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
335 SetResult(g_otmCtx, res);
338 //In case of authentication failure
339 else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
341 //in case of error from owner credential
342 if(memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) != 0 &&
343 true == newDevDoxm->owned)
345 OIC_LOG(ERROR, TAG, "The owner credential may incorrect.");
347 if(OC_STACK_OK != RemoveCredential(&(newDevDoxm->deviceID)))
349 OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
351 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
353 //in case of error from wrong PIN, re-start the ownership transfer
354 else if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
356 OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
358 memcpy(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
359 newDevDoxm->owned = false;
360 g_otmCtx->attemptCnt++;
362 if(WRONG_PIN_MAX_ATTEMP > g_otmCtx->attemptCnt)
364 res = StartOwnershipTransfer(g_otmCtx, g_otmCtx->selectedDeviceInfo);
365 if(OC_STACK_OK != res)
367 SetResult(g_otmCtx, res);
368 OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
373 OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
374 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
379 OIC_LOG(ERROR, TAG, "Failed to establish secure session.");
380 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
389 * Function to save ownerPSK at provisioning tool end.
391 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
392 * @return OC_STACK_OK on success
394 static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
396 OIC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
398 OCStackResult res = OC_STACK_ERROR;
400 CAEndpoint_t endpoint;
401 memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
402 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
403 endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
404 endpoint.port = selectedDeviceInfo->securePort;
405 endpoint.adapter = selectedDeviceInfo->endpoint.adapter;
407 OicUuid_t ptDeviceID = {.id={0}};
408 if (OC_STACK_OK != GetDoxmDeviceID(&ptDeviceID))
410 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
414 uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
415 OicSecKey_t ownerKey = {ownerPSK, OWNER_PSK_LENGTH_128};
417 //Generating OwnerPSK
418 CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
419 (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
420 strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
421 ptDeviceID.id, sizeof(ptDeviceID.id),
422 selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
423 ownerPSK, OWNER_PSK_LENGTH_128);
425 if (CA_STATUS_OK == pskRet)
427 OIC_LOG(INFO, TAG,"ownerPSK dump:\n");
428 OIC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
429 //Generating new credential for provisioning tool
430 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
431 SYMMETRIC_PAIR_WISE_KEY, NULL,
432 &ownerKey, &ptDeviceID);
433 VERIFY_NON_NULL(TAG, cred, ERROR);
435 // TODO: Added as workaround. Will be replaced soon.
436 cred->privateData.encoding = OIC_ENCODING_RAW;
439 // NOTE: Test codes to use BASE64 encoded owner PSK.
440 uint32_t outSize = 0;
441 size_t b64BufSize = B64ENCODE_OUT_SAFESIZE((OWNER_PSK_LENGTH_128 + 1));
442 char* b64Buf = (uint8_t *)OICCalloc(1, b64BufSize);
443 VERIFY_NON_NULL(TAG, b64Buf, ERROR);
444 b64Encode(cred->privateData.data, cred->privateData.len, b64Buf, b64BufSize, &outSize);
446 OICFree( cred->privateData.data );
447 cred->privateData.data = (uint8_t *)OICCalloc(1, outSize + 1);
448 VERIFY_NON_NULL(TAG, cred->privateData.data, ERROR);
450 strncpy(cred->privateData.data, b64Buf, outSize);
451 cred->privateData.data[outSize] = '\0';
452 cred->privateData.encoding = OIC_ENCODING_BASE64;
453 cred->privateData.len = outSize;
455 #endif //End of Test codes
457 res = AddCredential(cred);
458 if(res != OC_STACK_OK)
460 DeleteCredList(cred);
466 OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
469 OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
475 * Callback handler for OwnerShipTransferModeHandler API.
477 * @param[in] ctx ctx value passed to callback from calling function.
478 * @param[in] UNUSED handle to an invocation
479 * @param[in] clientResponse Response from queries to remote servers.
480 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
481 * and OC_STACK_KEEP_TRANSACTION to keep it.
483 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
484 OCClientResponse *clientResponse)
486 OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
488 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
489 VERIFY_NON_NULL(TAG, ctx, WARNING);
491 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
493 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
495 OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
496 //Send request : GET /oic/sec/pstat
497 OCStackResult res = GetProvisioningStatusResource(otmCtx);
498 if(OC_STACK_OK != res)
500 OIC_LOG(WARNING, TAG, "Failed to get pstat information");
501 SetResult(otmCtx, res);
506 OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
507 clientResponse->result);
508 SetResult(otmCtx, clientResponse->result);
511 OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
514 return OC_STACK_DELETE_TRANSACTION;
518 * Callback handler for ProvisioningStatusResouceHandler API.
520 * @param[in] ctx ctx value passed to callback from calling function.
521 * @param[in] UNUSED handle to an invocation
522 * @param[in] clientResponse Response from queries to remote servers.
523 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
524 * and OC_STACK_KEEP_TRANSACTION to keep it.
526 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
527 OCClientResponse *clientResponse)
529 OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
531 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
532 VERIFY_NON_NULL(TAG, ctx, WARNING);
534 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
536 if (OC_STACK_OK == clientResponse->result)
538 if (NULL == clientResponse->payload)
540 OIC_LOG(INFO, TAG, "Skiping Null payload");
541 SetResult(otmCtx, OC_STACK_ERROR);
542 return OC_STACK_DELETE_TRANSACTION;
545 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
547 OIC_LOG(INFO, TAG, "Unknown payload type");
548 SetResult(otmCtx, OC_STACK_ERROR);
549 return OC_STACK_DELETE_TRANSACTION;
551 OicSecPstat_t* pstat = NULL;
552 OCStackResult result = CBORPayloadToPstat(
553 ((OCSecurityPayload*)clientResponse->payload)->securityData,
554 ((OCSecurityPayload*)clientResponse->payload)->payloadSize,
556 if(NULL == pstat || result != OC_STACK_OK)
558 OIC_LOG(ERROR, TAG, "Error while converting cbor to pstat.");
559 SetResult(otmCtx, OC_STACK_ERROR);
560 return OC_STACK_DELETE_TRANSACTION;
562 if(false == (TAKE_OWNER & pstat->cm))
564 OIC_LOG(ERROR, TAG, "Device pairing mode enabling owner transfer operations is disabled");
565 SetResult(otmCtx, OC_STACK_ERROR);
566 return OC_STACK_DELETE_TRANSACTION;
568 otmCtx->selectedDeviceInfo->pstat = pstat;
570 //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
571 SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
573 //Send request : POST /oic/sec/pstat [{"om":"bx11", .. }]
574 OCStackResult res = PostUpdateOperationMode(otmCtx);
575 if (OC_STACK_OK != res)
577 OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
578 SetResult(otmCtx, res);
583 OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
584 clientResponse->result);
585 SetResult(otmCtx, clientResponse->result);
588 OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
590 return OC_STACK_DELETE_TRANSACTION;
594 * Response handler for update owner uuid request.
596 * @param[in] ctx ctx value passed to callback from calling function.
597 * @param[in] UNUSED handle to an invocation
598 * @param[in] clientResponse Response from queries to remote servers.
599 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
600 * and OC_STACK_KEEP_TRANSACTION to keep it.
602 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
603 OCClientResponse *clientResponse)
605 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
606 VERIFY_NON_NULL(TAG, ctx, WARNING);
608 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
610 OCStackResult res = OC_STACK_OK;
611 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
613 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
615 if(otmCtx && otmCtx->selectedDeviceInfo)
617 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
618 if(OC_STACK_OK != res)
620 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
621 SetResult(otmCtx, res);
622 return OC_STACK_DELETE_TRANSACTION;
625 //POST owner credential to new device according to security spec B.
626 res = PostOwnerCredential(otmCtx);
627 if(OC_STACK_OK != res)
630 "OwnerUuidUpdateHandler:Failed to send PosT request for onwer credential");
631 SetResult(otmCtx, res);
632 return OC_STACK_DELETE_TRANSACTION;
638 res = clientResponse->result;
639 OIC_LOG_V(ERROR, TAG, "OwnerUuidHandler : Unexpected result %d", res);
640 SetResult(otmCtx, res);
643 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
646 return OC_STACK_DELETE_TRANSACTION;
650 * Response handler for update operation mode.
652 * @param[in] ctx ctx value passed to callback from calling function.
653 * @param[in] UNUSED handle to an invocation
654 * @param[in] clientResponse Response from queries to remote servers.
655 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
656 * and OC_STACK_KEEP_TRANSACTION to keep it.
658 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
659 OCClientResponse *clientResponse)
661 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
663 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
664 VERIFY_NON_NULL(TAG, ctx, WARNING);
666 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
668 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
670 OCStackResult res = OC_STACK_ERROR;
671 OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
673 //Load secret for temporal secure session.
674 if(g_OTMDatas[selOxm].loadSecretCB)
676 res = g_OTMDatas[selOxm].loadSecretCB(otmCtx);
677 if(OC_STACK_OK != res)
679 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
680 SetResult(otmCtx, res);
681 return OC_STACK_DELETE_TRANSACTION;
685 //It will be used in handshake event handler
688 //Try DTLS handshake to generate secure session
689 if(g_OTMDatas[selOxm].createSecureSessionCB)
691 res = g_OTMDatas[selOxm].createSecureSessionCB(otmCtx);
692 if(OC_STACK_OK != res)
694 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
695 SetResult(otmCtx, res);
696 return OC_STACK_DELETE_TRANSACTION;
702 OIC_LOG(ERROR, TAG, "Error while update operation mode");
703 SetResult(otmCtx, clientResponse->result);
706 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
709 return OC_STACK_DELETE_TRANSACTION;
713 * Response handler for update owner crendetial request.
715 * @param[in] ctx ctx value passed to callback from calling function.
716 * @param[in] UNUSED handle to an invocation
717 * @param[in] clientResponse Response from queries to remote servers.
718 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
719 * and OC_STACK_KEEP_TRANSACTION to keep it.
721 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
722 OCClientResponse *clientResponse)
724 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
725 VERIFY_NON_NULL(TAG, ctx, WARNING);
727 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
729 OCStackResult res = OC_STACK_OK;
730 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
732 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
734 if(otmCtx && otmCtx->selectedDeviceInfo)
736 //Close the temporal secure session to verify the owner credential
737 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
738 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
739 CAResult_t caResult = CA_STATUS_OK;
740 if(CA_ADAPTER_IP == endpoint->adapter)
742 caResult = CACloseDtlsSession(endpoint);
747 caResult = CAcloseTlsConnection(endpoint);
750 if(CA_STATUS_OK != caResult)
752 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
753 SetResult(otmCtx, caResult);
754 return OC_STACK_DELETE_TRANSACTION;
758 * If we select NULL cipher,
759 * client will select appropriate cipher suite according to server's cipher-suite list.
761 if(CA_ADAPTER_IP == endpoint->adapter)
763 caResult = CASelectCipherSuite(TLS_NULL_WITH_NULL_NULL, endpoint->adapter);
767 // TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 = 0xC037, /**< see RFC 5489 */
768 caResult = CASelectCipherSuite(0xC037, endpoint->adapter);
771 if(CA_STATUS_OK != caResult)
773 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
774 SetResult(otmCtx, caResult);
775 return OC_STACK_DELETE_TRANSACTION;
779 * in case of random PIN based OxM,
780 * revert get_psk_info callback of tinyDTLS to use owner credential.
782 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
784 OicUuid_t emptyUuid = { .id={0}};
785 SetUuidForRandomPinOxm(&emptyUuid);
787 if(CA_ADAPTER_IP == endpoint->adapter)
789 caResult = CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials);
794 caResult = CAregisterTlsCredentialsHandler(GetDtlsPskCredentials);
798 if(CA_STATUS_OK != caResult)
800 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
801 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
802 return OC_STACK_DELETE_TRANSACTION;
806 otmCtx->selectedDeviceInfo->connType |= CT_FLAG_SECURE;
808 //POST /oic/sec/doxm [{ ..., "owned":"TRUE" }]
809 res = PostOwnershipInformation(otmCtx);
810 if(OC_STACK_OK != res)
812 OIC_LOG(ERROR, TAG, "Failed to post ownership information to new device");
813 SetResult(otmCtx, res);
814 return OC_STACK_DELETE_TRANSACTION;
820 res = clientResponse->result;
821 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
822 SetResult(otmCtx, res);
825 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
828 return OC_STACK_DELETE_TRANSACTION;
833 * Response handler for update owner information request.
835 * @param[in] ctx ctx value passed to callback from calling function.
836 * @param[in] UNUSED handle to an invocation
837 * @param[in] clientResponse Response from queries to remote servers.
838 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
839 * and OC_STACK_KEEP_TRANSACTION to keep it.
841 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
842 OCClientResponse *clientResponse)
844 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
845 VERIFY_NON_NULL(TAG, ctx, WARNING);
847 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
849 OCStackResult res = OC_STACK_OK;
850 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
852 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
854 if(otmCtx && otmCtx->selectedDeviceInfo)
856 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
857 OIC_LOG(INFO, TAG, "Set Ready for provisioning state .");
859 res = PostProvisioningStatus(otmCtx);
860 if(OC_STACK_OK != res)
862 OIC_LOG(ERROR, TAG, "Failed to update pstat");
863 SetResult(otmCtx, res);
869 res = clientResponse->result;
870 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
871 SetResult(otmCtx, res);
874 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
877 return OC_STACK_DELETE_TRANSACTION;
881 * Response handler of update provisioning status.
883 * @param[in] ctx ctx value passed to callback from calling function.
884 * @param[in] UNUSED handle to an invocation
885 * @param[in] clientResponse Response from queries to remote servers.
886 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
887 * and OC_STACK_KEEP_TRANSACTION to keep it.
889 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
890 OCClientResponse *clientResponse)
892 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
894 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
895 VERIFY_NON_NULL(TAG, ctx, ERROR);
897 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
899 OCStackResult res = OC_STACK_OK;
901 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
903 if(otmCtx && otmCtx->selectedDeviceInfo)
905 OIC_LOG(INFO, TAG, "Device state is in Ready for Provisionig.");
907 res = PostNormalOperationStatus(otmCtx);
908 if(OC_STACK_OK != res)
910 OIC_LOG(ERROR, TAG, "Failed to update pstat");
911 SetResult(otmCtx, res);
917 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
918 clientResponse->result);
919 SetResult(otmCtx, clientResponse->result);
923 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
924 return OC_STACK_DELETE_TRANSACTION;
928 * Response handler of update provisioning status to Ready for Normal..
930 * @param[in] ctx ctx value passed to callback from calling function.
931 * @param[in] UNUSED handle to an invocation
932 * @param[in] clientResponse Response from queries to remote servers.
933 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
934 * and OC_STACK_KEEP_TRANSACTION to keep it.
936 static OCStackApplicationResult ReadyForNomalStatusHandler(void *ctx, OCDoHandle UNUSED,
937 OCClientResponse *clientResponse)
939 OIC_LOG_V(INFO, TAG, "IN ReadyForNomalStatusHandler.");
941 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
942 VERIFY_NON_NULL(TAG, ctx, ERROR);
944 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
947 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
949 OIC_LOG(INFO, TAG, "Device state is in Ready for Normal Operation.");
950 OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
951 if (OC_STACK_OK == res)
953 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
954 SetResult(otmCtx, OC_STACK_OK);
955 return OC_STACK_DELETE_TRANSACTION;
959 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
964 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
965 clientResponse->result);
966 SetResult(otmCtx, clientResponse->result);
970 OIC_LOG_V(INFO, TAG, "OUT ReadyForNomalStatusHandler.");
971 return OC_STACK_DELETE_TRANSACTION;
974 static OCStackResult PostOwnerCredential(OTMContext_t* otmCtx)
976 OIC_LOG(DEBUG, TAG, "IN PostOwnerCredential");
978 if(!otmCtx || !otmCtx->selectedDeviceInfo)
980 OIC_LOG(ERROR, TAG, "Invalid parameters");
981 return OC_STACK_INVALID_PARAM;
984 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
985 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
987 if(!PMGenerateQuery(true,
988 deviceInfo->endpoint.addr, deviceInfo->securePort,
989 deviceInfo->connType,
990 query, sizeof(query), OIC_RSRC_CRED_URI))
992 OIC_LOG(ERROR, TAG, "PostOwnerCredential : Failed to generate query");
993 return OC_STACK_ERROR;
995 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
996 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
999 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1000 return OC_STACK_NO_MEMORY;
1003 //Generate owner credential for new device
1004 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1005 const OicSecCred_t* ownerCredential = GetCredResourceData(&(deviceInfo->doxm->deviceID));
1006 if(!ownerCredential)
1008 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
1009 return OC_STACK_NO_RESOURCE;
1012 OicUuid_t credSubjectId = {.id={0}};
1013 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
1015 OicSecCred_t newCredential;
1016 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
1017 newCredential.next = NULL;
1019 //Set subject ID as PT's ID
1020 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
1022 //Fill private data as empty string
1023 newCredential.privateData.data = "";
1024 newCredential.privateData.len = 0;
1025 newCredential.privateData.encoding = ownerCredential->privateData.encoding;
1026 #ifdef __WITH_X509__
1027 newCredential.publicData.data = NULL;
1028 newCredential.publicData.len = 0;
1031 //Send owner credential to new device : POST /oic/sec/cred [ owner credential ]
1032 if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData,
1033 &secPayload->payloadSize, secureFlag))
1035 OICFree(secPayload);
1036 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
1037 return OC_STACK_ERROR;
1039 OIC_LOG(DEBUG, TAG, "Cred Payload:");
1040 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1042 OCCallbackData cbData;
1043 cbData.cb = &OwnerCredentialHandler;
1044 cbData.context = (void *)otmCtx;
1046 OCStackResult res = OCDoResource(NULL, OC_REST_POST, query,
1047 &deviceInfo->endpoint, (OCPayload*)secPayload,
1048 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1049 if (res != OC_STACK_OK)
1051 OIC_LOG(ERROR, TAG, "OCStack resource error");
1056 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
1057 return OC_STACK_NO_RESOURCE;
1060 OIC_LOG(DEBUG, TAG, "OUT PostOwnerCredential");
1065 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx)
1067 OIC_LOG(DEBUG, TAG, "IN PostOwnerTransferModeToResource");
1069 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1071 OIC_LOG(ERROR, TAG, "Invalid parameters");
1072 return OC_STACK_INVALID_PARAM;
1075 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1076 OicSecOxm_t selectedOxm = deviceInfo->doxm->oxmSel;
1077 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1079 if(!PMGenerateQuery(false,
1080 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1081 deviceInfo->connType,
1082 query, sizeof(query), OIC_RSRC_DOXM_URI))
1084 OIC_LOG(ERROR, TAG, "PostOwnerTransferModeToResource : Failed to generate query");
1085 return OC_STACK_ERROR;
1087 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1088 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1091 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1092 return OC_STACK_NO_MEMORY;
1094 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1095 OCStackResult res = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx,
1096 &secPayload->securityData, &secPayload->payloadSize);
1097 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1099 OCPayloadDestroy((OCPayload *)secPayload);
1100 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor");
1101 return OC_STACK_ERROR;
1104 OCCallbackData cbData;
1105 cbData.cb = &OwnerTransferModeHandler;
1106 cbData.context = (void *)otmCtx;
1108 res = OCDoResource(NULL, OC_REST_POST, query,
1109 &deviceInfo->endpoint, (OCPayload *)secPayload,
1110 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1111 if (res != OC_STACK_OK)
1113 OIC_LOG(ERROR, TAG, "OCStack resource error");
1116 OIC_LOG(DEBUG, TAG, "OUT PostOwnerTransferModeToResource");
1121 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1123 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1125 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1127 OIC_LOG(ERROR, TAG, "Invailed parameters");
1128 return OC_STACK_INVALID_PARAM;
1131 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1132 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1133 if(!PMGenerateQuery(false,
1134 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1135 deviceInfo->connType,
1136 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1138 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1139 return OC_STACK_ERROR;
1141 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1143 OCCallbackData cbData;
1144 cbData.cb = &ListMethodsHandler;
1145 cbData.context = (void *)otmCtx;
1147 OCStackResult res = OCDoResource(NULL, OC_REST_GET, query, NULL, NULL,
1148 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1149 if (res != OC_STACK_OK)
1151 OIC_LOG(ERROR, TAG, "OCStack resource error");
1154 OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1159 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx)
1161 OIC_LOG(DEBUG, TAG, "IN PostOwnerUuid");
1163 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1165 OIC_LOG(ERROR, TAG, "Invailed parameters");
1166 return OC_STACK_INVALID_PARAM;
1169 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1170 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1171 if(!PMGenerateQuery(true,
1172 deviceInfo->endpoint.addr, deviceInfo->securePort,
1173 deviceInfo->connType,
1174 query, sizeof(query), OIC_RSRC_DOXM_URI))
1176 OIC_LOG(ERROR, TAG, "PostOwnerUuid : Failed to generate query");
1177 return OC_STACK_ERROR;
1179 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1181 //Post PT's uuid to new device
1182 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1185 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1186 return OC_STACK_NO_MEMORY;
1188 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1189 OCStackResult res = g_OTMDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(
1190 otmCtx, &secPayload->securityData, &secPayload->payloadSize);
1191 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1193 OCPayloadDestroy((OCPayload *)secPayload);
1194 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to cbor.");
1195 return OC_STACK_INVALID_PARAM;
1197 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1199 OCCallbackData cbData;
1200 cbData.cb = &OwnerUuidUpdateHandler;
1201 cbData.context = (void *)otmCtx;
1204 res = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1205 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1206 if (res != OC_STACK_OK)
1208 OIC_LOG(ERROR, TAG, "OCStack resource error");
1211 OIC_LOG(DEBUG, TAG, "OUT PostOwnerUuid");
1216 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx)
1218 OIC_LOG(DEBUG, TAG, "IN PostOwnershipInformation");
1220 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1222 OIC_LOG(ERROR, TAG, "Invailed parameters");
1223 return OC_STACK_INVALID_PARAM;
1226 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1227 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1228 if(!PMGenerateQuery(true,
1229 deviceInfo->endpoint.addr, deviceInfo->securePort,
1230 deviceInfo->connType,
1231 query, sizeof(query), OIC_RSRC_DOXM_URI))
1233 OIC_LOG(ERROR, TAG, "PostOwnershipInformation : Failed to generate query");
1234 return OC_STACK_ERROR;
1236 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1238 //OwnershipInformationHandler
1239 OCSecurityPayload *secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1242 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1243 return OC_STACK_NO_MEMORY;
1246 otmCtx->selectedDeviceInfo->doxm->owned = true;
1248 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1249 OCStackResult res = DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm,
1250 &secPayload->securityData, &secPayload->payloadSize, true);
1251 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1253 OCPayloadDestroy((OCPayload *)secPayload);
1254 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1255 return OC_STACK_INVALID_PARAM;
1258 OCCallbackData cbData;
1259 cbData.cb = &OwnershipInformationHandler;
1260 cbData.context = (void *)otmCtx;
1263 res = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1264 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1265 if (res != OC_STACK_OK)
1267 OIC_LOG(ERROR, TAG, "OCStack resource error");
1270 OIC_LOG(DEBUG, TAG, "OUT PostOwnershipInformation");
1275 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx)
1277 OIC_LOG(DEBUG, TAG, "IN PostUpdateOperationMode");
1279 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1281 return OC_STACK_INVALID_PARAM;
1284 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1285 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1286 if(!PMGenerateQuery(false,
1287 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1288 deviceInfo->connType,
1289 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1291 OIC_LOG(ERROR, TAG, "PostUpdateOperationMode : Failed to generate query");
1292 return OC_STACK_ERROR;
1294 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1296 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1299 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1300 return OC_STACK_NO_MEMORY;
1302 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1303 OCStackResult res = PstatToCBORPayload(deviceInfo->pstat, &secPayload->securityData,
1304 &secPayload->payloadSize, true);
1305 if (OC_STACK_OK != res)
1307 OCPayloadDestroy((OCPayload *)secPayload);
1308 OIC_LOG(ERROR, TAG, "Error while converting pstat to cbor.");
1309 return OC_STACK_INVALID_PARAM;
1312 OCCallbackData cbData;
1313 cbData.cb = &OperationModeUpdateHandler;
1314 cbData.context = (void *)otmCtx;
1316 res = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1317 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1318 if (res != OC_STACK_OK)
1320 OIC_LOG(ERROR, TAG, "OCStack resource error");
1323 OIC_LOG(DEBUG, TAG, "OUT PostUpdateOperationMode");
1328 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
1330 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
1331 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1332 otmCtx->selectedDeviceInfo = selectedDevice;
1334 //Set to the lowest level OxM, and then find more higher level OxM.
1335 OCStackResult res = SelectProvisioningMethod(selectedDevice->doxm->oxm,
1336 selectedDevice->doxm->oxmLen,
1337 &selectedDevice->doxm->oxmSel);
1338 if(OC_STACK_OK != res)
1340 OIC_LOG(ERROR, TAG, "Failed to select the provisioning method");
1341 SetResult(otmCtx, res);
1344 OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
1346 //Send Req: POST /oic/sec/doxm [{..."OxmSel" :g_OTMDatas[Index of Selected OxM].OXMString,...}]
1347 res = PostOwnerTransferModeToResource(otmCtx);
1348 if(OC_STACK_OK != res)
1350 OIC_LOG(WARNING, TAG, "Failed to select the provisioning method");
1351 SetResult(otmCtx, res);
1355 //Register DTLS event handler to catch the dtls event while handshake
1356 if(CA_STATUS_OK != CARegisterDTLSHandshakeCallback(DTLSHandshakeCB))
1358 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register DTLS handshake callback.");
1361 //Register TLS event handler to catch the tls event while handshake
1362 if(CA_STATUS_OK != CAregisterTlsHandshakeCallback(DTLSHandshakeCB))
1364 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register TLS handshake callback.");
1367 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
1373 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
1375 OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
1379 OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
1380 return OC_STACK_INVALID_PARAM;
1382 if(oxmType >= OIC_OXM_COUNT)
1384 OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
1385 return OC_STACK_INVALID_PARAM;
1388 g_OTMDatas[oxmType].loadSecretCB= data->loadSecretCB;
1389 g_OTMDatas[oxmType].createSecureSessionCB = data->createSecureSessionCB;
1390 g_OTMDatas[oxmType].createSelectOxmPayloadCB = data->createSelectOxmPayloadCB;
1391 g_OTMDatas[oxmType].createOwnerTransferPayloadCB = data->createOwnerTransferPayloadCB;
1393 OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
1399 * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
1401 OCStackResult OTMDoOwnershipTransfer(void* ctx,
1402 OCProvisionDev_t *selectedDevicelist,
1403 OCProvisionResultCB resultCallback)
1405 OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
1407 if (NULL == selectedDevicelist)
1409 return OC_STACK_INVALID_PARAM;
1411 if (NULL == resultCallback)
1413 return OC_STACK_INVALID_CALLBACK;
1416 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
1419 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
1420 return OC_STACK_NO_MEMORY;
1422 otmCtx->ctxResultCallback = resultCallback;
1423 otmCtx->ctxHasError = false;
1424 otmCtx->userCtx = ctx;
1425 OCProvisionDev_t* pCurDev = selectedDevicelist;
1427 //Counting number of selected devices.
1428 otmCtx->ctxResultArraySize = 0;
1429 while(NULL != pCurDev)
1431 otmCtx->ctxResultArraySize++;
1432 pCurDev = pCurDev->next;
1435 otmCtx->ctxResultArray =
1436 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
1437 if(NULL == otmCtx->ctxResultArray)
1439 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
1441 return OC_STACK_NO_MEMORY;
1443 pCurDev = selectedDevicelist;
1445 OCStackResult res = OC_STACK_OK;
1446 //Fill the device UUID for result array.
1447 for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
1449 //Checking duplication of Device ID.
1450 bool isDuplicate = true;
1451 res = PDMIsDuplicateDevice(&pCurDev->doxm->deviceID, &isDuplicate);
1452 if (OC_STACK_OK != res)
1458 bool isStale = false;
1459 res = PDMIsDeviceStale(&pCurDev->doxm->deviceID, &isStale);
1460 if(OC_STACK_OK != res)
1462 OIC_LOG(ERROR, TAG, "Internal error in PDMIsDeviceStale");
1467 OIC_LOG(INFO, TAG, "Detected duplicated UUID in stale status, "\
1468 "this UUID will be removed from PDM");
1470 res = PDMDeleteDevice(&pCurDev->doxm->deviceID);
1471 if(OC_STACK_OK != res)
1473 OIC_LOG(ERROR, TAG, "Internal error in PDMDeleteDevice");
1479 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Device UUID is duplicated");
1480 res = OC_STACK_INVALID_PARAM;
1484 memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
1485 pCurDev->doxm->deviceID.id,
1487 otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
1488 pCurDev = pCurDev->next;
1491 StartOwnershipTransfer(otmCtx, selectedDevicelist);
1493 OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
1497 OICFree(otmCtx->ctxResultArray);
1502 OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx)
1504 OIC_LOG(INFO, TAG, "IN PostProvisioningStatus");
1506 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1508 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1509 return OC_STACK_INVALID_PARAM;
1512 //Change the TAKE_OWNER bit of CM to 0.
1513 otmCtx->selectedDeviceInfo->pstat->cm &= (~TAKE_OWNER);
1515 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1518 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1519 return OC_STACK_NO_MEMORY;
1521 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1522 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1523 &secPayload->securityData, &secPayload->payloadSize, true))
1525 OCPayloadDestroy((OCPayload *)secPayload);
1526 return OC_STACK_INVALID_JSON;
1528 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
1529 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1531 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1532 if(!PMGenerateQuery(true,
1533 otmCtx->selectedDeviceInfo->endpoint.addr,
1534 otmCtx->selectedDeviceInfo->securePort,
1535 otmCtx->selectedDeviceInfo->connType,
1536 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1538 OIC_LOG(ERROR, TAG, "PostProvisioningStatus : Failed to generate query");
1539 return OC_STACK_ERROR;
1541 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1543 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1544 cbData.cb = &ProvisioningStatusHandler;
1545 cbData.context = (void*)otmCtx;
1547 OCStackResult ret = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1548 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1549 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1550 if (ret != OC_STACK_OK)
1552 OIC_LOG(ERROR, TAG, "OCStack resource error");
1555 OIC_LOG(INFO, TAG, "OUT PostProvisioningStatus");
1560 OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx)
1562 OIC_LOG(INFO, TAG, "IN PostNormalOperationStatus");
1564 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1566 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1567 return OC_STACK_INVALID_PARAM;
1571 otmCtx->selectedDeviceInfo->pstat->isOp = true;
1573 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1576 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1577 return OC_STACK_NO_MEMORY;
1579 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1580 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1581 &secPayload->securityData, &secPayload->payloadSize, true))
1583 OCPayloadDestroy((OCPayload *)secPayload);
1584 return OC_STACK_INVALID_JSON;
1586 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
1587 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1589 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1590 if(!PMGenerateQuery(true,
1591 otmCtx->selectedDeviceInfo->endpoint.addr,
1592 otmCtx->selectedDeviceInfo->securePort,
1593 otmCtx->selectedDeviceInfo->connType,
1594 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1596 OIC_LOG(ERROR, TAG, "PostNormalOperationStatus : Failed to generate query");
1597 return OC_STACK_ERROR;
1599 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1601 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1602 cbData.cb = &ReadyForNomalStatusHandler;
1603 cbData.context = (void*)otmCtx;
1605 OCStackResult ret = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1606 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1607 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1608 if (ret != OC_STACK_OK)
1610 OIC_LOG(ERROR, TAG, "OCStack resource error");
1613 OIC_LOG(INFO, TAG, "OUT PostNormalOperationStatus");