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 PutOwnerTransferModeToResource(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 PutOwnerUuid(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 PutUpdateOperationMode(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 PutOwnerCredential(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 PutOwnershipInformation(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 PutProvisioningStatus(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 PutNormalOperationStatus(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)
246 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
248 OIC_LOG(WARNING, TAG, "Failed to revert is DTLS credential handler.");
250 OicUuid_t emptyUuid = { .id={0}};
251 SetUuidForRandomPinOxm(&emptyUuid);
254 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
256 if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
257 otmCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
259 otmCtx->ctxResultArray[i].res = res;
260 if(OC_STACK_OK != res)
262 otmCtx->ctxHasError = true;
269 //If all request is completed, invoke the user callback.
270 if(IsComplete(otmCtx))
272 otmCtx->ctxResultCallback(otmCtx->userCtx, otmCtx->ctxResultArraySize,
273 otmCtx->ctxResultArray, otmCtx->ctxHasError);
274 OICFree(otmCtx->ctxResultArray);
279 if(OC_STACK_OK != StartOwnershipTransfer(otmCtx,
280 otmCtx->selectedDeviceInfo->next))
282 OIC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
287 OIC_LOG(DEBUG, TAG, "OUT SetResult");
291 * Function to handle the handshake result in OTM.
292 * This function will be invoked after DTLS handshake
293 * @param endPoint [IN] The remote endpoint.
294 * @param errorInfo [IN] Error information from the endpoint.
297 void DTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
299 if(NULL != g_otmCtx && NULL != g_otmCtx->selectedDeviceInfo &&
300 NULL != endpoint && NULL != info)
302 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
303 endpoint->addr, endpoint->port, info->result);
305 OicSecDoxm_t* newDevDoxm = g_otmCtx->selectedDeviceInfo->doxm;
307 if(NULL != newDevDoxm)
309 OicUuid_t emptyUuid = {.id={0}};
311 //Make sure the address matches.
312 if(strncmp(g_otmCtx->selectedDeviceInfo->endpoint.addr,
314 sizeof(endpoint->addr)) == 0 &&
315 g_otmCtx->selectedDeviceInfo->securePort == endpoint->port)
317 OCStackResult res = OC_STACK_ERROR;
319 //If temporal secure sesstion established successfully
320 if(CA_STATUS_OK == info->result &&
321 false == newDevDoxm->owned &&
322 memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) == 0)
324 //Send request : PUT /oic/sec/doxm [{... , "devowner":"PT's UUID"}]
325 res = PutOwnerUuid(g_otmCtx);
326 if(OC_STACK_OK != res)
328 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
329 SetResult(g_otmCtx, res);
332 //In case of authentication failure
333 else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
335 //in case of error from owner credential
336 if(memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) != 0 &&
337 true == newDevDoxm->owned)
339 OIC_LOG(ERROR, TAG, "The owner credential may incorrect.");
341 if(OC_STACK_OK != RemoveCredential(&(newDevDoxm->deviceID)))
343 OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
345 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
347 //in case of error from wrong PIN, re-start the ownership transfer
348 else if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
350 OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
352 memcpy(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
353 newDevDoxm->owned = false;
354 g_otmCtx->attemptCnt++;
356 if(WRONG_PIN_MAX_ATTEMP > g_otmCtx->attemptCnt)
358 res = StartOwnershipTransfer(g_otmCtx, g_otmCtx->selectedDeviceInfo);
359 if(OC_STACK_OK != res)
361 SetResult(g_otmCtx, res);
362 OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
367 OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
368 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
373 OIC_LOG(ERROR, TAG, "Failed to establish secure session.");
374 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
383 * Function to save ownerPSK at provisioning tool end.
385 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
386 * @return OC_STACK_OK on success
388 static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
390 OIC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
392 OCStackResult res = OC_STACK_ERROR;
394 CAEndpoint_t endpoint;
395 memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
396 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
397 endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
398 endpoint.port = selectedDeviceInfo->securePort;
400 OicUuid_t ptDeviceID = {.id={0}};
401 if (OC_STACK_OK != GetDoxmDeviceID(&ptDeviceID))
403 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
407 uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
408 OicSecKey_t ownerKey = {ownerPSK, OWNER_PSK_LENGTH_128};
410 //Generating OwnerPSK
411 CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
412 (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
413 strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
414 ptDeviceID.id, sizeof(ptDeviceID.id),
415 selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
416 ownerPSK, OWNER_PSK_LENGTH_128);
418 if (CA_STATUS_OK == pskRet)
420 OIC_LOG(INFO, TAG,"ownerPSK dump:\n");
421 OIC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
422 //Generating new credential for provisioning tool
423 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
424 SYMMETRIC_PAIR_WISE_KEY, NULL,
425 &ownerKey, &ptDeviceID);
426 VERIFY_NON_NULL(TAG, cred, ERROR);
428 res = AddCredential(cred);
429 if(res != OC_STACK_OK)
431 DeleteCredList(cred);
437 OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
440 OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
446 * Callback handler for OwnerShipTransferModeHandler API.
448 * @param[in] ctx ctx value passed to callback from calling function.
449 * @param[in] UNUSED handle to an invocation
450 * @param[in] clientResponse Response from queries to remote servers.
451 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
452 * and OC_STACK_KEEP_TRANSACTION to keep it.
454 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
455 OCClientResponse *clientResponse)
457 OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
459 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
460 VERIFY_NON_NULL(TAG, ctx, WARNING);
462 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
464 if(clientResponse->result == OC_STACK_OK)
466 OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
467 //Send request : GET /oic/sec/pstat
468 OCStackResult res = GetProvisioningStatusResource(otmCtx);
469 if(OC_STACK_OK != res)
471 OIC_LOG(WARNING, TAG, "Failed to get pstat information");
472 SetResult(otmCtx, res);
477 OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
478 clientResponse->result);
479 SetResult(otmCtx, clientResponse->result);
482 OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
485 return OC_STACK_DELETE_TRANSACTION;
489 * Callback handler for ProvisioningStatusResouceHandler API.
491 * @param[in] ctx ctx value passed to callback from calling function.
492 * @param[in] UNUSED handle to an invocation
493 * @param[in] clientResponse Response from queries to remote servers.
494 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
495 * and OC_STACK_KEEP_TRANSACTION to keep it.
497 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
498 OCClientResponse *clientResponse)
500 OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
502 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
503 VERIFY_NON_NULL(TAG, ctx, WARNING);
505 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
507 if (OC_STACK_OK == clientResponse->result)
509 if (NULL == clientResponse->payload)
511 OIC_LOG(INFO, TAG, "Skiping Null payload");
512 SetResult(otmCtx, OC_STACK_ERROR);
513 return OC_STACK_DELETE_TRANSACTION;
516 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
518 OIC_LOG(INFO, TAG, "Unknown payload type");
519 SetResult(otmCtx, OC_STACK_ERROR);
520 return OC_STACK_DELETE_TRANSACTION;
522 OicSecPstat_t* pstat = NULL;
523 OCStackResult result = CBORPayloadToPstat(
524 ((OCSecurityPayload*)clientResponse->payload)->securityData,
525 ((OCSecurityPayload*)clientResponse->payload)->payloadSize,
527 if(NULL == pstat || result != OC_STACK_OK)
529 OIC_LOG(ERROR, TAG, "Error while converting cbor to pstat.");
530 SetResult(otmCtx, OC_STACK_ERROR);
531 return OC_STACK_DELETE_TRANSACTION;
533 if(false == (TAKE_OWNER & pstat->cm))
535 OIC_LOG(ERROR, TAG, "Device pairing mode enabling owner transfer operations is disabled");
536 SetResult(otmCtx, OC_STACK_ERROR);
537 return OC_STACK_DELETE_TRANSACTION;
539 otmCtx->selectedDeviceInfo->pstat = pstat;
541 //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
542 SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
544 //Send request : PUT /oic/sec/pstat [{"om":"bx11", .. }]
545 OCStackResult res = PutUpdateOperationMode(otmCtx);
546 if (OC_STACK_OK != res)
548 OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
549 SetResult(otmCtx, res);
554 OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
555 clientResponse->result);
556 SetResult(otmCtx, clientResponse->result);
559 OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
561 return OC_STACK_DELETE_TRANSACTION;
565 * Response handler for update owner uuid request.
567 * @param[in] ctx ctx value passed to callback from calling function.
568 * @param[in] UNUSED handle to an invocation
569 * @param[in] clientResponse Response from queries to remote servers.
570 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
571 * and OC_STACK_KEEP_TRANSACTION to keep it.
573 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
574 OCClientResponse *clientResponse)
576 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
577 VERIFY_NON_NULL(TAG, ctx, WARNING);
579 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
581 OCStackResult res = OC_STACK_OK;
582 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
584 if(OC_STACK_OK == clientResponse->result)
586 if(otmCtx && otmCtx->selectedDeviceInfo)
588 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
589 if(OC_STACK_OK != res)
591 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
592 SetResult(otmCtx, res);
593 return OC_STACK_DELETE_TRANSACTION;
596 //PUT owner credential to new device according to security spec B.
597 res = PutOwnerCredential(otmCtx);
598 if(OC_STACK_OK != res)
601 "OwnerUuidUpdateHandler:Failed to send PUT request for onwer credential");
602 SetResult(otmCtx, res);
603 return OC_STACK_DELETE_TRANSACTION;
609 res = clientResponse->result;
610 OIC_LOG_V(ERROR, TAG, "OwnerUuidHandler : Unexpected result %d", res);
611 SetResult(otmCtx, res);
614 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
617 return OC_STACK_DELETE_TRANSACTION;
621 * Response handler for update operation mode.
623 * @param[in] ctx ctx value passed to callback from calling function.
624 * @param[in] UNUSED handle to an invocation
625 * @param[in] clientResponse Response from queries to remote servers.
626 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
627 * and OC_STACK_KEEP_TRANSACTION to keep it.
629 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
630 OCClientResponse *clientResponse)
632 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
634 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
635 VERIFY_NON_NULL(TAG, ctx, WARNING);
637 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
639 if (OC_STACK_OK == clientResponse->result)
641 OCStackResult res = OC_STACK_ERROR;
642 OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
644 //Load secret for temporal secure session.
645 if(g_OTMDatas[selOxm].loadSecretCB)
647 res = g_OTMDatas[selOxm].loadSecretCB(otmCtx);
648 if(OC_STACK_OK != res)
650 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
651 SetResult(otmCtx, res);
652 return OC_STACK_DELETE_TRANSACTION;
656 //It will be used in handshake event handler
659 //Try DTLS handshake to generate secure session
660 if(g_OTMDatas[selOxm].createSecureSessionCB)
662 res = g_OTMDatas[selOxm].createSecureSessionCB(otmCtx);
663 if(OC_STACK_OK != res)
665 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
666 SetResult(otmCtx, res);
667 return OC_STACK_DELETE_TRANSACTION;
673 OIC_LOG(ERROR, TAG, "Error while update operation mode");
674 SetResult(otmCtx, clientResponse->result);
677 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
680 return OC_STACK_DELETE_TRANSACTION;
684 * Response handler for update owner crendetial request.
686 * @param[in] ctx ctx value passed to callback from calling function.
687 * @param[in] UNUSED handle to an invocation
688 * @param[in] clientResponse Response from queries to remote servers.
689 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
690 * and OC_STACK_KEEP_TRANSACTION to keep it.
692 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
693 OCClientResponse *clientResponse)
695 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
696 VERIFY_NON_NULL(TAG, ctx, WARNING);
698 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
700 OCStackResult res = OC_STACK_OK;
701 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
703 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
705 if(otmCtx && otmCtx->selectedDeviceInfo)
707 //Close the temporal secure session to verify the owner credential
708 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
709 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
710 CAResult_t caResult = CACloseDtlsSession(endpoint);
711 if(CA_STATUS_OK != caResult)
713 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
714 SetResult(otmCtx, caResult);
715 return OC_STACK_DELETE_TRANSACTION;
719 * If we select NULL cipher,
720 * client will select appropriate cipher suite according to server's cipher-suite list.
722 caResult = CASelectCipherSuite(TLS_NULL_WITH_NULL_NULL);
723 if(CA_STATUS_OK != caResult)
725 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
726 SetResult(otmCtx, caResult);
727 return OC_STACK_DELETE_TRANSACTION;
731 * in case of random PIN based OxM,
732 * revert get_psk_info callback of tinyDTLS to use owner credential.
734 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
736 OicUuid_t emptyUuid = { .id={0}};
737 SetUuidForRandomPinOxm(&emptyUuid);
739 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
741 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
742 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
743 return OC_STACK_DELETE_TRANSACTION;
747 //PUT /oic/sec/doxm [{ ..., "owned":"TRUE" }]
748 res = PutOwnershipInformation(otmCtx);
749 if(OC_STACK_OK != res)
751 OIC_LOG(ERROR, TAG, "Failed to put ownership information to new device");
752 SetResult(otmCtx, res);
753 return OC_STACK_DELETE_TRANSACTION;
759 res = clientResponse->result;
760 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
761 SetResult(otmCtx, res);
764 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
767 return OC_STACK_DELETE_TRANSACTION;
772 * Response handler for update owner information request.
774 * @param[in] ctx ctx value passed to callback from calling function.
775 * @param[in] UNUSED handle to an invocation
776 * @param[in] clientResponse Response from queries to remote servers.
777 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
778 * and OC_STACK_KEEP_TRANSACTION to keep it.
780 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
781 OCClientResponse *clientResponse)
783 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
784 VERIFY_NON_NULL(TAG, ctx, WARNING);
786 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
788 OCStackResult res = OC_STACK_OK;
789 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
791 if(OC_STACK_OK == clientResponse->result)
793 if(otmCtx && otmCtx->selectedDeviceInfo)
795 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
796 OIC_LOG(INFO, TAG, "Set Ready for provisioning state .");
798 res = PutProvisioningStatus(otmCtx);
799 if(OC_STACK_OK != res)
801 OIC_LOG(ERROR, TAG, "Failed to update pstat");
802 SetResult(otmCtx, res);
808 res = clientResponse->result;
809 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
810 SetResult(otmCtx, res);
813 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
816 return OC_STACK_DELETE_TRANSACTION;
820 * Response handler of update provisioning status.
822 * @param[in] ctx ctx value passed to callback from calling function.
823 * @param[in] UNUSED handle to an invocation
824 * @param[in] clientResponse Response from queries to remote servers.
825 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
826 * and OC_STACK_KEEP_TRANSACTION to keep it.
828 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
829 OCClientResponse *clientResponse)
831 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
833 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
834 VERIFY_NON_NULL(TAG, ctx, ERROR);
836 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
838 OCStackResult res = OC_STACK_OK;
840 if(OC_STACK_OK == clientResponse->result)
842 if(otmCtx && otmCtx->selectedDeviceInfo)
844 OIC_LOG(INFO, TAG, "Device state is in Ready for Provisionig.");
846 res = PutNormalOperationStatus(otmCtx);
847 if(OC_STACK_OK != res)
849 OIC_LOG(ERROR, TAG, "Failed to update pstat");
850 SetResult(otmCtx, res);
856 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
857 clientResponse->result);
858 SetResult(otmCtx, clientResponse->result);
862 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
863 return OC_STACK_DELETE_TRANSACTION;
867 * Response handler of update provisioning status to Ready for Normal..
869 * @param[in] ctx ctx value passed to callback from calling function.
870 * @param[in] UNUSED handle to an invocation
871 * @param[in] clientResponse Response from queries to remote servers.
872 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
873 * and OC_STACK_KEEP_TRANSACTION to keep it.
875 static OCStackApplicationResult ReadyForNomalStatusHandler(void *ctx, OCDoHandle UNUSED,
876 OCClientResponse *clientResponse)
878 OIC_LOG_V(INFO, TAG, "IN ReadyForNomalStatusHandler.");
880 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
881 VERIFY_NON_NULL(TAG, ctx, ERROR);
883 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
886 if (OC_STACK_OK == clientResponse->result)
888 OIC_LOG(INFO, TAG, "Device state is in Ready for Normal Operation.");
889 OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
890 if (OC_STACK_OK == res)
892 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
893 SetResult(otmCtx, OC_STACK_OK);
894 return OC_STACK_DELETE_TRANSACTION;
898 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
903 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
904 clientResponse->result);
905 SetResult(otmCtx, clientResponse->result);
909 OIC_LOG_V(INFO, TAG, "OUT ReadyForNomalStatusHandler.");
910 return OC_STACK_DELETE_TRANSACTION;
913 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx)
915 OIC_LOG(DEBUG, TAG, "IN PutOwnerCredential");
917 if(!otmCtx || !otmCtx->selectedDeviceInfo)
919 OIC_LOG(ERROR, TAG, "Invalid parameters");
920 return OC_STACK_INVALID_PARAM;
923 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
924 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
926 if(!PMGenerateQuery(true,
927 deviceInfo->endpoint.addr, deviceInfo->securePort,
928 deviceInfo->connType,
929 query, sizeof(query), OIC_RSRC_CRED_URI))
931 OIC_LOG(ERROR, TAG, "PutOwnerCredential : Failed to generate query");
932 return OC_STACK_ERROR;
934 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
935 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
938 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
939 return OC_STACK_NO_MEMORY;
942 //Generate owner credential for new device
943 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
944 const OicSecCred_t* ownerCredential = GetCredResourceData(&(deviceInfo->doxm->deviceID));
947 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
948 return OC_STACK_NO_RESOURCE;
951 OicUuid_t credSubjectId = {.id={0}};
952 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
954 OicSecCred_t newCredential;
955 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
956 newCredential.next = NULL;
958 //Set subject ID as PT's ID
959 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
961 //Fill private data as empty string
962 newCredential.privateData.data = NULL;
963 newCredential.privateData.len = 0;
965 newCredential.publicData.data = NULL;
966 newCredential.publicData.len = 0;
969 //Send owner credential to new device : PUT /oic/sec/cred [ owner credential ]
970 if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData, &secPayload->payloadSize))
973 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
974 return OC_STACK_ERROR;
976 OIC_LOG(DEBUG, TAG, "Cred Payload:");
977 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
979 OCCallbackData cbData;
980 cbData.cb = &OwnerCredentialHandler;
981 cbData.context = (void *)otmCtx;
983 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
984 &deviceInfo->endpoint, (OCPayload*)secPayload,
985 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
986 if (res != OC_STACK_OK)
988 OIC_LOG(ERROR, TAG, "OCStack resource error");
993 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
994 return OC_STACK_NO_RESOURCE;
997 OIC_LOG(DEBUG, TAG, "OUT PutOwnerCredential");
1002 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx)
1004 OIC_LOG(DEBUG, TAG, "IN PutOwnerTransferModeToResource");
1006 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1008 OIC_LOG(ERROR, TAG, "Invalid parameters");
1009 return OC_STACK_INVALID_PARAM;
1012 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1013 OicSecOxm_t selectedOxm = deviceInfo->doxm->oxmSel;
1014 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1016 if(!PMGenerateQuery(false,
1017 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1018 deviceInfo->connType,
1019 query, sizeof(query), OIC_RSRC_DOXM_URI))
1021 OIC_LOG(ERROR, TAG, "PutOwnerTransferModeToResource : Failed to generate query");
1022 return OC_STACK_ERROR;
1024 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1025 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1028 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1029 return OC_STACK_NO_MEMORY;
1031 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1032 OCStackResult res = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx,
1033 &secPayload->securityData, &secPayload->payloadSize);
1034 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1036 OCPayloadDestroy((OCPayload *)secPayload);
1037 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor");
1038 return OC_STACK_ERROR;
1041 OCCallbackData cbData;
1042 cbData.cb = &OwnerTransferModeHandler;
1043 cbData.context = (void *)otmCtx;
1045 res = OCDoResource(NULL, OC_REST_PUT, query,
1046 &deviceInfo->endpoint, (OCPayload *)secPayload,
1047 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1048 if (res != OC_STACK_OK)
1050 OIC_LOG(ERROR, TAG, "OCStack resource error");
1053 OIC_LOG(DEBUG, TAG, "OUT PutOwnerTransferModeToResource");
1058 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1060 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1062 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1064 OIC_LOG(ERROR, TAG, "Invailed parameters");
1065 return OC_STACK_INVALID_PARAM;
1068 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1069 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1070 if(!PMGenerateQuery(false,
1071 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1072 deviceInfo->connType,
1073 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1075 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1076 return OC_STACK_ERROR;
1078 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1080 OCCallbackData cbData;
1081 cbData.cb = &ListMethodsHandler;
1082 cbData.context = (void *)otmCtx;
1084 OCStackResult res = OCDoResource(NULL, OC_REST_GET, query, NULL, NULL,
1085 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1086 if (res != OC_STACK_OK)
1088 OIC_LOG(ERROR, TAG, "OCStack resource error");
1091 OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1096 static OCStackResult PutOwnerUuid(OTMContext_t* otmCtx)
1098 OIC_LOG(DEBUG, TAG, "IN PutOwnerUuid");
1100 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1102 OIC_LOG(ERROR, TAG, "Invailed parameters");
1103 return OC_STACK_INVALID_PARAM;
1106 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1107 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1108 if(!PMGenerateQuery(true,
1109 deviceInfo->endpoint.addr, deviceInfo->securePort,
1110 deviceInfo->connType,
1111 query, sizeof(query), OIC_RSRC_DOXM_URI))
1113 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1114 return OC_STACK_ERROR;
1116 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1118 //PUT PT's uuid to new device
1119 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1122 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1123 return OC_STACK_NO_MEMORY;
1125 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1126 OCStackResult res = g_OTMDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(
1127 otmCtx, &secPayload->securityData, &secPayload->payloadSize);
1128 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1130 OCPayloadDestroy((OCPayload *)secPayload);
1131 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to cbor.");
1132 return OC_STACK_INVALID_PARAM;
1134 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1136 OCCallbackData cbData;
1137 cbData.cb = &OwnerUuidUpdateHandler;
1138 cbData.context = (void *)otmCtx;
1141 res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload *)secPayload,
1142 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1143 if (res != OC_STACK_OK)
1145 OIC_LOG(ERROR, TAG, "OCStack resource error");
1148 OIC_LOG(DEBUG, TAG, "OUT PutOwnerUuid");
1153 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx)
1155 OIC_LOG(DEBUG, TAG, "IN PutOwnershipInformation");
1157 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1159 OIC_LOG(ERROR, TAG, "Invailed parameters");
1160 return OC_STACK_INVALID_PARAM;
1163 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1164 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1165 if(!PMGenerateQuery(true,
1166 deviceInfo->endpoint.addr, deviceInfo->securePort,
1167 deviceInfo->connType,
1168 query, sizeof(query), OIC_RSRC_DOXM_URI))
1170 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1171 return OC_STACK_ERROR;
1173 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1175 //OwnershipInformationHandler
1176 OCSecurityPayload *secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1179 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1180 return OC_STACK_NO_MEMORY;
1183 otmCtx->selectedDeviceInfo->doxm->owned = true;
1185 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1186 OCStackResult res = DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm,
1187 &secPayload->securityData, &secPayload->payloadSize);
1188 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1190 OCPayloadDestroy((OCPayload *)secPayload);
1191 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1192 return OC_STACK_INVALID_PARAM;
1195 OCCallbackData cbData;
1196 cbData.cb = &OwnershipInformationHandler;
1197 cbData.context = (void *)otmCtx;
1200 res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1201 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1202 if (res != OC_STACK_OK)
1204 OIC_LOG(ERROR, TAG, "OCStack resource error");
1207 OIC_LOG(DEBUG, TAG, "OUT PutOwnershipInformation");
1212 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx)
1214 OIC_LOG(DEBUG, TAG, "IN PutUpdateOperationMode");
1216 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1218 return OC_STACK_INVALID_PARAM;
1221 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1222 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1223 if(!PMGenerateQuery(false,
1224 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1225 deviceInfo->connType,
1226 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1228 OIC_LOG(ERROR, TAG, "PutUpdateOperationMode : Failed to generate query");
1229 return OC_STACK_ERROR;
1231 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1233 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1236 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1237 return OC_STACK_NO_MEMORY;
1239 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1240 OCStackResult res = PstatToCBORPayload(deviceInfo->pstat, &secPayload->securityData,
1241 &secPayload->payloadSize);
1242 if (OC_STACK_OK != res)
1244 OCPayloadDestroy((OCPayload *)secPayload);
1245 OIC_LOG(ERROR, TAG, "Error while converting pstat to cbor.");
1246 return OC_STACK_INVALID_PARAM;
1249 OCCallbackData cbData;
1250 cbData.cb = &OperationModeUpdateHandler;
1251 cbData.context = (void *)otmCtx;
1253 res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload *)secPayload,
1254 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1255 if (res != OC_STACK_OK)
1257 OIC_LOG(ERROR, TAG, "OCStack resource error");
1260 OIC_LOG(DEBUG, TAG, "OUT PutUpdateOperationMode");
1265 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
1267 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
1268 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1269 otmCtx->selectedDeviceInfo = selectedDevice;
1271 //Set to the lowest level OxM, and then find more higher level OxM.
1272 OCStackResult res = SelectProvisioningMethod(selectedDevice->doxm->oxm,
1273 selectedDevice->doxm->oxmLen,
1274 &selectedDevice->doxm->oxmSel);
1275 if(OC_STACK_OK != res)
1277 OIC_LOG(ERROR, TAG, "Failed to select the provisioning method");
1278 SetResult(otmCtx, res);
1281 OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
1283 //Send Req: PUT /oic/sec/doxm [{..."OxmSel" :g_OTMDatas[Index of Selected OxM].OXMString,...}]
1284 res = PutOwnerTransferModeToResource(otmCtx);
1285 if(OC_STACK_OK != res)
1287 OIC_LOG(WARNING, TAG, "Failed to select the provisioning method");
1288 SetResult(otmCtx, res);
1292 //Register DTLS event handler to catch the dtls event while handshake
1293 if(CA_STATUS_OK != CARegisterDTLSHandshakeCallback(DTLSHandshakeCB))
1295 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register DTLS handshake callback.");
1298 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
1304 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
1306 OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
1310 OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
1311 return OC_STACK_INVALID_PARAM;
1313 if(oxmType >= OIC_OXM_COUNT)
1315 OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
1316 return OC_STACK_INVALID_PARAM;
1319 g_OTMDatas[oxmType].loadSecretCB= data->loadSecretCB;
1320 g_OTMDatas[oxmType].createSecureSessionCB = data->createSecureSessionCB;
1321 g_OTMDatas[oxmType].createSelectOxmPayloadCB = data->createSelectOxmPayloadCB;
1322 g_OTMDatas[oxmType].createOwnerTransferPayloadCB = data->createOwnerTransferPayloadCB;
1324 OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
1330 * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
1332 OCStackResult OTMDoOwnershipTransfer(void* ctx,
1333 OCProvisionDev_t *selectedDevicelist,
1334 OCProvisionResultCB resultCallback)
1336 OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
1338 if (NULL == selectedDevicelist)
1340 return OC_STACK_INVALID_PARAM;
1342 if (NULL == resultCallback)
1344 return OC_STACK_INVALID_CALLBACK;
1347 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
1350 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
1351 return OC_STACK_NO_MEMORY;
1353 otmCtx->ctxResultCallback = resultCallback;
1354 otmCtx->ctxHasError = false;
1355 otmCtx->userCtx = ctx;
1356 OCProvisionDev_t* pCurDev = selectedDevicelist;
1358 //Counting number of selected devices.
1359 otmCtx->ctxResultArraySize = 0;
1360 while(NULL != pCurDev)
1362 otmCtx->ctxResultArraySize++;
1363 pCurDev = pCurDev->next;
1366 otmCtx->ctxResultArray =
1367 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
1368 if(NULL == otmCtx->ctxResultArray)
1370 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
1372 return OC_STACK_NO_MEMORY;
1374 pCurDev = selectedDevicelist;
1376 OCStackResult res = OC_STACK_OK;
1377 //Fill the device UUID for result array.
1378 for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
1380 //Checking duplication of Device ID.
1381 bool isDuplicate = true;
1382 res = PDMIsDuplicateDevice(&pCurDev->doxm->deviceID, &isDuplicate);
1383 if (OC_STACK_OK != res)
1389 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Device ID is duplicated");
1390 res = OC_STACK_INVALID_PARAM;
1393 memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
1394 pCurDev->doxm->deviceID.id,
1396 otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
1397 pCurDev = pCurDev->next;
1400 StartOwnershipTransfer(otmCtx, selectedDevicelist);
1402 OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
1406 OICFree(otmCtx->ctxResultArray);
1411 OCStackResult PutProvisioningStatus(OTMContext_t* otmCtx)
1413 OIC_LOG(INFO, TAG, "IN PutProvisioningStatus");
1415 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1417 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1418 return OC_STACK_INVALID_PARAM;
1421 //Change the TAKE_OWNER bit of CM to 0.
1422 otmCtx->selectedDeviceInfo->pstat->cm &= (~TAKE_OWNER);
1424 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1427 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1428 return OC_STACK_NO_MEMORY;
1430 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1431 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1432 &secPayload->securityData, &secPayload->payloadSize))
1434 OCPayloadDestroy((OCPayload *)secPayload);
1435 return OC_STACK_INVALID_JSON;
1437 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
1438 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1440 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1441 if(!PMGenerateQuery(true,
1442 otmCtx->selectedDeviceInfo->endpoint.addr,
1443 otmCtx->selectedDeviceInfo->securePort,
1444 otmCtx->selectedDeviceInfo->connType,
1445 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1447 OIC_LOG(ERROR, TAG, "PutProvisioningStatus : Failed to generate query");
1448 return OC_STACK_ERROR;
1450 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1452 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1453 cbData.cb = &ProvisioningStatusHandler;
1454 cbData.context = (void*)otmCtx;
1456 OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1457 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1458 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1459 if (ret != OC_STACK_OK)
1461 OIC_LOG(ERROR, TAG, "OCStack resource error");
1464 OIC_LOG(INFO, TAG, "OUT PutProvisioningStatus");
1469 OCStackResult PutNormalOperationStatus(OTMContext_t* otmCtx)
1471 OIC_LOG(INFO, TAG, "IN PutNormalOperationStatus");
1473 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1475 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1476 return OC_STACK_INVALID_PARAM;
1480 otmCtx->selectedDeviceInfo->pstat->isOp = true;
1482 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1485 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1486 return OC_STACK_NO_MEMORY;
1488 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1489 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1490 &secPayload->securityData, &secPayload->payloadSize))
1492 OCPayloadDestroy((OCPayload *)secPayload);
1493 return OC_STACK_INVALID_JSON;
1495 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
1496 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1498 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1499 if(!PMGenerateQuery(true,
1500 otmCtx->selectedDeviceInfo->endpoint.addr,
1501 otmCtx->selectedDeviceInfo->securePort,
1502 otmCtx->selectedDeviceInfo->connType,
1503 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1505 OIC_LOG(ERROR, TAG, "PutNormalOperationStatus : Failed to generate query");
1506 return OC_STACK_ERROR;
1508 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1510 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1511 cbData.cb = &ReadyForNomalStatusHandler;
1512 cbData.context = (void*)otmCtx;
1514 OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1515 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1516 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1517 if (ret != OC_STACK_OK)
1519 OIC_LOG(ERROR, TAG, "OCStack resource error");
1522 OIC_LOG(INFO, TAG, "OUT PutNormalOperationStatus");